blob: 90b223e3c253f412387e1dc759227e79ef8389ab [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/**
Neale Ranns710071b2018-09-24 12:36:26 +000025 * the logger
26 */
27vlib_log_class_t mfib_entry_logger;
28
29/**
Neale Ranns32e1c012016-11-22 17:07:28 +000030 * Debug macro
31 */
Neale Ranns710071b2018-09-24 12:36:26 +000032#define MFIB_ENTRY_DBG(_e, _fmt, _args...) \
Neale Ranns32e1c012016-11-22 17:07:28 +000033{ \
Neale Ranns710071b2018-09-24 12:36:26 +000034 vlib_log_debug(mfib_entry_logger, \
35 "e:[%d:%U]: " _fmt, \
Neale Ranns32e1c012016-11-22 17:07:28 +000036 mfib_entry_get_index(_e), \
Neale Ranns710071b2018-09-24 12:36:26 +000037 format_mfib_prefix, \
38 &_e->mfe_prefix, \
39 ##_args); \
Neale Ranns32e1c012016-11-22 17:07:28 +000040}
Neale Ranns32e1c012016-11-22 17:07:28 +000041
42/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -080043 * MFIB extensions to each path
44 */
45typedef 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/**
Neale Ranns32e1c012016-11-22 17:07:28 +000052 * The source of an MFIB entry
53 */
54typedef struct mfib_entry_src_t_
55{
56 /**
57 * Which source this is
58 */
59 mfib_source_t mfes_src;
60
61 /**
Neale Ranns32e1c012016-11-22 17:07:28 +000062 * Route flags
63 */
64 mfib_entry_flags_t mfes_flags;
65
66 /**
Neale Ranns0f26c5a2017-03-01 15:12:11 -080067 * 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.
Neale Ranns32e1c012016-11-22 17:07:28 +000085 */
86 mfib_itf_t *mfes_itfs;
87} mfib_entry_src_t;
88
89/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -080090 * Pool of path extensions
91 */
92static mfib_path_ext_t *mfib_path_ext_pool;
93
94/**
Neale Ranns32e1c012016-11-22 17:07:28 +000095 * String names for each source
96 */
97static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
98
99/*
100 * Pool for all fib_entries
101 */
102mfib_entry_t *mfib_entry_pool;
103
104static fib_node_t *
105mfib_entry_get_node (fib_node_index_t index)
106{
107 return ((fib_node_t*)mfib_entry_get(index));
108}
109
110static fib_protocol_t
111mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
112{
113 return (mfib_entry->mfe_prefix.fp_proto);
114}
115
116fib_forward_chain_type_t
117mfib_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
132static u8 *
133format_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
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800143static inline mfib_path_ext_t *
144mfib_entry_path_ext_get (index_t mi)
145{
146 return (pool_elt_at_index(mfib_path_ext_pool, mi));
147}
148
149static u8 *
150format_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
Neale Ranns32e1c012016-11-22 17:07:28 +0000161u8 *
162format_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 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800179 fib_node_index_t path_index, mpi;
180
Neale Ranns32e1c012016-11-22 17:07:28 +0000181 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 }
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700193 s = format (s, " Extensions:\n");
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800194 hash_foreach(path_index, mpi, msrc->mfes_exts,
195 ({
196 s = format(s, " %U\n", format_mfib_entry_path_ext, mpi);
197 }));
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700198 s = format (s, " Interface-Forwarding:\n");
Neale Ranns32e1c012016-11-22 17:07:28 +0000199 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 }));
Neale Rannse821ab12017-06-01 07:45:05 -0700211 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 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000215 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
232static mfib_entry_t*
233mfib_entry_from_fib_node (fib_node_t *node)
234{
Neale Ranns32e1c012016-11-22 17:07:28 +0000235 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
Neale Ranns32e1c012016-11-22 17:07:28 +0000236 return ((mfib_entry_t*)node);
237}
238
239static int
240mfib_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
248static void
249mfib_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
264static mfib_entry_src_t *
265mfib_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
293static mfib_entry_src_t *
294mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700295 mfib_source_t source)
Neale Ranns32e1c012016-11-22 17:07:28 +0000296{
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
309static mfib_entry_src_t*
310mfib_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
Neale Ranns15002542017-09-10 04:39:11 -0700329int
330mfib_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
Neale Ranns32e1c012016-11-22 17:07:28 +0000340static void
341mfib_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 }));
Neale Rannsbcc6aa42017-02-24 01:34:14 -0800350 hash_free(msrc->mfes_itfs);
351 msrc->mfes_itfs = NULL;
Neale Rannsa9374df2017-02-02 02:18:18 -0800352 fib_path_list_unlock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000353}
354
355static void
356mfib_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
Neale Ranns32e1c012016-11-22 17:07:28 +0000372u32
373mfib_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
383void
384mfib_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
392static mfib_entry_t *
393mfib_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
Marco Varlese47501da2017-08-29 14:04:06 +0200399 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
Neale Ranns32e1c012016-11-22 17:07:28 +0000400
401 fib_node_init(&mfib_entry->mfe_node,
402 FIB_NODE_TYPE_MFIB_ENTRY);
403
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800404 /*
405 * Some of the members require non-default initialisation
Dave Barachb7b92992018-10-17 10:38:51 -0400406 * so we also init those that don't and thus save on the call to clib_memset.
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800407 */
408 mfib_entry->mfe_flags = 0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000409 mfib_entry->mfe_fib_index = fib_index;
410 mfib_entry->mfe_prefix = *prefix;
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800411 mfib_entry->mfe_srcs = NULL;
412 mfib_entry->mfe_itfs = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800413 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
Neale Rannsc2aad532017-05-30 09:53:52 -0700414 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
Neale Ranns32e1c012016-11-22 17:07:28 +0000415
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
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800425static inline mfib_path_ext_t *
426mfib_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
441static mfib_path_ext_t*
442mfib_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
459static void
460mfib_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
Neale Ranns32e1c012016-11-22 17:07:28 +0000471typedef struct mfib_entry_collect_forwarding_ctx_t_
472{
473 load_balance_path_t * next_hops;
474 fib_forward_chain_type_t fct;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800475 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000476} mfib_entry_collect_forwarding_ctx_t;
477
Neale Ranns81424992017-05-18 03:03:22 -0700478static fib_path_list_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000479mfib_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 {
Neale Ranns81424992017-05-18 03:03:22 -0700493 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000494 }
495
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800496 /*
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 {
Neale Ranns81424992017-05-18 03:03:22 -0700507 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800508 }
509
Neale Ranns32e1c012016-11-22 17:07:28 +0000510 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:
Florin Corasce1b4c72017-01-26 14:25:34 -0800529 case FIB_FORW_CHAIN_TYPE_NSH:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700530 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns32e1c012016-11-22 17:07:28 +0000531 ASSERT(0);
532 break;
533 }
534
Neale Ranns81424992017-05-18 03:03:22 -0700535 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000536}
537
538static void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800539mfib_entry_stack (mfib_entry_t *mfib_entry,
540 mfib_entry_src_t *msrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000541{
Neale Ranns32e1c012016-11-22 17:07:28 +0000542 dpo_proto_t dp;
543
544 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
545
Neale Rannsc2aad532017-05-30 09:53:52 -0700546 /*
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
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800555 if (NULL != msrc &&
556 FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
Neale Ranns32e1c012016-11-22 17:07:28 +0000557 {
Neale Rannsa9374df2017-02-02 02:18:18 -0800558 mfib_entry_collect_forwarding_ctx_t ctx = {
559 .next_hops = NULL,
560 .fct = mfib_entry_get_default_chain_type(mfib_entry),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800561 .msrc = msrc,
Neale Rannsa9374df2017-02-02 02:18:18 -0800562 };
563
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800564 fib_path_list_walk(msrc->mfes_pl,
Neale Ranns32e1c012016-11-22 17:07:28 +0000565 mfib_entry_src_collect_forwarding,
566 &ctx);
567
Neale Rannsa9374df2017-02-02 02:18:18 -0800568 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
Neale Ranns32e1c012016-11-22 17:07:28 +0000569 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800570 if (NULL == ctx.next_hops)
Neale Rannsa9374df2017-02-02 02:18:18 -0800571 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800572 /*
573 * no next-hops, stack directly on the drop
574 */
Neale Rannsa9374df2017-02-02 02:18:18 -0800575 dpo_stack(DPO_MFIB_ENTRY, dp,
576 &mfib_entry->mfe_rep,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800577 drop_dpo_get(dp));
Neale Rannsa9374df2017-02-02 02:18:18 -0800578 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800579 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 }
Neale Rannsa9374df2017-02-02 02:18:18 -0800603 }
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));
Neale Ranns32e1c012016-11-22 17:07:28 +0000612
613 dpo_stack(DPO_MFIB_ENTRY, dp,
614 &mfib_entry->mfe_rep,
Neale Rannsa9374df2017-02-02 02:18:18 -0800615 &ctx.next_hops[0].path_dpo);
616 dpo_reset(&ctx.next_hops[0].path_dpo);
617 vec_free(ctx.next_hops);
Neale Ranns32e1c012016-11-22 17:07:28 +0000618 }
Neale Rannsc2aad532017-05-30 09:53:52 -0700619
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));
Neale Ranns32e1c012016-11-22 17:07:28 +0000630 }
631 else
632 {
633 dpo_stack(DPO_MFIB_ENTRY, dp,
634 &mfib_entry->mfe_rep,
635 drop_dpo_get(dp));
636 }
637}
638
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800639static fib_node_index_t
640mfib_entry_src_path_add (mfib_entry_src_t *msrc,
641 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000642{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800643 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000644 fib_route_path_t *rpaths;
645
Neale Rannsa9374df2017-02-02 02:18:18 -0800646 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
647
Neale Ranns32e1c012016-11-22 17:07:28 +0000648 /*
649 * path-lists require a vector of paths
650 */
651 rpaths = NULL;
652 vec_add1(rpaths, rpath[0]);
653
Neale Ranns32e1c012016-11-22 17:07:28 +0000654 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
655 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800656 /* 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);
Neale Ranns32e1c012016-11-22 17:07:28 +0000660 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800661
662 path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000663
664 vec_free(rpaths);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800665
666 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000667}
668
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800669static fib_node_index_t
670mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
671 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000672{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800673 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000674 fib_route_path_t *rpaths;
675
Neale Rannsa9374df2017-02-02 02:18:18 -0800676 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
677
Neale Ranns32e1c012016-11-22 17:07:28 +0000678 /*
679 * path-lists require a vector of paths
680 */
681 rpaths = NULL;
682 vec_add1(rpaths, rpath[0]);
683
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800684 path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000685
686 vec_free(rpaths);
687
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800688 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000689}
690
691static void
692mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry)
693{
Neale Ranns32e1c012016-11-22 17:07:28 +0000694 mfib_entry_src_t *bsrc;
695
Neale Ranns32e1c012016-11-22 17:07:28 +0000696 /*
697 * copy the forwarding data from the bast source
698 */
699 bsrc = mfib_entry_get_best_src(mfib_entry);
700
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800701 if (NULL != bsrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000702 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000703 mfib_entry->mfe_flags = bsrc->mfes_flags;
704 mfib_entry->mfe_itfs = bsrc->mfes_itfs;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800705 mfib_entry->mfe_rpf_id = bsrc->mfes_rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000706 }
707
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800708 mfib_entry_stack(mfib_entry, bsrc);
Neale Ranns32e1c012016-11-22 17:07:28 +0000709}
710
711
712fib_node_index_t
713mfib_entry_create (u32 fib_index,
714 mfib_source_t source,
715 const mfib_prefix_t *prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800716 fib_rpf_id_t rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +0000717 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;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800727 msrc->mfes_rpf_id = rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000728
729 mfib_entry_recalculate_forwarding(mfib_entry);
730
731 return (mfib_entry_index);
732}
733
734static int
735mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
736{
737 return (0 == vec_len(mfib_entry->mfe_srcs));
738}
739
740static int
741mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
742{
743 return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800744 0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
Neale Ranns32e1c012016-11-22 17:07:28 +0000745}
746
747int
748mfib_entry_update (fib_node_index_t mfib_entry_index,
749 mfib_source_t source,
Neale Rannsa9374df2017-02-02 02:18:18 -0800750 mfib_entry_flags_t entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800751 fib_rpf_id_t rpf_id,
Neale Rannsa9374df2017-02-02 02:18:18 -0800752 index_t repi)
Neale Ranns32e1c012016-11-22 17:07:28 +0000753{
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;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800760 msrc->mfes_rpf_id = rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000761
Neale Rannsa9374df2017-02-02 02:18:18 -0800762 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;
Neale Rannsda78f952017-05-24 09:15:43 -0700771 dpo_proto_t dp;
Neale Rannsa9374df2017-02-02 02:18:18 -0800772 dpo_id_t dpo = DPO_INVALID;
773
Neale Rannsda78f952017-05-24 09:15:43 -0700774 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
Neale Rannsa9374df2017-02-02 02:18:18 -0800775 old_pl_index = msrc->mfes_pl;
776
Neale Rannsda78f952017-05-24 09:15:43 -0700777 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
Neale Rannsa9374df2017-02-02 02:18:18 -0800778
779 msrc->mfes_pl =
Neale Rannsda78f952017-05-24 09:15:43 -0700780 fib_path_list_create_special(dp,
Neale Rannsa9374df2017-02-02 02:18:18 -0800781 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
Neale Ranns32e1c012016-11-22 17:07:28 +0000789 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
803static void
804mfib_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
811static void
812mfib_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
824void
825mfib_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{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800830 fib_node_index_t path_index;
831 mfib_path_ext_t *path_ext;
Neale Ranns32e1c012016-11-22 17:07:28 +0000832 mfib_entry_t *mfib_entry;
833 mfib_entry_src_t *msrc;
Neale Rannse821ab12017-06-01 07:45:05 -0700834 mfib_itf_flags_t old;
Neale Ranns32e1c012016-11-22 17:07:28 +0000835
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 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800841 * add the path to the path-list. If it's a duplicate we'll get
842 * back the original path.
Neale Ranns32e1c012016-11-22 17:07:28 +0000843 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800844 path_index = mfib_entry_src_path_add(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000845
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800846 /*
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)
Neale Ranns32e1c012016-11-22 17:07:28 +0000852 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800853 old = MFIB_ITF_FLAG_NONE;
854 path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000855 }
856 else
857 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800858 old = path_ext->mfpe_flags;
859 path_ext->mfpe_flags = itf_flags;
860 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000861
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800862 /*
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
Neale Rannse821ab12017-06-01 07:45:05 -0700870 if (old != itf_flags)
Neale Ranns32e1c012016-11-22 17:07:28 +0000871 {
Neale Rannse821ab12017-06-01 07:45:05 -0700872 /*
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)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800879 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800880 mfib_entry_itf_add(msrc,
881 rpath[0].frp_sw_if_index,
Neale Rannse821ab12017-06-01 07:45:05 -0700882 mfib_itf_create(path_index, itf_flags));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800883 }
884 else
885 {
Neale Rannse821ab12017-06-01 07:45:05 -0700886 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 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800896 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000897 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000898 }
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 */
909int
910mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
911 mfib_source_t source,
912 const fib_route_path_t *rpath)
913{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800914 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000915 mfib_entry_t *mfib_entry;
916 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000917
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 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800931 * remove the path from the path-list. If it's not there we'll get
932 * back invalid
Neale Ranns32e1c012016-11-22 17:07:28 +0000933 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800934 path_index = mfib_entry_src_path_remove(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000935
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800936 if (FIB_NODE_INDEX_INVALID != path_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000937 {
938 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800939 * don't need the extension, nor the interface anymore
Neale Ranns32e1c012016-11-22 17:07:28 +0000940 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800941 mfib_path_ext_remove(msrc, path_index);
942 if (~0 != rpath[0].frp_sw_if_index)
943 {
Neale Rannse821ab12017-06-01 07:45:05 -0700944 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 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800959 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000960 }
961
Neale Ranns32e1c012016-11-22 17:07:28 +0000962 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 */
981int
982mfib_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
995static int
996fib_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
1009static int
1010fib_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
1024static int
1025mfib_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
1068int
1069mfib_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
Neale Rannsc2aad532017-05-30 09:53:52 -07001077static void
1078mfib_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
Neale Ranns28c142e2018-09-07 09:37:07 -07001100u32
1101mfib_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
Neale Rannsc2aad532017-05-30 09:53:52 -07001110/*
1111 * mfib_entry_back_walk_notify
1112 *
1113 * A back walk has reach this entry.
1114 */
1115static fib_node_back_walk_rc_t
1116mfib_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
1124static void
1125mfib_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 */
1136static 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
Neale Ranns32e1c012016-11-22 17:07:28 +00001143void
1144mfib_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
1153void
1154mfib_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
1163static void
1164mfib_entry_dpo_lock (dpo_id_t *dpo)
1165{
1166}
1167static void
1168mfib_entry_dpo_unlock (dpo_id_t *dpo)
1169{
1170}
1171
1172const 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
1179const static char* const mfib_entry_ip4_nodes[] =
1180{
1181 "ip4-mfib-forward-rpf",
1182 NULL,
1183};
1184const static char* const mfib_entry_ip6_nodes[] =
1185{
1186 "ip6-mfib-forward-rpf",
1187 NULL,
1188};
1189
1190const 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
1196void
1197mfib_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);
Neale Ranns710071b2018-09-24 12:36:26 +00001201 mfib_entry_logger = vlib_log_register_class("mfib", "entry");
Neale Ranns32e1c012016-11-22 17:07:28 +00001202}
1203
1204void
1205mfib_entry_encode (fib_node_index_t mfib_entry_index,
1206 fib_route_path_encode_t **api_rpaths)
1207{
1208 mfib_entry_t *mfib_entry;
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001209 mfib_entry_src_t *bsrc;
Neale Ranns32e1c012016-11-22 17:07:28 +00001210
1211 mfib_entry = mfib_entry_get(mfib_entry_index);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001212 bsrc = mfib_entry_get_best_src(mfib_entry);
1213
1214 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
Neale Ranns5a8123b2017-01-26 01:18:23 -08001215 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001216 fib_path_list_walk(bsrc->mfes_pl,
Neale Ranns5a8123b2017-01-26 01:18:23 -08001217 fib_path_encode,
1218 api_rpaths);
1219 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001220}
1221
Neale Ranns5a8123b2017-01-26 01:18:23 -08001222
Neale Ranns32e1c012016-11-22 17:07:28 +00001223void
1224mfib_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
1233u32
1234mfib_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
Neale Rannsff233892017-12-12 00:21:19 -08001243const dpo_id_t*
1244mfib_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
Neale Ranns32e1c012016-11-22 17:07:28 +00001253void
1254mfib_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
1280u32
1281mfib_entry_pool_size (void)
1282{
1283 return (pool_elts(mfib_entry_pool));
1284}
1285
1286static clib_error_t *
1287show_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
Neale Rannsc756c1c2017-02-08 09:11:57 -08001328/*?
1329 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1330 * numerical indentifier.
1331 ?*/
Neale Ranns32e1c012016-11-22 17:07:28 +00001332VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1333 .path = "show mfib entry",
1334 .function = show_mfib_entry_command,
1335 .short_help = "show mfib entry",
1336};