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