fib: midchain adjacency optimisations
[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/adj/adj_delegate.h>
22 #include <vnet/fib/fib_node_list.h>
23
24 /* Adjacency packet/byte counters indexed by adjacency index. */
25 vlib_combined_counter_main_t adjacency_counters = {
26     .name = "adjacency",
27     .stat_segment_name = "/net/adjacency",
28 };
29
30 /*
31  * the single adj pool
32  */
33 ip_adjacency_t *adj_pool;
34
35 /**
36  * @brief Global Config for enabling per-adjacency counters.
37  * By default these are disabled.
38  */
39 int adj_per_adj_counters;
40
41 const ip46_address_t ADJ_BCAST_ADDR = {
42     .ip6 = {
43         .as_u64[0] = 0xffffffffffffffff,
44         .as_u64[1] = 0xffffffffffffffff,
45     },
46 };
47
48 /**
49  * Adj flag names
50  */
51 static const char *adj_attr_names[] = ADJ_ATTR_NAMES;
52
53 always_inline void
54 adj_poison (ip_adjacency_t * adj)
55 {
56     if (CLIB_DEBUG > 0)
57     {
58         clib_memset (adj, 0xfe, sizeof (adj[0]));
59     }
60 }
61
62 ip_adjacency_t *
63 adj_alloc (fib_protocol_t proto)
64 {
65     ip_adjacency_t *adj;
66
67     pool_get_aligned(adj_pool, adj, CLIB_CACHE_LINE_BYTES);
68
69     adj_poison(adj);
70
71     /* Make sure certain fields are always initialized. */
72     /* Validate adjacency counters. */
73     vlib_validate_combined_counter(&adjacency_counters,
74                                    adj_get_index(adj));
75     vlib_zero_combined_counter(&adjacency_counters,
76                                adj_get_index(adj));
77     fib_node_init(&adj->ia_node,
78                   FIB_NODE_TYPE_ADJ);
79
80     adj->ia_nh_proto = proto;
81     adj->ia_flags = 0;
82     adj->ia_cfg_index = 0;
83     adj->rewrite_header.sw_if_index = ~0;
84     adj->rewrite_header.flags = 0;
85     adj->lookup_next_index = 0;
86     adj->ia_delegates = NULL;
87
88     /* lest it become a midchain in the future */
89     clib_memset(&adj->sub_type.midchain.next_dpo, 0,
90            sizeof(adj->sub_type.midchain.next_dpo));
91
92     return (adj);
93 }
94
95 static int
96 adj_index_is_special (adj_index_t adj_index)
97 {
98     if (ADJ_INDEX_INVALID == adj_index)
99         return (!0);
100
101     return (0);
102 }
103
104 u8*
105 format_adj_flags (u8 * s, va_list * args)
106 {
107     adj_flags_t af;
108     adj_attr_t at;
109
110     af = va_arg (*args, int);
111
112     if (ADJ_FLAG_NONE == af)
113     {
114         return (format(s, "None"));
115     }
116     FOR_EACH_ADJ_ATTR(at)
117     {
118         if (af & (1 << at))
119         {
120             s = format(s, "%s ", adj_attr_names[at]);
121         }
122     }
123     return (s);
124 }
125
126 /**
127  * @brief Pretty print helper function for formatting specific adjacencies.
128  * @param s - input string to format
129  * @param args - other args passed to format function such as:
130  *                 - vnet_main_t
131  *                 - ip_lookup_main_t
132  *                 - adj_index
133  */
134 u8 *
135 format_ip_adjacency (u8 * s, va_list * args)
136 {
137     format_ip_adjacency_flags_t fiaf;
138     ip_adjacency_t * adj;
139     u32 adj_index;
140
141     adj_index = va_arg (*args, u32);
142     fiaf = va_arg (*args, format_ip_adjacency_flags_t);
143
144     if (!adj_is_valid(adj_index))
145       return format(s, "<invalid adjacency>");
146
147     adj = adj_get(adj_index);
148
149     switch (adj->lookup_next_index)
150     {
151     case IP_LOOKUP_NEXT_REWRITE:
152     case IP_LOOKUP_NEXT_BCAST:
153         s = format (s, "%U", format_adj_nbr, adj_index, 0);
154         break;
155     case IP_LOOKUP_NEXT_ARP:
156         s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
157         break;
158     case IP_LOOKUP_NEXT_GLEAN:
159         s = format (s, "%U", format_adj_glean, adj_index, 0);
160         break;
161     case IP_LOOKUP_NEXT_MIDCHAIN:
162         s = format (s, "%U", format_adj_midchain, adj_index, 2);
163         break;
164     case IP_LOOKUP_NEXT_MCAST:
165         s = format (s, "%U", format_adj_mcast, adj_index, 0);
166         break;
167     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
168         s = format (s, "%U", format_adj_mcast_midchain, adj_index, 0);
169         break;
170     case IP_LOOKUP_NEXT_DROP:
171     case IP_LOOKUP_NEXT_PUNT:
172     case IP_LOOKUP_NEXT_LOCAL:
173     case IP_LOOKUP_NEXT_ICMP_ERROR:
174     case IP_LOOKUP_N_NEXT:
175         break;
176     }
177
178     if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
179     {
180         vlib_counter_t counts;
181
182         vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
183         s = format (s, "\n   flags:%U", format_adj_flags, adj->ia_flags);
184         s = format (s, "\n   counts:[%Ld:%Ld]", counts.packets, counts.bytes);
185         s = format (s, "\n   locks:%d", adj->ia_node.fn_locks);
186         s = format(s, "\n delegates:");
187         s = adj_delegate_format(s, adj);
188
189         s = format(s, "\n children:");
190         if (fib_node_list_get_size(adj->ia_node.fn_children))
191         {
192             s = format(s, "\n  ");
193             s = fib_node_children_format(adj->ia_node.fn_children, s);
194         }
195     }
196
197     return s;
198 }
199
200 int
201 adj_recursive_loop_detect (adj_index_t ai,
202                            fib_node_index_t **entry_indicies)
203 {
204     ip_adjacency_t * adj;
205
206     adj = adj_get(ai);
207
208     switch (adj->lookup_next_index)
209     {
210     case IP_LOOKUP_NEXT_REWRITE:
211     case IP_LOOKUP_NEXT_ARP:
212     case IP_LOOKUP_NEXT_GLEAN:
213     case IP_LOOKUP_NEXT_MCAST:
214     case IP_LOOKUP_NEXT_BCAST:
215     case IP_LOOKUP_NEXT_DROP:
216     case IP_LOOKUP_NEXT_PUNT:
217     case IP_LOOKUP_NEXT_LOCAL:
218     case IP_LOOKUP_NEXT_ICMP_ERROR:
219     case IP_LOOKUP_N_NEXT:
220         /*
221          * these adjacency types are terminal graph nodes, so there's no
222          * possibility of a loop down here.
223          */
224         break;
225     case IP_LOOKUP_NEXT_MIDCHAIN:
226     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
227         return (adj_ndr_midchain_recursive_loop_detect(ai, entry_indicies));
228     }
229
230     return (0);
231 }
232
233 /*
234  * adj_last_lock_gone
235  *
236  * last lock/reference to the adj has gone, we no longer need it.
237  */
238 static void
239 adj_last_lock_gone (ip_adjacency_t *adj)
240 {
241     vlib_main_t * vm = vlib_get_main();
242
243     ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
244     ADJ_DBG(adj, "last-lock-gone");
245
246     adj_delegate_adj_deleted(adj);
247
248     vlib_worker_thread_barrier_sync (vm);
249
250     switch (adj->lookup_next_index)
251     {
252     case IP_LOOKUP_NEXT_MIDCHAIN:
253         adj_midchain_teardown(adj);
254         /* FALL THROUGH */
255     case IP_LOOKUP_NEXT_ARP:
256     case IP_LOOKUP_NEXT_REWRITE:
257     case IP_LOOKUP_NEXT_BCAST:
258         /*
259          * complete and incomplete nbr adjs
260          */
261         adj_nbr_remove(adj_get_index(adj),
262                        adj->ia_nh_proto,
263                        adj->ia_link,
264                        &adj->sub_type.nbr.next_hop,
265                        adj->rewrite_header.sw_if_index);
266         break;
267     case IP_LOOKUP_NEXT_GLEAN:
268         adj_glean_remove(adj->ia_nh_proto,
269                          adj->rewrite_header.sw_if_index);
270         break;
271     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
272         adj_midchain_teardown(adj);
273         /* FALL THROUGH */
274     case IP_LOOKUP_NEXT_MCAST:
275         adj_mcast_remove(adj->ia_nh_proto,
276                          adj->rewrite_header.sw_if_index);
277         break;
278     case IP_LOOKUP_NEXT_DROP:
279     case IP_LOOKUP_NEXT_PUNT:
280     case IP_LOOKUP_NEXT_LOCAL:
281     case IP_LOOKUP_NEXT_ICMP_ERROR:
282     case IP_LOOKUP_N_NEXT:
283         /*
284          * type not stored in any DB from which we need to remove it
285          */
286         break;
287     }
288
289     vlib_worker_thread_barrier_release(vm);
290
291     fib_node_deinit(&adj->ia_node);
292     ASSERT(0 == vec_len(adj->ia_delegates));
293     vec_free(adj->ia_delegates);
294     pool_put(adj_pool, adj);
295 }
296
297 u32
298 adj_dpo_get_urpf (const dpo_id_t *dpo)
299 {
300     ip_adjacency_t *adj;
301
302     adj = adj_get(dpo->dpoi_index);
303
304     return (adj->rewrite_header.sw_if_index);
305 }
306
307 void
308 adj_lock (adj_index_t adj_index)
309 {
310     ip_adjacency_t *adj;
311
312     if (adj_index_is_special(adj_index))
313     {
314         return;
315     }
316
317     adj = adj_get(adj_index);
318     ASSERT(adj);
319
320     ADJ_DBG(adj, "lock");
321     fib_node_lock(&adj->ia_node);
322 }
323
324 void
325 adj_unlock (adj_index_t adj_index)
326 {
327     ip_adjacency_t *adj;
328
329     if (adj_index_is_special(adj_index))
330     {
331         return;
332     }
333
334     adj = adj_get(adj_index);
335     ASSERT(adj);
336
337     ADJ_DBG(adj, "unlock");
338     ASSERT(adj);
339
340     fib_node_unlock(&adj->ia_node);
341 }
342
343 u32
344 adj_child_add (adj_index_t adj_index,
345                fib_node_type_t child_type,
346                fib_node_index_t child_index)
347 {
348     ASSERT(ADJ_INDEX_INVALID != adj_index);
349     if (adj_index_is_special(adj_index))
350     {
351         return (~0);
352     }
353
354     return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
355                                adj_index,
356                                child_type,
357                                child_index));
358 }
359
360 void
361 adj_child_remove (adj_index_t adj_index,
362                   u32 sibling_index)
363 {
364     if (adj_index_is_special(adj_index))
365     {
366         return;
367     }
368
369     fib_node_child_remove(FIB_NODE_TYPE_ADJ,
370                           adj_index,
371                           sibling_index);
372 }
373
374 /*
375  * Context for the walk to update the cached feature flags.
376  */
377 typedef struct adj_feature_update_t_
378 {
379     u8 arc;
380     u8 enable;
381 } adj_feature_update_ctx_t;
382
383 static adj_walk_rc_t
384 adj_feature_update_walk_cb (adj_index_t ai,
385                             void *arg)
386 {
387     adj_feature_update_ctx_t *ctx = arg;
388     ip_adjacency_t *adj;
389
390     adj = adj_get(ai);
391
392     /*
393      * this ugly mess matches the feature arc that is changing with affected
394      * adjacencies
395      */
396     if (((ctx->arc == ip6_main.lookup_main.output_feature_arc_index) &&
397          (VNET_LINK_IP6 == adj->ia_link)) ||
398         ((ctx->arc == ip4_main.lookup_main.output_feature_arc_index) &&
399          (VNET_LINK_IP4 == adj->ia_link)) ||
400         ((ctx->arc == mpls_main.output_feature_arc_index) &&
401          (VNET_LINK_MPLS == adj->ia_link)))
402     {
403         vnet_feature_main_t *fm = &feature_main;
404         vnet_feature_config_main_t *cm;
405
406         cm = &fm->feature_config_mains[ctx->arc];
407
408         if (ctx->enable)
409             adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
410         else
411             adj->rewrite_header.flags &= ~VNET_REWRITE_HAS_FEATURES;
412
413         adj->ia_cfg_index = vec_elt (cm->config_index_by_sw_if_index,
414                                      adj->rewrite_header.sw_if_index);
415     }
416     return (ADJ_WALK_RC_CONTINUE);
417 }
418
419 static void
420 adj_feature_update (u32 sw_if_index,
421                     u8 arc_index,
422                     u8 is_enable,
423                     void *data)
424 {
425     /*
426      * Walk all the adjacencies on the interface to update the cached
427      * 'has-features' flag
428      */
429     adj_feature_update_ctx_t ctx = {
430         .arc = arc_index,
431         .enable = is_enable,
432     };
433     adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx);
434 }
435
436 static adj_walk_rc_t
437 adj_mtu_update_walk_cb (adj_index_t ai,
438                         void *arg)
439 {
440     ip_adjacency_t *adj;
441
442     adj = adj_get(ai);
443
444     vnet_rewrite_update_mtu (vnet_get_main(), adj->ia_link,
445                              &adj->rewrite_header);
446
447     return (ADJ_WALK_RC_CONTINUE);
448 }
449
450 static clib_error_t *
451 adj_mtu_update (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
452 {
453   adj_walk (sw_if_index, adj_mtu_update_walk_cb, NULL);
454
455   return (NULL);
456 }
457
458 VNET_SW_INTERFACE_MTU_CHANGE_FUNCTION(adj_mtu_update);
459
460 /**
461  * @brief Walk the Adjacencies on a given interface
462  */
463 void
464 adj_walk (u32 sw_if_index,
465           adj_walk_cb_t cb,
466           void *ctx)
467 {
468     /*
469      * walk all the neighbor adjacencies
470      */
471     fib_protocol_t proto;
472
473     FOR_EACH_FIB_IP_PROTOCOL(proto)
474     {
475         adj_nbr_walk(sw_if_index, proto, cb, ctx);
476         adj_mcast_walk(sw_if_index, proto, cb, ctx);
477     }
478 }
479
480 /**
481  * @brief Return the link type of the adjacency
482  */
483 vnet_link_t
484 adj_get_link_type (adj_index_t ai)
485 {
486     const ip_adjacency_t *adj;
487
488     adj = adj_get(ai);
489
490     return (adj->ia_link);
491 }
492
493 /**
494  * @brief Return the sw interface index of the adjacency.
495  */
496 u32
497 adj_get_sw_if_index (adj_index_t ai)
498 {
499     const ip_adjacency_t *adj;
500
501     adj = adj_get(ai);
502
503     return (adj->rewrite_header.sw_if_index);
504 }
505
506 /**
507  * @brief Return true if the adjacency is 'UP', i.e. can be used for forwarding
508  * 0 is down, !0 is up.
509  */
510 int
511 adj_is_up (adj_index_t ai)
512 {
513     return (adj_bfd_is_up(ai));
514 }
515
516 /**
517  * @brief Return the rewrite string of the adjacency
518  */
519 const u8*
520 adj_get_rewrite (adj_index_t ai)
521 {
522     vnet_rewrite_header_t *rw;
523     ip_adjacency_t *adj;
524
525     adj = adj_get(ai);
526     rw = &adj->rewrite_header;
527
528     ASSERT (rw->data_bytes != 0xfefe);
529
530     return (rw->data - rw->data_bytes);
531 }
532
533 static fib_node_t *
534 adj_get_node (fib_node_index_t index)
535 {
536     ip_adjacency_t *adj;
537
538     adj = adj_get(index);
539
540     return (&adj->ia_node);
541 }
542
543 #define ADJ_FROM_NODE(_node)                                            \
544     ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
545
546 static void
547 adj_node_last_lock_gone (fib_node_t *node)
548 {
549     adj_last_lock_gone(ADJ_FROM_NODE(node));
550 }
551
552 static fib_node_back_walk_rc_t
553 adj_back_walk_notify (fib_node_t *node,
554                       fib_node_back_walk_ctx_t *ctx)
555 {
556     ip_adjacency_t *adj;
557
558     adj = ADJ_FROM_NODE(node);
559
560     switch (adj->lookup_next_index)
561     {
562     case IP_LOOKUP_NEXT_MIDCHAIN:
563         adj_midchain_delegate_restack(adj_get_index(adj));
564         break;
565     case IP_LOOKUP_NEXT_ARP:
566     case IP_LOOKUP_NEXT_REWRITE:
567     case IP_LOOKUP_NEXT_BCAST:
568     case IP_LOOKUP_NEXT_GLEAN:
569     case IP_LOOKUP_NEXT_MCAST:
570     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
571     case IP_LOOKUP_NEXT_DROP:
572     case IP_LOOKUP_NEXT_PUNT:
573     case IP_LOOKUP_NEXT_LOCAL:
574     case IP_LOOKUP_NEXT_ICMP_ERROR:
575     case IP_LOOKUP_N_NEXT:
576         /*
577          * Que pasa. yo soj en el final!
578          */
579         ASSERT(0);
580         break;
581     }
582
583     return (FIB_NODE_BACK_WALK_CONTINUE);
584 }
585
586 /*
587  * Adjacency's graph node virtual function table
588  */
589 static const fib_node_vft_t adj_vft = {
590     .fnv_get = adj_get_node,
591     .fnv_last_lock = adj_node_last_lock_gone,
592     .fnv_back_walk = adj_back_walk_notify,
593 };
594
595 static clib_error_t *
596 adj_module_init (vlib_main_t * vm)
597 {
598     fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
599
600     adj_nbr_module_init();
601     adj_glean_module_init();
602     adj_midchain_module_init();
603     adj_mcast_module_init();
604
605     vnet_feature_register(adj_feature_update, NULL);
606
607     return (NULL);
608 }
609
610 VLIB_INIT_FUNCTION (adj_module_init);
611
612 static clib_error_t *
613 adj_show (vlib_main_t * vm,
614           unformat_input_t * input,
615           vlib_cli_command_t * cmd)
616 {
617     adj_index_t ai = ADJ_INDEX_INVALID;
618     u32 sw_if_index = ~0;
619     int summary = 0;
620
621     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
622     {
623         if (unformat (input, "%d", &ai))
624             ;
625         else if (unformat (input, "summary") || unformat (input, "sum"))
626             summary = 1;
627         else if (unformat (input, "%U",
628                            unformat_vnet_sw_interface, vnet_get_main(),
629                            &sw_if_index))
630             ;
631         else
632             break;
633     }
634
635     if (summary)
636     {
637         vlib_cli_output (vm, "Number of adjacencies: %d", pool_elts(adj_pool));
638         vlib_cli_output (vm, "Per-adjacency counters: %s",
639                          (adj_are_counters_enabled() ?
640                           "enabled":
641                           "disabled"));
642     }
643     else
644     {
645         if (ADJ_INDEX_INVALID != ai)
646         {
647             if (pool_is_free_index(adj_pool, ai))
648             {
649                 vlib_cli_output (vm, "adjacency %d invalid", ai);
650                 return 0;
651             }
652
653             vlib_cli_output (vm, "[@%d] %U",
654                              ai,
655                              format_ip_adjacency,  ai,
656                              FORMAT_IP_ADJACENCY_DETAIL);
657         }
658         else
659         {
660             /* *INDENT-OFF* */
661             pool_foreach_index(ai, adj_pool,
662             ({
663                 if (~0 != sw_if_index &&
664                     sw_if_index != adj_get_sw_if_index(ai))
665                 {
666                 }
667                 else
668                 {
669                     vlib_cli_output (vm, "[@%d] %U",
670                                      ai,
671                                      format_ip_adjacency, ai,
672                                      FORMAT_IP_ADJACENCY_NONE);
673                 }
674             }));
675             /* *INDENT-ON* */
676         }
677     }
678     return 0;
679 }
680
681 /*?
682  * Show all adjacencies.
683  * @cliexpar
684  * @cliexstart{sh adj}
685  * [@0]
686  * [@1]  glean: loop0
687  * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
688  * [@3] mpls via 1.0.0.2 loop0: MPLS: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
689  * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
690  * [@5] mpls via 1.0.0.3 loop0: MPLS: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
691  * @cliexend
692  ?*/
693 VLIB_CLI_COMMAND (adj_show_command, static) = {
694     .path = "show adj",
695     .short_help = "show adj [<adj_index>] [interface] [summary]",
696     .function = adj_show,
697 };
698
699 /**
700  * @brief CLI invoked function to enable/disable per-adj counters
701  */
702 static clib_error_t *
703 adj_cli_counters_set (vlib_main_t * vm,
704                       unformat_input_t * input,
705                       vlib_cli_command_t * cmd)
706 {
707     clib_error_t *error = NULL;
708     int enable = ~0;
709
710     while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
711     {
712         if (unformat (input, "enable"))
713             enable = 1;
714         else if (unformat (input, "disable"))
715             enable = 0;
716         else
717             break;
718     }
719
720     if (enable != ~0)
721     {
722         /* user requested something sensible */
723         adj_per_adj_counters = enable;
724     }
725     else
726     {
727         error = clib_error_return (0, "specify 'enable' or 'disable'");
728     }
729
730     return (error);
731 }
732
733 /*?
734  * Enable/disable per-adjacency counters. This is optional because it comes
735  * with a non-negligible performance cost.
736  ?*/
737 VLIB_CLI_COMMAND (adj_cli_counters_set_command, static) = {
738     .path = "adjacency counters",
739     .short_help = "adjacency counters [enable|disable]",
740     .function = adj_cli_counters_set,
741 };