blob: 15355572cb90d16f67ee848c25f17986b8902de4 [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 */
15/*
16 * trace.c: VLIB trace buffer.
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>
41#include <vlib/threads.h>
Jon Loeliger5c1e48c2020-10-15 14:41:36 -040042#include <vnet/classify/vnet_classify.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070043
Dave Barach11fb09e2020-08-06 12:10:09 -040044u8 *vnet_trace_placeholder;
Dave Barachf8b85862018-08-17 18:29:07 -040045
Ed Warnickecb9cada2015-12-08 15:45:58 -070046/* Helper function for nodes which only trace buffer data. */
47void
48vlib_trace_frame_buffers_only (vlib_main_t * vm,
49 vlib_node_runtime_t * node,
50 u32 * buffers,
51 uword n_buffers,
52 uword next_buffer_stride,
53 uword n_buffer_data_bytes_in_trace)
54{
Dave Barach9b8ffd92016-07-08 08:13:45 -040055 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
57 n_left = n_buffers;
58 from = buffers;
Dave Barach9b8ffd92016-07-08 08:13:45 -040059
Ed Warnickecb9cada2015-12-08 15:45:58 -070060 while (n_left >= 4)
61 {
62 u32 bi0, bi1;
Dave Barach9b8ffd92016-07-08 08:13:45 -040063 vlib_buffer_t *b0, *b1;
64 u8 *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
66 /* Prefetch next iteration. */
67 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
68 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
69
70 bi0 = from[0];
71 bi1 = from[1];
72
73 b0 = vlib_get_buffer (vm, bi0);
74 b1 = vlib_get_buffer (vm, bi1);
75
76 if (b0->flags & VLIB_BUFFER_IS_TRACED)
77 {
78 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
Dave Barach178cf492018-11-13 16:34:13 -050079 clib_memcpy_fast (t0, b0->data + b0->current_data,
80 n_buffer_data_bytes_in_trace);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 }
82 if (b1->flags & VLIB_BUFFER_IS_TRACED)
83 {
84 t1 = vlib_add_trace (vm, node, b1, n_buffer_data_bytes_in_trace);
Dave Barach178cf492018-11-13 16:34:13 -050085 clib_memcpy_fast (t1, b1->data + b1->current_data,
86 n_buffer_data_bytes_in_trace);
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 }
88 from += 2;
89 n_left -= 2;
90 }
91
92 while (n_left >= 1)
93 {
94 u32 bi0;
Dave Barach9b8ffd92016-07-08 08:13:45 -040095 vlib_buffer_t *b0;
96 u8 *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
98 bi0 = from[0];
99
100 b0 = vlib_get_buffer (vm, bi0);
101
102 if (b0->flags & VLIB_BUFFER_IS_TRACED)
103 {
104 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
Dave Barach178cf492018-11-13 16:34:13 -0500105 clib_memcpy_fast (t0, b0->data + b0->current_data,
106 n_buffer_data_bytes_in_trace);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 }
108 from += 1;
109 n_left -= 1;
110 }
111}
112
113/* Free up all trace buffer memory. */
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400114void
Bud Grise0bcc9d52016-02-02 14:23:29 -0500115clear_trace_buffer (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116{
117 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400118 vlib_trace_main_t *tm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100120 foreach_vlib_main ()
121 {
122 tm = &this_vlib_main->trace_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100124 tm->trace_enable = 0;
125 vec_free (tm->nodes);
126 }
Jon Loeligerc0b19542020-05-11 08:43:51 -0500127
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100128 foreach_vlib_main ()
129 {
130 tm = &this_vlib_main->trace_main;
Bud Grised56a6f52016-02-19 12:10:33 -0500131
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100132 for (i = 0; i < vec_len (tm->trace_buffer_pool); i++)
133 if (!pool_is_free_index (tm->trace_buffer_pool, i))
134 vec_free (tm->trace_buffer_pool[i]);
135 pool_free (tm->trace_buffer_pool);
136 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137}
138
Dave Barach1201a802018-11-20 12:08:39 -0500139u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400140format_vlib_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400142 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
143 vlib_trace_header_t *h = va_arg (*va, vlib_trace_header_t *);
144 vlib_trace_header_t *e = vec_end (h);
145 vlib_node_t *node, *prev_node;
146 clib_time_t *ct = &vm->clib_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 f64 t;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 prev_node = 0;
150 while (h < e)
151 {
152 node = vlib_get_node (vm, h->node_index);
153
154 if (node != prev_node)
155 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 t =
157 (h->time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
158 s =
159 format (s, "\n%U: %v", format_time_interval, "h:m:s:u", t,
160 node->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 }
162 prev_node = node;
163
164 if (node->format_trace)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 s = format (s, "\n %U", node->format_trace, vm, node, h->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 s = format (s, "\n %U", node->format_buffer, h->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
169 h = vlib_trace_header_next (h);
170 }
171
172 return s;
173}
174
175/* Root of all trace cli commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177VLIB_CLI_COMMAND (trace_cli_command,static) = {
178 .path = "trace",
179 .short_help = "Packet tracer commands",
180};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400181/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Jon Loeligerc0b19542020-05-11 08:43:51 -0500183int
184trace_time_cmp (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500185{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400186 vlib_trace_header_t **t1 = a1;
187 vlib_trace_header_t **t2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500188 i64 dt = t1[0]->time - t2[0]->time;
189 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
190}
191
Bud Grise0bcc9d52016-02-02 14:23:29 -0500192/*
193 * Return 1 if this packet passes the trace filter, or 0 otherwise
194 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195u32
196filter_accept (vlib_trace_main_t * tm, vlib_trace_header_t * h)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500197{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 vlib_trace_header_t *e = vec_end (h);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500199
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 if (tm->filter_flag == 0)
201 return 1;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500202
Dave Barach27d978c2020-11-03 09:59:06 -0500203 /*
204 * When capturing a post-mortem dispatch trace,
205 * toss all existing traces once per dispatch cycle.
206 * So we can trace 4 billion pkts without running out of
207 * memory...
208 */
209 if (tm->filter_flag == FILTER_FLAG_POST_MORTEM)
210 return 0;
211
Bud Grise0bcc9d52016-02-02 14:23:29 -0500212 if (tm->filter_flag == FILTER_FLAG_INCLUDE)
213 {
214 while (h < e)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215 {
216 if (h->node_index == tm->filter_node_index)
217 return 1;
218 h = vlib_trace_header_next (h);
219 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500220 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221 }
222 else /* FILTER_FLAG_EXCLUDE */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500223 {
224 while (h < e)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400225 {
226 if (h->node_index == tm->filter_node_index)
227 return 0;
228 h = vlib_trace_header_next (h);
229 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500230 return 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400231 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500232
233 return 0;
234}
235
236/*
237 * Remove traces from the trace buffer pool that don't pass the filter
238 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400239void
240trace_apply_filter (vlib_main_t * vm)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500241{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400242 vlib_trace_main_t *tm = &vm->trace_main;
243 vlib_trace_header_t **h;
244 vlib_trace_header_t ***traces_to_remove = 0;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500245 u32 index;
246 u32 trace_index;
247 u32 n_accepted;
248
249 u32 accept;
250
251 if (tm->filter_flag == FILTER_FLAG_NONE)
252 return;
253
254 /*
255 * Ideally we would retain the first N traces that pass the filter instead
256 * of any N traces.
257 */
258 n_accepted = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400259 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100260 pool_foreach (h, tm->trace_buffer_pool)
261 {
Bud Grise0bcc9d52016-02-02 14:23:29 -0500262 accept = filter_accept(tm, h[0]);
263
264 if ((n_accepted == tm->filter_count) || !accept)
265 vec_add1 (traces_to_remove, h);
266 else
267 n_accepted++;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100268 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 /* *INDENT-ON* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500270
271 /* remove all traces that we don't want to keep */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272 for (index = 0; index < vec_len (traces_to_remove); index++)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500273 {
274 trace_index = traces_to_remove[index] - tm->trace_buffer_pool;
Damjan Marion8bea5892022-04-04 22:40:45 +0200275 vec_set_len (tm->trace_buffer_pool[trace_index], 0);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500276 pool_put_index (tm->trace_buffer_pool, trace_index);
277 }
278
279 vec_free (traces_to_remove);
280}
281
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282static clib_error_t *
283cli_show_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400284 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400286 vlib_trace_main_t *tm;
287 vlib_trace_header_t **h, **traces;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 u32 i, index = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400289 char *fmt;
290 u8 *s = 0;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500291 u32 max;
292
293 /*
294 * By default display only this many traces. To display more, explicitly
295 * specify a max. This prevents unexpectedly huge outputs.
296 */
297 max = 50;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400298 while (unformat_check_input (input) != (uword) UNFORMAT_END_OF_INPUT)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500299 {
300 if (unformat (input, "max %d", &max))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400301 ;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500302 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400303 return clib_error_create ("expected 'max COUNT', got `%U'",
304 format_unformat_error, input);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500305 }
306
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
308 /* Get active traces from pool. */
309
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100310 foreach_vlib_main ()
311 {
312 fmt = "------------------- Start of thread %d %s -------------------\n";
313 s = format (s, fmt, index, vlib_worker_threads[index].name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100315 tm = &this_vlib_main->trace_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100317 trace_apply_filter (this_vlib_main);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500318
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100319 traces = 0;
320 pool_foreach (h, tm->trace_buffer_pool)
321 {
322 vec_add1 (traces, h[0]);
323 }
324
325 if (vec_len (traces) == 0)
326 {
327 s = format (s, "No packets in trace buffer\n");
328 goto done;
329 }
330
331 /* Sort them by increasing time. */
332 vec_sort_with_function (traces, trace_time_cmp);
333
334 for (i = 0; i < vec_len (traces); i++)
335 {
336 if (i == max)
337 {
338 char *warn = "Limiting display to %d packets."
339 " To display more specify max.";
340 vlib_cli_output (vm, warn, max);
341 s = format (s, warn, max);
342 goto done;
343 }
344
345 s = format (s, "Packet %d\n%U\n\n", i + 1, format_vlib_trace, vm,
346 traces[i]);
347 }
348
349 done:
350 vec_free (traces);
351
352 index++;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100353 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354
Klement Sekera29396e62016-12-21 03:24:00 +0100355 vlib_cli_output (vm, "%v", s);
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700356 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 return 0;
358}
359
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361VLIB_CLI_COMMAND (show_trace_cli,static) = {
362 .path = "show trace",
Bud Grise0bcc9d52016-02-02 14:23:29 -0500363 .short_help = "Show trace buffer [max COUNT]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364 .function = cli_show_trace_buffer,
365};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400366/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
Dave Barach87d24db2019-12-04 17:19:12 -0500368int vlib_enable_disable_pkt_trace_filter (int enable) __attribute__ ((weak));
Jon Loeligerc0b19542020-05-11 08:43:51 -0500369
Dave Barach87d24db2019-12-04 17:19:12 -0500370int
371vlib_enable_disable_pkt_trace_filter (int enable)
372{
373 return 0;
374}
375
Jon Loeligerc0b19542020-05-11 08:43:51 -0500376void
377vlib_trace_stop_and_clear (void)
378{
379 vlib_enable_disable_pkt_trace_filter (0); /* disble tracing */
380 clear_trace_buffer ();
381}
382
383
384void
385trace_update_capture_options (u32 add, u32 node_index, u32 filter, u8 verbose)
386{
387 vlib_trace_main_t *tm;
388 vlib_trace_node_t *tn;
389
390 if (add == ~0)
391 add = 50;
392
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100393 foreach_vlib_main ()
Jon Loeligerc0b19542020-05-11 08:43:51 -0500394 {
395 tm = &this_vlib_main->trace_main;
396 tm->verbose = verbose;
397 vec_validate (tm->nodes, node_index);
398 tn = tm->nodes + node_index;
399
400 /*
401 * Adding 0 makes no real sense, and there wa no other way
402 * to explicilty zero-out the limits and count, so make
403 * an "add 0" request really be "set to 0".
404 */
405 if (add == 0)
406 tn->limit = tn->count = 0;
407 else
408 tn->limit += add;
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100409 }
Jon Loeligerc0b19542020-05-11 08:43:51 -0500410
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100411 foreach_vlib_main ()
Jon Loeligerc0b19542020-05-11 08:43:51 -0500412 {
413 tm = &this_vlib_main->trace_main;
414 tm->trace_enable = 1;
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100415 }
Jon Loeliger5c1e48c2020-10-15 14:41:36 -0400416
417 vlib_enable_disable_pkt_trace_filter (! !filter);
Jon Loeligerc0b19542020-05-11 08:43:51 -0500418}
419
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420static clib_error_t *
421cli_add_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400422 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423{
Neale Ranns3ee44042016-10-03 13:05:48 +0100424 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200425 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426 u32 node_index, add;
Damjan Mariondb7b2692016-06-09 16:16:27 +0200427 u8 verbose = 0;
Dave Barach87d24db2019-12-04 17:19:12 -0500428 int filter = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500429 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
Neale Ranns3ee44042016-10-03 13:05:48 +0100431 if (!unformat_user (input, unformat_line_input, line_input))
432 return 0;
433
Dave Barach11fb09e2020-08-06 12:10:09 -0400434 if (vnet_trace_placeholder == 0)
435 vec_validate_aligned (vnet_trace_placeholder, 2048,
436 CLIB_CACHE_LINE_BYTES);
Dave Barachf8b85862018-08-17 18:29:07 -0400437
Neale Ranns3ee44042016-10-03 13:05:48 +0100438 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
Damjan Mariondb7b2692016-06-09 16:16:27 +0200439 {
Neale Ranns3ee44042016-10-03 13:05:48 +0100440 if (unformat (line_input, "%U %d",
441 unformat_vlib_node, vm, &node_index, &add))
Damjan Mariondb7b2692016-06-09 16:16:27 +0200442 ;
Neale Ranns3ee44042016-10-03 13:05:48 +0100443 else if (unformat (line_input, "verbose"))
Damjan Mariondb7b2692016-06-09 16:16:27 +0200444 verbose = 1;
Dave Barach87d24db2019-12-04 17:19:12 -0500445 else if (unformat (line_input, "filter"))
446 filter = 1;
Damjan Mariondb7b2692016-06-09 16:16:27 +0200447 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500448 {
449 error = clib_error_create ("expected NODE COUNT, got `%U'",
450 format_unformat_error, line_input);
451 goto done;
452 }
Damjan Mariondb7b2692016-06-09 16:16:27 +0200453 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200455 node = vlib_get_node (vm, node_index);
456
457 if ((node->flags & VLIB_NODE_FLAG_TRACE_SUPPORTED) == 0)
458 {
459 error = clib_error_create ("node '%U' doesn't support per-node "
460 "tracing. There may be another way to "
461 "initiate trace on this node.",
462 format_vlib_node_name, vm, node_index);
463 goto done;
464 }
465
Jon Loeligerc0b19542020-05-11 08:43:51 -0500466 trace_update_capture_options (add, node_index, filter, verbose);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467
Billy McFalla9a20e72017-02-15 11:39:12 -0500468done:
469 unformat_free (line_input);
470
471 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472}
473
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475VLIB_CLI_COMMAND (add_trace_cli,static) = {
476 .path = "trace add",
Jon Loeligerc0b19542020-05-11 08:43:51 -0500477 .short_help = "trace add <input-graph-node> <add'l-pkts-for-node-> [filter] [verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 .function = cli_add_trace_buffer,
479};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481
Bud Grise0bcc9d52016-02-02 14:23:29 -0500482/*
483 * Configure a filter for packet traces.
484 *
485 * This supplements the packet trace feature so that only packets matching
486 * the filter are included in the trace. Currently the only filter is to
487 * keep packets that include a certain node in the trace or exclude a certain
488 * node in the trace.
489 *
490 * The count of traced packets in the "trace add" command is still used to
491 * create a certain number of traces. The "trace filter" command specifies
492 * how many of those packets should be retained in the trace.
493 *
494 * For example, 1Mpps of traffic is arriving and one of those packets is being
495 * dropped. To capture the trace for only that dropped packet, you can do:
496 * trace filter include error-drop 1
497 * trace add dpdk-input 1000000
498 * <wait one second>
499 * show trace
500 *
501 * Note that the filter could be implemented by capturing all traces and just
502 * reducing traces displayed by the "show trace" function. But that would
503 * require a lot of memory for storing the traces, making that infeasible.
504 *
505 * To remove traces from the trace pool that do not include a certain node
506 * requires that the trace be "complete" before applying the filter. To
507 * accomplish this, the trace pool is filtered upon each iteraction of the
508 * main vlib loop. Doing so keeps the number of allocated traces down to a
509 * reasonably low number. This requires that tracing for a buffer is not
510 * performed after the vlib main loop interation completes. i.e. you can't
511 * save away a buffer temporarily then inject it back into the graph and
512 * expect that the trace_index is still valid (such as a traffic manager might
513 * do). A new trace buffer should be allocated for those types of packets.
514 *
515 * The filter can be extended to support multiple nodes and other match
516 * criteria (e.g. input sw_if_index, mac address) but for now just checks if
517 * a specified node is in the trace or not in the trace.
518 */
Jon Loeligerc0b19542020-05-11 08:43:51 -0500519
520void
521trace_filter_set (u32 node_index, u32 flag, u32 count)
522{
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100523 foreach_vlib_main ()
524 {
525 vlib_trace_main_t *tm;
Jon Loeligerc0b19542020-05-11 08:43:51 -0500526
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100527 tm = &this_vlib_main->trace_main;
528 tm->filter_node_index = node_index;
529 tm->filter_flag = flag;
530 tm->filter_count = count;
Jon Loeligerc0b19542020-05-11 08:43:51 -0500531
Damjan Marion92ccf9b2021-03-26 11:38:01 +0100532 /*
533 * Clear the trace limits to stop any in-progress tracing
534 * Prevents runaway trace allocations when the filter changes
535 * (or is removed)
536 */
537 vec_free (tm->nodes);
538 }
Jon Loeligerc0b19542020-05-11 08:43:51 -0500539}
540
541
Bud Grise0bcc9d52016-02-02 14:23:29 -0500542static clib_error_t *
543cli_filter_trace (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400544 unformat_input_t * input, vlib_cli_command_t * cmd)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500545{
Bud Grise0bcc9d52016-02-02 14:23:29 -0500546 u32 filter_node_index;
547 u32 filter_flag;
548 u32 filter_count;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500549
550 if (unformat (input, "include %U %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400551 unformat_vlib_node, vm, &filter_node_index, &filter_count))
Bud Grise0bcc9d52016-02-02 14:23:29 -0500552 {
553 filter_flag = FILTER_FLAG_INCLUDE;
554 }
555 else if (unformat (input, "exclude %U %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400556 unformat_vlib_node, vm, &filter_node_index,
557 &filter_count))
Bud Grise0bcc9d52016-02-02 14:23:29 -0500558 {
559 filter_flag = FILTER_FLAG_EXCLUDE;
560 }
561 else if (unformat (input, "none"))
562 {
563 filter_flag = FILTER_FLAG_NONE;
564 filter_node_index = 0;
565 filter_count = 0;
566 }
567 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400568 return
569 clib_error_create
570 ("expected 'include NODE COUNT' or 'exclude NODE COUNT' or 'none', got `%U'",
571 format_unformat_error, input);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500572
Jon Loeligerc0b19542020-05-11 08:43:51 -0500573 trace_filter_set (filter_node_index, filter_flag, filter_count);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500574
575 return 0;
576}
577
Dave Barach9b8ffd92016-07-08 08:13:45 -0400578/* *INDENT-OFF* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500579VLIB_CLI_COMMAND (filter_trace_cli,static) = {
580 .path = "trace filter",
Jon Loeligerc0b19542020-05-11 08:43:51 -0500581 .short_help = "trace filter none | [include|exclude] NODE COUNT",
Bud Grise0bcc9d52016-02-02 14:23:29 -0500582 .function = cli_filter_trace,
583};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400584/* *INDENT-ON* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500585
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586static clib_error_t *
587cli_clear_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589{
Jon Loeligerc0b19542020-05-11 08:43:51 -0500590 vlib_trace_stop_and_clear ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 return 0;
592}
593
Dave Barach9b8ffd92016-07-08 08:13:45 -0400594/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595VLIB_CLI_COMMAND (clear_trace_cli,static) = {
596 .path = "clear trace",
597 .short_help = "Clear trace buffer and free memory",
598 .function = cli_clear_trace_buffer,
599};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Dave Barach11fb09e2020-08-06 12:10:09 -0400602/* Placeholder function to get us linked in. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400603void
604vlib_trace_cli_reference (void)
605{
606}
607
Dave Barach0a67b482020-06-08 11:17:19 -0400608void *
609vlib_add_trace (vlib_main_t * vm,
610 vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
611{
612 return vlib_add_trace_inline (vm, r, b, n_data_bytes);
613}
614
Mohammed Hawari52fa5f22023-05-26 14:52:50 +0200615vlib_is_packet_traced_fn_t *
616vlib_is_packet_traced_function_from_name (const char *name)
617{
618 vlib_trace_filter_function_registration_t *reg =
619 vlib_trace_filter_main.trace_filter_registration;
620 while (reg)
621 {
622 if (clib_strcmp (reg->name, name) == 0)
623 break;
624 reg = reg->next;
625 }
626 if (!reg)
627 return 0;
628 return reg->function;
629}
Dave Barach0a67b482020-06-08 11:17:19 -0400630
Maxime Peim3f407552023-07-03 17:45:51 +0200631vlib_is_packet_traced_fn_t *
Mohammed Hawari52fa5f22023-05-26 14:52:50 +0200632vlib_is_packet_traced_default_function ()
633{
634 vlib_trace_filter_function_registration_t *reg =
635 vlib_trace_filter_main.trace_filter_registration;
636 vlib_trace_filter_function_registration_t *tmp_reg = reg;
637 while (reg)
638 {
639 if (reg->priority > tmp_reg->priority)
640 tmp_reg = reg;
641 reg = reg->next;
642 }
643 return tmp_reg->function;
644}
Dave Barach0a67b482020-06-08 11:17:19 -0400645
Mohammed Hawari52fa5f22023-05-26 14:52:50 +0200646static clib_error_t *
647vlib_trace_filter_function_init (vlib_main_t *vm)
648{
649 vlib_is_packet_traced_fn_t *default_fn =
650 vlib_is_packet_traced_default_function ();
651 foreach_vlib_main ()
652 {
653 vlib_trace_main_t *tm = &this_vlib_main->trace_main;
654 tm->current_trace_filter_function = default_fn;
655 }
656 return 0;
657}
658
659vlib_trace_filter_main_t vlib_trace_filter_main;
660
661VLIB_INIT_FUNCTION (vlib_trace_filter_function_init);
662
663static clib_error_t *
664show_trace_filter_function (vlib_main_t *vm, unformat_input_t *input,
665 vlib_cli_command_t *cmd)
666{
667 vlib_trace_filter_main_t *tfm = &vlib_trace_filter_main;
668 vlib_trace_main_t *tm = &vm->trace_main;
669 vlib_is_packet_traced_fn_t *current_trace_filter_fn =
670 tm->current_trace_filter_function;
671 vlib_trace_filter_function_registration_t *reg =
672 tfm->trace_filter_registration;
673
674 while (reg)
675 {
676 vlib_cli_output (vm, "%sname:%s description: %s priority: %u",
677 reg->function == current_trace_filter_fn ? "(*) " : "",
678 reg->name, reg->description, reg->priority);
679 reg = reg->next;
680 }
681 return 0;
682}
683
684VLIB_CLI_COMMAND (show_trace_filter_function_cli, static) = {
685 .path = "show trace filter function",
686 .short_help = "show trace filter function",
687 .function = show_trace_filter_function,
688};
689
690uword
691unformat_vlib_trace_filter_function (unformat_input_t *input, va_list *args)
692{
693 vlib_is_packet_traced_fn_t **res =
694 va_arg (*args, vlib_is_packet_traced_fn_t **);
695 vlib_trace_filter_main_t *tfm = &vlib_trace_filter_main;
696
697 vlib_trace_filter_function_registration_t *reg =
698 tfm->trace_filter_registration;
699 while (reg)
700 {
701 if (unformat (input, reg->name))
702 {
703 *res = reg->function;
704 return 1;
705 }
706 reg = reg->next;
707 }
708 return 0;
709}
710
711void
712vlib_set_trace_filter_function (vlib_is_packet_traced_fn_t *x)
713{
714 foreach_vlib_main ()
715 {
716 this_vlib_main->trace_main.current_trace_filter_function = x;
717 }
718}
719
720static clib_error_t *
721set_trace_filter_function (vlib_main_t *vm, unformat_input_t *input,
722 vlib_cli_command_t *cmd)
723{
724 unformat_input_t _line_input, *line_input = &_line_input;
725 vlib_is_packet_traced_fn_t *res = 0;
726 clib_error_t *error = 0;
727
728 if (!unformat_user (input, unformat_line_input, line_input))
729 return 0;
730
731 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
732 {
733 if (unformat (line_input, "%U", unformat_vlib_trace_filter_function,
734 &res))
735 ;
736 else
737 {
738 error = clib_error_create (
739 "expected valid trace filter function, got `%U'",
740 format_unformat_error, line_input);
741 goto done;
742 }
743 }
744 vlib_set_trace_filter_function (res);
745
746done:
747 unformat_free (line_input);
748
749 return error;
750}
751
752VLIB_CLI_COMMAND (set_trace_filter_function_cli, static) = {
753 .path = "set trace filter function",
754 .short_help = "set trace filter function <func_name>",
755 .function = set_trace_filter_function,
756};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400757/*
758 * fd.io coding-style-patch-verification: ON
759 *
760 * Local Variables:
761 * eval: (c-set-style "gnu")
762 * End:
763 */