blob: b78346ce45a9ba95de055062b8227bcc46e98a51 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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#include <vnet/ip/format.h>
18#include <vnet/ip/lookup.h>
19#include <vnet/adj/adj.h>
20#include <vnet/dpo/load_balance.h>
21#include <vnet/dpo/drop_dpo.h>
22
23#include <vnet/fib/fib_entry.h>
24#include <vnet/fib/fib_walk.h>
25#include <vnet/fib/fib_entry_src.h>
26#include <vnet/fib/fib_entry_cover.h>
27#include <vnet/fib/fib_table.h>
28#include <vnet/fib/fib_internal.h>
29#include <vnet/fib/fib_attached_export.h>
30#include <vnet/fib/fib_path_ext.h>
Neale Ranns1f50bf82019-07-16 15:28:52 +000031#include <vnet/fib/fib_entry_delegate.h>
32#include <vnet/fib/fib_entry_track.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010033
34/*
35 * Array of strings/names for the FIB sources
36 */
Neale Ranns0bfe5d82016-08-25 15:29:12 +010037static const char *fib_attribute_names[] = FIB_ENTRY_ATTRIBUTES;
Neale Ranns89541992017-04-06 04:41:02 -070038static const char *fib_src_attribute_names[] = FIB_ENTRY_SRC_ATTRIBUTES;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010039
40/*
41 * Pool for all fib_entries
42 */
43static fib_entry_t *fib_entry_pool;
44
Neale Ranns710071b2018-09-24 12:36:26 +000045/**
46 * the logger
47 */
48vlib_log_class_t fib_entry_logger;
49
Neale Ranns0bfe5d82016-08-25 15:29:12 +010050fib_entry_t *
51fib_entry_get (fib_node_index_t index)
52{
53 return (pool_elt_at_index(fib_entry_pool, index));
54}
55
56static fib_node_t *
57fib_entry_get_node (fib_node_index_t index)
58{
59 return ((fib_node_t*)fib_entry_get(index));
60}
61
62fib_node_index_t
63fib_entry_get_index (const fib_entry_t * fib_entry)
64{
65 return (fib_entry - fib_entry_pool);
66}
67
Neale Rannsda78f952017-05-24 09:15:43 -070068fib_protocol_t
Neale Ranns0bfe5d82016-08-25 15:29:12 +010069fib_entry_get_proto (const fib_entry_t * fib_entry)
70{
71 return (fib_entry->fe_prefix.fp_proto);
72}
73
Neale Rannsda78f952017-05-24 09:15:43 -070074dpo_proto_t
75fib_entry_get_dpo_proto (const fib_entry_t * fib_entry)
76{
77 return (fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto));
78}
79
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080fib_forward_chain_type_t
81fib_entry_get_default_chain_type (const fib_entry_t *fib_entry)
82{
83 switch (fib_entry->fe_prefix.fp_proto)
84 {
85 case FIB_PROTOCOL_IP4:
86 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
87 case FIB_PROTOCOL_IP6:
88 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6);
89 case FIB_PROTOCOL_MPLS:
90 if (MPLS_EOS == fib_entry->fe_prefix.fp_eos)
Neale Ranns0f26c5a2017-03-01 15:12:11 -080091 return (FIB_FORW_CHAIN_TYPE_MPLS_EOS);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092 else
93 return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS);
94 }
95
96 return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4);
97}
98
99u8 *
Neale Ranns710071b2018-09-24 12:36:26 +0000100format_fib_entry_flags (u8 *s, va_list *args)
101{
102 fib_entry_attribute_t attr;
103 fib_entry_flag_t flag = va_arg(*args, int);
104
105 FOR_EACH_FIB_ATTRIBUTE(attr) {
106 if ((1<<attr) & flag) {
107 s = format (s, "%s,", fib_attribute_names[attr]);
108 }
109 }
110
111 return (s);
112}
113
114u8 *
115format_fib_entry_src_flags (u8 *s, va_list *args)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100116{
Neale Ranns89541992017-04-06 04:41:02 -0700117 fib_entry_src_attribute_t sattr;
Neale Ranns710071b2018-09-24 12:36:26 +0000118 fib_entry_src_flag_t flag = va_arg(*args, int);
119
120 FOR_EACH_FIB_SRC_ATTRIBUTE(sattr) {
121 if ((1<<sattr) & flag) {
122 s = format (s, "%s,", fib_src_attribute_names[sattr]);
123 }
124 }
125
126 return (s);
127}
128
129u8 *
130format_fib_entry (u8 * s, va_list * args)
131{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100132 fib_forward_chain_type_t fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100133 fib_entry_t *fib_entry;
134 fib_entry_src_t *src;
135 fib_node_index_t fei;
136 fib_source_t source;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100137 int level;
138
139 fei = va_arg (*args, fib_node_index_t);
140 level = va_arg (*args, int);
141 fib_entry = fib_entry_get(fei);
142
143 s = format (s, "%U", format_fib_prefix, &fib_entry->fe_prefix);
144
Neale Rannsabcf3ea2018-03-26 09:10:41 -0700145 if (level >= FIB_ENTRY_FORMAT_DETAIL)
146 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147 s = format (s, " fib:%d", fib_entry->fe_fib_index);
148 s = format (s, " index:%d", fib_entry_get_index(fib_entry));
149 s = format (s, " locks:%d", fib_entry->fe_node.fn_locks);
150
151 FOR_EACH_SRC_ADDED(fib_entry, src, source,
152 ({
Neale Ranns2297af02017-09-12 09:45:04 -0700153 s = format (s, "\n %U", format_fib_source, source);
Neale Ranns89541992017-04-06 04:41:02 -0700154 s = format (s, " refs:%d", src->fes_ref_count);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100155 if (FIB_ENTRY_FLAG_NONE != src->fes_entry_flags) {
Neale Ranns710071b2018-09-24 12:36:26 +0000156 s = format(s, " entry-flags:%U",
157 format_fib_entry_flags, src->fes_entry_flags);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100158 }
Neale Ranns89541992017-04-06 04:41:02 -0700159 if (FIB_ENTRY_SRC_FLAG_NONE != src->fes_flags) {
Neale Ranns710071b2018-09-24 12:36:26 +0000160 s = format(s, " src-flags:%U",
161 format_fib_entry_src_flags, src->fes_flags);
Neale Ranns89541992017-04-06 04:41:02 -0700162 }
Neale Ranns2303cb12018-02-21 04:57:17 -0800163 s = fib_entry_src_format(fib_entry, source, s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164 s = format (s, "\n");
165 if (FIB_NODE_INDEX_INVALID != src->fes_pl)
166 {
167 s = fib_path_list_format(src->fes_pl, s);
168 }
Neale Ranns81424992017-05-18 03:03:22 -0700169 s = format(s, "%U", format_fib_path_ext_list, &src->fes_path_exts);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170 }));
171
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100172 s = format (s, "\n forwarding: ");
Neale Rannsabcf3ea2018-03-26 09:10:41 -0700173 }
174 else
175 {
176 s = format (s, "\n");
177 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100178
179 fct = fib_entry_get_default_chain_type(fib_entry);
180
Neale Rannsad422ed2016-11-02 14:20:04 +0000181 if (!dpo_id_is_valid(&fib_entry->fe_lb))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100182 {
183 s = format (s, " UNRESOLVED\n");
184 return (s);
185 }
186 else
187 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000188 s = format(s, " %U-chain\n %U",
189 format_fib_forw_chain_type, fct,
190 format_dpo_id,
191 &fib_entry->fe_lb,
192 2);
193 s = format(s, "\n");
194
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100195 if (level >= FIB_ENTRY_FORMAT_DETAIL2)
196 {
Neale Ranns1f50bf82019-07-16 15:28:52 +0000197 index_t *fedi;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100198
Neale Ranns88fc83e2017-04-05 08:11:14 -0700199 s = format (s, " Delegates:\n");
Neale Ranns1f50bf82019-07-16 15:28:52 +0000200 vec_foreach(fedi, fib_entry->fe_delegates)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100201 {
Neale Ranns1f50bf82019-07-16 15:28:52 +0000202 s = format(s, " %U\n", format_fib_entry_delegate, *fedi);
203 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100204 }
205 }
206
207 if (level >= FIB_ENTRY_FORMAT_DETAIL2)
208 {
Neale Ranns88fc83e2017-04-05 08:11:14 -0700209 s = format(s, " Children:");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100210 s = fib_node_children_format(fib_entry->fe_node.fn_children, s);
211 }
212
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213 return (s);
214}
215
216static fib_entry_t*
217fib_entry_from_fib_node (fib_node_t *node)
218{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100219 ASSERT(FIB_NODE_TYPE_ENTRY == node->fn_type);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100220 return ((fib_entry_t*)node);
221}
222
223static void
224fib_entry_last_lock_gone (fib_node_t *node)
225{
Neale Rannsad422ed2016-11-02 14:20:04 +0000226 fib_entry_delegate_type_t fdt;
227 fib_entry_delegate_t *fed;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100228 fib_entry_t *fib_entry;
229
230 fib_entry = fib_entry_from_fib_node(node);
231
Neale Ranns2303cb12018-02-21 04:57:17 -0800232 ASSERT(!dpo_id_is_valid(&fib_entry->fe_lb));
233
Neale Rannsad422ed2016-11-02 14:20:04 +0000234 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100235 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000236 dpo_reset(&fed->fd_dpo);
237 fib_entry_delegate_remove(fib_entry, fdt);
238 });
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100239
240 FIB_ENTRY_DBG(fib_entry, "last-lock");
241
242 fib_node_deinit(&fib_entry->fe_node);
Neale Rannsad422ed2016-11-02 14:20:04 +0000243
244 ASSERT(0 == vec_len(fib_entry->fe_delegates));
245 vec_free(fib_entry->fe_delegates);
Neale Rannsa4e77662017-12-04 20:00:30 +0000246 vec_free(fib_entry->fe_srcs);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100247 pool_put(fib_entry_pool, fib_entry);
248}
249
Neale Rannsa4e77662017-12-04 20:00:30 +0000250static fib_entry_src_t*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100251fib_entry_get_best_src_i (const fib_entry_t *fib_entry)
252{
Neale Rannsa4e77662017-12-04 20:00:30 +0000253 fib_entry_src_t *bsrc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100254
255 /*
256 * the enum of sources is deliberately arranged in priority order
257 */
Neale Rannsa4e77662017-12-04 20:00:30 +0000258 if (0 == vec_len(fib_entry->fe_srcs))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100259 {
Neale Rannsa4e77662017-12-04 20:00:30 +0000260 bsrc = NULL;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100261 }
262 else
263 {
Neale Rannsa4e77662017-12-04 20:00:30 +0000264 bsrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100265 }
266
267 return (bsrc);
268}
269
270static fib_source_t
271fib_entry_src_get_source (const fib_entry_src_t *esrc)
272{
273 if (NULL != esrc)
274 {
275 return (esrc->fes_src);
276 }
Neale Ranns3bab8f92019-12-04 06:11:00 +0000277 return (FIB_SOURCE_INVALID);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100278}
279
280static fib_entry_flag_t
281fib_entry_src_get_flags (const fib_entry_src_t *esrc)
282{
283 if (NULL != esrc)
284 {
285 return (esrc->fes_entry_flags);
286 }
287 return (FIB_ENTRY_FLAG_NONE);
288}
289
290fib_entry_flag_t
291fib_entry_get_flags (fib_node_index_t fib_entry_index)
292{
293 return (fib_entry_get_flags_i(fib_entry_get(fib_entry_index)));
294}
295
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100296static void
297fib_entry_show_memory (void)
298{
299 u32 n_srcs = 0, n_exts = 0;
300 fib_entry_src_t *esrc;
301 fib_entry_t *entry;
302
303 fib_show_memory_usage("Entry",
304 pool_elts(fib_entry_pool),
305 pool_len(fib_entry_pool),
306 sizeof(fib_entry_t));
307
Damjan Marionb2c31b62020-12-13 21:47:40 +0100308 pool_foreach (entry, fib_entry_pool)
309 {
Neale Rannsa4e77662017-12-04 20:00:30 +0000310 n_srcs += vec_len(entry->fe_srcs);
311 vec_foreach(esrc, entry->fe_srcs)
312 {
313 n_exts += fib_path_ext_list_length(&esrc->fes_path_exts);
314 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100315 }
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100316
317 fib_show_memory_usage("Entry Source",
318 n_srcs, n_srcs, sizeof(fib_entry_src_t));
319 fib_show_memory_usage("Entry Path-Extensions",
320 n_exts, n_exts,
321 sizeof(fib_path_ext_t));
322}
323
Neale Ranns3ee44042016-10-03 13:05:48 +0100324/**
325 * @brief Contribute the set of Adjacencies that this entry forwards with
326 * to build the uRPF list of its children
327 */
328void
329fib_entry_contribute_urpf (fib_node_index_t entry_index,
330 index_t urpf)
331{
332 fib_entry_t *fib_entry;
333
334 fib_entry = fib_entry_get(entry_index);
335
336 return (fib_path_list_contribute_urpf(fib_entry->fe_parent, urpf));
337}
338
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100339/*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800340 * If the client is request a chain for multicast forwarding then swap
341 * the chain type to one that can provide such transport.
342 */
343static fib_forward_chain_type_t
344fib_entry_chain_type_mcast_to_ucast (fib_forward_chain_type_t fct)
345{
346 switch (fct)
347 {
348 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
349 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
350 /*
351 * we can only transport IP multicast packets if there is an
352 * LSP.
353 */
354 fct = FIB_FORW_CHAIN_TYPE_MPLS_EOS;
355 break;
356 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
357 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
358 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
359 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
360 case FIB_FORW_CHAIN_TYPE_ETHERNET:
361 case FIB_FORW_CHAIN_TYPE_NSH:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700362 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800363 break;
364 }
365
366 return (fct);
367}
368
369/*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100370 * fib_entry_contribute_forwarding
371 *
372 * Get an lock the forwarding information (DPO) contributed by the FIB entry.
373 */
374void
375fib_entry_contribute_forwarding (fib_node_index_t fib_entry_index,
Neale Rannsad422ed2016-11-02 14:20:04 +0000376 fib_forward_chain_type_t fct,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100377 dpo_id_t *dpo)
378{
Neale Rannsad422ed2016-11-02 14:20:04 +0000379 fib_entry_delegate_t *fed;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100380 fib_entry_t *fib_entry;
381
382 fib_entry = fib_entry_get(fib_entry_index);
383
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800384 /*
385 * mfib children ask for mcast chains. fix these to the appropriate ucast types.
386 */
387 fct = fib_entry_chain_type_mcast_to_ucast(fct);
388
Neale Rannsad422ed2016-11-02 14:20:04 +0000389 if (fct == fib_entry_get_default_chain_type(fib_entry))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100390 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000391 dpo_copy(dpo, &fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100392 }
Neale Rannsad422ed2016-11-02 14:20:04 +0000393 else
394 {
Neale Ranns1f50bf82019-07-16 15:28:52 +0000395 fed = fib_entry_delegate_find(fib_entry,
396 fib_entry_chain_type_to_delegate_type(fct));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100397
Neale Rannsad422ed2016-11-02 14:20:04 +0000398 if (NULL == fed)
399 {
Neale Rannsa66020b2019-10-14 08:52:43 -0700400 /*
401 * use a temporary DPO lest the delegate realloc in the recursive
402 * calculation.
403 */
404 dpo_id_t tmp = DPO_INVALID;
405
Neale Rannsad422ed2016-11-02 14:20:04 +0000406 /*
407 * on-demand create eos/non-eos.
408 * There is no on-demand delete because:
409 * - memory versus complexity & reliability:
410 * leaving unrequired [n]eos LB arounds wastes memory, cleaning
411 * then up on the right trigger is more code. i favour the latter.
412 */
413 fib_entry_src_mk_lb(fib_entry,
Neale Ranns1c59df72021-01-26 12:08:25 +0000414 fib_entry_get_best_source(fib_entry_index),
Neale Rannsad422ed2016-11-02 14:20:04 +0000415 fct,
Neale Rannsa66020b2019-10-14 08:52:43 -0700416 &tmp);
417
418 fed = fib_entry_delegate_find_or_add(
419 fib_entry,
420 fib_entry_chain_type_to_delegate_type(fct));
421
422 dpo_copy(&fed->fd_dpo, &tmp);
423 dpo_reset(&tmp);
Neale Rannsad422ed2016-11-02 14:20:04 +0000424 }
425
426 dpo_copy(dpo, &fed->fd_dpo);
427 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800428 /*
Neale Ranns2303cb12018-02-21 04:57:17 -0800429 * use the drop DPO is nothing else is present
430 */
431 if (!dpo_id_is_valid(dpo))
432 {
433 dpo_copy(dpo, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
434 }
435
436 /*
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800437 * don't allow the special index indicating replicate.vs.load-balance
438 * to escape to the clients
439 */
440 dpo->dpoi_index &= ~MPLS_IS_REPLICATE;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100441}
442
443const dpo_id_t *
444fib_entry_contribute_ip_forwarding (fib_node_index_t fib_entry_index)
445{
Neale Rannsad422ed2016-11-02 14:20:04 +0000446 fib_forward_chain_type_t fct;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100447 fib_entry_t *fib_entry;
448
449 fib_entry = fib_entry_get(fib_entry_index);
Neale Rannsad422ed2016-11-02 14:20:04 +0000450 fct = fib_entry_get_default_chain_type(fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100451
Neale Rannsad422ed2016-11-02 14:20:04 +0000452 ASSERT((fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP4 ||
453 fct == FIB_FORW_CHAIN_TYPE_UNICAST_IP6));
454
Neale Ranns2303cb12018-02-21 04:57:17 -0800455 if (dpo_id_is_valid(&fib_entry->fe_lb))
456 {
457 return (&fib_entry->fe_lb);
458 }
459
460 return (drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100461}
462
463adj_index_t
464fib_entry_get_adj (fib_node_index_t fib_entry_index)
465{
466 const dpo_id_t *dpo;
467
468 dpo = fib_entry_contribute_ip_forwarding(fib_entry_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100469
Neale Rannsca193612017-06-14 06:50:08 -0700470 if (dpo_id_is_valid(dpo))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100471 {
Neale Rannsca193612017-06-14 06:50:08 -0700472 dpo = load_balance_get_bucket(dpo->dpoi_index, 0);
473
474 if (dpo_is_adj(dpo))
475 {
476 return (dpo->dpoi_index);
477 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100478 }
479 return (ADJ_INDEX_INVALID);
480}
481
482fib_node_index_t
483fib_entry_get_path_list (fib_node_index_t fib_entry_index)
484{
485 fib_entry_t *fib_entry;
486
487 fib_entry = fib_entry_get(fib_entry_index);
488
489 return (fib_entry->fe_parent);
490}
491
492u32
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100493fib_entry_child_add (fib_node_index_t fib_entry_index,
494 fib_node_type_t child_type,
495 fib_node_index_t child_index)
496{
497 return (fib_node_child_add(FIB_NODE_TYPE_ENTRY,
498 fib_entry_index,
499 child_type,
500 child_index));
501};
502
503void
504fib_entry_child_remove (fib_node_index_t fib_entry_index,
505 u32 sibling_index)
506{
507 fib_node_child_remove(FIB_NODE_TYPE_ENTRY,
508 fib_entry_index,
509 sibling_index);
Neale Rannsad422ed2016-11-02 14:20:04 +0000510
511 if (0 == fib_node_get_n_children(FIB_NODE_TYPE_ENTRY,
512 fib_entry_index))
513 {
514 /*
515 * if there are no children left then there is no reason to keep
516 * the non-default forwarding chains. those chains are built only
517 * because the children want them.
518 */
519 fib_entry_delegate_type_t fdt;
520 fib_entry_delegate_t *fed;
521 fib_entry_t *fib_entry;
522
523 fib_entry = fib_entry_get(fib_entry_index);
524
525 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
526 {
527 dpo_reset(&fed->fd_dpo);
528 fib_entry_delegate_remove(fib_entry, fdt);
529 });
530 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100531}
532
533static fib_entry_t *
534fib_entry_alloc (u32 fib_index,
535 const fib_prefix_t *prefix,
536 fib_node_index_t *fib_entry_index)
537{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100538 fib_entry_t *fib_entry;
Neale Rannsad422ed2016-11-02 14:20:04 +0000539 fib_prefix_t *fep;
Damjan Marion66d4cb52022-03-17 18:59:46 +0100540 u8 need_barrier_sync = pool_get_will_expand (fib_entry_pool);
Stanislav Zaikin754cffb2021-09-28 18:31:32 +0200541 vlib_main_t *vm = vlib_get_main();
542 ASSERT (vm->thread_index == 0);
543
Stanislav Zaikin754cffb2021-09-28 18:31:32 +0200544 if (need_barrier_sync)
545 vlib_worker_thread_barrier_sync (vm);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100546
547 pool_get(fib_entry_pool, fib_entry);
Stanislav Zaikin754cffb2021-09-28 18:31:32 +0200548
549 if (need_barrier_sync)
550 vlib_worker_thread_barrier_release (vm);
551
Dave Barachb7b92992018-10-17 10:38:51 -0400552 clib_memset(fib_entry, 0, sizeof(*fib_entry));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100553
554 fib_node_init(&fib_entry->fe_node,
555 FIB_NODE_TYPE_ENTRY);
556
557 fib_entry->fe_fib_index = fib_index;
Neale Rannsad422ed2016-11-02 14:20:04 +0000558
559 /*
560 * the one time we need to update the const prefix is when
561 * the entry is first created
562 */
563 fep = (fib_prefix_t*)&(fib_entry->fe_prefix);
564 *fep = *prefix;
565
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100566 if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
567 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000568 fep->fp_len = 21;
569 if (MPLS_NON_EOS == fep->fp_eos)
570 {
571 fep->fp_payload_proto = DPO_PROTO_MPLS;
572 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100573 ASSERT(DPO_PROTO_NONE != fib_entry->fe_prefix.fp_payload_proto);
574 }
575
Neale Rannsad422ed2016-11-02 14:20:04 +0000576 dpo_reset(&fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100577
578 *fib_entry_index = fib_entry_get_index(fib_entry);
579
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100580 return (fib_entry);
581}
582
Neale Ranns08a70f12017-02-24 06:16:01 -0800583static fib_entry_t*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100584fib_entry_post_flag_update_actions (fib_entry_t *fib_entry,
Neale Ranns50bd1d32021-10-08 07:16:12 +0000585 fib_entry_flag_t old_flags,
586 u32 new_fib_index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100587{
Neale Ranns08a70f12017-02-24 06:16:01 -0800588 fib_node_index_t fei;
589
590 /*
591 * save the index so we can recover from pool reallocs
592 */
593 fei = fib_entry_get_index(fib_entry);
594
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100595 /*
596 * handle changes to attached export for import entries
597 */
598 int is_import = (FIB_ENTRY_FLAG_IMPORT & fib_entry_get_flags_i(fib_entry));
599 int was_import = (FIB_ENTRY_FLAG_IMPORT & old_flags);
600
601 if (!was_import && is_import)
602 {
603 /*
604 * transition from not exported to exported
605 */
606
607 /*
608 * there is an assumption here that the entry resolves via only
609 * one interface and that it is the cross VRF interface.
610 */
Neale Ranns50bd1d32021-10-08 07:16:12 +0000611 if (~0 == new_fib_index)
612 {
613 u32 sw_if_index = fib_path_list_get_resolving_interface(fib_entry->fe_parent);
614 new_fib_index = fib_table_get_index_for_sw_if_index(
615 fib_entry_get_proto(fib_entry),
616 sw_if_index);
617 }
618 fib_attached_export_import(fib_entry, new_fib_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100619 }
620 else if (was_import && !is_import)
621 {
622 /*
623 * transition from exported to not exported
624 */
625 fib_attached_export_purge(fib_entry);
626 }
Neale Ranns50bd1d32021-10-08 07:16:12 +0000627 else if (was_import && is_import && ~0 != new_fib_index)
628 {
629 /*
630 * transition from export from one table to another
631 */
632 fib_attached_export_purge(fib_entry);
633 fib_attached_export_import(fib_entry, new_fib_index);
634 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100635 /*
636 * else
637 * no change. nothing to do.
638 */
639
640 /*
Neale Ranns08a70f12017-02-24 06:16:01 -0800641 * reload the entry address post possible pool realloc
642 */
643 fib_entry = fib_entry_get(fei);
644
645 /*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100646 * handle changes to attached export for export entries
647 */
648 int is_attached = (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(fib_entry));
649 int was_attached = (FIB_ENTRY_FLAG_ATTACHED & old_flags);
650
651 if (!was_attached && is_attached)
652 {
653 /*
654 * transition to attached. time to export
655 */
656 // FIXME
657 }
658 // else FIXME
Neale Ranns08a70f12017-02-24 06:16:01 -0800659
660 return (fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100661}
662
Neale Ranns981a6902019-11-21 09:37:41 +0000663static fib_entry_t*
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100664fib_entry_post_install_actions (fib_entry_t *fib_entry,
665 fib_source_t source,
666 fib_entry_flag_t old_flags)
667{
Neale Ranns50bd1d32021-10-08 07:16:12 +0000668 fib_entry = fib_entry_post_flag_update_actions(fib_entry, old_flags, ~0);
Neale Ranns6ede5702020-02-13 09:12:36 +0000669 fib_entry = fib_entry_src_action_installed(fib_entry, source);
Neale Ranns981a6902019-11-21 09:37:41 +0000670
671 return (fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100672}
673
674fib_node_index_t
675fib_entry_create (u32 fib_index,
676 const fib_prefix_t *prefix,
677 fib_source_t source,
678 fib_entry_flag_t flags,
679 const fib_route_path_t *paths)
680{
681 fib_node_index_t fib_entry_index;
682 fib_entry_t *fib_entry;
683
684 ASSERT(0 < vec_len(paths));
685
686 fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
687
688 /*
689 * since this is a new entry create, we don't need to check for winning
690 * sources - there is only one.
691 */
692 fib_entry = fib_entry_src_action_add(fib_entry, source, flags,
693 drop_dpo_get(
694 fib_proto_to_dpo(
695 fib_entry_get_proto(fib_entry))));
696 fib_entry_src_action_path_swap(fib_entry,
697 source,
698 flags,
699 paths);
700 /*
701 * handle possible realloc's by refetching the pointer
702 */
703 fib_entry = fib_entry_get(fib_entry_index);
704 fib_entry_src_action_activate(fib_entry, source);
705
Neale Ranns981a6902019-11-21 09:37:41 +0000706 fib_entry = fib_entry_post_install_actions(fib_entry, source,
707 FIB_ENTRY_FLAG_NONE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100708
Neale Ranns710071b2018-09-24 12:36:26 +0000709 FIB_ENTRY_DBG(fib_entry, "create");
710
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100711 return (fib_entry_index);
712}
713
714fib_node_index_t
715fib_entry_create_special (u32 fib_index,
716 const fib_prefix_t *prefix,
717 fib_source_t source,
718 fib_entry_flag_t flags,
719 const dpo_id_t *dpo)
720{
721 fib_node_index_t fib_entry_index;
722 fib_entry_t *fib_entry;
723
724 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700725 * create and initialize the new enty
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100726 */
727 fib_entry = fib_entry_alloc(fib_index, prefix, &fib_entry_index);
728
729 /*
730 * create the path-list
731 */
732 fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
733 fib_entry_src_action_activate(fib_entry, source);
734
Neale Ranns981a6902019-11-21 09:37:41 +0000735 fib_entry = fib_entry_post_install_actions(fib_entry, source,
736 FIB_ENTRY_FLAG_NONE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100737
Neale Ranns710071b2018-09-24 12:36:26 +0000738 FIB_ENTRY_DBG(fib_entry, "create-special");
739
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100740 return (fib_entry_index);
741}
742
743static void
744fib_entry_post_update_actions (fib_entry_t *fib_entry,
745 fib_source_t source,
746 fib_entry_flag_t old_flags)
747{
748 /*
749 * backwalk to children to inform then of the change to forwarding.
750 */
751 fib_node_back_walk_ctx_t bw_ctx = {
752 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE,
753 };
754
755 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_get_index(fib_entry), &bw_ctx);
756
757 /*
758 * then inform any covered prefixes
759 */
760 fib_entry_cover_update_notify(fib_entry);
761
762 fib_entry_post_install_actions(fib_entry, source, old_flags);
763}
764
Neale Ranns89541992017-04-06 04:41:02 -0700765void
Neale Ranns2303cb12018-02-21 04:57:17 -0800766fib_entry_recalculate_forwarding (fib_node_index_t fib_entry_index)
Neale Ranns948e00f2016-10-20 13:39:34 +0100767{
Neale Ranns2303cb12018-02-21 04:57:17 -0800768 fib_source_t best_source;
769 fib_entry_t *fib_entry;
770 fib_entry_src_t *bsrc;
Neale Ranns89541992017-04-06 04:41:02 -0700771
Neale Ranns2303cb12018-02-21 04:57:17 -0800772 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns89541992017-04-06 04:41:02 -0700773
Neale Ranns2303cb12018-02-21 04:57:17 -0800774 bsrc = fib_entry_get_best_src_i(fib_entry);
775 best_source = fib_entry_src_get_source(bsrc);
776
777 fib_entry_src_action_reactivate(fib_entry, best_source);
778}
779
780static void
781fib_entry_source_change_w_flags (fib_entry_t *fib_entry,
782 fib_source_t old_source,
783 fib_entry_flag_t old_flags,
784 fib_source_t new_source)
785{
Neale Ranns3bab8f92019-12-04 06:11:00 +0000786 switch (fib_source_cmp(new_source, old_source))
Neale Ranns948e00f2016-10-20 13:39:34 +0100787 {
Neale Ranns3bab8f92019-12-04 06:11:00 +0000788 case FIB_SOURCE_CMP_BETTER:
Neale Ranns948e00f2016-10-20 13:39:34 +0100789 /*
790 * we have a new winning source.
791 */
Neale Ranns89541992017-04-06 04:41:02 -0700792 fib_entry_src_action_deactivate(fib_entry, old_source);
Neale Ranns948e00f2016-10-20 13:39:34 +0100793 fib_entry_src_action_activate(fib_entry, new_source);
Neale Ranns3bab8f92019-12-04 06:11:00 +0000794 break;
795
796 case FIB_SOURCE_CMP_WORSE:
Neale Ranns2303cb12018-02-21 04:57:17 -0800797 /*
798 * the new source loses. Re-activate the winning sources
799 * in case it is an interposer and hence relied on the losing
800 * source's path-list.
801 */
802 fib_entry_src_action_reactivate(fib_entry, old_source);
803 return;
Neale Ranns3bab8f92019-12-04 06:11:00 +0000804
805 case FIB_SOURCE_CMP_EQUAL:
Neale Ranns948e00f2016-10-20 13:39:34 +0100806 /*
807 * the new source is one this entry already has.
808 * But the path-list was updated, which will contribute new forwarding,
809 * so install it.
810 */
Neale Ranns89541992017-04-06 04:41:02 -0700811 fib_entry_src_action_reactivate(fib_entry, new_source);
Neale Ranns3bab8f92019-12-04 06:11:00 +0000812 break;
Neale Ranns948e00f2016-10-20 13:39:34 +0100813 }
814
815 fib_entry_post_update_actions(fib_entry, new_source, old_flags);
816}
817
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100818void
Neale Ranns2303cb12018-02-21 04:57:17 -0800819fib_entry_source_change (fib_entry_t *fib_entry,
820 fib_source_t old_source,
821 fib_source_t new_source)
822{
823 fib_entry_flag_t old_flags;
824
825 old_flags = fib_entry_get_flags_for_source(
826 fib_entry_get_index(fib_entry), old_source);
827
828 return (fib_entry_source_change_w_flags(fib_entry, old_source,
829 old_flags, new_source));
830}
831
832void
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100833fib_entry_special_add (fib_node_index_t fib_entry_index,
834 fib_source_t source,
835 fib_entry_flag_t flags,
836 const dpo_id_t *dpo)
837{
838 fib_source_t best_source;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100839 fib_entry_t *fib_entry;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100840
841 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns89541992017-04-06 04:41:02 -0700842 best_source = fib_entry_get_best_source(fib_entry_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100843
844 fib_entry = fib_entry_src_action_add(fib_entry, source, flags, dpo);
Neale Ranns89541992017-04-06 04:41:02 -0700845 fib_entry_source_change(fib_entry, best_source, source);
Neale Ranns710071b2018-09-24 12:36:26 +0000846 FIB_ENTRY_DBG(fib_entry, "special-add:%U", format_fib_source, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100847}
848
849void
Neale Ranns948e00f2016-10-20 13:39:34 +0100850fib_entry_special_update (fib_node_index_t fib_entry_index,
851 fib_source_t source,
852 fib_entry_flag_t flags,
853 const dpo_id_t *dpo)
854{
855 fib_source_t best_source;
Neale Ranns948e00f2016-10-20 13:39:34 +0100856 fib_entry_t *fib_entry;
Neale Ranns948e00f2016-10-20 13:39:34 +0100857
858 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns89541992017-04-06 04:41:02 -0700859 best_source = fib_entry_get_best_source(fib_entry_index);
Neale Ranns948e00f2016-10-20 13:39:34 +0100860
861 fib_entry = fib_entry_src_action_update(fib_entry, source, flags, dpo);
Neale Ranns89541992017-04-06 04:41:02 -0700862 fib_entry_source_change(fib_entry, best_source, source);
Neale Ranns710071b2018-09-24 12:36:26 +0000863
864 FIB_ENTRY_DBG(fib_entry, "special-updated:%U", format_fib_source, source);
Neale Ranns948e00f2016-10-20 13:39:34 +0100865}
866
867
868void
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100869fib_entry_path_add (fib_node_index_t fib_entry_index,
870 fib_source_t source,
871 fib_entry_flag_t flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700872 const fib_route_path_t *rpaths)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100873{
874 fib_source_t best_source;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100875 fib_entry_t *fib_entry;
Neale Rannsa4e77662017-12-04 20:00:30 +0000876 fib_entry_src_t *bsrc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100877
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100878 fib_entry = fib_entry_get(fib_entry_index);
879 ASSERT(NULL != fib_entry);
880
881 bsrc = fib_entry_get_best_src_i(fib_entry);
882 best_source = fib_entry_src_get_source(bsrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100883
Neale Ranns097fa662018-05-01 05:17:55 -0700884 fib_entry = fib_entry_src_action_path_add(fib_entry, source, flags, rpaths);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100885
Neale Ranns2303cb12018-02-21 04:57:17 -0800886 fib_entry_source_change(fib_entry, best_source, source);
Neale Ranns710071b2018-09-24 12:36:26 +0000887
888 FIB_ENTRY_DBG(fib_entry, "path add:%U", format_fib_source, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100889}
890
Neale Ranns89541992017-04-06 04:41:02 -0700891static fib_entry_src_flag_t
892fib_entry_src_burn_only_inherited (fib_entry_t *fib_entry)
893{
894 fib_entry_src_t *src;
895 fib_source_t source;
896 int has_only_inherited_sources = 1;
897
898 FOR_EACH_SRC_ADDED(fib_entry, src, source,
899 ({
900 if (!(src->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED))
901 {
902 has_only_inherited_sources = 0;
903 break;
904 }
905 }));
906 if (has_only_inherited_sources)
907 {
908 FOR_EACH_SRC_ADDED(fib_entry, src, source,
909 ({
910 fib_entry_src_action_remove(fib_entry, source);
911 }));
912 return (FIB_ENTRY_SRC_FLAG_NONE);
913 }
914 else
915 {
916 return (FIB_ENTRY_SRC_FLAG_ADDED);
917 }
918}
919
920static fib_entry_src_flag_t
921fib_entry_source_removed (fib_entry_t *fib_entry,
922 fib_entry_flag_t old_flags)
923{
924 const fib_entry_src_t *bsrc;
925 fib_source_t best_source;
926
927 /*
928 * if all that is left are inherited sources, then burn them
929 */
930 fib_entry_src_burn_only_inherited(fib_entry);
931
932 bsrc = fib_entry_get_best_src_i(fib_entry);
933 best_source = fib_entry_src_get_source(bsrc);
934
Neale Ranns3bab8f92019-12-04 06:11:00 +0000935 if (FIB_SOURCE_INVALID == best_source)
Neale Ranns2303cb12018-02-21 04:57:17 -0800936 {
Neale Ranns89541992017-04-06 04:41:02 -0700937 /*
938 * no more sources left. this entry is toast.
939 */
Neale Ranns50bd1d32021-10-08 07:16:12 +0000940 fib_entry = fib_entry_post_flag_update_actions(fib_entry, old_flags, ~0);
Neale Ranns89541992017-04-06 04:41:02 -0700941 fib_entry_src_action_uninstall(fib_entry);
942
943 return (FIB_ENTRY_SRC_FLAG_NONE);
944 }
945 else
946 {
947 fib_entry_src_action_activate(fib_entry, best_source);
948 }
949
950 fib_entry_post_update_actions(fib_entry, best_source, old_flags);
951
952 /*
953 * still have sources
954 */
955 return (FIB_ENTRY_SRC_FLAG_ADDED);
956}
957
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100958/*
959 * fib_entry_path_remove
960 *
961 * remove a path from the entry.
962 * return the fib_entry's index if it is still present, INVALID otherwise.
963 */
964fib_entry_src_flag_t
965fib_entry_path_remove (fib_node_index_t fib_entry_index,
966 fib_source_t source,
Neale Ranns097fa662018-05-01 05:17:55 -0700967 const fib_route_path_t *rpaths)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100968{
969 fib_entry_src_flag_t sflag;
970 fib_source_t best_source;
971 fib_entry_flag_t bflags;
972 fib_entry_t *fib_entry;
Neale Rannsa4e77662017-12-04 20:00:30 +0000973 fib_entry_src_t *bsrc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100974
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100975 fib_entry = fib_entry_get(fib_entry_index);
976 ASSERT(NULL != fib_entry);
977
978 bsrc = fib_entry_get_best_src_i(fib_entry);
979 best_source = fib_entry_src_get_source(bsrc);
980 bflags = fib_entry_src_get_flags(bsrc);
981
Neale Ranns097fa662018-05-01 05:17:55 -0700982 sflag = fib_entry_src_action_path_remove(fib_entry, source, rpaths);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100983
Neale Ranns710071b2018-09-24 12:36:26 +0000984 FIB_ENTRY_DBG(fib_entry, "path remove:%U", format_fib_source, source);
985
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100986 /*
987 * if the path list for the source passed is invalid,
988 * then we need to create a new one. else we are updating
989 * an existing.
990 */
Neale Ranns3bab8f92019-12-04 06:11:00 +0000991 switch (fib_source_cmp(source, best_source))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100992 {
Neale Ranns3bab8f92019-12-04 06:11:00 +0000993 case FIB_SOURCE_CMP_BETTER:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100994 /*
995 * Que! removing a path from a source that is better than the
996 * one this entry is using.
997 */
998 ASSERT(0);
Neale Ranns3bab8f92019-12-04 06:11:00 +0000999 break;
1000 case FIB_SOURCE_CMP_WORSE:
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001001 /*
Neale Ranns89541992017-04-06 04:41:02 -07001002 * the source is not the best. no need to update forwarding
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001003 */
Neale Ranns89541992017-04-06 04:41:02 -07001004 if (FIB_ENTRY_SRC_FLAG_ADDED & sflag)
1005 {
1006 /*
1007 * the source being removed still has paths
1008 */
1009 return (FIB_ENTRY_SRC_FLAG_ADDED);
1010 }
1011 else
1012 {
1013 /*
1014 * that was the last path from this source, check if those
1015 * that remain are non-inherited
1016 */
1017 return (fib_entry_src_burn_only_inherited(fib_entry));
Neale Ranns3bab8f92019-12-04 06:11:00 +00001018 }
1019 break;
1020 case FIB_SOURCE_CMP_EQUAL:
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001021 /*
1022 * removing a path from the path-list we were using.
1023 */
1024 if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
1025 {
1026 /*
1027 * the last path from the source was removed.
1028 * fallback to lower source
1029 */
Neale Ranns89541992017-04-06 04:41:02 -07001030 return (fib_entry_source_removed(fib_entry, bflags));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001031 }
1032 else
1033 {
1034 /*
1035 * re-install the new forwarding information
1036 */
Neale Ranns89541992017-04-06 04:41:02 -07001037 fib_entry_src_action_reactivate(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001038 }
Neale Ranns3bab8f92019-12-04 06:11:00 +00001039 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001040 }
1041
1042 fib_entry_post_update_actions(fib_entry, source, bflags);
1043
1044 /*
1045 * still have sources
1046 */
1047 return (FIB_ENTRY_SRC_FLAG_ADDED);
1048}
1049
1050/*
1051 * fib_entry_special_remove
1052 *
1053 * remove a special source from the entry.
1054 * return the fib_entry's index if it is still present, INVALID otherwise.
1055 */
1056fib_entry_src_flag_t
1057fib_entry_special_remove (fib_node_index_t fib_entry_index,
1058 fib_source_t source)
1059{
1060 fib_entry_src_flag_t sflag;
1061 fib_source_t best_source;
1062 fib_entry_flag_t bflags;
1063 fib_entry_t *fib_entry;
Neale Rannsa4e77662017-12-04 20:00:30 +00001064 fib_entry_src_t *bsrc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001065
1066 fib_entry = fib_entry_get(fib_entry_index);
1067 ASSERT(NULL != fib_entry);
1068
1069 bsrc = fib_entry_get_best_src_i(fib_entry);
1070 best_source = fib_entry_src_get_source(bsrc);
1071 bflags = fib_entry_src_get_flags(bsrc);
1072
Neale Ranns710071b2018-09-24 12:36:26 +00001073 FIB_ENTRY_DBG(fib_entry, "special remove:%U", format_fib_source, source);
1074
Neale Ranns8164a032019-03-26 07:25:11 -07001075 sflag = fib_entry_src_action_remove_or_update_inherit(fib_entry, source);
1076
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001077 /*
1078 * if the path list for the source passed is invalid,
1079 * then we need to create a new one. else we are updating
1080 * an existing.
1081 */
Neale Ranns3bab8f92019-12-04 06:11:00 +00001082 switch (fib_source_cmp(source, best_source))
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001083 {
Neale Ranns3bab8f92019-12-04 06:11:00 +00001084 case FIB_SOURCE_CMP_BETTER:
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001085 /*
1086 * Que! removing a path from a source that is better than the
1087 * one this entry is using. This can only mean it is a source
1088 * this prefix does not have.
1089 */
1090 return (FIB_ENTRY_SRC_FLAG_ADDED);
Neale Ranns3bab8f92019-12-04 06:11:00 +00001091
1092 case FIB_SOURCE_CMP_WORSE:
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001093 /*
Neale Ranns89541992017-04-06 04:41:02 -07001094 * the source is not the best. no need to update forwarding
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001095 */
Neale Ranns89541992017-04-06 04:41:02 -07001096 if (FIB_ENTRY_SRC_FLAG_ADDED & sflag)
1097 {
1098 /*
1099 * the source being removed still has paths
1100 */
1101 return (FIB_ENTRY_SRC_FLAG_ADDED);
1102 }
1103 else
1104 {
1105 /*
1106 * that was the last path from this source, check if those
1107 * that remain are non-inherited
1108 */
1109 if (FIB_ENTRY_SRC_FLAG_NONE == fib_entry_src_burn_only_inherited(fib_entry))
1110 {
1111 /*
1112 * no more sources left. this entry is toast.
1113 */
Neale Ranns50bd1d32021-10-08 07:16:12 +00001114 fib_entry = fib_entry_post_flag_update_actions(fib_entry, bflags, ~0);
Neale Ranns89541992017-04-06 04:41:02 -07001115 fib_entry_src_action_uninstall(fib_entry);
1116 return (FIB_ENTRY_SRC_FLAG_NONE);
1117 }
Neale Ranns2303cb12018-02-21 04:57:17 -08001118
1119 /*
1120 * reactivate the best source so the interposer gets restacked
1121 */
1122 fib_entry_src_action_reactivate(fib_entry, best_source);
1123
Neale Ranns89541992017-04-06 04:41:02 -07001124 return (FIB_ENTRY_SRC_FLAG_ADDED);
1125 }
Neale Ranns3bab8f92019-12-04 06:11:00 +00001126 break;
1127
1128 case FIB_SOURCE_CMP_EQUAL:
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001129 if (!(FIB_ENTRY_SRC_FLAG_ADDED & sflag))
1130 {
1131 /*
1132 * the source was removed. use the next best.
1133 */
Neale Ranns89541992017-04-06 04:41:02 -07001134 return (fib_entry_source_removed(fib_entry, bflags));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001135 }
1136 else
1137 {
1138 /*
1139 * re-install the new forwarding information
1140 */
1141 fib_entry_src_action_reactivate(fib_entry, source);
1142 }
Neale Ranns3bab8f92019-12-04 06:11:00 +00001143 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001144 }
1145
1146 fib_entry_post_update_actions(fib_entry, source, bflags);
1147
1148 /*
1149 * still have sources
1150 */
1151 return (FIB_ENTRY_SRC_FLAG_ADDED);
1152}
1153
1154/**
Neale Ranns89541992017-04-06 04:41:02 -07001155 * fib_entry_inherit
1156 *
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001157 * If the source on the cover is inheriting then push this source
Neale Ranns89541992017-04-06 04:41:02 -07001158 * down to the covered.
1159 */
1160void
1161fib_entry_inherit (fib_node_index_t cover,
1162 fib_node_index_t covered)
1163{
1164 fib_entry_src_inherit(fib_entry_get(cover),
1165 fib_entry_get(covered));
1166}
1167
1168/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001169 * fib_entry_delete
1170 *
1171 * The source is withdrawing all the paths it provided
1172 */
1173fib_entry_src_flag_t
1174fib_entry_delete (fib_node_index_t fib_entry_index,
1175 fib_source_t source)
1176{
1177 return (fib_entry_special_remove(fib_entry_index, source));
1178}
1179
1180/**
1181 * fib_entry_update
1182 *
1183 * The source has provided a new set of paths that will replace the old.
1184 */
1185void
1186fib_entry_update (fib_node_index_t fib_entry_index,
1187 fib_source_t source,
1188 fib_entry_flag_t flags,
1189 const fib_route_path_t *paths)
1190{
1191 fib_source_t best_source;
1192 fib_entry_flag_t bflags;
1193 fib_entry_t *fib_entry;
Neale Rannsa4e77662017-12-04 20:00:30 +00001194 fib_entry_src_t *bsrc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001195
1196 fib_entry = fib_entry_get(fib_entry_index);
1197 ASSERT(NULL != fib_entry);
1198
1199 bsrc = fib_entry_get_best_src_i(fib_entry);
1200 best_source = fib_entry_src_get_source(bsrc);
Neale Ranns2303cb12018-02-21 04:57:17 -08001201 bflags = fib_entry_get_flags_i(fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001202
Neale Ranns2303cb12018-02-21 04:57:17 -08001203 fib_entry = fib_entry_src_action_path_swap(fib_entry,
1204 source,
1205 flags,
1206 paths);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001207
Neale Ranns2303cb12018-02-21 04:57:17 -08001208 fib_entry_source_change_w_flags(fib_entry, best_source, bflags, source);
Neale Ranns710071b2018-09-24 12:36:26 +00001209 FIB_ENTRY_DBG(fib_entry, "update");
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001210}
1211
1212
1213/*
1214 * fib_entry_cover_changed
1215 *
1216 * this entry is tracking its cover and that cover has changed.
1217 */
1218void
1219fib_entry_cover_changed (fib_node_index_t fib_entry_index)
1220{
1221 fib_entry_src_cover_res_t res = {
1222 .install = !0,
1223 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
1224 };
Neale Ranns2303cb12018-02-21 04:57:17 -08001225 CLIB_UNUSED(fib_source_t source);
1226 fib_source_t best_source;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001227 fib_entry_flag_t bflags;
1228 fib_entry_t *fib_entry;
1229 fib_entry_src_t *esrc;
1230 u32 index;
1231
1232 bflags = FIB_ENTRY_FLAG_NONE;
1233 best_source = FIB_SOURCE_FIRST;
1234 fib_entry = fib_entry_get(fib_entry_index);
1235
1236 fib_attached_export_cover_change(fib_entry);
1237
1238 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001239 * propagate the notification to each of the added sources
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001240 */
1241 index = 0;
1242 FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
1243 ({
1244 if (0 == index)
1245 {
1246 /*
1247 * only the best source gets to set the back walk flags
1248 */
Neale Ranns2303cb12018-02-21 04:57:17 -08001249 res = fib_entry_src_action_cover_change(fib_entry, esrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001250 bflags = fib_entry_src_get_flags(esrc);
1251 best_source = fib_entry_src_get_source(esrc);
1252 }
1253 else
1254 {
Neale Ranns2303cb12018-02-21 04:57:17 -08001255 fib_entry_src_action_cover_change(fib_entry, esrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001256 }
1257 index++;
1258 }));
1259
1260 if (res.install)
1261 {
1262 fib_entry_src_action_reactivate(fib_entry,
1263 fib_entry_src_get_source(
1264 fib_entry_get_best_src_i(fib_entry)));
Neale Ranns981a6902019-11-21 09:37:41 +00001265 fib_entry = fib_entry_post_install_actions(fib_entry,
1266 best_source,
1267 bflags);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001268 }
1269 else
1270 {
1271 fib_entry_src_action_uninstall(fib_entry);
1272 }
1273
1274 if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
1275 {
1276 /*
1277 * time for walkies fido.
1278 */
1279 fib_node_back_walk_ctx_t bw_ctx = {
1280 .fnbw_reason = res.bw_reason,
1281 };
1282
1283 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
1284 }
Neale Ranns710071b2018-09-24 12:36:26 +00001285 FIB_ENTRY_DBG(fib_entry, "cover-changed");
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001286}
1287
1288/*
1289 * fib_entry_cover_updated
1290 *
1291 * this entry is tracking its cover and that cover has been updated
1292 * (i.e. its forwarding information has changed).
1293 */
1294void
1295fib_entry_cover_updated (fib_node_index_t fib_entry_index)
1296{
1297 fib_entry_src_cover_res_t res = {
1298 .install = !0,
1299 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
1300 };
Neale Ranns2303cb12018-02-21 04:57:17 -08001301 CLIB_UNUSED(fib_source_t source);
1302 fib_source_t best_source;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001303 fib_entry_flag_t bflags;
1304 fib_entry_t *fib_entry;
1305 fib_entry_src_t *esrc;
1306 u32 index;
1307
1308 bflags = FIB_ENTRY_FLAG_NONE;
1309 best_source = FIB_SOURCE_FIRST;
1310 fib_entry = fib_entry_get(fib_entry_index);
1311
1312 fib_attached_export_cover_update(fib_entry);
1313
1314 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001315 * propagate the notification to each of the added sources
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001316 */
1317 index = 0;
1318 FOR_EACH_SRC_ADDED(fib_entry, esrc, source,
1319 ({
1320 if (0 == index)
1321 {
1322 /*
Neale Ranns8f5fef22020-12-21 08:29:34 +00001323 * only the best source gets to set the install result
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001324 */
Neale Ranns2303cb12018-02-21 04:57:17 -08001325 res = fib_entry_src_action_cover_update(fib_entry, esrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001326 bflags = fib_entry_src_get_flags(esrc);
1327 best_source = fib_entry_src_get_source(esrc);
1328 }
1329 else
1330 {
Neale Ranns8f5fef22020-12-21 08:29:34 +00001331 /*
1332 * contirubting sources can set backwalk flags
1333 */
1334 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_CONTRIBUTING)
1335 {
1336 fib_entry_src_cover_res_t tmp = {
1337 .install = !0,
1338 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
1339 };
1340
1341 tmp = fib_entry_src_action_cover_update(fib_entry, esrc);
1342 res.bw_reason |= tmp.bw_reason;
1343 }
1344 else
1345 {
1346 fib_entry_src_action_cover_update(fib_entry, esrc);
1347 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001348 }
1349 index++;
1350 }));
1351
1352 if (res.install)
1353 {
1354 fib_entry_src_action_reactivate(fib_entry,
1355 fib_entry_src_get_source(
1356 fib_entry_get_best_src_i(fib_entry)));
Neale Ranns981a6902019-11-21 09:37:41 +00001357 fib_entry = fib_entry_post_install_actions(fib_entry,
1358 best_source,
1359 bflags);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001360 }
1361 else
1362 {
1363 fib_entry_src_action_uninstall(fib_entry);
1364 }
1365
1366 if (FIB_NODE_BW_REASON_FLAG_NONE != res.bw_reason)
1367 {
1368 /*
1369 * time for walkies fido.
1370 */
1371 fib_node_back_walk_ctx_t bw_ctx = {
1372 .fnbw_reason = res.bw_reason,
1373 };
1374
1375 fib_walk_sync(FIB_NODE_TYPE_ENTRY, fib_entry_index, &bw_ctx);
1376 }
Neale Ranns710071b2018-09-24 12:36:26 +00001377 FIB_ENTRY_DBG(fib_entry, "cover-updated");
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001378}
1379
1380int
1381fib_entry_recursive_loop_detect (fib_node_index_t entry_index,
1382 fib_node_index_t **entry_indicies)
1383{
1384 fib_entry_t *fib_entry;
1385 int was_looped, is_looped;
1386
1387 fib_entry = fib_entry_get(entry_index);
1388
1389 if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
1390 {
1391 fib_node_index_t *entries = *entry_indicies;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001392
1393 vec_add1(entries, entry_index);
1394 was_looped = fib_path_list_is_looped(fib_entry->fe_parent);
1395 is_looped = fib_path_list_recursive_loop_detect(fib_entry->fe_parent,
1396 &entries);
1397
1398 *entry_indicies = entries;
1399
1400 if (!!was_looped != !!is_looped)
1401 {
1402 /*
1403 * re-evaluate all the entry's forwarding
1404 * NOTE: this is an inplace modify
1405 */
Neale Rannsad422ed2016-11-02 14:20:04 +00001406 fib_entry_delegate_type_t fdt;
1407 fib_entry_delegate_t *fed;
1408
1409 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
1410 {
1411 fib_entry_src_mk_lb(fib_entry,
Neale Ranns1c59df72021-01-26 12:08:25 +00001412 fib_entry_get_best_source(entry_index),
Neale Rannsad422ed2016-11-02 14:20:04 +00001413 fib_entry_delegate_type_to_chain_type(fdt),
1414 &fed->fd_dpo);
1415 });
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001416 }
1417 }
1418 else
1419 {
1420 /*
1421 * the entry is currently not linked to a path-list. this happens
1422 * when it is this entry that is re-linking path-lists and has thus
1423 * broken the loop
1424 */
1425 is_looped = 0;
1426 }
1427
1428 return (is_looped);
1429}
1430
Neale Ranns50bd1d32021-10-08 07:16:12 +00001431/*
1432 * fib_entry_attached_cross_table
1433 *
1434 * Return true if the route is attached via an interface that
1435 * is not in the same table as the route
1436 */
1437static int
1438fib_entry_attached_cross_table (const fib_entry_t *fib_entry,
1439 u32 fib_index)
1440{
1441 const fib_prefix_t *pfx = &fib_entry->fe_prefix;
1442
1443 switch (pfx->fp_proto)
1444 {
1445 case FIB_PROTOCOL_MPLS:
1446 /* MPLS routes are never imported/exported */
1447 return (0);
1448 case FIB_PROTOCOL_IP6:
1449 /* Ignore link local addresses these also can't be imported/exported */
1450 if (ip6_address_is_link_local_unicast (&pfx->fp_addr.ip6))
1451 {
1452 return (0);
1453 }
1454 break;
1455 case FIB_PROTOCOL_IP4:
1456 break;
1457 }
1458
1459 return (fib_entry->fe_fib_index != fib_index);
1460}
1461
1462/*
1463 * fib_entry_back_walk_notify
1464 *
1465 * A back walk has reach this entry.
1466 */
1467static fib_node_back_walk_rc_t
1468fib_entry_back_walk_notify (fib_node_t *node,
1469 fib_node_back_walk_ctx_t *ctx)
1470{
1471 fib_source_t best_source;
1472 fib_entry_t *fib_entry;
1473 fib_entry_src_t *bsrc;
1474
1475 fib_entry = fib_entry_from_fib_node(node);
1476 bsrc = fib_entry_get_best_src_i(fib_entry);
1477 best_source = fib_entry_src_get_source(bsrc);
1478
1479 if (FIB_NODE_BW_REASON_FLAG_INTERFACE_BIND & ctx->fnbw_reason)
1480 {
1481 fib_entry_flag_t bflags;
1482
1483 bflags = fib_entry_src_get_flags(bsrc);
1484
1485 fib_entry_src_action_reactivate(fib_entry, best_source);
1486
1487 /* re-evaluate whether the prefix is cross table */
1488 if (fib_entry_attached_cross_table(
1489 fib_entry, ctx->interface_bind.fnbw_to_fib_index) &&
1490 !(bsrc->fes_entry_flags & FIB_ENTRY_FLAG_NO_ATTACHED_EXPORT))
1491 {
1492 bsrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1493 }
1494 else
1495 {
1496 bsrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1497 }
1498
1499 fib_entry = fib_entry_post_flag_update_actions(
1500 fib_entry, bflags,
1501 ctx->interface_bind.fnbw_to_fib_index);
1502 }
1503 else if (FIB_NODE_BW_REASON_FLAG_EVALUATE & ctx->fnbw_reason ||
1504 FIB_NODE_BW_REASON_FLAG_ADJ_UPDATE & ctx->fnbw_reason ||
1505 FIB_NODE_BW_REASON_FLAG_ADJ_DOWN & ctx->fnbw_reason ||
1506 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP & ctx->fnbw_reason ||
1507 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN & ctx->fnbw_reason ||
1508 FIB_NODE_BW_REASON_FLAG_INTERFACE_BIND & ctx->fnbw_reason ||
1509 FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE & ctx->fnbw_reason)
1510 {
1511 fib_entry_src_action_reactivate(fib_entry, best_source);
1512 }
1513
1514 /*
1515 * all other walk types can be reclassifed to a re-evaluate to
1516 * all recursive dependents.
1517 * By reclassifying we ensure that should any of these walk types meet
1518 * they can be merged.
1519 */
1520 ctx->fnbw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
1521
1522 /*
1523 * ... and nothing is forced sync from now on.
1524 */
1525 ctx->fnbw_flags &= ~FIB_NODE_BW_FLAG_FORCE_SYNC;
1526
1527 FIB_ENTRY_DBG(fib_entry, "bw:%U",
1528 format_fib_node_bw_reason, ctx->fnbw_reason);
1529
1530 /*
1531 * propagate the backwalk further if we haven't already reached the
1532 * maximum depth.
1533 */
1534 fib_walk_sync(FIB_NODE_TYPE_ENTRY,
1535 fib_entry_get_index(fib_entry),
1536 ctx);
1537
1538 return (FIB_NODE_BACK_WALK_CONTINUE);
1539}
1540
1541/*
1542 * The FIB path-list's graph node virtual function table
1543 */
1544static const fib_node_vft_t fib_entry_vft = {
1545 .fnv_get = fib_entry_get_node,
1546 .fnv_last_lock = fib_entry_last_lock_gone,
1547 .fnv_back_walk = fib_entry_back_walk_notify,
1548 .fnv_mem_show = fib_entry_show_memory,
1549};
1550
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001551u32
1552fib_entry_get_resolving_interface (fib_node_index_t entry_index)
1553{
Neale Rannsdf089a82016-10-02 16:39:06 +01001554 fib_entry_t *fib_entry;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001555
1556 fib_entry = fib_entry_get(entry_index);
1557
1558 return (fib_path_list_get_resolving_interface(fib_entry->fe_parent));
1559}
1560
Neale Rannscc4f7e12020-10-26 10:44:54 +00001561u32
1562fib_entry_get_any_resolving_interface (fib_node_index_t entry_index)
1563{
1564 const fib_entry_src_t *src;
1565 fib_entry_t *fib_entry;
1566 fib_source_t source;
1567 u32 sw_if_index;
1568
1569 fib_entry = fib_entry_get(entry_index);
1570
1571 FOR_EACH_SRC_ADDED(fib_entry, src, source,
1572 ({
1573 sw_if_index = fib_entry_get_resolving_interface_for_source (entry_index,
1574 source);
1575
1576 if (~0 != sw_if_index)
1577 break;
1578 }));
1579 return (sw_if_index);
1580}
1581
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001582fib_source_t
1583fib_entry_get_best_source (fib_node_index_t entry_index)
1584{
1585 fib_entry_t *fib_entry;
Neale Rannsa4e77662017-12-04 20:00:30 +00001586 fib_entry_src_t *bsrc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001587
1588 fib_entry = fib_entry_get(entry_index);
1589
1590 bsrc = fib_entry_get_best_src_i(fib_entry);
1591 return (fib_entry_src_get_source(bsrc));
1592}
1593
Neale Ranns88fc83e2017-04-05 08:11:14 -07001594/**
Neale Ranns56f949b2018-04-25 01:41:24 -07001595 * Return !0 is the entry represents a host prefix
1596 */
1597int
1598fib_entry_is_host (fib_node_index_t fib_entry_index)
1599{
Neale Rannsc5d43172018-07-30 08:04:40 -07001600 return (fib_prefix_is_host(fib_entry_get_prefix(fib_entry_index)));
Neale Ranns56f949b2018-04-25 01:41:24 -07001601}
1602
1603/**
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001604 * Return !0 is the entry is resolved, i.e. will return a valid forwarding
Neale Ranns88fc83e2017-04-05 08:11:14 -07001605 * chain
1606 */
1607int
1608fib_entry_is_resolved (fib_node_index_t fib_entry_index)
1609{
1610 fib_entry_delegate_t *fed;
1611 fib_entry_t *fib_entry;
1612
1613 fib_entry = fib_entry_get(fib_entry_index);
1614
Neale Ranns1f50bf82019-07-16 15:28:52 +00001615 fed = fib_entry_delegate_find(fib_entry, FIB_ENTRY_DELEGATE_BFD);
Neale Ranns88fc83e2017-04-05 08:11:14 -07001616
1617 if (NULL == fed)
1618 {
1619 /*
Neale Ranns57b58602017-07-15 07:37:25 -07001620 * no BFD tracking - consider it resolved.
Neale Ranns88fc83e2017-04-05 08:11:14 -07001621 */
1622 return (!0);
1623 }
1624 else
1625 {
1626 /*
1627 * defer to the state of the BFD tracking
1628 */
1629 return (FIB_BFD_STATE_UP == fed->fd_bfd_state);
1630 }
1631}
1632
Neale Ranns227038a2017-04-21 01:07:59 -07001633void
1634fib_entry_set_flow_hash_config (fib_node_index_t fib_entry_index,
1635 flow_hash_config_t hash_config)
1636{
1637 fib_entry_t *fib_entry;
1638
1639 fib_entry = fib_entry_get(fib_entry_index);
1640
1641 /*
1642 * pass the hash-config on to the load-balance object where it is cached.
1643 * we can ignore LBs in the delegate chains, since they will not be of the
1644 * correct protocol type (i.e. they are not IP)
1645 * There's no way, nor need, to change the hash config for MPLS.
1646 */
1647 if (dpo_id_is_valid(&fib_entry->fe_lb))
1648 {
1649 load_balance_t *lb;
1650
1651 ASSERT(DPO_LOAD_BALANCE == fib_entry->fe_lb.dpoi_type);
1652
1653 lb = load_balance_get(fib_entry->fe_lb.dpoi_index);
1654
1655 /*
1656 * atomic update for packets in flight
1657 */
1658 lb->lb_hash_config = hash_config;
1659 }
1660}
1661
Neale Ranns008dbe12018-09-07 09:32:36 -07001662u32
1663fib_entry_get_stats_index (fib_node_index_t fib_entry_index)
1664{
1665 fib_entry_t *fib_entry;
1666
1667 fib_entry = fib_entry_get(fib_entry_index);
1668
1669 return (fib_entry->fe_lb.dpoi_index);
1670}
1671
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001672static int
Neale Rannsad422ed2016-11-02 14:20:04 +00001673fib_ip4_address_compare (const ip4_address_t * a1,
1674 const ip4_address_t * a2)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001675{
1676 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001677 * IP addresses are unsigned ints. the return value here needs to be signed
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001678 * a simple subtraction won't cut it.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001679 * If the addresses are the same, the sort order is undefined, so phoey.
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001680 */
1681 return ((clib_net_to_host_u32(a1->data_u32) >
1682 clib_net_to_host_u32(a2->data_u32) ) ?
1683 1 : -1);
1684}
1685
1686static int
Neale Rannsad422ed2016-11-02 14:20:04 +00001687fib_ip6_address_compare (const ip6_address_t * a1,
1688 const ip6_address_t * a2)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001689{
1690 int i;
1691 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
1692 {
1693 int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) -
1694 clib_net_to_host_u16 (a2->as_u16[i]));
1695 if (cmp != 0)
1696 return cmp;
1697 }
1698 return 0;
1699}
1700
1701static int
1702fib_entry_cmp (fib_node_index_t fib_entry_index1,
1703 fib_node_index_t fib_entry_index2)
1704{
1705 fib_entry_t *fib_entry1, *fib_entry2;
1706 int cmp = 0;
1707
1708 fib_entry1 = fib_entry_get(fib_entry_index1);
1709 fib_entry2 = fib_entry_get(fib_entry_index2);
1710
1711 switch (fib_entry1->fe_prefix.fp_proto)
1712 {
1713 case FIB_PROTOCOL_IP4:
1714 cmp = fib_ip4_address_compare(&fib_entry1->fe_prefix.fp_addr.ip4,
1715 &fib_entry2->fe_prefix.fp_addr.ip4);
1716 break;
1717 case FIB_PROTOCOL_IP6:
1718 cmp = fib_ip6_address_compare(&fib_entry1->fe_prefix.fp_addr.ip6,
1719 &fib_entry2->fe_prefix.fp_addr.ip6);
1720 break;
1721 case FIB_PROTOCOL_MPLS:
1722 cmp = (fib_entry1->fe_prefix.fp_label - fib_entry2->fe_prefix.fp_label);
1723
1724 if (0 == cmp)
1725 {
1726 cmp = (fib_entry1->fe_prefix.fp_eos - fib_entry2->fe_prefix.fp_eos);
1727 }
1728 break;
1729 }
1730
1731 if (0 == cmp) {
1732 cmp = (fib_entry1->fe_prefix.fp_len - fib_entry2->fe_prefix.fp_len);
1733 }
1734 return (cmp);
1735}
1736
1737int
1738fib_entry_cmp_for_sort (void *i1, void *i2)
1739{
1740 fib_node_index_t *fib_entry_index1 = i1, *fib_entry_index2 = i2;
1741
1742 return (fib_entry_cmp(*fib_entry_index1,
1743 *fib_entry_index2));
1744}
1745
1746void
1747fib_entry_lock (fib_node_index_t fib_entry_index)
1748{
1749 fib_entry_t *fib_entry;
1750
1751 fib_entry = fib_entry_get(fib_entry_index);
1752
1753 fib_node_lock(&fib_entry->fe_node);
1754}
1755
1756void
1757fib_entry_unlock (fib_node_index_t fib_entry_index)
1758{
1759 fib_entry_t *fib_entry;
1760
1761 fib_entry = fib_entry_get(fib_entry_index);
1762
1763 fib_node_unlock(&fib_entry->fe_node);
1764}
1765
1766void
1767fib_entry_module_init (void)
1768{
Neale Ranns710071b2018-09-24 12:36:26 +00001769 fib_node_register_type(FIB_NODE_TYPE_ENTRY, &fib_entry_vft);
1770 fib_entry_logger = vlib_log_register_class("fib", "entry");
Neale Ranns1f50bf82019-07-16 15:28:52 +00001771
1772 fib_entry_track_module_init();
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001773}
1774
Neale Ranns097fa662018-05-01 05:17:55 -07001775fib_route_path_t *
1776fib_entry_encode (fib_node_index_t fib_entry_index)
Steven01b07122016-11-02 10:40:09 -07001777{
Neale Ranns775f73c2018-12-20 03:01:49 -08001778 fib_path_ext_list_t *ext_list;
Neale Ranns097fa662018-05-01 05:17:55 -07001779 fib_path_encode_ctx_t ctx = {
1780 .rpaths = NULL,
1781 };
Steven01b07122016-11-02 10:40:09 -07001782 fib_entry_t *fib_entry;
Neale Ranns775f73c2018-12-20 03:01:49 -08001783 fib_entry_src_t *bsrc;
Steven01b07122016-11-02 10:40:09 -07001784
Neale Ranns775f73c2018-12-20 03:01:49 -08001785 ext_list = NULL;
Steven01b07122016-11-02 10:40:09 -07001786 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns775f73c2018-12-20 03:01:49 -08001787 bsrc = fib_entry_get_best_src_i(fib_entry);
1788
1789 if (bsrc)
1790 {
1791 ext_list = &bsrc->fes_path_exts;
1792 }
1793
Neale Rannsa8d9f302017-02-20 09:17:02 -08001794 if (FIB_NODE_INDEX_INVALID != fib_entry->fe_parent)
1795 {
Neale Ranns775f73c2018-12-20 03:01:49 -08001796 fib_path_list_walk_w_ext(fib_entry->fe_parent,
1797 ext_list,
1798 fib_path_encode,
Neale Ranns097fa662018-05-01 05:17:55 -07001799 &ctx);
Neale Rannsa8d9f302017-02-20 09:17:02 -08001800 }
Neale Ranns097fa662018-05-01 05:17:55 -07001801
1802 return (ctx.rpaths);
Steven01b07122016-11-02 10:40:09 -07001803}
1804
Neale Rannsc5d43172018-07-30 08:04:40 -07001805const fib_prefix_t *
1806fib_entry_get_prefix (fib_node_index_t fib_entry_index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001807{
1808 fib_entry_t *fib_entry;
1809
1810 fib_entry = fib_entry_get(fib_entry_index);
Neale Rannsc5d43172018-07-30 08:04:40 -07001811
1812 return (&fib_entry->fe_prefix);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001813}
1814
1815u32
1816fib_entry_get_fib_index (fib_node_index_t fib_entry_index)
1817{
1818 fib_entry_t *fib_entry;
1819
1820 fib_entry = fib_entry_get(fib_entry_index);
1821
1822 return (fib_entry->fe_fib_index);
1823}
1824
1825u32
1826fib_entry_pool_size (void)
1827{
1828 return (pool_elts(fib_entry_pool));
1829}
1830
Matthew Smithee167e52020-07-02 17:24:17 -05001831#if CLIB_DEBUG > 0
Neale Rannscbe25aa2019-09-30 10:53:31 +00001832void
1833fib_table_assert_empty (const fib_table_t *fib_table)
1834{
1835 fib_node_index_t *fei, *feis = NULL;
1836 fib_entry_t *fib_entry;
1837
Damjan Marionb2c31b62020-12-13 21:47:40 +01001838 pool_foreach (fib_entry, fib_entry_pool)
1839 {
Neale Rannscbe25aa2019-09-30 10:53:31 +00001840 if (fib_entry->fe_fib_index == fib_table->ft_index)
1841 vec_add1 (feis, fib_entry_get_index(fib_entry));
Damjan Marionb2c31b62020-12-13 21:47:40 +01001842 }
Neale Rannscbe25aa2019-09-30 10:53:31 +00001843
1844 if (vec_len(feis))
1845 {
1846 vec_foreach (fei, feis)
1847 clib_error ("%U", format_fib_entry, *fei, FIB_ENTRY_FORMAT_DETAIL);
1848 }
1849
1850 ASSERT(0);
1851}
1852#endif
1853
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001854static clib_error_t *
1855show_fib_entry_command (vlib_main_t * vm,
1856 unformat_input_t * input,
1857 vlib_cli_command_t * cmd)
1858{
1859 fib_node_index_t fei;
1860
1861 if (unformat (input, "%d", &fei))
1862 {
1863 /*
1864 * show one in detail
1865 */
1866 if (!pool_is_free_index(fib_entry_pool, fei))
1867 {
1868 vlib_cli_output (vm, "%d@%U",
1869 fei,
1870 format_fib_entry, fei,
1871 FIB_ENTRY_FORMAT_DETAIL2);
1872 }
1873 else
1874 {
1875 vlib_cli_output (vm, "entry %d invalid", fei);
1876 }
1877 }
1878 else
1879 {
1880 /*
1881 * show all
1882 */
1883 vlib_cli_output (vm, "FIB Entries:");
Damjan Marionb2c31b62020-12-13 21:47:40 +01001884 pool_foreach_index (fei, fib_entry_pool)
1885 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001886 vlib_cli_output (vm, "%d@%U",
1887 fei,
1888 format_fib_entry, fei,
1889 FIB_ENTRY_FORMAT_BRIEF);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001890 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001891 }
1892
1893 return (NULL);
1894}
1895
1896VLIB_CLI_COMMAND (show_fib_entry, static) = {
1897 .path = "show fib entry",
1898 .function = show_fib_entry_command,
1899 .short_help = "show fib entry",
1900};