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;
41 static const char *mfib_src_attribute_names[] = MFIB_ENTRY_SRC_ATTRIBUTES;
44 * Pool for all fib_entries
46 mfib_entry_t *mfib_entry_pool;
49 mfib_entry_get_node (fib_node_index_t index)
51 return ((fib_node_t*)mfib_entry_get(index));
55 mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
57 return (mfib_entry->mfe_prefix.fp_proto);
60 fib_forward_chain_type_t
61 mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
63 switch (mfib_entry->mfe_prefix.fp_proto)
65 case FIB_PROTOCOL_IP4:
66 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
67 case FIB_PROTOCOL_IP6:
68 return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
69 case FIB_PROTOCOL_MPLS:
73 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
77 format_mfib_entry_dpo (u8 * s, va_list * args)
79 index_t fei = va_arg(*args, index_t);
80 CLIB_UNUSED(u32 indent) = va_arg(*args, u32);
82 return (format(s, "%U",
83 format_mfib_entry, fei,
84 MFIB_ENTRY_FORMAT_BRIEF));
87 static inline mfib_path_ext_t *
88 mfib_entry_path_ext_get (index_t mi)
90 return (pool_elt_at_index(mfib_path_ext_pool, mi));
94 format_mfib_entry_path_ext (u8 * s, va_list * args)
96 mfib_path_ext_t *path_ext;
97 index_t mpi = va_arg(*args, index_t);
99 path_ext = mfib_entry_path_ext_get(mpi);
100 return (format(s, "path:%d flags:%U",
102 format_mfib_itf_flags, path_ext->mfpe_flags));
106 format_mfib_entry_src_flags (u8 *s, va_list *args)
108 mfib_entry_src_attribute_t sattr;
109 mfib_entry_src_flags_t flag = va_arg(*args, int);
113 return format(s, "none");
116 FOR_EACH_MFIB_SRC_ATTRIBUTE(sattr) {
117 if ((1 << sattr) & flag) {
118 s = format (s, "%s,", mfib_src_attribute_names[sattr]);
126 format_mfib_entry (u8 * s, va_list * args)
128 fib_node_index_t fei, mfi;
129 mfib_entry_t *mfib_entry;
130 mfib_entry_src_t *msrc;
134 fei = va_arg (*args, fib_node_index_t);
135 level = va_arg (*args, int);
136 mfib_entry = mfib_entry_get(fei);
138 s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
139 s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);
141 if (level >= MFIB_ENTRY_FORMAT_DETAIL)
143 fib_node_index_t path_index, mpi;
145 s = format (s, "\n");
146 s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
147 s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
148 s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
149 vec_foreach(msrc, mfib_entry->mfe_srcs)
151 s = format (s, " src:%s flags:%U locks:%d:",
152 mfib_source_names[msrc->mfes_src],
153 format_mfib_entry_src_flags, msrc->mfes_flags,
154 msrc->mfes_ref_count);
155 if (msrc->mfes_cover != FIB_NODE_INDEX_INVALID)
157 s = format (s, " cover:%d", msrc->mfes_cover);
159 s = format (s, " %U\n", format_mfib_entry_flags, msrc->mfes_route_flags);
160 if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
162 s = fib_path_list_format(msrc->mfes_pl, s);
164 s = format (s, " Extensions:\n");
165 hash_foreach(path_index, mpi, msrc->mfes_exts,
167 s = format(s, " %U\n", format_mfib_entry_path_ext, mpi);
169 s = format (s, " Interface-Forwarding:\n");
170 hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
172 s = format(s, " %U\n", format_mfib_itf, mfi);
177 s = format(s, "\n Interfaces:");
178 hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
180 s = format(s, "\n %U", format_mfib_itf, mfi);
182 if (MFIB_RPF_ID_NONE != mfib_entry->mfe_rpf_id)
184 s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id);
186 s = format(s, "\n %U-chain\n %U",
187 format_fib_forw_chain_type,
188 mfib_entry_get_default_chain_type(mfib_entry),
190 &mfib_entry->mfe_rep,
194 if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
196 s = format(s, "\nchildren:");
197 s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
204 mfib_entry_from_fib_node (fib_node_t *node)
206 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
207 return ((mfib_entry_t*)node);
211 mfib_entry_src_cmp_for_sort (void * v1,
214 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
216 return (esrc1->mfes_src - esrc2->mfes_src);
220 mfib_entry_src_init (mfib_entry_t *mfib_entry,
221 mfib_source_t source)
224 mfib_entry_src_t esrc = {
225 .mfes_pl = FIB_NODE_INDEX_INVALID,
226 .mfes_route_flags = MFIB_ENTRY_FLAG_NONE,
228 .mfes_cover = FIB_NODE_INDEX_INVALID,
229 .mfes_sibling = FIB_NODE_INDEX_INVALID,
233 vec_add1(mfib_entry->mfe_srcs, esrc);
234 vec_sort_with_function(mfib_entry->mfe_srcs,
235 mfib_entry_src_cmp_for_sort);
238 static mfib_entry_src_t *
239 mfib_entry_src_find (const mfib_entry_t *mfib_entry,
240 mfib_source_t source,
244 mfib_entry_src_t *esrc;
248 vec_foreach(esrc, mfib_entry->mfe_srcs)
250 if (esrc->mfes_src == source)
267 static mfib_entry_src_t *
268 mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
269 mfib_source_t source)
271 mfib_entry_src_t *msrc;
273 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
277 mfib_entry_src_init(mfib_entry, source);
278 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
284 static mfib_entry_src_t *
285 mfib_entry_src_update (mfib_entry_t *mfib_entry,
286 mfib_source_t source,
288 mfib_entry_flags_t entry_flags)
290 mfib_entry_src_t *msrc;
292 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
294 msrc->mfes_route_flags = entry_flags;
295 msrc->mfes_rpf_id = rpf_id;
296 msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
301 static mfib_entry_src_t *
302 mfib_entry_src_update_and_lock (mfib_entry_t *mfib_entry,
303 mfib_source_t source,
305 mfib_entry_flags_t entry_flags)
307 mfib_entry_src_t *msrc;
309 msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
311 msrc->mfes_ref_count++;
312 msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
318 mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
320 mfib_entry_src_t *bsrc;
323 * the enum of sources is deliberately arranged in priority order
325 if (0 == vec_len(mfib_entry->mfe_srcs))
331 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
338 mfib_entry_get_best_source (const mfib_entry_t *mfib_entry)
340 mfib_entry_src_t *bsrc;
342 bsrc = mfib_entry_get_best_src(mfib_entry);
344 return (bsrc->mfes_src);
348 mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
349 mfib_source_t source)
351 mfib_entry_t *mfib_entry;
353 mfib_entry = mfib_entry_get(mfib_entry_index);
355 return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
359 mfib_entry_is_marked (fib_node_index_t mfib_entry_index,
360 mfib_source_t source)
362 mfib_entry_t *mfib_entry;
363 mfib_entry_src_t *esrc;
365 mfib_entry = mfib_entry_get(mfib_entry_index);
367 esrc = mfib_entry_src_find(mfib_entry, source, NULL);
375 return (!!(esrc->mfes_flags & MFIB_ENTRY_SRC_FLAG_STALE));
380 mfib_entry_mark (fib_node_index_t fib_entry_index,
381 mfib_source_t source)
383 mfib_entry_t *mfib_entry;
384 mfib_entry_src_t *esrc;
386 mfib_entry = mfib_entry_get(fib_entry_index);
388 esrc = mfib_entry_src_find(mfib_entry, source, NULL);
392 esrc->mfes_flags |= MFIB_ENTRY_SRC_FLAG_STALE;
397 mfib_entry_is_host (fib_node_index_t mfib_entry_index)
399 return (mfib_prefix_is_host(mfib_entry_get_prefix(mfib_entry_index)));
404 mfib_entry_src_flush (mfib_entry_src_t *msrc)
409 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
411 mfib_itf_delete(mfib_itf_get(mfii));
413 hash_free(msrc->mfes_itfs);
414 msrc->mfes_itfs = NULL;
415 fib_path_list_unlock(msrc->mfes_pl);
419 mfib_entry_src_remove (mfib_entry_t *mfib_entry,
420 mfib_source_t source)
423 mfib_entry_src_t *msrc;
426 msrc = mfib_entry_src_find(mfib_entry, source, &index);
430 ASSERT(0 != msrc->mfes_ref_count);
431 msrc->mfes_ref_count--;
433 if (0 == msrc->mfes_ref_count)
435 mfib_entry_src_deactivate(mfib_entry, msrc);
436 mfib_entry_src_flush(msrc);
438 vec_del1(mfib_entry->mfe_srcs, index);
439 if (vec_len (mfib_entry->mfe_srcs) > 1)
440 vec_sort_with_function(mfib_entry->mfe_srcs,
441 mfib_entry_src_cmp_for_sort);
447 mfib_entry_child_add (fib_node_index_t mfib_entry_index,
448 fib_node_type_t child_type,
449 fib_node_index_t child_index)
451 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
458 mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
461 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
466 static mfib_entry_t *
467 mfib_entry_alloc (u32 fib_index,
468 const mfib_prefix_t *prefix,
469 fib_node_index_t *mfib_entry_index)
471 mfib_entry_t *mfib_entry;
473 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
475 fib_node_init(&mfib_entry->mfe_node,
476 FIB_NODE_TYPE_MFIB_ENTRY);
479 * Some of the members require non-default initialisation
480 * so we also init those that don't and thus save on the call to clib_memset.
482 mfib_entry->mfe_flags = 0;
483 mfib_entry->mfe_fib_index = fib_index;
484 mfib_entry->mfe_prefix = *prefix;
485 mfib_entry->mfe_srcs = NULL;
486 mfib_entry->mfe_itfs = NULL;
487 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
488 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
490 dpo_reset(&mfib_entry->mfe_rep);
492 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
494 MFIB_ENTRY_DBG(mfib_entry, "alloc");
499 static inline mfib_path_ext_t *
500 mfib_entry_path_ext_find (mfib_path_ext_t *exts,
501 fib_node_index_t path_index)
505 p = hash_get(exts, path_index);
509 return (mfib_entry_path_ext_get(p[0]));
515 static mfib_path_ext_t*
516 mfib_path_ext_add (mfib_entry_src_t *msrc,
517 fib_node_index_t path_index,
518 mfib_itf_flags_t mfi_flags)
520 mfib_path_ext_t *path_ext;
522 pool_get(mfib_path_ext_pool, path_ext);
524 path_ext->mfpe_flags = mfi_flags;
525 path_ext->mfpe_path = path_index;
527 hash_set(msrc->mfes_exts, path_index,
528 path_ext - mfib_path_ext_pool);
534 mfib_path_ext_remove (mfib_entry_src_t *msrc,
535 fib_node_index_t path_index)
537 mfib_path_ext_t *path_ext;
539 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
541 hash_unset(msrc->mfes_exts, path_index);
542 pool_put(mfib_path_ext_pool, path_ext);
545 typedef struct mfib_entry_collect_forwarding_ctx_t_
547 load_balance_path_t * next_hops;
548 fib_forward_chain_type_t fct;
549 mfib_entry_src_t *msrc;
550 } mfib_entry_collect_forwarding_ctx_t;
552 static fib_path_list_walk_rc_t
553 mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
554 fib_node_index_t path_index,
557 mfib_entry_collect_forwarding_ctx_t *ctx;
558 load_balance_path_t *nh;
563 * if the path is not resolved, don't include it.
565 if (!fib_path_is_resolved(path_index))
567 return (FIB_PATH_LIST_WALK_CONTINUE);
571 * If the path is not forwarding to use it
573 mfib_path_ext_t *path_ext;
575 path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
578 if (NULL != path_ext &&
579 !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
581 return (FIB_PATH_LIST_WALK_CONTINUE);
586 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
587 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
589 * EOS traffic with no label to stack, we need the IP Adj
591 vec_add2(ctx->next_hops, nh, 1);
593 nh->path_index = path_index;
594 nh->path_weight = fib_path_get_weight(path_index);
595 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
598 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
599 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
600 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
601 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
602 case FIB_FORW_CHAIN_TYPE_ETHERNET:
603 case FIB_FORW_CHAIN_TYPE_NSH:
604 case FIB_FORW_CHAIN_TYPE_BIER:
609 return (FIB_PATH_LIST_WALK_CONTINUE);
613 mfib_entry_stack (mfib_entry_t *mfib_entry,
614 mfib_entry_src_t *msrc)
618 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
621 * unlink the enty from the previous path list.
623 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
625 fib_path_list_child_remove(mfib_entry->mfe_pl,
626 mfib_entry->mfe_sibling);
631 mfib_entry_collect_forwarding_ctx_t ctx = {
633 .fct = mfib_entry_get_default_chain_type(mfib_entry),
638 * link the entry to the path-list.
639 * The entry needs to be a child so that we receive the back-walk
640 * updates to recalculate forwarding.
642 mfib_entry->mfe_pl = msrc->mfes_pl;
643 mfib_entry->mfe_flags = msrc->mfes_route_flags;
644 mfib_entry->mfe_itfs = msrc->mfes_itfs;
645 mfib_entry->mfe_rpf_id = msrc->mfes_rpf_id;
647 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
649 mfib_entry->mfe_sibling =
650 fib_path_list_child_add(mfib_entry->mfe_pl,
651 FIB_NODE_TYPE_MFIB_ENTRY,
652 mfib_entry_get_index(mfib_entry));
654 fib_path_list_walk(mfib_entry->mfe_pl,
655 mfib_entry_src_collect_forwarding,
658 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
660 if (NULL == ctx.next_hops)
663 * no next-hops, stack directly on the drop
665 dpo_stack(DPO_MFIB_ENTRY, dp,
666 &mfib_entry->mfe_rep,
672 * each path contirbutes a next-hop. form a replicate
673 * from those choices.
675 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
676 dpo_is_drop(&mfib_entry->mfe_rep))
678 dpo_id_t tmp_dpo = DPO_INVALID;
682 replicate_create(0, dp));
684 dpo_stack(DPO_MFIB_ENTRY, dp,
685 &mfib_entry->mfe_rep,
690 replicate_multipath_update(&mfib_entry->mfe_rep,
697 * for exclusive routes the source provided a replicate DPO
698 * which we stashed in the special path list with one path,
699 * so we can stack directly on that.
701 ASSERT(1 == vec_len(ctx.next_hops));
703 if (NULL != ctx.next_hops)
705 dpo_stack(DPO_MFIB_ENTRY, dp,
706 &mfib_entry->mfe_rep,
707 &ctx.next_hops[0].path_dpo);
708 dpo_reset(&ctx.next_hops[0].path_dpo);
709 vec_free(ctx.next_hops);
713 dpo_stack(DPO_MFIB_ENTRY, dp,
714 &mfib_entry->mfe_rep,
721 dpo_stack(DPO_MFIB_ENTRY, dp,
722 &mfib_entry->mfe_rep,
727 * time for walkies fido.
729 fib_node_back_walk_ctx_t bw_ctx = {
730 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
733 fib_walk_sync(FIB_NODE_TYPE_MFIB_ENTRY,
734 mfib_entry_get_index(mfib_entry),
738 static fib_node_index_t*
739 mfib_entry_src_paths_add (mfib_entry_src_t *msrc,
740 const fib_route_path_t *rpaths)
742 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_route_flags));
744 msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
746 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
748 /* A non-shared path-list */
749 msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
751 fib_path_list_lock(msrc->mfes_pl);
754 return (fib_path_list_paths_add(msrc->mfes_pl, rpaths));
757 static fib_node_index_t*
758 mfib_entry_src_paths_remove (mfib_entry_src_t *msrc,
759 const fib_route_path_t *rpaths)
761 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_route_flags));
763 msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
765 return (fib_path_list_paths_remove(msrc->mfes_pl, rpaths));
769 mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry,
770 mfib_source_t old_best)
772 mfib_entry_src_t *bsrc, *osrc;
775 * copy the forwarding data from the bast source
777 bsrc = mfib_entry_get_best_src(mfib_entry);
778 osrc = mfib_entry_src_find(mfib_entry, old_best, NULL);
782 if (bsrc->mfes_src != old_best)
785 * we are changing from one source to another
786 * deactivate the old, and activate the new
788 mfib_entry_src_deactivate(mfib_entry, osrc);
789 mfib_entry_src_activate(mfib_entry, bsrc);
794 mfib_entry_src_deactivate(mfib_entry, osrc);
797 mfib_entry_stack(mfib_entry, bsrc);
798 mfib_entry_cover_update_notify(mfib_entry);
803 mfib_entry_create (u32 fib_index,
804 mfib_source_t source,
805 const mfib_prefix_t *prefix,
807 mfib_entry_flags_t entry_flags,
810 fib_node_index_t mfib_entry_index;
811 mfib_entry_t *mfib_entry;
812 mfib_entry_src_t *msrc;
814 mfib_entry = mfib_entry_alloc(fib_index, prefix,
816 msrc = mfib_entry_src_update(mfib_entry, source,
817 rpf_id, entry_flags);
819 if (INDEX_INVALID != repi)
822 * The source is providing its own replicate DPO.
823 * Create a sepcial path-list to manage it, that way
824 * this entry and the source are equivalent to a normal
827 fib_node_index_t old_pl_index;
829 dpo_id_t dpo = DPO_INVALID;
831 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
832 old_pl_index = msrc->mfes_pl;
834 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
837 fib_path_list_create_special(dp,
838 FIB_PATH_LIST_FLAG_EXCLUSIVE,
842 fib_path_list_lock(msrc->mfes_pl);
843 fib_path_list_unlock(old_pl_index);
846 mfib_entry_recalculate_forwarding(mfib_entry, MFIB_SOURCE_NONE);
848 return (mfib_entry_index);
852 mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
854 return (0 == vec_len(mfib_entry->mfe_srcs));
858 mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
860 return ((INDEX_INVALID == msrc->mfes_cover &&
861 MFIB_ENTRY_FLAG_NONE == msrc->mfes_route_flags &&
862 0 == fib_path_list_get_n_paths(msrc->mfes_pl)) &&
863 (0 == hash_elts(msrc->mfes_itfs)));
865 /* return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_route_flags) && */
866 /* (0 == fib_path_list_get_n_paths(msrc->mfes_pl)) && */
867 /* (0 == hash_elts(msrc->mfes_itfs))); */
872 mfib_entry_update_i (mfib_entry_t *mfib_entry,
873 mfib_entry_src_t *msrc,
874 mfib_source_t current_best,
877 if (INDEX_INVALID != repi)
880 * The source is providing its own replicate DPO.
881 * Create a sepcial path-list to manage it, that way
882 * this entry and the source are equivalent to a normal
885 fib_node_index_t old_pl_index;
887 dpo_id_t dpo = DPO_INVALID;
889 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
890 old_pl_index = msrc->mfes_pl;
892 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
895 fib_path_list_create_special(dp,
896 FIB_PATH_LIST_FLAG_EXCLUSIVE,
900 fib_path_list_lock(msrc->mfes_pl);
901 fib_path_list_unlock(old_pl_index);
904 if (mfib_entry_src_ok_for_delete(msrc))
907 * this source has no interfaces and no flags.
908 * it has nothing left to give - remove it
910 mfib_entry_src_remove(mfib_entry, msrc->mfes_src);
913 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
917 mfib_entry_special_add (fib_node_index_t mfib_entry_index,
918 mfib_source_t source,
919 mfib_entry_flags_t entry_flags,
923 mfib_source_t current_best;
924 mfib_entry_t *mfib_entry;
925 mfib_entry_src_t *msrc;
927 mfib_entry = mfib_entry_get(mfib_entry_index);
928 current_best = mfib_entry_get_best_source(mfib_entry);
930 msrc = mfib_entry_src_update_and_lock(mfib_entry, source, rpf_id,
933 mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
935 return (mfib_entry_ok_for_delete(mfib_entry));
939 mfib_entry_update (fib_node_index_t mfib_entry_index,
940 mfib_source_t source,
941 mfib_entry_flags_t entry_flags,
945 mfib_source_t current_best;
946 mfib_entry_t *mfib_entry;
947 mfib_entry_src_t *msrc;
949 mfib_entry = mfib_entry_get(mfib_entry_index);
950 current_best = mfib_entry_get_best_source(mfib_entry);
951 msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
953 mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
955 return (mfib_entry_ok_for_delete(mfib_entry));
959 mfib_entry_itf_add (mfib_entry_src_t *msrc,
963 hash_set(msrc->mfes_itfs, sw_if_index, mi);
967 mfib_entry_itf_remove (mfib_entry_src_t *msrc,
972 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
974 mfib_itf_delete(mfi);
976 hash_unset(msrc->mfes_itfs, sw_if_index);
980 mfib_entry_path_itf_based (const fib_route_path_t *rpath)
982 return (!(rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP) &&
983 ~0 != rpath->frp_sw_if_index);
987 mfib_entry_path_update (fib_node_index_t mfib_entry_index,
988 mfib_source_t source,
989 const fib_route_path_t *rpaths)
991 fib_node_index_t* path_indices, path_index;
992 const fib_route_path_t *rpath;
993 mfib_source_t current_best;
994 mfib_path_ext_t *path_ext;
995 const mfib_prefix_t *pfx;
996 mfib_entry_t *mfib_entry;
997 mfib_entry_src_t *msrc;
998 mfib_itf_flags_t old;
1001 mfib_entry = mfib_entry_get(mfib_entry_index);
1002 pfx = mfib_entry_get_prefix(mfib_entry_index);
1003 ASSERT(NULL != mfib_entry);
1004 current_best = mfib_entry_get_best_source(mfib_entry);
1005 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
1008 * add the path to the path-list. If it's a duplicate we'll get
1009 * back the original path.
1011 path_indices = mfib_entry_src_paths_add(msrc, rpaths);
1013 vec_foreach_index(ii, path_indices)
1015 path_index = path_indices[ii];
1016 rpath = &rpaths[ii];
1018 if (FIB_NODE_INDEX_INVALID == path_index)
1022 * find the path extension for that path
1024 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
1026 if (NULL == path_ext)
1028 old = MFIB_ITF_FLAG_NONE;
1029 path_ext = mfib_path_ext_add(msrc, path_index,
1030 rpath->frp_mitf_flags);
1034 old = path_ext->mfpe_flags;
1035 path_ext->mfpe_flags = rpath->frp_mitf_flags;
1039 * Has the path changed its contribution to the input interface set.
1040 * Which only paths with interfaces can do...
1042 if (mfib_entry_path_itf_based(rpath))
1044 mfib_itf_t *mfib_itf;
1046 if (old != rpath->frp_mitf_flags)
1049 * change of flag contributions
1051 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
1052 rpath->frp_sw_if_index);
1054 if (NULL == mfib_itf)
1056 index_t mfib_itf_i = mfib_itf_create(path_index,
1057 rpath->frp_mitf_flags);
1058 mfib_entry_itf_add(msrc,
1059 rpath->frp_sw_if_index,
1062 if (MFIB_ITF_FLAG_ACCEPT & rpath->frp_mitf_flags)
1064 /* new accepting interface - add the mac to the driver */
1065 mfib_itf_mac_add(mfib_itf_get(mfib_itf_i), pfx);
1070 u8 was_accept = !!(old & MFIB_ITF_FLAG_ACCEPT);
1071 u8 is_accept = !!(rpath->frp_mitf_flags & MFIB_ITF_FLAG_ACCEPT);
1073 if (mfib_itf_update(mfib_itf,
1075 rpath->frp_mitf_flags))
1078 * no more interface flags on this path, remove
1079 * from the data-plane set
1083 mfib_itf_mac_del(mfib_itf, pfx);
1086 mfib_entry_itf_remove(msrc, rpath->frp_sw_if_index);
1091 * is there a change to the ACCEPT flag that
1092 * requires us to update hte driver with the
1095 if (is_accept != was_accept)
1099 mfib_itf_mac_add(mfib_itf, pfx);
1101 else if (was_accept)
1103 mfib_itf_mac_del(mfib_itf, pfx);
1111 vec_free(path_indices);
1113 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1117 * mfib_entry_path_remove
1119 * remove a path from the entry.
1120 * return the mfib_entry's index if it is still present, INVALID otherwise.
1123 mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
1124 mfib_source_t source,
1125 const fib_route_path_t *rpaths)
1127 fib_node_index_t path_index, *path_indices;
1128 const fib_route_path_t *rpath;
1129 mfib_source_t current_best;
1130 const mfib_prefix_t *pfx;
1131 mfib_entry_t *mfib_entry;
1132 mfib_entry_src_t *msrc;
1135 mfib_entry = mfib_entry_get(mfib_entry_index);
1136 pfx = mfib_entry_get_prefix(mfib_entry_index);
1137 ASSERT(NULL != mfib_entry);
1138 current_best = mfib_entry_get_best_source(mfib_entry);
1139 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
1144 * there are no paths left for this source
1146 return (mfib_entry_ok_for_delete(mfib_entry));
1150 * remove the paths from the path-list. If it's not there we'll get
1151 * back an empty vector
1153 path_indices = mfib_entry_src_paths_remove(msrc, rpaths);
1155 vec_foreach_index(ii, path_indices)
1157 path_index = path_indices[ii];
1158 rpath = &rpaths[ii];
1160 if (FIB_NODE_INDEX_INVALID == path_index)
1164 * don't need the extension, nor the interface anymore
1166 mfib_path_ext_remove(msrc, path_index);
1167 if (mfib_entry_path_itf_based(rpath))
1169 u8 was_accept, is_accept;
1170 mfib_itf_t *mfib_itf;
1172 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
1173 rpath->frp_sw_if_index);
1174 was_accept = !!(MFIB_ITF_FLAG_ACCEPT & mfib_itf->mfi_flags);
1176 if (mfib_itf_update(mfib_itf,
1178 MFIB_ITF_FLAG_NONE))
1182 mfib_itf_mac_del(mfib_itf, pfx);
1186 * no more interface flags on this path, remove
1187 * from the data-plane set
1189 mfib_entry_itf_remove(msrc, rpath->frp_sw_if_index);
1193 is_accept = !!(MFIB_ITF_FLAG_ACCEPT & mfib_itf->mfi_flags);
1195 if (was_accept && !is_accept)
1197 mfib_itf_mac_del(mfib_itf, pfx);
1202 vec_free(path_indices);
1204 if (mfib_entry_src_ok_for_delete(msrc))
1207 * this source has no interfaces and no flags.
1208 * it has nothing left to give - remove it
1210 mfib_entry_src_remove(mfib_entry, source);
1213 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1215 return (mfib_entry_ok_for_delete(mfib_entry));
1221 * The source is withdrawing all the paths it provided
1224 mfib_entry_delete (fib_node_index_t mfib_entry_index,
1225 mfib_source_t source)
1227 mfib_source_t current_best;
1228 mfib_entry_t *mfib_entry;
1230 mfib_entry = mfib_entry_get(mfib_entry_index);
1231 current_best = mfib_entry_get_best_source(mfib_entry);
1232 mfib_entry_src_remove(mfib_entry, source);
1234 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1236 return (mfib_entry_ok_for_delete(mfib_entry));
1240 fib_ip4_address_compare (ip4_address_t * a1,
1244 * IP addresses are unsiged ints. the return value here needs to be signed
1245 * a simple subtraction won't cut it.
1246 * If the addresses are the same, the sort order is undefiend, so phoey.
1248 return ((clib_net_to_host_u32(a1->data_u32) >
1249 clib_net_to_host_u32(a2->data_u32) ) ?
1254 fib_ip6_address_compare (ip6_address_t * a1,
1258 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1260 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1261 clib_net_to_host_u16 (a2->as_u16[i]));
1269 mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1270 fib_node_index_t mfib_entry_index2)
1272 mfib_entry_t *mfib_entry1, *mfib_entry2;
1275 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1276 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1278 switch (mfib_entry1->mfe_prefix.fp_proto)
1280 case FIB_PROTOCOL_IP4:
1281 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1282 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1286 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1287 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1290 case FIB_PROTOCOL_IP6:
1291 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1292 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1296 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1297 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1300 case FIB_PROTOCOL_MPLS:
1307 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1313 mfib_entry_cmp_for_sort (void *i1, void *i2)
1315 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1317 return (mfib_entry_cmp(*mfib_entry_index1,
1318 *mfib_entry_index2));
1322 mfib_entry_last_lock_gone (fib_node_t *node)
1324 mfib_entry_t *mfib_entry;
1325 mfib_entry_src_t *msrc;
1327 mfib_entry = mfib_entry_from_fib_node(node);
1329 dpo_reset(&mfib_entry->mfe_rep);
1331 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1333 vec_foreach(msrc, mfib_entry->mfe_srcs)
1335 mfib_entry_src_flush(msrc);
1338 vec_free(mfib_entry->mfe_srcs);
1340 fib_node_deinit(&mfib_entry->mfe_node);
1341 pool_put(mfib_entry_pool, mfib_entry);
1345 mfib_entry_get_stats_index (fib_node_index_t fib_entry_index)
1347 mfib_entry_t *mfib_entry;
1349 mfib_entry = mfib_entry_get(fib_entry_index);
1351 return (mfib_entry->mfe_rep.dpoi_index);
1355 * mfib_entry_back_walk_notify
1357 * A back walk has reach this entry.
1359 static fib_node_back_walk_rc_t
1360 mfib_entry_back_walk_notify (fib_node_t *node,
1361 fib_node_back_walk_ctx_t *ctx)
1363 mfib_entry_t *mfib_entry;
1365 mfib_entry = mfib_entry_from_fib_node(node);
1366 mfib_entry_recalculate_forwarding(mfib_entry,
1367 mfib_entry_get_best_source(mfib_entry));
1369 return (FIB_NODE_BACK_WALK_CONTINUE);
1373 mfib_entry_show_memory (void)
1375 fib_show_memory_usage("multicast-Entry",
1376 pool_elts(mfib_entry_pool),
1377 pool_len(mfib_entry_pool),
1378 sizeof(mfib_entry_t));
1382 * The MFIB entry's graph node virtual function table
1384 static const fib_node_vft_t mfib_entry_vft = {
1385 .fnv_get = mfib_entry_get_node,
1386 .fnv_last_lock = mfib_entry_last_lock_gone,
1387 .fnv_back_walk = mfib_entry_back_walk_notify,
1388 .fnv_mem_show = mfib_entry_show_memory,
1392 mfib_entry_lock (fib_node_index_t mfib_entry_index)
1394 mfib_entry_t *mfib_entry;
1396 mfib_entry = mfib_entry_get(mfib_entry_index);
1398 fib_node_lock(&mfib_entry->mfe_node);
1402 mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1404 mfib_entry_t *mfib_entry;
1406 mfib_entry = mfib_entry_get(mfib_entry_index);
1408 fib_node_unlock(&mfib_entry->mfe_node);
1412 mfib_entry_dpo_lock (dpo_id_t *dpo)
1416 mfib_entry_dpo_unlock (dpo_id_t *dpo)
1420 const static dpo_vft_t mfib_entry_dpo_vft = {
1421 .dv_lock = mfib_entry_dpo_lock,
1422 .dv_unlock = mfib_entry_dpo_unlock,
1423 .dv_format = format_mfib_entry_dpo,
1424 .dv_mem_show = mfib_entry_show_memory,
1427 const static char* const mfib_entry_ip4_nodes[] =
1429 "ip4-mfib-forward-rpf",
1432 const static char* const mfib_entry_ip6_nodes[] =
1434 "ip6-mfib-forward-rpf",
1438 const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1440 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1441 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1445 mfib_entry_module_init (void)
1447 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1448 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1449 mfib_entry_logger = vlib_log_register_class("mfib", "entry");
1453 mfib_entry_encode (fib_node_index_t mfib_entry_index)
1455 fib_path_encode_ctx_t ctx = {
1458 mfib_entry_t *mfib_entry;
1459 fib_route_path_t *rpath;
1460 mfib_entry_src_t *bsrc;
1462 mfib_entry = mfib_entry_get(mfib_entry_index);
1463 bsrc = mfib_entry_get_best_src(mfib_entry);
1465 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
1467 fib_path_list_walk_w_ext(bsrc->mfes_pl,
1473 vec_foreach(rpath, ctx.rpaths)
1475 mfib_itf_t *mfib_itf;
1477 mfib_itf = mfib_entry_itf_find(bsrc->mfes_itfs,
1478 rpath->frp_sw_if_index);
1481 rpath->frp_mitf_flags = mfib_itf->mfi_flags;
1485 return (ctx.rpaths);
1488 const mfib_prefix_t *
1489 mfib_entry_get_prefix (fib_node_index_t mfib_entry_index)
1491 mfib_entry_t *mfib_entry;
1493 mfib_entry = mfib_entry_get(mfib_entry_index);
1495 return (&mfib_entry->mfe_prefix);
1499 mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1501 mfib_entry_t *mfib_entry;
1503 mfib_entry = mfib_entry_get(mfib_entry_index);
1505 return (mfib_entry->mfe_fib_index);
1509 mfib_entry_contribute_ip_forwarding (fib_node_index_t mfib_entry_index)
1511 mfib_entry_t *mfib_entry;
1513 mfib_entry = mfib_entry_get(mfib_entry_index);
1515 return (&mfib_entry->mfe_rep);
1519 mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1520 fib_forward_chain_type_t type,
1521 mfib_entry_fwd_flags_t flags,
1525 * An IP mFIB entry can only provide a forwarding chain that
1526 * is the same IP proto as the prefix.
1527 * No use-cases (i know of) for other combinations.
1529 mfib_entry_t *mfib_entry;
1532 mfib_entry = mfib_entry_get(mfib_entry_index);
1534 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1536 if (type == mfib_forw_chain_type_from_dpo_proto(dp))
1540 rep = replicate_get(mfib_entry->mfe_rep.dpoi_index);
1542 if ((rep->rep_flags & REPLICATE_FLAGS_HAS_LOCAL) &&
1543 (flags & MFIB_ENTRY_FWD_FLAG_NO_LOCAL))
1546 * caller does not want the local paths that the entry has
1548 dpo_proto_t rep_proto = rep->rep_proto;
1549 dpo_set(dpo, DPO_REPLICATE, rep_proto,
1550 replicate_dup(REPLICATE_FLAGS_NONE,
1551 mfib_entry->mfe_rep.dpoi_index));
1555 dpo_copy(dpo, &mfib_entry->mfe_rep);
1560 dpo_copy(dpo, drop_dpo_get(dp));
1565 * fib_entry_cover_changed
1567 * this entry is tracking its cover and that cover has changed.
1570 mfib_entry_cover_changed (fib_node_index_t mfib_entry_index)
1572 mfib_entry_t *mfib_entry;
1573 mfib_entry_src_t *msrc;
1576 mfib_entry = mfib_entry_get(mfib_entry_index);
1577 msrc = mfib_entry_get_best_src(mfib_entry);
1579 res = mfib_entry_src_cover_change(mfib_entry, msrc);
1581 if (MFIB_SRC_REEVALUATE == res)
1583 mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1585 MFIB_ENTRY_DBG(mfib_entry, "cover-changed");
1589 * mfib_entry_cover_updated
1591 * this entry is tracking its cover and that cover has been updated
1592 * (i.e. its forwarding information has changed).
1595 mfib_entry_cover_updated (fib_node_index_t mfib_entry_index)
1597 mfib_entry_t *mfib_entry;
1598 mfib_entry_src_t *msrc;
1601 mfib_entry = mfib_entry_get(mfib_entry_index);
1602 msrc = mfib_entry_get_best_src(mfib_entry);
1604 res = mfib_entry_src_cover_update(mfib_entry, msrc);
1606 if (MFIB_SRC_REEVALUATE == res)
1608 mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1610 MFIB_ENTRY_DBG(mfib_entry, "cover-updated");
1614 mfib_entry_pool_size (void)
1616 return (pool_elts(mfib_entry_pool));
1619 static clib_error_t *
1620 show_mfib_entry_command (vlib_main_t * vm,
1621 unformat_input_t * input,
1622 vlib_cli_command_t * cmd)
1624 fib_node_index_t fei;
1626 if (unformat (input, "%d", &fei))
1629 * show one in detail
1631 if (!pool_is_free_index(mfib_entry_pool, fei))
1633 vlib_cli_output (vm, "%d@%U",
1635 format_mfib_entry, fei,
1636 MFIB_ENTRY_FORMAT_DETAIL2);
1640 vlib_cli_output (vm, "entry %d invalid", fei);
1648 vlib_cli_output (vm, "FIB Entries:");
1649 pool_foreach_index(fei, mfib_entry_pool,
1651 vlib_cli_output (vm, "%d@%U",
1653 format_mfib_entry, fei,
1654 MFIB_ENTRY_FORMAT_BRIEF);
1662 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1663 * numerical indentifier.
1665 VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1666 .path = "show mfib entry",
1667 .function = show_mfib_entry_command,
1668 .short_help = "show mfib entry",