IP Multicast FIB (mfib)
[vpp.git] / src / vnet / mfib / mfib_entry.c
1 /*
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  */
54 typedef 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  */
80 static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
81
82 /*
83  * Pool for all fib_entries
84  */
85 mfib_entry_t *mfib_entry_pool;
86
87 static fib_node_t *
88 mfib_entry_get_node (fib_node_index_t index)
89 {
90     return ((fib_node_t*)mfib_entry_get(index));
91 }
92
93 static fib_protocol_t
94 mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
95 {
96     return (mfib_entry->mfe_prefix.fp_proto);
97 }
98
99 fib_forward_chain_type_t
100 mfib_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
115 static u8 *
116 format_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
126 u8 *
127 format_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
186 static mfib_entry_t*
187 mfib_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
195 static int
196 mfib_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
204 static void
205 mfib_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
220 static mfib_entry_src_t *
221 mfib_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
249 static mfib_entry_src_t *
250 mfib_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
265 static mfib_entry_src_t*
266 mfib_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
285 static void
286 mfib_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     }));
295 }
296
297 static void
298 mfib_entry_src_remove (mfib_entry_t *mfib_entry,
299                        mfib_source_t source)
300
301 {
302     mfib_entry_src_t *msrc;
303     u32 index = ~0;
304
305     msrc = mfib_entry_src_find(mfib_entry, source, &index);
306
307     if (NULL != msrc)
308     {
309         mfib_entry_src_flush(msrc);
310         vec_del1(mfib_entry->mfe_srcs, index);
311     }
312 }
313
314 static int
315 mfib_entry_src_n_itfs (const mfib_entry_src_t *msrc)
316 {
317     return (hash_elts(msrc->mfes_itfs));
318 }
319
320
321 static void
322 mfib_entry_last_lock_gone (fib_node_t *node)
323 {
324     mfib_entry_t *mfib_entry;
325     mfib_entry_src_t *msrc;
326
327     mfib_entry = mfib_entry_from_fib_node(node);
328
329     dpo_reset(&mfib_entry->mfe_rep);
330
331     MFIB_ENTRY_DBG(mfib_entry, "last-lock");
332
333     vec_foreach(msrc, mfib_entry->mfe_srcs)
334     {
335         mfib_entry_src_flush(msrc);
336     }
337
338     fib_path_list_unlock(mfib_entry->mfe_parent);
339     vec_free(mfib_entry->mfe_srcs);
340
341     fib_node_deinit(&mfib_entry->mfe_node);
342     pool_put(mfib_entry_pool, mfib_entry);
343 }
344
345 /*
346  * mfib_entry_back_walk_notify
347  *
348  * A back walk has reach this entry.
349  */
350 static fib_node_back_walk_rc_t
351 mfib_entry_back_walk_notify (fib_node_t *node,
352                             fib_node_back_walk_ctx_t *ctx)
353 {
354     // FIXME - re-evalute
355
356     return (FIB_NODE_BACK_WALK_CONTINUE);
357 }
358
359 static void
360 mfib_entry_show_memory (void)
361 {
362     fib_show_memory_usage("multicast-Entry",
363                           pool_elts(mfib_entry_pool),
364                           pool_len(mfib_entry_pool),
365                           sizeof(mfib_entry_t));
366 }
367
368 /*
369  * The MFIB entry's graph node virtual function table
370  */
371 static const fib_node_vft_t mfib_entry_vft = {
372     .fnv_get = mfib_entry_get_node,
373     .fnv_last_lock = mfib_entry_last_lock_gone,
374     .fnv_back_walk = mfib_entry_back_walk_notify,
375     .fnv_mem_show = mfib_entry_show_memory,
376 };
377
378 u32
379 mfib_entry_child_add (fib_node_index_t mfib_entry_index,
380                       fib_node_type_t child_type,
381                       fib_node_index_t child_index)
382 {
383     return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
384                                mfib_entry_index,
385                                child_type,
386                                child_index));
387 };
388
389 void
390 mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
391                          u32 sibling_index)
392 {
393     fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
394                           mfib_entry_index,
395                           sibling_index);
396 }
397
398 static mfib_entry_t *
399 mfib_entry_alloc (u32 fib_index,
400                   const mfib_prefix_t *prefix,
401                   fib_node_index_t *mfib_entry_index)
402 {
403     mfib_entry_t *mfib_entry;
404
405     pool_get(mfib_entry_pool, mfib_entry);
406     memset(mfib_entry, 0, sizeof(*mfib_entry));
407
408     fib_node_init(&mfib_entry->mfe_node,
409                   FIB_NODE_TYPE_MFIB_ENTRY);
410
411     mfib_entry->mfe_fib_index = fib_index;
412     mfib_entry->mfe_prefix = *prefix;
413     mfib_entry->mfe_parent = FIB_NODE_INDEX_INVALID;
414
415     dpo_reset(&mfib_entry->mfe_rep);
416
417     *mfib_entry_index = mfib_entry_get_index(mfib_entry);
418
419     MFIB_ENTRY_DBG(mfib_entry, "alloc");
420
421     return (mfib_entry);
422 }
423
424 typedef struct mfib_entry_collect_forwarding_ctx_t_
425 {
426     load_balance_path_t * next_hops;
427     fib_forward_chain_type_t fct;
428 } mfib_entry_collect_forwarding_ctx_t;
429
430 static int
431 mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
432                                    fib_node_index_t path_index,
433                                    void *arg)
434 {
435     mfib_entry_collect_forwarding_ctx_t *ctx;
436     load_balance_path_t *nh;
437
438     ctx = arg;
439
440     /*
441      * if the path is not resolved, don't include it.
442      */
443     if (!fib_path_is_resolved(path_index))
444     {
445         return (!0);
446     }
447
448     switch (ctx->fct)
449     {
450     case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
451     case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
452         /*
453          * EOS traffic with no label to stack, we need the IP Adj
454          */
455         vec_add2(ctx->next_hops, nh, 1);
456
457         nh->path_index = path_index;
458         nh->path_weight = fib_path_get_weight(path_index);
459         fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
460         break;
461
462     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
463     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
464     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
465     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
466     case FIB_FORW_CHAIN_TYPE_ETHERNET:
467         ASSERT(0);
468         break;
469     }
470
471     return (!0);
472 }
473
474 static void
475 mfib_entry_stack (mfib_entry_t *mfib_entry)
476 {
477     mfib_entry_collect_forwarding_ctx_t ctx = {
478         .next_hops = NULL,
479         .fct = mfib_entry_get_default_chain_type(mfib_entry),
480     };
481     dpo_proto_t dp;
482
483     dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
484
485     if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_parent)
486     {
487         fib_path_list_walk(mfib_entry->mfe_parent,
488                            mfib_entry_src_collect_forwarding,
489                            &ctx);
490
491         if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
492             dpo_is_drop(&mfib_entry->mfe_rep))
493         {
494             dpo_id_t tmp_dpo = DPO_INVALID;
495
496             dpo_set(&tmp_dpo,
497                     DPO_REPLICATE, dp,
498                     replicate_create(0, dp));
499
500             dpo_stack(DPO_MFIB_ENTRY, dp,
501                       &mfib_entry->mfe_rep,
502                       &tmp_dpo);
503
504             dpo_reset(&tmp_dpo);
505         }
506         replicate_multipath_update(&mfib_entry->mfe_rep,
507                                    ctx.next_hops);
508     }
509     else
510     {
511         dpo_stack(DPO_MFIB_ENTRY, dp,
512                   &mfib_entry->mfe_rep,
513                   drop_dpo_get(dp));
514     }
515 }
516
517 static void
518 mfib_entry_forwarding_path_add (mfib_entry_src_t *msrc,
519                                 const fib_route_path_t *rpath)
520 {
521     fib_node_index_t old_pl_index;
522     fib_route_path_t *rpaths;
523
524     /*
525      * path-lists require a vector of paths
526      */
527     rpaths = NULL;
528     vec_add1(rpaths, rpath[0]);
529
530     old_pl_index = msrc->mfes_pl;
531
532     if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
533     {
534         msrc->mfes_pl =
535             fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
536                                  rpaths);
537     }
538     else
539     {
540         msrc->mfes_pl =
541             fib_path_list_copy_and_path_add(msrc->mfes_pl,
542                                             FIB_PATH_LIST_FLAG_NO_URPF,
543                                             rpaths);
544     }
545     fib_path_list_lock(msrc->mfes_pl);
546     fib_path_list_unlock(old_pl_index);
547
548     vec_free(rpaths);
549 }
550
551 static int
552 mfib_entry_forwarding_path_remove (mfib_entry_src_t *msrc,
553                                    const fib_route_path_t *rpath)
554 {
555     fib_node_index_t old_pl_index;
556     fib_route_path_t *rpaths;
557
558     /*
559      * path-lists require a vector of paths
560      */
561     rpaths = NULL;
562     vec_add1(rpaths, rpath[0]);
563
564     old_pl_index = msrc->mfes_pl;
565
566     msrc->mfes_pl =
567         fib_path_list_copy_and_path_remove(msrc->mfes_pl,
568                                            FIB_PATH_LIST_FLAG_NONE,
569                                            rpaths);
570
571     fib_path_list_lock(msrc->mfes_pl);
572     fib_path_list_unlock(old_pl_index);
573
574     vec_free(rpaths);
575
576     return (FIB_NODE_INDEX_INVALID != msrc->mfes_pl);
577 }
578
579 static void
580 mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry)
581 {
582     fib_node_index_t old_pl_index;
583     mfib_entry_src_t *bsrc;
584
585     old_pl_index = mfib_entry->mfe_parent;
586
587     /*
588      * copy the forwarding data from the bast source
589      */
590     bsrc = mfib_entry_get_best_src(mfib_entry);
591
592     if (NULL == bsrc)
593     {
594         mfib_entry->mfe_parent = FIB_NODE_INDEX_INVALID;
595     }
596     else
597     {
598         mfib_entry->mfe_parent = bsrc->mfes_pl;
599         mfib_entry->mfe_flags = bsrc->mfes_flags;
600         mfib_entry->mfe_itfs = bsrc->mfes_itfs;
601     }
602
603     /*
604      * re-stack the entry on the best forwarding info.
605      */
606     if (old_pl_index != mfib_entry->mfe_parent ||
607         FIB_NODE_INDEX_INVALID == old_pl_index)
608     {
609         mfib_entry_stack(mfib_entry);
610
611         fib_path_list_lock(mfib_entry->mfe_parent);
612         fib_path_list_unlock(old_pl_index);
613     }
614 }
615
616
617 fib_node_index_t
618 mfib_entry_create (u32 fib_index,
619                    mfib_source_t source,
620                    const mfib_prefix_t *prefix,
621                    mfib_entry_flags_t entry_flags)
622 {
623     fib_node_index_t mfib_entry_index;
624     mfib_entry_t *mfib_entry;
625     mfib_entry_src_t *msrc;
626
627     mfib_entry = mfib_entry_alloc(fib_index, prefix,
628                                   &mfib_entry_index);
629     msrc = mfib_entry_src_find_or_create(mfib_entry, source);
630     msrc->mfes_flags = entry_flags;
631
632     mfib_entry_recalculate_forwarding(mfib_entry);
633
634     return (mfib_entry_index);
635 }
636
637 static int
638 mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
639 {
640     return (0 == vec_len(mfib_entry->mfe_srcs));
641 }
642
643 static int
644 mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
645 {
646     return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
647              0 == mfib_entry_src_n_itfs(msrc)));
648 }
649
650 int
651 mfib_entry_update (fib_node_index_t mfib_entry_index,
652                    mfib_source_t source,
653                    mfib_entry_flags_t entry_flags)
654 {
655     mfib_entry_t *mfib_entry;
656     mfib_entry_src_t *msrc;
657
658     mfib_entry = mfib_entry_get(mfib_entry_index);
659     msrc = mfib_entry_src_find_or_create(mfib_entry, source);
660     msrc->mfes_flags = entry_flags;
661
662     if (mfib_entry_src_ok_for_delete(msrc))
663     {
664         /*
665          * this source has no interfaces and no flags.
666          * it has nothing left to give - remove it
667          */
668         mfib_entry_src_remove(mfib_entry, source);
669     }
670
671     mfib_entry_recalculate_forwarding(mfib_entry);
672
673     return (mfib_entry_ok_for_delete(mfib_entry));
674 }
675
676 static void
677 mfib_entry_itf_add (mfib_entry_src_t *msrc,
678                     u32 sw_if_index,
679                     index_t mi)
680 {
681     hash_set(msrc->mfes_itfs, sw_if_index, mi);
682 }
683
684 static void
685 mfib_entry_itf_remove (mfib_entry_src_t *msrc,
686                        u32 sw_if_index)
687 {
688     mfib_itf_t *mfi;
689
690     mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
691
692     mfib_itf_delete(mfi);
693
694     hash_unset(msrc->mfes_itfs, sw_if_index);
695 }
696
697 void
698 mfib_entry_path_update (fib_node_index_t mfib_entry_index,
699                         mfib_source_t source,
700                         const fib_route_path_t *rpath,
701                         mfib_itf_flags_t itf_flags)
702 {
703     mfib_entry_t *mfib_entry;
704     mfib_entry_src_t *msrc;
705     mfib_itf_t *mfib_itf;
706
707     mfib_entry = mfib_entry_get(mfib_entry_index);
708     ASSERT(NULL != mfib_entry);
709     msrc = mfib_entry_src_find_or_create(mfib_entry, source);
710
711     /*
712      * search for the interface in the current set
713      */
714     mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
715                                    rpath[0].frp_sw_if_index);
716
717     if (NULL == mfib_itf)
718     {
719         /*
720          * this is a path we do not yet have. If it is forwarding then we
721          * add it to the replication set
722          */
723         if (itf_flags & MFIB_ITF_FLAG_FORWARD)
724         {
725             mfib_entry_forwarding_path_add(msrc, rpath);
726         }
727         /*
728          * construct a new ITF for this entry's list
729          */
730         mfib_entry_itf_add(msrc,
731                            rpath[0].frp_sw_if_index,
732                            mfib_itf_create(rpath[0].frp_sw_if_index,
733                                            itf_flags));
734     }
735     else
736     {
737         int was_forwarding = !!(mfib_itf->mfi_flags & MFIB_ITF_FLAG_FORWARD);
738         int is_forwarding  = !!(itf_flags & MFIB_ITF_FLAG_FORWARD);
739
740         if (!was_forwarding && is_forwarding)
741         {
742             mfib_entry_forwarding_path_add(msrc, rpath);
743         }
744         else if (was_forwarding && !is_forwarding)
745         {
746             mfib_entry_forwarding_path_remove(msrc, rpath);
747         }
748         /*
749          * packets in flight see these updates.
750          */
751         mfib_itf->mfi_flags = itf_flags;
752     }
753
754     mfib_entry_recalculate_forwarding(mfib_entry);
755 }
756
757 /*
758  * mfib_entry_path_remove
759  *
760  * remove a path from the entry.
761  * return the mfib_entry's index if it is still present, INVALID otherwise.
762  */
763 int
764 mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
765                         mfib_source_t source,
766                         const fib_route_path_t *rpath)
767 {
768     mfib_entry_t *mfib_entry;
769     mfib_entry_src_t *msrc;
770     mfib_itf_t *mfib_itf;
771
772     mfib_entry = mfib_entry_get(mfib_entry_index);
773     ASSERT(NULL != mfib_entry);
774     msrc = mfib_entry_src_find(mfib_entry, source, NULL);
775
776     if (NULL == msrc)
777     {
778         /*
779          * there are no paths left for this source
780          */
781         return (mfib_entry_ok_for_delete(mfib_entry));
782     }
783
784     /*
785      * search for the interface in the current set
786      */
787     mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
788                                    rpath[0].frp_sw_if_index);
789
790     if (NULL == mfib_itf)
791     {
792         /*
793          * removing a path that does not exist
794          */
795         return (mfib_entry_ok_for_delete(mfib_entry));
796     }
797
798     /*
799      * we have this path. If it is forwarding then we
800      * remove it to the replication set
801      */
802     if (mfib_itf->mfi_flags & MFIB_ITF_FLAG_FORWARD)
803     {
804         mfib_entry_forwarding_path_remove(msrc, rpath);
805     }
806
807     /*
808      * remove the interface/path from this entry's list
809      */
810     mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
811
812     if (mfib_entry_src_ok_for_delete(msrc))
813     {
814         /*
815          * this source has no interfaces and no flags.
816          * it has nothing left to give - remove it
817          */
818         mfib_entry_src_remove(mfib_entry, source);
819     }
820
821     mfib_entry_recalculate_forwarding(mfib_entry);
822
823     return (mfib_entry_ok_for_delete(mfib_entry));
824 }
825
826 /**
827  * mfib_entry_delete
828  *
829  * The source is withdrawing all the paths it provided
830  */
831 int
832 mfib_entry_delete (fib_node_index_t mfib_entry_index,
833                    mfib_source_t source)
834 {
835     mfib_entry_t *mfib_entry;
836
837     mfib_entry = mfib_entry_get(mfib_entry_index);
838     mfib_entry_src_remove(mfib_entry, source);
839
840     mfib_entry_recalculate_forwarding(mfib_entry);
841
842     return (mfib_entry_ok_for_delete(mfib_entry));
843 }
844
845 static int
846 fib_ip4_address_compare (ip4_address_t * a1,
847                          ip4_address_t * a2)
848 {
849     /*
850      * IP addresses are unsiged ints. the return value here needs to be signed
851      * a simple subtraction won't cut it.
852      * If the addresses are the same, the sort order is undefiend, so phoey.
853      */
854     return ((clib_net_to_host_u32(a1->data_u32) >
855              clib_net_to_host_u32(a2->data_u32) ) ?
856             1 : -1);
857 }
858
859 static int
860 fib_ip6_address_compare (ip6_address_t * a1,
861                          ip6_address_t * a2)
862 {
863   int i;
864   for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
865   {
866       int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
867                  clib_net_to_host_u16 (a2->as_u16[i]));
868       if (cmp != 0)
869           return cmp;
870   }
871   return 0;
872 }
873
874 static int
875 mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
876                 fib_node_index_t mfib_entry_index2)
877 {
878     mfib_entry_t *mfib_entry1, *mfib_entry2;
879     int cmp = 0;
880
881     mfib_entry1 = mfib_entry_get(mfib_entry_index1);
882     mfib_entry2 = mfib_entry_get(mfib_entry_index2);
883
884     switch (mfib_entry1->mfe_prefix.fp_proto)
885     {
886     case FIB_PROTOCOL_IP4:
887         cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
888                                       &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
889
890         if (0 == cmp)
891         {
892             cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
893                                           &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
894         }
895         break;
896     case FIB_PROTOCOL_IP6:
897         cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
898                                       &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
899
900         if (0 == cmp)
901         {
902             cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
903                                           &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
904         }
905         break;
906     case FIB_PROTOCOL_MPLS:
907         ASSERT(0);
908         cmp = 0;
909         break;
910     }
911
912     if (0 == cmp) {
913         cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
914     }
915     return (cmp);
916 }
917
918 int
919 mfib_entry_cmp_for_sort (void *i1, void *i2)
920 {
921     fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
922
923     return (mfib_entry_cmp(*mfib_entry_index1,
924                            *mfib_entry_index2));
925 }
926
927 void
928 mfib_entry_lock (fib_node_index_t mfib_entry_index)
929 {
930     mfib_entry_t *mfib_entry;
931
932     mfib_entry = mfib_entry_get(mfib_entry_index);
933
934     fib_node_lock(&mfib_entry->mfe_node);
935 }
936
937 void
938 mfib_entry_unlock (fib_node_index_t mfib_entry_index)
939 {
940     mfib_entry_t *mfib_entry;
941
942     mfib_entry = mfib_entry_get(mfib_entry_index);
943
944     fib_node_unlock(&mfib_entry->mfe_node);
945 }
946
947 static void
948 mfib_entry_dpo_lock (dpo_id_t *dpo)
949 {
950 }
951 static void
952 mfib_entry_dpo_unlock (dpo_id_t *dpo)
953 {
954 }
955
956 const static dpo_vft_t mfib_entry_dpo_vft = {
957     .dv_lock = mfib_entry_dpo_lock,
958     .dv_unlock = mfib_entry_dpo_unlock,
959     .dv_format = format_mfib_entry_dpo,
960     .dv_mem_show = mfib_entry_show_memory,
961 };
962
963 const static char* const mfib_entry_ip4_nodes[] =
964 {
965     "ip4-mfib-forward-rpf",
966     NULL,
967 };
968 const static char* const mfib_entry_ip6_nodes[] =
969 {
970     "ip6-mfib-forward-rpf",
971     NULL,
972 };
973
974 const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
975 {
976     [DPO_PROTO_IP4]  = mfib_entry_ip4_nodes,
977     [DPO_PROTO_IP6]  = mfib_entry_ip6_nodes,
978 };
979
980 void
981 mfib_entry_module_init (void)
982 {
983     fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
984     dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
985 }
986
987 void
988 mfib_entry_encode (fib_node_index_t mfib_entry_index,
989                   fib_route_path_encode_t **api_rpaths)
990 {
991     mfib_entry_t *mfib_entry;
992
993     mfib_entry = mfib_entry_get(mfib_entry_index);
994     fib_path_list_walk(mfib_entry->mfe_parent, fib_path_encode, api_rpaths);
995 }
996
997 void
998 mfib_entry_get_prefix (fib_node_index_t mfib_entry_index,
999                       mfib_prefix_t *pfx)
1000 {
1001     mfib_entry_t *mfib_entry;
1002
1003     mfib_entry = mfib_entry_get(mfib_entry_index);
1004     *pfx = mfib_entry->mfe_prefix;
1005 }
1006
1007 u32
1008 mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1009 {
1010     mfib_entry_t *mfib_entry;
1011
1012     mfib_entry = mfib_entry_get(mfib_entry_index);
1013
1014     return (mfib_entry->mfe_fib_index);
1015 }
1016
1017 void
1018 mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1019                                   fib_forward_chain_type_t type,
1020                                   dpo_id_t *dpo)
1021 {
1022     /*
1023      * An IP mFIB entry can only provide a forwarding chain that
1024      * is the same IP proto as the prefix.
1025      * No use-cases (i know of) for other combinations.
1026      */
1027     mfib_entry_t *mfib_entry;
1028     dpo_proto_t dp;
1029
1030     mfib_entry = mfib_entry_get(mfib_entry_index);
1031
1032     dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1033
1034     if (type == fib_forw_chain_type_from_dpo_proto(dp))
1035     {
1036         dpo_copy(dpo, &mfib_entry->mfe_rep);
1037     }
1038     else
1039     {
1040         dpo_copy(dpo, drop_dpo_get(dp));
1041     }
1042 }
1043
1044 u32
1045 mfib_entry_pool_size (void)
1046 {
1047     return (pool_elts(mfib_entry_pool));
1048 }
1049
1050 static clib_error_t *
1051 show_mfib_entry_command (vlib_main_t * vm,
1052                         unformat_input_t * input,
1053                         vlib_cli_command_t * cmd)
1054 {
1055     fib_node_index_t fei;
1056
1057     if (unformat (input, "%d", &fei))
1058     {
1059         /*
1060          * show one in detail
1061          */
1062         if (!pool_is_free_index(mfib_entry_pool, fei))
1063         {
1064             vlib_cli_output (vm, "%d@%U",
1065                              fei,
1066                              format_mfib_entry, fei,
1067                              MFIB_ENTRY_FORMAT_DETAIL2);
1068         }
1069         else
1070         {
1071             vlib_cli_output (vm, "entry %d invalid", fei);
1072         }
1073     }
1074     else
1075     {
1076         /*
1077          * show all
1078          */
1079         vlib_cli_output (vm, "FIB Entries:");
1080         pool_foreach_index(fei, mfib_entry_pool,
1081         ({
1082             vlib_cli_output (vm, "%d@%U",
1083                              fei,
1084                              format_mfib_entry, fei,
1085                              MFIB_ENTRY_FORMAT_BRIEF);
1086         }));
1087     }
1088
1089     return (NULL);
1090 }
1091
1092 VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1093   .path = "show mfib entry",
1094   .function = show_mfib_entry_command,
1095   .short_help = "show mfib entry",
1096 };