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>
28 #DEFIne MFIB_ENTRY_DBG(_e, _fmt, _args...) \
31 __tmp = format(__tmp, "e:[%d:%U", \
32 mfib_entry_get_index(_e), \
33 format_ip46_address, \
34 &_e->mfe_prefix.fp_grp_addr, \
36 __tmp = format(__tmp, "/%d,", \
37 _e->mfe_prefix.fp_len); \
38 __tmp = format(__tmp, "%U]", \
39 mfib_entry_get_index(_e), \
40 format_ip46_address, \
41 &_e->mfe_prefix.fp_src_addr, \
43 __tmp = format(__tmp, _fmt, ##_args); \
44 clib_warning("%s", __tmp); \
48 #define MFIB_ENTRY_DBG(_e, _fmt, _args...)
52 * MFIB extensions to each path
54 typedef struct mfib_path_ext_t_
56 mfib_itf_flags_t mfpe_flags;
57 fib_node_index_t mfpe_path;
61 * The source of an MFIB entry
63 typedef struct mfib_entry_src_t_
66 * Which source this is
68 mfib_source_t mfes_src;
73 mfib_entry_flags_t mfes_flags;
76 * The path-list of forwarding interfaces
78 fib_node_index_t mfes_pl;
83 fib_rpf_id_t mfes_rpf_id;
86 * Hash table of path extensions
88 mfib_path_ext_t *mfes_exts;
91 * The hash table of all interfaces.
92 * This is forwarding time information derived from the paths
93 * and their extensions.
95 mfib_itf_t *mfes_itfs;
99 * Pool of path extensions
101 static mfib_path_ext_t *mfib_path_ext_pool;
104 * String names for each source
106 static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
109 * Pool for all fib_entries
111 mfib_entry_t *mfib_entry_pool;
114 mfib_entry_get_node (fib_node_index_t index)
116 return ((fib_node_t*)mfib_entry_get(index));
119 static fib_protocol_t
120 mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
122 return (mfib_entry->mfe_prefix.fp_proto);
125 fib_forward_chain_type_t
126 mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
128 switch (mfib_entry->mfe_prefix.fp_proto)
130 case FIB_PROTOCOL_IP4:
131 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
132 case FIB_PROTOCOL_IP6:
133 return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
134 case FIB_PROTOCOL_MPLS:
138 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
142 format_mfib_entry_dpo (u8 * s, va_list * args)
144 index_t fei = va_arg(*args, index_t);
145 CLIB_UNUSED(u32 indent) = va_arg(*args, u32);
147 return (format(s, "%U",
148 format_mfib_entry, fei,
149 MFIB_ENTRY_FORMAT_BRIEF));
152 static inline mfib_path_ext_t *
153 mfib_entry_path_ext_get (index_t mi)
155 return (pool_elt_at_index(mfib_path_ext_pool, mi));
159 format_mfib_entry_path_ext (u8 * s, va_list * args)
161 mfib_path_ext_t *path_ext;
162 index_t mpi = va_arg(*args, index_t);
164 path_ext = mfib_entry_path_ext_get(mpi);
165 return (format(s, "path:%d flags:%U",
167 format_mfib_itf_flags, path_ext->mfpe_flags));
171 format_mfib_entry (u8 * s, va_list * args)
173 fib_node_index_t fei, mfi;
174 mfib_entry_t *mfib_entry;
175 mfib_entry_src_t *msrc;
179 fei = va_arg (*args, fib_node_index_t);
180 level = va_arg (*args, int);
181 mfib_entry = mfib_entry_get(fei);
183 s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
184 s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);
186 if (level >= MFIB_ENTRY_FORMAT_DETAIL)
188 fib_node_index_t path_index, mpi;
190 s = format (s, "\n");
191 s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
192 s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
193 s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
194 vec_foreach(msrc, mfib_entry->mfe_srcs)
196 s = format (s, " src:%s", mfib_source_names[msrc->mfes_src]);
197 s = format (s, ": %U\n", format_mfib_entry_flags, msrc->mfes_flags);
198 if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
200 s = fib_path_list_format(msrc->mfes_pl, s);
202 s = format (s, " Extensions:\n");
203 hash_foreach(path_index, mpi, msrc->mfes_exts,
205 s = format(s, " %U\n", format_mfib_entry_path_ext, mpi);
207 s = format (s, " Interface-Forwarding:\n");
208 hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
210 s = format(s, " %U\n", format_mfib_itf, mfi);
215 s = format(s, "\n Interfaces:");
216 hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
218 s = format(s, "\n %U", format_mfib_itf, mfi);
220 if (MFIB_RPF_ID_NONE != mfib_entry->mfe_rpf_id)
222 s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id);
224 s = format(s, "\n %U-chain\n %U",
225 format_fib_forw_chain_type,
226 mfib_entry_get_default_chain_type(mfib_entry),
228 &mfib_entry->mfe_rep,
232 if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
234 s = format(s, "\nchildren:");
235 s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
242 mfib_entry_from_fib_node (fib_node_t *node)
244 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
245 return ((mfib_entry_t*)node);
249 mfib_entry_src_cmp_for_sort (void * v1,
252 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
254 return (esrc1->mfes_src - esrc2->mfes_src);
258 mfib_entry_src_init (mfib_entry_t *mfib_entry,
259 mfib_source_t source)
262 mfib_entry_src_t esrc = {
263 .mfes_pl = FIB_NODE_INDEX_INVALID,
264 .mfes_flags = MFIB_ENTRY_FLAG_NONE,
268 vec_add1(mfib_entry->mfe_srcs, esrc);
269 vec_sort_with_function(mfib_entry->mfe_srcs,
270 mfib_entry_src_cmp_for_sort);
273 static mfib_entry_src_t *
274 mfib_entry_src_find (const mfib_entry_t *mfib_entry,
275 mfib_source_t source,
279 mfib_entry_src_t *esrc;
283 vec_foreach(esrc, mfib_entry->mfe_srcs)
285 if (esrc->mfes_src == source)
302 static mfib_entry_src_t *
303 mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
304 mfib_source_t source)
306 mfib_entry_src_t *esrc;
308 esrc = mfib_entry_src_find(mfib_entry, source, NULL);
312 mfib_entry_src_init(mfib_entry, source);
315 return (mfib_entry_src_find(mfib_entry, source, NULL));
318 static mfib_entry_src_t*
319 mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
321 mfib_entry_src_t *bsrc;
324 * the enum of sources is deliberately arranged in priority order
326 if (0 == vec_len(mfib_entry->mfe_srcs))
332 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
339 mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
340 mfib_source_t source)
342 mfib_entry_t *mfib_entry;
344 mfib_entry = mfib_entry_get(mfib_entry_index);
346 return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
350 mfib_entry_src_flush (mfib_entry_src_t *msrc)
355 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
357 mfib_itf_delete(mfib_itf_get(mfii));
359 hash_free(msrc->mfes_itfs);
360 msrc->mfes_itfs = NULL;
361 fib_path_list_unlock(msrc->mfes_pl);
365 mfib_entry_src_remove (mfib_entry_t *mfib_entry,
366 mfib_source_t source)
369 mfib_entry_src_t *msrc;
372 msrc = mfib_entry_src_find(mfib_entry, source, &index);
376 mfib_entry_src_flush(msrc);
377 vec_del1(mfib_entry->mfe_srcs, index);
382 mfib_entry_child_add (fib_node_index_t mfib_entry_index,
383 fib_node_type_t child_type,
384 fib_node_index_t child_index)
386 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
393 mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
396 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
401 static mfib_entry_t *
402 mfib_entry_alloc (u32 fib_index,
403 const mfib_prefix_t *prefix,
404 fib_node_index_t *mfib_entry_index)
406 mfib_entry_t *mfib_entry;
408 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
410 fib_node_init(&mfib_entry->mfe_node,
411 FIB_NODE_TYPE_MFIB_ENTRY);
414 * Some of the members require non-default initialisation
415 * so we also init those that don't and thus save on the call to memset.
417 mfib_entry->mfe_flags = 0;
418 mfib_entry->mfe_fib_index = fib_index;
419 mfib_entry->mfe_prefix = *prefix;
420 mfib_entry->mfe_srcs = NULL;
421 mfib_entry->mfe_itfs = NULL;
422 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
423 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
425 dpo_reset(&mfib_entry->mfe_rep);
427 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
429 MFIB_ENTRY_DBG(mfib_entry, "alloc");
434 static inline mfib_path_ext_t *
435 mfib_entry_path_ext_find (mfib_path_ext_t *exts,
436 fib_node_index_t path_index)
440 p = hash_get(exts, path_index);
444 return (mfib_entry_path_ext_get(p[0]));
450 static mfib_path_ext_t*
451 mfib_path_ext_add (mfib_entry_src_t *msrc,
452 fib_node_index_t path_index,
453 mfib_itf_flags_t mfi_flags)
455 mfib_path_ext_t *path_ext;
457 pool_get(mfib_path_ext_pool, path_ext);
459 path_ext->mfpe_flags = mfi_flags;
460 path_ext->mfpe_path = path_index;
462 hash_set(msrc->mfes_exts, path_index,
463 path_ext - mfib_path_ext_pool);
469 mfib_path_ext_remove (mfib_entry_src_t *msrc,
470 fib_node_index_t path_index)
472 mfib_path_ext_t *path_ext;
474 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
476 hash_unset(msrc->mfes_exts, path_index);
477 pool_put(mfib_path_ext_pool, path_ext);
480 typedef struct mfib_entry_collect_forwarding_ctx_t_
482 load_balance_path_t * next_hops;
483 fib_forward_chain_type_t fct;
484 mfib_entry_src_t *msrc;
485 } mfib_entry_collect_forwarding_ctx_t;
487 static fib_path_list_walk_rc_t
488 mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
489 fib_node_index_t path_index,
492 mfib_entry_collect_forwarding_ctx_t *ctx;
493 load_balance_path_t *nh;
498 * if the path is not resolved, don't include it.
500 if (!fib_path_is_resolved(path_index))
502 return (FIB_PATH_LIST_WALK_CONTINUE);
506 * If the path is not forwarding to use it
508 mfib_path_ext_t *path_ext;
510 path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
513 if (NULL != path_ext &&
514 !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
516 return (FIB_PATH_LIST_WALK_CONTINUE);
521 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
522 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
524 * EOS traffic with no label to stack, we need the IP Adj
526 vec_add2(ctx->next_hops, nh, 1);
528 nh->path_index = path_index;
529 nh->path_weight = fib_path_get_weight(path_index);
530 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
533 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
534 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
535 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
536 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
537 case FIB_FORW_CHAIN_TYPE_ETHERNET:
538 case FIB_FORW_CHAIN_TYPE_NSH:
539 case FIB_FORW_CHAIN_TYPE_BIER:
544 return (FIB_PATH_LIST_WALK_CONTINUE);
548 mfib_entry_stack (mfib_entry_t *mfib_entry,
549 mfib_entry_src_t *msrc)
553 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
556 * unlink the enty from the previous path list.
558 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
560 fib_path_list_child_remove(mfib_entry->mfe_pl,
561 mfib_entry->mfe_sibling);
565 FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
567 mfib_entry_collect_forwarding_ctx_t ctx = {
569 .fct = mfib_entry_get_default_chain_type(mfib_entry),
573 fib_path_list_walk(msrc->mfes_pl,
574 mfib_entry_src_collect_forwarding,
577 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
579 if (NULL == ctx.next_hops)
582 * no next-hops, stack directly on the drop
584 dpo_stack(DPO_MFIB_ENTRY, dp,
585 &mfib_entry->mfe_rep,
591 * each path contirbutes a next-hop. form a replicate
592 * from those choices.
594 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
595 dpo_is_drop(&mfib_entry->mfe_rep))
597 dpo_id_t tmp_dpo = DPO_INVALID;
601 replicate_create(0, dp));
603 dpo_stack(DPO_MFIB_ENTRY, dp,
604 &mfib_entry->mfe_rep,
609 replicate_multipath_update(&mfib_entry->mfe_rep,
616 * for exclusive routes the source provided a replicate DPO
617 * we we stashed inthe special path list with one path
618 * so we can stack directly on that.
620 ASSERT(1 == vec_len(ctx.next_hops));
622 dpo_stack(DPO_MFIB_ENTRY, dp,
623 &mfib_entry->mfe_rep,
624 &ctx.next_hops[0].path_dpo);
625 dpo_reset(&ctx.next_hops[0].path_dpo);
626 vec_free(ctx.next_hops);
630 * link the entry to the path-list.
631 * The entry needs to be a child so that we receive the back-walk
632 * updates to recalculate forwarding.
634 mfib_entry->mfe_pl = msrc->mfes_pl;
635 mfib_entry->mfe_sibling =
636 fib_path_list_child_add(mfib_entry->mfe_pl,
637 FIB_NODE_TYPE_MFIB_ENTRY,
638 mfib_entry_get_index(mfib_entry));
642 dpo_stack(DPO_MFIB_ENTRY, dp,
643 &mfib_entry->mfe_rep,
648 static fib_node_index_t
649 mfib_entry_src_path_add (mfib_entry_src_t *msrc,
650 const fib_route_path_t *rpath)
652 fib_node_index_t path_index;
653 fib_route_path_t *rpaths;
655 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
658 * path-lists require a vector of paths
661 vec_add1(rpaths, rpath[0]);
663 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
665 /* A non-shared path-list */
666 msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
668 fib_path_list_lock(msrc->mfes_pl);
671 path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
678 static fib_node_index_t
679 mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
680 const fib_route_path_t *rpath)
682 fib_node_index_t path_index;
683 fib_route_path_t *rpaths;
685 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
688 * path-lists require a vector of paths
691 vec_add1(rpaths, rpath[0]);
693 path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
701 mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry)
703 mfib_entry_src_t *bsrc;
706 * copy the forwarding data from the bast source
708 bsrc = mfib_entry_get_best_src(mfib_entry);
712 mfib_entry->mfe_flags = bsrc->mfes_flags;
713 mfib_entry->mfe_itfs = bsrc->mfes_itfs;
714 mfib_entry->mfe_rpf_id = bsrc->mfes_rpf_id;
717 mfib_entry_stack(mfib_entry, bsrc);
722 mfib_entry_create (u32 fib_index,
723 mfib_source_t source,
724 const mfib_prefix_t *prefix,
726 mfib_entry_flags_t entry_flags)
728 fib_node_index_t mfib_entry_index;
729 mfib_entry_t *mfib_entry;
730 mfib_entry_src_t *msrc;
732 mfib_entry = mfib_entry_alloc(fib_index, prefix,
734 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
735 msrc->mfes_flags = entry_flags;
736 msrc->mfes_rpf_id = rpf_id;
738 mfib_entry_recalculate_forwarding(mfib_entry);
740 return (mfib_entry_index);
744 mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
746 return (0 == vec_len(mfib_entry->mfe_srcs));
750 mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
752 return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
753 0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
757 mfib_entry_update (fib_node_index_t mfib_entry_index,
758 mfib_source_t source,
759 mfib_entry_flags_t entry_flags,
763 mfib_entry_t *mfib_entry;
764 mfib_entry_src_t *msrc;
766 mfib_entry = mfib_entry_get(mfib_entry_index);
767 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
768 msrc->mfes_flags = entry_flags;
769 msrc->mfes_rpf_id = rpf_id;
771 if (INDEX_INVALID != repi)
774 * The source is providing its own replicate DPO.
775 * Create a sepcial path-list to manage it, that way
776 * this entry and the source are equivalent to a normal
779 fib_node_index_t old_pl_index;
781 dpo_id_t dpo = DPO_INVALID;
783 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
784 old_pl_index = msrc->mfes_pl;
786 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
789 fib_path_list_create_special(dp,
790 FIB_PATH_LIST_FLAG_EXCLUSIVE,
794 fib_path_list_lock(msrc->mfes_pl);
795 fib_path_list_unlock(old_pl_index);
798 if (mfib_entry_src_ok_for_delete(msrc))
801 * this source has no interfaces and no flags.
802 * it has nothing left to give - remove it
804 mfib_entry_src_remove(mfib_entry, source);
807 mfib_entry_recalculate_forwarding(mfib_entry);
809 return (mfib_entry_ok_for_delete(mfib_entry));
813 mfib_entry_itf_add (mfib_entry_src_t *msrc,
817 hash_set(msrc->mfes_itfs, sw_if_index, mi);
821 mfib_entry_itf_remove (mfib_entry_src_t *msrc,
826 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
828 mfib_itf_delete(mfi);
830 hash_unset(msrc->mfes_itfs, sw_if_index);
834 mfib_entry_path_update (fib_node_index_t mfib_entry_index,
835 mfib_source_t source,
836 const fib_route_path_t *rpath,
837 mfib_itf_flags_t itf_flags)
839 fib_node_index_t path_index;
840 mfib_path_ext_t *path_ext;
841 mfib_entry_t *mfib_entry;
842 mfib_entry_src_t *msrc;
843 mfib_itf_flags_t old;
845 mfib_entry = mfib_entry_get(mfib_entry_index);
846 ASSERT(NULL != mfib_entry);
847 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
850 * add the path to the path-list. If it's a duplicate we'll get
851 * back the original path.
853 path_index = mfib_entry_src_path_add(msrc, rpath);
856 * find the path extension for that path
858 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
860 if (NULL == path_ext)
862 old = MFIB_ITF_FLAG_NONE;
863 path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
867 old = path_ext->mfpe_flags;
868 path_ext->mfpe_flags = itf_flags;
872 * Has the path changed its contribution to the input interface set.
873 * Which only paths with interfaces can do...
875 if (~0 != rpath[0].frp_sw_if_index)
877 mfib_itf_t *mfib_itf;
879 if (old != itf_flags)
882 * change of flag contributions
884 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
885 rpath[0].frp_sw_if_index);
887 if (NULL == mfib_itf)
889 mfib_entry_itf_add(msrc,
890 rpath[0].frp_sw_if_index,
891 mfib_itf_create(path_index, itf_flags));
895 if (mfib_itf_update(mfib_itf,
900 * no more interface flags on this path, remove
901 * from the data-plane set
903 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
909 mfib_entry_recalculate_forwarding(mfib_entry);
913 * mfib_entry_path_remove
915 * remove a path from the entry.
916 * return the mfib_entry's index if it is still present, INVALID otherwise.
919 mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
920 mfib_source_t source,
921 const fib_route_path_t *rpath)
923 fib_node_index_t path_index;
924 mfib_entry_t *mfib_entry;
925 mfib_entry_src_t *msrc;
927 mfib_entry = mfib_entry_get(mfib_entry_index);
928 ASSERT(NULL != mfib_entry);
929 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
934 * there are no paths left for this source
936 return (mfib_entry_ok_for_delete(mfib_entry));
940 * remove the path from the path-list. If it's not there we'll get
943 path_index = mfib_entry_src_path_remove(msrc, rpath);
945 if (FIB_NODE_INDEX_INVALID != path_index)
948 * don't need the extension, nor the interface anymore
950 mfib_path_ext_remove(msrc, path_index);
951 if (~0 != rpath[0].frp_sw_if_index)
953 mfib_itf_t *mfib_itf;
955 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
956 rpath[0].frp_sw_if_index);
958 if (mfib_itf_update(mfib_itf,
963 * no more interface flags on this path, remove
964 * from the data-plane set
966 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
971 if (mfib_entry_src_ok_for_delete(msrc))
974 * this source has no interfaces and no flags.
975 * it has nothing left to give - remove it
977 mfib_entry_src_remove(mfib_entry, source);
980 mfib_entry_recalculate_forwarding(mfib_entry);
982 return (mfib_entry_ok_for_delete(mfib_entry));
988 * The source is withdrawing all the paths it provided
991 mfib_entry_delete (fib_node_index_t mfib_entry_index,
992 mfib_source_t source)
994 mfib_entry_t *mfib_entry;
996 mfib_entry = mfib_entry_get(mfib_entry_index);
997 mfib_entry_src_remove(mfib_entry, source);
999 mfib_entry_recalculate_forwarding(mfib_entry);
1001 return (mfib_entry_ok_for_delete(mfib_entry));
1005 fib_ip4_address_compare (ip4_address_t * a1,
1009 * IP addresses are unsiged ints. the return value here needs to be signed
1010 * a simple subtraction won't cut it.
1011 * If the addresses are the same, the sort order is undefiend, so phoey.
1013 return ((clib_net_to_host_u32(a1->data_u32) >
1014 clib_net_to_host_u32(a2->data_u32) ) ?
1019 fib_ip6_address_compare (ip6_address_t * a1,
1023 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1025 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1026 clib_net_to_host_u16 (a2->as_u16[i]));
1034 mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1035 fib_node_index_t mfib_entry_index2)
1037 mfib_entry_t *mfib_entry1, *mfib_entry2;
1040 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1041 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1043 switch (mfib_entry1->mfe_prefix.fp_proto)
1045 case FIB_PROTOCOL_IP4:
1046 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1047 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1051 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1052 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1055 case FIB_PROTOCOL_IP6:
1056 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1057 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1061 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1062 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1065 case FIB_PROTOCOL_MPLS:
1072 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1078 mfib_entry_cmp_for_sort (void *i1, void *i2)
1080 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1082 return (mfib_entry_cmp(*mfib_entry_index1,
1083 *mfib_entry_index2));
1087 mfib_entry_last_lock_gone (fib_node_t *node)
1089 mfib_entry_t *mfib_entry;
1090 mfib_entry_src_t *msrc;
1092 mfib_entry = mfib_entry_from_fib_node(node);
1094 dpo_reset(&mfib_entry->mfe_rep);
1096 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1098 vec_foreach(msrc, mfib_entry->mfe_srcs)
1100 mfib_entry_src_flush(msrc);
1103 vec_free(mfib_entry->mfe_srcs);
1105 fib_node_deinit(&mfib_entry->mfe_node);
1106 pool_put(mfib_entry_pool, mfib_entry);
1110 mfib_entry_get_stats_index (fib_node_index_t fib_entry_index)
1112 mfib_entry_t *mfib_entry;
1114 mfib_entry = mfib_entry_get(fib_entry_index);
1116 return (mfib_entry->mfe_rep.dpoi_index);
1120 * mfib_entry_back_walk_notify
1122 * A back walk has reach this entry.
1124 static fib_node_back_walk_rc_t
1125 mfib_entry_back_walk_notify (fib_node_t *node,
1126 fib_node_back_walk_ctx_t *ctx)
1128 mfib_entry_recalculate_forwarding(mfib_entry_from_fib_node(node));
1130 return (FIB_NODE_BACK_WALK_CONTINUE);
1134 mfib_entry_show_memory (void)
1136 fib_show_memory_usage("multicast-Entry",
1137 pool_elts(mfib_entry_pool),
1138 pool_len(mfib_entry_pool),
1139 sizeof(mfib_entry_t));
1143 * The MFIB entry's graph node virtual function table
1145 static const fib_node_vft_t mfib_entry_vft = {
1146 .fnv_get = mfib_entry_get_node,
1147 .fnv_last_lock = mfib_entry_last_lock_gone,
1148 .fnv_back_walk = mfib_entry_back_walk_notify,
1149 .fnv_mem_show = mfib_entry_show_memory,
1153 mfib_entry_lock (fib_node_index_t mfib_entry_index)
1155 mfib_entry_t *mfib_entry;
1157 mfib_entry = mfib_entry_get(mfib_entry_index);
1159 fib_node_lock(&mfib_entry->mfe_node);
1163 mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1165 mfib_entry_t *mfib_entry;
1167 mfib_entry = mfib_entry_get(mfib_entry_index);
1169 fib_node_unlock(&mfib_entry->mfe_node);
1173 mfib_entry_dpo_lock (dpo_id_t *dpo)
1177 mfib_entry_dpo_unlock (dpo_id_t *dpo)
1181 const static dpo_vft_t mfib_entry_dpo_vft = {
1182 .dv_lock = mfib_entry_dpo_lock,
1183 .dv_unlock = mfib_entry_dpo_unlock,
1184 .dv_format = format_mfib_entry_dpo,
1185 .dv_mem_show = mfib_entry_show_memory,
1188 const static char* const mfib_entry_ip4_nodes[] =
1190 "ip4-mfib-forward-rpf",
1193 const static char* const mfib_entry_ip6_nodes[] =
1195 "ip6-mfib-forward-rpf",
1199 const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1201 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1202 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1206 mfib_entry_module_init (void)
1208 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1209 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1213 mfib_entry_encode (fib_node_index_t mfib_entry_index,
1214 fib_route_path_encode_t **api_rpaths)
1216 mfib_entry_t *mfib_entry;
1217 mfib_entry_src_t *bsrc;
1219 mfib_entry = mfib_entry_get(mfib_entry_index);
1220 bsrc = mfib_entry_get_best_src(mfib_entry);
1222 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
1224 fib_path_list_walk(bsrc->mfes_pl,
1232 mfib_entry_get_prefix (fib_node_index_t mfib_entry_index,
1235 mfib_entry_t *mfib_entry;
1237 mfib_entry = mfib_entry_get(mfib_entry_index);
1238 *pfx = mfib_entry->mfe_prefix;
1242 mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1244 mfib_entry_t *mfib_entry;
1246 mfib_entry = mfib_entry_get(mfib_entry_index);
1248 return (mfib_entry->mfe_fib_index);
1252 mfib_entry_contribute_ip_forwarding (fib_node_index_t mfib_entry_index)
1254 mfib_entry_t *mfib_entry;
1256 mfib_entry = mfib_entry_get(mfib_entry_index);
1258 return (&mfib_entry->mfe_rep);
1262 mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1263 fib_forward_chain_type_t type,
1267 * An IP mFIB entry can only provide a forwarding chain that
1268 * is the same IP proto as the prefix.
1269 * No use-cases (i know of) for other combinations.
1271 mfib_entry_t *mfib_entry;
1274 mfib_entry = mfib_entry_get(mfib_entry_index);
1276 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1278 if (type == fib_forw_chain_type_from_dpo_proto(dp))
1280 dpo_copy(dpo, &mfib_entry->mfe_rep);
1284 dpo_copy(dpo, drop_dpo_get(dp));
1289 mfib_entry_pool_size (void)
1291 return (pool_elts(mfib_entry_pool));
1294 static clib_error_t *
1295 show_mfib_entry_command (vlib_main_t * vm,
1296 unformat_input_t * input,
1297 vlib_cli_command_t * cmd)
1299 fib_node_index_t fei;
1301 if (unformat (input, "%d", &fei))
1304 * show one in detail
1306 if (!pool_is_free_index(mfib_entry_pool, fei))
1308 vlib_cli_output (vm, "%d@%U",
1310 format_mfib_entry, fei,
1311 MFIB_ENTRY_FORMAT_DETAIL2);
1315 vlib_cli_output (vm, "entry %d invalid", fei);
1323 vlib_cli_output (vm, "FIB Entries:");
1324 pool_foreach_index(fei, mfib_entry_pool,
1326 vlib_cli_output (vm, "%d@%U",
1328 format_mfib_entry, fei,
1329 MFIB_ENTRY_FORMAT_BRIEF);
1337 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1338 * numerical indentifier.
1340 VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1341 .path = "show mfib entry",
1342 .function = show_mfib_entry_command,
1343 .short_help = "show mfib entry",