blob: a89b72024005aaf9b9131a87fa25243016de4e94 [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/**
Dave Baracha690fdb2020-01-21 12:34:55 -050041 * @brief GDB callable function: pvh - Return vector header of vector
42 *
43 * @param *p - void - address of vector
44 *
45 * @return vh - vec_header_t, the vector header
46 *
47 */
48vec_header_t *
49pvh (void *p)
50{
51 return _vec_find (p);
52}
53
54
55/**
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070056 * @brief GDB callable function: pe - call pool_elts - number of elements in a pool
57 *
58 * @param *v - void - address of pool
59 *
60 * @return number - uword
61 *
62 */
sharath reddy1b0c9832017-11-29 20:08:11 +053063uword
64pe (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
sharath reddy1b0c9832017-11-29 20:08:11 +053066 return (pool_elts (v));
Ed Warnickecb9cada2015-12-08 15:45:58 -070067}
68
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070069/**
Christian Hoppsb71653e2020-07-13 11:57:27 -040070 * @brief GDB callable function: ph - call pool_header - get pool header.
71 *
72 * @param *p - void - address of pool
73 *
74 * @return pool_header_t
75 *
76 */
77pool_header_t *
78ph (void *p)
79{
80 return pool_header (p);
81}
82
83/**
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070084 * @brief GDB callable function: pifi - call pool_is_free_index - is passed index free?
85 *
86 * @param *p - void - address of pool
87 * @param *index - u32
88 *
89 * @return 0|1 - int
90 *
91 */
sharath reddy1b0c9832017-11-29 20:08:11 +053092int
93pifi (void *p, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
95 return pool_is_free_index (p, index);
96}
97
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070098/**
99 * @brief GDB callable function: debug_hex_bytes - return formatted hex string
100 *
101 * @param *s - u8
102 * @param n - u32 - number of bytes to format
103 *
104 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530105void
106debug_hex_bytes (u8 * s, u32 n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107{
108 fformat (stderr, "%U\n", format_hex_bytes, s, n);
109}
110
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700111/**
112 * @brief GDB callable function: vlib_dump_frame_ownership
113 *
114 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530115void
116vlib_dump_frame_ownership (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117{
sharath reddy1b0c9832017-11-29 20:08:11 +0530118 vlib_main_t *vm = vlib_get_main ();
119 vlib_node_main_t *nm = &vm->node_main;
120 vlib_node_runtime_t *this_node_runtime;
121 vlib_next_frame_t *nf;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 u32 first_nf_index;
123 u32 index;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700124
sharath reddy1b0c9832017-11-29 20:08:11 +0530125 vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL])
126 {
127 first_nf_index = this_node_runtime->next_frame_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
sharath reddy1b0c9832017-11-29 20:08:11 +0530129 for (index = first_nf_index; index < first_nf_index +
130 this_node_runtime->n_next_nodes; index++)
131 {
132 vlib_node_runtime_t *owned_runtime;
133 nf = vec_elt_at_index (vm->node_main.next_frames, index);
134 if (nf->flags & VLIB_FRAME_OWNER)
135 {
136 owned_runtime = vec_elt_at_index (nm->nodes_by_type[0],
137 nf->node_runtime_index);
138 fformat (stderr,
139 "%s next index %d owns enqueue rights to %s\n",
140 nm->nodes[this_node_runtime->node_index]->name,
141 index - first_nf_index,
142 nm->nodes[owned_runtime->node_index]->name);
Andreas Schultz58b2eb12019-07-15 15:40:56 +0200143 fformat (stderr, " nf index %d nf->frame %p\n",
144 nf - vm->node_main.next_frames, nf->frame);
sharath reddy1b0c9832017-11-29 20:08:11 +0530145 }
146 }
147 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148}
149
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700150/**
151 * @brief GDB callable function: vlib_runtime_index_to_node_name
152 *
153 * Takes node index and will return the node name.
154 *
155 * @param index - u32
156 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530157void
158vlib_runtime_index_to_node_name (u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159{
sharath reddy1b0c9832017-11-29 20:08:11 +0530160 vlib_main_t *vm = vlib_get_main ();
161 vlib_node_main_t *nm = &vm->node_main;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700162
Eyal Baricd307742018-07-22 12:45:15 +0300163 if (index >= vec_len (nm->nodes))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530165 fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 return;
167 }
168
sharath reddy1b0c9832017-11-29 20:08:11 +0530169 fformat (stderr, "node runtime index %d name %s\n", index,
170 nm->nodes[index]->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171}
172
sharath reddy1b0c9832017-11-29 20:08:11 +0530173void
174gdb_show_errors (int verbose)
Florin Coras66b11312017-07-31 17:18:03 -0700175{
176 extern vlib_cli_command_t vlib_cli_show_errors;
177 unformat_input_t input;
sharath reddy1b0c9832017-11-29 20:08:11 +0530178 vlib_main_t *vm = vlib_get_main ();
Florin Coras66b11312017-07-31 17:18:03 -0700179
180 if (verbose == 0)
181 unformat_init_string (&input, "verbose 0", 9);
182 else if (verbose == 1)
183 unformat_init_string (&input, "verbose 1", 9);
sharath reddy1b0c9832017-11-29 20:08:11 +0530184 else
Florin Coras66b11312017-07-31 17:18:03 -0700185 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530186 fformat (stderr, "verbose not 0 or 1\n");
Florin Coras66b11312017-07-31 17:18:03 -0700187 return;
188 }
189
sharath reddy1b0c9832017-11-29 20:08:11 +0530190 vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ );
Florin Coras66b11312017-07-31 17:18:03 -0700191 unformat_free (&input);
sharath reddy1b0c9832017-11-29 20:08:11 +0530192}
Florin Coras66b11312017-07-31 17:18:03 -0700193
sharath reddy1b0c9832017-11-29 20:08:11 +0530194void
195gdb_show_session (int verbose)
Florin Coras66b11312017-07-31 17:18:03 -0700196{
197 extern vlib_cli_command_t vlib_cli_show_session_command;
198 unformat_input_t input;
sharath reddy1b0c9832017-11-29 20:08:11 +0530199 vlib_main_t *vm = vlib_get_main ();
Florin Coras66b11312017-07-31 17:18:03 -0700200
201 if (verbose == 0)
202 unformat_init_string (&input, "verbose 0", 9);
203 else if (verbose == 1)
204 unformat_init_string (&input, "verbose 1", 9);
205 else if (verbose == 2)
206 unformat_init_string (&input, "verbose 2", 9);
sharath reddy1b0c9832017-11-29 20:08:11 +0530207 else
Florin Coras66b11312017-07-31 17:18:03 -0700208 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530209 fformat (stderr, "verbose not 0 - 2\n");
Florin Coras66b11312017-07-31 17:18:03 -0700210 return;
211 }
212
sharath reddy1b0c9832017-11-29 20:08:11 +0530213 vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ );
Florin Coras66b11312017-07-31 17:18:03 -0700214 unformat_free (&input);
sharath reddy1b0c9832017-11-29 20:08:11 +0530215}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Kingwel Xie35439082019-03-05 05:06:48 -0500217static int
218trace_cmp (void *a1, void *a2)
219{
220 vlib_trace_header_t **t1 = a1;
221 vlib_trace_header_t **t2 = a2;
222 i64 dt = t1[0]->time - t2[0]->time;
223 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
224}
225
226void
227gdb_show_traces ()
228{
229 vlib_trace_main_t *tm;
230 vlib_trace_header_t **h, **traces;
231 u32 i, index = 0;
232 char *fmt;
233 u8 *s = 0;
234 u32 max;
235
236 /* By default display only this many traces. */
237 max = 50;
238
239 /* Get active traces from pool. */
240
Benoît Gannefa500e92024-06-06 10:46:50 +0200241 foreach_vlib_main__ (0 /* no checks */)
242 {
243 fmt = "------------------- Start of thread %d %s -------------------\n";
244 s = format (s, fmt, index, vlib_worker_threads[index].name);
Kingwel Xie35439082019-03-05 05:06:48 -0500245
Benoît Gannefa500e92024-06-06 10:46:50 +0200246 tm = &this_vlib_main->trace_main;
Kingwel Xie35439082019-03-05 05:06:48 -0500247
Benoît Gannefa500e92024-06-06 10:46:50 +0200248 trace_apply_filter (this_vlib_main);
Kingwel Xie35439082019-03-05 05:06:48 -0500249
Benoît Gannefa500e92024-06-06 10:46:50 +0200250 traces = 0;
251 pool_foreach (h, tm->trace_buffer_pool)
252 {
253 vec_add1 (traces, h[0]);
254 }
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100255
Benoît Gannefa500e92024-06-06 10:46:50 +0200256 if (vec_len (traces) == 0)
257 {
258 s = format (s, "No packets in trace buffer\n");
259 goto done;
260 }
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100261
Benoît Gannefa500e92024-06-06 10:46:50 +0200262 /* Sort them by increasing time. */
263 vec_sort_with_function (traces, trace_cmp);
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100264
Benoît Gannefa500e92024-06-06 10:46:50 +0200265 for (i = 0; i < vec_len (traces); i++)
266 {
267 if (i == max)
268 {
269 fformat (stderr,
270 "Limiting display to %d packets."
271 " To display more specify max.",
272 max);
273 goto done;
274 }
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100275
Benoît Gannefa500e92024-06-06 10:46:50 +0200276 s = format (s, "Packet %d\n%U\n\n", i + 1, format_vlib_trace,
277 vlib_get_first_main (), traces[i]);
278 }
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100279
280 done:
281 vec_free (traces);
282
283 index++;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100284 }
Kingwel Xie35439082019-03-05 05:06:48 -0500285
Kingwel Xie35439082019-03-05 05:06:48 -0500286 fformat (stderr, "%v", s);
287 vec_free (s);
288}
289
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700290/**
291 * @brief GDB callable function: show_gdb_command_fn - show gdb
292 *
293 * Shows list of functions for VPP available in GDB
294 *
295 * @return error - clib_error_t
296 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297static clib_error_t *
298show_gdb_command_fn (vlib_main_t * vm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530299 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300{
301 vlib_cli_output (vm, "vl(p) returns vec_len(p)");
Dave Barach7bee7732017-10-18 18:48:11 -0400302 vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]");
303 vlib_cli_output (vm, "vb2(b) returns vnet_buffer2(b) [opaque2]");
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100304 vlib_cli_output (vm, "vbi(b) returns b index");
Christian Hoppsb71653e2020-07-13 11:57:27 -0400305 vlib_cli_output (vm,
306 "vgb(bi) returns vlib_get_buffer(vlib_get_main(), bi)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307 vlib_cli_output (vm, "pe(p) returns pool_elts(p)");
Christian Hoppsb71653e2020-07-13 11:57:27 -0400308 vlib_cli_output (vm, "ph(p) returns pool_header(p)");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 vlib_cli_output (vm, "pifi(p, i) returns pool_is_free_index(p, i)");
Florin Coras66b11312017-07-31 17:18:03 -0700310 vlib_cli_output (vm, "gdb_show_errors(0|1) dumps error counters");
311 vlib_cli_output (vm, "gdb_show_session dumps session counters");
Kingwel Xie35439082019-03-05 05:06:48 -0500312 vlib_cli_output (vm, "gdb_show_traces() dumps buffer traces");
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100313 vlib_cli_output (vm, "gdb_validate_buffer(b) check vlib_buffer b sanity");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 vlib_cli_output (vm, "debug_hex_bytes (ptr, n_bytes) dumps n_bytes in hex");
315 vlib_cli_output (vm, "vlib_dump_frame_ownership() does what it says");
316 vlib_cli_output (vm, "vlib_runtime_index_to_node_name (index) prints NN");
317
318 return 0;
319}
320
321VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = {
322 .path = "show gdb",
323 .short_help = "Describe functions which can be called from gdb",
324 .function = show_gdb_command_fn,
325};
326
Christian Hoppsb71653e2020-07-13 11:57:27 -0400327vlib_buffer_t *
328vgb (u32 bi)
329{
330 return vlib_get_buffer (vlib_get_main (), bi);
331}
332
sharath reddy1b0c9832017-11-29 20:08:11 +0530333vnet_buffer_opaque_t *
334vb (void *vb_arg)
Dave Barachacd2a6a2017-05-16 17:41:34 -0400335{
sharath reddy1b0c9832017-11-29 20:08:11 +0530336 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
337 vnet_buffer_opaque_t *rv;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400338
sharath reddy1b0c9832017-11-29 20:08:11 +0530339 rv = vnet_buffer (b);
340
341 return rv;
Dave Barachacd2a6a2017-05-16 17:41:34 -0400342}
343
sharath reddy1b0c9832017-11-29 20:08:11 +0530344vnet_buffer_opaque2_t *
345vb2 (void *vb_arg)
Dave Barach7bee7732017-10-18 18:48:11 -0400346{
sharath reddy1b0c9832017-11-29 20:08:11 +0530347 vlib_buffer_t *b = (vlib_buffer_t *) vb_arg;
348 vnet_buffer_opaque2_t *rv;
Dave Barach7bee7732017-10-18 18:48:11 -0400349
sharath reddy1b0c9832017-11-29 20:08:11 +0530350 rv = vnet_buffer2 (b);
351
352 return rv;
Dave Barach7bee7732017-10-18 18:48:11 -0400353}
354
Benoît Ganne2b65f9c2019-11-21 16:53:31 +0100355u32
356vbi (vlib_buffer_t * b)
357{
358 vlib_main_t *vm = vlib_get_main ();
359 vlib_buffer_main_t *bm = vm->buffer_main;
360 u32 bi = pointer_to_uword (b) - bm->buffer_mem_start;
361 bi >>= CLIB_LOG2_CACHE_LINE_BYTES;
362 return bi;
363}
364
365int
366gdb_validate_buffer (vlib_buffer_t * b)
367{
368 vlib_main_t *vm = vlib_get_main ();
369 u32 bi = vbi (b);
370 u8 *s =
371 vlib_validate_buffers (vm, &bi, 0, 1, VLIB_BUFFER_KNOWN_ALLOCATED, 1);
372 if (s)
373 {
374 fformat (stderr, "gdb_validate_buffer(): %v", s);
375 return -1;
376 }
377 fformat (stderr, "gdb_validate_buffer(): no error found\n");
378 return 0;
379}
Dave Barach7bee7732017-10-18 18:48:11 -0400380
Benoît Gannef89bbbe2021-03-04 14:31:03 +0100381/**
382 * Dump a trajectory trace, reasonably easy to call from gdb
383 */
384void
385gdb_dump_trajectory_trace (u32 bi)
386{
387#if VLIB_BUFFER_TRACE_TRAJECTORY > 0
388 vlib_main_t *vm = vlib_get_main ();
389 vlib_node_main_t *vnm = &vm->node_main;
390 vlib_buffer_t *b;
391 u16 *trace;
392 u8 i;
393
394 b = vlib_get_buffer (vm, bi);
395
396 trace = b->trajectory_trace;
397
398 fformat (stderr, "Context trace for bi %d b 0x%llx, visited %d\n", bi, b,
399 b->trajectory_nb);
400
401 for (i = 0; i < b->trajectory_nb; i++)
402 {
403 u32 node_index;
404
405 node_index = trace[i];
406
407 if (node_index >= vec_len (vnm->nodes))
408 {
409 fformat (stderr, "Skip bogus node index %d\n", node_index);
410 continue;
411 }
412
413 fformat (stderr, "%v (%d)\n", vnm->nodes[node_index]->name, node_index);
414 }
415#else
416 fformat (stderr, "in vlib/buffers.h, "
417 "#define VLIB_BUFFER_TRACE_TRAJECTORY 1\n");
418
419#endif
420}
421
Benoît Ganne03f2a012021-07-20 16:49:13 +0200422void
423gdb_dump_buffer (vlib_buffer_t *b)
424{
425 fformat (stderr, "%U\n", format_vnet_buffer, b);
426}
427
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428/* Cafeteria plan, maybe you don't want these functions */
sharath reddy1b0c9832017-11-29 20:08:11 +0530429clib_error_t *
430gdb_func_init (vlib_main_t * vm)
431{
432 return 0;
433}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
435VLIB_INIT_FUNCTION (gdb_func_init);
sharath reddy1b0c9832017-11-29 20:08:11 +0530436
437/*
438 * fd.io coding-style-patch-verification: ON
439 *
440 * Local Variables:
441 * eval: (c-set-style "gnu")
442 * End:
443 */