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->adj.fesa_cover = FIB_NODE_INDEX_INVALID;
31 src->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;
131 * Add a path-extension indicating whether this path is resolved,
132 * because it passed the refinement check
135 fib_enty_src_adj_update_path_ext (fib_entry_src_t *src,
136 fib_node_index_t path_index,
137 fib_path_ext_adj_flags_t flags)
139 fib_path_ext_t *path_ext;
141 path_ext = fib_path_ext_list_find_by_path_index(&src->fes_path_exts,
144 if (NULL != path_ext)
146 path_ext->fpe_adj_flags = flags;
150 ASSERT(!"no path extension");
154 typedef struct fib_entry_src_path_list_walk_cxt_t_
156 fib_entry_src_t *src;
158 fib_path_ext_adj_flags_t flags;
159 } fib_entry_src_path_list_walk_cxt_t;
161 static fib_path_list_walk_rc_t
162 fib_entry_src_adj_path_list_walk (fib_node_index_t pl_index,
163 fib_node_index_t path_index,
166 fib_entry_src_path_list_walk_cxt_t *ctx;
170 adj_itf = fib_path_get_resolving_interface(path_index);
172 if (ctx->cover_itf == adj_itf)
174 fib_enty_src_adj_update_path_ext(ctx->src, path_index,
175 FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER);
176 ctx->flags |= FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER;
181 * if the interface the adj is on is unnumbered to the
182 * cover's, then allow that too.
184 vnet_sw_interface_t *swif;
186 swif = vnet_get_sw_interface (vnet_get_main(), adj_itf);
188 if (swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED &&
189 ctx->cover_itf == swif->unnumbered_sw_if_index)
191 fib_enty_src_adj_update_path_ext(ctx->src, path_index,
192 FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER);
193 ctx->flags |= FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER;
197 fib_enty_src_adj_update_path_ext(ctx->src, path_index,
198 FIB_PATH_EXT_ADJ_FLAG_NONE);
201 return (FIB_PATH_LIST_WALK_CONTINUE);
206 * Called when the source is the new longer best source on the entry
209 fib_entry_src_adj_activate (fib_entry_src_t *src,
210 const fib_entry_t *fib_entry)
215 * find the covering prefix. become a dependent thereof.
216 * there should always be a cover, though it may be the default route.
218 src->adj.fesa_cover = fib_table_get_less_specific(fib_entry->fe_fib_index,
219 &fib_entry->fe_prefix);
221 ASSERT(FIB_NODE_INDEX_INVALID != src->adj.fesa_cover);
222 ASSERT(fib_entry_get_index(fib_entry) != src->adj.fesa_cover);
224 cover = fib_entry_get(src->adj.fesa_cover);
226 ASSERT(cover != fib_entry);
228 src->adj.fesa_sibling =
229 fib_entry_cover_track(cover,
230 fib_entry_get_index(fib_entry));
233 * if the cover is attached on the same interface as this adj source then
234 * install the FIB entry via the adj. otherwise install a drop.
235 * This prevents ARP/ND entries that on interface X that do not belong
236 * on X's subnet from being added to the FIB. To do so would allow
237 * nefarious gratuitous ARP requests from attracting traffic to the sender.
239 * and yes, I really do mean attached and not connected.
241 * ip route add 10.0.0.0/24 Eth0
242 * is attached. and we want adj-fibs to install on Eth0.
244 if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
246 fib_entry_src_path_list_walk_cxt_t ctx = {
247 .cover_itf = fib_entry_get_resolving_interface(src->adj.fesa_cover),
248 .flags = FIB_PATH_EXT_ADJ_FLAG_NONE,
252 fib_path_list_walk(src->fes_pl,
253 fib_entry_src_adj_path_list_walk,
257 * active the entry is one of the paths refines the cover.
259 return (FIB_PATH_EXT_ADJ_FLAG_REFINES_COVER & ctx.flags);
266 * Called when the source is no longer best source on the entry
269 fib_entry_src_adj_deactivate (fib_entry_src_t *src,
270 const fib_entry_t *fib_entry)
275 * remove the depednecy on the covering entry
277 ASSERT(FIB_NODE_INDEX_INVALID != src->adj.fesa_cover);
278 cover = fib_entry_get(src->adj.fesa_cover);
280 fib_entry_cover_untrack(cover, src->adj.fesa_sibling);
283 * tell the cover this entry no longer needs exporting
285 fib_attached_export_covered_removed(cover, fib_entry_get_index(fib_entry));
287 src->adj.fesa_cover = FIB_NODE_INDEX_INVALID;
291 fib_entry_src_adj_format (fib_entry_src_t *src,
294 return (format(s, "cover:%d", src->adj.fesa_cover));
298 fib_entry_src_adj_installed (fib_entry_src_t *src,
299 const fib_entry_t *fib_entry)
302 * The adj source now rules! poke our cover to get exported
306 ASSERT(FIB_NODE_INDEX_INVALID != src->adj.fesa_cover);
307 cover = fib_entry_get(src->adj.fesa_cover);
309 fib_attached_export_covered_added(cover,
310 fib_entry_get_index(fib_entry));
313 static fib_entry_src_cover_res_t
314 fib_entry_src_adj_cover_change (fib_entry_src_t *src,
315 const fib_entry_t *fib_entry)
317 fib_entry_src_cover_res_t res = {
319 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
322 fib_entry_src_adj_deactivate(src, fib_entry);
324 res.install = fib_entry_src_adj_activate(src, fib_entry);
328 * ADJ fib can install
330 res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
337 * fib_entry_src_adj_cover_update
339 static fib_entry_src_cover_res_t
340 fib_entry_src_adj_cover_update (fib_entry_src_t *src,
341 const fib_entry_t *fib_entry)
344 * the cover has updated, i.e. its forwarding or flags
345 * have changed. don't decativate/activate here, since this
346 * prefix is updated during the covers walk.
348 fib_entry_src_cover_res_t res = {
350 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
354 ASSERT(FIB_NODE_INDEX_INVALID != src->adj.fesa_cover);
356 cover = fib_entry_get(src->adj.fesa_cover);
358 res.install = (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover));
363 const static fib_entry_src_vft_t adj_src_vft = {
364 .fesv_init = fib_entry_src_adj_init,
365 .fesv_path_swap = fib_entry_src_adj_path_swap,
366 .fesv_path_add = fib_entry_src_adj_path_add,
367 .fesv_path_remove = fib_entry_src_adj_path_remove,
368 .fesv_remove = fib_entry_src_adj_remove,
369 .fesv_activate = fib_entry_src_adj_activate,
370 .fesv_deactivate = fib_entry_src_adj_deactivate,
371 .fesv_format = fib_entry_src_adj_format,
372 .fesv_installed = fib_entry_src_adj_installed,
373 .fesv_cover_change = fib_entry_src_adj_cover_change,
374 .fesv_cover_update = fib_entry_src_adj_cover_update,
378 fib_entry_src_adj_register (void)
380 fib_entry_src_register(FIB_SOURCE_ADJ, &adj_src_vft);