blob: 402369d1dfc869c7251f718fa0b6b5cc1101eb67 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include "fib_entry.h"
17#include "fib_entry_src.h"
18#include "fib_path_list.h"
19#include "fib_internal.h"
20#include "fib_table.h"
21#include "fib_entry_cover.h"
22#include "fib_attached_export.h"
23
24/**
25 * Source initialisation Function
26 */
27static void
28fib_entry_src_interface_init (fib_entry_src_t *src)
29{
Neale Ranns2303cb12018-02-21 04:57:17 -080030 src->u.interface.fesi_cover = FIB_NODE_INDEX_INVALID;
31 src->u.interface.fesi_sibling = FIB_NODE_INDEX_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010032}
33
34static void
Neale Ranns9a69a602017-03-26 10:56:33 -070035fib_entry_src_interface_add (fib_entry_src_t *src,
36 const fib_entry_t *entry,
37 fib_entry_flag_t flags,
Neale Rannsda78f952017-05-24 09:15:43 -070038 dpo_proto_t proto,
Neale Ranns9a69a602017-03-26 10:56:33 -070039 const dpo_id_t *dpo)
40{
41 src->fes_pl = fib_path_list_create_special(
42 proto,
43 fib_entry_src_flags_2_path_list_flags(flags),
44 dpo);
45}
46
47static void
48fib_entry_src_interface_remove (fib_entry_src_t *src)
49{
50 src->fes_pl = FIB_NODE_INDEX_INVALID;
Neale Rannse2fe0972020-11-26 08:37:27 +000051 ASSERT(src->u.interface.fesi_sibling == ~0);
52}
53
54static int
55fib_entry_src_interface_update_glean (fib_entry_t *cover,
56 const fib_entry_t *local)
57{
58 fib_entry_src_t *src;
59 adj_index_t ai;
60
61 src = fib_entry_src_find (cover, FIB_SOURCE_INTERFACE);
62
63 if (NULL == src)
64 {
65 /*
66 * The cover is not an interface source, no work
67 */
68 return 0;
69 }
70
71 ai = fib_path_list_get_adj(src->fes_pl,
72 fib_entry_get_default_chain_type(cover));
73
74 if (INDEX_INVALID != ai)
75 {
76 ip_adjacency_t *adj;
77
78 adj = adj_get(ai);
79
80 if (IP_LOOKUP_NEXT_GLEAN == adj->lookup_next_index)
81 {
82 /*
83 * the connected prefix will link to a glean on a non-p2p
84 * interface.
85 * Ensure we are updating with a host in the connected's subnet
86 */
87 if (fib_prefix_is_cover(&adj->sub_type.glean.rx_pfx,
88 &local->fe_prefix))
89 {
90 adj->sub_type.glean.rx_pfx.fp_addr = local->fe_prefix.fp_addr;
91 return (1);
92 }
93 }
94 }
95
96 return (0);
97}
98
99static walk_rc_t
100fib_entry_src_interface_update_glean_walk (fib_entry_t *cover,
101 fib_node_index_t covered,
102 void *ctx)
103{
104 if (fib_entry_src_interface_update_glean(cover, fib_entry_get(covered)))
105 return (WALK_STOP);
106
107 return (WALK_CONTINUE);
Neale Ranns9a69a602017-03-26 10:56:33 -0700108}
109
110static void
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100111fib_entry_src_interface_path_swap (fib_entry_src_t *src,
112 const fib_entry_t *entry,
113 fib_path_list_flags_t pl_flags,
114 const fib_route_path_t *paths)
115{
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100116 src->fes_pl = fib_path_list_create(pl_flags, paths);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100117}
118
119/*
120 * Source activate.
121 * Called when the source is teh new longer best source on the entry
122 */
123static int
124fib_entry_src_interface_activate (fib_entry_src_t *src,
125 const fib_entry_t *fib_entry)
126{
127 fib_entry_t *cover;
128
129 if (FIB_ENTRY_FLAG_LOCAL & src->fes_entry_flags)
130 {
131 /*
132 * Track the covering attached/connected cover. This is so that
133 * during an attached export of the cover, this local prefix is
134 * also exported
135 */
Neale Ranns2303cb12018-02-21 04:57:17 -0800136 src->u.interface.fesi_cover =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100137 fib_table_get_less_specific(fib_entry->fe_fib_index,
138 &fib_entry->fe_prefix);
139
Neale Ranns2303cb12018-02-21 04:57:17 -0800140 ASSERT(FIB_NODE_INDEX_INVALID != src->u.interface.fesi_cover);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100141
Neale Ranns2303cb12018-02-21 04:57:17 -0800142 cover = fib_entry_get(src->u.interface.fesi_cover);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100143
Neale Ranns2303cb12018-02-21 04:57:17 -0800144 src->u.interface.fesi_sibling =
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145 fib_entry_cover_track(cover, fib_entry_get_index(fib_entry));
Neale Rannse2fe0972020-11-26 08:37:27 +0000146
147 fib_entry_src_interface_update_glean(cover, fib_entry);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100148 }
149
150 return (!0);
151}
152
153
154/*
155 * Source Deactivate.
156 * Called when the source is no longer best source on the entry
157 */
158static void
159fib_entry_src_interface_deactivate (fib_entry_src_t *src,
160 const fib_entry_t *fib_entry)
161{
162 fib_entry_t *cover;
163
164 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700165 * remove the dependency on the covering entry
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100166 */
Neale Ranns2303cb12018-02-21 04:57:17 -0800167 if (FIB_NODE_INDEX_INVALID != src->u.interface.fesi_cover)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100168 {
Neale Ranns2303cb12018-02-21 04:57:17 -0800169 cover = fib_entry_get(src->u.interface.fesi_cover);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170
Neale Ranns2303cb12018-02-21 04:57:17 -0800171 fib_entry_cover_untrack(cover, src->u.interface.fesi_sibling);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100172
Neale Ranns2303cb12018-02-21 04:57:17 -0800173 src->u.interface.fesi_cover = FIB_NODE_INDEX_INVALID;
Neale Rannse2fe0972020-11-26 08:37:27 +0000174 src->u.interface.fesi_sibling = ~0;
175
176 fib_entry_cover_walk(cover,
177 fib_entry_src_interface_update_glean_walk,
178 NULL);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100179 }
180}
181
182static fib_entry_src_cover_res_t
183fib_entry_src_interface_cover_change (fib_entry_src_t *src,
184 const fib_entry_t *fib_entry)
185{
186 fib_entry_src_cover_res_t res = {
187 .install = !0,
188 .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE,
189 };
190
Neale Ranns2303cb12018-02-21 04:57:17 -0800191 if (FIB_NODE_INDEX_INVALID == src->u.interface.fesi_cover)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100192 {
193 /*
194 * not tracking the cover. surprised we got poked?
195 */
196 return (res);
197 }
198
199 /*
200 * this function is called when this entry's cover has a more specific
201 * entry inserted benaeth it. That does not necessarily mean that this
202 * entry is covered by the new prefix. check that
203 */
Neale Ranns2303cb12018-02-21 04:57:17 -0800204 if (src->u.interface.fesi_cover !=
205 fib_table_get_less_specific(fib_entry->fe_fib_index,
206 &fib_entry->fe_prefix))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100207 {
208 fib_entry_src_interface_deactivate(src, fib_entry);
209 fib_entry_src_interface_activate(src, fib_entry);
210 }
211 return (res);
212}
213
214static void
215fib_entry_src_interface_installed (fib_entry_src_t *src,
216 const fib_entry_t *fib_entry)
217{
218 /*
219 * The interface source now rules! poke our cover to get exported
220 */
221 fib_entry_t *cover;
222
Neale Ranns2303cb12018-02-21 04:57:17 -0800223 if (FIB_NODE_INDEX_INVALID != src->u.interface.fesi_cover)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100224 {
Neale Ranns2303cb12018-02-21 04:57:17 -0800225 cover = fib_entry_get(src->u.interface.fesi_cover);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100226
227 fib_attached_export_covered_added(cover,
228 fib_entry_get_index(fib_entry));
229 }
230}
231
232static u8*
233fib_entry_src_interface_format (fib_entry_src_t *src,
234 u8* s)
235{
Neale Ranns2303cb12018-02-21 04:57:17 -0800236 return (format(s, " cover:%d", src->u.interface.fesi_cover));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100237}
238
239const static fib_entry_src_vft_t interface_src_vft = {
240 .fesv_init = fib_entry_src_interface_init,
Neale Ranns9a69a602017-03-26 10:56:33 -0700241 .fesv_add = fib_entry_src_interface_add,
242 .fesv_remove = fib_entry_src_interface_remove,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100243 .fesv_path_swap = fib_entry_src_interface_path_swap,
244 .fesv_activate = fib_entry_src_interface_activate,
245 .fesv_deactivate = fib_entry_src_interface_deactivate,
246 .fesv_format = fib_entry_src_interface_format,
247 .fesv_installed = fib_entry_src_interface_installed,
248 .fesv_cover_change = fib_entry_src_interface_cover_change,
249 /*
250 * not concerned about updates to the cover. the cover will
251 * decide to export or not
252 */
253};
254
255void
256fib_entry_src_interface_register (void)
257{
Neale Ranns3bab8f92019-12-04 06:11:00 +0000258 fib_entry_src_behaviour_register(FIB_SOURCE_BH_INTERFACE,
259 &interface_src_vft);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100260}