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 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 34 | u32 |
| 35 | vl (void *p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | { |
| 37 | return vec_len (p); |
| 38 | } |
| 39 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 40 | /** |
| 41 | * @brief GDB callable function: pe - call pool_elts - number of elements in a pool |
| 42 | * |
| 43 | * @param *v - void - address of pool |
| 44 | * |
| 45 | * @return number - uword |
| 46 | * |
| 47 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 48 | uword |
| 49 | pe (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 51 | return (pool_elts (v)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 54 | /** |
| 55 | * @brief GDB callable function: pifi - call pool_is_free_index - is passed index free? |
| 56 | * |
| 57 | * @param *p - void - address of pool |
| 58 | * @param *index - u32 |
| 59 | * |
| 60 | * @return 0|1 - int |
| 61 | * |
| 62 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 63 | int |
| 64 | pifi (void *p, u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | { |
| 66 | return pool_is_free_index (p, index); |
| 67 | } |
| 68 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 69 | /** |
| 70 | * @brief GDB callable function: debug_hex_bytes - return formatted hex string |
| 71 | * |
| 72 | * @param *s - u8 |
| 73 | * @param n - u32 - number of bytes to format |
| 74 | * |
| 75 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 76 | void |
| 77 | debug_hex_bytes (u8 * s, u32 n) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | { |
| 79 | fformat (stderr, "%U\n", format_hex_bytes, s, n); |
| 80 | } |
| 81 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @brief GDB callable function: vlib_dump_frame_ownership |
| 84 | * |
| 85 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 86 | void |
| 87 | vlib_dump_frame_ownership (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 89 | vlib_main_t *vm = vlib_get_main (); |
| 90 | vlib_node_main_t *nm = &vm->node_main; |
| 91 | vlib_node_runtime_t *this_node_runtime; |
| 92 | vlib_next_frame_t *nf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | u32 first_nf_index; |
| 94 | u32 index; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 95 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 96 | vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) |
| 97 | { |
| 98 | first_nf_index = this_node_runtime->next_frame_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 100 | for (index = first_nf_index; index < first_nf_index + |
| 101 | this_node_runtime->n_next_nodes; index++) |
| 102 | { |
| 103 | vlib_node_runtime_t *owned_runtime; |
| 104 | nf = vec_elt_at_index (vm->node_main.next_frames, index); |
| 105 | if (nf->flags & VLIB_FRAME_OWNER) |
| 106 | { |
| 107 | owned_runtime = vec_elt_at_index (nm->nodes_by_type[0], |
| 108 | nf->node_runtime_index); |
| 109 | fformat (stderr, |
| 110 | "%s next index %d owns enqueue rights to %s\n", |
| 111 | nm->nodes[this_node_runtime->node_index]->name, |
| 112 | index - first_nf_index, |
| 113 | nm->nodes[owned_runtime->node_index]->name); |
Andreas Schultz | 58b2eb1 | 2019-07-15 15:40:56 +0200 | [diff] [blame] | 114 | fformat (stderr, " nf index %d nf->frame %p\n", |
| 115 | nf - vm->node_main.next_frames, nf->frame); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 121 | /** |
| 122 | * @brief GDB callable function: vlib_runtime_index_to_node_name |
| 123 | * |
| 124 | * Takes node index and will return the node name. |
| 125 | * |
| 126 | * @param index - u32 |
| 127 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 128 | void |
| 129 | vlib_runtime_index_to_node_name (u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 131 | vlib_main_t *vm = vlib_get_main (); |
| 132 | vlib_node_main_t *nm = &vm->node_main; |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 133 | |
Eyal Bari | cd30774 | 2018-07-22 12:45:15 +0300 | [diff] [blame] | 134 | if (index >= vec_len (nm->nodes)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 136 | fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 140 | fformat (stderr, "node runtime index %d name %s\n", index, |
| 141 | nm->nodes[index]->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 144 | void |
| 145 | gdb_show_errors (int verbose) |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 146 | { |
| 147 | extern vlib_cli_command_t vlib_cli_show_errors; |
| 148 | unformat_input_t input; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 149 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 150 | |
| 151 | if (verbose == 0) |
| 152 | unformat_init_string (&input, "verbose 0", 9); |
| 153 | else if (verbose == 1) |
| 154 | unformat_init_string (&input, "verbose 1", 9); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 155 | else |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 156 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 157 | fformat (stderr, "verbose not 0 or 1\n"); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 161 | vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ ); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 162 | unformat_free (&input); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 163 | } |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 164 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 165 | void |
| 166 | gdb_show_session (int verbose) |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 167 | { |
| 168 | extern vlib_cli_command_t vlib_cli_show_session_command; |
| 169 | unformat_input_t input; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 170 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 171 | |
| 172 | if (verbose == 0) |
| 173 | unformat_init_string (&input, "verbose 0", 9); |
| 174 | else if (verbose == 1) |
| 175 | unformat_init_string (&input, "verbose 1", 9); |
| 176 | else if (verbose == 2) |
| 177 | unformat_init_string (&input, "verbose 2", 9); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 178 | else |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 179 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 180 | fformat (stderr, "verbose not 0 - 2\n"); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 184 | vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ ); |
Florin Coras | 66b1131 | 2017-07-31 17:18:03 -0700 | [diff] [blame] | 185 | unformat_free (&input); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 186 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | |
Kingwel Xie | 3543908 | 2019-03-05 05:06:48 -0500 | [diff] [blame] | 188 | static int |
| 189 | trace_cmp (void *a1, void *a2) |
| 190 | { |
| 191 | vlib_trace_header_t **t1 = a1; |
| 192 | vlib_trace_header_t **t2 = a2; |
| 193 | i64 dt = t1[0]->time - t2[0]->time; |
| 194 | return dt < 0 ? -1 : (dt > 0 ? +1 : 0); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | gdb_show_traces () |
| 199 | { |
| 200 | vlib_trace_main_t *tm; |
| 201 | vlib_trace_header_t **h, **traces; |
| 202 | u32 i, index = 0; |
| 203 | char *fmt; |
| 204 | u8 *s = 0; |
| 205 | u32 max; |
| 206 | |
| 207 | /* By default display only this many traces. */ |
| 208 | max = 50; |
| 209 | |
| 210 | /* Get active traces from pool. */ |
| 211 | |
| 212 | /* *INDENT-OFF* */ |
| 213 | foreach_vlib_main ( |
| 214 | ({ |
| 215 | fmt = "------------------- Start of thread %d %s -------------------\n"; |
| 216 | s = format (s, fmt, index, vlib_worker_threads[index].name); |
| 217 | |
| 218 | tm = &this_vlib_main->trace_main; |
| 219 | |
| 220 | trace_apply_filter(this_vlib_main); |
| 221 | |
| 222 | traces = 0; |
| 223 | pool_foreach (h, tm->trace_buffer_pool, |
| 224 | ({ |
| 225 | vec_add1 (traces, h[0]); |
| 226 | })); |
| 227 | |
| 228 | if (vec_len (traces) == 0) |
| 229 | { |
| 230 | s = format (s, "No packets in trace buffer\n"); |
| 231 | goto done; |
| 232 | } |
| 233 | |
| 234 | /* Sort them by increasing time. */ |
| 235 | vec_sort_with_function (traces, trace_cmp); |
| 236 | |
| 237 | for (i = 0; i < vec_len (traces); i++) |
| 238 | { |
| 239 | if (i == max) |
| 240 | { |
| 241 | fformat (stderr, "Limiting display to %d packets." |
| 242 | " To display more specify max.", max); |
| 243 | goto done; |
| 244 | } |
| 245 | |
| 246 | s = format (s, "Packet %d\n%U\n\n", i + 1, |
| 247 | format_vlib_trace, vlib_mains[0], traces[i]); |
| 248 | } |
| 249 | |
| 250 | done: |
| 251 | vec_free (traces); |
| 252 | |
| 253 | index++; |
| 254 | })); |
| 255 | /* *INDENT-ON* */ |
| 256 | |
| 257 | fformat (stderr, "%v", s); |
| 258 | vec_free (s); |
| 259 | } |
| 260 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 261 | /** |
| 262 | * @brief GDB callable function: show_gdb_command_fn - show gdb |
| 263 | * |
| 264 | * Shows list of functions for VPP available in GDB |
| 265 | * |
| 266 | * @return error - clib_error_t |
| 267 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | static clib_error_t * |
| 269 | show_gdb_command_fn (vlib_main_t * vm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 270 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | { |
| 272 | vlib_cli_output (vm, "vl(p) returns vec_len(p)"); |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 273 | vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]"); |
| 274 | vlib_cli_output (vm, "vb2(b) returns vnet_buffer2(b) [opaque2]"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | vlib_cli_output (vm, "pe(p) returns pool_elts(p)"); |
| 276 | 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] | 277 | vlib_cli_output (vm, "gdb_show_errors(0|1) dumps error counters"); |
| 278 | vlib_cli_output (vm, "gdb_show_session dumps session counters"); |
Kingwel Xie | 3543908 | 2019-03-05 05:06:48 -0500 | [diff] [blame] | 279 | vlib_cli_output (vm, "gdb_show_traces() dumps buffer traces"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex"); |
| 281 | vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says"); |
| 282 | vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN"); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 287 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = { |
| 289 | .path = "show gdb", |
| 290 | .short_help = "Describe functions which can be called from gdb", |
| 291 | .function = show_gdb_command_fn, |
| 292 | }; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 293 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 295 | vnet_buffer_opaque_t * |
| 296 | vb (void *vb_arg) |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 297 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 298 | vlib_buffer_t *b = (vlib_buffer_t *) vb_arg; |
| 299 | vnet_buffer_opaque_t *rv; |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 300 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 301 | rv = vnet_buffer (b); |
| 302 | |
| 303 | return rv; |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 304 | } |
| 305 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 306 | vnet_buffer_opaque2_t * |
| 307 | vb2 (void *vb_arg) |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 308 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 309 | vlib_buffer_t *b = (vlib_buffer_t *) vb_arg; |
| 310 | vnet_buffer_opaque2_t *rv; |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 311 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 312 | rv = vnet_buffer2 (b); |
| 313 | |
| 314 | return rv; |
Dave Barach | 7bee773 | 2017-10-18 18:48:11 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | /* Cafeteria plan, maybe you don't want these functions */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 319 | clib_error_t * |
| 320 | gdb_func_init (vlib_main_t * vm) |
| 321 | { |
| 322 | return 0; |
| 323 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
| 325 | VLIB_INIT_FUNCTION (gdb_func_init); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 326 | |
| 327 | /* |
| 328 | * fd.io coding-style-patch-verification: ON |
| 329 | * |
| 330 | * Local Variables: |
| 331 | * eval: (c-set-style "gnu") |
| 332 | * End: |
| 333 | */ |