blob: a88f375ec19f699294c418a58bcf7ddc6b6f7b61 [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 Rannse821ab12017-06-01 07:45:05 -0700220 if (MFIB_RPF_ID_NONE != mfib_entry->mfe_rpf_id)
221 {
222 s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id);
223 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000224 s = format(s, "\n %U-chain\n %U",
225 format_fib_forw_chain_type,
226 mfib_entry_get_default_chain_type(mfib_entry),
227 format_dpo_id,
228 &mfib_entry->mfe_rep,
229 2);
230 s = format(s, "\n");
231
232 if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
233 {
234 s = format(s, "\nchildren:");
235 s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
236 }
237
238 return (s);
239}
240
241static mfib_entry_t*
242mfib_entry_from_fib_node (fib_node_t *node)
243{
Neale Ranns32e1c012016-11-22 17:07:28 +0000244 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
Neale Ranns32e1c012016-11-22 17:07:28 +0000245 return ((mfib_entry_t*)node);
246}
247
248static int
249mfib_entry_src_cmp_for_sort (void * v1,
250 void * v2)
251{
252 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
253
254 return (esrc1->mfes_src - esrc2->mfes_src);
255}
256
257static void
258mfib_entry_src_init (mfib_entry_t *mfib_entry,
259 mfib_source_t source)
260
261{
262 mfib_entry_src_t esrc = {
263 .mfes_pl = FIB_NODE_INDEX_INVALID,
264 .mfes_flags = MFIB_ENTRY_FLAG_NONE,
265 .mfes_src = source,
266 };
267
268 vec_add1(mfib_entry->mfe_srcs, esrc);
269 vec_sort_with_function(mfib_entry->mfe_srcs,
270 mfib_entry_src_cmp_for_sort);
271}
272
273static mfib_entry_src_t *
274mfib_entry_src_find (const mfib_entry_t *mfib_entry,
275 mfib_source_t source,
276 u32 *index)
277
278{
279 mfib_entry_src_t *esrc;
280 int ii;
281
282 ii = 0;
283 vec_foreach(esrc, mfib_entry->mfe_srcs)
284 {
285 if (esrc->mfes_src == source)
286 {
287 if (NULL != index)
288 {
289 *index = ii;
290 }
291 return (esrc);
292 }
293 else
294 {
295 ii++;
296 }
297 }
298
299 return (NULL);
300}
301
302static mfib_entry_src_t *
303mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700304 mfib_source_t source)
Neale Ranns32e1c012016-11-22 17:07:28 +0000305{
306 mfib_entry_src_t *esrc;
307
308 esrc = mfib_entry_src_find(mfib_entry, source, NULL);
309
310 if (NULL == esrc)
311 {
312 mfib_entry_src_init(mfib_entry, source);
313 }
314
315 return (mfib_entry_src_find(mfib_entry, source, NULL));
316}
317
318static mfib_entry_src_t*
319mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
320{
321 mfib_entry_src_t *bsrc;
322
323 /*
324 * the enum of sources is deliberately arranged in priority order
325 */
326 if (0 == vec_len(mfib_entry->mfe_srcs))
327 {
328 bsrc = NULL;
329 }
330 else
331 {
332 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
333 }
334
335 return (bsrc);
336}
337
Neale Ranns15002542017-09-10 04:39:11 -0700338int
339mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
340 mfib_source_t source)
341{
342 mfib_entry_t *mfib_entry;
343
344 mfib_entry = mfib_entry_get(mfib_entry_index);
345
346 return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
347}
348
Neale Ranns32e1c012016-11-22 17:07:28 +0000349static void
350mfib_entry_src_flush (mfib_entry_src_t *msrc)
351{
352 u32 sw_if_index;
353 index_t mfii;
354
355 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
356 ({
357 mfib_itf_delete(mfib_itf_get(mfii));
358 }));
Neale Rannsbcc6aa42017-02-24 01:34:14 -0800359 hash_free(msrc->mfes_itfs);
360 msrc->mfes_itfs = NULL;
Neale Rannsa9374df2017-02-02 02:18:18 -0800361 fib_path_list_unlock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000362}
363
364static void
365mfib_entry_src_remove (mfib_entry_t *mfib_entry,
366 mfib_source_t source)
367
368{
369 mfib_entry_src_t *msrc;
370 u32 index = ~0;
371
372 msrc = mfib_entry_src_find(mfib_entry, source, &index);
373
374 if (NULL != msrc)
375 {
376 mfib_entry_src_flush(msrc);
377 vec_del1(mfib_entry->mfe_srcs, index);
378 }
379}
380
Neale Ranns32e1c012016-11-22 17:07:28 +0000381u32
382mfib_entry_child_add (fib_node_index_t mfib_entry_index,
383 fib_node_type_t child_type,
384 fib_node_index_t child_index)
385{
386 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
387 mfib_entry_index,
388 child_type,
389 child_index));
390};
391
392void
393mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
394 u32 sibling_index)
395{
396 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
397 mfib_entry_index,
398 sibling_index);
399}
400
401static mfib_entry_t *
402mfib_entry_alloc (u32 fib_index,
403 const mfib_prefix_t *prefix,
404 fib_node_index_t *mfib_entry_index)
405{
406 mfib_entry_t *mfib_entry;
407
Marco Varlese47501da2017-08-29 14:04:06 +0200408 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
Neale Ranns32e1c012016-11-22 17:07:28 +0000409
410 fib_node_init(&mfib_entry->mfe_node,
411 FIB_NODE_TYPE_MFIB_ENTRY);
412
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800413 /*
414 * Some of the members require non-default initialisation
415 * so we also init those that don't and thus save on the call to memset.
416 */
417 mfib_entry->mfe_flags = 0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000418 mfib_entry->mfe_fib_index = fib_index;
419 mfib_entry->mfe_prefix = *prefix;
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800420 mfib_entry->mfe_srcs = NULL;
421 mfib_entry->mfe_itfs = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800422 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
Neale Rannsc2aad532017-05-30 09:53:52 -0700423 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
Neale Ranns32e1c012016-11-22 17:07:28 +0000424
425 dpo_reset(&mfib_entry->mfe_rep);
426
427 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
428
429 MFIB_ENTRY_DBG(mfib_entry, "alloc");
430
431 return (mfib_entry);
432}
433
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800434static inline mfib_path_ext_t *
435mfib_entry_path_ext_find (mfib_path_ext_t *exts,
436 fib_node_index_t path_index)
437{
438 uword *p;
439
440 p = hash_get(exts, path_index);
441
442 if (NULL != p)
443 {
444 return (mfib_entry_path_ext_get(p[0]));
445 }
446
447 return (NULL);
448}
449
450static mfib_path_ext_t*
451mfib_path_ext_add (mfib_entry_src_t *msrc,
452 fib_node_index_t path_index,
453 mfib_itf_flags_t mfi_flags)
454{
455 mfib_path_ext_t *path_ext;
456
457 pool_get(mfib_path_ext_pool, path_ext);
458
459 path_ext->mfpe_flags = mfi_flags;
460 path_ext->mfpe_path = path_index;
461
462 hash_set(msrc->mfes_exts, path_index,
463 path_ext - mfib_path_ext_pool);
464
465 return (path_ext);
466}
467
468static void
469mfib_path_ext_remove (mfib_entry_src_t *msrc,
470 fib_node_index_t path_index)
471{
472 mfib_path_ext_t *path_ext;
473
474 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
475
476 hash_unset(msrc->mfes_exts, path_index);
477 pool_put(mfib_path_ext_pool, path_ext);
478}
479
Neale Ranns32e1c012016-11-22 17:07:28 +0000480typedef struct mfib_entry_collect_forwarding_ctx_t_
481{
482 load_balance_path_t * next_hops;
483 fib_forward_chain_type_t fct;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800484 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000485} mfib_entry_collect_forwarding_ctx_t;
486
Neale Ranns81424992017-05-18 03:03:22 -0700487static fib_path_list_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000488mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
489 fib_node_index_t path_index,
490 void *arg)
491{
492 mfib_entry_collect_forwarding_ctx_t *ctx;
493 load_balance_path_t *nh;
494
495 ctx = arg;
496
497 /*
498 * if the path is not resolved, don't include it.
499 */
500 if (!fib_path_is_resolved(path_index))
501 {
Neale Ranns81424992017-05-18 03:03:22 -0700502 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000503 }
504
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800505 /*
506 * If the path is not forwarding to use it
507 */
508 mfib_path_ext_t *path_ext;
509
510 path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
511 path_index);
512
513 if (NULL != path_ext &&
514 !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
515 {
Neale Ranns81424992017-05-18 03:03:22 -0700516 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800517 }
518
Neale Ranns32e1c012016-11-22 17:07:28 +0000519 switch (ctx->fct)
520 {
521 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
522 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
523 /*
524 * EOS traffic with no label to stack, we need the IP Adj
525 */
526 vec_add2(ctx->next_hops, nh, 1);
527
528 nh->path_index = path_index;
529 nh->path_weight = fib_path_get_weight(path_index);
530 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
531 break;
532
533 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
534 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
535 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
536 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
537 case FIB_FORW_CHAIN_TYPE_ETHERNET:
Florin Corasce1b4c72017-01-26 14:25:34 -0800538 case FIB_FORW_CHAIN_TYPE_NSH:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700539 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns32e1c012016-11-22 17:07:28 +0000540 ASSERT(0);
541 break;
542 }
543
Neale Ranns81424992017-05-18 03:03:22 -0700544 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000545}
546
547static void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800548mfib_entry_stack (mfib_entry_t *mfib_entry,
549 mfib_entry_src_t *msrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000550{
Neale Ranns32e1c012016-11-22 17:07:28 +0000551 dpo_proto_t dp;
552
553 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
554
Neale Rannsc2aad532017-05-30 09:53:52 -0700555 /*
556 * unlink the enty from the previous path list.
557 */
558 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
559 {
560 fib_path_list_child_remove(mfib_entry->mfe_pl,
561 mfib_entry->mfe_sibling);
562 }
563
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800564 if (NULL != msrc &&
565 FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
Neale Ranns32e1c012016-11-22 17:07:28 +0000566 {
Neale Rannsa9374df2017-02-02 02:18:18 -0800567 mfib_entry_collect_forwarding_ctx_t ctx = {
568 .next_hops = NULL,
569 .fct = mfib_entry_get_default_chain_type(mfib_entry),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800570 .msrc = msrc,
Neale Rannsa9374df2017-02-02 02:18:18 -0800571 };
572
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800573 fib_path_list_walk(msrc->mfes_pl,
Neale Ranns32e1c012016-11-22 17:07:28 +0000574 mfib_entry_src_collect_forwarding,
575 &ctx);
576
Neale Rannsa9374df2017-02-02 02:18:18 -0800577 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
Neale Ranns32e1c012016-11-22 17:07:28 +0000578 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800579 if (NULL == ctx.next_hops)
Neale Rannsa9374df2017-02-02 02:18:18 -0800580 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800581 /*
582 * no next-hops, stack directly on the drop
583 */
Neale Rannsa9374df2017-02-02 02:18:18 -0800584 dpo_stack(DPO_MFIB_ENTRY, dp,
585 &mfib_entry->mfe_rep,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800586 drop_dpo_get(dp));
Neale Rannsa9374df2017-02-02 02:18:18 -0800587 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800588 else
589 {
590 /*
591 * each path contirbutes a next-hop. form a replicate
592 * from those choices.
593 */
594 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
595 dpo_is_drop(&mfib_entry->mfe_rep))
596 {
597 dpo_id_t tmp_dpo = DPO_INVALID;
598
599 dpo_set(&tmp_dpo,
600 DPO_REPLICATE, dp,
601 replicate_create(0, dp));
602
603 dpo_stack(DPO_MFIB_ENTRY, dp,
604 &mfib_entry->mfe_rep,
605 &tmp_dpo);
606
607 dpo_reset(&tmp_dpo);
608 }
609 replicate_multipath_update(&mfib_entry->mfe_rep,
610 ctx.next_hops);
611 }
Neale Rannsa9374df2017-02-02 02:18:18 -0800612 }
613 else
614 {
615 /*
616 * for exclusive routes the source provided a replicate DPO
617 * we we stashed inthe special path list with one path
618 * so we can stack directly on that.
619 */
620 ASSERT(1 == vec_len(ctx.next_hops));
Neale Ranns32e1c012016-11-22 17:07:28 +0000621
622 dpo_stack(DPO_MFIB_ENTRY, dp,
623 &mfib_entry->mfe_rep,
Neale Rannsa9374df2017-02-02 02:18:18 -0800624 &ctx.next_hops[0].path_dpo);
625 dpo_reset(&ctx.next_hops[0].path_dpo);
626 vec_free(ctx.next_hops);
Neale Ranns32e1c012016-11-22 17:07:28 +0000627 }
Neale Rannsc2aad532017-05-30 09:53:52 -0700628
629 /*
630 * link the entry to the path-list.
631 * The entry needs to be a child so that we receive the back-walk
632 * updates to recalculate forwarding.
633 */
634 mfib_entry->mfe_pl = msrc->mfes_pl;
635 mfib_entry->mfe_sibling =
636 fib_path_list_child_add(mfib_entry->mfe_pl,
637 FIB_NODE_TYPE_MFIB_ENTRY,
638 mfib_entry_get_index(mfib_entry));
Neale Ranns32e1c012016-11-22 17:07:28 +0000639 }
640 else
641 {
642 dpo_stack(DPO_MFIB_ENTRY, dp,
643 &mfib_entry->mfe_rep,
644 drop_dpo_get(dp));
645 }
646}
647
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800648static fib_node_index_t
649mfib_entry_src_path_add (mfib_entry_src_t *msrc,
650 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000651{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800652 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000653 fib_route_path_t *rpaths;
654
Neale Rannsa9374df2017-02-02 02:18:18 -0800655 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
656
Neale Ranns32e1c012016-11-22 17:07:28 +0000657 /*
658 * path-lists require a vector of paths
659 */
660 rpaths = NULL;
661 vec_add1(rpaths, rpath[0]);
662
Neale Ranns32e1c012016-11-22 17:07:28 +0000663 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
664 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800665 /* A non-shared path-list */
666 msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
667 NULL);
668 fib_path_list_lock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000669 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800670
671 path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000672
673 vec_free(rpaths);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800674
675 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000676}
677
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800678static fib_node_index_t
679mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
680 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000681{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800682 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000683 fib_route_path_t *rpaths;
684
Neale Rannsa9374df2017-02-02 02:18:18 -0800685 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
686
Neale Ranns32e1c012016-11-22 17:07:28 +0000687 /*
688 * path-lists require a vector of paths
689 */
690 rpaths = NULL;
691 vec_add1(rpaths, rpath[0]);
692
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800693 path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000694
695 vec_free(rpaths);
696
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800697 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000698}
699
700static void
701mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry)
702{
Neale Ranns32e1c012016-11-22 17:07:28 +0000703 mfib_entry_src_t *bsrc;
704
Neale Ranns32e1c012016-11-22 17:07:28 +0000705 /*
706 * copy the forwarding data from the bast source
707 */
708 bsrc = mfib_entry_get_best_src(mfib_entry);
709
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800710 if (NULL != bsrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000711 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000712 mfib_entry->mfe_flags = bsrc->mfes_flags;
713 mfib_entry->mfe_itfs = bsrc->mfes_itfs;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800714 mfib_entry->mfe_rpf_id = bsrc->mfes_rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000715 }
716
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800717 mfib_entry_stack(mfib_entry, bsrc);
Neale Ranns32e1c012016-11-22 17:07:28 +0000718}
719
720
721fib_node_index_t
722mfib_entry_create (u32 fib_index,
723 mfib_source_t source,
724 const mfib_prefix_t *prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800725 fib_rpf_id_t rpf_id,
Neale Ranns32e1c012016-11-22 17:07:28 +0000726 mfib_entry_flags_t entry_flags)
727{
728 fib_node_index_t mfib_entry_index;
729 mfib_entry_t *mfib_entry;
730 mfib_entry_src_t *msrc;
731
732 mfib_entry = mfib_entry_alloc(fib_index, prefix,
733 &mfib_entry_index);
734 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
735 msrc->mfes_flags = entry_flags;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800736 msrc->mfes_rpf_id = rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000737
738 mfib_entry_recalculate_forwarding(mfib_entry);
739
740 return (mfib_entry_index);
741}
742
743static int
744mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
745{
746 return (0 == vec_len(mfib_entry->mfe_srcs));
747}
748
749static int
750mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
751{
752 return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800753 0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
Neale Ranns32e1c012016-11-22 17:07:28 +0000754}
755
756int
757mfib_entry_update (fib_node_index_t mfib_entry_index,
758 mfib_source_t source,
Neale Rannsa9374df2017-02-02 02:18:18 -0800759 mfib_entry_flags_t entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800760 fib_rpf_id_t rpf_id,
Neale Rannsa9374df2017-02-02 02:18:18 -0800761 index_t repi)
Neale Ranns32e1c012016-11-22 17:07:28 +0000762{
763 mfib_entry_t *mfib_entry;
764 mfib_entry_src_t *msrc;
765
766 mfib_entry = mfib_entry_get(mfib_entry_index);
767 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
768 msrc->mfes_flags = entry_flags;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800769 msrc->mfes_rpf_id = rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000770
Neale Rannsa9374df2017-02-02 02:18:18 -0800771 if (INDEX_INVALID != repi)
772 {
773 /*
774 * The source is providing its own replicate DPO.
775 * Create a sepcial path-list to manage it, that way
776 * this entry and the source are equivalent to a normal
777 * entry
778 */
779 fib_node_index_t old_pl_index;
Neale Rannsda78f952017-05-24 09:15:43 -0700780 dpo_proto_t dp;
Neale Rannsa9374df2017-02-02 02:18:18 -0800781 dpo_id_t dpo = DPO_INVALID;
782
Neale Rannsda78f952017-05-24 09:15:43 -0700783 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
Neale Rannsa9374df2017-02-02 02:18:18 -0800784 old_pl_index = msrc->mfes_pl;
785
Neale Rannsda78f952017-05-24 09:15:43 -0700786 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
Neale Rannsa9374df2017-02-02 02:18:18 -0800787
788 msrc->mfes_pl =
Neale Rannsda78f952017-05-24 09:15:43 -0700789 fib_path_list_create_special(dp,
Neale Rannsa9374df2017-02-02 02:18:18 -0800790 FIB_PATH_LIST_FLAG_EXCLUSIVE,
791 &dpo);
792
793 dpo_reset(&dpo);
794 fib_path_list_lock(msrc->mfes_pl);
795 fib_path_list_unlock(old_pl_index);
796 }
797
Neale Ranns32e1c012016-11-22 17:07:28 +0000798 if (mfib_entry_src_ok_for_delete(msrc))
799 {
800 /*
801 * this source has no interfaces and no flags.
802 * it has nothing left to give - remove it
803 */
804 mfib_entry_src_remove(mfib_entry, source);
805 }
806
807 mfib_entry_recalculate_forwarding(mfib_entry);
808
809 return (mfib_entry_ok_for_delete(mfib_entry));
810}
811
812static void
813mfib_entry_itf_add (mfib_entry_src_t *msrc,
814 u32 sw_if_index,
815 index_t mi)
816{
817 hash_set(msrc->mfes_itfs, sw_if_index, mi);
818}
819
820static void
821mfib_entry_itf_remove (mfib_entry_src_t *msrc,
822 u32 sw_if_index)
823{
824 mfib_itf_t *mfi;
825
826 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
827
828 mfib_itf_delete(mfi);
829
830 hash_unset(msrc->mfes_itfs, sw_if_index);
831}
832
833void
834mfib_entry_path_update (fib_node_index_t mfib_entry_index,
835 mfib_source_t source,
836 const fib_route_path_t *rpath,
837 mfib_itf_flags_t itf_flags)
838{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800839 fib_node_index_t path_index;
840 mfib_path_ext_t *path_ext;
Neale Ranns32e1c012016-11-22 17:07:28 +0000841 mfib_entry_t *mfib_entry;
842 mfib_entry_src_t *msrc;
Neale Rannse821ab12017-06-01 07:45:05 -0700843 mfib_itf_flags_t old;
Neale Ranns32e1c012016-11-22 17:07:28 +0000844
845 mfib_entry = mfib_entry_get(mfib_entry_index);
846 ASSERT(NULL != mfib_entry);
847 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
848
849 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800850 * add the path to the path-list. If it's a duplicate we'll get
851 * back the original path.
Neale Ranns32e1c012016-11-22 17:07:28 +0000852 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800853 path_index = mfib_entry_src_path_add(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000854
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800855 /*
856 * find the path extension for that path
857 */
858 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
859
860 if (NULL == path_ext)
Neale Ranns32e1c012016-11-22 17:07:28 +0000861 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800862 old = MFIB_ITF_FLAG_NONE;
863 path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000864 }
865 else
866 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800867 old = path_ext->mfpe_flags;
868 path_ext->mfpe_flags = itf_flags;
869 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000870
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800871 /*
872 * Has the path changed its contribution to the input interface set.
873 * Which only paths with interfaces can do...
874 */
875 if (~0 != rpath[0].frp_sw_if_index)
876 {
877 mfib_itf_t *mfib_itf;
878
Neale Rannse821ab12017-06-01 07:45:05 -0700879 if (old != itf_flags)
Neale Ranns32e1c012016-11-22 17:07:28 +0000880 {
Neale Rannse821ab12017-06-01 07:45:05 -0700881 /*
882 * change of flag contributions
883 */
884 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
885 rpath[0].frp_sw_if_index);
886
887 if (NULL == mfib_itf)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800888 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800889 mfib_entry_itf_add(msrc,
890 rpath[0].frp_sw_if_index,
Neale Rannse821ab12017-06-01 07:45:05 -0700891 mfib_itf_create(path_index, itf_flags));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800892 }
893 else
894 {
Neale Rannse821ab12017-06-01 07:45:05 -0700895 if (mfib_itf_update(mfib_itf,
896 path_index,
897 itf_flags))
898 {
899 /*
900 * no more interface flags on this path, remove
901 * from the data-plane set
902 */
903 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
904 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800905 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000906 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000907 }
908
909 mfib_entry_recalculate_forwarding(mfib_entry);
910}
911
912/*
913 * mfib_entry_path_remove
914 *
915 * remove a path from the entry.
916 * return the mfib_entry's index if it is still present, INVALID otherwise.
917 */
918int
919mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
920 mfib_source_t source,
921 const fib_route_path_t *rpath)
922{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800923 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000924 mfib_entry_t *mfib_entry;
925 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000926
927 mfib_entry = mfib_entry_get(mfib_entry_index);
928 ASSERT(NULL != mfib_entry);
929 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
930
931 if (NULL == msrc)
932 {
933 /*
934 * there are no paths left for this source
935 */
936 return (mfib_entry_ok_for_delete(mfib_entry));
937 }
938
939 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800940 * remove the path from the path-list. If it's not there we'll get
941 * back invalid
Neale Ranns32e1c012016-11-22 17:07:28 +0000942 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800943 path_index = mfib_entry_src_path_remove(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000944
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800945 if (FIB_NODE_INDEX_INVALID != path_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000946 {
947 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800948 * don't need the extension, nor the interface anymore
Neale Ranns32e1c012016-11-22 17:07:28 +0000949 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800950 mfib_path_ext_remove(msrc, path_index);
951 if (~0 != rpath[0].frp_sw_if_index)
952 {
Neale Rannse821ab12017-06-01 07:45:05 -0700953 mfib_itf_t *mfib_itf;
954
955 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
956 rpath[0].frp_sw_if_index);
957
958 if (mfib_itf_update(mfib_itf,
959 path_index,
960 MFIB_ITF_FLAG_NONE))
961 {
962 /*
963 * no more interface flags on this path, remove
964 * from the data-plane set
965 */
966 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
967 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800968 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000969 }
970
Neale Ranns32e1c012016-11-22 17:07:28 +0000971 if (mfib_entry_src_ok_for_delete(msrc))
972 {
973 /*
974 * this source has no interfaces and no flags.
975 * it has nothing left to give - remove it
976 */
977 mfib_entry_src_remove(mfib_entry, source);
978 }
979
980 mfib_entry_recalculate_forwarding(mfib_entry);
981
982 return (mfib_entry_ok_for_delete(mfib_entry));
983}
984
985/**
986 * mfib_entry_delete
987 *
988 * The source is withdrawing all the paths it provided
989 */
990int
991mfib_entry_delete (fib_node_index_t mfib_entry_index,
992 mfib_source_t source)
993{
994 mfib_entry_t *mfib_entry;
995
996 mfib_entry = mfib_entry_get(mfib_entry_index);
997 mfib_entry_src_remove(mfib_entry, source);
998
999 mfib_entry_recalculate_forwarding(mfib_entry);
1000
1001 return (mfib_entry_ok_for_delete(mfib_entry));
1002}
1003
1004static int
1005fib_ip4_address_compare (ip4_address_t * a1,
1006 ip4_address_t * a2)
1007{
1008 /*
1009 * IP addresses are unsiged ints. the return value here needs to be signed
1010 * a simple subtraction won't cut it.
1011 * If the addresses are the same, the sort order is undefiend, so phoey.
1012 */
1013 return ((clib_net_to_host_u32(a1->data_u32) >
1014 clib_net_to_host_u32(a2->data_u32) ) ?
1015 1 : -1);
1016}
1017
1018static int
1019fib_ip6_address_compare (ip6_address_t * a1,
1020 ip6_address_t * a2)
1021{
1022 int i;
1023 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1024 {
1025 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1026 clib_net_to_host_u16 (a2->as_u16[i]));
1027 if (cmp != 0)
1028 return cmp;
1029 }
1030 return 0;
1031}
1032
1033static int
1034mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1035 fib_node_index_t mfib_entry_index2)
1036{
1037 mfib_entry_t *mfib_entry1, *mfib_entry2;
1038 int cmp = 0;
1039
1040 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1041 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1042
1043 switch (mfib_entry1->mfe_prefix.fp_proto)
1044 {
1045 case FIB_PROTOCOL_IP4:
1046 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1047 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1048
1049 if (0 == cmp)
1050 {
1051 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1052 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1053 }
1054 break;
1055 case FIB_PROTOCOL_IP6:
1056 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1057 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1058
1059 if (0 == cmp)
1060 {
1061 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1062 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1063 }
1064 break;
1065 case FIB_PROTOCOL_MPLS:
1066 ASSERT(0);
1067 cmp = 0;
1068 break;
1069 }
1070
1071 if (0 == cmp) {
1072 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1073 }
1074 return (cmp);
1075}
1076
1077int
1078mfib_entry_cmp_for_sort (void *i1, void *i2)
1079{
1080 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1081
1082 return (mfib_entry_cmp(*mfib_entry_index1,
1083 *mfib_entry_index2));
1084}
1085
Neale Rannsc2aad532017-05-30 09:53:52 -07001086static void
1087mfib_entry_last_lock_gone (fib_node_t *node)
1088{
1089 mfib_entry_t *mfib_entry;
1090 mfib_entry_src_t *msrc;
1091
1092 mfib_entry = mfib_entry_from_fib_node(node);
1093
1094 dpo_reset(&mfib_entry->mfe_rep);
1095
1096 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1097
1098 vec_foreach(msrc, mfib_entry->mfe_srcs)
1099 {
1100 mfib_entry_src_flush(msrc);
1101 }
1102
1103 vec_free(mfib_entry->mfe_srcs);
1104
1105 fib_node_deinit(&mfib_entry->mfe_node);
1106 pool_put(mfib_entry_pool, mfib_entry);
1107}
1108
1109/*
1110 * mfib_entry_back_walk_notify
1111 *
1112 * A back walk has reach this entry.
1113 */
1114static fib_node_back_walk_rc_t
1115mfib_entry_back_walk_notify (fib_node_t *node,
1116 fib_node_back_walk_ctx_t *ctx)
1117{
1118 mfib_entry_recalculate_forwarding(mfib_entry_from_fib_node(node));
1119
1120 return (FIB_NODE_BACK_WALK_CONTINUE);
1121}
1122
1123static void
1124mfib_entry_show_memory (void)
1125{
1126 fib_show_memory_usage("multicast-Entry",
1127 pool_elts(mfib_entry_pool),
1128 pool_len(mfib_entry_pool),
1129 sizeof(mfib_entry_t));
1130}
1131
1132/*
1133 * The MFIB entry's graph node virtual function table
1134 */
1135static const fib_node_vft_t mfib_entry_vft = {
1136 .fnv_get = mfib_entry_get_node,
1137 .fnv_last_lock = mfib_entry_last_lock_gone,
1138 .fnv_back_walk = mfib_entry_back_walk_notify,
1139 .fnv_mem_show = mfib_entry_show_memory,
1140};
1141
Neale Ranns32e1c012016-11-22 17:07:28 +00001142void
1143mfib_entry_lock (fib_node_index_t mfib_entry_index)
1144{
1145 mfib_entry_t *mfib_entry;
1146
1147 mfib_entry = mfib_entry_get(mfib_entry_index);
1148
1149 fib_node_lock(&mfib_entry->mfe_node);
1150}
1151
1152void
1153mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1154{
1155 mfib_entry_t *mfib_entry;
1156
1157 mfib_entry = mfib_entry_get(mfib_entry_index);
1158
1159 fib_node_unlock(&mfib_entry->mfe_node);
1160}
1161
1162static void
1163mfib_entry_dpo_lock (dpo_id_t *dpo)
1164{
1165}
1166static void
1167mfib_entry_dpo_unlock (dpo_id_t *dpo)
1168{
1169}
1170
1171const static dpo_vft_t mfib_entry_dpo_vft = {
1172 .dv_lock = mfib_entry_dpo_lock,
1173 .dv_unlock = mfib_entry_dpo_unlock,
1174 .dv_format = format_mfib_entry_dpo,
1175 .dv_mem_show = mfib_entry_show_memory,
1176};
1177
1178const static char* const mfib_entry_ip4_nodes[] =
1179{
1180 "ip4-mfib-forward-rpf",
1181 NULL,
1182};
1183const static char* const mfib_entry_ip6_nodes[] =
1184{
1185 "ip6-mfib-forward-rpf",
1186 NULL,
1187};
1188
1189const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1190{
1191 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1192 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1193};
1194
1195void
1196mfib_entry_module_init (void)
1197{
1198 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1199 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
1200}
1201
1202void
1203mfib_entry_encode (fib_node_index_t mfib_entry_index,
1204 fib_route_path_encode_t **api_rpaths)
1205{
1206 mfib_entry_t *mfib_entry;
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001207 mfib_entry_src_t *bsrc;
Neale Ranns32e1c012016-11-22 17:07:28 +00001208
1209 mfib_entry = mfib_entry_get(mfib_entry_index);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001210 bsrc = mfib_entry_get_best_src(mfib_entry);
1211
1212 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
Neale Ranns5a8123b2017-01-26 01:18:23 -08001213 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001214 fib_path_list_walk(bsrc->mfes_pl,
Neale Ranns5a8123b2017-01-26 01:18:23 -08001215 fib_path_encode,
1216 api_rpaths);
1217 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001218}
1219
Neale Ranns5a8123b2017-01-26 01:18:23 -08001220
Neale Ranns32e1c012016-11-22 17:07:28 +00001221void
1222mfib_entry_get_prefix (fib_node_index_t mfib_entry_index,
1223 mfib_prefix_t *pfx)
1224{
1225 mfib_entry_t *mfib_entry;
1226
1227 mfib_entry = mfib_entry_get(mfib_entry_index);
1228 *pfx = mfib_entry->mfe_prefix;
1229}
1230
1231u32
1232mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1233{
1234 mfib_entry_t *mfib_entry;
1235
1236 mfib_entry = mfib_entry_get(mfib_entry_index);
1237
1238 return (mfib_entry->mfe_fib_index);
1239}
1240
Neale Rannsff233892017-12-12 00:21:19 -08001241const dpo_id_t*
1242mfib_entry_contribute_ip_forwarding (fib_node_index_t mfib_entry_index)
1243{
1244 mfib_entry_t *mfib_entry;
1245
1246 mfib_entry = mfib_entry_get(mfib_entry_index);
1247
1248 return (&mfib_entry->mfe_rep);
1249}
1250
Neale Ranns32e1c012016-11-22 17:07:28 +00001251void
1252mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1253 fib_forward_chain_type_t type,
1254 dpo_id_t *dpo)
1255{
1256 /*
1257 * An IP mFIB entry can only provide a forwarding chain that
1258 * is the same IP proto as the prefix.
1259 * No use-cases (i know of) for other combinations.
1260 */
1261 mfib_entry_t *mfib_entry;
1262 dpo_proto_t dp;
1263
1264 mfib_entry = mfib_entry_get(mfib_entry_index);
1265
1266 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1267
1268 if (type == fib_forw_chain_type_from_dpo_proto(dp))
1269 {
1270 dpo_copy(dpo, &mfib_entry->mfe_rep);
1271 }
1272 else
1273 {
1274 dpo_copy(dpo, drop_dpo_get(dp));
1275 }
1276}
1277
1278u32
1279mfib_entry_pool_size (void)
1280{
1281 return (pool_elts(mfib_entry_pool));
1282}
1283
1284static clib_error_t *
1285show_mfib_entry_command (vlib_main_t * vm,
1286 unformat_input_t * input,
1287 vlib_cli_command_t * cmd)
1288{
1289 fib_node_index_t fei;
1290
1291 if (unformat (input, "%d", &fei))
1292 {
1293 /*
1294 * show one in detail
1295 */
1296 if (!pool_is_free_index(mfib_entry_pool, fei))
1297 {
1298 vlib_cli_output (vm, "%d@%U",
1299 fei,
1300 format_mfib_entry, fei,
1301 MFIB_ENTRY_FORMAT_DETAIL2);
1302 }
1303 else
1304 {
1305 vlib_cli_output (vm, "entry %d invalid", fei);
1306 }
1307 }
1308 else
1309 {
1310 /*
1311 * show all
1312 */
1313 vlib_cli_output (vm, "FIB Entries:");
1314 pool_foreach_index(fei, mfib_entry_pool,
1315 ({
1316 vlib_cli_output (vm, "%d@%U",
1317 fei,
1318 format_mfib_entry, fei,
1319 MFIB_ENTRY_FORMAT_BRIEF);
1320 }));
1321 }
1322
1323 return (NULL);
1324}
1325
Neale Rannsc756c1c2017-02-08 09:11:57 -08001326/*?
1327 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1328 * numerical indentifier.
1329 ?*/
Neale Ranns32e1c012016-11-22 17:07:28 +00001330VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1331 .path = "show mfib entry",
1332 .function = show_mfib_entry_command,
1333 .short_help = "show mfib entry",
1334};