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;
197 } fib_entry_src_collect_forwarding_ctx_t;
200 * @brief Determine whether this FIB entry should use a load-balance MAP
201 * to support PIC edge fast convergence
204 fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
207 * We'll use a LB map if the path-list has multiple recursive paths.
208 * recursive paths implies BGP, and hence scale.
210 if (ctx->n_recursive_constrained > 1 &&
211 fib_path_list_is_popular(ctx->esrc->fes_pl))
213 return (LOAD_BALANCE_FLAG_USES_MAP);
215 return (LOAD_BALANCE_FLAG_NONE);
219 fib_entry_src_valid_out_label (mpls_label_t label)
221 return ((MPLS_LABEL_IS_REAL(label) ||
222 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
223 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
224 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
228 * @brief Turn the chain type requested by the client into the one they
231 fib_forward_chain_type_t
232 fib_entry_chain_type_fixup (const fib_entry_t *entry,
233 fib_forward_chain_type_t fct)
236 * The EOS chain is a tricky since one cannot know the adjacency
237 * to link to without knowing what the packets payload protocol
238 * will be once the label is popped.
240 fib_forward_chain_type_t dfct;
242 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct)
247 dfct = fib_entry_get_default_chain_type(entry);
249 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS == dfct)
252 * If the entry being asked is a eos-MPLS label entry,
253 * then use the payload-protocol field, that we stashed there
254 * for just this purpose
256 return (fib_forw_chain_type_from_dpo_proto(
257 entry->fe_prefix.fp_payload_proto));
260 * else give them what this entry would be by default. i.e. if it's a v6
261 * entry, then the label its local labelled should be carrying v6 traffic.
262 * If it's a non-EOS label entry, then there are more labels and we want
269 fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
270 fib_entry_src_collect_forwarding_ctx_t *ctx)
272 load_balance_path_t *nh;
275 * no extension => no out-going label for this path. that's OK
276 * in the case of an IP or EOS chain, but not for non-EOS
280 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
281 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
282 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
283 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
285 * EOS traffic with no label to stack, we need the IP Adj
287 vec_add2(ctx->next_hops, nh, 1);
289 nh->path_index = path_index;
290 nh->path_weight = fib_path_get_weight(path_index);
291 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
294 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
295 if (fib_path_is_exclusive(path_index) ||
296 fib_path_is_deag(path_index))
298 vec_add2(ctx->next_hops, nh, 1);
300 nh->path_index = path_index;
301 nh->path_weight = fib_path_get_weight(path_index);
302 fib_path_contribute_forwarding(path_index,
303 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
307 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
310 * no label. we need a chain based on the payload. fixup.
312 vec_add2(ctx->next_hops, nh, 1);
314 nh->path_index = path_index;
315 nh->path_weight = fib_path_get_weight(path_index);
316 fib_path_contribute_forwarding(path_index,
317 fib_entry_chain_type_fixup(ctx->fib_entry,
320 fib_path_stack_mpls_disp(path_index,
321 ctx->fib_entry->fe_prefix.fp_payload_proto,
326 case FIB_FORW_CHAIN_TYPE_ETHERNET:
327 case FIB_FORW_CHAIN_TYPE_NSH:
333 static fib_path_list_walk_rc_t
334 fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
335 fib_node_index_t path_index,
338 fib_entry_src_collect_forwarding_ctx_t *ctx;
339 fib_path_ext_t *path_ext;
344 * if the path is not resolved, don't include it.
346 if (!fib_path_is_resolved(path_index))
348 return (FIB_PATH_LIST_WALK_CONTINUE);
351 if (fib_path_is_recursive_constrained(path_index))
353 ctx->n_recursive_constrained += 1;
355 if (0xffff == ctx->preference)
358 * not set a preference yet, so the first path we encounter
359 * sets the preference we are collecting.
361 ctx->preference = fib_path_get_preference(path_index);
363 else if (ctx->preference != fib_path_get_preference(path_index))
366 * this path does not belong to the same preference as the
367 * previous paths encountered. we are done now.
369 return (FIB_PATH_LIST_WALK_STOP);
373 * get the matching path-extension for the path being visited.
375 path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
378 if (NULL != path_ext)
380 switch (path_ext->fpe_type)
382 case FIB_PATH_EXT_MPLS:
383 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
386 * found a matching extension. stack it to obtain the forwarding
387 * info for this path.
390 fib_path_ext_stack(path_ext,
392 fib_entry_chain_type_fixup(ctx->fib_entry,
398 fib_entry_src_get_path_forwarding(path_index, ctx);
401 case FIB_PATH_EXT_ADJ:
402 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
404 fib_entry_src_get_path_forwarding(path_index, ctx);
408 * the path does not refine the cover, meaning that
409 * the adjacency doesdoes not match the sub-net on the link.
410 * So this path does not contribute forwarding.
417 fib_entry_src_get_path_forwarding(path_index, ctx);
420 return (FIB_PATH_LIST_WALK_CONTINUE);
424 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
425 const fib_entry_src_t *esrc,
426 fib_forward_chain_type_t fct,
429 dpo_proto_t lb_proto;
432 * If the entry has path extensions then we construct a load-balance
433 * by stacking the extensions on the forwarding chains of the paths.
434 * Otherwise we use the load-balance of the path-list
436 fib_entry_src_collect_forwarding_ctx_t ctx = {
438 .fib_entry = fib_entry,
440 .n_recursive_constrained = 0,
442 .preference = 0xffff,
446 * As an optimisation we allocate the vector of next-hops to be sized
447 * equal to the maximum nuber of paths we will need, which is also the
448 * most likely number we will need, since in most cases the paths are 'up'.
450 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
451 vec_reset_length(ctx.next_hops);
453 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
455 fib_path_list_walk(esrc->fes_pl,
456 fib_entry_src_collect_forwarding,
459 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
462 * the client provided the DPO that the entry should link to.
463 * all entries must link to a LB, so if it is an LB already
464 * then we can use it.
466 if ((1 == vec_len(ctx.next_hops)) &&
467 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
469 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
470 dpo_reset(&ctx.next_hops[0].path_dpo);
475 if (!dpo_id_is_valid(dpo_lb))
480 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
485 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
489 flow_hash_config_t fhc;
493 * if the protocol for the LB we are building does not match that
494 * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
495 * then the fib_index is not an index that relates to the table
496 * type we need. So get the default flow-hash config instead.
498 fp = dpo_proto_to_fib(lb_proto);
500 if (fib_entry->fe_prefix.fp_proto != fp)
502 fhc = fib_table_get_default_flow_hash_config(fp);
506 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index, fp);
511 load_balance_create(0, lb_proto, fhc));
515 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
520 replicate_multipath_update(dpo_lb, ctx.next_hops);
524 load_balance_multipath_update(dpo_lb,
526 fib_entry_calc_lb_flags(&ctx));
527 vec_free(ctx.next_hops);
530 * if this entry is sourced by the uRPF-exempt source then we
531 * append the always present local0 interface (index 0) to the
532 * uRPF list so it is not empty. that way packets pass the loose check.
534 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
536 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
537 FIB_SOURCE_URPF_EXEMPT) ||
538 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
539 (0 == fib_urpf_check_size(ui)))
542 * The uRPF list we get from the path-list is shared by all
543 * other users of the list, but the uRPF exemption applies
544 * only to this prefix. So we need our own list.
546 ui = fib_urpf_list_alloc_and_lock();
547 fib_urpf_list_append(ui, 0);
548 fib_urpf_list_bake(ui);
549 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
550 fib_urpf_list_unlock(ui);
554 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
556 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
557 fib_entry_get_flags_i(fib_entry));
562 fib_entry_src_action_install (fib_entry_t *fib_entry,
566 * Install the forwarding chain for the given source into the forwarding
569 fib_forward_chain_type_t fct;
570 fib_entry_src_t *esrc;
573 fct = fib_entry_get_default_chain_type(fib_entry);
574 esrc = fib_entry_src_find(fib_entry, source, NULL);
577 * Every entry has its own load-balance object. All changes to the entry's
578 * forwarding result in an inplace modify of the load-balance. This means
579 * the load-balance object only needs to be added to the forwarding
580 * DB once, when it is created.
582 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
584 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
586 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
587 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
590 * insert the adj into the data-plane forwarding trie
594 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
595 &fib_entry->fe_prefix,
600 * if any of the other chain types are already created they will need
603 fib_entry_delegate_type_t fdt;
604 fib_entry_delegate_t *fed;
606 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
608 fib_entry_src_mk_lb(fib_entry, esrc,
609 fib_entry_delegate_type_to_chain_type(fdt),
615 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
618 * uninstall the forwarding chain from the forwarding tables
620 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
621 fib_entry->fe_adj_index);
623 if (dpo_id_is_valid(&fib_entry->fe_lb))
625 fib_table_fwding_dpo_remove(
626 fib_entry->fe_fib_index,
627 &fib_entry->fe_prefix,
630 dpo_reset(&fib_entry->fe_lb);
635 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
637 fib_node_index_t *entries = NULL;
639 fib_path_list_recursive_loop_detect(path_list_index, &entries);
645 fib_entry_src_action_activate (fib_entry_t *fib_entry,
649 int houston_we_are_go_for_install;
650 fib_entry_src_t *esrc;
652 esrc = fib_entry_src_find(fib_entry, source, NULL);
654 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
655 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
657 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
659 if (NULL != fib_entry_src_vft[source].fesv_activate)
661 houston_we_are_go_for_install =
662 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
667 * the source is not providing an activate function, we'll assume
668 * therefore it has no objection to installing the entry
670 houston_we_are_go_for_install = !0;
674 * link to the path-list provided by the source, and go check
675 * if that forms any loops in the graph.
677 fib_entry->fe_parent = esrc->fes_pl;
678 fib_entry->fe_sibling =
679 fib_path_list_child_add(fib_entry->fe_parent,
681 fib_entry_get_index(fib_entry));
683 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
685 FIB_ENTRY_DBG(fib_entry, "activate: %d",
686 fib_entry->fe_parent);
688 if (0 != houston_we_are_go_for_install)
690 fib_entry_src_action_install(fib_entry, source);
694 fib_entry_src_action_uninstall(fib_entry);
699 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
703 fib_node_index_t path_list_index;
704 fib_entry_src_t *esrc;
706 esrc = fib_entry_src_find(fib_entry, source, NULL);
708 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
710 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
712 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
715 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
717 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
720 * un-link from an old path-list. Check for any loops this will clear
722 path_list_index = fib_entry->fe_parent;
723 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
725 fib_entry_recursive_loop_detect_i(path_list_index);
728 * this will unlock the path-list, so it may be invalid thereafter.
730 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
731 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
735 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
738 fib_entry_src_t *esrc;
740 vec_foreach(esrc, fib_entry->fe_srcs)
742 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
744 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
752 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
755 fib_node_index_t path_list_index;
756 fib_entry_src_t *esrc;
758 esrc = fib_entry_src_find(fib_entry, source, NULL);
760 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
762 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
763 fib_entry->fe_parent,
766 if (fib_entry->fe_parent != esrc->fes_pl)
769 * un-link from an old path-list. Check for any loops this will clear
771 path_list_index = fib_entry->fe_parent;
772 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
775 * temporary lock so it doesn't get deleted when this entry is no
778 fib_path_list_lock(path_list_index);
781 * this entry is no longer a child. after unlinking check if any loops
784 fib_path_list_child_remove(path_list_index,
785 fib_entry->fe_sibling);
787 fib_entry_recursive_loop_detect_i(path_list_index);
790 * link to the path-list provided by the source, and go check
791 * if that forms any loops in the graph.
793 fib_entry->fe_parent = esrc->fes_pl;
794 fib_entry->fe_sibling =
795 fib_path_list_child_add(fib_entry->fe_parent,
797 fib_entry_get_index(fib_entry));
799 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
800 fib_path_list_unlock(path_list_index);
802 fib_entry_src_action_install(fib_entry, source);
803 fib_entry_src_action_fwd_update(fib_entry, source);
807 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
810 fib_entry_src_t *esrc;
812 esrc = fib_entry_src_find(fib_entry, source, NULL);
814 if (NULL != fib_entry_src_vft[source].fesv_installed)
816 fib_entry_src_vft[source].fesv_installed(esrc,
820 fib_entry_src_action_fwd_update(fib_entry, source);
824 * fib_entry_src_action_add
826 * Adding a source can result in a new fib_entry being created, which
827 * can inturn mean the pool is realloc'd and thus the entry passed as
828 * an argument it also realloc'd
829 * @return the original entry
832 fib_entry_src_action_add (fib_entry_t *fib_entry,
834 fib_entry_flag_t flags,
837 fib_node_index_t fib_entry_index;
838 fib_entry_src_t *esrc;
840 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
842 esrc->fes_ref_count++;
844 if (1 != esrc->fes_ref_count)
847 * we only want to add the source on the 0->1 transition
852 esrc->fes_entry_flags = flags;
855 * save variable so we can recover from a fib_entry realloc.
857 fib_entry_index = fib_entry_get_index(fib_entry);
859 if (NULL != fib_entry_src_vft[source].fesv_add)
861 fib_entry_src_vft[source].fesv_add(esrc,
864 fib_entry_get_proto(fib_entry),
868 fib_entry = fib_entry_get(fib_entry_index);
870 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
872 fib_path_list_lock(esrc->fes_pl);
875 * the source owns a lock on the entry
877 fib_entry_lock(fib_entry_get_index(fib_entry));
883 * fib_entry_src_action_update
885 * Adding a source can result in a new fib_entry being created, which
886 * can inturn mean the pool is realloc'd and thus the entry passed as
887 * an argument it also realloc'd
888 * @return the original entry
891 fib_entry_src_action_update (fib_entry_t *fib_entry,
893 fib_entry_flag_t flags,
896 fib_node_index_t fib_entry_index, old_path_list_index;
897 fib_entry_src_t *esrc;
899 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
902 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
904 old_path_list_index = esrc->fes_pl;
905 esrc->fes_entry_flags = flags;
908 * save variable so we can recover from a fib_entry realloc.
910 fib_entry_index = fib_entry_get_index(fib_entry);
912 if (NULL != fib_entry_src_vft[source].fesv_add)
914 fib_entry_src_vft[source].fesv_add(esrc,
917 fib_entry_get_proto(fib_entry),
921 fib_entry = fib_entry_get(fib_entry_index);
923 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
925 fib_path_list_lock(esrc->fes_pl);
926 fib_path_list_unlock(old_path_list_index);
933 fib_entry_src_action_remove (fib_entry_t *fib_entry,
937 fib_node_index_t old_path_list;
938 fib_entry_src_flag_t sflags;
939 fib_entry_src_t *esrc;
941 esrc = fib_entry_src_find(fib_entry, source, NULL);
944 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
946 esrc->fes_ref_count--;
947 sflags = esrc->fes_flags;
949 if (0 != esrc->fes_ref_count)
952 * only remove the source on the 1->0 transisition
957 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
959 fib_entry_src_action_deactivate(fib_entry, source);
962 old_path_list = esrc->fes_pl;
964 if (NULL != fib_entry_src_vft[source].fesv_remove)
966 fib_entry_src_vft[source].fesv_remove(esrc);
969 fib_path_list_unlock(old_path_list);
970 fib_entry_unlock(fib_entry_get_index(fib_entry));
972 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
973 fib_entry_src_action_deinit(fib_entry, source);
979 * fib_route_attached_cross_table
981 * Return true the the route is attached via an interface that
982 * is not in the same table as the route
985 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
986 const fib_route_path_t *rpath)
989 * - All zeros next-hop
990 * - a valid interface
991 * - entry's fib index not equeal to interface's index
993 if (ip46_address_is_zero(&rpath->frp_addr) &&
994 (~0 != rpath->frp_sw_if_index) &&
995 (fib_entry->fe_fib_index !=
996 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
997 rpath->frp_sw_if_index)))
1005 * fib_route_attached_cross_table
1007 * Return true the the route is attached via an interface that
1008 * is not in the same table as the route
1011 fib_path_is_attached (const fib_route_path_t *rpath)
1014 * - All zeros next-hop
1015 * - a valid interface
1017 if (ip46_address_is_zero(&rpath->frp_addr) &&
1018 (~0 != rpath->frp_sw_if_index))
1022 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1029 fib_path_list_flags_t
1030 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1032 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1034 if (eflags & FIB_ENTRY_FLAG_DROP)
1036 plf |= FIB_PATH_LIST_FLAG_DROP;
1038 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1040 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1042 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1044 plf |= FIB_PATH_LIST_FLAG_LOCAL;
1051 fib_entry_flags_update (const fib_entry_t *fib_entry,
1052 const fib_route_path_t *rpath,
1053 fib_path_list_flags_t *pl_flags,
1054 fib_entry_src_t *esrc)
1056 if ((esrc->fes_src == FIB_SOURCE_API) ||
1057 (esrc->fes_src == FIB_SOURCE_CLI))
1059 if (fib_path_is_attached(rpath))
1061 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1065 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1068 if (fib_route_attached_cross_table(fib_entry, rpath))
1070 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1074 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1079 * fib_entry_src_action_add
1081 * Adding a source can result in a new fib_entry being created, which
1082 * can inturn mean the pool is realloc'd and thus the entry passed as
1083 * an argument it also realloc'd
1087 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1088 fib_source_t source,
1089 fib_entry_flag_t flags,
1090 const fib_route_path_t *rpath)
1092 fib_node_index_t old_path_list, fib_entry_index;
1093 fib_path_list_flags_t pl_flags;
1094 fib_entry_src_t *esrc;
1097 * save variable so we can recover from a fib_entry realloc.
1099 fib_entry_index = fib_entry_get_index(fib_entry);
1101 esrc = fib_entry_src_find(fib_entry, source, NULL);
1105 fib_entry_src_action_add(fib_entry,
1110 fib_entry_get_proto(fib_entry))));
1111 esrc = fib_entry_src_find(fib_entry, source, NULL);
1115 * we are no doubt modifying a path-list. If the path-list
1116 * is shared, and hence not modifiable, then the index returned
1117 * will be for a different path-list. This FIB entry to needs
1118 * to maintain its lock appropriately.
1120 old_path_list = esrc->fes_pl;
1122 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1124 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1125 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1127 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1128 fib_entry = fib_entry_get(fib_entry_index);
1130 fib_path_list_lock(esrc->fes_pl);
1131 fib_path_list_unlock(old_path_list);
1137 * fib_entry_src_action_swap
1139 * The source is providing new paths to replace the old ones.
1140 * Adding a source can result in a new fib_entry being created, which
1141 * can inturn mean the pool is realloc'd and thus the entry passed as
1142 * an argument it also realloc'd
1146 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1147 fib_source_t source,
1148 fib_entry_flag_t flags,
1149 const fib_route_path_t *rpaths)
1151 fib_node_index_t old_path_list, fib_entry_index;
1152 fib_path_list_flags_t pl_flags;
1153 const fib_route_path_t *rpath;
1154 fib_entry_src_t *esrc;
1156 esrc = fib_entry_src_find(fib_entry, source, NULL);
1159 * save variable so we can recover from a fib_entry realloc.
1161 fib_entry_index = fib_entry_get_index(fib_entry);
1165 fib_entry = fib_entry_src_action_add(fib_entry,
1170 fib_entry_get_proto(fib_entry))));
1171 esrc = fib_entry_src_find(fib_entry, source, NULL);
1175 * swapping paths may create a new path-list (or may use an existing shared)
1176 * but we are certainly getting a different one. This FIB entry to needs
1177 * to maintain its lock appropriately.
1179 old_path_list = esrc->fes_pl;
1181 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1183 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1185 vec_foreach(rpath, rpaths)
1187 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1190 fib_entry_src_vft[source].fesv_path_swap(esrc,
1195 fib_entry = fib_entry_get(fib_entry_index);
1197 fib_path_list_lock(esrc->fes_pl);
1198 fib_path_list_unlock(old_path_list);
1203 fib_entry_src_flag_t
1204 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1205 fib_source_t source,
1206 const fib_route_path_t *rpath)
1208 fib_path_list_flags_t pl_flags;
1209 fib_node_index_t old_path_list;
1210 fib_entry_src_t *esrc;
1212 esrc = fib_entry_src_find(fib_entry, source, NULL);
1214 ASSERT(NULL != esrc);
1215 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1218 * we no doubt modifying a path-list. If the path-list
1219 * is shared, and hence not modifiable, then the index returned
1220 * will be for a different path-list. This FIB entry to needs
1221 * to maintain its lock appropriately.
1223 old_path_list = esrc->fes_pl;
1225 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1227 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1228 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1230 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1233 * lock the new path-list, unlock the old if it had one
1235 fib_path_list_unlock(old_path_list);
1237 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1238 fib_path_list_lock(esrc->fes_pl);
1239 return (FIB_ENTRY_SRC_FLAG_ADDED);
1244 * no more paths left from this source
1246 fib_entry_src_action_remove(fib_entry, source);
1247 return (FIB_ENTRY_SRC_FLAG_NONE);
1252 fib_entry_src_format (fib_entry_t *fib_entry,
1253 fib_source_t source,
1256 fib_entry_src_t *esrc;
1258 esrc = fib_entry_src_find(fib_entry, source, NULL);
1260 if (NULL != fib_entry_src_vft[source].fesv_format)
1262 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1268 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1269 fib_source_t source)
1271 fib_entry_t *fib_entry;
1272 fib_entry_src_t *esrc;
1274 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1275 return (ADJ_INDEX_INVALID);
1277 fib_entry = fib_entry_get(fib_entry_index);
1278 esrc = fib_entry_src_find(fib_entry, source, NULL);
1282 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1284 return (fib_path_list_get_adj(
1286 fib_entry_get_default_chain_type(fib_entry)));
1289 return (ADJ_INDEX_INVALID);
1293 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1294 fib_source_t source,
1297 fib_entry_t *fib_entry;
1298 fib_entry_src_t *esrc;
1300 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1303 fib_entry = fib_entry_get(fib_entry_index);
1304 esrc = fib_entry_src_find(fib_entry, source, NULL);
1308 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1310 fib_path_list_contribute_forwarding(
1312 fib_entry_get_default_chain_type(fib_entry),
1315 return (dpo_id_is_valid(dpo));
1322 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1323 fib_source_t source)
1325 fib_entry_t *fib_entry;
1326 fib_entry_src_t *esrc;
1328 fib_entry = fib_entry_get(entry_index);
1330 esrc = fib_entry_src_find(fib_entry, source, NULL);
1334 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1336 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1343 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1344 fib_source_t source)
1346 fib_entry_t *fib_entry;
1347 fib_entry_src_t *esrc;
1349 fib_entry = fib_entry_get(entry_index);
1351 esrc = fib_entry_src_find(fib_entry, source, NULL);
1355 return (esrc->fes_entry_flags);
1358 return (FIB_ENTRY_FLAG_NONE);
1362 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1364 fib_entry_flag_t flags;
1367 * the vector of sources is deliberately arranged in priority order
1369 if (0 == vec_len(fib_entry->fe_srcs))
1371 flags = FIB_ENTRY_FLAG_NONE;
1375 fib_entry_src_t *esrc;
1377 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1378 flags = esrc->fes_entry_flags;
1385 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1386 fib_source_t source,
1389 fib_entry_t *fib_entry;
1390 fib_entry_src_t *esrc;
1392 fib_entry = fib_entry_get(fib_entry_index);
1393 esrc = fib_entry_src_find(fib_entry, source, NULL);
1396 NULL != fib_entry_src_vft[source].fesv_set_data)
1398 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1403 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1404 fib_source_t source)
1406 fib_entry_t *fib_entry;
1407 fib_entry_src_t *esrc;
1409 fib_entry = fib_entry_get(fib_entry_index);
1410 esrc = fib_entry_src_find(fib_entry, source, NULL);
1413 NULL != fib_entry_src_vft[source].fesv_get_data)
1415 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1421 fib_entry_src_module_init (void)
1423 fib_entry_src_rr_register();
1424 fib_entry_src_interface_register();
1425 fib_entry_src_default_route_register();
1426 fib_entry_src_special_register();
1427 fib_entry_src_api_register();
1428 fib_entry_src_adj_register();
1429 fib_entry_src_mpls_register();
1430 fib_entry_src_lisp_register();