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_signal.h>
26 mfib_table_get (fib_node_index_t index,
31 case FIB_PROTOCOL_IP4:
32 return (pool_elt_at_index(ip4_main.mfibs, index));
33 case FIB_PROTOCOL_IP6:
34 return (pool_elt_at_index(ip6_main.mfibs, index));
35 case FIB_PROTOCOL_MPLS:
42 static inline fib_node_index_t
43 mfib_table_lookup_i (const mfib_table_t *mfib_table,
44 const mfib_prefix_t *prefix)
46 switch (prefix->fp_proto)
48 case FIB_PROTOCOL_IP4:
49 return (ip4_mfib_table_lookup(&mfib_table->v4,
50 &prefix->fp_src_addr.ip4,
51 &prefix->fp_grp_addr.ip4,
53 case FIB_PROTOCOL_IP6:
54 return (ip6_mfib_table_lookup(&mfib_table->v6,
55 &prefix->fp_src_addr.ip6,
56 &prefix->fp_grp_addr.ip6,
58 case FIB_PROTOCOL_MPLS:
61 return (FIB_NODE_INDEX_INVALID);
65 mfib_table_lookup (u32 fib_index,
66 const mfib_prefix_t *prefix)
68 return (mfib_table_lookup_i(mfib_table_get(fib_index, prefix->fp_proto), prefix));
71 static inline fib_node_index_t
72 mfib_table_lookup_exact_match_i (const mfib_table_t *mfib_table,
73 const mfib_prefix_t *prefix)
75 switch (prefix->fp_proto)
77 case FIB_PROTOCOL_IP4:
78 return (ip4_mfib_table_lookup_exact_match(&mfib_table->v4,
79 &prefix->fp_grp_addr.ip4,
80 &prefix->fp_src_addr.ip4,
82 case FIB_PROTOCOL_IP6:
83 return (ip6_mfib_table_lookup_exact_match(&mfib_table->v6,
84 &prefix->fp_grp_addr.ip6,
85 &prefix->fp_src_addr.ip6,
87 case FIB_PROTOCOL_MPLS:
90 return (FIB_NODE_INDEX_INVALID);
94 mfib_table_lookup_exact_match (u32 fib_index,
95 const mfib_prefix_t *prefix)
97 return (mfib_table_lookup_exact_match_i(mfib_table_get(fib_index,
103 mfib_table_entry_remove (mfib_table_t *mfib_table,
104 const mfib_prefix_t *prefix,
105 fib_node_index_t fib_entry_index)
107 vlib_smp_unsafe_warning();
109 mfib_table->mft_total_route_counts--;
111 switch (prefix->fp_proto)
113 case FIB_PROTOCOL_IP4:
114 ip4_mfib_table_entry_remove(&mfib_table->v4,
115 &prefix->fp_grp_addr.ip4,
116 &prefix->fp_src_addr.ip4,
119 case FIB_PROTOCOL_IP6:
120 ip6_mfib_table_entry_remove(&mfib_table->v6,
121 &prefix->fp_grp_addr.ip6,
122 &prefix->fp_src_addr.ip6,
125 case FIB_PROTOCOL_MPLS:
130 mfib_entry_unlock(fib_entry_index);
134 mfib_table_entry_insert (mfib_table_t *mfib_table,
135 const mfib_prefix_t *prefix,
136 fib_node_index_t mfib_entry_index)
138 vlib_smp_unsafe_warning();
140 mfib_entry_lock(mfib_entry_index);
141 mfib_table->mft_total_route_counts++;
143 switch (prefix->fp_proto)
145 case FIB_PROTOCOL_IP4:
146 ip4_mfib_table_entry_insert(&mfib_table->v4,
147 &prefix->fp_grp_addr.ip4,
148 &prefix->fp_src_addr.ip4,
152 case FIB_PROTOCOL_IP6:
153 ip6_mfib_table_entry_insert(&mfib_table->v6,
154 &prefix->fp_grp_addr.ip6,
155 &prefix->fp_src_addr.ip6,
159 case FIB_PROTOCOL_MPLS:
165 mfib_table_entry_update (u32 fib_index,
166 const mfib_prefix_t *prefix,
167 mfib_source_t source,
168 mfib_entry_flags_t entry_flags)
170 fib_node_index_t mfib_entry_index;
171 mfib_table_t *mfib_table;
173 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
174 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
176 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
178 if (MFIB_ENTRY_FLAG_NONE != entry_flags)
181 * update to a non-existing entry with non-zero flags
183 mfib_entry_index = mfib_entry_create(fib_index, source,
184 prefix, entry_flags);
186 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
190 * the entry doesn't exist and the request is to set no flags
191 * the result would be an entry that doesn't exist - so do nothing
196 mfib_entry_lock(mfib_entry_index);
198 if (mfib_entry_update(mfib_entry_index,
204 * this update means we can now remove the entry.
206 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
209 mfib_entry_unlock(mfib_entry_index);
212 return (mfib_entry_index);
216 mfib_table_entry_path_update (u32 fib_index,
217 const mfib_prefix_t *prefix,
218 mfib_source_t source,
219 const fib_route_path_t *rpath,
220 mfib_itf_flags_t itf_flags)
222 fib_node_index_t mfib_entry_index;
223 mfib_table_t *mfib_table;
225 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
226 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
228 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
230 mfib_entry_index = mfib_entry_create(fib_index,
233 MFIB_ENTRY_FLAG_NONE);
235 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
238 mfib_entry_path_update(mfib_entry_index,
243 return (mfib_entry_index);
247 mfib_table_entry_path_remove (u32 fib_index,
248 const mfib_prefix_t *prefix,
249 mfib_source_t source,
250 const fib_route_path_t *rpath)
252 fib_node_index_t mfib_entry_index;
253 mfib_table_t *mfib_table;
255 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
256 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
258 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
261 * removing an etry that does not exist. i'll allow it.
269 * don't nobody go nowhere
271 mfib_entry_lock(mfib_entry_index);
273 no_more_sources = mfib_entry_path_remove(mfib_entry_index,
280 * last source gone. remove from the table
282 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
285 mfib_entry_unlock(mfib_entry_index);
290 mfib_table_entry_special_add (u32 fib_index,
291 const mfib_prefix_t *prefix,
292 mfib_source_t source,
293 mfib_entry_flags_t entry_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,
307 MFIB_ENTRY_FLAG_NONE);
309 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
312 mfib_entry_update(mfib_entry_index, source,
313 (MFIB_ENTRY_FLAG_EXCLUSIVE | entry_flags),
316 return (mfib_entry_index);
320 mfib_table_entry_delete_i (u32 fib_index,
321 fib_node_index_t mfib_entry_index,
322 const mfib_prefix_t *prefix,
323 mfib_source_t source)
325 mfib_table_t *mfib_table;
327 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
330 * don't nobody go nowhere
332 mfib_entry_lock(mfib_entry_index);
334 if (mfib_entry_delete(mfib_entry_index, source))
337 * last source gone. remove from the table
339 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
343 * still has sources, leave it be.
346 mfib_entry_unlock(mfib_entry_index);
350 mfib_table_entry_delete (u32 fib_index,
351 const mfib_prefix_t *prefix,
352 mfib_source_t source)
354 fib_node_index_t mfib_entry_index;
356 mfib_entry_index = mfib_table_lookup_exact_match(fib_index, prefix);
358 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
361 * removing an etry that does not exist.
362 * i'll allow it, but i won't like it.
364 clib_warning("%U not in FIB", format_mfib_prefix, prefix);
368 mfib_table_entry_delete_i(fib_index, mfib_entry_index,
374 mfib_table_entry_delete_index (fib_node_index_t mfib_entry_index,
375 mfib_source_t source)
377 mfib_prefix_t prefix;
379 mfib_entry_get_prefix(mfib_entry_index, &prefix);
381 mfib_table_entry_delete_i(mfib_entry_get_fib_index(mfib_entry_index),
382 mfib_entry_index, &prefix, source);
386 mfib_table_get_index_for_sw_if_index (fib_protocol_t proto,
391 case FIB_PROTOCOL_IP4:
392 return (ip4_mfib_table_get_index_for_sw_if_index(sw_if_index));
393 case FIB_PROTOCOL_IP6:
394 return (ip6_mfib_table_get_index_for_sw_if_index(sw_if_index));
395 case FIB_PROTOCOL_MPLS:
403 mfib_table_find (fib_protocol_t proto,
408 case FIB_PROTOCOL_IP4:
409 return (ip4_mfib_index_from_table_id(table_id));
410 case FIB_PROTOCOL_IP6:
411 return (ip6_mfib_index_from_table_id(table_id));
412 case FIB_PROTOCOL_MPLS:
420 mfib_table_find_or_create_and_lock (fib_protocol_t proto,
423 mfib_table_t *mfib_table;
428 case FIB_PROTOCOL_IP4:
429 fi = ip4_mfib_table_find_or_create_and_lock(table_id);
431 case FIB_PROTOCOL_IP6:
432 fi = ip6_mfib_table_find_or_create_and_lock(table_id);
434 case FIB_PROTOCOL_MPLS:
439 mfib_table = mfib_table_get(fi, proto);
441 mfib_table->mft_desc = format(NULL, "%U-VRF:%d",
442 format_fib_protocol, proto,
449 mfib_table_destroy (mfib_table_t *mfib_table)
451 vec_free(mfib_table->mft_desc);
453 switch (mfib_table->mft_proto)
455 case FIB_PROTOCOL_IP4:
456 ip4_mfib_table_destroy(&mfib_table->v4);
458 case FIB_PROTOCOL_IP6:
459 ip6_mfib_table_destroy(&mfib_table->v6);
461 case FIB_PROTOCOL_MPLS:
468 mfib_table_unlock (u32 fib_index,
469 fib_protocol_t proto)
471 mfib_table_t *mfib_table;
473 mfib_table = mfib_table_get(fib_index, proto);
474 mfib_table->mft_locks--;
476 if (0 == mfib_table->mft_locks)
478 mfib_table_destroy(mfib_table);
483 mfib_table_lock (u32 fib_index,
484 fib_protocol_t proto)
486 mfib_table_t *mfib_table;
488 mfib_table = mfib_table_get(fib_index, proto);
489 mfib_table->mft_locks++;
493 mfib_table_walk (u32 fib_index,
494 fib_protocol_t proto,
495 mfib_table_walk_fn_t fn,
500 case FIB_PROTOCOL_IP4:
501 ip4_mfib_table_walk(ip4_mfib_get(fib_index), fn, ctx);
503 case FIB_PROTOCOL_IP6:
504 ip6_mfib_table_walk(ip6_mfib_get(fib_index), fn, ctx);
506 case FIB_PROTOCOL_MPLS:
512 format_mfib_table_name (u8* s, va_list ap)
514 fib_node_index_t fib_index = va_arg(ap, fib_node_index_t);
515 fib_protocol_t proto = va_arg(ap, int); // int promotion
516 mfib_table_t *mfib_table;
518 mfib_table = mfib_table_get(fib_index, proto);
520 s = format(s, "%v", mfib_table->mft_desc);
525 static clib_error_t *
526 mfib_module_init (vlib_main_t * vm)
528 clib_error_t * error;
530 if ((error = vlib_call_init_function (vm, fib_module_init)))
532 if ((error = vlib_call_init_function (vm, rn_module_init)))
535 mfib_entry_module_init();
536 mfib_signal_module_init();
541 VLIB_INIT_FUNCTION(mfib_module_init);