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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
30 static fib_entry_src_vft_t fib_entry_src_vft[FIB_SOURCE_MAX];
33 fib_entry_src_register (fib_source_t source,
34 const fib_entry_src_vft_t *vft)
36 fib_entry_src_vft[source] = *vft;
40 fib_entry_src_cmp_for_sort (void * v1,
43 fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
45 return (esrc1->fes_src - esrc2->fes_src);
49 fib_entry_src_action_init (fib_entry_t *fib_entry,
53 fib_entry_src_t esrc = {
54 .fes_pl = FIB_NODE_INDEX_INVALID,
55 .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
59 if (NULL != fib_entry_src_vft[source].fesv_init)
61 fib_entry_src_vft[source].fesv_init(&esrc);
64 vec_add1(fib_entry->fe_srcs, esrc);
65 vec_sort_with_function(fib_entry->fe_srcs,
66 fib_entry_src_cmp_for_sort);
69 static fib_entry_src_t *
70 fib_entry_src_find (const fib_entry_t *fib_entry,
75 fib_entry_src_t *esrc;
79 vec_foreach(esrc, fib_entry->fe_srcs)
81 if (esrc->fes_src == source)
99 fib_entry_is_sourced (fib_node_index_t fib_entry_index,
102 fib_entry_t *fib_entry;
104 fib_entry = fib_entry_get(fib_entry_index);
106 return (NULL != fib_entry_src_find(fib_entry, source, NULL));
109 static fib_entry_src_t *
110 fib_entry_src_find_or_create (fib_entry_t *fib_entry,
114 fib_entry_src_t *esrc;
116 esrc = fib_entry_src_find(fib_entry, source, NULL);
120 fib_entry_src_action_init(fib_entry, source);
123 return (fib_entry_src_find(fib_entry, source, NULL));
127 fib_entry_src_action_deinit (fib_entry_t *fib_entry,
131 fib_entry_src_t *esrc;
134 esrc = fib_entry_src_find(fib_entry, source, &index);
136 ASSERT(NULL != esrc);
138 if (NULL != fib_entry_src_vft[source].fesv_deinit)
140 fib_entry_src_vft[source].fesv_deinit(esrc);
143 fib_path_ext_list_flush(&esrc->fes_path_exts);
144 vec_del1(fib_entry->fe_srcs, index);
147 fib_entry_src_cover_res_t
148 fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
151 if (NULL != fib_entry_src_vft[source].fesv_cover_change)
153 return (fib_entry_src_vft[source].fesv_cover_change(
154 fib_entry_src_find(fib_entry, source, NULL),
158 fib_entry_src_cover_res_t res = {
160 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
165 fib_entry_src_cover_res_t
166 fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
169 if (NULL != fib_entry_src_vft[source].fesv_cover_update)
171 return (fib_entry_src_vft[source].fesv_cover_update(
172 fib_entry_src_find(fib_entry, source, NULL),
176 fib_entry_src_cover_res_t res = {
178 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
183 typedef struct fib_entry_src_collect_forwarding_ctx_t_
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;
191 } fib_entry_src_collect_forwarding_ctx_t;
194 * @brief Determine whether this FIB entry should use a load-balance MAP
195 * to support PIC edge fast convergence
198 fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
201 * We'll use a LB map if the path-list has multiple recursive paths.
202 * recursive paths implies BGP, and hence scale.
204 if (ctx->n_recursive_constrained > 1 &&
205 fib_path_list_is_popular(ctx->esrc->fes_pl))
207 return (LOAD_BALANCE_FLAG_USES_MAP);
209 return (LOAD_BALANCE_FLAG_NONE);
213 fib_entry_src_valid_out_label (mpls_label_t label)
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));
222 * @brief Turn the chain type requested by the client into the one they
225 fib_forward_chain_type_t
226 fib_entry_chain_type_fixup (const fib_entry_t *entry,
227 fib_forward_chain_type_t fct)
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.
234 fib_forward_chain_type_t dfct;
236 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct)
241 dfct = fib_entry_get_default_chain_type(entry);
243 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS == dfct)
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
250 return (fib_forw_chain_type_from_dpo_proto(
251 entry->fe_prefix.fp_payload_proto));
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
263 fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
264 fib_entry_src_collect_forwarding_ctx_t *ctx)
266 load_balance_path_t *nh;
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
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:
279 * EOS traffic with no label to stack, we need the IP Adj
281 vec_add2(ctx->next_hops, nh, 1);
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);
288 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
289 if (fib_path_is_exclusive(path_index) ||
290 fib_path_is_deag(path_index))
292 vec_add2(ctx->next_hops, nh, 1);
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,
301 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
304 * no label. we need a chain based on the payload. fixup.
306 vec_add2(ctx->next_hops, nh, 1);
308 nh->path_index = path_index;
309 nh->path_weight = fib_path_get_weight(path_index);
310 fib_path_contribute_forwarding(path_index,
311 fib_entry_chain_type_fixup(ctx->fib_entry,
314 fib_path_stack_mpls_disp(path_index,
315 ctx->fib_entry->fe_prefix.fp_payload_proto,
320 case FIB_FORW_CHAIN_TYPE_ETHERNET:
321 case FIB_FORW_CHAIN_TYPE_NSH:
327 static fib_path_list_walk_rc_t
328 fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
329 fib_node_index_t path_index,
332 fib_entry_src_collect_forwarding_ctx_t *ctx;
333 fib_path_ext_t *path_ext;
338 * if the path is not resolved, don't include it.
340 if (!fib_path_is_resolved(path_index))
342 return (FIB_PATH_LIST_WALK_CONTINUE);
345 if (fib_path_is_recursive_constrained(path_index))
347 ctx->n_recursive_constrained += 1;
349 if (0xffff == ctx->preference)
352 * not set a preference yet, so the first path we encounter
353 * sets the preference we are collecting.
355 ctx->preference = fib_path_get_preference(path_index);
357 else if (ctx->preference != fib_path_get_preference(path_index))
360 * this path does not belong to the same preference as the
361 * previous paths encountered. we are done now.
363 return (FIB_PATH_LIST_WALK_STOP);
367 * get the matching path-extension for the path being visited.
369 path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
372 if (NULL != path_ext)
374 switch (path_ext->fpe_type)
376 case FIB_PATH_EXT_MPLS:
377 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
380 * found a matching extension. stack it to obtain the forwarding
381 * info for this path.
384 fib_path_ext_stack(path_ext,
386 fib_entry_chain_type_fixup(ctx->fib_entry,
392 fib_entry_src_get_path_forwarding(path_index, ctx);
395 case FIB_PATH_EXT_ADJ:
396 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
398 fib_entry_src_get_path_forwarding(path_index, ctx);
402 * the path does not refine the cover, meaning that
403 * the adjacency doesdoes not match the sub-net on the link.
404 * So this path does not contribute forwarding.
411 fib_entry_src_get_path_forwarding(path_index, ctx);
414 return (FIB_PATH_LIST_WALK_CONTINUE);
418 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
419 const fib_entry_src_t *esrc,
420 fib_forward_chain_type_t fct,
423 dpo_proto_t lb_proto;
426 * If the entry has path extensions then we construct a load-balance
427 * by stacking the extensions on the forwarding chains of the paths.
428 * Otherwise we use the load-balance of the path-list
430 fib_entry_src_collect_forwarding_ctx_t ctx = {
432 .fib_entry = fib_entry,
434 .n_recursive_constrained = 0,
436 .preference = 0xffff,
440 * As an optimisation we allocate the vector of next-hops to be sized
441 * equal to the maximum nuber of paths we will need, which is also the
442 * most likely number we will need, since in most cases the paths are 'up'.
444 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
445 vec_reset_length(ctx.next_hops);
447 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
449 fib_path_list_walk(esrc->fes_pl,
450 fib_entry_src_collect_forwarding,
453 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
456 * the client provided the DPO that the entry should link to.
457 * all entries must link to a LB, so if it is an LB already
458 * then we can use it.
460 if ((1 == vec_len(ctx.next_hops)) &&
461 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
463 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
464 dpo_reset(&ctx.next_hops[0].path_dpo);
469 if (!dpo_id_is_valid(dpo_lb))
474 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
479 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
483 flow_hash_config_t fhc;
487 * if the protocol for the LB we are building does not match that
488 * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
489 * then the fib_index is not an index that relates to the table
490 * type we need. So get the default flow-hash config instead.
492 fp = dpo_proto_to_fib(lb_proto);
494 if (fib_entry->fe_prefix.fp_proto != fp)
496 fhc = fib_table_get_default_flow_hash_config(fp);
500 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index, fp);
505 load_balance_create(0, lb_proto, fhc));
509 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
514 replicate_multipath_update(dpo_lb, ctx.next_hops);
518 load_balance_multipath_update(dpo_lb,
520 fib_entry_calc_lb_flags(&ctx));
521 vec_free(ctx.next_hops);
524 * if this entry is sourced by the uRPF-exempt source then we
525 * append the always present local0 interface (index 0) to the
526 * uRPF list so it is not empty. that way packets pass the loose check.
528 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
530 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
531 FIB_SOURCE_URPF_EXEMPT) ||
532 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
533 (0 == fib_urpf_check_size(ui)))
536 * The uRPF list we get from the path-list is shared by all
537 * other users of the list, but the uRPF exemption applies
538 * only to this prefix. So we need our own list.
540 ui = fib_urpf_list_alloc_and_lock();
541 fib_urpf_list_append(ui, 0);
542 fib_urpf_list_bake(ui);
543 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
544 fib_urpf_list_unlock(ui);
548 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
550 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
551 fib_entry_get_flags_i(fib_entry));
556 fib_entry_src_action_install (fib_entry_t *fib_entry,
560 * Install the forwarding chain for the given source into the forwarding
563 fib_forward_chain_type_t fct;
564 fib_entry_src_t *esrc;
567 fct = fib_entry_get_default_chain_type(fib_entry);
568 esrc = fib_entry_src_find(fib_entry, source, NULL);
571 * Every entry has its own load-balance object. All changes to the entry's
572 * forwarding result in an inplace modify of the load-balance. This means
573 * the load-balance object only needs to be added to the forwarding
574 * DB once, when it is created.
576 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
578 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
580 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
581 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
584 * insert the adj into the data-plane forwarding trie
588 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
589 &fib_entry->fe_prefix,
594 * if any of the other chain types are already created they will need
597 fib_entry_delegate_type_t fdt;
598 fib_entry_delegate_t *fed;
600 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
602 fib_entry_src_mk_lb(fib_entry, esrc,
603 fib_entry_delegate_type_to_chain_type(fdt),
609 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
612 * uninstall the forwarding chain from the forwarding tables
614 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
615 fib_entry->fe_adj_index);
617 if (dpo_id_is_valid(&fib_entry->fe_lb))
619 fib_table_fwding_dpo_remove(
620 fib_entry->fe_fib_index,
621 &fib_entry->fe_prefix,
624 dpo_reset(&fib_entry->fe_lb);
629 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
631 fib_node_index_t *entries = NULL;
633 fib_path_list_recursive_loop_detect(path_list_index, &entries);
639 fib_entry_src_action_activate (fib_entry_t *fib_entry,
643 int houston_we_are_go_for_install;
644 fib_entry_src_t *esrc;
646 esrc = fib_entry_src_find(fib_entry, source, NULL);
648 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
649 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
651 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
653 if (NULL != fib_entry_src_vft[source].fesv_activate)
655 houston_we_are_go_for_install =
656 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
661 * the source is not providing an activate function, we'll assume
662 * therefore it has no objection to installing the entry
664 houston_we_are_go_for_install = !0;
668 * link to the path-list provided by the source, and go check
669 * if that forms any loops in the graph.
671 fib_entry->fe_parent = esrc->fes_pl;
672 fib_entry->fe_sibling =
673 fib_path_list_child_add(fib_entry->fe_parent,
675 fib_entry_get_index(fib_entry));
677 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
679 FIB_ENTRY_DBG(fib_entry, "activate: %d",
680 fib_entry->fe_parent);
682 if (0 != houston_we_are_go_for_install)
684 fib_entry_src_action_install(fib_entry, source);
688 fib_entry_src_action_uninstall(fib_entry);
693 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
697 fib_node_index_t path_list_index;
698 fib_entry_src_t *esrc;
700 esrc = fib_entry_src_find(fib_entry, source, NULL);
702 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
704 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
706 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
709 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
711 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
714 * un-link from an old path-list. Check for any loops this will clear
716 path_list_index = fib_entry->fe_parent;
717 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
719 fib_entry_recursive_loop_detect_i(path_list_index);
722 * this will unlock the path-list, so it may be invalid thereafter.
724 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
725 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
729 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
732 fib_entry_src_t *esrc;
734 vec_foreach(esrc, fib_entry->fe_srcs)
736 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
738 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
746 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
749 fib_node_index_t path_list_index;
750 fib_entry_src_t *esrc;
752 esrc = fib_entry_src_find(fib_entry, source, NULL);
754 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
756 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
757 fib_entry->fe_parent,
760 if (fib_entry->fe_parent != esrc->fes_pl)
763 * un-link from an old path-list. Check for any loops this will clear
765 path_list_index = fib_entry->fe_parent;
766 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
769 * temporary lock so it doesn't get deleted when this entry is no
772 fib_path_list_lock(path_list_index);
775 * this entry is no longer a child. after unlinking check if any loops
778 fib_path_list_child_remove(path_list_index,
779 fib_entry->fe_sibling);
781 fib_entry_recursive_loop_detect_i(path_list_index);
784 * link to the path-list provided by the source, and go check
785 * if that forms any loops in the graph.
787 fib_entry->fe_parent = esrc->fes_pl;
788 fib_entry->fe_sibling =
789 fib_path_list_child_add(fib_entry->fe_parent,
791 fib_entry_get_index(fib_entry));
793 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
794 fib_path_list_unlock(path_list_index);
796 fib_entry_src_action_install(fib_entry, source);
797 fib_entry_src_action_fwd_update(fib_entry, source);
801 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
804 fib_entry_src_t *esrc;
806 esrc = fib_entry_src_find(fib_entry, source, NULL);
808 if (NULL != fib_entry_src_vft[source].fesv_installed)
810 fib_entry_src_vft[source].fesv_installed(esrc,
814 fib_entry_src_action_fwd_update(fib_entry, source);
818 * fib_entry_src_action_add
820 * Adding a source can result in a new fib_entry being created, which
821 * can inturn mean the pool is realloc'd and thus the entry passed as
822 * an argument it also realloc'd
823 * @return the original entry
826 fib_entry_src_action_add (fib_entry_t *fib_entry,
828 fib_entry_flag_t flags,
831 fib_node_index_t fib_entry_index;
832 fib_entry_src_t *esrc;
834 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
836 esrc->fes_ref_count++;
838 if (1 != esrc->fes_ref_count)
841 * we only want to add the source on the 0->1 transition
846 esrc->fes_entry_flags = flags;
849 * save variable so we can recover from a fib_entry realloc.
851 fib_entry_index = fib_entry_get_index(fib_entry);
853 if (NULL != fib_entry_src_vft[source].fesv_add)
855 fib_entry_src_vft[source].fesv_add(esrc,
858 fib_entry_get_dpo_proto(fib_entry),
862 fib_entry = fib_entry_get(fib_entry_index);
864 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
866 fib_path_list_lock(esrc->fes_pl);
869 * the source owns a lock on the entry
871 fib_entry_lock(fib_entry_get_index(fib_entry));
877 * fib_entry_src_action_update
879 * Adding a source can result in a new fib_entry being created, which
880 * can inturn mean the pool is realloc'd and thus the entry passed as
881 * an argument it also realloc'd
882 * @return the original entry
885 fib_entry_src_action_update (fib_entry_t *fib_entry,
887 fib_entry_flag_t flags,
890 fib_node_index_t fib_entry_index, old_path_list_index;
891 fib_entry_src_t *esrc;
893 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
896 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
898 old_path_list_index = esrc->fes_pl;
899 esrc->fes_entry_flags = flags;
902 * save variable so we can recover from a fib_entry realloc.
904 fib_entry_index = fib_entry_get_index(fib_entry);
906 if (NULL != fib_entry_src_vft[source].fesv_add)
908 fib_entry_src_vft[source].fesv_add(esrc,
911 fib_entry_get_dpo_proto(fib_entry),
915 fib_entry = fib_entry_get(fib_entry_index);
917 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
919 fib_path_list_lock(esrc->fes_pl);
920 fib_path_list_unlock(old_path_list_index);
927 fib_entry_src_action_remove (fib_entry_t *fib_entry,
931 fib_node_index_t old_path_list;
932 fib_entry_src_flag_t sflags;
933 fib_entry_src_t *esrc;
935 esrc = fib_entry_src_find(fib_entry, source, NULL);
938 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
940 esrc->fes_ref_count--;
941 sflags = esrc->fes_flags;
943 if (0 != esrc->fes_ref_count)
946 * only remove the source on the 1->0 transisition
951 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
953 fib_entry_src_action_deactivate(fib_entry, source);
956 old_path_list = esrc->fes_pl;
958 if (NULL != fib_entry_src_vft[source].fesv_remove)
960 fib_entry_src_vft[source].fesv_remove(esrc);
963 fib_path_list_unlock(old_path_list);
964 fib_entry_unlock(fib_entry_get_index(fib_entry));
966 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
967 fib_entry_src_action_deinit(fib_entry, source);
973 * fib_route_attached_cross_table
975 * Return true the the route is attached via an interface that
976 * is not in the same table as the route
979 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
980 const fib_route_path_t *rpath)
983 * - All zeros next-hop
984 * - a valid interface
985 * - entry's fib index not equeal to interface's index
987 if (ip46_address_is_zero(&rpath->frp_addr) &&
988 (~0 != rpath->frp_sw_if_index) &&
989 (fib_entry->fe_fib_index !=
990 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
991 rpath->frp_sw_if_index)))
999 * fib_route_attached_cross_table
1001 * Return true the the route is attached via an interface that
1002 * is not in the same table as the route
1005 fib_path_is_attached (const fib_route_path_t *rpath)
1008 * - All zeros next-hop
1009 * - a valid interface
1011 if (ip46_address_is_zero(&rpath->frp_addr) &&
1012 (~0 != rpath->frp_sw_if_index))
1016 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1023 fib_path_list_flags_t
1024 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1026 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1028 if (eflags & FIB_ENTRY_FLAG_DROP)
1030 plf |= FIB_PATH_LIST_FLAG_DROP;
1032 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1034 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1036 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1038 plf |= FIB_PATH_LIST_FLAG_LOCAL;
1045 fib_entry_flags_update (const fib_entry_t *fib_entry,
1046 const fib_route_path_t *rpath,
1047 fib_path_list_flags_t *pl_flags,
1048 fib_entry_src_t *esrc)
1050 if ((esrc->fes_src == FIB_SOURCE_API) ||
1051 (esrc->fes_src == FIB_SOURCE_CLI))
1053 if (fib_path_is_attached(rpath))
1055 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1059 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1062 if (fib_route_attached_cross_table(fib_entry, rpath))
1064 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1068 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1073 * fib_entry_src_action_add
1075 * Adding a source can result in a new fib_entry being created, which
1076 * can inturn mean the pool is realloc'd and thus the entry passed as
1077 * an argument it also realloc'd
1081 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1082 fib_source_t source,
1083 fib_entry_flag_t flags,
1084 const fib_route_path_t *rpath)
1086 fib_node_index_t old_path_list, fib_entry_index;
1087 fib_path_list_flags_t pl_flags;
1088 fib_entry_src_t *esrc;
1091 * save variable so we can recover from a fib_entry realloc.
1093 fib_entry_index = fib_entry_get_index(fib_entry);
1095 esrc = fib_entry_src_find(fib_entry, source, NULL);
1099 fib_entry_src_action_add(fib_entry,
1103 fib_entry_get_dpo_proto(fib_entry)));
1104 esrc = fib_entry_src_find(fib_entry, source, NULL);
1108 * we are no doubt modifying a path-list. If the path-list
1109 * is shared, and hence not modifiable, then the index returned
1110 * will be for a different path-list. This FIB entry to needs
1111 * to maintain its lock appropriately.
1113 old_path_list = esrc->fes_pl;
1115 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1117 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1118 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1120 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1121 fib_entry = fib_entry_get(fib_entry_index);
1123 fib_path_list_lock(esrc->fes_pl);
1124 fib_path_list_unlock(old_path_list);
1130 * fib_entry_src_action_swap
1132 * The source is providing new paths to replace the old ones.
1133 * Adding a source can result in a new fib_entry being created, which
1134 * can inturn mean the pool is realloc'd and thus the entry passed as
1135 * an argument it also realloc'd
1139 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1140 fib_source_t source,
1141 fib_entry_flag_t flags,
1142 const fib_route_path_t *rpaths)
1144 fib_node_index_t old_path_list, fib_entry_index;
1145 fib_path_list_flags_t pl_flags;
1146 const fib_route_path_t *rpath;
1147 fib_entry_src_t *esrc;
1149 esrc = fib_entry_src_find(fib_entry, source, NULL);
1152 * save variable so we can recover from a fib_entry realloc.
1154 fib_entry_index = fib_entry_get_index(fib_entry);
1158 fib_entry = fib_entry_src_action_add(fib_entry,
1162 fib_entry_get_dpo_proto(fib_entry)));
1163 esrc = fib_entry_src_find(fib_entry, source, NULL);
1167 * swapping paths may create a new path-list (or may use an existing shared)
1168 * but we are certainly getting a different one. This FIB entry to needs
1169 * to maintain its lock appropriately.
1171 old_path_list = esrc->fes_pl;
1173 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1175 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1177 vec_foreach(rpath, rpaths)
1179 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1182 fib_entry_src_vft[source].fesv_path_swap(esrc,
1187 fib_entry = fib_entry_get(fib_entry_index);
1189 fib_path_list_lock(esrc->fes_pl);
1190 fib_path_list_unlock(old_path_list);
1195 fib_entry_src_flag_t
1196 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1197 fib_source_t source,
1198 const fib_route_path_t *rpath)
1200 fib_path_list_flags_t pl_flags;
1201 fib_node_index_t old_path_list;
1202 fib_entry_src_t *esrc;
1204 esrc = fib_entry_src_find(fib_entry, source, NULL);
1206 ASSERT(NULL != esrc);
1207 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1210 * we no doubt modifying a path-list. If the path-list
1211 * is shared, and hence not modifiable, then the index returned
1212 * will be for a different path-list. This FIB entry to needs
1213 * to maintain its lock appropriately.
1215 old_path_list = esrc->fes_pl;
1217 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1219 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1220 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1222 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1225 * lock the new path-list, unlock the old if it had one
1227 fib_path_list_unlock(old_path_list);
1229 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1230 fib_path_list_lock(esrc->fes_pl);
1231 return (FIB_ENTRY_SRC_FLAG_ADDED);
1236 * no more paths left from this source
1238 fib_entry_src_action_remove(fib_entry, source);
1239 return (FIB_ENTRY_SRC_FLAG_NONE);
1244 fib_entry_src_format (fib_entry_t *fib_entry,
1245 fib_source_t source,
1248 fib_entry_src_t *esrc;
1250 esrc = fib_entry_src_find(fib_entry, source, NULL);
1252 if (NULL != fib_entry_src_vft[source].fesv_format)
1254 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1260 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1261 fib_source_t source)
1263 fib_entry_t *fib_entry;
1264 fib_entry_src_t *esrc;
1266 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1267 return (ADJ_INDEX_INVALID);
1269 fib_entry = fib_entry_get(fib_entry_index);
1270 esrc = fib_entry_src_find(fib_entry, source, NULL);
1274 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1276 return (fib_path_list_get_adj(
1278 fib_entry_get_default_chain_type(fib_entry)));
1281 return (ADJ_INDEX_INVALID);
1285 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1286 fib_source_t source,
1289 fib_entry_t *fib_entry;
1290 fib_entry_src_t *esrc;
1292 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1295 fib_entry = fib_entry_get(fib_entry_index);
1296 esrc = fib_entry_src_find(fib_entry, source, NULL);
1300 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1302 fib_path_list_contribute_forwarding(
1304 fib_entry_get_default_chain_type(fib_entry),
1307 return (dpo_id_is_valid(dpo));
1314 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1315 fib_source_t source)
1317 fib_entry_t *fib_entry;
1318 fib_entry_src_t *esrc;
1320 fib_entry = fib_entry_get(entry_index);
1322 esrc = fib_entry_src_find(fib_entry, source, NULL);
1326 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1328 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1335 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1336 fib_source_t source)
1338 fib_entry_t *fib_entry;
1339 fib_entry_src_t *esrc;
1341 fib_entry = fib_entry_get(entry_index);
1343 esrc = fib_entry_src_find(fib_entry, source, NULL);
1347 return (esrc->fes_entry_flags);
1350 return (FIB_ENTRY_FLAG_NONE);
1354 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1356 fib_entry_flag_t flags;
1359 * the vector of sources is deliberately arranged in priority order
1361 if (0 == vec_len(fib_entry->fe_srcs))
1363 flags = FIB_ENTRY_FLAG_NONE;
1367 fib_entry_src_t *esrc;
1369 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1370 flags = esrc->fes_entry_flags;
1377 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1378 fib_source_t source,
1381 fib_entry_t *fib_entry;
1382 fib_entry_src_t *esrc;
1384 fib_entry = fib_entry_get(fib_entry_index);
1385 esrc = fib_entry_src_find(fib_entry, source, NULL);
1388 NULL != fib_entry_src_vft[source].fesv_set_data)
1390 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1395 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1396 fib_source_t source)
1398 fib_entry_t *fib_entry;
1399 fib_entry_src_t *esrc;
1401 fib_entry = fib_entry_get(fib_entry_index);
1402 esrc = fib_entry_src_find(fib_entry, source, NULL);
1405 NULL != fib_entry_src_vft[source].fesv_get_data)
1407 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1413 fib_entry_src_module_init (void)
1415 fib_entry_src_rr_register();
1416 fib_entry_src_interface_register();
1417 fib_entry_src_default_route_register();
1418 fib_entry_src_special_register();
1419 fib_entry_src_api_register();
1420 fib_entry_src_adj_register();
1421 fib_entry_src_mpls_register();
1422 fib_entry_src_lisp_register();