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 vec_free(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_collect_forwarding (fib_node_index_t pl_index,
269 fib_node_index_t path_index,
272 fib_entry_src_collect_forwarding_ctx_t *ctx;
273 fib_path_ext_t *path_ext;
279 * if the path is not resolved, don't include it.
281 if (!fib_path_is_resolved(path_index))
286 if (fib_path_is_recursive_constrained(path_index))
288 ctx->n_recursive_constrained += 1;
292 * get the matching path-extension for the path being visited.
295 vec_foreach(path_ext, ctx->esrc->fes_path_exts)
297 if (path_ext->fpe_path_index == path_index)
305 fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
308 * found a matching extension. stack it to obtain the forwarding
309 * info for this path.
312 fib_path_ext_stack(path_ext,
314 fib_entry_chain_type_fixup(ctx->fib_entry,
320 load_balance_path_t *nh;
323 * no extension => no out-going label for this path. that's OK
324 * in the case of an IP or EOS chain, but not for non-EOS
328 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
329 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
330 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
331 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
333 * EOS traffic with no label to stack, we need the IP Adj
335 vec_add2(ctx->next_hops, nh, 1);
337 nh->path_index = path_index;
338 nh->path_weight = fib_path_get_weight(path_index);
339 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
342 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
343 if (fib_path_is_exclusive(path_index) ||
344 fib_path_is_deag(path_index))
346 vec_add2(ctx->next_hops, nh, 1);
348 nh->path_index = path_index;
349 nh->path_weight = fib_path_get_weight(path_index);
350 fib_path_contribute_forwarding(path_index,
351 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
355 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
358 * no label. we need a chain based on the payload. fixup.
360 vec_add2(ctx->next_hops, nh, 1);
362 nh->path_index = path_index;
363 nh->path_weight = fib_path_get_weight(path_index);
364 fib_path_contribute_forwarding(path_index,
365 fib_entry_chain_type_fixup(ctx->fib_entry,
368 fib_path_stack_mpls_disp(path_index,
369 ctx->fib_entry->fe_prefix.fp_payload_proto,
374 case FIB_FORW_CHAIN_TYPE_ETHERNET:
375 case FIB_FORW_CHAIN_TYPE_NSH:
385 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
386 const fib_entry_src_t *esrc,
387 fib_forward_chain_type_t fct,
390 dpo_proto_t lb_proto;
393 * If the entry has path extensions then we construct a load-balance
394 * by stacking the extensions on the forwarding chains of the paths.
395 * Otherwise we use the load-balance of the path-list
397 fib_entry_src_collect_forwarding_ctx_t ctx = {
399 .fib_entry = fib_entry,
401 .n_recursive_constrained = 0,
406 * As an optimisation we allocate the vector of next-hops to be sized
407 * equal to the maximum nuber of paths we will need, which is also the
408 * most likely number we will need, since in most cases the paths are 'up'.
410 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
411 vec_reset_length(ctx.next_hops);
413 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
415 fib_path_list_walk(esrc->fes_pl,
416 fib_entry_src_collect_forwarding,
419 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
422 * the client provided the DPO that the entry should link to.
423 * all entries must link to a LB, so if it is an LB already
424 * then we can use it.
426 if ((1 == vec_len(ctx.next_hops)) &&
427 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
429 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
430 dpo_reset(&ctx.next_hops[0].path_dpo);
435 if (!dpo_id_is_valid(dpo_lb))
440 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
445 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
449 flow_hash_config_t fhc;
451 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
452 dpo_proto_to_fib(lb_proto));
456 load_balance_create(0, lb_proto, fhc));
460 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
465 replicate_multipath_update(dpo_lb, ctx.next_hops);
469 load_balance_multipath_update(dpo_lb,
471 fib_entry_calc_lb_flags(&ctx));
472 vec_free(ctx.next_hops);
475 * if this entry is sourced by the uRPF-exempt source then we
476 * append the always present local0 interface (index 0) to the
477 * uRPF list so it is not empty. that way packets pass the loose check.
479 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
481 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
482 FIB_SOURCE_URPF_EXEMPT) ||
483 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
484 (0 == fib_urpf_check_size(ui)))
487 * The uRPF list we get from the path-list is shared by all
488 * other users of the list, but the uRPF exemption applies
489 * only to this prefix. So we need our own list.
491 ui = fib_urpf_list_alloc_and_lock();
492 fib_urpf_list_append(ui, 0);
493 fib_urpf_list_bake(ui);
494 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
495 fib_urpf_list_unlock(ui);
499 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
501 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
502 fib_entry_get_flags_i(fib_entry));
507 fib_entry_src_action_install (fib_entry_t *fib_entry,
511 * Install the forwarding chain for the given source into the forwarding
514 fib_forward_chain_type_t fct;
515 fib_entry_src_t *esrc;
518 fct = fib_entry_get_default_chain_type(fib_entry);
519 esrc = fib_entry_src_find(fib_entry, source, NULL);
522 * Every entry has its own load-balance object. All changes to the entry's
523 * forwarding result in an inplace modify of the load-balance. This means
524 * the load-balance object only needs to be added to the forwarding
525 * DB once, when it is created.
527 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
529 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
531 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
532 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
535 * insert the adj into the data-plane forwarding trie
539 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
540 &fib_entry->fe_prefix,
545 * if any of the other chain types are already created they will need
548 fib_entry_delegate_type_t fdt;
549 fib_entry_delegate_t *fed;
551 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
553 fib_entry_src_mk_lb(fib_entry, esrc,
554 fib_entry_delegate_type_to_chain_type(fdt),
560 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
563 * uninstall the forwarding chain from the forwarding tables
565 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
566 fib_entry->fe_adj_index);
568 if (dpo_id_is_valid(&fib_entry->fe_lb))
570 fib_table_fwding_dpo_remove(
571 fib_entry->fe_fib_index,
572 &fib_entry->fe_prefix,
575 dpo_reset(&fib_entry->fe_lb);
580 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
582 fib_node_index_t *entries = NULL;
584 fib_path_list_recursive_loop_detect(path_list_index, &entries);
590 fib_entry_src_action_activate (fib_entry_t *fib_entry,
594 int houston_we_are_go_for_install;
595 fib_entry_src_t *esrc;
597 esrc = fib_entry_src_find(fib_entry, source, NULL);
599 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
600 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
602 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
604 if (NULL != fib_entry_src_vft[source].fesv_activate)
606 houston_we_are_go_for_install =
607 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
612 * the source is not providing an activate function, we'll assume
613 * therefore it has no objection to installing the entry
615 houston_we_are_go_for_install = !0;
619 * link to the path-list provided by the source, and go check
620 * if that forms any loops in the graph.
622 fib_entry->fe_parent = esrc->fes_pl;
623 fib_entry->fe_sibling =
624 fib_path_list_child_add(fib_entry->fe_parent,
626 fib_entry_get_index(fib_entry));
628 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
630 FIB_ENTRY_DBG(fib_entry, "activate: %d",
631 fib_entry->fe_parent);
633 if (0 != houston_we_are_go_for_install)
635 fib_entry_src_action_install(fib_entry, source);
639 fib_entry_src_action_uninstall(fib_entry);
644 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
648 fib_node_index_t path_list_index;
649 fib_entry_src_t *esrc;
651 esrc = fib_entry_src_find(fib_entry, source, NULL);
653 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
655 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
657 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
660 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
662 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
665 * un-link from an old path-list. Check for any loops this will clear
667 path_list_index = fib_entry->fe_parent;
668 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
670 fib_entry_recursive_loop_detect_i(path_list_index);
673 * this will unlock the path-list, so it may be invalid thereafter.
675 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
676 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
680 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
683 fib_entry_src_t *esrc;
685 vec_foreach(esrc, fib_entry->fe_srcs)
687 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
689 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
697 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
700 fib_node_index_t path_list_index;
701 fib_entry_src_t *esrc;
703 esrc = fib_entry_src_find(fib_entry, source, NULL);
705 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
707 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
708 fib_entry->fe_parent,
711 if (fib_entry->fe_parent != esrc->fes_pl)
714 * un-link from an old path-list. Check for any loops this will clear
716 path_list_index = fib_entry->fe_parent;
717 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
720 * temporary lock so it doesn't get deleted when this entry is no
723 fib_path_list_lock(path_list_index);
726 * this entry is no longer a child. after unlinking check if any loops
729 fib_path_list_child_remove(path_list_index,
730 fib_entry->fe_sibling);
732 fib_entry_recursive_loop_detect_i(path_list_index);
735 * link to the path-list provided by the source, and go check
736 * if that forms any loops in the graph.
738 fib_entry->fe_parent = esrc->fes_pl;
739 fib_entry->fe_sibling =
740 fib_path_list_child_add(fib_entry->fe_parent,
742 fib_entry_get_index(fib_entry));
744 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
745 fib_path_list_unlock(path_list_index);
747 fib_entry_src_action_install(fib_entry, source);
748 fib_entry_src_action_fwd_update(fib_entry, source);
752 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
755 fib_entry_src_t *esrc;
757 esrc = fib_entry_src_find(fib_entry, source, NULL);
759 if (NULL != fib_entry_src_vft[source].fesv_installed)
761 fib_entry_src_vft[source].fesv_installed(esrc,
765 fib_entry_src_action_fwd_update(fib_entry, source);
769 * fib_entry_src_action_add
771 * Adding a source can result in a new fib_entry being created, which
772 * can inturn mean the pool is realloc'd and thus the entry passed as
773 * an argument it also realloc'd
774 * @return the original entry
777 fib_entry_src_action_add (fib_entry_t *fib_entry,
779 fib_entry_flag_t flags,
782 fib_node_index_t fib_entry_index;
783 fib_entry_src_t *esrc;
785 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
787 esrc->fes_ref_count++;
789 if (1 != esrc->fes_ref_count)
792 * we only want to add the source on the 0->1 transition
797 esrc->fes_entry_flags = flags;
800 * save variable so we can recover from a fib_entry realloc.
802 fib_entry_index = fib_entry_get_index(fib_entry);
804 if (NULL != fib_entry_src_vft[source].fesv_add)
806 fib_entry_src_vft[source].fesv_add(esrc,
809 fib_entry_get_proto(fib_entry),
813 fib_entry = fib_entry_get(fib_entry_index);
815 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
817 fib_path_list_lock(esrc->fes_pl);
820 * the source owns a lock on the entry
822 fib_entry_lock(fib_entry_get_index(fib_entry));
828 * fib_entry_src_action_update
830 * Adding a source can result in a new fib_entry being created, which
831 * can inturn mean the pool is realloc'd and thus the entry passed as
832 * an argument it also realloc'd
833 * @return the original entry
836 fib_entry_src_action_update (fib_entry_t *fib_entry,
838 fib_entry_flag_t flags,
841 fib_node_index_t fib_entry_index, old_path_list_index;
842 fib_entry_src_t *esrc;
844 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
847 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
849 old_path_list_index = esrc->fes_pl;
850 esrc->fes_entry_flags = flags;
853 * save variable so we can recover from a fib_entry realloc.
855 fib_entry_index = fib_entry_get_index(fib_entry);
857 if (NULL != fib_entry_src_vft[source].fesv_add)
859 fib_entry_src_vft[source].fesv_add(esrc,
862 fib_entry_get_proto(fib_entry),
866 fib_entry = fib_entry_get(fib_entry_index);
868 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
870 fib_path_list_lock(esrc->fes_pl);
871 fib_path_list_unlock(old_path_list_index);
878 fib_entry_src_action_remove (fib_entry_t *fib_entry,
882 fib_node_index_t old_path_list;
883 fib_entry_src_flag_t sflags;
884 fib_entry_src_t *esrc;
886 esrc = fib_entry_src_find(fib_entry, source, NULL);
889 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
891 esrc->fes_ref_count--;
892 sflags = esrc->fes_flags;
894 if (0 != esrc->fes_ref_count)
897 * only remove the source on the 1->0 transisition
902 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
904 fib_entry_src_action_deactivate(fib_entry, source);
907 old_path_list = esrc->fes_pl;
909 if (NULL != fib_entry_src_vft[source].fesv_remove)
911 fib_entry_src_vft[source].fesv_remove(esrc);
914 fib_path_list_unlock(old_path_list);
915 fib_entry_unlock(fib_entry_get_index(fib_entry));
917 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
918 fib_entry_src_action_deinit(fib_entry, source);
924 * fib_route_attached_cross_table
926 * Return true the the route is attached via an interface that
927 * is not in the same table as the route
930 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
931 const fib_route_path_t *rpath)
934 * - All zeros next-hop
935 * - a valid interface
936 * - entry's fib index not equeal to interface's index
938 if (ip46_address_is_zero(&rpath->frp_addr) &&
939 (~0 != rpath->frp_sw_if_index) &&
940 (fib_entry->fe_fib_index !=
941 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
942 rpath->frp_sw_if_index)))
950 * fib_route_attached_cross_table
952 * Return true the the route is attached via an interface that
953 * is not in the same table as the route
956 fib_path_is_attached (const fib_route_path_t *rpath)
959 * - All zeros next-hop
960 * - a valid interface
962 if (ip46_address_is_zero(&rpath->frp_addr) &&
963 (~0 != rpath->frp_sw_if_index))
967 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
974 fib_path_list_flags_t
975 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
977 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
979 if (eflags & FIB_ENTRY_FLAG_DROP)
981 plf |= FIB_PATH_LIST_FLAG_DROP;
983 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
985 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
987 if (eflags & FIB_ENTRY_FLAG_LOCAL)
989 plf |= FIB_PATH_LIST_FLAG_LOCAL;
996 fib_entry_flags_update (const fib_entry_t *fib_entry,
997 const fib_route_path_t *rpath,
998 fib_path_list_flags_t *pl_flags,
999 fib_entry_src_t *esrc)
1001 if ((esrc->fes_src == FIB_SOURCE_API) ||
1002 (esrc->fes_src == FIB_SOURCE_CLI))
1004 if (fib_path_is_attached(rpath))
1006 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1010 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1013 if (fib_route_attached_cross_table(fib_entry, rpath))
1015 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1019 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1024 * fib_entry_src_path_ext_add
1026 * append a path extension to the entry's list
1029 fib_entry_src_path_ext_append (fib_entry_src_t *esrc,
1030 const fib_route_path_t *rpath)
1032 if (NULL != rpath->frp_label_stack)
1034 fib_path_ext_t *path_ext;
1036 vec_add2(esrc->fes_path_exts, path_ext, 1);
1038 fib_path_ext_init(path_ext, esrc->fes_pl, rpath);
1043 * fib_entry_src_path_ext_insert
1045 * insert, sorted, a path extension to the entry's list.
1046 * It's not strictly necessary in sort the path extensions, since each
1047 * extension has the path index to which it resolves. However, by being
1048 * sorted the load-balance produced has a deterministic order, not an order
1049 * based on the sequence of extension additions. this is a considerable benefit.
1052 fib_entry_src_path_ext_insert (fib_entry_src_t *esrc,
1053 const fib_route_path_t *rpath)
1055 if (0 == vec_len(esrc->fes_path_exts))
1056 return (fib_entry_src_path_ext_append(esrc, rpath));
1058 if (NULL != rpath->frp_label_stack)
1060 fib_path_ext_t path_ext;
1063 fib_path_ext_init(&path_ext, esrc->fes_pl, rpath);
1065 while (i < vec_len(esrc->fes_path_exts) &&
1066 (fib_path_ext_cmp(&esrc->fes_path_exts[i], rpath) < 0))
1071 vec_insert_elts(esrc->fes_path_exts, &path_ext, 1, i);
1076 * fib_entry_src_action_add
1078 * Adding a source can result in a new fib_entry being created, which
1079 * can inturn mean the pool is realloc'd and thus the entry passed as
1080 * an argument it also realloc'd
1084 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1085 fib_source_t source,
1086 fib_entry_flag_t flags,
1087 const fib_route_path_t *rpath)
1089 fib_node_index_t old_path_list, fib_entry_index;
1090 fib_path_list_flags_t pl_flags;
1091 fib_path_ext_t *path_ext;
1092 fib_entry_src_t *esrc;
1095 * save variable so we can recover from a fib_entry realloc.
1097 fib_entry_index = fib_entry_get_index(fib_entry);
1099 esrc = fib_entry_src_find(fib_entry, source, NULL);
1103 fib_entry_src_action_add(fib_entry,
1108 fib_entry_get_proto(fib_entry))));
1109 esrc = fib_entry_src_find(fib_entry, source, NULL);
1113 * we are no doubt modifying a path-list. If the path-list
1114 * is shared, and hence not modifiable, then the index returned
1115 * will be for a different path-list. This FIB entry to needs
1116 * to maintain its lock appropriately.
1118 old_path_list = esrc->fes_pl;
1120 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1122 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1123 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1125 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1126 fib_entry = fib_entry_get(fib_entry_index);
1129 * re-resolve all the path-extensions with the new path-list
1131 vec_foreach(path_ext, esrc->fes_path_exts)
1133 fib_path_ext_resolve(path_ext, esrc->fes_pl);
1136 * if the path has a label we need to add a path extension
1138 fib_entry_src_path_ext_insert(esrc, rpath);
1140 fib_path_list_lock(esrc->fes_pl);
1141 fib_path_list_unlock(old_path_list);
1147 * fib_entry_src_action_swap
1149 * The source is providing new paths to replace the old ones.
1150 * Adding a source can result in a new fib_entry being created, which
1151 * can inturn mean the pool is realloc'd and thus the entry passed as
1152 * an argument it also realloc'd
1156 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1157 fib_source_t source,
1158 fib_entry_flag_t flags,
1159 const fib_route_path_t *rpaths)
1161 fib_node_index_t old_path_list, fib_entry_index;
1162 fib_path_list_flags_t pl_flags;
1163 const fib_route_path_t *rpath;
1164 fib_path_ext_t *path_ext;
1165 fib_entry_src_t *esrc;
1167 esrc = fib_entry_src_find(fib_entry, source, NULL);
1170 * save variable so we can recover from a fib_entry realloc.
1172 fib_entry_index = fib_entry_get_index(fib_entry);
1176 fib_entry = fib_entry_src_action_add(fib_entry,
1181 fib_entry_get_proto(fib_entry))));
1182 esrc = fib_entry_src_find(fib_entry, source, NULL);
1186 * swapping paths may create a new path-list (or may use an existing shared)
1187 * but we are certainly getting a different one. This FIB entry to needs
1188 * to maintain its lock appropriately.
1190 old_path_list = esrc->fes_pl;
1192 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1194 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1196 vec_foreach(rpath, rpaths)
1198 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1201 fib_entry_src_vft[source].fesv_path_swap(esrc,
1206 vec_foreach(path_ext, esrc->fes_path_exts)
1208 vec_free(path_ext->fpe_label_stack);
1210 vec_free(esrc->fes_path_exts);
1212 vec_foreach(rpath, rpaths)
1214 fib_entry_src_path_ext_append(esrc, rpath);
1217 fib_entry = fib_entry_get(fib_entry_index);
1219 fib_path_list_lock(esrc->fes_pl);
1220 fib_path_list_unlock(old_path_list);
1225 fib_entry_src_flag_t
1226 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1227 fib_source_t source,
1228 const fib_route_path_t *rpath)
1230 fib_path_list_flags_t pl_flags;
1231 fib_node_index_t old_path_list;
1232 fib_path_ext_t *path_ext;
1233 fib_entry_src_t *esrc;
1235 esrc = fib_entry_src_find(fib_entry, source, NULL);
1237 ASSERT(NULL != esrc);
1238 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1241 * we no doubt modifying a path-list. If the path-list
1242 * is shared, and hence not modifiable, then the index returned
1243 * will be for a different path-list. This FIB entry to needs
1244 * to maintain its lock appropriately.
1246 old_path_list = esrc->fes_pl;
1248 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1250 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1251 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1253 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1255 * find the matching path extension and remove it
1257 vec_foreach(path_ext, esrc->fes_path_exts)
1259 if (!fib_path_ext_cmp(path_ext, rpath))
1262 * delete the element moving the remaining elements down 1 position.
1263 * this preserves the sorted order.
1265 vec_free(path_ext->fpe_label_stack);
1266 vec_delete(esrc->fes_path_exts, 1, (path_ext - esrc->fes_path_exts));
1271 * re-resolve all the path-extensions with the new path-list
1273 vec_foreach(path_ext, esrc->fes_path_exts)
1275 fib_path_ext_resolve(path_ext, esrc->fes_pl);
1279 * lock the new path-list, unlock the old if it had one
1281 fib_path_list_unlock(old_path_list);
1283 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1284 fib_path_list_lock(esrc->fes_pl);
1285 return (FIB_ENTRY_SRC_FLAG_ADDED);
1290 * no more paths left from this source
1292 fib_entry_src_action_remove(fib_entry, source);
1293 return (FIB_ENTRY_SRC_FLAG_NONE);
1298 fib_entry_src_format (fib_entry_t *fib_entry,
1299 fib_source_t source,
1302 fib_entry_src_t *esrc;
1304 esrc = fib_entry_src_find(fib_entry, source, NULL);
1306 if (NULL != fib_entry_src_vft[source].fesv_format)
1308 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1314 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1315 fib_source_t source)
1317 fib_entry_t *fib_entry;
1318 fib_entry_src_t *esrc;
1320 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1321 return (ADJ_INDEX_INVALID);
1323 fib_entry = fib_entry_get(fib_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_adj(
1332 fib_entry_get_default_chain_type(fib_entry)));
1335 return (ADJ_INDEX_INVALID);
1339 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1340 fib_source_t source,
1343 fib_entry_t *fib_entry;
1344 fib_entry_src_t *esrc;
1346 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1349 fib_entry = fib_entry_get(fib_entry_index);
1350 esrc = fib_entry_src_find(fib_entry, source, NULL);
1354 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1356 fib_path_list_contribute_forwarding(
1358 fib_entry_get_default_chain_type(fib_entry),
1361 return (dpo_id_is_valid(dpo));
1368 fib_entry_get_resolving_interface_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 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1382 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1389 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1390 fib_source_t source)
1392 fib_entry_t *fib_entry;
1393 fib_entry_src_t *esrc;
1395 fib_entry = fib_entry_get(entry_index);
1397 esrc = fib_entry_src_find(fib_entry, source, NULL);
1401 return (esrc->fes_entry_flags);
1404 return (FIB_ENTRY_FLAG_NONE);
1408 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1410 fib_entry_flag_t flags;
1413 * the vector of sources is deliberately arranged in priority order
1415 if (0 == vec_len(fib_entry->fe_srcs))
1417 flags = FIB_ENTRY_FLAG_NONE;
1421 fib_entry_src_t *esrc;
1423 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1424 flags = esrc->fes_entry_flags;
1431 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1432 fib_source_t source,
1435 fib_entry_t *fib_entry;
1436 fib_entry_src_t *esrc;
1438 fib_entry = fib_entry_get(fib_entry_index);
1439 esrc = fib_entry_src_find(fib_entry, source, NULL);
1442 NULL != fib_entry_src_vft[source].fesv_set_data)
1444 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1449 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1450 fib_source_t source)
1452 fib_entry_t *fib_entry;
1453 fib_entry_src_t *esrc;
1455 fib_entry = fib_entry_get(fib_entry_index);
1456 esrc = fib_entry_src_find(fib_entry, source, NULL);
1459 NULL != fib_entry_src_vft[source].fesv_get_data)
1461 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1467 fib_entry_src_module_init (void)
1469 fib_entry_src_rr_register();
1470 fib_entry_src_interface_register();
1471 fib_entry_src_default_route_register();
1472 fib_entry_src_special_register();
1473 fib_entry_src_api_register();
1474 fib_entry_src_adj_register();
1475 fib_entry_src_mpls_register();
1476 fib_entry_src_lisp_register();