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_get_proto (const fib_entry_t * fib_entry)
35 return (fib_entry->fe_prefix.fp_proto);
39 fib_entry_src_register (fib_source_t source,
40 const fib_entry_src_vft_t *vft)
42 fib_entry_src_vft[source] = *vft;
46 fib_entry_src_cmp_for_sort (void * v1,
49 fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
51 return (esrc1->fes_src - esrc2->fes_src);
55 fib_entry_src_action_init (fib_entry_t *fib_entry,
59 fib_entry_src_t esrc = {
60 .fes_pl = FIB_NODE_INDEX_INVALID,
61 .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
65 if (NULL != fib_entry_src_vft[source].fesv_init)
67 fib_entry_src_vft[source].fesv_init(&esrc);
70 vec_add1(fib_entry->fe_srcs, esrc);
71 vec_sort_with_function(fib_entry->fe_srcs,
72 fib_entry_src_cmp_for_sort);
75 static fib_entry_src_t *
76 fib_entry_src_find (const fib_entry_t *fib_entry,
81 fib_entry_src_t *esrc;
85 vec_foreach(esrc, fib_entry->fe_srcs)
87 if (esrc->fes_src == source)
105 fib_entry_is_sourced (fib_node_index_t fib_entry_index,
108 fib_entry_t *fib_entry;
110 fib_entry = fib_entry_get(fib_entry_index);
112 return (NULL != fib_entry_src_find(fib_entry, source, NULL));
115 static fib_entry_src_t *
116 fib_entry_src_find_or_create (fib_entry_t *fib_entry,
120 fib_entry_src_t *esrc;
122 esrc = fib_entry_src_find(fib_entry, source, NULL);
126 fib_entry_src_action_init(fib_entry, source);
129 return (fib_entry_src_find(fib_entry, source, NULL));
133 fib_entry_src_action_deinit (fib_entry_t *fib_entry,
137 fib_entry_src_t *esrc;
140 esrc = fib_entry_src_find(fib_entry, source, &index);
142 ASSERT(NULL != esrc);
144 if (NULL != fib_entry_src_vft[source].fesv_deinit)
146 fib_entry_src_vft[source].fesv_deinit(esrc);
149 fib_path_ext_list_flush(&esrc->fes_path_exts);
150 vec_del1(fib_entry->fe_srcs, index);
153 fib_entry_src_cover_res_t
154 fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
157 if (NULL != fib_entry_src_vft[source].fesv_cover_change)
159 return (fib_entry_src_vft[source].fesv_cover_change(
160 fib_entry_src_find(fib_entry, source, NULL),
164 fib_entry_src_cover_res_t res = {
166 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
171 fib_entry_src_cover_res_t
172 fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
175 if (NULL != fib_entry_src_vft[source].fesv_cover_update)
177 return (fib_entry_src_vft[source].fesv_cover_update(
178 fib_entry_src_find(fib_entry, source, NULL),
182 fib_entry_src_cover_res_t res = {
184 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
189 typedef struct fib_entry_src_collect_forwarding_ctx_t_
191 load_balance_path_t *next_hops;
192 const fib_entry_t *fib_entry;
193 const fib_entry_src_t *esrc;
194 fib_forward_chain_type_t fct;
195 int n_recursive_constrained;
196 } fib_entry_src_collect_forwarding_ctx_t;
199 * @brief Determine whether this FIB entry should use a load-balance MAP
200 * to support PIC edge fast convergence
203 fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
206 * We'll use a LB map if the path-list has multiple recursive paths.
207 * recursive paths implies BGP, and hence scale.
209 if (ctx->n_recursive_constrained > 1 &&
210 fib_path_list_is_popular(ctx->esrc->fes_pl))
212 return (LOAD_BALANCE_FLAG_USES_MAP);
214 return (LOAD_BALANCE_FLAG_NONE);
218 fib_entry_src_valid_out_label (mpls_label_t label)
220 return ((MPLS_LABEL_IS_REAL(label) ||
221 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
222 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
223 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
227 * @brief Turn the chain type requested by the client into the one they
230 fib_forward_chain_type_t
231 fib_entry_chain_type_fixup (const fib_entry_t *entry,
232 fib_forward_chain_type_t fct)
235 * The EOS chain is a tricky since one cannot know the adjacency
236 * to link to without knowing what the packets payload protocol
237 * will be once the label is popped.
239 fib_forward_chain_type_t dfct;
241 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct)
246 dfct = fib_entry_get_default_chain_type(entry);
248 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS == dfct)
251 * If the entry being asked is a eos-MPLS label entry,
252 * then use the payload-protocol field, that we stashed there
253 * for just this purpose
255 return (fib_forw_chain_type_from_dpo_proto(
256 entry->fe_prefix.fp_payload_proto));
259 * else give them what this entry would be by default. i.e. if it's a v6
260 * entry, then the label its local labelled should be carrying v6 traffic.
261 * If it's a non-EOS label entry, then there are more labels and we want
268 fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
269 fib_entry_src_collect_forwarding_ctx_t *ctx)
271 load_balance_path_t *nh;
274 * no extension => no out-going label for this path. that's OK
275 * in the case of an IP or EOS chain, but not for non-EOS
279 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
280 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
281 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
282 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
284 * EOS traffic with no label to stack, we need the IP Adj
286 vec_add2(ctx->next_hops, nh, 1);
288 nh->path_index = path_index;
289 nh->path_weight = fib_path_get_weight(path_index);
290 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
293 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
294 if (fib_path_is_exclusive(path_index) ||
295 fib_path_is_deag(path_index))
297 vec_add2(ctx->next_hops, nh, 1);
299 nh->path_index = path_index;
300 nh->path_weight = fib_path_get_weight(path_index);
301 fib_path_contribute_forwarding(path_index,
302 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
306 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
309 * no label. we need a chain based on the payload. fixup.
311 vec_add2(ctx->next_hops, nh, 1);
313 nh->path_index = path_index;
314 nh->path_weight = fib_path_get_weight(path_index);
315 fib_path_contribute_forwarding(path_index,
316 fib_entry_chain_type_fixup(ctx->fib_entry,
319 fib_path_stack_mpls_disp(path_index,
320 ctx->fib_entry->fe_prefix.fp_payload_proto,
325 case FIB_FORW_CHAIN_TYPE_ETHERNET:
326 case FIB_FORW_CHAIN_TYPE_NSH:
332 static fib_path_list_walk_rc_t
333 fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
334 fib_node_index_t path_index,
337 fib_entry_src_collect_forwarding_ctx_t *ctx;
338 fib_path_ext_t *path_ext;
343 * if the path is not resolved, don't include it.
345 if (!fib_path_is_resolved(path_index))
347 return (FIB_PATH_LIST_WALK_CONTINUE);
350 if (fib_path_is_recursive_constrained(path_index))
352 ctx->n_recursive_constrained += 1;
356 * get the matching path-extension for the path being visited.
358 path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
361 if (NULL != path_ext)
363 switch (path_ext->fpe_type)
365 case FIB_PATH_EXT_MPLS:
366 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
369 * found a matching extension. stack it to obtain the forwarding
370 * info for this path.
373 fib_path_ext_stack(path_ext,
375 fib_entry_chain_type_fixup(ctx->fib_entry,
381 fib_entry_src_get_path_forwarding(path_index, ctx);
384 case FIB_PATH_EXT_ADJ:
385 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
387 fib_entry_src_get_path_forwarding(path_index, ctx);
391 * the path does not refine the cover, meaning that
392 * the adjacency doesdoes not match the sub-net on the link.
393 * So this path does not contribute forwarding.
400 fib_entry_src_get_path_forwarding(path_index, ctx);
403 return (FIB_PATH_LIST_WALK_CONTINUE);
407 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
408 const fib_entry_src_t *esrc,
409 fib_forward_chain_type_t fct,
412 dpo_proto_t lb_proto;
415 * If the entry has path extensions then we construct a load-balance
416 * by stacking the extensions on the forwarding chains of the paths.
417 * Otherwise we use the load-balance of the path-list
419 fib_entry_src_collect_forwarding_ctx_t ctx = {
421 .fib_entry = fib_entry,
423 .n_recursive_constrained = 0,
428 * As an optimisation we allocate the vector of next-hops to be sized
429 * equal to the maximum nuber of paths we will need, which is also the
430 * most likely number we will need, since in most cases the paths are 'up'.
432 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
433 vec_reset_length(ctx.next_hops);
435 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
437 fib_path_list_walk(esrc->fes_pl,
438 fib_entry_src_collect_forwarding,
441 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
444 * the client provided the DPO that the entry should link to.
445 * all entries must link to a LB, so if it is an LB already
446 * then we can use it.
448 if ((1 == vec_len(ctx.next_hops)) &&
449 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
451 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
452 dpo_reset(&ctx.next_hops[0].path_dpo);
457 if (!dpo_id_is_valid(dpo_lb))
462 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
467 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
471 flow_hash_config_t fhc;
475 * if the protocol for the LB we are building does not match that
476 * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
477 * then the fib_index is not an index that relates to the table
478 * type we need. So get the default flow-hash config instead.
480 fp = dpo_proto_to_fib(lb_proto);
482 if (fib_entry->fe_prefix.fp_proto != fp)
484 fhc = fib_table_get_default_flow_hash_config(fp);
488 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index, fp);
493 load_balance_create(0, lb_proto, fhc));
497 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
502 replicate_multipath_update(dpo_lb, ctx.next_hops);
506 load_balance_multipath_update(dpo_lb,
508 fib_entry_calc_lb_flags(&ctx));
509 vec_free(ctx.next_hops);
512 * if this entry is sourced by the uRPF-exempt source then we
513 * append the always present local0 interface (index 0) to the
514 * uRPF list so it is not empty. that way packets pass the loose check.
516 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
518 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
519 FIB_SOURCE_URPF_EXEMPT) ||
520 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
521 (0 == fib_urpf_check_size(ui)))
524 * The uRPF list we get from the path-list is shared by all
525 * other users of the list, but the uRPF exemption applies
526 * only to this prefix. So we need our own list.
528 ui = fib_urpf_list_alloc_and_lock();
529 fib_urpf_list_append(ui, 0);
530 fib_urpf_list_bake(ui);
531 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
532 fib_urpf_list_unlock(ui);
536 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
538 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
539 fib_entry_get_flags_i(fib_entry));
544 fib_entry_src_action_install (fib_entry_t *fib_entry,
548 * Install the forwarding chain for the given source into the forwarding
551 fib_forward_chain_type_t fct;
552 fib_entry_src_t *esrc;
555 fct = fib_entry_get_default_chain_type(fib_entry);
556 esrc = fib_entry_src_find(fib_entry, source, NULL);
559 * Every entry has its own load-balance object. All changes to the entry's
560 * forwarding result in an inplace modify of the load-balance. This means
561 * the load-balance object only needs to be added to the forwarding
562 * DB once, when it is created.
564 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
566 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
568 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
569 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
572 * insert the adj into the data-plane forwarding trie
576 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
577 &fib_entry->fe_prefix,
582 * if any of the other chain types are already created they will need
585 fib_entry_delegate_type_t fdt;
586 fib_entry_delegate_t *fed;
588 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
590 fib_entry_src_mk_lb(fib_entry, esrc,
591 fib_entry_delegate_type_to_chain_type(fdt),
597 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
600 * uninstall the forwarding chain from the forwarding tables
602 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
603 fib_entry->fe_adj_index);
605 if (dpo_id_is_valid(&fib_entry->fe_lb))
607 fib_table_fwding_dpo_remove(
608 fib_entry->fe_fib_index,
609 &fib_entry->fe_prefix,
612 dpo_reset(&fib_entry->fe_lb);
617 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
619 fib_node_index_t *entries = NULL;
621 fib_path_list_recursive_loop_detect(path_list_index, &entries);
627 fib_entry_src_action_activate (fib_entry_t *fib_entry,
631 int houston_we_are_go_for_install;
632 fib_entry_src_t *esrc;
634 esrc = fib_entry_src_find(fib_entry, source, NULL);
636 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
637 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
639 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
641 if (NULL != fib_entry_src_vft[source].fesv_activate)
643 houston_we_are_go_for_install =
644 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
649 * the source is not providing an activate function, we'll assume
650 * therefore it has no objection to installing the entry
652 houston_we_are_go_for_install = !0;
656 * link to the path-list provided by the source, and go check
657 * if that forms any loops in the graph.
659 fib_entry->fe_parent = esrc->fes_pl;
660 fib_entry->fe_sibling =
661 fib_path_list_child_add(fib_entry->fe_parent,
663 fib_entry_get_index(fib_entry));
665 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
667 FIB_ENTRY_DBG(fib_entry, "activate: %d",
668 fib_entry->fe_parent);
670 if (0 != houston_we_are_go_for_install)
672 fib_entry_src_action_install(fib_entry, source);
676 fib_entry_src_action_uninstall(fib_entry);
681 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
685 fib_node_index_t path_list_index;
686 fib_entry_src_t *esrc;
688 esrc = fib_entry_src_find(fib_entry, source, NULL);
690 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
692 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
694 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
697 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
699 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
702 * un-link from an old path-list. Check for any loops this will clear
704 path_list_index = fib_entry->fe_parent;
705 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
707 fib_entry_recursive_loop_detect_i(path_list_index);
710 * this will unlock the path-list, so it may be invalid thereafter.
712 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
713 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
717 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
720 fib_entry_src_t *esrc;
722 vec_foreach(esrc, fib_entry->fe_srcs)
724 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
726 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
734 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
737 fib_node_index_t path_list_index;
738 fib_entry_src_t *esrc;
740 esrc = fib_entry_src_find(fib_entry, source, NULL);
742 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
744 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
745 fib_entry->fe_parent,
748 if (fib_entry->fe_parent != esrc->fes_pl)
751 * un-link from an old path-list. Check for any loops this will clear
753 path_list_index = fib_entry->fe_parent;
754 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
757 * temporary lock so it doesn't get deleted when this entry is no
760 fib_path_list_lock(path_list_index);
763 * this entry is no longer a child. after unlinking check if any loops
766 fib_path_list_child_remove(path_list_index,
767 fib_entry->fe_sibling);
769 fib_entry_recursive_loop_detect_i(path_list_index);
772 * link to the path-list provided by the source, and go check
773 * if that forms any loops in the graph.
775 fib_entry->fe_parent = esrc->fes_pl;
776 fib_entry->fe_sibling =
777 fib_path_list_child_add(fib_entry->fe_parent,
779 fib_entry_get_index(fib_entry));
781 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
782 fib_path_list_unlock(path_list_index);
784 fib_entry_src_action_install(fib_entry, source);
785 fib_entry_src_action_fwd_update(fib_entry, source);
789 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
792 fib_entry_src_t *esrc;
794 esrc = fib_entry_src_find(fib_entry, source, NULL);
796 if (NULL != fib_entry_src_vft[source].fesv_installed)
798 fib_entry_src_vft[source].fesv_installed(esrc,
802 fib_entry_src_action_fwd_update(fib_entry, source);
806 * fib_entry_src_action_add
808 * Adding a source can result in a new fib_entry being created, which
809 * can inturn mean the pool is realloc'd and thus the entry passed as
810 * an argument it also realloc'd
811 * @return the original entry
814 fib_entry_src_action_add (fib_entry_t *fib_entry,
816 fib_entry_flag_t flags,
819 fib_node_index_t fib_entry_index;
820 fib_entry_src_t *esrc;
822 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
824 esrc->fes_ref_count++;
826 if (1 != esrc->fes_ref_count)
829 * we only want to add the source on the 0->1 transition
834 esrc->fes_entry_flags = flags;
837 * save variable so we can recover from a fib_entry realloc.
839 fib_entry_index = fib_entry_get_index(fib_entry);
841 if (NULL != fib_entry_src_vft[source].fesv_add)
843 fib_entry_src_vft[source].fesv_add(esrc,
846 fib_entry_get_proto(fib_entry),
850 fib_entry = fib_entry_get(fib_entry_index);
852 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
854 fib_path_list_lock(esrc->fes_pl);
857 * the source owns a lock on the entry
859 fib_entry_lock(fib_entry_get_index(fib_entry));
865 * fib_entry_src_action_update
867 * Adding a source can result in a new fib_entry being created, which
868 * can inturn mean the pool is realloc'd and thus the entry passed as
869 * an argument it also realloc'd
870 * @return the original entry
873 fib_entry_src_action_update (fib_entry_t *fib_entry,
875 fib_entry_flag_t flags,
878 fib_node_index_t fib_entry_index, old_path_list_index;
879 fib_entry_src_t *esrc;
881 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
884 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
886 old_path_list_index = esrc->fes_pl;
887 esrc->fes_entry_flags = flags;
890 * save variable so we can recover from a fib_entry realloc.
892 fib_entry_index = fib_entry_get_index(fib_entry);
894 if (NULL != fib_entry_src_vft[source].fesv_add)
896 fib_entry_src_vft[source].fesv_add(esrc,
899 fib_entry_get_proto(fib_entry),
903 fib_entry = fib_entry_get(fib_entry_index);
905 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
907 fib_path_list_lock(esrc->fes_pl);
908 fib_path_list_unlock(old_path_list_index);
915 fib_entry_src_action_remove (fib_entry_t *fib_entry,
919 fib_node_index_t old_path_list;
920 fib_entry_src_flag_t sflags;
921 fib_entry_src_t *esrc;
923 esrc = fib_entry_src_find(fib_entry, source, NULL);
926 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
928 esrc->fes_ref_count--;
929 sflags = esrc->fes_flags;
931 if (0 != esrc->fes_ref_count)
934 * only remove the source on the 1->0 transisition
939 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
941 fib_entry_src_action_deactivate(fib_entry, source);
944 old_path_list = esrc->fes_pl;
946 if (NULL != fib_entry_src_vft[source].fesv_remove)
948 fib_entry_src_vft[source].fesv_remove(esrc);
951 fib_path_list_unlock(old_path_list);
952 fib_entry_unlock(fib_entry_get_index(fib_entry));
954 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
955 fib_entry_src_action_deinit(fib_entry, source);
961 * fib_route_attached_cross_table
963 * Return true the the route is attached via an interface that
964 * is not in the same table as the route
967 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
968 const fib_route_path_t *rpath)
971 * - All zeros next-hop
972 * - a valid interface
973 * - entry's fib index not equeal to interface's index
975 if (ip46_address_is_zero(&rpath->frp_addr) &&
976 (~0 != rpath->frp_sw_if_index) &&
977 (fib_entry->fe_fib_index !=
978 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
979 rpath->frp_sw_if_index)))
987 * fib_route_attached_cross_table
989 * Return true the the route is attached via an interface that
990 * is not in the same table as the route
993 fib_path_is_attached (const fib_route_path_t *rpath)
996 * - All zeros next-hop
997 * - a valid interface
999 if (ip46_address_is_zero(&rpath->frp_addr) &&
1000 (~0 != rpath->frp_sw_if_index))
1004 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1011 fib_path_list_flags_t
1012 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1014 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1016 if (eflags & FIB_ENTRY_FLAG_DROP)
1018 plf |= FIB_PATH_LIST_FLAG_DROP;
1020 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1022 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1024 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1026 plf |= FIB_PATH_LIST_FLAG_LOCAL;
1033 fib_entry_flags_update (const fib_entry_t *fib_entry,
1034 const fib_route_path_t *rpath,
1035 fib_path_list_flags_t *pl_flags,
1036 fib_entry_src_t *esrc)
1038 if ((esrc->fes_src == FIB_SOURCE_API) ||
1039 (esrc->fes_src == FIB_SOURCE_CLI))
1041 if (fib_path_is_attached(rpath))
1043 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1047 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1050 if (fib_route_attached_cross_table(fib_entry, rpath))
1052 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1056 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1061 * fib_entry_src_action_add
1063 * Adding a source can result in a new fib_entry being created, which
1064 * can inturn mean the pool is realloc'd and thus the entry passed as
1065 * an argument it also realloc'd
1069 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1070 fib_source_t source,
1071 fib_entry_flag_t flags,
1072 const fib_route_path_t *rpath)
1074 fib_node_index_t old_path_list, fib_entry_index;
1075 fib_path_list_flags_t pl_flags;
1076 fib_entry_src_t *esrc;
1079 * save variable so we can recover from a fib_entry realloc.
1081 fib_entry_index = fib_entry_get_index(fib_entry);
1083 esrc = fib_entry_src_find(fib_entry, source, NULL);
1087 fib_entry_src_action_add(fib_entry,
1092 fib_entry_get_proto(fib_entry))));
1093 esrc = fib_entry_src_find(fib_entry, source, NULL);
1097 * we are no doubt modifying a path-list. If the path-list
1098 * is shared, and hence not modifiable, then the index returned
1099 * will be for a different path-list. This FIB entry to needs
1100 * to maintain its lock appropriately.
1102 old_path_list = esrc->fes_pl;
1104 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1106 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1107 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1109 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1110 fib_entry = fib_entry_get(fib_entry_index);
1112 fib_path_list_lock(esrc->fes_pl);
1113 fib_path_list_unlock(old_path_list);
1119 * fib_entry_src_action_swap
1121 * The source is providing new paths to replace the old ones.
1122 * Adding a source can result in a new fib_entry being created, which
1123 * can inturn mean the pool is realloc'd and thus the entry passed as
1124 * an argument it also realloc'd
1128 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1129 fib_source_t source,
1130 fib_entry_flag_t flags,
1131 const fib_route_path_t *rpaths)
1133 fib_node_index_t old_path_list, fib_entry_index;
1134 fib_path_list_flags_t pl_flags;
1135 const fib_route_path_t *rpath;
1136 fib_entry_src_t *esrc;
1138 esrc = fib_entry_src_find(fib_entry, source, NULL);
1141 * save variable so we can recover from a fib_entry realloc.
1143 fib_entry_index = fib_entry_get_index(fib_entry);
1147 fib_entry = fib_entry_src_action_add(fib_entry,
1152 fib_entry_get_proto(fib_entry))));
1153 esrc = fib_entry_src_find(fib_entry, source, NULL);
1157 * swapping paths may create a new path-list (or may use an existing shared)
1158 * but we are certainly getting a different one. This FIB entry to needs
1159 * to maintain its lock appropriately.
1161 old_path_list = esrc->fes_pl;
1163 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1165 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1167 vec_foreach(rpath, rpaths)
1169 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1172 fib_entry_src_vft[source].fesv_path_swap(esrc,
1177 fib_entry = fib_entry_get(fib_entry_index);
1179 fib_path_list_lock(esrc->fes_pl);
1180 fib_path_list_unlock(old_path_list);
1185 fib_entry_src_flag_t
1186 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1187 fib_source_t source,
1188 const fib_route_path_t *rpath)
1190 fib_path_list_flags_t pl_flags;
1191 fib_node_index_t old_path_list;
1192 fib_entry_src_t *esrc;
1194 esrc = fib_entry_src_find(fib_entry, source, NULL);
1196 ASSERT(NULL != esrc);
1197 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1200 * we no doubt modifying a path-list. If the path-list
1201 * is shared, and hence not modifiable, then the index returned
1202 * will be for a different path-list. This FIB entry to needs
1203 * to maintain its lock appropriately.
1205 old_path_list = esrc->fes_pl;
1207 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1209 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1210 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1212 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1215 * lock the new path-list, unlock the old if it had one
1217 fib_path_list_unlock(old_path_list);
1219 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1220 fib_path_list_lock(esrc->fes_pl);
1221 return (FIB_ENTRY_SRC_FLAG_ADDED);
1226 * no more paths left from this source
1228 fib_entry_src_action_remove(fib_entry, source);
1229 return (FIB_ENTRY_SRC_FLAG_NONE);
1234 fib_entry_src_format (fib_entry_t *fib_entry,
1235 fib_source_t source,
1238 fib_entry_src_t *esrc;
1240 esrc = fib_entry_src_find(fib_entry, source, NULL);
1242 if (NULL != fib_entry_src_vft[source].fesv_format)
1244 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1250 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1251 fib_source_t source)
1253 fib_entry_t *fib_entry;
1254 fib_entry_src_t *esrc;
1256 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1257 return (ADJ_INDEX_INVALID);
1259 fib_entry = fib_entry_get(fib_entry_index);
1260 esrc = fib_entry_src_find(fib_entry, source, NULL);
1264 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1266 return (fib_path_list_get_adj(
1268 fib_entry_get_default_chain_type(fib_entry)));
1271 return (ADJ_INDEX_INVALID);
1275 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1276 fib_source_t source,
1279 fib_entry_t *fib_entry;
1280 fib_entry_src_t *esrc;
1282 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1285 fib_entry = fib_entry_get(fib_entry_index);
1286 esrc = fib_entry_src_find(fib_entry, source, NULL);
1290 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1292 fib_path_list_contribute_forwarding(
1294 fib_entry_get_default_chain_type(fib_entry),
1297 return (dpo_id_is_valid(dpo));
1304 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1305 fib_source_t source)
1307 fib_entry_t *fib_entry;
1308 fib_entry_src_t *esrc;
1310 fib_entry = fib_entry_get(entry_index);
1312 esrc = fib_entry_src_find(fib_entry, source, NULL);
1316 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1318 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1325 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1326 fib_source_t source)
1328 fib_entry_t *fib_entry;
1329 fib_entry_src_t *esrc;
1331 fib_entry = fib_entry_get(entry_index);
1333 esrc = fib_entry_src_find(fib_entry, source, NULL);
1337 return (esrc->fes_entry_flags);
1340 return (FIB_ENTRY_FLAG_NONE);
1344 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1346 fib_entry_flag_t flags;
1349 * the vector of sources is deliberately arranged in priority order
1351 if (0 == vec_len(fib_entry->fe_srcs))
1353 flags = FIB_ENTRY_FLAG_NONE;
1357 fib_entry_src_t *esrc;
1359 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1360 flags = esrc->fes_entry_flags;
1367 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1368 fib_source_t source,
1371 fib_entry_t *fib_entry;
1372 fib_entry_src_t *esrc;
1374 fib_entry = fib_entry_get(fib_entry_index);
1375 esrc = fib_entry_src_find(fib_entry, source, NULL);
1378 NULL != fib_entry_src_vft[source].fesv_set_data)
1380 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1385 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1386 fib_source_t source)
1388 fib_entry_t *fib_entry;
1389 fib_entry_src_t *esrc;
1391 fib_entry = fib_entry_get(fib_entry_index);
1392 esrc = fib_entry_src_find(fib_entry, source, NULL);
1395 NULL != fib_entry_src_vft[source].fesv_get_data)
1397 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1403 fib_entry_src_module_init (void)
1405 fib_entry_src_rr_register();
1406 fib_entry_src_interface_register();
1407 fib_entry_src_default_route_register();
1408 fib_entry_src_special_register();
1409 fib_entry_src_api_register();
1410 fib_entry_src_adj_register();
1411 fib_entry_src_mpls_register();
1412 fib_entry_src_lisp_register();