blob: 82d0a46eb1f2fcdf684051e7400b41d42ba9daf4 [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 Rannsc819fc62018-02-16 02:44:05 -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 Rannsc819fc62018-02-16 02:44:05 -080073 adj_lock(adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010074
Neale Rannsc819fc62018-02-16 02:44:05 -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 Rannsc819fc62018-02-16 02:44:05 -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 Rannsc819fc62018-02-16 02:44:05 -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 if (sw_if_index < vec_len(adj_gleans[proto]))
114 {
115 return (adj_gleans[proto][sw_if_index]);
116 }
117 return (ADJ_INDEX_INVALID);
118}
119
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100120void
121adj_glean_remove (fib_protocol_t proto,
122 u32 sw_if_index)
123{
124 ASSERT(sw_if_index < vec_len(adj_gleans[proto]));
125
126 adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID;
127}
128
129static clib_error_t *
130adj_glean_interface_state_change (vnet_main_t * vnm,
131 u32 sw_if_index,
132 u32 flags)
133{
134 /*
135 * for each glean on the interface trigger a walk back to the children
136 */
137 fib_protocol_t proto;
138 ip_adjacency_t *adj;
139
140
141 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
142 {
143 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
144 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
145 continue;
146
147 adj = adj_get(adj_gleans[proto][sw_if_index]);
148
149 fib_node_back_walk_ctx_t bw_ctx = {
150 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
151 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
152 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
153 };
154
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100155 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100156 }
157
158 return (NULL);
159}
160
161VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change);
162
Neale Ranns8b37b872016-11-21 12:25:22 +0000163/**
164 * @brief Invoked on each SW interface of a HW interface when the
165 * HW interface state changes
166 */
167static void
168adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
169 u32 sw_if_index,
170 void *arg)
171{
172 adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg);
173}
174
175/**
176 * @brief Registered callback for HW interface state changes
177 */
178static clib_error_t *
179adj_glean_hw_interface_state_change (vnet_main_t * vnm,
180 u32 hw_if_index,
181 u32 flags)
182{
183 /*
184 * walk SW interfaces on the HW
185 */
186 uword sw_flags;
187
188 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
189 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
190 0);
191
192 vnet_hw_interface_walk_sw(vnm, hw_if_index,
193 adj_nbr_hw_sw_interface_state_change,
194 (void*) sw_flags);
195
196 return (NULL);
197}
198
199VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
200 adj_glean_hw_interface_state_change);
201
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202static clib_error_t *
203adj_glean_interface_delete (vnet_main_t * vnm,
204 u32 sw_if_index,
205 u32 is_add)
206{
207 /*
208 * for each glean on the interface trigger a walk back to the children
209 */
210 fib_protocol_t proto;
211 ip_adjacency_t *adj;
212
213 if (is_add)
214 {
215 /*
216 * not interested in interface additions. we will not back walk
217 * to resolve paths through newly added interfaces. Why? The control
218 * plane should have the brains to add interfaces first, then routes.
219 * So the case where there are paths with a interface that matches
220 * one just created is the case where the path resolved through an
221 * interface that was deleted, and still has not been removed. The
222 * new interface added, is NO GUARANTEE that the interface being
223 * added now, even though it may have the same sw_if_index, is the
224 * same interface that the path needs. So tough!
225 * If the control plane wants these routes to resolve it needs to
226 * remove and add them again.
227 */
228 return (NULL);
229 }
230
231 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
232 {
233 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
234 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
235 continue;
236
237 adj = adj_get(adj_gleans[proto][sw_if_index]);
238
239 fib_node_back_walk_ctx_t bw_ctx = {
240 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
241 };
242
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100243 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100244 }
245
246 return (NULL);
247}
248
249VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
250
251u8*
252format_adj_glean (u8* s, va_list *ap)
253{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400254 index_t index = va_arg(*ap, index_t);
255 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100256 vnet_main_t * vnm = vnet_get_main();
257 ip_adjacency_t * adj = adj_get(index);
258
Neale Rannsc819fc62018-02-16 02:44:05 -0800259 s = format(s, "%U-glean: %U",
260 format_fib_protocol, adj->ia_nh_proto,
261 format_vnet_sw_interface_name,
262 vnm,
263 vnet_get_sw_interface(vnm,
264 adj->rewrite_header.sw_if_index));
265 s = format (s, " %U",
266 format_vnet_rewrite,
267 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
268
269 return (s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100270}
271
272
273static void
274adj_dpo_lock (dpo_id_t *dpo)
275{
276 adj_lock(dpo->dpoi_index);
277}
278static void
279adj_dpo_unlock (dpo_id_t *dpo)
280{
281 adj_unlock(dpo->dpoi_index);
282}
283
284const static dpo_vft_t adj_glean_dpo_vft = {
285 .dv_lock = adj_dpo_lock,
286 .dv_unlock = adj_dpo_unlock,
287 .dv_format = format_adj_glean,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700288 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100289};
290
291/**
292 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
293 * object.
294 *
295 * this means that these graph nodes are ones from which a glean is the
296 * parent object in the DPO-graph.
297 */
298const static char* const glean_ip4_nodes[] =
299{
300 "ip4-glean",
301 NULL,
302};
303const static char* const glean_ip6_nodes[] =
304{
305 "ip6-glean",
306 NULL,
307};
308
309const static char* const * const glean_nodes[DPO_PROTO_NUM] =
310{
311 [DPO_PROTO_IP4] = glean_ip4_nodes,
312 [DPO_PROTO_IP6] = glean_ip6_nodes,
313 [DPO_PROTO_MPLS] = NULL,
314};
315
316void
317adj_glean_module_init (void)
318{
319 dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
320}