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>
42 #include <vnet/classify/vnet_classify.h>
44 u8 *vnet_trace_placeholder;
46 /* Helper function for nodes which only trace buffer data. */
48 vlib_trace_frame_buffers_only (vlib_main_t * vm,
49 vlib_node_runtime_t * node,
52 uword next_buffer_stride,
53 uword n_buffer_data_bytes_in_trace)
63 vlib_buffer_t *b0, *b1;
66 /* Prefetch next iteration. */
67 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
68 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
73 b0 = vlib_get_buffer (vm, bi0);
74 b1 = vlib_get_buffer (vm, bi1);
76 if (b0->flags & VLIB_BUFFER_IS_TRACED)
78 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
79 clib_memcpy_fast (t0, b0->data + b0->current_data,
80 n_buffer_data_bytes_in_trace);
82 if (b1->flags & VLIB_BUFFER_IS_TRACED)
84 t1 = vlib_add_trace (vm, node, b1, n_buffer_data_bytes_in_trace);
85 clib_memcpy_fast (t1, b1->data + b1->current_data,
86 n_buffer_data_bytes_in_trace);
100 b0 = vlib_get_buffer (vm, bi0);
102 if (b0->flags & VLIB_BUFFER_IS_TRACED)
104 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
105 clib_memcpy_fast (t0, b0->data + b0->current_data,
106 n_buffer_data_bytes_in_trace);
113 /* Free up all trace buffer memory. */
115 clear_trace_buffer (void)
118 vlib_trace_main_t *tm;
123 tm = &this_vlib_main->trace_main;
125 tm->trace_enable = 0;
126 vec_free (tm->nodes);
131 tm = &this_vlib_main->trace_main;
133 for (i = 0; i < vec_len (tm->trace_buffer_pool); i++)
134 if (! pool_is_free_index (tm->trace_buffer_pool, i))
135 vec_free (tm->trace_buffer_pool[i]);
136 pool_free (tm->trace_buffer_pool);
142 format_vlib_trace (u8 * s, va_list * va)
144 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
145 vlib_trace_header_t *h = va_arg (*va, vlib_trace_header_t *);
146 vlib_trace_header_t *e = vec_end (h);
147 vlib_node_t *node, *prev_node;
148 clib_time_t *ct = &vm->clib_time;
154 node = vlib_get_node (vm, h->node_index);
156 if (node != prev_node)
159 (h->time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
161 format (s, "\n%U: %v", format_time_interval, "h:m:s:u", t,
166 if (node->format_trace)
167 s = format (s, "\n %U", node->format_trace, vm, node, h->data);
169 s = format (s, "\n %U", node->format_buffer, h->data);
171 h = vlib_trace_header_next (h);
177 /* Root of all trace cli commands. */
179 VLIB_CLI_COMMAND (trace_cli_command,static) = {
181 .short_help = "Packet tracer commands",
186 trace_time_cmp (void *a1, void *a2)
188 vlib_trace_header_t **t1 = a1;
189 vlib_trace_header_t **t2 = a2;
190 i64 dt = t1[0]->time - t2[0]->time;
191 return dt < 0 ? -1 : (dt > 0 ? +1 : 0);
195 * Return 1 if this packet passes the trace filter, or 0 otherwise
198 filter_accept (vlib_trace_main_t * tm, vlib_trace_header_t * h)
200 vlib_trace_header_t *e = vec_end (h);
202 if (tm->filter_flag == 0)
206 * When capturing a post-mortem dispatch trace,
207 * toss all existing traces once per dispatch cycle.
208 * So we can trace 4 billion pkts without running out of
211 if (tm->filter_flag == FILTER_FLAG_POST_MORTEM)
214 if (tm->filter_flag == FILTER_FLAG_INCLUDE)
218 if (h->node_index == tm->filter_node_index)
220 h = vlib_trace_header_next (h);
224 else /* FILTER_FLAG_EXCLUDE */
228 if (h->node_index == tm->filter_node_index)
230 h = vlib_trace_header_next (h);
239 * Remove traces from the trace buffer pool that don't pass the filter
242 trace_apply_filter (vlib_main_t * vm)
244 vlib_trace_main_t *tm = &vm->trace_main;
245 vlib_trace_header_t **h;
246 vlib_trace_header_t ***traces_to_remove = 0;
253 if (tm->filter_flag == FILTER_FLAG_NONE)
257 * Ideally we would retain the first N traces that pass the filter instead
262 pool_foreach (h, tm->trace_buffer_pool)
264 accept = filter_accept(tm, h[0]);
266 if ((n_accepted == tm->filter_count) || !accept)
267 vec_add1 (traces_to_remove, h);
273 /* remove all traces that we don't want to keep */
274 for (index = 0; index < vec_len (traces_to_remove); index++)
276 trace_index = traces_to_remove[index] - tm->trace_buffer_pool;
277 _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
278 pool_put_index (tm->trace_buffer_pool, trace_index);
281 vec_free (traces_to_remove);
284 static clib_error_t *
285 cli_show_trace_buffer (vlib_main_t * vm,
286 unformat_input_t * input, vlib_cli_command_t * cmd)
288 vlib_trace_main_t *tm;
289 vlib_trace_header_t **h, **traces;
296 * By default display only this many traces. To display more, explicitly
297 * specify a max. This prevents unexpectedly huge outputs.
300 while (unformat_check_input (input) != (uword) UNFORMAT_END_OF_INPUT)
302 if (unformat (input, "max %d", &max))
305 return clib_error_create ("expected 'max COUNT', got `%U'",
306 format_unformat_error, input);
310 /* Get active traces from pool. */
315 fmt = "------------------- Start of thread %d %s -------------------\n";
316 s = format (s, fmt, index, vlib_worker_threads[index].name);
318 tm = &this_vlib_main->trace_main;
320 trace_apply_filter(this_vlib_main);
323 pool_foreach (h, tm->trace_buffer_pool)
325 vec_add1 (traces, h[0]);
328 if (vec_len (traces) == 0)
330 s = format (s, "No packets in trace buffer\n");
334 /* Sort them by increasing time. */
335 vec_sort_with_function (traces, trace_time_cmp);
337 for (i = 0; i < vec_len (traces); i++)
341 vlib_cli_output (vm, "Limiting display to %d packets."
342 " To display more specify max.", max);
346 s = format (s, "Packet %d\n%U\n\n", i + 1,
347 format_vlib_trace, vm, traces[i]);
357 vlib_cli_output (vm, "%v", s);
363 VLIB_CLI_COMMAND (show_trace_cli,static) = {
364 .path = "show trace",
365 .short_help = "Show trace buffer [max COUNT]",
366 .function = cli_show_trace_buffer,
370 int vlib_enable_disable_pkt_trace_filter (int enable) __attribute__ ((weak));
373 vlib_enable_disable_pkt_trace_filter (int enable)
379 vlib_trace_stop_and_clear (void)
381 vlib_enable_disable_pkt_trace_filter (0); /* disble tracing */
382 clear_trace_buffer ();
387 trace_update_capture_options (u32 add, u32 node_index, u32 filter, u8 verbose)
389 vlib_trace_main_t *tm;
390 vlib_trace_node_t *tn;
398 tm = &this_vlib_main->trace_main;
399 tm->verbose = verbose;
400 vec_validate (tm->nodes, node_index);
401 tn = tm->nodes + node_index;
404 * Adding 0 makes no real sense, and there wa no other way
405 * to explicilty zero-out the limits and count, so make
406 * an "add 0" request really be "set to 0".
409 tn->limit = tn->count = 0;
416 tm = &this_vlib_main->trace_main;
417 tm->trace_enable = 1;
421 vlib_enable_disable_pkt_trace_filter (! !filter);
424 static clib_error_t *
425 cli_add_trace_buffer (vlib_main_t * vm,
426 unformat_input_t * input, vlib_cli_command_t * cmd)
428 unformat_input_t _line_input, *line_input = &_line_input;
433 clib_error_t *error = 0;
435 if (!unformat_user (input, unformat_line_input, line_input))
438 if (vnet_trace_placeholder == 0)
439 vec_validate_aligned (vnet_trace_placeholder, 2048,
440 CLIB_CACHE_LINE_BYTES);
442 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
444 if (unformat (line_input, "%U %d",
445 unformat_vlib_node, vm, &node_index, &add))
447 else if (unformat (line_input, "verbose"))
449 else if (unformat (line_input, "filter"))
453 error = clib_error_create ("expected NODE COUNT, got `%U'",
454 format_unformat_error, line_input);
459 node = vlib_get_node (vm, node_index);
461 if ((node->flags & VLIB_NODE_FLAG_TRACE_SUPPORTED) == 0)
463 error = clib_error_create ("node '%U' doesn't support per-node "
464 "tracing. There may be another way to "
465 "initiate trace on this node.",
466 format_vlib_node_name, vm, node_index);
470 u32 filter_table = classify_get_trace_chain ();
471 if (filter && filter_table == ~0)
473 error = clib_error_create ("No packet trace filter configured...");
477 trace_update_capture_options (add, node_index, filter, verbose);
480 unformat_free (line_input);
486 VLIB_CLI_COMMAND (add_trace_cli,static) = {
488 .short_help = "trace add <input-graph-node> <add'l-pkts-for-node-> [filter] [verbose]",
489 .function = cli_add_trace_buffer,
494 * Configure a filter for packet traces.
496 * This supplements the packet trace feature so that only packets matching
497 * the filter are included in the trace. Currently the only filter is to
498 * keep packets that include a certain node in the trace or exclude a certain
501 * The count of traced packets in the "trace add" command is still used to
502 * create a certain number of traces. The "trace filter" command specifies
503 * how many of those packets should be retained in the trace.
505 * For example, 1Mpps of traffic is arriving and one of those packets is being
506 * dropped. To capture the trace for only that dropped packet, you can do:
507 * trace filter include error-drop 1
508 * trace add dpdk-input 1000000
512 * Note that the filter could be implemented by capturing all traces and just
513 * reducing traces displayed by the "show trace" function. But that would
514 * require a lot of memory for storing the traces, making that infeasible.
516 * To remove traces from the trace pool that do not include a certain node
517 * requires that the trace be "complete" before applying the filter. To
518 * accomplish this, the trace pool is filtered upon each iteraction of the
519 * main vlib loop. Doing so keeps the number of allocated traces down to a
520 * reasonably low number. This requires that tracing for a buffer is not
521 * performed after the vlib main loop interation completes. i.e. you can't
522 * save away a buffer temporarily then inject it back into the graph and
523 * expect that the trace_index is still valid (such as a traffic manager might
524 * do). A new trace buffer should be allocated for those types of packets.
526 * The filter can be extended to support multiple nodes and other match
527 * criteria (e.g. input sw_if_index, mac address) but for now just checks if
528 * a specified node is in the trace or not in the trace.
532 trace_filter_set (u32 node_index, u32 flag, u32 count)
537 vlib_trace_main_t *tm;
539 tm = &this_vlib_main->trace_main;
540 tm->filter_node_index = node_index;
541 tm->filter_flag = flag;
542 tm->filter_count = count;
545 * Clear the trace limits to stop any in-progress tracing
546 * Prevents runaway trace allocations when the filter changes
549 vec_free (tm->nodes);
555 static clib_error_t *
556 cli_filter_trace (vlib_main_t * vm,
557 unformat_input_t * input, vlib_cli_command_t * cmd)
559 u32 filter_node_index;
563 if (unformat (input, "include %U %d",
564 unformat_vlib_node, vm, &filter_node_index, &filter_count))
566 filter_flag = FILTER_FLAG_INCLUDE;
568 else if (unformat (input, "exclude %U %d",
569 unformat_vlib_node, vm, &filter_node_index,
572 filter_flag = FILTER_FLAG_EXCLUDE;
574 else if (unformat (input, "none"))
576 filter_flag = FILTER_FLAG_NONE;
577 filter_node_index = 0;
583 ("expected 'include NODE COUNT' or 'exclude NODE COUNT' or 'none', got `%U'",
584 format_unformat_error, input);
586 trace_filter_set (filter_node_index, filter_flag, filter_count);
592 VLIB_CLI_COMMAND (filter_trace_cli,static) = {
593 .path = "trace filter",
594 .short_help = "trace filter none | [include|exclude] NODE COUNT",
595 .function = cli_filter_trace,
599 static clib_error_t *
600 cli_clear_trace_buffer (vlib_main_t * vm,
601 unformat_input_t * input, vlib_cli_command_t * cmd)
603 vlib_trace_stop_and_clear ();
608 VLIB_CLI_COMMAND (clear_trace_cli,static) = {
609 .path = "clear trace",
610 .short_help = "Clear trace buffer and free memory",
611 .function = cli_clear_trace_buffer,
615 /* Placeholder function to get us linked in. */
617 vlib_trace_cli_reference (void)
622 vnet_is_packet_traced (vlib_buffer_t * b,
623 u32 classify_table_index, int func)
624 __attribute__ ((weak));
627 vnet_is_packet_traced (vlib_buffer_t * b, u32 classify_table_index, int func)
629 clib_warning ("BUG: STUB called");
634 vlib_add_trace (vlib_main_t * vm,
635 vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
637 return vlib_add_trace_inline (vm, r, b, n_data_bytes);
643 * fd.io coding-style-patch-verification: ON
646 * eval: (c-set-style "gnu")