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 <vlib/vlib.h>
17 #include <vnet/ip/format.h>
18 #include <vnet/ip/lookup.h>
19 #include <vnet/adj/adj.h>
20 #include <vnet/dpo/load_balance.h>
21 #include <vnet/dpo/drop_dpo.h>
23 #include <vnet/fib/fib_entry.h>
24 #include <vnet/fib/fib_walk.h>
25 #include <vnet/fib/fib_entry_src.h>
26 #include <vnet/fib/fib_entry_cover.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/fib/fib_internal.h>
29 #include <vnet/fib/fib_attached_export.h>
30 #include <vnet/fib/fib_path_ext.h>
33 * Array of strings/names for the FIB sources
35 static const char *fib_source_names[] = FIB_SOURCES;
36 static const char *fib_attribute_names[] = FIB_ENTRY_ATTRIBUTES;
39 * Pool for all fib_entries
41 static fib_entry_t *fib_entry_pool;
44 fib_entry_get (fib_node_index_t index)
46 return (pool_elt_at_index(fib_entry_pool, index));
50 fib_entry_get_node (fib_node_index_t index)
52 return ((fib_node_t*)fib_entry_get(index));
56 fib_entry_get_index (const fib_entry_t * fib_entry)
58 return (fib_entry - fib_entry_pool);
62 fib_entry_get_proto (const fib_entry_t * fib_entry)
64 return (fib_entry->fe_prefix.fp_proto);
68 fib_entry_get_dpo_proto (const fib_entry_t * fib_entry)
70 return (fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto));
73 fib_forward_chain_type_t
74 fib_entry_get_default_chain_type (const fib_entry_t *fib_entry)
76 switch (fib_entry->fe_prefix.fp_proto)
78 case FIB_PROTOCOL_IP4:
79 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
80 case FIB_PROTOCOL_IP6:
81 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
82 case FIB_PROTOCOL_MPLS:
83 if (MPLS_EOS == fib_entry->fe_prefix.fp_eos)
84 return (FIB_FORW_CHAIN_TYPE_MPLS_EOS);
86 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
89 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
93 format_fib_source (u8 * s, va_list * args)
95 fib_source_t source = va_arg (*args, int);
97 s = format (s, "src:%s", fib_source_names[source]);
103 format_fib_entry (u8 * s, va_list * args)
105 fib_forward_chain_type_t fct;
106 fib_entry_attribute_t attr;
107 fib_entry_t *fib_entry;
108 fib_entry_src_t *src;
109 fib_node_index_t fei;
113 fei = va_arg (*args, fib_node_index_t);
114 level = va_arg (*args, int);
115 fib_entry = fib_entry_get(fei);
117 s = format (s, "%U", format_fib_prefix, &fib_entry->fe_prefix);
119 if (level >= FIB_ENTRY_FORMAT_DETAIL)
121 s = format (s, " fib:%d", fib_entry->fe_fib_index);
122 s = format (s, " index:%d", fib_entry_get_index(fib_entry));
123 s = format (s, " locks:%d", fib_entry->fe_node.fn_locks);
125 FOR_EACH_SRC_ADDED(fib_entry, src, source,
127 s = format (s, "\n %U", format_fib_source, source);
128 s = fib_entry_src_format(fib_entry, source, s);
129 s = format (s, " refs:%d ", src->fes_ref_count);
130 if (FIB_ENTRY_FLAG_NONE != src->fes_entry_flags) {
131 s = format(s, "flags:");
132 FOR_EACH_FIB_ATTRIBUTE(attr) {
133 if ((1<<attr) & src->fes_entry_flags) {
134 s = format (s, "%s,", fib_attribute_names[attr]);
138 s = format (s, "\n");
139 if (FIB_NODE_INDEX_INVALID != src->fes_pl)
141 s = fib_path_list_format(src->fes_pl, s);
143 s = format(s, "%U", format_fib_path_ext_list, &src->fes_path_exts);
146 s = format (s, "\n forwarding: ");
150 s = format (s, "\n");
153 fct = fib_entry_get_default_chain_type(fib_entry);
155 if (!dpo_id_is_valid(&fib_entry->fe_lb))
157 s = format (s, " UNRESOLVED\n");
162 s = format(s, " %U-chain\n %U",
163 format_fib_forw_chain_type, fct,
169 if (level >= FIB_ENTRY_FORMAT_DETAIL2)
171 fib_entry_delegate_type_t fdt;
172 fib_entry_delegate_t *fed;
174 s = format (s, " Delegates:\n");
175 FOR_EACH_DELEGATE(fib_entry, fdt, fed,
177 s = format(s, " %U\n", format_fib_entry_deletegate, fed);
182 if (level >= FIB_ENTRY_FORMAT_DETAIL2)
184 s = format(s, " Children:");
185 s = fib_node_children_format(fib_entry->fe_node.fn_children, s);
192 fib_entry_from_fib_node (fib_node_t *node)
195 ASSERT(FIB_NODE_TYPE_ENTRY == node->fn_type);
197 return ((fib_entry_t*)node);
201 fib_entry_last_lock_gone (fib_node_t *node)
203 fib_entry_delegate_type_t fdt;
204 fib_entry_delegate_t *fed;
205 fib_entry_t *fib_entry;
207 fib_entry = fib_entry_from_fib_node(node);
209 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
211 dpo_reset(&fed->fd_dpo);
212 fib_entry_delegate_remove(fib_entry, fdt);
215 FIB_ENTRY_DBG(fib_entry, "last-lock");
217 fib_node_deinit(&fib_entry->fe_node);
218 // FIXME -RR Backwalk
220 ASSERT(0 == vec_len(fib_entry->fe_delegates));
221 vec_free(fib_entry->fe_delegates);
222 vec_free(fib_entry->fe_srcs);
223 pool_put(fib_entry_pool, fib_entry);
226 static fib_entry_src_t*
227 fib_entry_get_best_src_i (const fib_entry_t *fib_entry)
229 fib_entry_src_t *bsrc;
232 * the enum of sources is deliberately arranged in priority order
234 if (0 == vec_len(fib_entry->fe_srcs))
240 bsrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
247 fib_entry_src_get_source (const fib_entry_src_t *esrc)
251 return (esrc->fes_src);
253 return (FIB_SOURCE_MAX);
256 static fib_entry_flag_t
257 fib_entry_src_get_flags (const fib_entry_src_t *esrc)
261 return (esrc->fes_entry_flags);
263 return (FIB_ENTRY_FLAG_NONE);
267 fib_entry_get_flags (fib_node_index_t fib_entry_index)
269 return (fib_entry_get_flags_i(fib_entry_get(fib_entry_index)));
273 * fib_entry_back_walk_notify
275 * A back walk has reach this entry.
277 static fib_node_back_walk_rc_t
278 fib_entry_back_walk_notify (fib_node_t *node,
279 fib_node_back_walk_ctx_t *ctx)
281 fib_entry_t *fib_entry;
283 fib_entry = fib_entry_from_fib_node(node);
285 if (FIB_NODE_BW_REASON_FLAG_EVALUATE & ctx->fnbw_reason ||
286 FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE & ctx->fnbw_reason ||
287 FIB_NODE_BW_REASON_FLAG_ADJ_DOWN & ctx->fnbw_reason ||
288 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP & ctx->fnbw_reason ||
289 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN & ctx->fnbw_reason ||
290 FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE & ctx->fnbw_reason)
292 fib_entry_src_action_reactivate(fib_entry,
293 fib_entry_get_best_source(
294 fib_entry_get_index(fib_entry)));
298 * all other walk types can be reclassifed to a re-evaluate to
299 * all recursive dependents.
300 * By reclassifying we ensure that should any of these walk types meet
301 * they can be merged.
303 ctx->fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
306 * ... and nothing is forced sync from now on.
308 ctx->fnbw_flags &= ~FIB_NODE_BW_FLAG_FORCE_SYNC;
311 * propagate the backwalk further if we haven't already reached the
314 fib_walk_sync(FIB_NODE_TYPE_ENTRY,
315 fib_entry_get_index(fib_entry),
318 return (FIB_NODE_BACK_WALK_CONTINUE);
322 fib_entry_show_memory (void)
324 u32 n_srcs = 0, n_exts = 0;
325 fib_entry_src_t *esrc;
328 fib_show_memory_usage("Entry",
329 pool_elts(fib_entry_pool),
330 pool_len(fib_entry_pool),
331 sizeof(fib_entry_t));
333 pool_foreach(entry, fib_entry_pool,
335 n_srcs += vec_len(entry->fe_srcs);
336 vec_foreach(esrc, entry->fe_srcs)
338 n_exts += fib_path_ext_list_length(&esrc->fes_path_exts);
342 fib_show_memory_usage("Entry Source",
343 n_srcs, n_srcs, sizeof(fib_entry_src_t));
344 fib_show_memory_usage("Entry Path-Extensions",
346 sizeof(fib_path_ext_t));
350 * The FIB path-list's graph node virtual function table
352 static const fib_node_vft_t fib_entry_vft = {
353 .fnv_get = fib_entry_get_node,
354 .fnv_last_lock = fib_entry_last_lock_gone,
355 .fnv_back_walk = fib_entry_back_walk_notify,
356 .fnv_mem_show = fib_entry_show_memory,
360 * @brief Contribute the set of Adjacencies that this entry forwards with
361 * to build the uRPF list of its children
364 fib_entry_contribute_urpf (fib_node_index_t entry_index,
367 fib_entry_t *fib_entry;
369 fib_entry = fib_entry_get(entry_index);
371 return (fib_path_list_contribute_urpf(fib_entry->fe_parent, urpf));
375 * If the client is request a chain for multicast forwarding then swap
376 * the chain type to one that can provide such transport.
378 static fib_forward_chain_type_t
379 fib_entry_chain_type_mcast_to_ucast (fib_forward_chain_type_t fct)
383 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
384 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
386 * we can only transport IP multicast packets if there is an
389 fct = FIB_FORW_CHAIN_TYPE_MPLS_EOS;
391 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
392 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
393 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
394 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
395 case FIB_FORW_CHAIN_TYPE_ETHERNET:
396 case FIB_FORW_CHAIN_TYPE_NSH:
397 case FIB_FORW_CHAIN_TYPE_BIER:
405 * fib_entry_contribute_forwarding
407 * Get an lock the forwarding information (DPO) contributed by the FIB entry.
410 fib_entry_contribute_forwarding (fib_node_index_t fib_entry_index,
411 fib_forward_chain_type_t fct,
414 fib_entry_delegate_t *fed;
415 fib_entry_t *fib_entry;
417 fib_entry = fib_entry_get(fib_entry_index);
420 * mfib children ask for mcast chains. fix these to the appropriate ucast types.
422 fct = fib_entry_chain_type_mcast_to_ucast(fct);
424 if (fct == fib_entry_get_default_chain_type(fib_entry))
426 dpo_copy(dpo, &fib_entry->fe_lb);
430 fed = fib_entry_delegate_get(fib_entry,
431 fib_entry_chain_type_to_delegate_type(fct));
435 fed = fib_entry_delegate_find_or_add(
437 fib_entry_chain_type_to_delegate_type(fct));
439 * on-demand create eos/non-eos.
440 * There is no on-demand delete because:
441 * - memory versus complexity & reliability:
442 * leaving unrequired [n]eos LB arounds wastes memory, cleaning
443 * then up on the right trigger is more code. i favour the latter.
445 fib_entry_src_mk_lb(fib_entry,
446 fib_entry_get_best_src_i(fib_entry),
451 dpo_copy(dpo, &fed->fd_dpo);
454 * don't allow the special index indicating replicate.vs.load-balance
455 * to escape to the clients
457 dpo->dpoi_index &= ~MPLS_IS_REPLICATE;
461 fib_entry_contribute_ip_forwarding (fib_node_index_t fib_entry_index)
463 fib_forward_chain_type_t fct;
464 fib_entry_t *fib_entry;
466 fib_entry = fib_entry_get(fib_entry_index);
467 fct = fib_entry_get_default_chain_type(fib_entry);
469 ASSERT((fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP4 ||
470 fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP6));
472 return (&fib_entry->fe_lb);
476 fib_entry_get_adj (fib_node_index_t fib_entry_index)
480 dpo = fib_entry_contribute_ip_forwarding(fib_entry_index);
482 if (dpo_id_is_valid(dpo))
484 dpo = load_balance_get_bucket(dpo->dpoi_index, 0);
488 return (dpo->dpoi_index);
491 return (ADJ_INDEX_INVALID);
495 fib_entry_get_path_list (fib_node_index_t fib_entry_index)
497 fib_entry_t *fib_entry;
499 fib_entry = fib_entry_get(fib_entry_index);
501 return (fib_entry->fe_parent);
505 fib_entry_child_add (fib_node_index_t fib_entry_index,
506 fib_node_type_t child_type,
507 fib_node_index_t child_index)
509 return (fib_node_child_add(FIB_NODE_TYPE_ENTRY,
516 fib_entry_child_remove (fib_node_index_t fib_entry_index,
519 fib_node_child_remove(FIB_NODE_TYPE_ENTRY,
523 if (0 == fib_node_get_n_children(FIB_NODE_TYPE_ENTRY,
527 * if there are no children left then there is no reason to keep
528 * the non-default forwarding chains. those chains are built only
529 * because the children want them.
531 fib_entry_delegate_type_t fdt;
532 fib_entry_delegate_t *fed;
533 fib_entry_t *fib_entry;
535 fib_entry = fib_entry_get(fib_entry_index);
537 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
539 dpo_reset(&fed->fd_dpo);
540 fib_entry_delegate_remove(fib_entry, fdt);
546 fib_entry_alloc (u32 fib_index,
547 const fib_prefix_t *prefix,
548 fib_node_index_t *fib_entry_index)
550 fib_entry_t *fib_entry;
553 pool_get(fib_entry_pool, fib_entry);
554 memset(fib_entry, 0, sizeof(*fib_entry));
556 fib_node_init(&fib_entry->fe_node,
557 FIB_NODE_TYPE_ENTRY);
559 fib_entry->fe_fib_index = fib_index;
562 * the one time we need to update the const prefix is when
563 * the entry is first created
565 fep = (fib_prefix_t*)&(fib_entry->fe_prefix);
568 if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
571 if (MPLS_NON_EOS == fep->fp_eos)
573 fep->fp_payload_proto = DPO_PROTO_MPLS;
575 ASSERT(DPO_PROTO_NONE != fib_entry->fe_prefix.fp_payload_proto);
578 dpo_reset(&fib_entry->fe_lb);
580 *fib_entry_index = fib_entry_get_index(fib_entry);
582 FIB_ENTRY_DBG(fib_entry, "alloc");
588 fib_entry_post_flag_update_actions (fib_entry_t *fib_entry,
590 fib_entry_flag_t old_flags)
592 fib_node_index_t fei;
595 * save the index so we can recover from pool reallocs
597 fei = fib_entry_get_index(fib_entry);
600 * handle changes to attached export for import entries
602 int is_import = (FIB_ENTRY_FLAG_IMPORT & fib_entry_get_flags_i(fib_entry));
603 int was_import = (FIB_ENTRY_FLAG_IMPORT & old_flags);
605 if (!was_import && is_import)
608 * transition from not exported to exported
612 * there is an assumption here that the entry resolves via only
613 * one interface and that it is the cross VRF interface.
615 u32 sw_if_index = fib_path_list_get_resolving_interface(fib_entry->fe_parent);
617 fib_attached_export_import(fib_entry,
618 fib_table_get_index_for_sw_if_index(
619 fib_entry_get_proto(fib_entry),
622 else if (was_import && !is_import)
625 * transition from exported to not exported
627 fib_attached_export_purge(fib_entry);
631 * no change. nothing to do.
635 * reload the entry address post possible pool realloc
637 fib_entry = fib_entry_get(fei);
640 * handle changes to attached export for export entries
642 int is_attached = (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(fib_entry));
643 int was_attached = (FIB_ENTRY_FLAG_ATTACHED & old_flags);
645 if (!was_attached && is_attached)
648 * transition to attached. time to export
658 fib_entry_post_install_actions (fib_entry_t *fib_entry,
660 fib_entry_flag_t old_flags)
662 fib_entry = fib_entry_post_flag_update_actions(fib_entry,
665 fib_entry_src_action_installed(fib_entry, source);
669 fib_entry_create (u32 fib_index,
670 const fib_prefix_t *prefix,
672 fib_entry_flag_t flags,
673 const fib_route_path_t *paths)
675 fib_node_index_t fib_entry_index;
676 fib_entry_t *fib_entry;
678 ASSERT(0 < vec_len(paths));
680 fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
683 * since this is a new entry create, we don't need to check for winning
684 * sources - there is only one.
686 fib_entry = fib_entry_src_action_add(fib_entry, source, flags,
689 fib_entry_get_proto(fib_entry))));
690 fib_entry_src_action_path_swap(fib_entry,
695 * handle possible realloc's by refetching the pointer
697 fib_entry = fib_entry_get(fib_entry_index);
698 fib_entry_src_action_activate(fib_entry, source);
700 fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE);
702 return (fib_entry_index);
706 fib_entry_create_special (u32 fib_index,
707 const fib_prefix_t *prefix,
709 fib_entry_flag_t flags,
712 fib_node_index_t fib_entry_index;
713 fib_entry_t *fib_entry;
716 * create and initiliase the new enty
718 fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
721 * create the path-list
723 fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
724 fib_entry_src_action_activate(fib_entry, source);
726 fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE);
728 return (fib_entry_index);
732 fib_entry_post_update_actions (fib_entry_t *fib_entry,
734 fib_entry_flag_t old_flags)
737 * backwalk to children to inform then of the change to forwarding.
739 fib_node_back_walk_ctx_t bw_ctx = {
740 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
743 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_get_index(fib_entry), &bw_ctx);
746 * then inform any covered prefixes
748 fib_entry_cover_update_notify(fib_entry);
750 fib_entry_post_install_actions(fib_entry, source, old_flags);
754 fib_entry_source_change (fib_entry_t *fib_entry,
755 fib_source_t best_source,
756 fib_source_t new_source,
757 fib_entry_flag_t old_flags)
760 * if the path list for the source passed is invalid,
761 * then we need to create a new one. else we are updating
764 if (new_source < best_source)
767 * we have a new winning source.
769 fib_entry_src_action_deactivate(fib_entry, best_source);
770 fib_entry_src_action_activate(fib_entry, new_source);
772 else if (new_source > best_source)
775 * the new source loses. nothing to do here.
776 * the data from the source is saved in the path-list created
783 * the new source is one this entry already has.
784 * But the path-list was updated, which will contribute new forwarding,
787 fib_entry_src_action_deactivate(fib_entry, new_source);
788 fib_entry_src_action_activate(fib_entry, new_source);
791 fib_entry_post_update_actions(fib_entry, new_source, old_flags);
795 fib_entry_special_add (fib_node_index_t fib_entry_index,
797 fib_entry_flag_t flags,
800 fib_source_t best_source;
801 fib_entry_flag_t bflags;
802 fib_entry_t *fib_entry;
803 fib_entry_src_t *bsrc;
805 fib_entry = fib_entry_get(fib_entry_index);
807 bsrc = fib_entry_get_best_src_i(fib_entry);
808 best_source = fib_entry_src_get_source(bsrc);
809 bflags = fib_entry_src_get_flags(bsrc);
811 fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
812 fib_entry_source_change(fib_entry, best_source, source, bflags);
816 fib_entry_special_update (fib_node_index_t fib_entry_index,
818 fib_entry_flag_t flags,
821 fib_source_t best_source;
822 fib_entry_flag_t bflags;
823 fib_entry_t *fib_entry;
824 fib_entry_src_t *bsrc;
826 fib_entry = fib_entry_get(fib_entry_index);
828 bsrc = fib_entry_get_best_src_i(fib_entry);
829 best_source = fib_entry_src_get_source(bsrc);
830 bflags = fib_entry_src_get_flags(bsrc);
832 fib_entry = fib_entry_src_action_update(fib_entry, source, flags, dpo);
833 fib_entry_source_change(fib_entry, best_source, source, bflags);
838 fib_entry_path_add (fib_node_index_t fib_entry_index,
840 fib_entry_flag_t flags,
841 const fib_route_path_t *rpath)
843 fib_source_t best_source;
844 fib_entry_flag_t bflags;
845 fib_entry_t *fib_entry;
846 fib_entry_src_t *bsrc;
848 ASSERT(1 == vec_len(rpath));
850 fib_entry = fib_entry_get(fib_entry_index);
851 ASSERT(NULL != fib_entry);
853 bsrc = fib_entry_get_best_src_i(fib_entry);
854 best_source = fib_entry_src_get_source(bsrc);
855 bflags = fib_entry_src_get_flags(bsrc);
857 fib_entry = fib_entry_src_action_path_add(fib_entry, source, flags, rpath);
860 * if the path list for the source passed is invalid,
861 * then we need to create a new one. else we are updating
864 if (source < best_source)
867 * we have a new winning source.
869 fib_entry_src_action_deactivate(fib_entry, best_source);
870 fib_entry_src_action_activate(fib_entry, source);
872 else if (source > best_source)
875 * the new source loses. nothing to do here.
876 * the data from the source is saved in the path-list created
883 * the new source is one this entry already has.
884 * But the path-list was updated, which will contribute new forwarding,
887 fib_entry_src_action_deactivate(fib_entry, source);
888 fib_entry_src_action_activate(fib_entry, source);
891 fib_entry_post_update_actions(fib_entry, source, bflags);
895 * fib_entry_path_remove
897 * remove a path from the entry.
898 * return the fib_entry's index if it is still present, INVALID otherwise.
901 fib_entry_path_remove (fib_node_index_t fib_entry_index,
903 const fib_route_path_t *rpath)
905 fib_entry_src_flag_t sflag;
906 fib_source_t best_source;
907 fib_entry_flag_t bflags;
908 fib_entry_t *fib_entry;
909 fib_entry_src_t *bsrc;
911 ASSERT(1 == vec_len(rpath));
913 fib_entry = fib_entry_get(fib_entry_index);
914 ASSERT(NULL != fib_entry);
916 bsrc = fib_entry_get_best_src_i(fib_entry);
917 best_source = fib_entry_src_get_source(bsrc);
918 bflags = fib_entry_src_get_flags(bsrc);
920 sflag = fib_entry_src_action_path_remove(fib_entry, source, rpath);
923 * if the path list for the source passed is invalid,
924 * then we need to create a new one. else we are updating
927 if (source < best_source )
930 * Que! removing a path from a source that is better than the
931 * one this entry is using.
935 else if (source > best_source )
938 * the source is not the best. nothing to do.
940 return (FIB_ENTRY_SRC_FLAG_ADDED);
945 * removing a path from the path-list we were using.
947 if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
950 * the last path from the source was removed.
951 * fallback to lower source
953 bsrc = fib_entry_get_best_src_i(fib_entry);
954 best_source = fib_entry_src_get_source(bsrc);
956 if (FIB_SOURCE_MAX == best_source) {
958 * no more sources left. this entry is toast.
960 fib_entry = fib_entry_post_flag_update_actions(fib_entry,
963 fib_entry_src_action_uninstall(fib_entry);
965 return (FIB_ENTRY_SRC_FLAG_NONE);
969 fib_entry_src_action_activate(fib_entry, best_source);
970 source = best_source;
976 * re-install the new forwarding information
978 fib_entry_src_action_deactivate(fib_entry, source);
979 fib_entry_src_action_activate(fib_entry, source);
983 fib_entry_post_update_actions(fib_entry, source, bflags);
988 return (FIB_ENTRY_SRC_FLAG_ADDED);
992 * fib_entry_special_remove
994 * remove a special source from the entry.
995 * return the fib_entry's index if it is still present, INVALID otherwise.
998 fib_entry_special_remove (fib_node_index_t fib_entry_index,
1001 fib_entry_src_flag_t sflag;
1002 fib_source_t best_source;
1003 fib_entry_flag_t bflags;
1004 fib_entry_t *fib_entry;
1005 fib_entry_src_t *bsrc;
1007 fib_entry = fib_entry_get(fib_entry_index);
1008 ASSERT(NULL != fib_entry);
1010 bsrc = fib_entry_get_best_src_i(fib_entry);
1011 best_source = fib_entry_src_get_source(bsrc);
1012 bflags = fib_entry_src_get_flags(bsrc);
1014 sflag = fib_entry_src_action_remove(fib_entry, source);
1017 * if the path list for the source passed is invalid,
1018 * then we need to create a new one. else we are updating
1021 if (source < best_source )
1024 * Que! removing a path from a source that is better than the
1025 * one this entry is using. This can only mean it is a source
1026 * this prefix does not have.
1028 return (FIB_ENTRY_SRC_FLAG_ADDED);
1030 else if (source > best_source ) {
1032 * the source is not the best. nothing to do.
1034 return (FIB_ENTRY_SRC_FLAG_ADDED);
1038 if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
1041 * the source was removed. use the next best.
1043 bsrc = fib_entry_get_best_src_i(fib_entry);
1044 best_source = fib_entry_src_get_source(bsrc);
1046 if (FIB_SOURCE_MAX == best_source) {
1048 * no more sources left. this entry is toast.
1050 fib_entry = fib_entry_post_flag_update_actions(fib_entry,
1053 fib_entry_src_action_uninstall(fib_entry);
1055 return (FIB_ENTRY_SRC_FLAG_NONE);
1059 fib_entry_src_action_activate(fib_entry, best_source);
1060 source = best_source;
1066 * re-install the new forwarding information
1068 fib_entry_src_action_reactivate(fib_entry, source);
1072 fib_entry_post_update_actions(fib_entry, source, bflags);
1075 * still have sources
1077 return (FIB_ENTRY_SRC_FLAG_ADDED);
1083 * The source is withdrawing all the paths it provided
1085 fib_entry_src_flag_t
1086 fib_entry_delete (fib_node_index_t fib_entry_index,
1087 fib_source_t source)
1089 return (fib_entry_special_remove(fib_entry_index, source));
1095 * The source has provided a new set of paths that will replace the old.
1098 fib_entry_update (fib_node_index_t fib_entry_index,
1099 fib_source_t source,
1100 fib_entry_flag_t flags,
1101 const fib_route_path_t *paths)
1103 fib_source_t best_source;
1104 fib_entry_flag_t bflags;
1105 fib_entry_t *fib_entry;
1106 fib_entry_src_t *bsrc;
1108 fib_entry = fib_entry_get(fib_entry_index);
1109 ASSERT(NULL != fib_entry);
1111 bsrc = fib_entry_get_best_src_i(fib_entry);
1112 best_source = fib_entry_src_get_source(bsrc);
1113 bflags = fib_entry_src_get_flags(bsrc);
1115 fib_entry_src_action_path_swap(fib_entry,
1120 * handle possible realloc's by refetching the pointer
1122 fib_entry = fib_entry_get(fib_entry_index);
1125 * if the path list for the source passed is invalid,
1126 * then we need to create a new one. else we are updating
1129 if (source < best_source)
1132 * we have a new winning source.
1134 fib_entry_src_action_deactivate(fib_entry, best_source);
1135 fib_entry_src_action_activate(fib_entry, source);
1137 else if (source > best_source) {
1139 * the new source loses. nothing to do here.
1140 * the data from the source is saved in the path-list created
1147 * the new source is one this entry already has.
1148 * But the path-list was updated, which will contribute new forwarding,
1151 fib_entry_src_action_deactivate(fib_entry, source);
1152 fib_entry_src_action_activate(fib_entry, source);
1155 fib_entry_post_update_actions(fib_entry, source, bflags);
1160 * fib_entry_cover_changed
1162 * this entry is tracking its cover and that cover has changed.
1165 fib_entry_cover_changed (fib_node_index_t fib_entry_index)
1167 fib_entry_src_cover_res_t res = {
1169 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
1171 fib_source_t source, best_source;
1172 fib_entry_flag_t bflags;
1173 fib_entry_t *fib_entry;
1174 fib_entry_src_t *esrc;
1177 bflags = FIB_ENTRY_FLAG_NONE;
1178 best_source = FIB_SOURCE_FIRST;
1179 fib_entry = fib_entry_get(fib_entry_index);
1181 fib_attached_export_cover_change(fib_entry);
1184 * propagate the notificuation to each of the added sources
1187 FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
1192 * only the best source gets to set the back walk flags
1194 res = fib_entry_src_action_cover_change(fib_entry, source);
1195 bflags = fib_entry_src_get_flags(esrc);
1196 best_source = fib_entry_src_get_source(esrc);
1200 fib_entry_src_action_cover_change(fib_entry, source);
1207 fib_entry_src_action_reactivate(fib_entry,
1208 fib_entry_src_get_source(
1209 fib_entry_get_best_src_i(fib_entry)));
1210 fib_entry_post_install_actions(fib_entry, best_source, bflags);
1214 fib_entry_src_action_uninstall(fib_entry);
1217 if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
1220 * time for walkies fido.
1222 fib_node_back_walk_ctx_t bw_ctx = {
1223 .fnbw_reason = res.bw_reason,
1226 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
1231 * fib_entry_cover_updated
1233 * this entry is tracking its cover and that cover has been updated
1234 * (i.e. its forwarding information has changed).
1237 fib_entry_cover_updated (fib_node_index_t fib_entry_index)
1239 fib_entry_src_cover_res_t res = {
1241 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
1243 fib_source_t source, best_source;
1244 fib_entry_flag_t bflags;
1245 fib_entry_t *fib_entry;
1246 fib_entry_src_t *esrc;
1249 bflags = FIB_ENTRY_FLAG_NONE;
1250 best_source = FIB_SOURCE_FIRST;
1251 fib_entry = fib_entry_get(fib_entry_index);
1253 fib_attached_export_cover_update(fib_entry);
1256 * propagate the notificuation to each of the added sources
1259 FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
1264 * only the best source gets to set the back walk flags
1266 res = fib_entry_src_action_cover_update(fib_entry, source);
1267 bflags = fib_entry_src_get_flags(esrc);
1268 best_source = fib_entry_src_get_source(esrc);
1272 fib_entry_src_action_cover_update(fib_entry, source);
1279 fib_entry_src_action_reactivate(fib_entry,
1280 fib_entry_src_get_source(
1281 fib_entry_get_best_src_i(fib_entry)));
1282 fib_entry_post_install_actions(fib_entry, best_source, bflags);
1286 fib_entry_src_action_uninstall(fib_entry);
1289 if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
1292 * time for walkies fido.
1294 fib_node_back_walk_ctx_t bw_ctx = {
1295 .fnbw_reason = res.bw_reason,
1298 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
1303 fib_entry_recursive_loop_detect (fib_node_index_t entry_index,
1304 fib_node_index_t **entry_indicies)
1306 fib_entry_t *fib_entry;
1307 int was_looped, is_looped;
1309 fib_entry = fib_entry_get(entry_index);
1311 if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
1313 fib_node_index_t *entries = *entry_indicies;
1315 vec_add1(entries, entry_index);
1316 was_looped = fib_path_list_is_looped(fib_entry->fe_parent);
1317 is_looped = fib_path_list_recursive_loop_detect(fib_entry->fe_parent,
1320 *entry_indicies = entries;
1322 if (!!was_looped != !!is_looped)
1325 * re-evaluate all the entry's forwarding
1326 * NOTE: this is an inplace modify
1328 fib_entry_delegate_type_t fdt;
1329 fib_entry_delegate_t *fed;
1331 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
1333 fib_entry_src_mk_lb(fib_entry,
1334 fib_entry_get_best_src_i(fib_entry),
1335 fib_entry_delegate_type_to_chain_type(fdt),
1343 * the entry is currently not linked to a path-list. this happens
1344 * when it is this entry that is re-linking path-lists and has thus
1354 fib_entry_get_resolving_interface (fib_node_index_t entry_index)
1356 fib_entry_t *fib_entry;
1358 fib_entry = fib_entry_get(entry_index);
1360 return (fib_path_list_get_resolving_interface(fib_entry->fe_parent));
1364 fib_entry_get_best_source (fib_node_index_t entry_index)
1366 fib_entry_t *fib_entry;
1367 fib_entry_src_t *bsrc;
1369 fib_entry = fib_entry_get(entry_index);
1371 bsrc = fib_entry_get_best_src_i(fib_entry);
1372 return (fib_entry_src_get_source(bsrc));
1376 * Return !0 is the entry is reoslved, i.e. will return a valid forwarding
1380 fib_entry_is_resolved (fib_node_index_t fib_entry_index)
1382 fib_entry_delegate_t *fed;
1383 fib_entry_t *fib_entry;
1385 fib_entry = fib_entry_get(fib_entry_index);
1387 fed = fib_entry_delegate_get(fib_entry, FIB_ENTRY_DELEGATE_BFD);
1392 * no BFD tracking - consider it resolved.
1399 * defer to the state of the BFD tracking
1401 return (FIB_BFD_STATE_UP == fed->fd_bfd_state);
1406 fib_entry_set_flow_hash_config (fib_node_index_t fib_entry_index,
1407 flow_hash_config_t hash_config)
1409 fib_entry_t *fib_entry;
1411 fib_entry = fib_entry_get(fib_entry_index);
1414 * pass the hash-config on to the load-balance object where it is cached.
1415 * we can ignore LBs in the delegate chains, since they will not be of the
1416 * correct protocol type (i.e. they are not IP)
1417 * There's no way, nor need, to change the hash config for MPLS.
1419 if (dpo_id_is_valid(&fib_entry->fe_lb))
1423 ASSERT(DPO_LOAD_BALANCE == fib_entry->fe_lb.dpoi_type);
1425 lb = load_balance_get(fib_entry->fe_lb.dpoi_index);
1428 * atomic update for packets in flight
1430 lb->lb_hash_config = hash_config;
1435 fib_ip4_address_compare (const ip4_address_t * a1,
1436 const ip4_address_t * a2)
1439 * IP addresses are unsiged ints. the return value here needs to be signed
1440 * a simple subtraction won't cut it.
1441 * If the addresses are the same, the sort order is undefiend, so phoey.
1443 return ((clib_net_to_host_u32(a1->data_u32) >
1444 clib_net_to_host_u32(a2->data_u32) ) ?
1449 fib_ip6_address_compare (const ip6_address_t * a1,
1450 const ip6_address_t * a2)
1453 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1455 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1456 clib_net_to_host_u16 (a2->as_u16[i]));
1464 fib_entry_cmp (fib_node_index_t fib_entry_index1,
1465 fib_node_index_t fib_entry_index2)
1467 fib_entry_t *fib_entry1, *fib_entry2;
1470 fib_entry1 = fib_entry_get(fib_entry_index1);
1471 fib_entry2 = fib_entry_get(fib_entry_index2);
1473 switch (fib_entry1->fe_prefix.fp_proto)
1475 case FIB_PROTOCOL_IP4:
1476 cmp = fib_ip4_address_compare(&fib_entry1->fe_prefix.fp_addr.ip4,
1477 &fib_entry2->fe_prefix.fp_addr.ip4);
1479 case FIB_PROTOCOL_IP6:
1480 cmp = fib_ip6_address_compare(&fib_entry1->fe_prefix.fp_addr.ip6,
1481 &fib_entry2->fe_prefix.fp_addr.ip6);
1483 case FIB_PROTOCOL_MPLS:
1484 cmp = (fib_entry1->fe_prefix.fp_label - fib_entry2->fe_prefix.fp_label);
1488 cmp = (fib_entry1->fe_prefix.fp_eos - fib_entry2->fe_prefix.fp_eos);
1494 cmp = (fib_entry1->fe_prefix.fp_len - fib_entry2->fe_prefix.fp_len);
1500 fib_entry_cmp_for_sort (void *i1, void *i2)
1502 fib_node_index_t *fib_entry_index1 = i1, *fib_entry_index2 = i2;
1504 return (fib_entry_cmp(*fib_entry_index1,
1505 *fib_entry_index2));
1509 fib_entry_lock (fib_node_index_t fib_entry_index)
1511 fib_entry_t *fib_entry;
1513 fib_entry = fib_entry_get(fib_entry_index);
1515 fib_node_lock(&fib_entry->fe_node);
1519 fib_entry_unlock (fib_node_index_t fib_entry_index)
1521 fib_entry_t *fib_entry;
1523 fib_entry = fib_entry_get(fib_entry_index);
1525 fib_node_unlock(&fib_entry->fe_node);
1529 fib_entry_module_init (void)
1531 fib_node_register_type (FIB_NODE_TYPE_ENTRY, &fib_entry_vft);
1535 fib_entry_encode (fib_node_index_t fib_entry_index,
1536 fib_route_path_encode_t **api_rpaths)
1538 fib_entry_t *fib_entry;
1540 fib_entry = fib_entry_get(fib_entry_index);
1541 if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
1543 fib_path_list_walk(fib_entry->fe_parent, fib_path_encode, api_rpaths);
1548 fib_entry_get_prefix (fib_node_index_t fib_entry_index,
1551 fib_entry_t *fib_entry;
1553 fib_entry = fib_entry_get(fib_entry_index);
1554 *pfx = fib_entry->fe_prefix;
1558 fib_entry_get_fib_index (fib_node_index_t fib_entry_index)
1560 fib_entry_t *fib_entry;
1562 fib_entry = fib_entry_get(fib_entry_index);
1564 return (fib_entry->fe_fib_index);
1568 fib_entry_pool_size (void)
1570 return (pool_elts(fib_entry_pool));
1573 static clib_error_t *
1574 show_fib_entry_command (vlib_main_t * vm,
1575 unformat_input_t * input,
1576 vlib_cli_command_t * cmd)
1578 fib_node_index_t fei;
1580 if (unformat (input, "%d", &fei))
1583 * show one in detail
1585 if (!pool_is_free_index(fib_entry_pool, fei))
1587 vlib_cli_output (vm, "%d@%U",
1589 format_fib_entry, fei,
1590 FIB_ENTRY_FORMAT_DETAIL2);
1594 vlib_cli_output (vm, "entry %d invalid", fei);
1602 vlib_cli_output (vm, "FIB Entries:");
1603 pool_foreach_index(fei, fib_entry_pool,
1605 vlib_cli_output (vm, "%d@%U",
1607 format_fib_entry, fei,
1608 FIB_ENTRY_FORMAT_BRIEF);
1615 VLIB_CLI_COMMAND (show_fib_entry, static) = {
1616 .path = "show fib entry",
1617 .function = show_fib_entry_command,
1618 .short_help = "show fib entry",