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