blob: 79d2f9a50a30dd1a53e1e31944d0f0f3401481eb [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>
Neale Ranns9e829a82018-12-17 05:50:32 -080019#include <vnet/mfib/mfib_entry_src.h>
20#include <vnet/mfib/mfib_entry_cover.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000021#include <vnet/fib/fib_path_list.h>
Neale Ranns9e829a82018-12-17 05:50:32 -080022#include <vnet/fib/fib_walk.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000023
24#include <vnet/dpo/drop_dpo.h>
25#include <vnet/dpo/replicate_dpo.h>
26
27/**
Neale Ranns710071b2018-09-24 12:36:26 +000028 * the logger
29 */
30vlib_log_class_t mfib_entry_logger;
31
32/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -080033 * Pool of path extensions
34 */
35static mfib_path_ext_t *mfib_path_ext_pool;
36
37/**
Neale Ranns32e1c012016-11-22 17:07:28 +000038 * String names for each source
39 */
40static const char *mfib_source_names[] = MFIB_SOURCE_NAMES;
41
42/*
43 * Pool for all fib_entries
44 */
45mfib_entry_t *mfib_entry_pool;
46
47static fib_node_t *
48mfib_entry_get_node (fib_node_index_t index)
49{
50 return ((fib_node_t*)mfib_entry_get(index));
51}
52
53static fib_protocol_t
54mfib_entry_get_proto (const mfib_entry_t * mfib_entry)
55{
56 return (mfib_entry->mfe_prefix.fp_proto);
57}
58
59fib_forward_chain_type_t
60mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry)
61{
62 switch (mfib_entry->mfe_prefix.fp_proto)
63 {
64 case FIB_PROTOCOL_IP4:
65 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
66 case FIB_PROTOCOL_IP6:
67 return (FIB_FORW_CHAIN_TYPE_MCAST_IP6);
68 case FIB_PROTOCOL_MPLS:
69 ASSERT(0);
70 break;
71 }
72 return (FIB_FORW_CHAIN_TYPE_MCAST_IP4);
73}
74
75static u8 *
76format_mfib_entry_dpo (u8 * s, va_list * args)
77{
78 index_t fei = va_arg(*args, index_t);
79 CLIB_UNUSED(u32 indent) = va_arg(*args, u32);
80
81 return (format(s, "%U",
82 format_mfib_entry, fei,
83 MFIB_ENTRY_FORMAT_BRIEF));
84}
85
Neale Ranns0f26c5a2017-03-01 15:12:11 -080086static inline mfib_path_ext_t *
87mfib_entry_path_ext_get (index_t mi)
88{
89 return (pool_elt_at_index(mfib_path_ext_pool, mi));
90}
91
92static u8 *
93format_mfib_entry_path_ext (u8 * s, va_list * args)
94{
95 mfib_path_ext_t *path_ext;
96 index_t mpi = va_arg(*args, index_t);
97
98 path_ext = mfib_entry_path_ext_get(mpi);
99 return (format(s, "path:%d flags:%U",
100 path_ext->mfpe_path,
101 format_mfib_itf_flags, path_ext->mfpe_flags));
102}
103
Neale Ranns32e1c012016-11-22 17:07:28 +0000104u8 *
105format_mfib_entry (u8 * s, va_list * args)
106{
107 fib_node_index_t fei, mfi;
108 mfib_entry_t *mfib_entry;
109 mfib_entry_src_t *msrc;
110 u32 sw_if_index;
111 int level;
112
113 fei = va_arg (*args, fib_node_index_t);
114 level = va_arg (*args, int);
115 mfib_entry = mfib_entry_get(fei);
116
117 s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix);
118 s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags);
119
120 if (level >= MFIB_ENTRY_FORMAT_DETAIL)
121 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800122 fib_node_index_t path_index, mpi;
123
Neale Ranns32e1c012016-11-22 17:07:28 +0000124 s = format (s, "\n");
125 s = format (s, " fib:%d", mfib_entry->mfe_fib_index);
126 s = format (s, " index:%d", mfib_entry_get_index(mfib_entry));
127 s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks);
128 vec_foreach(msrc, mfib_entry->mfe_srcs)
129 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800130 s = format (s, " src:%s locks:%d:",
131 mfib_source_names[msrc->mfes_src],
132 msrc->mfes_ref_count);
133 if (msrc->mfes_cover != FIB_NODE_INDEX_INVALID)
134 {
135 s = format (s, " cover:%d", msrc->mfes_cover);
136 }
137 s = format (s, " %U\n", format_mfib_entry_flags, msrc->mfes_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000138 if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl)
139 {
140 s = fib_path_list_format(msrc->mfes_pl, s);
141 }
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700142 s = format (s, " Extensions:\n");
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800143 hash_foreach(path_index, mpi, msrc->mfes_exts,
144 ({
145 s = format(s, " %U\n", format_mfib_entry_path_ext, mpi);
146 }));
Neale Ranns5d85f2d2017-05-02 10:17:17 -0700147 s = format (s, " Interface-Forwarding:\n");
Neale Ranns32e1c012016-11-22 17:07:28 +0000148 hash_foreach(sw_if_index, mfi, msrc->mfes_itfs,
149 ({
150 s = format(s, " %U\n", format_mfib_itf, mfi);
151 }));
152 }
153 }
154
155 s = format(s, "\n Interfaces:");
156 hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs,
157 ({
158 s = format(s, "\n %U", format_mfib_itf, mfi);
159 }));
Neale Rannse821ab12017-06-01 07:45:05 -0700160 if (MFIB_RPF_ID_NONE != mfib_entry->mfe_rpf_id)
161 {
162 s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id);
163 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000164 s = format(s, "\n %U-chain\n %U",
165 format_fib_forw_chain_type,
166 mfib_entry_get_default_chain_type(mfib_entry),
167 format_dpo_id,
168 &mfib_entry->mfe_rep,
169 2);
170 s = format(s, "\n");
171
172 if (level >= MFIB_ENTRY_FORMAT_DETAIL2)
173 {
174 s = format(s, "\nchildren:");
175 s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s);
176 }
177
178 return (s);
179}
180
181static mfib_entry_t*
182mfib_entry_from_fib_node (fib_node_t *node)
183{
Neale Ranns32e1c012016-11-22 17:07:28 +0000184 ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type);
Neale Ranns32e1c012016-11-22 17:07:28 +0000185 return ((mfib_entry_t*)node);
186}
187
188static int
189mfib_entry_src_cmp_for_sort (void * v1,
190 void * v2)
191{
192 mfib_entry_src_t *esrc1 = v1, *esrc2 = v2;
193
194 return (esrc1->mfes_src - esrc2->mfes_src);
195}
196
197static void
198mfib_entry_src_init (mfib_entry_t *mfib_entry,
199 mfib_source_t source)
200
201{
202 mfib_entry_src_t esrc = {
203 .mfes_pl = FIB_NODE_INDEX_INVALID,
204 .mfes_flags = MFIB_ENTRY_FLAG_NONE,
205 .mfes_src = source,
Neale Ranns9e829a82018-12-17 05:50:32 -0800206 .mfes_cover = FIB_NODE_INDEX_INVALID,
207 .mfes_sibling = FIB_NODE_INDEX_INVALID,
208 .mfes_ref_count = 1,
Neale Ranns32e1c012016-11-22 17:07:28 +0000209 };
210
211 vec_add1(mfib_entry->mfe_srcs, esrc);
212 vec_sort_with_function(mfib_entry->mfe_srcs,
213 mfib_entry_src_cmp_for_sort);
214}
215
216static mfib_entry_src_t *
217mfib_entry_src_find (const mfib_entry_t *mfib_entry,
218 mfib_source_t source,
219 u32 *index)
220
221{
222 mfib_entry_src_t *esrc;
223 int ii;
224
225 ii = 0;
226 vec_foreach(esrc, mfib_entry->mfe_srcs)
227 {
228 if (esrc->mfes_src == source)
229 {
230 if (NULL != index)
231 {
232 *index = ii;
233 }
234 return (esrc);
235 }
236 else
237 {
238 ii++;
239 }
240 }
241
242 return (NULL);
243}
244
245static mfib_entry_src_t *
246mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700247 mfib_source_t source)
Neale Ranns32e1c012016-11-22 17:07:28 +0000248{
Neale Ranns9e829a82018-12-17 05:50:32 -0800249 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000250
Neale Ranns9e829a82018-12-17 05:50:32 -0800251 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
Neale Ranns32e1c012016-11-22 17:07:28 +0000252
Neale Ranns9e829a82018-12-17 05:50:32 -0800253 if (NULL == msrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000254 {
255 mfib_entry_src_init(mfib_entry, source);
Neale Ranns9e829a82018-12-17 05:50:32 -0800256 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
Neale Ranns32e1c012016-11-22 17:07:28 +0000257 }
258
Neale Ranns9e829a82018-12-17 05:50:32 -0800259 return (msrc);
Neale Ranns32e1c012016-11-22 17:07:28 +0000260}
261
Neale Ranns9e829a82018-12-17 05:50:32 -0800262static mfib_entry_src_t *
263mfib_entry_src_update (mfib_entry_t *mfib_entry,
264 mfib_source_t source,
265 fib_rpf_id_t rpf_id,
266 mfib_entry_flags_t entry_flags)
267{
268 mfib_entry_src_t *msrc;
269
270 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
271
272 msrc->mfes_flags = entry_flags;
273 msrc->mfes_rpf_id = rpf_id;
274
275 return (msrc);
276}
277
278static mfib_entry_src_t *
279mfib_entry_src_update_and_lock (mfib_entry_t *mfib_entry,
280 mfib_source_t source,
281 fib_rpf_id_t rpf_id,
282 mfib_entry_flags_t entry_flags)
283{
284 mfib_entry_src_t *msrc;
285
286 msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
287
288 msrc->mfes_ref_count++;
289
290 return (msrc);
291}
292
293mfib_entry_src_t*
Neale Ranns32e1c012016-11-22 17:07:28 +0000294mfib_entry_get_best_src (const mfib_entry_t *mfib_entry)
295{
296 mfib_entry_src_t *bsrc;
297
298 /*
299 * the enum of sources is deliberately arranged in priority order
300 */
301 if (0 == vec_len(mfib_entry->mfe_srcs))
302 {
303 bsrc = NULL;
304 }
305 else
306 {
307 bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0);
308 }
309
310 return (bsrc);
311}
312
Neale Ranns9e829a82018-12-17 05:50:32 -0800313static mfib_source_t
314mfib_entry_get_best_source (const mfib_entry_t *mfib_entry)
315{
316 mfib_entry_src_t *bsrc;
317
318 bsrc = mfib_entry_get_best_src(mfib_entry);
319
320 return (bsrc->mfes_src);
321}
322
Neale Ranns15002542017-09-10 04:39:11 -0700323int
324mfib_entry_is_sourced (fib_node_index_t mfib_entry_index,
325 mfib_source_t source)
326{
327 mfib_entry_t *mfib_entry;
328
329 mfib_entry = mfib_entry_get(mfib_entry_index);
330
331 return (NULL != mfib_entry_src_find(mfib_entry, source, NULL));
332}
333
Neale Ranns9e829a82018-12-17 05:50:32 -0800334int
335mfib_entry_is_host (fib_node_index_t mfib_entry_index)
336{
337 return (mfib_prefix_is_host(mfib_entry_get_prefix(mfib_entry_index)));
338}
339
340
Neale Ranns32e1c012016-11-22 17:07:28 +0000341static void
342mfib_entry_src_flush (mfib_entry_src_t *msrc)
343{
344 u32 sw_if_index;
345 index_t mfii;
346
347 hash_foreach(sw_if_index, mfii, msrc->mfes_itfs,
348 ({
349 mfib_itf_delete(mfib_itf_get(mfii));
350 }));
Neale Rannsbcc6aa42017-02-24 01:34:14 -0800351 hash_free(msrc->mfes_itfs);
352 msrc->mfes_itfs = NULL;
Neale Rannsa9374df2017-02-02 02:18:18 -0800353 fib_path_list_unlock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000354}
355
356static void
357mfib_entry_src_remove (mfib_entry_t *mfib_entry,
358 mfib_source_t source)
359
360{
361 mfib_entry_src_t *msrc;
362 u32 index = ~0;
363
364 msrc = mfib_entry_src_find(mfib_entry, source, &index);
365
366 if (NULL != msrc)
367 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800368 ASSERT(0 != msrc->mfes_ref_count);
369 msrc->mfes_ref_count--;
370
371 if (0 == msrc->mfes_ref_count)
372 {
373 mfib_entry_src_deactivate(mfib_entry, msrc);
374 mfib_entry_src_flush(msrc);
375
376 vec_del1(mfib_entry->mfe_srcs, index);
377 if (vec_len (mfib_entry->mfe_srcs) > 1)
378 vec_sort_with_function(mfib_entry->mfe_srcs,
379 mfib_entry_src_cmp_for_sort);
380 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000381 }
382}
383
Neale Ranns32e1c012016-11-22 17:07:28 +0000384u32
385mfib_entry_child_add (fib_node_index_t mfib_entry_index,
386 fib_node_type_t child_type,
387 fib_node_index_t child_index)
388{
389 return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY,
390 mfib_entry_index,
391 child_type,
392 child_index));
393};
394
395void
396mfib_entry_child_remove (fib_node_index_t mfib_entry_index,
397 u32 sibling_index)
398{
399 fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY,
400 mfib_entry_index,
401 sibling_index);
402}
403
404static mfib_entry_t *
405mfib_entry_alloc (u32 fib_index,
406 const mfib_prefix_t *prefix,
407 fib_node_index_t *mfib_entry_index)
408{
409 mfib_entry_t *mfib_entry;
410
Marco Varlese47501da2017-08-29 14:04:06 +0200411 pool_get_aligned(mfib_entry_pool, mfib_entry, CLIB_CACHE_LINE_BYTES);
Neale Ranns32e1c012016-11-22 17:07:28 +0000412
413 fib_node_init(&mfib_entry->mfe_node,
414 FIB_NODE_TYPE_MFIB_ENTRY);
415
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800416 /*
417 * Some of the members require non-default initialisation
Dave Barachb7b92992018-10-17 10:38:51 -0400418 * so we also init those that don't and thus save on the call to clib_memset.
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800419 */
420 mfib_entry->mfe_flags = 0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000421 mfib_entry->mfe_fib_index = fib_index;
422 mfib_entry->mfe_prefix = *prefix;
Neale Ranns5a72c1c2017-02-24 08:29:22 -0800423 mfib_entry->mfe_srcs = NULL;
424 mfib_entry->mfe_itfs = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800425 mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE;
Neale Rannsc2aad532017-05-30 09:53:52 -0700426 mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID;
Neale Ranns32e1c012016-11-22 17:07:28 +0000427
428 dpo_reset(&mfib_entry->mfe_rep);
429
430 *mfib_entry_index = mfib_entry_get_index(mfib_entry);
431
432 MFIB_ENTRY_DBG(mfib_entry, "alloc");
433
434 return (mfib_entry);
435}
436
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800437static inline mfib_path_ext_t *
438mfib_entry_path_ext_find (mfib_path_ext_t *exts,
439 fib_node_index_t path_index)
440{
441 uword *p;
442
443 p = hash_get(exts, path_index);
444
445 if (NULL != p)
446 {
447 return (mfib_entry_path_ext_get(p[0]));
448 }
449
450 return (NULL);
451}
452
453static mfib_path_ext_t*
454mfib_path_ext_add (mfib_entry_src_t *msrc,
455 fib_node_index_t path_index,
456 mfib_itf_flags_t mfi_flags)
457{
458 mfib_path_ext_t *path_ext;
459
460 pool_get(mfib_path_ext_pool, path_ext);
461
462 path_ext->mfpe_flags = mfi_flags;
463 path_ext->mfpe_path = path_index;
464
465 hash_set(msrc->mfes_exts, path_index,
466 path_ext - mfib_path_ext_pool);
467
468 return (path_ext);
469}
470
471static void
472mfib_path_ext_remove (mfib_entry_src_t *msrc,
473 fib_node_index_t path_index)
474{
475 mfib_path_ext_t *path_ext;
476
477 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
478
479 hash_unset(msrc->mfes_exts, path_index);
480 pool_put(mfib_path_ext_pool, path_ext);
481}
482
Neale Ranns32e1c012016-11-22 17:07:28 +0000483typedef struct mfib_entry_collect_forwarding_ctx_t_
484{
485 load_balance_path_t * next_hops;
486 fib_forward_chain_type_t fct;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800487 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000488} mfib_entry_collect_forwarding_ctx_t;
489
Neale Ranns81424992017-05-18 03:03:22 -0700490static fib_path_list_walk_rc_t
Neale Ranns32e1c012016-11-22 17:07:28 +0000491mfib_entry_src_collect_forwarding (fib_node_index_t pl_index,
492 fib_node_index_t path_index,
493 void *arg)
494{
495 mfib_entry_collect_forwarding_ctx_t *ctx;
496 load_balance_path_t *nh;
497
498 ctx = arg;
499
500 /*
501 * if the path is not resolved, don't include it.
502 */
503 if (!fib_path_is_resolved(path_index))
504 {
Neale Ranns81424992017-05-18 03:03:22 -0700505 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000506 }
507
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800508 /*
509 * If the path is not forwarding to use it
510 */
511 mfib_path_ext_t *path_ext;
512
513 path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts,
514 path_index);
515
516 if (NULL != path_ext &&
517 !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD))
518 {
Neale Ranns81424992017-05-18 03:03:22 -0700519 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800520 }
521
Neale Ranns32e1c012016-11-22 17:07:28 +0000522 switch (ctx->fct)
523 {
524 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
525 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
526 /*
527 * EOS traffic with no label to stack, we need the IP Adj
528 */
529 vec_add2(ctx->next_hops, nh, 1);
530
531 nh->path_index = path_index;
532 nh->path_weight = fib_path_get_weight(path_index);
533 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
534 break;
535
536 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
537 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
538 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
539 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
540 case FIB_FORW_CHAIN_TYPE_ETHERNET:
Florin Corasce1b4c72017-01-26 14:25:34 -0800541 case FIB_FORW_CHAIN_TYPE_NSH:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700542 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns32e1c012016-11-22 17:07:28 +0000543 ASSERT(0);
544 break;
545 }
546
Neale Ranns81424992017-05-18 03:03:22 -0700547 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000548}
549
550static void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800551mfib_entry_stack (mfib_entry_t *mfib_entry,
552 mfib_entry_src_t *msrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000553{
Neale Ranns32e1c012016-11-22 17:07:28 +0000554 dpo_proto_t dp;
555
556 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
557
Neale Rannsc2aad532017-05-30 09:53:52 -0700558 /*
559 * unlink the enty from the previous path list.
560 */
561 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
562 {
563 fib_path_list_child_remove(mfib_entry->mfe_pl,
564 mfib_entry->mfe_sibling);
565 }
566
Neale Ranns9e829a82018-12-17 05:50:32 -0800567 if (NULL != msrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000568 {
Neale Rannsa9374df2017-02-02 02:18:18 -0800569 mfib_entry_collect_forwarding_ctx_t ctx = {
570 .next_hops = NULL,
571 .fct = mfib_entry_get_default_chain_type(mfib_entry),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800572 .msrc = msrc,
Neale Rannsa9374df2017-02-02 02:18:18 -0800573 };
574
Neale Ranns9e829a82018-12-17 05:50:32 -0800575 /*
576 * link the entry to the path-list.
577 * The entry needs to be a child so that we receive the back-walk
578 * updates to recalculate forwarding.
579 */
580 mfib_entry->mfe_pl = msrc->mfes_pl;
581 mfib_entry->mfe_flags = msrc->mfes_flags;
582 mfib_entry->mfe_itfs = msrc->mfes_itfs;
583 mfib_entry->mfe_rpf_id = msrc->mfes_rpf_id;
Neale Ranns32e1c012016-11-22 17:07:28 +0000584
Neale Ranns9e829a82018-12-17 05:50:32 -0800585 if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl)
586 {
587 mfib_entry->mfe_sibling =
588 fib_path_list_child_add(mfib_entry->mfe_pl,
589 FIB_NODE_TYPE_MFIB_ENTRY,
590 mfib_entry_get_index(mfib_entry));
591
592 fib_path_list_walk(mfib_entry->mfe_pl,
593 mfib_entry_src_collect_forwarding,
594 &ctx);
595 }
Neale Rannsa9374df2017-02-02 02:18:18 -0800596 if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags))
Neale Ranns32e1c012016-11-22 17:07:28 +0000597 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800598 if (NULL == ctx.next_hops)
Neale Rannsa9374df2017-02-02 02:18:18 -0800599 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800600 /*
601 * no next-hops, stack directly on the drop
602 */
Neale Rannsa9374df2017-02-02 02:18:18 -0800603 dpo_stack(DPO_MFIB_ENTRY, dp,
604 &mfib_entry->mfe_rep,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800605 drop_dpo_get(dp));
Neale Rannsa9374df2017-02-02 02:18:18 -0800606 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800607 else
608 {
609 /*
610 * each path contirbutes a next-hop. form a replicate
611 * from those choices.
612 */
613 if (!dpo_id_is_valid(&mfib_entry->mfe_rep) ||
614 dpo_is_drop(&mfib_entry->mfe_rep))
615 {
616 dpo_id_t tmp_dpo = DPO_INVALID;
617
618 dpo_set(&tmp_dpo,
619 DPO_REPLICATE, dp,
620 replicate_create(0, dp));
621
622 dpo_stack(DPO_MFIB_ENTRY, dp,
623 &mfib_entry->mfe_rep,
624 &tmp_dpo);
625
626 dpo_reset(&tmp_dpo);
627 }
628 replicate_multipath_update(&mfib_entry->mfe_rep,
629 ctx.next_hops);
630 }
Neale Rannsa9374df2017-02-02 02:18:18 -0800631 }
632 else
633 {
634 /*
635 * for exclusive routes the source provided a replicate DPO
Neale Rannsb7778b62018-12-21 07:00:56 -0800636 * which we stashed in the special path list with one path,
Neale Rannsa9374df2017-02-02 02:18:18 -0800637 * so we can stack directly on that.
638 */
639 ASSERT(1 == vec_len(ctx.next_hops));
Neale Ranns32e1c012016-11-22 17:07:28 +0000640
Neale Rannsb7778b62018-12-21 07:00:56 -0800641 if (NULL != ctx.next_hops)
642 {
643 dpo_stack(DPO_MFIB_ENTRY, dp,
644 &mfib_entry->mfe_rep,
645 &ctx.next_hops[0].path_dpo);
646 dpo_reset(&ctx.next_hops[0].path_dpo);
647 vec_free(ctx.next_hops);
648 }
649 else
650 {
651 dpo_stack(DPO_MFIB_ENTRY, dp,
652 &mfib_entry->mfe_rep,
653 drop_dpo_get(dp));
654 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000655 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000656 }
657 else
658 {
659 dpo_stack(DPO_MFIB_ENTRY, dp,
660 &mfib_entry->mfe_rep,
661 drop_dpo_get(dp));
662 }
Neale Ranns9e829a82018-12-17 05:50:32 -0800663
664 /*
665 * time for walkies fido.
666 */
667 fib_node_back_walk_ctx_t bw_ctx = {
668 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
669 };
670
671 fib_walk_sync(FIB_NODE_TYPE_MFIB_ENTRY,
672 mfib_entry_get_index(mfib_entry),
673 &bw_ctx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000674}
675
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800676static fib_node_index_t
677mfib_entry_src_path_add (mfib_entry_src_t *msrc,
678 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000679{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800680 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000681 fib_route_path_t *rpaths;
682
Neale Rannsa9374df2017-02-02 02:18:18 -0800683 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
684
Neale Ranns32e1c012016-11-22 17:07:28 +0000685 /*
686 * path-lists require a vector of paths
687 */
688 rpaths = NULL;
689 vec_add1(rpaths, rpath[0]);
690
Neale Ranns32e1c012016-11-22 17:07:28 +0000691 if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl)
692 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800693 /* A non-shared path-list */
694 msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF,
695 NULL);
696 fib_path_list_lock(msrc->mfes_pl);
Neale Ranns32e1c012016-11-22 17:07:28 +0000697 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800698
699 path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000700
701 vec_free(rpaths);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800702
703 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000704}
705
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800706static fib_node_index_t
707mfib_entry_src_path_remove (mfib_entry_src_t *msrc,
708 const fib_route_path_t *rpath)
Neale Ranns32e1c012016-11-22 17:07:28 +0000709{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800710 fib_node_index_t path_index;
Neale Ranns32e1c012016-11-22 17:07:28 +0000711 fib_route_path_t *rpaths;
712
Neale Rannsa9374df2017-02-02 02:18:18 -0800713 ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags));
714
Neale Ranns32e1c012016-11-22 17:07:28 +0000715 /*
716 * path-lists require a vector of paths
717 */
718 rpaths = NULL;
719 vec_add1(rpaths, rpath[0]);
720
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800721 path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths);
Neale Ranns32e1c012016-11-22 17:07:28 +0000722
723 vec_free(rpaths);
724
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800725 return (path_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000726}
727
728static void
Neale Ranns9e829a82018-12-17 05:50:32 -0800729mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry,
730 mfib_source_t old_best)
Neale Ranns32e1c012016-11-22 17:07:28 +0000731{
Neale Ranns9e829a82018-12-17 05:50:32 -0800732 mfib_entry_src_t *bsrc, *osrc;
Neale Ranns32e1c012016-11-22 17:07:28 +0000733
Neale Ranns32e1c012016-11-22 17:07:28 +0000734 /*
735 * copy the forwarding data from the bast source
736 */
737 bsrc = mfib_entry_get_best_src(mfib_entry);
Neale Ranns9e829a82018-12-17 05:50:32 -0800738 osrc = mfib_entry_src_find(mfib_entry, old_best, NULL);
Neale Ranns32e1c012016-11-22 17:07:28 +0000739
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800740 if (NULL != bsrc)
Neale Ranns32e1c012016-11-22 17:07:28 +0000741 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800742 if (bsrc->mfes_src != old_best)
743 {
744 /*
745 * we are changing from one source to another
746 * deactivate the old, and activate the new
747 */
748 mfib_entry_src_deactivate(mfib_entry, osrc);
749 mfib_entry_src_activate(mfib_entry, bsrc);
750 }
751 }
752 else
753 {
754 mfib_entry_src_deactivate(mfib_entry, osrc);
Neale Ranns32e1c012016-11-22 17:07:28 +0000755 }
756
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800757 mfib_entry_stack(mfib_entry, bsrc);
Neale Ranns9e829a82018-12-17 05:50:32 -0800758 mfib_entry_cover_update_notify(mfib_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +0000759}
760
761
762fib_node_index_t
763mfib_entry_create (u32 fib_index,
764 mfib_source_t source,
765 const mfib_prefix_t *prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800766 fib_rpf_id_t rpf_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800767 mfib_entry_flags_t entry_flags,
768 index_t repi)
Neale Ranns32e1c012016-11-22 17:07:28 +0000769{
770 fib_node_index_t mfib_entry_index;
771 mfib_entry_t *mfib_entry;
772 mfib_entry_src_t *msrc;
773
774 mfib_entry = mfib_entry_alloc(fib_index, prefix,
775 &mfib_entry_index);
Neale Ranns9e829a82018-12-17 05:50:32 -0800776 msrc = mfib_entry_src_update(mfib_entry, source,
777 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000778
Neale Ranns9e829a82018-12-17 05:50:32 -0800779 if (INDEX_INVALID != repi)
780 {
781 /*
782 * The source is providing its own replicate DPO.
783 * Create a sepcial path-list to manage it, that way
784 * this entry and the source are equivalent to a normal
785 * entry
786 */
787 fib_node_index_t old_pl_index;
788 dpo_proto_t dp;
789 dpo_id_t dpo = DPO_INVALID;
790
791 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
792 old_pl_index = msrc->mfes_pl;
793
794 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
795
796 msrc->mfes_pl =
797 fib_path_list_create_special(dp,
798 FIB_PATH_LIST_FLAG_EXCLUSIVE,
799 &dpo);
800
801 dpo_reset(&dpo);
802 fib_path_list_lock(msrc->mfes_pl);
803 fib_path_list_unlock(old_pl_index);
804 }
805
806 mfib_entry_recalculate_forwarding(mfib_entry, MFIB_SOURCE_NONE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000807
808 return (mfib_entry_index);
809}
810
811static int
812mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry)
813{
814 return (0 == vec_len(mfib_entry->mfe_srcs));
815}
816
817static int
818mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc)
819{
Neale Ranns9e829a82018-12-17 05:50:32 -0800820 return ((INDEX_INVALID == msrc->mfes_cover &&
821 MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags &&
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800822 0 == fib_path_list_get_n_paths(msrc->mfes_pl)));
Neale Ranns32e1c012016-11-22 17:07:28 +0000823}
824
Neale Ranns9e829a82018-12-17 05:50:32 -0800825
826static void
827mfib_entry_update_i (mfib_entry_t *mfib_entry,
828 mfib_entry_src_t *msrc,
829 mfib_source_t current_best,
830 index_t repi)
Neale Ranns32e1c012016-11-22 17:07:28 +0000831{
Neale Rannsa9374df2017-02-02 02:18:18 -0800832 if (INDEX_INVALID != repi)
833 {
834 /*
835 * The source is providing its own replicate DPO.
836 * Create a sepcial path-list to manage it, that way
837 * this entry and the source are equivalent to a normal
838 * entry
839 */
840 fib_node_index_t old_pl_index;
Neale Rannsda78f952017-05-24 09:15:43 -0700841 dpo_proto_t dp;
Neale Rannsa9374df2017-02-02 02:18:18 -0800842 dpo_id_t dpo = DPO_INVALID;
843
Neale Rannsda78f952017-05-24 09:15:43 -0700844 dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry));
Neale Rannsa9374df2017-02-02 02:18:18 -0800845 old_pl_index = msrc->mfes_pl;
846
Neale Rannsda78f952017-05-24 09:15:43 -0700847 dpo_set(&dpo, DPO_REPLICATE, dp, repi);
Neale Rannsa9374df2017-02-02 02:18:18 -0800848
849 msrc->mfes_pl =
Neale Rannsda78f952017-05-24 09:15:43 -0700850 fib_path_list_create_special(dp,
Neale Rannsa9374df2017-02-02 02:18:18 -0800851 FIB_PATH_LIST_FLAG_EXCLUSIVE,
852 &dpo);
853
854 dpo_reset(&dpo);
855 fib_path_list_lock(msrc->mfes_pl);
856 fib_path_list_unlock(old_pl_index);
857 }
858
Neale Ranns32e1c012016-11-22 17:07:28 +0000859 if (mfib_entry_src_ok_for_delete(msrc))
860 {
861 /*
862 * this source has no interfaces and no flags.
863 * it has nothing left to give - remove it
864 */
Neale Ranns9e829a82018-12-17 05:50:32 -0800865 mfib_entry_src_remove(mfib_entry, msrc->mfes_src);
Neale Ranns32e1c012016-11-22 17:07:28 +0000866 }
867
Neale Ranns9e829a82018-12-17 05:50:32 -0800868 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
869}
870
871int
872mfib_entry_special_add (fib_node_index_t mfib_entry_index,
873 mfib_source_t source,
874 mfib_entry_flags_t entry_flags,
875 fib_rpf_id_t rpf_id,
876 index_t repi)
877{
878 mfib_source_t current_best;
879 mfib_entry_t *mfib_entry;
880 mfib_entry_src_t *msrc;
881
882 mfib_entry = mfib_entry_get(mfib_entry_index);
883 current_best = mfib_entry_get_best_source(mfib_entry);
884
885 msrc = mfib_entry_src_update_and_lock(mfib_entry, source, rpf_id,
886 entry_flags);
887
888 mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
889
890 return (mfib_entry_ok_for_delete(mfib_entry));
891}
892
893int
894mfib_entry_update (fib_node_index_t mfib_entry_index,
895 mfib_source_t source,
896 mfib_entry_flags_t entry_flags,
897 fib_rpf_id_t rpf_id,
898 index_t repi)
899{
900 mfib_source_t current_best;
901 mfib_entry_t *mfib_entry;
902 mfib_entry_src_t *msrc;
903
904 mfib_entry = mfib_entry_get(mfib_entry_index);
905 current_best = mfib_entry_get_best_source(mfib_entry);
906 msrc = mfib_entry_src_update(mfib_entry, source, rpf_id, entry_flags);
907
908 mfib_entry_update_i(mfib_entry, msrc, current_best, repi);
Neale Ranns32e1c012016-11-22 17:07:28 +0000909
910 return (mfib_entry_ok_for_delete(mfib_entry));
911}
912
913static void
914mfib_entry_itf_add (mfib_entry_src_t *msrc,
915 u32 sw_if_index,
916 index_t mi)
917{
918 hash_set(msrc->mfes_itfs, sw_if_index, mi);
919}
920
921static void
922mfib_entry_itf_remove (mfib_entry_src_t *msrc,
923 u32 sw_if_index)
924{
925 mfib_itf_t *mfi;
926
927 mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index);
928
929 mfib_itf_delete(mfi);
930
931 hash_unset(msrc->mfes_itfs, sw_if_index);
932}
933
934void
935mfib_entry_path_update (fib_node_index_t mfib_entry_index,
936 mfib_source_t source,
937 const fib_route_path_t *rpath,
938 mfib_itf_flags_t itf_flags)
939{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800940 fib_node_index_t path_index;
Neale Ranns9e829a82018-12-17 05:50:32 -0800941 mfib_source_t current_best;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800942 mfib_path_ext_t *path_ext;
Neale Ranns32e1c012016-11-22 17:07:28 +0000943 mfib_entry_t *mfib_entry;
944 mfib_entry_src_t *msrc;
Neale Rannse821ab12017-06-01 07:45:05 -0700945 mfib_itf_flags_t old;
Neale Ranns32e1c012016-11-22 17:07:28 +0000946
947 mfib_entry = mfib_entry_get(mfib_entry_index);
948 ASSERT(NULL != mfib_entry);
Neale Ranns9e829a82018-12-17 05:50:32 -0800949 current_best = mfib_entry_get_best_source(mfib_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +0000950 msrc = mfib_entry_src_find_or_create(mfib_entry, source);
951
952 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800953 * add the path to the path-list. If it's a duplicate we'll get
954 * back the original path.
Neale Ranns32e1c012016-11-22 17:07:28 +0000955 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800956 path_index = mfib_entry_src_path_add(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +0000957
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800958 /*
959 * find the path extension for that path
960 */
961 path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index);
962
963 if (NULL == path_ext)
Neale Ranns32e1c012016-11-22 17:07:28 +0000964 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800965 old = MFIB_ITF_FLAG_NONE;
966 path_ext = mfib_path_ext_add(msrc, path_index, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000967 }
968 else
969 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800970 old = path_ext->mfpe_flags;
971 path_ext->mfpe_flags = itf_flags;
972 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000973
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800974 /*
975 * Has the path changed its contribution to the input interface set.
976 * Which only paths with interfaces can do...
977 */
978 if (~0 != rpath[0].frp_sw_if_index)
979 {
980 mfib_itf_t *mfib_itf;
981
Neale Rannse821ab12017-06-01 07:45:05 -0700982 if (old != itf_flags)
Neale Ranns32e1c012016-11-22 17:07:28 +0000983 {
Neale Rannse821ab12017-06-01 07:45:05 -0700984 /*
985 * change of flag contributions
986 */
987 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
988 rpath[0].frp_sw_if_index);
989
990 if (NULL == mfib_itf)
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800991 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800992 mfib_entry_itf_add(msrc,
993 rpath[0].frp_sw_if_index,
Neale Rannse821ab12017-06-01 07:45:05 -0700994 mfib_itf_create(path_index, itf_flags));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800995 }
996 else
997 {
Neale Rannse821ab12017-06-01 07:45:05 -0700998 if (mfib_itf_update(mfib_itf,
999 path_index,
1000 itf_flags))
1001 {
1002 /*
1003 * no more interface flags on this path, remove
1004 * from the data-plane set
1005 */
1006 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
1007 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001008 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001009 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001010 }
1011
Neale Ranns9e829a82018-12-17 05:50:32 -08001012 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
Neale Ranns32e1c012016-11-22 17:07:28 +00001013}
1014
1015/*
1016 * mfib_entry_path_remove
1017 *
1018 * remove a path from the entry.
1019 * return the mfib_entry's index if it is still present, INVALID otherwise.
1020 */
1021int
1022mfib_entry_path_remove (fib_node_index_t mfib_entry_index,
1023 mfib_source_t source,
1024 const fib_route_path_t *rpath)
1025{
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001026 fib_node_index_t path_index;
Neale Ranns9e829a82018-12-17 05:50:32 -08001027 mfib_source_t current_best;
Neale Ranns32e1c012016-11-22 17:07:28 +00001028 mfib_entry_t *mfib_entry;
1029 mfib_entry_src_t *msrc;
Neale Ranns32e1c012016-11-22 17:07:28 +00001030
1031 mfib_entry = mfib_entry_get(mfib_entry_index);
1032 ASSERT(NULL != mfib_entry);
Neale Ranns9e829a82018-12-17 05:50:32 -08001033 current_best = mfib_entry_get_best_source(mfib_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001034 msrc = mfib_entry_src_find(mfib_entry, source, NULL);
1035
1036 if (NULL == msrc)
1037 {
1038 /*
1039 * there are no paths left for this source
1040 */
1041 return (mfib_entry_ok_for_delete(mfib_entry));
1042 }
1043
1044 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001045 * remove the path from the path-list. If it's not there we'll get
1046 * back invalid
Neale Ranns32e1c012016-11-22 17:07:28 +00001047 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001048 path_index = mfib_entry_src_path_remove(msrc, rpath);
Neale Ranns32e1c012016-11-22 17:07:28 +00001049
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001050 if (FIB_NODE_INDEX_INVALID != path_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001051 {
1052 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001053 * don't need the extension, nor the interface anymore
Neale Ranns32e1c012016-11-22 17:07:28 +00001054 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001055 mfib_path_ext_remove(msrc, path_index);
1056 if (~0 != rpath[0].frp_sw_if_index)
1057 {
Neale Rannse821ab12017-06-01 07:45:05 -07001058 mfib_itf_t *mfib_itf;
1059
1060 mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs,
1061 rpath[0].frp_sw_if_index);
1062
1063 if (mfib_itf_update(mfib_itf,
1064 path_index,
1065 MFIB_ITF_FLAG_NONE))
1066 {
1067 /*
1068 * no more interface flags on this path, remove
1069 * from the data-plane set
1070 */
1071 mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index);
1072 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001073 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001074 }
1075
Neale Ranns32e1c012016-11-22 17:07:28 +00001076 if (mfib_entry_src_ok_for_delete(msrc))
1077 {
1078 /*
1079 * this source has no interfaces and no flags.
1080 * it has nothing left to give - remove it
1081 */
1082 mfib_entry_src_remove(mfib_entry, source);
1083 }
1084
Neale Ranns9e829a82018-12-17 05:50:32 -08001085 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
Neale Ranns32e1c012016-11-22 17:07:28 +00001086
1087 return (mfib_entry_ok_for_delete(mfib_entry));
1088}
1089
1090/**
1091 * mfib_entry_delete
1092 *
1093 * The source is withdrawing all the paths it provided
1094 */
1095int
1096mfib_entry_delete (fib_node_index_t mfib_entry_index,
1097 mfib_source_t source)
1098{
Neale Ranns9e829a82018-12-17 05:50:32 -08001099 mfib_source_t current_best;
Neale Ranns32e1c012016-11-22 17:07:28 +00001100 mfib_entry_t *mfib_entry;
1101
1102 mfib_entry = mfib_entry_get(mfib_entry_index);
Neale Ranns9e829a82018-12-17 05:50:32 -08001103 current_best = mfib_entry_get_best_source(mfib_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001104 mfib_entry_src_remove(mfib_entry, source);
1105
Neale Ranns9e829a82018-12-17 05:50:32 -08001106 mfib_entry_recalculate_forwarding(mfib_entry, current_best);
Neale Ranns32e1c012016-11-22 17:07:28 +00001107
1108 return (mfib_entry_ok_for_delete(mfib_entry));
1109}
1110
1111static int
1112fib_ip4_address_compare (ip4_address_t * a1,
1113 ip4_address_t * a2)
1114{
1115 /*
1116 * IP addresses are unsiged ints. the return value here needs to be signed
1117 * a simple subtraction won't cut it.
1118 * If the addresses are the same, the sort order is undefiend, so phoey.
1119 */
1120 return ((clib_net_to_host_u32(a1->data_u32) >
1121 clib_net_to_host_u32(a2->data_u32) ) ?
1122 1 : -1);
1123}
1124
1125static int
1126fib_ip6_address_compare (ip6_address_t * a1,
1127 ip6_address_t * a2)
1128{
1129 int i;
1130 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1131 {
1132 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1133 clib_net_to_host_u16 (a2->as_u16[i]));
1134 if (cmp != 0)
1135 return cmp;
1136 }
1137 return 0;
1138}
1139
1140static int
1141mfib_entry_cmp (fib_node_index_t mfib_entry_index1,
1142 fib_node_index_t mfib_entry_index2)
1143{
1144 mfib_entry_t *mfib_entry1, *mfib_entry2;
1145 int cmp = 0;
1146
1147 mfib_entry1 = mfib_entry_get(mfib_entry_index1);
1148 mfib_entry2 = mfib_entry_get(mfib_entry_index2);
1149
1150 switch (mfib_entry1->mfe_prefix.fp_proto)
1151 {
1152 case FIB_PROTOCOL_IP4:
1153 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4,
1154 &mfib_entry2->mfe_prefix.fp_grp_addr.ip4);
1155
1156 if (0 == cmp)
1157 {
1158 cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4,
1159 &mfib_entry2->mfe_prefix.fp_src_addr.ip4);
1160 }
1161 break;
1162 case FIB_PROTOCOL_IP6:
1163 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6,
1164 &mfib_entry2->mfe_prefix.fp_grp_addr.ip6);
1165
1166 if (0 == cmp)
1167 {
1168 cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6,
1169 &mfib_entry2->mfe_prefix.fp_src_addr.ip6);
1170 }
1171 break;
1172 case FIB_PROTOCOL_MPLS:
1173 ASSERT(0);
1174 cmp = 0;
1175 break;
1176 }
1177
1178 if (0 == cmp) {
1179 cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len);
1180 }
1181 return (cmp);
1182}
1183
1184int
1185mfib_entry_cmp_for_sort (void *i1, void *i2)
1186{
1187 fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2;
1188
1189 return (mfib_entry_cmp(*mfib_entry_index1,
1190 *mfib_entry_index2));
1191}
1192
Neale Rannsc2aad532017-05-30 09:53:52 -07001193static void
1194mfib_entry_last_lock_gone (fib_node_t *node)
1195{
1196 mfib_entry_t *mfib_entry;
1197 mfib_entry_src_t *msrc;
1198
1199 mfib_entry = mfib_entry_from_fib_node(node);
1200
1201 dpo_reset(&mfib_entry->mfe_rep);
1202
1203 MFIB_ENTRY_DBG(mfib_entry, "last-lock");
1204
1205 vec_foreach(msrc, mfib_entry->mfe_srcs)
1206 {
1207 mfib_entry_src_flush(msrc);
1208 }
1209
1210 vec_free(mfib_entry->mfe_srcs);
1211
1212 fib_node_deinit(&mfib_entry->mfe_node);
1213 pool_put(mfib_entry_pool, mfib_entry);
1214}
1215
Neale Ranns28c142e2018-09-07 09:37:07 -07001216u32
1217mfib_entry_get_stats_index (fib_node_index_t fib_entry_index)
1218{
1219 mfib_entry_t *mfib_entry;
1220
1221 mfib_entry = mfib_entry_get(fib_entry_index);
1222
1223 return (mfib_entry->mfe_rep.dpoi_index);
1224}
1225
Neale Rannsc2aad532017-05-30 09:53:52 -07001226/*
1227 * mfib_entry_back_walk_notify
1228 *
1229 * A back walk has reach this entry.
1230 */
1231static fib_node_back_walk_rc_t
1232mfib_entry_back_walk_notify (fib_node_t *node,
1233 fib_node_back_walk_ctx_t *ctx)
1234{
Neale Ranns9e829a82018-12-17 05:50:32 -08001235 mfib_entry_t *mfib_entry;
1236
1237 mfib_entry = mfib_entry_from_fib_node(node);
1238 mfib_entry_recalculate_forwarding(mfib_entry,
1239 mfib_entry_get_best_source(mfib_entry));
Neale Rannsc2aad532017-05-30 09:53:52 -07001240
1241 return (FIB_NODE_BACK_WALK_CONTINUE);
1242}
1243
1244static void
1245mfib_entry_show_memory (void)
1246{
1247 fib_show_memory_usage("multicast-Entry",
1248 pool_elts(mfib_entry_pool),
1249 pool_len(mfib_entry_pool),
1250 sizeof(mfib_entry_t));
1251}
1252
1253/*
1254 * The MFIB entry's graph node virtual function table
1255 */
1256static const fib_node_vft_t mfib_entry_vft = {
1257 .fnv_get = mfib_entry_get_node,
1258 .fnv_last_lock = mfib_entry_last_lock_gone,
1259 .fnv_back_walk = mfib_entry_back_walk_notify,
1260 .fnv_mem_show = mfib_entry_show_memory,
1261};
1262
Neale Ranns32e1c012016-11-22 17:07:28 +00001263void
1264mfib_entry_lock (fib_node_index_t mfib_entry_index)
1265{
1266 mfib_entry_t *mfib_entry;
1267
1268 mfib_entry = mfib_entry_get(mfib_entry_index);
1269
1270 fib_node_lock(&mfib_entry->mfe_node);
1271}
1272
1273void
1274mfib_entry_unlock (fib_node_index_t mfib_entry_index)
1275{
1276 mfib_entry_t *mfib_entry;
1277
1278 mfib_entry = mfib_entry_get(mfib_entry_index);
1279
1280 fib_node_unlock(&mfib_entry->mfe_node);
1281}
1282
1283static void
1284mfib_entry_dpo_lock (dpo_id_t *dpo)
1285{
1286}
1287static void
1288mfib_entry_dpo_unlock (dpo_id_t *dpo)
1289{
1290}
1291
1292const static dpo_vft_t mfib_entry_dpo_vft = {
1293 .dv_lock = mfib_entry_dpo_lock,
1294 .dv_unlock = mfib_entry_dpo_unlock,
1295 .dv_format = format_mfib_entry_dpo,
1296 .dv_mem_show = mfib_entry_show_memory,
1297};
1298
1299const static char* const mfib_entry_ip4_nodes[] =
1300{
1301 "ip4-mfib-forward-rpf",
1302 NULL,
1303};
1304const static char* const mfib_entry_ip6_nodes[] =
1305{
1306 "ip6-mfib-forward-rpf",
1307 NULL,
1308};
1309
1310const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] =
1311{
1312 [DPO_PROTO_IP4] = mfib_entry_ip4_nodes,
1313 [DPO_PROTO_IP6] = mfib_entry_ip6_nodes,
1314};
1315
1316void
1317mfib_entry_module_init (void)
1318{
1319 fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft);
1320 dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes);
Neale Ranns710071b2018-09-24 12:36:26 +00001321 mfib_entry_logger = vlib_log_register_class("mfib", "entry");
Neale Ranns32e1c012016-11-22 17:07:28 +00001322}
1323
1324void
1325mfib_entry_encode (fib_node_index_t mfib_entry_index,
1326 fib_route_path_encode_t **api_rpaths)
1327{
1328 mfib_entry_t *mfib_entry;
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001329 mfib_entry_src_t *bsrc;
Neale Ranns32e1c012016-11-22 17:07:28 +00001330
1331 mfib_entry = mfib_entry_get(mfib_entry_index);
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001332 bsrc = mfib_entry_get_best_src(mfib_entry);
1333
1334 if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl)
Neale Ranns5a8123b2017-01-26 01:18:23 -08001335 {
Neale Ranns775f73c2018-12-20 03:01:49 -08001336 fib_path_list_walk_w_ext(bsrc->mfes_pl,
1337 NULL,
1338 fib_path_encode,
1339 api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -08001340 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001341}
1342
Neale Ranns9e829a82018-12-17 05:50:32 -08001343const mfib_prefix_t *
1344mfib_entry_get_prefix (fib_node_index_t mfib_entry_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001345{
1346 mfib_entry_t *mfib_entry;
1347
1348 mfib_entry = mfib_entry_get(mfib_entry_index);
Neale Ranns9e829a82018-12-17 05:50:32 -08001349
1350 return (&mfib_entry->mfe_prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001351}
1352
1353u32
1354mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index)
1355{
1356 mfib_entry_t *mfib_entry;
1357
1358 mfib_entry = mfib_entry_get(mfib_entry_index);
1359
1360 return (mfib_entry->mfe_fib_index);
1361}
1362
Neale Rannsff233892017-12-12 00:21:19 -08001363const dpo_id_t*
1364mfib_entry_contribute_ip_forwarding (fib_node_index_t mfib_entry_index)
1365{
1366 mfib_entry_t *mfib_entry;
1367
1368 mfib_entry = mfib_entry_get(mfib_entry_index);
1369
1370 return (&mfib_entry->mfe_rep);
1371}
1372
Neale Ranns32e1c012016-11-22 17:07:28 +00001373void
1374mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index,
1375 fib_forward_chain_type_t type,
Neale Ranns9e829a82018-12-17 05:50:32 -08001376 mfib_entry_fwd_flags_t flags,
Neale Ranns32e1c012016-11-22 17:07:28 +00001377 dpo_id_t *dpo)
1378{
1379 /*
1380 * An IP mFIB entry can only provide a forwarding chain that
1381 * is the same IP proto as the prefix.
1382 * No use-cases (i know of) for other combinations.
1383 */
1384 mfib_entry_t *mfib_entry;
1385 dpo_proto_t dp;
1386
1387 mfib_entry = mfib_entry_get(mfib_entry_index);
1388
1389 dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto);
1390
Neale Ranns9e829a82018-12-17 05:50:32 -08001391 if (type == mfib_forw_chain_type_from_dpo_proto(dp))
Neale Ranns32e1c012016-11-22 17:07:28 +00001392 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001393 replicate_t * rep;
1394
1395 rep = replicate_get(mfib_entry->mfe_rep.dpoi_index);
1396
1397 if ((rep->rep_flags & REPLICATE_FLAGS_HAS_LOCAL) &&
1398 (flags & MFIB_ENTRY_FWD_FLAG_NO_LOCAL))
1399 {
1400 /*
1401 * caller does not want the local paths that the entry has
1402 */
1403 dpo_set(dpo, DPO_REPLICATE, rep->rep_proto,
1404 replicate_dup(REPLICATE_FLAGS_NONE,
1405 mfib_entry->mfe_rep.dpoi_index));
1406 }
1407 else
1408 {
1409 dpo_copy(dpo, &mfib_entry->mfe_rep);
1410 }
Neale Ranns32e1c012016-11-22 17:07:28 +00001411 }
1412 else
1413 {
1414 dpo_copy(dpo, drop_dpo_get(dp));
1415 }
1416}
1417
Neale Ranns9e829a82018-12-17 05:50:32 -08001418/*
1419 * fib_entry_cover_changed
1420 *
1421 * this entry is tracking its cover and that cover has changed.
1422 */
1423void
1424mfib_entry_cover_changed (fib_node_index_t mfib_entry_index)
1425{
1426 mfib_entry_t *mfib_entry;
1427 mfib_entry_src_t *msrc;
1428 mfib_src_res_t res;
1429
1430 mfib_entry = mfib_entry_get(mfib_entry_index);
1431 msrc = mfib_entry_get_best_src(mfib_entry);
1432
1433 res = mfib_entry_src_cover_change(mfib_entry, msrc);
1434
1435 if (MFIB_SRC_REEVALUATE == res)
1436 {
1437 mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1438 }
1439 MFIB_ENTRY_DBG(mfib_entry, "cover-changed");
1440}
1441
1442/*
1443 * mfib_entry_cover_updated
1444 *
1445 * this entry is tracking its cover and that cover has been updated
1446 * (i.e. its forwarding information has changed).
1447 */
1448void
1449mfib_entry_cover_updated (fib_node_index_t mfib_entry_index)
1450{
1451 mfib_entry_t *mfib_entry;
1452 mfib_entry_src_t *msrc;
1453 mfib_src_res_t res;
1454
1455 mfib_entry = mfib_entry_get(mfib_entry_index);
1456 msrc = mfib_entry_get_best_src(mfib_entry);
1457
1458 res = mfib_entry_src_cover_update(mfib_entry, msrc);
1459
1460 if (MFIB_SRC_REEVALUATE == res)
1461 {
1462 mfib_entry_recalculate_forwarding(mfib_entry, msrc->mfes_src);
1463 }
1464 MFIB_ENTRY_DBG(mfib_entry, "cover-updated");
1465}
1466
Neale Ranns32e1c012016-11-22 17:07:28 +00001467u32
1468mfib_entry_pool_size (void)
1469{
1470 return (pool_elts(mfib_entry_pool));
1471}
1472
1473static clib_error_t *
1474show_mfib_entry_command (vlib_main_t * vm,
1475 unformat_input_t * input,
1476 vlib_cli_command_t * cmd)
1477{
1478 fib_node_index_t fei;
1479
1480 if (unformat (input, "%d", &fei))
1481 {
1482 /*
1483 * show one in detail
1484 */
1485 if (!pool_is_free_index(mfib_entry_pool, fei))
1486 {
1487 vlib_cli_output (vm, "%d@%U",
1488 fei,
1489 format_mfib_entry, fei,
1490 MFIB_ENTRY_FORMAT_DETAIL2);
1491 }
1492 else
1493 {
1494 vlib_cli_output (vm, "entry %d invalid", fei);
1495 }
1496 }
1497 else
1498 {
1499 /*
1500 * show all
1501 */
1502 vlib_cli_output (vm, "FIB Entries:");
1503 pool_foreach_index(fei, mfib_entry_pool,
1504 ({
1505 vlib_cli_output (vm, "%d@%U",
1506 fei,
1507 format_mfib_entry, fei,
1508 MFIB_ENTRY_FORMAT_BRIEF);
1509 }));
1510 }
1511
1512 return (NULL);
1513}
1514
Neale Rannsc756c1c2017-02-08 09:11:57 -08001515/*?
1516 * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique
1517 * numerical indentifier.
1518 ?*/
Neale Ranns32e1c012016-11-22 17:07:28 +00001519VLIB_CLI_COMMAND (show_mfib_entry, static) = {
1520 .path = "show mfib entry",
1521 .function = show_mfib_entry_command,
1522 .short_help = "show mfib entry",
1523};