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_placeholder;
46 vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
48 ASSERT (!pool_is_free_index (tm->trace_buffer_pool,
49 vlib_buffer_get_trace_index (b)));
52 int vlib_add_handoff_trace (vlib_main_t * vm, vlib_buffer_t * b);
55 vlib_add_trace_inline (vlib_main_t * vm,
56 vlib_node_runtime_t * r, vlib_buffer_t * b,
59 vlib_trace_main_t *tm = &vm->trace_main;
60 vlib_trace_header_t *h;
61 u32 n_data_words, trace_index;
63 ASSERT (vnet_trace_placeholder);
65 if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_IS_TRACED) == 0))
66 return vnet_trace_placeholder;
68 if (PREDICT_FALSE (tm->add_trace_callback != 0))
70 return tm->add_trace_callback ((struct vlib_main_t *) vm,
71 (struct vlib_node_runtime_t *) r,
72 (struct vlib_buffer_t *) b,
75 else if (PREDICT_FALSE (tm->trace_enable == 0))
77 ASSERT (vec_len (vnet_trace_placeholder) >= n_data_bytes + sizeof (*h));
78 return vnet_trace_placeholder;
81 /* Are we trying to trace a handoff case? */
82 if (PREDICT_FALSE (vlib_buffer_get_trace_thread (b) != vm->thread_index))
83 if (PREDICT_FALSE (!vlib_add_handoff_trace (vm, b)))
84 return vnet_trace_placeholder;
87 * there is a small chance of a race condition with 'clear trace' here: if a
88 * buffer was set to be traced before the 'clear trace' and is still going
89 * through the graph after the 'clear trace', its trace_index is staled as
90 * the pool was destroyed.
91 * The pool may have been re-allocated because of a new traced buffer, and
92 * the trace_index might be valid by pure (bad) luck. In that case the trace
93 * will be a mix of both buffer traces, but this should be acceptable.
95 trace_index = vlib_buffer_get_trace_index (b);
96 if (PREDICT_FALSE (pool_is_free_index (tm->trace_buffer_pool, trace_index)))
97 return vnet_trace_placeholder;
99 n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
100 n_data_words = n_data_bytes / sizeof (h[0]);
101 vec_add2_aligned (tm->trace_buffer_pool[trace_index], h, 1 + n_data_words,
104 h->time = vm->cpu_time_last_node_dispatch;
105 h->n_data = n_data_words;
106 h->node_index = r->node_index;
111 /* Non-inline (typical use-case) version of the above */
112 void *vlib_add_trace (vlib_main_t * vm,
113 vlib_node_runtime_t * r, vlib_buffer_t * b,
116 always_inline vlib_trace_header_t *
117 vlib_trace_header_next (vlib_trace_header_t * h)
119 return h + 1 + h->n_data;
123 vlib_free_trace (vlib_main_t * vm, vlib_buffer_t * b)
125 vlib_trace_main_t *tm = &vm->trace_main;
126 u32 trace_index = vlib_buffer_get_trace_index (b);
127 vlib_validate_trace (tm, b);
128 vec_set_len (tm->trace_buffer_pool[trace_index], 0);
129 pool_put_index (tm->trace_buffer_pool, trace_index);
133 vlib_trace_next_frame (vlib_main_t * vm,
134 vlib_node_runtime_t * r, u32 next_index)
136 vlib_next_frame_t *nf;
137 nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
138 nf->flags |= VLIB_FRAME_TRACE;
141 void trace_apply_filter (vlib_main_t *vm);
144 * Mark buffer as traced and allocate trace buffer.
145 * return 1 if the buffer is successfully traced, 0 if not
146 * A buffer might not be traced if tracing is off or if the packet did not
149 always_inline __clib_warn_unused_result int
150 vlib_trace_buffer (vlib_main_t * vm,
151 vlib_node_runtime_t * r,
152 u32 next_index, vlib_buffer_t * b, int follow_chain)
154 vlib_trace_main_t *tm = &vm->trace_main;
155 vlib_trace_header_t **h;
157 if (PREDICT_FALSE (tm->trace_enable == 0))
160 /* Classifier filter in use? */
161 if (PREDICT_FALSE (vlib_global_main.trace_filter.trace_filter_enable))
163 /* See if we're supposed to trace this packet... */
164 if (tm->current_trace_filter_function (
165 b, vlib_global_main.trace_filter.classify_table_index,
166 0 /* full classify */) != 1)
171 * Apply filter to existing traces to keep number of allocated traces low.
172 * Performed each time around the main loop.
174 if (tm->last_main_loop_count != vm->main_loop_count)
176 tm->last_main_loop_count = vm->main_loop_count;
177 trace_apply_filter (vm);
179 if (tm->trace_buffer_callback)
180 (tm->trace_buffer_callback) ((struct vlib_main_t *) vm,
181 (struct vlib_trace_main_t *) tm);
184 vlib_trace_next_frame (vm, r, next_index);
186 pool_get (tm->trace_buffer_pool, h);
190 b->flags |= VLIB_BUFFER_IS_TRACED;
191 b->trace_handle = vlib_buffer_make_trace_handle
192 (vm->thread_index, h - tm->trace_buffer_pool);
194 while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
200 vlib_buffer_copy_trace_flag (vlib_main_t * vm, vlib_buffer_t * b,
203 vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
204 b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
205 b_target->trace_handle = b->trace_handle;
209 vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
211 vlib_trace_main_t *tm = &vm->trace_main;
212 vlib_trace_node_t *tn;
214 if (rt->node_index >= vec_len (tm->nodes))
216 tn = tm->nodes + rt->node_index;
217 ASSERT (tn->count <= tn->limit);
219 return tn->limit - tn->count;
223 vlib_set_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt, u32 count)
225 vlib_trace_main_t *tm = &vm->trace_main;
226 vlib_trace_node_t *tn = vec_elt_at_index (tm->nodes, rt->node_index);
228 ASSERT (count <= tn->limit);
229 tn->count = tn->limit - count;
232 /* Helper function for nodes which only trace buffer data. */
234 vlib_trace_frame_buffers_only (vlib_main_t * vm,
235 vlib_node_runtime_t * node,
238 uword next_buffer_stride,
239 uword n_buffer_data_bytes_in_trace);
241 #endif /* included_vlib_trace_funcs_h */
244 * fd.io coding-style-patch-verification: ON
247 * eval: (c-set-style "gnu")