4d7172cdb580ec735b6b2803de6ddbc9ca9eb908
[vpp.git] / src / vnet / adj / adj_mcast.c
1 /*
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_mcast.h>
17 #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  */
26 static adj_index_t *adj_mcasts[FIB_PROTOCOL_MAX];
27
28 static u32
29 adj_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  */
50 adj_index_t
51 adj_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
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);
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     adj_delegate_adj_created(adj);
92
93     return (adj_get_index(adj));
94 }
95
96 /**
97  * adj_mcast_update_rewrite
98  *
99  * Update the adjacency's rewrite string. A NULL string implies the
100  * rewrite is reset (i.e. when ARP/ND entry is gone).
101  * NB: the adj being updated may be handling traffic in the DP.
102  */
103 void
104 adj_mcast_update_rewrite (adj_index_t adj_index,
105                           u8 *rewrite,
106                           u8 offset)
107 {
108     ip_adjacency_t *adj;
109
110     ASSERT(ADJ_INDEX_INVALID != adj_index);
111
112     adj = adj_get(adj_index);
113
114     /*
115      * update the adj's rewrite string and build the arc
116      * from the rewrite node to the interface's TX node
117      */
118     adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_MCAST,
119                                     adj_get_mcast_node(adj->ia_nh_proto),
120                                     vnet_tx_node_index_for_sw_interface(
121                                         vnet_get_main(),
122                                         adj->rewrite_header.sw_if_index),
123                                     rewrite);
124     /*
125      * set the offset corresponding to the mcast IP address rewrite
126      */
127     adj->rewrite_header.dst_mcast_offset = offset;
128 }
129
130 /**
131  * adj_mcast_midchain_update_rewrite
132  *
133  * Update the adjacency's rewrite string. A NULL string implies the
134  * rewrite is reset (i.e. when ARP/ND entry is gone).
135  * NB: the adj being updated may be handling traffic in the DP.
136  */
137 void
138 adj_mcast_midchain_update_rewrite (adj_index_t adj_index,
139                                    adj_midchain_fixup_t fixup,
140                                    const void *fixup_data,
141                                    adj_flags_t flags,
142                                    u8 *rewrite,
143                                    u8 offset,
144                                    u32 mask)
145 {
146     ip_adjacency_t *adj;
147
148     ASSERT(ADJ_INDEX_INVALID != adj_index);
149
150     adj = adj_get(adj_index);
151
152     /*
153      * one time only update. since we don't support changing the tunnel
154      * src,dst, this is all we need.
155      */
156     ASSERT(adj->lookup_next_index == IP_LOOKUP_NEXT_MCAST);
157     /*
158      * tunnels can always provide a rewrite.
159      */
160     ASSERT(NULL != rewrite);
161
162     adj_midchain_setup(adj_index, fixup, fixup_data, flags);
163
164     /*
165      * update the adj's rewrite string and build the arc
166      * from the rewrite node to the interface's TX node
167      */
168     adj_nbr_update_rewrite_internal(adj, IP_LOOKUP_NEXT_MCAST_MIDCHAIN,
169                                     adj_get_mcast_node(adj->ia_nh_proto),
170                                     vnet_tx_node_index_for_sw_interface(
171                                         vnet_get_main(),
172                                         adj->rewrite_header.sw_if_index),
173                                     rewrite);
174
175     adj->rewrite_header.dst_mcast_offset = offset;
176 }
177
178 void
179 adj_mcast_remove (fib_protocol_t proto,
180                   u32 sw_if_index)
181 {
182     ASSERT(sw_if_index < vec_len(adj_mcasts[proto]));
183
184     adj_mcasts[proto][sw_if_index] = ADJ_INDEX_INVALID;
185 }
186
187 static clib_error_t *
188 adj_mcast_interface_state_change (vnet_main_t * vnm,
189                                   u32 sw_if_index,
190                                   u32 flags)
191 {
192     /*
193      * for each mcast on the interface trigger a walk back to the children
194      */
195     fib_protocol_t proto;
196     ip_adjacency_t *adj;
197
198
199     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
200     {
201         if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
202             ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
203             continue;
204
205         adj = adj_get(adj_mcasts[proto][sw_if_index]);
206
207         fib_node_back_walk_ctx_t bw_ctx = {
208             .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ?
209                             FIB_NODE_BW_REASON_FLAG_INTERFACE_UP :
210                             FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN),
211         };
212
213         fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
214     }
215
216     return (NULL);
217 }
218
219 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_mcast_interface_state_change);
220
221 /**
222  * @brief Invoked on each SW interface of a HW interface when the
223  * HW interface state changes
224  */
225 static walk_rc_t
226 adj_mcast_hw_sw_interface_state_change (vnet_main_t * vnm,
227                                         u32 sw_if_index,
228                                         void *arg)
229 {
230     adj_mcast_interface_state_change(vnm, sw_if_index, (uword) arg);
231
232     return (WALK_CONTINUE);
233 }
234
235 /**
236  * @brief Registered callback for HW interface state changes
237  */
238 static clib_error_t *
239 adj_mcast_hw_interface_state_change (vnet_main_t * vnm,
240                                      u32 hw_if_index,
241                                      u32 flags)
242 {
243     /*
244      * walk SW interfaces on the HW
245      */
246     uword sw_flags;
247
248     sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
249                 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
250                 0);
251
252     vnet_hw_interface_walk_sw(vnm, hw_if_index,
253                               adj_mcast_hw_sw_interface_state_change,
254                               (void*) sw_flags);
255
256     return (NULL);
257 }
258
259 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
260     adj_mcast_hw_interface_state_change);
261
262 static clib_error_t *
263 adj_mcast_interface_delete (vnet_main_t * vnm,
264                             u32 sw_if_index,
265                             u32 is_add)
266 {
267     /*
268      * for each mcast on the interface trigger a walk back to the children
269      */
270     fib_protocol_t proto;
271     ip_adjacency_t *adj;
272
273     if (is_add)
274     {
275         /*
276          * not interested in interface additions. we will not back walk
277          * to resolve paths through newly added interfaces. Why? The control
278          * plane should have the brains to add interfaces first, then routes.
279          * So the case where there are paths with a interface that matches
280          * one just created is the case where the path resolved through an
281          * interface that was deleted, and still has not been removed. The
282          * new interface added, is NO GUARANTEE that the interface being
283          * added now, even though it may have the same sw_if_index, is the
284          * same interface that the path needs. So tough!
285          * If the control plane wants these routes to resolve it needs to
286          * remove and add them again.
287          */
288         return (NULL);
289     }
290
291     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
292     {
293         if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
294             ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
295             continue;
296
297         adj = adj_get(adj_mcasts[proto][sw_if_index]);
298
299         fib_node_back_walk_ctx_t bw_ctx = {
300             .fnbw_reason =  FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
301         };
302
303         fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
304     }
305
306     return (NULL);
307 }
308
309 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_mcast_interface_delete);
310
311 /**
312  * @brief Walk the multicast Adjacencies on a given interface
313  */
314 void
315 adj_mcast_walk (u32 sw_if_index,
316                 fib_protocol_t proto,
317                 adj_walk_cb_t cb,
318                 void *ctx)
319 {
320     if (vec_len(adj_mcasts[proto]) > sw_if_index)
321     {
322         if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
323         {
324             cb(adj_mcasts[proto][sw_if_index], ctx);
325         }
326     }
327 }
328
329 u8*
330 format_adj_mcast (u8* s, va_list *ap)
331 {
332     index_t index = va_arg(*ap, index_t);
333     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
334     ip_adjacency_t * adj;
335
336     if (!adj_is_valid(index))
337       return format(s, "<invalid adjacency>");
338
339     adj = adj_get(index);
340
341     s = format(s, "%U-mcast: ",
342                format_fib_protocol, adj->ia_nh_proto);
343     if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
344         s = format(s, "[features] ");
345     s = format (s, "%U",
346                 format_vnet_rewrite,
347                 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
348
349     return (s);
350 }
351
352 u8*
353 format_adj_mcast_midchain (u8* s, va_list *ap)
354 {
355     index_t index = va_arg(*ap, index_t);
356     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
357     ip_adjacency_t * adj = adj_get(index);
358
359     s = format(s, "%U-mcast-midchain: ",
360                format_fib_protocol, adj->ia_nh_proto);
361     s = format (s, "%U",
362                 format_vnet_rewrite,
363                 &adj->rewrite_header,
364                 sizeof (adj->rewrite_data), 0);
365     s = format (s, "\n%Ustacked-on:\n%U%U",
366                 format_white_space, indent,
367                 format_white_space, indent+2,
368                 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
369
370     return (s);
371 }
372
373
374 static void
375 adj_dpo_lock (dpo_id_t *dpo)
376 {
377     adj_lock(dpo->dpoi_index);
378 }
379 static void
380 adj_dpo_unlock (dpo_id_t *dpo)
381 {
382     adj_unlock(dpo->dpoi_index);
383 }
384
385 const static dpo_vft_t adj_mcast_dpo_vft = {
386     .dv_lock = adj_dpo_lock,
387     .dv_unlock = adj_dpo_unlock,
388     .dv_format = format_adj_mcast,
389     .dv_get_urpf = adj_dpo_get_urpf,
390 };
391 const static dpo_vft_t adj_mcast_midchain_dpo_vft = {
392     .dv_lock = adj_dpo_lock,
393     .dv_unlock = adj_dpo_unlock,
394     .dv_format = format_adj_mcast_midchain,
395     .dv_get_urpf = adj_dpo_get_urpf,
396 };
397
398 /**
399  * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
400  *        object.
401  *
402  * this means that these graph nodes are ones from which a mcast is the
403  * parent object in the DPO-graph.
404  */
405 const static char* const adj_mcast_ip4_nodes[] =
406 {
407     "ip4-rewrite-mcast",
408     NULL,
409 };
410 const static char* const adj_mcast_ip6_nodes[] =
411 {
412     "ip6-rewrite-mcast",
413     NULL,
414 };
415
416 const static char* const * const adj_mcast_nodes[DPO_PROTO_NUM] =
417 {
418     [DPO_PROTO_IP4]  = adj_mcast_ip4_nodes,
419     [DPO_PROTO_IP6]  = adj_mcast_ip6_nodes,
420     [DPO_PROTO_MPLS] = NULL,
421 };
422
423 /**
424  * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
425  *        object.
426  *
427  * this means that these graph nodes are ones from which a mcast is the
428  * parent object in the DPO-graph.
429  */
430 const static char* const adj_mcast_midchain_ip4_nodes[] =
431 {
432     "ip4-mcast-midchain",
433     NULL,
434 };
435 const static char* const adj_mcast_midchain_ip6_nodes[] =
436 {
437     "ip6-mcast-midchain",
438     NULL,
439 };
440
441 const static char* const * const adj_mcast_midchain_nodes[DPO_PROTO_NUM] =
442 {
443     [DPO_PROTO_IP4]  = adj_mcast_midchain_ip4_nodes,
444     [DPO_PROTO_IP6]  = adj_mcast_midchain_ip6_nodes,
445     [DPO_PROTO_MPLS] = NULL,
446 };
447
448 /**
449  * @brief Return the size of the adj DB.
450  * This is only for testing purposes so an efficient implementation is not needed
451  */
452 u32
453 adj_mcast_db_size (void)
454 {
455     u32 n_adjs, sw_if_index;
456     fib_protocol_t proto;
457
458     n_adjs = 0;
459     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
460     {
461         for (sw_if_index = 0;
462              sw_if_index < vec_len(adj_mcasts[proto]);
463              sw_if_index++)
464         {
465             if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
466             {
467                 n_adjs++;
468             }
469         }
470     }
471     
472     return (n_adjs);
473 }
474
475 void
476 adj_mcast_module_init (void)
477 {
478     dpo_register(DPO_ADJACENCY_MCAST,
479                  &adj_mcast_dpo_vft,
480                  adj_mcast_nodes);
481     dpo_register(DPO_ADJACENCY_MCAST_MIDCHAIN,
482                  &adj_mcast_midchain_dpo_vft,
483                  adj_mcast_midchain_nodes);
484 }