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:
404 * fib_entry_contribute_forwarding
406 * Get an lock the forwarding information (DPO) contributed by the FIB entry.
409 fib_entry_contribute_forwarding (fib_node_index_t fib_entry_index,
410 fib_forward_chain_type_t fct,
413 fib_entry_delegate_t *fed;
414 fib_entry_t *fib_entry;
416 fib_entry = fib_entry_get(fib_entry_index);
419 * mfib children ask for mcast chains. fix these to the appropriate ucast types.
421 fct = fib_entry_chain_type_mcast_to_ucast(fct);
423 if (fct == fib_entry_get_default_chain_type(fib_entry))
425 dpo_copy(dpo, &fib_entry->fe_lb);
429 fed = fib_entry_delegate_get(fib_entry,
430 fib_entry_chain_type_to_delegate_type(fct));
434 fed = fib_entry_delegate_find_or_add(
436 fib_entry_chain_type_to_delegate_type(fct));
438 * on-demand create eos/non-eos.
439 * There is no on-demand delete because:
440 * - memory versus complexity & reliability:
441 * leaving unrequired [n]eos LB arounds wastes memory, cleaning
442 * then up on the right trigger is more code. i favour the latter.
444 fib_entry_src_mk_lb(fib_entry,
445 fib_entry_get_best_src_i(fib_entry),
450 dpo_copy(dpo, &fed->fd_dpo);
453 * don't allow the special index indicating replicate.vs.load-balance
454 * to escape to the clients
456 dpo->dpoi_index &= ~MPLS_IS_REPLICATE;
460 fib_entry_contribute_ip_forwarding (fib_node_index_t fib_entry_index)
462 fib_forward_chain_type_t fct;
463 fib_entry_t *fib_entry;
465 fib_entry = fib_entry_get(fib_entry_index);
466 fct = fib_entry_get_default_chain_type(fib_entry);
468 ASSERT((fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP4 ||
469 fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP6));
471 return (&fib_entry->fe_lb);
475 fib_entry_get_adj (fib_node_index_t fib_entry_index)
479 dpo = fib_entry_contribute_ip_forwarding(fib_entry_index);
481 if (dpo_id_is_valid(dpo))
483 dpo = load_balance_get_bucket(dpo->dpoi_index, 0);
487 return (dpo->dpoi_index);
490 return (ADJ_INDEX_INVALID);
494 fib_entry_get_path_list (fib_node_index_t fib_entry_index)
496 fib_entry_t *fib_entry;
498 fib_entry = fib_entry_get(fib_entry_index);
500 return (fib_entry->fe_parent);
504 fib_entry_child_add (fib_node_index_t fib_entry_index,
505 fib_node_type_t child_type,
506 fib_node_index_t child_index)
508 return (fib_node_child_add(FIB_NODE_TYPE_ENTRY,
515 fib_entry_child_remove (fib_node_index_t fib_entry_index,
518 fib_node_child_remove(FIB_NODE_TYPE_ENTRY,
522 if (0 == fib_node_get_n_children(FIB_NODE_TYPE_ENTRY,
526 * if there are no children left then there is no reason to keep
527 * the non-default forwarding chains. those chains are built only
528 * because the children want them.
530 fib_entry_delegate_type_t fdt;
531 fib_entry_delegate_t *fed;
532 fib_entry_t *fib_entry;
534 fib_entry = fib_entry_get(fib_entry_index);
536 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
538 dpo_reset(&fed->fd_dpo);
539 fib_entry_delegate_remove(fib_entry, fdt);
545 fib_entry_alloc (u32 fib_index,
546 const fib_prefix_t *prefix,
547 fib_node_index_t *fib_entry_index)
549 fib_entry_t *fib_entry;
552 pool_get(fib_entry_pool, fib_entry);
553 memset(fib_entry, 0, sizeof(*fib_entry));
555 fib_node_init(&fib_entry->fe_node,
556 FIB_NODE_TYPE_ENTRY);
558 fib_entry->fe_fib_index = fib_index;
561 * the one time we need to update the const prefix is when
562 * the entry is first created
564 fep = (fib_prefix_t*)&(fib_entry->fe_prefix);
567 if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
570 if (MPLS_NON_EOS == fep->fp_eos)
572 fep->fp_payload_proto = DPO_PROTO_MPLS;
574 ASSERT(DPO_PROTO_NONE != fib_entry->fe_prefix.fp_payload_proto);
577 dpo_reset(&fib_entry->fe_lb);
579 *fib_entry_index = fib_entry_get_index(fib_entry);
581 FIB_ENTRY_DBG(fib_entry, "alloc");
587 fib_entry_post_flag_update_actions (fib_entry_t *fib_entry,
589 fib_entry_flag_t old_flags)
591 fib_node_index_t fei;
594 * save the index so we can recover from pool reallocs
596 fei = fib_entry_get_index(fib_entry);
599 * handle changes to attached export for import entries
601 int is_import = (FIB_ENTRY_FLAG_IMPORT & fib_entry_get_flags_i(fib_entry));
602 int was_import = (FIB_ENTRY_FLAG_IMPORT & old_flags);
604 if (!was_import && is_import)
607 * transition from not exported to exported
611 * there is an assumption here that the entry resolves via only
612 * one interface and that it is the cross VRF interface.
614 u32 sw_if_index = fib_path_list_get_resolving_interface(fib_entry->fe_parent);
616 fib_attached_export_import(fib_entry,
617 fib_table_get_index_for_sw_if_index(
618 fib_entry_get_proto(fib_entry),
621 else if (was_import && !is_import)
624 * transition from exported to not exported
626 fib_attached_export_purge(fib_entry);
630 * no change. nothing to do.
634 * reload the entry address post possible pool realloc
636 fib_entry = fib_entry_get(fei);
639 * handle changes to attached export for export entries
641 int is_attached = (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(fib_entry));
642 int was_attached = (FIB_ENTRY_FLAG_ATTACHED & old_flags);
644 if (!was_attached && is_attached)
647 * transition to attached. time to export
657 fib_entry_post_install_actions (fib_entry_t *fib_entry,
659 fib_entry_flag_t old_flags)
661 fib_entry = fib_entry_post_flag_update_actions(fib_entry,
664 fib_entry_src_action_installed(fib_entry, source);
668 fib_entry_create (u32 fib_index,
669 const fib_prefix_t *prefix,
671 fib_entry_flag_t flags,
672 const fib_route_path_t *paths)
674 fib_node_index_t fib_entry_index;
675 fib_entry_t *fib_entry;
677 ASSERT(0 < vec_len(paths));
679 fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
682 * since this is a new entry create, we don't need to check for winning
683 * sources - there is only one.
685 fib_entry = fib_entry_src_action_add(fib_entry, source, flags,
688 fib_entry_get_proto(fib_entry))));
689 fib_entry_src_action_path_swap(fib_entry,
694 * handle possible realloc's by refetching the pointer
696 fib_entry = fib_entry_get(fib_entry_index);
697 fib_entry_src_action_activate(fib_entry, source);
699 fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE);
701 return (fib_entry_index);
705 fib_entry_create_special (u32 fib_index,
706 const fib_prefix_t *prefix,
708 fib_entry_flag_t flags,
711 fib_node_index_t fib_entry_index;
712 fib_entry_t *fib_entry;
715 * create and initiliase the new enty
717 fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
720 * create the path-list
722 fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
723 fib_entry_src_action_activate(fib_entry, source);
725 fib_entry_post_install_actions(fib_entry, source, FIB_ENTRY_FLAG_NONE);
727 return (fib_entry_index);
731 fib_entry_post_update_actions (fib_entry_t *fib_entry,
733 fib_entry_flag_t old_flags)
736 * backwalk to children to inform then of the change to forwarding.
738 fib_node_back_walk_ctx_t bw_ctx = {
739 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
742 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_get_index(fib_entry), &bw_ctx);
745 * then inform any covered prefixes
747 fib_entry_cover_update_notify(fib_entry);
749 fib_entry_post_install_actions(fib_entry, source, old_flags);
753 fib_entry_source_change (fib_entry_t *fib_entry,
754 fib_source_t best_source,
755 fib_source_t new_source,
756 fib_entry_flag_t old_flags)
759 * if the path list for the source passed is invalid,
760 * then we need to create a new one. else we are updating
763 if (new_source < best_source)
766 * we have a new winning source.
768 fib_entry_src_action_deactivate(fib_entry, best_source);
769 fib_entry_src_action_activate(fib_entry, new_source);
771 else if (new_source > best_source)
774 * the new source loses. nothing to do here.
775 * the data from the source is saved in the path-list created
782 * the new source is one this entry already has.
783 * But the path-list was updated, which will contribute new forwarding,
786 fib_entry_src_action_deactivate(fib_entry, new_source);
787 fib_entry_src_action_activate(fib_entry, new_source);
790 fib_entry_post_update_actions(fib_entry, new_source, old_flags);
794 fib_entry_special_add (fib_node_index_t fib_entry_index,
796 fib_entry_flag_t flags,
799 fib_source_t best_source;
800 fib_entry_flag_t bflags;
801 fib_entry_t *fib_entry;
802 fib_entry_src_t *bsrc;
804 fib_entry = fib_entry_get(fib_entry_index);
806 bsrc = fib_entry_get_best_src_i(fib_entry);
807 best_source = fib_entry_src_get_source(bsrc);
808 bflags = fib_entry_src_get_flags(bsrc);
810 fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
811 fib_entry_source_change(fib_entry, best_source, source, bflags);
815 fib_entry_special_update (fib_node_index_t fib_entry_index,
817 fib_entry_flag_t flags,
820 fib_source_t best_source;
821 fib_entry_flag_t bflags;
822 fib_entry_t *fib_entry;
823 fib_entry_src_t *bsrc;
825 fib_entry = fib_entry_get(fib_entry_index);
827 bsrc = fib_entry_get_best_src_i(fib_entry);
828 best_source = fib_entry_src_get_source(bsrc);
829 bflags = fib_entry_src_get_flags(bsrc);
831 fib_entry = fib_entry_src_action_update(fib_entry, source, flags, dpo);
832 fib_entry_source_change(fib_entry, best_source, source, bflags);
837 fib_entry_path_add (fib_node_index_t fib_entry_index,
839 fib_entry_flag_t flags,
840 const fib_route_path_t *rpath)
842 fib_source_t best_source;
843 fib_entry_flag_t bflags;
844 fib_entry_t *fib_entry;
845 fib_entry_src_t *bsrc;
847 ASSERT(1 == vec_len(rpath));
849 fib_entry = fib_entry_get(fib_entry_index);
850 ASSERT(NULL != fib_entry);
852 bsrc = fib_entry_get_best_src_i(fib_entry);
853 best_source = fib_entry_src_get_source(bsrc);
854 bflags = fib_entry_src_get_flags(bsrc);
856 fib_entry = fib_entry_src_action_path_add(fib_entry, source, flags, rpath);
859 * if the path list for the source passed is invalid,
860 * then we need to create a new one. else we are updating
863 if (source < best_source)
866 * we have a new winning source.
868 fib_entry_src_action_deactivate(fib_entry, best_source);
869 fib_entry_src_action_activate(fib_entry, source);
871 else if (source > best_source)
874 * the new source loses. nothing to do here.
875 * the data from the source is saved in the path-list created
882 * the new source is one this entry already has.
883 * But the path-list was updated, which will contribute new forwarding,
886 fib_entry_src_action_deactivate(fib_entry, source);
887 fib_entry_src_action_activate(fib_entry, source);
890 fib_entry_post_update_actions(fib_entry, source, bflags);
894 * fib_entry_path_remove
896 * remove a path from the entry.
897 * return the fib_entry's index if it is still present, INVALID otherwise.
900 fib_entry_path_remove (fib_node_index_t fib_entry_index,
902 const fib_route_path_t *rpath)
904 fib_entry_src_flag_t sflag;
905 fib_source_t best_source;
906 fib_entry_flag_t bflags;
907 fib_entry_t *fib_entry;
908 fib_entry_src_t *bsrc;
910 ASSERT(1 == vec_len(rpath));
912 fib_entry = fib_entry_get(fib_entry_index);
913 ASSERT(NULL != fib_entry);
915 bsrc = fib_entry_get_best_src_i(fib_entry);
916 best_source = fib_entry_src_get_source(bsrc);
917 bflags = fib_entry_src_get_flags(bsrc);
919 sflag = fib_entry_src_action_path_remove(fib_entry, source, rpath);
922 * if the path list for the source passed is invalid,
923 * then we need to create a new one. else we are updating
926 if (source < best_source )
929 * Que! removing a path from a source that is better than the
930 * one this entry is using.
934 else if (source > best_source )
937 * the source is not the best. nothing to do.
939 return (FIB_ENTRY_SRC_FLAG_ADDED);
944 * removing a path from the path-list we were using.
946 if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
949 * the last path from the source was removed.
950 * fallback to lower source
952 bsrc = fib_entry_get_best_src_i(fib_entry);
953 best_source = fib_entry_src_get_source(bsrc);
955 if (FIB_SOURCE_MAX == best_source) {
957 * no more sources left. this entry is toast.
959 fib_entry = fib_entry_post_flag_update_actions(fib_entry,
962 fib_entry_src_action_uninstall(fib_entry);
964 return (FIB_ENTRY_SRC_FLAG_NONE);
968 fib_entry_src_action_activate(fib_entry, best_source);
969 source = best_source;
975 * re-install the new forwarding information
977 fib_entry_src_action_deactivate(fib_entry, source);
978 fib_entry_src_action_activate(fib_entry, source);
982 fib_entry_post_update_actions(fib_entry, source, bflags);
987 return (FIB_ENTRY_SRC_FLAG_ADDED);
991 * fib_entry_special_remove
993 * remove a special source from the entry.
994 * return the fib_entry's index if it is still present, INVALID otherwise.
997 fib_entry_special_remove (fib_node_index_t fib_entry_index,
1000 fib_entry_src_flag_t sflag;
1001 fib_source_t best_source;
1002 fib_entry_flag_t bflags;
1003 fib_entry_t *fib_entry;
1004 fib_entry_src_t *bsrc;
1006 fib_entry = fib_entry_get(fib_entry_index);
1007 ASSERT(NULL != fib_entry);
1009 bsrc = fib_entry_get_best_src_i(fib_entry);
1010 best_source = fib_entry_src_get_source(bsrc);
1011 bflags = fib_entry_src_get_flags(bsrc);
1013 sflag = fib_entry_src_action_remove(fib_entry, source);
1016 * if the path list for the source passed is invalid,
1017 * then we need to create a new one. else we are updating
1020 if (source < best_source )
1023 * Que! removing a path from a source that is better than the
1024 * one this entry is using. This can only mean it is a source
1025 * this prefix does not have.
1027 return (FIB_ENTRY_SRC_FLAG_ADDED);
1029 else if (source > best_source ) {
1031 * the source is not the best. nothing to do.
1033 return (FIB_ENTRY_SRC_FLAG_ADDED);
1037 if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
1040 * the source was removed. use the next best.
1042 bsrc = fib_entry_get_best_src_i(fib_entry);
1043 best_source = fib_entry_src_get_source(bsrc);
1045 if (FIB_SOURCE_MAX == best_source) {
1047 * no more sources left. this entry is toast.
1049 fib_entry = fib_entry_post_flag_update_actions(fib_entry,
1052 fib_entry_src_action_uninstall(fib_entry);
1054 return (FIB_ENTRY_SRC_FLAG_NONE);
1058 fib_entry_src_action_activate(fib_entry, best_source);
1059 source = best_source;
1065 * re-install the new forwarding information
1067 fib_entry_src_action_reactivate(fib_entry, source);
1071 fib_entry_post_update_actions(fib_entry, source, bflags);
1074 * still have sources
1076 return (FIB_ENTRY_SRC_FLAG_ADDED);
1082 * The source is withdrawing all the paths it provided
1084 fib_entry_src_flag_t
1085 fib_entry_delete (fib_node_index_t fib_entry_index,
1086 fib_source_t source)
1088 return (fib_entry_special_remove(fib_entry_index, source));
1094 * The source has provided a new set of paths that will replace the old.
1097 fib_entry_update (fib_node_index_t fib_entry_index,
1098 fib_source_t source,
1099 fib_entry_flag_t flags,
1100 const fib_route_path_t *paths)
1102 fib_source_t best_source;
1103 fib_entry_flag_t bflags;
1104 fib_entry_t *fib_entry;
1105 fib_entry_src_t *bsrc;
1107 fib_entry = fib_entry_get(fib_entry_index);
1108 ASSERT(NULL != fib_entry);
1110 bsrc = fib_entry_get_best_src_i(fib_entry);
1111 best_source = fib_entry_src_get_source(bsrc);
1112 bflags = fib_entry_src_get_flags(bsrc);
1114 fib_entry_src_action_path_swap(fib_entry,
1119 * handle possible realloc's by refetching the pointer
1121 fib_entry = fib_entry_get(fib_entry_index);
1124 * if the path list for the source passed is invalid,
1125 * then we need to create a new one. else we are updating
1128 if (source < best_source)
1131 * we have a new winning source.
1133 fib_entry_src_action_deactivate(fib_entry, best_source);
1134 fib_entry_src_action_activate(fib_entry, source);
1136 else if (source > best_source) {
1138 * the new source loses. nothing to do here.
1139 * the data from the source is saved in the path-list created
1146 * the new source is one this entry already has.
1147 * But the path-list was updated, which will contribute new forwarding,
1150 fib_entry_src_action_deactivate(fib_entry, source);
1151 fib_entry_src_action_activate(fib_entry, source);
1154 fib_entry_post_update_actions(fib_entry, source, bflags);
1159 * fib_entry_cover_changed
1161 * this entry is tracking its cover and that cover has changed.
1164 fib_entry_cover_changed (fib_node_index_t fib_entry_index)
1166 fib_entry_src_cover_res_t res = {
1168 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
1170 fib_source_t source, best_source;
1171 fib_entry_flag_t bflags;
1172 fib_entry_t *fib_entry;
1173 fib_entry_src_t *esrc;
1176 bflags = FIB_ENTRY_FLAG_NONE;
1177 best_source = FIB_SOURCE_FIRST;
1178 fib_entry = fib_entry_get(fib_entry_index);
1180 fib_attached_export_cover_change(fib_entry);
1183 * propagate the notificuation to each of the added sources
1186 FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
1191 * only the best source gets to set the back walk flags
1193 res = fib_entry_src_action_cover_change(fib_entry, source);
1194 bflags = fib_entry_src_get_flags(esrc);
1195 best_source = fib_entry_src_get_source(esrc);
1199 fib_entry_src_action_cover_change(fib_entry, source);
1206 fib_entry_src_action_reactivate(fib_entry,
1207 fib_entry_src_get_source(
1208 fib_entry_get_best_src_i(fib_entry)));
1209 fib_entry_post_install_actions(fib_entry, best_source, bflags);
1213 fib_entry_src_action_uninstall(fib_entry);
1216 if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
1219 * time for walkies fido.
1221 fib_node_back_walk_ctx_t bw_ctx = {
1222 .fnbw_reason = res.bw_reason,
1225 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
1230 * fib_entry_cover_updated
1232 * this entry is tracking its cover and that cover has been updated
1233 * (i.e. its forwarding information has changed).
1236 fib_entry_cover_updated (fib_node_index_t fib_entry_index)
1238 fib_entry_src_cover_res_t res = {
1240 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
1242 fib_source_t source, best_source;
1243 fib_entry_flag_t bflags;
1244 fib_entry_t *fib_entry;
1245 fib_entry_src_t *esrc;
1248 bflags = FIB_ENTRY_FLAG_NONE;
1249 best_source = FIB_SOURCE_FIRST;
1250 fib_entry = fib_entry_get(fib_entry_index);
1252 fib_attached_export_cover_update(fib_entry);
1255 * propagate the notificuation to each of the added sources
1258 FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
1263 * only the best source gets to set the back walk flags
1265 res = fib_entry_src_action_cover_update(fib_entry, source);
1266 bflags = fib_entry_src_get_flags(esrc);
1267 best_source = fib_entry_src_get_source(esrc);
1271 fib_entry_src_action_cover_update(fib_entry, source);
1278 fib_entry_src_action_reactivate(fib_entry,
1279 fib_entry_src_get_source(
1280 fib_entry_get_best_src_i(fib_entry)));
1281 fib_entry_post_install_actions(fib_entry, best_source, bflags);
1285 fib_entry_src_action_uninstall(fib_entry);
1288 if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
1291 * time for walkies fido.
1293 fib_node_back_walk_ctx_t bw_ctx = {
1294 .fnbw_reason = res.bw_reason,
1297 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
1302 fib_entry_recursive_loop_detect (fib_node_index_t entry_index,
1303 fib_node_index_t **entry_indicies)
1305 fib_entry_t *fib_entry;
1306 int was_looped, is_looped;
1308 fib_entry = fib_entry_get(entry_index);
1310 if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
1312 fib_node_index_t *entries = *entry_indicies;
1314 vec_add1(entries, entry_index);
1315 was_looped = fib_path_list_is_looped(fib_entry->fe_parent);
1316 is_looped = fib_path_list_recursive_loop_detect(fib_entry->fe_parent,
1319 *entry_indicies = entries;
1321 if (!!was_looped != !!is_looped)
1324 * re-evaluate all the entry's forwarding
1325 * NOTE: this is an inplace modify
1327 fib_entry_delegate_type_t fdt;
1328 fib_entry_delegate_t *fed;
1330 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
1332 fib_entry_src_mk_lb(fib_entry,
1333 fib_entry_get_best_src_i(fib_entry),
1334 fib_entry_delegate_type_to_chain_type(fdt),
1342 * the entry is currently not linked to a path-list. this happens
1343 * when it is this entry that is re-linking path-lists and has thus
1353 fib_entry_get_resolving_interface (fib_node_index_t entry_index)
1355 fib_entry_t *fib_entry;
1357 fib_entry = fib_entry_get(entry_index);
1359 return (fib_path_list_get_resolving_interface(fib_entry->fe_parent));
1363 fib_entry_get_best_source (fib_node_index_t entry_index)
1365 fib_entry_t *fib_entry;
1366 fib_entry_src_t *bsrc;
1368 fib_entry = fib_entry_get(entry_index);
1370 bsrc = fib_entry_get_best_src_i(fib_entry);
1371 return (fib_entry_src_get_source(bsrc));
1375 * Return !0 is the entry is reoslved, i.e. will return a valid forwarding
1379 fib_entry_is_resolved (fib_node_index_t fib_entry_index)
1381 fib_entry_delegate_t *fed;
1382 fib_entry_t *fib_entry;
1384 fib_entry = fib_entry_get(fib_entry_index);
1386 fed = fib_entry_delegate_get(fib_entry, FIB_ENTRY_DELEGATE_BFD);
1391 * no BFD tracking - consider it resolved.
1398 * defer to the state of the BFD tracking
1400 return (FIB_BFD_STATE_UP == fed->fd_bfd_state);
1405 fib_entry_set_flow_hash_config (fib_node_index_t fib_entry_index,
1406 flow_hash_config_t hash_config)
1408 fib_entry_t *fib_entry;
1410 fib_entry = fib_entry_get(fib_entry_index);
1413 * pass the hash-config on to the load-balance object where it is cached.
1414 * we can ignore LBs in the delegate chains, since they will not be of the
1415 * correct protocol type (i.e. they are not IP)
1416 * There's no way, nor need, to change the hash config for MPLS.
1418 if (dpo_id_is_valid(&fib_entry->fe_lb))
1422 ASSERT(DPO_LOAD_BALANCE == fib_entry->fe_lb.dpoi_type);
1424 lb = load_balance_get(fib_entry->fe_lb.dpoi_index);
1427 * atomic update for packets in flight
1429 lb->lb_hash_config = hash_config;
1434 fib_ip4_address_compare (const ip4_address_t * a1,
1435 const ip4_address_t * a2)
1438 * IP addresses are unsiged ints. the return value here needs to be signed
1439 * a simple subtraction won't cut it.
1440 * If the addresses are the same, the sort order is undefiend, so phoey.
1442 return ((clib_net_to_host_u32(a1->data_u32) >
1443 clib_net_to_host_u32(a2->data_u32) ) ?
1448 fib_ip6_address_compare (const ip6_address_t * a1,
1449 const ip6_address_t * a2)
1452 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1454 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1455 clib_net_to_host_u16 (a2->as_u16[i]));
1463 fib_entry_cmp (fib_node_index_t fib_entry_index1,
1464 fib_node_index_t fib_entry_index2)
1466 fib_entry_t *fib_entry1, *fib_entry2;
1469 fib_entry1 = fib_entry_get(fib_entry_index1);
1470 fib_entry2 = fib_entry_get(fib_entry_index2);
1472 switch (fib_entry1->fe_prefix.fp_proto)
1474 case FIB_PROTOCOL_IP4:
1475 cmp = fib_ip4_address_compare(&fib_entry1->fe_prefix.fp_addr.ip4,
1476 &fib_entry2->fe_prefix.fp_addr.ip4);
1478 case FIB_PROTOCOL_IP6:
1479 cmp = fib_ip6_address_compare(&fib_entry1->fe_prefix.fp_addr.ip6,
1480 &fib_entry2->fe_prefix.fp_addr.ip6);
1482 case FIB_PROTOCOL_MPLS:
1483 cmp = (fib_entry1->fe_prefix.fp_label - fib_entry2->fe_prefix.fp_label);
1487 cmp = (fib_entry1->fe_prefix.fp_eos - fib_entry2->fe_prefix.fp_eos);
1493 cmp = (fib_entry1->fe_prefix.fp_len - fib_entry2->fe_prefix.fp_len);
1499 fib_entry_cmp_for_sort (void *i1, void *i2)
1501 fib_node_index_t *fib_entry_index1 = i1, *fib_entry_index2 = i2;
1503 return (fib_entry_cmp(*fib_entry_index1,
1504 *fib_entry_index2));
1508 fib_entry_lock (fib_node_index_t fib_entry_index)
1510 fib_entry_t *fib_entry;
1512 fib_entry = fib_entry_get(fib_entry_index);
1514 fib_node_lock(&fib_entry->fe_node);
1518 fib_entry_unlock (fib_node_index_t fib_entry_index)
1520 fib_entry_t *fib_entry;
1522 fib_entry = fib_entry_get(fib_entry_index);
1524 fib_node_unlock(&fib_entry->fe_node);
1528 fib_entry_module_init (void)
1530 fib_node_register_type (FIB_NODE_TYPE_ENTRY, &fib_entry_vft);
1534 fib_entry_encode (fib_node_index_t fib_entry_index,
1535 fib_route_path_encode_t **api_rpaths)
1537 fib_entry_t *fib_entry;
1539 fib_entry = fib_entry_get(fib_entry_index);
1540 if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
1542 fib_path_list_walk(fib_entry->fe_parent, fib_path_encode, api_rpaths);
1547 fib_entry_get_prefix (fib_node_index_t fib_entry_index,
1550 fib_entry_t *fib_entry;
1552 fib_entry = fib_entry_get(fib_entry_index);
1553 *pfx = fib_entry->fe_prefix;
1557 fib_entry_get_fib_index (fib_node_index_t fib_entry_index)
1559 fib_entry_t *fib_entry;
1561 fib_entry = fib_entry_get(fib_entry_index);
1563 return (fib_entry->fe_fib_index);
1567 fib_entry_pool_size (void)
1569 return (pool_elts(fib_entry_pool));
1572 static clib_error_t *
1573 show_fib_entry_command (vlib_main_t * vm,
1574 unformat_input_t * input,
1575 vlib_cli_command_t * cmd)
1577 fib_node_index_t fei;
1579 if (unformat (input, "%d", &fei))
1582 * show one in detail
1584 if (!pool_is_free_index(fib_entry_pool, fei))
1586 vlib_cli_output (vm, "%d@%U",
1588 format_fib_entry, fei,
1589 FIB_ENTRY_FORMAT_DETAIL2);
1593 vlib_cli_output (vm, "entry %d invalid", fei);
1601 vlib_cli_output (vm, "FIB Entries:");
1602 pool_foreach_index(fei, fib_entry_pool,
1604 vlib_cli_output (vm, "%d@%U",
1606 format_fib_entry, fei,
1607 FIB_ENTRY_FORMAT_BRIEF);
1614 VLIB_CLI_COMMAND (show_fib_entry, static) = {
1615 .path = "show fib entry",
1616 .function = show_fib_entry_command,
1617 .short_help = "show fib entry",