Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Luke | 6edd360 | 2018-06-12 22:45:06 -0400 | [diff] [blame] | 41 | #include <vlib/unix/unix.h> |
Damjan Marion | eccf509 | 2016-11-18 16:59:24 +0100 | [diff] [blame] | 42 | #include <vppinfra/cpu.h> |
Damjan Marion | e5ef1d7 | 2017-03-02 12:33:48 +0100 | [diff] [blame] | 43 | #include <unistd.h> |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 44 | #include <ctype.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
| 46 | /* Root of all show commands. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 47 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | VLIB_CLI_COMMAND (vlib_cli_show_command, static) = { |
| 49 | .path = "show", |
| 50 | .short_help = "Show commands", |
| 51 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 52 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
| 54 | /* Root of all clear commands. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 55 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | VLIB_CLI_COMMAND (vlib_cli_clear_command, static) = { |
| 57 | .path = "clear", |
| 58 | .short_help = "Clear commands", |
| 59 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 60 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | |
| 62 | /* Root of all set commands. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 63 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | VLIB_CLI_COMMAND (vlib_cli_set_command, static) = { |
| 65 | .path = "set", |
| 66 | .short_help = "Set commands", |
| 67 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 68 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | |
| 70 | /* Root of all test commands. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 71 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | VLIB_CLI_COMMAND (vlib_cli_test_command, static) = { |
| 73 | .path = "test", |
| 74 | .short_help = "Test commands", |
| 75 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 76 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
| 78 | /* Returns bitmap of commands which match key. */ |
| 79 | static uword * |
| 80 | vlib_cli_sub_command_match (vlib_cli_command_t * c, unformat_input_t * input) |
| 81 | { |
| 82 | int i, n; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 83 | uword *match = 0; |
| 84 | vlib_cli_parse_position_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
| 86 | unformat_skip_white_space (input); |
| 87 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 88 | for (i = 0;; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | { |
| 90 | uword k; |
| 91 | |
| 92 | k = unformat_get_input (input); |
| 93 | switch (k) |
| 94 | { |
| 95 | case 'a' ... 'z': |
| 96 | case 'A' ... 'Z': |
| 97 | case '0' ... '9': |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 98 | case '-': |
| 99 | case '_': |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | break; |
| 101 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 102 | case ' ': |
| 103 | case '\t': |
| 104 | case '\r': |
| 105 | case '\n': |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | case UNFORMAT_END_OF_INPUT: |
| 107 | /* White space or end of input removes any non-white |
| 108 | matches that were before possible. */ |
| 109 | if (i < vec_len (c->sub_command_positions) |
| 110 | && clib_bitmap_count_set_bits (match) > 1) |
| 111 | { |
| 112 | p = vec_elt_at_index (c->sub_command_positions, i); |
| 113 | for (n = 0; n < vec_len (p->bitmaps); n++) |
| 114 | match = clib_bitmap_andnot (match, p->bitmaps[n]); |
| 115 | } |
| 116 | goto done; |
| 117 | |
| 118 | default: |
| 119 | unformat_put_input (input); |
| 120 | goto done; |
| 121 | } |
| 122 | |
| 123 | if (i >= vec_len (c->sub_command_positions)) |
| 124 | { |
| 125 | no_match: |
| 126 | clib_bitmap_free (match); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | p = vec_elt_at_index (c->sub_command_positions, i); |
| 131 | if (vec_len (p->bitmaps) == 0) |
| 132 | goto no_match; |
| 133 | |
| 134 | n = k - p->min_char; |
| 135 | if (n < 0 || n >= vec_len (p->bitmaps)) |
| 136 | goto no_match; |
| 137 | |
| 138 | if (i == 0) |
| 139 | match = clib_bitmap_dup (p->bitmaps[n]); |
| 140 | else |
| 141 | match = clib_bitmap_and (match, p->bitmaps[n]); |
| 142 | |
| 143 | if (clib_bitmap_is_zero (match)) |
| 144 | goto no_match; |
| 145 | } |
| 146 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 147 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | return match; |
| 149 | } |
| 150 | |
| 151 | /* Looks for string based sub-input formatted { SUB-INPUT }. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 152 | uword |
| 153 | unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 155 | unformat_input_t *sub_input = va_arg (*args, unformat_input_t *); |
| 156 | u8 *s; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | uword c; |
| 158 | |
| 159 | while (1) |
| 160 | { |
| 161 | c = unformat_get_input (i); |
| 162 | switch (c) |
| 163 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 164 | case ' ': |
| 165 | case '\t': |
| 166 | case '\n': |
| 167 | case '\r': |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | case '\f': |
| 169 | break; |
| 170 | |
| 171 | case '{': |
| 172 | default: |
| 173 | /* Put back paren. */ |
| 174 | if (c != UNFORMAT_END_OF_INPUT) |
| 175 | unformat_put_input (i); |
| 176 | |
| 177 | if (c == '{' && unformat (i, "%v", &s)) |
| 178 | { |
| 179 | unformat_init_vector (sub_input, s); |
| 180 | return 1; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static vlib_cli_command_t * |
| 189 | get_sub_command (vlib_cli_main_t * cm, vlib_cli_command_t * parent, u32 si) |
| 190 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 191 | vlib_cli_sub_command_t *s = vec_elt_at_index (parent->sub_commands, si); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | return vec_elt_at_index (cm->commands, s->index); |
| 193 | } |
| 194 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 195 | static uword |
| 196 | unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 198 | vlib_main_t *vm = va_arg (*args, vlib_main_t *); |
| 199 | vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *); |
| 200 | vlib_cli_command_t **result = va_arg (*args, vlib_cli_command_t **); |
| 201 | vlib_cli_main_t *cm = &vm->cli_main; |
| 202 | uword *match_bitmap, is_unique, index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | |
| 204 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 205 | vlib_cli_sub_rule_t *sr; |
| 206 | vlib_cli_parse_rule_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | vec_foreach (sr, c->sub_rules) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 208 | { |
| 209 | void **d; |
| 210 | r = vec_elt_at_index (cm->parse_rules, sr->rule_index); |
| 211 | vec_add2 (cm->parse_rule_data, d, 1); |
| 212 | vec_reset_length (d[0]); |
| 213 | if (r->data_size) |
| 214 | d[0] = _vec_resize (d[0], |
| 215 | /* length increment */ 1, |
| 216 | r->data_size, |
| 217 | /* header_bytes */ 0, |
| 218 | /* data align */ sizeof (uword)); |
| 219 | if (unformat_user (i, r->unformat_function, vm, d[0])) |
| 220 | { |
| 221 | *result = vec_elt_at_index (cm->commands, sr->command_index); |
| 222 | return 1; |
| 223 | } |
| 224 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | match_bitmap = vlib_cli_sub_command_match (c, i); |
| 228 | is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1; |
| 229 | index = ~0; |
| 230 | if (is_unique) |
| 231 | { |
| 232 | index = clib_bitmap_first_set (match_bitmap); |
| 233 | *result = get_sub_command (cm, c, index); |
| 234 | } |
| 235 | clib_bitmap_free (match_bitmap); |
| 236 | |
| 237 | return is_unique; |
| 238 | } |
| 239 | |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 240 | static int |
| 241 | vlib_cli_cmp_strings (void *a1, void *a2) |
| 242 | { |
| 243 | u8 *c1 = *(u8 **) a1; |
| 244 | u8 *c2 = *(u8 **) a2; |
| 245 | |
| 246 | return vec_cmp (c1, c2); |
| 247 | } |
| 248 | |
| 249 | u8 ** |
| 250 | vlib_cli_get_possible_completions (u8 * str) |
| 251 | { |
| 252 | vlib_cli_command_t *c; |
| 253 | vlib_cli_sub_command_t *sc; |
| 254 | vlib_main_t *vm = vlib_get_main (); |
| 255 | vlib_cli_main_t *vcm = &vm->cli_main; |
| 256 | uword *match_bitmap = 0; |
| 257 | uword index, is_unique, help_next_level; |
| 258 | u8 **result = 0; |
| 259 | unformat_input_t input; |
| 260 | unformat_init_vector (&input, vec_dup (str)); |
| 261 | c = vec_elt_at_index (vcm->commands, 0); |
| 262 | |
| 263 | /* remove trailing whitespace, except for one of them */ |
| 264 | while (vec_len (input.buffer) >= 2 && |
| 265 | isspace (input.buffer[vec_len (input.buffer) - 1]) && |
| 266 | isspace (input.buffer[vec_len (input.buffer) - 2])) |
| 267 | { |
| 268 | vec_del1 (input.buffer, vec_len (input.buffer) - 1); |
| 269 | } |
| 270 | |
| 271 | /* if input is empty, directly return list of root commands */ |
| 272 | if (vec_len (input.buffer) == 0 || |
| 273 | (vec_len (input.buffer) == 1 && isspace (input.buffer[0]))) |
| 274 | { |
| 275 | vec_foreach (sc, c->sub_commands) |
| 276 | { |
| 277 | vec_add1 (result, (u8 *) sc->name); |
| 278 | } |
| 279 | goto done; |
| 280 | } |
| 281 | |
| 282 | /* add a trailing '?' so that vlib_cli_sub_command_match can find |
| 283 | * all commands starting with the input string */ |
| 284 | vec_add1 (input.buffer, '?'); |
| 285 | |
| 286 | while (1) |
| 287 | { |
| 288 | match_bitmap = vlib_cli_sub_command_match (c, &input); |
| 289 | /* no match: return no result */ |
| 290 | if (match_bitmap == 0) |
| 291 | { |
| 292 | goto done; |
| 293 | } |
| 294 | is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1; |
| 295 | /* unique match: try to step one subcommand level further */ |
| 296 | if (is_unique) |
| 297 | { |
| 298 | /* stop if no more input */ |
| 299 | if (input.index >= vec_len (input.buffer) - 1) |
| 300 | { |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | index = clib_bitmap_first_set (match_bitmap); |
| 305 | c = get_sub_command (vcm, c, index); |
| 306 | clib_bitmap_free (match_bitmap); |
| 307 | continue; |
| 308 | } |
| 309 | /* multiple matches: stop here, return all matches */ |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | /* remove trailing '?' */ |
| 314 | vec_del1 (input.buffer, vec_len (input.buffer) - 1); |
| 315 | |
| 316 | /* if we have a space at the end of input, and a unique match, |
| 317 | * autocomplete the next level of subcommands */ |
| 318 | help_next_level = (vec_len (str) == 0) || isspace (str[vec_len (str) - 1]); |
| 319 | /* *INDENT-OFF* */ |
| 320 | clib_bitmap_foreach(index, match_bitmap, { |
| 321 | if (help_next_level && is_unique) { |
| 322 | c = get_sub_command (vcm, c, index); |
| 323 | vec_foreach (sc, c->sub_commands) { |
| 324 | vec_add1 (result, (u8*) sc->name); |
| 325 | } |
| 326 | goto done; /* break doesn't work in this macro-loop */ |
| 327 | } |
| 328 | sc = &c->sub_commands[index]; |
| 329 | vec_add1(result, (u8*) sc->name); |
| 330 | }); |
| 331 | /* *INDENT-ON* */ |
| 332 | |
| 333 | done: |
| 334 | clib_bitmap_free (match_bitmap); |
| 335 | unformat_free (&input); |
| 336 | |
Yoann Desmouceaux | 4227eef | 2017-05-24 15:51:48 +0200 | [diff] [blame] | 337 | if (result) |
| 338 | vec_sort_with_function (result, vlib_cli_cmp_strings); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 339 | return result; |
| 340 | } |
| 341 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 342 | static u8 * |
| 343 | format_vlib_cli_command_help (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 345 | vlib_cli_command_t *c = va_arg (*args, vlib_cli_command_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | int is_long = va_arg (*args, int); |
| 347 | if (is_long && c->long_help) |
| 348 | s = format (s, "%s", c->long_help); |
| 349 | else if (c->short_help) |
| 350 | s = format (s, "%s", c->short_help); |
| 351 | else |
| 352 | s = format (s, "%v commands", c->path); |
| 353 | return s; |
| 354 | } |
| 355 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 356 | static u8 * |
| 357 | format_vlib_cli_parse_rule_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 359 | vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | return format (s, "<%U>", format_c_identifier, r->name); |
| 361 | } |
| 362 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 363 | static u8 * |
| 364 | format_vlib_cli_path (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 366 | u8 *path = va_arg (*args, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | int i, in_rule; |
| 368 | in_rule = 0; |
| 369 | for (i = 0; i < vec_len (path); i++) |
| 370 | { |
| 371 | switch (path[i]) |
| 372 | { |
| 373 | case '%': |
| 374 | in_rule = 1; |
| 375 | vec_add1 (s, '<'); /* start of <RULE> */ |
| 376 | break; |
| 377 | |
| 378 | case '_': |
| 379 | /* _ -> space in rules. */ |
| 380 | vec_add1 (s, in_rule ? ' ' : '_'); |
| 381 | break; |
| 382 | |
| 383 | case ' ': |
| 384 | if (in_rule) |
| 385 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 386 | vec_add1 (s, '>'); /* end of <RULE> */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | in_rule = 0; |
| 388 | } |
| 389 | vec_add1 (s, ' '); |
| 390 | break; |
| 391 | |
| 392 | default: |
| 393 | vec_add1 (s, path[i]); |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (in_rule) |
| 399 | vec_add1 (s, '>'); /* terminate <RULE> */ |
| 400 | |
| 401 | return s; |
| 402 | } |
| 403 | |
| 404 | static vlib_cli_command_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 405 | all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 407 | vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index); |
| 408 | vlib_cli_sub_command_t *sc; |
| 409 | vlib_cli_sub_rule_t *sr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | |
| 411 | if (c->function) |
| 412 | vec_add1 (subs, c[0]); |
| 413 | |
| 414 | vec_foreach (sr, c->sub_rules) |
| 415 | subs = all_subs (cm, subs, sr->command_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 416 | vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | |
| 418 | return subs; |
| 419 | } |
| 420 | |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 421 | static int |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 422 | vlib_cli_cmp_rule (void *a1, void *a2) |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 423 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 424 | vlib_cli_sub_rule_t *r1 = a1; |
| 425 | vlib_cli_sub_rule_t *r2 = a2; |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 426 | |
| 427 | return vec_cmp (r1->name, r2->name); |
| 428 | } |
| 429 | |
| 430 | static int |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 431 | vlib_cli_cmp_command (void *a1, void *a2) |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 432 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 433 | vlib_cli_command_t *c1 = a1; |
| 434 | vlib_cli_command_t *c2 = a2; |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 435 | |
| 436 | return vec_cmp (c1->path, c2->path); |
| 437 | } |
| 438 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | static clib_error_t * |
| 440 | vlib_cli_dispatch_sub_commands (vlib_main_t * vm, |
| 441 | vlib_cli_main_t * cm, |
| 442 | unformat_input_t * input, |
| 443 | uword parent_command_index) |
| 444 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 445 | vlib_cli_command_t *parent, *c; |
| 446 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | unformat_input_t sub_input; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 448 | u8 *string; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | uword is_main_dispatch = cm == &vm->cli_main; |
| 450 | |
| 451 | parent = vec_elt_at_index (cm->commands, parent_command_index); |
| 452 | if (is_main_dispatch && unformat (input, "help")) |
| 453 | { |
| 454 | uword help_at_end_of_line, i; |
| 455 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 456 | help_at_end_of_line = |
| 457 | unformat_check_input (input) == UNFORMAT_END_OF_INPUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 458 | while (1) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 459 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | c = parent; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 461 | if (unformat_user |
| 462 | (input, unformat_vlib_cli_sub_command, vm, c, &parent)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | ; |
| 464 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 465 | else if (!(unformat_check_input (input) == UNFORMAT_END_OF_INPUT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | goto unknown; |
| 467 | |
| 468 | else |
| 469 | break; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 470 | } |
| 471 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | /* help SUB-COMMAND => long format help. |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 473 | "help" at end of line: show all commands. */ |
| 474 | if (!help_at_end_of_line) |
| 475 | vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c, |
| 476 | /* is_long */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | |
| 478 | else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0) |
| 479 | vlib_cli_output (vm, "%v: no sub-commands", c->path); |
| 480 | |
| 481 | else |
| 482 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 483 | vlib_cli_sub_command_t *sc; |
| 484 | vlib_cli_sub_rule_t *sr, *subs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | |
| 486 | subs = vec_dup (c->sub_rules); |
| 487 | |
| 488 | /* Add in rules if any. */ |
| 489 | vec_foreach (sc, c->sub_commands) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 490 | { |
| 491 | vec_add2 (subs, sr, 1); |
| 492 | sr->name = sc->name; |
| 493 | sr->command_index = sc->index; |
| 494 | sr->rule_index = ~0; |
| 495 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 496 | |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 497 | vec_sort_with_function (subs, vlib_cli_cmp_rule); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 499 | for (i = 0; i < vec_len (subs); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 501 | vlib_cli_command_t *d; |
| 502 | vlib_cli_parse_rule_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | |
| 504 | d = vec_elt_at_index (cm->commands, subs[i].command_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 505 | r = |
| 506 | subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules, |
| 507 | subs |
| 508 | [i].rule_index) : |
| 509 | 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 510 | |
| 511 | if (r) |
| 512 | vlib_cli_output |
| 513 | (vm, " %-30U %U", |
| 514 | format_vlib_cli_parse_rule_name, r, |
| 515 | format_vlib_cli_command_help, d, /* is_long */ 0); |
| 516 | else |
| 517 | vlib_cli_output |
| 518 | (vm, " %-30v %U", |
| 519 | subs[i].name, |
| 520 | format_vlib_cli_command_help, d, /* is_long */ 0); |
| 521 | } |
| 522 | |
| 523 | vec_free (subs); |
| 524 | } |
| 525 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 526 | |
| 527 | else if (is_main_dispatch |
| 528 | && (unformat (input, "choices") || unformat (input, "?"))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 530 | vlib_cli_command_t *sub, *subs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | |
| 532 | subs = all_subs (cm, 0, parent_command_index); |
Matus Fabian | d2dc3df | 2015-12-14 10:31:33 -0500 | [diff] [blame] | 533 | vec_sort_with_function (subs, vlib_cli_cmp_command); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | vec_foreach (sub, subs) |
| 535 | vlib_cli_output (vm, " %-40U %U", |
| 536 | format_vlib_cli_path, sub->path, |
| 537 | format_vlib_cli_command_help, sub, /* is_long */ 0); |
| 538 | vec_free (subs); |
| 539 | } |
| 540 | |
| 541 | else if (unformat (input, "comment %v", &string)) |
| 542 | { |
| 543 | vec_free (string); |
| 544 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 545 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | else if (unformat (input, "uncomment %U", |
| 547 | unformat_vlib_cli_sub_input, &sub_input)) |
| 548 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 549 | error = |
| 550 | vlib_cli_dispatch_sub_commands (vm, cm, &sub_input, |
| 551 | parent_command_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | unformat_free (&sub_input); |
| 553 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 554 | |
| 555 | else |
| 556 | if (unformat_user (input, unformat_vlib_cli_sub_command, vm, parent, &c)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 557 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 558 | unformat_input_t *si; |
| 559 | uword has_sub_commands = |
| 560 | vec_len (c->sub_commands) + vec_len (c->sub_rules) > 0; |
| 561 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | si = input; |
| 563 | if (unformat_user (input, unformat_vlib_cli_sub_input, &sub_input)) |
| 564 | si = &sub_input; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 565 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | if (has_sub_commands) |
| 567 | error = vlib_cli_dispatch_sub_commands (vm, cm, si, c - cm->commands); |
| 568 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 569 | if (has_sub_commands && !error) |
| 570 | /* Found valid sub-command. */ ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 571 | |
| 572 | else if (c->function) |
| 573 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 574 | clib_error_t *c_error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 575 | |
| 576 | /* Skip white space for benefit of called function. */ |
| 577 | unformat_skip_white_space (si); |
| 578 | |
| 579 | if (unformat (si, "?")) |
| 580 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 581 | vlib_cli_output (vm, " %-40U %U", format_vlib_cli_path, c->path, format_vlib_cli_command_help, c, /* is_long */ |
| 582 | 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 583 | } |
| 584 | else |
| 585 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 586 | if (!c->is_mp_safe) |
| 587 | vlib_worker_thread_barrier_sync (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 588 | |
| 589 | c_error = c->function (vm, si, c); |
| 590 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 591 | if (!c->is_mp_safe) |
| 592 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 593 | |
| 594 | if (c_error) |
| 595 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 596 | error = |
| 597 | clib_error_return (0, "%v: %v", c->path, c_error->what); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | clib_error_free (c_error); |
| 599 | /* Free sub input. */ |
| 600 | if (si != input) |
| 601 | unformat_free (si); |
| 602 | |
| 603 | return error; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /* Free any previous error. */ |
| 608 | clib_error_free (error); |
| 609 | } |
| 610 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 611 | else if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 612 | error = clib_error_return (0, "%v: no sub-commands", c->path); |
| 613 | |
| 614 | /* Free sub input. */ |
| 615 | if (si != input) |
| 616 | unformat_free (si); |
| 617 | } |
| 618 | |
| 619 | else |
| 620 | goto unknown; |
| 621 | |
| 622 | return error; |
| 623 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 624 | unknown: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 625 | if (parent->path) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 626 | return clib_error_return (0, "%v: unknown input `%U'", parent->path, |
| 627 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 628 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 629 | return clib_error_return (0, "unknown input `%U'", format_unformat_error, |
| 630 | input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 634 | void vlib_unix_error_report (vlib_main_t *, clib_error_t *) |
| 635 | __attribute__ ((weak)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 637 | void |
| 638 | vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error) |
| 639 | { |
| 640 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 641 | |
| 642 | /* Process CLI input. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 643 | void |
| 644 | vlib_cli_input (vlib_main_t * vm, |
| 645 | unformat_input_t * input, |
| 646 | vlib_cli_output_function_t * function, uword function_arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 647 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 648 | vlib_process_t *cp = vlib_get_current_process (vm); |
| 649 | vlib_cli_main_t *cm = &vm->cli_main; |
| 650 | clib_error_t *error; |
| 651 | vlib_cli_output_function_t *save_function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | uword save_function_arg; |
| 653 | |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 654 | save_function = cp->output_function; |
| 655 | save_function_arg = cp->output_function_arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 657 | cp->output_function = function; |
| 658 | cp->output_function_arg = function_arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 659 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 660 | do |
| 661 | { |
| 662 | vec_reset_length (cm->parse_rule_data); |
| 663 | error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input, /* parent */ |
| 664 | 0); |
| 665 | } |
| 666 | while (!error && !unformat (input, "%U", unformat_eof)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | |
| 668 | if (error) |
| 669 | { |
| 670 | vlib_cli_output (vm, "%v", error->what); |
| 671 | vlib_unix_error_report (vm, error); |
| 672 | clib_error_free (error); |
| 673 | } |
| 674 | |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 675 | cp->output_function = save_function; |
| 676 | cp->output_function_arg = save_function_arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* Output to current CLI connection. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 680 | void |
| 681 | vlib_cli_output (vlib_main_t * vm, char *fmt, ...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 682 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 683 | vlib_process_t *cp = vlib_get_current_process (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 684 | va_list va; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 685 | u8 *s; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 686 | |
| 687 | va_start (va, fmt); |
| 688 | s = va_format (0, fmt, &va); |
| 689 | va_end (va); |
| 690 | |
| 691 | /* Terminate with \n if not present. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 692 | if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n') |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 693 | vec_add1 (s, '\n'); |
| 694 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 695 | if ((!cp) || (!cp->output_function)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | fformat (stdout, "%v", s); |
| 697 | else |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 698 | cp->output_function (cp->output_function_arg, s, vec_len (s)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 699 | |
| 700 | vec_free (s); |
| 701 | } |
| 702 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 703 | void *vl_msg_push_heap (void) __attribute__ ((weak)); |
| 704 | void vl_msg_pop_heap (void *oldheap) __attribute__ ((weak)); |
| 705 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | static clib_error_t * |
| 707 | show_memory_usage (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 708 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 709 | { |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 710 | int verbose = 0, api_segment = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 711 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 712 | u32 index = 0; |
| 713 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 714 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 716 | if (unformat (input, "verbose")) |
| 717 | verbose = 1; |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 718 | else if (unformat (input, "api-segment")) |
| 719 | api_segment = 1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 720 | else |
| 721 | { |
| 722 | error = clib_error_return (0, "unknown input `%U'", |
| 723 | format_unformat_error, input); |
| 724 | return error; |
| 725 | } |
| 726 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 728 | if (api_segment) |
| 729 | { |
| 730 | void *oldheap = vl_msg_push_heap (); |
| 731 | u8 *s_in_svm = |
| 732 | format (0, "%U\n", format_mheap, clib_mem_get_heap (), 1); |
| 733 | vl_msg_pop_heap (oldheap); |
| 734 | u8 *s = vec_dup (s_in_svm); |
| 735 | |
| 736 | oldheap = vl_msg_push_heap (); |
| 737 | vec_free (s_in_svm); |
| 738 | vl_msg_pop_heap (oldheap); |
| 739 | vlib_cli_output (vm, "API segment start:"); |
| 740 | vlib_cli_output (vm, "%v", s); |
| 741 | vlib_cli_output (vm, "API segment end:"); |
| 742 | vec_free (s); |
| 743 | } |
| 744 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 745 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | foreach_vlib_main ( |
| 747 | ({ |
| 748 | vlib_cli_output (vm, "Thread %d %v\n", index, vlib_worker_threads[index].name); |
| 749 | vlib_cli_output (vm, "%U\n", format_mheap, clib_per_cpu_mheaps[index], verbose); |
| 750 | index++; |
| 751 | })); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 752 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 756 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | VLIB_CLI_COMMAND (show_memory_usage_command, static) = { |
| 758 | .path = "show memory", |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 759 | .short_help = "[verbose | api-segment] Show current memory usage", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 760 | .function = show_memory_usage, |
| 761 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 762 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 763 | |
| 764 | static clib_error_t * |
Damjan Marion | eccf509 | 2016-11-18 16:59:24 +0100 | [diff] [blame] | 765 | show_cpu (vlib_main_t * vm, unformat_input_t * input, |
| 766 | vlib_cli_command_t * cmd) |
| 767 | { |
| 768 | #define _(a,b,c) vlib_cli_output (vm, "%-25s " b, a ":", c); |
| 769 | _("Model name", "%U", format_cpu_model_name); |
| 770 | _("Microarchitecture", "%U", format_cpu_uarch); |
| 771 | _("Flags", "%U", format_cpu_flags); |
| 772 | _("Base frequency", "%.2f GHz", |
| 773 | ((f64) vm->clib_time.clocks_per_second) * 1e-9); |
| 774 | #undef _ |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | /*? |
| 779 | * Displays various information about the CPU. |
| 780 | * |
| 781 | * @cliexpar |
| 782 | * @cliexstart{show cpu} |
| 783 | * Model name: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz |
| 784 | * Microarchitecture: Broadwell (Broadwell-EP/EX) |
| 785 | * Flags: sse3 ssse3 sse41 sse42 avx avx2 aes |
| 786 | * Base Frequency: 3.20 GHz |
| 787 | * @cliexend |
| 788 | ?*/ |
| 789 | /* *INDENT-OFF* */ |
| 790 | VLIB_CLI_COMMAND (show_cpu_command, static) = { |
| 791 | .path = "show cpu", |
| 792 | .short_help = "Show cpu information", |
| 793 | .function = show_cpu, |
| 794 | }; |
| 795 | |
| 796 | /* *INDENT-ON* */ |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 797 | |
Damjan Marion | eccf509 | 2016-11-18 16:59:24 +0100 | [diff] [blame] | 798 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 799 | enable_disable_memory_trace (vlib_main_t * vm, |
| 800 | unformat_input_t * input, |
| 801 | vlib_cli_command_t * cmd) |
| 802 | { |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 803 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 804 | int enable; |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 805 | int api_segment = 0; |
| 806 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 807 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 808 | |
| 809 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 810 | return 0; |
| 811 | |
| 812 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 813 | { |
Dave Barach | 1ddbc01 | 2018-06-13 09:26:05 -0400 | [diff] [blame] | 814 | if (unformat (line_input, "%U", unformat_vlib_enable_disable, &enable)) |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 815 | ; |
| 816 | else if (unformat (line_input, "api-segment")) |
| 817 | api_segment = 1; |
| 818 | else |
| 819 | { |
| 820 | unformat_free (line_input); |
| 821 | return clib_error_return (0, "invalid input"); |
| 822 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 823 | } |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 824 | unformat_free (line_input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 825 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 826 | if (api_segment) |
| 827 | oldheap = vl_msg_push_heap (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | clib_mem_trace (enable); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 829 | if (api_segment) |
| 830 | vl_msg_pop_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 831 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 832 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 835 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 836 | VLIB_CLI_COMMAND (enable_disable_memory_trace_command, static) = { |
| 837 | .path = "memory-trace", |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 838 | .short_help = "on|off [api-segment] Enable/disable memory allocation trace", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | .function = enable_disable_memory_trace, |
| 840 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 841 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | |
| 843 | |
| 844 | static clib_error_t * |
| 845 | test_heap_validate (vlib_main_t * vm, unformat_input_t * input, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 846 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 847 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 848 | clib_error_t *error = 0; |
| 849 | void *heap; |
| 850 | mheap_t *mheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 851 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 852 | if (unformat (input, "on")) |
| 853 | { |
| 854 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 855 | foreach_vlib_main({ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 856 | heap = clib_per_cpu_mheaps[this_vlib_main->thread_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | mheap = mheap_header(heap); |
| 858 | mheap->flags |= MHEAP_FLAG_VALIDATE; |
| 859 | // Turn off small object cache because it delays detection of errors |
| 860 | mheap->flags &= ~MHEAP_FLAG_SMALL_OBJECT_CACHE; |
| 861 | }); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 862 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 863 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 864 | } |
| 865 | else if (unformat (input, "off")) |
| 866 | { |
| 867 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 868 | foreach_vlib_main({ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 869 | heap = clib_per_cpu_mheaps[this_vlib_main->thread_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 870 | mheap = mheap_header(heap); |
| 871 | mheap->flags &= ~MHEAP_FLAG_VALIDATE; |
| 872 | mheap->flags |= MHEAP_FLAG_SMALL_OBJECT_CACHE; |
| 873 | }); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 874 | /* *INDENT-ON* */ |
| 875 | } |
| 876 | else if (unformat (input, "now")) |
| 877 | { |
| 878 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 879 | foreach_vlib_main({ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 880 | heap = clib_per_cpu_mheaps[this_vlib_main->thread_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 881 | mheap = mheap_header(heap); |
| 882 | mheap_validate(heap); |
| 883 | }); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 884 | /* *INDENT-ON* */ |
| 885 | vlib_cli_output (vm, "heap validation complete"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 886 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 887 | } |
| 888 | else |
| 889 | { |
| 890 | return clib_error_return (0, "unknown input `%U'", |
| 891 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 894 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 897 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 898 | VLIB_CLI_COMMAND (cmd_test_heap_validate,static) = { |
| 899 | .path = "test heap-validate", |
| 900 | .short_help = "<on/off/now> validate heap on future allocs/frees or right now", |
| 901 | .function = test_heap_validate, |
| 902 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 903 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 904 | |
Damjan Marion | e5ef1d7 | 2017-03-02 12:33:48 +0100 | [diff] [blame] | 905 | static clib_error_t * |
| 906 | restart_cmd_fn (vlib_main_t * vm, unformat_input_t * input, |
| 907 | vlib_cli_command_t * cmd) |
| 908 | { |
Chris Luke | 6edd360 | 2018-06-12 22:45:06 -0400 | [diff] [blame] | 909 | clib_file_main_t *fm = &file_main; |
| 910 | clib_file_t *f; |
Damjan Marion | e5ef1d7 | 2017-03-02 12:33:48 +0100 | [diff] [blame] | 911 | |
Chris Luke | 6edd360 | 2018-06-12 22:45:06 -0400 | [diff] [blame] | 912 | /* environ(7) does not indicate a header for this */ |
| 913 | extern char **environ; |
| 914 | |
| 915 | /* Close all known open files */ |
| 916 | /* *INDENT-OFF* */ |
| 917 | pool_foreach(f, fm->file_pool, |
| 918 | ({ |
| 919 | if (f->file_descriptor > 2) |
| 920 | close(f->file_descriptor); |
| 921 | })); |
| 922 | /* *INDENT-ON* */ |
| 923 | |
| 924 | /* Exec ourself */ |
| 925 | execve (vm->name, (char **) vm->argv, environ); |
Damjan Marion | e5ef1d7 | 2017-03-02 12:33:48 +0100 | [diff] [blame] | 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /* *INDENT-OFF* */ |
| 931 | VLIB_CLI_COMMAND (restart_cmd,static) = { |
| 932 | .path = "restart", |
| 933 | .short_help = "restart process", |
| 934 | .function = restart_cmd_fn, |
| 935 | }; |
| 936 | /* *INDENT-ON* */ |
| 937 | |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 938 | #ifdef TEST_CODE |
| 939 | /* |
| 940 | * A trivial test harness to verify the per-process output_function |
| 941 | * is working correcty. |
| 942 | */ |
| 943 | |
| 944 | static clib_error_t * |
| 945 | sleep_ten_seconds (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 946 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 947 | { |
| 948 | u16 i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 949 | u16 my_id = rand (); |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 950 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 951 | vlib_cli_output (vm, "Starting 10 seconds sleep with id %u\n", my_id); |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 952 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 953 | for (i = 0; i < 10; i++) |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 954 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 955 | vlib_process_wait_for_event_or_clock (vm, 1.0); |
| 956 | vlib_cli_output (vm, "Iteration number %u, my id: %u\n", i, my_id); |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 957 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 958 | vlib_cli_output (vm, "Done with sleep with id %u\n", my_id); |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 959 | return 0; |
| 960 | } |
| 961 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 962 | /* *INDENT-OFF* */ |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 963 | VLIB_CLI_COMMAND (ping_command, static) = { |
| 964 | .path = "test sleep", |
| 965 | .function = sleep_ten_seconds, |
| 966 | .short_help = "Sleep for 10 seconds", |
| 967 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 968 | /* *INDENT-ON* */ |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 969 | #endif /* ifdef TEST_CODE */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 970 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 971 | static uword |
| 972 | vlib_cli_normalize_path (char *input, char **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 974 | char *i = input; |
| 975 | char *s = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 976 | uword l = 0; |
| 977 | uword index_of_last_space = ~0; |
| 978 | |
| 979 | while (*i != 0) |
| 980 | { |
| 981 | u8 c = *i++; |
| 982 | /* Multiple white space -> single space. */ |
| 983 | switch (c) |
| 984 | { |
| 985 | case ' ': |
| 986 | case '\t': |
| 987 | case '\n': |
| 988 | case '\r': |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 989 | if (l > 0 && s[l - 1] != ' ') |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 990 | { |
| 991 | vec_add1 (s, ' '); |
| 992 | l++; |
| 993 | } |
| 994 | break; |
| 995 | |
| 996 | default: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 997 | if (l > 0 && s[l - 1] == ' ') |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 998 | index_of_last_space = vec_len (s); |
| 999 | vec_add1 (s, c); |
| 1000 | l++; |
| 1001 | break; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* Remove any extra space at end. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1006 | if (l > 0 && s[l - 1] == ' ') |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1007 | _vec_len (s) -= 1; |
| 1008 | |
| 1009 | *result = s; |
| 1010 | return index_of_last_space; |
| 1011 | } |
| 1012 | |
| 1013 | always_inline uword |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1014 | parent_path_len (char *path) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1015 | { |
| 1016 | word i; |
| 1017 | for (i = vec_len (path) - 1; i >= 0; i--) |
| 1018 | { |
| 1019 | if (path[i] == ' ') |
| 1020 | return i; |
| 1021 | } |
| 1022 | return ~0; |
| 1023 | } |
| 1024 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1025 | static void |
| 1026 | add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1027 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1028 | vlib_cli_command_t *p, *c; |
| 1029 | vlib_cli_sub_command_t *sub_c; |
| 1030 | u8 *sub_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1031 | word i, l; |
| 1032 | |
| 1033 | p = vec_elt_at_index (cm->commands, parent_index); |
| 1034 | c = vec_elt_at_index (cm->commands, child_index); |
| 1035 | |
| 1036 | l = parent_path_len (c->path); |
| 1037 | if (l == ~0) |
| 1038 | sub_name = vec_dup ((u8 *) c->path); |
| 1039 | else |
| 1040 | { |
| 1041 | ASSERT (l + 1 < vec_len (c->path)); |
| 1042 | sub_name = 0; |
| 1043 | vec_add (sub_name, c->path + l + 1, vec_len (c->path) - (l + 1)); |
| 1044 | } |
| 1045 | |
| 1046 | if (sub_name[0] == '%') |
| 1047 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1048 | uword *q; |
| 1049 | vlib_cli_sub_rule_t *sr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1050 | |
| 1051 | /* Remove %. */ |
| 1052 | vec_delete (sub_name, 1, 0); |
| 1053 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1054 | if (!p->sub_rule_index_by_name) |
| 1055 | p->sub_rule_index_by_name = hash_create_vec ( /* initial length */ 32, |
| 1056 | sizeof (sub_name[0]), |
| 1057 | sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1058 | q = hash_get_mem (p->sub_rule_index_by_name, sub_name); |
| 1059 | if (q) |
| 1060 | { |
| 1061 | sr = vec_elt_at_index (p->sub_rules, q[0]); |
| 1062 | ASSERT (sr->command_index == child_index); |
| 1063 | return; |
| 1064 | } |
| 1065 | |
| 1066 | q = hash_get_mem (cm->parse_rule_index_by_name, sub_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1067 | if (!q) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 1068 | { |
| 1069 | clib_error ("reference to unknown rule `%%%v' in path `%v'", |
| 1070 | sub_name, c->path); |
| 1071 | return; |
| 1072 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1073 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1074 | hash_set_mem (p->sub_rule_index_by_name, sub_name, |
| 1075 | vec_len (p->sub_rules)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1076 | vec_add2 (p->sub_rules, sr, 1); |
| 1077 | sr->name = sub_name; |
| 1078 | sr->rule_index = q[0]; |
| 1079 | sr->command_index = child_index; |
| 1080 | return; |
| 1081 | } |
| 1082 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1083 | if (!p->sub_command_index_by_name) |
| 1084 | p->sub_command_index_by_name = hash_create_vec ( /* initial length */ 32, |
| 1085 | sizeof (c->path[0]), |
| 1086 | sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1087 | |
| 1088 | /* Check if sub-command has already been created. */ |
| 1089 | if (hash_get_mem (p->sub_command_index_by_name, sub_name)) |
| 1090 | { |
| 1091 | vec_free (sub_name); |
| 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | vec_add2 (p->sub_commands, sub_c, 1); |
| 1096 | sub_c->index = child_index; |
| 1097 | sub_c->name = sub_name; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1098 | hash_set_mem (p->sub_command_index_by_name, sub_c->name, |
| 1099 | sub_c - p->sub_commands); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1100 | |
| 1101 | vec_validate (p->sub_command_positions, vec_len (sub_c->name) - 1); |
| 1102 | for (i = 0; i < vec_len (sub_c->name); i++) |
| 1103 | { |
| 1104 | int n; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1105 | vlib_cli_parse_position_t *pos; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1106 | |
| 1107 | pos = vec_elt_at_index (p->sub_command_positions, i); |
| 1108 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1109 | if (!pos->bitmaps) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1110 | pos->min_char = sub_c->name[i]; |
| 1111 | |
| 1112 | n = sub_c->name[i] - pos->min_char; |
| 1113 | if (n < 0) |
| 1114 | { |
| 1115 | pos->min_char = sub_c->name[i]; |
| 1116 | vec_insert (pos->bitmaps, -n, 0); |
| 1117 | n = 0; |
| 1118 | } |
| 1119 | |
| 1120 | vec_validate (pos->bitmaps, n); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1121 | pos->bitmaps[n] = |
| 1122 | clib_bitmap_ori (pos->bitmaps[n], sub_c - p->sub_commands); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | static void |
| 1127 | vlib_cli_make_parent (vlib_cli_main_t * cm, uword ci) |
| 1128 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1129 | uword p_len, pi, *p; |
| 1130 | char *p_path; |
| 1131 | vlib_cli_command_t *c, *parent; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1132 | |
| 1133 | /* Root command (index 0) should have already been added. */ |
| 1134 | ASSERT (vec_len (cm->commands) > 0); |
| 1135 | |
| 1136 | c = vec_elt_at_index (cm->commands, ci); |
| 1137 | p_len = parent_path_len (c->path); |
| 1138 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1139 | /* No space? Parent is root command. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1140 | if (p_len == ~0) |
| 1141 | { |
| 1142 | add_sub_command (cm, 0, ci); |
| 1143 | return; |
| 1144 | } |
| 1145 | |
| 1146 | p_path = 0; |
| 1147 | vec_add (p_path, c->path, p_len); |
| 1148 | |
| 1149 | p = hash_get_mem (cm->command_index_by_path, p_path); |
| 1150 | |
| 1151 | /* Parent exists? */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1152 | if (!p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1153 | { |
| 1154 | /* Parent does not exist; create it. */ |
| 1155 | vec_add2 (cm->commands, parent, 1); |
| 1156 | parent->path = p_path; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1157 | hash_set_mem (cm->command_index_by_path, parent->path, |
| 1158 | parent - cm->commands); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1159 | pi = parent - cm->commands; |
| 1160 | } |
| 1161 | else |
| 1162 | { |
| 1163 | pi = p[0]; |
| 1164 | vec_free (p_path); |
| 1165 | } |
| 1166 | |
| 1167 | add_sub_command (cm, pi, ci); |
| 1168 | |
| 1169 | /* Create parent's parent. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1170 | if (!p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1171 | vlib_cli_make_parent (cm, pi); |
| 1172 | } |
| 1173 | |
| 1174 | always_inline uword |
| 1175 | vlib_cli_command_is_empty (vlib_cli_command_t * c) |
| 1176 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1177 | return (c->long_help == 0 && c->short_help == 0 && c->function == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1180 | clib_error_t * |
| 1181 | vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1182 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1183 | vlib_cli_main_t *cm = &vm->cli_main; |
| 1184 | clib_error_t *error = 0; |
| 1185 | uword ci, *p; |
| 1186 | char *normalized_path; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1187 | |
| 1188 | if ((error = vlib_call_init_function (vm, vlib_cli_init))) |
| 1189 | return error; |
| 1190 | |
| 1191 | (void) vlib_cli_normalize_path (c->path, &normalized_path); |
| 1192 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1193 | if (!cm->command_index_by_path) |
| 1194 | cm->command_index_by_path = hash_create_vec ( /* initial length */ 32, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1195 | sizeof (c->path[0]), |
| 1196 | sizeof (uword)); |
| 1197 | |
| 1198 | /* See if command already exists with given path. */ |
| 1199 | p = hash_get_mem (cm->command_index_by_path, normalized_path); |
| 1200 | if (p) |
| 1201 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1202 | vlib_cli_command_t *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1203 | |
| 1204 | ci = p[0]; |
| 1205 | d = vec_elt_at_index (cm->commands, ci); |
| 1206 | |
| 1207 | /* If existing command was created via vlib_cli_make_parent |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1208 | replaced it with callers data. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1209 | if (vlib_cli_command_is_empty (d)) |
| 1210 | { |
| 1211 | vlib_cli_command_t save = d[0]; |
| 1212 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1213 | ASSERT (!vlib_cli_command_is_empty (c)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1214 | |
| 1215 | /* Copy callers fields. */ |
| 1216 | d[0] = c[0]; |
| 1217 | |
| 1218 | /* Save internal fields. */ |
| 1219 | d->path = save.path; |
| 1220 | d->sub_commands = save.sub_commands; |
| 1221 | d->sub_command_index_by_name = save.sub_command_index_by_name; |
| 1222 | d->sub_command_positions = save.sub_command_positions; |
| 1223 | d->sub_rules = save.sub_rules; |
| 1224 | } |
| 1225 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1226 | error = |
| 1227 | clib_error_return (0, "duplicate command name with path %v", |
| 1228 | normalized_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1229 | |
| 1230 | vec_free (normalized_path); |
| 1231 | if (error) |
| 1232 | return error; |
| 1233 | } |
| 1234 | else |
| 1235 | { |
| 1236 | /* Command does not exist: create it. */ |
| 1237 | |
| 1238 | /* Add root command (index 0). */ |
| 1239 | if (vec_len (cm->commands) == 0) |
| 1240 | { |
| 1241 | /* Create command with index 0; path is empty string. */ |
| 1242 | vec_resize (cm->commands, 1); |
| 1243 | } |
| 1244 | |
| 1245 | ci = vec_len (cm->commands); |
| 1246 | hash_set_mem (cm->command_index_by_path, normalized_path, ci); |
| 1247 | vec_add1 (cm->commands, c[0]); |
| 1248 | |
| 1249 | c = vec_elt_at_index (cm->commands, ci); |
| 1250 | c->path = normalized_path; |
| 1251 | |
| 1252 | /* Don't inherit from registration. */ |
| 1253 | c->sub_commands = 0; |
| 1254 | c->sub_command_index_by_name = 0; |
| 1255 | c->sub_command_positions = 0; |
| 1256 | } |
| 1257 | |
| 1258 | vlib_cli_make_parent (cm, ci); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | clib_error_t * |
| 1263 | vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg) |
| 1264 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1265 | vlib_cli_main_t *cm = &vm->cli_main; |
| 1266 | vlib_cli_parse_rule_t *r; |
| 1267 | clib_error_t *error = 0; |
| 1268 | u8 *r_name; |
| 1269 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1270 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1271 | if (!cm->parse_rule_index_by_name) |
| 1272 | cm->parse_rule_index_by_name = hash_create_vec ( /* initial length */ 32, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1273 | sizeof (r->name[0]), |
| 1274 | sizeof (uword)); |
| 1275 | |
| 1276 | /* Make vector copy of name. */ |
| 1277 | r_name = format (0, "%s", r_reg->name); |
| 1278 | |
| 1279 | if ((p = hash_get_mem (cm->parse_rule_index_by_name, r_name))) |
| 1280 | { |
| 1281 | vec_free (r_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1282 | return clib_error_return (0, "duplicate parse rule name `%s'", |
| 1283 | r_reg->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | vec_add2 (cm->parse_rules, r, 1); |
| 1287 | r[0] = r_reg[0]; |
| 1288 | r->name = (char *) r_name; |
| 1289 | hash_set_mem (cm->parse_rule_index_by_name, r->name, r - cm->parse_rules); |
| 1290 | |
| 1291 | return error; |
| 1292 | } |
| 1293 | |
| 1294 | #if 0 |
| 1295 | /* $$$ turn back on again someday, maybe */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1296 | static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm, |
| 1297 | vlib_cli_parse_rule_t * |
| 1298 | lo, |
| 1299 | vlib_cli_parse_rule_t * |
| 1300 | hi) |
| 1301 | __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1302 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1303 | clib_error_t *error = 0; |
| 1304 | vlib_cli_parse_rule_t *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1305 | |
| 1306 | for (r = lo; r < hi; r = clib_elf_section_data_next (r, 0)) |
| 1307 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1308 | if (!r->name || strlen (r->name) == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1309 | { |
| 1310 | error = clib_error_return (0, "parse rule with no name"); |
| 1311 | goto done; |
| 1312 | } |
| 1313 | |
| 1314 | error = vlib_cli_register_parse_rule (vm, r); |
| 1315 | if (error) |
| 1316 | goto done; |
| 1317 | } |
| 1318 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1319 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1320 | return error; |
| 1321 | } |
| 1322 | #endif |
| 1323 | |
Damjan Marion | 9c2b9f4 | 2017-04-21 13:56:40 +0200 | [diff] [blame] | 1324 | static int |
| 1325 | cli_path_compare (void *a1, void *a2) |
| 1326 | { |
| 1327 | u8 **s1 = a1; |
| 1328 | u8 **s2 = a2; |
| 1329 | |
| 1330 | if ((vec_len (*s1) < vec_len (*s2)) && |
| 1331 | memcmp ((char *) *s1, (char *) *s2, vec_len (*s1)) == 0) |
| 1332 | return -1; |
| 1333 | |
| 1334 | |
| 1335 | if ((vec_len (*s1) > vec_len (*s2)) && |
| 1336 | memcmp ((char *) *s1, (char *) *s2, vec_len (*s2)) == 0) |
| 1337 | return 1; |
| 1338 | |
| 1339 | return vec_cmp (*s1, *s2); |
| 1340 | } |
| 1341 | |
| 1342 | static clib_error_t * |
| 1343 | show_cli_cmd_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1344 | vlib_cli_command_t * cmd) |
| 1345 | { |
| 1346 | vlib_cli_main_t *cm = &vm->cli_main; |
| 1347 | vlib_cli_command_t *cli; |
| 1348 | u8 **paths = 0, **s; |
| 1349 | |
| 1350 | /* *INDENT-OFF* */ |
| 1351 | vec_foreach (cli, cm->commands) |
| 1352 | if (vec_len (cli->path) > 0) |
| 1353 | vec_add1 (paths, (u8 *) cli->path); |
| 1354 | |
| 1355 | vec_sort_with_function (paths, cli_path_compare); |
| 1356 | |
| 1357 | vec_foreach (s, paths) |
| 1358 | vlib_cli_output (vm, "%v", *s); |
| 1359 | /* *INDENT-ON* */ |
| 1360 | |
| 1361 | vec_free (paths); |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | /* *INDENT-OFF* */ |
| 1366 | VLIB_CLI_COMMAND (show_cli_command, static) = { |
| 1367 | .path = "show cli", |
| 1368 | .short_help = "Show cli commands", |
| 1369 | .function = show_cli_cmd_fn, |
| 1370 | }; |
| 1371 | /* *INDENT-ON* */ |
| 1372 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1373 | static clib_error_t * |
| 1374 | vlib_cli_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1375 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1376 | vlib_cli_main_t *cm = &vm->cli_main; |
| 1377 | clib_error_t *error = 0; |
| 1378 | vlib_cli_command_t *cmd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1379 | |
| 1380 | cmd = cm->cli_command_registrations; |
| 1381 | |
| 1382 | while (cmd) |
| 1383 | { |
| 1384 | error = vlib_cli_register (vm, cmd); |
| 1385 | if (error) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1386 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1387 | cmd = cmd->next_cli_command; |
| 1388 | } |
| 1389 | return error; |
| 1390 | } |
| 1391 | |
| 1392 | VLIB_INIT_FUNCTION (vlib_cli_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1393 | |
| 1394 | /* |
| 1395 | * fd.io coding-style-patch-verification: ON |
| 1396 | * |
| 1397 | * Local Variables: |
| 1398 | * eval: (c-set-style "gnu") |
| 1399 | * End: |
| 1400 | */ |