blob: 5280eae9904874556da71458e156725c03804a69 [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
43always_inline void
44vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
45{
Dave Barach9b8ffd92016-07-08 08:13:45 -040046 /*
47 * this assert seems right, but goes off constantly.
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 * disabling it appears to make the pain go away
49 */
50 ASSERT (1 || b->flags & VLIB_BUFFER_IS_TRACED);
Dave Barach9b8ffd92016-07-08 08:13:45 -040051 ASSERT (!pool_is_free_index (tm->trace_buffer_pool, b->trace_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -070052}
53
54always_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
62 vlib_validate_trace (tm, b);
63
64 n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
65 n_data_words = n_data_bytes / sizeof (h[0]);
66 vec_add2_aligned (tm->trace_buffer_pool[b->trace_index], h,
Dave Barach9b8ffd92016-07-08 08:13:45 -040067 1 + n_data_words, sizeof (h[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
69 h->time = vm->cpu_time_last_node_dispatch;
70 h->n_data = n_data_words;
71 h->node_index = r->node_index;
72
73 return h->data;
74}
Dave Barach9b8ffd92016-07-08 08:13:45 -040075
Ed Warnickecb9cada2015-12-08 15:45:58 -070076always_inline vlib_trace_header_t *
77vlib_trace_header_next (vlib_trace_header_t * h)
Dave Barach9b8ffd92016-07-08 08:13:45 -040078{
79 return h + 1 + h->n_data;
80}
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
82always_inline void
83vlib_free_trace (vlib_main_t * vm, vlib_buffer_t * b)
84{
Dave Barach9b8ffd92016-07-08 08:13:45 -040085 vlib_trace_main_t *tm = &vm->trace_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 vlib_validate_trace (tm, b);
87 _vec_len (tm->trace_buffer_pool[b->trace_index]) = 0;
88 pool_put_index (tm->trace_buffer_pool, b->trace_index);
89}
90
91always_inline void
92vlib_trace_next_frame (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040093 vlib_node_runtime_t * r, u32 next_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
Dave Barach9b8ffd92016-07-08 08:13:45 -040095 vlib_next_frame_t *nf;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
97 nf->flags |= VLIB_FRAME_TRACE;
98}
99
Bud Grise0bcc9d52016-02-02 14:23:29 -0500100void trace_apply_filter (vlib_main_t * vm);
101
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102/* Mark buffer as traced and allocate trace buffer. */
103always_inline void
104vlib_trace_buffer (vlib_main_t * vm,
105 vlib_node_runtime_t * r,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400106 u32 next_index, vlib_buffer_t * b, int follow_chain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400108 vlib_trace_main_t *tm = &vm->trace_main;
109 vlib_trace_header_t **h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Bud Grise0bcc9d52016-02-02 14:23:29 -0500111 /*
112 * Apply filter to existing traces to keep number of allocated traces low.
113 * Performed each time around the main loop.
114 */
115 if (tm->last_main_loop_count != vm->main_loop_count)
116 {
117 tm->last_main_loop_count = vm->main_loop_count;
118 trace_apply_filter (vm);
119 }
120
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 vlib_trace_next_frame (vm, r, next_index);
122
123 pool_get (tm->trace_buffer_pool, h);
124
Dave Barach9b8ffd92016-07-08 08:13:45 -0400125 do
126 {
127 b->flags |= VLIB_BUFFER_IS_TRACED;
128 b->trace_index = h - tm->trace_buffer_pool;
129 }
130 while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131}
132
133always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400134vlib_buffer_copy_trace_flag (vlib_main_t * vm, vlib_buffer_t * b,
135 u32 bi_target)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137 vlib_buffer_t *b_target = vlib_get_buffer (vm, bi_target);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
139 b_target->trace_index = b->trace_index;
140}
141
142always_inline u32
143vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
144{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145 vlib_trace_main_t *tm = &vm->trace_main;
146 vlib_trace_node_t *tn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 int n;
148
149 if (rt->node_index >= vec_len (tm->nodes))
150 return 0;
151 tn = tm->nodes + rt->node_index;
152 n = tn->limit - tn->count;
153 ASSERT (n >= 0);
154
155 return n;
156}
157
158always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400159vlib_set_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt, u32 count)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 vlib_trace_main_t *tm = &vm->trace_main;
162 vlib_trace_node_t *tn = vec_elt_at_index (tm->nodes, rt->node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
164 ASSERT (count <= tn->limit);
165 tn->count = tn->limit - count;
166}
167
168/* Helper function for nodes which only trace buffer data. */
169void
170vlib_trace_frame_buffers_only (vlib_main_t * vm,
171 vlib_node_runtime_t * node,
172 u32 * buffers,
173 uword n_buffers,
174 uword next_buffer_stride,
175 uword n_buffer_data_bytes_in_trace);
176
177#endif /* included_vlib_trace_funcs_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400178
179/*
180 * fd.io coding-style-patch-verification: ON
181 *
182 * Local Variables:
183 * eval: (c-set-style "gnu")
184 * End:
185 */