blob: 56cbfc7b6a2cc13be6a0d7a03e006d10a35c7ec8 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Dave Barachf8b85862018-08-17 18:29:07 -040043extern u8 *vnet_trace_dummy;
44
Ed Warnickecb9cada2015-12-08 15:45:58 -070045always_inline void
46vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
47{
Dave Barach9b8ffd92016-07-08 08:13:45 -040048 /*
49 * this assert seems right, but goes off constantly.
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 * disabling it appears to make the pain go away
51 */
52 ASSERT (1 || b->flags & VLIB_BUFFER_IS_TRACED);
Dave Barach9b8ffd92016-07-08 08:13:45 -040053 ASSERT (!pool_is_free_index (tm->trace_buffer_pool, b->trace_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -070054}
55
56always_inline void *
57vlib_add_trace (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040058 vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
Dave Barach9b8ffd92016-07-08 08:13:45 -040060 vlib_trace_main_t *tm = &vm->trace_main;
61 vlib_trace_header_t *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 u32 n_data_words;
63
Dave Barachf8b85862018-08-17 18:29:07 -040064 ASSERT (vnet_trace_dummy);
65
Gary Boon4681e072019-05-03 13:30:14 -040066 if (PREDICT_FALSE (tm->add_trace_callback != 0))
67 {
68 return tm->add_trace_callback ((struct vlib_main_t *) vm,
69 (struct vlib_node_runtime_t *) r,
70 (struct vlib_buffer_t *) b,
71 n_data_bytes);
72 }
73 else if (PREDICT_FALSE (tm->trace_enable == 0))
Dave Barachf8b85862018-08-17 18:29:07 -040074 {
75 ASSERT (vec_len (vnet_trace_dummy) >= n_data_bytes + sizeof (*h));
76 return vnet_trace_dummy;
77 }
78
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 vlib_validate_trace (tm, b);
80
81 n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
82 n_data_words = n_data_bytes / sizeof (h[0]);
83 vec_add2_aligned (tm->trace_buffer_pool[b->trace_index], h,
Dave Barach9b8ffd92016-07-08 08:13:45 -040084 1 + n_data_words, sizeof (h[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 h->time = vm->cpu_time_last_node_dispatch;
87 h->n_data = n_data_words;
88 h->node_index = r->node_index;
89
90 return h->data;
91}
Dave Barach9b8ffd92016-07-08 08:13:45 -040092
Ed Warnickecb9cada2015-12-08 15:45:58 -070093always_inline vlib_trace_header_t *
94vlib_trace_header_next (vlib_trace_header_t * h)
Dave Barach9b8ffd92016-07-08 08:13:45 -040095{
96 return h + 1 + h->n_data;
97}
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99always_inline void
100vlib_free_trace (vlib_main_t * vm, vlib_buffer_t * b)
101{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400102 vlib_trace_main_t *tm = &vm->trace_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 vlib_validate_trace (tm, b);
104 _vec_len (tm->trace_buffer_pool[b->trace_index]) = 0;
105 pool_put_index (tm->trace_buffer_pool, b->trace_index);
106}
107
108always_inline void
109vlib_trace_next_frame (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400110 vlib_node_runtime_t * r, u32 next_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400112 vlib_next_frame_t *nf;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
114 nf->flags |= VLIB_FRAME_TRACE;
115}
116
Bud Grise0bcc9d52016-02-02 14:23:29 -0500117void trace_apply_filter (vlib_main_t * vm);
118
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119/* Mark buffer as traced and allocate trace buffer. */
120always_inline void
121vlib_trace_buffer (vlib_main_t * vm,
122 vlib_node_runtime_t * r,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400123 u32 next_index, vlib_buffer_t * b, int follow_chain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400125 vlib_trace_main_t *tm = &vm->trace_main;
126 vlib_trace_header_t **h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Dave Barachf8b85862018-08-17 18:29:07 -0400128 if (PREDICT_FALSE (tm->trace_enable == 0))
129 return;
130
Bud Grise0bcc9d52016-02-02 14:23:29 -0500131 /*
132 * Apply filter to existing traces to keep number of allocated traces low.
133 * Performed each time around the main loop.
134 */
135 if (tm->last_main_loop_count != vm->main_loop_count)
136 {
137 tm->last_main_loop_count = vm->main_loop_count;
138 trace_apply_filter (vm);
Gary Boon4681e072019-05-03 13:30:14 -0400139
140 if (tm->trace_buffer_callback)
141 (tm->trace_buffer_callback) ((struct vlib_main_t *) vm,
142 (struct vlib_trace_main_t *) tm);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500143 }
144
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 vlib_trace_next_frame (vm, r, next_index);
146
147 pool_get (tm->trace_buffer_pool, h);
148
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149 do
150 {
151 b->flags |= VLIB_BUFFER_IS_TRACED;
152 b->trace_index = h - tm->trace_buffer_pool;
153 }
154 while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155}
156
157always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158vlib_buffer_copy_trace_flag (vlib_main_t * vm, vlib_buffer_t * b,
159 u32 bi_target)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
163 b_target->trace_index = b->trace_index;
164}
165
166always_inline u32
167vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
168{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400169 vlib_trace_main_t *tm = &vm->trace_main;
170 vlib_trace_node_t *tn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 int n;
172
173 if (rt->node_index >= vec_len (tm->nodes))
174 return 0;
175 tn = tm->nodes + rt->node_index;
176 n = tn->limit - tn->count;
177 ASSERT (n >= 0);
178
179 return n;
180}
181
182always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183vlib_set_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt, u32 count)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400185 vlib_trace_main_t *tm = &vm->trace_main;
186 vlib_trace_node_t *tn = vec_elt_at_index (tm->nodes, rt->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 ASSERT (count <= tn->limit);
189 tn->count = tn->limit - count;
190}
191
192/* Helper function for nodes which only trace buffer data. */
193void
194vlib_trace_frame_buffers_only (vlib_main_t * vm,
195 vlib_node_runtime_t * node,
196 u32 * buffers,
197 uword n_buffers,
198 uword next_buffer_stride,
199 uword n_buffer_data_bytes_in_trace);
200
201#endif /* included_vlib_trace_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202
203/*
204 * fd.io coding-style-patch-verification: ON
205 *
206 * Local Variables:
207 * eval: (c-set-style "gnu")
208 * End:
209 */