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