blob: aa1d5a24a0f9e9023cbc8b54e18a3897889d14d1 [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>
20
Neale Ranns3ee44042016-10-03 13:05:48 +010021#include <vnet/fib/fib_entry_src.h>
22#include <vnet/fib/fib_table.h>
23#include <vnet/fib/fib_path_ext.h>
24#include <vnet/fib/fib_urpf_list.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010025
26/*
27 * per-source type vft
28 */
29static fib_entry_src_vft_t fib_entry_src_vft[FIB_SOURCE_MAX];
30
31static fib_protocol_t
32fib_entry_get_proto (const fib_entry_t * fib_entry)
33{
34 return (fib_entry->fe_prefix.fp_proto);
35}
36
37void
38fib_entry_src_register (fib_source_t source,
39 const fib_entry_src_vft_t *vft)
40{
41 fib_entry_src_vft[source] = *vft;
42}
43
44static int
45fib_entry_src_cmp_for_sort (void * v1,
46 void * v2)
47{
48 fib_entry_src_t *esrc1 = v1, *esrc2 = v2;
49
50 return (esrc1->fes_src - esrc2->fes_src);
51}
52
53void
54fib_entry_src_action_init (fib_entry_t *fib_entry,
55 fib_source_t source)
56
57{
58 fib_entry_src_t esrc = {
59 .fes_pl = FIB_NODE_INDEX_INVALID,
60 .fes_flags = FIB_ENTRY_SRC_FLAG_NONE,
61 .fes_src = source,
62 };
63
64 if (NULL != fib_entry_src_vft[source].fesv_init)
65 {
66 fib_entry_src_vft[source].fesv_init(&esrc);
67 }
68
69 vec_add1(fib_entry->fe_srcs, esrc);
70 vec_sort_with_function(fib_entry->fe_srcs,
71 fib_entry_src_cmp_for_sort);
72}
73
74static fib_entry_src_t *
75fib_entry_src_find (const fib_entry_t *fib_entry,
76 fib_source_t source,
77 u32 *index)
78
79{
80 fib_entry_src_t *esrc;
81 int ii;
82
83 ii = 0;
84 vec_foreach(esrc, fib_entry->fe_srcs)
85 {
86 if (esrc->fes_src == source)
87 {
88 if (NULL != index)
89 {
90 *index = ii;
91 }
92 return (esrc);
93 }
94 else
95 {
96 ii++;
97 }
98 }
99
100 return (NULL);
101}
102
103int
104fib_entry_is_sourced (fib_node_index_t fib_entry_index,
105 fib_source_t source)
106{
107 fib_entry_t *fib_entry;
108
109 fib_entry = fib_entry_get(fib_entry_index);
110
111 return (NULL != fib_entry_src_find(fib_entry, source, NULL));
112}
113
114static fib_entry_src_t *
115fib_entry_src_find_or_create (fib_entry_t *fib_entry,
116 fib_source_t source,
117 u32 *index)
118{
119 fib_entry_src_t *esrc;
120
121 esrc = fib_entry_src_find(fib_entry, source, NULL);
122
123 if (NULL == esrc)
124 {
125 fib_entry_src_action_init(fib_entry, source);
126 }
127
128 return (fib_entry_src_find(fib_entry, source, NULL));
129}
130
131void
132fib_entry_src_action_deinit (fib_entry_t *fib_entry,
133 fib_source_t source)
134
135{
136 fib_entry_src_t *esrc;
137 u32 index = ~0;
138
139 esrc = fib_entry_src_find(fib_entry, source, &index);
140
141 ASSERT(NULL != esrc);
142
143 if (NULL != fib_entry_src_vft[source].fesv_deinit)
144 {
145 fib_entry_src_vft[source].fesv_deinit(esrc);
146 }
147
148 vec_free(esrc->fes_path_exts);
149 vec_del1(fib_entry->fe_srcs, index);
150}
151
152fib_entry_src_cover_res_t
153fib_entry_src_action_cover_change (fib_entry_t *fib_entry,
154 fib_source_t source)
155{
156 if (NULL != fib_entry_src_vft[source].fesv_cover_change)
157 {
158 return (fib_entry_src_vft[source].fesv_cover_change(
159 fib_entry_src_find(fib_entry, source, NULL),
160 fib_entry));
161 }
162
163 fib_entry_src_cover_res_t res = {
164 .install = !0,
165 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
166 };
167 return (res);
168}
169
170fib_entry_src_cover_res_t
171fib_entry_src_action_cover_update (fib_entry_t *fib_entry,
172 fib_source_t source)
173{
174 if (NULL != fib_entry_src_vft[source].fesv_cover_update)
175 {
176 return (fib_entry_src_vft[source].fesv_cover_update(
177 fib_entry_src_find(fib_entry, source, NULL),
178 fib_entry));
179 }
180
181 fib_entry_src_cover_res_t res = {
182 .install = !0,
183 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
184 };
185 return (res);
186}
187
188typedef struct fib_entry_src_collect_forwarding_ctx_t_
189{
190 load_balance_path_t * next_hops;
191 const fib_entry_t *fib_entry;
192 const fib_entry_src_t *esrc;
193 fib_forward_chain_type_t fct;
194 int is_recursive;
195} fib_entry_src_collect_forwarding_ctx_t;
196
197/**
198 * @brief Determine whether this FIB entry should use a load-balance MAP
199 * to support PIC edge fast convergence
200 */
201load_balance_flags_t
202fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx)
203{
204 /**
205 * We'll use a LB map is the path-list has recursive paths.
206 * recursive paths implies BGP, and hence scale.
207 */
208 if (ctx->is_recursive)
209 {
210 return (LOAD_BALANCE_FLAG_USES_MAP);
211 }
212 return (LOAD_BALANCE_FLAG_NONE);
213}
214
215static int
216fib_entry_src_valid_out_label (mpls_label_t label)
217{
218 return ((MPLS_LABEL_IS_REAL(label) ||
219 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label ||
220 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label ||
221 MPLS_IETF_IMPLICIT_NULL_LABEL == label));
222}
223
Neale Rannsad422ed2016-11-02 14:20:04 +0000224/**
225 * @brief Turn the chain type requested by the client into the one they
226 * really wanted
227 */
228fib_forward_chain_type_t
229fib_entry_chain_type_fixup (const fib_entry_t *entry,
230 fib_forward_chain_type_t fct)
231{
232 ASSERT(FIB_FORW_CHAIN_TYPE_MPLS_EOS == fct);
233
234 /*
235 * The EOS chain is a tricky since one cannot know the adjacency
236 * to link to without knowing what the packets payload protocol
237 * will be once the label is popped.
238 */
239 fib_forward_chain_type_t dfct;
240
241 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 Ranns0bfe5d82016-08-25 15:29:12 +0100262static int
263fib_entry_src_collect_forwarding (fib_node_index_t pl_index,
264 fib_node_index_t path_index,
265 void *arg)
266{
267 fib_entry_src_collect_forwarding_ctx_t *ctx;
268 fib_path_ext_t *path_ext;
Neale Ranns08a70f12017-02-24 06:16:01 -0800269 int have_path_ext;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270
271 ctx = arg;
272
273 /*
274 * if the path is not resolved, don't include it.
275 */
276 if (!fib_path_is_resolved(path_index))
277 {
278 return (!0);
279 }
280
281 if (fib_path_is_recursive(path_index))
282 {
283 ctx->is_recursive = 1;
284 }
285
286 /*
287 * get the matching path-extension for the path being visited.
288 */
Neale Ranns08a70f12017-02-24 06:16:01 -0800289 have_path_ext = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100290 vec_foreach(path_ext, ctx->esrc->fes_path_exts)
291 {
292 if (path_ext->fpe_path_index == path_index)
Neale Ranns08a70f12017-02-24 06:16:01 -0800293 {
294 have_path_ext = 1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100295 break;
Neale Ranns08a70f12017-02-24 06:16:01 -0800296 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100297 }
298
Neale Ranns08a70f12017-02-24 06:16:01 -0800299 if (have_path_ext &&
Neale Rannsad422ed2016-11-02 14:20:04 +0000300 fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0]))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100301 {
302 /*
303 * found a matching extension. stack it to obtain the forwarding
304 * info for this path.
305 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000306 ctx->next_hops = fib_path_ext_stack(path_ext, ctx->fib_entry, ctx->fct, ctx->next_hops);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100307 }
308 else
309 {
310 load_balance_path_t *nh;
311
312 /*
313 * no extension => no out-going label for this path. that's OK
314 * in the case of an IP or EOS chain, but not for non-EOS
315 */
316 switch (ctx->fct)
317 {
318 case FIB_FORW_CHAIN_TYPE_UNICAST_IP4:
319 case FIB_FORW_CHAIN_TYPE_UNICAST_IP6:
Neale Ranns32e1c012016-11-22 17:07:28 +0000320 case FIB_FORW_CHAIN_TYPE_MCAST_IP4:
321 case FIB_FORW_CHAIN_TYPE_MCAST_IP6:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100322 /*
323 * EOS traffic with no label to stack, we need the IP Adj
324 */
325 vec_add2(ctx->next_hops, nh, 1);
326
327 nh->path_index = path_index;
328 nh->path_weight = fib_path_get_weight(path_index);
329 fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo);
330
331 break;
332 case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS:
333 if (fib_path_is_exclusive(path_index) ||
334 fib_path_is_deag(path_index))
335 {
336 vec_add2(ctx->next_hops, nh, 1);
337
338 nh->path_index = path_index;
339 nh->path_weight = fib_path_get_weight(path_index);
340 fib_path_contribute_forwarding(path_index,
341 FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS,
342 &nh->path_dpo);
343 }
344 break;
345 case FIB_FORW_CHAIN_TYPE_MPLS_EOS:
Neale Rannsad422ed2016-11-02 14:20:04 +0000346 {
347 /*
348 * no label. we need a chain based on the payload. fixup.
349 */
350 vec_add2(ctx->next_hops, nh, 1);
351
352 nh->path_index = path_index;
353 nh->path_weight = fib_path_get_weight(path_index);
354 fib_path_contribute_forwarding(path_index,
355 fib_entry_chain_type_fixup(ctx->fib_entry,
356 ctx->fct),
357 &nh->path_dpo);
358
359 break;
360 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100361 case FIB_FORW_CHAIN_TYPE_ETHERNET:
Florin Corasce1b4c72017-01-26 14:25:34 -0800362 case FIB_FORW_CHAIN_TYPE_NSH:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100363 ASSERT(0);
364 break;
365 }
366 }
367
368 return (!0);
369}
370
371void
372fib_entry_src_mk_lb (fib_entry_t *fib_entry,
373 const fib_entry_src_t *esrc,
374 fib_forward_chain_type_t fct,
375 dpo_id_t *dpo_lb)
376{
377 dpo_proto_t lb_proto;
378
379 /*
380 * If the entry has path extensions then we construct a load-balance
381 * by stacking the extensions on the forwarding chains of the paths.
382 * Otherwise we use the load-balance of the path-list
383 */
384 fib_entry_src_collect_forwarding_ctx_t ctx = {
385 .esrc = esrc,
386 .fib_entry = fib_entry,
387 .next_hops = NULL,
388 .is_recursive = 0,
389 .fct = fct,
390 };
391
Neale Rannsc0790cf2017-01-05 01:01:47 -0800392 /*
393 * As an optimisation we allocate the vector of next-hops to be sized
394 * equal to the maximum nuber of paths we will need, which is also the
395 * most likely number we will need, since in most cases the paths are 'up'.
396 */
397 vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl));
398 vec_reset_length(ctx.next_hops);
399
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000400 lb_proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100401
402 fib_path_list_walk(esrc->fes_pl,
403 fib_entry_src_collect_forwarding,
404 &ctx);
405
406 if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE)
407 {
408 /*
409 * the client provided the DPO that the entry should link to.
410 * all entries must link to a LB, so if it is an LB already
411 * then we can use it.
412 */
413 if ((1 == vec_len(ctx.next_hops)) &&
414 (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type))
415 {
416 dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo);
417 dpo_reset(&ctx.next_hops[0].path_dpo);
418 return;
419 }
420 }
421
422 if (!dpo_id_is_valid(dpo_lb))
423 {
424 /*
425 * first time create
426 */
427 flow_hash_config_t fhc;
428
429 fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index,
430 dpo_proto_to_fib(lb_proto));
431 dpo_set(dpo_lb,
432 DPO_LOAD_BALANCE,
433 lb_proto,
434 load_balance_create(0, lb_proto, fhc));
435 }
436
437 load_balance_multipath_update(dpo_lb,
438 ctx.next_hops,
439 fib_entry_calc_lb_flags(&ctx));
Neale Ranns33a7dd52016-10-07 15:14:33 +0100440 vec_free(ctx.next_hops);
Neale Ranns3ee44042016-10-03 13:05:48 +0100441
442 /*
443 * if this entry is sourced by the uRPF-exempt source then we
444 * append the always present local0 interface (index 0) to the
445 * uRPF list so it is not empty. that way packets pass the loose check.
446 */
447 index_t ui = fib_path_list_get_urpf(esrc->fes_pl);
448
Shwetha Bhandari78372a92017-01-18 12:43:54 +0530449 if ((fib_entry_is_sourced(fib_entry_get_index(fib_entry),
450 FIB_SOURCE_URPF_EXEMPT) ||
451 (esrc->fes_entry_flags & FIB_ENTRY_FLAG_LOOSE_URPF_EXEMPT))&&
Neale Ranns3ee44042016-10-03 13:05:48 +0100452 (0 == fib_urpf_check_size(ui)))
453 {
454 /*
455 * The uRPF list we get from the path-list is shared by all
456 * other users of the list, but the uRPF exemption applies
457 * only to this prefix. So we need our own list.
458 */
459 ui = fib_urpf_list_alloc_and_lock();
460 fib_urpf_list_append(ui, 0);
461 fib_urpf_list_bake(ui);
462 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
463 fib_urpf_list_unlock(ui);
464 }
465 else
466 {
467 load_balance_set_urpf(dpo_lb->dpoi_index, ui);
468 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000469 load_balance_set_fib_entry_flags(dpo_lb->dpoi_index,
470 fib_entry_get_flags_i(fib_entry));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100471}
472
473void
474fib_entry_src_action_install (fib_entry_t *fib_entry,
475 fib_source_t source)
476{
477 /*
478 * Install the forwarding chain for the given source into the forwarding
479 * tables
480 */
481 fib_forward_chain_type_t fct;
482 fib_entry_src_t *esrc;
Neale Ranns33a7dd52016-10-07 15:14:33 +0100483 int insert;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100484
485 fct = fib_entry_get_default_chain_type(fib_entry);
486 esrc = fib_entry_src_find(fib_entry, source, NULL);
487
Neale Ranns33a7dd52016-10-07 15:14:33 +0100488 /*
489 * Every entry has its own load-balance object. All changes to the entry's
490 * forwarding result in an inplace modify of the load-balance. This means
491 * the load-balance object only needs to be added to the forwarding
492 * DB once, when it is created.
493 */
Neale Rannsad422ed2016-11-02 14:20:04 +0000494 insert = !dpo_id_is_valid(&fib_entry->fe_lb);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100495
Neale Rannsad422ed2016-11-02 14:20:04 +0000496 fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100497
Neale Rannsad422ed2016-11-02 14:20:04 +0000498 ASSERT(dpo_id_is_valid(&fib_entry->fe_lb));
499 FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100500
501 /*
502 * insert the adj into the data-plane forwarding trie
503 */
Neale Ranns33a7dd52016-10-07 15:14:33 +0100504 if (insert)
505 {
506 fib_table_fwding_dpo_update(fib_entry->fe_fib_index,
507 &fib_entry->fe_prefix,
Neale Rannsad422ed2016-11-02 14:20:04 +0000508 &fib_entry->fe_lb);
Neale Ranns33a7dd52016-10-07 15:14:33 +0100509 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100510
Neale Rannsad422ed2016-11-02 14:20:04 +0000511 /*
512 * if any of the other chain types are already created they will need
513 * updating too
514 */
515 fib_entry_delegate_type_t fdt;
516 fib_entry_delegate_t *fed;
517
518 FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100519 {
Neale Rannsad422ed2016-11-02 14:20:04 +0000520 fib_entry_src_mk_lb(fib_entry, esrc,
521 fib_entry_delegate_type_to_chain_type(fdt),
522 &fed->fd_dpo);
523 });
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100524}
525
526void
527fib_entry_src_action_uninstall (fib_entry_t *fib_entry)
528{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100529 /*
Neale Ranns3ee44042016-10-03 13:05:48 +0100530 * uninstall the forwarding chain from the forwarding tables
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100531 */
532 FIB_ENTRY_DBG(fib_entry, "uninstall: %d",
533 fib_entry->fe_adj_index);
534
Neale Rannsad422ed2016-11-02 14:20:04 +0000535 if (dpo_id_is_valid(&fib_entry->fe_lb))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100536 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100537 fib_table_fwding_dpo_remove(
538 fib_entry->fe_fib_index,
539 &fib_entry->fe_prefix,
Neale Rannsad422ed2016-11-02 14:20:04 +0000540 &fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100541
Neale Rannsad422ed2016-11-02 14:20:04 +0000542 dpo_reset(&fib_entry->fe_lb);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100543 }
544}
545
546static void
547fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index)
548{
549 fib_node_index_t *entries = NULL;
550
551 fib_path_list_recursive_loop_detect(path_list_index, &entries);
552
553 vec_free(entries);
554}
555
556void
557fib_entry_src_action_activate (fib_entry_t *fib_entry,
558 fib_source_t source)
559
560{
561 int houston_we_are_go_for_install;
562 fib_entry_src_t *esrc;
563
564 esrc = fib_entry_src_find(fib_entry, source, NULL);
565
566 ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE));
567 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
568
569 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE;
570
571 if (NULL != fib_entry_src_vft[source].fesv_activate)
572 {
573 houston_we_are_go_for_install =
574 fib_entry_src_vft[source].fesv_activate(esrc, fib_entry);
575 }
576 else
577 {
578 /*
579 * the source is not providing an activate function, we'll assume
580 * therefore it has no objection to installing the entry
581 */
582 houston_we_are_go_for_install = !0;
583 }
584
585 /*
586 * link to the path-list provided by the source, and go check
587 * if that forms any loops in the graph.
588 */
589 fib_entry->fe_parent = esrc->fes_pl;
590 fib_entry->fe_sibling =
591 fib_path_list_child_add(fib_entry->fe_parent,
592 FIB_NODE_TYPE_ENTRY,
593 fib_entry_get_index(fib_entry));
594
595 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
596
597 FIB_ENTRY_DBG(fib_entry, "activate: %d",
598 fib_entry->fe_parent);
599
600 if (0 != houston_we_are_go_for_install)
601 {
602 fib_entry_src_action_install(fib_entry, source);
603 }
604 else
605 {
606 fib_entry_src_action_uninstall(fib_entry);
607 }
608}
609
610void
611fib_entry_src_action_deactivate (fib_entry_t *fib_entry,
612 fib_source_t source)
613
614{
615 fib_node_index_t path_list_index;
616 fib_entry_src_t *esrc;
617
618 esrc = fib_entry_src_find(fib_entry, source, NULL);
619
620 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
621
622 if (NULL != fib_entry_src_vft[source].fesv_deactivate)
623 {
624 fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry);
625 }
626
627 esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE;
628
629 FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent);
630
631 /*
632 * un-link from an old path-list. Check for any loops this will clear
633 */
634 path_list_index = fib_entry->fe_parent;
635 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
636
637 fib_entry_recursive_loop_detect_i(path_list_index);
638
639 /*
640 * this will unlock the path-list, so it may be invalid thereafter.
641 */
642 fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling);
643 fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID;
644}
645
646static void
647fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry,
648 fib_source_t source)
649{
650 fib_entry_src_t *esrc;
651
652 vec_foreach(esrc, fib_entry->fe_srcs)
653 {
654 if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update)
655 {
656 fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc,
657 fib_entry,
658 source);
659 }
660 }
661}
662
663void
664fib_entry_src_action_reactivate (fib_entry_t *fib_entry,
665 fib_source_t source)
666{
667 fib_node_index_t path_list_index;
668 fib_entry_src_t *esrc;
669
670 esrc = fib_entry_src_find(fib_entry, source, NULL);
671
672 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE);
673
674 FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d",
675 fib_entry->fe_parent,
676 esrc->fes_pl);
677
678 if (fib_entry->fe_parent != esrc->fes_pl)
679 {
680 /*
681 * un-link from an old path-list. Check for any loops this will clear
682 */
683 path_list_index = fib_entry->fe_parent;
684 fib_entry->fe_parent = FIB_NODE_INDEX_INVALID;
685
686 /*
687 * temporary lock so it doesn't get deleted when this entry is no
688 * longer a child.
689 */
690 fib_path_list_lock(path_list_index);
691
692 /*
693 * this entry is no longer a child. after unlinking check if any loops
694 * were broken
695 */
696 fib_path_list_child_remove(path_list_index,
697 fib_entry->fe_sibling);
698
699 fib_entry_recursive_loop_detect_i(path_list_index);
700
701 /*
702 * link to the path-list provided by the source, and go check
703 * if that forms any loops in the graph.
704 */
705 fib_entry->fe_parent = esrc->fes_pl;
706 fib_entry->fe_sibling =
707 fib_path_list_child_add(fib_entry->fe_parent,
708 FIB_NODE_TYPE_ENTRY,
709 fib_entry_get_index(fib_entry));
710
711 fib_entry_recursive_loop_detect_i(fib_entry->fe_parent);
712 fib_path_list_unlock(path_list_index);
713 }
714 fib_entry_src_action_install(fib_entry, source);
715 fib_entry_src_action_fwd_update(fib_entry, source);
716}
717
718void
719fib_entry_src_action_installed (const fib_entry_t *fib_entry,
720 fib_source_t source)
721{
722 fib_entry_src_t *esrc;
723
724 esrc = fib_entry_src_find(fib_entry, source, NULL);
725
726 if (NULL != fib_entry_src_vft[source].fesv_installed)
727 {
728 fib_entry_src_vft[source].fesv_installed(esrc,
729 fib_entry);
730 }
731
732 fib_entry_src_action_fwd_update(fib_entry, source);
733}
734
735/*
736 * fib_entry_src_action_add
737 *
738 * Adding a source can result in a new fib_entry being created, which
739 * can inturn mean the pool is realloc'd and thus the entry passed as
740 * an argument it also realloc'd
741 * @return the original entry
742 */
743fib_entry_t *
744fib_entry_src_action_add (fib_entry_t *fib_entry,
745 fib_source_t source,
746 fib_entry_flag_t flags,
747 const dpo_id_t *dpo)
748{
749 fib_node_index_t fib_entry_index;
750 fib_entry_src_t *esrc;
751
752 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
753
754 esrc->fes_ref_count++;
755
756 if (1 != esrc->fes_ref_count)
757 {
758 /*
759 * we only want to add the source on the 0->1 transition
760 */
761 return (fib_entry);
762 }
763
764 esrc->fes_entry_flags = flags;
765
766 /*
767 * save variable so we can recover from a fib_entry realloc.
768 */
769 fib_entry_index = fib_entry_get_index(fib_entry);
770
771 if (NULL != fib_entry_src_vft[source].fesv_add)
772 {
773 fib_entry_src_vft[source].fesv_add(esrc,
774 fib_entry,
775 flags,
776 fib_entry_get_proto(fib_entry),
777 dpo);
778 }
779
780 fib_entry = fib_entry_get(fib_entry_index);
781
782 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
783
784 fib_path_list_lock(esrc->fes_pl);
785
786 /*
787 * the source owns a lock on the entry
788 */
789 fib_entry_lock(fib_entry_get_index(fib_entry));
790
791 return (fib_entry);
792}
793
Neale Ranns948e00f2016-10-20 13:39:34 +0100794/*
795 * fib_entry_src_action_update
796 *
797 * Adding a source can result in a new fib_entry being created, which
798 * can inturn mean the pool is realloc'd and thus the entry passed as
799 * an argument it also realloc'd
800 * @return the original entry
801 */
802fib_entry_t *
803fib_entry_src_action_update (fib_entry_t *fib_entry,
804 fib_source_t source,
805 fib_entry_flag_t flags,
806 const dpo_id_t *dpo)
807{
808 fib_node_index_t fib_entry_index, old_path_list_index;
809 fib_entry_src_t *esrc;
810
811 esrc = fib_entry_src_find_or_create(fib_entry, source, NULL);
812
813 if (NULL == esrc)
814 return (fib_entry_src_action_add(fib_entry, source, flags, dpo));
815
816 old_path_list_index = esrc->fes_pl;
817 esrc->fes_entry_flags = flags;
818
819 /*
820 * save variable so we can recover from a fib_entry realloc.
821 */
822 fib_entry_index = fib_entry_get_index(fib_entry);
823
824 if (NULL != fib_entry_src_vft[source].fesv_add)
825 {
826 fib_entry_src_vft[source].fesv_add(esrc,
827 fib_entry,
828 flags,
829 fib_entry_get_proto(fib_entry),
830 dpo);
831 }
832
833 fib_entry = fib_entry_get(fib_entry_index);
834
835 esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED;
836
837 fib_path_list_lock(esrc->fes_pl);
838 fib_path_list_unlock(old_path_list_index);
839
840 return (fib_entry);
841}
842
843
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100844fib_entry_src_flag_t
845fib_entry_src_action_remove (fib_entry_t *fib_entry,
846 fib_source_t source)
847
848{
849 fib_node_index_t old_path_list;
850 fib_entry_src_flag_t sflags;
851 fib_entry_src_t *esrc;
852
853 esrc = fib_entry_src_find(fib_entry, source, NULL);
854
855 if (NULL == esrc)
856 return (FIB_ENTRY_SRC_FLAG_ACTIVE);
857
858 esrc->fes_ref_count--;
859 sflags = esrc->fes_flags;
860
861 if (0 != esrc->fes_ref_count)
862 {
863 /*
864 * only remove the source on the 1->0 transisition
865 */
866 return (sflags);
867 }
868
869 if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)
870 {
871 fib_entry_src_action_deactivate(fib_entry, source);
872 }
873
874 old_path_list = esrc->fes_pl;
875
876 if (NULL != fib_entry_src_vft[source].fesv_remove)
877 {
878 fib_entry_src_vft[source].fesv_remove(esrc);
879 }
880
881 fib_path_list_unlock(old_path_list);
882 fib_entry_unlock(fib_entry_get_index(fib_entry));
883
884 sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED;
885 fib_entry_src_action_deinit(fib_entry, source);
886
887 return (sflags);
888}
889
890static inline int
891fib_route_recurses_via_self (const fib_prefix_t *prefix,
892 const fib_route_path_t *rpath)
893{
894 /*
895 * not all zeros next hop &&
896 * is recursive path &&
897 * nexthop is same as the route's address
898 */
899 return ((!ip46_address_is_zero(&rpath->frp_addr)) &&
900 (~0 == rpath->frp_sw_if_index) &&
901 (0 == ip46_address_cmp(&rpath->frp_addr, &prefix->fp_addr)));
902
903}
904
905/*
906 * fib_route_attached_cross_table
907 *
908 * Return true the the route is attached via an interface that
909 * is not in the same table as the route
910 */
911static inline int
912fib_route_attached_cross_table (const fib_entry_t *fib_entry,
913 const fib_route_path_t *rpath)
914{
915 /*
916 * - All zeros next-hop
917 * - a valid interface
918 * - entry's fib index not equeal to interface's index
919 */
920 if (ip46_address_is_zero(&rpath->frp_addr) &&
921 (~0 != rpath->frp_sw_if_index) &&
922 (fib_entry->fe_fib_index !=
923 fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry),
924 rpath->frp_sw_if_index)))
925 {
926 return (!0);
927 }
928 return (0);
929}
930
931/*
932 * fib_route_attached_cross_table
933 *
934 * Return true the the route is attached via an interface that
935 * is not in the same table as the route
936 */
937static inline int
938fib_path_is_attached (const fib_route_path_t *rpath)
939{
940 /*
941 * - All zeros next-hop
942 * - a valid interface
943 */
944 if (ip46_address_is_zero(&rpath->frp_addr) &&
945 (~0 != rpath->frp_sw_if_index))
946 {
947 return (!0);
948 }
Neale Ranns4b919a52017-03-11 05:55:21 -0800949 else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED)
950 {
951 return (!0);
952 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100953 return (0);
954}
955
956fib_path_list_flags_t
957fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags)
958{
959 fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE;
960
961 if (eflags & FIB_ENTRY_FLAG_DROP)
962 {
963 plf |= FIB_PATH_LIST_FLAG_DROP;
964 }
965 if (eflags & FIB_ENTRY_FLAG_LOCAL)
966 {
967 plf |= FIB_PATH_LIST_FLAG_LOCAL;
968 }
969 if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE)
970 {
971 plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE;
972 }
973
974 return (plf);
975}
976
977static void
978fib_entry_flags_update (const fib_entry_t *fib_entry,
979 const fib_route_path_t *rpath,
980 fib_path_list_flags_t *pl_flags,
981 fib_entry_src_t *esrc)
982{
983 /*
984 * don't allow the addition of a recursive looped path for prefix
985 * via itself.
986 */
987 if (fib_route_recurses_via_self(&fib_entry->fe_prefix, rpath))
988 {
989 /*
990 * force the install of a drop path-list.
991 * we want the entry to have some path-list, mainly so
992 * the dodgy path can be rmeoved when the source stops playing
993 * silly buggers.
994 */
995 *pl_flags |= FIB_PATH_LIST_FLAG_DROP;
996 }
997 else
998 {
999 *pl_flags &= ~FIB_PATH_LIST_FLAG_DROP;
1000 }
1001
1002 if ((esrc->fes_src == FIB_SOURCE_API) ||
1003 (esrc->fes_src == FIB_SOURCE_CLI))
1004 {
1005 if (fib_path_is_attached(rpath))
1006 {
1007 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED;
1008 }
1009 else
1010 {
1011 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED;
1012 }
1013 }
1014 if (fib_route_attached_cross_table(fib_entry, rpath))
1015 {
1016 esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT;
1017 }
1018 else
1019 {
1020 esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT;
1021 }
1022}
1023
1024/*
1025 * fib_entry_src_path_ext_add
1026 *
1027 * append a path extension to the entry's list
1028 */
1029static void
1030fib_entry_src_path_ext_append (fib_entry_src_t *esrc,
1031 const fib_route_path_t *rpath)
1032{
Neale Rannsad422ed2016-11-02 14:20:04 +00001033 if (NULL != rpath->frp_label_stack)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001034 {
1035 fib_path_ext_t *path_ext;
1036
1037 vec_add2(esrc->fes_path_exts, path_ext, 1);
1038
1039 fib_path_ext_init(path_ext, esrc->fes_pl, rpath);
1040 }
1041}
1042
1043/*
1044 * fib_entry_src_path_ext_insert
1045 *
1046 * insert, sorted, a path extension to the entry's list.
1047 * It's not strictly necessary in sort the path extensions, since each
1048 * extension has the path index to which it resolves. However, by being
1049 * sorted the load-balance produced has a deterministic order, not an order
1050 * based on the sequence of extension additions. this is a considerable benefit.
1051 */
1052static void
1053fib_entry_src_path_ext_insert (fib_entry_src_t *esrc,
1054 const fib_route_path_t *rpath)
1055{
1056 if (0 == vec_len(esrc->fes_path_exts))
1057 return (fib_entry_src_path_ext_append(esrc, rpath));
1058
Neale Rannsad422ed2016-11-02 14:20:04 +00001059 if (NULL != rpath->frp_label_stack)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001060 {
1061 fib_path_ext_t path_ext;
1062 int i = 0;
1063
1064 fib_path_ext_init(&path_ext, esrc->fes_pl, rpath);
1065
1066 while (i < vec_len(esrc->fes_path_exts) &&
1067 (fib_path_ext_cmp(&esrc->fes_path_exts[i], rpath) < 0))
1068 {
1069 i++;
1070 }
1071
1072 vec_insert_elts(esrc->fes_path_exts, &path_ext, 1, i);
1073 }
1074}
1075
1076/*
1077 * fib_entry_src_action_add
1078 *
1079 * Adding a source can result in a new fib_entry being created, which
1080 * can inturn mean the pool is realloc'd and thus the entry passed as
1081 * an argument it also realloc'd
1082 * @return the entry
1083 */
1084fib_entry_t*
1085fib_entry_src_action_path_add (fib_entry_t *fib_entry,
1086 fib_source_t source,
1087 fib_entry_flag_t flags,
1088 const fib_route_path_t *rpath)
1089{
1090 fib_node_index_t old_path_list, fib_entry_index;
1091 fib_path_list_flags_t pl_flags;
1092 fib_path_ext_t *path_ext;
1093 fib_entry_src_t *esrc;
1094
1095 /*
1096 * save variable so we can recover from a fib_entry realloc.
1097 */
1098 fib_entry_index = fib_entry_get_index(fib_entry);
1099
1100 esrc = fib_entry_src_find(fib_entry, source, NULL);
1101 if (NULL == esrc)
1102 {
1103 fib_entry =
1104 fib_entry_src_action_add(fib_entry,
1105 source,
1106 flags,
1107 drop_dpo_get(
1108 fib_proto_to_dpo(
1109 fib_entry_get_proto(fib_entry))));
1110 esrc = fib_entry_src_find(fib_entry, source, NULL);
1111 }
1112
1113 /*
1114 * we are no doubt modifying a path-list. If the path-list
1115 * is shared, and hence not modifiable, then the index returned
1116 * will be for a different path-list. This FIB entry to needs
1117 * to maintain its lock appropriately.
1118 */
1119 old_path_list = esrc->fes_pl;
1120
1121 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add);
1122
1123 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1124 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1125
1126 fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath);
1127 fib_entry = fib_entry_get(fib_entry_index);
1128
1129 /*
1130 * re-resolve all the path-extensions with the new path-list
1131 */
1132 vec_foreach(path_ext, esrc->fes_path_exts)
1133 {
1134 fib_path_ext_resolve(path_ext, esrc->fes_pl);
1135 }
1136 /*
1137 * if the path has a label we need to add a path extension
1138 */
1139 fib_entry_src_path_ext_insert(esrc, rpath);
1140
1141 fib_path_list_lock(esrc->fes_pl);
1142 fib_path_list_unlock(old_path_list);
1143
1144 return (fib_entry);
1145}
1146
1147/*
1148 * fib_entry_src_action_swap
1149 *
1150 * The source is providing new paths to replace the old ones.
1151 * Adding a source can result in a new fib_entry being created, which
1152 * can inturn mean the pool is realloc'd and thus the entry passed as
1153 * an argument it also realloc'd
1154 * @return the entry
1155 */
1156fib_entry_t*
1157fib_entry_src_action_path_swap (fib_entry_t *fib_entry,
1158 fib_source_t source,
1159 fib_entry_flag_t flags,
1160 const fib_route_path_t *rpaths)
1161{
1162 fib_node_index_t old_path_list, fib_entry_index;
1163 fib_path_list_flags_t pl_flags;
1164 const fib_route_path_t *rpath;
Neale Rannsad422ed2016-11-02 14:20:04 +00001165 fib_path_ext_t *path_ext;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001166 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(
1181 fib_proto_to_dpo(
1182 fib_entry_get_proto(fib_entry))));
1183 esrc = fib_entry_src_find(fib_entry, source, NULL);
1184 }
1185
1186 /*
1187 * swapping paths may create a new path-list (or may use an existing shared)
1188 * but we are certainly getting a different one. This FIB entry to needs
1189 * to maintain its lock appropriately.
1190 */
1191 old_path_list = esrc->fes_pl;
1192
1193 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap);
1194
Neale Rannsdf089a82016-10-02 16:39:06 +01001195 pl_flags = fib_entry_src_flags_2_path_list_flags(flags);
1196
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001197 vec_foreach(rpath, rpaths)
1198 {
1199 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1200 }
1201
1202 fib_entry_src_vft[source].fesv_path_swap(esrc,
1203 fib_entry,
1204 pl_flags,
1205 rpaths);
1206
Neale Rannsad422ed2016-11-02 14:20:04 +00001207 vec_foreach(path_ext, esrc->fes_path_exts)
1208 {
1209 vec_free(path_ext->fpe_label_stack);
1210 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001211 vec_free(esrc->fes_path_exts);
Neale Rannsad422ed2016-11-02 14:20:04 +00001212
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001213 vec_foreach(rpath, rpaths)
1214 {
1215 fib_entry_src_path_ext_append(esrc, rpath);
1216 }
1217
1218 fib_entry = fib_entry_get(fib_entry_index);
1219
1220 fib_path_list_lock(esrc->fes_pl);
1221 fib_path_list_unlock(old_path_list);
1222
1223 return (fib_entry);
1224}
1225
1226fib_entry_src_flag_t
1227fib_entry_src_action_path_remove (fib_entry_t *fib_entry,
1228 fib_source_t source,
1229 const fib_route_path_t *rpath)
1230{
1231 fib_path_list_flags_t pl_flags;
1232 fib_node_index_t old_path_list;
1233 fib_path_ext_t *path_ext;
1234 fib_entry_src_t *esrc;
1235
1236 esrc = fib_entry_src_find(fib_entry, source, NULL);
1237
1238 ASSERT(NULL != esrc);
1239 ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED);
1240
1241 /*
1242 * we no doubt modifying a path-list. If the path-list
1243 * is shared, and hence not modifiable, then the index returned
1244 * will be for a different path-list. This FIB entry to needs
1245 * to maintain its lock appropriately.
1246 */
1247 old_path_list = esrc->fes_pl;
1248
1249 ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove);
1250
1251 pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry));
1252 fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc);
1253
1254 fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath);
1255 /*
1256 * find the matching path extension and remove it
1257 */
1258 vec_foreach(path_ext, esrc->fes_path_exts)
1259 {
1260 if (!fib_path_ext_cmp(path_ext, rpath))
1261 {
1262 /*
1263 * delete the element moving the remaining elements down 1 position.
1264 * this preserves the sorted order.
1265 */
Neale Rannsad422ed2016-11-02 14:20:04 +00001266 vec_free(path_ext->fpe_label_stack);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001267 vec_delete(esrc->fes_path_exts, 1, (path_ext - esrc->fes_path_exts));
1268 break;
1269 }
1270 }
1271 /*
1272 * re-resolve all the path-extensions with the new path-list
1273 */
1274 vec_foreach(path_ext, esrc->fes_path_exts)
1275 {
1276 fib_path_ext_resolve(path_ext, esrc->fes_pl);
1277 }
1278
1279 /*
1280 * lock the new path-list, unlock the old if it had one
1281 */
1282 fib_path_list_unlock(old_path_list);
1283
1284 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) {
1285 fib_path_list_lock(esrc->fes_pl);
1286 return (FIB_ENTRY_SRC_FLAG_ADDED);
1287 }
1288 else
1289 {
1290 /*
1291 * no more paths left from this source
1292 */
1293 fib_entry_src_action_remove(fib_entry, source);
1294 return (FIB_ENTRY_SRC_FLAG_NONE);
1295 }
1296}
1297
1298u8*
1299fib_entry_src_format (fib_entry_t *fib_entry,
1300 fib_source_t source,
1301 u8* s)
1302{
1303 fib_entry_src_t *esrc;
1304
1305 esrc = fib_entry_src_find(fib_entry, source, NULL);
1306
1307 if (NULL != fib_entry_src_vft[source].fesv_format)
1308 {
1309 return (fib_entry_src_vft[source].fesv_format(esrc, s));
1310 }
1311 return (s);
1312}
1313
1314adj_index_t
1315fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index,
1316 fib_source_t source)
1317{
1318 fib_entry_t *fib_entry;
1319 fib_entry_src_t *esrc;
1320
1321 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1322 return (ADJ_INDEX_INVALID);
1323
1324 fib_entry = fib_entry_get(fib_entry_index);
1325 esrc = fib_entry_src_find(fib_entry, source, NULL);
1326
1327 if (NULL != esrc)
1328 {
1329 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1330 {
1331 return (fib_path_list_get_adj(
1332 esrc->fes_pl,
1333 fib_entry_get_default_chain_type(fib_entry)));
1334 }
1335 }
1336 return (ADJ_INDEX_INVALID);
1337}
1338
1339const int
1340fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index,
1341 fib_source_t source,
1342 dpo_id_t *dpo)
1343{
1344 fib_entry_t *fib_entry;
1345 fib_entry_src_t *esrc;
1346
1347 if (FIB_NODE_INDEX_INVALID == fib_entry_index)
1348 return (0);
1349
1350 fib_entry = fib_entry_get(fib_entry_index);
1351 esrc = fib_entry_src_find(fib_entry, source, NULL);
1352
1353 if (NULL != esrc)
1354 {
1355 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1356 {
1357 fib_path_list_contribute_forwarding(
1358 esrc->fes_pl,
1359 fib_entry_get_default_chain_type(fib_entry),
1360 dpo);
1361
1362 return (dpo_id_is_valid(dpo));
1363 }
1364 }
1365 return (0);
1366}
1367
Neale Rannsdf089a82016-10-02 16:39:06 +01001368u32
1369fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index,
1370 fib_source_t source)
1371{
1372 fib_entry_t *fib_entry;
1373 fib_entry_src_t *esrc;
1374
1375 fib_entry = fib_entry_get(entry_index);
1376
1377 esrc = fib_entry_src_find(fib_entry, source, NULL);
1378
1379 if (NULL != esrc)
1380 {
1381 if (FIB_NODE_INDEX_INVALID != esrc->fes_pl)
1382 {
1383 return (fib_path_list_get_resolving_interface(esrc->fes_pl));
1384 }
1385 }
1386 return (~0);
1387}
1388
1389fib_entry_flag_t
1390fib_entry_get_flags_for_source (fib_node_index_t entry_index,
1391 fib_source_t source)
1392{
1393 fib_entry_t *fib_entry;
1394 fib_entry_src_t *esrc;
1395
1396 fib_entry = fib_entry_get(entry_index);
1397
1398 esrc = fib_entry_src_find(fib_entry, source, NULL);
1399
1400 if (NULL != esrc)
1401 {
1402 return (esrc->fes_entry_flags);
1403 }
1404
1405 return (FIB_ENTRY_FLAG_NONE);
1406}
1407
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001408fib_entry_flag_t
1409fib_entry_get_flags_i (const fib_entry_t *fib_entry)
1410{
1411 fib_entry_flag_t flags;
1412
1413 /*
1414 * the vector of sources is deliberately arranged in priority order
1415 */
1416 if (0 == vec_len(fib_entry->fe_srcs))
1417 {
1418 flags = FIB_ENTRY_FLAG_NONE;
1419 }
1420 else
1421 {
1422 fib_entry_src_t *esrc;
1423
1424 esrc = vec_elt_at_index(fib_entry->fe_srcs, 0);
1425 flags = esrc->fes_entry_flags;
1426 }
1427
1428 return (flags);
1429}
1430
1431void
1432fib_entry_set_source_data (fib_node_index_t fib_entry_index,
1433 fib_source_t source,
1434 const void *data)
1435{
1436 fib_entry_t *fib_entry;
1437 fib_entry_src_t *esrc;
1438
1439 fib_entry = fib_entry_get(fib_entry_index);
1440 esrc = fib_entry_src_find(fib_entry, source, NULL);
1441
1442 if (NULL != esrc &&
1443 NULL != fib_entry_src_vft[source].fesv_set_data)
1444 {
1445 fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data);
1446 }
1447}
1448
1449const void*
1450fib_entry_get_source_data (fib_node_index_t fib_entry_index,
1451 fib_source_t source)
1452{
1453 fib_entry_t *fib_entry;
1454 fib_entry_src_t *esrc;
1455
1456 fib_entry = fib_entry_get(fib_entry_index);
1457 esrc = fib_entry_src_find(fib_entry, source, NULL);
1458
1459 if (NULL != esrc &&
1460 NULL != fib_entry_src_vft[source].fesv_get_data)
1461 {
1462 return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry));
1463 }
1464 return (NULL);
1465}
1466
1467void
1468fib_entry_src_module_init (void)
1469{
1470 fib_entry_src_rr_register();
1471 fib_entry_src_interface_register();
1472 fib_entry_src_default_route_register();
1473 fib_entry_src_special_register();
1474 fib_entry_src_api_register();
1475 fib_entry_src_adj_register();
1476 fib_entry_src_mpls_register();
1477 fib_entry_src_lisp_register();
1478}