blob: 1d73af3bf0d5b1c2e1fc6cc8cd433f804ed02c33 [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 Ranns0bfe5d82016-08-25 15:29:12 +010026
27/*
28 * per-source type vft
29 */
30static fib_entry_src_vft_t fib_entry_src_vft[FIB_SOURCE_MAX];
31
Neale Ranns0bfe5d82016-08-25 15:29:12 +010032void
33fib_entry_src_register (fib_source_t source,
34 const fib_entry_src_vft_t *vft)
35{
36 fib_entry_src_vft[source] = *vft;
37}
38
39static int
40fib_entry_src_cmp_for_sort (void * v1,
41 void * v2)
42{
43 fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
44
45 return (esrc1->fes_src - esrc2->fes_src);
46}
47
48void
49fib_entry_src_action_init (fib_entry_t *fib_entry,
50 fib_source_t source)
51
52{
53 fib_entry_src_t esrc = {
54 .fes_pl = FIB_NODE_INDEX_INVALID,
55 .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
56 .fes_src = source,
57 };
58
59 if (NULL != fib_entry_src_vft[source].fesv_init)
60 {
61 fib_entry_src_vft[source].fesv_init(&esrc);
62 }
63
Neale Rannsa4e77662017-12-04 20:00:30 +000064 vec_add1(fib_entry->fe_srcs, esrc);
65 vec_sort_with_function(fib_entry->fe_srcs,
66 fib_entry_src_cmp_for_sort);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010067}
68
69static fib_entry_src_t *
Neale Rannsa4e77662017-12-04 20:00:30 +000070fib_entry_src_find (const fib_entry_t *fib_entry,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071 fib_source_t source,
72 u32 *index)
73
74{
75 fib_entry_src_t *esrc;
76 int ii;
77
78 ii = 0;
Neale Rannsa4e77662017-12-04 20:00:30 +000079 vec_foreach(esrc, fib_entry->fe_srcs)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080 {
Neale Rannsa4e77662017-12-04 20:00:30 +000081 if (esrc->fes_src == source)
82 {
83 if (NULL != index)
84 {
85 *index = ii;
86 }
87 return (esrc);
88 }
89 else
90 {
91 ii++;
92 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010093 }
94
95 return (NULL);
96}
97
98int
99fib_entry_is_sourced (fib_node_index_t fib_entry_index,
100 fib_source_t source)
101{
102 fib_entry_t *fib_entry;
103
104 fib_entry = fib_entry_get(fib_entry_index);
105
106 return (NULL != fib_entry_src_find(fib_entry, source, NULL));
107}
108
109static fib_entry_src_t *
110fib_entry_src_find_or_create (fib_entry_t *fib_entry,
Neale Rannsa4e77662017-12-04 20:00:30 +0000111 fib_source_t source,
112 u32 *index)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100113{
114 fib_entry_src_t *esrc;
115
116 esrc = fib_entry_src_find(fib_entry, source, NULL);
117
118 if (NULL == esrc)
119 {
120 fib_entry_src_action_init(fib_entry, source);
121 }
122
123 return (fib_entry_src_find(fib_entry, source, NULL));
124}
125
126void
127fib_entry_src_action_deinit (fib_entry_t *fib_entry,
128 fib_source_t source)
129
130{
131 fib_entry_src_t *esrc;
132 u32 index = ~0;
133
134 esrc = fib_entry_src_find(fib_entry, source, &index);
135
136 ASSERT(NULL != esrc);
137
138 if (NULL != fib_entry_src_vft[source].fesv_deinit)
139 {
140 fib_entry_src_vft[source].fesv_deinit(esrc);
141 }
142
Neale Ranns81424992017-05-18 03:03:22 -0700143 fib_path_ext_list_flush(&esrc->fes_path_exts);
Neale Rannsa4e77662017-12-04 20:00:30 +0000144 vec_del1(fib_entry->fe_srcs, index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145}
146
147fib_entry_src_cover_res_t
148fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
149 fib_source_t source)
150{
151 if (NULL != fib_entry_src_vft[source].fesv_cover_change)
152 {
153 return (fib_entry_src_vft[source].fesv_cover_change(
154 fib_entry_src_find(fib_entry, source, NULL),
155 fib_entry));
156 }
157
158 fib_entry_src_cover_res_t res = {
159 .install = !0,
160 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
161 };
162 return (res);
163}
164
165fib_entry_src_cover_res_t
166fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
167 fib_source_t source)
168{
169 if (NULL != fib_entry_src_vft[source].fesv_cover_update)
170 {
171 return (fib_entry_src_vft[source].fesv_cover_update(
172 fib_entry_src_find(fib_entry, source, NULL),
173 fib_entry));
174 }
175
176 fib_entry_src_cover_res_t res = {
177 .install = !0,
178 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
179 };
180 return (res);
181}
182
183typedef struct fib_entry_src_collect_forwarding_ctx_t_
184{
Neale Ranns81424992017-05-18 03:03:22 -0700185 load_balance_path_t *next_hops;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100186 const fib_entry_t *fib_entry;
187 const fib_entry_src_t *esrc;
188 fib_forward_chain_type_t fct;
Neale Rannsf12a83f2017-04-18 09:09:40 -0700189 int n_recursive_constrained;
Neale Ranns57b58602017-07-15 07:37:25 -0700190 u16 preference;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100191} fib_entry_src_collect_forwarding_ctx_t;
192
193/**
194 * @brief Determine whether this FIB entry should use a load-balance MAP
195 * to support PIC edge fast convergence
196 */
197load_balance_flags_t
198fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
199{
200 /**
Neale Rannsf12a83f2017-04-18 09:09:40 -0700201 * We'll use a LB map if the path-list has multiple recursive paths.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202 * recursive paths implies BGP, and hence scale.
203 */
Neale Rannsf12a83f2017-04-18 09:09:40 -0700204 if (ctx->n_recursive_constrained > 1 &&
205 fib_path_list_is_popular(ctx->esrc->fes_pl))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100206 {
207 return (LOAD_BALANCE_FLAG_USES_MAP);
208 }
209 return (LOAD_BALANCE_FLAG_NONE);
210}
211
212static int
213fib_entry_src_valid_out_label (mpls_label_t label)
214{
215 return ((MPLS_LABEL_IS_REAL(label) ||
216 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
217 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
218 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
219}
220
Neale Rannsad422ed2016-11-02 14:20:04 +0000221/**
222 * @brief Turn the chain type requested by the client into the one they
223 * really wanted
224 */
225fib_forward_chain_type_t
226fib_entry_chain_type_fixup (const fib_entry_t *entry,
227 fib_forward_chain_type_t fct)
228{
Neale Rannsad422ed2016-11-02 14:20:04 +0000229 /*
230 * The EOS chain is a tricky since one cannot know the adjacency
231 * to link to without knowing what the packets payload protocol
232 * will be once the label is popped.
233 */
234 fib_forward_chain_type_t dfct;
235
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800236 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS != fct)
237 {
238 return (fct);
239 }
240
Neale Rannsad422ed2016-11-02 14:20:04 +0000241 dfct = fib_entry_get_default_chain_type(entry);
242
243 if (FIB_FORW_CHAIN_TYPE_MPLS_EOS == dfct)
244 {
245 /*
246 * If the entry being asked is a eos-MPLS label entry,
247 * then use the payload-protocol field, that we stashed there
248 * for just this purpose
249 */
250 return (fib_forw_chain_type_from_dpo_proto(
251 entry->fe_prefix.fp_payload_proto));
252 }
253 /*
254 * else give them what this entry would be by default. i.e. if it's a v6
255 * entry, then the label its local labelled should be carrying v6 traffic.
256 * If it's a non-EOS label entry, then there are more labels and we want
257 * a non-eos chain.
258 */
259 return (dfct);
260}
261
Neale Ranns62fe07c2017-10-31 12:28:22 -0700262static dpo_proto_t
263fib_prefix_get_payload_proto (const fib_prefix_t *pfx)
264{
265 switch (pfx->fp_proto)
266 {
267 case FIB_PROTOCOL_IP4:
268 return (DPO_PROTO_IP4);
269 case FIB_PROTOCOL_IP6:
270 return (DPO_PROTO_IP6);
271 case FIB_PROTOCOL_MPLS:
272 return (pfx->fp_payload_proto);
273 }
274
275 ASSERT(0);
276 return (DPO_PROTO_IP4);
277}
278
Neale Ranns81424992017-05-18 03:03:22 -0700279static void
280fib_entry_src_get_path_forwarding (fib_node_index_t path_index,
281 fib_entry_src_collect_forwarding_ctx_t *ctx)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100282{
Neale Ranns81424992017-05-18 03:03:22 -0700283 load_balance_path_t *nh;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100284
285 /*
Neale Ranns81424992017-05-18 03:03:22 -0700286 * no extension => no out-going label for this path. that's OK
287 * in the case of an IP or EOS chain, but not for non-EOS
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100288 */
Neale Ranns81424992017-05-18 03:03:22 -0700289 switch (ctx->fct)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100290 {
Neale Ranns81424992017-05-18 03:03:22 -0700291 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
292 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
293 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
294 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
Neale Rannsd792d9c2017-10-21 10:53:20 -0700295 case FIB_FORW_CHAIN_TYPE_BIER:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100296 /*
Neale Ranns81424992017-05-18 03:03:22 -0700297 * EOS traffic with no label to stack, we need the IP Adj
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100298 */
Neale Ranns81424992017-05-18 03:03:22 -0700299 vec_add2(ctx->next_hops, nh, 1);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100300
Neale Ranns81424992017-05-18 03:03:22 -0700301 nh->path_index = path_index;
302 nh->path_weight = fib_path_get_weight(path_index);
303 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
304
305 break;
306 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
307 if (fib_path_is_exclusive(path_index) ||
308 fib_path_is_deag(path_index))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100309 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100310 vec_add2(ctx->next_hops, nh, 1);
311
312 nh->path_index = path_index;
313 nh->path_weight = fib_path_get_weight(path_index);
Neale Ranns81424992017-05-18 03:03:22 -0700314 fib_path_contribute_forwarding(path_index,
315 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
316 &nh->path_dpo);
317 }
318 break;
319 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
Neale Rannsad422ed2016-11-02 14:20:04 +0000320 {
321 /*
322 * no label. we need a chain based on the payload. fixup.
323 */
324 vec_add2(ctx->next_hops, nh, 1);
325
326 nh->path_index = path_index;
327 nh->path_weight = fib_path_get_weight(path_index);
328 fib_path_contribute_forwarding(path_index,
329 fib_entry_chain_type_fixup(ctx->fib_entry,
330 ctx->fct),
331 &nh->path_dpo);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800332 fib_path_stack_mpls_disp(path_index,
Neale Ranns62fe07c2017-10-31 12:28:22 -0700333 fib_prefix_get_payload_proto(&ctx->fib_entry->fe_prefix),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800334 &nh->path_dpo);
Neale Rannsad422ed2016-11-02 14:20:04 +0000335
336 break;
337 }
Neale Ranns81424992017-05-18 03:03:22 -0700338 case FIB_FORW_CHAIN_TYPE_ETHERNET:
339 case FIB_FORW_CHAIN_TYPE_NSH:
340 ASSERT(0);
341 break;
342 }
343}
344
345static fib_path_list_walk_rc_t
346fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
347 fib_node_index_t path_index,
348 void *arg)
349{
350 fib_entry_src_collect_forwarding_ctx_t *ctx;
351 fib_path_ext_t *path_ext;
352
353 ctx = arg;
354
355 /*
356 * if the path is not resolved, don't include it.
357 */
358 if (!fib_path_is_resolved(path_index))
359 {
360 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100361 }
362
Neale Ranns81424992017-05-18 03:03:22 -0700363 if (fib_path_is_recursive_constrained(path_index))
364 {
365 ctx->n_recursive_constrained += 1;
366 }
Neale Ranns57b58602017-07-15 07:37:25 -0700367 if (0xffff == ctx->preference)
368 {
369 /*
370 * not set a preference yet, so the first path we encounter
371 * sets the preference we are collecting.
372 */
373 ctx->preference = fib_path_get_preference(path_index);
374 }
375 else if (ctx->preference != fib_path_get_preference(path_index))
376 {
377 /*
378 * this path does not belong to the same preference as the
379 * previous paths encountered. we are done now.
380 */
381 return (FIB_PATH_LIST_WALK_STOP);
382 }
Neale Ranns81424992017-05-18 03:03:22 -0700383
384 /*
385 * get the matching path-extension for the path being visited.
386 */
387 path_ext = fib_path_ext_list_find_by_path_index(&ctx->esrc->fes_path_exts,
388 path_index);
389
390 if (NULL != path_ext)
391 {
392 switch (path_ext->fpe_type)
393 {
394 case FIB_PATH_EXT_MPLS:
395 if (fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
396 {
397 /*
398 * found a matching extension. stack it to obtain the forwarding
399 * info for this path.
400 */
401 ctx->next_hops =
402 fib_path_ext_stack(path_ext,
403 ctx->fct,
404 fib_entry_chain_type_fixup(ctx->fib_entry,
405 ctx->fct),
406 ctx->next_hops);
407 }
408 else
409 {
410 fib_entry_src_get_path_forwarding(path_index, ctx);
411 }
412 break;
413 case FIB_PATH_EXT_ADJ:
414 if (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & path_ext->fpe_adj_flags)
415 {
416 fib_entry_src_get_path_forwarding(path_index, ctx);
417 }
418 /*
419 * else
420 * the path does not refine the cover, meaning that
421 * the adjacency doesdoes not match the sub-net on the link.
422 * So this path does not contribute forwarding.
423 */
424 break;
425 }
426 }
427 else
428 {
429 fib_entry_src_get_path_forwarding(path_index, ctx);
430 }
431
432 return (FIB_PATH_LIST_WALK_CONTINUE);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100433}
434
435void
436fib_entry_src_mk_lb (fib_entry_t *fib_entry,
437 const fib_entry_src_t *esrc,
438 fib_forward_chain_type_t fct,
439 dpo_id_t *dpo_lb)
440{
441 dpo_proto_t lb_proto;
442
443 /*
444 * If the entry has path extensions then we construct a load-balance
445 * by stacking the extensions on the forwarding chains of the paths.
446 * Otherwise we use the load-balance of the path-list
447 */
448 fib_entry_src_collect_forwarding_ctx_t ctx = {
449 .esrc = esrc,
450 .fib_entry = fib_entry,
451 .next_hops = NULL,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700452 .n_recursive_constrained = 0,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100453 .fct = fct,
Neale Ranns57b58602017-07-15 07:37:25 -0700454 .preference = 0xffff,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100455 };
456
Neale Rannsc0790cf2017-01-05 01:01:47 -0800457 /*
458 * As an optimisation we allocate the vector of next-hops to be sized
459 * equal to the maximum nuber of paths we will need, which is also the
460 * most likely number we will need, since in most cases the paths are 'up'.
461 */
462 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
463 vec_reset_length(ctx.next_hops);
464
Neale Rannsf12a83f2017-04-18 09:09:40 -0700465 lb_proto = fib_forw_chain_type_to_dpo_proto(fct);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100466
467 fib_path_list_walk(esrc->fes_pl,
468 fib_entry_src_collect_forwarding,
469 &ctx);
470
471 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
472 {
473 /*
474 * the client provided the DPO that the entry should link to.
475 * all entries must link to a LB, so if it is an LB already
476 * then we can use it.
477 */
478 if ((1 == vec_len(ctx.next_hops)) &&
479 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
480 {
481 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
482 dpo_reset(&ctx.next_hops[0].path_dpo);
483 return;
484 }
485 }
486
487 if (!dpo_id_is_valid(dpo_lb))
488 {
489 /*
490 * first time create
491 */
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800492 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
493 {
494 dpo_set(dpo_lb,
495 DPO_REPLICATE,
496 lb_proto,
497 MPLS_IS_REPLICATE | replicate_create(0, lb_proto));
498 }
499 else
500 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700501 fib_protocol_t flow_hash_proto;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800502 flow_hash_config_t fhc;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100503
Neale Ranns41da54f2017-05-02 10:15:19 -0700504 /*
505 * if the protocol for the LB we are building does not match that
506 * of the fib_entry (i.e. we are build the [n]EOS LB for an IPv[46]
507 * then the fib_index is not an index that relates to the table
508 * type we need. So get the default flow-hash config instead.
509 */
Neale Rannsd792d9c2017-10-21 10:53:20 -0700510 flow_hash_proto = dpo_proto_to_fib(lb_proto);
511 if (fib_entry->fe_prefix.fp_proto != flow_hash_proto)
Neale Ranns41da54f2017-05-02 10:15:19 -0700512 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700513 fhc = fib_table_get_default_flow_hash_config(flow_hash_proto);
Neale Ranns41da54f2017-05-02 10:15:19 -0700514 }
515 else
516 {
Neale Rannsd792d9c2017-10-21 10:53:20 -0700517 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
518 flow_hash_proto);
Neale Ranns41da54f2017-05-02 10:15:19 -0700519 }
Neale Rannsd792d9c2017-10-21 10:53:20 -0700520
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800521 dpo_set(dpo_lb,
522 DPO_LOAD_BALANCE,
523 lb_proto,
524 load_balance_create(0, lb_proto, fhc));
525 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100526 }
527
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800528 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_MULTICAST)
Neale Ranns3ee44042016-10-03 13:05:48 +0100529 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800530 /*
531 * MPLS multicast
532 */
533 replicate_multipath_update(dpo_lb, ctx.next_hops);
Neale Ranns3ee44042016-10-03 13:05:48 +0100534 }
535 else
536 {
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800537 load_balance_multipath_update(dpo_lb,
538 ctx.next_hops,
539 fib_entry_calc_lb_flags(&ctx));
540 vec_free(ctx.next_hops);
541
542 /*
543 * if this entry is sourced by the uRPF-exempt source then we
544 * append the always present local0 interface (index 0) to the
545 * uRPF list so it is not empty. that way packets pass the loose check.
546 */
547 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
548
549 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
550 FIB_SOURCE_URPF_EXEMPT) ||
551 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
552 (0 == fib_urpf_check_size(ui)))
553 {
554 /*
555 * The uRPF list we get from the path-list is shared by all
556 * other users of the list, but the uRPF exemption applies
557 * only to this prefix. So we need our own list.
558 */
559 ui = fib_urpf_list_alloc_and_lock();
560 fib_urpf_list_append(ui, 0);
561 fib_urpf_list_bake(ui);
562 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
563 fib_urpf_list_unlock(ui);
564 }
565 else
566 {
567 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
568 }
569 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
570 fib_entry_get_flags_i(fib_entry));
Neale Ranns3ee44042016-10-03 13:05:48 +0100571 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100572}
573
574void
575fib_entry_src_action_install (fib_entry_t *fib_entry,
576 fib_source_t source)
577{
578 /*
579 * Install the forwarding chain for the given source into the forwarding
580 * tables
581 */
582 fib_forward_chain_type_t fct;
583 fib_entry_src_t *esrc;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100584 int insert;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100585
586 fct = fib_entry_get_default_chain_type(fib_entry);
587 esrc = fib_entry_src_find(fib_entry, source, NULL);
588
Neale Ranns33a7dd52016-10-07 15:14:33 +0100589 /*
590 * Every entry has its own load-balance object. All changes to the entry's
591 * forwarding result in an inplace modify of the load-balance. This means
592 * the load-balance object only needs to be added to the forwarding
593 * DB once, when it is created.
594 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000595 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100596
Neale Rannsad422ed2016-11-02 14:20:04 +0000597 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100598
Neale Rannsad422ed2016-11-02 14:20:04 +0000599 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
600 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100601
602 /*
603 * insert the adj into the data-plane forwarding trie
604 */
Neale Ranns33a7dd52016-10-07 15:14:33 +0100605 if (insert)
606 {
607 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
608 &fib_entry->fe_prefix,
Neale Rannsad422ed2016-11-02 14:20:04 +0000609 &fib_entry->fe_lb);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100610 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100611
Neale Rannsad422ed2016-11-02 14:20:04 +0000612 /*
613 * if any of the other chain types are already created they will need
614 * updating too
615 */
616 fib_entry_delegate_type_t fdt;
617 fib_entry_delegate_t *fed;
618
619 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100620 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000621 fib_entry_src_mk_lb(fib_entry, esrc,
622 fib_entry_delegate_type_to_chain_type(fdt),
623 &fed->fd_dpo);
624 });
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100625}
626
627void
628fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
629{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100630 /*
Neale Ranns3ee44042016-10-03 13:05:48 +0100631 * uninstall the forwarding chain from the forwarding tables
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100632 */
633 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
634 fib_entry->fe_adj_index);
635
Neale Rannsad422ed2016-11-02 14:20:04 +0000636 if (dpo_id_is_valid(&fib_entry->fe_lb))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100637 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100638 fib_table_fwding_dpo_remove(
639 fib_entry->fe_fib_index,
640 &fib_entry->fe_prefix,
Neale Rannsad422ed2016-11-02 14:20:04 +0000641 &fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100642
Neale Rannsad422ed2016-11-02 14:20:04 +0000643 dpo_reset(&fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100644 }
645}
646
647static void
648fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
649{
650 fib_node_index_t *entries = NULL;
651
652 fib_path_list_recursive_loop_detect(path_list_index, &entries);
653
654 vec_free(entries);
655}
656
657void
658fib_entry_src_action_activate (fib_entry_t *fib_entry,
659 fib_source_t source)
660
661{
662 int houston_we_are_go_for_install;
663 fib_entry_src_t *esrc;
664
665 esrc = fib_entry_src_find(fib_entry, source, NULL);
666
667 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
668 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
669
670 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
671
672 if (NULL != fib_entry_src_vft[source].fesv_activate)
673 {
674 houston_we_are_go_for_install =
675 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
676 }
677 else
678 {
679 /*
680 * the source is not providing an activate function, we'll assume
681 * therefore it has no objection to installing the entry
682 */
683 houston_we_are_go_for_install = !0;
684 }
685
686 /*
687 * link to the path-list provided by the source, and go check
688 * if that forms any loops in the graph.
689 */
690 fib_entry->fe_parent = esrc->fes_pl;
691 fib_entry->fe_sibling =
692 fib_path_list_child_add(fib_entry->fe_parent,
693 FIB_NODE_TYPE_ENTRY,
694 fib_entry_get_index(fib_entry));
695
696 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
697
698 FIB_ENTRY_DBG(fib_entry, "activate: %d",
699 fib_entry->fe_parent);
700
701 if (0 != houston_we_are_go_for_install)
702 {
703 fib_entry_src_action_install(fib_entry, source);
704 }
705 else
706 {
707 fib_entry_src_action_uninstall(fib_entry);
708 }
709}
710
711void
712fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
713 fib_source_t source)
714
715{
716 fib_node_index_t path_list_index;
717 fib_entry_src_t *esrc;
718
719 esrc = fib_entry_src_find(fib_entry, source, NULL);
720
721 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
722
723 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
724 {
725 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
726 }
727
728 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
729
730 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
731
732 /*
733 * un-link from an old path-list. Check for any loops this will clear
734 */
735 path_list_index = fib_entry->fe_parent;
736 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
737
738 fib_entry_recursive_loop_detect_i(path_list_index);
739
740 /*
741 * this will unlock the path-list, so it may be invalid thereafter.
742 */
743 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
744 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
745}
746
Neale Rannsa4e77662017-12-04 20:00:30 +0000747static void
748fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
749 fib_source_t source)
750{
751 fib_entry_src_t *esrc;
752
753 vec_foreach(esrc, fib_entry->fe_srcs)
754 {
755 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
756 {
757 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
758 fib_entry,
759 source);
760 }
761 }
762}
763
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100764void
765fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
766 fib_source_t source)
767{
768 fib_node_index_t path_list_index;
769 fib_entry_src_t *esrc;
770
771 esrc = fib_entry_src_find(fib_entry, source, NULL);
772
773 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
774
775 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
776 fib_entry->fe_parent,
777 esrc->fes_pl);
778
779 if (fib_entry->fe_parent != esrc->fes_pl)
780 {
781 /*
782 * un-link from an old path-list. Check for any loops this will clear
783 */
784 path_list_index = fib_entry->fe_parent;
785 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
786
787 /*
788 * temporary lock so it doesn't get deleted when this entry is no
789 * longer a child.
790 */
791 fib_path_list_lock(path_list_index);
792
793 /*
794 * this entry is no longer a child. after unlinking check if any loops
795 * were broken
796 */
797 fib_path_list_child_remove(path_list_index,
798 fib_entry->fe_sibling);
799
800 fib_entry_recursive_loop_detect_i(path_list_index);
801
802 /*
803 * link to the path-list provided by the source, and go check
804 * if that forms any loops in the graph.
805 */
806 fib_entry->fe_parent = esrc->fes_pl;
807 fib_entry->fe_sibling =
808 fib_path_list_child_add(fib_entry->fe_parent,
809 FIB_NODE_TYPE_ENTRY,
810 fib_entry_get_index(fib_entry));
811
812 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
813 fib_path_list_unlock(path_list_index);
814 }
815 fib_entry_src_action_install(fib_entry, source);
Neale Rannsa4e77662017-12-04 20:00:30 +0000816 fib_entry_src_action_fwd_update(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100817}
818
819void
Neale Rannsa4e77662017-12-04 20:00:30 +0000820fib_entry_src_action_installed (const fib_entry_t *fib_entry,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100821 fib_source_t source)
822{
823 fib_entry_src_t *esrc;
824
825 esrc = fib_entry_src_find(fib_entry, source, NULL);
826
827 if (NULL != fib_entry_src_vft[source].fesv_installed)
828 {
829 fib_entry_src_vft[source].fesv_installed(esrc,
830 fib_entry);
831 }
Neale Rannsa4e77662017-12-04 20:00:30 +0000832
833 fib_entry_src_action_fwd_update(fib_entry, source);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100834}
835
836/*
837 * fib_entry_src_action_add
838 *
839 * Adding a source can result in a new fib_entry being created, which
840 * can inturn mean the pool is realloc'd and thus the entry passed as
841 * an argument it also realloc'd
842 * @return the original entry
843 */
844fib_entry_t *
845fib_entry_src_action_add (fib_entry_t *fib_entry,
846 fib_source_t source,
847 fib_entry_flag_t flags,
848 const dpo_id_t *dpo)
849{
850 fib_node_index_t fib_entry_index;
851 fib_entry_src_t *esrc;
852
Neale Rannsa4e77662017-12-04 20:00:30 +0000853 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100854
855 esrc->fes_ref_count++;
856
857 if (1 != esrc->fes_ref_count)
858 {
859 /*
860 * we only want to add the source on the 0->1 transition
861 */
862 return (fib_entry);
863 }
864
865 esrc->fes_entry_flags = flags;
866
867 /*
868 * save variable so we can recover from a fib_entry realloc.
869 */
870 fib_entry_index = fib_entry_get_index(fib_entry);
871
872 if (NULL != fib_entry_src_vft[source].fesv_add)
873 {
874 fib_entry_src_vft[source].fesv_add(esrc,
875 fib_entry,
876 flags,
Neale Rannsda78f952017-05-24 09:15:43 -0700877 fib_entry_get_dpo_proto(fib_entry),
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100878 dpo);
879 }
880
881 fib_entry = fib_entry_get(fib_entry_index);
882
883 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
884
885 fib_path_list_lock(esrc->fes_pl);
886
887 /*
888 * the source owns a lock on the entry
889 */
890 fib_entry_lock(fib_entry_get_index(fib_entry));
891
892 return (fib_entry);
893}
894
Neale Ranns948e00f2016-10-20 13:39:34 +0100895/*
896 * fib_entry_src_action_update
897 *
898 * Adding a source can result in a new fib_entry being created, which
899 * can inturn mean the pool is realloc'd and thus the entry passed as
900 * an argument it also realloc'd
901 * @return the original entry
902 */
903fib_entry_t *
904fib_entry_src_action_update (fib_entry_t *fib_entry,
905 fib_source_t source,
906 fib_entry_flag_t flags,
907 const dpo_id_t *dpo)
908{
909 fib_node_index_t fib_entry_index, old_path_list_index;
910 fib_entry_src_t *esrc;
911
Neale Rannsa4e77662017-12-04 20:00:30 +0000912 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
Neale Ranns948e00f2016-10-20 13:39:34 +0100913
914 if (NULL == esrc)
915 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
916
917 old_path_list_index = esrc->fes_pl;
918 esrc->fes_entry_flags = flags;
919
920 /*
921 * save variable so we can recover from a fib_entry realloc.
922 */
923 fib_entry_index = fib_entry_get_index(fib_entry);
924
925 if (NULL != fib_entry_src_vft[source].fesv_add)
926 {
927 fib_entry_src_vft[source].fesv_add(esrc,
928 fib_entry,
929 flags,
Neale Rannsda78f952017-05-24 09:15:43 -0700930 fib_entry_get_dpo_proto(fib_entry),
Neale Ranns948e00f2016-10-20 13:39:34 +0100931 dpo);
932 }
933
934 fib_entry = fib_entry_get(fib_entry_index);
935
936 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
937
938 fib_path_list_lock(esrc->fes_pl);
939 fib_path_list_unlock(old_path_list_index);
940
941 return (fib_entry);
942}
943
944
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100945fib_entry_src_flag_t
946fib_entry_src_action_remove (fib_entry_t *fib_entry,
947 fib_source_t source)
948
949{
950 fib_node_index_t old_path_list;
951 fib_entry_src_flag_t sflags;
952 fib_entry_src_t *esrc;
953
954 esrc = fib_entry_src_find(fib_entry, source, NULL);
955
956 if (NULL == esrc)
957 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
958
959 esrc->fes_ref_count--;
960 sflags = esrc->fes_flags;
961
962 if (0 != esrc->fes_ref_count)
963 {
964 /*
965 * only remove the source on the 1->0 transisition
966 */
967 return (sflags);
968 }
969
970 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
971 {
972 fib_entry_src_action_deactivate(fib_entry, source);
973 }
974
975 old_path_list = esrc->fes_pl;
976
977 if (NULL != fib_entry_src_vft[source].fesv_remove)
978 {
979 fib_entry_src_vft[source].fesv_remove(esrc);
980 }
981
982 fib_path_list_unlock(old_path_list);
983 fib_entry_unlock(fib_entry_get_index(fib_entry));
984
985 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
986 fib_entry_src_action_deinit(fib_entry, source);
987
988 return (sflags);
989}
990
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100991/*
992 * fib_route_attached_cross_table
993 *
994 * Return true the the route is attached via an interface that
995 * is not in the same table as the route
996 */
997static inline int
998fib_route_attached_cross_table (const fib_entry_t *fib_entry,
999 const fib_route_path_t *rpath)
1000{
1001 /*
1002 * - All zeros next-hop
1003 * - a valid interface
1004 * - entry's fib index not equeal to interface's index
1005 */
1006 if (ip46_address_is_zero(&rpath->frp_addr) &&
1007 (~0 != rpath->frp_sw_if_index) &&
1008 (fib_entry->fe_fib_index !=
1009 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
1010 rpath->frp_sw_if_index)))
1011 {
1012 return (!0);
1013 }
1014 return (0);
1015}
1016
1017/*
1018 * fib_route_attached_cross_table
1019 *
1020 * Return true the the route is attached via an interface that
1021 * is not in the same table as the route
1022 */
1023static inline int
1024fib_path_is_attached (const fib_route_path_t *rpath)
1025{
1026 /*
1027 * - All zeros next-hop
1028 * - a valid interface
1029 */
1030 if (ip46_address_is_zero(&rpath->frp_addr) &&
1031 (~0 != rpath->frp_sw_if_index))
1032 {
1033 return (!0);
1034 }
Neale Ranns4b919a52017-03-11 05:55:21 -08001035 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
1036 {
1037 return (!0);
1038 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001039 return (0);
1040}
1041
1042fib_path_list_flags_t
1043fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
1044{
1045 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
1046
1047 if (eflags & FIB_ENTRY_FLAG_DROP)
1048 {
1049 plf |= FIB_PATH_LIST_FLAG_DROP;
1050 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001051 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
1052 {
1053 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
1054 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001055 if (eflags & FIB_ENTRY_FLAG_LOCAL)
1056 {
1057 plf |= FIB_PATH_LIST_FLAG_LOCAL;
1058 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001059
1060 return (plf);
1061}
1062
1063static void
1064fib_entry_flags_update (const fib_entry_t *fib_entry,
1065 const fib_route_path_t *rpath,
1066 fib_path_list_flags_t *pl_flags,
1067 fib_entry_src_t *esrc)
1068{
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001069 if ((esrc->fes_src == FIB_SOURCE_API) ||
1070 (esrc->fes_src == FIB_SOURCE_CLI))
1071 {
1072 if (fib_path_is_attached(rpath))
1073 {
1074 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1075 }
1076 else
1077 {
1078 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1079 }
1080 }
1081 if (fib_route_attached_cross_table(fib_entry, rpath))
1082 {
1083 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1084 }
1085 else
1086 {
1087 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1088 }
1089}
1090
1091/*
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001092 * fib_entry_src_action_add
1093 *
1094 * Adding a source can result in a new fib_entry being created, which
1095 * can inturn mean the pool is realloc'd and thus the entry passed as
1096 * an argument it also realloc'd
1097 * @return the entry
1098 */
1099fib_entry_t*
1100fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1101 fib_source_t source,
1102 fib_entry_flag_t flags,
1103 const fib_route_path_t *rpath)
1104{
1105 fib_node_index_t old_path_list, fib_entry_index;
1106 fib_path_list_flags_t pl_flags;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001107 fib_entry_src_t *esrc;
1108
1109 /*
1110 * save variable so we can recover from a fib_entry realloc.
1111 */
1112 fib_entry_index = fib_entry_get_index(fib_entry);
1113
1114 esrc = fib_entry_src_find(fib_entry, source, NULL);
1115 if (NULL == esrc)
1116 {
1117 fib_entry =
1118 fib_entry_src_action_add(fib_entry,
1119 source,
1120 flags,
1121 drop_dpo_get(
Neale Rannsda78f952017-05-24 09:15:43 -07001122 fib_entry_get_dpo_proto(fib_entry)));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001123 esrc = fib_entry_src_find(fib_entry, source, NULL);
1124 }
1125
1126 /*
1127 * we are no doubt modifying a path-list. If the path-list
1128 * is shared, and hence not modifiable, then the index returned
1129 * will be for a different path-list. This FIB entry to needs
1130 * to maintain its lock appropriately.
1131 */
1132 old_path_list = esrc->fes_pl;
1133
1134 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1135
1136 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1137 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1138
1139 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1140 fib_entry = fib_entry_get(fib_entry_index);
1141
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001142 fib_path_list_lock(esrc->fes_pl);
1143 fib_path_list_unlock(old_path_list);
1144
1145 return (fib_entry);
1146}
1147
1148/*
1149 * fib_entry_src_action_swap
1150 *
1151 * The source is providing new paths to replace the old ones.
1152 * Adding a source can result in a new fib_entry being created, which
1153 * can inturn mean the pool is realloc'd and thus the entry passed as
1154 * an argument it also realloc'd
1155 * @return the entry
1156 */
1157fib_entry_t*
1158fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1159 fib_source_t source,
1160 fib_entry_flag_t flags,
1161 const fib_route_path_t *rpaths)
1162{
1163 fib_node_index_t old_path_list, fib_entry_index;
1164 fib_path_list_flags_t pl_flags;
1165 const fib_route_path_t *rpath;
1166 fib_entry_src_t *esrc;
1167
1168 esrc = fib_entry_src_find(fib_entry, source, NULL);
1169
1170 /*
1171 * save variable so we can recover from a fib_entry realloc.
1172 */
1173 fib_entry_index = fib_entry_get_index(fib_entry);
1174
1175 if (NULL == esrc)
1176 {
1177 fib_entry = fib_entry_src_action_add(fib_entry,
1178 source,
1179 flags,
1180 drop_dpo_get(
Neale Rannsda78f952017-05-24 09:15:43 -07001181 fib_entry_get_dpo_proto(fib_entry)));
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001182 esrc = fib_entry_src_find(fib_entry, source, NULL);
1183 }
1184
1185 /*
1186 * swapping paths may create a new path-list (or may use an existing shared)
1187 * but we are certainly getting a different one. This FIB entry to needs
1188 * to maintain its lock appropriately.
1189 */
1190 old_path_list = esrc->fes_pl;
1191
1192 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1193
Neale Rannsdf089a82016-10-02 16:39:06 +01001194 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1195
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001196 vec_foreach(rpath, rpaths)
1197 {
1198 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1199 }
1200
1201 fib_entry_src_vft[source].fesv_path_swap(esrc,
1202 fib_entry,
1203 pl_flags,
1204 rpaths);
1205
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001206 fib_entry = fib_entry_get(fib_entry_index);
1207
1208 fib_path_list_lock(esrc->fes_pl);
1209 fib_path_list_unlock(old_path_list);
1210
1211 return (fib_entry);
1212}
1213
1214fib_entry_src_flag_t
1215fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1216 fib_source_t source,
1217 const fib_route_path_t *rpath)
1218{
1219 fib_path_list_flags_t pl_flags;
1220 fib_node_index_t old_path_list;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001221 fib_entry_src_t *esrc;
1222
1223 esrc = fib_entry_src_find(fib_entry, source, NULL);
1224
1225 ASSERT(NULL != esrc);
1226 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1227
1228 /*
1229 * we no doubt modifying a path-list. If the path-list
1230 * is shared, and hence not modifiable, then the index returned
1231 * will be for a different path-list. This FIB entry to needs
1232 * to maintain its lock appropriately.
1233 */
1234 old_path_list = esrc->fes_pl;
1235
1236 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1237
1238 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1239 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1240
1241 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001242
1243 /*
1244 * lock the new path-list, unlock the old if it had one
1245 */
1246 fib_path_list_unlock(old_path_list);
1247
1248 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1249 fib_path_list_lock(esrc->fes_pl);
1250 return (FIB_ENTRY_SRC_FLAG_ADDED);
1251 }
1252 else
1253 {
1254 /*
1255 * no more paths left from this source
1256 */
1257 fib_entry_src_action_remove(fib_entry, source);
1258 return (FIB_ENTRY_SRC_FLAG_NONE);
1259 }
1260}
1261
1262u8*
1263fib_entry_src_format (fib_entry_t *fib_entry,
1264 fib_source_t source,
1265 u8* s)
1266{
1267 fib_entry_src_t *esrc;
1268
1269 esrc = fib_entry_src_find(fib_entry, source, NULL);
1270
1271 if (NULL != fib_entry_src_vft[source].fesv_format)
1272 {
1273 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1274 }
1275 return (s);
1276}
1277
1278adj_index_t
1279fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1280 fib_source_t source)
1281{
1282 fib_entry_t *fib_entry;
1283 fib_entry_src_t *esrc;
1284
1285 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1286 return (ADJ_INDEX_INVALID);
1287
1288 fib_entry = fib_entry_get(fib_entry_index);
1289 esrc = fib_entry_src_find(fib_entry, source, NULL);
1290
1291 if (NULL != esrc)
1292 {
1293 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1294 {
1295 return (fib_path_list_get_adj(
1296 esrc->fes_pl,
1297 fib_entry_get_default_chain_type(fib_entry)));
1298 }
1299 }
1300 return (ADJ_INDEX_INVALID);
1301}
1302
1303const int
1304fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1305 fib_source_t source,
1306 dpo_id_t *dpo)
1307{
1308 fib_entry_t *fib_entry;
1309 fib_entry_src_t *esrc;
1310
1311 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1312 return (0);
1313
1314 fib_entry = fib_entry_get(fib_entry_index);
1315 esrc = fib_entry_src_find(fib_entry, source, NULL);
1316
1317 if (NULL != esrc)
1318 {
1319 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1320 {
1321 fib_path_list_contribute_forwarding(
1322 esrc->fes_pl,
1323 fib_entry_get_default_chain_type(fib_entry),
Neale Ranns91286372017-12-05 13:24:04 -08001324 FIB_PATH_LIST_FWD_FLAG_NONE,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001325 dpo);
1326
1327 return (dpo_id_is_valid(dpo));
1328 }
1329 }
1330 return (0);
1331}
1332
Neale Rannsdf089a82016-10-02 16:39:06 +01001333u32
1334fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1335 fib_source_t source)
1336{
1337 fib_entry_t *fib_entry;
1338 fib_entry_src_t *esrc;
1339
1340 fib_entry = fib_entry_get(entry_index);
1341
1342 esrc = fib_entry_src_find(fib_entry, source, NULL);
1343
1344 if (NULL != esrc)
1345 {
1346 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1347 {
1348 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1349 }
1350 }
1351 return (~0);
1352}
1353
1354fib_entry_flag_t
1355fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1356 fib_source_t source)
1357{
1358 fib_entry_t *fib_entry;
1359 fib_entry_src_t *esrc;
1360
1361 fib_entry = fib_entry_get(entry_index);
1362
1363 esrc = fib_entry_src_find(fib_entry, source, NULL);
1364
1365 if (NULL != esrc)
1366 {
1367 return (esrc->fes_entry_flags);
1368 }
1369
1370 return (FIB_ENTRY_FLAG_NONE);
1371}
1372
Neale Rannsa4e77662017-12-04 20:00:30 +00001373fib_entry_flag_t
1374fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1375{
1376 fib_entry_flag_t flags;
1377
1378 /*
1379 * the vector of sources is deliberately arranged in priority order
1380 */
1381 if (0 == vec_len(fib_entry->fe_srcs))
1382 {
1383 flags = FIB_ENTRY_FLAG_NONE;
1384 }
1385 else
1386 {
1387 fib_entry_src_t *esrc;
1388
1389 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1390 flags = esrc->fes_entry_flags;
1391 }
1392
1393 return (flags);
1394}
1395
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001396void
1397fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1398 fib_source_t source,
1399 const void *data)
1400{
1401 fib_entry_t *fib_entry;
1402 fib_entry_src_t *esrc;
1403
1404 fib_entry = fib_entry_get(fib_entry_index);
1405 esrc = fib_entry_src_find(fib_entry, source, NULL);
1406
1407 if (NULL != esrc &&
1408 NULL != fib_entry_src_vft[source].fesv_set_data)
1409 {
1410 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1411 }
1412}
1413
1414const void*
1415fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1416 fib_source_t source)
1417{
1418 fib_entry_t *fib_entry;
1419 fib_entry_src_t *esrc;
1420
1421 fib_entry = fib_entry_get(fib_entry_index);
1422 esrc = fib_entry_src_find(fib_entry, source, NULL);
1423
1424 if (NULL != esrc &&
1425 NULL != fib_entry_src_vft[source].fesv_get_data)
1426 {
1427 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1428 }
1429 return (NULL);
1430}
1431
1432void
1433fib_entry_src_module_init (void)
1434{
1435 fib_entry_src_rr_register();
1436 fib_entry_src_interface_register();
1437 fib_entry_src_default_route_register();
1438 fib_entry_src_special_register();
1439 fib_entry_src_api_register();
1440 fib_entry_src_adj_register();
1441 fib_entry_src_mpls_register();
1442 fib_entry_src_lisp_register();
1443}