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>
18 #include <vnet/mfib/mfib_entry.h>
19 #include <vnet/mfib/mfib_entry_src.h>
20 #include <vnet/mfib/mfib_entry_cover.h>
21 #include <vnet/fib/fib_path_list.h>
22 #include <vnet/fib/fib_walk.h>
24 #include <vnet/dpo/drop_dpo.h>
25 #include <vnet/dpo/replicate_dpo.h>
30 vlib_log_class_t mfib_entry_logger;
33 * Pool of path extensions
35 static mfib_path_ext_t *mfib_path_ext_pool;
38 * String names for each source
40 static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
43 * Pool for all fib_entries
45 mfib_entry_t *mfib_entry_pool;
48 mfib_entry_get_node (fib_node_index_t index)
50 return ((fib_node_t*)mfib_entry_get(index));
54 mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
56 return (mfib_entry->mfe_prefix.fp_proto);
59 fib_forward_chain_type_t
60 mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
62 switch (mfib_entry->mfe_prefix.fp_proto)
64 case FIB_PROTOCOL_IP4:
65 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
66 case FIB_PROTOCOL_IP6:
67 return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
68 case FIB_PROTOCOL_MPLS:
72 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
76 format_mfib_entry_dpo (u8 * s, va_list * args)
78 index_t fei = va_arg(*args, index_t);
79 CLIB_UNUSED(u32 indent) = va_arg(*args, u32);
81 return (format(s, "%U",
82 format_mfib_entry, fei,
83 MFIB_ENTRY_FORMAT_BRIEF));
86 static inline mfib_path_ext_t *
87 mfib_entry_path_ext_get (index_t mi)
89 return (pool_elt_at_index(mfib_path_ext_pool, mi));
93 format_mfib_entry_path_ext (u8 * s, va_list * args)
95 mfib_path_ext_t *path_ext;
96 index_t mpi = va_arg(*args, index_t);
98 path_ext = mfib_entry_path_ext_get(mpi);
99 return (format(s, "path:%d flags:%U",
101 format_mfib_itf_flags, path_ext->mfpe_flags));
105 format_mfib_entry (u8 * s, va_list * args)
107 fib_node_index_t fei, mfi;
108 mfib_entry_t *mfib_entry;
109 mfib_entry_src_t *msrc;
113 fei = va_arg (*args, fib_node_index_t);
114 level = va_arg (*args, int);
115 mfib_entry = mfib_entry_get(fei);
117 s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
118 s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);
120 if (level >= MFIB_ENTRY_FORMAT_DETAIL)
122 fib_node_index_t path_index, mpi;
124 s = format (s, "\n");
125 s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
126 s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
127 s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
128 vec_foreach(msrc, mfib_entry->mfe_srcs)
130 s = format (s, " src:%s locks:%d:",
131 mfib_source_names[msrc->mfes_src],
132 msrc->mfes_ref_count);
133 if (msrc->mfes_cover != FIB_NODE_INDEX_INVALID)
135 s = format (s, " cover:%d", msrc->mfes_cover);
137 s = format (s, " %U\n", format_mfib_entry_flags, msrc->mfes_flags);
138 if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
140 s = fib_path_list_format(msrc->mfes_pl, s);
142 s = format (s, " Extensions:\n");
143 hash_foreach(path_index, mpi, msrc->mfes_exts,
145 s = format(s, " %U\n", format_mfib_entry_path_ext, mpi);
147 s = format (s, " Interface-Forwarding:\n");
148 hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
150 s = format(s, " %U\n", format_mfib_itf, mfi);
155 s = format(s, "\n Interfaces:");
156 hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
158 s = format(s, "\n %U", format_mfib_itf, mfi);
160 if (MFIB_RPF_ID_NONE != mfib_entry->mfe_rpf_id)
162 s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id);
164 s = format(s, "\n %U-chain\n %U",
165 format_fib_forw_chain_type,
166 mfib_entry_get_default_chain_type(mfib_entry),
168 &mfib_entry->mfe_rep,
172 if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
174 s = format(s, "\nchildren:");
175 s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
182 mfib_entry_from_fib_node (fib_node_t *node)
184 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
185 return ((mfib_entry_t*)node);
189 mfib_entry_src_cmp_for_sort (void * v1,
192 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
194 return (esrc1->mfes_src - esrc2->mfes_src);
198 mfib_entry_src_init (mfib_entry_t *mfib_entry,
199 mfib_source_t source)
202 mfib_entry_src_t esrc = {
203 .mfes_pl = FIB_NODE_INDEX_INVALID,
204 .mfes_flags = MFIB_ENTRY_FLAG_NONE,
206 .mfes_cover = FIB_NODE_INDEX_INVALID,
207 .mfes_sibling = FIB_NODE_INDEX_INVALID,
211 vec_add1(mfib_entry->mfe_srcs, esrc);
212 vec_sort_with_function(mfib_entry->mfe_srcs,
213 mfib_entry_src_cmp_for_sort);
216 static mfib_entry_src_t *
217 mfib_entry_src_find (const mfib_entry_t *mfib_entry,
218 mfib_source_t source,
222 mfib_entry_src_t *esrc;
226 vec_foreach(esrc, mfib_entry->mfe_srcs)
228 if (esrc->mfes_src == source)
245 static mfib_entry_src_t *
246 mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
247 mfib_source_t source)
249 mfib_entry_src_t *msrc;
251 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
255 mfib_entry_src_init(mfib_entry, source);
256 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
262 static mfib_entry_src_t *
263 mfib_entry_src_update (mfib_entry_t *mfib_entry,
264 mfib_source_t source,
266 mfib_entry_flags_t entry_flags)
268 mfib_entry_src_t *msrc;
270 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
272 msrc->mfes_flags = entry_flags;
273 msrc->mfes_rpf_id = rpf_id;
278 static mfib_entry_src_t *
279 mfib_entry_src_update_and_lock (mfib_entry_t *mfib_entry,
280 mfib_source_t source,
282 mfib_entry_flags_t entry_flags)
284 mfib_entry_src_t *msrc;
286 msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
288 msrc->mfes_ref_count++;
294 mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
296 mfib_entry_src_t *bsrc;
299 * the enum of sources is deliberately arranged in priority order
301 if (0 == vec_len(mfib_entry->mfe_srcs))
307 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
314 mfib_entry_get_best_source (const mfib_entry_t *mfib_entry)
316 mfib_entry_src_t *bsrc;
318 bsrc = mfib_entry_get_best_src(mfib_entry);
320 return (bsrc->mfes_src);
324 mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
325 mfib_source_t source)
327 mfib_entry_t *mfib_entry;
329 mfib_entry = mfib_entry_get(mfib_entry_index);
331 return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
335 mfib_entry_is_host (fib_node_index_t mfib_entry_index)
337 return (mfib_prefix_is_host(mfib_entry_get_prefix(mfib_entry_index)));
342 mfib_entry_src_flush (mfib_entry_src_t *msrc)
347 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
349 mfib_itf_delete(mfib_itf_get(mfii));
351 hash_free(msrc->mfes_itfs);
352 msrc->mfes_itfs = NULL;
353 fib_path_list_unlock(msrc->mfes_pl);
357 mfib_entry_src_remove (mfib_entry_t *mfib_entry,
358 mfib_source_t source)
361 mfib_entry_src_t *msrc;
364 msrc = mfib_entry_src_find(mfib_entry, source, &index);
368 ASSERT(0 != msrc->mfes_ref_count);
369 msrc->mfes_ref_count--;
371 if (0 == msrc->mfes_ref_count)
373 mfib_entry_src_deactivate(mfib_entry, msrc);
374 mfib_entry_src_flush(msrc);
376 vec_del1(mfib_entry->mfe_srcs, index);
377 if (vec_len (mfib_entry->mfe_srcs) > 1)
378 vec_sort_with_function(mfib_entry->mfe_srcs,
379 mfib_entry_src_cmp_for_sort);
385 mfib_entry_child_add (fib_node_index_t mfib_entry_index,
386 fib_node_type_t child_type,
387 fib_node_index_t child_index)
389 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
396 mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
399 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
404 static mfib_entry_t *
405 mfib_entry_alloc (u32 fib_index,
406 const mfib_prefix_t *prefix,
407 fib_node_index_t *mfib_entry_index)
409 mfib_entry_t *mfib_entry;
411 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
413 fib_node_init(&mfib_entry->mfe_node,
414 FIB_NODE_TYPE_MFIB_ENTRY);
417 * Some of the members require non-default initialisation
418 * so we also init those that don't and thus save on the call to clib_memset.
420 mfib_entry->mfe_flags = 0;
421 mfib_entry->mfe_fib_index = fib_index;
422 mfib_entry->mfe_prefix = *prefix;
423 mfib_entry->mfe_srcs = NULL;
424 mfib_entry->mfe_itfs = NULL;
425 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
426 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
428 dpo_reset(&mfib_entry->mfe_rep);
430 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
432 MFIB_ENTRY_DBG(mfib_entry, "alloc");
437 static inline mfib_path_ext_t *
438 mfib_entry_path_ext_find (mfib_path_ext_t *exts,
439 fib_node_index_t path_index)
443 p = hash_get(exts, path_index);
447 return (mfib_entry_path_ext_get(p[0]));
453 static mfib_path_ext_t*
454 mfib_path_ext_add (mfib_entry_src_t *msrc,
455 fib_node_index_t path_index,
456 mfib_itf_flags_t mfi_flags)
458 mfib_path_ext_t *path_ext;
460 pool_get(mfib_path_ext_pool, path_ext);
462 path_ext->mfpe_flags = mfi_flags;
463 path_ext->mfpe_path = path_index;
465 hash_set(msrc->mfes_exts, path_index,
466 path_ext - mfib_path_ext_pool);
472 mfib_path_ext_remove (mfib_entry_src_t *msrc,
473 fib_node_index_t path_index)
475 mfib_path_ext_t *path_ext;
477 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
479 hash_unset(msrc->mfes_exts, path_index);
480 pool_put(mfib_path_ext_pool, path_ext);
483 typedef struct mfib_entry_collect_forwarding_ctx_t_
485 load_balance_path_t * next_hops;
486 fib_forward_chain_type_t fct;
487 mfib_entry_src_t *msrc;
488 } mfib_entry_collect_forwarding_ctx_t;
490 static fib_path_list_walk_rc_t
491 mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
492 fib_node_index_t path_index,
495 mfib_entry_collect_forwarding_ctx_t *ctx;
496 load_balance_path_t *nh;
501 * if the path is not resolved, don't include it.
503 if (!fib_path_is_resolved(path_index))
505 return (FIB_PATH_LIST_WALK_CONTINUE);
509 * If the path is not forwarding to use it
511 mfib_path_ext_t *path_ext;
513 path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
516 if (NULL != path_ext &&
517 !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
519 return (FIB_PATH_LIST_WALK_CONTINUE);
524 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
525 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
527 * EOS traffic with no label to stack, we need the IP Adj
529 vec_add2(ctx->next_hops, nh, 1);
531 nh->path_index = path_index;
532 nh->path_weight = fib_path_get_weight(path_index);
533 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
536 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
537 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
538 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
539 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
540 case FIB_FORW_CHAIN_TYPE_ETHERNET:
541 case FIB_FORW_CHAIN_TYPE_NSH:
542 case FIB_FORW_CHAIN_TYPE_BIER:
547 return (FIB_PATH_LIST_WALK_CONTINUE);
551 mfib_entry_stack (mfib_entry_t *mfib_entry,
552 mfib_entry_src_t *msrc)
556 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
559 * unlink the enty from the previous path list.
561 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
563 fib_path_list_child_remove(mfib_entry->mfe_pl,
564 mfib_entry->mfe_sibling);
569 mfib_entry_collect_forwarding_ctx_t ctx = {
571 .fct = mfib_entry_get_default_chain_type(mfib_entry),
576 * link the entry to the path-list.
577 * The entry needs to be a child so that we receive the back-walk
578 * updates to recalculate forwarding.
580 mfib_entry->mfe_pl = msrc->mfes_pl;
581 mfib_entry->mfe_flags = msrc->mfes_flags;
582 mfib_entry->mfe_itfs = msrc->mfes_itfs;
583 mfib_entry->mfe_rpf_id = msrc->mfes_rpf_id;
585 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
587 mfib_entry->mfe_sibling =
588 fib_path_list_child_add(mfib_entry->mfe_pl,
589 FIB_NODE_TYPE_MFIB_ENTRY,
590 mfib_entry_get_index(mfib_entry));
592 fib_path_list_walk(mfib_entry->mfe_pl,
593 mfib_entry_src_collect_forwarding,
596 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
598 if (NULL == ctx.next_hops)
601 * no next-hops, stack directly on the drop
603 dpo_stack(DPO_MFIB_ENTRY, dp,
604 &mfib_entry->mfe_rep,
610 * each path contirbutes a next-hop. form a replicate
611 * from those choices.
613 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
614 dpo_is_drop(&mfib_entry->mfe_rep))
616 dpo_id_t tmp_dpo = DPO_INVALID;
620 replicate_create(0, dp));
622 dpo_stack(DPO_MFIB_ENTRY, dp,
623 &mfib_entry->mfe_rep,
628 replicate_multipath_update(&mfib_entry->mfe_rep,
635 * for exclusive routes the source provided a replicate DPO
636 * which we stashed in the special path list with one path,
637 * so we can stack directly on that.
639 ASSERT(1 == vec_len(ctx.next_hops));
641 if (NULL != ctx.next_hops)
643 dpo_stack(DPO_MFIB_ENTRY, dp,
644 &mfib_entry->mfe_rep,
645 &ctx.next_hops[0].path_dpo);
646 dpo_reset(&ctx.next_hops[0].path_dpo);
647 vec_free(ctx.next_hops);
651 dpo_stack(DPO_MFIB_ENTRY, dp,
652 &mfib_entry->mfe_rep,
659 dpo_stack(DPO_MFIB_ENTRY, dp,
660 &mfib_entry->mfe_rep,
665 * time for walkies fido.
667 fib_node_back_walk_ctx_t bw_ctx = {
668 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
671 fib_walk_sync(FIB_NODE_TYPE_MFIB_ENTRY,
672 mfib_entry_get_index(mfib_entry),
676 static fib_node_index_t
677 mfib_entry_src_path_add (mfib_entry_src_t *msrc,
678 const fib_route_path_t *rpath)
680 fib_node_index_t path_index;
681 fib_route_path_t *rpaths;
683 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
686 * path-lists require a vector of paths
689 vec_add1(rpaths, rpath[0]);
691 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
693 /* A non-shared path-list */
694 msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
696 fib_path_list_lock(msrc->mfes_pl);
699 path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
706 static fib_node_index_t
707 mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
708 const fib_route_path_t *rpath)
710 fib_node_index_t path_index;
711 fib_route_path_t *rpaths;
713 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
716 * path-lists require a vector of paths
719 vec_add1(rpaths, rpath[0]);
721 path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
729 mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry,
730 mfib_source_t old_best)
732 mfib_entry_src_t *bsrc, *osrc;
735 * copy the forwarding data from the bast source
737 bsrc = mfib_entry_get_best_src(mfib_entry);
738 osrc = mfib_entry_src_find(mfib_entry, old_best, NULL);
742 if (bsrc->mfes_src != old_best)
745 * we are changing from one source to another
746 * deactivate the old, and activate the new
748 mfib_entry_src_deactivate(mfib_entry, osrc);
749 mfib_entry_src_activate(mfib_entry, bsrc);
754 mfib_entry_src_deactivate(mfib_entry, osrc);
757 mfib_entry_stack(mfib_entry, bsrc);
758 mfib_entry_cover_update_notify(mfib_entry);
763 mfib_entry_create (u32 fib_index,
764 mfib_source_t source,
765 const mfib_prefix_t *prefix,
767 mfib_entry_flags_t entry_flags,
770 fib_node_index_t mfib_entry_index;
771 mfib_entry_t *mfib_entry;
772 mfib_entry_src_t *msrc;
774 mfib_entry = mfib_entry_alloc(fib_index, prefix,
776 msrc = mfib_entry_src_update(mfib_entry, source,
777 rpf_id, entry_flags);
779 if (INDEX_INVALID != repi)
782 * The source is providing its own replicate DPO.
783 * Create a sepcial path-list to manage it, that way
784 * this entry and the source are equivalent to a normal
787 fib_node_index_t old_pl_index;
789 dpo_id_t dpo = DPO_INVALID;
791 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
792 old_pl_index = msrc->mfes_pl;
794 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
797 fib_path_list_create_special(dp,
798 FIB_PATH_LIST_FLAG_EXCLUSIVE,
802 fib_path_list_lock(msrc->mfes_pl);
803 fib_path_list_unlock(old_pl_index);
806 mfib_entry_recalculate_forwarding(mfib_entry, MFIB_SOURCE_NONE);
808 return (mfib_entry_index);
812 mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
814 return (0 == vec_len(mfib_entry->mfe_srcs));
818 mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
820 return ((INDEX_INVALID == msrc->mfes_cover &&
821 MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
822 0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
827 mfib_entry_update_i (mfib_entry_t *mfib_entry,
828 mfib_entry_src_t *msrc,
829 mfib_source_t current_best,
832 if (INDEX_INVALID != repi)
835 * The source is providing its own replicate DPO.
836 * Create a sepcial path-list to manage it, that way
837 * this entry and the source are equivalent to a normal
840 fib_node_index_t old_pl_index;
842 dpo_id_t dpo = DPO_INVALID;
844 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
845 old_pl_index = msrc->mfes_pl;
847 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
850 fib_path_list_create_special(dp,
851 FIB_PATH_LIST_FLAG_EXCLUSIVE,
855 fib_path_list_lock(msrc->mfes_pl);
856 fib_path_list_unlock(old_pl_index);
859 if (mfib_entry_src_ok_for_delete(msrc))
862 * this source has no interfaces and no flags.
863 * it has nothing left to give - remove it
865 mfib_entry_src_remove(mfib_entry, msrc->mfes_src);
868 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
872 mfib_entry_special_add (fib_node_index_t mfib_entry_index,
873 mfib_source_t source,
874 mfib_entry_flags_t entry_flags,
878 mfib_source_t current_best;
879 mfib_entry_t *mfib_entry;
880 mfib_entry_src_t *msrc;
882 mfib_entry = mfib_entry_get(mfib_entry_index);
883 current_best = mfib_entry_get_best_source(mfib_entry);
885 msrc = mfib_entry_src_update_and_lock(mfib_entry, source, rpf_id,
888 mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
890 return (mfib_entry_ok_for_delete(mfib_entry));
894 mfib_entry_update (fib_node_index_t mfib_entry_index,
895 mfib_source_t source,
896 mfib_entry_flags_t entry_flags,
900 mfib_source_t current_best;
901 mfib_entry_t *mfib_entry;
902 mfib_entry_src_t *msrc;
904 mfib_entry = mfib_entry_get(mfib_entry_index);
905 current_best = mfib_entry_get_best_source(mfib_entry);
906 msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
908 mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
910 return (mfib_entry_ok_for_delete(mfib_entry));
914 mfib_entry_itf_add (mfib_entry_src_t *msrc,
918 hash_set(msrc->mfes_itfs, sw_if_index, mi);
922 mfib_entry_itf_remove (mfib_entry_src_t *msrc,
927 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
929 mfib_itf_delete(mfi);
931 hash_unset(msrc->mfes_itfs, sw_if_index);
935 mfib_entry_path_update (fib_node_index_t mfib_entry_index,
936 mfib_source_t source,
937 const fib_route_path_t *rpath,
938 mfib_itf_flags_t itf_flags)
940 fib_node_index_t path_index;
941 mfib_source_t current_best;
942 mfib_path_ext_t *path_ext;
943 mfib_entry_t *mfib_entry;
944 mfib_entry_src_t *msrc;
945 mfib_itf_flags_t old;
947 mfib_entry = mfib_entry_get(mfib_entry_index);
948 ASSERT(NULL != mfib_entry);
949 current_best = mfib_entry_get_best_source(mfib_entry);
950 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
953 * add the path to the path-list. If it's a duplicate we'll get
954 * back the original path.
956 path_index = mfib_entry_src_path_add(msrc, rpath);
959 * find the path extension for that path
961 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
963 if (NULL == path_ext)
965 old = MFIB_ITF_FLAG_NONE;
966 path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
970 old = path_ext->mfpe_flags;
971 path_ext->mfpe_flags = itf_flags;
975 * Has the path changed its contribution to the input interface set.
976 * Which only paths with interfaces can do...
978 if (~0 != rpath[0].frp_sw_if_index)
980 mfib_itf_t *mfib_itf;
982 if (old != itf_flags)
985 * change of flag contributions
987 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
988 rpath[0].frp_sw_if_index);
990 if (NULL == mfib_itf)
992 mfib_entry_itf_add(msrc,
993 rpath[0].frp_sw_if_index,
994 mfib_itf_create(path_index, itf_flags));
998 if (mfib_itf_update(mfib_itf,
1003 * no more interface flags on this path, remove
1004 * from the data-plane set
1006 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
1012 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1016 * mfib_entry_path_remove
1018 * remove a path from the entry.
1019 * return the mfib_entry's index if it is still present, INVALID otherwise.
1022 mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
1023 mfib_source_t source,
1024 const fib_route_path_t *rpath)
1026 fib_node_index_t path_index;
1027 mfib_source_t current_best;
1028 mfib_entry_t *mfib_entry;
1029 mfib_entry_src_t *msrc;
1031 mfib_entry = mfib_entry_get(mfib_entry_index);
1032 ASSERT(NULL != mfib_entry);
1033 current_best = mfib_entry_get_best_source(mfib_entry);
1034 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
1039 * there are no paths left for this source
1041 return (mfib_entry_ok_for_delete(mfib_entry));
1045 * remove the path from the path-list. If it's not there we'll get
1048 path_index = mfib_entry_src_path_remove(msrc, rpath);
1050 if (FIB_NODE_INDEX_INVALID != path_index)
1053 * don't need the extension, nor the interface anymore
1055 mfib_path_ext_remove(msrc, path_index);
1056 if (~0 != rpath[0].frp_sw_if_index)
1058 mfib_itf_t *mfib_itf;
1060 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
1061 rpath[0].frp_sw_if_index);
1063 if (mfib_itf_update(mfib_itf,
1065 MFIB_ITF_FLAG_NONE))
1068 * no more interface flags on this path, remove
1069 * from the data-plane set
1071 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
1076 if (mfib_entry_src_ok_for_delete(msrc))
1079 * this source has no interfaces and no flags.
1080 * it has nothing left to give - remove it
1082 mfib_entry_src_remove(mfib_entry, source);
1085 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1087 return (mfib_entry_ok_for_delete(mfib_entry));
1093 * The source is withdrawing all the paths it provided
1096 mfib_entry_delete (fib_node_index_t mfib_entry_index,
1097 mfib_source_t source)
1099 mfib_source_t current_best;
1100 mfib_entry_t *mfib_entry;
1102 mfib_entry = mfib_entry_get(mfib_entry_index);
1103 current_best = mfib_entry_get_best_source(mfib_entry);
1104 mfib_entry_src_remove(mfib_entry, source);
1106 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1108 return (mfib_entry_ok_for_delete(mfib_entry));
1112 fib_ip4_address_compare (ip4_address_t * a1,
1116 * IP addresses are unsiged ints. the return value here needs to be signed
1117 * a simple subtraction won't cut it.
1118 * If the addresses are the same, the sort order is undefiend, so phoey.
1120 return ((clib_net_to_host_u32(a1->data_u32) >
1121 clib_net_to_host_u32(a2->data_u32) ) ?
1126 fib_ip6_address_compare (ip6_address_t * a1,
1130 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1132 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1133 clib_net_to_host_u16 (a2->as_u16[i]));
1141 mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1142 fib_node_index_t mfib_entry_index2)
1144 mfib_entry_t *mfib_entry1, *mfib_entry2;
1147 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1148 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1150 switch (mfib_entry1->mfe_prefix.fp_proto)
1152 case FIB_PROTOCOL_IP4:
1153 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1154 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1158 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1159 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1162 case FIB_PROTOCOL_IP6:
1163 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1164 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1168 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1169 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1172 case FIB_PROTOCOL_MPLS:
1179 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1185 mfib_entry_cmp_for_sort (void *i1, void *i2)
1187 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1189 return (mfib_entry_cmp(*mfib_entry_index1,
1190 *mfib_entry_index2));
1194 mfib_entry_last_lock_gone (fib_node_t *node)
1196 mfib_entry_t *mfib_entry;
1197 mfib_entry_src_t *msrc;
1199 mfib_entry = mfib_entry_from_fib_node(node);
1201 dpo_reset(&mfib_entry->mfe_rep);
1203 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1205 vec_foreach(msrc, mfib_entry->mfe_srcs)
1207 mfib_entry_src_flush(msrc);
1210 vec_free(mfib_entry->mfe_srcs);
1212 fib_node_deinit(&mfib_entry->mfe_node);
1213 pool_put(mfib_entry_pool, mfib_entry);
1217 mfib_entry_get_stats_index (fib_node_index_t fib_entry_index)
1219 mfib_entry_t *mfib_entry;
1221 mfib_entry = mfib_entry_get(fib_entry_index);
1223 return (mfib_entry->mfe_rep.dpoi_index);
1227 * mfib_entry_back_walk_notify
1229 * A back walk has reach this entry.
1231 static fib_node_back_walk_rc_t
1232 mfib_entry_back_walk_notify (fib_node_t *node,
1233 fib_node_back_walk_ctx_t *ctx)
1235 mfib_entry_t *mfib_entry;
1237 mfib_entry = mfib_entry_from_fib_node(node);
1238 mfib_entry_recalculate_forwarding(mfib_entry,
1239 mfib_entry_get_best_source(mfib_entry));
1241 return (FIB_NODE_BACK_WALK_CONTINUE);
1245 mfib_entry_show_memory (void)
1247 fib_show_memory_usage("multicast-Entry",
1248 pool_elts(mfib_entry_pool),
1249 pool_len(mfib_entry_pool),
1250 sizeof(mfib_entry_t));
1254 * The MFIB entry's graph node virtual function table
1256 static const fib_node_vft_t mfib_entry_vft = {
1257 .fnv_get = mfib_entry_get_node,
1258 .fnv_last_lock = mfib_entry_last_lock_gone,
1259 .fnv_back_walk = mfib_entry_back_walk_notify,
1260 .fnv_mem_show = mfib_entry_show_memory,
1264 mfib_entry_lock (fib_node_index_t mfib_entry_index)
1266 mfib_entry_t *mfib_entry;
1268 mfib_entry = mfib_entry_get(mfib_entry_index);
1270 fib_node_lock(&mfib_entry->mfe_node);
1274 mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1276 mfib_entry_t *mfib_entry;
1278 mfib_entry = mfib_entry_get(mfib_entry_index);
1280 fib_node_unlock(&mfib_entry->mfe_node);
1284 mfib_entry_dpo_lock (dpo_id_t *dpo)
1288 mfib_entry_dpo_unlock (dpo_id_t *dpo)
1292 const static dpo_vft_t mfib_entry_dpo_vft = {
1293 .dv_lock = mfib_entry_dpo_lock,
1294 .dv_unlock = mfib_entry_dpo_unlock,
1295 .dv_format = format_mfib_entry_dpo,
1296 .dv_mem_show = mfib_entry_show_memory,
1299 const static char* const mfib_entry_ip4_nodes[] =
1301 "ip4-mfib-forward-rpf",
1304 const static char* const mfib_entry_ip6_nodes[] =
1306 "ip6-mfib-forward-rpf",
1310 const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1312 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1313 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1317 mfib_entry_module_init (void)
1319 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1320 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1321 mfib_entry_logger = vlib_log_register_class("mfib", "entry");
1325 mfib_entry_encode (fib_node_index_t mfib_entry_index,
1326 fib_route_path_encode_t **api_rpaths)
1328 fib_route_path_encode_t *api_rpath;
1329 mfib_entry_t *mfib_entry;
1330 mfib_entry_src_t *bsrc;
1332 mfib_entry = mfib_entry_get(mfib_entry_index);
1333 bsrc = mfib_entry_get_best_src(mfib_entry);
1335 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
1337 fib_path_list_walk_w_ext(bsrc->mfes_pl,
1343 vec_foreach(api_rpath, *api_rpaths)
1345 mfib_itf_t *mfib_itf;
1347 mfib_itf = mfib_entry_itf_find(bsrc->mfes_itfs,
1348 api_rpath->rpath.frp_sw_if_index);
1351 api_rpath->rpath.frp_mitf_flags = mfib_itf->mfi_flags;
1356 const mfib_prefix_t *
1357 mfib_entry_get_prefix (fib_node_index_t mfib_entry_index)
1359 mfib_entry_t *mfib_entry;
1361 mfib_entry = mfib_entry_get(mfib_entry_index);
1363 return (&mfib_entry->mfe_prefix);
1367 mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1369 mfib_entry_t *mfib_entry;
1371 mfib_entry = mfib_entry_get(mfib_entry_index);
1373 return (mfib_entry->mfe_fib_index);
1377 mfib_entry_contribute_ip_forwarding (fib_node_index_t mfib_entry_index)
1379 mfib_entry_t *mfib_entry;
1381 mfib_entry = mfib_entry_get(mfib_entry_index);
1383 return (&mfib_entry->mfe_rep);
1387 mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1388 fib_forward_chain_type_t type,
1389 mfib_entry_fwd_flags_t flags,
1393 * An IP mFIB entry can only provide a forwarding chain that
1394 * is the same IP proto as the prefix.
1395 * No use-cases (i know of) for other combinations.
1397 mfib_entry_t *mfib_entry;
1400 mfib_entry = mfib_entry_get(mfib_entry_index);
1402 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1404 if (type == mfib_forw_chain_type_from_dpo_proto(dp))
1408 rep = replicate_get(mfib_entry->mfe_rep.dpoi_index);
1410 if ((rep->rep_flags & REPLICATE_FLAGS_HAS_LOCAL) &&
1411 (flags & MFIB_ENTRY_FWD_FLAG_NO_LOCAL))
1414 * caller does not want the local paths that the entry has
1416 dpo_set(dpo, DPO_REPLICATE, rep->rep_proto,
1417 replicate_dup(REPLICATE_FLAGS_NONE,
1418 mfib_entry->mfe_rep.dpoi_index));
1422 dpo_copy(dpo, &mfib_entry->mfe_rep);
1427 dpo_copy(dpo, drop_dpo_get(dp));
1432 * fib_entry_cover_changed
1434 * this entry is tracking its cover and that cover has changed.
1437 mfib_entry_cover_changed (fib_node_index_t mfib_entry_index)
1439 mfib_entry_t *mfib_entry;
1440 mfib_entry_src_t *msrc;
1443 mfib_entry = mfib_entry_get(mfib_entry_index);
1444 msrc = mfib_entry_get_best_src(mfib_entry);
1446 res = mfib_entry_src_cover_change(mfib_entry, msrc);
1448 if (MFIB_SRC_REEVALUATE == res)
1450 mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1452 MFIB_ENTRY_DBG(mfib_entry, "cover-changed");
1456 * mfib_entry_cover_updated
1458 * this entry is tracking its cover and that cover has been updated
1459 * (i.e. its forwarding information has changed).
1462 mfib_entry_cover_updated (fib_node_index_t mfib_entry_index)
1464 mfib_entry_t *mfib_entry;
1465 mfib_entry_src_t *msrc;
1468 mfib_entry = mfib_entry_get(mfib_entry_index);
1469 msrc = mfib_entry_get_best_src(mfib_entry);
1471 res = mfib_entry_src_cover_update(mfib_entry, msrc);
1473 if (MFIB_SRC_REEVALUATE == res)
1475 mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1477 MFIB_ENTRY_DBG(mfib_entry, "cover-updated");
1481 mfib_entry_pool_size (void)
1483 return (pool_elts(mfib_entry_pool));
1486 static clib_error_t *
1487 show_mfib_entry_command (vlib_main_t * vm,
1488 unformat_input_t * input,
1489 vlib_cli_command_t * cmd)
1491 fib_node_index_t fei;
1493 if (unformat (input, "%d", &fei))
1496 * show one in detail
1498 if (!pool_is_free_index(mfib_entry_pool, fei))
1500 vlib_cli_output (vm, "%d@%U",
1502 format_mfib_entry, fei,
1503 MFIB_ENTRY_FORMAT_DETAIL2);
1507 vlib_cli_output (vm, "entry %d invalid", fei);
1515 vlib_cli_output (vm, "FIB Entries:");
1516 pool_foreach_index(fei, mfib_entry_pool,
1518 vlib_cli_output (vm, "%d@%U",
1520 format_mfib_entry, fei,
1521 MFIB_ENTRY_FORMAT_BRIEF);
1529 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1530 * numerical indentifier.
1532 VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1533 .path = "show mfib entry",
1534 .function = show_mfib_entry_command,
1535 .short_help = "show mfib entry",