blob: 68154b37ff89c4203d11fe3d97c0a7501205964e [file] [log] [blame]
Neale Ranns32e1c012016-11-22 17:07:28 +00001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vlib/vlib.h>
17#include <vnet/dpo/drop_dpo.h>
18
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>
Neale Ranns9e829a82018-12-17 05:50:32 -080023#include <vnet/mfib/mfib_entry_src.h>
24#include <vnet/mfib/mfib_entry_cover.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000025#include <vnet/mfib/mfib_signal.h>
26
27mfib_table_t *
28mfib_table_get (fib_node_index_t index,
29 fib_protocol_t proto)
30{
31 switch (proto)
32 {
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:
38 break;
39 }
40 ASSERT(0);
41 return (NULL);
42}
43
44static inline fib_node_index_t
45mfib_table_lookup_i (const mfib_table_t *mfib_table,
46 const mfib_prefix_t *prefix)
47{
48 switch (prefix->fp_proto)
49 {
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,
54 prefix->fp_len));
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,
59 prefix->fp_len));
60 case FIB_PROTOCOL_MPLS:
61 break;
62 }
63 return (FIB_NODE_INDEX_INVALID);
64}
65
66fib_node_index_t
67mfib_table_lookup (u32 fib_index,
68 const mfib_prefix_t *prefix)
69{
70 return (mfib_table_lookup_i(mfib_table_get(fib_index, prefix->fp_proto), prefix));
71}
72
73static inline fib_node_index_t
74mfib_table_lookup_exact_match_i (const mfib_table_t *mfib_table,
75 const mfib_prefix_t *prefix)
76{
77 switch (prefix->fp_proto)
78 {
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,
83 prefix->fp_len));
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,
88 prefix->fp_len));
89 case FIB_PROTOCOL_MPLS:
90 break;
91 }
92 return (FIB_NODE_INDEX_INVALID);
93}
94
95fib_node_index_t
96mfib_table_lookup_exact_match (u32 fib_index,
97 const mfib_prefix_t *prefix)
98{
99 return (mfib_table_lookup_exact_match_i(mfib_table_get(fib_index,
100 prefix->fp_proto),
101 prefix));
102}
103
Neale Ranns9e829a82018-12-17 05:50:32 -0800104static fib_node_index_t
105mfib_table_get_less_specific_i (const mfib_table_t *mfib_table,
106 const mfib_prefix_t *prefix)
107{
108 switch (prefix->fp_proto)
109 {
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,
114 prefix->fp_len));
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,
119 prefix->fp_len));
120 case FIB_PROTOCOL_MPLS:
121 break;
122 }
123 return (FIB_NODE_INDEX_INVALID);
124}
125
126fib_node_index_t
127mfib_table_get_less_specific (u32 fib_index,
128 const mfib_prefix_t *prefix)
129{
130 return (mfib_table_get_less_specific_i(mfib_table_get(fib_index,
131 prefix->fp_proto),
132 prefix));
133}
134
Neale Ranns32e1c012016-11-22 17:07:28 +0000135static void
136mfib_table_entry_remove (mfib_table_t *mfib_table,
137 const mfib_prefix_t *prefix,
Neale Ranns9e829a82018-12-17 05:50:32 -0800138 fib_node_index_t mfib_entry_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000139{
140 vlib_smp_unsafe_warning();
141
142 mfib_table->mft_total_route_counts--;
143
144 switch (prefix->fp_proto)
145 {
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,
150 prefix->fp_len);
151 break;
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,
156 prefix->fp_len);
157 break;
158 case FIB_PROTOCOL_MPLS:
159 ASSERT(0);
160 break;
161 }
162
Neale Ranns9e829a82018-12-17 05:50:32 -0800163 mfib_entry_cover_change_notify(mfib_entry_index,
164 FIB_NODE_INDEX_INVALID);
165 mfib_entry_unlock(mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000166}
167
168static void
Neale Ranns9e829a82018-12-17 05:50:32 -0800169mfib_table_post_insert_actions (mfib_table_t *mfib_table,
170 const mfib_prefix_t *prefix,
171 fib_node_index_t mfib_entry_index)
172{
173 fib_node_index_t mfib_entry_cover_index;
174
175 /*
176 * find the covering entry
177 */
178 mfib_entry_cover_index = mfib_table_get_less_specific_i(mfib_table,
179 prefix);
180 /*
181 * the indicies are the same when the default route is first added
182 */
183 if (mfib_entry_cover_index != mfib_entry_index)
184 {
185 /*
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.
191 */
192 if (!mfib_entry_is_host(mfib_entry_index))
193 {
194 mfib_entry_cover_change_notify(mfib_entry_cover_index,
195 mfib_entry_index);
196 }
197 }
198}
199
200
201static void
Neale Ranns32e1c012016-11-22 17:07:28 +0000202mfib_table_entry_insert (mfib_table_t *mfib_table,
203 const mfib_prefix_t *prefix,
204 fib_node_index_t mfib_entry_index)
205{
206 vlib_smp_unsafe_warning();
207
208 mfib_entry_lock(mfib_entry_index);
209 mfib_table->mft_total_route_counts++;
210
211 switch (prefix->fp_proto)
212 {
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,
217 prefix->fp_len,
218 mfib_entry_index);
219 break;
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,
224 prefix->fp_len,
225 mfib_entry_index);
226 break;
227 case FIB_PROTOCOL_MPLS:
228 break;
229 }
Neale Ranns9e829a82018-12-17 05:50:32 -0800230
231 mfib_table_post_insert_actions(mfib_table, prefix, mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000232}
233
234fib_node_index_t
235mfib_table_entry_update (u32 fib_index,
236 const mfib_prefix_t *prefix,
237 mfib_source_t source,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800238 fib_rpf_id_t rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +0000239 mfib_entry_flags_t entry_flags)
240{
241 fib_node_index_t mfib_entry_index;
242 mfib_table_t *mfib_table;
243
244 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
245 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
246
247 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
248 {
249 if (MFIB_ENTRY_FLAG_NONE != entry_flags)
250 {
251 /*
252 * update to a non-existing entry with non-zero flags
253 */
254 mfib_entry_index = mfib_entry_create(fib_index, source,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800255 prefix, rpf_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800256 entry_flags,
257 INDEX_INVALID);
Neale Ranns32e1c012016-11-22 17:07:28 +0000258
259 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
260 }
261 /*
262 * else
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
265 */
266 }
267 else
268 {
269 mfib_entry_lock(mfib_entry_index);
270
Neale Rannsa9374df2017-02-02 02:18:18 -0800271 if (mfib_entry_update(mfib_entry_index,
272 source,
273 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800274 rpf_id,
Neale Rannsa9374df2017-02-02 02:18:18 -0800275 INDEX_INVALID))
Neale Ranns32e1c012016-11-22 17:07:28 +0000276 {
277 /*
278 * this update means we can now remove the entry.
279 */
280 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
281 }
282
283 mfib_entry_unlock(mfib_entry_index);
284 }
285
286 return (mfib_entry_index);
287}
288
289fib_node_index_t
290mfib_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)
295{
296 fib_node_index_t mfib_entry_index;
297 mfib_table_t *mfib_table;
298
299 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
300 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
301
302 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
303 {
304 mfib_entry_index = mfib_entry_create(fib_index,
305 source,
306 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800307 MFIB_RPF_ID_NONE,
Neale Ranns9e829a82018-12-17 05:50:32 -0800308 MFIB_ENTRY_FLAG_NONE,
309 INDEX_INVALID);
310
311 mfib_entry_path_update(mfib_entry_index,
312 source,
313 rpath,
314 itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000315
316 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
317 }
Neale Ranns9e829a82018-12-17 05:50:32 -0800318 else
319 {
320 mfib_entry_path_update(mfib_entry_index,
321 source,
322 rpath,
323 itf_flags);
324 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000325 return (mfib_entry_index);
326}
327
328void
329mfib_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)
333{
334 fib_node_index_t mfib_entry_index;
335 mfib_table_t *mfib_table;
336
337 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
338 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
339
340 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
341 {
342 /*
343 * removing an etry that does not exist. i'll allow it.
344 */
345 }
346 else
347 {
348 int no_more_sources;
349
350 /*
351 * don't nobody go nowhere
352 */
353 mfib_entry_lock(mfib_entry_index);
354
355 no_more_sources = mfib_entry_path_remove(mfib_entry_index,
356 source,
357 rpath);
358
359 if (no_more_sources)
360 {
361 /*
362 * last source gone. remove from the table
363 */
364 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
365 }
366
367 mfib_entry_unlock(mfib_entry_index);
368 }
369}
370
Neale Rannsa9374df2017-02-02 02:18:18 -0800371fib_node_index_t
372mfib_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,
Neale Ranns9e829a82018-12-17 05:50:32 -0800376 index_t repi)
Neale Rannsa9374df2017-02-02 02:18:18 -0800377{
378 fib_node_index_t mfib_entry_index;
379 mfib_table_t *mfib_table;
380
381 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
382 mfib_entry_index = mfib_table_lookup_exact_match_i(mfib_table, prefix);
383
Neale Ranns9e829a82018-12-17 05:50:32 -0800384 if (INDEX_INVALID != repi)
385 {
386 entry_flags |= MFIB_ENTRY_FLAG_EXCLUSIVE;
387 }
388
Neale Rannsa9374df2017-02-02 02:18:18 -0800389 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
390 {
391 mfib_entry_index = mfib_entry_create(fib_index,
392 source,
393 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800394 MFIB_RPF_ID_NONE,
Neale Ranns9e829a82018-12-17 05:50:32 -0800395 entry_flags,
396 repi);
Neale Rannsa9374df2017-02-02 02:18:18 -0800397
398 mfib_table_entry_insert(mfib_table, prefix, mfib_entry_index);
399 }
Neale Ranns9e829a82018-12-17 05:50:32 -0800400 else
401 {
402 mfib_entry_special_add(mfib_entry_index, source, entry_flags,
403 MFIB_RPF_ID_NONE, repi);
404 }
Neale Rannsa9374df2017-02-02 02:18:18 -0800405
406 return (mfib_entry_index);
407}
408
Neale Ranns32e1c012016-11-22 17:07:28 +0000409static void
410mfib_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)
414{
415 mfib_table_t *mfib_table;
416
417 mfib_table = mfib_table_get(fib_index, prefix->fp_proto);
418
419 /*
420 * don't nobody go nowhere
421 */
422 mfib_entry_lock(mfib_entry_index);
423
424 if (mfib_entry_delete(mfib_entry_index, source))
425 {
426 /*
427 * last source gone. remove from the table
428 */
429 mfib_table_entry_remove(mfib_table, prefix, mfib_entry_index);
430 }
431 /*
432 * else
433 * still has sources, leave it be.
434 */
435
436 mfib_entry_unlock(mfib_entry_index);
437}
438
439void
440mfib_table_entry_delete (u32 fib_index,
441 const mfib_prefix_t *prefix,
442 mfib_source_t source)
443{
444 fib_node_index_t mfib_entry_index;
445
446 mfib_entry_index = mfib_table_lookup_exact_match(fib_index, prefix);
447
448 if (FIB_NODE_INDEX_INVALID == mfib_entry_index)
449 {
450 /*
451 * removing an etry that does not exist.
452 * i'll allow it, but i won't like it.
453 */
454 clib_warning("%U not in FIB", format_mfib_prefix, prefix);
455 }
456 else
457 {
458 mfib_table_entry_delete_i(fib_index, mfib_entry_index,
459 prefix, source);
460 }
461}
462
463void
464mfib_table_entry_delete_index (fib_node_index_t mfib_entry_index,
465 mfib_source_t source)
466{
Neale Ranns9e829a82018-12-17 05:50:32 -0800467 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +0000468
Neale Ranns9e829a82018-12-17 05:50:32 -0800469 prefix = mfib_entry_get_prefix(mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000470
471 mfib_table_entry_delete_i(mfib_entry_get_fib_index(mfib_entry_index),
Neale Ranns9e829a82018-12-17 05:50:32 -0800472 mfib_entry_index, prefix, source);
Neale Ranns32e1c012016-11-22 17:07:28 +0000473}
474
475u32
476mfib_table_get_index_for_sw_if_index (fib_protocol_t proto,
477 u32 sw_if_index)
478{
479 switch (proto)
480 {
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:
486 ASSERT(0);
487 break;
488 }
489 return (~0);
490}
491
492u32
Neale Ranns775f73c2018-12-20 03:01:49 -0800493mfib_table_get_table_id (u32 fib_index,
494 fib_protocol_t proto)
495{
496 mfib_table_t *mfib_table;
497
498 mfib_table = mfib_table_get(fib_index, proto);
499
500 return ((NULL != mfib_table ? mfib_table->mft_table_id : ~0));
501}
502
503u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000504mfib_table_find (fib_protocol_t proto,
505 u32 table_id)
506{
507 switch (proto)
508 {
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:
514 ASSERT(0);
515 break;
516 }
517 return (~0);
518}
519
Neale Ranns2297af02017-09-12 09:45:04 -0700520static u32
521mfib_table_find_or_create_and_lock_i (fib_protocol_t proto,
522 u32 table_id,
523 mfib_source_t src,
524 const u8 *name)
Neale Ranns32e1c012016-11-22 17:07:28 +0000525{
526 mfib_table_t *mfib_table;
527 fib_node_index_t fi;
528
529 switch (proto)
530 {
531 case FIB_PROTOCOL_IP4:
Neale Ranns15002542017-09-10 04:39:11 -0700532 fi = ip4_mfib_table_find_or_create_and_lock(table_id, src);
Neale Ranns32e1c012016-11-22 17:07:28 +0000533 break;
534 case FIB_PROTOCOL_IP6:
Neale Ranns15002542017-09-10 04:39:11 -0700535 fi = ip6_mfib_table_find_or_create_and_lock(table_id, src);
Neale Ranns32e1c012016-11-22 17:07:28 +0000536 break;
537 case FIB_PROTOCOL_MPLS:
538 default:
539 return (~0);
540 }
541
542 mfib_table = mfib_table_get(fi, proto);
543
Neale Ranns2297af02017-09-12 09:45:04 -0700544 if (NULL == mfib_table->mft_desc)
545 {
546 if (name && name[0])
547 {
548 mfib_table->mft_desc = format(NULL, "%s", name);
549 }
550 else
551 {
552 mfib_table->mft_desc = format(NULL, "%U-VRF:%d",
553 format_fib_protocol, proto,
554 table_id);
555 }
556 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000557
558 return (fi);
559}
560
Neale Ranns2297af02017-09-12 09:45:04 -0700561u32
562mfib_table_find_or_create_and_lock (fib_protocol_t proto,
563 u32 table_id,
564 mfib_source_t src)
565{
566 return (mfib_table_find_or_create_and_lock_i(proto, table_id,
567 src, NULL));
568}
569
570u32
571mfib_table_find_or_create_and_lock_w_name (fib_protocol_t proto,
572 u32 table_id,
573 mfib_source_t src,
574 const u8 *name)
575{
576 return (mfib_table_find_or_create_and_lock_i(proto, table_id,
577 src, name));
578}
579
Neale Ranns15002542017-09-10 04:39:11 -0700580/**
581 * @brief Table flush context. Store the indicies of matching FIB entries
582 * that need to be removed.
583 */
584typedef struct mfib_table_flush_ctx_t_
585{
586 /**
587 * The list of entries to flush
588 */
589 fib_node_index_t *mftf_entries;
590
591 /**
592 * The source we are flushing
593 */
594 mfib_source_t mftf_source;
595} mfib_table_flush_ctx_t;
596
597static int
598mfib_table_flush_cb (fib_node_index_t mfib_entry_index,
599 void *arg)
600{
601 mfib_table_flush_ctx_t *ctx = arg;
602
603 if (mfib_entry_is_sourced(mfib_entry_index, ctx->mftf_source))
604 {
605 vec_add1(ctx->mftf_entries, mfib_entry_index);
606 }
607 return (1);
608}
609
610void
611mfib_table_flush (u32 mfib_index,
612 fib_protocol_t proto,
613 mfib_source_t source)
614{
615 fib_node_index_t *mfib_entry_index;
616 mfib_table_flush_ctx_t ctx = {
617 .mftf_entries = NULL,
618 .mftf_source = source,
619 };
620
621 mfib_table_walk(mfib_index, proto,
622 mfib_table_flush_cb,
623 &ctx);
624
625 vec_foreach(mfib_entry_index, ctx.mftf_entries)
626 {
627 mfib_table_entry_delete_index(*mfib_entry_index, source);
628 }
629
630 vec_free(ctx.mftf_entries);
631}
632
Neale Ranns32e1c012016-11-22 17:07:28 +0000633static void
634mfib_table_destroy (mfib_table_t *mfib_table)
635{
636 vec_free(mfib_table->mft_desc);
637
638 switch (mfib_table->mft_proto)
639 {
640 case FIB_PROTOCOL_IP4:
641 ip4_mfib_table_destroy(&mfib_table->v4);
642 break;
643 case FIB_PROTOCOL_IP6:
644 ip6_mfib_table_destroy(&mfib_table->v6);
645 break;
646 case FIB_PROTOCOL_MPLS:
647 ASSERT(0);
648 break;
649 }
650}
651
652void
653mfib_table_unlock (u32 fib_index,
Neale Ranns15002542017-09-10 04:39:11 -0700654 fib_protocol_t proto,
655 mfib_source_t source)
Neale Ranns32e1c012016-11-22 17:07:28 +0000656{
657 mfib_table_t *mfib_table;
658
659 mfib_table = mfib_table_get(fib_index, proto);
Neale Ranns15002542017-09-10 04:39:11 -0700660 mfib_table->mft_locks[source]--;
661 mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS]--;
Neale Ranns32e1c012016-11-22 17:07:28 +0000662
Neale Ranns15002542017-09-10 04:39:11 -0700663 if (0 == mfib_table->mft_locks[source])
Neale Ranns32e1c012016-11-22 17:07:28 +0000664 {
Neale Ranns15002542017-09-10 04:39:11 -0700665 /*
666 * The source no longer needs the table. flush any routes
667 * from it just in case
668 */
669 mfib_table_flush(fib_index, proto, source);
670 }
671
672 if (0 == mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS])
673 {
674 /*
675 * no more locak from any source - kill it
676 */
677 mfib_table_destroy(mfib_table);
Neale Ranns32e1c012016-11-22 17:07:28 +0000678 }
679}
680
681void
682mfib_table_lock (u32 fib_index,
Neale Ranns15002542017-09-10 04:39:11 -0700683 fib_protocol_t proto,
684 mfib_source_t source)
Neale Ranns32e1c012016-11-22 17:07:28 +0000685{
686 mfib_table_t *mfib_table;
687
688 mfib_table = mfib_table_get(fib_index, proto);
Neale Ranns15002542017-09-10 04:39:11 -0700689 mfib_table->mft_locks[source]++;
690 mfib_table->mft_locks[MFIB_TABLE_TOTAL_LOCKS]++;
Neale Ranns32e1c012016-11-22 17:07:28 +0000691}
692
Neale Ranns9e829a82018-12-17 05:50:32 -0800693u32
694mfib_table_get_n_routes (fib_node_index_t fib_index,
695 fib_protocol_t proto)
696{
697 mfib_table_t *mfib_table;
698
699 mfib_table = mfib_table_get(fib_index, proto);
700
701 return (mfib_table->mft_total_route_counts);
702}
703
Neale Ranns5a8123b2017-01-26 01:18:23 -0800704void
705mfib_table_walk (u32 fib_index,
706 fib_protocol_t proto,
707 mfib_table_walk_fn_t fn,
708 void *ctx)
709{
710 switch (proto)
711 {
712 case FIB_PROTOCOL_IP4:
713 ip4_mfib_table_walk(ip4_mfib_get(fib_index), fn, ctx);
714 break;
715 case FIB_PROTOCOL_IP6:
716 ip6_mfib_table_walk(ip6_mfib_get(fib_index), fn, ctx);
717 break;
718 case FIB_PROTOCOL_MPLS:
719 break;
720 }
721}
722
Neale Ranns32e1c012016-11-22 17:07:28 +0000723u8*
Christophe Fontained3c008d2017-10-02 18:10:54 +0200724format_mfib_table_name (u8* s, va_list *ap)
Neale Ranns32e1c012016-11-22 17:07:28 +0000725{
Christophe Fontained3c008d2017-10-02 18:10:54 +0200726 fib_node_index_t fib_index = va_arg(*ap, fib_node_index_t);
727 fib_protocol_t proto = va_arg(*ap, int); // int promotion
Neale Ranns32e1c012016-11-22 17:07:28 +0000728 mfib_table_t *mfib_table;
729
730 mfib_table = mfib_table_get(fib_index, proto);
731
732 s = format(s, "%v", mfib_table->mft_desc);
733
734 return (s);
735}
736
Neale Rannsc87aafa2017-11-29 00:59:31 -0800737u8 *
738format_mfib_table_memory (u8 *s, va_list *args)
739{
740 s = format(s, "%U", format_ip4_mfib_table_memory);
741 s = format(s, "%U", format_ip6_mfib_table_memory);
742
743 return (s);
744}
745
Neale Ranns32e1c012016-11-22 17:07:28 +0000746static clib_error_t *
747mfib_module_init (vlib_main_t * vm)
748{
749 clib_error_t * error;
750
Neale Ranns9e829a82018-12-17 05:50:32 -0800751 mfib_entry_src_module_init();
Neale Ranns1ec36522017-11-29 05:20:37 -0800752 mfib_entry_module_init();
753 mfib_signal_module_init();
754
Neale Ranns32e1c012016-11-22 17:07:28 +0000755 if ((error = vlib_call_init_function (vm, fib_module_init)))
756 return (error);
757 if ((error = vlib_call_init_function (vm, rn_module_init)))
758 return (error);
759
Neale Ranns32e1c012016-11-22 17:07:28 +0000760 return (error);
761}
762
763VLIB_INIT_FUNCTION(mfib_module_init);