blob: 8d86e2a9f00c5095bc30cd9d812e592cdeb4672f [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
71 adj->rewrite_header.data_bytes = 0;
72
73 vnet_rewrite_for_sw_interface(vnet_get_main(),
74 adj_fib_proto_2_nd(proto),
75 sw_if_index,
76 adj_get_glean_node(proto)->index,
77 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
78 &adj->rewrite_header,
79 sizeof (adj->rewrite_data));
80 }
81 else
82 {
83 adj = adj_get(adj_gleans[proto][sw_if_index]);
84 }
85
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010086 adj_lock(adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010087
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010088 return (adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010089}
90
91void
92adj_glean_remove (fib_protocol_t proto,
93 u32 sw_if_index)
94{
95 ASSERT(sw_if_index < vec_len(adj_gleans[proto]));
96
97 adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID;
98}
99
100static clib_error_t *
101adj_glean_interface_state_change (vnet_main_t * vnm,
102 u32 sw_if_index,
103 u32 flags)
104{
105 /*
106 * for each glean on the interface trigger a walk back to the children
107 */
108 fib_protocol_t proto;
109 ip_adjacency_t *adj;
110
111
112 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
113 {
114 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
115 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
116 continue;
117
118 adj = adj_get(adj_gleans[proto][sw_if_index]);
119
120 fib_node_back_walk_ctx_t bw_ctx = {
121 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
122 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
123 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
124 };
125
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100126 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100127 }
128
129 return (NULL);
130}
131
132VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change);
133
Neale Ranns8b37b872016-11-21 12:25:22 +0000134/**
135 * @brief Invoked on each SW interface of a HW interface when the
136 * HW interface state changes
137 */
138static void
139adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
140 u32 sw_if_index,
141 void *arg)
142{
143 adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg);
144}
145
146/**
147 * @brief Registered callback for HW interface state changes
148 */
149static clib_error_t *
150adj_glean_hw_interface_state_change (vnet_main_t * vnm,
151 u32 hw_if_index,
152 u32 flags)
153{
154 /*
155 * walk SW interfaces on the HW
156 */
157 uword sw_flags;
158
159 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
160 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
161 0);
162
163 vnet_hw_interface_walk_sw(vnm, hw_if_index,
164 adj_nbr_hw_sw_interface_state_change,
165 (void*) sw_flags);
166
167 return (NULL);
168}
169
170VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
171 adj_glean_hw_interface_state_change);
172
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100173static clib_error_t *
174adj_glean_interface_delete (vnet_main_t * vnm,
175 u32 sw_if_index,
176 u32 is_add)
177{
178 /*
179 * for each glean on the interface trigger a walk back to the children
180 */
181 fib_protocol_t proto;
182 ip_adjacency_t *adj;
183
184 if (is_add)
185 {
186 /*
187 * not interested in interface additions. we will not back walk
188 * to resolve paths through newly added interfaces. Why? The control
189 * plane should have the brains to add interfaces first, then routes.
190 * So the case where there are paths with a interface that matches
191 * one just created is the case where the path resolved through an
192 * interface that was deleted, and still has not been removed. The
193 * new interface added, is NO GUARANTEE that the interface being
194 * added now, even though it may have the same sw_if_index, is the
195 * same interface that the path needs. So tough!
196 * If the control plane wants these routes to resolve it needs to
197 * remove and add them again.
198 */
199 return (NULL);
200 }
201
202 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
203 {
204 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
205 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
206 continue;
207
208 adj = adj_get(adj_gleans[proto][sw_if_index]);
209
210 fib_node_back_walk_ctx_t bw_ctx = {
211 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
212 };
213
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100214 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100215 }
216
217 return (NULL);
218}
219
220VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
221
222u8*
223format_adj_glean (u8* s, va_list *ap)
224{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400225 index_t index = va_arg(*ap, index_t);
226 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100227 vnet_main_t * vnm = vnet_get_main();
228 ip_adjacency_t * adj = adj_get(index);
229
Neale Rannsb80c5362016-10-08 13:03:40 +0100230 return (format(s, "%U-glean: %U",
231 format_fib_protocol, adj->ia_nh_proto,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100232 format_vnet_sw_interface_name,
233 vnm,
234 vnet_get_sw_interface(vnm,
235 adj->rewrite_header.sw_if_index)));
236}
237
238
239static void
240adj_dpo_lock (dpo_id_t *dpo)
241{
242 adj_lock(dpo->dpoi_index);
243}
244static void
245adj_dpo_unlock (dpo_id_t *dpo)
246{
247 adj_unlock(dpo->dpoi_index);
248}
249
250const static dpo_vft_t adj_glean_dpo_vft = {
251 .dv_lock = adj_dpo_lock,
252 .dv_unlock = adj_dpo_unlock,
253 .dv_format = format_adj_glean,
254};
255
256/**
257 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
258 * object.
259 *
260 * this means that these graph nodes are ones from which a glean is the
261 * parent object in the DPO-graph.
262 */
263const static char* const glean_ip4_nodes[] =
264{
265 "ip4-glean",
266 NULL,
267};
268const static char* const glean_ip6_nodes[] =
269{
270 "ip6-glean",
271 NULL,
272};
273
274const static char* const * const glean_nodes[DPO_PROTO_NUM] =
275{
276 [DPO_PROTO_IP4] = glean_ip4_nodes,
277 [DPO_PROTO_IP6] = glean_ip6_nodes,
278 [DPO_PROTO_MPLS] = NULL,
279};
280
281void
282adj_glean_module_init (void)
283{
284 dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);
285}