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>
28 mfib_table_get (fib_node_index_t index,
33 case FIB_PROTOCOL_IP4:
34 return (pool_elt_at_index(ip4_main.mfibs, index));
35 case FIB_PROTOCOL_IP6:
36 return (pool_elt_at_index(ip6_main.mfibs, index));
37 case FIB_PROTOCOL_MPLS:
44 static inline fib_node_index_t
45 mfib_table_lookup_i (const mfib_table_t *mfib_table,
46 const mfib_prefix_t *prefix)
48 switch (prefix->fp_proto)
50 case FIB_PROTOCOL_IP4:
51 return (ip4_mfib_table_lookup(&mfib_table->v4,
52 &prefix->fp_src_addr.ip4,
53 &prefix->fp_grp_addr.ip4,
55 case FIB_PROTOCOL_IP6:
56 return (ip6_mfib_table_lookup(&mfib_table->v6,
57 &prefix->fp_src_addr.ip6,
58 &prefix->fp_grp_addr.ip6,
60 case FIB_PROTOCOL_MPLS:
63 return (FIB_NODE_INDEX_INVALID);
67 mfib_table_lookup (u32 fib_index,
68 const mfib_prefix_t *prefix)
70 return (mfib_table_lookup_i(mfib_table_get(fib_index, prefix->fp_proto), prefix));
73 static inline fib_node_index_t
74 mfib_table_lookup_exact_match_i (const mfib_table_t *mfib_table,
75 const mfib_prefix_t *prefix)
77 switch (prefix->fp_proto)
79 case FIB_PROTOCOL_IP4:
80 return (ip4_mfib_table_lookup_exact_match(&mfib_table->v4,
81 &prefix->fp_grp_addr.ip4,
82 &prefix->fp_src_addr.ip4,
84 case FIB_PROTOCOL_IP6:
85 return (ip6_mfib_table_lookup_exact_match(&mfib_table->v6,
86 &prefix->fp_grp_addr.ip6,
87 &prefix->fp_src_addr.ip6,
89 case FIB_PROTOCOL_MPLS:
92 return (FIB_NODE_INDEX_INVALID);
96 mfib_table_lookup_exact_match (u32 fib_index,
97 const mfib_prefix_t *prefix)
99 return (mfib_table_lookup_exact_match_i(mfib_table_get(fib_index,
104 static fib_node_index_t
105 mfib_table_get_less_specific_i (const mfib_table_t *mfib_table,
106 const mfib_prefix_t *prefix)
108 switch (prefix->fp_proto)
110 case FIB_PROTOCOL_IP4:
111 return (ip4_mfib_table_get_less_specific(&mfib_table->v4,
112 &prefix->fp_src_addr.ip4,
113 &prefix->fp_grp_addr.ip4,
115 case FIB_PROTOCOL_IP6:
116 return (ip6_mfib_table_get_less_specific(&mfib_table->v6,
117 &prefix->fp_src_addr.ip6,
118 &prefix->fp_grp_addr.ip6,
120 case FIB_PROTOCOL_MPLS:
123 return (FIB_NODE_INDEX_INVALID);
127 mfib_table_get_less_specific (u32 fib_index,
128 const mfib_prefix_t *prefix)
130 return (mfib_table_get_less_specific_i(mfib_table_get(fib_index,
136 mfib_table_entry_remove (mfib_table_t *mfib_table,
137 const mfib_prefix_t *prefix,
138 fib_node_index_t mfib_entry_index)
140 vlib_smp_unsafe_warning();
142 mfib_table->mft_total_route_counts--;
144 switch (prefix->fp_proto)
146 case FIB_PROTOCOL_IP4:
147 ip4_mfib_table_entry_remove(&mfib_table->v4,
148 &prefix->fp_grp_addr.ip4,
149 &prefix->fp_src_addr.ip4,
152 case FIB_PROTOCOL_IP6:
153 ip6_mfib_table_entry_remove(&mfib_table->v6,
154 &prefix->fp_grp_addr.ip6,
155 &prefix->fp_src_addr.ip6,
158 case FIB_PROTOCOL_MPLS:
163 mfib_entry_cover_change_notify(mfib_entry_index,
164 FIB_NODE_INDEX_INVALID);
165 mfib_entry_unlock(mfib_entry_index);
169 mfib_table_post_insert_actions (mfib_table_t *mfib_table,
170 const mfib_prefix_t *prefix,
171 fib_node_index_t mfib_entry_index)
173 fib_node_index_t mfib_entry_cover_index;
176 * find the covering entry
178 mfib_entry_cover_index = mfib_table_get_less_specific_i(mfib_table,
181 * the indicies are the same when the default route is first added
183 if (mfib_entry_cover_index != mfib_entry_index)
186 * inform the covering entry that a new more specific
187 * has been inserted beneath it.
188 * If the prefix that has been inserted is a host route
189 * then it is not possible that it will be the cover for any
190 * other entry, so we can elide the walk.
192 if (!mfib_entry_is_host(mfib_entry_index))
194 mfib_entry_cover_change_notify(mfib_entry_cover_index,
202 mfib_table_entry_insert (mfib_table_t *mfib_table,
203 const mfib_prefix_t *prefix,
204 fib_node_index_t mfib_entry_index)
206 vlib_smp_unsafe_warning();
208 mfib_entry_lock(mfib_entry_index);
209 mfib_table->mft_total_route_counts++;
211 switch (prefix->fp_proto)
213 case FIB_PROTOCOL_IP4:
214 ip4_mfib_table_entry_insert(&mfib_table->v4,
215 &prefix->fp_grp_addr.ip4,
216 &prefix->fp_src_addr.ip4,
220 case FIB_PROTOCOL_IP6:
221 ip6_mfib_table_entry_insert(&mfib_table->v6,
222 &prefix->fp_grp_addr.ip6,
223 &prefix->fp_src_addr.ip6,
227 case FIB_PROTOCOL_MPLS:
231 mfib_table_post_insert_actions(mfib_table, prefix, mfib_entry_index);
235 mfib_table_entry_update (u32 fib_index,
236 const mfib_prefix_t *prefix,
237 mfib_source_t source,
239 mfib_entry_flags_t entry_flags)
241 fib_node_index_t mfib_entry_index;
242 mfib_table_t *mfib_table;
244 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
245 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
247 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
249 if (MFIB_ENTRY_FLAG_NONE != entry_flags)
252 * update to a non-existing entry with non-zero flags
254 mfib_entry_index = mfib_entry_create(fib_index, source,
259 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
263 * the entry doesn't exist and the request is to set no flags
264 * the result would be an entry that doesn't exist - so do nothing
269 mfib_entry_lock(mfib_entry_index);
271 if (mfib_entry_update(mfib_entry_index,
278 * this update means we can now remove the entry.
280 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
283 mfib_entry_unlock(mfib_entry_index);
286 return (mfib_entry_index);
290 mfib_table_entry_path_update (u32 fib_index,
291 const mfib_prefix_t *prefix,
292 mfib_source_t source,
293 const fib_route_path_t *rpath,
294 mfib_itf_flags_t itf_flags)
296 fib_node_index_t mfib_entry_index;
297 mfib_table_t *mfib_table;
299 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
300 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
302 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
304 mfib_entry_index = mfib_entry_create(fib_index,
308 MFIB_ENTRY_FLAG_NONE,
311 mfib_entry_path_update(mfib_entry_index,
316 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
320 mfib_entry_path_update(mfib_entry_index,
325 return (mfib_entry_index);
329 mfib_table_entry_path_remove (u32 fib_index,
330 const mfib_prefix_t *prefix,
331 mfib_source_t source,
332 const fib_route_path_t *rpath)
334 fib_node_index_t mfib_entry_index;
335 mfib_table_t *mfib_table;
337 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
338 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
340 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
343 * removing an etry that does not exist. i'll allow it.
351 * don't nobody go nowhere
353 mfib_entry_lock(mfib_entry_index);
355 no_more_sources = mfib_entry_path_remove(mfib_entry_index,
362 * last source gone. remove from the table
364 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
367 mfib_entry_unlock(mfib_entry_index);
372 mfib_table_entry_special_add (u32 fib_index,
373 const mfib_prefix_t *prefix,
374 mfib_source_t source,
375 mfib_entry_flags_t entry_flags,
378 fib_node_index_t mfib_entry_index;
379 mfib_table_t *mfib_table;
381 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
382 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
384 if (INDEX_INVALID != repi)
386 entry_flags |= MFIB_ENTRY_FLAG_EXCLUSIVE;
389 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
391 mfib_entry_index = mfib_entry_create(fib_index,
398 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
402 mfib_entry_special_add(mfib_entry_index, source, entry_flags,
403 MFIB_RPF_ID_NONE, repi);
406 return (mfib_entry_index);
410 mfib_table_entry_delete_i (u32 fib_index,
411 fib_node_index_t mfib_entry_index,
412 const mfib_prefix_t *prefix,
413 mfib_source_t source)
415 mfib_table_t *mfib_table;
417 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
420 * don't nobody go nowhere
422 mfib_entry_lock(mfib_entry_index);
424 if (mfib_entry_delete(mfib_entry_index, source))
427 * last source gone. remove from the table
429 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
433 * still has sources, leave it be.
436 mfib_entry_unlock(mfib_entry_index);
440 mfib_table_entry_delete (u32 fib_index,
441 const mfib_prefix_t *prefix,
442 mfib_source_t source)
444 fib_node_index_t mfib_entry_index;
446 mfib_entry_index = mfib_table_lookup_exact_match(fib_index, prefix);
448 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
451 * removing an etry that does not exist.
452 * i'll allow it, but i won't like it.
454 clib_warning("%U not in FIB", format_mfib_prefix, prefix);
458 mfib_table_entry_delete_i(fib_index, mfib_entry_index,
464 mfib_table_entry_delete_index (fib_node_index_t mfib_entry_index,
465 mfib_source_t source)
467 const mfib_prefix_t *prefix;
469 prefix = mfib_entry_get_prefix(mfib_entry_index);
471 mfib_table_entry_delete_i(mfib_entry_get_fib_index(mfib_entry_index),
472 mfib_entry_index, prefix, source);
476 mfib_table_get_index_for_sw_if_index (fib_protocol_t proto,
481 case FIB_PROTOCOL_IP4:
482 return (ip4_mfib_table_get_index_for_sw_if_index(sw_if_index));
483 case FIB_PROTOCOL_IP6:
484 return (ip6_mfib_table_get_index_for_sw_if_index(sw_if_index));
485 case FIB_PROTOCOL_MPLS:
493 mfib_table_get_table_id (u32 fib_index,
494 fib_protocol_t proto)
496 mfib_table_t *mfib_table;
498 mfib_table = mfib_table_get(fib_index, proto);
500 return ((NULL != mfib_table ? mfib_table->mft_table_id : ~0));
504 mfib_table_find (fib_protocol_t proto,
509 case FIB_PROTOCOL_IP4:
510 return (ip4_mfib_index_from_table_id(table_id));
511 case FIB_PROTOCOL_IP6:
512 return (ip6_mfib_index_from_table_id(table_id));
513 case FIB_PROTOCOL_MPLS:
521 mfib_table_find_or_create_and_lock_i (fib_protocol_t proto,
526 mfib_table_t *mfib_table;
531 case FIB_PROTOCOL_IP4:
532 fi = ip4_mfib_table_find_or_create_and_lock(table_id, src);
534 case FIB_PROTOCOL_IP6:
535 fi = ip6_mfib_table_find_or_create_and_lock(table_id, src);
537 case FIB_PROTOCOL_MPLS:
542 mfib_table = mfib_table_get(fi, proto);
544 if (NULL == mfib_table->mft_desc)
548 mfib_table->mft_desc = format(NULL, "%s", name);
552 mfib_table->mft_desc = format(NULL, "%U-VRF:%d",
553 format_fib_protocol, proto,
562 mfib_table_find_or_create_and_lock (fib_protocol_t proto,
566 return (mfib_table_find_or_create_and_lock_i(proto, table_id,
571 mfib_table_find_or_create_and_lock_w_name (fib_protocol_t proto,
576 return (mfib_table_find_or_create_and_lock_i(proto, table_id,
581 * @brief Table flush context. Store the indicies of matching FIB entries
582 * that need to be removed.
584 typedef struct mfib_table_flush_ctx_t_
587 * The list of entries to flush
589 fib_node_index_t *mftf_entries;
592 * The source we are flushing
594 mfib_source_t mftf_source;
595 } mfib_table_flush_ctx_t;
598 mfib_table_flush_cb (fib_node_index_t mfib_entry_index,
601 mfib_table_flush_ctx_t *ctx = arg;
603 if (mfib_entry_is_sourced(mfib_entry_index, ctx->mftf_source))
605 vec_add1(ctx->mftf_entries, mfib_entry_index);
611 mfib_table_flush (u32 mfib_index,
612 fib_protocol_t proto,
613 mfib_source_t source)
615 fib_node_index_t *mfib_entry_index;
616 mfib_table_flush_ctx_t ctx = {
617 .mftf_entries = NULL,
618 .mftf_source = source,
621 mfib_table_walk(mfib_index, proto,
625 vec_foreach(mfib_entry_index, ctx.mftf_entries)
627 mfib_table_entry_delete_index(*mfib_entry_index, source);
630 vec_free(ctx.mftf_entries);
634 mfib_table_destroy (mfib_table_t *mfib_table)
636 vec_free(mfib_table->mft_desc);
638 switch (mfib_table->mft_proto)
640 case FIB_PROTOCOL_IP4:
641 ip4_mfib_table_destroy(&mfib_table->v4);
643 case FIB_PROTOCOL_IP6:
644 ip6_mfib_table_destroy(&mfib_table->v6);
646 case FIB_PROTOCOL_MPLS:
653 mfib_table_unlock (u32 fib_index,
654 fib_protocol_t proto,
655 mfib_source_t source)
657 mfib_table_t *mfib_table;
659 mfib_table = mfib_table_get(fib_index, proto);
660 mfib_table->mft_locks[source]--;
661 mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS]--;
663 if (0 == mfib_table->mft_locks[source])
666 * The source no longer needs the table. flush any routes
667 * from it just in case
669 mfib_table_flush(fib_index, proto, source);
672 if (0 == mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS])
675 * no more locak from any source - kill it
677 mfib_table_destroy(mfib_table);
682 mfib_table_lock (u32 fib_index,
683 fib_protocol_t proto,
684 mfib_source_t source)
686 mfib_table_t *mfib_table;
688 mfib_table = mfib_table_get(fib_index, proto);
689 mfib_table->mft_locks[source]++;
690 mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS]++;
694 mfib_table_get_n_routes (fib_node_index_t fib_index,
695 fib_protocol_t proto)
697 mfib_table_t *mfib_table;
699 mfib_table = mfib_table_get(fib_index, proto);
701 return (mfib_table->mft_total_route_counts);
705 mfib_table_walk (u32 fib_index,
706 fib_protocol_t proto,
707 mfib_table_walk_fn_t fn,
712 case FIB_PROTOCOL_IP4:
713 ip4_mfib_table_walk(ip4_mfib_get(fib_index), fn, ctx);
715 case FIB_PROTOCOL_IP6:
716 ip6_mfib_table_walk(ip6_mfib_get(fib_index), fn, ctx);
718 case FIB_PROTOCOL_MPLS:
724 format_mfib_table_name (u8* s, va_list *ap)
726 fib_node_index_t fib_index = va_arg(*ap, fib_node_index_t);
727 fib_protocol_t proto = va_arg(*ap, int); // int promotion
728 mfib_table_t *mfib_table;
730 mfib_table = mfib_table_get(fib_index, proto);
732 s = format(s, "%v", mfib_table->mft_desc);
738 format_mfib_table_memory (u8 *s, va_list *args)
740 s = format(s, "%U", format_ip4_mfib_table_memory);
741 s = format(s, "%U", format_ip6_mfib_table_memory);
746 static clib_error_t *
747 mfib_module_init (vlib_main_t * vm)
749 clib_error_t * error;
751 mfib_entry_src_module_init();
752 mfib_entry_module_init();
753 mfib_signal_module_init();
755 if ((error = vlib_call_init_function (vm, fib_module_init)))
757 if ((error = vlib_call_init_function (vm, rn_module_init)))
763 VLIB_INIT_FUNCTION(mfib_module_init);