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_cover.h"
24 #include "fib_entry.h"
25 #include "fib_table.h"
28 * fib_entry_src_rr_resolve_via_connected
30 * Resolve via a connected cover.
33 fib_entry_src_rr_resolve_via_connected (fib_entry_src_t *src,
34 const fib_entry_t *fib_entry,
35 const fib_entry_t *cover)
37 const fib_route_path_t path = {
38 .frp_proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto),
39 .frp_addr = fib_entry->fe_prefix.fp_addr,
40 .frp_sw_if_index = fib_entry_get_resolving_interface(
41 fib_entry_get_index(cover)),
45 fib_route_path_t *paths = NULL;
46 vec_add1(paths, path);
49 * since the cover is connected, the address this entry corresponds
50 * to is a peer (ARP-able for) on the interface to which the cover is
51 * connected. The fact we resolve via the cover, just means this RR
52 * source is the first SRC to use said peer. The ARP source will be along
53 * shortly to over-rule this RR source.
55 src->fes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NONE, paths);
56 src->fes_entry_flags = fib_entry_get_flags(fib_entry_get_index(cover));
63 * Source initialisation Function
66 fib_entry_src_rr_init (fib_entry_src_t *src)
68 src->rr.fesr_cover = FIB_NODE_INDEX_INVALID;
69 src->rr.fesr_sibling = FIB_NODE_INDEX_INVALID;
74 * use the path-list of the cover, unless it would form a loop.
75 * that is unless the cover is via this entry.
76 * If a loop were to form it would be a 1 level loop (i.e. X via X),
77 * and there would be 2 locks on the path-list; one since its used
78 * by the cover, and 1 from here. The first lock will go when the
79 * cover is removed, the second, and last, when the covered walk
80 * occurs during the cover's removel - this is not a place where
81 * we can handle last lock gone.
82 * In short, don't let the loop form. The usual rules of 'we must
83 * let it form so we know when it breaks' don't apply here, since
84 * the loop will break when the cover changes, and this function
85 * will be called again when that happens.
88 fib_entry_src_rr_use_covers_pl (fib_entry_src_t *src,
89 const fib_entry_t *fib_entry,
90 const fib_entry_t *cover)
92 fib_node_index_t *entries = NULL;
95 proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto);
96 vec_add1(entries, fib_entry_get_index(fib_entry));
98 if (fib_path_list_recursive_loop_detect(cover->fe_parent,
101 src->fes_pl = fib_path_list_create_special(proto,
102 FIB_PATH_LIST_FLAG_DROP,
103 drop_dpo_get(proto));
107 src->fes_pl = cover->fe_parent;
113 * Source activation. Called when the source is the new best source on the entry
116 fib_entry_src_rr_activate (fib_entry_src_t *src,
117 const fib_entry_t *fib_entry)
122 * find the covering prefix. become a dependent thereof.
123 * for IP there should always be a cover, though it may be the default route.
124 * For MPLS there is never a cover.
126 if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto)
128 src->fes_pl = fib_path_list_create_special(DPO_PROTO_MPLS,
129 FIB_PATH_LIST_FLAG_DROP,
131 fib_path_list_lock(src->fes_pl);
135 src->rr.fesr_cover = fib_table_get_less_specific(fib_entry->fe_fib_index,
136 &fib_entry->fe_prefix);
138 ASSERT(FIB_NODE_INDEX_INVALID != src->rr.fesr_cover);
140 cover = fib_entry_get(src->rr.fesr_cover);
142 src->rr.fesr_sibling =
143 fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
146 * if the cover is attached then install an attached-host path
147 * (like an adj-fib). Otherwise inherit the forwarding from the cover
149 if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
151 fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
155 fib_entry_src_rr_use_covers_pl(src, fib_entry, cover);
157 fib_path_list_lock(src->fes_pl);
160 * return go for install
167 * Called when the source is no longer best source on the entry
170 fib_entry_src_rr_deactivate (fib_entry_src_t *src,
171 const fib_entry_t *fib_entry)
176 * remove the depednecy on the covering entry
178 if (FIB_NODE_INDEX_INVALID != src->rr.fesr_cover)
180 cover = fib_entry_get(src->rr.fesr_cover);
181 fib_entry_cover_untrack(cover, src->rr.fesr_sibling);
182 src->rr.fesr_cover = FIB_NODE_INDEX_INVALID;
185 fib_path_list_unlock(src->fes_pl);
186 src->fes_pl = FIB_NODE_INDEX_INVALID;
187 src->fes_entry_flags = FIB_ENTRY_FLAG_NONE;
190 static fib_entry_src_cover_res_t
191 fib_entry_src_rr_cover_change (fib_entry_src_t *src,
192 const fib_entry_t *fib_entry)
194 fib_entry_src_cover_res_t res = {
196 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
199 if (FIB_NODE_INDEX_INVALID == src->rr.fesr_cover)
202 * the source may be added, but it is not active
203 * if it is not tracking the cover.
209 * this function is called when this entry's cover has a more specific
210 * entry inserted benaeth it. That does not necessarily mean that this
211 * entry is covered by the new prefix. check that
213 if (src->rr.fesr_cover != fib_table_get_less_specific(fib_entry->fe_fib_index,
214 &fib_entry->fe_prefix))
216 fib_entry_src_rr_deactivate(src, fib_entry);
217 fib_entry_src_rr_activate(src, fib_entry);
220 * dependent children need to re-resolve to the new forwarding info
222 res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
228 * fib_entry_src_rr_cover_update
230 * This entry's cover has updated its forwarding info. This entry
231 * will need to re-inheret.
233 static fib_entry_src_cover_res_t
234 fib_entry_src_rr_cover_update (fib_entry_src_t *src,
235 const fib_entry_t *fib_entry)
237 fib_entry_src_cover_res_t res = {
239 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
241 fib_node_index_t old_path_list;
244 if (FIB_NODE_INDEX_INVALID == src->rr.fesr_cover)
247 * the source may be added, but it is not active
248 * if it is not tracking the cover.
253 cover = fib_entry_get(src->rr.fesr_cover);
254 old_path_list = src->fes_pl;
257 * if the ocver is attached then install an attached-host path
258 * (like an adj-fib). Otherwise inherit the forwarding from the cover
260 if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover))
262 fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover);
266 fib_entry_src_rr_use_covers_pl(src, fib_entry, cover);
268 fib_path_list_lock(src->fes_pl);
269 fib_path_list_unlock(old_path_list);
272 * dependent children need to re-resolve to the new forwarding info
274 res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE;
280 fib_entry_src_rr_format (fib_entry_src_t *src,
283 return (format(s, "cover:%d", src->rr.fesr_cover));
286 const static fib_entry_src_vft_t rr_src_vft = {
287 .fesv_init = fib_entry_src_rr_init,
288 .fesv_activate = fib_entry_src_rr_activate,
289 .fesv_deactivate = fib_entry_src_rr_deactivate,
290 .fesv_cover_change = fib_entry_src_rr_cover_change,
291 .fesv_cover_update = fib_entry_src_rr_cover_update,
292 .fesv_format = fib_entry_src_rr_format,
296 fib_entry_src_rr_register (void)
298 fib_entry_src_register(FIB_SOURCE_RR, &rr_src_vft);
299 fib_entry_src_register(FIB_SOURCE_URPF_EXEMPT, &rr_src_vft);