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