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 | */ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 15 | /** |
| 16 | * @file |
| 17 | * @brief Host utility functions |
| 18 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 19 | #include <vppinfra/format.h> |
| 20 | #include <vlib/vlib.h> |
| 21 | |
| 22 | #include <vlib/threads.h> |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 23 | #include <vnet/vnet.h> |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 24 | #include <vppinfra/format.h> |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * @brief GDB callable function: vl - Return vector length of vector |
| 28 | * |
| 29 | * @param *p - void - address of vector |
| 30 | * |
| 31 | * @return length - u32 |
| 32 | * |
| 33 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | u32 vl(void *p) |
| 35 | { |
| 36 | return vec_len (p); |
| 37 | } |
| 38 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @brief GDB callable function: pe - call pool_elts - number of elements in a pool |
| 41 | * |
| 42 | * @param *v - void - address of pool |
| 43 | * |
| 44 | * @return number - uword |
| 45 | * |
| 46 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | uword pe (void *v) |
| 48 | { |
| 49 | return (pool_elts(v)); |
| 50 | } |
| 51 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 52 | /** |
| 53 | * @brief GDB callable function: pifi - call pool_is_free_index - is passed index free? |
| 54 | * |
| 55 | * @param *p - void - address of pool |
| 56 | * @param *index - u32 |
| 57 | * |
| 58 | * @return 0|1 - int |
| 59 | * |
| 60 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | int pifi (void *p, u32 index) |
| 62 | { |
| 63 | return pool_is_free_index (p, index); |
| 64 | } |
| 65 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 66 | /** |
| 67 | * @brief GDB callable function: debug_hex_bytes - return formatted hex string |
| 68 | * |
| 69 | * @param *s - u8 |
| 70 | * @param n - u32 - number of bytes to format |
| 71 | * |
| 72 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | void debug_hex_bytes (u8 *s, u32 n) |
| 74 | { |
| 75 | fformat (stderr, "%U\n", format_hex_bytes, s, n); |
| 76 | } |
| 77 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @brief GDB callable function: vlib_dump_frame_ownership |
| 80 | * |
| 81 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | void vlib_dump_frame_ownership (void) |
| 83 | { |
| 84 | vlib_main_t * vm = vlib_get_main(); |
| 85 | vlib_node_main_t * nm = &vm->node_main; |
| 86 | vlib_node_runtime_t * this_node_runtime; |
| 87 | vlib_next_frame_t * nf; |
| 88 | u32 first_nf_index; |
| 89 | u32 index; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 90 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | vec_foreach(this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) |
| 92 | { |
| 93 | first_nf_index = this_node_runtime->next_frame_index; |
| 94 | |
| 95 | for (index = first_nf_index; index < first_nf_index + |
| 96 | this_node_runtime->n_next_nodes; index++) |
| 97 | { |
| 98 | vlib_node_runtime_t * owned_runtime; |
| 99 | nf = vec_elt_at_index (vm->node_main.next_frames, index); |
| 100 | if (nf->flags & VLIB_FRAME_OWNER) |
| 101 | { |
| 102 | owned_runtime = vec_elt_at_index (nm->nodes_by_type[0], |
| 103 | nf->node_runtime_index); |
| 104 | fformat(stderr, |
| 105 | "%s next index %d owns enqueue rights to %s\n", |
| 106 | nm->nodes[this_node_runtime->node_index]->name, |
| 107 | index - first_nf_index, |
| 108 | nm->nodes[owned_runtime->node_index]->name); |
| 109 | fformat (stderr, " nf index %d nf->frame_index %d\n", |
| 110 | nf - vm->node_main.next_frames, |
| 111 | nf->frame_index); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 117 | /** |
| 118 | * @brief GDB callable function: vlib_runtime_index_to_node_name |
| 119 | * |
| 120 | * Takes node index and will return the node name. |
| 121 | * |
| 122 | * @param index - u32 |
| 123 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | void vlib_runtime_index_to_node_name (u32 index) |
| 125 | { |
| 126 | vlib_main_t * vm = vlib_get_main(); |
| 127 | vlib_node_main_t * nm = &vm->node_main; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 128 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | if (index > vec_len (nm->nodes)) |
| 130 | { |
| 131 | fformat(stderr, "%d out of range, max %d\n", vec_len(nm->nodes)); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | fformat(stderr, "node runtime index %d name %s\n", index, nm->nodes[index]->name); |
| 136 | } |
| 137 | |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 138 | void gdb_show_errors (int verbose) |
| 139 | { |
| 140 | extern vlib_cli_command_t vlib_cli_show_errors; |
| 141 | unformat_input_t input; |
| 142 | vlib_main_t * vm = vlib_get_main(); |
| 143 | |
| 144 | if (verbose == 0) |
| 145 | unformat_init_string (&input, "verbose 0", 9); |
| 146 | else if (verbose == 1) |
| 147 | unformat_init_string (&input, "verbose 1", 9); |
| 148 | else |
| 149 | { |
| 150 | fformat(stderr, "verbose not 0 or 1\n"); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | vlib_cli_show_errors.function (vm, &input, 0 /* cmd */); |
| 155 | unformat_free (&input); |
| 156 | } |
| 157 | |
| 158 | void gdb_show_session (int verbose) |
| 159 | { |
| 160 | extern vlib_cli_command_t vlib_cli_show_session_command; |
| 161 | unformat_input_t input; |
| 162 | vlib_main_t * vm = vlib_get_main(); |
| 163 | |
| 164 | if (verbose == 0) |
| 165 | unformat_init_string (&input, "verbose 0", 9); |
| 166 | else if (verbose == 1) |
| 167 | unformat_init_string (&input, "verbose 1", 9); |
| 168 | else if (verbose == 2) |
| 169 | unformat_init_string (&input, "verbose 2", 9); |
| 170 | else |
| 171 | { |
| 172 | fformat(stderr, "verbose not 0 - 2\n"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */); |
| 177 | unformat_free (&input); |
| 178 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 180 | /** |
| 181 | * @brief GDB callable function: show_gdb_command_fn - show gdb |
| 182 | * |
| 183 | * Shows list of functions for VPP available in GDB |
| 184 | * |
| 185 | * @return error - clib_error_t |
| 186 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | static clib_error_t * |
| 188 | show_gdb_command_fn (vlib_main_t * vm, |
| 189 | unformat_input_t * input, |
| 190 | vlib_cli_command_t * cmd) |
| 191 | { |
| 192 | vlib_cli_output (vm, "vl(p) returns vec_len(p)"); |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 193 | vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]"); |
| 194 | vlib_cli_output (vm, "vb2(b) returns vnet_buffer2(b) [opaque2]"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | vlib_cli_output (vm, "pe(p) returns pool_elts(p)"); |
| 196 | vlib_cli_output (vm, "pifi(p, i) returns pool_is_free_index(p, i)"); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 197 | vlib_cli_output (vm, "gdb_show_errors(0|1) dumps error counters"); |
| 198 | vlib_cli_output (vm, "gdb_show_session dumps session counters"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 199 | vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex"); |
| 200 | vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says"); |
| 201 | vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN"); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = { |
| 207 | .path = "show gdb", |
| 208 | .short_help = "Describe functions which can be called from gdb", |
| 209 | .function = show_gdb_command_fn, |
| 210 | }; |
| 211 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 212 | vnet_buffer_opaque_t *vb (void *vb_arg) |
| 213 | { |
| 214 | vlib_buffer_t *b = (vlib_buffer_t *)vb_arg; |
| 215 | vnet_buffer_opaque_t *rv; |
| 216 | |
| 217 | rv = vnet_buffer (b); |
| 218 | |
| 219 | return rv; |
| 220 | } |
| 221 | |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 222 | vnet_buffer_opaque2_t *vb2 (void *vb_arg) |
| 223 | { |
| 224 | vlib_buffer_t *b = (vlib_buffer_t *)vb_arg; |
| 225 | vnet_buffer_opaque2_t *rv; |
| 226 | |
| 227 | rv = vnet_buffer2(b); |
| 228 | |
| 229 | return rv; |
| 230 | } |
| 231 | |
| 232 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | /* Cafeteria plan, maybe you don't want these functions */ |
| 234 | clib_error_t * |
| 235 | gdb_func_init (vlib_main_t * vm) { return 0; } |
| 236 | |
| 237 | VLIB_INIT_FUNCTION (gdb_func_init); |