2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/adj/adj.h>
17 #include <vnet/dpo/load_balance.h>
18 #include <vnet/dpo/mpls_label_dpo.h>
19 #include <vnet/dpo/drop_dpo.h>
20 #include <vnet/dpo/replicate_dpo.h>
22 #include <vnet/fib/fib_entry_src.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vnet/fib/fib_path_ext.h>
25 #include <vnet/fib/fib_urpf_list.h>
30 static fib_entry_src_vft_t fib_entry_src_vft[FIB_SOURCE_MAX];
33 fib_entry_src_register (fib_source_t source,
34 const fib_entry_src_vft_t *vft)
36 fib_entry_src_vft[source] = *vft;
40 fib_entry_src_cmp_for_sort (void * v1,
43 fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
45 return (esrc1->fes_src - esrc2->fes_src);
49 fib_entry_src_action_init (fib_entry_t *fib_entry,
53 fib_entry_src_t esrc = {
54 .fes_pl = FIB_NODE_INDEX_INVALID,
55 .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
59 if (NULL != fib_entry_src_vft[source].fesv_init)
61 fib_entry_src_vft[source].fesv_init(&esrc);
64 if (fib_entry_has_multiple_srcs(fib_entry))
66 vec_add1(fib_entry->fe_u_src.fe_srcs, esrc);
68 vec_sort_with_function(fib_entry->fe_u_src.fe_srcs,
69 fib_entry_src_cmp_for_sort);
74 * is this the very first source
76 if (FIB_SOURCE_INVALID == fib_entry->fe_u_src.fe_src.fes_src)
78 clib_memcpy(&fib_entry->fe_u_src.fe_src, &esrc, sizeof(esrc));
83 * transitioning to multiple sources.
84 * allocate the vecotr of sources.
86 fib_entry_src_t *srcs = NULL;
88 vec_validate(srcs, 1);
93 if (fib_entry->fe_u_src.fe_src.fes_src < esrc.fes_src)
95 srcs[0] = fib_entry->fe_u_src.fe_src;
101 srcs[1] = fib_entry->fe_u_src.fe_src;
103 memset(&fib_entry->fe_u_src.fe_src, 0,
104 sizeof(fib_entry->fe_u_src.fe_src));
105 fib_entry->fe_u_src.fe_srcs = srcs;
107 fib_entry->fe_node.fn_pad |= FIB_ENTRY_NODE_FLAG_MULTIPLE_SRCS;
113 fib_entry_has_multiple_srcs(const fib_entry_t * fib_entry)
115 return (fib_entry->fe_node.fn_pad & FIB_ENTRY_NODE_FLAG_MULTIPLE_SRCS);
118 static fib_entry_src_t *
119 fib_entry_src_find (fib_entry_t *fib_entry,
124 fib_entry_src_t *esrc;
128 if (fib_entry_has_multiple_srcs(fib_entry))
130 vec_foreach(esrc, fib_entry->fe_u_src.fe_srcs)
132 if (esrc->fes_src == source)
148 esrc = &fib_entry->fe_u_src.fe_src;
149 if (esrc->fes_src == source)
162 static fib_entry_src_t *
163 fib_entry_src_delete (fib_entry_t *fib_entry,
169 ASSERT(!fib_entry_has_multiple_srcs(fib_entry));
170 memset(&fib_entry->fe_u_src.fe_src, 0,
171 sizeof(fib_entry->fe_u_src.fe_src));
175 vec_del1(fib_entry->fe_u_src.fe_srcs, index);
177 ASSERT(vec_len(fib_entry->fe_u_src.fe_srcs));
178 if (1 == vec_len(fib_entry->fe_u_src.fe_srcs))
181 * Is there much point in transitioning back?
182 * We've paid the cost of the malloc for the vector,
184 * Favour memory use. If the expectation that multiple sources
185 * is rare is correct, then we should expect this entry is
186 * unlikely to need the vector again
188 fib_entry_src_t *srcs;
190 srcs = fib_entry->fe_u_src.fe_srcs;
191 fib_entry->fe_node.fn_pad &= ~FIB_ENTRY_NODE_FLAG_MULTIPLE_SRCS;
192 clib_memcpy(&fib_entry->fe_u_src.fe_src,
194 sizeof(fib_entry->fe_u_src.fe_src));
199 vec_sort_with_function(fib_entry->fe_u_src.fe_srcs,
200 fib_entry_src_cmp_for_sort);
208 fib_entry_is_sourced (fib_node_index_t fib_entry_index,
211 fib_entry_t *fib_entry;
213 fib_entry = fib_entry_get(fib_entry_index);
215 return (NULL != fib_entry_src_find(fib_entry, source, NULL));
218 static fib_entry_src_t *
219 fib_entry_src_find_or_create (fib_entry_t *fib_entry,
222 fib_entry_src_t *esrc;
224 esrc = fib_entry_src_find(fib_entry, source, NULL);
228 fib_entry_src_action_init(fib_entry, source);
231 return (fib_entry_src_find(fib_entry, source, NULL));
235 fib_entry_src_action_deinit (fib_entry_t *fib_entry,
239 fib_entry_src_t *esrc;
242 esrc = fib_entry_src_find(fib_entry, source, &index);
244 ASSERT(NULL != esrc);
246 if (NULL != fib_entry_src_vft[source].fesv_deinit)
248 fib_entry_src_vft[source].fesv_deinit(esrc);
251 fib_path_ext_list_flush(&esrc->fes_path_exts);
252 fib_entry_src_delete(fib_entry, index);
255 fib_entry_src_cover_res_t
256 fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
259 if (NULL != fib_entry_src_vft[source].fesv_cover_change)
261 return (fib_entry_src_vft[source].fesv_cover_change(
262 fib_entry_src_find(fib_entry, source, NULL),
266 fib_entry_src_cover_res_t res = {
268 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
273 fib_entry_src_cover_res_t
274 fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
277 if (NULL != fib_entry_src_vft[source].fesv_cover_update)
279 return (fib_entry_src_vft[source].fesv_cover_update(
280 fib_entry_src_find(fib_entry, source, NULL),
284 fib_entry_src_cover_res_t res = {
286 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
291 typedef struct fib_entry_src_collect_forwarding_ctx_t_
293 load_balance_path_t *next_hops;
294 const fib_entry_t *fib_entry;
295 const fib_entry_src_t *esrc;
296 fib_forward_chain_type_t fct;
297 int n_recursive_constrained;
299 } fib_entry_src_collect_forwarding_ctx_t;
302 * @brief Determine whether this FIB entry should use a load-balance MAP
303 * to support PIC edge fast convergence
306 fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
309 * We'll use a LB map if the path-list has multiple recursive paths.
310 * recursive paths implies BGP, and hence scale.
312 if (ctx->n_recursive_constrained > 1 &&
313 fib_path_list_is_popular(ctx->esrc->fes_pl))
315 return (LOAD_BALANCE_FLAG_USES_MAP);
317 return (LOAD_BALANCE_FLAG_NONE);
321 fib_entry_src_valid_out_label (mpls_label_t label)
323 return ((MPLS_LABEL_IS_REAL(label) ||
324 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
325 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
326 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
330 * @brief Turn the chain type requested by the client into the one they
333 fib_forward_chain_type_t
334 fib_entry_chain_type_fixup (const fib_entry_t *entry,
335 fib_forward_chain_type_t fct)
338 * The EOS chain is a tricky since one cannot know the adjacency
339 * to link to without knowing what the packets payload protocol
340 * will be once the label is popped.
342 fib_forward_chain_type_t dfct;
344 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct)
349 dfct = fib_entry_get_default_chain_type(entry);
351 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS == dfct)
354 * If the entry being asked is a eos-MPLS label entry,
355 * then use the payload-protocol field, that we stashed there
356 * for just this purpose
358 return (fib_forw_chain_type_from_dpo_proto(
359 entry->fe_prefix.fp_payload_proto));
362 * else give them what this entry would be by default. i.e. if it's a v6
363 * entry, then the label its local labelled should be carrying v6 traffic.
364 * If it's a non-EOS label entry, then there are more labels and we want
371 fib_prefix_get_payload_proto (const fib_prefix_t *pfx)
373 switch (pfx->fp_proto)
375 case FIB_PROTOCOL_IP4:
376 return (DPO_PROTO_IP4);
377 case FIB_PROTOCOL_IP6:
378 return (DPO_PROTO_IP6);
379 case FIB_PROTOCOL_MPLS:
380 return (pfx->fp_payload_proto);
384 return (DPO_PROTO_IP4);
388 fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
389 fib_entry_src_collect_forwarding_ctx_t *ctx)
391 load_balance_path_t *nh;
394 * no extension => no out-going label for this path. that's OK
395 * in the case of an IP or EOS chain, but not for non-EOS
399 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
400 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
401 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
402 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
403 case FIB_FORW_CHAIN_TYPE_BIER:
405 * EOS traffic with no label to stack, we need the IP Adj
407 vec_add2(ctx->next_hops, nh, 1);
409 nh->path_index = path_index;
410 nh->path_weight = fib_path_get_weight(path_index);
411 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
414 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
415 if (fib_path_is_exclusive(path_index) ||
416 fib_path_is_deag(path_index))
418 vec_add2(ctx->next_hops, nh, 1);
420 nh->path_index = path_index;
421 nh->path_weight = fib_path_get_weight(path_index);
422 fib_path_contribute_forwarding(path_index,
423 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
427 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
430 * no label. we need a chain based on the payload. fixup.
432 vec_add2(ctx->next_hops, nh, 1);
434 nh->path_index = path_index;
435 nh->path_weight = fib_path_get_weight(path_index);
436 fib_path_contribute_forwarding(path_index,
437 fib_entry_chain_type_fixup(ctx->fib_entry,
440 fib_path_stack_mpls_disp(path_index,
441 fib_prefix_get_payload_proto(&ctx->fib_entry->fe_prefix),
446 case FIB_FORW_CHAIN_TYPE_ETHERNET:
447 case FIB_FORW_CHAIN_TYPE_NSH:
453 static fib_path_list_walk_rc_t
454 fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
455 fib_node_index_t path_index,
458 fib_entry_src_collect_forwarding_ctx_t *ctx;
459 fib_path_ext_t *path_ext;
464 * if the path is not resolved, don't include it.
466 if (!fib_path_is_resolved(path_index))
468 return (FIB_PATH_LIST_WALK_CONTINUE);
471 if (fib_path_is_recursive_constrained(path_index))
473 ctx->n_recursive_constrained += 1;
475 if (0xffff == ctx->preference)
478 * not set a preference yet, so the first path we encounter
479 * sets the preference we are collecting.
481 ctx->preference = fib_path_get_preference(path_index);
483 else if (ctx->preference != fib_path_get_preference(path_index))
486 * this path does not belong to the same preference as the
487 * previous paths encountered. we are done now.
489 return (FIB_PATH_LIST_WALK_STOP);
493 * get the matching path-extension for the path being visited.
495 path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
498 if (NULL != path_ext)
500 switch (path_ext->fpe_type)
502 case FIB_PATH_EXT_MPLS:
503 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
506 * found a matching extension. stack it to obtain the forwarding
507 * info for this path.
510 fib_path_ext_stack(path_ext,
512 fib_entry_chain_type_fixup(ctx->fib_entry,
518 fib_entry_src_get_path_forwarding(path_index, ctx);
521 case FIB_PATH_EXT_ADJ:
522 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
524 fib_entry_src_get_path_forwarding(path_index, ctx);
528 * the path does not refine the cover, meaning that
529 * the adjacency doesdoes not match the sub-net on the link.
530 * So this path does not contribute forwarding.
537 fib_entry_src_get_path_forwarding(path_index, ctx);
540 return (FIB_PATH_LIST_WALK_CONTINUE);
544 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
545 const fib_entry_src_t *esrc,
546 fib_forward_chain_type_t fct,
549 dpo_proto_t lb_proto;
552 * If the entry has path extensions then we construct a load-balance
553 * by stacking the extensions on the forwarding chains of the paths.
554 * Otherwise we use the load-balance of the path-list
556 fib_entry_src_collect_forwarding_ctx_t ctx = {
558 .fib_entry = fib_entry,
560 .n_recursive_constrained = 0,
562 .preference = 0xffff,
566 * As an optimisation we allocate the vector of next-hops to be sized
567 * equal to the maximum nuber of paths we will need, which is also the
568 * most likely number we will need, since in most cases the paths are 'up'.
570 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
571 vec_reset_length(ctx.next_hops);
573 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
575 fib_path_list_walk(esrc->fes_pl,
576 fib_entry_src_collect_forwarding,
579 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
582 * the client provided the DPO that the entry should link to.
583 * all entries must link to a LB, so if it is an LB already
584 * then we can use it.
586 if ((1 == vec_len(ctx.next_hops)) &&
587 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
589 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
590 dpo_reset(&ctx.next_hops[0].path_dpo);
595 if (!dpo_id_is_valid(dpo_lb))
600 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
605 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
609 fib_protocol_t flow_hash_proto;
610 flow_hash_config_t fhc;
613 * if the protocol for the LB we are building does not match that
614 * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
615 * then the fib_index is not an index that relates to the table
616 * type we need. So get the default flow-hash config instead.
618 flow_hash_proto = dpo_proto_to_fib(lb_proto);
619 if (fib_entry->fe_prefix.fp_proto != flow_hash_proto)
621 fhc = fib_table_get_default_flow_hash_config(flow_hash_proto);
625 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
632 load_balance_create(0, lb_proto, fhc));
636 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
641 replicate_multipath_update(dpo_lb, ctx.next_hops);
645 load_balance_multipath_update(dpo_lb,
647 fib_entry_calc_lb_flags(&ctx));
648 vec_free(ctx.next_hops);
651 * if this entry is sourced by the uRPF-exempt source then we
652 * append the always present local0 interface (index 0) to the
653 * uRPF list so it is not empty. that way packets pass the loose check.
655 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
657 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
658 FIB_SOURCE_URPF_EXEMPT) ||
659 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
660 (0 == fib_urpf_check_size(ui)))
663 * The uRPF list we get from the path-list is shared by all
664 * other users of the list, but the uRPF exemption applies
665 * only to this prefix. So we need our own list.
667 ui = fib_urpf_list_alloc_and_lock();
668 fib_urpf_list_append(ui, 0);
669 fib_urpf_list_bake(ui);
670 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
671 fib_urpf_list_unlock(ui);
675 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
677 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
678 fib_entry_get_flags_i(fib_entry));
683 fib_entry_src_action_install (fib_entry_t *fib_entry,
687 * Install the forwarding chain for the given source into the forwarding
690 fib_forward_chain_type_t fct;
691 fib_entry_src_t *esrc;
694 fct = fib_entry_get_default_chain_type(fib_entry);
695 esrc = fib_entry_src_find(fib_entry, source, NULL);
698 * Every entry has its own load-balance object. All changes to the entry's
699 * forwarding result in an inplace modify of the load-balance. This means
700 * the load-balance object only needs to be added to the forwarding
701 * DB once, when it is created.
703 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
705 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
707 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
708 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
711 * insert the adj into the data-plane forwarding trie
715 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
716 &fib_entry->fe_prefix,
721 * if any of the other chain types are already created they will need
724 fib_entry_delegate_type_t fdt;
725 fib_entry_delegate_t *fed;
727 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
729 fib_entry_src_mk_lb(fib_entry, esrc,
730 fib_entry_delegate_type_to_chain_type(fdt),
736 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
739 * uninstall the forwarding chain from the forwarding tables
741 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
742 fib_entry->fe_adj_index);
744 if (dpo_id_is_valid(&fib_entry->fe_lb))
746 fib_table_fwding_dpo_remove(
747 fib_entry->fe_fib_index,
748 &fib_entry->fe_prefix,
751 dpo_reset(&fib_entry->fe_lb);
756 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
758 fib_node_index_t *entries = NULL;
760 fib_path_list_recursive_loop_detect(path_list_index, &entries);
766 fib_entry_src_action_activate (fib_entry_t *fib_entry,
770 int houston_we_are_go_for_install;
771 fib_entry_src_t *esrc;
773 esrc = fib_entry_src_find(fib_entry, source, NULL);
775 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
776 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
778 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
780 if (NULL != fib_entry_src_vft[source].fesv_activate)
782 houston_we_are_go_for_install =
783 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
788 * the source is not providing an activate function, we'll assume
789 * therefore it has no objection to installing the entry
791 houston_we_are_go_for_install = !0;
795 * link to the path-list provided by the source, and go check
796 * if that forms any loops in the graph.
798 fib_entry->fe_parent = esrc->fes_pl;
799 fib_entry->fe_sibling =
800 fib_path_list_child_add(fib_entry->fe_parent,
802 fib_entry_get_index(fib_entry));
804 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
806 FIB_ENTRY_DBG(fib_entry, "activate: %d",
807 fib_entry->fe_parent);
809 if (0 != houston_we_are_go_for_install)
811 fib_entry_src_action_install(fib_entry, source);
815 fib_entry_src_action_uninstall(fib_entry);
820 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
824 fib_node_index_t path_list_index;
825 fib_entry_src_t *esrc;
827 esrc = fib_entry_src_find(fib_entry, source, NULL);
829 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
831 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
833 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
836 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
838 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
841 * un-link from an old path-list. Check for any loops this will clear
843 path_list_index = fib_entry->fe_parent;
844 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
846 fib_entry_recursive_loop_detect_i(path_list_index);
849 * this will unlock the path-list, so it may be invalid thereafter.
851 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
852 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
856 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
859 fib_node_index_t path_list_index;
860 fib_entry_src_t *esrc;
862 esrc = fib_entry_src_find(fib_entry, source, NULL);
864 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
866 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
867 fib_entry->fe_parent,
870 if (fib_entry->fe_parent != esrc->fes_pl)
873 * un-link from an old path-list. Check for any loops this will clear
875 path_list_index = fib_entry->fe_parent;
876 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
879 * temporary lock so it doesn't get deleted when this entry is no
882 fib_path_list_lock(path_list_index);
885 * this entry is no longer a child. after unlinking check if any loops
888 fib_path_list_child_remove(path_list_index,
889 fib_entry->fe_sibling);
891 fib_entry_recursive_loop_detect_i(path_list_index);
894 * link to the path-list provided by the source, and go check
895 * if that forms any loops in the graph.
897 fib_entry->fe_parent = esrc->fes_pl;
898 fib_entry->fe_sibling =
899 fib_path_list_child_add(fib_entry->fe_parent,
901 fib_entry_get_index(fib_entry));
903 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
904 fib_path_list_unlock(path_list_index);
906 fib_entry_src_action_install(fib_entry, source);
910 fib_entry_src_action_installed (fib_entry_t *fib_entry,
913 fib_entry_src_t *esrc;
915 esrc = fib_entry_src_find(fib_entry, source, NULL);
917 if (NULL != fib_entry_src_vft[source].fesv_installed)
919 fib_entry_src_vft[source].fesv_installed(esrc,
925 * fib_entry_src_action_add
927 * Adding a source can result in a new fib_entry being created, which
928 * can inturn mean the pool is realloc'd and thus the entry passed as
929 * an argument it also realloc'd
930 * @return the original entry
933 fib_entry_src_action_add (fib_entry_t *fib_entry,
935 fib_entry_flag_t flags,
938 fib_node_index_t fib_entry_index;
939 fib_entry_src_t *esrc;
941 esrc = fib_entry_src_find_or_create(fib_entry, source);
943 esrc->fes_ref_count++;
945 if (1 != esrc->fes_ref_count)
948 * we only want to add the source on the 0->1 transition
953 esrc->fes_entry_flags = flags;
956 * save variable so we can recover from a fib_entry realloc.
958 fib_entry_index = fib_entry_get_index(fib_entry);
960 if (NULL != fib_entry_src_vft[source].fesv_add)
962 fib_entry_src_vft[source].fesv_add(esrc,
965 fib_entry_get_dpo_proto(fib_entry),
969 fib_entry = fib_entry_get(fib_entry_index);
971 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
973 fib_path_list_lock(esrc->fes_pl);
976 * the source owns a lock on the entry
978 fib_entry_lock(fib_entry_get_index(fib_entry));
984 * fib_entry_src_action_update
986 * Adding a source can result in a new fib_entry being created, which
987 * can inturn mean the pool is realloc'd and thus the entry passed as
988 * an argument it also realloc'd
989 * @return the original entry
992 fib_entry_src_action_update (fib_entry_t *fib_entry,
994 fib_entry_flag_t flags,
997 fib_node_index_t fib_entry_index, old_path_list_index;
998 fib_entry_src_t *esrc;
1000 esrc = fib_entry_src_find_or_create(fib_entry, source);
1003 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
1005 old_path_list_index = esrc->fes_pl;
1006 esrc->fes_entry_flags = flags;
1009 * save variable so we can recover from a fib_entry realloc.
1011 fib_entry_index = fib_entry_get_index(fib_entry);
1013 if (NULL != fib_entry_src_vft[source].fesv_add)
1015 fib_entry_src_vft[source].fesv_add(esrc,
1018 fib_entry_get_dpo_proto(fib_entry),
1022 fib_entry = fib_entry_get(fib_entry_index);
1024 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
1026 fib_path_list_lock(esrc->fes_pl);
1027 fib_path_list_unlock(old_path_list_index);
1033 fib_entry_src_flag_t
1034 fib_entry_src_action_remove (fib_entry_t *fib_entry,
1035 fib_source_t source)
1038 fib_node_index_t old_path_list;
1039 fib_entry_src_flag_t sflags;
1040 fib_entry_src_t *esrc;
1042 esrc = fib_entry_src_find(fib_entry, source, NULL);
1045 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
1047 esrc->fes_ref_count--;
1048 sflags = esrc->fes_flags;
1050 if (0 != esrc->fes_ref_count)
1053 * only remove the source on the 1->0 transisition
1058 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
1060 fib_entry_src_action_deactivate(fib_entry, source);
1063 old_path_list = esrc->fes_pl;
1065 if (NULL != fib_entry_src_vft[source].fesv_remove)
1067 fib_entry_src_vft[source].fesv_remove(esrc);
1070 fib_path_list_unlock(old_path_list);
1071 fib_entry_unlock(fib_entry_get_index(fib_entry));
1073 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
1074 fib_entry_src_action_deinit(fib_entry, source);
1080 * fib_route_attached_cross_table
1082 * Return true the the route is attached via an interface that
1083 * is not in the same table as the route
1086 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
1087 const fib_route_path_t *rpath)
1090 * - All zeros next-hop
1091 * - a valid interface
1092 * - entry's fib index not equeal to interface's index
1094 if (ip46_address_is_zero(&rpath->frp_addr) &&
1095 (~0 != rpath->frp_sw_if_index) &&
1096 (fib_entry->fe_fib_index !=
1097 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
1098 rpath->frp_sw_if_index)))
1106 * fib_route_attached_cross_table
1108 * Return true the the route is attached via an interface that
1109 * is not in the same table as the route
1112 fib_path_is_attached (const fib_route_path_t *rpath)
1115 * - All zeros next-hop
1116 * - a valid interface
1118 if (ip46_address_is_zero(&rpath->frp_addr) &&
1119 (~0 != rpath->frp_sw_if_index))
1123 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1130 fib_path_list_flags_t
1131 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1133 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1135 if (eflags & FIB_ENTRY_FLAG_DROP)
1137 plf |= FIB_PATH_LIST_FLAG_DROP;
1139 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1141 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1143 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1145 plf |= FIB_PATH_LIST_FLAG_LOCAL;
1152 fib_entry_flags_update (const fib_entry_t *fib_entry,
1153 const fib_route_path_t *rpath,
1154 fib_path_list_flags_t *pl_flags,
1155 fib_entry_src_t *esrc)
1157 if ((esrc->fes_src == FIB_SOURCE_API) ||
1158 (esrc->fes_src == FIB_SOURCE_CLI))
1160 if (fib_path_is_attached(rpath))
1162 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1166 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1169 if (fib_route_attached_cross_table(fib_entry, rpath))
1171 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1175 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1180 * fib_entry_src_action_add
1182 * Adding a source can result in a new fib_entry being created, which
1183 * can inturn mean the pool is realloc'd and thus the entry passed as
1184 * an argument it also realloc'd
1188 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1189 fib_source_t source,
1190 fib_entry_flag_t flags,
1191 const fib_route_path_t *rpath)
1193 fib_node_index_t old_path_list, fib_entry_index;
1194 fib_path_list_flags_t pl_flags;
1195 fib_entry_src_t *esrc;
1198 * save variable so we can recover from a fib_entry realloc.
1200 fib_entry_index = fib_entry_get_index(fib_entry);
1202 esrc = fib_entry_src_find(fib_entry, source, NULL);
1206 fib_entry_src_action_add(fib_entry,
1210 fib_entry_get_dpo_proto(fib_entry)));
1211 esrc = fib_entry_src_find(fib_entry, source, NULL);
1215 * we are no doubt modifying a path-list. If the path-list
1216 * is shared, and hence not modifiable, then the index returned
1217 * will be for a different path-list. This FIB entry to needs
1218 * to maintain its lock appropriately.
1220 old_path_list = esrc->fes_pl;
1222 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1224 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1225 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1227 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1228 fib_entry = fib_entry_get(fib_entry_index);
1230 fib_path_list_lock(esrc->fes_pl);
1231 fib_path_list_unlock(old_path_list);
1237 * fib_entry_src_action_swap
1239 * The source is providing new paths to replace the old ones.
1240 * Adding a source can result in a new fib_entry being created, which
1241 * can inturn mean the pool is realloc'd and thus the entry passed as
1242 * an argument it also realloc'd
1246 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1247 fib_source_t source,
1248 fib_entry_flag_t flags,
1249 const fib_route_path_t *rpaths)
1251 fib_node_index_t old_path_list, fib_entry_index;
1252 fib_path_list_flags_t pl_flags;
1253 const fib_route_path_t *rpath;
1254 fib_entry_src_t *esrc;
1256 esrc = fib_entry_src_find(fib_entry, source, NULL);
1259 * save variable so we can recover from a fib_entry realloc.
1261 fib_entry_index = fib_entry_get_index(fib_entry);
1265 fib_entry = fib_entry_src_action_add(fib_entry,
1269 fib_entry_get_dpo_proto(fib_entry)));
1270 esrc = fib_entry_src_find(fib_entry, source, NULL);
1274 * swapping paths may create a new path-list (or may use an existing shared)
1275 * but we are certainly getting a different one. This FIB entry to needs
1276 * to maintain its lock appropriately.
1278 old_path_list = esrc->fes_pl;
1280 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1282 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1284 vec_foreach(rpath, rpaths)
1286 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1289 fib_entry_src_vft[source].fesv_path_swap(esrc,
1294 fib_entry = fib_entry_get(fib_entry_index);
1296 fib_path_list_lock(esrc->fes_pl);
1297 fib_path_list_unlock(old_path_list);
1302 fib_entry_src_flag_t
1303 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1304 fib_source_t source,
1305 const fib_route_path_t *rpath)
1307 fib_path_list_flags_t pl_flags;
1308 fib_node_index_t old_path_list;
1309 fib_entry_src_t *esrc;
1311 esrc = fib_entry_src_find(fib_entry, source, NULL);
1313 ASSERT(NULL != esrc);
1314 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1317 * we no doubt modifying a path-list. If the path-list
1318 * is shared, and hence not modifiable, then the index returned
1319 * will be for a different path-list. This FIB entry to needs
1320 * to maintain its lock appropriately.
1322 old_path_list = esrc->fes_pl;
1324 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1326 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1327 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1329 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1332 * lock the new path-list, unlock the old if it had one
1334 fib_path_list_unlock(old_path_list);
1336 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1337 fib_path_list_lock(esrc->fes_pl);
1338 return (FIB_ENTRY_SRC_FLAG_ADDED);
1343 * no more paths left from this source
1345 fib_entry_src_action_remove(fib_entry, source);
1346 return (FIB_ENTRY_SRC_FLAG_NONE);
1351 fib_entry_src_format (fib_entry_t *fib_entry,
1352 fib_source_t source,
1355 fib_entry_src_t *esrc;
1357 esrc = fib_entry_src_find(fib_entry, source, NULL);
1359 if (NULL != fib_entry_src_vft[source].fesv_format)
1361 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1367 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1368 fib_source_t source)
1370 fib_entry_t *fib_entry;
1371 fib_entry_src_t *esrc;
1373 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1374 return (ADJ_INDEX_INVALID);
1376 fib_entry = fib_entry_get(fib_entry_index);
1377 esrc = fib_entry_src_find(fib_entry, source, NULL);
1381 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1383 return (fib_path_list_get_adj(
1385 fib_entry_get_default_chain_type(fib_entry)));
1388 return (ADJ_INDEX_INVALID);
1392 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1393 fib_source_t source,
1396 fib_entry_t *fib_entry;
1397 fib_entry_src_t *esrc;
1399 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1402 fib_entry = fib_entry_get(fib_entry_index);
1403 esrc = fib_entry_src_find(fib_entry, source, NULL);
1407 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1409 fib_path_list_contribute_forwarding(
1411 fib_entry_get_default_chain_type(fib_entry),
1414 return (dpo_id_is_valid(dpo));
1421 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1422 fib_source_t source)
1424 fib_entry_t *fib_entry;
1425 fib_entry_src_t *esrc;
1427 fib_entry = fib_entry_get(entry_index);
1429 esrc = fib_entry_src_find(fib_entry, source, NULL);
1433 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1435 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1442 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1443 fib_source_t source)
1445 fib_entry_t *fib_entry;
1446 fib_entry_src_t *esrc;
1448 fib_entry = fib_entry_get(entry_index);
1450 esrc = fib_entry_src_find(fib_entry, source, NULL);
1454 return (esrc->fes_entry_flags);
1457 return (FIB_ENTRY_FLAG_NONE);
1461 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1462 fib_source_t source,
1465 fib_entry_t *fib_entry;
1466 fib_entry_src_t *esrc;
1468 fib_entry = fib_entry_get(fib_entry_index);
1469 esrc = fib_entry_src_find(fib_entry, source, NULL);
1472 NULL != fib_entry_src_vft[source].fesv_set_data)
1474 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1479 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1480 fib_source_t source)
1482 fib_entry_t *fib_entry;
1483 fib_entry_src_t *esrc;
1485 fib_entry = fib_entry_get(fib_entry_index);
1486 esrc = fib_entry_src_find(fib_entry, source, NULL);
1489 NULL != fib_entry_src_vft[source].fesv_get_data)
1491 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1497 fib_entry_src_module_init (void)
1499 fib_entry_src_rr_register();
1500 fib_entry_src_interface_register();
1501 fib_entry_src_default_route_register();
1502 fib_entry_src_special_register();
1503 fib_entry_src_api_register();
1504 fib_entry_src_adj_register();
1505 fib_entry_src_mpls_register();
1506 fib_entry_src_lisp_register();