667aa485f7c218951b03128e5efcad438543531a
[vpp.git] / src / vnet / fib / fib_entry_src.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/dpo/load_balance.h>
18 #include <vnet/dpo/mpls_label_dpo.h>
19 #include <vnet/dpo/drop_dpo.h>
20 #include <vnet/dpo/replicate_dpo.h>
21
22 #include <vnet/fib/fib_entry_src.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vnet/fib/fib_path_ext.h>
25 #include <vnet/fib/fib_urpf_list.h>
26
27 /*
28  * per-source type vft
29  */
30 static fib_entry_src_vft_t fib_entry_src_vft[FIB_SOURCE_MAX];
31
32 void
33 fib_entry_src_register (fib_source_t source,
34                         const fib_entry_src_vft_t *vft)
35 {
36     fib_entry_src_vft[source] = *vft;
37 }
38
39 static int
40 fib_entry_src_cmp_for_sort (void * v1,
41                             void * v2)
42 {
43     fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
44
45     return (esrc1->fes_src - esrc2->fes_src);
46 }
47
48 void
49 fib_entry_src_action_init (fib_entry_t *fib_entry,
50                            fib_source_t source)
51
52 {
53     fib_entry_src_t esrc = {
54         .fes_pl = FIB_NODE_INDEX_INVALID,
55         .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
56         .fes_src = source,
57     };
58
59     if (NULL != fib_entry_src_vft[source].fesv_init)
60     {
61         fib_entry_src_vft[source].fesv_init(&esrc);
62     }
63
64     vec_add1(fib_entry->fe_srcs, esrc);
65     vec_sort_with_function(fib_entry->fe_srcs,
66                            fib_entry_src_cmp_for_sort);
67 }
68
69 static fib_entry_src_t *
70 fib_entry_src_find (const fib_entry_t *fib_entry,
71                     fib_source_t source,
72                     u32 *index)
73
74 {
75     fib_entry_src_t *esrc;
76     int ii;
77
78     ii = 0;
79     vec_foreach(esrc, fib_entry->fe_srcs)
80     {
81         if (esrc->fes_src == source)
82         {
83             if (NULL != index)
84             {
85                 *index = ii;
86             }
87             return (esrc);
88         }
89         else
90         {
91             ii++;
92         }
93     }
94
95     return (NULL);
96 }
97
98 int
99 fib_entry_is_sourced (fib_node_index_t fib_entry_index,
100                       fib_source_t source)
101 {
102     fib_entry_t *fib_entry;
103
104     fib_entry = fib_entry_get(fib_entry_index);
105
106     return (NULL != fib_entry_src_find(fib_entry, source, NULL));
107 }
108
109 static fib_entry_src_t *
110 fib_entry_src_find_or_create (fib_entry_t *fib_entry,
111                               fib_source_t source,
112                               u32 *index)
113 {
114     fib_entry_src_t *esrc;
115
116     esrc = fib_entry_src_find(fib_entry, source, NULL);
117
118     if (NULL == esrc)
119     {
120         fib_entry_src_action_init(fib_entry, source);
121     }
122
123     return (fib_entry_src_find(fib_entry, source, NULL));
124 }
125
126 void
127 fib_entry_src_action_deinit (fib_entry_t *fib_entry,
128                              fib_source_t source)
129
130 {
131     fib_entry_src_t *esrc;
132     u32 index = ~0;
133
134     esrc = fib_entry_src_find(fib_entry, source, &index);
135
136     ASSERT(NULL != esrc);
137
138     if (NULL != fib_entry_src_vft[source].fesv_deinit)
139     {
140         fib_entry_src_vft[source].fesv_deinit(esrc);
141     }
142
143     fib_path_ext_list_flush(&esrc->fes_path_exts);
144     vec_del1(fib_entry->fe_srcs, index);
145 }
146
147 fib_entry_src_cover_res_t
148 fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
149                                    fib_source_t source)
150 {
151     if (NULL != fib_entry_src_vft[source].fesv_cover_change)
152     {
153         return (fib_entry_src_vft[source].fesv_cover_change(
154                     fib_entry_src_find(fib_entry, source, NULL),
155                     fib_entry));
156     }
157
158     fib_entry_src_cover_res_t res = {
159         .install = !0,
160         .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
161     };
162     return (res);
163 }
164
165 fib_entry_src_cover_res_t
166 fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
167                                    fib_source_t source)
168 {
169     if (NULL != fib_entry_src_vft[source].fesv_cover_update)
170     {
171         return (fib_entry_src_vft[source].fesv_cover_update(
172                     fib_entry_src_find(fib_entry, source, NULL),
173                     fib_entry));
174     }
175
176     fib_entry_src_cover_res_t res = {
177         .install = !0,
178         .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
179     };
180     return (res);
181 }
182
183 typedef struct fib_entry_src_collect_forwarding_ctx_t_
184 {
185     load_balance_path_t *next_hops;
186     const fib_entry_t *fib_entry;
187     const fib_entry_src_t *esrc;
188     fib_forward_chain_type_t fct;
189     int n_recursive_constrained;
190     u16 preference;
191 } fib_entry_src_collect_forwarding_ctx_t;
192
193 /**
194  * @brief Determine whether this FIB entry should use a load-balance MAP
195  * to support PIC edge fast convergence
196  */
197 load_balance_flags_t
198 fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
199 {
200     /**
201      * We'll use a LB map if the path-list has multiple recursive paths.
202      * recursive paths implies BGP, and hence scale.
203      */
204     if (ctx->n_recursive_constrained > 1 &&
205         fib_path_list_is_popular(ctx->esrc->fes_pl))
206     {
207         return (LOAD_BALANCE_FLAG_USES_MAP);
208     }
209     return (LOAD_BALANCE_FLAG_NONE);
210 }
211
212 static int
213 fib_entry_src_valid_out_label (mpls_label_t label)
214 {
215     return ((MPLS_LABEL_IS_REAL(label) ||
216              MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
217              MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
218              MPLS_IETF_IMPLICIT_NULL_LABEL == label));
219 }
220
221 /**
222  * @brief Turn the chain type requested by the client into the one they
223  * really wanted
224  */
225 fib_forward_chain_type_t
226 fib_entry_chain_type_fixup (const fib_entry_t *entry,
227                             fib_forward_chain_type_t fct)
228 {
229     /*
230      * The EOS chain is a tricky since one cannot know the adjacency
231      * to link to without knowing what the packets payload protocol
232      * will be once the label is popped.
233      */
234     fib_forward_chain_type_t dfct;
235
236     if (FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct)
237     {
238         return (fct);
239     }
240
241     dfct = fib_entry_get_default_chain_type(entry);
242
243     if (FIB_FORW_CHAIN_TYPE_MPLS_EOS == dfct)
244     {
245         /*
246          * If the entry being asked is a eos-MPLS label entry,
247          * then use the payload-protocol field, that we stashed there
248          * for just this purpose
249          */
250         return (fib_forw_chain_type_from_dpo_proto(
251                     entry->fe_prefix.fp_payload_proto));
252     }
253     /*
254      * else give them what this entry would be by default. i.e. if it's a v6
255      * entry, then the label its local labelled should be carrying v6 traffic.
256      * If it's a non-EOS label entry, then there are more labels and we want
257      * a non-eos chain.
258      */
259     return (dfct);
260 }
261
262 static void
263 fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
264                                    fib_entry_src_collect_forwarding_ctx_t *ctx)
265 {
266     load_balance_path_t *nh;
267
268     /*
269      * no extension => no out-going label for this path. that's OK
270      * in the case of an IP or EOS chain, but not for non-EOS
271      */
272     switch (ctx->fct)
273     {
274     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
275     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
276     case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
277     case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
278     case FIB_FORW_CHAIN_TYPE_BIER:
279         /*
280          * EOS traffic with no label to stack, we need the IP Adj
281          */
282         vec_add2(ctx->next_hops, nh, 1);
283
284         nh->path_index = path_index;
285         nh->path_weight = fib_path_get_weight(path_index);
286         fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
287
288         break;
289     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
290         if (fib_path_is_exclusive(path_index) ||
291             fib_path_is_deag(path_index))
292         {
293             vec_add2(ctx->next_hops, nh, 1);
294
295             nh->path_index = path_index;
296             nh->path_weight = fib_path_get_weight(path_index);
297             fib_path_contribute_forwarding(path_index,
298                                            FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
299                                            &nh->path_dpo);
300         }
301         break;
302     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
303         {
304             /*
305              * no label. we need a chain based on the payload. fixup.
306              */
307             vec_add2(ctx->next_hops, nh, 1);
308
309             nh->path_index = path_index;
310             nh->path_weight = fib_path_get_weight(path_index);
311             fib_path_contribute_forwarding(path_index,
312                                            fib_entry_chain_type_fixup(ctx->fib_entry,
313                                                                       ctx->fct),
314                                            &nh->path_dpo);
315             fib_path_stack_mpls_disp(path_index,
316                                      ctx->fib_entry->fe_prefix.fp_payload_proto,
317                                      &nh->path_dpo);
318
319             break;
320         }
321     case FIB_FORW_CHAIN_TYPE_ETHERNET:
322     case FIB_FORW_CHAIN_TYPE_NSH:
323         ASSERT(0);
324         break;
325     }
326 }
327
328 static fib_path_list_walk_rc_t
329 fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
330                                   fib_node_index_t path_index,
331                                   void *arg)
332 {
333     fib_entry_src_collect_forwarding_ctx_t *ctx;
334     fib_path_ext_t *path_ext;
335
336     ctx = arg;
337
338     /*
339      * if the path is not resolved, don't include it.
340      */
341     if (!fib_path_is_resolved(path_index))
342     {
343         return (FIB_PATH_LIST_WALK_CONTINUE);
344     }
345
346     if (fib_path_is_recursive_constrained(path_index))
347     {
348         ctx->n_recursive_constrained += 1;
349     }
350     if (0xffff == ctx->preference)
351     {
352         /*
353          * not set a preference yet, so the first path we encounter
354          * sets the preference we are collecting.
355          */
356         ctx->preference = fib_path_get_preference(path_index);
357     }
358     else if (ctx->preference != fib_path_get_preference(path_index))
359     {
360         /*
361          * this path does not belong to the same preference as the
362          * previous paths encountered. we are done now.
363          */
364         return (FIB_PATH_LIST_WALK_STOP);
365     }
366
367     /*
368      * get the matching path-extension for the path being visited.
369      */
370     path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
371                                                     path_index);
372
373     if (NULL != path_ext)
374     {
375         switch (path_ext->fpe_type)
376         {
377         case FIB_PATH_EXT_MPLS:
378             if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
379             {
380                 /*
381                  * found a matching extension. stack it to obtain the forwarding
382                  * info for this path.
383                  */
384                 ctx->next_hops =
385                     fib_path_ext_stack(path_ext,
386                                        ctx->fct,
387                                        fib_entry_chain_type_fixup(ctx->fib_entry,
388                                                                   ctx->fct),
389                                        ctx->next_hops);
390             }
391             else
392             {
393                 fib_entry_src_get_path_forwarding(path_index, ctx);
394             }
395             break;
396         case FIB_PATH_EXT_ADJ:
397             if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
398             {
399                 fib_entry_src_get_path_forwarding(path_index, ctx);
400             }
401             /*
402              * else
403              *  the path does not refine the cover, meaning that
404              *  the adjacency doesdoes not match the sub-net on the link.
405              *  So this path does not contribute forwarding.
406              */
407             break;
408         }
409     }
410     else
411     {
412         fib_entry_src_get_path_forwarding(path_index, ctx);
413     }
414
415     return (FIB_PATH_LIST_WALK_CONTINUE);
416 }
417
418 void
419 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
420                      const fib_entry_src_t *esrc,
421                      fib_forward_chain_type_t fct,
422                      dpo_id_t *dpo_lb)
423 {
424     dpo_proto_t lb_proto;
425
426     /*
427      * If the entry has path extensions then we construct a load-balance
428      * by stacking the extensions on the forwarding chains of the paths.
429      * Otherwise we use the load-balance of the path-list
430      */
431     fib_entry_src_collect_forwarding_ctx_t ctx = {
432         .esrc = esrc,
433         .fib_entry = fib_entry,
434         .next_hops = NULL,
435         .n_recursive_constrained = 0,
436         .fct = fct,
437         .preference = 0xffff,
438     };
439
440     /*
441      * As an optimisation we allocate the vector of next-hops to be sized
442      * equal to the maximum nuber of paths we will need, which is also the
443      * most likely number we will need, since in most cases the paths are 'up'.
444      */
445     vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
446     vec_reset_length(ctx.next_hops);
447
448     lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
449
450     fib_path_list_walk(esrc->fes_pl,
451                        fib_entry_src_collect_forwarding,
452                        &ctx);
453
454     if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
455     {
456         /*
457          * the client provided the DPO that the entry should link to.
458          * all entries must link to a LB, so if it is an LB already
459          * then we can use it.
460          */
461         if ((1 == vec_len(ctx.next_hops)) &&
462             (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
463         {
464             dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
465             dpo_reset(&ctx.next_hops[0].path_dpo);
466             return;
467         }
468     }
469
470     if (!dpo_id_is_valid(dpo_lb))
471     {
472         /*
473          * first time create
474          */
475         if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
476         {
477             dpo_set(dpo_lb,
478                     DPO_REPLICATE,
479                     lb_proto,
480                     MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
481         }
482         else
483         {
484             fib_protocol_t flow_hash_proto;
485             flow_hash_config_t fhc;
486
487             /*
488              * if the protocol for the LB we are building does not match that
489              * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
490              * then the fib_index is not an index that relates to the table
491              * type we need. So get the default flow-hash config instead.
492              */
493             flow_hash_proto = dpo_proto_to_fib(lb_proto);
494             if (fib_entry->fe_prefix.fp_proto != flow_hash_proto)
495             {
496                 fhc = fib_table_get_default_flow_hash_config(flow_hash_proto);
497             }
498             else
499             {
500                 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
501                                                      flow_hash_proto);
502             }
503
504             dpo_set(dpo_lb,
505                     DPO_LOAD_BALANCE,
506                     lb_proto,
507                     load_balance_create(0, lb_proto, fhc));
508         }
509     }
510
511     if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
512     {
513         /*
514          * MPLS multicast
515          */
516         replicate_multipath_update(dpo_lb, ctx.next_hops);
517     }
518     else
519     {
520         load_balance_multipath_update(dpo_lb,
521                                       ctx.next_hops,
522                                       fib_entry_calc_lb_flags(&ctx));
523         vec_free(ctx.next_hops);
524
525         /*
526          * if this entry is sourced by the uRPF-exempt source then we
527          * append the always present local0 interface (index 0) to the
528          * uRPF list so it is not empty. that way packets pass the loose check.
529          */
530         index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
531
532         if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
533                                   FIB_SOURCE_URPF_EXEMPT) ||
534              (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
535             (0 == fib_urpf_check_size(ui)))
536         {
537             /*
538              * The uRPF list we get from the path-list is shared by all
539              * other users of the list, but the uRPF exemption applies
540              * only to this prefix. So we need our own list.
541              */
542             ui = fib_urpf_list_alloc_and_lock();
543             fib_urpf_list_append(ui, 0);
544             fib_urpf_list_bake(ui);
545             load_balance_set_urpf(dpo_lb->dpoi_index, ui);
546             fib_urpf_list_unlock(ui);
547         }
548         else
549         {
550             load_balance_set_urpf(dpo_lb->dpoi_index, ui);
551         }
552         load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
553                                          fib_entry_get_flags_i(fib_entry));
554     }
555 }
556
557 void
558 fib_entry_src_action_install (fib_entry_t *fib_entry,
559                               fib_source_t source)
560 {
561     /*
562      * Install the forwarding chain for the given source into the forwarding
563      * tables
564      */
565     fib_forward_chain_type_t fct;
566     fib_entry_src_t *esrc;
567     int insert;
568
569     fct = fib_entry_get_default_chain_type(fib_entry);
570     esrc = fib_entry_src_find(fib_entry, source, NULL);
571
572     /*
573      * Every entry has its own load-balance object. All changes to the entry's
574      * forwarding result in an inplace modify of the load-balance. This means
575      * the load-balance object only needs to be added to the forwarding
576      * DB once, when it is created.
577      */
578     insert = !dpo_id_is_valid(&fib_entry->fe_lb);
579
580     fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
581
582     ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
583     FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
584
585     /*
586      * insert the adj into the data-plane forwarding trie
587      */
588     if (insert)
589     {
590        fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
591                                    &fib_entry->fe_prefix,
592                                    &fib_entry->fe_lb);
593     }
594
595     /*
596      * if any of the other chain types are already created they will need
597      * updating too
598      */
599     fib_entry_delegate_type_t fdt;
600     fib_entry_delegate_t *fed;
601
602     FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
603     {
604         fib_entry_src_mk_lb(fib_entry, esrc,
605                             fib_entry_delegate_type_to_chain_type(fdt),
606                             &fed->fd_dpo);
607     });
608 }
609
610 void
611 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
612 {
613     /*
614      * uninstall the forwarding chain from the forwarding tables
615      */
616     FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
617                   fib_entry->fe_adj_index);
618
619     if (dpo_id_is_valid(&fib_entry->fe_lb))
620     {
621         fib_table_fwding_dpo_remove(
622             fib_entry->fe_fib_index,
623             &fib_entry->fe_prefix,
624             &fib_entry->fe_lb);
625
626         dpo_reset(&fib_entry->fe_lb);
627     }
628 }
629
630 static void
631 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
632 {
633     fib_node_index_t *entries = NULL;
634
635     fib_path_list_recursive_loop_detect(path_list_index, &entries);
636
637     vec_free(entries);
638 }
639
640 void
641 fib_entry_src_action_activate (fib_entry_t *fib_entry,
642                                fib_source_t source)
643
644 {
645     int houston_we_are_go_for_install;
646     fib_entry_src_t *esrc;
647
648     esrc = fib_entry_src_find(fib_entry, source, NULL);
649
650     ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
651     ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
652
653     esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
654
655     if (NULL != fib_entry_src_vft[source].fesv_activate)
656     {
657         houston_we_are_go_for_install =
658             fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
659     }
660     else
661     {
662         /*
663          * the source is not providing an activate function, we'll assume
664          * therefore it has no objection to installing the entry
665          */
666         houston_we_are_go_for_install = !0;
667     }
668
669     /*
670      * link to the path-list provided by the source, and go check
671      * if that forms any loops in the graph.
672      */
673     fib_entry->fe_parent = esrc->fes_pl;
674     fib_entry->fe_sibling =
675         fib_path_list_child_add(fib_entry->fe_parent,
676                                 FIB_NODE_TYPE_ENTRY,
677                                 fib_entry_get_index(fib_entry));
678
679     fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
680
681     FIB_ENTRY_DBG(fib_entry, "activate: %d",
682                   fib_entry->fe_parent);
683
684     if (0 != houston_we_are_go_for_install)
685     {
686         fib_entry_src_action_install(fib_entry, source);
687     }
688     else
689     {
690         fib_entry_src_action_uninstall(fib_entry);
691     }
692 }
693
694 void
695 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
696                                  fib_source_t source)
697
698 {
699     fib_node_index_t path_list_index;
700     fib_entry_src_t *esrc;
701
702     esrc = fib_entry_src_find(fib_entry, source, NULL);
703
704     ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
705
706     if (NULL != fib_entry_src_vft[source].fesv_deactivate)
707     {
708         fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
709     }
710
711     esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
712
713     FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
714
715     /*
716      * un-link from an old path-list. Check for any loops this will clear
717      */
718     path_list_index = fib_entry->fe_parent;
719     fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
720
721     fib_entry_recursive_loop_detect_i(path_list_index);
722
723     /*
724      * this will unlock the path-list, so it may be invalid thereafter.
725      */
726     fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
727     fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
728 }
729
730 static void
731 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
732                                  fib_source_t source)
733 {
734     fib_entry_src_t *esrc;
735
736     vec_foreach(esrc, fib_entry->fe_srcs)
737     {
738         if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
739         {
740             fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
741                                                              fib_entry,
742                                                              source);
743         }
744     }
745 }
746
747 void
748 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
749                                  fib_source_t source)
750 {
751     fib_node_index_t path_list_index;
752     fib_entry_src_t *esrc;
753
754     esrc = fib_entry_src_find(fib_entry, source, NULL);
755
756     ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
757
758     FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
759                   fib_entry->fe_parent,
760                   esrc->fes_pl);
761
762     if (fib_entry->fe_parent != esrc->fes_pl)
763     {
764         /*
765          * un-link from an old path-list. Check for any loops this will clear
766          */
767         path_list_index = fib_entry->fe_parent;
768         fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
769
770         /*
771          * temporary lock so it doesn't get deleted when this entry is no
772          * longer a child.
773          */
774         fib_path_list_lock(path_list_index);
775
776         /*
777          * this entry is no longer a child. after unlinking check if any loops
778          * were broken
779          */
780         fib_path_list_child_remove(path_list_index,
781                                    fib_entry->fe_sibling);
782
783         fib_entry_recursive_loop_detect_i(path_list_index);
784
785         /*
786          * link to the path-list provided by the source, and go check
787          * if that forms any loops in the graph.
788          */
789         fib_entry->fe_parent = esrc->fes_pl;
790         fib_entry->fe_sibling =
791             fib_path_list_child_add(fib_entry->fe_parent,
792                                     FIB_NODE_TYPE_ENTRY,
793                                     fib_entry_get_index(fib_entry));
794
795         fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
796         fib_path_list_unlock(path_list_index);
797     }
798     fib_entry_src_action_install(fib_entry, source);
799     fib_entry_src_action_fwd_update(fib_entry, source);
800 }
801
802 void
803 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
804                                 fib_source_t source)
805 {
806     fib_entry_src_t *esrc;
807
808     esrc = fib_entry_src_find(fib_entry, source, NULL);
809
810     if (NULL != fib_entry_src_vft[source].fesv_installed)
811     {
812         fib_entry_src_vft[source].fesv_installed(esrc,
813                                                  fib_entry);
814     }
815
816     fib_entry_src_action_fwd_update(fib_entry, source);
817 }
818
819 /*
820  * fib_entry_src_action_add
821  *
822  * Adding a source can result in a new fib_entry being created, which
823  * can inturn mean the pool is realloc'd and thus the entry passed as
824  * an argument it also realloc'd
825  * @return the original entry
826  */
827 fib_entry_t *
828 fib_entry_src_action_add (fib_entry_t *fib_entry,
829                           fib_source_t source,
830                           fib_entry_flag_t flags,
831                           const dpo_id_t *dpo)
832 {
833     fib_node_index_t fib_entry_index;
834     fib_entry_src_t *esrc;
835
836     esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
837
838     esrc->fes_ref_count++;
839
840     if (1 != esrc->fes_ref_count)
841     {
842         /*
843          * we only want to add the source on the 0->1 transition
844          */
845         return (fib_entry);
846     }
847
848     esrc->fes_entry_flags = flags;
849
850     /*
851      * save variable so we can recover from a fib_entry realloc.
852      */
853     fib_entry_index = fib_entry_get_index(fib_entry);
854
855     if (NULL != fib_entry_src_vft[source].fesv_add)
856     {
857         fib_entry_src_vft[source].fesv_add(esrc,
858                                            fib_entry,
859                                            flags,
860                                            fib_entry_get_dpo_proto(fib_entry),
861                                            dpo);
862     }
863
864     fib_entry = fib_entry_get(fib_entry_index);
865
866     esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
867
868     fib_path_list_lock(esrc->fes_pl);
869
870     /*
871      * the source owns a lock on the entry
872      */
873     fib_entry_lock(fib_entry_get_index(fib_entry));
874
875     return (fib_entry);
876 }
877
878 /*
879  * fib_entry_src_action_update
880  *
881  * Adding a source can result in a new fib_entry being created, which
882  * can inturn mean the pool is realloc'd and thus the entry passed as
883  * an argument it also realloc'd
884  * @return the original entry
885  */
886 fib_entry_t *
887 fib_entry_src_action_update (fib_entry_t *fib_entry,
888                              fib_source_t source,
889                              fib_entry_flag_t flags,
890                              const dpo_id_t *dpo)
891 {
892     fib_node_index_t fib_entry_index, old_path_list_index;
893     fib_entry_src_t *esrc;
894
895     esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
896
897     if (NULL == esrc)
898         return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
899
900     old_path_list_index = esrc->fes_pl;
901     esrc->fes_entry_flags = flags;
902
903     /*
904      * save variable so we can recover from a fib_entry realloc.
905      */
906     fib_entry_index = fib_entry_get_index(fib_entry);
907
908     if (NULL != fib_entry_src_vft[source].fesv_add)
909     {
910         fib_entry_src_vft[source].fesv_add(esrc,
911                                            fib_entry,
912                                            flags,
913                                            fib_entry_get_dpo_proto(fib_entry),
914                                            dpo);
915     }
916
917     fib_entry = fib_entry_get(fib_entry_index);
918
919     esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
920
921     fib_path_list_lock(esrc->fes_pl);
922     fib_path_list_unlock(old_path_list_index);
923
924     return (fib_entry);
925 }
926
927
928 fib_entry_src_flag_t
929 fib_entry_src_action_remove (fib_entry_t *fib_entry,
930                              fib_source_t source)
931
932 {
933     fib_node_index_t old_path_list;
934     fib_entry_src_flag_t sflags;
935     fib_entry_src_t *esrc;
936
937     esrc = fib_entry_src_find(fib_entry, source, NULL);
938
939     if (NULL == esrc)
940         return (FIB_ENTRY_SRC_FLAG_ACTIVE);
941
942     esrc->fes_ref_count--;
943     sflags = esrc->fes_flags;
944
945     if (0 != esrc->fes_ref_count)
946     {
947         /*
948          * only remove the source on the 1->0 transisition
949          */
950         return (sflags);
951     }
952
953     if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
954     {
955         fib_entry_src_action_deactivate(fib_entry, source);
956     }
957
958     old_path_list = esrc->fes_pl;
959
960     if (NULL != fib_entry_src_vft[source].fesv_remove)
961     {
962         fib_entry_src_vft[source].fesv_remove(esrc);
963     }
964
965     fib_path_list_unlock(old_path_list);
966     fib_entry_unlock(fib_entry_get_index(fib_entry));
967
968     sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
969     fib_entry_src_action_deinit(fib_entry, source);
970
971     return (sflags);
972 }
973
974 /*
975  * fib_route_attached_cross_table
976  *
977  * Return true the the route is attached via an interface that
978  * is not in the same table as the route
979  */
980 static inline int
981 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
982                                 const fib_route_path_t *rpath)
983 {
984     /*
985      * - All zeros next-hop
986      * - a valid interface
987      * - entry's fib index not equeal to interface's index
988      */
989     if (ip46_address_is_zero(&rpath->frp_addr) &&
990         (~0 != rpath->frp_sw_if_index) &&
991         (fib_entry->fe_fib_index != 
992          fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
993                                              rpath->frp_sw_if_index)))
994     {
995         return (!0);
996     }
997     return (0);
998 }
999
1000 /*
1001  * fib_route_attached_cross_table
1002  *
1003  * Return true the the route is attached via an interface that
1004  * is not in the same table as the route
1005  */
1006 static inline int
1007 fib_path_is_attached (const fib_route_path_t *rpath)
1008 {
1009     /*
1010      * - All zeros next-hop
1011      * - a valid interface
1012      */
1013     if (ip46_address_is_zero(&rpath->frp_addr) &&
1014         (~0 != rpath->frp_sw_if_index))
1015     {
1016         return (!0);
1017     }
1018     else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1019     {
1020         return (!0);
1021     }
1022     return (0);
1023 }
1024
1025 fib_path_list_flags_t
1026 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1027 {
1028     fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1029
1030     if (eflags & FIB_ENTRY_FLAG_DROP)
1031     {
1032         plf |= FIB_PATH_LIST_FLAG_DROP;
1033     }
1034     if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1035     {
1036         plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1037     }
1038     if (eflags & FIB_ENTRY_FLAG_LOCAL)
1039     {
1040         plf |= FIB_PATH_LIST_FLAG_LOCAL;
1041     }
1042
1043     return (plf);
1044 }
1045
1046 static void
1047 fib_entry_flags_update (const fib_entry_t *fib_entry,
1048                         const fib_route_path_t *rpath,
1049                         fib_path_list_flags_t *pl_flags,
1050                         fib_entry_src_t *esrc)
1051 {
1052     if ((esrc->fes_src == FIB_SOURCE_API) ||
1053         (esrc->fes_src == FIB_SOURCE_CLI))
1054     {
1055         if (fib_path_is_attached(rpath))
1056         {
1057             esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1058         }
1059         else
1060         {
1061             esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1062         }
1063     }
1064     if (fib_route_attached_cross_table(fib_entry, rpath))
1065     {
1066         esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1067     }
1068     else
1069     {
1070         esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1071     }
1072 }
1073
1074 /*
1075  * fib_entry_src_action_add
1076  *
1077  * Adding a source can result in a new fib_entry being created, which
1078  * can inturn mean the pool is realloc'd and thus the entry passed as
1079  * an argument it also realloc'd
1080  * @return the entry
1081  */
1082 fib_entry_t*
1083 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1084                                fib_source_t source,
1085                                fib_entry_flag_t flags,
1086                                const fib_route_path_t *rpath)
1087 {
1088     fib_node_index_t old_path_list, fib_entry_index;
1089     fib_path_list_flags_t pl_flags;
1090     fib_entry_src_t *esrc;
1091
1092     /*
1093      * save variable so we can recover from a fib_entry realloc.
1094      */
1095     fib_entry_index = fib_entry_get_index(fib_entry);
1096
1097     esrc = fib_entry_src_find(fib_entry, source, NULL);
1098     if (NULL == esrc)
1099     {
1100         fib_entry =
1101             fib_entry_src_action_add(fib_entry,
1102                                      source,
1103                                      flags,
1104                                      drop_dpo_get(
1105                                          fib_entry_get_dpo_proto(fib_entry)));
1106         esrc = fib_entry_src_find(fib_entry, source, NULL);
1107     }
1108
1109     /*
1110      * we are no doubt modifying a path-list. If the path-list
1111      * is shared, and hence not modifiable, then the index returned
1112      * will be for a different path-list. This FIB entry to needs
1113      * to maintain its lock appropriately.
1114      */
1115     old_path_list = esrc->fes_pl;
1116
1117     ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1118
1119     pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1120     fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1121
1122     fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1123     fib_entry = fib_entry_get(fib_entry_index);
1124
1125     fib_path_list_lock(esrc->fes_pl);
1126     fib_path_list_unlock(old_path_list);
1127
1128     return (fib_entry);
1129 }
1130
1131 /*
1132  * fib_entry_src_action_swap
1133  *
1134  * The source is providing new paths to replace the old ones.
1135  * Adding a source can result in a new fib_entry being created, which
1136  * can inturn mean the pool is realloc'd and thus the entry passed as
1137  * an argument it also realloc'd
1138  * @return the entry
1139  */
1140 fib_entry_t*
1141 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1142                                 fib_source_t source,
1143                                 fib_entry_flag_t flags,                         
1144                                 const fib_route_path_t *rpaths)
1145 {
1146     fib_node_index_t old_path_list, fib_entry_index;
1147     fib_path_list_flags_t pl_flags;
1148     const fib_route_path_t *rpath;
1149     fib_entry_src_t *esrc;
1150
1151     esrc = fib_entry_src_find(fib_entry, source, NULL);
1152
1153     /*
1154      * save variable so we can recover from a fib_entry realloc.
1155      */
1156     fib_entry_index = fib_entry_get_index(fib_entry);
1157
1158     if (NULL == esrc)
1159     {
1160         fib_entry = fib_entry_src_action_add(fib_entry,
1161                                              source,
1162                                              flags,
1163                                              drop_dpo_get(
1164                                                  fib_entry_get_dpo_proto(fib_entry)));
1165         esrc = fib_entry_src_find(fib_entry, source, NULL);
1166     }
1167
1168     /*
1169      * swapping paths may create a new path-list (or may use an existing shared)
1170      * but we are certainly getting a different one. This FIB entry to needs
1171      * to maintain its lock appropriately.
1172      */
1173     old_path_list = esrc->fes_pl;
1174
1175     ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1176
1177     pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1178
1179     vec_foreach(rpath, rpaths)
1180     {
1181         fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1182     }
1183
1184     fib_entry_src_vft[source].fesv_path_swap(esrc,
1185                                              fib_entry,
1186                                              pl_flags,
1187                                              rpaths);
1188
1189     fib_entry = fib_entry_get(fib_entry_index);
1190
1191     fib_path_list_lock(esrc->fes_pl);
1192     fib_path_list_unlock(old_path_list);
1193
1194     return (fib_entry);
1195 }
1196
1197 fib_entry_src_flag_t
1198 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1199                                   fib_source_t source,
1200                                   const fib_route_path_t *rpath)
1201 {
1202     fib_path_list_flags_t pl_flags;
1203     fib_node_index_t old_path_list;
1204     fib_entry_src_t *esrc;
1205
1206     esrc = fib_entry_src_find(fib_entry, source, NULL);
1207
1208     ASSERT(NULL != esrc);
1209     ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1210
1211     /*
1212      * we no doubt modifying a path-list. If the path-list
1213      * is shared, and hence not modifiable, then the index returned
1214      * will be for a different path-list. This FIB entry to needs
1215      * to maintain its lock appropriately.
1216      */
1217     old_path_list = esrc->fes_pl;
1218
1219     ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1220
1221     pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1222     fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1223
1224     fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1225
1226     /*
1227      * lock the new path-list, unlock the old if it had one
1228      */
1229     fib_path_list_unlock(old_path_list);
1230
1231     if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1232         fib_path_list_lock(esrc->fes_pl);
1233         return (FIB_ENTRY_SRC_FLAG_ADDED);
1234     }
1235     else
1236     {
1237         /*
1238          * no more paths left from this source
1239          */
1240         fib_entry_src_action_remove(fib_entry, source);
1241         return (FIB_ENTRY_SRC_FLAG_NONE);
1242     }
1243 }
1244
1245 u8*
1246 fib_entry_src_format (fib_entry_t *fib_entry,
1247                       fib_source_t source,
1248                       u8* s)
1249 {
1250     fib_entry_src_t *esrc;
1251
1252     esrc = fib_entry_src_find(fib_entry, source, NULL);
1253
1254     if (NULL != fib_entry_src_vft[source].fesv_format)
1255     {
1256         return (fib_entry_src_vft[source].fesv_format(esrc, s));
1257     }
1258     return (s);
1259 }
1260
1261 adj_index_t
1262 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1263                               fib_source_t source)
1264 {
1265     fib_entry_t *fib_entry;
1266     fib_entry_src_t *esrc;
1267
1268     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1269         return (ADJ_INDEX_INVALID);
1270
1271     fib_entry = fib_entry_get(fib_entry_index);
1272     esrc = fib_entry_src_find(fib_entry, source, NULL);
1273
1274     if (NULL != esrc)
1275     {
1276         if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1277         {
1278             return (fib_path_list_get_adj(
1279                         esrc->fes_pl,
1280                         fib_entry_get_default_chain_type(fib_entry)));
1281         }
1282     }
1283     return (ADJ_INDEX_INVALID);
1284 }
1285
1286 const int
1287 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1288                               fib_source_t source,
1289                               dpo_id_t *dpo)
1290 {
1291     fib_entry_t *fib_entry;
1292     fib_entry_src_t *esrc;
1293
1294     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1295         return (0);
1296
1297     fib_entry = fib_entry_get(fib_entry_index);
1298     esrc = fib_entry_src_find(fib_entry, source, NULL);
1299
1300     if (NULL != esrc)
1301     {
1302         if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1303         {
1304             fib_path_list_contribute_forwarding(
1305                 esrc->fes_pl,
1306                 fib_entry_get_default_chain_type(fib_entry),
1307                 dpo);
1308
1309             return (dpo_id_is_valid(dpo));
1310         }
1311     }
1312     return (0);
1313 }
1314
1315 u32
1316 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1317                                               fib_source_t source)
1318 {
1319     fib_entry_t *fib_entry;
1320     fib_entry_src_t *esrc;
1321
1322     fib_entry = fib_entry_get(entry_index);
1323
1324     esrc = fib_entry_src_find(fib_entry, source, NULL);
1325
1326     if (NULL != esrc)
1327     {
1328         if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1329         {
1330             return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1331         }
1332     }
1333     return (~0);
1334 }
1335
1336 fib_entry_flag_t
1337 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1338                                 fib_source_t source)
1339 {
1340     fib_entry_t *fib_entry;
1341     fib_entry_src_t *esrc;
1342
1343     fib_entry = fib_entry_get(entry_index);
1344
1345     esrc = fib_entry_src_find(fib_entry, source, NULL);
1346
1347     if (NULL != esrc)
1348     {
1349         return (esrc->fes_entry_flags);
1350     }
1351
1352     return (FIB_ENTRY_FLAG_NONE);
1353 }
1354
1355 fib_entry_flag_t
1356 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1357 {
1358     fib_entry_flag_t flags;
1359
1360     /*
1361      * the vector of sources is deliberately arranged in priority order
1362      */
1363     if (0 == vec_len(fib_entry->fe_srcs))
1364     {
1365         flags = FIB_ENTRY_FLAG_NONE;
1366     }
1367     else
1368     {
1369         fib_entry_src_t *esrc;
1370
1371         esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1372         flags = esrc->fes_entry_flags;
1373     }
1374
1375     return (flags);
1376 }
1377
1378 void
1379 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1380                            fib_source_t source,
1381                            const void *data)
1382 {
1383     fib_entry_t *fib_entry;
1384     fib_entry_src_t *esrc;
1385
1386     fib_entry = fib_entry_get(fib_entry_index);
1387     esrc = fib_entry_src_find(fib_entry, source, NULL);
1388
1389     if (NULL != esrc &&
1390         NULL != fib_entry_src_vft[source].fesv_set_data)
1391     {
1392         fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1393     }
1394 }
1395
1396 const void*
1397 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1398                            fib_source_t source)
1399 {
1400     fib_entry_t *fib_entry;
1401     fib_entry_src_t *esrc;
1402
1403     fib_entry = fib_entry_get(fib_entry_index);
1404     esrc = fib_entry_src_find(fib_entry, source, NULL);
1405
1406     if (NULL != esrc &&
1407         NULL != fib_entry_src_vft[source].fesv_get_data)
1408     {
1409         return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1410     }
1411     return (NULL);
1412 }
1413
1414 void
1415 fib_entry_src_module_init (void)
1416 {
1417     fib_entry_src_rr_register();
1418     fib_entry_src_interface_register();
1419     fib_entry_src_default_route_register();
1420     fib_entry_src_special_register();
1421     fib_entry_src_api_register();
1422     fib_entry_src_adj_register();
1423     fib_entry_src_mpls_register();
1424     fib_entry_src_lisp_register();
1425 }