blob: cfc3bda2cd097db7ac2c2694b3bebbc1335051d4 [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 Barachf8b85862018-08-17 18:29:07 -040043u8 *vnet_trace_dummy;
44
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);
Damjan Marionf1213b82016-03-13 02:22:06 +010078 clib_memcpy (t0, b0->data + b0->current_data,
Dave Barach9b8ffd92016-07-08 08:13:45 -040079 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);
Damjan Marionf1213b82016-03-13 02:22:06 +010084 clib_memcpy (t1, b1->data + b1->current_data,
Dave Barach9b8ffd92016-07-08 08:13:45 -040085 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);
Damjan Marionf1213b82016-03-13 02:22:06 +0100104 clib_memcpy (t0, b0->data + b0->current_data,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400105 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;
Bud Grised56a6f52016-02-19 12:10:33 -0500125
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 for (i = 0; i < vec_len (tm->trace_buffer_pool); i++)
127 if (! pool_is_free_index (tm->trace_buffer_pool, i))
128 vec_free (tm->trace_buffer_pool[i]);
129 pool_free (tm->trace_buffer_pool);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400131 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132}
133
Dave Barach9b8ffd92016-07-08 08:13:45 -0400134static u8 *
135format_vlib_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
138 vlib_trace_header_t *h = va_arg (*va, vlib_trace_header_t *);
139 vlib_trace_header_t *e = vec_end (h);
140 vlib_node_t *node, *prev_node;
141 clib_time_t *ct = &vm->clib_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 f64 t;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400143
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 prev_node = 0;
145 while (h < e)
146 {
147 node = vlib_get_node (vm, h->node_index);
148
149 if (node != prev_node)
150 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151 t =
152 (h->time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
153 s =
154 format (s, "\n%U: %v", format_time_interval, "h:m:s:u", t,
155 node->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 }
157 prev_node = node;
158
159 if (node->format_trace)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 s = format (s, "\n %U", node->format_trace, vm, node, h->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400162 s = format (s, "\n %U", node->format_buffer, h->data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
164 h = vlib_trace_header_next (h);
165 }
166
167 return s;
168}
169
170/* Root of all trace cli commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400171/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172VLIB_CLI_COMMAND (trace_cli_command,static) = {
173 .path = "trace",
174 .short_help = "Packet tracer commands",
175};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500178static int
Dave Barach9b8ffd92016-07-08 08:13:45 -0400179trace_cmp (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500180{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400181 vlib_trace_header_t **t1 = a1;
182 vlib_trace_header_t **t2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500183 i64 dt = t1[0]->time - t2[0]->time;
184 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
185}
186
Bud Grise0bcc9d52016-02-02 14:23:29 -0500187/*
188 * Return 1 if this packet passes the trace filter, or 0 otherwise
189 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400190u32
191filter_accept (vlib_trace_main_t * tm, vlib_trace_header_t * h)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500192{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400193 vlib_trace_header_t *e = vec_end (h);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500194
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195 if (tm->filter_flag == 0)
196 return 1;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500197
198 if (tm->filter_flag == FILTER_FLAG_INCLUDE)
199 {
200 while (h < e)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400201 {
202 if (h->node_index == tm->filter_node_index)
203 return 1;
204 h = vlib_trace_header_next (h);
205 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500206 return 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207 }
208 else /* FILTER_FLAG_EXCLUDE */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500209 {
210 while (h < e)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400211 {
212 if (h->node_index == tm->filter_node_index)
213 return 0;
214 h = vlib_trace_header_next (h);
215 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500216 return 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400217 }
Bud Grise0bcc9d52016-02-02 14:23:29 -0500218
219 return 0;
220}
221
222/*
223 * Remove traces from the trace buffer pool that don't pass the filter
224 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400225void
226trace_apply_filter (vlib_main_t * vm)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500227{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400228 vlib_trace_main_t *tm = &vm->trace_main;
229 vlib_trace_header_t **h;
230 vlib_trace_header_t ***traces_to_remove = 0;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500231 u32 index;
232 u32 trace_index;
233 u32 n_accepted;
234
235 u32 accept;
236
237 if (tm->filter_flag == FILTER_FLAG_NONE)
238 return;
239
240 /*
241 * Ideally we would retain the first N traces that pass the filter instead
242 * of any N traces.
243 */
244 n_accepted = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400245 /* *INDENT-OFF* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500246 pool_foreach (h, tm->trace_buffer_pool,
247 ({
248 accept = filter_accept(tm, h[0]);
249
250 if ((n_accepted == tm->filter_count) || !accept)
251 vec_add1 (traces_to_remove, h);
252 else
253 n_accepted++;
254 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255 /* *INDENT-ON* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500256
257 /* remove all traces that we don't want to keep */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400258 for (index = 0; index < vec_len (traces_to_remove); index++)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500259 {
260 trace_index = traces_to_remove[index] - tm->trace_buffer_pool;
261 _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
262 pool_put_index (tm->trace_buffer_pool, trace_index);
263 }
264
265 vec_free (traces_to_remove);
266}
267
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268static clib_error_t *
269cli_show_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400270 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272 vlib_trace_main_t *tm;
273 vlib_trace_header_t **h, **traces;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 u32 i, index = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400275 char *fmt;
276 u8 *s = 0;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500277 u32 max;
278
279 /*
280 * By default display only this many traces. To display more, explicitly
281 * specify a max. This prevents unexpectedly huge outputs.
282 */
283 max = 50;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400284 while (unformat_check_input (input) != (uword) UNFORMAT_END_OF_INPUT)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500285 {
286 if (unformat (input, "max %d", &max))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400287 ;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500288 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400289 return clib_error_create ("expected 'max COUNT', got `%U'",
290 format_unformat_error, input);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500291 }
292
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
294 /* Get active traces from pool. */
295
Dave Barach9b8ffd92016-07-08 08:13:45 -0400296 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 foreach_vlib_main (
298 ({
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700299 fmt = "------------------- Start of thread %d %s -------------------\n";
300 s = format (s, fmt, index, vlib_worker_threads[index].name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
302 tm = &this_vlib_main->trace_main;
303
Bud Grise0bcc9d52016-02-02 14:23:29 -0500304 trace_apply_filter(this_vlib_main);
305
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 traces = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400307 pool_foreach (h, tm->trace_buffer_pool,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 ({
309 vec_add1 (traces, h[0]);
310 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400311
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 if (vec_len (traces) == 0)
313 {
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700314 s = format (s, "No packets in trace buffer\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 goto done;
316 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 /* Sort them by increasing time. */
Matus Fabiand2dc3df2015-12-14 10:31:33 -0500319 vec_sort_with_function (traces, trace_cmp);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400320
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 for (i = 0; i < vec_len (traces); i++)
322 {
Bud Grise0bcc9d52016-02-02 14:23:29 -0500323 if (i == max)
324 {
325 vlib_cli_output (vm, "Limiting display to %d packets."
326 " To display more specify max.", max);
327 goto done;
328 }
329
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700330 s = format (s, "Packet %d\n%U\n\n", i + 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 format_vlib_trace, vm, traces[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400333
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 done:
335 vec_free (traces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
337 index++;
338 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400339 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Klement Sekera29396e62016-12-21 03:24:00 +0100341 vlib_cli_output (vm, "%v", s);
Ed Warnicke81aa0e52015-12-22 18:55:08 -0700342 vec_free (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343 return 0;
344}
345
Dave Barach9b8ffd92016-07-08 08:13:45 -0400346/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347VLIB_CLI_COMMAND (show_trace_cli,static) = {
348 .path = "show trace",
Bud Grise0bcc9d52016-02-02 14:23:29 -0500349 .short_help = "Show trace buffer [max COUNT]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 .function = cli_show_trace_buffer,
351};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400352/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
354static clib_error_t *
355cli_add_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357{
Neale Ranns3ee44042016-10-03 13:05:48 +0100358 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400359 vlib_trace_main_t *tm;
360 vlib_trace_node_t *tn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 u32 node_index, add;
Damjan Mariondb7b2692016-06-09 16:16:27 +0200362 u8 verbose = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500363 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364
Neale Ranns3ee44042016-10-03 13:05:48 +0100365 if (!unformat_user (input, unformat_line_input, line_input))
366 return 0;
367
Dave Barachf8b85862018-08-17 18:29:07 -0400368 if (vnet_trace_dummy == 0)
369 vec_validate_aligned (vnet_trace_dummy, 2048, CLIB_CACHE_LINE_BYTES);
370
Neale Ranns3ee44042016-10-03 13:05:48 +0100371 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
Damjan Mariondb7b2692016-06-09 16:16:27 +0200372 {
Neale Ranns3ee44042016-10-03 13:05:48 +0100373 if (unformat (line_input, "%U %d",
374 unformat_vlib_node, vm, &node_index, &add))
Damjan Mariondb7b2692016-06-09 16:16:27 +0200375 ;
Neale Ranns3ee44042016-10-03 13:05:48 +0100376 else if (unformat (line_input, "verbose"))
Damjan Mariondb7b2692016-06-09 16:16:27 +0200377 verbose = 1;
378 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500379 {
380 error = clib_error_create ("expected NODE COUNT, got `%U'",
381 format_unformat_error, line_input);
382 goto done;
383 }
Damjan Mariondb7b2692016-06-09 16:16:27 +0200384 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385
Neale Ranns3ee44042016-10-03 13:05:48 +0100386 /* *INDENT-OFF* */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387 foreach_vlib_main ((
Neale Ranns3ee44042016-10-03 13:05:48 +0100388 {
Neale Ranns3ee44042016-10-03 13:05:48 +0100389 tm = &this_vlib_main->trace_main;
Neale Ranns3ee44042016-10-03 13:05:48 +0100390 tm->verbose = verbose;
Neale Ranns3ee44042016-10-03 13:05:48 +0100391 vec_validate (tm->nodes, node_index);
392 tn = tm->nodes + node_index;
Dave Barachf8b85862018-08-17 18:29:07 -0400393 tn->limit += add;
394 tm->trace_enable = 1;
Neale Ranns3ee44042016-10-03 13:05:48 +0100395 }));
396 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397
Billy McFalla9a20e72017-02-15 11:39:12 -0500398done:
399 unformat_free (line_input);
400
401 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402}
403
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405VLIB_CLI_COMMAND (add_trace_cli,static) = {
406 .path = "trace add",
407 .short_help = "Trace given number of packets",
408 .function = cli_add_trace_buffer,
409};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400410/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Bud Grise0bcc9d52016-02-02 14:23:29 -0500412
413/*
414 * Configure a filter for packet traces.
415 *
416 * This supplements the packet trace feature so that only packets matching
417 * the filter are included in the trace. Currently the only filter is to
418 * keep packets that include a certain node in the trace or exclude a certain
419 * node in the trace.
420 *
421 * The count of traced packets in the "trace add" command is still used to
422 * create a certain number of traces. The "trace filter" command specifies
423 * how many of those packets should be retained in the trace.
424 *
425 * For example, 1Mpps of traffic is arriving and one of those packets is being
426 * dropped. To capture the trace for only that dropped packet, you can do:
427 * trace filter include error-drop 1
428 * trace add dpdk-input 1000000
429 * <wait one second>
430 * show trace
431 *
432 * Note that the filter could be implemented by capturing all traces and just
433 * reducing traces displayed by the "show trace" function. But that would
434 * require a lot of memory for storing the traces, making that infeasible.
435 *
436 * To remove traces from the trace pool that do not include a certain node
437 * requires that the trace be "complete" before applying the filter. To
438 * accomplish this, the trace pool is filtered upon each iteraction of the
439 * main vlib loop. Doing so keeps the number of allocated traces down to a
440 * reasonably low number. This requires that tracing for a buffer is not
441 * performed after the vlib main loop interation completes. i.e. you can't
442 * save away a buffer temporarily then inject it back into the graph and
443 * expect that the trace_index is still valid (such as a traffic manager might
444 * do). A new trace buffer should be allocated for those types of packets.
445 *
446 * The filter can be extended to support multiple nodes and other match
447 * criteria (e.g. input sw_if_index, mac address) but for now just checks if
448 * a specified node is in the trace or not in the trace.
449 */
450static clib_error_t *
451cli_filter_trace (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400452 unformat_input_t * input, vlib_cli_command_t * cmd)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500453{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454 vlib_trace_main_t *tm = &vm->trace_main;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500455 u32 filter_node_index;
456 u32 filter_flag;
457 u32 filter_count;
Bud Grise0bcc9d52016-02-02 14:23:29 -0500458
459 if (unformat (input, "include %U %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400460 unformat_vlib_node, vm, &filter_node_index, &filter_count))
Bud Grise0bcc9d52016-02-02 14:23:29 -0500461 {
462 filter_flag = FILTER_FLAG_INCLUDE;
463 }
464 else if (unformat (input, "exclude %U %d",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400465 unformat_vlib_node, vm, &filter_node_index,
466 &filter_count))
Bud Grise0bcc9d52016-02-02 14:23:29 -0500467 {
468 filter_flag = FILTER_FLAG_EXCLUDE;
469 }
470 else if (unformat (input, "none"))
471 {
472 filter_flag = FILTER_FLAG_NONE;
473 filter_node_index = 0;
474 filter_count = 0;
475 }
476 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 return
478 clib_error_create
479 ("expected 'include NODE COUNT' or 'exclude NODE COUNT' or 'none', got `%U'",
480 format_unformat_error, input);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500481
Dave Barach9b8ffd92016-07-08 08:13:45 -0400482 /* *INDENT-OFF* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500483 foreach_vlib_main (
Dave Barach9b8ffd92016-07-08 08:13:45 -0400484 ({
Bud Grise0bcc9d52016-02-02 14:23:29 -0500485 tm = &this_vlib_main->trace_main;
486 tm->filter_node_index = filter_node_index;
487 tm->filter_flag = filter_flag;
488 tm->filter_count = filter_count;
489
490 /*
491 * Clear the trace limits to stop any in-progress tracing
Dave Barachf8b85862018-08-17 18:29:07 -0400492 * Prevents runaway trace allocations when the filter changes
493 * (or is removed)
Bud Grise0bcc9d52016-02-02 14:23:29 -0500494 */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500495 vec_free (tm->nodes);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500496 }));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400497 /* *INDENT-ON* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500498
499 return 0;
500}
501
Dave Barach9b8ffd92016-07-08 08:13:45 -0400502/* *INDENT-OFF* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500503VLIB_CLI_COMMAND (filter_trace_cli,static) = {
504 .path = "trace filter",
505 .short_help = "filter trace output - include NODE COUNT | exclude NODE COUNT | none",
506 .function = cli_filter_trace,
507};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400508/* *INDENT-ON* */
Bud Grise0bcc9d52016-02-02 14:23:29 -0500509
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510static clib_error_t *
511cli_clear_trace_buffer (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400512 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513{
Bud Grise0bcc9d52016-02-02 14:23:29 -0500514 clear_trace_buffer ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 return 0;
516}
517
Dave Barach9b8ffd92016-07-08 08:13:45 -0400518/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519VLIB_CLI_COMMAND (clear_trace_cli,static) = {
520 .path = "clear trace",
521 .short_help = "Clear trace buffer and free memory",
522 .function = cli_clear_trace_buffer,
523};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400524/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
526/* Dummy function to get us linked in. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400527void
528vlib_trace_cli_reference (void)
529{
530}
531
532/*
533 * fd.io coding-style-patch-verification: ON
534 *
535 * Local Variables:
536 * eval: (c-set-style "gnu")
537 * End:
538 */