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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/adj/adj.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/fib/fib_walk.h>
21 * The 'DB' of all glean adjs.
22 * There is only one glean per-interface per-protocol, so this is a per-interface
25 static adj_index_t *adj_gleans[FIB_PROTOCOL_MAX];
28 adj_get_glean_node (fib_protocol_t proto)
31 case FIB_PROTOCOL_IP4:
32 return (ip4_glean_node.index);
33 case FIB_PROTOCOL_IP6:
34 return (ip6_glean_node.index);
35 case FIB_PROTOCOL_MPLS:
43 * adj_glean_add_or_lock
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.
50 adj_glean_add_or_lock (fib_protocol_t proto,
53 const ip46_address_t *nh_addr)
57 vec_validate_init_empty(adj_gleans[proto], sw_if_index, ADJ_INDEX_INVALID);
59 if (ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
61 adj = adj_alloc(proto);
63 adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN;
64 adj->ia_nh_proto = proto;
66 adj->ia_node_index = adj_get_glean_node(proto);
67 adj_gleans[proto][sw_if_index] = adj_get_index(adj);
71 adj->sub_type.glean.receive_addr = *nh_addr;
75 adj->sub_type.glean.receive_addr = zero_addr;
78 adj->rewrite_header.sw_if_index = sw_if_index;
79 adj->rewrite_header.data_bytes = 0;
80 adj->rewrite_header.max_l3_packet_bytes =
81 vnet_sw_interface_get_mtu(vnet_get_main(), sw_if_index,
82 vnet_link_to_mtu(linkt));
83 adj_lock(adj_get_index(adj));
85 vnet_update_adjacency_for_sw_interface(vnet_get_main(),
91 adj = adj_get(adj_gleans[proto][sw_if_index]);
92 adj_lock(adj_get_index(adj));
95 adj_delegate_adj_created(adj);
97 return (adj_get_index(adj));
101 * adj_glean_update_rewrite
104 adj_glean_update_rewrite (adj_index_t adj_index)
108 ASSERT(ADJ_INDEX_INVALID != adj_index);
110 adj = adj_get(adj_index);
112 vnet_rewrite_for_sw_interface(vnet_get_main(),
113 adj_fib_proto_2_nd(adj->ia_nh_proto),
114 adj->rewrite_header.sw_if_index,
116 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
117 &adj->rewrite_header,
118 sizeof (adj->rewrite_data));
122 adj_glean_get (fib_protocol_t proto,
125 if (sw_if_index < vec_len(adj_gleans[proto]))
127 return (adj_gleans[proto][sw_if_index]);
129 return (ADJ_INDEX_INVALID);
133 adj_glean_remove (fib_protocol_t proto,
136 ASSERT(sw_if_index < vec_len(adj_gleans[proto]));
138 adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID;
141 static clib_error_t *
142 adj_glean_interface_state_change (vnet_main_t * vnm,
147 * for each glean on the interface trigger a walk back to the children
149 fib_protocol_t proto;
153 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
155 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
156 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
159 adj = adj_get(adj_gleans[proto][sw_if_index]);
161 fib_node_back_walk_ctx_t bw_ctx = {
162 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
163 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
164 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
167 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
173 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change);
176 * @brief Invoked on each SW interface of a HW interface when the
177 * HW interface state changes
180 adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm,
184 adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg);
186 return (WALK_CONTINUE);
190 * @brief Registered callback for HW interface state changes
192 static clib_error_t *
193 adj_glean_hw_interface_state_change (vnet_main_t * vnm,
198 * walk SW interfaces on the HW
202 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
203 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
206 vnet_hw_interface_walk_sw(vnm, hw_if_index,
207 adj_nbr_hw_sw_interface_state_change,
213 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
214 adj_glean_hw_interface_state_change);
216 static clib_error_t *
217 adj_glean_interface_delete (vnet_main_t * vnm,
222 * for each glean on the interface trigger a walk back to the children
224 fib_protocol_t proto;
230 * not interested in interface additions. we will not back walk
231 * to resolve paths through newly added interfaces. Why? The control
232 * plane should have the brains to add interfaces first, then routes.
233 * So the case where there are paths with a interface that matches
234 * one just created is the case where the path resolved through an
235 * interface that was deleted, and still has not been removed. The
236 * new interface added, is NO GUARANTEE that the interface being
237 * added now, even though it may have the same sw_if_index, is the
238 * same interface that the path needs. So tough!
239 * If the control plane wants these routes to resolve it needs to
240 * remove and add them again.
245 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
247 if (sw_if_index >= vec_len(adj_gleans[proto]) ||
248 ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index])
251 adj = adj_get(adj_gleans[proto][sw_if_index]);
253 fib_node_back_walk_ctx_t bw_ctx = {
254 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
257 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
263 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete);
266 format_adj_glean (u8* s, va_list *ap)
268 index_t index = va_arg(*ap, index_t);
269 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
270 ip_adjacency_t * adj = adj_get(index);
272 s = format(s, "%U-glean: %U",
273 format_fib_protocol, adj->ia_nh_proto,
275 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
282 adj_dpo_lock (dpo_id_t *dpo)
284 adj_lock(dpo->dpoi_index);
287 adj_dpo_unlock (dpo_id_t *dpo)
289 adj_unlock(dpo->dpoi_index);
292 const static dpo_vft_t adj_glean_dpo_vft = {
293 .dv_lock = adj_dpo_lock,
294 .dv_unlock = adj_dpo_unlock,
295 .dv_format = format_adj_glean,
296 .dv_get_urpf = adj_dpo_get_urpf,
300 * @brief The per-protocol VLIB graph nodes that are assigned to a glean
303 * this means that these graph nodes are ones from which a glean is the
304 * parent object in the DPO-graph.
306 const static char* const glean_ip4_nodes[] =
311 const static char* const glean_ip6_nodes[] =
317 const static char* const * const glean_nodes[DPO_PROTO_NUM] =
319 [DPO_PROTO_IP4] = glean_ip4_nodes,
320 [DPO_PROTO_IP6] = glean_ip6_nodes,
321 [DPO_PROTO_MPLS] = NULL,
325 adj_glean_module_init (void)
327 dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes);