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_mcast.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/fib/fib_walk.h>
19 #include <vnet/ip/ip.h>
22 * The 'DB' of all mcast adjs.
23 * There is only one mcast per-interface per-protocol, so this is a per-interface
26 static adj_index_t *adj_mcasts[FIB_PROTOCOL_MAX];
29 adj_get_mcast_node (fib_protocol_t proto)
32 case FIB_PROTOCOL_IP4:
33 return (ip4_rewrite_mcast_node.index);
34 case FIB_PROTOCOL_IP6:
35 return (ip6_rewrite_mcast_node.index);
36 case FIB_PROTOCOL_MPLS:
44 * adj_mcast_add_or_lock
46 * The next_hop address here is used for source address selection in the DP.
47 * The mcast adj is added to an interface's connected prefix, the next-hop
48 * passed here is the local prefix on the same interface.
51 adj_mcast_add_or_lock (fib_protocol_t proto,
52 vnet_link_t link_type,
57 vec_validate_init_empty(adj_mcasts[proto], sw_if_index, ADJ_INDEX_INVALID);
59 if (ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
63 vnm = vnet_get_main();
64 adj = adj_alloc(proto);
66 adj->lookup_next_index = IP_LOOKUP_NEXT_MCAST;
67 adj->ia_nh_proto = proto;
68 adj->ia_link = link_type;
69 adj_mcasts[proto][sw_if_index] = adj_get_index(adj);
70 adj_lock(adj_get_index(adj));
72 vnet_rewrite_init(vnm, sw_if_index, link_type,
73 adj_get_mcast_node(proto),
74 vnet_tx_node_index_for_sw_interface(vnm, sw_if_index),
75 &adj->rewrite_header);
78 * we need a rewrite where the destination IP address is converted
79 * to the appropriate link-layer address. This is interface specific.
80 * So ask the interface to do it.
82 vnet_update_adjacency_for_sw_interface(vnm, sw_if_index,
87 adj = adj_get(adj_mcasts[proto][sw_if_index]);
88 adj_lock(adj_get_index(adj));
91 return (adj_get_index(adj));
95 * adj_mcast_update_rewrite
97 * Update the adjacency's rewrite string. A NULL string implies the
98 * rewirte is reset (i.e. when ARP/ND etnry is gone).
99 * NB: the adj being updated may be handling traffic in the DP.
102 adj_mcast_update_rewrite (adj_index_t adj_index,
108 ASSERT(ADJ_INDEX_INVALID != adj_index);
110 adj = adj_get(adj_index);
113 * update the adj's rewrite string and build the arc
114 * from the rewrite node to the interface's TX node
116 adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_MCAST,
117 adj_get_mcast_node(adj->ia_nh_proto),
118 vnet_tx_node_index_for_sw_interface(
120 adj->rewrite_header.sw_if_index),
123 * set the offset corresponding to the mcast IP address rewrite
125 adj->rewrite_header.dst_mcast_offset = offset;
129 * adj_mcast_midchain_update_rewrite
131 * Update the adjacency's rewrite string. A NULL string implies the
132 * rewirte is reset (i.e. when ARP/ND etnry is gone).
133 * NB: the adj being updated may be handling traffic in the DP.
136 adj_mcast_midchain_update_rewrite (adj_index_t adj_index,
137 adj_midchain_fixup_t fixup,
138 const void *fixup_data,
146 ASSERT(ADJ_INDEX_INVALID != adj_index);
148 adj = adj_get(adj_index);
151 * one time only update. since we don't support chainging the tunnel
152 * src,dst, this is all we need.
154 ASSERT(adj->lookup_next_index == IP_LOOKUP_NEXT_MCAST);
156 * tunnels can always provide a rewrite.
158 ASSERT(NULL != rewrite);
160 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
163 * update the adj's rewrite string and build the arc
164 * from the rewrite node to the interface's TX node
166 adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_MCAST_MIDCHAIN,
167 adj_get_mcast_node(adj->ia_nh_proto),
168 vnet_tx_node_index_for_sw_interface(
170 adj->rewrite_header.sw_if_index),
173 adj->rewrite_header.dst_mcast_offset = offset;
177 adj_mcast_remove (fib_protocol_t proto,
180 ASSERT(sw_if_index < vec_len(adj_mcasts[proto]));
182 adj_mcasts[proto][sw_if_index] = ADJ_INDEX_INVALID;
185 static clib_error_t *
186 adj_mcast_interface_state_change (vnet_main_t * vnm,
191 * for each mcast on the interface trigger a walk back to the children
193 fib_protocol_t proto;
197 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
199 if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
200 ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
203 adj = adj_get(adj_mcasts[proto][sw_if_index]);
205 fib_node_back_walk_ctx_t bw_ctx = {
206 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
207 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
208 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
211 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
217 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_mcast_interface_state_change);
220 * @brief Invoked on each SW interface of a HW interface when the
221 * HW interface state changes
224 adj_mcast_hw_sw_interface_state_change (vnet_main_t * vnm,
228 adj_mcast_interface_state_change(vnm, sw_if_index, (uword) arg);
230 return (WALK_CONTINUE);
234 * @brief Registered callback for HW interface state changes
236 static clib_error_t *
237 adj_mcast_hw_interface_state_change (vnet_main_t * vnm,
242 * walk SW interfaces on the HW
246 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
247 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
250 vnet_hw_interface_walk_sw(vnm, hw_if_index,
251 adj_mcast_hw_sw_interface_state_change,
257 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
258 adj_mcast_hw_interface_state_change);
260 static clib_error_t *
261 adj_mcast_interface_delete (vnet_main_t * vnm,
266 * for each mcast on the interface trigger a walk back to the children
268 fib_protocol_t proto;
274 * not interested in interface additions. we will not back walk
275 * to resolve paths through newly added interfaces. Why? The control
276 * plane should have the brains to add interfaces first, then routes.
277 * So the case where there are paths with a interface that matches
278 * one just created is the case where the path resolved through an
279 * interface that was deleted, and still has not been removed. The
280 * new interface added, is NO GUARANTEE that the interface being
281 * added now, even though it may have the same sw_if_index, is the
282 * same interface that the path needs. So tough!
283 * If the control plane wants these routes to resolve it needs to
284 * remove and add them again.
289 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
291 if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
292 ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
295 adj = adj_get(adj_mcasts[proto][sw_if_index]);
297 fib_node_back_walk_ctx_t bw_ctx = {
298 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
301 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
307 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_mcast_interface_delete);
310 * @brief Walk the multicast Adjacencies on a given interface
313 adj_mcast_walk (u32 sw_if_index,
314 fib_protocol_t proto,
318 if (vec_len(adj_mcasts[proto]) > sw_if_index)
320 if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
322 cb(adj_mcasts[proto][sw_if_index], ctx);
328 format_adj_mcast (u8* s, va_list *ap)
330 index_t index = va_arg(*ap, index_t);
331 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
332 ip_adjacency_t * adj = adj_get(index);
334 s = format(s, "%U-mcast: ",
335 format_fib_protocol, adj->ia_nh_proto);
336 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
337 s = format(s, "[features] ");
340 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
346 format_adj_mcast_midchain (u8* s, va_list *ap)
348 index_t index = va_arg(*ap, index_t);
349 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
350 ip_adjacency_t * adj = adj_get(index);
352 s = format(s, "%U-mcast-midchain: ",
353 format_fib_protocol, adj->ia_nh_proto);
356 &adj->rewrite_header,
357 sizeof (adj->rewrite_data), 0);
358 s = format (s, "\n%Ustacked-on:\n%U%U",
359 format_white_space, indent,
360 format_white_space, indent+2,
361 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
368 adj_dpo_lock (dpo_id_t *dpo)
370 adj_lock(dpo->dpoi_index);
373 adj_dpo_unlock (dpo_id_t *dpo)
375 adj_unlock(dpo->dpoi_index);
378 const static dpo_vft_t adj_mcast_dpo_vft = {
379 .dv_lock = adj_dpo_lock,
380 .dv_unlock = adj_dpo_unlock,
381 .dv_format = format_adj_mcast,
382 .dv_get_urpf = adj_dpo_get_urpf,
384 const static dpo_vft_t adj_mcast_midchain_dpo_vft = {
385 .dv_lock = adj_dpo_lock,
386 .dv_unlock = adj_dpo_unlock,
387 .dv_format = format_adj_mcast_midchain,
388 .dv_get_urpf = adj_dpo_get_urpf,
392 * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
395 * this means that these graph nodes are ones from which a mcast is the
396 * parent object in the DPO-graph.
398 const static char* const adj_mcast_ip4_nodes[] =
403 const static char* const adj_mcast_ip6_nodes[] =
409 const static char* const * const adj_mcast_nodes[DPO_PROTO_NUM] =
411 [DPO_PROTO_IP4] = adj_mcast_ip4_nodes,
412 [DPO_PROTO_IP6] = adj_mcast_ip6_nodes,
413 [DPO_PROTO_MPLS] = NULL,
417 * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
420 * this means that these graph nodes are ones from which a mcast is the
421 * parent object in the DPO-graph.
423 const static char* const adj_mcast_midchain_ip4_nodes[] =
425 "ip4-mcast-midchain",
428 const static char* const adj_mcast_midchain_ip6_nodes[] =
430 "ip6-mcast-midchain",
434 const static char* const * const adj_mcast_midchain_nodes[DPO_PROTO_NUM] =
436 [DPO_PROTO_IP4] = adj_mcast_midchain_ip4_nodes,
437 [DPO_PROTO_IP6] = adj_mcast_midchain_ip6_nodes,
438 [DPO_PROTO_MPLS] = NULL,
442 * @brief Return the size of the adj DB.
443 * This is only for testing purposes so an efficient implementation is not needed
446 adj_mcast_db_size (void)
448 u32 n_adjs, sw_if_index;
449 fib_protocol_t proto;
452 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
454 for (sw_if_index = 0;
455 sw_if_index < vec_len(adj_mcasts[proto]);
458 if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
469 adj_mcast_module_init (void)
471 dpo_register(DPO_ADJACENCY_MCAST,
474 dpo_register(DPO_ADJACENCY_MCAST_MIDCHAIN,
475 &adj_mcast_midchain_dpo_vft,
476 adj_mcast_midchain_nodes);