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