blob: c94d28e8713f31e247ae42f99c271c818a5a3abc [file] [log] [blame]
Neale Ranns32e1c012016-11-22 17:07:28 +00001/*
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
Neale Ranns0f26c5a2017-03-01 15:12:11 -080016#include <vnet/adj/adj_mcast.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000017#include <vnet/adj/adj_internal.h>
18#include <vnet/fib/fib_walk.h>
19#include <vnet/ip/ip.h>
20
21/*
22 * The 'DB' of all mcast adjs.
23 * There is only one mcast per-interface per-protocol, so this is a per-interface
24 * vector
25 */
26static adj_index_t *adj_mcasts[FIB_PROTOCOL_MAX];
27
28static u32
29adj_get_mcast_node (fib_protocol_t proto)
30{
31 switch (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:
37 break;
38 }
39 ASSERT(0);
40 return (0);
41}
42
43/*
44 * adj_mcast_add_or_lock
45 *
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.
49 */
50adj_index_t
51adj_mcast_add_or_lock (fib_protocol_t proto,
52 vnet_link_t link_type,
53 u32 sw_if_index)
54{
55 ip_adjacency_t * adj;
56
57 vec_validate_init_empty(adj_mcasts[proto], sw_if_index, ADJ_INDEX_INVALID);
58
59 if (ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
60 {
61 vnet_main_t *vnm;
62
63 vnm = vnet_get_main();
64 adj = adj_alloc(proto);
65
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));
71
Ole Troand7231612018-06-07 10:17:57 +020072 vnet_rewrite_init(vnm, sw_if_index, link_type,
Neale Ranns32e1c012016-11-22 17:07:28 +000073 adj_get_mcast_node(proto),
74 vnet_tx_node_index_for_sw_interface(vnm, sw_if_index),
75 &adj->rewrite_header);
76
77 /*
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.
81 */
82 vnet_update_adjacency_for_sw_interface(vnm, sw_if_index,
83 adj_get_index(adj));
84 }
85 else
86 {
87 adj = adj_get(adj_mcasts[proto][sw_if_index]);
88 adj_lock(adj_get_index(adj));
89 }
90
91 return (adj_get_index(adj));
92}
93
94/**
95 * adj_mcast_update_rewrite
96 *
97 * Update the adjacency's rewrite string. A NULL string implies the
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070098 * rewrite is reset (i.e. when ARP/ND entry is gone).
Neale Ranns32e1c012016-11-22 17:07:28 +000099 * NB: the adj being updated may be handling traffic in the DP.
100 */
101void
102adj_mcast_update_rewrite (adj_index_t adj_index,
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700103 u8 *rewrite,
Neale Ranns889fe942017-06-01 05:43:19 -0400104 u8 offset)
Neale Ranns32e1c012016-11-22 17:07:28 +0000105{
106 ip_adjacency_t *adj;
107
108 ASSERT(ADJ_INDEX_INVALID != adj_index);
109
110 adj = adj_get(adj_index);
111
112 /*
113 * update the adj's rewrite string and build the arc
114 * from the rewrite node to the interface's TX node
115 */
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(
119 vnet_get_main(),
120 adj->rewrite_header.sw_if_index),
121 rewrite);
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700122 /*
Neale Ranns889fe942017-06-01 05:43:19 -0400123 * set the offset corresponding to the mcast IP address rewrite
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700124 */
125 adj->rewrite_header.dst_mcast_offset = offset;
Neale Ranns32e1c012016-11-22 17:07:28 +0000126}
127
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800128/**
129 * adj_mcast_midchain_update_rewrite
130 *
131 * Update the adjacency's rewrite string. A NULL string implies the
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700132 * rewrite is reset (i.e. when ARP/ND entry is gone).
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800133 * NB: the adj being updated may be handling traffic in the DP.
134 */
135void
136adj_mcast_midchain_update_rewrite (adj_index_t adj_index,
137 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800138 const void *fixup_data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800139 adj_flags_t flags,
140 u8 *rewrite,
141 u8 offset,
142 u32 mask)
143{
144 ip_adjacency_t *adj;
145
146 ASSERT(ADJ_INDEX_INVALID != adj_index);
147
148 adj = adj_get(adj_index);
149
150 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700151 * one time only update. since we don't support changing the tunnel
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800152 * src,dst, this is all we need.
153 */
154 ASSERT(adj->lookup_next_index == IP_LOOKUP_NEXT_MCAST);
155 /*
156 * tunnels can always provide a rewrite.
157 */
158 ASSERT(NULL != rewrite);
159
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800160 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800161
162 /*
163 * update the adj's rewrite string and build the arc
164 * from the rewrite node to the interface's TX node
165 */
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(
169 vnet_get_main(),
170 adj->rewrite_header.sw_if_index),
171 rewrite);
172
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800173 adj->rewrite_header.dst_mcast_offset = offset;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800174}
175
Neale Ranns32e1c012016-11-22 17:07:28 +0000176void
177adj_mcast_remove (fib_protocol_t proto,
178 u32 sw_if_index)
179{
180 ASSERT(sw_if_index < vec_len(adj_mcasts[proto]));
181
182 adj_mcasts[proto][sw_if_index] = ADJ_INDEX_INVALID;
183}
184
185static clib_error_t *
186adj_mcast_interface_state_change (vnet_main_t * vnm,
187 u32 sw_if_index,
188 u32 flags)
189{
190 /*
191 * for each mcast on the interface trigger a walk back to the children
192 */
193 fib_protocol_t proto;
194 ip_adjacency_t *adj;
195
196
197 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
198 {
199 if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
200 ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
201 continue;
202
203 adj = adj_get(adj_mcasts[proto][sw_if_index]);
204
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),
209 };
210
211 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
212 }
213
214 return (NULL);
215}
216
217VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_mcast_interface_state_change);
218
219/**
220 * @brief Invoked on each SW interface of a HW interface when the
221 * HW interface state changes
222 */
Neale Ranns0053de62018-05-22 08:40:52 -0700223static walk_rc_t
Neale Rannsc2aad532017-05-30 09:53:52 -0700224adj_mcast_hw_sw_interface_state_change (vnet_main_t * vnm,
225 u32 sw_if_index,
226 void *arg)
Neale Ranns32e1c012016-11-22 17:07:28 +0000227{
228 adj_mcast_interface_state_change(vnm, sw_if_index, (uword) arg);
Neale Ranns0053de62018-05-22 08:40:52 -0700229
230 return (WALK_CONTINUE);
Neale Ranns32e1c012016-11-22 17:07:28 +0000231}
232
233/**
234 * @brief Registered callback for HW interface state changes
235 */
236static clib_error_t *
237adj_mcast_hw_interface_state_change (vnet_main_t * vnm,
238 u32 hw_if_index,
239 u32 flags)
240{
241 /*
242 * walk SW interfaces on the HW
243 */
244 uword sw_flags;
245
246 sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
247 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
248 0);
249
250 vnet_hw_interface_walk_sw(vnm, hw_if_index,
Neale Rannsc2aad532017-05-30 09:53:52 -0700251 adj_mcast_hw_sw_interface_state_change,
Neale Ranns32e1c012016-11-22 17:07:28 +0000252 (void*) sw_flags);
253
254 return (NULL);
255}
256
257VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
258 adj_mcast_hw_interface_state_change);
259
260static clib_error_t *
261adj_mcast_interface_delete (vnet_main_t * vnm,
262 u32 sw_if_index,
263 u32 is_add)
264{
265 /*
266 * for each mcast on the interface trigger a walk back to the children
267 */
268 fib_protocol_t proto;
269 ip_adjacency_t *adj;
270
271 if (is_add)
272 {
273 /*
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.
285 */
286 return (NULL);
287 }
288
289 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
290 {
291 if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
292 ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
293 continue;
294
295 adj = adj_get(adj_mcasts[proto][sw_if_index]);
296
297 fib_node_back_walk_ctx_t bw_ctx = {
298 .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
299 };
300
301 fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
302 }
303
304 return (NULL);
305}
306
307VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_mcast_interface_delete);
308
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800309/**
310 * @brief Walk the multicast Adjacencies on a given interface
311 */
312void
313adj_mcast_walk (u32 sw_if_index,
314 fib_protocol_t proto,
315 adj_walk_cb_t cb,
316 void *ctx)
317{
318 if (vec_len(adj_mcasts[proto]) > sw_if_index)
319 {
320 if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
321 {
322 cb(adj_mcasts[proto][sw_if_index], ctx);
323 }
324 }
325}
326
Neale Ranns32e1c012016-11-22 17:07:28 +0000327u8*
328format_adj_mcast (u8* s, va_list *ap)
329{
330 index_t index = va_arg(*ap, index_t);
331 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
BenoƮt Ganne138c37a2019-07-18 17:34:28 +0200332 ip_adjacency_t * adj;
333
334 if (!adj_is_valid(index))
335 return format(s, "<invalid adjacency>");
336
337 adj = adj_get(index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000338
339 s = format(s, "%U-mcast: ",
340 format_fib_protocol, adj->ia_nh_proto);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800341 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
342 s = format(s, "[features] ");
Neale Ranns32e1c012016-11-22 17:07:28 +0000343 s = format (s, "%U",
344 format_vnet_rewrite,
Neale Rannsb069a692017-03-15 12:34:25 -0400345 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
Neale Ranns32e1c012016-11-22 17:07:28 +0000346
347 return (s);
348}
349
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800350u8*
351format_adj_mcast_midchain (u8* s, va_list *ap)
352{
353 index_t index = va_arg(*ap, index_t);
354 CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800355 ip_adjacency_t * adj = adj_get(index);
356
357 s = format(s, "%U-mcast-midchain: ",
358 format_fib_protocol, adj->ia_nh_proto);
359 s = format (s, "%U",
360 format_vnet_rewrite,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800361 &adj->rewrite_header,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800362 sizeof (adj->rewrite_data), 0);
363 s = format (s, "\n%Ustacked-on:\n%U%U",
364 format_white_space, indent,
365 format_white_space, indent+2,
366 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
367
368 return (s);
369}
370
Neale Ranns32e1c012016-11-22 17:07:28 +0000371
372static void
373adj_dpo_lock (dpo_id_t *dpo)
374{
375 adj_lock(dpo->dpoi_index);
376}
377static void
378adj_dpo_unlock (dpo_id_t *dpo)
379{
380 adj_unlock(dpo->dpoi_index);
381}
382
383const static dpo_vft_t adj_mcast_dpo_vft = {
384 .dv_lock = adj_dpo_lock,
385 .dv_unlock = adj_dpo_unlock,
386 .dv_format = format_adj_mcast,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700387 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns32e1c012016-11-22 17:07:28 +0000388};
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800389const static dpo_vft_t adj_mcast_midchain_dpo_vft = {
390 .dv_lock = adj_dpo_lock,
391 .dv_unlock = adj_dpo_unlock,
392 .dv_format = format_adj_mcast_midchain,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700393 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800394};
Neale Ranns32e1c012016-11-22 17:07:28 +0000395
396/**
397 * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
398 * object.
399 *
400 * this means that these graph nodes are ones from which a mcast is the
401 * parent object in the DPO-graph.
402 */
403const static char* const adj_mcast_ip4_nodes[] =
404{
405 "ip4-rewrite-mcast",
406 NULL,
407};
408const static char* const adj_mcast_ip6_nodes[] =
409{
410 "ip6-rewrite-mcast",
411 NULL,
412};
413
414const static char* const * const adj_mcast_nodes[DPO_PROTO_NUM] =
415{
416 [DPO_PROTO_IP4] = adj_mcast_ip4_nodes,
417 [DPO_PROTO_IP6] = adj_mcast_ip6_nodes,
418 [DPO_PROTO_MPLS] = NULL,
419};
420
421/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800422 * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
423 * object.
424 *
425 * this means that these graph nodes are ones from which a mcast is the
426 * parent object in the DPO-graph.
427 */
428const static char* const adj_mcast_midchain_ip4_nodes[] =
429{
430 "ip4-mcast-midchain",
431 NULL,
432};
433const static char* const adj_mcast_midchain_ip6_nodes[] =
434{
435 "ip6-mcast-midchain",
436 NULL,
437};
438
439const static char* const * const adj_mcast_midchain_nodes[DPO_PROTO_NUM] =
440{
441 [DPO_PROTO_IP4] = adj_mcast_midchain_ip4_nodes,
442 [DPO_PROTO_IP6] = adj_mcast_midchain_ip6_nodes,
443 [DPO_PROTO_MPLS] = NULL,
444};
445
446/**
Neale Ranns32e1c012016-11-22 17:07:28 +0000447 * @brief Return the size of the adj DB.
448 * This is only for testing purposes so an efficient implementation is not needed
449 */
450u32
451adj_mcast_db_size (void)
452{
453 u32 n_adjs, sw_if_index;
454 fib_protocol_t proto;
455
456 n_adjs = 0;
457 for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
458 {
459 for (sw_if_index = 0;
460 sw_if_index < vec_len(adj_mcasts[proto]);
461 sw_if_index++)
462 {
463 if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
464 {
465 n_adjs++;
466 }
467 }
468 }
469
470 return (n_adjs);
471}
472
473void
474adj_mcast_module_init (void)
475{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800476 dpo_register(DPO_ADJACENCY_MCAST,
477 &adj_mcast_dpo_vft,
478 adj_mcast_nodes);
479 dpo_register(DPO_ADJACENCY_MCAST_MIDCHAIN,
480 &adj_mcast_midchain_dpo_vft,
481 adj_mcast_midchain_nodes);
Neale Ranns32e1c012016-11-22 17:07:28 +0000482}