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