blob: d78773edf07a643288e3b15dd8ccf78c9c616e42 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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)07203af2016-08-25 13:37:37 -070015/**
16 * @file
17 * @brief Host utility functions
18 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070019#include <vppinfra/format.h>
20#include <vlib/vlib.h>
21
22#include <vlib/threads.h>
Dave Barachacd2a6a2017-05-16 17:41:34 -040023#include <vnet/vnet.h>
Florin Coras66b11312017-07-31 17:18:03 -070024#include <vppinfra/format.h>
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070025
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 reddy1b0c9832017-11-29 20:08:11 +053034u32
35vl (void *p)
Ed Warnickecb9cada2015-12-08 15:45:58 -070036{
37 return vec_len (p);
38}
39
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070040/**
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 reddy1b0c9832017-11-29 20:08:11 +053048uword
49pe (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070050{
sharath reddy1b0c9832017-11-29 20:08:11 +053051 return (pool_elts (v));
Ed Warnickecb9cada2015-12-08 15:45:58 -070052}
53
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070054/**
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 reddy1b0c9832017-11-29 20:08:11 +053063int
64pifi (void *p, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
66 return pool_is_free_index (p, index);
67}
68
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070069/**
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 reddy1b0c9832017-11-29 20:08:11 +053076void
77debug_hex_bytes (u8 * s, u32 n)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078{
79 fformat (stderr, "%U\n", format_hex_bytes, s, n);
80}
81
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070082/**
83 * @brief GDB callable function: vlib_dump_frame_ownership
84 *
85 */
sharath reddy1b0c9832017-11-29 20:08:11 +053086void
87vlib_dump_frame_ownership (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070088{
sharath reddy1b0c9832017-11-29 20:08:11 +053089 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 Warnickecb9cada2015-12-08 15:45:58 -070093 u32 first_nf_index;
94 u32 index;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070095
sharath reddy1b0c9832017-11-29 20:08:11 +053096 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 Warnickecb9cada2015-12-08 15:45:58 -070099
sharath reddy1b0c9832017-11-29 20:08:11 +0530100 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);
114 fformat (stderr, " nf index %d nf->frame_index %d\n",
115 nf - vm->node_main.next_frames, nf->frame_index);
116 }
117 }
118 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119}
120
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700121/**
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 reddy1b0c9832017-11-29 20:08:11 +0530128void
129vlib_runtime_index_to_node_name (u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130{
sharath reddy1b0c9832017-11-29 20:08:11 +0530131 vlib_main_t *vm = vlib_get_main ();
132 vlib_node_main_t *nm = &vm->node_main;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700133
Eyal Baricd307742018-07-22 12:45:15 +0300134 if (index >= vec_len (nm->nodes))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530136 fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 return;
138 }
139
sharath reddy1b0c9832017-11-29 20:08:11 +0530140 fformat (stderr, "node runtime index %d name %s\n", index,
141 nm->nodes[index]->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142}
143
sharath reddy1b0c9832017-11-29 20:08:11 +0530144void
145gdb_show_errors (int verbose)
Florin Coras66b11312017-07-31 17:18:03 -0700146{
147 extern vlib_cli_command_t vlib_cli_show_errors;
148 unformat_input_t input;
sharath reddy1b0c9832017-11-29 20:08:11 +0530149 vlib_main_t *vm = vlib_get_main ();
Florin Coras66b11312017-07-31 17:18:03 -0700150
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 reddy1b0c9832017-11-29 20:08:11 +0530155 else
Florin Coras66b11312017-07-31 17:18:03 -0700156 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530157 fformat (stderr, "verbose not 0 or 1\n");
Florin Coras66b11312017-07-31 17:18:03 -0700158 return;
159 }
160
sharath reddy1b0c9832017-11-29 20:08:11 +0530161 vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ );
Florin Coras66b11312017-07-31 17:18:03 -0700162 unformat_free (&input);
sharath reddy1b0c9832017-11-29 20:08:11 +0530163}
Florin Coras66b11312017-07-31 17:18:03 -0700164
sharath reddy1b0c9832017-11-29 20:08:11 +0530165void
166gdb_show_session (int verbose)
Florin Coras66b11312017-07-31 17:18:03 -0700167{
168 extern vlib_cli_command_t vlib_cli_show_session_command;
169 unformat_input_t input;
sharath reddy1b0c9832017-11-29 20:08:11 +0530170 vlib_main_t *vm = vlib_get_main ();
Florin Coras66b11312017-07-31 17:18:03 -0700171
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 reddy1b0c9832017-11-29 20:08:11 +0530178 else
Florin Coras66b11312017-07-31 17:18:03 -0700179 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530180 fformat (stderr, "verbose not 0 - 2\n");
Florin Coras66b11312017-07-31 17:18:03 -0700181 return;
182 }
183
sharath reddy1b0c9832017-11-29 20:08:11 +0530184 vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ );
Florin Coras66b11312017-07-31 17:18:03 -0700185 unformat_free (&input);
sharath reddy1b0c9832017-11-29 20:08:11 +0530186}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700188/**
189 * @brief GDB callable function: show_gdb_command_fn - show gdb
190 *
191 * Shows list of functions for VPP available in GDB
192 *
193 * @return error - clib_error_t
194 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195static clib_error_t *
196show_gdb_command_fn (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530197 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198{
199 vlib_cli_output (vm, "vl(p) returns vec_len(p)");
Dave Barach7bee7732017-10-18 18:48:11 -0400200 vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]");
201 vlib_cli_output (vm, "vb2(b) returns vnet_buffer2(b) [opaque2]");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202 vlib_cli_output (vm, "pe(p) returns pool_elts(p)");
203 vlib_cli_output (vm, "pifi(p, i) returns pool_is_free_index(p, i)");
Florin Coras66b11312017-07-31 17:18:03 -0700204 vlib_cli_output (vm, "gdb_show_errors(0|1) dumps error counters");
205 vlib_cli_output (vm, "gdb_show_session dumps session counters");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex");
207 vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says");
208 vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN");
209
210 return 0;
211}
212
sharath reddy1b0c9832017-11-29 20:08:11 +0530213/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = {
215 .path = "show gdb",
216 .short_help = "Describe functions which can be called from gdb",
217 .function = show_gdb_command_fn,
218};
sharath reddy1b0c9832017-11-29 20:08:11 +0530219/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
sharath reddy1b0c9832017-11-29 20:08:11 +0530221vnet_buffer_opaque_t *
222vb (void *vb_arg)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400223{
sharath reddy1b0c9832017-11-29 20:08:11 +0530224 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
225 vnet_buffer_opaque_t *rv;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400226
sharath reddy1b0c9832017-11-29 20:08:11 +0530227 rv = vnet_buffer (b);
228
229 return rv;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400230}
231
sharath reddy1b0c9832017-11-29 20:08:11 +0530232vnet_buffer_opaque2_t *
233vb2 (void *vb_arg)
Dave Barach7bee7732017-10-18 18:48:11 -0400234{
sharath reddy1b0c9832017-11-29 20:08:11 +0530235 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
236 vnet_buffer_opaque2_t *rv;
Dave Barach7bee7732017-10-18 18:48:11 -0400237
sharath reddy1b0c9832017-11-29 20:08:11 +0530238 rv = vnet_buffer2 (b);
239
240 return rv;
Dave Barach7bee7732017-10-18 18:48:11 -0400241}
242
243
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244/* Cafeteria plan, maybe you don't want these functions */
sharath reddy1b0c9832017-11-29 20:08:11 +0530245clib_error_t *
246gdb_func_init (vlib_main_t * vm)
247{
248 return 0;
249}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
251VLIB_INIT_FUNCTION (gdb_func_init);
sharath reddy1b0c9832017-11-29 20:08:11 +0530252
253/*
254 * fd.io coding-style-patch-verification: ON
255 *
256 * Local Variables:
257 * eval: (c-set-style "gnu")
258 * End:
259 */