2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlib/vlib.h>
17 #include <vnet/dpo/drop_dpo.h>
19 #include <vnet/mfib/mfib_table.h>
20 #include <vnet/mfib/ip4_mfib.h>
21 #include <vnet/mfib/ip6_mfib.h>
22 #include <vnet/mfib/mfib_entry.h>
23 #include <vnet/mfib/mfib_entry_src.h>
24 #include <vnet/mfib/mfib_entry_cover.h>
25 #include <vnet/mfib/mfib_signal.h>
27 const static char * mfib_table_flags_strings[] = MFIB_TABLE_ATTRIBUTES;
30 mfib_table_get (fib_node_index_t index,
35 case FIB_PROTOCOL_IP4:
36 return (pool_elt_at_index(ip4_main.mfibs, index));
37 case FIB_PROTOCOL_IP6:
38 return (pool_elt_at_index(ip6_main.mfibs, index));
39 case FIB_PROTOCOL_MPLS:
46 static inline fib_node_index_t
47 mfib_table_lookup_i (const mfib_table_t *mfib_table,
48 const mfib_prefix_t *prefix)
50 switch (prefix->fp_proto)
52 case FIB_PROTOCOL_IP4:
53 return (ip4_mfib_table_lookup(&mfib_table->v4,
54 &prefix->fp_src_addr.ip4,
55 &prefix->fp_grp_addr.ip4,
57 case FIB_PROTOCOL_IP6:
58 return (ip6_mfib_table_lookup(&mfib_table->v6,
59 &prefix->fp_src_addr.ip6,
60 &prefix->fp_grp_addr.ip6,
62 case FIB_PROTOCOL_MPLS:
65 return (FIB_NODE_INDEX_INVALID);
69 mfib_table_lookup (u32 fib_index,
70 const mfib_prefix_t *prefix)
72 return (mfib_table_lookup_i(mfib_table_get(fib_index, prefix->fp_proto), prefix));
75 static inline fib_node_index_t
76 mfib_table_lookup_exact_match_i (const mfib_table_t *mfib_table,
77 const mfib_prefix_t *prefix)
79 switch (prefix->fp_proto)
81 case FIB_PROTOCOL_IP4:
82 return (ip4_mfib_table_lookup_exact_match(&mfib_table->v4,
83 &prefix->fp_grp_addr.ip4,
84 &prefix->fp_src_addr.ip4,
86 case FIB_PROTOCOL_IP6:
87 return (ip6_mfib_table_lookup_exact_match(&mfib_table->v6,
88 &prefix->fp_grp_addr.ip6,
89 &prefix->fp_src_addr.ip6,
91 case FIB_PROTOCOL_MPLS:
94 return (FIB_NODE_INDEX_INVALID);
98 mfib_table_lookup_exact_match (u32 fib_index,
99 const mfib_prefix_t *prefix)
101 return (mfib_table_lookup_exact_match_i(mfib_table_get(fib_index,
106 static fib_node_index_t
107 mfib_table_get_less_specific_i (const mfib_table_t *mfib_table,
108 const mfib_prefix_t *prefix)
110 switch (prefix->fp_proto)
112 case FIB_PROTOCOL_IP4:
113 return (ip4_mfib_table_get_less_specific(&mfib_table->v4,
114 &prefix->fp_src_addr.ip4,
115 &prefix->fp_grp_addr.ip4,
117 case FIB_PROTOCOL_IP6:
118 return (ip6_mfib_table_get_less_specific(&mfib_table->v6,
119 &prefix->fp_src_addr.ip6,
120 &prefix->fp_grp_addr.ip6,
122 case FIB_PROTOCOL_MPLS:
125 return (FIB_NODE_INDEX_INVALID);
129 mfib_table_get_less_specific (u32 fib_index,
130 const mfib_prefix_t *prefix)
132 return (mfib_table_get_less_specific_i(mfib_table_get(fib_index,
138 mfib_table_entry_remove (mfib_table_t *mfib_table,
139 const mfib_prefix_t *prefix,
140 fib_node_index_t mfib_entry_index)
142 vlib_smp_unsafe_warning();
144 mfib_table->mft_total_route_counts--;
146 switch (prefix->fp_proto)
148 case FIB_PROTOCOL_IP4:
149 ip4_mfib_table_entry_remove(&mfib_table->v4,
150 &prefix->fp_grp_addr.ip4,
151 &prefix->fp_src_addr.ip4,
154 case FIB_PROTOCOL_IP6:
155 ip6_mfib_table_entry_remove(&mfib_table->v6,
156 &prefix->fp_grp_addr.ip6,
157 &prefix->fp_src_addr.ip6,
160 case FIB_PROTOCOL_MPLS:
165 mfib_entry_cover_change_notify(mfib_entry_index,
166 FIB_NODE_INDEX_INVALID);
167 mfib_entry_unlock(mfib_entry_index);
171 mfib_table_post_insert_actions (mfib_table_t *mfib_table,
172 const mfib_prefix_t *prefix,
173 fib_node_index_t mfib_entry_index)
175 fib_node_index_t mfib_entry_cover_index;
178 * find the covering entry
180 mfib_entry_cover_index = mfib_table_get_less_specific_i(mfib_table,
183 * the indicies are the same when the default route is first added
185 if (mfib_entry_cover_index != mfib_entry_index)
188 * inform the covering entry that a new more specific
189 * has been inserted beneath it.
190 * If the prefix that has been inserted is a host route
191 * then it is not possible that it will be the cover for any
192 * other entry, so we can elide the walk.
194 if (!mfib_entry_is_host(mfib_entry_index))
196 mfib_entry_cover_change_notify(mfib_entry_cover_index,
204 mfib_table_entry_insert (mfib_table_t *mfib_table,
205 const mfib_prefix_t *prefix,
206 fib_node_index_t mfib_entry_index)
208 vlib_smp_unsafe_warning();
210 mfib_entry_lock(mfib_entry_index);
211 mfib_table->mft_total_route_counts++;
213 switch (prefix->fp_proto)
215 case FIB_PROTOCOL_IP4:
216 ip4_mfib_table_entry_insert(&mfib_table->v4,
217 &prefix->fp_grp_addr.ip4,
218 &prefix->fp_src_addr.ip4,
222 case FIB_PROTOCOL_IP6:
223 ip6_mfib_table_entry_insert(&mfib_table->v6,
224 &prefix->fp_grp_addr.ip6,
225 &prefix->fp_src_addr.ip6,
229 case FIB_PROTOCOL_MPLS:
233 mfib_table_post_insert_actions(mfib_table, prefix, mfib_entry_index);
237 mfib_table_entry_update (u32 fib_index,
238 const mfib_prefix_t *prefix,
239 mfib_source_t source,
241 mfib_entry_flags_t entry_flags)
243 fib_node_index_t mfib_entry_index;
244 mfib_table_t *mfib_table;
246 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
247 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
249 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
251 if (MFIB_ENTRY_FLAG_NONE != entry_flags)
254 * update to a non-existing entry with non-zero flags
256 mfib_entry_index = mfib_entry_create(fib_index, source,
261 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
265 * the entry doesn't exist and the request is to set no flags
266 * the result would be an entry that doesn't exist - so do nothing
271 mfib_entry_lock(mfib_entry_index);
273 if (mfib_entry_update(mfib_entry_index,
280 * this update means we can now remove the entry.
282 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
285 mfib_entry_unlock(mfib_entry_index);
288 return (mfib_entry_index);
291 static fib_node_index_t
292 mfib_table_entry_paths_update_i (u32 fib_index,
293 const mfib_prefix_t *prefix,
294 mfib_source_t source,
295 mfib_entry_flags_t entry_flags,
296 const fib_route_path_t *rpaths)
298 fib_node_index_t mfib_entry_index;
299 mfib_table_t *mfib_table;
301 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
302 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
304 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
306 mfib_entry_index = mfib_entry_create(fib_index,
313 mfib_entry_path_update(mfib_entry_index, source, rpaths);
315 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
319 mfib_entry_path_update(mfib_entry_index, source, rpaths);
321 return (mfib_entry_index);
326 mfib_table_entry_path_update (u32 fib_index,
327 const mfib_prefix_t *prefix,
328 mfib_source_t source,
329 mfib_entry_flags_t entry_flags,
330 const fib_route_path_t *rpath)
332 fib_node_index_t mfib_entry_index;
333 fib_route_path_t *rpaths = NULL;
335 vec_add1(rpaths, *rpath);
337 mfib_entry_index = mfib_table_entry_paths_update_i(fib_index, prefix,
342 return (mfib_entry_index);
346 mfib_table_entry_paths_update (u32 fib_index,
347 const mfib_prefix_t *prefix,
348 mfib_source_t source,
349 mfib_entry_flags_t entry_flags,
350 const fib_route_path_t *rpaths)
352 return (mfib_table_entry_paths_update_i(fib_index, prefix,
353 source, entry_flags, rpaths));
357 mfib_table_entry_paths_remove_i (u32 fib_index,
358 const mfib_prefix_t *prefix,
359 mfib_source_t source,
360 const fib_route_path_t *rpaths)
362 fib_node_index_t mfib_entry_index;
363 mfib_table_t *mfib_table;
365 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
366 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
368 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
371 * removing an entry that does not exist. i'll allow it.
379 * don't nobody go nowhere
381 mfib_entry_lock(mfib_entry_index);
383 no_more_sources = mfib_entry_path_remove(mfib_entry_index,
390 * last source gone. remove from the table
392 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
395 mfib_entry_unlock(mfib_entry_index);
399 mfib_table_entry_paths_remove (u32 fib_index,
400 const mfib_prefix_t *prefix,
401 mfib_source_t source,
402 const fib_route_path_t *rpaths)
404 mfib_table_entry_paths_remove_i(fib_index,
411 mfib_table_entry_path_remove (u32 fib_index,
412 const mfib_prefix_t *prefix,
413 mfib_source_t source,
414 const fib_route_path_t *rpath)
416 fib_route_path_t *rpaths = NULL;
418 vec_add1(rpaths, *rpath);
420 mfib_table_entry_paths_remove_i(fib_index,
429 mfib_table_entry_special_add (u32 fib_index,
430 const mfib_prefix_t *prefix,
431 mfib_source_t source,
432 mfib_entry_flags_t entry_flags,
435 fib_node_index_t mfib_entry_index;
436 mfib_table_t *mfib_table;
438 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
439 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
441 if (INDEX_INVALID != repi)
443 entry_flags |= MFIB_ENTRY_FLAG_EXCLUSIVE;
446 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
448 mfib_entry_index = mfib_entry_create(fib_index,
455 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
459 mfib_entry_special_add(mfib_entry_index, source, entry_flags,
460 MFIB_RPF_ID_NONE, repi);
463 return (mfib_entry_index);
467 mfib_table_entry_delete_i (u32 fib_index,
468 fib_node_index_t mfib_entry_index,
469 const mfib_prefix_t *prefix,
470 mfib_source_t source)
472 mfib_table_t *mfib_table;
474 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
477 * don't nobody go nowhere
479 mfib_entry_lock(mfib_entry_index);
481 if (mfib_entry_delete(mfib_entry_index, source))
484 * last source gone. remove from the table
486 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
490 * still has sources, leave it be.
493 mfib_entry_unlock(mfib_entry_index);
497 mfib_table_entry_delete (u32 fib_index,
498 const mfib_prefix_t *prefix,
499 mfib_source_t source)
501 fib_node_index_t mfib_entry_index;
503 mfib_entry_index = mfib_table_lookup_exact_match(fib_index, prefix);
505 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
508 * removing an etry that does not exist.
509 * i'll allow it, but i won't like it.
511 clib_warning("%U not in FIB", format_mfib_prefix, prefix);
515 mfib_table_entry_delete_i(fib_index, mfib_entry_index,
521 mfib_table_entry_delete_index (fib_node_index_t mfib_entry_index,
522 mfib_source_t source)
524 mfib_table_entry_delete_i(mfib_entry_get_fib_index(mfib_entry_index),
526 mfib_entry_get_prefix(mfib_entry_index),
531 mfib_table_get_index_for_sw_if_index (fib_protocol_t proto,
536 case FIB_PROTOCOL_IP4:
537 return (ip4_mfib_table_get_index_for_sw_if_index(sw_if_index));
538 case FIB_PROTOCOL_IP6:
539 return (ip6_mfib_table_get_index_for_sw_if_index(sw_if_index));
540 case FIB_PROTOCOL_MPLS:
548 mfib_table_get_table_id (u32 fib_index,
549 fib_protocol_t proto)
551 mfib_table_t *mfib_table;
553 mfib_table = mfib_table_get(fib_index, proto);
555 return ((NULL != mfib_table ? mfib_table->mft_table_id : ~0));
559 mfib_table_find (fib_protocol_t proto,
564 case FIB_PROTOCOL_IP4:
565 return (ip4_mfib_index_from_table_id(table_id));
566 case FIB_PROTOCOL_IP6:
567 return (ip6_mfib_index_from_table_id(table_id));
568 case FIB_PROTOCOL_MPLS:
576 mfib_table_find_or_create_and_lock_i (fib_protocol_t proto,
581 mfib_table_t *mfib_table;
586 case FIB_PROTOCOL_IP4:
587 fi = ip4_mfib_table_find_or_create_and_lock(table_id, src);
589 case FIB_PROTOCOL_IP6:
590 fi = ip6_mfib_table_find_or_create_and_lock(table_id, src);
592 case FIB_PROTOCOL_MPLS:
597 mfib_table = mfib_table_get(fi, proto);
599 if (NULL == mfib_table->mft_desc)
603 mfib_table->mft_desc = format(NULL, "%s", name);
607 mfib_table->mft_desc = format(NULL, "%U-VRF:%d",
608 format_fib_protocol, proto,
617 mfib_table_find_or_create_and_lock (fib_protocol_t proto,
621 return (mfib_table_find_or_create_and_lock_i(proto, table_id,
626 mfib_table_find_or_create_and_lock_w_name (fib_protocol_t proto,
631 return (mfib_table_find_or_create_and_lock_i(proto, table_id,
636 * @brief Table flush context. Store the indicies of matching FIB entries
637 * that need to be removed.
639 typedef struct mfib_table_flush_ctx_t_
642 * The list of entries to flush
644 fib_node_index_t *mftf_entries;
647 * The source we are flushing
649 mfib_source_t mftf_source;
650 } mfib_table_flush_ctx_t;
653 mfib_table_flush_cb (fib_node_index_t mfib_entry_index,
656 mfib_table_flush_ctx_t *ctx = arg;
658 if (mfib_entry_is_sourced(mfib_entry_index, ctx->mftf_source))
660 vec_add1(ctx->mftf_entries, mfib_entry_index);
662 return (WALK_CONTINUE);
666 mfib_table_flush (u32 mfib_index,
667 fib_protocol_t proto,
668 mfib_source_t source)
670 fib_node_index_t *mfib_entry_index;
671 mfib_table_flush_ctx_t ctx = {
672 .mftf_entries = NULL,
673 .mftf_source = source,
676 mfib_table_walk(mfib_index, proto,
680 vec_foreach(mfib_entry_index, ctx.mftf_entries)
682 mfib_table_entry_delete_index(*mfib_entry_index, source);
685 vec_free(ctx.mftf_entries);
689 mfib_table_mark_cb (fib_node_index_t fib_entry_index,
692 mfib_table_flush_ctx_t *ctx = arg;
694 if (mfib_entry_is_sourced(fib_entry_index, ctx->mftf_source))
696 mfib_entry_mark(fib_entry_index, ctx->mftf_source);
698 return (WALK_CONTINUE);
702 mfib_table_mark (u32 fib_index,
703 fib_protocol_t proto,
704 mfib_source_t source)
706 mfib_table_flush_ctx_t ctx = {
707 .mftf_source = source,
709 mfib_table_t *mfib_table;
711 mfib_table = mfib_table_get(fib_index, proto);
713 mfib_table->mft_epoch++;
714 mfib_table->mft_flags |= MFIB_TABLE_FLAG_RESYNC;
716 mfib_table_walk(fib_index, proto,
722 mfib_table_sweep_cb (fib_node_index_t fib_entry_index,
725 mfib_table_flush_ctx_t *ctx = arg;
727 if (mfib_entry_is_marked(fib_entry_index, ctx->mftf_source))
729 vec_add1(ctx->mftf_entries, fib_entry_index);
731 return (WALK_CONTINUE);
735 mfib_table_sweep (u32 fib_index,
736 fib_protocol_t proto,
737 mfib_source_t source)
739 mfib_table_flush_ctx_t ctx = {
740 .mftf_source = source,
742 fib_node_index_t *fib_entry_index;
743 mfib_table_t *mfib_table;
745 mfib_table = mfib_table_get(fib_index, proto);
747 mfib_table->mft_flags &= ~MFIB_TABLE_FLAG_RESYNC;
749 mfib_table_walk(fib_index, proto,
753 vec_foreach(fib_entry_index, ctx.mftf_entries)
755 mfib_table_entry_delete_index(*fib_entry_index, source);
758 vec_free(ctx.mftf_entries);
762 mfib_table_destroy (mfib_table_t *mfib_table)
764 vec_free(mfib_table->mft_desc);
766 switch (mfib_table->mft_proto)
768 case FIB_PROTOCOL_IP4:
769 ip4_mfib_table_destroy(&mfib_table->v4);
771 case FIB_PROTOCOL_IP6:
772 ip6_mfib_table_destroy(&mfib_table->v6);
774 case FIB_PROTOCOL_MPLS:
781 mfib_table_unlock (u32 fib_index,
782 fib_protocol_t proto,
783 mfib_source_t source)
785 mfib_table_t *mfib_table;
787 mfib_table = mfib_table_get(fib_index, proto);
788 mfib_table->mft_locks[source]--;
789 mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS]--;
791 if (0 == mfib_table->mft_locks[source])
794 * The source no longer needs the table. flush any routes
795 * from it just in case
797 mfib_table_flush(fib_index, proto, source);
800 if (0 == mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS])
803 * no more locak from any source - kill it
805 mfib_table_destroy(mfib_table);
810 mfib_table_lock (u32 fib_index,
811 fib_protocol_t proto,
812 mfib_source_t source)
814 mfib_table_t *mfib_table;
816 mfib_table = mfib_table_get(fib_index, proto);
817 mfib_table->mft_locks[source]++;
818 mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS]++;
822 mfib_table_get_n_routes (fib_node_index_t fib_index,
823 fib_protocol_t proto)
825 mfib_table_t *mfib_table;
827 mfib_table = mfib_table_get(fib_index, proto);
829 return (mfib_table->mft_total_route_counts);
833 mfib_table_walk (u32 fib_index,
834 fib_protocol_t proto,
835 mfib_table_walk_fn_t fn,
840 case FIB_PROTOCOL_IP4:
841 ip4_mfib_table_walk(ip4_mfib_get(fib_index), fn, ctx);
843 case FIB_PROTOCOL_IP6:
844 ip6_mfib_table_walk(ip6_mfib_get(fib_index), fn, ctx);
846 case FIB_PROTOCOL_MPLS:
852 format_mfib_table_flags (u8 *s, va_list *args)
854 mfib_table_flags_t flags = va_arg(*args, int);
855 mfib_table_attribute_t attr;
859 return format(s, "none");
862 FOR_EACH_MFIB_TABLE_ATTRIBUTE(attr) {
863 if (1 << attr & flags) {
864 s = format(s, "%s", mfib_table_flags_strings[attr]);
872 format_mfib_table_name (u8* s, va_list *ap)
874 fib_node_index_t fib_index = va_arg(*ap, fib_node_index_t);
875 fib_protocol_t proto = va_arg(*ap, int); // int promotion
876 mfib_table_t *mfib_table;
878 mfib_table = mfib_table_get(fib_index, proto);
880 s = format(s, "%v", mfib_table->mft_desc);
886 format_mfib_table_memory (u8 *s, va_list *args)
888 s = format(s, "%U", format_ip4_mfib_table_memory);
889 s = format(s, "%U", format_ip6_mfib_table_memory);
894 static clib_error_t *
895 mfib_module_init (vlib_main_t * vm)
897 clib_error_t * error;
899 mfib_entry_src_module_init();
900 mfib_entry_module_init();
901 mfib_signal_module_init();
903 if ((error = vlib_call_init_function (vm, fib_module_init)))
905 if ((error = vlib_call_init_function (vm, rn_module_init)))
911 VLIB_INIT_FUNCTION(mfib_module_init);