blob: c0275e898bd0989ee1be626c3bb2324f889611ba [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 <vnet/adj/adj.h>
17#include <vnet/dpo/load_balance.h>
18#include <vnet/dpo/mpls_label_dpo.h>
19#include <vnet/dpo/drop_dpo.h>
Neale Ranns0f26c5a2017-03-01 15:12:11 -080020#include <vnet/dpo/replicate_dpo.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021
Neale Ranns3ee44042016-10-03 13:05:48 +010022#include <vnet/fib/fib_entry_src.h>
23#include <vnet/fib/fib_table.h>
24#include <vnet/fib/fib_path_ext.h>
25#include <vnet/fib/fib_urpf_list.h>
Neale Ranns1f50bf82019-07-16 15:28:52 +000026#include <vnet/fib/fib_entry_delegate.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010027
28/*
29 * per-source type vft
30 */
Neale Ranns3bab8f92019-12-04 06:11:00 +000031static fib_entry_src_vft_t fib_entry_src_bh_vft[FIB_SOURCE_BH_MAX];
Neale Ranns0bfe5d82016-08-25 15:29:12 +010032
Neale Ranns2303cb12018-02-21 04:57:17 -080033/**
34 * Get the VFT for a given source. This is a combination of the source
35 * enum and the interposer flags
36 */
37const fib_entry_src_vft_t*
38fib_entry_src_get_vft (const fib_entry_src_t *esrc)
39{
Neale Ranns3bab8f92019-12-04 06:11:00 +000040 fib_source_behaviour_t bh;
41
42 bh = fib_source_get_behaviour(esrc->fes_src);
43
Neale Ranns2303cb12018-02-21 04:57:17 -080044 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_INTERPOSE)
45 {
Neale Ranns3bab8f92019-12-04 06:11:00 +000046 return (&fib_entry_src_bh_vft[FIB_SOURCE_BH_INTERPOSE]);
Neale Ranns2303cb12018-02-21 04:57:17 -080047 }
48
Neale Ranns3bab8f92019-12-04 06:11:00 +000049 return (&fib_entry_src_bh_vft[bh]);
Neale Ranns2303cb12018-02-21 04:57:17 -080050}
51
52static void
53fib_entry_src_copy_default (const fib_entry_src_t *orig_src,
54 const fib_entry_t *fib_entry,
55 fib_entry_src_t *copy_src)
56{
57 clib_memcpy(&copy_src->u, &orig_src->u, sizeof(copy_src->u));
58}
59
Neale Ranns0bfe5d82016-08-25 15:29:12 +010060void
Neale Ranns3bab8f92019-12-04 06:11:00 +000061fib_entry_src_behaviour_register (fib_source_behaviour_t bh,
62 const fib_entry_src_vft_t *vft)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010063{
Neale Ranns3bab8f92019-12-04 06:11:00 +000064 fib_entry_src_bh_vft[bh] = *vft;
Neale Ranns2303cb12018-02-21 04:57:17 -080065
Neale Ranns3bab8f92019-12-04 06:11:00 +000066 if (NULL == fib_entry_src_bh_vft[bh].fesv_copy)
Neale Ranns2303cb12018-02-21 04:57:17 -080067 {
Neale Ranns3bab8f92019-12-04 06:11:00 +000068 fib_entry_src_bh_vft[bh].fesv_copy = fib_entry_src_copy_default;
Neale Ranns2303cb12018-02-21 04:57:17 -080069 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070}
71
72static int
73fib_entry_src_cmp_for_sort (void * v1,
74 void * v2)
75{
76 fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
77
Neale Ranns3bab8f92019-12-04 06:11:00 +000078 return (fib_source_get_prio(esrc1->fes_src) -
79 fib_source_get_prio(esrc2->fes_src));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080}
81
Neale Ranns2303cb12018-02-21 04:57:17 -080082static void
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083fib_entry_src_action_init (fib_entry_t *fib_entry,
Neale Ranns2303cb12018-02-21 04:57:17 -080084 fib_source_t source,
85 fib_entry_flag_t flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010086{
87 fib_entry_src_t esrc = {
88 .fes_pl = FIB_NODE_INDEX_INVALID,
89 .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
90 .fes_src = source,
Neale Ranns2303cb12018-02-21 04:57:17 -080091 .fes_entry_flags = flags,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092 };
93
Neale Ranns6ede5702020-02-13 09:12:36 +000094 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, &esrc, fesv_init, (&esrc));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010095
Neale Rannsa4e77662017-12-04 20:00:30 +000096 vec_add1(fib_entry->fe_srcs, esrc);
97 vec_sort_with_function(fib_entry->fe_srcs,
98 fib_entry_src_cmp_for_sort);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010099}
100
101static fib_entry_src_t *
Neale Ranns2303cb12018-02-21 04:57:17 -0800102fib_entry_src_find_i (const fib_entry_t *fib_entry,
103 fib_source_t source,
104 u32 *index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100105
106{
107 fib_entry_src_t *esrc;
108 int ii;
109
110 ii = 0;
Neale Rannsa4e77662017-12-04 20:00:30 +0000111 vec_foreach(esrc, fib_entry->fe_srcs)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100112 {
Neale Rannsa4e77662017-12-04 20:00:30 +0000113 if (esrc->fes_src == source)
114 {
115 if (NULL != index)
116 {
117 *index = ii;
118 }
119 return (esrc);
120 }
121 else
122 {
123 ii++;
124 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100125 }
126
127 return (NULL);
128}
129
Neale Rannse2fe0972020-11-26 08:37:27 +0000130fib_entry_src_t *
Neale Ranns2303cb12018-02-21 04:57:17 -0800131fib_entry_src_find (const fib_entry_t *fib_entry,
132 fib_source_t source)
133
134{
135 return (fib_entry_src_find_i(fib_entry, source, NULL));
136}
137
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100138int
139fib_entry_is_sourced (fib_node_index_t fib_entry_index,
140 fib_source_t source)
141{
142 fib_entry_t *fib_entry;
143
144 fib_entry = fib_entry_get(fib_entry_index);
145
Neale Ranns2303cb12018-02-21 04:57:17 -0800146 return (NULL != fib_entry_src_find(fib_entry, source));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147}
148
Neale Ranns9db6ada2019-11-08 12:42:31 +0000149int
150fib_entry_is_marked (fib_node_index_t fib_entry_index,
151 fib_source_t source)
152{
153 fib_entry_t *fib_entry;
154 fib_entry_src_t *esrc;
155
156 fib_entry = fib_entry_get(fib_entry_index);
157
158 esrc = fib_entry_src_find(fib_entry, source);
159
160 if (NULL == esrc)
161 {
162 return (0);
163 }
164 else
165 {
166 return (!!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_STALE));
167 }
168}
169
170void
171fib_entry_mark (fib_node_index_t fib_entry_index,
172 fib_source_t source)
173{
174 fib_entry_t *fib_entry;
175 fib_entry_src_t *esrc;
176
177 fib_entry = fib_entry_get(fib_entry_index);
178
179 esrc = fib_entry_src_find(fib_entry, source);
180
181 if (NULL != esrc)
182 {
183 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_STALE;
184 }
185}
186
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100187static fib_entry_src_t *
188fib_entry_src_find_or_create (fib_entry_t *fib_entry,
Neale Ranns2303cb12018-02-21 04:57:17 -0800189 fib_source_t source,
190 fib_entry_flag_t flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191{
192 fib_entry_src_t *esrc;
193
Neale Ranns2303cb12018-02-21 04:57:17 -0800194 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100195
196 if (NULL == esrc)
197 {
Neale Ranns2303cb12018-02-21 04:57:17 -0800198 fib_entry_src_action_init(fib_entry, source, flags);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100199 }
200
Neale Ranns2303cb12018-02-21 04:57:17 -0800201 return (fib_entry_src_find(fib_entry, source));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202}
203
Neale Ranns2303cb12018-02-21 04:57:17 -0800204static void
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100205fib_entry_src_action_deinit (fib_entry_t *fib_entry,
206 fib_source_t source)
207
208{
209 fib_entry_src_t *esrc;
210 u32 index = ~0;
211
Neale Ranns2303cb12018-02-21 04:57:17 -0800212 esrc = fib_entry_src_find_i(fib_entry, source, &index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213
214 ASSERT(NULL != esrc);
215
Neale Ranns6ede5702020-02-13 09:12:36 +0000216 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_deinit, (esrc));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100217
Neale Ranns81424992017-05-18 03:03:22 -0700218 fib_path_ext_list_flush(&esrc->fes_path_exts);
Neale Rannsa4e77662017-12-04 20:00:30 +0000219 vec_del1(fib_entry->fe_srcs, index);
Neale Ranns75b39f82018-10-26 04:17:54 -0700220 vec_sort_with_function(fib_entry->fe_srcs,
221 fib_entry_src_cmp_for_sort);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100222}
223
224fib_entry_src_cover_res_t
225fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
Neale Ranns2303cb12018-02-21 04:57:17 -0800226 fib_entry_src_t *esrc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100227{
Neale Ranns2303cb12018-02-21 04:57:17 -0800228 FIB_ENTRY_SRC_VFT_INVOKE_AND_RETURN(esrc, fesv_cover_change,
229 (esrc, fib_entry));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100230
231 fib_entry_src_cover_res_t res = {
232 .install = !0,
233 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
234 };
235 return (res);
236}
237
238fib_entry_src_cover_res_t
239fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
Neale Ranns2303cb12018-02-21 04:57:17 -0800240 fib_entry_src_t *esrc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100241{
Neale Ranns2303cb12018-02-21 04:57:17 -0800242 FIB_ENTRY_SRC_VFT_INVOKE_AND_RETURN(esrc, fesv_cover_update,
243 (esrc, fib_entry));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244
245 fib_entry_src_cover_res_t res = {
246 .install = !0,
247 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
248 };
249 return (res);
250}
251
252typedef struct fib_entry_src_collect_forwarding_ctx_t_
253{
Neale Ranns81424992017-05-18 03:03:22 -0700254 load_balance_path_t *next_hops;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100255 const fib_entry_t *fib_entry;
Neale Ranns1c59df72021-01-26 12:08:25 +0000256 i32 start_source_index, end_source_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100257 fib_forward_chain_type_t fct;
Neale Rannsf12a83f2017-04-18 09:09:40 -0700258 int n_recursive_constrained;
Neale Ranns57b58602017-07-15 07:37:25 -0700259 u16 preference;
Neale Ranns53962fb2021-12-20 18:18:42 +0000260 dpo_proto_t payload_proto;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100261} fib_entry_src_collect_forwarding_ctx_t;
262
263/**
264 * @brief Determine whether this FIB entry should use a load-balance MAP
265 * to support PIC edge fast convergence
266 */
Neale Ranns1c59df72021-01-26 12:08:25 +0000267static load_balance_flags_t
268fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx,
269 const fib_entry_src_t *esrc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270{
271 /**
Neale Rannsf12a83f2017-04-18 09:09:40 -0700272 * We'll use a LB map if the path-list has multiple recursive paths.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100273 * recursive paths implies BGP, and hence scale.
274 */
Neale Rannsf12a83f2017-04-18 09:09:40 -0700275 if (ctx->n_recursive_constrained > 1 &&
Neale Ranns1c59df72021-01-26 12:08:25 +0000276 fib_path_list_is_popular(esrc->fes_pl))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100277 {
278 return (LOAD_BALANCE_FLAG_USES_MAP);
279 }
280 return (LOAD_BALANCE_FLAG_NONE);
281}
282
283static int
284fib_entry_src_valid_out_label (mpls_label_t label)
285{
286 return ((MPLS_LABEL_IS_REAL(label) ||
Neale Ranns31ed7442018-02-23 05:29:09 -0800287 MPLS_LABEL_POP == label ||
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100288 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
289 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
290 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
291}
292
Neale Ranns62fe07c2017-10-31 12:28:22 -0700293static dpo_proto_t
294fib_prefix_get_payload_proto (const fib_prefix_t *pfx)
295{
296 switch (pfx->fp_proto)
297 {
298 case FIB_PROTOCOL_IP4:
299 return (DPO_PROTO_IP4);
300 case FIB_PROTOCOL_IP6:
301 return (DPO_PROTO_IP6);
302 case FIB_PROTOCOL_MPLS:
303 return (pfx->fp_payload_proto);
304 }
305
306 ASSERT(0);
307 return (DPO_PROTO_IP4);
308}
309
Neale Ranns81424992017-05-18 03:03:22 -0700310static void
311fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
312 fib_entry_src_collect_forwarding_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100313{
Neale Ranns81424992017-05-18 03:03:22 -0700314 load_balance_path_t *nh;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100315
316 /*
Neale Ranns81424992017-05-18 03:03:22 -0700317 * no extension => no out-going label for this path. that's OK
318 * in the case of an IP or EOS chain, but not for non-EOS
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100319 */
Neale Ranns81424992017-05-18 03:03:22 -0700320 switch (ctx->fct)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100321 {
Neale Ranns81424992017-05-18 03:03:22 -0700322 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
323 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
324 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
325 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700326 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100327 /*
Neale Ranns81424992017-05-18 03:03:22 -0700328 * EOS traffic with no label to stack, we need the IP Adj
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100329 */
Neale Ranns81424992017-05-18 03:03:22 -0700330 vec_add2(ctx->next_hops, nh, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100331
Neale Ranns81424992017-05-18 03:03:22 -0700332 nh->path_index = path_index;
333 nh->path_weight = fib_path_get_weight(path_index);
Neale Ranns53962fb2021-12-20 18:18:42 +0000334 fib_path_contribute_forwarding(path_index, ctx->fct,
335 ctx->payload_proto, &nh->path_dpo);
Neale Ranns81424992017-05-18 03:03:22 -0700336
337 break;
338 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
339 if (fib_path_is_exclusive(path_index) ||
340 fib_path_is_deag(path_index))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100341 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100342 vec_add2(ctx->next_hops, nh, 1);
343
344 nh->path_index = path_index;
345 nh->path_weight = fib_path_get_weight(path_index);
Neale Ranns81424992017-05-18 03:03:22 -0700346 fib_path_contribute_forwarding(path_index,
347 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
Neale Ranns53962fb2021-12-20 18:18:42 +0000348 ctx->payload_proto,
Neale Ranns81424992017-05-18 03:03:22 -0700349 &nh->path_dpo);
350 }
351 break;
352 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
Neale Rannsad422ed2016-11-02 14:20:04 +0000353 {
354 /*
355 * no label. we need a chain based on the payload. fixup.
356 */
357 vec_add2(ctx->next_hops, nh, 1);
358
359 nh->path_index = path_index;
360 nh->path_weight = fib_path_get_weight(path_index);
361 fib_path_contribute_forwarding(path_index,
Neale Ranns53962fb2021-12-20 18:18:42 +0000362 ctx->fct,
363 ctx->payload_proto,
Neale Rannsad422ed2016-11-02 14:20:04 +0000364 &nh->path_dpo);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800365 fib_path_stack_mpls_disp(path_index,
Neale Ranns53962fb2021-12-20 18:18:42 +0000366 ctx->payload_proto,
Neale Ranns31ed7442018-02-23 05:29:09 -0800367 FIB_MPLS_LSP_MODE_PIPE,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800368 &nh->path_dpo);
Neale Rannsad422ed2016-11-02 14:20:04 +0000369
370 break;
371 }
Neale Ranns81424992017-05-18 03:03:22 -0700372 case FIB_FORW_CHAIN_TYPE_ETHERNET:
373 case FIB_FORW_CHAIN_TYPE_NSH:
374 ASSERT(0);
375 break;
376 }
377}
378
379static fib_path_list_walk_rc_t
380fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
381 fib_node_index_t path_index,
382 void *arg)
383{
384 fib_entry_src_collect_forwarding_ctx_t *ctx;
Neale Ranns1c59df72021-01-26 12:08:25 +0000385 const fib_entry_src_t *esrc;
Neale Ranns81424992017-05-18 03:03:22 -0700386 fib_path_ext_t *path_ext;
Neale Ranns2303cb12018-02-21 04:57:17 -0800387 u32 n_nhs;
Neale Ranns81424992017-05-18 03:03:22 -0700388
389 ctx = arg;
Neale Ranns2303cb12018-02-21 04:57:17 -0800390 n_nhs = vec_len(ctx->next_hops);
Neale Ranns81424992017-05-18 03:03:22 -0700391
392 /*
Neale Ranns1c59df72021-01-26 12:08:25 +0000393 * walk the paths and extension of the best non-interpose source
394 */
395 esrc = &ctx->fib_entry->fe_srcs[ctx->end_source_index];
396
397 /*
Neale Ranns81424992017-05-18 03:03:22 -0700398 * if the path is not resolved, don't include it.
399 */
400 if (!fib_path_is_resolved(path_index))
401 {
402 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100403 }
404
Neale Ranns81424992017-05-18 03:03:22 -0700405 if (fib_path_is_recursive_constrained(path_index))
406 {
407 ctx->n_recursive_constrained += 1;
408 }
Neale Ranns57b58602017-07-15 07:37:25 -0700409 if (0xffff == ctx->preference)
410 {
411 /*
412 * not set a preference yet, so the first path we encounter
413 * sets the preference we are collecting.
414 */
415 ctx->preference = fib_path_get_preference(path_index);
416 }
417 else if (ctx->preference != fib_path_get_preference(path_index))
418 {
419 /*
420 * this path does not belong to the same preference as the
421 * previous paths encountered. we are done now.
422 */
423 return (FIB_PATH_LIST_WALK_STOP);
424 }
Neale Ranns81424992017-05-18 03:03:22 -0700425
426 /*
427 * get the matching path-extension for the path being visited.
428 */
Neale Ranns1c59df72021-01-26 12:08:25 +0000429 path_ext = fib_path_ext_list_find_by_path_index(&esrc->fes_path_exts,
Neale Ranns81424992017-05-18 03:03:22 -0700430 path_index);
431
432 if (NULL != path_ext)
433 {
434 switch (path_ext->fpe_type)
435 {
436 case FIB_PATH_EXT_MPLS:
Neale Ranns31ed7442018-02-23 05:29:09 -0800437 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0].fml_value))
Neale Ranns81424992017-05-18 03:03:22 -0700438 {
439 /*
440 * found a matching extension. stack it to obtain the forwarding
441 * info for this path.
442 */
443 ctx->next_hops =
444 fib_path_ext_stack(path_ext,
Neale Ranns53962fb2021-12-20 18:18:42 +0000445 ctx->payload_proto,
Neale Ranns81424992017-05-18 03:03:22 -0700446 ctx->fct,
Neale Ranns81424992017-05-18 03:03:22 -0700447 ctx->next_hops);
448 }
449 else
450 {
451 fib_entry_src_get_path_forwarding(path_index, ctx);
452 }
453 break;
454 case FIB_PATH_EXT_ADJ:
455 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
456 {
457 fib_entry_src_get_path_forwarding(path_index, ctx);
458 }
459 /*
460 * else
461 * the path does not refine the cover, meaning that
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700462 * the adjacency does/does not match the sub-net on the link.
Neale Ranns81424992017-05-18 03:03:22 -0700463 * So this path does not contribute forwarding.
464 */
465 break;
466 }
467 }
468 else
469 {
470 fib_entry_src_get_path_forwarding(path_index, ctx);
471 }
472
Neale Ranns2303cb12018-02-21 04:57:17 -0800473 /*
474 * a this point 'ctx' has the DPO the path contributed, plus
475 * any labels from path extensions.
476 * check if there are any interpose sources that want to contribute
477 */
478 if (n_nhs < vec_len(ctx->next_hops))
479 {
480 /*
481 * the path contributed a new choice.
482 */
483 const fib_entry_src_vft_t *vft;
484
Neale Ranns1c59df72021-01-26 12:08:25 +0000485 /*
486 * roll up the sources that are interposes
487 */
488 i32 index;
Neale Ranns2303cb12018-02-21 04:57:17 -0800489
Neale Ranns1c59df72021-01-26 12:08:25 +0000490 for (index = ctx->end_source_index;
491 index >= ctx->start_source_index;
492 index--)
Neale Ranns2303cb12018-02-21 04:57:17 -0800493 {
494 const dpo_id_t *interposer;
495
Neale Ranns1c59df72021-01-26 12:08:25 +0000496 esrc = &ctx->fib_entry->fe_srcs[index];
497 vft = fib_entry_src_get_vft(esrc);
498
499 if (!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_CONTRIBUTING) ||
500 !(esrc->fes_entry_flags & FIB_ENTRY_FLAG_INTERPOSE))
501 continue;
502
503 ASSERT(vft->fesv_contribute_interpose);
504 interposer = vft->fesv_contribute_interpose(esrc, ctx->fib_entry);
Neale Ranns2303cb12018-02-21 04:57:17 -0800505
506 if (NULL != interposer)
507 {
508 dpo_id_t clone = DPO_INVALID;
509
510 dpo_mk_interpose(interposer,
511 &ctx->next_hops[n_nhs].path_dpo,
512 &clone);
513
514 dpo_copy(&ctx->next_hops[n_nhs].path_dpo, &clone);
515 dpo_reset(&clone);
516 }
517 }
518 }
519
Neale Ranns81424992017-05-18 03:03:22 -0700520 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100521}
522
523void
524fib_entry_src_mk_lb (fib_entry_t *fib_entry,
Neale Ranns1c59df72021-01-26 12:08:25 +0000525 fib_source_t source,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100526 fib_forward_chain_type_t fct,
527 dpo_id_t *dpo_lb)
528{
Neale Ranns1c59df72021-01-26 12:08:25 +0000529 const fib_entry_src_t *esrc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100530 dpo_proto_t lb_proto;
Neale Ranns1c59df72021-01-26 12:08:25 +0000531 u32 start, end;
532
533 /*
534 * The source passed here is the 'best', i.e. the one the client
535 * wants. however, if it's an interpose then it does not contribute
536 * the forwarding, the next best source that is not an interpose does.
537 * So roll down the sources, to find the best non-interpose
538 */
539 vec_foreach_index (start, fib_entry->fe_srcs)
540 {
541 if (source == fib_entry->fe_srcs[start].fes_src)
542 break;
543 }
544 for (end = start; end < vec_len (fib_entry->fe_srcs); end++)
545 {
546 if (!(fib_entry->fe_srcs[end].fes_entry_flags &
547 FIB_ENTRY_FLAG_INTERPOSE) &&
548 (fib_entry->fe_srcs[end].fes_flags &
549 FIB_ENTRY_SRC_FLAG_CONTRIBUTING))
550 break;
551 }
552 if (end == vec_len(fib_entry->fe_srcs))
553 {
554 /* didn't find any contributing non-interpose sources */
555 end = start;
556 }
557
558 esrc = &fib_entry->fe_srcs[end];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100559
560 /*
561 * If the entry has path extensions then we construct a load-balance
562 * by stacking the extensions on the forwarding chains of the paths.
563 * Otherwise we use the load-balance of the path-list
564 */
565 fib_entry_src_collect_forwarding_ctx_t ctx = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100566 .fib_entry = fib_entry,
567 .next_hops = NULL,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700568 .n_recursive_constrained = 0,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100569 .fct = fct,
Neale Ranns57b58602017-07-15 07:37:25 -0700570 .preference = 0xffff,
Neale Ranns1c59df72021-01-26 12:08:25 +0000571 .start_source_index = start,
572 .end_source_index = end,
Neale Ranns53962fb2021-12-20 18:18:42 +0000573 .payload_proto = fib_prefix_get_payload_proto(&fib_entry->fe_prefix),
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100574 };
575
Neale Rannsc0790cf2017-01-05 01:01:47 -0800576 /*
577 * As an optimisation we allocate the vector of next-hops to be sized
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700578 * equal to the maximum number of paths we will need, which is also the
Neale Rannsc0790cf2017-01-05 01:01:47 -0800579 * most likely number we will need, since in most cases the paths are 'up'.
580 */
581 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
582 vec_reset_length(ctx.next_hops);
583
Neale Rannsf12a83f2017-04-18 09:09:40 -0700584 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100585
586 fib_path_list_walk(esrc->fes_pl,
587 fib_entry_src_collect_forwarding,
588 &ctx);
589
590 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
591 {
592 /*
593 * the client provided the DPO that the entry should link to.
594 * all entries must link to a LB, so if it is an LB already
595 * then we can use it.
596 */
597 if ((1 == vec_len(ctx.next_hops)) &&
598 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
599 {
600 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
601 dpo_reset(&ctx.next_hops[0].path_dpo);
602 return;
603 }
604 }
605
606 if (!dpo_id_is_valid(dpo_lb))
607 {
608 /*
609 * first time create
610 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800611 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
612 {
613 dpo_set(dpo_lb,
614 DPO_REPLICATE,
615 lb_proto,
616 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
617 }
618 else
619 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700620 fib_protocol_t flow_hash_proto;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800621 flow_hash_config_t fhc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100622
Neale Ranns41da54f2017-05-02 10:15:19 -0700623 /*
624 * if the protocol for the LB we are building does not match that
625 * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
626 * then the fib_index is not an index that relates to the table
627 * type we need. So get the default flow-hash config instead.
628 */
Neale Rannsd792d9c2017-10-21 10:53:20 -0700629 flow_hash_proto = dpo_proto_to_fib(lb_proto);
630 if (fib_entry->fe_prefix.fp_proto != flow_hash_proto)
Neale Ranns41da54f2017-05-02 10:15:19 -0700631 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700632 fhc = fib_table_get_default_flow_hash_config(flow_hash_proto);
Neale Ranns41da54f2017-05-02 10:15:19 -0700633 }
634 else
635 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700636 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
637 flow_hash_proto);
Neale Ranns41da54f2017-05-02 10:15:19 -0700638 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700639
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800640 dpo_set(dpo_lb,
641 DPO_LOAD_BALANCE,
642 lb_proto,
643 load_balance_create(0, lb_proto, fhc));
644 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100645 }
646
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800647 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
Neale Ranns3ee44042016-10-03 13:05:48 +0100648 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800649 /*
650 * MPLS multicast
651 */
652 replicate_multipath_update(dpo_lb, ctx.next_hops);
Neale Ranns3ee44042016-10-03 13:05:48 +0100653 }
654 else
655 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800656 load_balance_multipath_update(dpo_lb,
657 ctx.next_hops,
Neale Ranns1c59df72021-01-26 12:08:25 +0000658 fib_entry_calc_lb_flags(&ctx, esrc));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800659 vec_free(ctx.next_hops);
660
661 /*
662 * if this entry is sourced by the uRPF-exempt source then we
663 * append the always present local0 interface (index 0) to the
664 * uRPF list so it is not empty. that way packets pass the loose check.
665 */
666 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
667
668 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
669 FIB_SOURCE_URPF_EXEMPT) ||
670 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
671 (0 == fib_urpf_check_size(ui)))
672 {
673 /*
674 * The uRPF list we get from the path-list is shared by all
675 * other users of the list, but the uRPF exemption applies
676 * only to this prefix. So we need our own list.
677 */
678 ui = fib_urpf_list_alloc_and_lock();
679 fib_urpf_list_append(ui, 0);
680 fib_urpf_list_bake(ui);
681 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
682 fib_urpf_list_unlock(ui);
683 }
684 else
685 {
686 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
687 }
688 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
689 fib_entry_get_flags_i(fib_entry));
Neale Ranns3ee44042016-10-03 13:05:48 +0100690 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100691}
692
693void
694fib_entry_src_action_install (fib_entry_t *fib_entry,
695 fib_source_t source)
696{
697 /*
698 * Install the forwarding chain for the given source into the forwarding
699 * tables
700 */
701 fib_forward_chain_type_t fct;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100702 int insert;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100703
704 fct = fib_entry_get_default_chain_type(fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100705
Neale Ranns33a7dd52016-10-07 15:14:33 +0100706 /*
707 * Every entry has its own load-balance object. All changes to the entry's
708 * forwarding result in an inplace modify of the load-balance. This means
709 * the load-balance object only needs to be added to the forwarding
710 * DB once, when it is created.
711 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000712 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100713
Neale Ranns1c59df72021-01-26 12:08:25 +0000714 fib_entry_src_mk_lb(fib_entry, source, fct, &fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100715
Neale Rannsad422ed2016-11-02 14:20:04 +0000716 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
717 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100718
719 /*
720 * insert the adj into the data-plane forwarding trie
721 */
Neale Ranns33a7dd52016-10-07 15:14:33 +0100722 if (insert)
723 {
724 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
725 &fib_entry->fe_prefix,
Neale Rannsad422ed2016-11-02 14:20:04 +0000726 &fib_entry->fe_lb);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100727 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100728
Neale Rannsad422ed2016-11-02 14:20:04 +0000729 /*
730 * if any of the other chain types are already created they will need
731 * updating too
732 */
733 fib_entry_delegate_type_t fdt;
734 fib_entry_delegate_t *fed;
735
736 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100737 {
Neale Ranns1c59df72021-01-26 12:08:25 +0000738 fib_entry_src_mk_lb(fib_entry, source,
Neale Rannsad422ed2016-11-02 14:20:04 +0000739 fib_entry_delegate_type_to_chain_type(fdt),
740 &fed->fd_dpo);
741 });
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100742}
743
744void
745fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
746{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100747 /*
Neale Ranns3ee44042016-10-03 13:05:48 +0100748 * uninstall the forwarding chain from the forwarding tables
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100749 */
Neale Ranns710071b2018-09-24 12:36:26 +0000750 FIB_ENTRY_DBG(fib_entry, "uninstall");
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100751
Neale Rannsad422ed2016-11-02 14:20:04 +0000752 if (dpo_id_is_valid(&fib_entry->fe_lb))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100753 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100754 fib_table_fwding_dpo_remove(
755 fib_entry->fe_fib_index,
756 &fib_entry->fe_prefix,
Neale Rannsad422ed2016-11-02 14:20:04 +0000757 &fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100758
Neale Rannsad422ed2016-11-02 14:20:04 +0000759 dpo_reset(&fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100760 }
761}
762
763static void
764fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
765{
766 fib_node_index_t *entries = NULL;
767
768 fib_path_list_recursive_loop_detect(path_list_index, &entries);
769
770 vec_free(entries);
771}
772
Neale Ranns89541992017-04-06 04:41:02 -0700773/*
774 * fib_entry_src_action_copy
775 *
776 * copy a source data from another entry to this one
777 */
Neale Ranns2303cb12018-02-21 04:57:17 -0800778static fib_entry_t *
Neale Ranns89541992017-04-06 04:41:02 -0700779fib_entry_src_action_copy (fib_entry_t *fib_entry,
780 const fib_entry_src_t *orig_src)
781{
782 fib_entry_src_t *esrc;
783
Neale Ranns2303cb12018-02-21 04:57:17 -0800784 esrc = fib_entry_src_find_or_create(fib_entry,
785 orig_src->fes_src,
786 orig_src->fes_entry_flags);
Neale Ranns89541992017-04-06 04:41:02 -0700787
Neale Ranns6ede5702020-02-13 09:12:36 +0000788 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_copy,
Neale Ranns2303cb12018-02-21 04:57:17 -0800789 (orig_src, fib_entry, esrc));
790
791 fib_path_list_unlock(esrc->fes_pl);
792
793 /*
794 * copy over all the data ...
795 */
796 esrc->fes_flags = orig_src->fes_flags;
797 esrc->fes_pl = orig_src->fes_pl;
798
799 /*
800 * ... then update
801 */
Neale Ranns89541992017-04-06 04:41:02 -0700802 esrc->fes_ref_count = 1;
803 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_INHERITED;
Neale Ranns2303cb12018-02-21 04:57:17 -0800804 esrc->fes_flags &= ~(FIB_ENTRY_SRC_FLAG_ACTIVE |
805 FIB_ENTRY_SRC_FLAG_CONTRIBUTING);
Neale Ranns89541992017-04-06 04:41:02 -0700806 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_COVERED_INHERIT;
807
808 /*
809 * the source owns a lock on the entry
810 */
811 fib_path_list_lock(esrc->fes_pl);
812 fib_entry_lock(fib_entry_get_index(fib_entry));
813
814 return (fib_entry);
815}
816
817/*
818 * fib_entry_src_action_update
819 *
820 * copy a source data from another entry to this one
821 */
822static fib_entry_src_t *
823fib_entry_src_action_update_from_cover (fib_entry_t *fib_entry,
824 const fib_entry_src_t *orig_src)
825{
826 fib_entry_src_t *esrc;
827
Neale Ranns2303cb12018-02-21 04:57:17 -0800828 esrc = fib_entry_src_find_or_create(fib_entry,
829 orig_src->fes_src,
830 orig_src->fes_entry_flags);
Neale Ranns89541992017-04-06 04:41:02 -0700831
832 /*
833 * the source owns a lock on the entry
834 */
835 fib_path_list_unlock(esrc->fes_pl);
836 esrc->fes_pl = orig_src->fes_pl;
837 fib_path_list_lock(esrc->fes_pl);
838
839 return (esrc);
840}
841
842static fib_table_walk_rc_t
843fib_entry_src_covered_inherit_add_i (fib_entry_t *fib_entry,
844 const fib_entry_src_t *cover_src)
845{
846 fib_entry_src_t *esrc;
847
Neale Ranns2303cb12018-02-21 04:57:17 -0800848 esrc = fib_entry_src_find(fib_entry, cover_src->fes_src);
Neale Ranns89541992017-04-06 04:41:02 -0700849
850 if (cover_src == esrc)
851 {
852 return (FIB_TABLE_WALK_CONTINUE);
853 }
854
855 if (NULL != esrc)
856 {
857 /*
858 * the covered entry already has this source.
859 */
860 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT)
861 {
862 /*
863 * the covered source is itself a COVERED_INHERIT, i.e.
864 * it also pushes this source down the sub-tree.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700865 * We consider this more specific covered to be the owner
Neale Ranns89541992017-04-06 04:41:02 -0700866 * of the sub-tree from this point down.
867 */
868 return (FIB_TABLE_WALK_SUB_TREE_STOP);
869 }
870 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED)
871 {
872 /*
873 * The covered's source data has been inherited, presumably
874 * from this cover, i.e. this is a modify.
875 */
876 esrc = fib_entry_src_action_update_from_cover(fib_entry, cover_src);
877 fib_entry_source_change(fib_entry, esrc->fes_src, esrc->fes_src);
878 }
879 else
880 {
881 /*
882 * The covered's source was not inherited and it is also
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700883 * not inheriting. Nevertheless, it still owns the sub-tree from
Neale Ranns89541992017-04-06 04:41:02 -0700884 * this point down.
885 */
886 return (FIB_TABLE_WALK_SUB_TREE_STOP);
887 }
888 }
889 else
890 {
891 /*
892 * The covered does not have this source - add it.
893 */
894 fib_source_t best_source;
895
896 best_source = fib_entry_get_best_source(
897 fib_entry_get_index(fib_entry));
898
899 fib_entry_src_action_copy(fib_entry, cover_src);
900 fib_entry_source_change(fib_entry, best_source, cover_src->fes_src);
901
902 }
903 return (FIB_TABLE_WALK_CONTINUE);
904}
905
906static fib_table_walk_rc_t
907fib_entry_src_covered_inherit_walk_add (fib_node_index_t fei,
908 void *ctx)
909{
910 return (fib_entry_src_covered_inherit_add_i(fib_entry_get(fei), ctx));
911}
912
913static fib_table_walk_rc_t
914fib_entry_src_covered_inherit_walk_remove (fib_node_index_t fei,
915 void *ctx)
916{
917 fib_entry_src_t *cover_src, *esrc;
918 fib_entry_t *fib_entry;
919
920 fib_entry = fib_entry_get(fei);
921
922 cover_src = ctx;
Neale Ranns2303cb12018-02-21 04:57:17 -0800923 esrc = fib_entry_src_find(fib_entry, cover_src->fes_src);
Neale Ranns89541992017-04-06 04:41:02 -0700924
925 if (cover_src == esrc)
926 {
927 return (FIB_TABLE_WALK_CONTINUE);
928 }
929
930 if (NULL != esrc)
931 {
932 /*
933 * the covered entry already has this source.
934 */
935 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT)
936 {
937 /*
938 * the covered source is itself a COVERED_INHERIT, i.e.
939 * it also pushes this source down the sub-tree.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700940 * We consider this more specific covered to be the owner
Neale Ranns89541992017-04-06 04:41:02 -0700941 * of the sub-tree from this point down.
942 */
943 return (FIB_TABLE_WALK_SUB_TREE_STOP);
944 }
945 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED)
946 {
947 /*
948 * The covered's source data has been inherited, presumably
949 * from this cover
950 */
951 fib_entry_src_flag_t remaining;
952
953 remaining = fib_entry_special_remove(fei, cover_src->fes_src);
954
955 ASSERT(FIB_ENTRY_SRC_FLAG_ADDED == remaining);
956 }
957 else
958 {
959 /*
960 * The covered's source was not inherited and it is also
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700961 * not inheriting. Nevertheless, it still owns the sub-tree from
Neale Ranns89541992017-04-06 04:41:02 -0700962 * this point down.
963 */
964 return (FIB_TABLE_WALK_SUB_TREE_STOP);
965 }
966 }
967 else
968 {
969 /*
970 * The covered does not have this source - that's an error,
971 * since it should have inherited, but there is nothing we can do
972 * about it now.
973 */
974 }
975 return (FIB_TABLE_WALK_CONTINUE);
976}
977
978void
979fib_entry_src_inherit (const fib_entry_t *cover,
980 fib_entry_t *covered)
981{
982 CLIB_UNUSED(fib_source_t source);
983 const fib_entry_src_t *src;
984
985 FOR_EACH_SRC_ADDED(cover, src, source,
986 ({
987 if ((src->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT) ||
988 (src->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED))
989 {
990 fib_entry_src_covered_inherit_add_i(covered, src);
991 }
992 }))
993}
994
995static void
996fib_entry_src_covered_inherit_add (fib_entry_t *fib_entry,
997 fib_source_t source)
998
999{
1000 fib_entry_src_t *esrc;
1001
Neale Ranns2303cb12018-02-21 04:57:17 -08001002 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns89541992017-04-06 04:41:02 -07001003
1004 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
1005
1006 if ((esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT) ||
1007 (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED))
1008 {
1009 fib_table_sub_tree_walk(fib_entry->fe_fib_index,
1010 fib_entry->fe_prefix.fp_proto,
1011 &fib_entry->fe_prefix,
1012 fib_entry_src_covered_inherit_walk_add,
1013 esrc);
1014 }
1015}
1016
1017static void
1018fib_entry_src_covered_inherit_remove (fib_entry_t *fib_entry,
1019 fib_entry_src_t *esrc)
1020
1021{
1022 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
1023
1024 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT)
1025 {
1026 fib_table_sub_tree_walk(fib_entry->fe_fib_index,
1027 fib_entry->fe_prefix.fp_proto,
1028 &fib_entry->fe_prefix,
1029 fib_entry_src_covered_inherit_walk_remove,
1030 esrc);
1031 }
1032}
1033
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001034void
1035fib_entry_src_action_activate (fib_entry_t *fib_entry,
1036 fib_source_t source)
1037
1038{
1039 int houston_we_are_go_for_install;
Neale Ranns2303cb12018-02-21 04:57:17 -08001040 const fib_entry_src_vft_t *vft;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001041 fib_entry_src_t *esrc;
1042
Neale Ranns2303cb12018-02-21 04:57:17 -08001043 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001044
1045 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
1046 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1047
Neale Ranns2303cb12018-02-21 04:57:17 -08001048 esrc->fes_flags |= (FIB_ENTRY_SRC_FLAG_ACTIVE |
1049 FIB_ENTRY_SRC_FLAG_CONTRIBUTING);
1050 vft = fib_entry_src_get_vft(esrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001051
Neale Ranns2303cb12018-02-21 04:57:17 -08001052 if (NULL != vft->fesv_activate)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001053 {
Neale Ranns2303cb12018-02-21 04:57:17 -08001054 houston_we_are_go_for_install = vft->fesv_activate(esrc, fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001055 }
1056 else
1057 {
1058 /*
1059 * the source is not providing an activate function, we'll assume
1060 * therefore it has no objection to installing the entry
1061 */
1062 houston_we_are_go_for_install = !0;
1063 }
1064
1065 /*
1066 * link to the path-list provided by the source, and go check
1067 * if that forms any loops in the graph.
1068 */
1069 fib_entry->fe_parent = esrc->fes_pl;
1070 fib_entry->fe_sibling =
1071 fib_path_list_child_add(fib_entry->fe_parent,
1072 FIB_NODE_TYPE_ENTRY,
1073 fib_entry_get_index(fib_entry));
1074
1075 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
1076
1077 FIB_ENTRY_DBG(fib_entry, "activate: %d",
1078 fib_entry->fe_parent);
1079
Neale Ranns89541992017-04-06 04:41:02 -07001080 /*
1081 * If this source should push its state to covered prefixs, do that now.
1082 */
1083 fib_entry_src_covered_inherit_add(fib_entry, source);
1084
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001085 if (0 != houston_we_are_go_for_install)
1086 {
1087 fib_entry_src_action_install(fib_entry, source);
1088 }
1089 else
1090 {
1091 fib_entry_src_action_uninstall(fib_entry);
1092 }
1093}
1094
1095void
1096fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
1097 fib_source_t source)
1098
1099{
1100 fib_node_index_t path_list_index;
1101 fib_entry_src_t *esrc;
1102
Neale Ranns2303cb12018-02-21 04:57:17 -08001103 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001104
1105 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
1106
Neale Ranns6ede5702020-02-13 09:12:36 +00001107 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_deactivate,
Neale Ranns2303cb12018-02-21 04:57:17 -08001108 (esrc, fib_entry));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001109
Neale Ranns2303cb12018-02-21 04:57:17 -08001110 esrc->fes_flags &= ~(FIB_ENTRY_SRC_FLAG_ACTIVE |
1111 FIB_ENTRY_SRC_FLAG_CONTRIBUTING);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001112
1113 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
1114
1115 /*
Neale Ranns89541992017-04-06 04:41:02 -07001116 * If this source should pull its state from covered prefixs, do that now.
1117 * If this source also has the INHERITED flag set then it has a cover
1118 * that wants to push down forwarding. We only want the covereds to see
1119 * one update.
1120 */
1121 fib_entry_src_covered_inherit_remove(fib_entry, esrc);
1122
1123 /*
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001124 * un-link from an old path-list. Check for any loops this will clear
1125 */
1126 path_list_index = fib_entry->fe_parent;
1127 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
1128
1129 fib_entry_recursive_loop_detect_i(path_list_index);
1130
1131 /*
1132 * this will unlock the path-list, so it may be invalid thereafter.
1133 */
1134 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
1135 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
1136}
1137
Neale Rannsa4e77662017-12-04 20:00:30 +00001138static void
1139fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
1140 fib_source_t source)
1141{
1142 fib_entry_src_t *esrc;
1143
1144 vec_foreach(esrc, fib_entry->fe_srcs)
1145 {
Neale Ranns6ede5702020-02-13 09:12:36 +00001146 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_fwd_update,
Neale Ranns2303cb12018-02-21 04:57:17 -08001147 (esrc, fib_entry, source));
Neale Rannsa4e77662017-12-04 20:00:30 +00001148 }
1149}
1150
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001151void
1152fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
1153 fib_source_t source)
1154{
1155 fib_node_index_t path_list_index;
Neale Ranns2303cb12018-02-21 04:57:17 -08001156 const fib_entry_src_vft_t *vft;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001157 fib_entry_src_t *esrc;
Neale Ranns2303cb12018-02-21 04:57:17 -08001158 int remain_installed;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001159
Neale Ranns2303cb12018-02-21 04:57:17 -08001160 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001161
1162 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
1163
1164 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
1165 fib_entry->fe_parent,
1166 esrc->fes_pl);
1167
Neale Ranns2303cb12018-02-21 04:57:17 -08001168 /*
1169 * call the source to reactive and get the go/no-go to remain installed
1170 */
1171 vft = fib_entry_src_get_vft(esrc);
1172
1173 if (NULL != vft->fesv_reactivate)
1174 {
1175 remain_installed = vft->fesv_reactivate(esrc, fib_entry);
1176 }
1177 else
1178 {
1179 remain_installed = 1;
1180 }
1181
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001182 if (fib_entry->fe_parent != esrc->fes_pl)
1183 {
1184 /*
1185 * un-link from an old path-list. Check for any loops this will clear
1186 */
1187 path_list_index = fib_entry->fe_parent;
1188 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
1189
1190 /*
1191 * temporary lock so it doesn't get deleted when this entry is no
1192 * longer a child.
1193 */
1194 fib_path_list_lock(path_list_index);
1195
1196 /*
1197 * this entry is no longer a child. after unlinking check if any loops
1198 * were broken
1199 */
1200 fib_path_list_child_remove(path_list_index,
1201 fib_entry->fe_sibling);
1202
1203 fib_entry_recursive_loop_detect_i(path_list_index);
1204
1205 /*
1206 * link to the path-list provided by the source, and go check
1207 * if that forms any loops in the graph.
1208 */
1209 fib_entry->fe_parent = esrc->fes_pl;
1210 fib_entry->fe_sibling =
1211 fib_path_list_child_add(fib_entry->fe_parent,
1212 FIB_NODE_TYPE_ENTRY,
1213 fib_entry_get_index(fib_entry));
1214
1215 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
1216 fib_path_list_unlock(path_list_index);
Neale Ranns89541992017-04-06 04:41:02 -07001217
1218 /*
Neale Ranns89541992017-04-06 04:41:02 -07001219 * If this source should push its state to covered prefixs, do that now.
1220 */
1221 fib_entry_src_covered_inherit_add(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001222 }
Neale Ranns2303cb12018-02-21 04:57:17 -08001223
1224 if (!remain_installed)
1225 {
1226 fib_entry_src_action_uninstall(fib_entry);
1227 }
1228 else
1229 {
1230 fib_entry_src_action_install(fib_entry, source);
1231 }
Neale Rannsa4e77662017-12-04 20:00:30 +00001232 fib_entry_src_action_fwd_update(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001233}
1234
Neale Ranns6ede5702020-02-13 09:12:36 +00001235fib_entry_t *
1236fib_entry_src_action_installed (fib_entry_t *fib_entry,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001237 fib_source_t source)
1238{
1239 fib_entry_src_t *esrc;
1240
Neale Ranns2303cb12018-02-21 04:57:17 -08001241 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001242
Neale Ranns6ede5702020-02-13 09:12:36 +00001243 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_installed,
Neale Ranns2303cb12018-02-21 04:57:17 -08001244 (esrc, fib_entry));
Neale Rannsa4e77662017-12-04 20:00:30 +00001245
1246 fib_entry_src_action_fwd_update(fib_entry, source);
Neale Ranns6ede5702020-02-13 09:12:36 +00001247
1248 return (fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001249}
1250
1251/*
1252 * fib_entry_src_action_add
1253 *
1254 * Adding a source can result in a new fib_entry being created, which
1255 * can inturn mean the pool is realloc'd and thus the entry passed as
1256 * an argument it also realloc'd
1257 * @return the original entry
1258 */
1259fib_entry_t *
1260fib_entry_src_action_add (fib_entry_t *fib_entry,
1261 fib_source_t source,
1262 fib_entry_flag_t flags,
1263 const dpo_id_t *dpo)
1264{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001265 fib_entry_src_t *esrc;
1266
Neale Ranns2303cb12018-02-21 04:57:17 -08001267 esrc = fib_entry_src_find_or_create(fib_entry, source, flags);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001268
Neale Ranns2303cb12018-02-21 04:57:17 -08001269 ASSERT(esrc->fes_ref_count < 255);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001270 esrc->fes_ref_count++;
1271
Neale Ranns2303cb12018-02-21 04:57:17 -08001272 if (flags != esrc->fes_entry_flags)
1273 {
Neale Ranns6ede5702020-02-13 09:12:36 +00001274 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_flags_change,
Neale Ranns2303cb12018-02-21 04:57:17 -08001275 (esrc, fib_entry, flags));
1276 }
1277 esrc->fes_entry_flags = flags;
1278
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001279 if (1 != esrc->fes_ref_count)
1280 {
1281 /*
1282 * we only want to add the source on the 0->1 transition
1283 */
1284 return (fib_entry);
1285 }
1286
Neale Ranns6ede5702020-02-13 09:12:36 +00001287 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_add,
Neale Ranns2303cb12018-02-21 04:57:17 -08001288 (esrc,
1289 fib_entry,
1290 flags,
1291 fib_entry_get_dpo_proto(fib_entry),
1292 dpo));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001293
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001294 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
1295
1296 fib_path_list_lock(esrc->fes_pl);
1297
1298 /*
1299 * the source owns a lock on the entry
1300 */
1301 fib_entry_lock(fib_entry_get_index(fib_entry));
1302
1303 return (fib_entry);
1304}
1305
Neale Ranns948e00f2016-10-20 13:39:34 +01001306/*
1307 * fib_entry_src_action_update
1308 *
1309 * Adding a source can result in a new fib_entry being created, which
1310 * can inturn mean the pool is realloc'd and thus the entry passed as
1311 * an argument it also realloc'd
1312 * @return the original entry
1313 */
1314fib_entry_t *
1315fib_entry_src_action_update (fib_entry_t *fib_entry,
1316 fib_source_t source,
1317 fib_entry_flag_t flags,
1318 const dpo_id_t *dpo)
1319{
Neale Ranns6ede5702020-02-13 09:12:36 +00001320 fib_node_index_t old_path_list_index;
Neale Ranns948e00f2016-10-20 13:39:34 +01001321 fib_entry_src_t *esrc;
1322
Neale Ranns2303cb12018-02-21 04:57:17 -08001323 esrc = fib_entry_src_find_or_create(fib_entry, source, flags);
Neale Ranns948e00f2016-10-20 13:39:34 +01001324
1325 if (NULL == esrc)
Neale Ranns89541992017-04-06 04:41:02 -07001326 {
Neale Ranns948e00f2016-10-20 13:39:34 +01001327 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
Neale Ranns89541992017-04-06 04:41:02 -07001328 }
Neale Ranns948e00f2016-10-20 13:39:34 +01001329
1330 old_path_list_index = esrc->fes_pl;
1331 esrc->fes_entry_flags = flags;
1332
Neale Ranns6ede5702020-02-13 09:12:36 +00001333 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_add,
Neale Ranns2303cb12018-02-21 04:57:17 -08001334 (esrc,
1335 fib_entry,
1336 flags,
1337 fib_entry_get_dpo_proto(fib_entry),
1338 dpo));
Neale Ranns948e00f2016-10-20 13:39:34 +01001339
Neale Ranns948e00f2016-10-20 13:39:34 +01001340 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
1341
1342 fib_path_list_lock(esrc->fes_pl);
1343 fib_path_list_unlock(old_path_list_index);
1344
1345 return (fib_entry);
1346}
1347
Neale Ranns89541992017-04-06 04:41:02 -07001348fib_entry_src_flag_t
1349fib_entry_src_action_remove_or_update_inherit (fib_entry_t *fib_entry,
1350 fib_source_t source)
1351{
1352 fib_entry_src_t *esrc;
1353
Neale Ranns2303cb12018-02-21 04:57:17 -08001354 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns89541992017-04-06 04:41:02 -07001355
1356 if (NULL == esrc)
1357 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
1358
1359 if ((esrc->fes_entry_flags & FIB_ENTRY_FLAG_COVERED_INHERIT) &&
1360 (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_INHERITED))
1361 {
1362 fib_entry_src_t *cover_src;
1363 fib_node_index_t coveri;
1364 fib_entry_t *cover;
1365
1366 /*
1367 * this source was pushing inherited state, but so is its
1368 * cover. Now that this source is going away, we need to
1369 * pull the covers forwarding and use it to update the covereds.
1370 * Go grab the path-list from the cover, rather than start a walk from
1371 * the cover, so we don't recursively update this entry.
1372 */
1373 coveri = fib_table_get_less_specific(fib_entry->fe_fib_index,
1374 &fib_entry->fe_prefix);
1375
1376 /*
1377 * only the default route has itself as its own cover, but the
1378 * default route cannot have inherited from something else.
1379 */
1380 ASSERT(coveri != fib_entry_get_index(fib_entry));
1381
1382 cover = fib_entry_get(coveri);
Neale Ranns2303cb12018-02-21 04:57:17 -08001383 cover_src = fib_entry_src_find(cover, source);
Neale Ranns89541992017-04-06 04:41:02 -07001384
1385 ASSERT(NULL != cover_src);
1386
1387 esrc = fib_entry_src_action_update_from_cover(fib_entry, cover_src);
1388 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_COVERED_INHERIT;
1389
1390 /*
1391 * Now push the new state from the cover down to the covereds
1392 */
1393 fib_entry_src_covered_inherit_add(fib_entry, source);
1394
1395 return (esrc->fes_flags);
1396 }
1397 else
1398 {
1399 return (fib_entry_src_action_remove(fib_entry, source));
1400 }
1401}
Neale Ranns948e00f2016-10-20 13:39:34 +01001402
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001403fib_entry_src_flag_t
1404fib_entry_src_action_remove (fib_entry_t *fib_entry,
1405 fib_source_t source)
1406
1407{
1408 fib_node_index_t old_path_list;
1409 fib_entry_src_flag_t sflags;
1410 fib_entry_src_t *esrc;
1411
Neale Ranns2303cb12018-02-21 04:57:17 -08001412 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001413
1414 if (NULL == esrc)
1415 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
1416
1417 esrc->fes_ref_count--;
1418 sflags = esrc->fes_flags;
1419
1420 if (0 != esrc->fes_ref_count)
1421 {
1422 /*
1423 * only remove the source on the 1->0 transisition
1424 */
1425 return (sflags);
1426 }
1427
1428 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
1429 {
Neale Ranns89541992017-04-06 04:41:02 -07001430 fib_entry_src_action_deactivate(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001431 }
Neale Ranns2303cb12018-02-21 04:57:17 -08001432 else if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_CONTRIBUTING)
1433 {
Neale Ranns6ede5702020-02-13 09:12:36 +00001434 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_deactivate,
Neale Ranns2303cb12018-02-21 04:57:17 -08001435 (esrc, fib_entry));
1436 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_CONTRIBUTING;
1437 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001438
1439 old_path_list = esrc->fes_pl;
1440
Neale Ranns6ede5702020-02-13 09:12:36 +00001441 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_remove, (esrc));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001442
1443 fib_path_list_unlock(old_path_list);
1444 fib_entry_unlock(fib_entry_get_index(fib_entry));
1445
1446 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
1447 fib_entry_src_action_deinit(fib_entry, source);
1448
1449 return (sflags);
1450}
1451
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001452/*
1453 * fib_route_attached_cross_table
1454 *
1455 * Return true the the route is attached via an interface that
1456 * is not in the same table as the route
1457 */
Neale Ranns50bd1d32021-10-08 07:16:12 +00001458static int
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001459fib_route_attached_cross_table (const fib_entry_t *fib_entry,
1460 const fib_route_path_t *rpath)
1461{
Neale Ranns66edaf22021-07-09 13:03:52 +00001462 const fib_prefix_t *pfx = &fib_entry->fe_prefix;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001463
Neale Ranns66edaf22021-07-09 13:03:52 +00001464 switch (pfx->fp_proto)
Neale Rannseca834e2018-03-13 07:51:50 -07001465 {
Neale Ranns66edaf22021-07-09 13:03:52 +00001466 case FIB_PROTOCOL_MPLS:
1467 /* MPLS routes are never imported/exported */
1468 return (0);
1469 case FIB_PROTOCOL_IP6:
1470 /* Ignore link local addresses these also can't be imported/exported */
1471 if (ip6_address_is_link_local_unicast (&pfx->fp_addr.ip6))
1472 {
Neale Rannse3241f02021-12-19 15:10:20 +00001473 return (0);
Neale Ranns66edaf22021-07-09 13:03:52 +00001474 }
1475 break;
1476 case FIB_PROTOCOL_IP4:
1477 break;
Neale Rannseca834e2018-03-13 07:51:50 -07001478 }
1479
1480 /*
Neale Ranns66edaf22021-07-09 13:03:52 +00001481 * an attached path and entry's fib index not equal to interface's index
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001482 */
Neale Ranns66edaf22021-07-09 13:03:52 +00001483 if (fib_route_path_is_attached(rpath) &&
1484 fib_entry->fe_fib_index !=
1485 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
1486 rpath->frp_sw_if_index))
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001487 {
1488 return (!0);
1489 }
1490 return (0);
1491}
1492
1493fib_path_list_flags_t
1494fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1495{
1496 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1497
1498 if (eflags & FIB_ENTRY_FLAG_DROP)
1499 {
1500 plf |= FIB_PATH_LIST_FLAG_DROP;
1501 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001502 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1503 {
1504 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1505 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001506 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1507 {
1508 plf |= FIB_PATH_LIST_FLAG_LOCAL;
1509 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001510
1511 return (plf);
1512}
1513
1514static void
1515fib_entry_flags_update (const fib_entry_t *fib_entry,
Neale Ranns097fa662018-05-01 05:17:55 -07001516 const fib_route_path_t *rpaths,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001517 fib_path_list_flags_t *pl_flags,
1518 fib_entry_src_t *esrc)
1519{
Neale Ranns097fa662018-05-01 05:17:55 -07001520 const fib_route_path_t *rpath;
1521
1522 vec_foreach(rpath, rpaths)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001523 {
Neale Ranns097fa662018-05-01 05:17:55 -07001524 if ((esrc->fes_src == FIB_SOURCE_API) ||
1525 (esrc->fes_src == FIB_SOURCE_CLI))
1526 {
Neale Ranns66edaf22021-07-09 13:03:52 +00001527 if (fib_route_path_is_attached(rpath))
Neale Ranns097fa662018-05-01 05:17:55 -07001528 {
1529 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1530 }
1531 else
1532 {
1533 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1534 }
1535 if (rpath->frp_flags & FIB_ROUTE_PATH_DEAG)
1536 {
1537 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT;
1538 }
1539 }
1540 if (fib_route_attached_cross_table(fib_entry, rpath) &&
1541 !(esrc->fes_entry_flags & FIB_ENTRY_FLAG_NO_ATTACHED_EXPORT))
1542 {
1543 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1544 }
1545 else
1546 {
1547 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1548 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001549 }
1550}
1551
1552/*
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001553 * fib_entry_src_action_add
1554 *
1555 * Adding a source can result in a new fib_entry being created, which
1556 * can inturn mean the pool is realloc'd and thus the entry passed as
1557 * an argument it also realloc'd
1558 * @return the entry
1559 */
1560fib_entry_t*
1561fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1562 fib_source_t source,
1563 fib_entry_flag_t flags,
Neale Ranns097fa662018-05-01 05:17:55 -07001564 const fib_route_path_t *rpaths)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001565{
Neale Ranns6ede5702020-02-13 09:12:36 +00001566 fib_node_index_t old_path_list;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001567 fib_path_list_flags_t pl_flags;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001568 fib_entry_src_t *esrc;
1569
Neale Ranns2303cb12018-02-21 04:57:17 -08001570 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001571 if (NULL == esrc)
1572 {
Vijayabhaskar Katamreddy0c2319f2018-10-25 13:28:12 -07001573 const dpo_id_t *dpo;
1574
1575 if (flags == FIB_ENTRY_FLAG_EXCLUSIVE) {
Neale Ranns097fa662018-05-01 05:17:55 -07001576 dpo = &rpaths->dpo;
Vijayabhaskar Katamreddy0c2319f2018-10-25 13:28:12 -07001577 } else {
1578 dpo = drop_dpo_get(fib_entry_get_dpo_proto(fib_entry));
1579 }
1580
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001581 fib_entry =
1582 fib_entry_src_action_add(fib_entry,
1583 source,
1584 flags,
Vijayabhaskar Katamreddy0c2319f2018-10-25 13:28:12 -07001585 dpo);
Neale Ranns2303cb12018-02-21 04:57:17 -08001586 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001587 }
1588
1589 /*
1590 * we are no doubt modifying a path-list. If the path-list
1591 * is shared, and hence not modifiable, then the index returned
1592 * will be for a different path-list. This FIB entry to needs
1593 * to maintain its lock appropriately.
1594 */
1595 old_path_list = esrc->fes_pl;
1596
Neale Ranns2303cb12018-02-21 04:57:17 -08001597 ASSERT(FIB_ENTRY_SRC_VFT_EXISTS(esrc, fesv_path_add));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001598
1599 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
Neale Ranns097fa662018-05-01 05:17:55 -07001600 fib_entry_flags_update(fib_entry, rpaths, &pl_flags, esrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001601
Neale Ranns6ede5702020-02-13 09:12:36 +00001602 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_path_add,
Neale Ranns097fa662018-05-01 05:17:55 -07001603 (esrc, fib_entry, pl_flags, rpaths));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001604
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001605 fib_path_list_lock(esrc->fes_pl);
1606 fib_path_list_unlock(old_path_list);
1607
1608 return (fib_entry);
1609}
1610
1611/*
1612 * fib_entry_src_action_swap
1613 *
1614 * The source is providing new paths to replace the old ones.
1615 * Adding a source can result in a new fib_entry being created, which
1616 * can inturn mean the pool is realloc'd and thus the entry passed as
1617 * an argument it also realloc'd
1618 * @return the entry
1619 */
1620fib_entry_t*
1621fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1622 fib_source_t source,
Neale Ranns2303cb12018-02-21 04:57:17 -08001623 fib_entry_flag_t flags,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001624 const fib_route_path_t *rpaths)
1625{
Neale Ranns6ede5702020-02-13 09:12:36 +00001626 fib_node_index_t old_path_list;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001627 fib_path_list_flags_t pl_flags;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001628 fib_entry_src_t *esrc;
1629
Neale Ranns2303cb12018-02-21 04:57:17 -08001630 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001631
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001632 if (NULL == esrc)
1633 {
Vijayabhaskar Katamreddy0c2319f2018-10-25 13:28:12 -07001634 const dpo_id_t *dpo;
1635
1636 if (flags == FIB_ENTRY_FLAG_EXCLUSIVE) {
1637 dpo = &rpaths->dpo;
1638 } else {
1639 dpo = drop_dpo_get(fib_entry_get_dpo_proto(fib_entry));
1640 }
1641
Neale Ranns2303cb12018-02-21 04:57:17 -08001642 fib_entry = fib_entry_src_action_add(fib_entry,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001643 source,
1644 flags,
Vijayabhaskar Katamreddy0c2319f2018-10-25 13:28:12 -07001645 dpo);
Neale Ranns2303cb12018-02-21 04:57:17 -08001646 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001647 }
Neale Ranns89541992017-04-06 04:41:02 -07001648 else
1649 {
Neale Ranns2303cb12018-02-21 04:57:17 -08001650 if (flags != esrc->fes_entry_flags)
1651 {
Neale Ranns6ede5702020-02-13 09:12:36 +00001652 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_flags_change,
Neale Ranns2303cb12018-02-21 04:57:17 -08001653 (esrc, fib_entry, flags));
1654 }
Neale Ranns89541992017-04-06 04:41:02 -07001655 esrc->fes_entry_flags = flags;
1656 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001657
1658 /*
1659 * swapping paths may create a new path-list (or may use an existing shared)
1660 * but we are certainly getting a different one. This FIB entry to needs
1661 * to maintain its lock appropriately.
1662 */
1663 old_path_list = esrc->fes_pl;
1664
Neale Ranns2303cb12018-02-21 04:57:17 -08001665 ASSERT(FIB_ENTRY_SRC_VFT_EXISTS(esrc, fesv_path_swap));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001666
Neale Rannsdf089a82016-10-02 16:39:06 +01001667 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1668
Neale Rannsb5d61a92019-07-09 14:29:35 +00001669 fib_entry_flags_update(fib_entry, rpaths, &pl_flags, esrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001670
Neale Ranns6ede5702020-02-13 09:12:36 +00001671 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_path_swap,
Neale Ranns2303cb12018-02-21 04:57:17 -08001672 (esrc, fib_entry,
1673 pl_flags, rpaths));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001674
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001675 fib_path_list_lock(esrc->fes_pl);
1676 fib_path_list_unlock(old_path_list);
1677
1678 return (fib_entry);
1679}
1680
1681fib_entry_src_flag_t
1682fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1683 fib_source_t source,
Neale Ranns097fa662018-05-01 05:17:55 -07001684 const fib_route_path_t *rpaths)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001685{
1686 fib_path_list_flags_t pl_flags;
1687 fib_node_index_t old_path_list;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001688 fib_entry_src_t *esrc;
1689
Neale Ranns2303cb12018-02-21 04:57:17 -08001690 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001691
1692 ASSERT(NULL != esrc);
1693 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1694
1695 /*
1696 * we no doubt modifying a path-list. If the path-list
1697 * is shared, and hence not modifiable, then the index returned
1698 * will be for a different path-list. This FIB entry to needs
1699 * to maintain its lock appropriately.
1700 */
1701 old_path_list = esrc->fes_pl;
1702
Neale Ranns2303cb12018-02-21 04:57:17 -08001703 ASSERT(FIB_ENTRY_SRC_VFT_EXISTS(esrc, fesv_path_remove));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001704
1705 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
Neale Ranns097fa662018-05-01 05:17:55 -07001706 fib_entry_flags_update(fib_entry, rpaths, &pl_flags, esrc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001707
Neale Ranns6ede5702020-02-13 09:12:36 +00001708 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_path_remove,
Neale Ranns097fa662018-05-01 05:17:55 -07001709 (esrc, pl_flags, rpaths));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001710
1711 /*
1712 * lock the new path-list, unlock the old if it had one
1713 */
1714 fib_path_list_unlock(old_path_list);
1715
1716 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1717 fib_path_list_lock(esrc->fes_pl);
1718 return (FIB_ENTRY_SRC_FLAG_ADDED);
1719 }
1720 else
1721 {
1722 /*
1723 * no more paths left from this source
1724 */
Neale Ranns89541992017-04-06 04:41:02 -07001725 fib_entry_src_action_remove_or_update_inherit(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001726 return (FIB_ENTRY_SRC_FLAG_NONE);
1727 }
1728}
1729
1730u8*
1731fib_entry_src_format (fib_entry_t *fib_entry,
1732 fib_source_t source,
1733 u8* s)
1734{
1735 fib_entry_src_t *esrc;
1736
Neale Ranns2303cb12018-02-21 04:57:17 -08001737 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001738
Neale Ranns2303cb12018-02-21 04:57:17 -08001739 FIB_ENTRY_SRC_VFT_INVOKE_AND_RETURN(esrc, fesv_format, (esrc, s));
1740
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001741 return (s);
1742}
1743
1744adj_index_t
1745fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1746 fib_source_t source)
1747{
1748 fib_entry_t *fib_entry;
1749 fib_entry_src_t *esrc;
1750
1751 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1752 return (ADJ_INDEX_INVALID);
1753
1754 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns2303cb12018-02-21 04:57:17 -08001755 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001756
1757 if (NULL != esrc)
1758 {
1759 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1760 {
1761 return (fib_path_list_get_adj(
1762 esrc->fes_pl,
1763 fib_entry_get_default_chain_type(fib_entry)));
1764 }
1765 }
1766 return (ADJ_INDEX_INVALID);
1767}
1768
1769const int
1770fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1771 fib_source_t source,
1772 dpo_id_t *dpo)
1773{
1774 fib_entry_t *fib_entry;
1775 fib_entry_src_t *esrc;
1776
1777 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1778 return (0);
1779
1780 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns2303cb12018-02-21 04:57:17 -08001781 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001782
1783 if (NULL != esrc)
1784 {
1785 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1786 {
1787 fib_path_list_contribute_forwarding(
1788 esrc->fes_pl,
1789 fib_entry_get_default_chain_type(fib_entry),
Neale Ranns91286372017-12-05 13:24:04 -08001790 FIB_PATH_LIST_FWD_FLAG_NONE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001791 dpo);
1792
1793 return (dpo_id_is_valid(dpo));
1794 }
1795 }
1796 return (0);
1797}
1798
Neale Rannsdf089a82016-10-02 16:39:06 +01001799u32
1800fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1801 fib_source_t source)
1802{
1803 fib_entry_t *fib_entry;
1804 fib_entry_src_t *esrc;
1805
1806 fib_entry = fib_entry_get(entry_index);
1807
Neale Ranns2303cb12018-02-21 04:57:17 -08001808 esrc = fib_entry_src_find(fib_entry, source);
Neale Rannsdf089a82016-10-02 16:39:06 +01001809
1810 if (NULL != esrc)
1811 {
1812 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1813 {
1814 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1815 }
1816 }
1817 return (~0);
1818}
1819
1820fib_entry_flag_t
1821fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1822 fib_source_t source)
1823{
1824 fib_entry_t *fib_entry;
1825 fib_entry_src_t *esrc;
1826
1827 fib_entry = fib_entry_get(entry_index);
1828
Neale Ranns2303cb12018-02-21 04:57:17 -08001829 esrc = fib_entry_src_find(fib_entry, source);
Neale Rannsdf089a82016-10-02 16:39:06 +01001830
1831 if (NULL != esrc)
1832 {
1833 return (esrc->fes_entry_flags);
1834 }
1835
1836 return (FIB_ENTRY_FLAG_NONE);
1837}
1838
Benoît Ganne99c358d2019-07-17 14:47:23 +02001839fib_source_t
1840fib_entry_get_source_i (const fib_entry_t *fib_entry)
1841{
1842 /* the vector of sources is deliberately arranged in priority order */
1843 if (0 == vec_len(fib_entry->fe_srcs))
1844 return (FIB_SOURCE_INVALID);
1845 return (vec_elt(fib_entry->fe_srcs, 0).fes_src);
1846}
1847
Neale Rannsa4e77662017-12-04 20:00:30 +00001848fib_entry_flag_t
1849fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1850{
Benoît Ganne99c358d2019-07-17 14:47:23 +02001851 /* the vector of sources is deliberately arranged in priority order */
Neale Rannsa4e77662017-12-04 20:00:30 +00001852 if (0 == vec_len(fib_entry->fe_srcs))
Benoît Ganne99c358d2019-07-17 14:47:23 +02001853 return (FIB_ENTRY_FLAG_NONE);
1854 return (vec_elt(fib_entry->fe_srcs, 0).fes_entry_flags);
Neale Rannsa4e77662017-12-04 20:00:30 +00001855}
1856
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001857void
1858fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1859 fib_source_t source,
1860 const void *data)
1861{
1862 fib_entry_t *fib_entry;
1863 fib_entry_src_t *esrc;
1864
1865 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns2303cb12018-02-21 04:57:17 -08001866 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001867
Neale Ranns2303cb12018-02-21 04:57:17 -08001868 if (NULL != esrc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001869 {
Neale Ranns6ede5702020-02-13 09:12:36 +00001870 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, esrc, fesv_set_data,
Neale Ranns2303cb12018-02-21 04:57:17 -08001871 (esrc, fib_entry, data));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001872 }
1873}
1874
1875const void*
1876fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1877 fib_source_t source)
1878{
1879 fib_entry_t *fib_entry;
1880 fib_entry_src_t *esrc;
1881
1882 fib_entry = fib_entry_get(fib_entry_index);
Neale Ranns2303cb12018-02-21 04:57:17 -08001883 esrc = fib_entry_src_find(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001884
Neale Ranns2303cb12018-02-21 04:57:17 -08001885 if (NULL != esrc)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001886 {
Neale Ranns2303cb12018-02-21 04:57:17 -08001887 FIB_ENTRY_SRC_VFT_INVOKE_AND_RETURN(esrc, fesv_get_data,
1888 (esrc, fib_entry));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001889 }
1890 return (NULL);
1891}
1892
1893void
1894fib_entry_src_module_init (void)
1895{
1896 fib_entry_src_rr_register();
1897 fib_entry_src_interface_register();
Neale Ranns2303cb12018-02-21 04:57:17 -08001898 fib_entry_src_interpose_register();
Neale Ranns3bab8f92019-12-04 06:11:00 +00001899 fib_entry_src_drop_register();
1900 fib_entry_src_simple_register();
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001901 fib_entry_src_api_register();
1902 fib_entry_src_adj_register();
1903 fib_entry_src_mpls_register();
1904 fib_entry_src_lisp_register();
1905}