blob: acbe90bb10937cc20da846d5226c81ee805497b3 [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
18#include <vnet/mfib/mfib_entry.h>
19#include <vnet/fib/fib_path_list.h>
20
21#include <vnet/dpo/drop_dpo.h>
22#include <vnet/dpo/replicate_dpo.h>
23
24/**
25 * Debug macro
26 */
27#ifdef MFIB_DEBUG
28#DEFIne MFIB_ENTRY_DBG(_e, _fmt, _args...) \
29{ \
30 u8*__tmp = NULL; \
31 __tmp = format(__tmp, "e:[%d:%U", \
32 mfib_entry_get_index(_e), \
33 format_ip46_address, \
34 &_e->mfe_prefix.fp_grp_addr, \
35 IP46_TYPE_ANY); \
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, \
42 IP46_TYPE_ANY); \
43 __tmp = format(__tmp, _fmt, ##_args); \
44 clib_warning("%s", __tmp); \
45 vec_free(__tmp); \
46}
47#else
48#define MFIB_ENTRY_DBG(_e, _fmt, _args...)
49#endif
50
51/**
52 * The source of an MFIB entry
53 */
54typedef struct mfib_entry_src_t_
55{
56 /**
57 * Which source this is
58 */
59 mfib_source_t mfes_src;
60
61 /**
62 * The path-list of forwarding interfaces
63 */
64 fib_node_index_t mfes_pl;
65
66 /**
67 * Route flags
68 */
69 mfib_entry_flags_t mfes_flags;
70
71 /**
72 * The hash table of all interfaces
73 */
74 mfib_itf_t *mfes_itfs;
75} mfib_entry_src_t;
76
77/**
78 * String names for each source
79 */
80static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
81
82/*
83 * Pool for all fib_entries
84 */
85mfib_entry_t *mfib_entry_pool;
86
87static fib_node_t *
88mfib_entry_get_node (fib_node_index_t index)
89{
90 return ((fib_node_t*)mfib_entry_get(index));
91}
92
93static fib_protocol_t
94mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
95{
96 return (mfib_entry->mfe_prefix.fp_proto);
97}
98
99fib_forward_chain_type_t
100mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
101{
102 switch (mfib_entry->mfe_prefix.fp_proto)
103 {
104 case FIB_PROTOCOL_IP4:
105 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
106 case FIB_PROTOCOL_IP6:
107 return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
108 case FIB_PROTOCOL_MPLS:
109 ASSERT(0);
110 break;
111 }
112 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
113}
114
115static u8 *
116format_mfib_entry_dpo (u8 * s, va_list * args)
117{
118 index_t fei = va_arg(*args, index_t);
119 CLIB_UNUSED(u32 indent) = va_arg(*args, u32);
120
121 return (format(s, "%U",
122 format_mfib_entry, fei,
123 MFIB_ENTRY_FORMAT_BRIEF));
124}
125
126u8 *
127format_mfib_entry (u8 * s, va_list * args)
128{
129 fib_node_index_t fei, mfi;
130 mfib_entry_t *mfib_entry;
131 mfib_entry_src_t *msrc;
132 u32 sw_if_index;
133 int level;
134
135 fei = va_arg (*args, fib_node_index_t);
136 level = va_arg (*args, int);
137 mfib_entry = mfib_entry_get(fei);
138
139 s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
140 s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);
141
142 if (level >= MFIB_ENTRY_FORMAT_DETAIL)
143 {
144 s = format (s, "\n");
145 s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
146 s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
147 s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
148 vec_foreach(msrc, mfib_entry->mfe_srcs)
149 {
150 s = format (s, " src:%s", mfib_source_names[msrc->mfes_src]);
151 s = format (s, ": %U\n", format_mfib_entry_flags, msrc->mfes_flags);
152 if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
153 {
154 s = fib_path_list_format(msrc->mfes_pl, s);
155 }
156 hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
157 ({
158 s = format(s, " %U\n", format_mfib_itf, mfi);
159 }));
160 }
161 }
162
163 s = format(s, "\n Interfaces:");
164 hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
165 ({
166 s = format(s, "\n %U", format_mfib_itf, mfi);
167 }));
168
169 s = format(s, "\n %U-chain\n %U",
170 format_fib_forw_chain_type,
171 mfib_entry_get_default_chain_type(mfib_entry),
172 format_dpo_id,
173 &mfib_entry->mfe_rep,
174 2);
175 s = format(s, "\n");
176
177 if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
178 {
179 s = format(s, "\nchildren:");
180 s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
181 }
182
183 return (s);
184}
185
186static mfib_entry_t*
187mfib_entry_from_fib_node (fib_node_t *node)
188{
189#if CLIB_DEBUG > 0
190 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
191#endif
192 return ((mfib_entry_t*)node);
193}
194
195static int
196mfib_entry_src_cmp_for_sort (void * v1,
197 void * v2)
198{
199 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
200
201 return (esrc1->mfes_src - esrc2->mfes_src);
202}
203
204static void
205mfib_entry_src_init (mfib_entry_t *mfib_entry,
206 mfib_source_t source)
207
208{
209 mfib_entry_src_t esrc = {
210 .mfes_pl = FIB_NODE_INDEX_INVALID,
211 .mfes_flags = MFIB_ENTRY_FLAG_NONE,
212 .mfes_src = source,
213 };
214
215 vec_add1(mfib_entry->mfe_srcs, esrc);
216 vec_sort_with_function(mfib_entry->mfe_srcs,
217 mfib_entry_src_cmp_for_sort);
218}
219
220static mfib_entry_src_t *
221mfib_entry_src_find (const mfib_entry_t *mfib_entry,
222 mfib_source_t source,
223 u32 *index)
224
225{
226 mfib_entry_src_t *esrc;
227 int ii;
228
229 ii = 0;
230 vec_foreach(esrc, mfib_entry->mfe_srcs)
231 {
232 if (esrc->mfes_src == source)
233 {
234 if (NULL != index)
235 {
236 *index = ii;
237 }
238 return (esrc);
239 }
240 else
241 {
242 ii++;
243 }
244 }
245
246 return (NULL);
247}
248
249static mfib_entry_src_t *
250mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
251 mfib_source_t source)
252{
253 mfib_entry_src_t *esrc;
254
255 esrc = mfib_entry_src_find(mfib_entry, source, NULL);
256
257 if (NULL == esrc)
258 {
259 mfib_entry_src_init(mfib_entry, source);
260 }
261
262 return (mfib_entry_src_find(mfib_entry, source, NULL));
263}
264
265static mfib_entry_src_t*
266mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
267{
268 mfib_entry_src_t *bsrc;
269
270 /*
271 * the enum of sources is deliberately arranged in priority order
272 */
273 if (0 == vec_len(mfib_entry->mfe_srcs))
274 {
275 bsrc = NULL;
276 }
277 else
278 {
279 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
280 }
281
282 return (bsrc);
283}
284
285static void
286mfib_entry_src_flush (mfib_entry_src_t *msrc)
287{
288 u32 sw_if_index;
289 index_t mfii;
290
291 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
292 ({
293 mfib_itf_delete(mfib_itf_get(mfii));
294 }));
Neale Rannsa9374df2017-02-02 02:18:18 -0800295 fib_path_list_unlock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000296}
297
298static void
299mfib_entry_src_remove (mfib_entry_t *mfib_entry,
300 mfib_source_t source)
301
302{
303 mfib_entry_src_t *msrc;
304 u32 index = ~0;
305
306 msrc = mfib_entry_src_find(mfib_entry, source, &index);
307
308 if (NULL != msrc)
309 {
310 mfib_entry_src_flush(msrc);
311 vec_del1(mfib_entry->mfe_srcs, index);
312 }
313}
314
315static int
316mfib_entry_src_n_itfs (const mfib_entry_src_t *msrc)
317{
318 return (hash_elts(msrc->mfes_itfs));
319}
320
321
322static void
323mfib_entry_last_lock_gone (fib_node_t *node)
324{
325 mfib_entry_t *mfib_entry;
326 mfib_entry_src_t *msrc;
327
328 mfib_entry = mfib_entry_from_fib_node(node);
329
330 dpo_reset(&mfib_entry->mfe_rep);
331
332 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
333
334 vec_foreach(msrc, mfib_entry->mfe_srcs)
335 {
336 mfib_entry_src_flush(msrc);
337 }
338
339 fib_path_list_unlock(mfib_entry->mfe_parent);
340 vec_free(mfib_entry->mfe_srcs);
341
342 fib_node_deinit(&mfib_entry->mfe_node);
343 pool_put(mfib_entry_pool, mfib_entry);
344}
345
346/*
347 * mfib_entry_back_walk_notify
348 *
349 * A back walk has reach this entry.
350 */
351static fib_node_back_walk_rc_t
352mfib_entry_back_walk_notify (fib_node_t *node,
353 fib_node_back_walk_ctx_t *ctx)
354{
355 // FIXME - re-evalute
356
357 return (FIB_NODE_BACK_WALK_CONTINUE);
358}
359
360static void
361mfib_entry_show_memory (void)
362{
363 fib_show_memory_usage("multicast-Entry",
364 pool_elts(mfib_entry_pool),
365 pool_len(mfib_entry_pool),
366 sizeof(mfib_entry_t));
367}
368
369/*
370 * The MFIB entry's graph node virtual function table
371 */
372static const fib_node_vft_t mfib_entry_vft = {
373 .fnv_get = mfib_entry_get_node,
374 .fnv_last_lock = mfib_entry_last_lock_gone,
375 .fnv_back_walk = mfib_entry_back_walk_notify,
376 .fnv_mem_show = mfib_entry_show_memory,
377};
378
379u32
380mfib_entry_child_add (fib_node_index_t mfib_entry_index,
381 fib_node_type_t child_type,
382 fib_node_index_t child_index)
383{
384 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
385 mfib_entry_index,
386 child_type,
387 child_index));
388};
389
390void
391mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
392 u32 sibling_index)
393{
394 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
395 mfib_entry_index,
396 sibling_index);
397}
398
399static mfib_entry_t *
400mfib_entry_alloc (u32 fib_index,
401 const mfib_prefix_t *prefix,
402 fib_node_index_t *mfib_entry_index)
403{
404 mfib_entry_t *mfib_entry;
405
406 pool_get(mfib_entry_pool, mfib_entry);
407 memset(mfib_entry, 0, sizeof(*mfib_entry));
408
409 fib_node_init(&mfib_entry->mfe_node,
410 FIB_NODE_TYPE_MFIB_ENTRY);
411
412 mfib_entry->mfe_fib_index = fib_index;
413 mfib_entry->mfe_prefix = *prefix;
414 mfib_entry->mfe_parent = FIB_NODE_INDEX_INVALID;
415
416 dpo_reset(&mfib_entry->mfe_rep);
417
418 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
419
420 MFIB_ENTRY_DBG(mfib_entry, "alloc");
421
422 return (mfib_entry);
423}
424
425typedef struct mfib_entry_collect_forwarding_ctx_t_
426{
427 load_balance_path_t * next_hops;
428 fib_forward_chain_type_t fct;
429} mfib_entry_collect_forwarding_ctx_t;
430
431static int
432mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
433 fib_node_index_t path_index,
434 void *arg)
435{
436 mfib_entry_collect_forwarding_ctx_t *ctx;
437 load_balance_path_t *nh;
438
439 ctx = arg;
440
441 /*
442 * if the path is not resolved, don't include it.
443 */
444 if (!fib_path_is_resolved(path_index))
445 {
446 return (!0);
447 }
448
449 switch (ctx->fct)
450 {
451 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
452 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
453 /*
454 * EOS traffic with no label to stack, we need the IP Adj
455 */
456 vec_add2(ctx->next_hops, nh, 1);
457
458 nh->path_index = path_index;
459 nh->path_weight = fib_path_get_weight(path_index);
460 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
461 break;
462
463 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
464 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
465 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
466 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
467 case FIB_FORW_CHAIN_TYPE_ETHERNET:
Florin Corasce1b4c72017-01-26 14:25:34 -0800468 case FIB_FORW_CHAIN_TYPE_NSH:
Neale Ranns32e1c012016-11-22 17:07:28 +0000469 ASSERT(0);
470 break;
471 }
472
473 return (!0);
474}
475
476static void
477mfib_entry_stack (mfib_entry_t *mfib_entry)
478{
Neale Ranns32e1c012016-11-22 17:07:28 +0000479 dpo_proto_t dp;
480
481 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
482
483 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_parent)
484 {
Neale Rannsa9374df2017-02-02 02:18:18 -0800485 mfib_entry_collect_forwarding_ctx_t ctx = {
486 .next_hops = NULL,
487 .fct = mfib_entry_get_default_chain_type(mfib_entry),
488 };
489
Neale Ranns32e1c012016-11-22 17:07:28 +0000490 fib_path_list_walk(mfib_entry->mfe_parent,
491 mfib_entry_src_collect_forwarding,
492 &ctx);
493
Neale Rannsa9374df2017-02-02 02:18:18 -0800494 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
Neale Ranns32e1c012016-11-22 17:07:28 +0000495 {
Neale Rannsa9374df2017-02-02 02:18:18 -0800496 /*
497 * each path contirbutes a next-hop. form a replicate
498 * from those choices.
499 */
500 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
501 dpo_is_drop(&mfib_entry->mfe_rep))
502 {
503 dpo_id_t tmp_dpo = DPO_INVALID;
Neale Ranns32e1c012016-11-22 17:07:28 +0000504
Neale Rannsa9374df2017-02-02 02:18:18 -0800505 dpo_set(&tmp_dpo,
506 DPO_REPLICATE, dp,
507 replicate_create(0, dp));
508
509 dpo_stack(DPO_MFIB_ENTRY, dp,
510 &mfib_entry->mfe_rep,
511 &tmp_dpo);
512
513 dpo_reset(&tmp_dpo);
514 }
515 replicate_multipath_update(&mfib_entry->mfe_rep,
516 ctx.next_hops);
517 }
518 else
519 {
520 /*
521 * for exclusive routes the source provided a replicate DPO
522 * we we stashed inthe special path list with one path
523 * so we can stack directly on that.
524 */
525 ASSERT(1 == vec_len(ctx.next_hops));
Neale Ranns32e1c012016-11-22 17:07:28 +0000526
527 dpo_stack(DPO_MFIB_ENTRY, dp,
528 &mfib_entry->mfe_rep,
Neale Rannsa9374df2017-02-02 02:18:18 -0800529 &ctx.next_hops[0].path_dpo);
530 dpo_reset(&ctx.next_hops[0].path_dpo);
531 vec_free(ctx.next_hops);
Neale Ranns32e1c012016-11-22 17:07:28 +0000532 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000533 }
534 else
535 {
536 dpo_stack(DPO_MFIB_ENTRY, dp,
537 &mfib_entry->mfe_rep,
538 drop_dpo_get(dp));
539 }
540}
541
542static void
543mfib_entry_forwarding_path_add (mfib_entry_src_t *msrc,
544 const fib_route_path_t *rpath)
545{
546 fib_node_index_t old_pl_index;
547 fib_route_path_t *rpaths;
548
Neale Rannsa9374df2017-02-02 02:18:18 -0800549 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
550
Neale Ranns32e1c012016-11-22 17:07:28 +0000551 /*
552 * path-lists require a vector of paths
553 */
554 rpaths = NULL;
555 vec_add1(rpaths, rpath[0]);
556
557 old_pl_index = msrc->mfes_pl;
558
559 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
560 {
561 msrc->mfes_pl =
562 fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
563 rpaths);
564 }
565 else
566 {
567 msrc->mfes_pl =
568 fib_path_list_copy_and_path_add(msrc->mfes_pl,
569 FIB_PATH_LIST_FLAG_NO_URPF,
570 rpaths);
571 }
572 fib_path_list_lock(msrc->mfes_pl);
573 fib_path_list_unlock(old_pl_index);
574
575 vec_free(rpaths);
576}
577
578static int
579mfib_entry_forwarding_path_remove (mfib_entry_src_t *msrc,
580 const fib_route_path_t *rpath)
581{
582 fib_node_index_t old_pl_index;
583 fib_route_path_t *rpaths;
584
Neale Rannsa9374df2017-02-02 02:18:18 -0800585 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
586
Neale Ranns32e1c012016-11-22 17:07:28 +0000587 /*
588 * path-lists require a vector of paths
589 */
590 rpaths = NULL;
591 vec_add1(rpaths, rpath[0]);
592
593 old_pl_index = msrc->mfes_pl;
594
595 msrc->mfes_pl =
596 fib_path_list_copy_and_path_remove(msrc->mfes_pl,
597 FIB_PATH_LIST_FLAG_NONE,
598 rpaths);
599
600 fib_path_list_lock(msrc->mfes_pl);
601 fib_path_list_unlock(old_pl_index);
602
603 vec_free(rpaths);
604
605 return (FIB_NODE_INDEX_INVALID != msrc->mfes_pl);
606}
607
608static void
609mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry)
610{
611 fib_node_index_t old_pl_index;
612 mfib_entry_src_t *bsrc;
613
614 old_pl_index = mfib_entry->mfe_parent;
615
616 /*
617 * copy the forwarding data from the bast source
618 */
619 bsrc = mfib_entry_get_best_src(mfib_entry);
620
621 if (NULL == bsrc)
622 {
623 mfib_entry->mfe_parent = FIB_NODE_INDEX_INVALID;
624 }
625 else
626 {
627 mfib_entry->mfe_parent = bsrc->mfes_pl;
628 mfib_entry->mfe_flags = bsrc->mfes_flags;
629 mfib_entry->mfe_itfs = bsrc->mfes_itfs;
630 }
631
632 /*
633 * re-stack the entry on the best forwarding info.
634 */
635 if (old_pl_index != mfib_entry->mfe_parent ||
636 FIB_NODE_INDEX_INVALID == old_pl_index)
637 {
638 mfib_entry_stack(mfib_entry);
639
640 fib_path_list_lock(mfib_entry->mfe_parent);
641 fib_path_list_unlock(old_pl_index);
642 }
643}
644
645
646fib_node_index_t
647mfib_entry_create (u32 fib_index,
648 mfib_source_t source,
649 const mfib_prefix_t *prefix,
650 mfib_entry_flags_t entry_flags)
651{
652 fib_node_index_t mfib_entry_index;
653 mfib_entry_t *mfib_entry;
654 mfib_entry_src_t *msrc;
655
656 mfib_entry = mfib_entry_alloc(fib_index, prefix,
657 &mfib_entry_index);
658 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
659 msrc->mfes_flags = entry_flags;
660
661 mfib_entry_recalculate_forwarding(mfib_entry);
662
663 return (mfib_entry_index);
664}
665
666static int
667mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
668{
669 return (0 == vec_len(mfib_entry->mfe_srcs));
670}
671
672static int
673mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
674{
675 return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
676 0 == mfib_entry_src_n_itfs(msrc)));
677}
678
679int
680mfib_entry_update (fib_node_index_t mfib_entry_index,
681 mfib_source_t source,
Neale Rannsa9374df2017-02-02 02:18:18 -0800682 mfib_entry_flags_t entry_flags,
683 index_t repi)
Neale Ranns32e1c012016-11-22 17:07:28 +0000684{
685 mfib_entry_t *mfib_entry;
686 mfib_entry_src_t *msrc;
687
688 mfib_entry = mfib_entry_get(mfib_entry_index);
689 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
690 msrc->mfes_flags = entry_flags;
691
Neale Rannsa9374df2017-02-02 02:18:18 -0800692 if (INDEX_INVALID != repi)
693 {
694 /*
695 * The source is providing its own replicate DPO.
696 * Create a sepcial path-list to manage it, that way
697 * this entry and the source are equivalent to a normal
698 * entry
699 */
700 fib_node_index_t old_pl_index;
701 fib_protocol_t fp;
702 dpo_id_t dpo = DPO_INVALID;
703
704 fp = mfib_entry_get_proto(mfib_entry);
705 old_pl_index = msrc->mfes_pl;
706
707 dpo_set(&dpo, DPO_REPLICATE,
708 fib_proto_to_dpo(fp),
709 repi);
710
711 msrc->mfes_pl =
712 fib_path_list_create_special(fp,
713 FIB_PATH_LIST_FLAG_EXCLUSIVE,
714 &dpo);
715
716 dpo_reset(&dpo);
717 fib_path_list_lock(msrc->mfes_pl);
718 fib_path_list_unlock(old_pl_index);
719 }
720
Neale Ranns32e1c012016-11-22 17:07:28 +0000721 if (mfib_entry_src_ok_for_delete(msrc))
722 {
723 /*
724 * this source has no interfaces and no flags.
725 * it has nothing left to give - remove it
726 */
727 mfib_entry_src_remove(mfib_entry, source);
728 }
729
730 mfib_entry_recalculate_forwarding(mfib_entry);
731
732 return (mfib_entry_ok_for_delete(mfib_entry));
733}
734
735static void
736mfib_entry_itf_add (mfib_entry_src_t *msrc,
737 u32 sw_if_index,
738 index_t mi)
739{
740 hash_set(msrc->mfes_itfs, sw_if_index, mi);
741}
742
743static void
744mfib_entry_itf_remove (mfib_entry_src_t *msrc,
745 u32 sw_if_index)
746{
747 mfib_itf_t *mfi;
748
749 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
750
751 mfib_itf_delete(mfi);
752
753 hash_unset(msrc->mfes_itfs, sw_if_index);
754}
755
756void
757mfib_entry_path_update (fib_node_index_t mfib_entry_index,
758 mfib_source_t source,
759 const fib_route_path_t *rpath,
760 mfib_itf_flags_t itf_flags)
761{
762 mfib_entry_t *mfib_entry;
763 mfib_entry_src_t *msrc;
764 mfib_itf_t *mfib_itf;
765
766 mfib_entry = mfib_entry_get(mfib_entry_index);
767 ASSERT(NULL != mfib_entry);
768 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
769
770 /*
771 * search for the interface in the current set
772 */
773 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
774 rpath[0].frp_sw_if_index);
775
776 if (NULL == mfib_itf)
777 {
778 /*
779 * this is a path we do not yet have. If it is forwarding then we
780 * add it to the replication set
781 */
782 if (itf_flags & MFIB_ITF_FLAG_FORWARD)
783 {
784 mfib_entry_forwarding_path_add(msrc, rpath);
785 }
786 /*
787 * construct a new ITF for this entry's list
788 */
789 mfib_entry_itf_add(msrc,
790 rpath[0].frp_sw_if_index,
791 mfib_itf_create(rpath[0].frp_sw_if_index,
792 itf_flags));
793 }
794 else
795 {
796 int was_forwarding = !!(mfib_itf->mfi_flags & MFIB_ITF_FLAG_FORWARD);
797 int is_forwarding = !!(itf_flags & MFIB_ITF_FLAG_FORWARD);
798
799 if (!was_forwarding && is_forwarding)
800 {
801 mfib_entry_forwarding_path_add(msrc, rpath);
802 }
803 else if (was_forwarding && !is_forwarding)
804 {
805 mfib_entry_forwarding_path_remove(msrc, rpath);
806 }
807 /*
808 * packets in flight see these updates.
809 */
810 mfib_itf->mfi_flags = itf_flags;
811 }
812
813 mfib_entry_recalculate_forwarding(mfib_entry);
814}
815
816/*
817 * mfib_entry_path_remove
818 *
819 * remove a path from the entry.
820 * return the mfib_entry's index if it is still present, INVALID otherwise.
821 */
822int
823mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
824 mfib_source_t source,
825 const fib_route_path_t *rpath)
826{
827 mfib_entry_t *mfib_entry;
828 mfib_entry_src_t *msrc;
829 mfib_itf_t *mfib_itf;
830
831 mfib_entry = mfib_entry_get(mfib_entry_index);
832 ASSERT(NULL != mfib_entry);
833 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
834
835 if (NULL == msrc)
836 {
837 /*
838 * there are no paths left for this source
839 */
840 return (mfib_entry_ok_for_delete(mfib_entry));
841 }
842
843 /*
844 * search for the interface in the current set
845 */
846 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
847 rpath[0].frp_sw_if_index);
848
849 if (NULL == mfib_itf)
850 {
851 /*
852 * removing a path that does not exist
853 */
854 return (mfib_entry_ok_for_delete(mfib_entry));
855 }
856
857 /*
858 * we have this path. If it is forwarding then we
859 * remove it to the replication set
860 */
861 if (mfib_itf->mfi_flags & MFIB_ITF_FLAG_FORWARD)
862 {
863 mfib_entry_forwarding_path_remove(msrc, rpath);
864 }
865
866 /*
867 * remove the interface/path from this entry's list
868 */
869 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
870
871 if (mfib_entry_src_ok_for_delete(msrc))
872 {
873 /*
874 * this source has no interfaces and no flags.
875 * it has nothing left to give - remove it
876 */
877 mfib_entry_src_remove(mfib_entry, source);
878 }
879
880 mfib_entry_recalculate_forwarding(mfib_entry);
881
882 return (mfib_entry_ok_for_delete(mfib_entry));
883}
884
885/**
886 * mfib_entry_delete
887 *
888 * The source is withdrawing all the paths it provided
889 */
890int
891mfib_entry_delete (fib_node_index_t mfib_entry_index,
892 mfib_source_t source)
893{
894 mfib_entry_t *mfib_entry;
895
896 mfib_entry = mfib_entry_get(mfib_entry_index);
897 mfib_entry_src_remove(mfib_entry, source);
898
899 mfib_entry_recalculate_forwarding(mfib_entry);
900
901 return (mfib_entry_ok_for_delete(mfib_entry));
902}
903
904static int
905fib_ip4_address_compare (ip4_address_t * a1,
906 ip4_address_t * a2)
907{
908 /*
909 * IP addresses are unsiged ints. the return value here needs to be signed
910 * a simple subtraction won't cut it.
911 * If the addresses are the same, the sort order is undefiend, so phoey.
912 */
913 return ((clib_net_to_host_u32(a1->data_u32) >
914 clib_net_to_host_u32(a2->data_u32) ) ?
915 1 : -1);
916}
917
918static int
919fib_ip6_address_compare (ip6_address_t * a1,
920 ip6_address_t * a2)
921{
922 int i;
923 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
924 {
925 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
926 clib_net_to_host_u16 (a2->as_u16[i]));
927 if (cmp != 0)
928 return cmp;
929 }
930 return 0;
931}
932
933static int
934mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
935 fib_node_index_t mfib_entry_index2)
936{
937 mfib_entry_t *mfib_entry1, *mfib_entry2;
938 int cmp = 0;
939
940 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
941 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
942
943 switch (mfib_entry1->mfe_prefix.fp_proto)
944 {
945 case FIB_PROTOCOL_IP4:
946 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
947 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
948
949 if (0 == cmp)
950 {
951 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
952 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
953 }
954 break;
955 case FIB_PROTOCOL_IP6:
956 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
957 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
958
959 if (0 == cmp)
960 {
961 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
962 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
963 }
964 break;
965 case FIB_PROTOCOL_MPLS:
966 ASSERT(0);
967 cmp = 0;
968 break;
969 }
970
971 if (0 == cmp) {
972 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
973 }
974 return (cmp);
975}
976
977int
978mfib_entry_cmp_for_sort (void *i1, void *i2)
979{
980 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
981
982 return (mfib_entry_cmp(*mfib_entry_index1,
983 *mfib_entry_index2));
984}
985
986void
987mfib_entry_lock (fib_node_index_t mfib_entry_index)
988{
989 mfib_entry_t *mfib_entry;
990
991 mfib_entry = mfib_entry_get(mfib_entry_index);
992
993 fib_node_lock(&mfib_entry->mfe_node);
994}
995
996void
997mfib_entry_unlock (fib_node_index_t mfib_entry_index)
998{
999 mfib_entry_t *mfib_entry;
1000
1001 mfib_entry = mfib_entry_get(mfib_entry_index);
1002
1003 fib_node_unlock(&mfib_entry->mfe_node);
1004}
1005
1006static void
1007mfib_entry_dpo_lock (dpo_id_t *dpo)
1008{
1009}
1010static void
1011mfib_entry_dpo_unlock (dpo_id_t *dpo)
1012{
1013}
1014
1015const static dpo_vft_t mfib_entry_dpo_vft = {
1016 .dv_lock = mfib_entry_dpo_lock,
1017 .dv_unlock = mfib_entry_dpo_unlock,
1018 .dv_format = format_mfib_entry_dpo,
1019 .dv_mem_show = mfib_entry_show_memory,
1020};
1021
1022const static char* const mfib_entry_ip4_nodes[] =
1023{
1024 "ip4-mfib-forward-rpf",
1025 NULL,
1026};
1027const static char* const mfib_entry_ip6_nodes[] =
1028{
1029 "ip6-mfib-forward-rpf",
1030 NULL,
1031};
1032
1033const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1034{
1035 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1036 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1037};
1038
1039void
1040mfib_entry_module_init (void)
1041{
1042 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1043 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1044}
1045
1046void
1047mfib_entry_encode (fib_node_index_t mfib_entry_index,
1048 fib_route_path_encode_t **api_rpaths)
1049{
1050 mfib_entry_t *mfib_entry;
1051
1052 mfib_entry = mfib_entry_get(mfib_entry_index);
1053 fib_path_list_walk(mfib_entry->mfe_parent, fib_path_encode, api_rpaths);
1054}
1055
1056void
1057mfib_entry_get_prefix (fib_node_index_t mfib_entry_index,
1058 mfib_prefix_t *pfx)
1059{
1060 mfib_entry_t *mfib_entry;
1061
1062 mfib_entry = mfib_entry_get(mfib_entry_index);
1063 *pfx = mfib_entry->mfe_prefix;
1064}
1065
1066u32
1067mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1068{
1069 mfib_entry_t *mfib_entry;
1070
1071 mfib_entry = mfib_entry_get(mfib_entry_index);
1072
1073 return (mfib_entry->mfe_fib_index);
1074}
1075
1076void
1077mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1078 fib_forward_chain_type_t type,
1079 dpo_id_t *dpo)
1080{
1081 /*
1082 * An IP mFIB entry can only provide a forwarding chain that
1083 * is the same IP proto as the prefix.
1084 * No use-cases (i know of) for other combinations.
1085 */
1086 mfib_entry_t *mfib_entry;
1087 dpo_proto_t dp;
1088
1089 mfib_entry = mfib_entry_get(mfib_entry_index);
1090
1091 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1092
1093 if (type == fib_forw_chain_type_from_dpo_proto(dp))
1094 {
1095 dpo_copy(dpo, &mfib_entry->mfe_rep);
1096 }
1097 else
1098 {
1099 dpo_copy(dpo, drop_dpo_get(dp));
1100 }
1101}
1102
1103u32
1104mfib_entry_pool_size (void)
1105{
1106 return (pool_elts(mfib_entry_pool));
1107}
1108
1109static clib_error_t *
1110show_mfib_entry_command (vlib_main_t * vm,
1111 unformat_input_t * input,
1112 vlib_cli_command_t * cmd)
1113{
1114 fib_node_index_t fei;
1115
1116 if (unformat (input, "%d", &fei))
1117 {
1118 /*
1119 * show one in detail
1120 */
1121 if (!pool_is_free_index(mfib_entry_pool, fei))
1122 {
1123 vlib_cli_output (vm, "%d@%U",
1124 fei,
1125 format_mfib_entry, fei,
1126 MFIB_ENTRY_FORMAT_DETAIL2);
1127 }
1128 else
1129 {
1130 vlib_cli_output (vm, "entry %d invalid", fei);
1131 }
1132 }
1133 else
1134 {
1135 /*
1136 * show all
1137 */
1138 vlib_cli_output (vm, "FIB Entries:");
1139 pool_foreach_index(fei, mfib_entry_pool,
1140 ({
1141 vlib_cli_output (vm, "%d@%U",
1142 fei,
1143 format_mfib_entry, fei,
1144 MFIB_ENTRY_FORMAT_BRIEF);
1145 }));
1146 }
1147
1148 return (NULL);
1149}
1150
Neale Rannsc756c1c2017-02-08 09:11:57 -08001151/*?
1152 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1153 * numerical indentifier.
1154 ?*/
Neale Ranns32e1c012016-11-22 17:07:28 +00001155VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1156 .path = "show mfib entry",
1157 .function = show_mfib_entry_command,
1158 .short_help = "show mfib entry",
1159};