8050882d436aa583ad5d569cba9bc3391aa47326
[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/mfib/mfib_entry_src.h>
20 #include <vnet/mfib/mfib_entry_cover.h>
21 #include <vnet/fib/fib_path_list.h>
22 #include <vnet/fib/fib_walk.h>
23
24 #include <vnet/dpo/drop_dpo.h>
25 #include <vnet/dpo/replicate_dpo.h>
26
27 /**
28  * the logger
29  */
30 vlib_log_class_t mfib_entry_logger;
31
32 /**
33  * Pool of path extensions
34  */
35 static mfib_path_ext_t *mfib_path_ext_pool;
36
37 /**
38  * String names for each source
39  */
40 static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
41 static const char *mfib_src_attribute_names[] = MFIB_ENTRY_SRC_ATTRIBUTES;
42
43 /*
44  * Pool for all fib_entries
45  */
46 mfib_entry_t *mfib_entry_pool;
47
48 static fib_node_t *
49 mfib_entry_get_node (fib_node_index_t index)
50 {
51     return ((fib_node_t*)mfib_entry_get(index));
52 }
53
54 static fib_protocol_t
55 mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
56 {
57     return (mfib_entry->mfe_prefix.fp_proto);
58 }
59
60 fib_forward_chain_type_t
61 mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
62 {
63     switch (mfib_entry->mfe_prefix.fp_proto)
64     {
65     case FIB_PROTOCOL_IP4:
66         return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
67     case FIB_PROTOCOL_IP6:
68         return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
69     case FIB_PROTOCOL_MPLS:
70         ASSERT(0);
71         break;
72     }
73     return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
74 }
75
76 static u8 *
77 format_mfib_entry_dpo (u8 * s, va_list * args)
78 {
79     index_t fei = va_arg(*args, index_t);
80     CLIB_UNUSED(u32 indent) = va_arg(*args, u32);
81
82     return (format(s, "%U",
83                    format_mfib_entry, fei,
84                    MFIB_ENTRY_FORMAT_BRIEF));
85 }
86
87 static inline mfib_path_ext_t *
88 mfib_entry_path_ext_get (index_t mi)
89 {
90     return (pool_elt_at_index(mfib_path_ext_pool, mi));
91 }
92
93 static u8 *
94 format_mfib_entry_path_ext (u8 * s, va_list * args)
95 {
96     mfib_path_ext_t *path_ext;
97     index_t mpi = va_arg(*args, index_t);
98
99     path_ext = mfib_entry_path_ext_get(mpi);
100     return (format(s, "path:%d flags:%U",
101                    path_ext->mfpe_path,
102                    format_mfib_itf_flags, path_ext->mfpe_flags));
103 }
104
105 u8 *
106 format_mfib_entry_src_flags (u8 *s, va_list *args)
107 {
108     mfib_entry_src_attribute_t sattr;
109     mfib_entry_src_flags_t flag = va_arg(*args, int);
110
111     if (!flag)
112     {
113         return format(s, "none");
114     }
115
116     FOR_EACH_MFIB_SRC_ATTRIBUTE(sattr) {
117         if ((1 << sattr) & flag) {
118             s = format (s, "%s,", mfib_src_attribute_names[sattr]);
119         }
120     }
121
122     return (s);
123 }
124
125 u8 *
126 format_mfib_entry (u8 * s, va_list * args)
127 {
128     fib_node_index_t fei, mfi;
129     mfib_entry_t *mfib_entry;
130     mfib_entry_src_t *msrc;
131     u32 sw_if_index;
132     int level;
133
134     fei = va_arg (*args, fib_node_index_t);
135     level = va_arg (*args, int);
136     mfib_entry = mfib_entry_get(fei);
137
138     s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
139     s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);
140
141     if (level >= MFIB_ENTRY_FORMAT_DETAIL)
142     {
143         fib_node_index_t path_index, mpi;
144
145         s = format (s, "\n");
146         s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
147         s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
148         s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
149         vec_foreach(msrc, mfib_entry->mfe_srcs)
150         {
151             s = format (s, "  src:%s flags:%U locks:%d:",
152                         mfib_source_names[msrc->mfes_src],
153                         format_mfib_entry_src_flags, msrc->mfes_flags,
154                         msrc->mfes_ref_count);
155             if (msrc->mfes_cover != FIB_NODE_INDEX_INVALID)
156             {
157                 s = format (s, " cover:%d", msrc->mfes_cover);
158             }
159             s = format (s, " %U\n", format_mfib_entry_flags, msrc->mfes_route_flags);
160             if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
161             {
162                 s = fib_path_list_format(msrc->mfes_pl, s);
163             }
164             s = format (s, "    Extensions:\n");
165             hash_foreach(path_index, mpi, msrc->mfes_exts,
166             ({
167                 s = format(s, "     %U\n", format_mfib_entry_path_ext, mpi);
168             }));
169             s = format (s, "    Interface-Forwarding:\n");
170             hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
171             ({
172                 s = format(s, "    %U\n", format_mfib_itf, mfi);
173             }));
174         }
175     }
176
177     s = format(s, "\n  Interfaces:");
178     hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
179     ({
180         s = format(s, "\n  %U", format_mfib_itf, mfi);
181     }));
182     if (MFIB_RPF_ID_NONE != mfib_entry->mfe_rpf_id)
183     {
184         s = format(s, "\n  RPF-ID:%d", mfib_entry->mfe_rpf_id);
185     }
186     s = format(s, "\n  %U-chain\n  %U",
187                format_fib_forw_chain_type,
188                mfib_entry_get_default_chain_type(mfib_entry),
189                format_dpo_id,
190                &mfib_entry->mfe_rep,
191                2);
192     s = format(s, "\n");
193
194     if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
195     {
196         s = format(s, "\nchildren:");
197         s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
198     }
199
200     return (s);
201 }
202
203 static mfib_entry_t*
204 mfib_entry_from_fib_node (fib_node_t *node)
205 {
206     ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
207     return ((mfib_entry_t*)node);
208 }
209
210 static int
211 mfib_entry_src_cmp_for_sort (void * v1,
212                              void * v2)
213 {
214     mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
215
216     return (esrc1->mfes_src - esrc2->mfes_src);
217 }
218
219 static void
220 mfib_entry_src_init (mfib_entry_t *mfib_entry,
221                      mfib_source_t source)
222
223 {
224     mfib_entry_src_t esrc = {
225         .mfes_pl = FIB_NODE_INDEX_INVALID,
226         .mfes_route_flags = MFIB_ENTRY_FLAG_NONE,
227         .mfes_src = source,
228         .mfes_cover = FIB_NODE_INDEX_INVALID,
229         .mfes_sibling = FIB_NODE_INDEX_INVALID,
230         .mfes_ref_count = 1,
231     };
232
233     vec_add1(mfib_entry->mfe_srcs, esrc);
234     vec_sort_with_function(mfib_entry->mfe_srcs,
235                            mfib_entry_src_cmp_for_sort);
236 }
237
238 static mfib_entry_src_t *
239 mfib_entry_src_find (const mfib_entry_t *mfib_entry,
240                      mfib_source_t source,
241                      u32 *index)
242
243 {
244     mfib_entry_src_t *esrc;
245     int ii;
246
247     ii = 0;
248     vec_foreach(esrc, mfib_entry->mfe_srcs)
249     {
250         if (esrc->mfes_src == source)
251         {
252             if (NULL != index)
253             {
254                 *index = ii;
255             }
256             return (esrc);
257         }
258         else
259         {
260             ii++;
261         }
262     }
263
264     return (NULL);
265 }
266
267 static mfib_entry_src_t *
268 mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
269                                mfib_source_t source)
270 {
271     mfib_entry_src_t *msrc;
272
273     msrc = mfib_entry_src_find(mfib_entry, source, NULL);
274
275     if (NULL == msrc)
276     {
277         mfib_entry_src_init(mfib_entry, source);
278         msrc = mfib_entry_src_find(mfib_entry, source, NULL);
279     }
280
281     return (msrc);
282 }
283
284 static mfib_entry_src_t *
285 mfib_entry_src_update (mfib_entry_t *mfib_entry,
286                        mfib_source_t source,
287                        fib_rpf_id_t rpf_id,
288                        mfib_entry_flags_t entry_flags)
289 {
290     mfib_entry_src_t *msrc;
291
292     msrc = mfib_entry_src_find_or_create(mfib_entry, source);
293
294     msrc->mfes_route_flags = entry_flags;
295     msrc->mfes_rpf_id = rpf_id;
296     msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
297
298     return (msrc);
299 }
300
301 static mfib_entry_src_t *
302 mfib_entry_src_update_and_lock (mfib_entry_t *mfib_entry,
303                                 mfib_source_t source,
304                                 fib_rpf_id_t rpf_id,
305                                 mfib_entry_flags_t entry_flags)
306 {
307     mfib_entry_src_t *msrc;
308
309     msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
310
311     msrc->mfes_ref_count++;
312     msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
313
314     return (msrc);
315 }
316
317 mfib_entry_src_t*
318 mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
319 {
320     mfib_entry_src_t *bsrc;
321
322     /*
323      * the enum of sources is deliberately arranged in priority order
324      */
325     if (0 == vec_len(mfib_entry->mfe_srcs))
326     {
327         bsrc = NULL;
328     }
329     else
330     {
331         bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
332     }
333
334     return (bsrc);
335 }
336
337 static mfib_source_t
338 mfib_entry_get_best_source (const mfib_entry_t *mfib_entry)
339 {
340     mfib_entry_src_t *bsrc;
341
342     bsrc = mfib_entry_get_best_src(mfib_entry);
343
344     return (bsrc->mfes_src);
345 }
346
347 int
348 mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
349                        mfib_source_t source)
350 {
351     mfib_entry_t *mfib_entry;
352
353     mfib_entry = mfib_entry_get(mfib_entry_index);
354
355     return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
356 }
357
358 int
359 mfib_entry_is_marked (fib_node_index_t mfib_entry_index,
360                       mfib_source_t source)
361 {
362     mfib_entry_t *mfib_entry;
363     mfib_entry_src_t *esrc;
364
365     mfib_entry = mfib_entry_get(mfib_entry_index);
366
367     esrc = mfib_entry_src_find(mfib_entry, source, NULL);
368
369     if (NULL == esrc)
370     {
371         return (0);
372     }
373     else
374     {
375         return (!!(esrc->mfes_flags & MFIB_ENTRY_SRC_FLAG_STALE));
376     }
377 }
378
379 void
380 mfib_entry_mark (fib_node_index_t fib_entry_index,
381                  mfib_source_t source)
382 {
383     mfib_entry_t *mfib_entry;
384     mfib_entry_src_t *esrc;
385
386     mfib_entry = mfib_entry_get(fib_entry_index);
387
388     esrc = mfib_entry_src_find(mfib_entry, source, NULL);
389
390     if (NULL != esrc)
391     {
392         esrc->mfes_flags |= MFIB_ENTRY_SRC_FLAG_STALE;
393     }
394 }
395
396 int
397 mfib_entry_is_host (fib_node_index_t mfib_entry_index)
398 {
399     return (mfib_prefix_is_host(mfib_entry_get_prefix(mfib_entry_index)));
400 }
401
402
403 static void
404 mfib_entry_src_flush (mfib_entry_src_t *msrc)
405 {
406     u32 sw_if_index;
407     index_t mfii;
408
409     hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
410     ({
411         mfib_itf_delete(mfib_itf_get(mfii));
412     }));
413     hash_free(msrc->mfes_itfs);
414     msrc->mfes_itfs = NULL;
415     fib_path_list_unlock(msrc->mfes_pl);
416 }
417
418 static void
419 mfib_entry_src_remove (mfib_entry_t *mfib_entry,
420                        mfib_source_t source)
421
422 {
423     mfib_entry_src_t *msrc;
424     u32 index = ~0;
425
426     msrc = mfib_entry_src_find(mfib_entry, source, &index);
427
428     if (NULL != msrc)
429     {
430         ASSERT(0 != msrc->mfes_ref_count);
431         msrc->mfes_ref_count--;
432
433         if (0 == msrc->mfes_ref_count)
434         {
435             mfib_entry_src_deactivate(mfib_entry, msrc);
436             mfib_entry_src_flush(msrc);
437
438             vec_del1(mfib_entry->mfe_srcs, index);
439             if (vec_len (mfib_entry->mfe_srcs) > 1)
440                 vec_sort_with_function(mfib_entry->mfe_srcs,
441                                        mfib_entry_src_cmp_for_sort);
442         }
443     }
444 }
445
446 u32
447 mfib_entry_child_add (fib_node_index_t mfib_entry_index,
448                       fib_node_type_t child_type,
449                       fib_node_index_t child_index)
450 {
451     return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
452                                mfib_entry_index,
453                                child_type,
454                                child_index));
455 };
456
457 void
458 mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
459                          u32 sibling_index)
460 {
461     fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
462                           mfib_entry_index,
463                           sibling_index);
464 }
465
466 static mfib_entry_t *
467 mfib_entry_alloc (u32 fib_index,
468                   const mfib_prefix_t *prefix,
469                   fib_node_index_t *mfib_entry_index)
470 {
471     mfib_entry_t *mfib_entry;
472
473     pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
474
475     fib_node_init(&mfib_entry->mfe_node,
476                   FIB_NODE_TYPE_MFIB_ENTRY);
477
478     /*
479      * Some of the members require non-default initialisation
480      * so we also init those that don't and thus save on the call to clib_memset.
481      */
482     mfib_entry->mfe_flags = 0;
483     mfib_entry->mfe_fib_index = fib_index;
484     mfib_entry->mfe_prefix = *prefix;
485     mfib_entry->mfe_srcs = NULL;
486     mfib_entry->mfe_itfs = NULL;
487     mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
488     mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
489
490     dpo_reset(&mfib_entry->mfe_rep);
491
492     *mfib_entry_index = mfib_entry_get_index(mfib_entry);
493
494     MFIB_ENTRY_DBG(mfib_entry, "alloc");
495
496     return (mfib_entry);
497 }
498
499 static inline mfib_path_ext_t *
500 mfib_entry_path_ext_find (mfib_path_ext_t *exts,
501                           fib_node_index_t path_index)
502 {
503     uword *p;
504
505     p = hash_get(exts, path_index);
506
507     if (NULL != p)
508     {
509         return (mfib_entry_path_ext_get(p[0]));
510     }
511
512     return (NULL);
513 }
514
515 static mfib_path_ext_t*
516 mfib_path_ext_add (mfib_entry_src_t *msrc,
517                    fib_node_index_t path_index,
518                    mfib_itf_flags_t mfi_flags)
519 {
520     mfib_path_ext_t *path_ext;
521
522     pool_get(mfib_path_ext_pool, path_ext);
523
524     path_ext->mfpe_flags = mfi_flags;
525     path_ext->mfpe_path = path_index;
526
527     hash_set(msrc->mfes_exts, path_index,
528              path_ext - mfib_path_ext_pool);
529
530     return (path_ext);
531 }
532
533 static void
534 mfib_path_ext_remove (mfib_entry_src_t *msrc,
535                       fib_node_index_t path_index)
536 {
537     mfib_path_ext_t *path_ext;
538
539     path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
540
541     hash_unset(msrc->mfes_exts, path_index);
542     pool_put(mfib_path_ext_pool, path_ext);
543 }
544
545 typedef struct mfib_entry_collect_forwarding_ctx_t_
546 {
547     load_balance_path_t * next_hops;
548     fib_forward_chain_type_t fct;
549     mfib_entry_src_t *msrc;
550     dpo_proto_t payload_proto;
551 } mfib_entry_collect_forwarding_ctx_t;
552
553 static fib_path_list_walk_rc_t
554 mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
555                                    fib_node_index_t path_index,
556                                    void *arg)
557 {
558     mfib_entry_collect_forwarding_ctx_t *ctx;
559     load_balance_path_t *nh;
560
561     ctx = arg;
562
563     /*
564      * if the path is not resolved, don't include it.
565      */
566     if (!fib_path_is_resolved(path_index))
567     {
568         return (FIB_PATH_LIST_WALK_CONTINUE);
569     }
570
571     /*
572      * If the path is not forwarding to use it
573      */
574     mfib_path_ext_t *path_ext;
575     
576     path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
577                                         path_index);
578
579     if (NULL != path_ext &&
580         !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
581     {
582         return (FIB_PATH_LIST_WALK_CONTINUE);
583     }
584     
585     switch (ctx->fct)
586     {
587     case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
588     case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
589         /*
590          * EOS traffic with no label to stack, we need the IP Adj
591          */
592         vec_add2(ctx->next_hops, nh, 1);
593
594         nh->path_index = path_index;
595         nh->path_weight = fib_path_get_weight(path_index);
596         fib_path_contribute_forwarding(path_index, ctx->fct,
597                                        ctx->payload_proto, &nh->path_dpo);
598         break;
599
600     case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
601     case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
602     case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
603     case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
604     case FIB_FORW_CHAIN_TYPE_ETHERNET:
605     case FIB_FORW_CHAIN_TYPE_NSH:
606     case FIB_FORW_CHAIN_TYPE_BIER:
607         ASSERT(0);
608         break;
609     }
610
611     return (FIB_PATH_LIST_WALK_CONTINUE);
612 }
613
614 static void
615 mfib_entry_stack (mfib_entry_t *mfib_entry,
616                   mfib_entry_src_t *msrc)
617 {
618     dpo_proto_t dp;
619
620     dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
621
622     /*
623      * unlink the enty from the previous path list.
624      */
625     if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
626     {
627         fib_path_list_child_remove(mfib_entry->mfe_pl,
628                                    mfib_entry->mfe_sibling);
629     }
630
631     if (NULL != msrc)
632     {
633         mfib_entry_collect_forwarding_ctx_t ctx = {
634             .next_hops = NULL,
635             .fct = mfib_entry_get_default_chain_type(mfib_entry),
636             .msrc = msrc,
637             .payload_proto = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto),
638         };
639
640         /*
641          * link the entry to the path-list.
642          * The entry needs to be a child so that we receive the back-walk
643          * updates to recalculate forwarding.
644          */
645         mfib_entry->mfe_pl = msrc->mfes_pl;
646         mfib_entry->mfe_flags = msrc->mfes_route_flags;
647         mfib_entry->mfe_itfs = msrc->mfes_itfs;
648         mfib_entry->mfe_rpf_id = msrc->mfes_rpf_id;
649
650         if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
651         {
652             mfib_entry->mfe_sibling =
653                 fib_path_list_child_add(mfib_entry->mfe_pl,
654                                         FIB_NODE_TYPE_MFIB_ENTRY,
655                                         mfib_entry_get_index(mfib_entry));
656
657             fib_path_list_walk(mfib_entry->mfe_pl,
658                                mfib_entry_src_collect_forwarding,
659                                &ctx);
660         }
661         if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
662         {
663             if (NULL == ctx.next_hops)
664             {
665                 /*
666                  * no next-hops, stack directly on the drop
667                  */
668                 dpo_stack(DPO_MFIB_ENTRY, dp,
669                           &mfib_entry->mfe_rep,
670                           drop_dpo_get(dp));
671             }
672             else
673             {
674                 /*
675                  * each path contirbutes a next-hop. form a replicate
676                  * from those choices.
677                  */
678                 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
679                     dpo_is_drop(&mfib_entry->mfe_rep))
680                 {
681                     dpo_id_t tmp_dpo = DPO_INVALID;
682
683                     dpo_set(&tmp_dpo,
684                             DPO_REPLICATE, dp,
685                             replicate_create(0, dp));
686
687                     dpo_stack(DPO_MFIB_ENTRY, dp,
688                               &mfib_entry->mfe_rep,
689                               &tmp_dpo);
690
691                     dpo_reset(&tmp_dpo);
692                 }
693                 replicate_multipath_update(&mfib_entry->mfe_rep,
694                                            ctx.next_hops);
695             }
696         }
697         else
698         {
699             /*
700              * for exclusive routes the source provided a replicate DPO
701              * which we stashed in the special path list with one path,
702              * so we can stack directly on that.
703              */
704             ASSERT(1 == vec_len(ctx.next_hops));
705
706             if (NULL != ctx.next_hops)
707             {
708                 dpo_stack(DPO_MFIB_ENTRY, dp,
709                           &mfib_entry->mfe_rep,
710                           &ctx.next_hops[0].path_dpo);
711                 dpo_reset(&ctx.next_hops[0].path_dpo);
712                 vec_free(ctx.next_hops);
713             }
714             else
715             {
716                 dpo_stack(DPO_MFIB_ENTRY, dp,
717                           &mfib_entry->mfe_rep,
718                           drop_dpo_get(dp));
719             }
720         }
721     }
722     else
723     {
724         dpo_stack(DPO_MFIB_ENTRY, dp,
725                   &mfib_entry->mfe_rep,
726                   drop_dpo_get(dp));
727     }
728
729     /*
730      * time for walkies fido.
731      */
732     fib_node_back_walk_ctx_t bw_ctx = {
733         .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
734     };
735
736     fib_walk_sync(FIB_NODE_TYPE_MFIB_ENTRY,
737                   mfib_entry_get_index(mfib_entry),
738                   &bw_ctx);
739 }
740
741 static fib_node_index_t*
742 mfib_entry_src_paths_add (mfib_entry_src_t *msrc,
743                           const fib_route_path_t *rpaths)
744 {
745     ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_route_flags));
746
747     msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
748
749     if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
750     {
751         /* A non-shared path-list */
752         msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
753                                              NULL);
754         fib_path_list_lock(msrc->mfes_pl);
755     }
756
757     return (fib_path_list_paths_add(msrc->mfes_pl, rpaths));
758 }
759
760 static fib_node_index_t*
761 mfib_entry_src_paths_remove (mfib_entry_src_t *msrc,
762                              const fib_route_path_t *rpaths)
763 {
764     ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_route_flags));
765
766     msrc->mfes_flags &= ~MFIB_ENTRY_SRC_FLAG_STALE;
767
768     return (fib_path_list_paths_remove(msrc->mfes_pl, rpaths));
769 }
770
771 static void
772 mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry,
773                                    mfib_source_t old_best)
774 {
775     mfib_entry_src_t *bsrc, *osrc;
776
777     /*
778      * copy the forwarding data from the bast source
779      */
780     bsrc = mfib_entry_get_best_src(mfib_entry);
781     osrc = mfib_entry_src_find(mfib_entry, old_best, NULL);
782
783     if (NULL != bsrc)
784     {
785         if (bsrc->mfes_src != old_best)
786         {
787             /*
788              * we are changing from one source to another
789              * deactivate the old, and activate the new
790              */
791             mfib_entry_src_deactivate(mfib_entry, osrc);
792             mfib_entry_src_activate(mfib_entry, bsrc);
793         }
794     }
795     else
796     {
797         mfib_entry_src_deactivate(mfib_entry, osrc);
798     }
799
800     mfib_entry_stack(mfib_entry, bsrc);
801     mfib_entry_cover_update_notify(mfib_entry);
802 }
803
804
805 fib_node_index_t
806 mfib_entry_create (u32 fib_index,
807                    mfib_source_t source,
808                    const mfib_prefix_t *prefix,
809                    fib_rpf_id_t rpf_id,
810                    mfib_entry_flags_t entry_flags,
811                    index_t repi)
812 {
813     fib_node_index_t mfib_entry_index;
814     mfib_entry_t *mfib_entry;
815     mfib_entry_src_t *msrc;
816
817     mfib_entry = mfib_entry_alloc(fib_index, prefix,
818                                   &mfib_entry_index);
819     msrc = mfib_entry_src_update(mfib_entry, source,
820                                  rpf_id, entry_flags);
821
822     if (INDEX_INVALID != repi)
823     {
824         /*
825          * The source is providing its own replicate DPO.
826          * Create a sepcial path-list to manage it, that way
827          * this entry and the source are equivalent to a normal
828          * entry
829          */
830         fib_node_index_t old_pl_index;
831         dpo_proto_t dp;
832         dpo_id_t dpo = DPO_INVALID;
833
834         dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
835         old_pl_index = msrc->mfes_pl;
836
837         dpo_set(&dpo, DPO_REPLICATE, dp, repi);
838
839         msrc->mfes_pl =
840             fib_path_list_create_special(dp,
841                                          FIB_PATH_LIST_FLAG_EXCLUSIVE,
842                                          &dpo);
843
844         dpo_reset(&dpo);
845         fib_path_list_lock(msrc->mfes_pl);
846         fib_path_list_unlock(old_pl_index);
847     }
848
849     mfib_entry_recalculate_forwarding(mfib_entry, MFIB_SOURCE_NONE);
850
851     return (mfib_entry_index);
852 }
853
854 static int
855 mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
856 {
857     return (0 == vec_len(mfib_entry->mfe_srcs));
858 }
859
860 static int
861 mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
862 {
863     return ((INDEX_INVALID == msrc->mfes_cover &&
864              MFIB_ENTRY_FLAG_NONE == msrc->mfes_route_flags &&
865              0 == fib_path_list_get_n_paths(msrc->mfes_pl)) &&
866             (0 == hash_elts(msrc->mfes_itfs)));
867
868     /* return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_route_flags) && */
869     /*         (0 == fib_path_list_get_n_paths(msrc->mfes_pl)) && */
870     /*         (0 == hash_elts(msrc->mfes_itfs))); */
871 }
872
873
874 static void
875 mfib_entry_update_i (mfib_entry_t *mfib_entry,
876                      mfib_entry_src_t *msrc,
877                      mfib_source_t current_best,
878                      index_t repi)
879 {
880     if (INDEX_INVALID != repi)
881     {
882         /*
883          * The source is providing its own replicate DPO.
884          * Create a sepcial path-list to manage it, that way
885          * this entry and the source are equivalent to a normal
886          * entry
887          */
888         fib_node_index_t old_pl_index;
889         dpo_proto_t dp;
890         dpo_id_t dpo = DPO_INVALID;
891
892         dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
893         old_pl_index = msrc->mfes_pl;
894
895         dpo_set(&dpo, DPO_REPLICATE, dp, repi);
896
897         msrc->mfes_pl =
898             fib_path_list_create_special(dp,
899                                          FIB_PATH_LIST_FLAG_EXCLUSIVE,
900                                          &dpo);
901
902         dpo_reset(&dpo);
903         fib_path_list_lock(msrc->mfes_pl);
904         fib_path_list_unlock(old_pl_index);
905     }
906
907     if (mfib_entry_src_ok_for_delete(msrc))
908     {
909         /*
910          * this source has no interfaces and no flags.
911          * it has nothing left to give - remove it
912          */
913         mfib_entry_src_remove(mfib_entry, msrc->mfes_src);
914     }
915
916     mfib_entry_recalculate_forwarding(mfib_entry, current_best);
917 }
918
919 int
920 mfib_entry_special_add (fib_node_index_t mfib_entry_index,
921                         mfib_source_t source,
922                         mfib_entry_flags_t entry_flags,
923                         fib_rpf_id_t rpf_id,
924                         index_t repi)
925 {
926     mfib_source_t current_best;
927     mfib_entry_t *mfib_entry;
928     mfib_entry_src_t *msrc;
929
930     mfib_entry = mfib_entry_get(mfib_entry_index);
931     current_best = mfib_entry_get_best_source(mfib_entry);
932
933     msrc = mfib_entry_src_update_and_lock(mfib_entry, source, rpf_id,
934                                           entry_flags);
935
936     mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
937
938     return (mfib_entry_ok_for_delete(mfib_entry));
939 }
940
941 int
942 mfib_entry_update (fib_node_index_t mfib_entry_index,
943                    mfib_source_t source,
944                    mfib_entry_flags_t entry_flags,
945                    fib_rpf_id_t rpf_id,
946                    index_t repi)
947 {
948     mfib_source_t current_best;
949     mfib_entry_t *mfib_entry;
950     mfib_entry_src_t *msrc;
951
952     mfib_entry = mfib_entry_get(mfib_entry_index);
953     current_best = mfib_entry_get_best_source(mfib_entry);
954     msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
955
956     mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
957
958     return (mfib_entry_ok_for_delete(mfib_entry));
959 }
960
961 static void
962 mfib_entry_itf_add (mfib_entry_src_t *msrc,
963                     u32 sw_if_index,
964                     index_t mi)
965 {
966     hash_set(msrc->mfes_itfs, sw_if_index, mi);
967 }
968
969 static void
970 mfib_entry_itf_remove (mfib_entry_src_t *msrc,
971                        u32 sw_if_index)
972 {
973     mfib_itf_t *mfi;
974
975     mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
976
977     mfib_itf_delete(mfi);
978
979     hash_unset(msrc->mfes_itfs, sw_if_index);
980 }
981
982 static int
983 mfib_entry_path_itf_based (const fib_route_path_t *rpath)
984 {
985     return (!(rpath->frp_flags & FIB_ROUTE_PATH_BIER_IMP) &&
986             ~0 != rpath->frp_sw_if_index);
987 }
988
989 void
990 mfib_entry_path_update (fib_node_index_t mfib_entry_index,
991                         mfib_source_t source,
992                         const fib_route_path_t *rpaths)
993 {
994     fib_node_index_t* path_indices, path_index;
995     const fib_route_path_t *rpath;
996     mfib_source_t current_best;
997     mfib_path_ext_t *path_ext;
998     const mfib_prefix_t *pfx;
999     mfib_entry_t *mfib_entry;
1000     mfib_entry_src_t *msrc;
1001     mfib_itf_flags_t old;
1002     u32 ii;
1003
1004     mfib_entry = mfib_entry_get(mfib_entry_index);
1005     pfx = mfib_entry_get_prefix(mfib_entry_index);
1006     ASSERT(NULL != mfib_entry);
1007     current_best = mfib_entry_get_best_source(mfib_entry);
1008     msrc = mfib_entry_src_find_or_create(mfib_entry, source);
1009
1010     /*
1011      * add the path to the path-list. If it's a duplicate we'll get
1012      * back the original path.
1013      */
1014     path_indices = mfib_entry_src_paths_add(msrc, rpaths);
1015
1016     vec_foreach_index(ii, path_indices)
1017     {
1018         path_index = path_indices[ii];
1019         rpath = &rpaths[ii];
1020
1021         if (FIB_NODE_INDEX_INVALID == path_index)
1022             continue;
1023
1024         /*
1025          * find the path extension for that path
1026          */
1027         path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
1028
1029         if (NULL == path_ext)
1030         {
1031             old = MFIB_ITF_FLAG_NONE;
1032             path_ext = mfib_path_ext_add(msrc, path_index,
1033                                          rpath->frp_mitf_flags);
1034         }
1035         else
1036         {
1037             old = path_ext->mfpe_flags;
1038             path_ext->mfpe_flags = rpath->frp_mitf_flags;
1039         }
1040
1041         /*
1042          * Has the path changed its contribution to the input interface set.
1043          * Which only paths with interfaces can do...
1044          */
1045         if (mfib_entry_path_itf_based(rpath))
1046         {
1047             mfib_itf_t *mfib_itf;
1048
1049             if (old != rpath->frp_mitf_flags)
1050             {
1051                 /*
1052                  * change of flag contributions
1053                  */
1054                 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
1055                                                rpath->frp_sw_if_index);
1056
1057                 if (NULL == mfib_itf)
1058                 {
1059                     index_t mfib_itf_i = mfib_itf_create(path_index,
1060                                                          rpath->frp_mitf_flags);
1061                     mfib_entry_itf_add(msrc,
1062                                        rpath->frp_sw_if_index,
1063                                        mfib_itf_i);
1064
1065                     if (MFIB_ITF_FLAG_ACCEPT & rpath->frp_mitf_flags)
1066                     {
1067                         /* new accepting interface - add the mac to the driver */
1068                         mfib_itf_mac_add(mfib_itf_get(mfib_itf_i), pfx);
1069                     }
1070                 }
1071                 else
1072                 {
1073                     u8 was_accept = !!(old & MFIB_ITF_FLAG_ACCEPT);
1074                     u8 is_accept = !!(rpath->frp_mitf_flags & MFIB_ITF_FLAG_ACCEPT);
1075
1076                     if (mfib_itf_update(mfib_itf,
1077                                         path_index,
1078                                         rpath->frp_mitf_flags))
1079                     {
1080                         /*
1081                          * no more interface flags on this path, remove
1082                          * from the data-plane set
1083                          */
1084                         if (was_accept)
1085                         {
1086                             mfib_itf_mac_del(mfib_itf, pfx);
1087
1088                         }
1089                         mfib_entry_itf_remove(msrc, rpath->frp_sw_if_index);
1090                     }
1091                     else
1092                     {
1093                         /*
1094                          * is there a change to the ACCEPT flag that
1095                          * requires us to update hte driver with the
1096                          * MAC
1097                          */
1098                         if (is_accept != was_accept)
1099                         {
1100                             if (is_accept)
1101                             {
1102                                 mfib_itf_mac_add(mfib_itf, pfx);
1103                             }
1104                             else if (was_accept)
1105                             {
1106                                 mfib_itf_mac_del(mfib_itf, pfx);
1107                             }
1108                         }
1109                     }
1110                 }
1111             }
1112         }
1113     }
1114     vec_free(path_indices);
1115
1116     mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1117 }
1118
1119 /*
1120  * mfib_entry_path_remove
1121  *
1122  * remove a path from the entry.
1123  * return the mfib_entry's index if it is still present, INVALID otherwise.
1124  */
1125 int
1126 mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
1127                         mfib_source_t source,
1128                         const fib_route_path_t *rpaths)
1129 {
1130     fib_node_index_t path_index, *path_indices;
1131     const fib_route_path_t *rpath;
1132     mfib_source_t current_best;
1133     const mfib_prefix_t *pfx;
1134     mfib_entry_t *mfib_entry;
1135     mfib_entry_src_t *msrc;
1136     u32 ii;
1137
1138     mfib_entry = mfib_entry_get(mfib_entry_index);
1139     pfx = mfib_entry_get_prefix(mfib_entry_index);
1140     ASSERT(NULL != mfib_entry);
1141     current_best = mfib_entry_get_best_source(mfib_entry);
1142     msrc = mfib_entry_src_find(mfib_entry, source, NULL);
1143
1144     if (NULL == msrc)
1145     {
1146         /*
1147          * there are no paths left for this source
1148          */
1149         return (mfib_entry_ok_for_delete(mfib_entry));
1150     }
1151
1152     /*
1153      * remove the paths from the path-list. If it's not there we'll get
1154      * back an empty vector
1155      */
1156     path_indices = mfib_entry_src_paths_remove(msrc, rpaths);
1157
1158     vec_foreach_index(ii, path_indices)
1159     {
1160         path_index = path_indices[ii];
1161         rpath = &rpaths[ii];
1162
1163         if (FIB_NODE_INDEX_INVALID == path_index)
1164             continue;
1165       
1166         /*
1167          * don't need the extension, nor the interface anymore
1168          */
1169         mfib_path_ext_remove(msrc, path_index);
1170         if (mfib_entry_path_itf_based(rpath))
1171         {
1172             u8 was_accept, is_accept;
1173             mfib_itf_t *mfib_itf;
1174
1175             mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
1176                                            rpath->frp_sw_if_index);
1177             was_accept = !!(MFIB_ITF_FLAG_ACCEPT & mfib_itf->mfi_flags);
1178
1179             if (mfib_itf_update(mfib_itf,
1180                                 path_index,
1181                                 MFIB_ITF_FLAG_NONE))
1182             {
1183                 if (was_accept)
1184                 {
1185                     mfib_itf_mac_del(mfib_itf, pfx);                    
1186                 }
1187
1188                 /*
1189                  * no more interface flags on this path, remove
1190                  * from the data-plane set
1191                  */
1192                 mfib_entry_itf_remove(msrc, rpath->frp_sw_if_index);
1193             }
1194             else
1195             {
1196                 is_accept = !!(MFIB_ITF_FLAG_ACCEPT & mfib_itf->mfi_flags);
1197
1198                 if (was_accept && !is_accept)
1199                 {
1200                     mfib_itf_mac_del(mfib_itf, pfx);                    
1201                 }
1202             }
1203         }
1204     }
1205     vec_free(path_indices);
1206
1207     if (mfib_entry_src_ok_for_delete(msrc))
1208       {
1209         /*
1210          * this source has no interfaces and no flags.
1211          * it has nothing left to give - remove it
1212          */
1213         mfib_entry_src_remove(mfib_entry, source);
1214       }
1215
1216     mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1217
1218     return (mfib_entry_ok_for_delete(mfib_entry));
1219 }
1220
1221 /**
1222  * mfib_entry_delete
1223  *
1224  * The source is withdrawing all the paths it provided
1225  */
1226 int
1227 mfib_entry_delete (fib_node_index_t mfib_entry_index,
1228                    mfib_source_t source)
1229 {
1230     mfib_source_t current_best;
1231     mfib_entry_t *mfib_entry;
1232
1233     mfib_entry = mfib_entry_get(mfib_entry_index);
1234     current_best = mfib_entry_get_best_source(mfib_entry);
1235     mfib_entry_src_remove(mfib_entry, source);
1236
1237     mfib_entry_recalculate_forwarding(mfib_entry, current_best);
1238
1239     return (mfib_entry_ok_for_delete(mfib_entry));
1240 }
1241
1242 static int
1243 fib_ip4_address_compare (ip4_address_t * a1,
1244                          ip4_address_t * a2)
1245 {
1246     /*
1247      * IP addresses are unsiged ints. the return value here needs to be signed
1248      * a simple subtraction won't cut it.
1249      * If the addresses are the same, the sort order is undefiend, so phoey.
1250      */
1251     return ((clib_net_to_host_u32(a1->data_u32) >
1252              clib_net_to_host_u32(a2->data_u32) ) ?
1253             1 : -1);
1254 }
1255
1256 static int
1257 fib_ip6_address_compare (ip6_address_t * a1,
1258                          ip6_address_t * a2)
1259 {
1260   int i;
1261   for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1262   {
1263       int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1264                  clib_net_to_host_u16 (a2->as_u16[i]));
1265       if (cmp != 0)
1266           return cmp;
1267   }
1268   return 0;
1269 }
1270
1271 static int
1272 mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1273                 fib_node_index_t mfib_entry_index2)
1274 {
1275     mfib_entry_t *mfib_entry1, *mfib_entry2;
1276     int cmp = 0;
1277
1278     mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1279     mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1280
1281     switch (mfib_entry1->mfe_prefix.fp_proto)
1282     {
1283     case FIB_PROTOCOL_IP4:
1284         cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1285                                       &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1286
1287         if (0 == cmp)
1288         {
1289             cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1290                                           &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1291         }
1292         break;
1293     case FIB_PROTOCOL_IP6:
1294         cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1295                                       &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1296
1297         if (0 == cmp)
1298         {
1299             cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1300                                           &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1301         }
1302         break;
1303     case FIB_PROTOCOL_MPLS:
1304         ASSERT(0);
1305         cmp = 0;
1306         break;
1307     }
1308
1309     if (0 == cmp) {
1310         cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1311     }
1312     return (cmp);
1313 }
1314
1315 int
1316 mfib_entry_cmp_for_sort (void *i1, void *i2)
1317 {
1318     fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1319
1320     return (mfib_entry_cmp(*mfib_entry_index1,
1321                            *mfib_entry_index2));
1322 }
1323
1324 static void
1325 mfib_entry_last_lock_gone (fib_node_t *node)
1326 {
1327     mfib_entry_t *mfib_entry;
1328     mfib_entry_src_t *msrc;
1329
1330     mfib_entry = mfib_entry_from_fib_node(node);
1331
1332     dpo_reset(&mfib_entry->mfe_rep);
1333
1334     MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1335
1336     vec_foreach(msrc, mfib_entry->mfe_srcs)
1337     {
1338         mfib_entry_src_flush(msrc);
1339     }
1340
1341     vec_free(mfib_entry->mfe_srcs);
1342
1343     fib_node_deinit(&mfib_entry->mfe_node);
1344     pool_put(mfib_entry_pool, mfib_entry);
1345 }
1346
1347 u32
1348 mfib_entry_get_stats_index (fib_node_index_t fib_entry_index)
1349 {
1350     mfib_entry_t *mfib_entry;
1351
1352     mfib_entry = mfib_entry_get(fib_entry_index);
1353
1354     return (mfib_entry->mfe_rep.dpoi_index);
1355 }
1356
1357 /*
1358  * mfib_entry_back_walk_notify
1359  *
1360  * A back walk has reach this entry.
1361  */
1362 static fib_node_back_walk_rc_t
1363 mfib_entry_back_walk_notify (fib_node_t *node,
1364                             fib_node_back_walk_ctx_t *ctx)
1365 {
1366     mfib_entry_t *mfib_entry;
1367
1368     mfib_entry = mfib_entry_from_fib_node(node);
1369     mfib_entry_recalculate_forwarding(mfib_entry,
1370                                       mfib_entry_get_best_source(mfib_entry));
1371
1372     return (FIB_NODE_BACK_WALK_CONTINUE);
1373 }
1374
1375 static void
1376 mfib_entry_show_memory (void)
1377 {
1378     fib_show_memory_usage("multicast-Entry",
1379                           pool_elts(mfib_entry_pool),
1380                           pool_len(mfib_entry_pool),
1381                           sizeof(mfib_entry_t));
1382 }
1383
1384 /*
1385  * The MFIB entry's graph node virtual function table
1386  */
1387 static const fib_node_vft_t mfib_entry_vft = {
1388     .fnv_get = mfib_entry_get_node,
1389     .fnv_last_lock = mfib_entry_last_lock_gone,
1390     .fnv_back_walk = mfib_entry_back_walk_notify,
1391     .fnv_mem_show = mfib_entry_show_memory,
1392 };
1393
1394 void
1395 mfib_entry_lock (fib_node_index_t mfib_entry_index)
1396 {
1397     mfib_entry_t *mfib_entry;
1398
1399     mfib_entry = mfib_entry_get(mfib_entry_index);
1400
1401     fib_node_lock(&mfib_entry->mfe_node);
1402 }
1403
1404 void
1405 mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1406 {
1407     mfib_entry_t *mfib_entry;
1408
1409     mfib_entry = mfib_entry_get(mfib_entry_index);
1410
1411     fib_node_unlock(&mfib_entry->mfe_node);
1412 }
1413
1414 static void
1415 mfib_entry_dpo_lock (dpo_id_t *dpo)
1416 {
1417 }
1418 static void
1419 mfib_entry_dpo_unlock (dpo_id_t *dpo)
1420 {
1421 }
1422
1423 const static dpo_vft_t mfib_entry_dpo_vft = {
1424     .dv_lock = mfib_entry_dpo_lock,
1425     .dv_unlock = mfib_entry_dpo_unlock,
1426     .dv_format = format_mfib_entry_dpo,
1427     .dv_mem_show = mfib_entry_show_memory,
1428 };
1429
1430 const static char* const mfib_entry_ip4_nodes[] =
1431 {
1432     "ip4-mfib-forward-rpf",
1433     NULL,
1434 };
1435 const static char* const mfib_entry_ip6_nodes[] =
1436 {
1437     "ip6-mfib-forward-rpf",
1438     NULL,
1439 };
1440
1441 const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1442 {
1443     [DPO_PROTO_IP4]  = mfib_entry_ip4_nodes,
1444     [DPO_PROTO_IP6]  = mfib_entry_ip6_nodes,
1445 };
1446
1447 void
1448 mfib_entry_module_init (void)
1449 {
1450     fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1451     dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1452     mfib_entry_logger = vlib_log_register_class("mfib", "entry");
1453 }
1454
1455 fib_route_path_t*
1456 mfib_entry_encode (fib_node_index_t mfib_entry_index)
1457 {
1458     fib_path_encode_ctx_t ctx = {
1459         .rpaths = NULL,
1460     };
1461     mfib_entry_t *mfib_entry;
1462     fib_route_path_t *rpath;
1463     mfib_entry_src_t *bsrc;
1464
1465     mfib_entry = mfib_entry_get(mfib_entry_index);
1466     bsrc = mfib_entry_get_best_src(mfib_entry);
1467
1468     if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
1469     {
1470         fib_path_list_walk_w_ext(bsrc->mfes_pl,
1471                                  NULL,
1472                                  fib_path_encode,
1473                                  &ctx);
1474     }
1475
1476     vec_foreach(rpath, ctx.rpaths)
1477     {
1478         mfib_itf_t *mfib_itf;
1479
1480         mfib_itf = mfib_entry_itf_find(bsrc->mfes_itfs,
1481                                        rpath->frp_sw_if_index);
1482         if (mfib_itf)
1483         {
1484             rpath->frp_mitf_flags = mfib_itf->mfi_flags;
1485         }
1486     }
1487
1488     return (ctx.rpaths);
1489 }
1490
1491 const mfib_prefix_t *
1492 mfib_entry_get_prefix (fib_node_index_t mfib_entry_index)
1493 {
1494     mfib_entry_t *mfib_entry;
1495
1496     mfib_entry = mfib_entry_get(mfib_entry_index);
1497
1498     return (&mfib_entry->mfe_prefix);
1499 }
1500
1501 u32
1502 mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1503 {
1504     mfib_entry_t *mfib_entry;
1505
1506     mfib_entry = mfib_entry_get(mfib_entry_index);
1507
1508     return (mfib_entry->mfe_fib_index);
1509 }
1510
1511 const dpo_id_t*
1512 mfib_entry_contribute_ip_forwarding (fib_node_index_t mfib_entry_index)
1513 {
1514     mfib_entry_t *mfib_entry;
1515
1516     mfib_entry = mfib_entry_get(mfib_entry_index);
1517
1518     return (&mfib_entry->mfe_rep);
1519 }
1520
1521 void
1522 mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1523                                   fib_forward_chain_type_t type,
1524                                   mfib_entry_fwd_flags_t flags,
1525                                   dpo_id_t *dpo)
1526 {
1527     /*
1528      * An IP mFIB entry can only provide a forwarding chain that
1529      * is the same IP proto as the prefix.
1530      * No use-cases (i know of) for other combinations.
1531      */
1532     mfib_entry_t *mfib_entry;
1533     dpo_proto_t dp;
1534
1535     mfib_entry = mfib_entry_get(mfib_entry_index);
1536
1537     dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1538
1539     if (type == mfib_forw_chain_type_from_dpo_proto(dp))
1540     {
1541         replicate_t * rep;
1542
1543         rep = replicate_get(mfib_entry->mfe_rep.dpoi_index);
1544
1545         if ((rep->rep_flags & REPLICATE_FLAGS_HAS_LOCAL) &&
1546             (flags & MFIB_ENTRY_FWD_FLAG_NO_LOCAL))
1547         {
1548             /*
1549              * caller does not want the local paths that the entry has
1550              */
1551             dpo_proto_t rep_proto = rep->rep_proto;
1552             dpo_set(dpo, DPO_REPLICATE, rep_proto,
1553                     replicate_dup(REPLICATE_FLAGS_NONE,
1554                                   mfib_entry->mfe_rep.dpoi_index));
1555         }
1556         else
1557         {
1558             dpo_copy(dpo, &mfib_entry->mfe_rep);
1559         }
1560     }
1561     else
1562     {
1563         dpo_copy(dpo, drop_dpo_get(dp));
1564     }
1565 }
1566
1567 /*
1568  * fib_entry_cover_changed
1569  *
1570  * this entry is tracking its cover and that cover has changed.
1571  */
1572 void
1573 mfib_entry_cover_changed (fib_node_index_t mfib_entry_index)
1574 {
1575     mfib_entry_t *mfib_entry;
1576     mfib_entry_src_t *msrc;
1577     mfib_src_res_t res;
1578
1579     mfib_entry = mfib_entry_get(mfib_entry_index);
1580     msrc = mfib_entry_get_best_src(mfib_entry);
1581
1582     res = mfib_entry_src_cover_change(mfib_entry, msrc);
1583
1584     if (MFIB_SRC_REEVALUATE == res)
1585     {
1586         mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1587     }
1588     MFIB_ENTRY_DBG(mfib_entry, "cover-changed");
1589 }
1590
1591 /*
1592  * mfib_entry_cover_updated
1593  *
1594  * this entry is tracking its cover and that cover has been updated
1595  * (i.e. its forwarding information has changed).
1596  */
1597 void
1598 mfib_entry_cover_updated (fib_node_index_t mfib_entry_index)
1599 {
1600     mfib_entry_t *mfib_entry;
1601     mfib_entry_src_t *msrc;
1602     mfib_src_res_t res;
1603
1604     mfib_entry = mfib_entry_get(mfib_entry_index);
1605     msrc = mfib_entry_get_best_src(mfib_entry);
1606
1607     res = mfib_entry_src_cover_update(mfib_entry, msrc);
1608
1609     if (MFIB_SRC_REEVALUATE == res)
1610     {
1611         mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1612     }
1613     MFIB_ENTRY_DBG(mfib_entry, "cover-updated");
1614 }
1615
1616 u32
1617 mfib_entry_pool_size (void)
1618 {
1619     return (pool_elts(mfib_entry_pool));
1620 }
1621
1622 static clib_error_t *
1623 show_mfib_entry_command (vlib_main_t * vm,
1624                         unformat_input_t * input,
1625                         vlib_cli_command_t * cmd)
1626 {
1627     fib_node_index_t fei;
1628
1629     if (unformat (input, "%d", &fei))
1630     {
1631         /*
1632          * show one in detail
1633          */
1634         if (!pool_is_free_index(mfib_entry_pool, fei))
1635         {
1636             vlib_cli_output (vm, "%d@%U",
1637                              fei,
1638                              format_mfib_entry, fei,
1639                              MFIB_ENTRY_FORMAT_DETAIL2);
1640         }
1641         else
1642         {
1643             vlib_cli_output (vm, "entry %d invalid", fei);
1644         }
1645     }
1646     else
1647     {
1648         /*
1649          * show all
1650          */
1651         vlib_cli_output (vm, "FIB Entries:");
1652         pool_foreach_index (fei, mfib_entry_pool)
1653          {
1654             vlib_cli_output (vm, "%d@%U",
1655                              fei,
1656                              format_mfib_entry, fei,
1657                              MFIB_ENTRY_FORMAT_BRIEF);
1658         }
1659     }
1660
1661     return (NULL);
1662 }
1663
1664 /*?
1665  * This command displays an entry, or all entries, in the mfib tables indexed
1666  * by their unique numerical identifier.
1667  ?*/
1668 VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1669   .path = "show mfib entry",
1670   .function = show_mfib_entry_command,
1671   .short_help = "show mfib entry",
1672 };