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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vnet/ip/format.h>
18 #include <vnet/ip/lookup.h>
19 #include <vnet/adj/adj.h>
20 #include <vnet/dpo/drop_dpo.h>
22 #include "fib_entry_src.h"
23 #include "fib_entry_src_rr.h"
24 #include "fib_entry_cover.h"
25 #include "fib_entry.h"
26 #include "fib_table.h"
29 * Source initialisation Function
32 fib_entry_src_interpose_init (fib_entry_src_t *src)
34 src->u.interpose.fesi_cover = FIB_NODE_INDEX_INVALID;
35 src->u.interpose.fesi_sibling = FIB_NODE_INDEX_INVALID;
39 * Source deinitialisation Function
42 fib_entry_src_interpose_deinit (fib_entry_src_t *src)
44 ASSERT(src->u.interpose.fesi_cover == FIB_NODE_INDEX_INVALID);
46 src->u.interpose.fesi_cover = FIB_NODE_INDEX_INVALID;
47 src->u.interpose.fesi_sibling = FIB_NODE_INDEX_INVALID;
49 dpo_reset(&src->u.interpose.fesi_dpo);
52 static fib_entry_src_t *
53 fib_entry_src_rr_get_next_best (const fib_entry_src_t *src,
54 const fib_entry_t *fib_entry)
56 fib_entry_src_t *next_src, *best_src = NULL;
59 FOR_EACH_SRC_ADDED(fib_entry, next_src, source,
62 * skip to the next best source after this one
64 switch (fib_source_cmp(source, src->fes_src))
66 case FIB_SOURCE_CMP_BETTER:
67 case FIB_SOURCE_CMP_EQUAL:
69 case FIB_SOURCE_CMP_WORSE:
79 * Source activation. Called when the source is the new best source on the entry
82 fib_entry_src_interpose_activate (fib_entry_src_t *src,
83 const fib_entry_t *fib_entry)
85 fib_entry_src_t *best_src;
86 fib_node_index_t old_pl;
90 src->fes_pl = FIB_NODE_INDEX_INVALID;
93 * The goal here is to find a path-list that will contribute forwarding
95 * First check this entry for other sources that have a path-list
97 best_src = fib_entry_src_rr_get_next_best(src, fib_entry);
101 const fib_entry_src_vft_t *vft;
103 best_src->fes_flags |= FIB_ENTRY_SRC_FLAG_CONTRIBUTING;
104 vft = fib_entry_src_get_vft(best_src);
106 * there is another source for this entry. activate it so it
107 * can provide forwarding
109 if (NULL != vft->fesv_activate)
111 if (vft->fesv_activate(best_src, fib_entry))
114 * next best source activated ok, use its path list
116 src->fes_pl = best_src->fes_pl;
121 * the best source won't install so will use a drop
125 dproto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto);
128 fib_path_list_create_special(dproto,
129 FIB_PATH_LIST_FLAG_DROP,
130 drop_dpo_get(dproto));
136 * next best source does not require activation, use its path list
138 src->fes_pl = best_src->fes_pl;
144 * find the covering prefix. become a dependent thereof.
145 * for IP there should always be a cover, though it may be the default route.
146 * For MPLS there is never a cover.
148 if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
150 src->fes_pl = fib_path_list_create_special(DPO_PROTO_MPLS,
151 FIB_PATH_LIST_FLAG_DROP,
156 src->u.interpose.fesi_cover =
157 fib_table_get_less_specific(fib_entry->fe_fib_index,
158 &fib_entry->fe_prefix);
160 ASSERT(FIB_NODE_INDEX_INVALID != src->u.interpose.fesi_cover);
162 cover = fib_entry_get(src->u.interpose.fesi_cover);
164 src->u.interpose.fesi_sibling =
165 fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
168 * if the cover is attached then install an attached-host path
169 * (like an adj-fib). Otherwise inherit the forwarding from the cover
171 if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
173 fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
177 fib_entry_src_rr_use_covers_pl(src, fib_entry, cover);
182 fib_path_list_unlock(old_pl);
183 fib_path_list_lock(src->fes_pl);
186 * return go for install
193 * Called when the source is no longer best source on the entry
196 fib_entry_src_interpose_deactivate (fib_entry_src_t *src,
197 const fib_entry_t *fib_entry)
201 if (FIB_NODE_INDEX_INVALID != src->u.interpose.fesi_cover)
204 * remove the dependency on the covering entry, if that's
205 * what was contributing the path-list
207 cover = fib_entry_get(src->u.interpose.fesi_cover);
208 fib_entry_cover_untrack(cover, src->u.interpose.fesi_sibling);
209 src->u.interpose.fesi_cover = FIB_NODE_INDEX_INVALID;
213 fib_entry_src_t *best_src;
215 best_src = fib_entry_src_rr_get_next_best(src, fib_entry);
219 best_src->fes_flags &= ~FIB_ENTRY_SRC_FLAG_CONTRIBUTING;
221 * there is another source for this entry. activate it so it
222 * can provide forwarding
224 FIB_ENTRY_SRC_VFT_INVOKE(fib_entry, best_src, fesv_deactivate,
225 (best_src, fib_entry));
229 fib_path_list_unlock(src->fes_pl);
230 src->fes_pl = FIB_NODE_INDEX_INVALID;
231 src->fes_entry_flags &= ~FIB_ENTRY_FLAGS_RR_INHERITED;
235 fib_entry_src_interpose_reactivate (fib_entry_src_t *src,
236 const fib_entry_t *fib_entry)
238 fib_entry_src_interpose_deactivate(src, fib_entry);
239 return (fib_entry_src_interpose_activate(src, fib_entry));
242 static fib_entry_src_cover_res_t
243 fib_entry_src_interpose_cover_change (fib_entry_src_t *src,
244 const fib_entry_t *fib_entry)
246 fib_entry_src_cover_res_t res = {
248 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
251 if (FIB_NODE_INDEX_INVALID == src->u.interpose.fesi_cover)
254 * the source may be added, but it is not active
255 * if it is not tracking the cover.
261 * this function is called when this entry's cover has a more specific
262 * entry inserted beneath it. That does not necessarily mean that this
263 * entry is covered by the new prefix. check that
265 if (src->u.interpose.fesi_cover !=
266 fib_table_get_less_specific(fib_entry->fe_fib_index,
267 &fib_entry->fe_prefix))
269 fib_entry_src_interpose_deactivate(src, fib_entry);
270 fib_entry_src_interpose_activate(src, fib_entry);
273 * dependent children need to re-resolve to the new forwarding info
275 res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
281 fib_entry_src_interpose_add (fib_entry_src_t *src,
282 const fib_entry_t *entry,
283 fib_entry_flag_t flags,
287 dpo_copy(&src->u.interpose.fesi_dpo, dpo);
291 fib_entry_src_interpose_remove (fib_entry_src_t *src)
293 dpo_reset(&src->u.interpose.fesi_dpo);
297 fib_entry_src_interpose_set_data (fib_entry_src_t *src,
298 const fib_entry_t *fib_entry,
301 const dpo_id_t *dpo = data;
303 dpo_copy(&src->u.interpose.fesi_dpo, dpo);
307 * Contribute forwarding to interpose in the chain
309 const dpo_id_t* fib_entry_src_interpose_contribute(const fib_entry_src_t *src,
310 const fib_entry_t *fib_entry)
312 return (&src->u.interpose.fesi_dpo);
316 fib_entry_src_interpose_copy (const fib_entry_src_t *orig_src,
317 const fib_entry_t *fib_entry,
318 fib_entry_src_t *copy_src)
320 copy_src->u.interpose.fesi_cover = orig_src->u.interpose.fesi_cover;
322 if (FIB_NODE_INDEX_INVALID != copy_src->u.interpose.fesi_cover)
326 cover = fib_entry_get(orig_src->u.interpose.fesi_cover);
327 copy_src->u.interpose.fesi_sibling =
328 fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
331 dpo_copy(©_src->u.interpose.fesi_dpo,
332 &orig_src->u.interpose.fesi_dpo);
336 fib_entry_src_interpose_flag_change (fib_entry_src_t *src,
337 const fib_entry_t *fib_entry,
338 fib_entry_flag_t new_flags)
340 if (!(new_flags & FIB_ENTRY_FLAG_INTERPOSE))
343 * stop tracking the source contributing forwarding
344 * and reset the interposer DPO
346 fib_entry_src_interpose_deactivate(src, fib_entry);
347 fib_entry_src_interpose_deinit(src);
352 fib_entry_src_interpose_format (fib_entry_src_t *src,
355 s = format(s, " cover:%d interpose:\n%U%U",
356 src->u.interpose.fesi_cover,
357 format_white_space, 6,
358 format_dpo_id, &src->u.interpose.fesi_dpo, 8);
363 const static fib_entry_src_vft_t interpose_src_vft = {
364 .fesv_init = fib_entry_src_interpose_init,
365 .fesv_deinit = fib_entry_src_interpose_deinit,
366 .fesv_activate = fib_entry_src_interpose_activate,
367 .fesv_reactivate = fib_entry_src_interpose_reactivate,
368 .fesv_deactivate = fib_entry_src_interpose_deactivate,
369 .fesv_cover_change = fib_entry_src_interpose_cover_change,
370 .fesv_cover_update = fib_entry_src_rr_cover_update,
371 .fesv_format = fib_entry_src_interpose_format,
372 .fesv_add = fib_entry_src_interpose_add,
373 .fesv_remove = fib_entry_src_interpose_remove,
374 .fesv_contribute_interpose = fib_entry_src_interpose_contribute,
375 .fesv_set_data = fib_entry_src_interpose_set_data,
376 .fesv_copy = fib_entry_src_interpose_copy,
377 .fesv_flags_change = fib_entry_src_interpose_flag_change,
381 fib_entry_src_interpose_register (void)
383 fib_entry_src_behaviour_register(FIB_SOURCE_BH_INTERPOSE,