blob: 74881d7f67c9bb9168c3edad522907e77bfad988 [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,
Ole Troan6ee40512018-02-12 18:14:39 +010051 vnet_link_t linkt,
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052 u32 sw_if_index,
53 const ip46_address_t *nh_addr)
54{
55 ip_adjacency_t * adj;
56
57 vec_validate_init_empty(adj_gleans[proto], sw_if_index, ADJ_INDEX_INVALID);
58
59 if (ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
60 {
61 adj = adj_alloc(proto);
62
63 adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN;
64 adj->ia_nh_proto = proto;
Ole Troan6ee40512018-02-12 18:14:39 +010065 adj->ia_link = linkt;
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010066 adj_gleans[proto][sw_if_index] = adj_get_index(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010067
68 if (NULL != nh_addr)
69 {
70 adj->sub_type.glean.receive_addr = *nh_addr;
71 }
Ole Troan6ee40512018-02-12 18:14:39 +010072 else
73 {
74 adj->sub_type.glean.receive_addr = zero_addr;
75 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +010076
Neale Rannsc819fc62018-02-16 02:44:05 -080077 adj->rewrite_header.sw_if_index = sw_if_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010078 adj->rewrite_header.data_bytes = 0;
Ole Troan6ee40512018-02-12 18:14:39 +010079 adj->rewrite_header.max_l3_packet_bytes =
80 vnet_sw_interface_get_mtu(vnet_get_main(), sw_if_index, VLIB_TX);
81
Neale Rannsc819fc62018-02-16 02:44:05 -080082 adj_lock(adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010083
Neale Rannsc819fc62018-02-16 02:44:05 -080084 vnet_update_adjacency_for_sw_interface(vnet_get_main(),
85 sw_if_index,
86 adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010087 }
88 else
89 {
90 adj = adj_get(adj_gleans[proto][sw_if_index]);
Neale Rannsc819fc62018-02-16 02:44:05 -080091 adj_lock(adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010092 }
93
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010094 return (adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010095}
96
Neale Rannsc819fc62018-02-16 02:44:05 -080097/**
98 * adj_glean_update_rewrite
99 */
100void
101adj_glean_update_rewrite (adj_index_t adj_index)
102{
103 ip_adjacency_t *adj;
104
105 ASSERT(ADJ_INDEX_INVALID != adj_index);
106
107 adj = adj_get(adj_index);
108
109 vnet_rewrite_for_sw_interface(vnet_get_main(),
110 adj_fib_proto_2_nd(adj->ia_nh_proto),
111 adj->rewrite_header.sw_if_index,
112 adj_get_glean_node(adj->ia_nh_proto)->index,
113 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
114 &adj->rewrite_header,
115 sizeof (adj->rewrite_data));
116}
117
118adj_index_t
119adj_glean_get (fib_protocol_t proto,
120 u32 sw_if_index)
121{
122 if (sw_if_index < vec_len(adj_gleans[proto]))
123 {
124 return (adj_gleans[proto][sw_if_index]);
125 }
126 return (ADJ_INDEX_INVALID);
127}
128
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100129void
130adj_glean_remove (fib_protocol_t proto,
131 u32 sw_if_index)
132{
133 ASSERT(sw_if_index < vec_len(adj_gleans[proto]));
134
135 adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID;
136}
137
138static clib_error_t *
139adj_glean_interface_state_change (vnet_main_t * vnm,
140 u32 sw_if_index,
141 u32 flags)
142{
143 /*
144 * for each glean on the interface trigger a walk back to the children
145 */
146 fib_protocol_t proto;
147 ip_adjacency_t *adj;
148
149
150 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
151 {
152 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
153 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
154 continue;
155
156 adj = adj_get(adj_gleans[proto][sw_if_index]);
157
158 fib_node_back_walk_ctx_t bw_ctx = {
159 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
160 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
161 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
162 };
163
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100164 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100165 }
166
167 return (NULL);
168}
169
170VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change);
171
Neale Ranns8b37b872016-11-21 12:25:22 +0000172/**
173 * @brief Invoked on each SW interface of a HW interface when the
174 * HW interface state changes
175 */
Neale Ranns0053de62018-05-22 08:40:52 -0700176static walk_rc_t
Neale Ranns8b37b872016-11-21 12:25:22 +0000177adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
178 u32 sw_if_index,
179 void *arg)
180{
181 adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg);
Neale Ranns0053de62018-05-22 08:40:52 -0700182
183 return (WALK_CONTINUE);
Neale Ranns8b37b872016-11-21 12:25:22 +0000184}
185
186/**
187 * @brief Registered callback for HW interface state changes
188 */
189static clib_error_t *
190adj_glean_hw_interface_state_change (vnet_main_t * vnm,
191 u32 hw_if_index,
192 u32 flags)
193{
194 /*
195 * walk SW interfaces on the HW
196 */
197 uword sw_flags;
198
199 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
200 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
201 0);
202
203 vnet_hw_interface_walk_sw(vnm, hw_if_index,
204 adj_nbr_hw_sw_interface_state_change,
205 (void*) sw_flags);
206
207 return (NULL);
208}
209
210VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
211 adj_glean_hw_interface_state_change);
212
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100213static clib_error_t *
214adj_glean_interface_delete (vnet_main_t * vnm,
215 u32 sw_if_index,
216 u32 is_add)
217{
218 /*
219 * for each glean on the interface trigger a walk back to the children
220 */
221 fib_protocol_t proto;
222 ip_adjacency_t *adj;
223
224 if (is_add)
225 {
226 /*
227 * not interested in interface additions. we will not back walk
228 * to resolve paths through newly added interfaces. Why? The control
229 * plane should have the brains to add interfaces first, then routes.
230 * So the case where there are paths with a interface that matches
231 * one just created is the case where the path resolved through an
232 * interface that was deleted, and still has not been removed. The
233 * new interface added, is NO GUARANTEE that the interface being
234 * added now, even though it may have the same sw_if_index, is the
235 * same interface that the path needs. So tough!
236 * If the control plane wants these routes to resolve it needs to
237 * remove and add them again.
238 */
239 return (NULL);
240 }
241
242 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
243 {
244 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
245 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
246 continue;
247
248 adj = adj_get(adj_gleans[proto][sw_if_index]);
249
250 fib_node_back_walk_ctx_t bw_ctx = {
251 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
252 };
253
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100254 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100255 }
256
257 return (NULL);
258}
259
260VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
261
262u8*
263format_adj_glean (u8* s, va_list *ap)
264{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400265 index_t index = va_arg(*ap, index_t);
266 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100267 ip_adjacency_t * adj = adj_get(index);
268
Neale Rannsc819fc62018-02-16 02:44:05 -0800269 s = format(s, "%U-glean: %U",
270 format_fib_protocol, adj->ia_nh_proto,
Neale Rannsc819fc62018-02-16 02:44:05 -0800271 format_vnet_rewrite,
272 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
273
274 return (s);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100275}
276
277
278static void
279adj_dpo_lock (dpo_id_t *dpo)
280{
281 adj_lock(dpo->dpoi_index);
282}
283static void
284adj_dpo_unlock (dpo_id_t *dpo)
285{
286 adj_unlock(dpo->dpoi_index);
287}
288
289const static dpo_vft_t adj_glean_dpo_vft = {
290 .dv_lock = adj_dpo_lock,
291 .dv_unlock = adj_dpo_unlock,
292 .dv_format = format_adj_glean,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700293 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100294};
295
296/**
297 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
298 * object.
299 *
300 * this means that these graph nodes are ones from which a glean is the
301 * parent object in the DPO-graph.
302 */
303const static char* const glean_ip4_nodes[] =
304{
305 "ip4-glean",
306 NULL,
307};
308const static char* const glean_ip6_nodes[] =
309{
310 "ip6-glean",
311 NULL,
312};
313
314const static char* const * const glean_nodes[DPO_PROTO_NUM] =
315{
316 [DPO_PROTO_IP4] = glean_ip4_nodes,
317 [DPO_PROTO_IP6] = glean_ip6_nodes,
318 [DPO_PROTO_MPLS] = NULL,
319};
320
321void
322adj_glean_module_init (void)
323{
324 dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
325}