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/fib/fib_path_list.h>
21 #include <vnet/dpo/drop_dpo.h>
22 #include <vnet/dpo/replicate_dpo.h>
27 vlib_log_class_t mfib_entry_logger;
32 #define MFIB_ENTRY_DBG(_e, _fmt, _args...) \
34 vlib_log_debug(mfib_entry_logger, \
36 mfib_entry_get_index(_e), \
43 * MFIB extensions to each path
45 typedef struct mfib_path_ext_t_
47 mfib_itf_flags_t mfpe_flags;
48 fib_node_index_t mfpe_path;
52 * The source of an MFIB entry
54 typedef struct mfib_entry_src_t_
57 * Which source this is
59 mfib_source_t mfes_src;
64 mfib_entry_flags_t mfes_flags;
67 * The path-list of forwarding interfaces
69 fib_node_index_t mfes_pl;
74 fib_rpf_id_t mfes_rpf_id;
77 * Hash table of path extensions
79 mfib_path_ext_t *mfes_exts;
82 * The hash table of all interfaces.
83 * This is forwarding time information derived from the paths
84 * and their extensions.
86 mfib_itf_t *mfes_itfs;
90 * Pool of path extensions
92 static mfib_path_ext_t *mfib_path_ext_pool;
95 * String names for each source
97 static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
100 * Pool for all fib_entries
102 mfib_entry_t *mfib_entry_pool;
105 mfib_entry_get_node (fib_node_index_t index)
107 return ((fib_node_t*)mfib_entry_get(index));
110 static fib_protocol_t
111 mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
113 return (mfib_entry->mfe_prefix.fp_proto);
116 fib_forward_chain_type_t
117 mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
119 switch (mfib_entry->mfe_prefix.fp_proto)
121 case FIB_PROTOCOL_IP4:
122 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
123 case FIB_PROTOCOL_IP6:
124 return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
125 case FIB_PROTOCOL_MPLS:
129 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
133 format_mfib_entry_dpo (u8 * s, va_list * args)
135 index_t fei = va_arg(*args, index_t);
136 CLIB_UNUSED(u32 indent) = va_arg(*args, u32);
138 return (format(s, "%U",
139 format_mfib_entry, fei,
140 MFIB_ENTRY_FORMAT_BRIEF));
143 static inline mfib_path_ext_t *
144 mfib_entry_path_ext_get (index_t mi)
146 return (pool_elt_at_index(mfib_path_ext_pool, mi));
150 format_mfib_entry_path_ext (u8 * s, va_list * args)
152 mfib_path_ext_t *path_ext;
153 index_t mpi = va_arg(*args, index_t);
155 path_ext = mfib_entry_path_ext_get(mpi);
156 return (format(s, "path:%d flags:%U",
158 format_mfib_itf_flags, path_ext->mfpe_flags));
162 format_mfib_entry (u8 * s, va_list * args)
164 fib_node_index_t fei, mfi;
165 mfib_entry_t *mfib_entry;
166 mfib_entry_src_t *msrc;
170 fei = va_arg (*args, fib_node_index_t);
171 level = va_arg (*args, int);
172 mfib_entry = mfib_entry_get(fei);
174 s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
175 s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);
177 if (level >= MFIB_ENTRY_FORMAT_DETAIL)
179 fib_node_index_t path_index, mpi;
181 s = format (s, "\n");
182 s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
183 s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
184 s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
185 vec_foreach(msrc, mfib_entry->mfe_srcs)
187 s = format (s, " src:%s", mfib_source_names[msrc->mfes_src]);
188 s = format (s, ": %U\n", format_mfib_entry_flags, msrc->mfes_flags);
189 if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
191 s = fib_path_list_format(msrc->mfes_pl, s);
193 s = format (s, " Extensions:\n");
194 hash_foreach(path_index, mpi, msrc->mfes_exts,
196 s = format(s, " %U\n", format_mfib_entry_path_ext, mpi);
198 s = format (s, " Interface-Forwarding:\n");
199 hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
201 s = format(s, " %U\n", format_mfib_itf, mfi);
206 s = format(s, "\n Interfaces:");
207 hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
209 s = format(s, "\n %U", format_mfib_itf, mfi);
211 if (MFIB_RPF_ID_NONE != mfib_entry->mfe_rpf_id)
213 s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id);
215 s = format(s, "\n %U-chain\n %U",
216 format_fib_forw_chain_type,
217 mfib_entry_get_default_chain_type(mfib_entry),
219 &mfib_entry->mfe_rep,
223 if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
225 s = format(s, "\nchildren:");
226 s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
233 mfib_entry_from_fib_node (fib_node_t *node)
235 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
236 return ((mfib_entry_t*)node);
240 mfib_entry_src_cmp_for_sort (void * v1,
243 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
245 return (esrc1->mfes_src - esrc2->mfes_src);
249 mfib_entry_src_init (mfib_entry_t *mfib_entry,
250 mfib_source_t source)
253 mfib_entry_src_t esrc = {
254 .mfes_pl = FIB_NODE_INDEX_INVALID,
255 .mfes_flags = MFIB_ENTRY_FLAG_NONE,
259 vec_add1(mfib_entry->mfe_srcs, esrc);
260 vec_sort_with_function(mfib_entry->mfe_srcs,
261 mfib_entry_src_cmp_for_sort);
264 static mfib_entry_src_t *
265 mfib_entry_src_find (const mfib_entry_t *mfib_entry,
266 mfib_source_t source,
270 mfib_entry_src_t *esrc;
274 vec_foreach(esrc, mfib_entry->mfe_srcs)
276 if (esrc->mfes_src == source)
293 static mfib_entry_src_t *
294 mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
295 mfib_source_t source)
297 mfib_entry_src_t *esrc;
299 esrc = mfib_entry_src_find(mfib_entry, source, NULL);
303 mfib_entry_src_init(mfib_entry, source);
306 return (mfib_entry_src_find(mfib_entry, source, NULL));
309 static mfib_entry_src_t*
310 mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
312 mfib_entry_src_t *bsrc;
315 * the enum of sources is deliberately arranged in priority order
317 if (0 == vec_len(mfib_entry->mfe_srcs))
323 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
330 mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
331 mfib_source_t source)
333 mfib_entry_t *mfib_entry;
335 mfib_entry = mfib_entry_get(mfib_entry_index);
337 return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
341 mfib_entry_src_flush (mfib_entry_src_t *msrc)
346 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
348 mfib_itf_delete(mfib_itf_get(mfii));
350 hash_free(msrc->mfes_itfs);
351 msrc->mfes_itfs = NULL;
352 fib_path_list_unlock(msrc->mfes_pl);
356 mfib_entry_src_remove (mfib_entry_t *mfib_entry,
357 mfib_source_t source)
360 mfib_entry_src_t *msrc;
363 msrc = mfib_entry_src_find(mfib_entry, source, &index);
367 mfib_entry_src_flush(msrc);
368 vec_del1(mfib_entry->mfe_srcs, index);
373 mfib_entry_child_add (fib_node_index_t mfib_entry_index,
374 fib_node_type_t child_type,
375 fib_node_index_t child_index)
377 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
384 mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
387 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
392 static mfib_entry_t *
393 mfib_entry_alloc (u32 fib_index,
394 const mfib_prefix_t *prefix,
395 fib_node_index_t *mfib_entry_index)
397 mfib_entry_t *mfib_entry;
399 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
401 fib_node_init(&mfib_entry->mfe_node,
402 FIB_NODE_TYPE_MFIB_ENTRY);
405 * Some of the members require non-default initialisation
406 * so we also init those that don't and thus save on the call to clib_memset.
408 mfib_entry->mfe_flags = 0;
409 mfib_entry->mfe_fib_index = fib_index;
410 mfib_entry->mfe_prefix = *prefix;
411 mfib_entry->mfe_srcs = NULL;
412 mfib_entry->mfe_itfs = NULL;
413 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
414 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
416 dpo_reset(&mfib_entry->mfe_rep);
418 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
420 MFIB_ENTRY_DBG(mfib_entry, "alloc");
425 static inline mfib_path_ext_t *
426 mfib_entry_path_ext_find (mfib_path_ext_t *exts,
427 fib_node_index_t path_index)
431 p = hash_get(exts, path_index);
435 return (mfib_entry_path_ext_get(p[0]));
441 static mfib_path_ext_t*
442 mfib_path_ext_add (mfib_entry_src_t *msrc,
443 fib_node_index_t path_index,
444 mfib_itf_flags_t mfi_flags)
446 mfib_path_ext_t *path_ext;
448 pool_get(mfib_path_ext_pool, path_ext);
450 path_ext->mfpe_flags = mfi_flags;
451 path_ext->mfpe_path = path_index;
453 hash_set(msrc->mfes_exts, path_index,
454 path_ext - mfib_path_ext_pool);
460 mfib_path_ext_remove (mfib_entry_src_t *msrc,
461 fib_node_index_t path_index)
463 mfib_path_ext_t *path_ext;
465 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
467 hash_unset(msrc->mfes_exts, path_index);
468 pool_put(mfib_path_ext_pool, path_ext);
471 typedef struct mfib_entry_collect_forwarding_ctx_t_
473 load_balance_path_t * next_hops;
474 fib_forward_chain_type_t fct;
475 mfib_entry_src_t *msrc;
476 } mfib_entry_collect_forwarding_ctx_t;
478 static fib_path_list_walk_rc_t
479 mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
480 fib_node_index_t path_index,
483 mfib_entry_collect_forwarding_ctx_t *ctx;
484 load_balance_path_t *nh;
489 * if the path is not resolved, don't include it.
491 if (!fib_path_is_resolved(path_index))
493 return (FIB_PATH_LIST_WALK_CONTINUE);
497 * If the path is not forwarding to use it
499 mfib_path_ext_t *path_ext;
501 path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
504 if (NULL != path_ext &&
505 !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
507 return (FIB_PATH_LIST_WALK_CONTINUE);
512 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
513 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
515 * EOS traffic with no label to stack, we need the IP Adj
517 vec_add2(ctx->next_hops, nh, 1);
519 nh->path_index = path_index;
520 nh->path_weight = fib_path_get_weight(path_index);
521 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
524 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
525 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
526 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
527 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
528 case FIB_FORW_CHAIN_TYPE_ETHERNET:
529 case FIB_FORW_CHAIN_TYPE_NSH:
530 case FIB_FORW_CHAIN_TYPE_BIER:
535 return (FIB_PATH_LIST_WALK_CONTINUE);
539 mfib_entry_stack (mfib_entry_t *mfib_entry,
540 mfib_entry_src_t *msrc)
544 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
547 * unlink the enty from the previous path list.
549 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
551 fib_path_list_child_remove(mfib_entry->mfe_pl,
552 mfib_entry->mfe_sibling);
556 FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
558 mfib_entry_collect_forwarding_ctx_t ctx = {
560 .fct = mfib_entry_get_default_chain_type(mfib_entry),
564 fib_path_list_walk(msrc->mfes_pl,
565 mfib_entry_src_collect_forwarding,
568 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
570 if (NULL == ctx.next_hops)
573 * no next-hops, stack directly on the drop
575 dpo_stack(DPO_MFIB_ENTRY, dp,
576 &mfib_entry->mfe_rep,
582 * each path contirbutes a next-hop. form a replicate
583 * from those choices.
585 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
586 dpo_is_drop(&mfib_entry->mfe_rep))
588 dpo_id_t tmp_dpo = DPO_INVALID;
592 replicate_create(0, dp));
594 dpo_stack(DPO_MFIB_ENTRY, dp,
595 &mfib_entry->mfe_rep,
600 replicate_multipath_update(&mfib_entry->mfe_rep,
607 * for exclusive routes the source provided a replicate DPO
608 * we we stashed inthe special path list with one path
609 * so we can stack directly on that.
611 ASSERT(1 == vec_len(ctx.next_hops));
613 dpo_stack(DPO_MFIB_ENTRY, dp,
614 &mfib_entry->mfe_rep,
615 &ctx.next_hops[0].path_dpo);
616 dpo_reset(&ctx.next_hops[0].path_dpo);
617 vec_free(ctx.next_hops);
621 * link the entry to the path-list.
622 * The entry needs to be a child so that we receive the back-walk
623 * updates to recalculate forwarding.
625 mfib_entry->mfe_pl = msrc->mfes_pl;
626 mfib_entry->mfe_sibling =
627 fib_path_list_child_add(mfib_entry->mfe_pl,
628 FIB_NODE_TYPE_MFIB_ENTRY,
629 mfib_entry_get_index(mfib_entry));
633 dpo_stack(DPO_MFIB_ENTRY, dp,
634 &mfib_entry->mfe_rep,
639 static fib_node_index_t
640 mfib_entry_src_path_add (mfib_entry_src_t *msrc,
641 const fib_route_path_t *rpath)
643 fib_node_index_t path_index;
644 fib_route_path_t *rpaths;
646 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
649 * path-lists require a vector of paths
652 vec_add1(rpaths, rpath[0]);
654 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
656 /* A non-shared path-list */
657 msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
659 fib_path_list_lock(msrc->mfes_pl);
662 path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
669 static fib_node_index_t
670 mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
671 const fib_route_path_t *rpath)
673 fib_node_index_t path_index;
674 fib_route_path_t *rpaths;
676 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
679 * path-lists require a vector of paths
682 vec_add1(rpaths, rpath[0]);
684 path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
692 mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry)
694 mfib_entry_src_t *bsrc;
697 * copy the forwarding data from the bast source
699 bsrc = mfib_entry_get_best_src(mfib_entry);
703 mfib_entry->mfe_flags = bsrc->mfes_flags;
704 mfib_entry->mfe_itfs = bsrc->mfes_itfs;
705 mfib_entry->mfe_rpf_id = bsrc->mfes_rpf_id;
708 mfib_entry_stack(mfib_entry, bsrc);
713 mfib_entry_create (u32 fib_index,
714 mfib_source_t source,
715 const mfib_prefix_t *prefix,
717 mfib_entry_flags_t entry_flags)
719 fib_node_index_t mfib_entry_index;
720 mfib_entry_t *mfib_entry;
721 mfib_entry_src_t *msrc;
723 mfib_entry = mfib_entry_alloc(fib_index, prefix,
725 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
726 msrc->mfes_flags = entry_flags;
727 msrc->mfes_rpf_id = rpf_id;
729 mfib_entry_recalculate_forwarding(mfib_entry);
731 return (mfib_entry_index);
735 mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
737 return (0 == vec_len(mfib_entry->mfe_srcs));
741 mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
743 return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
744 0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
748 mfib_entry_update (fib_node_index_t mfib_entry_index,
749 mfib_source_t source,
750 mfib_entry_flags_t entry_flags,
754 mfib_entry_t *mfib_entry;
755 mfib_entry_src_t *msrc;
757 mfib_entry = mfib_entry_get(mfib_entry_index);
758 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
759 msrc->mfes_flags = entry_flags;
760 msrc->mfes_rpf_id = rpf_id;
762 if (INDEX_INVALID != repi)
765 * The source is providing its own replicate DPO.
766 * Create a sepcial path-list to manage it, that way
767 * this entry and the source are equivalent to a normal
770 fib_node_index_t old_pl_index;
772 dpo_id_t dpo = DPO_INVALID;
774 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
775 old_pl_index = msrc->mfes_pl;
777 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
780 fib_path_list_create_special(dp,
781 FIB_PATH_LIST_FLAG_EXCLUSIVE,
785 fib_path_list_lock(msrc->mfes_pl);
786 fib_path_list_unlock(old_pl_index);
789 if (mfib_entry_src_ok_for_delete(msrc))
792 * this source has no interfaces and no flags.
793 * it has nothing left to give - remove it
795 mfib_entry_src_remove(mfib_entry, source);
798 mfib_entry_recalculate_forwarding(mfib_entry);
800 return (mfib_entry_ok_for_delete(mfib_entry));
804 mfib_entry_itf_add (mfib_entry_src_t *msrc,
808 hash_set(msrc->mfes_itfs, sw_if_index, mi);
812 mfib_entry_itf_remove (mfib_entry_src_t *msrc,
817 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
819 mfib_itf_delete(mfi);
821 hash_unset(msrc->mfes_itfs, sw_if_index);
825 mfib_entry_path_update (fib_node_index_t mfib_entry_index,
826 mfib_source_t source,
827 const fib_route_path_t *rpath,
828 mfib_itf_flags_t itf_flags)
830 fib_node_index_t path_index;
831 mfib_path_ext_t *path_ext;
832 mfib_entry_t *mfib_entry;
833 mfib_entry_src_t *msrc;
834 mfib_itf_flags_t old;
836 mfib_entry = mfib_entry_get(mfib_entry_index);
837 ASSERT(NULL != mfib_entry);
838 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
841 * add the path to the path-list. If it's a duplicate we'll get
842 * back the original path.
844 path_index = mfib_entry_src_path_add(msrc, rpath);
847 * find the path extension for that path
849 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
851 if (NULL == path_ext)
853 old = MFIB_ITF_FLAG_NONE;
854 path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
858 old = path_ext->mfpe_flags;
859 path_ext->mfpe_flags = itf_flags;
863 * Has the path changed its contribution to the input interface set.
864 * Which only paths with interfaces can do...
866 if (~0 != rpath[0].frp_sw_if_index)
868 mfib_itf_t *mfib_itf;
870 if (old != itf_flags)
873 * change of flag contributions
875 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
876 rpath[0].frp_sw_if_index);
878 if (NULL == mfib_itf)
880 mfib_entry_itf_add(msrc,
881 rpath[0].frp_sw_if_index,
882 mfib_itf_create(path_index, itf_flags));
886 if (mfib_itf_update(mfib_itf,
891 * no more interface flags on this path, remove
892 * from the data-plane set
894 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
900 mfib_entry_recalculate_forwarding(mfib_entry);
904 * mfib_entry_path_remove
906 * remove a path from the entry.
907 * return the mfib_entry's index if it is still present, INVALID otherwise.
910 mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
911 mfib_source_t source,
912 const fib_route_path_t *rpath)
914 fib_node_index_t path_index;
915 mfib_entry_t *mfib_entry;
916 mfib_entry_src_t *msrc;
918 mfib_entry = mfib_entry_get(mfib_entry_index);
919 ASSERT(NULL != mfib_entry);
920 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
925 * there are no paths left for this source
927 return (mfib_entry_ok_for_delete(mfib_entry));
931 * remove the path from the path-list. If it's not there we'll get
934 path_index = mfib_entry_src_path_remove(msrc, rpath);
936 if (FIB_NODE_INDEX_INVALID != path_index)
939 * don't need the extension, nor the interface anymore
941 mfib_path_ext_remove(msrc, path_index);
942 if (~0 != rpath[0].frp_sw_if_index)
944 mfib_itf_t *mfib_itf;
946 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
947 rpath[0].frp_sw_if_index);
949 if (mfib_itf_update(mfib_itf,
954 * no more interface flags on this path, remove
955 * from the data-plane set
957 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
962 if (mfib_entry_src_ok_for_delete(msrc))
965 * this source has no interfaces and no flags.
966 * it has nothing left to give - remove it
968 mfib_entry_src_remove(mfib_entry, source);
971 mfib_entry_recalculate_forwarding(mfib_entry);
973 return (mfib_entry_ok_for_delete(mfib_entry));
979 * The source is withdrawing all the paths it provided
982 mfib_entry_delete (fib_node_index_t mfib_entry_index,
983 mfib_source_t source)
985 mfib_entry_t *mfib_entry;
987 mfib_entry = mfib_entry_get(mfib_entry_index);
988 mfib_entry_src_remove(mfib_entry, source);
990 mfib_entry_recalculate_forwarding(mfib_entry);
992 return (mfib_entry_ok_for_delete(mfib_entry));
996 fib_ip4_address_compare (ip4_address_t * a1,
1000 * IP addresses are unsiged ints. the return value here needs to be signed
1001 * a simple subtraction won't cut it.
1002 * If the addresses are the same, the sort order is undefiend, so phoey.
1004 return ((clib_net_to_host_u32(a1->data_u32) >
1005 clib_net_to_host_u32(a2->data_u32) ) ?
1010 fib_ip6_address_compare (ip6_address_t * a1,
1014 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1016 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1017 clib_net_to_host_u16 (a2->as_u16[i]));
1025 mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1026 fib_node_index_t mfib_entry_index2)
1028 mfib_entry_t *mfib_entry1, *mfib_entry2;
1031 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1032 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1034 switch (mfib_entry1->mfe_prefix.fp_proto)
1036 case FIB_PROTOCOL_IP4:
1037 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1038 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1042 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1043 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1046 case FIB_PROTOCOL_IP6:
1047 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1048 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1052 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1053 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1056 case FIB_PROTOCOL_MPLS:
1063 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1069 mfib_entry_cmp_for_sort (void *i1, void *i2)
1071 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1073 return (mfib_entry_cmp(*mfib_entry_index1,
1074 *mfib_entry_index2));
1078 mfib_entry_last_lock_gone (fib_node_t *node)
1080 mfib_entry_t *mfib_entry;
1081 mfib_entry_src_t *msrc;
1083 mfib_entry = mfib_entry_from_fib_node(node);
1085 dpo_reset(&mfib_entry->mfe_rep);
1087 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1089 vec_foreach(msrc, mfib_entry->mfe_srcs)
1091 mfib_entry_src_flush(msrc);
1094 vec_free(mfib_entry->mfe_srcs);
1096 fib_node_deinit(&mfib_entry->mfe_node);
1097 pool_put(mfib_entry_pool, mfib_entry);
1101 mfib_entry_get_stats_index (fib_node_index_t fib_entry_index)
1103 mfib_entry_t *mfib_entry;
1105 mfib_entry = mfib_entry_get(fib_entry_index);
1107 return (mfib_entry->mfe_rep.dpoi_index);
1111 * mfib_entry_back_walk_notify
1113 * A back walk has reach this entry.
1115 static fib_node_back_walk_rc_t
1116 mfib_entry_back_walk_notify (fib_node_t *node,
1117 fib_node_back_walk_ctx_t *ctx)
1119 mfib_entry_recalculate_forwarding(mfib_entry_from_fib_node(node));
1121 return (FIB_NODE_BACK_WALK_CONTINUE);
1125 mfib_entry_show_memory (void)
1127 fib_show_memory_usage("multicast-Entry",
1128 pool_elts(mfib_entry_pool),
1129 pool_len(mfib_entry_pool),
1130 sizeof(mfib_entry_t));
1134 * The MFIB entry's graph node virtual function table
1136 static const fib_node_vft_t mfib_entry_vft = {
1137 .fnv_get = mfib_entry_get_node,
1138 .fnv_last_lock = mfib_entry_last_lock_gone,
1139 .fnv_back_walk = mfib_entry_back_walk_notify,
1140 .fnv_mem_show = mfib_entry_show_memory,
1144 mfib_entry_lock (fib_node_index_t mfib_entry_index)
1146 mfib_entry_t *mfib_entry;
1148 mfib_entry = mfib_entry_get(mfib_entry_index);
1150 fib_node_lock(&mfib_entry->mfe_node);
1154 mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1156 mfib_entry_t *mfib_entry;
1158 mfib_entry = mfib_entry_get(mfib_entry_index);
1160 fib_node_unlock(&mfib_entry->mfe_node);
1164 mfib_entry_dpo_lock (dpo_id_t *dpo)
1168 mfib_entry_dpo_unlock (dpo_id_t *dpo)
1172 const static dpo_vft_t mfib_entry_dpo_vft = {
1173 .dv_lock = mfib_entry_dpo_lock,
1174 .dv_unlock = mfib_entry_dpo_unlock,
1175 .dv_format = format_mfib_entry_dpo,
1176 .dv_mem_show = mfib_entry_show_memory,
1179 const static char* const mfib_entry_ip4_nodes[] =
1181 "ip4-mfib-forward-rpf",
1184 const static char* const mfib_entry_ip6_nodes[] =
1186 "ip6-mfib-forward-rpf",
1190 const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1192 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1193 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1197 mfib_entry_module_init (void)
1199 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1200 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1201 mfib_entry_logger = vlib_log_register_class("mfib", "entry");
1205 mfib_entry_encode (fib_node_index_t mfib_entry_index,
1206 fib_route_path_encode_t **api_rpaths)
1208 mfib_entry_t *mfib_entry;
1209 mfib_entry_src_t *bsrc;
1211 mfib_entry = mfib_entry_get(mfib_entry_index);
1212 bsrc = mfib_entry_get_best_src(mfib_entry);
1214 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
1216 fib_path_list_walk(bsrc->mfes_pl,
1224 mfib_entry_get_prefix (fib_node_index_t mfib_entry_index,
1227 mfib_entry_t *mfib_entry;
1229 mfib_entry = mfib_entry_get(mfib_entry_index);
1230 *pfx = mfib_entry->mfe_prefix;
1234 mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1236 mfib_entry_t *mfib_entry;
1238 mfib_entry = mfib_entry_get(mfib_entry_index);
1240 return (mfib_entry->mfe_fib_index);
1244 mfib_entry_contribute_ip_forwarding (fib_node_index_t mfib_entry_index)
1246 mfib_entry_t *mfib_entry;
1248 mfib_entry = mfib_entry_get(mfib_entry_index);
1250 return (&mfib_entry->mfe_rep);
1254 mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1255 fib_forward_chain_type_t type,
1259 * An IP mFIB entry can only provide a forwarding chain that
1260 * is the same IP proto as the prefix.
1261 * No use-cases (i know of) for other combinations.
1263 mfib_entry_t *mfib_entry;
1266 mfib_entry = mfib_entry_get(mfib_entry_index);
1268 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1270 if (type == fib_forw_chain_type_from_dpo_proto(dp))
1272 dpo_copy(dpo, &mfib_entry->mfe_rep);
1276 dpo_copy(dpo, drop_dpo_get(dp));
1281 mfib_entry_pool_size (void)
1283 return (pool_elts(mfib_entry_pool));
1286 static clib_error_t *
1287 show_mfib_entry_command (vlib_main_t * vm,
1288 unformat_input_t * input,
1289 vlib_cli_command_t * cmd)
1291 fib_node_index_t fei;
1293 if (unformat (input, "%d", &fei))
1296 * show one in detail
1298 if (!pool_is_free_index(mfib_entry_pool, fei))
1300 vlib_cli_output (vm, "%d@%U",
1302 format_mfib_entry, fei,
1303 MFIB_ENTRY_FORMAT_DETAIL2);
1307 vlib_cli_output (vm, "entry %d invalid", fei);
1315 vlib_cli_output (vm, "FIB Entries:");
1316 pool_foreach_index(fei, mfib_entry_pool,
1318 vlib_cli_output (vm, "%d@%U",
1320 format_mfib_entry, fei,
1321 MFIB_ENTRY_FORMAT_BRIEF);
1329 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1330 * numerical indentifier.
1332 VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1333 .path = "show mfib entry",
1334 .function = show_mfib_entry_command,
1335 .short_help = "show mfib entry",