FD.io VPP  v21.01.1
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 {
59  vlib_trace_main_t *tm = &vm->trace_main;
61  u32 n_data_words;
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  vlib_validate_trace (tm, b);
87 
88  n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
89  n_data_words = n_data_bytes / sizeof (h[0]);
91  1 + n_data_words, sizeof (h[0]));
92 
94  h->n_data = n_data_words;
95  h->node_index = r->node_index;
96 
97  return h->data;
98 }
99 
100 /* Non-inline (typical use-case) version of the above */
101 void *vlib_add_trace (vlib_main_t * vm,
103  u32 n_data_bytes);
104 
107 {
108  return h + 1 + h->n_data;
109 }
110 
111 always_inline void
113 {
114  vlib_trace_main_t *tm = &vm->trace_main;
115  u32 trace_index = vlib_buffer_get_trace_index (b);
116  vlib_validate_trace (tm, b);
117  _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
118  pool_put_index (tm->trace_buffer_pool, trace_index);
119 }
120 
121 always_inline void
123  vlib_node_runtime_t * r, u32 next_index)
124 {
125  vlib_next_frame_t *nf;
126  nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
127  nf->flags |= VLIB_FRAME_TRACE;
128 }
129 
130 void trace_apply_filter (vlib_main_t * vm);
132  u32 classify_table_index, int func);
133 
134 
135 /*
136  * Mark buffer as traced and allocate trace buffer.
137  * return 1 if the buffer is successfully traced, 0 if not
138  * A buffer might not be traced if tracing is off or if the packet did not
139  * match the filter.
140  */
141 always_inline __clib_warn_unused_result int
144  u32 next_index, vlib_buffer_t * b, int follow_chain)
145 {
146  vlib_trace_main_t *tm = &vm->trace_main;
148 
149  if (PREDICT_FALSE (tm->trace_enable == 0))
150  return 0;
151 
152  /* Classifier filter in use? */
154  {
155  /* See if we're supposed to trace this packet... */
158  0 /* full classify */ ) != 1)
159  return 0;
160  }
161 
162  /*
163  * Apply filter to existing traces to keep number of allocated traces low.
164  * Performed each time around the main loop.
165  */
166  if (tm->last_main_loop_count != vm->main_loop_count)
167  {
169  trace_apply_filter (vm);
170 
171  if (tm->trace_buffer_callback)
172  (tm->trace_buffer_callback) ((struct vlib_main_t *) vm,
173  (struct vlib_trace_main_t *) tm);
174  }
175 
176  vlib_trace_next_frame (vm, r, next_index);
177 
178  pool_get (tm->trace_buffer_pool, h);
179 
180  do
181  {
182  b->flags |= VLIB_BUFFER_IS_TRACED;
184  (vm->thread_index, h - tm->trace_buffer_pool);
185  }
186  while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
187 
188  return 1;
189 }
190 
191 always_inline void
193  u32 bi_target)
194 {
195  vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
196  b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
197  b_target->trace_handle = b->trace_handle;
198 }
199 
202 {
203  vlib_trace_main_t *tm = &vm->trace_main;
204  vlib_trace_node_t *tn;
205 
206  if (rt->node_index >= vec_len (tm->nodes))
207  return 0;
208  tn = tm->nodes + rt->node_index;
209  ASSERT (tn->count <= tn->limit);
210 
211  return tn->limit - tn->count;
212 }
213 
214 always_inline void
216 {
217  vlib_trace_main_t *tm = &vm->trace_main;
219 
220  ASSERT (count <= tn->limit);
221  tn->count = tn->limit - count;
222 }
223 
224 /* Helper function for nodes which only trace buffer data. */
225 void
227  vlib_node_runtime_t * node,
228  u32 * buffers,
229  uword n_buffers,
230  uword next_buffer_stride,
231  uword n_buffer_data_bytes_in_trace);
232 
233 #endif /* included_vlib_trace_funcs_h */
234 
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */
volatile u32 main_loop_count
Definition: main.h:135
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
vlib_main_t vlib_global_main
Definition: main.c:2041
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
static u32 vlib_buffer_make_trace_handle(u32 thread, u32 pool_index)
Construct a trace handle from thread and pool index.
Definition: buffer.h:366
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:201
u32 classify_table_index
Definition: main.h:80
static u32 vlib_buffer_get_trace_index(vlib_buffer_t *b)
Extract the trace (pool) index from a trace handle.
Definition: buffer.h:392
vlib_trace_node_t * nodes
Definition: trace.h:101
static vlib_trace_header_t * vlib_trace_header_next(vlib_trace_header_t *h)
Definition: trace_funcs.h:106
#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:642
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
u32 thread_index
Definition: main.h:250
vlib_main_t * vm
Definition: in2out_ed.c:1580
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:634
vlib_trace_buffer_callback_t * trace_buffer_callback
Definition: trace.h:107
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:251
unsigned char u8
Definition: types.h:56
vlib_trace_header_t ** trace_buffer_pool
Definition: trace.h:86
u32 last_main_loop_count
Definition: trace.h:88
u32 trace_enable
Definition: trace.h:98
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static void vlib_trace_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index)
Definition: trace_funcs.h:122
unsigned int u32
Definition: types.h:88
u8 trace_filter_enable
Definition: main.h:79
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:317
static void vlib_free_trace(vlib_main_t *vm, vlib_buffer_t *b)
Definition: trace_funcs.h:112
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:142
vec_header_t h
Definition: buffer.c:322
u8 * vnet_trace_placeholder
Definition: trace.c:44
int vlib_add_handoff_trace(vlib_main_t *vm, vlib_buffer_t *b)
u32 trace_handle
Specifies trace buffer handle if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:163
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define always_inline
Definition: ipsec.h:28
vlib_add_trace_callback_t * add_trace_callback
Definition: trace.h:110
u32 node_index
Node index.
Definition: node.h:488
u32 classify_table_index
Definition: fib_types.api:68
int vnet_is_packet_traced(vlib_buffer_t *b, u32 classify_table_index, int func)
static void vlib_buffer_copy_trace_flag(vlib_main_t *vm, vlib_buffer_t *b, u32 bi_target)
Definition: trace_funcs.h:192
void trace_apply_filter(vlib_main_t *vm)
Definition: trace.c:242
static u32 vlib_buffer_get_trace_thread(vlib_buffer_t *b)
Extract the thread id from a trace handle.
Definition: buffer.h:380
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:386
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:298
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:265
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:330
#define ASSERT(truth)
vlib_trace_main_t trace_main
Definition: main.h:194
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
#define VLIB_FRAME_TRACE
Definition: node.h:435
vlib_trace_filter_t trace_filter
Definition: main.h:207
u8 count
Definition: dhcp.api:208
static void vlib_validate_trace(vlib_trace_main_t *tm, vlib_buffer_t *b)
Definition: trace_funcs.h:46
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:215
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
u64 cpu_time_last_node_dispatch
Definition: main.h:129