blob: 257c3a3a4ff2a054b47d00dfe7b30bbeeafa6682 [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 Baracha638c182019-06-21 18:24:07 -040048 ASSERT (!pool_is_free_index (tm->trace_buffer_pool,
49 vlib_buffer_get_trace_index (b)));
Ed Warnickecb9cada2015-12-08 15:45:58 -070050}
51
Dave Baracha638c182019-06-21 18:24:07 -040052void vlib_add_handoff_trace (vlib_main_t * vm, vlib_buffer_t * b);
53
Ed Warnickecb9cada2015-12-08 15:45:58 -070054always_inline void *
55vlib_add_trace (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040056 vlib_node_runtime_t * r, vlib_buffer_t * b, u32 n_data_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
Dave Barach9b8ffd92016-07-08 08:13:45 -040058 vlib_trace_main_t *tm = &vm->trace_main;
59 vlib_trace_header_t *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060 u32 n_data_words;
61
Dave Barachf8b85862018-08-17 18:29:07 -040062 ASSERT (vnet_trace_dummy);
63
Gary Boon4681e072019-05-03 13:30:14 -040064 if (PREDICT_FALSE (tm->add_trace_callback != 0))
65 {
66 return tm->add_trace_callback ((struct vlib_main_t *) vm,
67 (struct vlib_node_runtime_t *) r,
68 (struct vlib_buffer_t *) b,
69 n_data_bytes);
70 }
71 else if (PREDICT_FALSE (tm->trace_enable == 0))
Dave Barachf8b85862018-08-17 18:29:07 -040072 {
73 ASSERT (vec_len (vnet_trace_dummy) >= n_data_bytes + sizeof (*h));
74 return vnet_trace_dummy;
75 }
76
Dave Baracha638c182019-06-21 18:24:07 -040077 /* Are we trying to trace a handoff case? */
78 if (PREDICT_FALSE (vlib_buffer_get_trace_thread (b) != vm->thread_index))
79 vlib_add_handoff_trace (vm, b);
80
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 vlib_validate_trace (tm, b);
82
83 n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
84 n_data_words = n_data_bytes / sizeof (h[0]);
Dave Baracha638c182019-06-21 18:24:07 -040085 vec_add2_aligned (tm->trace_buffer_pool[vlib_buffer_get_trace_index (b)], h,
Dave Barach9b8ffd92016-07-08 08:13:45 -040086 1 + n_data_words, sizeof (h[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
88 h->time = vm->cpu_time_last_node_dispatch;
89 h->n_data = n_data_words;
90 h->node_index = r->node_index;
91
92 return h->data;
93}
Dave Barach9b8ffd92016-07-08 08:13:45 -040094
Ed Warnickecb9cada2015-12-08 15:45:58 -070095always_inline vlib_trace_header_t *
96vlib_trace_header_next (vlib_trace_header_t * h)
Dave Barach9b8ffd92016-07-08 08:13:45 -040097{
98 return h + 1 + h->n_data;
99}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101always_inline void
102vlib_free_trace (vlib_main_t * vm, vlib_buffer_t * b)
103{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400104 vlib_trace_main_t *tm = &vm->trace_main;
Dave Baracha638c182019-06-21 18:24:07 -0400105 u32 trace_index = vlib_buffer_get_trace_index (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 vlib_validate_trace (tm, b);
Dave Baracha638c182019-06-21 18:24:07 -0400107 _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
108 pool_put_index (tm->trace_buffer_pool, trace_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109}
110
111always_inline void
112vlib_trace_next_frame (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400113 vlib_node_runtime_t * r, u32 next_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400115 vlib_next_frame_t *nf;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
117 nf->flags |= VLIB_FRAME_TRACE;
118}
119
Bud Grise0bcc9d52016-02-02 14:23:29 -0500120void trace_apply_filter (vlib_main_t * vm);
121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122/* Mark buffer as traced and allocate trace buffer. */
123always_inline void
124vlib_trace_buffer (vlib_main_t * vm,
125 vlib_node_runtime_t * r,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400126 u32 next_index, vlib_buffer_t * b, int follow_chain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400128 vlib_trace_main_t *tm = &vm->trace_main;
129 vlib_trace_header_t **h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Dave Barachf8b85862018-08-17 18:29:07 -0400131 if (PREDICT_FALSE (tm->trace_enable == 0))
132 return;
133
Bud Grise0bcc9d52016-02-02 14:23:29 -0500134 /*
135 * Apply filter to existing traces to keep number of allocated traces low.
136 * Performed each time around the main loop.
137 */
138 if (tm->last_main_loop_count != vm->main_loop_count)
139 {
140 tm->last_main_loop_count = vm->main_loop_count;
141 trace_apply_filter (vm);
Gary Boon4681e072019-05-03 13:30:14 -0400142
143 if (tm->trace_buffer_callback)
144 (tm->trace_buffer_callback) ((struct vlib_main_t *) vm,
145 (struct vlib_trace_main_t *) tm);
Bud Grise0bcc9d52016-02-02 14:23:29 -0500146 }
147
Neale Ranns6b03ab72019-07-30 07:14:25 -0700148 vlib_trace_next_frame (vm, r, next_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
150 pool_get (tm->trace_buffer_pool, h);
151
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 do
153 {
154 b->flags |= VLIB_BUFFER_IS_TRACED;
Dave Baracha638c182019-06-21 18:24:07 -0400155 b->trace_handle = vlib_buffer_make_trace_handle
156 (vm->thread_index, h - tm->trace_buffer_pool);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157 }
158 while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159}
160
161always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400162vlib_buffer_copy_trace_flag (vlib_main_t * vm, vlib_buffer_t * b,
163 u32 bi_target)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
Dave Baracha638c182019-06-21 18:24:07 -0400167 b_target->trace_handle = b->trace_handle;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168}
169
170always_inline u32
171vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
172{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400173 vlib_trace_main_t *tm = &vm->trace_main;
174 vlib_trace_node_t *tn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 int n;
176
177 if (rt->node_index >= vec_len (tm->nodes))
178 return 0;
179 tn = tm->nodes + rt->node_index;
180 n = tn->limit - tn->count;
181 ASSERT (n >= 0);
182
183 return n;
184}
185
186always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400187vlib_set_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt, u32 count)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400189 vlib_trace_main_t *tm = &vm->trace_main;
190 vlib_trace_node_t *tn = vec_elt_at_index (tm->nodes, rt->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
192 ASSERT (count <= tn->limit);
193 tn->count = tn->limit - count;
194}
195
196/* Helper function for nodes which only trace buffer data. */
197void
198vlib_trace_frame_buffers_only (vlib_main_t * vm,
199 vlib_node_runtime_t * node,
200 u32 * buffers,
201 uword n_buffers,
202 uword next_buffer_stride,
203 uword n_buffer_data_bytes_in_trace);
204
205#endif /* included_vlib_trace_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400206
207/*
208 * fd.io coding-style-patch-verification: ON
209 *
210 * Local Variables:
211 * eval: (c-set-style "gnu")
212 * End:
213 */