stats: fix memory leakage when adding / deleting interfaces
[vpp.git] / src / vlib / error.c
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  * error.c: VLIB error handler
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 #include <vlib/vlib.h>
41 #include <vppinfra/heap.h>
42 #include <vlib/stat_weak_inlines.h>
43
44 uword
45 vlib_error_drop_buffers (vlib_main_t * vm,
46                          vlib_node_runtime_t * node,
47                          u32 * buffers,
48                          u32 next_buffer_stride,
49                          u32 n_buffers,
50                          u32 next_index,
51                          u32 drop_error_node, u32 drop_error_code)
52 {
53   u32 n_left_this_frame, n_buffers_left, *args, n_args_left;
54   vlib_error_t drop_error;
55
56   drop_error = vlib_error_set (drop_error_node, drop_error_code);
57
58   n_buffers_left = n_buffers;
59   while (n_buffers_left > 0)
60     {
61       vlib_get_next_frame (vm, node, next_index, args, n_args_left);
62
63       n_left_this_frame = clib_min (n_buffers_left, n_args_left);
64       n_buffers_left -= n_left_this_frame;
65       n_args_left -= n_left_this_frame;
66
67       while (n_left_this_frame >= 4)
68         {
69           u32 bi0, bi1, bi2, bi3;
70           vlib_buffer_t *b0, *b1, *b2, *b3;
71
72           args[0] = bi0 = buffers[0];
73           args[1] = bi1 = buffers[1];
74           args[2] = bi2 = buffers[2];
75           args[3] = bi3 = buffers[3];
76
77           b0 = vlib_get_buffer (vm, bi0);
78           b1 = vlib_get_buffer (vm, bi1);
79           b2 = vlib_get_buffer (vm, bi2);
80           b3 = vlib_get_buffer (vm, bi3);
81
82           b0->error = drop_error;
83           b1->error = drop_error;
84           b2->error = drop_error;
85           b3->error = drop_error;
86
87           buffers += 4;
88           args += 4;
89           n_left_this_frame -= 4;
90         }
91
92       while (n_left_this_frame >= 1)
93         {
94           u32 bi0;
95           vlib_buffer_t *b0;
96
97           args[0] = bi0 = buffers[0];
98
99           b0 = vlib_get_buffer (vm, bi0);
100           b0->error = drop_error;
101
102           buffers += 1;
103           args += 1;
104           n_left_this_frame -= 1;
105         }
106
107       vlib_put_next_frame (vm, node, next_index, n_args_left);
108     }
109
110   return n_buffers;
111 }
112
113 /* Reserves given number of error codes for given node. */
114 void
115 vlib_register_errors (vlib_main_t * vm,
116                       u32 node_index, u32 n_errors, char *error_strings[])
117 {
118   vlib_error_main_t *em = &vm->error_main;
119   vlib_node_t *n = vlib_get_node (vm, node_index);
120   uword l;
121   void *oldheap;
122
123   ASSERT (vlib_get_thread_index () == 0);
124
125   /* Free up any previous error strings. */
126   if (n->n_errors > 0)
127     heap_dealloc (em->error_strings_heap, n->error_heap_handle);
128
129   n->n_errors = n_errors;
130   n->error_strings = error_strings;
131
132   if (n_errors == 0)
133     return;
134
135   n->error_heap_index =
136     heap_alloc (em->error_strings_heap, n_errors, n->error_heap_handle);
137
138   l = vec_len (em->error_strings_heap);
139
140   clib_memcpy (vec_elt_at_index (em->error_strings_heap, n->error_heap_index),
141                error_strings, n_errors * sizeof (error_strings[0]));
142
143   vec_validate (vm->error_elog_event_types, l - 1);
144
145   /* Switch to the stats segment ... */
146   oldheap = vlib_stats_push_heap (0);
147
148   /* Allocate a counter/elog type for each error. */
149   vec_validate (em->counters, l - 1);
150
151   /* Zero counters for re-registrations of errors. */
152   if (n->error_heap_index + n_errors <= vec_len (em->counters_last_clear))
153     clib_memcpy (em->counters + n->error_heap_index,
154                  em->counters_last_clear + n->error_heap_index,
155                  n_errors * sizeof (em->counters[0]));
156   else
157     clib_memset (em->counters + n->error_heap_index,
158                  0, n_errors * sizeof (em->counters[0]));
159
160   /* Register counter indices in the stat segment directory */
161   {
162     int i;
163     u8 *error_name;
164
165     for (i = 0; i < n_errors; i++)
166       {
167         error_name = format (0, "/err/%v/%s%c", n->name, error_strings[i], 0);
168         /* Note: error_name consumed by the following call */
169         vlib_stats_register_error_index (oldheap, error_name, em->counters,
170                                          n->error_heap_index + i);
171       }
172   }
173
174   /* (re)register the em->counters base address, switch back to main heap */
175   vlib_stats_pop_heap2 (em->counters, vm->thread_index, oldheap, 1);
176
177   {
178     elog_event_type_t t;
179     uword i;
180
181     clib_memset (&t, 0, sizeof (t));
182     for (i = 0; i < n_errors; i++)
183       {
184         t.format = (char *) format (0, "%v %s: %%d",
185                                     n->name, error_strings[i]);
186         vm->error_elog_event_types[n->error_heap_index + i] = t;
187       }
188   }
189 }
190
191 static clib_error_t *
192 show_errors (vlib_main_t * vm,
193              unformat_input_t * input, vlib_cli_command_t * cmd)
194 {
195   vlib_error_main_t *em = &vm->error_main;
196   vlib_node_t *n;
197   u32 code, i, ni;
198   u64 c;
199   int index = 0;
200   int verbose = 0;
201   u64 *sums = 0;
202
203   if (unformat (input, "verbose %d", &verbose))
204     ;
205   else if (unformat (input, "verbose"))
206     verbose = 1;
207
208   vec_validate (sums, vec_len (em->counters));
209
210   if (verbose)
211     vlib_cli_output (vm, "%=10s%=40s%=20s%=6s", "Count", "Node", "Reason",
212                      "Index");
213   else
214     vlib_cli_output (vm, "%=10s%=40s%=6s", "Count", "Node", "Reason");
215
216
217   /* *INDENT-OFF* */
218   foreach_vlib_main(({
219     em = &this_vlib_main->error_main;
220
221     if (verbose)
222       vlib_cli_output(vm, "Thread %u (%v):", index,
223                       vlib_worker_threads[index].name);
224
225     for (ni = 0; ni < vec_len (this_vlib_main->node_main.nodes); ni++)
226       {
227         n = vlib_get_node (this_vlib_main, ni);
228         for (code = 0; code < n->n_errors; code++)
229           {
230             i = n->error_heap_index + code;
231             c = em->counters[i];
232             if (i < vec_len (em->counters_last_clear))
233               c -= em->counters_last_clear[i];
234             sums[i] += c;
235
236             if (c == 0 && verbose < 2)
237               continue;
238
239             if (verbose)
240               vlib_cli_output (vm, "%10Ld%=40v%=20s%=6d", c, n->name,
241                                em->error_strings_heap[i], i);
242             else
243               vlib_cli_output (vm, "%10d%=40v%s", c, n->name,
244                                em->error_strings_heap[i]);
245           }
246       }
247     index++;
248   }));
249   /* *INDENT-ON* */
250
251   if (verbose)
252     vlib_cli_output (vm, "Total:");
253
254   for (ni = 0; ni < vec_len (vm->node_main.nodes); ni++)
255     {
256       n = vlib_get_node (vm, ni);
257       for (code = 0; code < n->n_errors; code++)
258         {
259           i = n->error_heap_index + code;
260           if (sums[i])
261             {
262               if (verbose)
263                 vlib_cli_output (vm, "%10Ld%=40v%=20s%=10d", sums[i], n->name,
264                                  em->error_strings_heap[i], i);
265             }
266         }
267     }
268
269   vec_free (sums);
270
271   return 0;
272 }
273
274 /* *INDENT-OFF* */
275 VLIB_CLI_COMMAND (vlib_cli_show_errors) = {
276   .path = "show errors",
277   .short_help = "Show error counts",
278   .function = show_errors,
279 };
280 /* *INDENT-ON* */
281
282 /* *INDENT-OFF* */
283 VLIB_CLI_COMMAND (cli_show_node_counters, static) = {
284   .path = "show node counters",
285   .short_help = "Show node counters",
286   .function = show_errors,
287 };
288 /* *INDENT-ON* */
289
290 static clib_error_t *
291 clear_error_counters (vlib_main_t * vm,
292                       unformat_input_t * input, vlib_cli_command_t * cmd)
293 {
294   vlib_error_main_t *em;
295   u32 i;
296
297   /* *INDENT-OFF* */
298   foreach_vlib_main(({
299     em = &this_vlib_main->error_main;
300     vec_validate (em->counters_last_clear, vec_len (em->counters) - 1);
301     for (i = 0; i < vec_len (em->counters); i++)
302       em->counters_last_clear[i] = em->counters[i];
303   }));
304   /* *INDENT-ON* */
305   return 0;
306 }
307
308 /* *INDENT-OFF* */
309 VLIB_CLI_COMMAND (cli_clear_error_counters, static) = {
310   .path = "clear errors",
311   .short_help = "Clear error counters",
312   .function = clear_error_counters,
313 };
314 /* *INDENT-ON* */
315
316 /* *INDENT-OFF* */
317 VLIB_CLI_COMMAND (cli_clear_node_counters, static) = {
318   .path = "clear node counters",
319   .short_help = "Clear node counters",
320   .function = clear_error_counters,
321 };
322 /* *INDENT-ON* */
323
324 /*
325  * fd.io coding-style-patch-verification: ON
326  *
327  * Local Variables:
328  * eval: (c-set-style "gnu")
329  * End:
330  */