classify: vpp packet tracer support
[vpp.git] / src / vlib / trace_funcs.h
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
43 extern u8 *vnet_trace_dummy;
44
45 always_inline void
46 vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
47 {
48   ASSERT (!pool_is_free_index (tm->trace_buffer_pool,
49                                vlib_buffer_get_trace_index (b)));
50 }
51
52 void vlib_add_handoff_trace (vlib_main_t * vm, vlib_buffer_t * b);
53
54 always_inline void *
55 vlib_add_trace (vlib_main_t * vm,
56                 vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
57 {
58   vlib_trace_main_t *tm = &vm->trace_main;
59   vlib_trace_header_t *h;
60   u32 n_data_words;
61
62   ASSERT (vnet_trace_dummy);
63
64   if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_IS_TRACED) == 0))
65     return vnet_trace_dummy;
66
67   if (PREDICT_FALSE (tm->add_trace_callback != 0))
68     {
69       return tm->add_trace_callback ((struct vlib_main_t *) vm,
70                                      (struct vlib_node_runtime_t *) r,
71                                      (struct vlib_buffer_t *) b,
72                                      n_data_bytes);
73     }
74   else if (PREDICT_FALSE (tm->trace_enable == 0))
75     {
76       ASSERT (vec_len (vnet_trace_dummy) >= n_data_bytes + sizeof (*h));
77       return vnet_trace_dummy;
78     }
79
80   /* Are we trying to trace a handoff case? */
81   if (PREDICT_FALSE (vlib_buffer_get_trace_thread (b) != vm->thread_index))
82     vlib_add_handoff_trace (vm, b);
83
84   vlib_validate_trace (tm, b);
85
86   n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
87   n_data_words = n_data_bytes / sizeof (h[0]);
88   vec_add2_aligned (tm->trace_buffer_pool[vlib_buffer_get_trace_index (b)], h,
89                     1 + n_data_words, sizeof (h[0]));
90
91   h->time = vm->cpu_time_last_node_dispatch;
92   h->n_data = n_data_words;
93   h->node_index = r->node_index;
94
95   return h->data;
96 }
97
98 always_inline vlib_trace_header_t *
99 vlib_trace_header_next (vlib_trace_header_t * h)
100 {
101   return h + 1 + h->n_data;
102 }
103
104 always_inline void
105 vlib_free_trace (vlib_main_t * vm, vlib_buffer_t * b)
106 {
107   vlib_trace_main_t *tm = &vm->trace_main;
108   u32 trace_index = vlib_buffer_get_trace_index (b);
109   vlib_validate_trace (tm, b);
110   _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
111   pool_put_index (tm->trace_buffer_pool, trace_index);
112 }
113
114 always_inline void
115 vlib_trace_next_frame (vlib_main_t * vm,
116                        vlib_node_runtime_t * r, u32 next_index)
117 {
118   vlib_next_frame_t *nf;
119   nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
120   nf->flags |= VLIB_FRAME_TRACE;
121 }
122
123 void trace_apply_filter (vlib_main_t * vm);
124 int vnet_is_packet_traced (vlib_buffer_t * b,
125                            u32 classify_table_index, int func);
126
127
128 /* Mark buffer as traced and allocate trace buffer. */
129 always_inline void
130 vlib_trace_buffer (vlib_main_t * vm,
131                    vlib_node_runtime_t * r,
132                    u32 next_index, vlib_buffer_t * b, int follow_chain)
133 {
134   vlib_trace_main_t *tm = &vm->trace_main;
135   vlib_trace_header_t **h;
136
137   if (PREDICT_FALSE (tm->trace_enable == 0))
138     return;
139
140   /* Classifier filter in use? */
141   if (PREDICT_FALSE (vlib_global_main.trace_filter.trace_filter_enable))
142     {
143       /* See if we're supposed to trace this packet... */
144       if (vnet_is_packet_traced
145           (b, vlib_global_main.trace_filter.trace_classify_table_index,
146            0 /* full classify */ ) != 1)
147         return;
148     }
149
150   /*
151    * Apply filter to existing traces to keep number of allocated traces low.
152    * Performed each time around the main loop.
153    */
154   if (tm->last_main_loop_count != vm->main_loop_count)
155     {
156       tm->last_main_loop_count = vm->main_loop_count;
157       trace_apply_filter (vm);
158
159       if (tm->trace_buffer_callback)
160         (tm->trace_buffer_callback) ((struct vlib_main_t *) vm,
161                                      (struct vlib_trace_main_t *) tm);
162     }
163
164   vlib_trace_next_frame (vm, r, next_index);
165
166   pool_get (tm->trace_buffer_pool, h);
167
168   do
169     {
170       b->flags |= VLIB_BUFFER_IS_TRACED;
171       b->trace_handle = vlib_buffer_make_trace_handle
172         (vm->thread_index, h - tm->trace_buffer_pool);
173     }
174   while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
175 }
176
177 always_inline void
178 vlib_buffer_copy_trace_flag (vlib_main_t * vm, vlib_buffer_t * b,
179                              u32 bi_target)
180 {
181   vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
182   b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
183   b_target->trace_handle = b->trace_handle;
184 }
185
186 always_inline u32
187 vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
188 {
189   vlib_trace_main_t *tm = &vm->trace_main;
190   vlib_trace_node_t *tn;
191   int n;
192
193   if (rt->node_index >= vec_len (tm->nodes))
194     return 0;
195   tn = tm->nodes + rt->node_index;
196   n = tn->limit - tn->count;
197   ASSERT (n >= 0);
198
199   return n;
200 }
201
202 always_inline void
203 vlib_set_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt, u32 count)
204 {
205   vlib_trace_main_t *tm = &vm->trace_main;
206   vlib_trace_node_t *tn = vec_elt_at_index (tm->nodes, rt->node_index);
207
208   ASSERT (count <= tn->limit);
209   tn->count = tn->limit - count;
210 }
211
212 /* Helper function for nodes which only trace buffer data. */
213 void
214 vlib_trace_frame_buffers_only (vlib_main_t * vm,
215                                vlib_node_runtime_t * node,
216                                u32 * buffers,
217                                uword n_buffers,
218                                uword next_buffer_stride,
219                                uword n_buffer_data_bytes_in_trace);
220
221 #endif /* included_vlib_trace_funcs_h */
222
223 /*
224  * fd.io coding-style-patch-verification: ON
225  *
226  * Local Variables:
227  * eval: (c-set-style "gnu")
228  * End:
229  */