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