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_funcs.h: 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 #ifndef included_vlib_trace_funcs_h
41 #define included_vlib_trace_funcs_h
43 extern u8 *vnet_trace_dummy;
46 vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
49 * this assert seems right, but goes off constantly.
50 * disabling it appears to make the pain go away
52 ASSERT (1 || b->flags & VLIB_BUFFER_IS_TRACED);
53 ASSERT (!pool_is_free_index (tm->trace_buffer_pool, b->trace_index));
57 vlib_add_trace (vlib_main_t * vm,
58 vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
60 vlib_trace_main_t *tm = &vm->trace_main;
61 vlib_trace_header_t *h;
64 ASSERT (vnet_trace_dummy);
66 if (PREDICT_FALSE (tm->trace_enable == 0))
68 ASSERT (vec_len (vnet_trace_dummy) >= n_data_bytes + sizeof (*h));
69 return vnet_trace_dummy;
72 vlib_validate_trace (tm, b);
74 n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
75 n_data_words = n_data_bytes / sizeof (h[0]);
76 vec_add2_aligned (tm->trace_buffer_pool[b->trace_index], h,
77 1 + n_data_words, sizeof (h[0]));
79 h->time = vm->cpu_time_last_node_dispatch;
80 h->n_data = n_data_words;
81 h->node_index = r->node_index;
86 always_inline vlib_trace_header_t *
87 vlib_trace_header_next (vlib_trace_header_t * h)
89 return h + 1 + h->n_data;
93 vlib_free_trace (vlib_main_t * vm, vlib_buffer_t * b)
95 vlib_trace_main_t *tm = &vm->trace_main;
96 vlib_validate_trace (tm, b);
97 _vec_len (tm->trace_buffer_pool[b->trace_index]) = 0;
98 pool_put_index (tm->trace_buffer_pool, b->trace_index);
102 vlib_trace_next_frame (vlib_main_t * vm,
103 vlib_node_runtime_t * r, u32 next_index)
105 vlib_next_frame_t *nf;
106 nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
107 nf->flags |= VLIB_FRAME_TRACE;
110 void trace_apply_filter (vlib_main_t * vm);
112 /* Mark buffer as traced and allocate trace buffer. */
114 vlib_trace_buffer (vlib_main_t * vm,
115 vlib_node_runtime_t * r,
116 u32 next_index, vlib_buffer_t * b, int follow_chain)
118 vlib_trace_main_t *tm = &vm->trace_main;
119 vlib_trace_header_t **h;
121 if (PREDICT_FALSE (tm->trace_enable == 0))
125 * Apply filter to existing traces to keep number of allocated traces low.
126 * Performed each time around the main loop.
128 if (tm->last_main_loop_count != vm->main_loop_count)
130 tm->last_main_loop_count = vm->main_loop_count;
131 trace_apply_filter (vm);
134 vlib_trace_next_frame (vm, r, next_index);
136 pool_get (tm->trace_buffer_pool, h);
140 b->flags |= VLIB_BUFFER_IS_TRACED;
141 b->trace_index = h - tm->trace_buffer_pool;
143 while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
147 vlib_buffer_copy_trace_flag (vlib_main_t * vm, vlib_buffer_t * b,
150 vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
151 b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
152 b_target->trace_index = b->trace_index;
156 vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
158 vlib_trace_main_t *tm = &vm->trace_main;
159 vlib_trace_node_t *tn;
162 if (rt->node_index >= vec_len (tm->nodes))
164 tn = tm->nodes + rt->node_index;
165 n = tn->limit - tn->count;
172 vlib_set_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt, u32 count)
174 vlib_trace_main_t *tm = &vm->trace_main;
175 vlib_trace_node_t *tn = vec_elt_at_index (tm->nodes, rt->node_index);
177 ASSERT (count <= tn->limit);
178 tn->count = tn->limit - count;
181 /* Helper function for nodes which only trace buffer data. */
183 vlib_trace_frame_buffers_only (vlib_main_t * vm,
184 vlib_node_runtime_t * node,
187 uword next_buffer_stride,
188 uword n_buffer_data_bytes_in_trace);
190 #endif /* included_vlib_trace_funcs_h */
193 * fd.io coding-style-patch-verification: ON
196 * eval: (c-set-style "gnu")