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