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>
43 /* Helper function for nodes which only trace buffer data. */
45 vlib_trace_frame_buffers_only (vlib_main_t * vm,
46 vlib_node_runtime_t * node,
49 uword next_buffer_stride,
50 uword n_buffer_data_bytes_in_trace)
60 vlib_buffer_t * b0, * b1;
63 /* Prefetch next iteration. */
64 vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
65 vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
70 b0 = vlib_get_buffer (vm, bi0);
71 b1 = vlib_get_buffer (vm, bi1);
73 if (b0->flags & VLIB_BUFFER_IS_TRACED)
75 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
76 memcpy (t0, b0->data + b0->current_data,
77 n_buffer_data_bytes_in_trace);
79 if (b1->flags & VLIB_BUFFER_IS_TRACED)
81 t1 = vlib_add_trace (vm, node, b1, n_buffer_data_bytes_in_trace);
82 memcpy (t1, b1->data + b1->current_data,
83 n_buffer_data_bytes_in_trace);
97 b0 = vlib_get_buffer (vm, bi0);
99 if (b0->flags & VLIB_BUFFER_IS_TRACED)
101 t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
102 memcpy (t0, b0->data + b0->current_data,
103 n_buffer_data_bytes_in_trace);
110 /* Free up all trace buffer memory. */
112 clear_trace_buffer (vlib_trace_main_t * tm)
120 tm = &this_vlib_main->trace_main;
121 mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
123 for (i = 0; i < vec_len (tm->trace_buffer_pool); i++)
124 if (! pool_is_free_index (tm->trace_buffer_pool, i))
125 vec_free (tm->trace_buffer_pool[i]);
126 pool_free (tm->trace_buffer_pool);
127 clib_mem_set_heap (mainheap);
131 static u8 * format_vlib_trace (u8 * s, va_list * va)
133 vlib_main_t * vm = va_arg (*va, vlib_main_t *);
134 vlib_trace_header_t * h = va_arg (*va, vlib_trace_header_t *);
135 vlib_trace_header_t * e = vec_end (h);
136 vlib_node_t * node, * prev_node;
137 clib_time_t * ct = &vm->clib_time;
143 node = vlib_get_node (vm, h->node_index);
145 if (node != prev_node)
147 t = (h->time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
148 s = format (s, "\n%U: %v",
149 format_time_interval, "h:m:s:u", t,
154 if (node->format_trace)
155 s = format (s, "\n %U",
156 node->format_trace, vm, node, h->data);
158 s = format (s, "\n %U",
159 node->format_buffer, h->data);
161 h = vlib_trace_header_next (h);
167 /* Root of all trace cli commands. */
168 VLIB_CLI_COMMAND (trace_cli_command,static) = {
170 .short_help = "Packet tracer commands",
173 static clib_error_t *
174 cli_show_trace_buffer (vlib_main_t * vm,
175 unformat_input_t * input,
176 vlib_cli_command_t * cmd)
178 vlib_trace_main_t * tm = &vm->trace_main;
179 vlib_trace_header_t ** h, ** traces;
183 /* Get active traces from pool. */
189 fmt = "------------------- Start of thread %d %v -------------------";
190 vlib_cli_output (vm, fmt, index, vlib_worker_threads[index].name);
192 tm = &this_vlib_main->trace_main;
194 mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
196 pool_foreach (h, tm->trace_buffer_pool,
198 vec_add1 (traces, h[0]);
201 if (vec_len (traces) == 0)
203 clib_mem_set_heap (mainheap);
204 vlib_cli_output (vm, "No packets in trace buffer");
208 /* Sort them by increasing time. */
209 vec_sort (traces, t0, t1, ({
210 i64 dt = t0[0]->time - t1[0]->time;
211 dt < 0 ? -1 : (dt > 0 ? +1 : 0);
214 for (i = 0; i < vec_len (traces); i++)
216 clib_mem_set_heap (mainheap);
218 vlib_cli_output (vm, "Packet %d\n%U\n\n", i + 1,
219 format_vlib_trace, vm, traces[i]);
221 mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
226 clib_mem_set_heap (mainheap);
234 VLIB_CLI_COMMAND (show_trace_cli,static) = {
235 .path = "show trace",
236 .short_help = "Show trace buffer",
237 .function = cli_show_trace_buffer,
240 static clib_error_t *
241 cli_add_trace_buffer (vlib_main_t * vm,
242 unformat_input_t * input,
243 vlib_cli_command_t * cmd)
245 vlib_trace_main_t * tm;
246 vlib_trace_node_t * tn;
249 if (unformat (input, "%U %d", unformat_vlib_node, vm, &node_index, &add))
252 return clib_error_create ("expected NODE COUNT, got `%U'",
253 format_unformat_error, input);
258 tm = &this_vlib_main->trace_main;
260 oldheap = clib_mem_set_heap (this_vlib_main->heap_base);
262 vec_validate (tm->nodes, node_index);
263 tn = tm->nodes + node_index;
265 clib_mem_set_heap (oldheap);
271 VLIB_CLI_COMMAND (add_trace_cli,static) = {
273 .short_help = "Trace given number of packets",
274 .function = cli_add_trace_buffer,
277 static clib_error_t *
278 cli_clear_trace_buffer (vlib_main_t * vm,
279 unformat_input_t * input,
280 vlib_cli_command_t * cmd)
282 vlib_trace_main_t * tm = &vm->trace_main;
283 clear_trace_buffer (tm);
287 VLIB_CLI_COMMAND (clear_trace_cli,static) = {
288 .path = "clear trace",
289 .short_help = "Clear trace buffer and free memory",
290 .function = cli_clear_trace_buffer,
293 /* Dummy function to get us linked in. */
294 void vlib_trace_cli_reference (void) {}