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,
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,
109 ASSERT(ADJ_INDEX_INVALID != adj_index);
111 adj = adj_get(adj_index);
114 * update the adj's rewrite string and build the arc
115 * from the rewrite node to the interface's TX node
117 adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_MCAST,
118 adj_get_mcast_node(adj->ia_nh_proto),
119 vnet_tx_node_index_for_sw_interface(
121 adj->rewrite_header.sw_if_index),
124 * set the fields corresponding to the mcast IP address rewrite
125 * The mask must be stored in network byte order, since the packet's
126 * IP address will also be in network order.
128 adj->rewrite_header.dst_mcast_offset = offset;
129 adj->rewrite_header.dst_mcast_mask = clib_host_to_net_u32(mask);
133 * adj_mcast_midchain_update_rewrite
135 * Update the adjacency's rewrite string. A NULL string implies the
136 * rewirte is reset (i.e. when ARP/ND etnry is gone).
137 * NB: the adj being updated may be handling traffic in the DP.
140 adj_mcast_midchain_update_rewrite (adj_index_t adj_index,
141 adj_midchain_fixup_t fixup,
142 const void *fixup_data,
150 ASSERT(ADJ_INDEX_INVALID != adj_index);
152 adj = adj_get(adj_index);
155 * one time only update. since we don't support chainging the tunnel
156 * src,dst, this is all we need.
158 ASSERT(adj->lookup_next_index == IP_LOOKUP_NEXT_MCAST);
160 * tunnels can always provide a rewrite.
162 ASSERT(NULL != rewrite);
164 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
167 * update the adj's rewrite string and build the arc
168 * from the rewrite node to the interface's TX node
170 adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_MCAST_MIDCHAIN,
171 adj_get_mcast_node(adj->ia_nh_proto),
172 vnet_tx_node_index_for_sw_interface(
174 adj->rewrite_header.sw_if_index),
178 * set the fields corresponding to the mcast IP address rewrite
179 * The mask must be stored in network byte order, since the packet's
180 * IP address will also be in network order.
182 adj->rewrite_header.dst_mcast_offset = offset;
183 adj->rewrite_header.dst_mcast_mask = clib_host_to_net_u32(mask);
187 adj_mcast_remove (fib_protocol_t proto,
190 ASSERT(sw_if_index < vec_len(adj_mcasts[proto]));
192 adj_mcasts[proto][sw_if_index] = ADJ_INDEX_INVALID;
195 static clib_error_t *
196 adj_mcast_interface_state_change (vnet_main_t * vnm,
201 * for each mcast on the interface trigger a walk back to the children
203 fib_protocol_t proto;
207 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
209 if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
210 ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
213 adj = adj_get(adj_mcasts[proto][sw_if_index]);
215 fib_node_back_walk_ctx_t bw_ctx = {
216 .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
217 FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
218 FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
221 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
227 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_mcast_interface_state_change);
230 * @brief Invoked on each SW interface of a HW interface when the
231 * HW interface state changes
234 adj_mcast_hw_sw_interface_state_change (vnet_main_t * vnm,
238 adj_mcast_interface_state_change(vnm, sw_if_index, (uword) arg);
242 * @brief Registered callback for HW interface state changes
244 static clib_error_t *
245 adj_mcast_hw_interface_state_change (vnet_main_t * vnm,
250 * walk SW interfaces on the HW
254 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
255 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
258 vnet_hw_interface_walk_sw(vnm, hw_if_index,
259 adj_mcast_hw_sw_interface_state_change,
265 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
266 adj_mcast_hw_interface_state_change);
268 static clib_error_t *
269 adj_mcast_interface_delete (vnet_main_t * vnm,
274 * for each mcast on the interface trigger a walk back to the children
276 fib_protocol_t proto;
282 * not interested in interface additions. we will not back walk
283 * to resolve paths through newly added interfaces. Why? The control
284 * plane should have the brains to add interfaces first, then routes.
285 * So the case where there are paths with a interface that matches
286 * one just created is the case where the path resolved through an
287 * interface that was deleted, and still has not been removed. The
288 * new interface added, is NO GUARANTEE that the interface being
289 * added now, even though it may have the same sw_if_index, is the
290 * same interface that the path needs. So tough!
291 * If the control plane wants these routes to resolve it needs to
292 * remove and add them again.
297 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
299 if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
300 ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
303 adj = adj_get(adj_mcasts[proto][sw_if_index]);
305 fib_node_back_walk_ctx_t bw_ctx = {
306 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
309 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
315 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_mcast_interface_delete);
318 * @brief Walk the multicast Adjacencies on a given interface
321 adj_mcast_walk (u32 sw_if_index,
322 fib_protocol_t proto,
326 if (vec_len(adj_mcasts[proto]) > sw_if_index)
328 if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
330 cb(adj_mcasts[proto][sw_if_index], ctx);
336 format_adj_mcast (u8* s, va_list *ap)
338 index_t index = va_arg(*ap, index_t);
339 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
340 ip_adjacency_t * adj = adj_get(index);
342 s = format(s, "%U-mcast: ",
343 format_fib_protocol, adj->ia_nh_proto);
344 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
345 s = format(s, "[features] ");
348 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
354 format_adj_mcast_midchain (u8* s, va_list *ap)
356 index_t index = va_arg(*ap, index_t);
357 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
358 ip_adjacency_t * adj = adj_get(index);
360 s = format(s, "%U-mcast-midchain: ",
361 format_fib_protocol, adj->ia_nh_proto);
364 &adj->rewrite_header,
365 sizeof (adj->rewrite_data), 0);
366 s = format (s, "\n%Ustacked-on:\n%U%U",
367 format_white_space, indent,
368 format_white_space, indent+2,
369 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
376 adj_dpo_lock (dpo_id_t *dpo)
378 adj_lock(dpo->dpoi_index);
381 adj_dpo_unlock (dpo_id_t *dpo)
383 adj_unlock(dpo->dpoi_index);
386 const static dpo_vft_t adj_mcast_dpo_vft = {
387 .dv_lock = adj_dpo_lock,
388 .dv_unlock = adj_dpo_unlock,
389 .dv_format = format_adj_mcast,
390 .dv_get_urpf = adj_dpo_get_urpf,
392 const static dpo_vft_t adj_mcast_midchain_dpo_vft = {
393 .dv_lock = adj_dpo_lock,
394 .dv_unlock = adj_dpo_unlock,
395 .dv_format = format_adj_mcast_midchain,
396 .dv_get_urpf = adj_dpo_get_urpf,
400 * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
403 * this means that these graph nodes are ones from which a mcast is the
404 * parent object in the DPO-graph.
406 const static char* const adj_mcast_ip4_nodes[] =
411 const static char* const adj_mcast_ip6_nodes[] =
417 const static char* const * const adj_mcast_nodes[DPO_PROTO_NUM] =
419 [DPO_PROTO_IP4] = adj_mcast_ip4_nodes,
420 [DPO_PROTO_IP6] = adj_mcast_ip6_nodes,
421 [DPO_PROTO_MPLS] = NULL,
425 * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
428 * this means that these graph nodes are ones from which a mcast is the
429 * parent object in the DPO-graph.
431 const static char* const adj_mcast_midchain_ip4_nodes[] =
433 "ip4-mcast-midchain",
436 const static char* const adj_mcast_midchain_ip6_nodes[] =
438 "ip6-mcast-midchain",
442 const static char* const * const adj_mcast_midchain_nodes[DPO_PROTO_NUM] =
444 [DPO_PROTO_IP4] = adj_mcast_midchain_ip4_nodes,
445 [DPO_PROTO_IP6] = adj_mcast_midchain_ip6_nodes,
446 [DPO_PROTO_MPLS] = NULL,
450 * @brief Return the size of the adj DB.
451 * This is only for testing purposes so an efficient implementation is not needed
454 adj_mcast_db_size (void)
456 u32 n_adjs, sw_if_index;
457 fib_protocol_t proto;
460 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
462 for (sw_if_index = 0;
463 sw_if_index < vec_len(adj_mcasts[proto]);
466 if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
477 adj_mcast_module_init (void)
479 dpo_register(DPO_ADJACENCY_MCAST,
482 dpo_register(DPO_ADJACENCY_MCAST_MIDCHAIN,
483 &adj_mcast_midchain_dpo_vft,
484 adj_mcast_midchain_nodes);