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:
278 case FIB_FORW_CHAIN_TYPE_BIER:
280 * EOS traffic with no label to stack, we need the IP Adj
282 vec_add2(ctx->next_hops, nh, 1);
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);
289 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
290 if (fib_path_is_exclusive(path_index) ||
291 fib_path_is_deag(path_index))
293 vec_add2(ctx->next_hops, nh, 1);
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,
302 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
305 * no label. we need a chain based on the payload. fixup.
307 vec_add2(ctx->next_hops, nh, 1);
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,
315 fib_path_stack_mpls_disp(path_index,
316 ctx->fib_entry->fe_prefix.fp_payload_proto,
321 case FIB_FORW_CHAIN_TYPE_ETHERNET:
322 case FIB_FORW_CHAIN_TYPE_NSH:
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,
333 fib_entry_src_collect_forwarding_ctx_t *ctx;
334 fib_path_ext_t *path_ext;
339 * if the path is not resolved, don't include it.
341 if (!fib_path_is_resolved(path_index))
343 return (FIB_PATH_LIST_WALK_CONTINUE);
346 if (fib_path_is_recursive_constrained(path_index))
348 ctx->n_recursive_constrained += 1;
350 if (0xffff == ctx->preference)
353 * not set a preference yet, so the first path we encounter
354 * sets the preference we are collecting.
356 ctx->preference = fib_path_get_preference(path_index);
358 else if (ctx->preference != fib_path_get_preference(path_index))
361 * this path does not belong to the same preference as the
362 * previous paths encountered. we are done now.
364 return (FIB_PATH_LIST_WALK_STOP);
368 * get the matching path-extension for the path being visited.
370 path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
373 if (NULL != path_ext)
375 switch (path_ext->fpe_type)
377 case FIB_PATH_EXT_MPLS:
378 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
381 * found a matching extension. stack it to obtain the forwarding
382 * info for this path.
385 fib_path_ext_stack(path_ext,
387 fib_entry_chain_type_fixup(ctx->fib_entry,
393 fib_entry_src_get_path_forwarding(path_index, ctx);
396 case FIB_PATH_EXT_ADJ:
397 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
399 fib_entry_src_get_path_forwarding(path_index, ctx);
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.
412 fib_entry_src_get_path_forwarding(path_index, ctx);
415 return (FIB_PATH_LIST_WALK_CONTINUE);
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,
424 dpo_proto_t lb_proto;
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
431 fib_entry_src_collect_forwarding_ctx_t ctx = {
433 .fib_entry = fib_entry,
435 .n_recursive_constrained = 0,
437 .preference = 0xffff,
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'.
445 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
446 vec_reset_length(ctx.next_hops);
448 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
450 fib_path_list_walk(esrc->fes_pl,
451 fib_entry_src_collect_forwarding,
454 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
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.
461 if ((1 == vec_len(ctx.next_hops)) &&
462 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
464 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
465 dpo_reset(&ctx.next_hops[0].path_dpo);
470 if (!dpo_id_is_valid(dpo_lb))
475 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
480 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
484 fib_protocol_t flow_hash_proto;
485 flow_hash_config_t fhc;
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.
493 flow_hash_proto = dpo_proto_to_fib(lb_proto);
494 if (fib_entry->fe_prefix.fp_proto != flow_hash_proto)
496 fhc = fib_table_get_default_flow_hash_config(flow_hash_proto);
500 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
507 load_balance_create(0, lb_proto, fhc));
511 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
516 replicate_multipath_update(dpo_lb, ctx.next_hops);
520 load_balance_multipath_update(dpo_lb,
522 fib_entry_calc_lb_flags(&ctx));
523 vec_free(ctx.next_hops);
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.
530 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
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)))
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.
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);
550 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
552 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
553 fib_entry_get_flags_i(fib_entry));
558 fib_entry_src_action_install (fib_entry_t *fib_entry,
562 * Install the forwarding chain for the given source into the forwarding
565 fib_forward_chain_type_t fct;
566 fib_entry_src_t *esrc;
569 fct = fib_entry_get_default_chain_type(fib_entry);
570 esrc = fib_entry_src_find(fib_entry, source, NULL);
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.
578 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
580 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
582 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
583 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
586 * insert the adj into the data-plane forwarding trie
590 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
591 &fib_entry->fe_prefix,
596 * if any of the other chain types are already created they will need
599 fib_entry_delegate_type_t fdt;
600 fib_entry_delegate_t *fed;
602 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
604 fib_entry_src_mk_lb(fib_entry, esrc,
605 fib_entry_delegate_type_to_chain_type(fdt),
611 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
614 * uninstall the forwarding chain from the forwarding tables
616 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
617 fib_entry->fe_adj_index);
619 if (dpo_id_is_valid(&fib_entry->fe_lb))
621 fib_table_fwding_dpo_remove(
622 fib_entry->fe_fib_index,
623 &fib_entry->fe_prefix,
626 dpo_reset(&fib_entry->fe_lb);
631 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
633 fib_node_index_t *entries = NULL;
635 fib_path_list_recursive_loop_detect(path_list_index, &entries);
641 fib_entry_src_action_activate (fib_entry_t *fib_entry,
645 int houston_we_are_go_for_install;
646 fib_entry_src_t *esrc;
648 esrc = fib_entry_src_find(fib_entry, source, NULL);
650 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
651 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
653 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
655 if (NULL != fib_entry_src_vft[source].fesv_activate)
657 houston_we_are_go_for_install =
658 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
663 * the source is not providing an activate function, we'll assume
664 * therefore it has no objection to installing the entry
666 houston_we_are_go_for_install = !0;
670 * link to the path-list provided by the source, and go check
671 * if that forms any loops in the graph.
673 fib_entry->fe_parent = esrc->fes_pl;
674 fib_entry->fe_sibling =
675 fib_path_list_child_add(fib_entry->fe_parent,
677 fib_entry_get_index(fib_entry));
679 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
681 FIB_ENTRY_DBG(fib_entry, "activate: %d",
682 fib_entry->fe_parent);
684 if (0 != houston_we_are_go_for_install)
686 fib_entry_src_action_install(fib_entry, source);
690 fib_entry_src_action_uninstall(fib_entry);
695 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
699 fib_node_index_t path_list_index;
700 fib_entry_src_t *esrc;
702 esrc = fib_entry_src_find(fib_entry, source, NULL);
704 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
706 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
708 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
711 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
713 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
716 * un-link from an old path-list. Check for any loops this will clear
718 path_list_index = fib_entry->fe_parent;
719 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
721 fib_entry_recursive_loop_detect_i(path_list_index);
724 * this will unlock the path-list, so it may be invalid thereafter.
726 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
727 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
731 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
734 fib_entry_src_t *esrc;
736 vec_foreach(esrc, fib_entry->fe_srcs)
738 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
740 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
748 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
751 fib_node_index_t path_list_index;
752 fib_entry_src_t *esrc;
754 esrc = fib_entry_src_find(fib_entry, source, NULL);
756 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
758 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
759 fib_entry->fe_parent,
762 if (fib_entry->fe_parent != esrc->fes_pl)
765 * un-link from an old path-list. Check for any loops this will clear
767 path_list_index = fib_entry->fe_parent;
768 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
771 * temporary lock so it doesn't get deleted when this entry is no
774 fib_path_list_lock(path_list_index);
777 * this entry is no longer a child. after unlinking check if any loops
780 fib_path_list_child_remove(path_list_index,
781 fib_entry->fe_sibling);
783 fib_entry_recursive_loop_detect_i(path_list_index);
786 * link to the path-list provided by the source, and go check
787 * if that forms any loops in the graph.
789 fib_entry->fe_parent = esrc->fes_pl;
790 fib_entry->fe_sibling =
791 fib_path_list_child_add(fib_entry->fe_parent,
793 fib_entry_get_index(fib_entry));
795 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
796 fib_path_list_unlock(path_list_index);
798 fib_entry_src_action_install(fib_entry, source);
799 fib_entry_src_action_fwd_update(fib_entry, source);
803 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
806 fib_entry_src_t *esrc;
808 esrc = fib_entry_src_find(fib_entry, source, NULL);
810 if (NULL != fib_entry_src_vft[source].fesv_installed)
812 fib_entry_src_vft[source].fesv_installed(esrc,
816 fib_entry_src_action_fwd_update(fib_entry, source);
820 * fib_entry_src_action_add
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
828 fib_entry_src_action_add (fib_entry_t *fib_entry,
830 fib_entry_flag_t flags,
833 fib_node_index_t fib_entry_index;
834 fib_entry_src_t *esrc;
836 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
838 esrc->fes_ref_count++;
840 if (1 != esrc->fes_ref_count)
843 * we only want to add the source on the 0->1 transition
848 esrc->fes_entry_flags = flags;
851 * save variable so we can recover from a fib_entry realloc.
853 fib_entry_index = fib_entry_get_index(fib_entry);
855 if (NULL != fib_entry_src_vft[source].fesv_add)
857 fib_entry_src_vft[source].fesv_add(esrc,
860 fib_entry_get_dpo_proto(fib_entry),
864 fib_entry = fib_entry_get(fib_entry_index);
866 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
868 fib_path_list_lock(esrc->fes_pl);
871 * the source owns a lock on the entry
873 fib_entry_lock(fib_entry_get_index(fib_entry));
879 * fib_entry_src_action_update
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
887 fib_entry_src_action_update (fib_entry_t *fib_entry,
889 fib_entry_flag_t flags,
892 fib_node_index_t fib_entry_index, old_path_list_index;
893 fib_entry_src_t *esrc;
895 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
898 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
900 old_path_list_index = esrc->fes_pl;
901 esrc->fes_entry_flags = flags;
904 * save variable so we can recover from a fib_entry realloc.
906 fib_entry_index = fib_entry_get_index(fib_entry);
908 if (NULL != fib_entry_src_vft[source].fesv_add)
910 fib_entry_src_vft[source].fesv_add(esrc,
913 fib_entry_get_dpo_proto(fib_entry),
917 fib_entry = fib_entry_get(fib_entry_index);
919 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
921 fib_path_list_lock(esrc->fes_pl);
922 fib_path_list_unlock(old_path_list_index);
929 fib_entry_src_action_remove (fib_entry_t *fib_entry,
933 fib_node_index_t old_path_list;
934 fib_entry_src_flag_t sflags;
935 fib_entry_src_t *esrc;
937 esrc = fib_entry_src_find(fib_entry, source, NULL);
940 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
942 esrc->fes_ref_count--;
943 sflags = esrc->fes_flags;
945 if (0 != esrc->fes_ref_count)
948 * only remove the source on the 1->0 transisition
953 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
955 fib_entry_src_action_deactivate(fib_entry, source);
958 old_path_list = esrc->fes_pl;
960 if (NULL != fib_entry_src_vft[source].fesv_remove)
962 fib_entry_src_vft[source].fesv_remove(esrc);
965 fib_path_list_unlock(old_path_list);
966 fib_entry_unlock(fib_entry_get_index(fib_entry));
968 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
969 fib_entry_src_action_deinit(fib_entry, source);
975 * fib_route_attached_cross_table
977 * Return true the the route is attached via an interface that
978 * is not in the same table as the route
981 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
982 const fib_route_path_t *rpath)
985 * - All zeros next-hop
986 * - a valid interface
987 * - entry's fib index not equeal to interface's index
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)))
1001 * fib_route_attached_cross_table
1003 * Return true the the route is attached via an interface that
1004 * is not in the same table as the route
1007 fib_path_is_attached (const fib_route_path_t *rpath)
1010 * - All zeros next-hop
1011 * - a valid interface
1013 if (ip46_address_is_zero(&rpath->frp_addr) &&
1014 (~0 != rpath->frp_sw_if_index))
1018 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1025 fib_path_list_flags_t
1026 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1028 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1030 if (eflags & FIB_ENTRY_FLAG_DROP)
1032 plf |= FIB_PATH_LIST_FLAG_DROP;
1034 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1036 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1038 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1040 plf |= FIB_PATH_LIST_FLAG_LOCAL;
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)
1052 if ((esrc->fes_src == FIB_SOURCE_API) ||
1053 (esrc->fes_src == FIB_SOURCE_CLI))
1055 if (fib_path_is_attached(rpath))
1057 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1061 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1064 if (fib_route_attached_cross_table(fib_entry, rpath))
1066 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1070 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1075 * fib_entry_src_action_add
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
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)
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;
1093 * save variable so we can recover from a fib_entry realloc.
1095 fib_entry_index = fib_entry_get_index(fib_entry);
1097 esrc = fib_entry_src_find(fib_entry, source, NULL);
1101 fib_entry_src_action_add(fib_entry,
1105 fib_entry_get_dpo_proto(fib_entry)));
1106 esrc = fib_entry_src_find(fib_entry, source, NULL);
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.
1115 old_path_list = esrc->fes_pl;
1117 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
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);
1122 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1123 fib_entry = fib_entry_get(fib_entry_index);
1125 fib_path_list_lock(esrc->fes_pl);
1126 fib_path_list_unlock(old_path_list);
1132 * fib_entry_src_action_swap
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
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)
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;
1151 esrc = fib_entry_src_find(fib_entry, source, NULL);
1154 * save variable so we can recover from a fib_entry realloc.
1156 fib_entry_index = fib_entry_get_index(fib_entry);
1160 fib_entry = fib_entry_src_action_add(fib_entry,
1164 fib_entry_get_dpo_proto(fib_entry)));
1165 esrc = fib_entry_src_find(fib_entry, source, NULL);
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.
1173 old_path_list = esrc->fes_pl;
1175 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1177 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1179 vec_foreach(rpath, rpaths)
1181 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1184 fib_entry_src_vft[source].fesv_path_swap(esrc,
1189 fib_entry = fib_entry_get(fib_entry_index);
1191 fib_path_list_lock(esrc->fes_pl);
1192 fib_path_list_unlock(old_path_list);
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)
1202 fib_path_list_flags_t pl_flags;
1203 fib_node_index_t old_path_list;
1204 fib_entry_src_t *esrc;
1206 esrc = fib_entry_src_find(fib_entry, source, NULL);
1208 ASSERT(NULL != esrc);
1209 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
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.
1217 old_path_list = esrc->fes_pl;
1219 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
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);
1224 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1227 * lock the new path-list, unlock the old if it had one
1229 fib_path_list_unlock(old_path_list);
1231 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1232 fib_path_list_lock(esrc->fes_pl);
1233 return (FIB_ENTRY_SRC_FLAG_ADDED);
1238 * no more paths left from this source
1240 fib_entry_src_action_remove(fib_entry, source);
1241 return (FIB_ENTRY_SRC_FLAG_NONE);
1246 fib_entry_src_format (fib_entry_t *fib_entry,
1247 fib_source_t source,
1250 fib_entry_src_t *esrc;
1252 esrc = fib_entry_src_find(fib_entry, source, NULL);
1254 if (NULL != fib_entry_src_vft[source].fesv_format)
1256 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1262 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1263 fib_source_t source)
1265 fib_entry_t *fib_entry;
1266 fib_entry_src_t *esrc;
1268 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1269 return (ADJ_INDEX_INVALID);
1271 fib_entry = fib_entry_get(fib_entry_index);
1272 esrc = fib_entry_src_find(fib_entry, source, NULL);
1276 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1278 return (fib_path_list_get_adj(
1280 fib_entry_get_default_chain_type(fib_entry)));
1283 return (ADJ_INDEX_INVALID);
1287 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1288 fib_source_t source,
1291 fib_entry_t *fib_entry;
1292 fib_entry_src_t *esrc;
1294 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1297 fib_entry = fib_entry_get(fib_entry_index);
1298 esrc = fib_entry_src_find(fib_entry, source, NULL);
1302 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1304 fib_path_list_contribute_forwarding(
1306 fib_entry_get_default_chain_type(fib_entry),
1309 return (dpo_id_is_valid(dpo));
1316 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1317 fib_source_t source)
1319 fib_entry_t *fib_entry;
1320 fib_entry_src_t *esrc;
1322 fib_entry = fib_entry_get(entry_index);
1324 esrc = fib_entry_src_find(fib_entry, source, NULL);
1328 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1330 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1337 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1338 fib_source_t source)
1340 fib_entry_t *fib_entry;
1341 fib_entry_src_t *esrc;
1343 fib_entry = fib_entry_get(entry_index);
1345 esrc = fib_entry_src_find(fib_entry, source, NULL);
1349 return (esrc->fes_entry_flags);
1352 return (FIB_ENTRY_FLAG_NONE);
1356 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1358 fib_entry_flag_t flags;
1361 * the vector of sources is deliberately arranged in priority order
1363 if (0 == vec_len(fib_entry->fe_srcs))
1365 flags = FIB_ENTRY_FLAG_NONE;
1369 fib_entry_src_t *esrc;
1371 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1372 flags = esrc->fes_entry_flags;
1379 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1380 fib_source_t source,
1383 fib_entry_t *fib_entry;
1384 fib_entry_src_t *esrc;
1386 fib_entry = fib_entry_get(fib_entry_index);
1387 esrc = fib_entry_src_find(fib_entry, source, NULL);
1390 NULL != fib_entry_src_vft[source].fesv_set_data)
1392 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1397 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1398 fib_source_t source)
1400 fib_entry_t *fib_entry;
1401 fib_entry_src_t *esrc;
1403 fib_entry = fib_entry_get(fib_entry_index);
1404 esrc = fib_entry_src_find(fib_entry, source, NULL);
1407 NULL != fib_entry_src_vft[source].fesv_get_data)
1409 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1415 fib_entry_src_module_init (void)
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();