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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * trace.c: VLIB trace buffer.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vlib/threads.h>
45 /* Helper function for nodes which only trace buffer data. */
47 vlib_trace_frame_buffers_only (vlib_main_t * vm,
48 vlib_node_runtime_t * node,
51 uword next_buffer_stride,
52 uword n_buffer_data_bytes_in_trace)
62 vlib_buffer_t *b0, *b1;
65 /* Prefetch next iteration. */
66 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
67 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
72 b0 = vlib_get_buffer (vm, bi0);
73 b1 = vlib_get_buffer (vm, bi1);
75 if (b0->flags & VLIB_BUFFER_IS_TRACED)
77 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
78 clib_memcpy_fast (t0, b0->data + b0->current_data,
79 n_buffer_data_bytes_in_trace);
81 if (b1->flags & VLIB_BUFFER_IS_TRACED)
83 t1 = vlib_add_trace (vm, node, b1, n_buffer_data_bytes_in_trace);
84 clib_memcpy_fast (t1, b1->data + b1->current_data,
85 n_buffer_data_bytes_in_trace);
99 b0 = vlib_get_buffer (vm, bi0);
101 if (b0->flags & VLIB_BUFFER_IS_TRACED)
103 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
104 clib_memcpy_fast (t0, b0->data + b0->current_data,
105 n_buffer_data_bytes_in_trace);
112 /* Free up all trace buffer memory. */
114 clear_trace_buffer (void)
117 vlib_trace_main_t *tm;
122 tm = &this_vlib_main->trace_main;
124 tm->trace_enable = 0;
126 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);
135 format_vlib_trace (u8 * s, va_list * va)
137 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;
147 node = vlib_get_node (vm, h->node_index);
149 if (node != prev_node)
152 (h->time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
154 format (s, "\n%U: %v", format_time_interval, "h:m:s:u", t,
159 if (node->format_trace)
160 s = format (s, "\n %U", node->format_trace, vm, node, h->data);
162 s = format (s, "\n %U", node->format_buffer, h->data);
164 h = vlib_trace_header_next (h);
170 /* Root of all trace cli commands. */
172 VLIB_CLI_COMMAND (trace_cli_command,static) = {
174 .short_help = "Packet tracer commands",
179 trace_cmp (void *a1, void *a2)
181 vlib_trace_header_t **t1 = a1;
182 vlib_trace_header_t **t2 = a2;
183 i64 dt = t1[0]->time - t2[0]->time;
184 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
188 * Return 1 if this packet passes the trace filter, or 0 otherwise
191 filter_accept (vlib_trace_main_t * tm, vlib_trace_header_t * h)
193 vlib_trace_header_t *e = vec_end (h);
195 if (tm->filter_flag == 0)
198 if (tm->filter_flag == FILTER_FLAG_INCLUDE)
202 if (h->node_index == tm->filter_node_index)
204 h = vlib_trace_header_next (h);
208 else /* FILTER_FLAG_EXCLUDE */
212 if (h->node_index == tm->filter_node_index)
214 h = vlib_trace_header_next (h);
223 * Remove traces from the trace buffer pool that don't pass the filter
226 trace_apply_filter (vlib_main_t * vm)
228 vlib_trace_main_t *tm = &vm->trace_main;
229 vlib_trace_header_t **h;
230 vlib_trace_header_t ***traces_to_remove = 0;
237 if (tm->filter_flag == FILTER_FLAG_NONE)
241 * Ideally we would retain the first N traces that pass the filter instead
246 pool_foreach (h, tm->trace_buffer_pool,
248 accept = filter_accept(tm, h[0]);
250 if ((n_accepted == tm->filter_count) || !accept)
251 vec_add1 (traces_to_remove, h);
257 /* remove all traces that we don't want to keep */
258 for (index = 0; index < vec_len (traces_to_remove); index++)
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);
265 vec_free (traces_to_remove);
268 static clib_error_t *
269 cli_show_trace_buffer (vlib_main_t * vm,
270 unformat_input_t * input, vlib_cli_command_t * cmd)
272 vlib_trace_main_t *tm;
273 vlib_trace_header_t **h, **traces;
280 * By default display only this many traces. To display more, explicitly
281 * specify a max. This prevents unexpectedly huge outputs.
284 while (unformat_check_input (input) != (uword) UNFORMAT_END_OF_INPUT)
286 if (unformat (input, "max %d", &max))
289 return clib_error_create ("expected 'max COUNT', got `%U'",
290 format_unformat_error, input);
294 /* Get active traces from pool. */
299 fmt = "------------------- Start of thread %d %s -------------------\n";
300 s = format (s, fmt, index, vlib_worker_threads[index].name);
302 tm = &this_vlib_main->trace_main;
304 trace_apply_filter(this_vlib_main);
307 pool_foreach (h, tm->trace_buffer_pool,
309 vec_add1 (traces, h[0]);
312 if (vec_len (traces) == 0)
314 s = format (s, "No packets in trace buffer\n");
318 /* Sort them by increasing time. */
319 vec_sort_with_function (traces, trace_cmp);
321 for (i = 0; i < vec_len (traces); i++)
325 vlib_cli_output (vm, "Limiting display to %d packets."
326 " To display more specify max.", max);
330 s = format (s, "Packet %d\n%U\n\n", i + 1,
331 format_vlib_trace, vm, traces[i]);
341 vlib_cli_output (vm, "%v", s);
347 VLIB_CLI_COMMAND (show_trace_cli,static) = {
348 .path = "show trace",
349 .short_help = "Show trace buffer [max COUNT]",
350 .function = cli_show_trace_buffer,
354 int vlib_enable_disable_pkt_trace_filter (int enable) __attribute__ ((weak));
356 vlib_enable_disable_pkt_trace_filter (int enable)
361 static clib_error_t *
362 cli_add_trace_buffer (vlib_main_t * vm,
363 unformat_input_t * input, vlib_cli_command_t * cmd)
365 unformat_input_t _line_input, *line_input = &_line_input;
366 vlib_trace_main_t *tm;
368 vlib_trace_node_t *tn;
372 clib_error_t *error = 0;
374 if (!unformat_user (input, unformat_line_input, line_input))
377 if (vnet_trace_dummy == 0)
378 vec_validate_aligned (vnet_trace_dummy, 2048, CLIB_CACHE_LINE_BYTES);
380 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
382 if (unformat (line_input, "%U %d",
383 unformat_vlib_node, vm, &node_index, &add))
385 else if (unformat (line_input, "verbose"))
387 else if (unformat (line_input, "filter"))
391 error = clib_error_create ("expected NODE COUNT, got `%U'",
392 format_unformat_error, line_input);
397 node = vlib_get_node (vm, node_index);
399 if ((node->flags & VLIB_NODE_FLAG_TRACE_SUPPORTED) == 0)
401 error = clib_error_create ("node '%U' doesn't support per-node "
402 "tracing. There may be another way to "
403 "initiate trace on this node.",
404 format_vlib_node_name, vm, node_index);
410 if (vlib_enable_disable_pkt_trace_filter (1 /* enable */ ))
412 error = clib_error_create ("No packet trace filter configured...");
420 tm = &this_vlib_main->trace_main;
421 tm->verbose = verbose;
422 vec_validate (tm->nodes, node_index);
423 tn = tm->nodes + node_index;
425 tm->trace_enable = 1;
430 unformat_free (line_input);
436 VLIB_CLI_COMMAND (add_trace_cli,static) = {
438 .short_help = "Trace given number of packets",
439 .function = cli_add_trace_buffer,
444 * Configure a filter for packet traces.
446 * This supplements the packet trace feature so that only packets matching
447 * the filter are included in the trace. Currently the only filter is to
448 * keep packets that include a certain node in the trace or exclude a certain
451 * The count of traced packets in the "trace add" command is still used to
452 * create a certain number of traces. The "trace filter" command specifies
453 * how many of those packets should be retained in the trace.
455 * For example, 1Mpps of traffic is arriving and one of those packets is being
456 * dropped. To capture the trace for only that dropped packet, you can do:
457 * trace filter include error-drop 1
458 * trace add dpdk-input 1000000
462 * Note that the filter could be implemented by capturing all traces and just
463 * reducing traces displayed by the "show trace" function. But that would
464 * require a lot of memory for storing the traces, making that infeasible.
466 * To remove traces from the trace pool that do not include a certain node
467 * requires that the trace be "complete" before applying the filter. To
468 * accomplish this, the trace pool is filtered upon each iteraction of the
469 * main vlib loop. Doing so keeps the number of allocated traces down to a
470 * reasonably low number. This requires that tracing for a buffer is not
471 * performed after the vlib main loop interation completes. i.e. you can't
472 * save away a buffer temporarily then inject it back into the graph and
473 * expect that the trace_index is still valid (such as a traffic manager might
474 * do). A new trace buffer should be allocated for those types of packets.
476 * The filter can be extended to support multiple nodes and other match
477 * criteria (e.g. input sw_if_index, mac address) but for now just checks if
478 * a specified node is in the trace or not in the trace.
480 static clib_error_t *
481 cli_filter_trace (vlib_main_t * vm,
482 unformat_input_t * input, vlib_cli_command_t * cmd)
484 vlib_trace_main_t *tm = &vm->trace_main;
485 u32 filter_node_index;
489 if (unformat (input, "include %U %d",
490 unformat_vlib_node, vm, &filter_node_index, &filter_count))
492 filter_flag = FILTER_FLAG_INCLUDE;
494 else if (unformat (input, "exclude %U %d",
495 unformat_vlib_node, vm, &filter_node_index,
498 filter_flag = FILTER_FLAG_EXCLUDE;
500 else if (unformat (input, "none"))
502 filter_flag = FILTER_FLAG_NONE;
503 filter_node_index = 0;
509 ("expected 'include NODE COUNT' or 'exclude NODE COUNT' or 'none', got `%U'",
510 format_unformat_error, input);
515 tm = &this_vlib_main->trace_main;
516 tm->filter_node_index = filter_node_index;
517 tm->filter_flag = filter_flag;
518 tm->filter_count = filter_count;
521 * Clear the trace limits to stop any in-progress tracing
522 * Prevents runaway trace allocations when the filter changes
525 vec_free (tm->nodes);
533 VLIB_CLI_COMMAND (filter_trace_cli,static) = {
534 .path = "trace filter",
535 .short_help = "filter trace output - include NODE COUNT | exclude NODE COUNT | none",
536 .function = cli_filter_trace,
540 static clib_error_t *
541 cli_clear_trace_buffer (vlib_main_t * vm,
542 unformat_input_t * input, vlib_cli_command_t * cmd)
544 vlib_enable_disable_pkt_trace_filter (0 /* enable */ );
545 clear_trace_buffer ();
550 VLIB_CLI_COMMAND (clear_trace_cli,static) = {
551 .path = "clear trace",
552 .short_help = "Clear trace buffer and free memory",
553 .function = cli_clear_trace_buffer,
557 /* Dummy function to get us linked in. */
559 vlib_trace_cli_reference (void)
564 vnet_is_packet_traced (vlib_buffer_t * b,
565 u32 classify_table_index, int func)
566 __attribute__ ((weak));
569 vnet_is_packet_traced (vlib_buffer_t * b, u32 classify_table_index, int func)
571 clib_warning ("BUG: STUB called");
576 * fd.io coding-style-patch-verification: ON
579 * eval: (c-set-style "gnu")