make per-adj counters configurable
[vpp.git] / src / vnet / adj / adj.c
1 /*
2  * Copyright (c) 2016 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 #include <vnet/adj/adj.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/adj/adj_glean.h>
19 #include <vnet/adj/adj_midchain.h>
20 #include <vnet/adj/adj_mcast.h>
21 #include <vnet/fib/fib_node_list.h>
22
23 /*
24  * Special Adj with index zero. we need to define this since the v4 mtrie
25  * assumes an index of 0 implies the ply is empty. therefore all 'real'
26  * adjs need a non-zero index.
27  */
28 static ip_adjacency_t *special_v4_miss_adj_with_index_zero;
29
30 /* Adjacency packet/byte counters indexed by adjacency index. */
31 vlib_combined_counter_main_t adjacency_counters;
32
33 /*
34  * the single adj pool
35  */
36 ip_adjacency_t *adj_pool;
37
38 /**
39  * @brief Global Config for enabling per-adjacency counters.
40  * By default these are disabled.
41  */
42 int adj_per_adj_counters;
43
44 always_inline void
45 adj_poison (ip_adjacency_t * adj)
46 {
47     if (CLIB_DEBUG > 0)
48     {
49         memset (adj, 0xfe, sizeof (adj[0]));
50     }
51 }
52
53 ip_adjacency_t *
54 adj_alloc (fib_protocol_t proto)
55 {
56     ip_adjacency_t *adj;
57
58     pool_get(adj_pool, adj);
59
60     adj_poison(adj);
61
62     /* Make sure certain fields are always initialized. */
63     /* Validate adjacency counters. */
64     vlib_validate_combined_counter(&adjacency_counters,
65                                    adj_get_index(adj));
66
67     adj->rewrite_header.sw_if_index = ~0;
68     adj->n_adj = 1;
69     adj->lookup_next_index = 0;
70
71     fib_node_init(&adj->ia_node,
72                   FIB_NODE_TYPE_ADJ);
73     adj->ia_nh_proto = proto;
74     adj->ia_flags = 0;
75
76     ip4_main.lookup_main.adjacency_heap = adj_pool;
77     ip6_main.lookup_main.adjacency_heap = adj_pool;
78
79     return (adj);
80 }
81
82 static int
83 adj_index_is_special (adj_index_t adj_index)
84 {
85     if (ADJ_INDEX_INVALID == adj_index)
86         return (!0);
87
88     return (0);
89 }
90
91 /**
92  * @brief Pretty print helper function for formatting specific adjacencies.
93  * @param s - input string to format
94  * @param args - other args passed to format function such as:
95  *                 - vnet_main_t
96  *                 - ip_lookup_main_t
97  *                 - adj_index
98  */
99 u8 *
100 format_ip_adjacency (u8 * s, va_list * args)
101 {
102     format_ip_adjacency_flags_t fiaf;
103     ip_adjacency_t * adj;
104     u32 adj_index;
105
106     adj_index = va_arg (*args, u32);
107     fiaf = va_arg (*args, format_ip_adjacency_flags_t);
108     adj = adj_get(adj_index);
109   
110     switch (adj->lookup_next_index)
111     {
112     case IP_LOOKUP_NEXT_REWRITE:
113         s = format (s, "%U", format_adj_nbr, adj_index, 0);
114         break;
115     case IP_LOOKUP_NEXT_ARP:
116         s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
117         break;
118     case IP_LOOKUP_NEXT_GLEAN:
119         s = format (s, "%U", format_adj_glean, adj_index, 0);
120         break;
121     case IP_LOOKUP_NEXT_MIDCHAIN:
122         s = format (s, "%U", format_adj_midchain, adj_index, 2);
123         break;
124     case IP_LOOKUP_NEXT_MCAST:
125         s = format (s, "%U", format_adj_mcast, adj_index, 0);
126         break;
127     default:
128         break;
129     }
130
131     if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
132     {
133         vlib_counter_t counts;
134
135         vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
136         s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
137         s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
138         s = format (s, " node:[%d]:%U",
139                     adj->rewrite_header.node_index,
140                     format_vlib_node_name, vlib_get_main(),
141                     adj->rewrite_header.node_index);
142         s = format (s, " next:[%d]:%U",
143                     adj->rewrite_header.next_index,
144                     format_vlib_next_node_name,
145                     vlib_get_main(),
146                     adj->rewrite_header.node_index,
147                     adj->rewrite_header.next_index);
148         s = format(s, "\n children:\n  ");
149         s = fib_node_children_format(adj->ia_node.fn_children, s);
150     }
151
152     return s;
153 }
154
155 /*
156  * adj_last_lock_gone
157  *
158  * last lock/reference to the adj has gone, we no longer need it.
159  */
160 static void
161 adj_last_lock_gone (ip_adjacency_t *adj)
162 {
163     vlib_main_t * vm = vlib_get_main();
164
165     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
166     ADJ_DBG(adj, "last-lock-gone");
167
168     vlib_worker_thread_barrier_sync (vm);
169
170     switch (adj->lookup_next_index)
171     {
172     case IP_LOOKUP_NEXT_MIDCHAIN:
173         dpo_reset(&adj->sub_type.midchain.next_dpo);
174         /* FALL THROUGH */
175     case IP_LOOKUP_NEXT_ARP:
176     case IP_LOOKUP_NEXT_REWRITE:
177         /*
178          * complete and incomplete nbr adjs
179          */
180         adj_nbr_remove(adj_get_index(adj),
181                        adj->ia_nh_proto,
182                        adj->ia_link,
183                        &adj->sub_type.nbr.next_hop,
184                        adj->rewrite_header.sw_if_index);
185         break;
186     case IP_LOOKUP_NEXT_GLEAN:
187         adj_glean_remove(adj->ia_nh_proto,
188                          adj->rewrite_header.sw_if_index);
189         break;
190     case IP_LOOKUP_NEXT_MCAST:
191         adj_mcast_remove(adj->ia_nh_proto,
192                          adj->rewrite_header.sw_if_index);
193         break;
194     default:
195         /*
196          * type not stored in any DB from which we need to remove it
197          */
198         break;
199     }
200
201     vlib_worker_thread_barrier_release(vm);
202
203     fib_node_deinit(&adj->ia_node);
204     pool_put(adj_pool, adj);
205 }
206
207 void
208 adj_lock (adj_index_t adj_index)
209 {
210     ip_adjacency_t *adj;
211
212     if (adj_index_is_special(adj_index))
213     {
214         return;
215     }
216
217     adj = adj_get(adj_index);
218     ASSERT(adj);
219
220     ADJ_DBG(adj, "lock");
221     fib_node_lock(&adj->ia_node);
222 }
223
224 void
225 adj_unlock (adj_index_t adj_index)
226 {
227     ip_adjacency_t *adj;
228
229     if (adj_index_is_special(adj_index))
230     {
231         return;
232     }
233
234     adj = adj_get(adj_index);
235     ASSERT(adj);
236
237     ADJ_DBG(adj, "unlock");
238     ASSERT(adj);
239
240     fib_node_unlock(&adj->ia_node);
241 }
242
243 u32
244 adj_child_add (adj_index_t adj_index,
245                fib_node_type_t child_type,
246                fib_node_index_t child_index)
247 {
248     ASSERT(ADJ_INDEX_INVALID != adj_index);
249     if (adj_index_is_special(adj_index))
250     {
251         return (~0);
252     }
253
254     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
255                                adj_index,
256                                child_type,
257                                child_index));
258 }
259
260 void
261 adj_child_remove (adj_index_t adj_index,
262                   u32 sibling_index)
263 {
264     if (adj_index_is_special(adj_index))
265     {
266         return;
267     }
268
269     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
270                           adj_index,
271                           sibling_index);
272 }
273
274 /**
275  * @brief Return the link type of the adjacency
276  */
277 vnet_link_t
278 adj_get_link_type (adj_index_t ai)
279 {
280     const ip_adjacency_t *adj;
281
282     adj = adj_get(ai);
283
284     return (adj->ia_link); 
285 }
286
287 /**
288  * @brief Return the sw interface index of the adjacency.
289  */
290 u32
291 adj_get_sw_if_index (adj_index_t ai)
292 {
293     const ip_adjacency_t *adj;
294
295     adj = adj_get(ai);
296
297     return (adj->rewrite_header.sw_if_index);
298 }
299
300 /**
301  * @brief Return the link type of the adjacency
302  */
303 const u8*
304 adj_get_rewrite (adj_index_t ai)
305 {
306     vnet_rewrite_header_t *rw;
307     ip_adjacency_t *adj;
308
309     adj = adj_get(ai);
310     rw = &adj->rewrite_header;
311
312     ASSERT (rw->data_bytes != 0xfefe);
313
314     return (rw->data - rw->data_bytes);
315 }
316
317 static fib_node_t *
318 adj_get_node (fib_node_index_t index)
319 {
320     ip_adjacency_t *adj;
321
322     adj = adj_get(index);
323
324     return (&adj->ia_node);
325 }
326
327 #define ADJ_FROM_NODE(_node)                                            \
328     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
329
330 static void
331 adj_node_last_lock_gone (fib_node_t *node)
332 {
333     adj_last_lock_gone(ADJ_FROM_NODE(node));
334 }
335
336 static fib_node_back_walk_rc_t
337 adj_back_walk_notify (fib_node_t *node,
338                       fib_node_back_walk_ctx_t *ctx)
339 {
340     /*
341      * Que pasa. yo soj en el final!
342      */
343     ASSERT(0);
344
345     return (FIB_NODE_BACK_WALK_CONTINUE);
346 }
347
348 /*
349  * Adjacency's graph node virtual function table
350  */
351 static const fib_node_vft_t adj_vft = {
352     .fnv_get = adj_get_node,
353     .fnv_last_lock = adj_node_last_lock_gone,
354     .fnv_back_walk = adj_back_walk_notify,
355 };
356
357 static clib_error_t *
358 adj_module_init (vlib_main_t * vm)
359 {
360     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
361
362     adj_nbr_module_init();
363     adj_glean_module_init();
364     adj_midchain_module_init();
365     adj_mcast_module_init();
366
367     /*
368      * one special adj to reserve index 0
369      */
370     special_v4_miss_adj_with_index_zero = adj_alloc(FIB_PROTOCOL_IP4);
371
372     return (NULL);
373 }
374
375 VLIB_INIT_FUNCTION (adj_module_init);
376
377 static clib_error_t *
378 adj_show (vlib_main_t * vm,
379           unformat_input_t * input,
380           vlib_cli_command_t * cmd)
381 {
382     adj_index_t ai = ADJ_INDEX_INVALID;
383     u32 sw_if_index = ~0;
384     int summary = 0;
385
386     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
387     {
388         if (unformat (input, "%d", &ai))
389             ;
390         else if (unformat (input, "sum"))
391             summary = 1;
392         else if (unformat (input, "summary"))
393             summary = 1;
394         else if (unformat (input, "%U",
395                            unformat_vnet_sw_interface, vnet_get_main(),
396                            &sw_if_index))
397             ;
398         else
399             break;
400     }
401
402     if (summary)
403     {
404         vlib_cli_output (vm, "Number of adjacenies: %d", pool_elts(adj_pool));
405         vlib_cli_output (vm, "Per-adjacency counters: %s",
406                          (adj_are_counters_enabled() ?
407                           "enabled":
408                           "disabled"));
409     }
410     else
411     {
412         if (ADJ_INDEX_INVALID != ai)
413         {
414             if (pool_is_free_index(adj_pool, ai))
415             {
416                 vlib_cli_output (vm, "adjacency %d invalid", ai);
417                 return 0;
418             }
419
420             vlib_cli_output (vm, "[@%d] %U",
421                              ai,
422                              format_ip_adjacency,  ai,
423                              FORMAT_IP_ADJACENCY_DETAIL);
424         }
425         else
426         {
427             /* *INDENT-OFF* */
428             pool_foreach_index(ai, adj_pool,
429             ({
430                 if (~0 != sw_if_index &&
431                     sw_if_index != adj_get_sw_if_index(ai))
432                 {
433                 }
434                 else
435                 {
436                     vlib_cli_output (vm, "[@%d] %U",
437                                      ai,
438                                      format_ip_adjacency, ai,
439                                      FORMAT_IP_ADJACENCY_NONE);
440                 }
441             }));
442             /* *INDENT-ON* */
443         }
444     }
445     return 0;
446 }
447
448 /*?
449  * Show all adjacencies.
450  * @cliexpar
451  * @cliexstart{sh adj}
452  * [@0]
453  * [@1]  glean: loop0
454  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
455  * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
456  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
457  * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
458  * @cliexend
459  ?*/
460 VLIB_CLI_COMMAND (adj_show_command, static) = {
461     .path = "show adj",
462     .short_help = "show adj [<adj_index>] [interface] [summary]",
463     .function = adj_show,
464 };
465
466 /**
467  * @brief CLI invoked function to enable/disable per-adj counters
468  */
469 static clib_error_t *
470 adj_cli_counters_set (vlib_main_t * vm,
471                       unformat_input_t * input,
472                       vlib_cli_command_t * cmd)
473 {
474     clib_error_t *error = NULL;
475     int enable = ~0;
476
477     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
478     {
479         if (unformat (input, "enable"))
480             enable = 1;
481         else if (unformat (input, "disable"))
482             enable = 0;
483         else
484             break;
485     }
486
487     if (enable != ~0)
488     {
489         /* user requested something sensible */
490         adj_per_adj_counters = enable;
491     }
492     else
493     {
494         error = clib_error_return (0, "specify 'enable' or 'disable'");
495     }
496
497     return (error);
498 }
499
500 /*?
501  * Enabe/disble per-adjacency counters. This is optional because it comes with
502  * a non-negligible performance cost.
503  ?*/
504 VLIB_CLI_COMMAND (adj_cli_counters_set_command, static) = {
505     .path = "adjacency counters",
506     .short_help = "adjacency counters [enable|disable]",
507     .function = adj_cli_counters_set,
508 };