blob: 09e6c0a0da9d33a78b161a112cd6db7d4414cc85 [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 <vnet/adj/adj.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010017#include <vnet/adj/adj_internal.h>
18#include <vnet/fib/fib_walk.h>
19
20/*
21 * The 'DB' of all glean adjs.
22 * There is only one glean per-interface per-protocol, so this is a per-interface
23 * vector
24 */
25static adj_index_t *adj_gleans[FIB_PROTOCOL_MAX];
26
27static inline vlib_node_registration_t*
28adj_get_glean_node (fib_protocol_t proto)
29{
30 switch (proto) {
31 case FIB_PROTOCOL_IP4:
32 return (&ip4_glean_node);
33 case FIB_PROTOCOL_IP6:
34 return (&ip6_glean_node);
35 case FIB_PROTOCOL_MPLS:
36 break;
37 }
38 ASSERT(0);
39 return (NULL);
40}
41
42/*
43 * adj_glean_add_or_lock
44 *
45 * The next_hop address here is used for source address selection in the DP.
46 * The glean adj is added to an interface's connected prefix, the next-hop
47 * passed here is the local prefix on the same interface.
48 */
49adj_index_t
50adj_glean_add_or_lock (fib_protocol_t proto,
51 u32 sw_if_index,
52 const ip46_address_t *nh_addr)
53{
54 ip_adjacency_t * adj;
55
56 vec_validate_init_empty(adj_gleans[proto], sw_if_index, ADJ_INDEX_INVALID);
57
58 if (ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
59 {
60 adj = adj_alloc(proto);
61
62 adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN;
63 adj->ia_nh_proto = proto;
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010064 adj_gleans[proto][sw_if_index] = adj_get_index(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065
66 if (NULL != nh_addr)
67 {
68 adj->sub_type.glean.receive_addr = *nh_addr;
69 }
70
Neale Ranns8b30e472018-02-12 08:36:11 -080071 adj->rewrite_header.sw_if_index = sw_if_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010072 adj->rewrite_header.data_bytes = 0;
Neale Ranns8b30e472018-02-12 08:36:11 -080073 adj_lock(adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074
Neale Ranns8b30e472018-02-12 08:36:11 -080075 vnet_update_adjacency_for_sw_interface(vnet_get_main(),
76 sw_if_index,
77 adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010078 }
79 else
80 {
81 adj = adj_get(adj_gleans[proto][sw_if_index]);
Neale Ranns8b30e472018-02-12 08:36:11 -080082 adj_lock(adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083 }
84
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010085 return (adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010086}
87
Neale Ranns8b30e472018-02-12 08:36:11 -080088/**
89 * adj_glean_update_rewrite
90 */
91void
92adj_glean_update_rewrite (adj_index_t adj_index)
93{
94 ip_adjacency_t *adj;
95
96 ASSERT(ADJ_INDEX_INVALID != adj_index);
97
98 adj = adj_get(adj_index);
99
100 vnet_rewrite_for_sw_interface(vnet_get_main(),
101 adj_fib_proto_2_nd(adj->ia_nh_proto),
102 adj->rewrite_header.sw_if_index,
103 adj_get_glean_node(adj->ia_nh_proto)->index,
104 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
105 &adj->rewrite_header,
106 sizeof (adj->rewrite_data));
107}
108
109adj_index_t
110adj_glean_get (fib_protocol_t proto,
111 u32 sw_if_index)
112{
113 return (adj_gleans[proto][sw_if_index]);
114}
115
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100116void
117adj_glean_remove (fib_protocol_t proto,
118 u32 sw_if_index)
119{
120 ASSERT(sw_if_index < vec_len(adj_gleans[proto]));
121
122 adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID;
123}
124
125static clib_error_t *
126adj_glean_interface_state_change (vnet_main_t * vnm,
127 u32 sw_if_index,
128 u32 flags)
129{
130 /*
131 * for each glean on the interface trigger a walk back to the children
132 */
133 fib_protocol_t proto;
134 ip_adjacency_t *adj;
135
136
137 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
138 {
139 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
140 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
141 continue;
142
143 adj = adj_get(adj_gleans[proto][sw_if_index]);
144
145 fib_node_back_walk_ctx_t bw_ctx = {
146 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
147 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
148 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
149 };
150
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100151 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100152 }
153
154 return (NULL);
155}
156
157VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change);
158
Neale Ranns8b37b872016-11-21 12:25:22 +0000159/**
160 * @brief Invoked on each SW interface of a HW interface when the
161 * HW interface state changes
162 */
163static void
164adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
165 u32 sw_if_index,
166 void *arg)
167{
168 adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg);
169}
170
171/**
172 * @brief Registered callback for HW interface state changes
173 */
174static clib_error_t *
175adj_glean_hw_interface_state_change (vnet_main_t * vnm,
176 u32 hw_if_index,
177 u32 flags)
178{
179 /*
180 * walk SW interfaces on the HW
181 */
182 uword sw_flags;
183
184 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
185 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
186 0);
187
188 vnet_hw_interface_walk_sw(vnm, hw_if_index,
189 adj_nbr_hw_sw_interface_state_change,
190 (void*) sw_flags);
191
192 return (NULL);
193}
194
195VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
196 adj_glean_hw_interface_state_change);
197
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100198static clib_error_t *
199adj_glean_interface_delete (vnet_main_t * vnm,
200 u32 sw_if_index,
201 u32 is_add)
202{
203 /*
204 * for each glean on the interface trigger a walk back to the children
205 */
206 fib_protocol_t proto;
207 ip_adjacency_t *adj;
208
209 if (is_add)
210 {
211 /*
212 * not interested in interface additions. we will not back walk
213 * to resolve paths through newly added interfaces. Why? The control
214 * plane should have the brains to add interfaces first, then routes.
215 * So the case where there are paths with a interface that matches
216 * one just created is the case where the path resolved through an
217 * interface that was deleted, and still has not been removed. The
218 * new interface added, is NO GUARANTEE that the interface being
219 * added now, even though it may have the same sw_if_index, is the
220 * same interface that the path needs. So tough!
221 * If the control plane wants these routes to resolve it needs to
222 * remove and add them again.
223 */
224 return (NULL);
225 }
226
227 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
228 {
229 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
230 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
231 continue;
232
233 adj = adj_get(adj_gleans[proto][sw_if_index]);
234
235 fib_node_back_walk_ctx_t bw_ctx = {
236 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
237 };
238
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100239 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100240 }
241
242 return (NULL);
243}
244
245VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
246
247u8*
248format_adj_glean (u8* s, va_list *ap)
249{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400250 index_t index = va_arg(*ap, index_t);
251 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100252 vnet_main_t * vnm = vnet_get_main();
253 ip_adjacency_t * adj = adj_get(index);
254
Neale Ranns8b30e472018-02-12 08:36:11 -0800255 s = format(s, "%U-glean: %U",
256 format_fib_protocol, adj->ia_nh_proto,
257 format_vnet_sw_interface_name,
258 vnm,
259 vnet_get_sw_interface(vnm,
260 adj->rewrite_header.sw_if_index));
261 s = format (s, " %U",
262 format_vnet_rewrite,
263 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
264
265 return (s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100266}
267
268
269static void
270adj_dpo_lock (dpo_id_t *dpo)
271{
272 adj_lock(dpo->dpoi_index);
273}
274static void
275adj_dpo_unlock (dpo_id_t *dpo)
276{
277 adj_unlock(dpo->dpoi_index);
278}
279
280const static dpo_vft_t adj_glean_dpo_vft = {
281 .dv_lock = adj_dpo_lock,
282 .dv_unlock = adj_dpo_unlock,
283 .dv_format = format_adj_glean,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700284 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100285};
286
287/**
288 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
289 * object.
290 *
291 * this means that these graph nodes are ones from which a glean is the
292 * parent object in the DPO-graph.
293 */
294const static char* const glean_ip4_nodes[] =
295{
296 "ip4-glean",
297 NULL,
298};
299const static char* const glean_ip6_nodes[] =
300{
301 "ip6-glean",
302 NULL,
303};
304
305const static char* const * const glean_nodes[DPO_PROTO_NUM] =
306{
307 [DPO_PROTO_IP4] = glean_ip4_nodes,
308 [DPO_PROTO_IP6] = glean_ip6_nodes,
309 [DPO_PROTO_MPLS] = NULL,
310};
311
312void
313adj_glean_module_init (void)
314{
315 dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
316}