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 if (source <= src->fes_src)
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;
122 * next best source does not require activation, use its path list
124 src->fes_pl = best_src->fes_pl;
130 * find the covering prefix. become a dependent thereof.
131 * for IP there should always be a cover, though it may be the default route.
132 * For MPLS there is never a cover.
134 if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
136 src->fes_pl = fib_path_list_create_special(DPO_PROTO_MPLS,
137 FIB_PATH_LIST_FLAG_DROP,
142 src->u.interpose.fesi_cover =
143 fib_table_get_less_specific(fib_entry->fe_fib_index,
144 &fib_entry->fe_prefix);
146 ASSERT(FIB_NODE_INDEX_INVALID != src->u.interpose.fesi_cover);
148 cover = fib_entry_get(src->u.interpose.fesi_cover);
150 src->u.interpose.fesi_sibling =
151 fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
154 * if the cover is attached then install an attached-host path
155 * (like an adj-fib). Otherwise inherit the forwarding from the cover
157 if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
159 fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
163 fib_entry_src_rr_use_covers_pl(src, fib_entry, cover);
168 fib_path_list_unlock(old_pl);
169 fib_path_list_lock(src->fes_pl);
172 * return go for install
179 * Called when the source is no longer best source on the entry
182 fib_entry_src_interpose_deactivate (fib_entry_src_t *src,
183 const fib_entry_t *fib_entry)
187 if (FIB_NODE_INDEX_INVALID != src->u.interpose.fesi_cover)
190 * remove the depednecy on the covering entry, if that's
191 * what was contributing the path-list
193 cover = fib_entry_get(src->u.interpose.fesi_cover);
194 fib_entry_cover_untrack(cover, src->u.interpose.fesi_sibling);
195 src->u.interpose.fesi_cover = FIB_NODE_INDEX_INVALID;
199 fib_entry_src_t *best_src;
201 best_src = fib_entry_src_rr_get_next_best(src, fib_entry);
205 best_src->fes_flags &= ~FIB_ENTRY_SRC_FLAG_CONTRIBUTING;
207 * there is another source for this entry. activate it so it
208 * can provide forwarding
210 FIB_ENTRY_SRC_VFT_INVOKE(best_src, fesv_deactivate,
211 (best_src, fib_entry));
215 fib_path_list_unlock(src->fes_pl);
216 src->fes_pl = FIB_NODE_INDEX_INVALID;
217 src->fes_entry_flags &= ~FIB_ENTRY_FLAGS_RR_INHERITED;
221 fib_entry_src_interpose_reactivate (fib_entry_src_t *src,
222 const fib_entry_t *fib_entry)
224 fib_entry_src_interpose_deactivate(src, fib_entry);
225 return (fib_entry_src_interpose_activate(src, fib_entry));
228 static fib_entry_src_cover_res_t
229 fib_entry_src_interpose_cover_change (fib_entry_src_t *src,
230 const fib_entry_t *fib_entry)
232 fib_entry_src_cover_res_t res = {
234 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
237 if (FIB_NODE_INDEX_INVALID == src->u.interpose.fesi_cover)
240 * the source may be added, but it is not active
241 * if it is not tracking the cover.
247 * this function is called when this entry's cover has a more specific
248 * entry inserted benaeth it. That does not necessarily mean that this
249 * entry is covered by the new prefix. check that
251 if (src->u.interpose.fesi_cover !=
252 fib_table_get_less_specific(fib_entry->fe_fib_index,
253 &fib_entry->fe_prefix))
255 fib_entry_src_interpose_deactivate(src, fib_entry);
256 fib_entry_src_interpose_activate(src, fib_entry);
259 * dependent children need to re-resolve to the new forwarding info
261 res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
267 fib_entry_src_interpose_add (fib_entry_src_t *src,
268 const fib_entry_t *entry,
269 fib_entry_flag_t flags,
273 dpo_copy(&src->u.interpose.fesi_dpo, dpo);
277 fib_entry_src_interpose_remove (fib_entry_src_t *src)
279 dpo_reset(&src->u.interpose.fesi_dpo);
283 fib_entry_src_interpose_set_data (fib_entry_src_t *src,
284 const fib_entry_t *fib_entry,
287 const dpo_id_t *dpo = data;
289 dpo_copy(&src->u.interpose.fesi_dpo, dpo);
293 * Contribute forwarding to interpose in the chain
295 const dpo_id_t* fib_entry_src_interpose_contribute(const fib_entry_src_t *src,
296 const fib_entry_t *fib_entry)
298 return (&src->u.interpose.fesi_dpo);
302 fib_entry_src_interpose_copy (const fib_entry_src_t *orig_src,
303 const fib_entry_t *fib_entry,
304 fib_entry_src_t *copy_src)
306 copy_src->u.interpose.fesi_cover = orig_src->u.interpose.fesi_cover;
308 if (FIB_NODE_INDEX_INVALID != copy_src->u.interpose.fesi_cover)
312 cover = fib_entry_get(orig_src->u.interpose.fesi_cover);
313 copy_src->u.interpose.fesi_sibling =
314 fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
317 dpo_copy(©_src->u.interpose.fesi_dpo,
318 &orig_src->u.interpose.fesi_dpo);
322 fib_entry_src_interpose_flag_change (fib_entry_src_t *src,
323 const fib_entry_t *fib_entry,
324 fib_entry_flag_t new_flags)
326 if (!(new_flags & FIB_ENTRY_FLAG_INTERPOSE))
329 * stop tracking the source contributing forwarding
330 * and reset the interposer DPO
332 fib_entry_src_interpose_deactivate(src, fib_entry);
333 fib_entry_src_interpose_deinit(src);
338 fib_entry_src_interpose_format (fib_entry_src_t *src,
341 s = format(s, " cover:%d interpose:\n%U%U",
342 src->u.interpose.fesi_cover,
343 format_white_space, 6,
344 format_dpo_id, &src->u.interpose.fesi_dpo, 8);
349 const static fib_entry_src_vft_t interpose_src_vft = {
350 .fesv_init = fib_entry_src_interpose_init,
351 .fesv_deinit = fib_entry_src_interpose_deinit,
352 .fesv_activate = fib_entry_src_interpose_activate,
353 .fesv_reactivate = fib_entry_src_interpose_reactivate,
354 .fesv_deactivate = fib_entry_src_interpose_deactivate,
355 .fesv_cover_change = fib_entry_src_interpose_cover_change,
356 .fesv_cover_update = fib_entry_src_rr_cover_update,
357 .fesv_format = fib_entry_src_interpose_format,
358 .fesv_add = fib_entry_src_interpose_add,
359 .fesv_remove = fib_entry_src_interpose_remove,
360 .fesv_contribute_interpose = fib_entry_src_interpose_contribute,
361 .fesv_set_data = fib_entry_src_interpose_set_data,
362 .fesv_copy = fib_entry_src_interpose_copy,
363 .fesv_flags_change = fib_entry_src_interpose_flag_change,
367 fib_entry_src_interpose_register (void)
369 fib_entry_src_register(FIB_SOURCE_INTERPOSE, &interpose_src_vft);