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