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