blob: 2d14111585706b10141f8fb7e07254fb39f559da [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * cli.c: command line interface
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#include <vlib/vlib.h>
Damjan Marioneccf5092016-11-18 16:59:24 +010041#include <vppinfra/cpu.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
43/* Root of all show commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040044/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070045VLIB_CLI_COMMAND (vlib_cli_show_command, static) = {
46 .path = "show",
47 .short_help = "Show commands",
48};
Dave Barach9b8ffd92016-07-08 08:13:45 -040049/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
51/* Root of all clear commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040052/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070053VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = {
54 .path = "clear",
55 .short_help = "Clear commands",
56};
Dave Barach9b8ffd92016-07-08 08:13:45 -040057/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
59/* Root of all set commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040060/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070061VLIB_CLI_COMMAND (vlib_cli_set_command, static) = {
62 .path = "set",
63 .short_help = "Set commands",
64};
Dave Barach9b8ffd92016-07-08 08:13:45 -040065/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
67/* Root of all test commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040068/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070069VLIB_CLI_COMMAND (vlib_cli_test_command, static) = {
70 .path = "test",
71 .short_help = "Test commands",
72};
Dave Barach9b8ffd92016-07-08 08:13:45 -040073/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75/* Returns bitmap of commands which match key. */
76static uword *
77vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input)
78{
79 int i, n;
Dave Barach9b8ffd92016-07-08 08:13:45 -040080 uword *match = 0;
81 vlib_cli_parse_position_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 unformat_skip_white_space (input);
84
Dave Barach9b8ffd92016-07-08 08:13:45 -040085 for (i = 0;; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 {
87 uword k;
88
89 k = unformat_get_input (input);
90 switch (k)
91 {
92 case 'a' ... 'z':
93 case 'A' ... 'Z':
94 case '0' ... '9':
Dave Barach9b8ffd92016-07-08 08:13:45 -040095 case '-':
96 case '_':
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 break;
98
Dave Barach9b8ffd92016-07-08 08:13:45 -040099 case ' ':
100 case '\t':
101 case '\r':
102 case '\n':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 case UNFORMAT_END_OF_INPUT:
104 /* White space or end of input removes any non-white
105 matches that were before possible. */
106 if (i < vec_len (c->sub_command_positions)
107 && clib_bitmap_count_set_bits (match) > 1)
108 {
109 p = vec_elt_at_index (c->sub_command_positions, i);
110 for (n = 0; n < vec_len (p->bitmaps); n++)
111 match = clib_bitmap_andnot (match, p->bitmaps[n]);
112 }
113 goto done;
114
115 default:
116 unformat_put_input (input);
117 goto done;
118 }
119
120 if (i >= vec_len (c->sub_command_positions))
121 {
122 no_match:
123 clib_bitmap_free (match);
124 return 0;
125 }
126
127 p = vec_elt_at_index (c->sub_command_positions, i);
128 if (vec_len (p->bitmaps) == 0)
129 goto no_match;
130
131 n = k - p->min_char;
132 if (n < 0 || n >= vec_len (p->bitmaps))
133 goto no_match;
134
135 if (i == 0)
136 match = clib_bitmap_dup (p->bitmaps[n]);
137 else
138 match = clib_bitmap_and (match, p->bitmaps[n]);
139
140 if (clib_bitmap_is_zero (match))
141 goto no_match;
142 }
143
Dave Barach9b8ffd92016-07-08 08:13:45 -0400144done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 return match;
146}
147
148/* Looks for string based sub-input formatted { SUB-INPUT }. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149uword
150unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 unformat_input_t *sub_input = va_arg (*args, unformat_input_t *);
153 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 uword c;
155
156 while (1)
157 {
158 c = unformat_get_input (i);
159 switch (c)
160 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 case ' ':
162 case '\t':
163 case '\n':
164 case '\r':
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 case '\f':
166 break;
167
168 case '{':
169 default:
170 /* Put back paren. */
171 if (c != UNFORMAT_END_OF_INPUT)
172 unformat_put_input (i);
173
174 if (c == '{' && unformat (i, "%v", &s))
175 {
176 unformat_init_vector (sub_input, s);
177 return 1;
178 }
179 return 0;
180 }
181 }
182 return 0;
183}
184
185static vlib_cli_command_t *
186get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si)
187{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400188 vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 return vec_elt_at_index (cm->commands, s->index);
190}
191
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192static uword
193unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
196 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
197 vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **);
198 vlib_cli_main_t *cm = &vm->cli_main;
199 uword *match_bitmap, is_unique, index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
201 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202 vlib_cli_sub_rule_t *sr;
203 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 vec_foreach (sr, c->sub_rules)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400205 {
206 void **d;
207 r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
208 vec_add2 (cm->parse_rule_data, d, 1);
209 vec_reset_length (d[0]);
210 if (r->data_size)
211 d[0] = _vec_resize (d[0],
212 /* length increment */ 1,
213 r->data_size,
214 /* header_bytes */ 0,
215 /* data align */ sizeof (uword));
216 if (unformat_user (i, r->unformat_function, vm, d[0]))
217 {
218 *result = vec_elt_at_index (cm->commands, sr->command_index);
219 return 1;
220 }
221 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 }
223
224 match_bitmap = vlib_cli_sub_command_match (c, i);
225 is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
226 index = ~0;
227 if (is_unique)
228 {
229 index = clib_bitmap_first_set (match_bitmap);
230 *result = get_sub_command (cm, c, index);
231 }
232 clib_bitmap_free (match_bitmap);
233
234 return is_unique;
235}
236
Dave Barach9b8ffd92016-07-08 08:13:45 -0400237static u8 *
238format_vlib_cli_command_help (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400240 vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 int is_long = va_arg (*args, int);
242 if (is_long && c->long_help)
243 s = format (s, "%s", c->long_help);
244 else if (c->short_help)
245 s = format (s, "%s", c->short_help);
246 else
247 s = format (s, "%v commands", c->path);
248 return s;
249}
250
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251static u8 *
252format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400254 vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 return format (s, "<%U>", format_c_identifier, r->name);
256}
257
Dave Barach9b8ffd92016-07-08 08:13:45 -0400258static u8 *
259format_vlib_cli_path (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400261 u8 *path = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 int i, in_rule;
263 in_rule = 0;
264 for (i = 0; i < vec_len (path); i++)
265 {
266 switch (path[i])
267 {
268 case '%':
269 in_rule = 1;
270 vec_add1 (s, '<'); /* start of <RULE> */
271 break;
272
273 case '_':
274 /* _ -> space in rules. */
275 vec_add1 (s, in_rule ? ' ' : '_');
276 break;
277
278 case ' ':
279 if (in_rule)
280 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400281 vec_add1 (s, '>'); /* end of <RULE> */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282 in_rule = 0;
283 }
284 vec_add1 (s, ' ');
285 break;
286
287 default:
288 vec_add1 (s, path[i]);
289 break;
290 }
291 }
292
293 if (in_rule)
294 vec_add1 (s, '>'); /* terminate <RULE> */
295
296 return s;
297}
298
299static vlib_cli_command_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400300all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400302 vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
303 vlib_cli_sub_command_t *sc;
304 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
306 if (c->function)
307 vec_add1 (subs, c[0]);
308
309 vec_foreach (sr, c->sub_rules)
310 subs = all_subs (cm, subs, sr->command_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400311 vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
313 return subs;
314}
315
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500316static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317vlib_cli_cmp_rule (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500318{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400319 vlib_cli_sub_rule_t *r1 = a1;
320 vlib_cli_sub_rule_t *r2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500321
322 return vec_cmp (r1->name, r2->name);
323}
324
325static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400326vlib_cli_cmp_command (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500327{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328 vlib_cli_command_t *c1 = a1;
329 vlib_cli_command_t *c2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500330
331 return vec_cmp (c1->path, c2->path);
332}
333
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334static clib_error_t *
335vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
336 vlib_cli_main_t * cm,
337 unformat_input_t * input,
338 uword parent_command_index)
339{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400340 vlib_cli_command_t *parent, *c;
341 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343 u8 *string;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344 uword is_main_dispatch = cm == &vm->cli_main;
345
346 parent = vec_elt_at_index (cm->commands, parent_command_index);
347 if (is_main_dispatch && unformat (input, "help"))
348 {
349 uword help_at_end_of_line, i;
350
Dave Barach9b8ffd92016-07-08 08:13:45 -0400351 help_at_end_of_line =
352 unformat_check_input (input) == UNFORMAT_END_OF_INPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 while (1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 c = parent;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356 if (unformat_user
357 (input, unformat_vlib_cli_sub_command, vm, c, &parent))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 ;
359
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 goto unknown;
362
363 else
364 break;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400365 }
366
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 /* help SUB-COMMAND => long format help.
Dave Barach9b8ffd92016-07-08 08:13:45 -0400368 "help" at end of line: show all commands. */
369 if (!help_at_end_of_line)
370 vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
371 /* is_long */ 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372
373 else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
374 vlib_cli_output (vm, "%v: no sub-commands", c->path);
375
376 else
377 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400378 vlib_cli_sub_command_t *sc;
379 vlib_cli_sub_rule_t *sr, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
381 subs = vec_dup (c->sub_rules);
382
383 /* Add in rules if any. */
384 vec_foreach (sc, c->sub_commands)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400385 {
386 vec_add2 (subs, sr, 1);
387 sr->name = sc->name;
388 sr->command_index = sc->index;
389 sr->rule_index = ~0;
390 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500392 vec_sort_with_function (subs, vlib_cli_cmp_rule);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393
Dave Barach9b8ffd92016-07-08 08:13:45 -0400394 for (i = 0; i < vec_len (subs); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400396 vlib_cli_command_t *d;
397 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
399 d = vec_elt_at_index (cm->commands, subs[i].command_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400400 r =
401 subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
402 subs
403 [i].rule_index) :
404 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
406 if (r)
407 vlib_cli_output
408 (vm, " %-30U %U",
409 format_vlib_cli_parse_rule_name, r,
410 format_vlib_cli_command_help, d, /* is_long */ 0);
411 else
412 vlib_cli_output
413 (vm, " %-30v %U",
414 subs[i].name,
415 format_vlib_cli_command_help, d, /* is_long */ 0);
416 }
417
418 vec_free (subs);
419 }
420 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400421
422 else if (is_main_dispatch
423 && (unformat (input, "choices") || unformat (input, "?")))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400425 vlib_cli_command_t *sub, *subs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
427 subs = all_subs (cm, 0, parent_command_index);
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500428 vec_sort_with_function (subs, vlib_cli_cmp_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 vec_foreach (sub, subs)
430 vlib_cli_output (vm, " %-40U %U",
431 format_vlib_cli_path, sub->path,
432 format_vlib_cli_command_help, sub, /* is_long */ 0);
433 vec_free (subs);
434 }
435
436 else if (unformat (input, "comment %v", &string))
437 {
438 vec_free (string);
439 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441 else if (unformat (input, "uncomment %U",
442 unformat_vlib_cli_sub_input, &sub_input))
443 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 error =
445 vlib_cli_dispatch_sub_commands (vm, cm, &sub_input,
446 parent_command_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 unformat_free (&sub_input);
448 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400449
450 else
451 if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400453 unformat_input_t *si;
454 uword has_sub_commands =
455 vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0;
456
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 si = input;
458 if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input))
459 si = &sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 if (has_sub_commands)
462 error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands);
463
Dave Barach9b8ffd92016-07-08 08:13:45 -0400464 if (has_sub_commands && !error)
465 /* Found valid sub-command. */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
467 else if (c->function)
468 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400469 clib_error_t *c_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470
471 /* Skip white space for benefit of called function. */
472 unformat_skip_white_space (si);
473
474 if (unformat (si, "?"))
475 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476 vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */
477 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 }
479 else
480 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400481 if (!c->is_mp_safe)
482 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483
484 c_error = c->function (vm, si, c);
485
Dave Barach9b8ffd92016-07-08 08:13:45 -0400486 if (!c->is_mp_safe)
487 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
489 if (c_error)
490 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400491 error =
492 clib_error_return (0, "%v: %v", c->path, c_error->what);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 clib_error_free (c_error);
494 /* Free sub input. */
495 if (si != input)
496 unformat_free (si);
497
498 return error;
499 }
500 }
501
502 /* Free any previous error. */
503 clib_error_free (error);
504 }
505
Dave Barach9b8ffd92016-07-08 08:13:45 -0400506 else if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 error = clib_error_return (0, "%v: no sub-commands", c->path);
508
509 /* Free sub input. */
510 if (si != input)
511 unformat_free (si);
512 }
513
514 else
515 goto unknown;
516
517 return error;
518
Dave Barach9b8ffd92016-07-08 08:13:45 -0400519unknown:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 if (parent->path)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400521 return clib_error_return (0, "%v: unknown input `%U'", parent->path,
522 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400524 return clib_error_return (0, "unknown input `%U'", format_unformat_error,
525 input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526}
527
528
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529void vlib_unix_error_report (vlib_main_t *, clib_error_t *)
530 __attribute__ ((weak));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Dave Barach9b8ffd92016-07-08 08:13:45 -0400532void
533vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error)
534{
535}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
537/* Process CLI input. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400538void
539vlib_cli_input (vlib_main_t * vm,
540 unformat_input_t * input,
541 vlib_cli_output_function_t * function, uword function_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400543 vlib_process_t *cp = vlib_get_current_process (vm);
544 vlib_cli_main_t *cm = &vm->cli_main;
545 clib_error_t *error;
546 vlib_cli_output_function_t *save_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 uword save_function_arg;
548
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000549 save_function = cp->output_function;
550 save_function_arg = cp->output_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000552 cp->output_function = function;
553 cp->output_function_arg = function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Dave Barach9b8ffd92016-07-08 08:13:45 -0400555 do
556 {
557 vec_reset_length (cm->parse_rule_data);
558 error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */
559 0);
560 }
561 while (!error && !unformat (input, "%U", unformat_eof));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
563 if (error)
564 {
565 vlib_cli_output (vm, "%v", error->what);
566 vlib_unix_error_report (vm, error);
567 clib_error_free (error);
568 }
569
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000570 cp->output_function = save_function;
571 cp->output_function_arg = save_function_arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572}
573
574/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400575void
576vlib_cli_output (vlib_main_t * vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400578 vlib_process_t *cp = vlib_get_current_process (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579 va_list va;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580 u8 *s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
582 va_start (va, fmt);
583 s = va_format (0, fmt, &va);
584 va_end (va);
585
586 /* Terminate with \n if not present. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400587 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588 vec_add1 (s, '\n');
589
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590 if ((!cp) || (!cp->output_function))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 fformat (stdout, "%v", s);
592 else
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000593 cp->output_function (cp->output_function_arg, s, vec_len (s));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594
595 vec_free (s);
596}
597
598static clib_error_t *
599show_memory_usage (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601{
602 int verbose = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400603 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604 u32 index = 0;
605
Dave Barach9b8ffd92016-07-08 08:13:45 -0400606 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400608 if (unformat (input, "verbose"))
609 verbose = 1;
610 else
611 {
612 error = clib_error_return (0, "unknown input `%U'",
613 format_unformat_error, input);
614 return error;
615 }
616 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
Dave Barach9b8ffd92016-07-08 08:13:45 -0400618 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619 foreach_vlib_main (
620 ({
621 vlib_cli_output (vm, "Thread %d %v\n", index, vlib_worker_threads[index].name);
622 vlib_cli_output (vm, "%U\n", format_mheap, clib_per_cpu_mheaps[index], verbose);
623 index++;
624 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400625 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626 return 0;
627}
628
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630VLIB_CLI_COMMAND (show_memory_usage_command, static) = {
631 .path = "show memory",
632 .short_help = "Show current memory usage",
633 .function = show_memory_usage,
634};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
637static clib_error_t *
Damjan Marioneccf5092016-11-18 16:59:24 +0100638show_cpu (vlib_main_t * vm, unformat_input_t * input,
639 vlib_cli_command_t * cmd)
640{
641#define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c);
642 _("Model name", "%U", format_cpu_model_name);
643 _("Microarchitecture", "%U", format_cpu_uarch);
644 _("Flags", "%U", format_cpu_flags);
645 _("Base frequency", "%.2f GHz",
646 ((f64) vm->clib_time.clocks_per_second) * 1e-9);
647#undef _
648 return 0;
649}
650
651/*?
652 * Displays various information about the CPU.
653 *
654 * @cliexpar
655 * @cliexstart{show cpu}
656 * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
657 * Microarchitecture: Broadwell (Broadwell-EP/EX)
658 * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes
659 * Base Frequency: 3.20 GHz
660 * @cliexend
661?*/
662/* *INDENT-OFF* */
663VLIB_CLI_COMMAND (show_cpu_command, static) = {
664 .path = "show cpu",
665 .short_help = "Show cpu information",
666 .function = show_cpu,
667};
668
669/* *INDENT-ON* */
670static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671enable_disable_memory_trace (vlib_main_t * vm,
672 unformat_input_t * input,
673 vlib_cli_command_t * cmd)
674{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400675 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 int enable;
677
Dave Barach9b8ffd92016-07-08 08:13:45 -0400678 if (!unformat_user (input, unformat_vlib_enable_disable, &enable))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679 {
680 error = clib_error_return (0, "expecting enable/on or disable/off");
681 goto done;
682 }
683
684 clib_mem_trace (enable);
685
Dave Barach9b8ffd92016-07-08 08:13:45 -0400686done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 return error;
688}
689
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = {
692 .path = "memory-trace",
693 .short_help = "Enable/disable memory allocation trace",
694 .function = enable_disable_memory_trace,
695};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400696/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697
698
699static clib_error_t *
700test_heap_validate (vlib_main_t * vm, unformat_input_t * input,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400703 clib_error_t *error = 0;
704 void *heap;
705 mheap_t *mheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706
Dave Barach9b8ffd92016-07-08 08:13:45 -0400707 if (unformat (input, "on"))
708 {
709 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 foreach_vlib_main({
711 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
712 mheap = mheap_header(heap);
713 mheap->flags |= MHEAP_FLAG_VALIDATE;
714 // Turn off small object cache because it delays detection of errors
715 mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE;
716 });
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 }
720 else if (unformat (input, "off"))
721 {
722 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 foreach_vlib_main({
724 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
725 mheap = mheap_header(heap);
726 mheap->flags &= ~MHEAP_FLAG_VALIDATE;
727 mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE;
728 });
Dave Barach9b8ffd92016-07-08 08:13:45 -0400729 /* *INDENT-ON* */
730 }
731 else if (unformat (input, "now"))
732 {
733 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700734 foreach_vlib_main({
735 heap = clib_per_cpu_mheaps[this_vlib_main->cpu_index];
736 mheap = mheap_header(heap);
737 mheap_validate(heap);
738 });
Dave Barach9b8ffd92016-07-08 08:13:45 -0400739 /* *INDENT-ON* */
740 vlib_cli_output (vm, "heap validation complete");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741
Dave Barach9b8ffd92016-07-08 08:13:45 -0400742 }
743 else
744 {
745 return clib_error_return (0, "unknown input `%U'",
746 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 }
748
Dave Barach9b8ffd92016-07-08 08:13:45 -0400749 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750}
751
Dave Barach9b8ffd92016-07-08 08:13:45 -0400752/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = {
754 .path = "test heap-validate",
755 .short_help = "<on/off/now> validate heap on future allocs/frees or right now",
756 .function = test_heap_validate,
757};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400758/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000760#ifdef TEST_CODE
761/*
762 * A trivial test harness to verify the per-process output_function
763 * is working correcty.
764 */
765
766static clib_error_t *
767sleep_ten_seconds (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400768 unformat_input_t * input, vlib_cli_command_t * cmd)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000769{
770 u16 i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400771 u16 my_id = rand ();
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000772
Dave Barach9b8ffd92016-07-08 08:13:45 -0400773 vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000774
Dave Barach9b8ffd92016-07-08 08:13:45 -0400775 for (i = 0; i < 10; i++)
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000776 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400777 vlib_process_wait_for_event_or_clock (vm, 1.0);
778 vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000779 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400780 vlib_cli_output (vm, "Done with sleep with id %u\n", my_id);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000781 return 0;
782}
783
Dave Barach9b8ffd92016-07-08 08:13:45 -0400784/* *INDENT-OFF* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000785VLIB_CLI_COMMAND (ping_command, static) = {
786 .path = "test sleep",
787 .function = sleep_ten_seconds,
788 .short_help = "Sleep for 10 seconds",
789};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400790/* *INDENT-ON* */
Andrew Yourtchenko716d9592016-05-10 10:51:34 +0000791#endif /* ifdef TEST_CODE */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792
Dave Barach9b8ffd92016-07-08 08:13:45 -0400793static uword
794vlib_cli_normalize_path (char *input, char **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 char *i = input;
797 char *s = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798 uword l = 0;
799 uword index_of_last_space = ~0;
800
801 while (*i != 0)
802 {
803 u8 c = *i++;
804 /* Multiple white space -> single space. */
805 switch (c)
806 {
807 case ' ':
808 case '\t':
809 case '\n':
810 case '\r':
Dave Barach9b8ffd92016-07-08 08:13:45 -0400811 if (l > 0 && s[l - 1] != ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812 {
813 vec_add1 (s, ' ');
814 l++;
815 }
816 break;
817
818 default:
Dave Barach9b8ffd92016-07-08 08:13:45 -0400819 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820 index_of_last_space = vec_len (s);
821 vec_add1 (s, c);
822 l++;
823 break;
824 }
825 }
826
827 /* Remove any extra space at end. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400828 if (l > 0 && s[l - 1] == ' ')
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 _vec_len (s) -= 1;
830
831 *result = s;
832 return index_of_last_space;
833}
834
835always_inline uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836parent_path_len (char *path)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837{
838 word i;
839 for (i = vec_len (path) - 1; i >= 0; i--)
840 {
841 if (path[i] == ' ')
842 return i;
843 }
844 return ~0;
845}
846
Dave Barach9b8ffd92016-07-08 08:13:45 -0400847static void
848add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 vlib_cli_command_t *p, *c;
851 vlib_cli_sub_command_t *sub_c;
852 u8 *sub_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853 word i, l;
854
855 p = vec_elt_at_index (cm->commands, parent_index);
856 c = vec_elt_at_index (cm->commands, child_index);
857
858 l = parent_path_len (c->path);
859 if (l == ~0)
860 sub_name = vec_dup ((u8 *) c->path);
861 else
862 {
863 ASSERT (l + 1 < vec_len (c->path));
864 sub_name = 0;
865 vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1));
866 }
867
868 if (sub_name[0] == '%')
869 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400870 uword *q;
871 vlib_cli_sub_rule_t *sr;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872
873 /* Remove %. */
874 vec_delete (sub_name, 1, 0);
875
Dave Barach9b8ffd92016-07-08 08:13:45 -0400876 if (!p->sub_rule_index_by_name)
877 p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
878 sizeof (sub_name[0]),
879 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 q = hash_get_mem (p->sub_rule_index_by_name, sub_name);
881 if (q)
882 {
883 sr = vec_elt_at_index (p->sub_rules, q[0]);
884 ASSERT (sr->command_index == child_index);
885 return;
886 }
887
888 q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400889 if (!q)
Ed Warnicke853e7202016-08-12 11:42:26 -0700890 {
891 clib_error ("reference to unknown rule `%%%v' in path `%v'",
892 sub_name, c->path);
893 return;
894 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895
Dave Barach9b8ffd92016-07-08 08:13:45 -0400896 hash_set_mem (p->sub_rule_index_by_name, sub_name,
897 vec_len (p->sub_rules));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 vec_add2 (p->sub_rules, sr, 1);
899 sr->name = sub_name;
900 sr->rule_index = q[0];
901 sr->command_index = child_index;
902 return;
903 }
904
Dave Barach9b8ffd92016-07-08 08:13:45 -0400905 if (!p->sub_command_index_by_name)
906 p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32,
907 sizeof (c->path[0]),
908 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909
910 /* Check if sub-command has already been created. */
911 if (hash_get_mem (p->sub_command_index_by_name, sub_name))
912 {
913 vec_free (sub_name);
914 return;
915 }
916
917 vec_add2 (p->sub_commands, sub_c, 1);
918 sub_c->index = child_index;
919 sub_c->name = sub_name;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400920 hash_set_mem (p->sub_command_index_by_name, sub_c->name,
921 sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922
923 vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1);
924 for (i = 0; i < vec_len (sub_c->name); i++)
925 {
926 int n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400927 vlib_cli_parse_position_t *pos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928
929 pos = vec_elt_at_index (p->sub_command_positions, i);
930
Dave Barach9b8ffd92016-07-08 08:13:45 -0400931 if (!pos->bitmaps)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 pos->min_char = sub_c->name[i];
933
934 n = sub_c->name[i] - pos->min_char;
935 if (n < 0)
936 {
937 pos->min_char = sub_c->name[i];
938 vec_insert (pos->bitmaps, -n, 0);
939 n = 0;
940 }
941
942 vec_validate (pos->bitmaps, n);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400943 pos->bitmaps[n] =
944 clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945 }
946}
947
948static void
949vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci)
950{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400951 uword p_len, pi, *p;
952 char *p_path;
953 vlib_cli_command_t *c, *parent;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
955 /* Root command (index 0) should have already been added. */
956 ASSERT (vec_len (cm->commands) > 0);
957
958 c = vec_elt_at_index (cm->commands, ci);
959 p_len = parent_path_len (c->path);
960
Dave Barach9b8ffd92016-07-08 08:13:45 -0400961 /* No space? Parent is root command. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962 if (p_len == ~0)
963 {
964 add_sub_command (cm, 0, ci);
965 return;
966 }
967
968 p_path = 0;
969 vec_add (p_path, c->path, p_len);
970
971 p = hash_get_mem (cm->command_index_by_path, p_path);
972
973 /* Parent exists? */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400974 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 {
976 /* Parent does not exist; create it. */
977 vec_add2 (cm->commands, parent, 1);
978 parent->path = p_path;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400979 hash_set_mem (cm->command_index_by_path, parent->path,
980 parent - cm->commands);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981 pi = parent - cm->commands;
982 }
983 else
984 {
985 pi = p[0];
986 vec_free (p_path);
987 }
988
989 add_sub_command (cm, pi, ci);
990
991 /* Create parent's parent. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400992 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993 vlib_cli_make_parent (cm, pi);
994}
995
996always_inline uword
997vlib_cli_command_is_empty (vlib_cli_command_t * c)
998{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400999 return (c->long_help == 0 && c->short_help == 0 && c->function == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000}
1001
Dave Barach9b8ffd92016-07-08 08:13:45 -04001002clib_error_t *
1003vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001005 vlib_cli_main_t *cm = &vm->cli_main;
1006 clib_error_t *error = 0;
1007 uword ci, *p;
1008 char *normalized_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009
1010 if ((error = vlib_call_init_function (vm, vlib_cli_init)))
1011 return error;
1012
1013 (void) vlib_cli_normalize_path (c->path, &normalized_path);
1014
Dave Barach9b8ffd92016-07-08 08:13:45 -04001015 if (!cm->command_index_by_path)
1016 cm->command_index_by_path = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 sizeof (c->path[0]),
1018 sizeof (uword));
1019
1020 /* See if command already exists with given path. */
1021 p = hash_get_mem (cm->command_index_by_path, normalized_path);
1022 if (p)
1023 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001024 vlib_cli_command_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025
1026 ci = p[0];
1027 d = vec_elt_at_index (cm->commands, ci);
1028
1029 /* If existing command was created via vlib_cli_make_parent
Dave Barach9b8ffd92016-07-08 08:13:45 -04001030 replaced it with callers data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031 if (vlib_cli_command_is_empty (d))
1032 {
1033 vlib_cli_command_t save = d[0];
1034
Dave Barach9b8ffd92016-07-08 08:13:45 -04001035 ASSERT (!vlib_cli_command_is_empty (c));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
1037 /* Copy callers fields. */
1038 d[0] = c[0];
1039
1040 /* Save internal fields. */
1041 d->path = save.path;
1042 d->sub_commands = save.sub_commands;
1043 d->sub_command_index_by_name = save.sub_command_index_by_name;
1044 d->sub_command_positions = save.sub_command_positions;
1045 d->sub_rules = save.sub_rules;
1046 }
1047 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001048 error =
1049 clib_error_return (0, "duplicate command name with path %v",
1050 normalized_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051
1052 vec_free (normalized_path);
1053 if (error)
1054 return error;
1055 }
1056 else
1057 {
1058 /* Command does not exist: create it. */
1059
1060 /* Add root command (index 0). */
1061 if (vec_len (cm->commands) == 0)
1062 {
1063 /* Create command with index 0; path is empty string. */
1064 vec_resize (cm->commands, 1);
1065 }
1066
1067 ci = vec_len (cm->commands);
1068 hash_set_mem (cm->command_index_by_path, normalized_path, ci);
1069 vec_add1 (cm->commands, c[0]);
1070
1071 c = vec_elt_at_index (cm->commands, ci);
1072 c->path = normalized_path;
1073
1074 /* Don't inherit from registration. */
1075 c->sub_commands = 0;
1076 c->sub_command_index_by_name = 0;
1077 c->sub_command_positions = 0;
1078 }
1079
1080 vlib_cli_make_parent (cm, ci);
1081 return 0;
1082}
1083
1084clib_error_t *
1085vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
1086{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001087 vlib_cli_main_t *cm = &vm->cli_main;
1088 vlib_cli_parse_rule_t *r;
1089 clib_error_t *error = 0;
1090 u8 *r_name;
1091 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092
Dave Barach9b8ffd92016-07-08 08:13:45 -04001093 if (!cm->parse_rule_index_by_name)
1094 cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 sizeof (r->name[0]),
1096 sizeof (uword));
1097
1098 /* Make vector copy of name. */
1099 r_name = format (0, "%s", r_reg->name);
1100
1101 if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name)))
1102 {
1103 vec_free (r_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001104 return clib_error_return (0, "duplicate parse rule name `%s'",
1105 r_reg->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106 }
1107
1108 vec_add2 (cm->parse_rules, r, 1);
1109 r[0] = r_reg[0];
1110 r->name = (char *) r_name;
1111 hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules);
1112
1113 return error;
1114}
1115
1116#if 0
1117/* $$$ turn back on again someday, maybe */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001118static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
1119 vlib_cli_parse_rule_t *
1120 lo,
1121 vlib_cli_parse_rule_t *
1122 hi)
1123 __attribute__ ((unused))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001125 clib_error_t *error = 0;
1126 vlib_cli_parse_rule_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127
1128 for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0))
1129 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001130 if (!r->name || strlen (r->name) == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131 {
1132 error = clib_error_return (0, "parse rule with no name");
1133 goto done;
1134 }
1135
1136 error = vlib_cli_register_parse_rule (vm, r);
1137 if (error)
1138 goto done;
1139 }
1140
Dave Barach9b8ffd92016-07-08 08:13:45 -04001141done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 return error;
1143}
1144#endif
1145
Dave Barach9b8ffd92016-07-08 08:13:45 -04001146static clib_error_t *
1147vlib_cli_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001149 vlib_cli_main_t *cm = &vm->cli_main;
1150 clib_error_t *error = 0;
1151 vlib_cli_command_t *cmd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152
1153 cmd = cm->cli_command_registrations;
1154
1155 while (cmd)
1156 {
1157 error = vlib_cli_register (vm, cmd);
1158 if (error)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001159 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160 cmd = cmd->next_cli_command;
1161 }
1162 return error;
1163}
1164
1165VLIB_INIT_FUNCTION (vlib_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001166
1167/*
1168 * fd.io coding-style-patch-verification: ON
1169 *
1170 * Local Variables:
1171 * eval: (c-set-style "gnu")
1172 * End:
1173 */