blob: 6c356c6b267f3a1865e4d85e595810a2ab21f0cb [file] [log] [blame]
Neale Ranns32e1c012016-11-22 17:07:28 +00001/*
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/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -080052 * MFIB extensions to each path
53 */
54typedef 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/**
Neale Ranns32e1c012016-11-22 17:07:28 +000061 * The source of an MFIB entry
62 */
63typedef struct mfib_entry_src_t_
64{
65 /**
66 * Which source this is
67 */
68 mfib_source_t mfes_src;
69
70 /**
Neale Ranns32e1c012016-11-22 17:07:28 +000071 * Route flags
72 */
73 mfib_entry_flags_t mfes_flags;
74
75 /**
Neale Ranns0f26c5a2017-03-01 15:12:11 -080076 * 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.
Neale Ranns32e1c012016-11-22 17:07:28 +000094 */
95 mfib_itf_t *mfes_itfs;
96} mfib_entry_src_t;
97
98/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -080099 * Pool of path extensions
100 */
101static mfib_path_ext_t *mfib_path_ext_pool;
102
103/**
Neale Ranns32e1c012016-11-22 17:07:28 +0000104 * String names for each source
105 */
106static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
107
108/*
109 * Pool for all fib_entries
110 */
111mfib_entry_t *mfib_entry_pool;
112
113static fib_node_t *
114mfib_entry_get_node (fib_node_index_t index)
115{
116 return ((fib_node_t*)mfib_entry_get(index));
117}
118
119static fib_protocol_t
120mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
121{
122 return (mfib_entry->mfe_prefix.fp_proto);
123}
124
125fib_forward_chain_type_t
126mfib_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
141static u8 *
142format_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
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800152static inline mfib_path_ext_t *
153mfib_entry_path_ext_get (index_t mi)
154{
155 return (pool_elt_at_index(mfib_path_ext_pool, mi));
156}
157
158static u8 *
159format_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
Neale Ranns32e1c012016-11-22 17:07:28 +0000170u8 *
171format_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 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800188 fib_node_index_t path_index, mpi;
189
Neale Ranns32e1c012016-11-22 17:07:28 +0000190 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 }
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700202 s = format (s, " Extensions:\n");
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800203 hash_foreach(path_index, mpi, msrc->mfes_exts,
204 ({
205 s = format(s, " %U\n", format_mfib_entry_path_ext, mpi);
206 }));
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700207 s = format (s, " Interface-Forwarding:\n");
Neale Ranns32e1c012016-11-22 17:07:28 +0000208 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 }));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800220 s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id);
Neale Ranns32e1c012016-11-22 17:07:28 +0000221 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
238static mfib_entry_t*
239mfib_entry_from_fib_node (fib_node_t *node)
240{
Neale Ranns32e1c012016-11-22 17:07:28 +0000241 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
Neale Ranns32e1c012016-11-22 17:07:28 +0000242 return ((mfib_entry_t*)node);
243}
244
245static int
246mfib_entry_src_cmp_for_sort (void * v1,
247 void * v2)
248{
249 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
250
251 return (esrc1->mfes_src - esrc2->mfes_src);
252}
253
254static void
255mfib_entry_src_init (mfib_entry_t *mfib_entry,
256 mfib_source_t source)
257
258{
259 mfib_entry_src_t esrc = {
260 .mfes_pl = FIB_NODE_INDEX_INVALID,
261 .mfes_flags = MFIB_ENTRY_FLAG_NONE,
262 .mfes_src = source,
263 };
264
265 vec_add1(mfib_entry->mfe_srcs, esrc);
266 vec_sort_with_function(mfib_entry->mfe_srcs,
267 mfib_entry_src_cmp_for_sort);
268}
269
270static mfib_entry_src_t *
271mfib_entry_src_find (const mfib_entry_t *mfib_entry,
272 mfib_source_t source,
273 u32 *index)
274
275{
276 mfib_entry_src_t *esrc;
277 int ii;
278
279 ii = 0;
280 vec_foreach(esrc, mfib_entry->mfe_srcs)
281 {
282 if (esrc->mfes_src == source)
283 {
284 if (NULL != index)
285 {
286 *index = ii;
287 }
288 return (esrc);
289 }
290 else
291 {
292 ii++;
293 }
294 }
295
296 return (NULL);
297}
298
299static mfib_entry_src_t *
300mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700301 mfib_source_t source)
Neale Ranns32e1c012016-11-22 17:07:28 +0000302{
303 mfib_entry_src_t *esrc;
304
305 esrc = mfib_entry_src_find(mfib_entry, source, NULL);
306
307 if (NULL == esrc)
308 {
309 mfib_entry_src_init(mfib_entry, source);
310 }
311
312 return (mfib_entry_src_find(mfib_entry, source, NULL));
313}
314
315static mfib_entry_src_t*
316mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
317{
318 mfib_entry_src_t *bsrc;
319
320 /*
321 * the enum of sources is deliberately arranged in priority order
322 */
323 if (0 == vec_len(mfib_entry->mfe_srcs))
324 {
325 bsrc = NULL;
326 }
327 else
328 {
329 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
330 }
331
332 return (bsrc);
333}
334
Neale Ranns15002542017-09-10 04:39:11 -0700335int
336mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
337 mfib_source_t source)
338{
339 mfib_entry_t *mfib_entry;
340
341 mfib_entry = mfib_entry_get(mfib_entry_index);
342
343 return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
344}
345
Neale Ranns32e1c012016-11-22 17:07:28 +0000346static void
347mfib_entry_src_flush (mfib_entry_src_t *msrc)
348{
349 u32 sw_if_index;
350 index_t mfii;
351
352 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
353 ({
354 mfib_itf_delete(mfib_itf_get(mfii));
355 }));
Neale Rannsbcc6aa42017-02-24 01:34:14 -0800356 hash_free(msrc->mfes_itfs);
357 msrc->mfes_itfs = NULL;
Neale Rannsa9374df2017-02-02 02:18:18 -0800358 fib_path_list_unlock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000359}
360
361static void
362mfib_entry_src_remove (mfib_entry_t *mfib_entry,
363 mfib_source_t source)
364
365{
366 mfib_entry_src_t *msrc;
367 u32 index = ~0;
368
369 msrc = mfib_entry_src_find(mfib_entry, source, &index);
370
371 if (NULL != msrc)
372 {
373 mfib_entry_src_flush(msrc);
374 vec_del1(mfib_entry->mfe_srcs, index);
375 }
376}
377
Neale Ranns32e1c012016-11-22 17:07:28 +0000378u32
379mfib_entry_child_add (fib_node_index_t mfib_entry_index,
380 fib_node_type_t child_type,
381 fib_node_index_t child_index)
382{
383 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
384 mfib_entry_index,
385 child_type,
386 child_index));
387};
388
389void
390mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
391 u32 sibling_index)
392{
393 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
394 mfib_entry_index,
395 sibling_index);
396}
397
398static mfib_entry_t *
399mfib_entry_alloc (u32 fib_index,
400 const mfib_prefix_t *prefix,
401 fib_node_index_t *mfib_entry_index)
402{
403 mfib_entry_t *mfib_entry;
404
Marco Varlese47501da2017-08-29 14:04:06 +0200405 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
Neale Ranns32e1c012016-11-22 17:07:28 +0000406
407 fib_node_init(&mfib_entry->mfe_node,
408 FIB_NODE_TYPE_MFIB_ENTRY);
409
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800410 /*
411 * Some of the members require non-default initialisation
412 * so we also init those that don't and thus save on the call to memset.
413 */
414 mfib_entry->mfe_flags = 0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000415 mfib_entry->mfe_fib_index = fib_index;
416 mfib_entry->mfe_prefix = *prefix;
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800417 mfib_entry->mfe_srcs = NULL;
418 mfib_entry->mfe_itfs = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800419 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
Neale Rannsc2aad532017-05-30 09:53:52 -0700420 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
Neale Ranns32e1c012016-11-22 17:07:28 +0000421
422 dpo_reset(&mfib_entry->mfe_rep);
423
424 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
425
426 MFIB_ENTRY_DBG(mfib_entry, "alloc");
427
428 return (mfib_entry);
429}
430
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800431static inline mfib_path_ext_t *
432mfib_entry_path_ext_find (mfib_path_ext_t *exts,
433 fib_node_index_t path_index)
434{
435 uword *p;
436
437 p = hash_get(exts, path_index);
438
439 if (NULL != p)
440 {
441 return (mfib_entry_path_ext_get(p[0]));
442 }
443
444 return (NULL);
445}
446
447static mfib_path_ext_t*
448mfib_path_ext_add (mfib_entry_src_t *msrc,
449 fib_node_index_t path_index,
450 mfib_itf_flags_t mfi_flags)
451{
452 mfib_path_ext_t *path_ext;
453
454 pool_get(mfib_path_ext_pool, path_ext);
455
456 path_ext->mfpe_flags = mfi_flags;
457 path_ext->mfpe_path = path_index;
458
459 hash_set(msrc->mfes_exts, path_index,
460 path_ext - mfib_path_ext_pool);
461
462 return (path_ext);
463}
464
465static void
466mfib_path_ext_remove (mfib_entry_src_t *msrc,
467 fib_node_index_t path_index)
468{
469 mfib_path_ext_t *path_ext;
470
471 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
472
473 hash_unset(msrc->mfes_exts, path_index);
474 pool_put(mfib_path_ext_pool, path_ext);
475}
476
Neale Ranns32e1c012016-11-22 17:07:28 +0000477typedef struct mfib_entry_collect_forwarding_ctx_t_
478{
479 load_balance_path_t * next_hops;
480 fib_forward_chain_type_t fct;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800481 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000482} mfib_entry_collect_forwarding_ctx_t;
483
Neale Ranns81424992017-05-18 03:03:22 -0700484static fib_path_list_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000485mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
486 fib_node_index_t path_index,
487 void *arg)
488{
489 mfib_entry_collect_forwarding_ctx_t *ctx;
490 load_balance_path_t *nh;
491
492 ctx = arg;
493
494 /*
495 * if the path is not resolved, don't include it.
496 */
497 if (!fib_path_is_resolved(path_index))
498 {
Neale Ranns81424992017-05-18 03:03:22 -0700499 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000500 }
501
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800502 /*
503 * If the path is not forwarding to use it
504 */
505 mfib_path_ext_t *path_ext;
506
507 path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
508 path_index);
509
510 if (NULL != path_ext &&
511 !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
512 {
Neale Ranns81424992017-05-18 03:03:22 -0700513 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800514 }
515
Neale Ranns32e1c012016-11-22 17:07:28 +0000516 switch (ctx->fct)
517 {
518 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
519 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
520 /*
521 * EOS traffic with no label to stack, we need the IP Adj
522 */
523 vec_add2(ctx->next_hops, nh, 1);
524
525 nh->path_index = path_index;
526 nh->path_weight = fib_path_get_weight(path_index);
527 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
528 break;
529
530 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
531 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
532 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
533 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
534 case FIB_FORW_CHAIN_TYPE_ETHERNET:
Florin Corasce1b4c72017-01-26 14:25:34 -0800535 case FIB_FORW_CHAIN_TYPE_NSH:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700536 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns32e1c012016-11-22 17:07:28 +0000537 ASSERT(0);
538 break;
539 }
540
Neale Ranns81424992017-05-18 03:03:22 -0700541 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000542}
543
544static void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800545mfib_entry_stack (mfib_entry_t *mfib_entry,
546 mfib_entry_src_t *msrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000547{
Neale Ranns32e1c012016-11-22 17:07:28 +0000548 dpo_proto_t dp;
549
550 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
551
Neale Rannsc2aad532017-05-30 09:53:52 -0700552 /*
553 * unlink the enty from the previous path list.
554 */
555 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
556 {
557 fib_path_list_child_remove(mfib_entry->mfe_pl,
558 mfib_entry->mfe_sibling);
559 }
560
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800561 if (NULL != msrc &&
562 FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
Neale Ranns32e1c012016-11-22 17:07:28 +0000563 {
Neale Rannsa9374df2017-02-02 02:18:18 -0800564 mfib_entry_collect_forwarding_ctx_t ctx = {
565 .next_hops = NULL,
566 .fct = mfib_entry_get_default_chain_type(mfib_entry),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800567 .msrc = msrc,
Neale Rannsa9374df2017-02-02 02:18:18 -0800568 };
569
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800570 fib_path_list_walk(msrc->mfes_pl,
Neale Ranns32e1c012016-11-22 17:07:28 +0000571 mfib_entry_src_collect_forwarding,
572 &ctx);
573
Neale Rannsa9374df2017-02-02 02:18:18 -0800574 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
Neale Ranns32e1c012016-11-22 17:07:28 +0000575 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800576 if (NULL == ctx.next_hops)
Neale Rannsa9374df2017-02-02 02:18:18 -0800577 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800578 /*
579 * no next-hops, stack directly on the drop
580 */
Neale Rannsa9374df2017-02-02 02:18:18 -0800581 dpo_stack(DPO_MFIB_ENTRY, dp,
582 &mfib_entry->mfe_rep,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800583 drop_dpo_get(dp));
Neale Rannsa9374df2017-02-02 02:18:18 -0800584 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800585 else
586 {
587 /*
588 * each path contirbutes a next-hop. form a replicate
589 * from those choices.
590 */
591 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
592 dpo_is_drop(&mfib_entry->mfe_rep))
593 {
594 dpo_id_t tmp_dpo = DPO_INVALID;
595
596 dpo_set(&tmp_dpo,
597 DPO_REPLICATE, dp,
598 replicate_create(0, dp));
599
600 dpo_stack(DPO_MFIB_ENTRY, dp,
601 &mfib_entry->mfe_rep,
602 &tmp_dpo);
603
604 dpo_reset(&tmp_dpo);
605 }
606 replicate_multipath_update(&mfib_entry->mfe_rep,
607 ctx.next_hops);
608 }
Neale Rannsa9374df2017-02-02 02:18:18 -0800609 }
610 else
611 {
612 /*
613 * for exclusive routes the source provided a replicate DPO
614 * we we stashed inthe special path list with one path
615 * so we can stack directly on that.
616 */
617 ASSERT(1 == vec_len(ctx.next_hops));
Neale Ranns32e1c012016-11-22 17:07:28 +0000618
619 dpo_stack(DPO_MFIB_ENTRY, dp,
620 &mfib_entry->mfe_rep,
Neale Rannsa9374df2017-02-02 02:18:18 -0800621 &ctx.next_hops[0].path_dpo);
622 dpo_reset(&ctx.next_hops[0].path_dpo);
623 vec_free(ctx.next_hops);
Neale Ranns32e1c012016-11-22 17:07:28 +0000624 }
Neale Rannsc2aad532017-05-30 09:53:52 -0700625
626 /*
627 * link the entry to the path-list.
628 * The entry needs to be a child so that we receive the back-walk
629 * updates to recalculate forwarding.
630 */
631 mfib_entry->mfe_pl = msrc->mfes_pl;
632 mfib_entry->mfe_sibling =
633 fib_path_list_child_add(mfib_entry->mfe_pl,
634 FIB_NODE_TYPE_MFIB_ENTRY,
635 mfib_entry_get_index(mfib_entry));
Neale Ranns32e1c012016-11-22 17:07:28 +0000636 }
637 else
638 {
639 dpo_stack(DPO_MFIB_ENTRY, dp,
640 &mfib_entry->mfe_rep,
641 drop_dpo_get(dp));
642 }
643}
644
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800645static fib_node_index_t
646mfib_entry_src_path_add (mfib_entry_src_t *msrc,
647 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000648{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800649 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000650 fib_route_path_t *rpaths;
651
Neale Rannsa9374df2017-02-02 02:18:18 -0800652 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
653
Neale Ranns32e1c012016-11-22 17:07:28 +0000654 /*
655 * path-lists require a vector of paths
656 */
657 rpaths = NULL;
658 vec_add1(rpaths, rpath[0]);
659
Neale Ranns32e1c012016-11-22 17:07:28 +0000660 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
661 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800662 /* A non-shared path-list */
663 msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
664 NULL);
665 fib_path_list_lock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000666 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800667
668 path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000669
670 vec_free(rpaths);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800671
672 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000673}
674
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800675static fib_node_index_t
676mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
677 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000678{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800679 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000680 fib_route_path_t *rpaths;
681
Neale Rannsa9374df2017-02-02 02:18:18 -0800682 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
683
Neale Ranns32e1c012016-11-22 17:07:28 +0000684 /*
685 * path-lists require a vector of paths
686 */
687 rpaths = NULL;
688 vec_add1(rpaths, rpath[0]);
689
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800690 path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000691
692 vec_free(rpaths);
693
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800694 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000695}
696
697static void
698mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry)
699{
Neale Ranns32e1c012016-11-22 17:07:28 +0000700 mfib_entry_src_t *bsrc;
701
Neale Ranns32e1c012016-11-22 17:07:28 +0000702 /*
703 * copy the forwarding data from the bast source
704 */
705 bsrc = mfib_entry_get_best_src(mfib_entry);
706
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800707 if (NULL != bsrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000708 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000709 mfib_entry->mfe_flags = bsrc->mfes_flags;
710 mfib_entry->mfe_itfs = bsrc->mfes_itfs;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800711 mfib_entry->mfe_rpf_id = bsrc->mfes_rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000712 }
713
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800714 mfib_entry_stack(mfib_entry, bsrc);
Neale Ranns32e1c012016-11-22 17:07:28 +0000715}
716
717
718fib_node_index_t
719mfib_entry_create (u32 fib_index,
720 mfib_source_t source,
721 const mfib_prefix_t *prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800722 fib_rpf_id_t rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +0000723 mfib_entry_flags_t entry_flags)
724{
725 fib_node_index_t mfib_entry_index;
726 mfib_entry_t *mfib_entry;
727 mfib_entry_src_t *msrc;
728
729 mfib_entry = mfib_entry_alloc(fib_index, prefix,
730 &mfib_entry_index);
731 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
732 msrc->mfes_flags = entry_flags;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800733 msrc->mfes_rpf_id = rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000734
735 mfib_entry_recalculate_forwarding(mfib_entry);
736
737 return (mfib_entry_index);
738}
739
740static int
741mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
742{
743 return (0 == vec_len(mfib_entry->mfe_srcs));
744}
745
746static int
747mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
748{
749 return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800750 0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
Neale Ranns32e1c012016-11-22 17:07:28 +0000751}
752
753int
754mfib_entry_update (fib_node_index_t mfib_entry_index,
755 mfib_source_t source,
Neale Rannsa9374df2017-02-02 02:18:18 -0800756 mfib_entry_flags_t entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800757 fib_rpf_id_t rpf_id,
Neale Rannsa9374df2017-02-02 02:18:18 -0800758 index_t repi)
Neale Ranns32e1c012016-11-22 17:07:28 +0000759{
760 mfib_entry_t *mfib_entry;
761 mfib_entry_src_t *msrc;
762
763 mfib_entry = mfib_entry_get(mfib_entry_index);
764 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
765 msrc->mfes_flags = entry_flags;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800766 msrc->mfes_rpf_id = rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000767
Neale Rannsa9374df2017-02-02 02:18:18 -0800768 if (INDEX_INVALID != repi)
769 {
770 /*
771 * The source is providing its own replicate DPO.
772 * Create a sepcial path-list to manage it, that way
773 * this entry and the source are equivalent to a normal
774 * entry
775 */
776 fib_node_index_t old_pl_index;
Neale Rannsda78f952017-05-24 09:15:43 -0700777 dpo_proto_t dp;
Neale Rannsa9374df2017-02-02 02:18:18 -0800778 dpo_id_t dpo = DPO_INVALID;
779
Neale Rannsda78f952017-05-24 09:15:43 -0700780 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
Neale Rannsa9374df2017-02-02 02:18:18 -0800781 old_pl_index = msrc->mfes_pl;
782
Neale Rannsda78f952017-05-24 09:15:43 -0700783 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
Neale Rannsa9374df2017-02-02 02:18:18 -0800784
785 msrc->mfes_pl =
Neale Rannsda78f952017-05-24 09:15:43 -0700786 fib_path_list_create_special(dp,
Neale Rannsa9374df2017-02-02 02:18:18 -0800787 FIB_PATH_LIST_FLAG_EXCLUSIVE,
788 &dpo);
789
790 dpo_reset(&dpo);
791 fib_path_list_lock(msrc->mfes_pl);
792 fib_path_list_unlock(old_pl_index);
793 }
794
Neale Ranns32e1c012016-11-22 17:07:28 +0000795 if (mfib_entry_src_ok_for_delete(msrc))
796 {
797 /*
798 * this source has no interfaces and no flags.
799 * it has nothing left to give - remove it
800 */
801 mfib_entry_src_remove(mfib_entry, source);
802 }
803
804 mfib_entry_recalculate_forwarding(mfib_entry);
805
806 return (mfib_entry_ok_for_delete(mfib_entry));
807}
808
809static void
810mfib_entry_itf_add (mfib_entry_src_t *msrc,
811 u32 sw_if_index,
812 index_t mi)
813{
814 hash_set(msrc->mfes_itfs, sw_if_index, mi);
815}
816
817static void
818mfib_entry_itf_remove (mfib_entry_src_t *msrc,
819 u32 sw_if_index)
820{
821 mfib_itf_t *mfi;
822
823 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
824
825 mfib_itf_delete(mfi);
826
827 hash_unset(msrc->mfes_itfs, sw_if_index);
828}
829
830void
831mfib_entry_path_update (fib_node_index_t mfib_entry_index,
832 mfib_source_t source,
833 const fib_route_path_t *rpath,
834 mfib_itf_flags_t itf_flags)
835{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800836 fib_node_index_t path_index;
837 mfib_path_ext_t *path_ext;
838 mfib_itf_flags_t old, new;
Neale Ranns32e1c012016-11-22 17:07:28 +0000839 mfib_entry_t *mfib_entry;
840 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000841
842 mfib_entry = mfib_entry_get(mfib_entry_index);
843 ASSERT(NULL != mfib_entry);
844 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
845
846 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800847 * add the path to the path-list. If it's a duplicate we'll get
848 * back the original path.
Neale Ranns32e1c012016-11-22 17:07:28 +0000849 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800850 path_index = mfib_entry_src_path_add(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000851
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800852 /*
853 * find the path extension for that path
854 */
855 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
856
857 if (NULL == path_ext)
Neale Ranns32e1c012016-11-22 17:07:28 +0000858 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800859 old = MFIB_ITF_FLAG_NONE;
860 path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000861 }
862 else
863 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800864 old = path_ext->mfpe_flags;
865 path_ext->mfpe_flags = itf_flags;
866 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000867
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800868 /*
869 * Has the path changed its contribution to the input interface set.
870 * Which only paths with interfaces can do...
871 */
872 if (~0 != rpath[0].frp_sw_if_index)
873 {
874 mfib_itf_t *mfib_itf;
875
876 new = itf_flags;
877
878 if (old != new)
Neale Ranns32e1c012016-11-22 17:07:28 +0000879 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800880 if (MFIB_ITF_FLAG_NONE == new)
881 {
882 /*
883 * no more interface flags on this path, remove
884 * from the data-plane set
885 */
886 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
887 }
888 else if (MFIB_ITF_FLAG_NONE == old)
889 {
890 /*
891 * This interface is now contributing
892 */
893 mfib_entry_itf_add(msrc,
894 rpath[0].frp_sw_if_index,
895 mfib_itf_create(rpath[0].frp_sw_if_index,
896 itf_flags));
897 }
898 else
899 {
900 /*
901 * change of flag contributions
902 */
903 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
904 rpath[0].frp_sw_if_index);
905 /* Seen by packets inflight */
906 mfib_itf->mfi_flags = new;
907 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000908 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000909 }
910
911 mfib_entry_recalculate_forwarding(mfib_entry);
912}
913
914/*
915 * mfib_entry_path_remove
916 *
917 * remove a path from the entry.
918 * return the mfib_entry's index if it is still present, INVALID otherwise.
919 */
920int
921mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
922 mfib_source_t source,
923 const fib_route_path_t *rpath)
924{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800925 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000926 mfib_entry_t *mfib_entry;
927 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000928
929 mfib_entry = mfib_entry_get(mfib_entry_index);
930 ASSERT(NULL != mfib_entry);
931 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
932
933 if (NULL == msrc)
934 {
935 /*
936 * there are no paths left for this source
937 */
938 return (mfib_entry_ok_for_delete(mfib_entry));
939 }
940
941 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800942 * remove the path from the path-list. If it's not there we'll get
943 * back invalid
Neale Ranns32e1c012016-11-22 17:07:28 +0000944 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800945 path_index = mfib_entry_src_path_remove(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000946
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800947 if (FIB_NODE_INDEX_INVALID != path_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000948 {
949 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800950 * don't need the extension, nor the interface anymore
Neale Ranns32e1c012016-11-22 17:07:28 +0000951 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800952 mfib_path_ext_remove(msrc, path_index);
953 if (~0 != rpath[0].frp_sw_if_index)
954 {
955 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
956 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000957 }
958
Neale Ranns32e1c012016-11-22 17:07:28 +0000959 if (mfib_entry_src_ok_for_delete(msrc))
960 {
961 /*
962 * this source has no interfaces and no flags.
963 * it has nothing left to give - remove it
964 */
965 mfib_entry_src_remove(mfib_entry, source);
966 }
967
968 mfib_entry_recalculate_forwarding(mfib_entry);
969
970 return (mfib_entry_ok_for_delete(mfib_entry));
971}
972
973/**
974 * mfib_entry_delete
975 *
976 * The source is withdrawing all the paths it provided
977 */
978int
979mfib_entry_delete (fib_node_index_t mfib_entry_index,
980 mfib_source_t source)
981{
982 mfib_entry_t *mfib_entry;
983
984 mfib_entry = mfib_entry_get(mfib_entry_index);
985 mfib_entry_src_remove(mfib_entry, source);
986
987 mfib_entry_recalculate_forwarding(mfib_entry);
988
989 return (mfib_entry_ok_for_delete(mfib_entry));
990}
991
992static int
993fib_ip4_address_compare (ip4_address_t * a1,
994 ip4_address_t * a2)
995{
996 /*
997 * IP addresses are unsiged ints. the return value here needs to be signed
998 * a simple subtraction won't cut it.
999 * If the addresses are the same, the sort order is undefiend, so phoey.
1000 */
1001 return ((clib_net_to_host_u32(a1->data_u32) >
1002 clib_net_to_host_u32(a2->data_u32) ) ?
1003 1 : -1);
1004}
1005
1006static int
1007fib_ip6_address_compare (ip6_address_t * a1,
1008 ip6_address_t * a2)
1009{
1010 int i;
1011 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1012 {
1013 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1014 clib_net_to_host_u16 (a2->as_u16[i]));
1015 if (cmp != 0)
1016 return cmp;
1017 }
1018 return 0;
1019}
1020
1021static int
1022mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1023 fib_node_index_t mfib_entry_index2)
1024{
1025 mfib_entry_t *mfib_entry1, *mfib_entry2;
1026 int cmp = 0;
1027
1028 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1029 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1030
1031 switch (mfib_entry1->mfe_prefix.fp_proto)
1032 {
1033 case FIB_PROTOCOL_IP4:
1034 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1035 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1036
1037 if (0 == cmp)
1038 {
1039 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1040 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1041 }
1042 break;
1043 case FIB_PROTOCOL_IP6:
1044 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1045 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1046
1047 if (0 == cmp)
1048 {
1049 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1050 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1051 }
1052 break;
1053 case FIB_PROTOCOL_MPLS:
1054 ASSERT(0);
1055 cmp = 0;
1056 break;
1057 }
1058
1059 if (0 == cmp) {
1060 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1061 }
1062 return (cmp);
1063}
1064
1065int
1066mfib_entry_cmp_for_sort (void *i1, void *i2)
1067{
1068 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1069
1070 return (mfib_entry_cmp(*mfib_entry_index1,
1071 *mfib_entry_index2));
1072}
1073
Neale Rannsc2aad532017-05-30 09:53:52 -07001074static void
1075mfib_entry_last_lock_gone (fib_node_t *node)
1076{
1077 mfib_entry_t *mfib_entry;
1078 mfib_entry_src_t *msrc;
1079
1080 mfib_entry = mfib_entry_from_fib_node(node);
1081
1082 dpo_reset(&mfib_entry->mfe_rep);
1083
1084 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1085
1086 vec_foreach(msrc, mfib_entry->mfe_srcs)
1087 {
1088 mfib_entry_src_flush(msrc);
1089 }
1090
1091 vec_free(mfib_entry->mfe_srcs);
1092
1093 fib_node_deinit(&mfib_entry->mfe_node);
1094 pool_put(mfib_entry_pool, mfib_entry);
1095}
1096
1097/*
1098 * mfib_entry_back_walk_notify
1099 *
1100 * A back walk has reach this entry.
1101 */
1102static fib_node_back_walk_rc_t
1103mfib_entry_back_walk_notify (fib_node_t *node,
1104 fib_node_back_walk_ctx_t *ctx)
1105{
1106 mfib_entry_recalculate_forwarding(mfib_entry_from_fib_node(node));
1107
1108 return (FIB_NODE_BACK_WALK_CONTINUE);
1109}
1110
1111static void
1112mfib_entry_show_memory (void)
1113{
1114 fib_show_memory_usage("multicast-Entry",
1115 pool_elts(mfib_entry_pool),
1116 pool_len(mfib_entry_pool),
1117 sizeof(mfib_entry_t));
1118}
1119
1120/*
1121 * The MFIB entry's graph node virtual function table
1122 */
1123static const fib_node_vft_t mfib_entry_vft = {
1124 .fnv_get = mfib_entry_get_node,
1125 .fnv_last_lock = mfib_entry_last_lock_gone,
1126 .fnv_back_walk = mfib_entry_back_walk_notify,
1127 .fnv_mem_show = mfib_entry_show_memory,
1128};
1129
Neale Ranns32e1c012016-11-22 17:07:28 +00001130void
1131mfib_entry_lock (fib_node_index_t mfib_entry_index)
1132{
1133 mfib_entry_t *mfib_entry;
1134
1135 mfib_entry = mfib_entry_get(mfib_entry_index);
1136
1137 fib_node_lock(&mfib_entry->mfe_node);
1138}
1139
1140void
1141mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1142{
1143 mfib_entry_t *mfib_entry;
1144
1145 mfib_entry = mfib_entry_get(mfib_entry_index);
1146
1147 fib_node_unlock(&mfib_entry->mfe_node);
1148}
1149
1150static void
1151mfib_entry_dpo_lock (dpo_id_t *dpo)
1152{
1153}
1154static void
1155mfib_entry_dpo_unlock (dpo_id_t *dpo)
1156{
1157}
1158
1159const static dpo_vft_t mfib_entry_dpo_vft = {
1160 .dv_lock = mfib_entry_dpo_lock,
1161 .dv_unlock = mfib_entry_dpo_unlock,
1162 .dv_format = format_mfib_entry_dpo,
1163 .dv_mem_show = mfib_entry_show_memory,
1164};
1165
1166const static char* const mfib_entry_ip4_nodes[] =
1167{
1168 "ip4-mfib-forward-rpf",
1169 NULL,
1170};
1171const static char* const mfib_entry_ip6_nodes[] =
1172{
1173 "ip6-mfib-forward-rpf",
1174 NULL,
1175};
1176
1177const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1178{
1179 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1180 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1181};
1182
1183void
1184mfib_entry_module_init (void)
1185{
1186 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1187 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1188}
1189
1190void
1191mfib_entry_encode (fib_node_index_t mfib_entry_index,
1192 fib_route_path_encode_t **api_rpaths)
1193{
1194 mfib_entry_t *mfib_entry;
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001195 mfib_entry_src_t *bsrc;
Neale Ranns32e1c012016-11-22 17:07:28 +00001196
1197 mfib_entry = mfib_entry_get(mfib_entry_index);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001198 bsrc = mfib_entry_get_best_src(mfib_entry);
1199
1200 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
Neale Ranns5a8123b2017-01-26 01:18:23 -08001201 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001202 fib_path_list_walk(bsrc->mfes_pl,
Neale Ranns5a8123b2017-01-26 01:18:23 -08001203 fib_path_encode,
1204 api_rpaths);
1205 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001206}
1207
Neale Ranns5a8123b2017-01-26 01:18:23 -08001208
Neale Ranns32e1c012016-11-22 17:07:28 +00001209void
1210mfib_entry_get_prefix (fib_node_index_t mfib_entry_index,
1211 mfib_prefix_t *pfx)
1212{
1213 mfib_entry_t *mfib_entry;
1214
1215 mfib_entry = mfib_entry_get(mfib_entry_index);
1216 *pfx = mfib_entry->mfe_prefix;
1217}
1218
1219u32
1220mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1221{
1222 mfib_entry_t *mfib_entry;
1223
1224 mfib_entry = mfib_entry_get(mfib_entry_index);
1225
1226 return (mfib_entry->mfe_fib_index);
1227}
1228
1229void
1230mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1231 fib_forward_chain_type_t type,
1232 dpo_id_t *dpo)
1233{
1234 /*
1235 * An IP mFIB entry can only provide a forwarding chain that
1236 * is the same IP proto as the prefix.
1237 * No use-cases (i know of) for other combinations.
1238 */
1239 mfib_entry_t *mfib_entry;
1240 dpo_proto_t dp;
1241
1242 mfib_entry = mfib_entry_get(mfib_entry_index);
1243
1244 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1245
1246 if (type == fib_forw_chain_type_from_dpo_proto(dp))
1247 {
1248 dpo_copy(dpo, &mfib_entry->mfe_rep);
1249 }
1250 else
1251 {
1252 dpo_copy(dpo, drop_dpo_get(dp));
1253 }
1254}
1255
1256u32
1257mfib_entry_pool_size (void)
1258{
1259 return (pool_elts(mfib_entry_pool));
1260}
1261
1262static clib_error_t *
1263show_mfib_entry_command (vlib_main_t * vm,
1264 unformat_input_t * input,
1265 vlib_cli_command_t * cmd)
1266{
1267 fib_node_index_t fei;
1268
1269 if (unformat (input, "%d", &fei))
1270 {
1271 /*
1272 * show one in detail
1273 */
1274 if (!pool_is_free_index(mfib_entry_pool, fei))
1275 {
1276 vlib_cli_output (vm, "%d@%U",
1277 fei,
1278 format_mfib_entry, fei,
1279 MFIB_ENTRY_FORMAT_DETAIL2);
1280 }
1281 else
1282 {
1283 vlib_cli_output (vm, "entry %d invalid", fei);
1284 }
1285 }
1286 else
1287 {
1288 /*
1289 * show all
1290 */
1291 vlib_cli_output (vm, "FIB Entries:");
1292 pool_foreach_index(fei, mfib_entry_pool,
1293 ({
1294 vlib_cli_output (vm, "%d@%U",
1295 fei,
1296 format_mfib_entry, fei,
1297 MFIB_ENTRY_FORMAT_BRIEF);
1298 }));
1299 }
1300
1301 return (NULL);
1302}
1303
Neale Rannsc756c1c2017-02-08 09:11:57 -08001304/*?
1305 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1306 * numerical indentifier.
1307 ?*/
Neale Ranns32e1c012016-11-22 17:07:28 +00001308VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1309 .path = "show mfib entry",
1310 .function = show_mfib_entry_command,
1311 .short_help = "show mfib entry",
1312};