blob: abd116622c719567f328aff2f2b1b4a743d21f70 [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>
42
Dave Barach11fb09e2020-08-06 12:10:09 -040043u8 *vnet_trace_placeholder;
Dave Barachf8b85862018-08-17 18:29:07 -040044
Ed Warnickecb9cada2015-12-08 15:45:58 -070045/* Helper function for nodes which only trace buffer data. */
46void
47vlib_trace_frame_buffers_only (vlib_main_t * vm,
48 vlib_node_runtime_t * node,
49 u32 * buffers,
50 uword n_buffers,
51 uword next_buffer_stride,
52 uword n_buffer_data_bytes_in_trace)
53{
Dave Barach9b8ffd92016-07-08 08:13:45 -040054 u32 n_left, *from;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
56 n_left = n_buffers;
57 from = buffers;
Dave Barach9b8ffd92016-07-08 08:13:45 -040058
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 while (n_left >= 4)
60 {
61 u32 bi0, bi1;
Dave Barach9b8ffd92016-07-08 08:13:45 -040062 vlib_buffer_t *b0, *b1;
63 u8 *t0, *t1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
65 /* Prefetch next iteration. */
66 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
67 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
68
69 bi0 = from[0];
70 bi1 = from[1];
71
72 b0 = vlib_get_buffer (vm, bi0);
73 b1 = vlib_get_buffer (vm, bi1);
74
75 if (b0->flags & VLIB_BUFFER_IS_TRACED)
76 {
77 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
Dave Barach178cf492018-11-13 16:34:13 -050078 clib_memcpy_fast (t0, b0->data + b0->current_data,
79 n_buffer_data_bytes_in_trace);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 }
81 if (b1->flags & VLIB_BUFFER_IS_TRACED)
82 {
83 t1 = vlib_add_trace (vm, node, b1, n_buffer_data_bytes_in_trace);
Dave Barach178cf492018-11-13 16:34:13 -050084 clib_memcpy_fast (t1, b1->data + b1->current_data,
85 n_buffer_data_bytes_in_trace);
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 }
87 from += 2;
88 n_left -= 2;
89 }
90
91 while (n_left >= 1)
92 {
93 u32 bi0;
Dave Barach9b8ffd92016-07-08 08:13:45 -040094 vlib_buffer_t *b0;
95 u8 *t0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 bi0 = from[0];
98
99 b0 = vlib_get_buffer (vm, bi0);
100
101 if (b0->flags & VLIB_BUFFER_IS_TRACED)
102 {
103 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
Dave Barach178cf492018-11-13 16:34:13 -0500104 clib_memcpy_fast (t0, b0->data + b0->current_data,
105 n_buffer_data_bytes_in_trace);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 }
107 from += 1;
108 n_left -= 1;
109 }
110}
111
112/* Free up all trace buffer memory. */
113always_inline void
Bud Grise0bcc9d52016-02-02 14:23:29 -0500114clear_trace_buffer (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
116 int i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 vlib_trace_main_t *tm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Dave Barach9b8ffd92016-07-08 08:13:45 -0400119 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 foreach_vlib_main (
121 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 tm = &this_vlib_main->trace_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
Dave Barachf8b85862018-08-17 18:29:07 -0400124 tm->trace_enable = 0;
Jon Loeligerc0b19542020-05-11 08:43:51 -0500125 vec_free (tm->nodes);
126 }));
127
128 foreach_vlib_main (
129 ({
130 tm = &this_vlib_main->trace_main;
Bud Grised56a6f52016-02-19 12:10:33 -0500131
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138}
139
Dave Barach1201a802018-11-20 12:08:39 -0500140u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141format_vlib_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400143 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
144 vlib_trace_header_t *h = va_arg (*va, vlib_trace_header_t *);
145 vlib_trace_header_t *e = vec_end (h);
146 vlib_node_t *node, *prev_node;
147 clib_time_t *ct = &vm->clib_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 f64 t;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 prev_node = 0;
151 while (h < e)
152 {
153 node = vlib_get_node (vm, h->node_index);
154
155 if (node != prev_node)
156 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157 t =
158 (h->time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
159 s =
160 format (s, "\n%U: %v", format_time_interval, "h:m:s:u", t,
161 node->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 }
163 prev_node = node;
164
165 if (node->format_trace)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400166 s = format (s, "\n %U", node->format_trace, vm, node, h->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400168 s = format (s, "\n %U", node->format_buffer, h->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
170 h = vlib_trace_header_next (h);
171 }
172
173 return s;
174}
175
176/* Root of all trace cli commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400177/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178VLIB_CLI_COMMAND (trace_cli_command,static) = {
179 .path = "trace",
180 .short_help = "Packet tracer commands",
181};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Jon Loeligerc0b19542020-05-11 08:43:51 -0500184int
185trace_time_cmp (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500186{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400187 vlib_trace_header_t **t1 = a1;
188 vlib_trace_header_t **t2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500189 i64 dt = t1[0]->time - t2[0]->time;
190 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
191}
192
Bud Grise0bcc9d52016-02-02 14:23:29 -0500193/*
194 * Return 1 if this packet passes the trace filter, or 0 otherwise
195 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196u32
197filter_accept (vlib_trace_main_t * tm, vlib_trace_header_t * h)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500198{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400199 vlib_trace_header_t *e = vec_end (h);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500200
Dave Barach9b8ffd92016-07-08 08:13:45 -0400201 if (tm->filter_flag == 0)
202 return 1;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500203
204 if (tm->filter_flag == FILTER_FLAG_INCLUDE)
205 {
206 while (h < e)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207 {
208 if (h->node_index == tm->filter_node_index)
209 return 1;
210 h = vlib_trace_header_next (h);
211 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500212 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400213 }
214 else /* FILTER_FLAG_EXCLUDE */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500215 {
216 while (h < e)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400217 {
218 if (h->node_index == tm->filter_node_index)
219 return 0;
220 h = vlib_trace_header_next (h);
221 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500222 return 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400223 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500224
225 return 0;
226}
227
228/*
229 * Remove traces from the trace buffer pool that don't pass the filter
230 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400231void
232trace_apply_filter (vlib_main_t * vm)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500233{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400234 vlib_trace_main_t *tm = &vm->trace_main;
235 vlib_trace_header_t **h;
236 vlib_trace_header_t ***traces_to_remove = 0;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500237 u32 index;
238 u32 trace_index;
239 u32 n_accepted;
240
241 u32 accept;
242
243 if (tm->filter_flag == FILTER_FLAG_NONE)
244 return;
245
246 /*
247 * Ideally we would retain the first N traces that pass the filter instead
248 * of any N traces.
249 */
250 n_accepted = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251 /* *INDENT-OFF* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500252 pool_foreach (h, tm->trace_buffer_pool,
253 ({
254 accept = filter_accept(tm, h[0]);
255
256 if ((n_accepted == tm->filter_count) || !accept)
257 vec_add1 (traces_to_remove, h);
258 else
259 n_accepted++;
260 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400261 /* *INDENT-ON* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500262
263 /* remove all traces that we don't want to keep */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400264 for (index = 0; index < vec_len (traces_to_remove); index++)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500265 {
266 trace_index = traces_to_remove[index] - tm->trace_buffer_pool;
267 _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
268 pool_put_index (tm->trace_buffer_pool, trace_index);
269 }
270
271 vec_free (traces_to_remove);
272}
273
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274static clib_error_t *
275cli_show_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400276 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400278 vlib_trace_main_t *tm;
279 vlib_trace_header_t **h, **traces;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 u32 i, index = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400281 char *fmt;
282 u8 *s = 0;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500283 u32 max;
284
285 /*
286 * By default display only this many traces. To display more, explicitly
287 * specify a max. This prevents unexpectedly huge outputs.
288 */
289 max = 50;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400290 while (unformat_check_input (input) != (uword) UNFORMAT_END_OF_INPUT)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500291 {
292 if (unformat (input, "max %d", &max))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400293 ;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500294 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400295 return clib_error_create ("expected 'max COUNT', got `%U'",
296 format_unformat_error, input);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500297 }
298
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
300 /* Get active traces from pool. */
301
Dave Barach9b8ffd92016-07-08 08:13:45 -0400302 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 foreach_vlib_main (
304 ({
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700305 fmt = "------------------- Start of thread %d %s -------------------\n";
306 s = format (s, fmt, index, vlib_worker_threads[index].name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
308 tm = &this_vlib_main->trace_main;
309
Bud Grise0bcc9d52016-02-02 14:23:29 -0500310 trace_apply_filter(this_vlib_main);
311
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 traces = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313 pool_foreach (h, tm->trace_buffer_pool,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 ({
315 vec_add1 (traces, h[0]);
316 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 if (vec_len (traces) == 0)
319 {
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700320 s = format (s, "No packets in trace buffer\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 goto done;
322 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400323
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 /* Sort them by increasing time. */
Jon Loeligerc0b19542020-05-11 08:43:51 -0500325 vec_sort_with_function (traces, trace_time_cmp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400326
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 for (i = 0; i < vec_len (traces); i++)
328 {
Bud Grise0bcc9d52016-02-02 14:23:29 -0500329 if (i == max)
330 {
331 vlib_cli_output (vm, "Limiting display to %d packets."
332 " To display more specify max.", max);
333 goto done;
334 }
335
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700336 s = format (s, "Packet %d\n%U\n\n", i + 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 format_vlib_trace, vm, traces[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400339
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 done:
341 vec_free (traces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
343 index++;
344 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400345 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Klement Sekera29396e62016-12-21 03:24:00 +0100347 vlib_cli_output (vm, "%v", s);
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700348 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349 return 0;
350}
351
Dave Barach9b8ffd92016-07-08 08:13:45 -0400352/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353VLIB_CLI_COMMAND (show_trace_cli,static) = {
354 .path = "show trace",
Bud Grise0bcc9d52016-02-02 14:23:29 -0500355 .short_help = "Show trace buffer [max COUNT]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 .function = cli_show_trace_buffer,
357};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Dave Barach87d24db2019-12-04 17:19:12 -0500360int vlib_enable_disable_pkt_trace_filter (int enable) __attribute__ ((weak));
Jon Loeligerc0b19542020-05-11 08:43:51 -0500361
Dave Barach87d24db2019-12-04 17:19:12 -0500362int
363vlib_enable_disable_pkt_trace_filter (int enable)
364{
365 return 0;
366}
367
Jon Loeligerc0b19542020-05-11 08:43:51 -0500368void
369vlib_trace_stop_and_clear (void)
370{
371 vlib_enable_disable_pkt_trace_filter (0); /* disble tracing */
372 clear_trace_buffer ();
373}
374
375
376void
377trace_update_capture_options (u32 add, u32 node_index, u32 filter, u8 verbose)
378{
379 vlib_trace_main_t *tm;
380 vlib_trace_node_t *tn;
381
382 if (add == ~0)
383 add = 50;
384
385 /* *INDENT-OFF* */
386 foreach_vlib_main ((
387 {
388 tm = &this_vlib_main->trace_main;
389 tm->verbose = verbose;
390 vec_validate (tm->nodes, node_index);
391 tn = tm->nodes + node_index;
392
393 /*
394 * Adding 0 makes no real sense, and there wa no other way
395 * to explicilty zero-out the limits and count, so make
396 * an "add 0" request really be "set to 0".
397 */
398 if (add == 0)
399 tn->limit = tn->count = 0;
400 else
401 tn->limit += add;
402 }));
403
404 foreach_vlib_main ((
405 {
406 tm = &this_vlib_main->trace_main;
407 tm->trace_enable = 1;
408 }));
409 /* *INDENT-ON* */
410}
411
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412static clib_error_t *
413cli_add_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400414 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415{
Neale Ranns3ee44042016-10-03 13:05:48 +0100416 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200417 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 u32 node_index, add;
Damjan Mariondb7b2692016-06-09 16:16:27 +0200419 u8 verbose = 0;
Dave Barach87d24db2019-12-04 17:19:12 -0500420 int filter = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500421 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Neale Ranns3ee44042016-10-03 13:05:48 +0100423 if (!unformat_user (input, unformat_line_input, line_input))
424 return 0;
425
Dave Barach11fb09e2020-08-06 12:10:09 -0400426 if (vnet_trace_placeholder == 0)
427 vec_validate_aligned (vnet_trace_placeholder, 2048,
428 CLIB_CACHE_LINE_BYTES);
Dave Barachf8b85862018-08-17 18:29:07 -0400429
Neale Ranns3ee44042016-10-03 13:05:48 +0100430 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
Damjan Mariondb7b2692016-06-09 16:16:27 +0200431 {
Neale Ranns3ee44042016-10-03 13:05:48 +0100432 if (unformat (line_input, "%U %d",
433 unformat_vlib_node, vm, &node_index, &add))
Damjan Mariondb7b2692016-06-09 16:16:27 +0200434 ;
Neale Ranns3ee44042016-10-03 13:05:48 +0100435 else if (unformat (line_input, "verbose"))
Damjan Mariondb7b2692016-06-09 16:16:27 +0200436 verbose = 1;
Dave Barach87d24db2019-12-04 17:19:12 -0500437 else if (unformat (line_input, "filter"))
438 filter = 1;
Damjan Mariondb7b2692016-06-09 16:16:27 +0200439 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500440 {
441 error = clib_error_create ("expected NODE COUNT, got `%U'",
442 format_unformat_error, line_input);
443 goto done;
444 }
Damjan Mariondb7b2692016-06-09 16:16:27 +0200445 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446
Damjan Marion7ca5aaa2019-09-24 18:10:49 +0200447 node = vlib_get_node (vm, node_index);
448
449 if ((node->flags & VLIB_NODE_FLAG_TRACE_SUPPORTED) == 0)
450 {
451 error = clib_error_create ("node '%U' doesn't support per-node "
452 "tracing. There may be another way to "
453 "initiate trace on this node.",
454 format_vlib_node_name, vm, node_index);
455 goto done;
456 }
457
Dave Barach87d24db2019-12-04 17:19:12 -0500458 if (filter)
459 {
460 if (vlib_enable_disable_pkt_trace_filter (1 /* enable */ ))
461 {
462 error = clib_error_create ("No packet trace filter configured...");
463 goto done;
464 }
465 }
466
Jon Loeligerc0b19542020-05-11 08:43:51 -0500467 trace_update_capture_options (add, node_index, filter, verbose);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Billy McFalla9a20e72017-02-15 11:39:12 -0500469done:
470 unformat_free (line_input);
471
472 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473}
474
Dave Barach9b8ffd92016-07-08 08:13:45 -0400475/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476VLIB_CLI_COMMAND (add_trace_cli,static) = {
477 .path = "trace add",
Jon Loeligerc0b19542020-05-11 08:43:51 -0500478 .short_help = "trace add <input-graph-node> <add'l-pkts-for-node-> [filter] [verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 .function = cli_add_trace_buffer,
480};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400481/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482
Bud Grise0bcc9d52016-02-02 14:23:29 -0500483/*
484 * Configure a filter for packet traces.
485 *
486 * This supplements the packet trace feature so that only packets matching
487 * the filter are included in the trace. Currently the only filter is to
488 * keep packets that include a certain node in the trace or exclude a certain
489 * node in the trace.
490 *
491 * The count of traced packets in the "trace add" command is still used to
492 * create a certain number of traces. The "trace filter" command specifies
493 * how many of those packets should be retained in the trace.
494 *
495 * For example, 1Mpps of traffic is arriving and one of those packets is being
496 * dropped. To capture the trace for only that dropped packet, you can do:
497 * trace filter include error-drop 1
498 * trace add dpdk-input 1000000
499 * <wait one second>
500 * show trace
501 *
502 * Note that the filter could be implemented by capturing all traces and just
503 * reducing traces displayed by the "show trace" function. But that would
504 * require a lot of memory for storing the traces, making that infeasible.
505 *
506 * To remove traces from the trace pool that do not include a certain node
507 * requires that the trace be "complete" before applying the filter. To
508 * accomplish this, the trace pool is filtered upon each iteraction of the
509 * main vlib loop. Doing so keeps the number of allocated traces down to a
510 * reasonably low number. This requires that tracing for a buffer is not
511 * performed after the vlib main loop interation completes. i.e. you can't
512 * save away a buffer temporarily then inject it back into the graph and
513 * expect that the trace_index is still valid (such as a traffic manager might
514 * do). A new trace buffer should be allocated for those types of packets.
515 *
516 * The filter can be extended to support multiple nodes and other match
517 * criteria (e.g. input sw_if_index, mac address) but for now just checks if
518 * a specified node is in the trace or not in the trace.
519 */
Jon Loeligerc0b19542020-05-11 08:43:51 -0500520
521void
522trace_filter_set (u32 node_index, u32 flag, u32 count)
523{
524 /* *INDENT-OFF* */
525 foreach_vlib_main (
526 ({
527 vlib_trace_main_t *tm;
528
529 tm = &this_vlib_main->trace_main;
530 tm->filter_node_index = node_index;
531 tm->filter_flag = flag;
532 tm->filter_count = count;
533
534 /*
535 * Clear the trace limits to stop any in-progress tracing
536 * Prevents runaway trace allocations when the filter changes
537 * (or is removed)
538 */
539 vec_free (tm->nodes);
540 }));
541 /* *INDENT-ON* */
542}
543
544
Bud Grise0bcc9d52016-02-02 14:23:29 -0500545static clib_error_t *
546cli_filter_trace (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400547 unformat_input_t * input, vlib_cli_command_t * cmd)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500548{
Bud Grise0bcc9d52016-02-02 14:23:29 -0500549 u32 filter_node_index;
550 u32 filter_flag;
551 u32 filter_count;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500552
553 if (unformat (input, "include %U %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400554 unformat_vlib_node, vm, &filter_node_index, &filter_count))
Bud Grise0bcc9d52016-02-02 14:23:29 -0500555 {
556 filter_flag = FILTER_FLAG_INCLUDE;
557 }
558 else if (unformat (input, "exclude %U %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400559 unformat_vlib_node, vm, &filter_node_index,
560 &filter_count))
Bud Grise0bcc9d52016-02-02 14:23:29 -0500561 {
562 filter_flag = FILTER_FLAG_EXCLUDE;
563 }
564 else if (unformat (input, "none"))
565 {
566 filter_flag = FILTER_FLAG_NONE;
567 filter_node_index = 0;
568 filter_count = 0;
569 }
570 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400571 return
572 clib_error_create
573 ("expected 'include NODE COUNT' or 'exclude NODE COUNT' or 'none', got `%U'",
574 format_unformat_error, input);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500575
Jon Loeligerc0b19542020-05-11 08:43:51 -0500576 trace_filter_set (filter_node_index, filter_flag, filter_count);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500577
578 return 0;
579}
580
Dave Barach9b8ffd92016-07-08 08:13:45 -0400581/* *INDENT-OFF* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500582VLIB_CLI_COMMAND (filter_trace_cli,static) = {
583 .path = "trace filter",
Jon Loeligerc0b19542020-05-11 08:43:51 -0500584 .short_help = "trace filter none | [include|exclude] NODE COUNT",
Bud Grise0bcc9d52016-02-02 14:23:29 -0500585 .function = cli_filter_trace,
586};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400587/* *INDENT-ON* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500588
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589static clib_error_t *
590cli_clear_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400591 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592{
Jon Loeligerc0b19542020-05-11 08:43:51 -0500593 vlib_trace_stop_and_clear ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594 return 0;
595}
596
Dave Barach9b8ffd92016-07-08 08:13:45 -0400597/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598VLIB_CLI_COMMAND (clear_trace_cli,static) = {
599 .path = "clear trace",
600 .short_help = "Clear trace buffer and free memory",
601 .function = cli_clear_trace_buffer,
602};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400603/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604
Dave Barach11fb09e2020-08-06 12:10:09 -0400605/* Placeholder function to get us linked in. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400606void
607vlib_trace_cli_reference (void)
608{
609}
610
Dave Barach87d24db2019-12-04 17:19:12 -0500611int
612vnet_is_packet_traced (vlib_buffer_t * b,
613 u32 classify_table_index, int func)
614__attribute__ ((weak));
615
616int
617vnet_is_packet_traced (vlib_buffer_t * b, u32 classify_table_index, int func)
618{
619 clib_warning ("BUG: STUB called");
620 return 1;
621}
622
Dave Barach0a67b482020-06-08 11:17:19 -0400623void *
624vlib_add_trace (vlib_main_t * vm,
625 vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
626{
627 return vlib_add_trace_inline (vm, r, b, n_data_bytes);
628}
629
630
631
Dave Barach9b8ffd92016-07-08 08:13:45 -0400632/*
633 * fd.io coding-style-patch-verification: ON
634 *
635 * Local Variables:
636 * eval: (c-set-style "gnu")
637 * End:
638 */