Initial commit of vpp code.
[vpp.git] / vlib / 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 always_inline void
44 vlib_validate_trace (vlib_trace_main_t * tm, vlib_buffer_t * b)
45 {
46   /* 
47    * this assert seems right, but goes off constantly. 
48    * disabling it appears to make the pain go away
49    */
50   ASSERT (1 || b->flags & VLIB_BUFFER_IS_TRACED);
51   ASSERT (! pool_is_free_index (tm->trace_buffer_pool, b->trace_index));
52 }
53
54 always_inline void *
55 vlib_add_trace (vlib_main_t * vm,
56                 vlib_node_runtime_t * r,
57                 vlib_buffer_t * b,
58                 u32 n_data_bytes)
59 {
60   vlib_trace_main_t * tm = &vm->trace_main;
61   vlib_trace_header_t * h;
62   u32 n_data_words;
63
64   vlib_validate_trace (tm, b);
65
66   n_data_bytes = round_pow2 (n_data_bytes, sizeof (h[0]));
67   n_data_words = n_data_bytes / sizeof (h[0]);
68   vec_add2_aligned (tm->trace_buffer_pool[b->trace_index], h,
69                     1 + n_data_words,
70                     sizeof (h[0]));
71
72   h->time = vm->cpu_time_last_node_dispatch;
73   h->n_data = n_data_words;
74   h->node_index = r->node_index;
75
76   return h->data;
77 }
78                                       
79 always_inline vlib_trace_header_t *
80 vlib_trace_header_next (vlib_trace_header_t * h)
81 { return h + 1 + h->n_data; }
82
83 always_inline void
84 vlib_free_trace (vlib_main_t * vm, vlib_buffer_t * b)
85 {
86   vlib_trace_main_t * tm = &vm->trace_main;
87   vlib_validate_trace (tm, b);
88   _vec_len (tm->trace_buffer_pool[b->trace_index]) = 0;
89   pool_put_index (tm->trace_buffer_pool, b->trace_index);
90 }
91
92 always_inline void
93 vlib_trace_next_frame (vlib_main_t * vm,
94                        vlib_node_runtime_t * r,
95                        u32 next_index)
96 {
97   vlib_next_frame_t * nf;
98   nf = vlib_node_runtime_get_next_frame (vm, r, next_index);
99   nf->flags |= VLIB_FRAME_TRACE;
100 }
101
102 /* Mark buffer as traced and allocate trace buffer. */
103 always_inline void
104 vlib_trace_buffer (vlib_main_t * vm,
105                    vlib_node_runtime_t * r,
106                    u32 next_index,
107                    vlib_buffer_t * b,
108                    int follow_chain)
109 {
110   vlib_trace_main_t * tm = &vm->trace_main;
111   vlib_trace_header_t ** h;
112
113   vlib_trace_next_frame (vm, r, next_index);
114
115   pool_get (tm->trace_buffer_pool, h);
116
117   do {
118     b->flags |= VLIB_BUFFER_IS_TRACED;
119     b->trace_index = h - tm->trace_buffer_pool;
120   } while (follow_chain && (b = vlib_get_next_buffer (vm, b)));
121 }
122
123 always_inline void
124 vlib_buffer_copy_trace_flag (vlib_main_t * vm, vlib_buffer_t * b, u32 bi_target)
125 {
126   vlib_buffer_t * b_target = vlib_get_buffer (vm, bi_target);
127   b_target->flags |= b->flags & VLIB_BUFFER_IS_TRACED;
128   b_target->trace_index = b->trace_index;
129 }
130
131 always_inline u32
132 vlib_get_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt)
133 {
134   vlib_trace_main_t * tm = &vm->trace_main;
135   vlib_trace_node_t * tn;
136   int n;
137
138   if (rt->node_index >= vec_len (tm->nodes))
139     return 0;
140   tn = tm->nodes + rt->node_index;
141   n = tn->limit - tn->count;
142   ASSERT (n >= 0);
143
144   return n;
145 }
146
147 always_inline void
148 vlib_set_trace_count (vlib_main_t * vm, vlib_node_runtime_t * rt,
149                       u32 count)
150 {
151   vlib_trace_main_t * tm = &vm->trace_main;
152   vlib_trace_node_t * tn = vec_elt_at_index (tm->nodes, rt->node_index);
153
154   ASSERT (count <= tn->limit);
155   tn->count = tn->limit - count;
156 }
157
158 /* Helper function for nodes which only trace buffer data. */
159 void
160 vlib_trace_frame_buffers_only (vlib_main_t * vm,
161                                vlib_node_runtime_t * node,
162                                u32 * buffers,
163                                uword n_buffers,
164                                uword next_buffer_stride,
165                                uword n_buffer_data_bytes_in_trace);
166
167 #endif /* included_vlib_trace_funcs_h */