FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
trace_funcs.h
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  * trace_funcs.h: VLIB trace buffer.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39 
40 #ifndef included_vlib_trace_funcs_h
41 #define included_vlib_trace_funcs_h
42 
44 
45 always_inline void
47 {
50 }
51 
53 
54 always_inline void *
57  u32 n_data_bytes)
58 {
61  u32 n_data_words, trace_index;
62 
64 
65  if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_IS_TRACED) == 0))
67 
68  if (PREDICT_FALSE (tm->add_trace_callback != 0))
69  {
70  return tm->add_trace_callback ((struct vlib_main_t *) vm,
71  (struct vlib_node_runtime_t *) r,
72  (struct vlib_buffer_t *) b,
73  n_data_bytes);
74  }
75  else if (PREDICT_FALSE (tm->trace_enable == 0))
76  {
77  ASSERT (vec_len (vnet_trace_placeholder) >= n_data_bytes + sizeof (*h));
79  }
80 
81  /* Are we trying to trace a handoff case? */
85 
86  /*
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.
94  */
95  trace_index = vlib_buffer_get_trace_index (b);
96  if (PREDICT_FALSE (pool_is_free_index (tm->trace_buffer_pool, trace_index)))
98 
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,
102  sizeof (h[0]));
103 
105  h->n_data = n_data_words;
106  h->node_index = r->node_index;
107 
108  return h->data;
109 }
110 
111 /* Non-inline (typical use-case) version of the above */
112 void *vlib_add_trace (vlib_main_t * vm,
114  u32 n_data_bytes);
115 
118 {
119  return h + 1 + h->n_data;
120 }
121 
122 always_inline void
124 {
126  u32 trace_index = vlib_buffer_get_trace_index (b);
127  vlib_validate_trace (tm, b);
128  _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
129  pool_put_index (tm->trace_buffer_pool, trace_index);
130 }
131 
132 always_inline void
135 {
136  vlib_next_frame_t *nf;
138  nf->flags |= VLIB_FRAME_TRACE;
139 }
140 
143  u32 classify_table_index, int func);
144 
145 
146 /*
147  * Mark buffer as traced and allocate trace buffer.
148  * return 1 if the buffer is successfully traced, 0 if not
149  * A buffer might not be traced if tracing is off or if the packet did not
150  * match the filter.
151  */
152 always_inline __clib_warn_unused_result int
155  u32 next_index, vlib_buffer_t * b, int follow_chain)
156 {
159 
160  if (PREDICT_FALSE (tm->trace_enable == 0))
161  return 0;
162 
163  /* Classifier filter in use? */
165  {
166  /* See if we're supposed to trace this packet... */
169  0 /* full classify */) != 1)
170  return 0;
171  }
172 
173  /*
174  * Apply filter to existing traces to keep number of allocated traces low.
175  * Performed each time around the main loop.
176  */
178  {
181 
182  if (tm->trace_buffer_callback)
183  (tm->trace_buffer_callback) ((struct vlib_main_t *) vm,
184  (struct vlib_trace_main_t *) tm);
185  }
186 
188 
190 
191  do
192  {
193  b->flags |= VLIB_BUFFER_IS_TRACED;
196  }
197  while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
198 
199  return 1;
200 }
201 
202 always_inline void
204  u32 bi_target)
205 {
206  vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
207  b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
208  b_target->trace_handle = b->trace_handle;
209 }
210 
213 {
215  vlib_trace_node_t *tn;
216 
217  if (rt->node_index >= vec_len (tm->nodes))
218  return 0;
219  tn = tm->nodes + rt->node_index;
220  ASSERT (tn->count <= tn->limit);
221 
222  return tn->limit - tn->count;
223 }
224 
225 always_inline void
227 {
229  vlib_trace_node_t *tn = vec_elt_at_index (tm->nodes, rt->node_index);
230 
231  ASSERT (count <= tn->limit);
232  tn->count = tn->limit - count;
233 }
234 
235 /* Helper function for nodes which only trace buffer data. */
236 void
239  u32 * buffers,
241  uword next_buffer_stride,
242  uword n_buffer_data_bytes_in_trace);
243 
244 #endif /* included_vlib_trace_funcs_h */
245 
246 /*
247  * fd.io coding-style-patch-verification: ON
248  *
249  * Local Variables:
250  * eval: (c-set-style "gnu")
251  * End:
252  */
vnet_trace_placeholder
u8 * vnet_trace_placeholder
Definition: trace.c:44
vlib_main_t::main_loop_count
volatile u32 main_loop_count
Definition: main.h:118
n_buffers
u32 n_buffers
Definition: interface_output.c:401
vlib_trace_next_frame
static void vlib_trace_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index)
Definition: trace_funcs.h:133
vnet_is_packet_traced
int vnet_is_packet_traced(vlib_buffer_t *b, u32 classify_table_index, int func)
Definition: vnet_classify.c:3042
next_index
nat44_ei_hairpin_src_next_t next_index
Definition: nat44_ei_hairpinning.c:412
vlib_trace_filter_t::classify_table_index
u32 classify_table_index
Definition: main.h:63
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vlib_trace_node_t::count
u32 count
Definition: trace.h:63
trace_apply_filter
void trace_apply_filter(vlib_main_t *vm)
Definition: trace.c:240
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vlib_get_trace_count
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:212
vlib_trace_node_t
Definition: trace.h:60
vlib_buffer_t::trace_handle
u32 trace_handle
Specifies trace buffer handle if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:172
vlib_buffer_get_trace_thread
static u32 vlib_buffer_get_trace_thread(vlib_buffer_t *b)
Extract the thread id from a trace handle.
Definition: buffer.h:404
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1071
pool_put_index
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:337
h
h
Definition: flowhash_template.h:372
vlib_buffer_copy_trace_flag
static void vlib_buffer_copy_trace_flag(vlib_main_t *vm, vlib_buffer_t *b, u32 bi_target)
Definition: trace_funcs.h:203
vlib_global_main_t::trace_filter
vlib_trace_filter_t trace_filter
Definition: main.h:304
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
round_pow2
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:279
vlib_validate_trace
static void vlib_validate_trace(vlib_trace_main_t *tm, vlib_buffer_t *b)
Definition: trace_funcs.h:46
count
u8 count
Definition: dhcp.api:208
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vlib_trace_header_next
static vlib_trace_header_t * vlib_trace_header_next(vlib_trace_header_t *h)
Definition: trace_funcs.h:117
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vlib_next_frame_t::flags
u32 flags
Definition: node.h:402
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_trace_filter_t::trace_filter_enable
u8 trace_filter_enable
Definition: main.h:62
vlib_global_main
vlib_global_main_t vlib_global_main
Definition: main.c:1786
uword
u64 uword
Definition: types.h:112
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:213
vlib_main_t::cpu_time_last_node_dispatch
u64 cpu_time_last_node_dispatch
Definition: main.h:112
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
vlib_trace_main_t::trace_buffer_callback
vlib_trace_buffer_callback_t * trace_buffer_callback
Definition: trace.h:107
vlib_set_trace_count
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:226
vlib_get_next_buffer
static vlib_buffer_t * vlib_get_next_buffer(vlib_main_t *vm, vlib_buffer_t *b)
Get next buffer in buffer linklist, or zero for end of list.
Definition: buffer_funcs.h:417
vlib_buffer_make_trace_handle
static u32 vlib_buffer_make_trace_handle(u32 thread, u32 pool_index)
Construct a trace handle from thread and pool index.
Definition: buffer.h:390
vlib_add_trace_inline
static void * vlib_add_trace_inline(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
vlib_trace_main_t::add_trace_callback
vlib_add_trace_callback_t * add_trace_callback
Definition: trace.h:110
vec_add2_aligned
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:656
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
vlib_trace_main_t::trace_buffer_pool
vlib_trace_header_t ** trace_buffer_pool
Definition: trace.h:86
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
vlib_trace_node_t::limit
u32 limit
Definition: trace.h:66
vlib_trace_main_t::last_main_loop_count
u32 last_main_loop_count
Definition: trace.h:88
vlib_add_handoff_trace
int vlib_add_handoff_trace(vlib_main_t *vm, vlib_buffer_t *b)
Definition: handoff_trace.c:100
vlib_trace_main_t::nodes
vlib_trace_node_t * nodes
Definition: trace.h:101
vlib_trace_frame_buffers_only
void vlib_trace_frame_buffers_only(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, uword n_buffers, uword next_buffer_stride, uword n_buffer_data_bytes_in_trace)
Definition: trace.c:48
vlib_main_t
Definition: main.h:102
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:399
vlib_trace_header_t
Definition: trace.h:45
vlib_free_trace
static void vlib_free_trace(vlib_main_t *vm, vlib_buffer_t *b)
Definition: trace_funcs.h:123
vlib_main_t::trace_main
vlib_trace_main_t trace_main
Definition: main.h:174
vlib_node_runtime_t
Definition: node.h:454
vlib_next_frame_t
Definition: node.h:393
vlib_trace_buffer
static __clib_warn_unused_result int vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:153
vlib_buffer_get_trace_index
static u32 vlib_buffer_get_trace_index(vlib_buffer_t *b)
Extract the trace (pool) index from a trace handle.
Definition: buffer.h:416
VLIB_FRAME_TRACE
#define VLIB_FRAME_TRACE
Definition: node.h:425
vlib_trace_main_t
Definition: trace.h:83
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
vlib_node_runtime_get_next_frame
static vlib_next_frame_t * vlib_node_runtime_get_next_frame(vlib_main_t *vm, vlib_node_runtime_t *n, u32 next_index)
Definition: node_funcs.h:321
vlib_trace_main_t::trace_enable
u32 trace_enable
Definition: trace.h:98
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
classify_table_index
u32 classify_table_index
Definition: fib_types.api:68