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>
21 #include <vnet/fib/fib_entry_src.h>
22 #include <vnet/fib/fib_table.h>
23 #include <vnet/fib/fib_path_ext.h>
24 #include <vnet/fib/fib_urpf_list.h>
29 static fib_entry_src_vft_t fib_entry_src_vft[FIB_SOURCE_MAX];
32 fib_entry_get_proto (const fib_entry_t * fib_entry)
34 return (fib_entry->fe_prefix.fp_proto);
38 fib_entry_src_register (fib_source_t source,
39 const fib_entry_src_vft_t *vft)
41 fib_entry_src_vft[source] = *vft;
45 fib_entry_src_cmp_for_sort (void * v1,
48 fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
50 return (esrc1->fes_src - esrc2->fes_src);
54 fib_entry_src_action_init (fib_entry_t *fib_entry,
58 fib_entry_src_t esrc = {
59 .fes_pl = FIB_NODE_INDEX_INVALID,
60 .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
64 if (NULL != fib_entry_src_vft[source].fesv_init)
66 fib_entry_src_vft[source].fesv_init(&esrc);
69 vec_add1(fib_entry->fe_srcs, esrc);
70 vec_sort_with_function(fib_entry->fe_srcs,
71 fib_entry_src_cmp_for_sort);
74 static fib_entry_src_t *
75 fib_entry_src_find (const fib_entry_t *fib_entry,
80 fib_entry_src_t *esrc;
84 vec_foreach(esrc, fib_entry->fe_srcs)
86 if (esrc->fes_src == source)
104 fib_entry_is_sourced (fib_node_index_t fib_entry_index,
107 fib_entry_t *fib_entry;
109 fib_entry = fib_entry_get(fib_entry_index);
111 return (NULL != fib_entry_src_find(fib_entry, source, NULL));
114 static fib_entry_src_t *
115 fib_entry_src_find_or_create (fib_entry_t *fib_entry,
119 fib_entry_src_t *esrc;
121 esrc = fib_entry_src_find(fib_entry, source, NULL);
125 fib_entry_src_action_init(fib_entry, source);
128 return (fib_entry_src_find(fib_entry, source, NULL));
132 fib_entry_src_action_deinit (fib_entry_t *fib_entry,
136 fib_entry_src_t *esrc;
139 esrc = fib_entry_src_find(fib_entry, source, &index);
141 ASSERT(NULL != esrc);
143 if (NULL != fib_entry_src_vft[source].fesv_deinit)
145 fib_entry_src_vft[source].fesv_deinit(esrc);
148 vec_free(esrc->fes_path_exts);
149 vec_del1(fib_entry->fe_srcs, index);
152 fib_entry_src_cover_res_t
153 fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
156 if (NULL != fib_entry_src_vft[source].fesv_cover_change)
158 return (fib_entry_src_vft[source].fesv_cover_change(
159 fib_entry_src_find(fib_entry, source, NULL),
163 fib_entry_src_cover_res_t res = {
165 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
170 fib_entry_src_cover_res_t
171 fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
174 if (NULL != fib_entry_src_vft[source].fesv_cover_update)
176 return (fib_entry_src_vft[source].fesv_cover_update(
177 fib_entry_src_find(fib_entry, source, NULL),
181 fib_entry_src_cover_res_t res = {
183 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
188 typedef struct fib_entry_src_collect_forwarding_ctx_t_
190 load_balance_path_t * next_hops;
191 const fib_entry_t *fib_entry;
192 const fib_entry_src_t *esrc;
193 fib_forward_chain_type_t fct;
195 } fib_entry_src_collect_forwarding_ctx_t;
198 * @brief Determine whether this FIB entry should use a load-balance MAP
199 * to support PIC edge fast convergence
202 fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
205 * We'll use a LB map is the path-list has recursive paths.
206 * recursive paths implies BGP, and hence scale.
208 if (ctx->is_recursive)
210 return (LOAD_BALANCE_FLAG_USES_MAP);
212 return (LOAD_BALANCE_FLAG_NONE);
216 fib_entry_src_valid_out_label (mpls_label_t label)
218 return ((MPLS_LABEL_IS_REAL(label) ||
219 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
220 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
221 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
225 * @brief Turn the chain type requested by the client into the one they
228 fib_forward_chain_type_t
229 fib_entry_chain_type_fixup (const fib_entry_t *entry,
230 fib_forward_chain_type_t fct)
232 ASSERT(FIB_FORW_CHAIN_TYPE_MPLS_EOS == 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 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_collect_forwarding (fib_node_index_t pl_index,
264 fib_node_index_t path_index,
267 fib_entry_src_collect_forwarding_ctx_t *ctx;
268 fib_path_ext_t *path_ext;
273 * if the path is not resolved, don't include it.
275 if (!fib_path_is_resolved(path_index))
280 if (fib_path_is_recursive(path_index))
282 ctx->is_recursive = 1;
286 * get the matching path-extension for the path being visited.
288 vec_foreach(path_ext, ctx->esrc->fes_path_exts)
290 if (path_ext->fpe_path_index == path_index)
294 if (NULL != path_ext &&
295 path_ext->fpe_path_index == path_index &&
296 fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
299 * found a matching extension. stack it to obtain the forwarding
300 * info for this path.
302 ctx->next_hops = fib_path_ext_stack(path_ext, ctx->fib_entry, ctx->fct, ctx->next_hops);
306 load_balance_path_t *nh;
309 * no extension => no out-going label for this path. that's OK
310 * in the case of an IP or EOS chain, but not for non-EOS
314 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
315 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
317 * EOS traffic with no label to stack, we need the IP Adj
319 vec_add2(ctx->next_hops, nh, 1);
321 nh->path_index = path_index;
322 nh->path_weight = fib_path_get_weight(path_index);
323 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
326 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
327 if (fib_path_is_exclusive(path_index) ||
328 fib_path_is_deag(path_index))
330 vec_add2(ctx->next_hops, nh, 1);
332 nh->path_index = path_index;
333 nh->path_weight = fib_path_get_weight(path_index);
334 fib_path_contribute_forwarding(path_index,
335 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
339 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
342 * no label. we need a chain based on the payload. fixup.
344 vec_add2(ctx->next_hops, nh, 1);
346 nh->path_index = path_index;
347 nh->path_weight = fib_path_get_weight(path_index);
348 fib_path_contribute_forwarding(path_index,
349 fib_entry_chain_type_fixup(ctx->fib_entry,
355 case FIB_FORW_CHAIN_TYPE_ETHERNET:
365 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
366 const fib_entry_src_t *esrc,
367 fib_forward_chain_type_t fct,
370 dpo_proto_t lb_proto;
373 * If the entry has path extensions then we construct a load-balance
374 * by stacking the extensions on the forwarding chains of the paths.
375 * Otherwise we use the load-balance of the path-list
377 fib_entry_src_collect_forwarding_ctx_t ctx = {
379 .fib_entry = fib_entry,
385 lb_proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto);
387 fib_path_list_walk(esrc->fes_pl,
388 fib_entry_src_collect_forwarding,
391 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
394 * the client provided the DPO that the entry should link to.
395 * all entries must link to a LB, so if it is an LB already
396 * then we can use it.
398 if ((1 == vec_len(ctx.next_hops)) &&
399 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
401 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
402 dpo_reset(&ctx.next_hops[0].path_dpo);
407 if (!dpo_id_is_valid(dpo_lb))
412 flow_hash_config_t fhc;
414 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
415 dpo_proto_to_fib(lb_proto));
419 load_balance_create(0, lb_proto, fhc));
422 load_balance_multipath_update(dpo_lb,
424 fib_entry_calc_lb_flags(&ctx));
425 vec_free(ctx.next_hops);
428 * if this entry is sourced by the uRPF-exempt source then we
429 * append the always present local0 interface (index 0) to the
430 * uRPF list so it is not empty. that way packets pass the loose check.
432 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
434 if (fib_entry_is_sourced(fib_entry_get_index(fib_entry),
435 FIB_SOURCE_URPF_EXEMPT) &&
436 (0 == fib_urpf_check_size(ui)))
439 * The uRPF list we get from the path-list is shared by all
440 * other users of the list, but the uRPF exemption applies
441 * only to this prefix. So we need our own list.
443 ui = fib_urpf_list_alloc_and_lock();
444 fib_urpf_list_append(ui, 0);
445 fib_urpf_list_bake(ui);
446 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
447 fib_urpf_list_unlock(ui);
451 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
456 fib_entry_src_action_install (fib_entry_t *fib_entry,
460 * Install the forwarding chain for the given source into the forwarding
463 fib_forward_chain_type_t fct;
464 fib_entry_src_t *esrc;
467 fct = fib_entry_get_default_chain_type(fib_entry);
468 esrc = fib_entry_src_find(fib_entry, source, NULL);
471 * Every entry has its own load-balance object. All changes to the entry's
472 * forwarding result in an inplace modify of the load-balance. This means
473 * the load-balance object only needs to be added to the forwarding
474 * DB once, when it is created.
476 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
478 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
480 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
481 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
484 * insert the adj into the data-plane forwarding trie
488 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
489 &fib_entry->fe_prefix,
494 * if any of the other chain types are already created they will need
497 fib_entry_delegate_type_t fdt;
498 fib_entry_delegate_t *fed;
500 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
502 fib_entry_src_mk_lb(fib_entry, esrc,
503 fib_entry_delegate_type_to_chain_type(fdt),
509 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
512 * uninstall the forwarding chain from the forwarding tables
514 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
515 fib_entry->fe_adj_index);
517 if (dpo_id_is_valid(&fib_entry->fe_lb))
519 fib_table_fwding_dpo_remove(
520 fib_entry->fe_fib_index,
521 &fib_entry->fe_prefix,
524 dpo_reset(&fib_entry->fe_lb);
529 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
531 fib_node_index_t *entries = NULL;
533 fib_path_list_recursive_loop_detect(path_list_index, &entries);
539 fib_entry_src_action_activate (fib_entry_t *fib_entry,
543 int houston_we_are_go_for_install;
544 fib_entry_src_t *esrc;
546 esrc = fib_entry_src_find(fib_entry, source, NULL);
548 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
549 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
551 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
553 if (NULL != fib_entry_src_vft[source].fesv_activate)
555 houston_we_are_go_for_install =
556 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
561 * the source is not providing an activate function, we'll assume
562 * therefore it has no objection to installing the entry
564 houston_we_are_go_for_install = !0;
568 * link to the path-list provided by the source, and go check
569 * if that forms any loops in the graph.
571 fib_entry->fe_parent = esrc->fes_pl;
572 fib_entry->fe_sibling =
573 fib_path_list_child_add(fib_entry->fe_parent,
575 fib_entry_get_index(fib_entry));
577 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
579 FIB_ENTRY_DBG(fib_entry, "activate: %d",
580 fib_entry->fe_parent);
582 if (0 != houston_we_are_go_for_install)
584 fib_entry_src_action_install(fib_entry, source);
588 fib_entry_src_action_uninstall(fib_entry);
593 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
597 fib_node_index_t path_list_index;
598 fib_entry_src_t *esrc;
600 esrc = fib_entry_src_find(fib_entry, source, NULL);
602 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
604 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
606 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
609 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
611 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
614 * un-link from an old path-list. Check for any loops this will clear
616 path_list_index = fib_entry->fe_parent;
617 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
619 fib_entry_recursive_loop_detect_i(path_list_index);
622 * this will unlock the path-list, so it may be invalid thereafter.
624 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
625 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
629 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
632 fib_entry_src_t *esrc;
634 vec_foreach(esrc, fib_entry->fe_srcs)
636 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
638 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
646 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
649 fib_node_index_t path_list_index;
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);
656 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
657 fib_entry->fe_parent,
660 if (fib_entry->fe_parent != esrc->fes_pl)
663 * un-link from an old path-list. Check for any loops this will clear
665 path_list_index = fib_entry->fe_parent;
666 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
669 * temporary lock so it doesn't get deleted when this entry is no
672 fib_path_list_lock(path_list_index);
675 * this entry is no longer a child. after unlinking check if any loops
678 fib_path_list_child_remove(path_list_index,
679 fib_entry->fe_sibling);
681 fib_entry_recursive_loop_detect_i(path_list_index);
684 * link to the path-list provided by the source, and go check
685 * if that forms any loops in the graph.
687 fib_entry->fe_parent = esrc->fes_pl;
688 fib_entry->fe_sibling =
689 fib_path_list_child_add(fib_entry->fe_parent,
691 fib_entry_get_index(fib_entry));
693 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
694 fib_path_list_unlock(path_list_index);
696 fib_entry_src_action_install(fib_entry, source);
697 fib_entry_src_action_fwd_update(fib_entry, source);
701 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
704 fib_entry_src_t *esrc;
706 esrc = fib_entry_src_find(fib_entry, source, NULL);
708 if (NULL != fib_entry_src_vft[source].fesv_installed)
710 fib_entry_src_vft[source].fesv_installed(esrc,
714 fib_entry_src_action_fwd_update(fib_entry, source);
718 * fib_entry_src_action_add
720 * Adding a source can result in a new fib_entry being created, which
721 * can inturn mean the pool is realloc'd and thus the entry passed as
722 * an argument it also realloc'd
723 * @return the original entry
726 fib_entry_src_action_add (fib_entry_t *fib_entry,
728 fib_entry_flag_t flags,
731 fib_node_index_t fib_entry_index;
732 fib_entry_src_t *esrc;
734 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
736 esrc->fes_ref_count++;
738 if (1 != esrc->fes_ref_count)
741 * we only want to add the source on the 0->1 transition
746 esrc->fes_entry_flags = flags;
749 * save variable so we can recover from a fib_entry realloc.
751 fib_entry_index = fib_entry_get_index(fib_entry);
753 if (NULL != fib_entry_src_vft[source].fesv_add)
755 fib_entry_src_vft[source].fesv_add(esrc,
758 fib_entry_get_proto(fib_entry),
762 fib_entry = fib_entry_get(fib_entry_index);
764 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
766 fib_path_list_lock(esrc->fes_pl);
769 * the source owns a lock on the entry
771 fib_entry_lock(fib_entry_get_index(fib_entry));
777 * fib_entry_src_action_update
779 * Adding a source can result in a new fib_entry being created, which
780 * can inturn mean the pool is realloc'd and thus the entry passed as
781 * an argument it also realloc'd
782 * @return the original entry
785 fib_entry_src_action_update (fib_entry_t *fib_entry,
787 fib_entry_flag_t flags,
790 fib_node_index_t fib_entry_index, old_path_list_index;
791 fib_entry_src_t *esrc;
793 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
796 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
798 old_path_list_index = esrc->fes_pl;
799 esrc->fes_entry_flags = flags;
802 * save variable so we can recover from a fib_entry realloc.
804 fib_entry_index = fib_entry_get_index(fib_entry);
806 if (NULL != fib_entry_src_vft[source].fesv_add)
808 fib_entry_src_vft[source].fesv_add(esrc,
811 fib_entry_get_proto(fib_entry),
815 fib_entry = fib_entry_get(fib_entry_index);
817 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
819 fib_path_list_lock(esrc->fes_pl);
820 fib_path_list_unlock(old_path_list_index);
827 fib_entry_src_action_remove (fib_entry_t *fib_entry,
831 fib_node_index_t old_path_list;
832 fib_entry_src_flag_t sflags;
833 fib_entry_src_t *esrc;
835 esrc = fib_entry_src_find(fib_entry, source, NULL);
838 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
840 esrc->fes_ref_count--;
841 sflags = esrc->fes_flags;
843 if (0 != esrc->fes_ref_count)
846 * only remove the source on the 1->0 transisition
851 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
853 fib_entry_src_action_deactivate(fib_entry, source);
856 old_path_list = esrc->fes_pl;
858 if (NULL != fib_entry_src_vft[source].fesv_remove)
860 fib_entry_src_vft[source].fesv_remove(esrc);
863 fib_path_list_unlock(old_path_list);
864 fib_entry_unlock(fib_entry_get_index(fib_entry));
866 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
867 fib_entry_src_action_deinit(fib_entry, source);
873 fib_route_recurses_via_self (const fib_prefix_t *prefix,
874 const fib_route_path_t *rpath)
877 * not all zeros next hop &&
878 * is recursive path &&
879 * nexthop is same as the route's address
881 return ((!ip46_address_is_zero(&rpath->frp_addr)) &&
882 (~0 == rpath->frp_sw_if_index) &&
883 (0 == ip46_address_cmp(&rpath->frp_addr, &prefix->fp_addr)));
888 * fib_route_attached_cross_table
890 * Return true the the route is attached via an interface that
891 * is not in the same table as the route
894 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
895 const fib_route_path_t *rpath)
898 * - All zeros next-hop
899 * - a valid interface
900 * - entry's fib index not equeal to interface's index
902 if (ip46_address_is_zero(&rpath->frp_addr) &&
903 (~0 != rpath->frp_sw_if_index) &&
904 (fib_entry->fe_fib_index !=
905 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
906 rpath->frp_sw_if_index)))
914 * fib_route_attached_cross_table
916 * Return true the the route is attached via an interface that
917 * is not in the same table as the route
920 fib_path_is_attached (const fib_route_path_t *rpath)
923 * - All zeros next-hop
924 * - a valid interface
926 if (ip46_address_is_zero(&rpath->frp_addr) &&
927 (~0 != rpath->frp_sw_if_index))
934 fib_path_list_flags_t
935 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
937 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
939 if (eflags & FIB_ENTRY_FLAG_DROP)
941 plf |= FIB_PATH_LIST_FLAG_DROP;
943 if (eflags & FIB_ENTRY_FLAG_LOCAL)
945 plf |= FIB_PATH_LIST_FLAG_LOCAL;
947 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
949 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
956 fib_entry_flags_update (const fib_entry_t *fib_entry,
957 const fib_route_path_t *rpath,
958 fib_path_list_flags_t *pl_flags,
959 fib_entry_src_t *esrc)
962 * don't allow the addition of a recursive looped path for prefix
965 if (fib_route_recurses_via_self(&fib_entry->fe_prefix, rpath))
968 * force the install of a drop path-list.
969 * we want the entry to have some path-list, mainly so
970 * the dodgy path can be rmeoved when the source stops playing
973 *pl_flags |= FIB_PATH_LIST_FLAG_DROP;
977 *pl_flags &= ~FIB_PATH_LIST_FLAG_DROP;
980 if ((esrc->fes_src == FIB_SOURCE_API) ||
981 (esrc->fes_src == FIB_SOURCE_CLI))
983 if (fib_path_is_attached(rpath))
985 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
989 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
992 if (fib_route_attached_cross_table(fib_entry, rpath))
994 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
998 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1003 * fib_entry_src_path_ext_add
1005 * append a path extension to the entry's list
1008 fib_entry_src_path_ext_append (fib_entry_src_t *esrc,
1009 const fib_route_path_t *rpath)
1011 if (NULL != rpath->frp_label_stack)
1013 fib_path_ext_t *path_ext;
1015 vec_add2(esrc->fes_path_exts, path_ext, 1);
1017 fib_path_ext_init(path_ext, esrc->fes_pl, rpath);
1022 * fib_entry_src_path_ext_insert
1024 * insert, sorted, a path extension to the entry's list.
1025 * It's not strictly necessary in sort the path extensions, since each
1026 * extension has the path index to which it resolves. However, by being
1027 * sorted the load-balance produced has a deterministic order, not an order
1028 * based on the sequence of extension additions. this is a considerable benefit.
1031 fib_entry_src_path_ext_insert (fib_entry_src_t *esrc,
1032 const fib_route_path_t *rpath)
1034 if (0 == vec_len(esrc->fes_path_exts))
1035 return (fib_entry_src_path_ext_append(esrc, rpath));
1037 if (NULL != rpath->frp_label_stack)
1039 fib_path_ext_t path_ext;
1042 fib_path_ext_init(&path_ext, esrc->fes_pl, rpath);
1044 while (i < vec_len(esrc->fes_path_exts) &&
1045 (fib_path_ext_cmp(&esrc->fes_path_exts[i], rpath) < 0))
1050 vec_insert_elts(esrc->fes_path_exts, &path_ext, 1, i);
1055 * fib_entry_src_action_add
1057 * Adding a source can result in a new fib_entry being created, which
1058 * can inturn mean the pool is realloc'd and thus the entry passed as
1059 * an argument it also realloc'd
1063 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1064 fib_source_t source,
1065 fib_entry_flag_t flags,
1066 const fib_route_path_t *rpath)
1068 fib_node_index_t old_path_list, fib_entry_index;
1069 fib_path_list_flags_t pl_flags;
1070 fib_path_ext_t *path_ext;
1071 fib_entry_src_t *esrc;
1074 * save variable so we can recover from a fib_entry realloc.
1076 fib_entry_index = fib_entry_get_index(fib_entry);
1078 esrc = fib_entry_src_find(fib_entry, source, NULL);
1082 fib_entry_src_action_add(fib_entry,
1087 fib_entry_get_proto(fib_entry))));
1088 esrc = fib_entry_src_find(fib_entry, source, NULL);
1092 * we are no doubt modifying a path-list. If the path-list
1093 * is shared, and hence not modifiable, then the index returned
1094 * will be for a different path-list. This FIB entry to needs
1095 * to maintain its lock appropriately.
1097 old_path_list = esrc->fes_pl;
1099 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1101 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1102 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1104 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1105 fib_entry = fib_entry_get(fib_entry_index);
1108 * re-resolve all the path-extensions with the new path-list
1110 vec_foreach(path_ext, esrc->fes_path_exts)
1112 fib_path_ext_resolve(path_ext, esrc->fes_pl);
1115 * if the path has a label we need to add a path extension
1117 fib_entry_src_path_ext_insert(esrc, rpath);
1119 fib_path_list_lock(esrc->fes_pl);
1120 fib_path_list_unlock(old_path_list);
1126 * fib_entry_src_action_swap
1128 * The source is providing new paths to replace the old ones.
1129 * Adding a source can result in a new fib_entry being created, which
1130 * can inturn mean the pool is realloc'd and thus the entry passed as
1131 * an argument it also realloc'd
1135 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1136 fib_source_t source,
1137 fib_entry_flag_t flags,
1138 const fib_route_path_t *rpaths)
1140 fib_node_index_t old_path_list, fib_entry_index;
1141 fib_path_list_flags_t pl_flags;
1142 const fib_route_path_t *rpath;
1143 fib_path_ext_t *path_ext;
1144 fib_entry_src_t *esrc;
1146 esrc = fib_entry_src_find(fib_entry, source, NULL);
1149 * save variable so we can recover from a fib_entry realloc.
1151 fib_entry_index = fib_entry_get_index(fib_entry);
1155 fib_entry = fib_entry_src_action_add(fib_entry,
1160 fib_entry_get_proto(fib_entry))));
1161 esrc = fib_entry_src_find(fib_entry, source, NULL);
1165 * swapping paths may create a new path-list (or may use an existing shared)
1166 * but we are certainly getting a different one. This FIB entry to needs
1167 * to maintain its lock appropriately.
1169 old_path_list = esrc->fes_pl;
1171 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1173 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1175 vec_foreach(rpath, rpaths)
1177 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1180 fib_entry_src_vft[source].fesv_path_swap(esrc,
1185 vec_foreach(path_ext, esrc->fes_path_exts)
1187 vec_free(path_ext->fpe_label_stack);
1189 vec_free(esrc->fes_path_exts);
1191 vec_foreach(rpath, rpaths)
1193 fib_entry_src_path_ext_append(esrc, rpath);
1196 fib_entry = fib_entry_get(fib_entry_index);
1198 fib_path_list_lock(esrc->fes_pl);
1199 fib_path_list_unlock(old_path_list);
1204 fib_entry_src_flag_t
1205 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1206 fib_source_t source,
1207 const fib_route_path_t *rpath)
1209 fib_path_list_flags_t pl_flags;
1210 fib_node_index_t old_path_list;
1211 fib_path_ext_t *path_ext;
1212 fib_entry_src_t *esrc;
1214 esrc = fib_entry_src_find(fib_entry, source, NULL);
1216 ASSERT(NULL != esrc);
1217 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1220 * we no doubt modifying a path-list. If the path-list
1221 * is shared, and hence not modifiable, then the index returned
1222 * will be for a different path-list. This FIB entry to needs
1223 * to maintain its lock appropriately.
1225 old_path_list = esrc->fes_pl;
1227 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1229 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1230 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1232 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1234 * find the matching path extension and remove it
1236 vec_foreach(path_ext, esrc->fes_path_exts)
1238 if (!fib_path_ext_cmp(path_ext, rpath))
1241 * delete the element moving the remaining elements down 1 position.
1242 * this preserves the sorted order.
1244 vec_free(path_ext->fpe_label_stack);
1245 vec_delete(esrc->fes_path_exts, 1, (path_ext - esrc->fes_path_exts));
1250 * re-resolve all the path-extensions with the new path-list
1252 vec_foreach(path_ext, esrc->fes_path_exts)
1254 fib_path_ext_resolve(path_ext, esrc->fes_pl);
1258 * lock the new path-list, unlock the old if it had one
1260 fib_path_list_unlock(old_path_list);
1262 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1263 fib_path_list_lock(esrc->fes_pl);
1264 return (FIB_ENTRY_SRC_FLAG_ADDED);
1269 * no more paths left from this source
1271 fib_entry_src_action_remove(fib_entry, source);
1272 return (FIB_ENTRY_SRC_FLAG_NONE);
1277 fib_entry_src_format (fib_entry_t *fib_entry,
1278 fib_source_t source,
1281 fib_entry_src_t *esrc;
1283 esrc = fib_entry_src_find(fib_entry, source, NULL);
1285 if (NULL != fib_entry_src_vft[source].fesv_format)
1287 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1293 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1294 fib_source_t source)
1296 fib_entry_t *fib_entry;
1297 fib_entry_src_t *esrc;
1299 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1300 return (ADJ_INDEX_INVALID);
1302 fib_entry = fib_entry_get(fib_entry_index);
1303 esrc = fib_entry_src_find(fib_entry, source, NULL);
1307 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1309 return (fib_path_list_get_adj(
1311 fib_entry_get_default_chain_type(fib_entry)));
1314 return (ADJ_INDEX_INVALID);
1318 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1319 fib_source_t source,
1322 fib_entry_t *fib_entry;
1323 fib_entry_src_t *esrc;
1325 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1328 fib_entry = fib_entry_get(fib_entry_index);
1329 esrc = fib_entry_src_find(fib_entry, source, NULL);
1333 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1335 fib_path_list_contribute_forwarding(
1337 fib_entry_get_default_chain_type(fib_entry),
1340 return (dpo_id_is_valid(dpo));
1347 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1348 fib_source_t source)
1350 fib_entry_t *fib_entry;
1351 fib_entry_src_t *esrc;
1353 fib_entry = fib_entry_get(entry_index);
1355 esrc = fib_entry_src_find(fib_entry, source, NULL);
1359 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1361 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1368 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1369 fib_source_t source)
1371 fib_entry_t *fib_entry;
1372 fib_entry_src_t *esrc;
1374 fib_entry = fib_entry_get(entry_index);
1376 esrc = fib_entry_src_find(fib_entry, source, NULL);
1380 return (esrc->fes_entry_flags);
1383 return (FIB_ENTRY_FLAG_NONE);
1387 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1389 fib_entry_flag_t flags;
1392 * the vector of sources is deliberately arranged in priority order
1394 if (0 == vec_len(fib_entry->fe_srcs))
1396 flags = FIB_ENTRY_FLAG_NONE;
1400 fib_entry_src_t *esrc;
1402 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1403 flags = esrc->fes_entry_flags;
1410 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1411 fib_source_t source,
1414 fib_entry_t *fib_entry;
1415 fib_entry_src_t *esrc;
1417 fib_entry = fib_entry_get(fib_entry_index);
1418 esrc = fib_entry_src_find(fib_entry, source, NULL);
1421 NULL != fib_entry_src_vft[source].fesv_set_data)
1423 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1428 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1429 fib_source_t source)
1431 fib_entry_t *fib_entry;
1432 fib_entry_src_t *esrc;
1434 fib_entry = fib_entry_get(fib_entry_index);
1435 esrc = fib_entry_src_find(fib_entry, source, NULL);
1438 NULL != fib_entry_src_vft[source].fesv_get_data)
1440 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1446 fib_entry_src_module_init (void)
1448 fib_entry_src_rr_register();
1449 fib_entry_src_interface_register();
1450 fib_entry_src_default_route_register();
1451 fib_entry_src_special_register();
1452 fib_entry_src_api_register();
1453 fib_entry_src_adj_register();
1454 fib_entry_src_mpls_register();
1455 fib_entry_src_lisp_register();