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