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 "fib_entry.h"
17 #include "fib_entry_src.h"
18 #include "fib_path_list.h"
19 #include "fib_table.h"
20 #include "fib_entry_cover.h"
21 #include "fib_attached_export.h"
22 #include "fib_path_ext.h"
25 * Source initialisation Function
28 fib_entry_src_adj_init (fib_entry_src_t *src)
30 src->u.adj.fesa_cover = FIB_NODE_INDEX_INVALID;
31 src->u.adj.fesa_sibling = FIB_NODE_INDEX_INVALID;
35 fib_entry_src_adj_path_add (fib_entry_src_t *src,
36 const fib_entry_t *entry,
37 fib_path_list_flags_t pl_flags,
38 const fib_route_path_t *paths)
40 const fib_route_path_t *rpath;
42 if (FIB_NODE_INDEX_INVALID == src->fes_pl)
44 src->fes_pl = fib_path_list_create(pl_flags, paths);
48 src->fes_pl = fib_path_list_copy_and_path_add(src->fes_pl,
54 * resolve the existing extensions
56 fib_path_ext_list_resolve(&src->fes_path_exts, src->fes_pl);
61 vec_foreach(rpath, paths)
63 fib_path_ext_list_insert(&src->fes_path_exts,
71 fib_entry_src_adj_path_remove (fib_entry_src_t *src,
72 fib_path_list_flags_t pl_flags,
73 const fib_route_path_t *rpaths)
75 const fib_route_path_t *rpath;
77 if (FIB_NODE_INDEX_INVALID != src->fes_pl)
79 src->fes_pl = fib_path_list_copy_and_path_remove(src->fes_pl,
85 * remove the path-extension for the path
87 vec_foreach(rpath, rpaths)
89 fib_path_ext_list_remove(&src->fes_path_exts, FIB_PATH_EXT_ADJ, rpath);
92 * resolve the remaining extensions
94 fib_path_ext_list_resolve(&src->fes_path_exts, src->fes_pl);
98 fib_entry_src_adj_path_swap (fib_entry_src_t *src,
99 const fib_entry_t *entry,
100 fib_path_list_flags_t pl_flags,
101 const fib_route_path_t *paths)
103 const fib_route_path_t *rpath;
106 * flush all the old extensions before we create a brand new path-list
108 fib_path_ext_list_flush(&src->fes_path_exts);
110 src->fes_pl = fib_path_list_create(pl_flags, paths);
115 vec_foreach(rpath, paths)
117 fib_path_ext_list_push_back(&src->fes_path_exts,
125 fib_entry_src_adj_remove (fib_entry_src_t *src)
127 src->fes_pl = FIB_NODE_INDEX_INVALID;
129 if (FIB_NODE_INDEX_INVALID != src->u.adj.fesa_cover)
131 fib_entry_cover_untrack(fib_entry_get(src->u.adj.fesa_cover),
132 src->u.adj.fesa_sibling);
137 * Add a path-extension indicating whether this path is resolved,
138 * because it passed the refinement check
141 fib_enty_src_adj_update_path_ext (fib_entry_src_t *src,
142 fib_node_index_t path_index,
143 fib_path_ext_adj_flags_t flags)
145 fib_path_ext_t *path_ext;
147 path_ext = fib_path_ext_list_find_by_path_index(&src->fes_path_exts,
150 if (NULL != path_ext)
152 path_ext->fpe_adj_flags = flags;
156 ASSERT(!"no path extension");
160 typedef struct fib_entry_src_path_list_walk_cxt_t_
162 fib_entry_src_t *src;
164 fib_path_ext_adj_flags_t flags;
165 } fib_entry_src_path_list_walk_cxt_t;
167 static fib_path_list_walk_rc_t
168 fib_entry_src_adj_path_list_walk (fib_node_index_t pl_index,
169 fib_node_index_t path_index,
172 fib_entry_src_path_list_walk_cxt_t *ctx;
176 adj_itf = fib_path_get_resolving_interface(path_index);
178 if (ctx->cover_itf == adj_itf)
180 fib_enty_src_adj_update_path_ext(ctx->src, path_index,
181 FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER);
182 ctx->flags |= FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER;
187 * if the interface the adj is on is unnumbered to the
188 * cover's, then allow that too.
190 vnet_sw_interface_t *swif;
192 swif = vnet_get_sw_interface (vnet_get_main(), adj_itf);
194 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED &&
195 ctx->cover_itf == swif->unnumbered_sw_if_index)
197 fib_enty_src_adj_update_path_ext(ctx->src, path_index,
198 FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER);
199 ctx->flags |= FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER;
203 fib_enty_src_adj_update_path_ext(ctx->src, path_index,
204 FIB_PATH_EXT_ADJ_FLAG_NONE);
207 return (FIB_PATH_LIST_WALK_CONTINUE);
211 fib_entry_src_adj_activate (fib_entry_src_t *src,
212 const fib_entry_t *fib_entry)
217 * find the covering prefix. become a dependent thereof.
218 * there should always be a cover, though it may be the default route.
220 src->u.adj.fesa_cover = fib_table_get_less_specific(fib_entry->fe_fib_index,
221 &fib_entry->fe_prefix);
223 ASSERT(FIB_NODE_INDEX_INVALID != src->u.adj.fesa_cover);
224 ASSERT(fib_entry_get_index(fib_entry) != src->u.adj.fesa_cover);
226 cover = fib_entry_get(src->u.adj.fesa_cover);
228 ASSERT(cover != fib_entry);
230 src->u.adj.fesa_sibling =
231 fib_entry_cover_track(cover,
232 fib_entry_get_index(fib_entry));
235 * if the cover is attached on the same interface as this adj source then
236 * install the FIB entry via the adj. otherwise install a drop.
237 * This prevents ARP/ND entries that on interface X that do not belong
238 * on X's subnet from being added to the FIB. To do so would allow
239 * nefarious gratuitous ARP requests from attracting traffic to the sender.
241 * and yes, I really do mean attached and not connected.
243 * ip route add 10.0.0.0/24 Eth0
244 * is attached. and we want adj-fibs to install on Eth0.
246 if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover) ||
247 (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_for_source(src->u.adj.fesa_cover,
248 FIB_SOURCE_INTERFACE)))
250 fib_entry_src_path_list_walk_cxt_t ctx = {
251 .cover_itf = fib_entry_get_resolving_interface(src->u.adj.fesa_cover),
252 .flags = FIB_PATH_EXT_ADJ_FLAG_NONE,
256 fib_path_list_walk(src->fes_pl,
257 fib_entry_src_adj_path_list_walk,
261 * active the entry is one of the paths refines the cover.
263 return (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & ctx.flags);
269 * Source re-activate.
270 * Called when the source path lit has changed and the source is still
274 fib_entry_src_adj_reactivate (fib_entry_src_t *src,
275 const fib_entry_t *fib_entry)
277 fib_entry_src_path_list_walk_cxt_t ctx = {
278 .cover_itf = fib_entry_get_resolving_interface(src->u.adj.fesa_cover),
279 .flags = FIB_PATH_EXT_ADJ_FLAG_NONE,
283 fib_path_list_walk(src->fes_pl,
284 fib_entry_src_adj_path_list_walk,
287 return (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & ctx.flags);
292 * Called when the source is no longer best source on the entry
295 fib_entry_src_adj_deactivate (fib_entry_src_t *src,
296 const fib_entry_t *fib_entry)
301 * remove the dependency on the covering entry
303 if (FIB_NODE_INDEX_INVALID == src->u.adj.fesa_cover)
306 * this is the case if the entry is in the non-forwarding trie
311 cover = fib_entry_get(src->u.adj.fesa_cover);
312 fib_entry_cover_untrack(cover, src->u.adj.fesa_sibling);
315 * tell the cover this entry no longer needs exporting
317 fib_attached_export_covered_removed(cover, fib_entry_get_index(fib_entry));
319 src->u.adj.fesa_cover = FIB_NODE_INDEX_INVALID;
320 src->u.adj.fesa_sibling = FIB_NODE_INDEX_INVALID;
324 fib_entry_src_adj_format (fib_entry_src_t *src,
327 return (format(s, " cover:%d", src->u.adj.fesa_cover));
331 fib_entry_src_adj_installed (fib_entry_src_t *src,
332 const fib_entry_t *fib_entry)
335 * The adj source now rules! poke our cover to get exported
339 ASSERT(FIB_NODE_INDEX_INVALID != src->u.adj.fesa_cover);
340 cover = fib_entry_get(src->u.adj.fesa_cover);
342 fib_attached_export_covered_added(cover,
343 fib_entry_get_index(fib_entry));
346 static fib_entry_src_cover_res_t
347 fib_entry_src_adj_cover_change (fib_entry_src_t *src,
348 const fib_entry_t *fib_entry)
350 fib_entry_src_cover_res_t res = {
352 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
356 * not interested in a change to the cover if the cover
357 * is not being tracked, i.e. the source is not active
359 if (FIB_NODE_INDEX_INVALID == src->u.adj.fesa_cover)
362 fib_entry_src_adj_deactivate(src, fib_entry);
364 res.install = fib_entry_src_adj_activate(src, fib_entry);
368 * ADJ fib can install
370 res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
373 FIB_ENTRY_DBG(fib_entry, "adj-src-cover-changed");
378 * fib_entry_src_adj_cover_update
380 static fib_entry_src_cover_res_t
381 fib_entry_src_adj_cover_update (fib_entry_src_t *src,
382 const fib_entry_t *fib_entry)
385 * the cover has updated, i.e. its forwarding or flags
386 * have changed. don't deactivate/activate here, since this
387 * prefix is updated during the covers walk.
389 fib_entry_src_cover_res_t res = {
391 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
396 * If there is no cover, then the source is not active and we can ignore
399 if (FIB_NODE_INDEX_INVALID != src->u.adj.fesa_cover)
401 cover = fib_entry_get(src->u.adj.fesa_cover);
403 res.install = (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover));
405 FIB_ENTRY_DBG(fib_entry, "adj-src-cover-updated");
410 const static fib_entry_src_vft_t adj_src_vft = {
411 .fesv_init = fib_entry_src_adj_init,
412 .fesv_path_swap = fib_entry_src_adj_path_swap,
413 .fesv_path_add = fib_entry_src_adj_path_add,
414 .fesv_path_remove = fib_entry_src_adj_path_remove,
415 .fesv_remove = fib_entry_src_adj_remove,
416 .fesv_activate = fib_entry_src_adj_activate,
417 .fesv_deactivate = fib_entry_src_adj_deactivate,
418 .fesv_reactivate = fib_entry_src_adj_reactivate,
419 .fesv_format = fib_entry_src_adj_format,
420 .fesv_installed = fib_entry_src_adj_installed,
421 .fesv_cover_change = fib_entry_src_adj_cover_change,
422 .fesv_cover_update = fib_entry_src_adj_cover_update,
426 fib_entry_src_adj_register (void)
428 fib_entry_src_behaviour_register(FIB_SOURCE_BH_ADJ, &adj_src_vft);