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 vec_add1(fib_entry->fe_srcs, esrc);
65 vec_sort_with_function(fib_entry->fe_srcs,
66 fib_entry_src_cmp_for_sort);
69 static fib_entry_src_t *
70 fib_entry_src_find (const fib_entry_t *fib_entry,
75 fib_entry_src_t *esrc;
79 vec_foreach(esrc, fib_entry->fe_srcs)
81 if (esrc->fes_src == source)
99 fib_entry_is_sourced (fib_node_index_t fib_entry_index,
102 fib_entry_t *fib_entry;
104 fib_entry = fib_entry_get(fib_entry_index);
106 return (NULL != fib_entry_src_find(fib_entry, source, NULL));
109 static fib_entry_src_t *
110 fib_entry_src_find_or_create (fib_entry_t *fib_entry,
114 fib_entry_src_t *esrc;
116 esrc = fib_entry_src_find(fib_entry, source, NULL);
120 fib_entry_src_action_init(fib_entry, source);
123 return (fib_entry_src_find(fib_entry, source, NULL));
127 fib_entry_src_action_deinit (fib_entry_t *fib_entry,
131 fib_entry_src_t *esrc;
134 esrc = fib_entry_src_find(fib_entry, source, &index);
136 ASSERT(NULL != esrc);
138 if (NULL != fib_entry_src_vft[source].fesv_deinit)
140 fib_entry_src_vft[source].fesv_deinit(esrc);
143 fib_path_ext_list_flush(&esrc->fes_path_exts);
144 vec_del1(fib_entry->fe_srcs, index);
147 fib_entry_src_cover_res_t
148 fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
151 if (NULL != fib_entry_src_vft[source].fesv_cover_change)
153 return (fib_entry_src_vft[source].fesv_cover_change(
154 fib_entry_src_find(fib_entry, source, NULL),
158 fib_entry_src_cover_res_t res = {
160 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
165 fib_entry_src_cover_res_t
166 fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
169 if (NULL != fib_entry_src_vft[source].fesv_cover_update)
171 return (fib_entry_src_vft[source].fesv_cover_update(
172 fib_entry_src_find(fib_entry, source, NULL),
176 fib_entry_src_cover_res_t res = {
178 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
183 typedef struct fib_entry_src_collect_forwarding_ctx_t_
185 load_balance_path_t *next_hops;
186 const fib_entry_t *fib_entry;
187 const fib_entry_src_t *esrc;
188 fib_forward_chain_type_t fct;
189 int n_recursive_constrained;
191 } fib_entry_src_collect_forwarding_ctx_t;
194 * @brief Determine whether this FIB entry should use a load-balance MAP
195 * to support PIC edge fast convergence
198 fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
201 * We'll use a LB map if the path-list has multiple recursive paths.
202 * recursive paths implies BGP, and hence scale.
204 if (ctx->n_recursive_constrained > 1 &&
205 fib_path_list_is_popular(ctx->esrc->fes_pl))
207 return (LOAD_BALANCE_FLAG_USES_MAP);
209 return (LOAD_BALANCE_FLAG_NONE);
213 fib_entry_src_valid_out_label (mpls_label_t label)
215 return ((MPLS_LABEL_IS_REAL(label) ||
216 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
217 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
218 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
222 * @brief Turn the chain type requested by the client into the one they
225 fib_forward_chain_type_t
226 fib_entry_chain_type_fixup (const fib_entry_t *entry,
227 fib_forward_chain_type_t fct)
230 * The EOS chain is a tricky since one cannot know the adjacency
231 * to link to without knowing what the packets payload protocol
232 * will be once the label is popped.
234 fib_forward_chain_type_t dfct;
236 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct)
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_prefix_get_payload_proto (const fib_prefix_t *pfx)
265 switch (pfx->fp_proto)
267 case FIB_PROTOCOL_IP4:
268 return (DPO_PROTO_IP4);
269 case FIB_PROTOCOL_IP6:
270 return (DPO_PROTO_IP6);
271 case FIB_PROTOCOL_MPLS:
272 return (pfx->fp_payload_proto);
276 return (DPO_PROTO_IP4);
280 fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
281 fib_entry_src_collect_forwarding_ctx_t *ctx)
283 load_balance_path_t *nh;
286 * no extension => no out-going label for this path. that's OK
287 * in the case of an IP or EOS chain, but not for non-EOS
291 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
292 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
293 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
294 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
295 case FIB_FORW_CHAIN_TYPE_BIER:
297 * EOS traffic with no label to stack, we need the IP Adj
299 vec_add2(ctx->next_hops, nh, 1);
301 nh->path_index = path_index;
302 nh->path_weight = fib_path_get_weight(path_index);
303 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
306 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
307 if (fib_path_is_exclusive(path_index) ||
308 fib_path_is_deag(path_index))
310 vec_add2(ctx->next_hops, nh, 1);
312 nh->path_index = path_index;
313 nh->path_weight = fib_path_get_weight(path_index);
314 fib_path_contribute_forwarding(path_index,
315 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
319 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
322 * no label. we need a chain based on the payload. fixup.
324 vec_add2(ctx->next_hops, nh, 1);
326 nh->path_index = path_index;
327 nh->path_weight = fib_path_get_weight(path_index);
328 fib_path_contribute_forwarding(path_index,
329 fib_entry_chain_type_fixup(ctx->fib_entry,
332 fib_path_stack_mpls_disp(path_index,
333 fib_prefix_get_payload_proto(&ctx->fib_entry->fe_prefix),
338 case FIB_FORW_CHAIN_TYPE_ETHERNET:
339 case FIB_FORW_CHAIN_TYPE_NSH:
345 static fib_path_list_walk_rc_t
346 fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
347 fib_node_index_t path_index,
350 fib_entry_src_collect_forwarding_ctx_t *ctx;
351 fib_path_ext_t *path_ext;
356 * if the path is not resolved, don't include it.
358 if (!fib_path_is_resolved(path_index))
360 return (FIB_PATH_LIST_WALK_CONTINUE);
363 if (fib_path_is_recursive_constrained(path_index))
365 ctx->n_recursive_constrained += 1;
367 if (0xffff == ctx->preference)
370 * not set a preference yet, so the first path we encounter
371 * sets the preference we are collecting.
373 ctx->preference = fib_path_get_preference(path_index);
375 else if (ctx->preference != fib_path_get_preference(path_index))
378 * this path does not belong to the same preference as the
379 * previous paths encountered. we are done now.
381 return (FIB_PATH_LIST_WALK_STOP);
385 * get the matching path-extension for the path being visited.
387 path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
390 if (NULL != path_ext)
392 switch (path_ext->fpe_type)
394 case FIB_PATH_EXT_MPLS:
395 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
398 * found a matching extension. stack it to obtain the forwarding
399 * info for this path.
402 fib_path_ext_stack(path_ext,
404 fib_entry_chain_type_fixup(ctx->fib_entry,
410 fib_entry_src_get_path_forwarding(path_index, ctx);
413 case FIB_PATH_EXT_ADJ:
414 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
416 fib_entry_src_get_path_forwarding(path_index, ctx);
420 * the path does not refine the cover, meaning that
421 * the adjacency doesdoes not match the sub-net on the link.
422 * So this path does not contribute forwarding.
429 fib_entry_src_get_path_forwarding(path_index, ctx);
432 return (FIB_PATH_LIST_WALK_CONTINUE);
436 fib_entry_src_mk_lb (fib_entry_t *fib_entry,
437 const fib_entry_src_t *esrc,
438 fib_forward_chain_type_t fct,
441 dpo_proto_t lb_proto;
444 * If the entry has path extensions then we construct a load-balance
445 * by stacking the extensions on the forwarding chains of the paths.
446 * Otherwise we use the load-balance of the path-list
448 fib_entry_src_collect_forwarding_ctx_t ctx = {
450 .fib_entry = fib_entry,
452 .n_recursive_constrained = 0,
454 .preference = 0xffff,
458 * As an optimisation we allocate the vector of next-hops to be sized
459 * equal to the maximum nuber of paths we will need, which is also the
460 * most likely number we will need, since in most cases the paths are 'up'.
462 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
463 vec_reset_length(ctx.next_hops);
465 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
467 fib_path_list_walk(esrc->fes_pl,
468 fib_entry_src_collect_forwarding,
471 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
474 * the client provided the DPO that the entry should link to.
475 * all entries must link to a LB, so if it is an LB already
476 * then we can use it.
478 if ((1 == vec_len(ctx.next_hops)) &&
479 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
481 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
482 dpo_reset(&ctx.next_hops[0].path_dpo);
487 if (!dpo_id_is_valid(dpo_lb))
492 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
497 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
501 fib_protocol_t flow_hash_proto;
502 flow_hash_config_t fhc;
505 * if the protocol for the LB we are building does not match that
506 * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
507 * then the fib_index is not an index that relates to the table
508 * type we need. So get the default flow-hash config instead.
510 flow_hash_proto = dpo_proto_to_fib(lb_proto);
511 if (fib_entry->fe_prefix.fp_proto != flow_hash_proto)
513 fhc = fib_table_get_default_flow_hash_config(flow_hash_proto);
517 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
524 load_balance_create(0, lb_proto, fhc));
528 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
533 replicate_multipath_update(dpo_lb, ctx.next_hops);
537 load_balance_multipath_update(dpo_lb,
539 fib_entry_calc_lb_flags(&ctx));
540 vec_free(ctx.next_hops);
543 * if this entry is sourced by the uRPF-exempt source then we
544 * append the always present local0 interface (index 0) to the
545 * uRPF list so it is not empty. that way packets pass the loose check.
547 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
549 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
550 FIB_SOURCE_URPF_EXEMPT) ||
551 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
552 (0 == fib_urpf_check_size(ui)))
555 * The uRPF list we get from the path-list is shared by all
556 * other users of the list, but the uRPF exemption applies
557 * only to this prefix. So we need our own list.
559 ui = fib_urpf_list_alloc_and_lock();
560 fib_urpf_list_append(ui, 0);
561 fib_urpf_list_bake(ui);
562 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
563 fib_urpf_list_unlock(ui);
567 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
569 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
570 fib_entry_get_flags_i(fib_entry));
575 fib_entry_src_action_install (fib_entry_t *fib_entry,
579 * Install the forwarding chain for the given source into the forwarding
582 fib_forward_chain_type_t fct;
583 fib_entry_src_t *esrc;
586 fct = fib_entry_get_default_chain_type(fib_entry);
587 esrc = fib_entry_src_find(fib_entry, source, NULL);
590 * Every entry has its own load-balance object. All changes to the entry's
591 * forwarding result in an inplace modify of the load-balance. This means
592 * the load-balance object only needs to be added to the forwarding
593 * DB once, when it is created.
595 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
597 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
599 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
600 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
603 * insert the adj into the data-plane forwarding trie
607 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
608 &fib_entry->fe_prefix,
613 * if any of the other chain types are already created they will need
616 fib_entry_delegate_type_t fdt;
617 fib_entry_delegate_t *fed;
619 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
621 fib_entry_src_mk_lb(fib_entry, esrc,
622 fib_entry_delegate_type_to_chain_type(fdt),
628 fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
631 * uninstall the forwarding chain from the forwarding tables
633 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
634 fib_entry->fe_adj_index);
636 if (dpo_id_is_valid(&fib_entry->fe_lb))
638 fib_table_fwding_dpo_remove(
639 fib_entry->fe_fib_index,
640 &fib_entry->fe_prefix,
643 dpo_reset(&fib_entry->fe_lb);
648 fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
650 fib_node_index_t *entries = NULL;
652 fib_path_list_recursive_loop_detect(path_list_index, &entries);
658 fib_entry_src_action_activate (fib_entry_t *fib_entry,
662 int houston_we_are_go_for_install;
663 fib_entry_src_t *esrc;
665 esrc = fib_entry_src_find(fib_entry, source, NULL);
667 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
668 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
670 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
672 if (NULL != fib_entry_src_vft[source].fesv_activate)
674 houston_we_are_go_for_install =
675 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
680 * the source is not providing an activate function, we'll assume
681 * therefore it has no objection to installing the entry
683 houston_we_are_go_for_install = !0;
687 * link to the path-list provided by the source, and go check
688 * if that forms any loops in the graph.
690 fib_entry->fe_parent = esrc->fes_pl;
691 fib_entry->fe_sibling =
692 fib_path_list_child_add(fib_entry->fe_parent,
694 fib_entry_get_index(fib_entry));
696 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
698 FIB_ENTRY_DBG(fib_entry, "activate: %d",
699 fib_entry->fe_parent);
701 if (0 != houston_we_are_go_for_install)
703 fib_entry_src_action_install(fib_entry, source);
707 fib_entry_src_action_uninstall(fib_entry);
712 fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
716 fib_node_index_t path_list_index;
717 fib_entry_src_t *esrc;
719 esrc = fib_entry_src_find(fib_entry, source, NULL);
721 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
723 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
725 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
728 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
730 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
733 * un-link from an old path-list. Check for any loops this will clear
735 path_list_index = fib_entry->fe_parent;
736 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
738 fib_entry_recursive_loop_detect_i(path_list_index);
741 * this will unlock the path-list, so it may be invalid thereafter.
743 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
744 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
748 fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
751 fib_entry_src_t *esrc;
753 vec_foreach(esrc, fib_entry->fe_srcs)
755 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
757 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
765 fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
768 fib_node_index_t path_list_index;
769 fib_entry_src_t *esrc;
771 esrc = fib_entry_src_find(fib_entry, source, NULL);
773 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
775 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
776 fib_entry->fe_parent,
779 if (fib_entry->fe_parent != esrc->fes_pl)
782 * un-link from an old path-list. Check for any loops this will clear
784 path_list_index = fib_entry->fe_parent;
785 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
788 * temporary lock so it doesn't get deleted when this entry is no
791 fib_path_list_lock(path_list_index);
794 * this entry is no longer a child. after unlinking check if any loops
797 fib_path_list_child_remove(path_list_index,
798 fib_entry->fe_sibling);
800 fib_entry_recursive_loop_detect_i(path_list_index);
803 * link to the path-list provided by the source, and go check
804 * if that forms any loops in the graph.
806 fib_entry->fe_parent = esrc->fes_pl;
807 fib_entry->fe_sibling =
808 fib_path_list_child_add(fib_entry->fe_parent,
810 fib_entry_get_index(fib_entry));
812 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
813 fib_path_list_unlock(path_list_index);
815 fib_entry_src_action_install(fib_entry, source);
816 fib_entry_src_action_fwd_update(fib_entry, source);
820 fib_entry_src_action_installed (const fib_entry_t *fib_entry,
823 fib_entry_src_t *esrc;
825 esrc = fib_entry_src_find(fib_entry, source, NULL);
827 if (NULL != fib_entry_src_vft[source].fesv_installed)
829 fib_entry_src_vft[source].fesv_installed(esrc,
833 fib_entry_src_action_fwd_update(fib_entry, source);
837 * fib_entry_src_action_add
839 * Adding a source can result in a new fib_entry being created, which
840 * can inturn mean the pool is realloc'd and thus the entry passed as
841 * an argument it also realloc'd
842 * @return the original entry
845 fib_entry_src_action_add (fib_entry_t *fib_entry,
847 fib_entry_flag_t flags,
850 fib_node_index_t fib_entry_index;
851 fib_entry_src_t *esrc;
853 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
855 esrc->fes_ref_count++;
857 if (1 != esrc->fes_ref_count)
860 * we only want to add the source on the 0->1 transition
865 esrc->fes_entry_flags = flags;
868 * save variable so we can recover from a fib_entry realloc.
870 fib_entry_index = fib_entry_get_index(fib_entry);
872 if (NULL != fib_entry_src_vft[source].fesv_add)
874 fib_entry_src_vft[source].fesv_add(esrc,
877 fib_entry_get_dpo_proto(fib_entry),
881 fib_entry = fib_entry_get(fib_entry_index);
883 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
885 fib_path_list_lock(esrc->fes_pl);
888 * the source owns a lock on the entry
890 fib_entry_lock(fib_entry_get_index(fib_entry));
896 * fib_entry_src_action_update
898 * Adding a source can result in a new fib_entry being created, which
899 * can inturn mean the pool is realloc'd and thus the entry passed as
900 * an argument it also realloc'd
901 * @return the original entry
904 fib_entry_src_action_update (fib_entry_t *fib_entry,
906 fib_entry_flag_t flags,
909 fib_node_index_t fib_entry_index, old_path_list_index;
910 fib_entry_src_t *esrc;
912 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
915 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
917 old_path_list_index = esrc->fes_pl;
918 esrc->fes_entry_flags = flags;
921 * save variable so we can recover from a fib_entry realloc.
923 fib_entry_index = fib_entry_get_index(fib_entry);
925 if (NULL != fib_entry_src_vft[source].fesv_add)
927 fib_entry_src_vft[source].fesv_add(esrc,
930 fib_entry_get_dpo_proto(fib_entry),
934 fib_entry = fib_entry_get(fib_entry_index);
936 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
938 fib_path_list_lock(esrc->fes_pl);
939 fib_path_list_unlock(old_path_list_index);
946 fib_entry_src_action_remove (fib_entry_t *fib_entry,
950 fib_node_index_t old_path_list;
951 fib_entry_src_flag_t sflags;
952 fib_entry_src_t *esrc;
954 esrc = fib_entry_src_find(fib_entry, source, NULL);
957 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
959 esrc->fes_ref_count--;
960 sflags = esrc->fes_flags;
962 if (0 != esrc->fes_ref_count)
965 * only remove the source on the 1->0 transisition
970 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
972 fib_entry_src_action_deactivate(fib_entry, source);
975 old_path_list = esrc->fes_pl;
977 if (NULL != fib_entry_src_vft[source].fesv_remove)
979 fib_entry_src_vft[source].fesv_remove(esrc);
982 fib_path_list_unlock(old_path_list);
983 fib_entry_unlock(fib_entry_get_index(fib_entry));
985 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
986 fib_entry_src_action_deinit(fib_entry, source);
992 * fib_route_attached_cross_table
994 * Return true the the route is attached via an interface that
995 * is not in the same table as the route
998 fib_route_attached_cross_table (const fib_entry_t *fib_entry,
999 const fib_route_path_t *rpath)
1002 * - All zeros next-hop
1003 * - a valid interface
1004 * - entry's fib index not equeal to interface's index
1006 if (ip46_address_is_zero(&rpath->frp_addr) &&
1007 (~0 != rpath->frp_sw_if_index) &&
1008 (fib_entry->fe_fib_index !=
1009 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
1010 rpath->frp_sw_if_index)))
1018 * fib_route_attached_cross_table
1020 * Return true the the route is attached via an interface that
1021 * is not in the same table as the route
1024 fib_path_is_attached (const fib_route_path_t *rpath)
1027 * - All zeros next-hop
1028 * - a valid interface
1030 if (ip46_address_is_zero(&rpath->frp_addr) &&
1031 (~0 != rpath->frp_sw_if_index))
1035 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1042 fib_path_list_flags_t
1043 fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1045 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1047 if (eflags & FIB_ENTRY_FLAG_DROP)
1049 plf |= FIB_PATH_LIST_FLAG_DROP;
1051 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1053 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1055 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1057 plf |= FIB_PATH_LIST_FLAG_LOCAL;
1064 fib_entry_flags_update (const fib_entry_t *fib_entry,
1065 const fib_route_path_t *rpath,
1066 fib_path_list_flags_t *pl_flags,
1067 fib_entry_src_t *esrc)
1069 if ((esrc->fes_src == FIB_SOURCE_API) ||
1070 (esrc->fes_src == FIB_SOURCE_CLI))
1072 if (fib_path_is_attached(rpath))
1074 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1078 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1081 if (fib_route_attached_cross_table(fib_entry, rpath))
1083 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1087 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1092 * fib_entry_src_action_add
1094 * Adding a source can result in a new fib_entry being created, which
1095 * can inturn mean the pool is realloc'd and thus the entry passed as
1096 * an argument it also realloc'd
1100 fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1101 fib_source_t source,
1102 fib_entry_flag_t flags,
1103 const fib_route_path_t *rpath)
1105 fib_node_index_t old_path_list, fib_entry_index;
1106 fib_path_list_flags_t pl_flags;
1107 fib_entry_src_t *esrc;
1110 * save variable so we can recover from a fib_entry realloc.
1112 fib_entry_index = fib_entry_get_index(fib_entry);
1114 esrc = fib_entry_src_find(fib_entry, source, NULL);
1118 fib_entry_src_action_add(fib_entry,
1122 fib_entry_get_dpo_proto(fib_entry)));
1123 esrc = fib_entry_src_find(fib_entry, source, NULL);
1127 * we are no doubt modifying a path-list. If the path-list
1128 * is shared, and hence not modifiable, then the index returned
1129 * will be for a different path-list. This FIB entry to needs
1130 * to maintain its lock appropriately.
1132 old_path_list = esrc->fes_pl;
1134 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1136 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1137 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1139 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1140 fib_entry = fib_entry_get(fib_entry_index);
1142 fib_path_list_lock(esrc->fes_pl);
1143 fib_path_list_unlock(old_path_list);
1149 * fib_entry_src_action_swap
1151 * The source is providing new paths to replace the old ones.
1152 * Adding a source can result in a new fib_entry being created, which
1153 * can inturn mean the pool is realloc'd and thus the entry passed as
1154 * an argument it also realloc'd
1158 fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1159 fib_source_t source,
1160 fib_entry_flag_t flags,
1161 const fib_route_path_t *rpaths)
1163 fib_node_index_t old_path_list, fib_entry_index;
1164 fib_path_list_flags_t pl_flags;
1165 const fib_route_path_t *rpath;
1166 fib_entry_src_t *esrc;
1168 esrc = fib_entry_src_find(fib_entry, source, NULL);
1171 * save variable so we can recover from a fib_entry realloc.
1173 fib_entry_index = fib_entry_get_index(fib_entry);
1177 fib_entry = fib_entry_src_action_add(fib_entry,
1181 fib_entry_get_dpo_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 fib_entry = fib_entry_get(fib_entry_index);
1208 fib_path_list_lock(esrc->fes_pl);
1209 fib_path_list_unlock(old_path_list);
1214 fib_entry_src_flag_t
1215 fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1216 fib_source_t source,
1217 const fib_route_path_t *rpath)
1219 fib_path_list_flags_t pl_flags;
1220 fib_node_index_t old_path_list;
1221 fib_entry_src_t *esrc;
1223 esrc = fib_entry_src_find(fib_entry, source, NULL);
1225 ASSERT(NULL != esrc);
1226 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1229 * we no doubt modifying a path-list. If the path-list
1230 * is shared, and hence not modifiable, then the index returned
1231 * will be for a different path-list. This FIB entry to needs
1232 * to maintain its lock appropriately.
1234 old_path_list = esrc->fes_pl;
1236 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1238 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1239 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1241 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1244 * lock the new path-list, unlock the old if it had one
1246 fib_path_list_unlock(old_path_list);
1248 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1249 fib_path_list_lock(esrc->fes_pl);
1250 return (FIB_ENTRY_SRC_FLAG_ADDED);
1255 * no more paths left from this source
1257 fib_entry_src_action_remove(fib_entry, source);
1258 return (FIB_ENTRY_SRC_FLAG_NONE);
1263 fib_entry_src_format (fib_entry_t *fib_entry,
1264 fib_source_t source,
1267 fib_entry_src_t *esrc;
1269 esrc = fib_entry_src_find(fib_entry, source, NULL);
1271 if (NULL != fib_entry_src_vft[source].fesv_format)
1273 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1279 fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1280 fib_source_t source)
1282 fib_entry_t *fib_entry;
1283 fib_entry_src_t *esrc;
1285 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1286 return (ADJ_INDEX_INVALID);
1288 fib_entry = fib_entry_get(fib_entry_index);
1289 esrc = fib_entry_src_find(fib_entry, source, NULL);
1293 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1295 return (fib_path_list_get_adj(
1297 fib_entry_get_default_chain_type(fib_entry)));
1300 return (ADJ_INDEX_INVALID);
1304 fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1305 fib_source_t source,
1308 fib_entry_t *fib_entry;
1309 fib_entry_src_t *esrc;
1311 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1314 fib_entry = fib_entry_get(fib_entry_index);
1315 esrc = fib_entry_src_find(fib_entry, source, NULL);
1319 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1321 fib_path_list_contribute_forwarding(
1323 fib_entry_get_default_chain_type(fib_entry),
1326 return (dpo_id_is_valid(dpo));
1333 fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1334 fib_source_t source)
1336 fib_entry_t *fib_entry;
1337 fib_entry_src_t *esrc;
1339 fib_entry = fib_entry_get(entry_index);
1341 esrc = fib_entry_src_find(fib_entry, source, NULL);
1345 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1347 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1354 fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1355 fib_source_t source)
1357 fib_entry_t *fib_entry;
1358 fib_entry_src_t *esrc;
1360 fib_entry = fib_entry_get(entry_index);
1362 esrc = fib_entry_src_find(fib_entry, source, NULL);
1366 return (esrc->fes_entry_flags);
1369 return (FIB_ENTRY_FLAG_NONE);
1373 fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1375 fib_entry_flag_t flags;
1378 * the vector of sources is deliberately arranged in priority order
1380 if (0 == vec_len(fib_entry->fe_srcs))
1382 flags = FIB_ENTRY_FLAG_NONE;
1386 fib_entry_src_t *esrc;
1388 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1389 flags = esrc->fes_entry_flags;
1396 fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1397 fib_source_t source,
1400 fib_entry_t *fib_entry;
1401 fib_entry_src_t *esrc;
1403 fib_entry = fib_entry_get(fib_entry_index);
1404 esrc = fib_entry_src_find(fib_entry, source, NULL);
1407 NULL != fib_entry_src_vft[source].fesv_set_data)
1409 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1414 fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1415 fib_source_t source)
1417 fib_entry_t *fib_entry;
1418 fib_entry_src_t *esrc;
1420 fib_entry = fib_entry_get(fib_entry_index);
1421 esrc = fib_entry_src_find(fib_entry, source, NULL);
1424 NULL != fib_entry_src_vft[source].fesv_get_data)
1426 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1432 fib_entry_src_module_init (void)
1434 fib_entry_src_rr_register();
1435 fib_entry_src_interface_register();
1436 fib_entry_src_default_route_register();
1437 fib_entry_src_special_register();
1438 fib_entry_src_api_register();
1439 fib_entry_src_adj_register();
1440 fib_entry_src_mpls_register();
1441 fib_entry_src_lisp_register();