Mcast rewrite optimisations
[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,
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     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
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.
100  */
101 void
102 adj_mcast_update_rewrite (adj_index_t adj_index,
103                           u8 *rewrite,
104                           u8 offset)
105 {
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);
122     /*
123      * set the offset corresponding to the mcast IP address rewrite
124      */
125     adj->rewrite_header.dst_mcast_offset = offset;
126 }
127
128 /**
129  * adj_mcast_midchain_update_rewrite
130  *
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.
134  */
135 void
136 adj_mcast_midchain_update_rewrite (adj_index_t adj_index,
137                                    adj_midchain_fixup_t fixup,
138                                    const void *fixup_data,
139                                    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     /*
151      * one time only update. since we don't support chainging the tunnel
152      * 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
160     adj_midchain_setup(adj_index, fixup, fixup_data, flags);
161
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
173     adj->rewrite_header.dst_mcast_offset = offset;
174 }
175
176 void
177 adj_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
185 static clib_error_t *
186 adj_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
217 VNET_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  */
223 static void
224 adj_mcast_hw_sw_interface_state_change (vnet_main_t * vnm,
225                                         u32 sw_if_index,
226                                         void *arg)
227 {
228     adj_mcast_interface_state_change(vnm, sw_if_index, (uword) arg);
229 }
230
231 /**
232  * @brief Registered callback for HW interface state changes
233  */
234 static clib_error_t *
235 adj_mcast_hw_interface_state_change (vnet_main_t * vnm,
236                                      u32 hw_if_index,
237                                      u32 flags)
238 {
239     /*
240      * walk SW interfaces on the HW
241      */
242     uword sw_flags;
243
244     sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
245                 VNET_SW_INTERFACE_FLAG_ADMIN_UP :
246                 0);
247
248     vnet_hw_interface_walk_sw(vnm, hw_if_index,
249                               adj_mcast_hw_sw_interface_state_change,
250                               (void*) sw_flags);
251
252     return (NULL);
253 }
254
255 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(
256     adj_mcast_hw_interface_state_change);
257
258 static clib_error_t *
259 adj_mcast_interface_delete (vnet_main_t * vnm,
260                             u32 sw_if_index,
261                             u32 is_add)
262 {
263     /*
264      * for each mcast on the interface trigger a walk back to the children
265      */
266     fib_protocol_t proto;
267     ip_adjacency_t *adj;
268
269     if (is_add)
270     {
271         /*
272          * not interested in interface additions. we will not back walk
273          * to resolve paths through newly added interfaces. Why? The control
274          * plane should have the brains to add interfaces first, then routes.
275          * So the case where there are paths with a interface that matches
276          * one just created is the case where the path resolved through an
277          * interface that was deleted, and still has not been removed. The
278          * new interface added, is NO GUARANTEE that the interface being
279          * added now, even though it may have the same sw_if_index, is the
280          * same interface that the path needs. So tough!
281          * If the control plane wants these routes to resolve it needs to
282          * remove and add them again.
283          */
284         return (NULL);
285     }
286
287     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
288     {
289         if (sw_if_index >= vec_len(adj_mcasts[proto]) ||
290             ADJ_INDEX_INVALID == adj_mcasts[proto][sw_if_index])
291             continue;
292
293         adj = adj_get(adj_mcasts[proto][sw_if_index]);
294
295         fib_node_back_walk_ctx_t bw_ctx = {
296             .fnbw_reason =  FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE,
297         };
298
299         fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx);
300     }
301
302     return (NULL);
303 }
304
305 VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_mcast_interface_delete);
306
307 /**
308  * @brief Walk the multicast Adjacencies on a given interface
309  */
310 void
311 adj_mcast_walk (u32 sw_if_index,
312                 fib_protocol_t proto,
313                 adj_walk_cb_t cb,
314                 void *ctx)
315 {
316     if (vec_len(adj_mcasts[proto]) > sw_if_index)
317     {
318         if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
319         {
320             cb(adj_mcasts[proto][sw_if_index], ctx);
321         }
322     }
323 }
324
325 u8*
326 format_adj_mcast (u8* s, va_list *ap)
327 {
328     index_t index = va_arg(*ap, index_t);
329     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
330     ip_adjacency_t * adj = adj_get(index);
331
332     s = format(s, "%U-mcast: ",
333                format_fib_protocol, adj->ia_nh_proto);
334     if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
335         s = format(s, "[features] ");
336     s = format (s, "%U",
337                 format_vnet_rewrite,
338                 &adj->rewrite_header, sizeof (adj->rewrite_data), 0);
339
340     return (s);
341 }
342
343 u8*
344 format_adj_mcast_midchain (u8* s, va_list *ap)
345 {
346     index_t index = va_arg(*ap, index_t);
347     CLIB_UNUSED(u32 indent) = va_arg(*ap, u32);
348     ip_adjacency_t * adj = adj_get(index);
349
350     s = format(s, "%U-mcast-midchain: ",
351                format_fib_protocol, adj->ia_nh_proto);
352     s = format (s, "%U",
353                 format_vnet_rewrite,
354                 &adj->rewrite_header,
355                 sizeof (adj->rewrite_data), 0);
356     s = format (s, "\n%Ustacked-on:\n%U%U",
357                 format_white_space, indent,
358                 format_white_space, indent+2,
359                 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
360
361     return (s);
362 }
363
364
365 static void
366 adj_dpo_lock (dpo_id_t *dpo)
367 {
368     adj_lock(dpo->dpoi_index);
369 }
370 static void
371 adj_dpo_unlock (dpo_id_t *dpo)
372 {
373     adj_unlock(dpo->dpoi_index);
374 }
375
376 const static dpo_vft_t adj_mcast_dpo_vft = {
377     .dv_lock = adj_dpo_lock,
378     .dv_unlock = adj_dpo_unlock,
379     .dv_format = format_adj_mcast,
380     .dv_get_urpf = adj_dpo_get_urpf,
381 };
382 const static dpo_vft_t adj_mcast_midchain_dpo_vft = {
383     .dv_lock = adj_dpo_lock,
384     .dv_unlock = adj_dpo_unlock,
385     .dv_format = format_adj_mcast_midchain,
386     .dv_get_urpf = adj_dpo_get_urpf,
387 };
388
389 /**
390  * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
391  *        object.
392  *
393  * this means that these graph nodes are ones from which a mcast is the
394  * parent object in the DPO-graph.
395  */
396 const static char* const adj_mcast_ip4_nodes[] =
397 {
398     "ip4-rewrite-mcast",
399     NULL,
400 };
401 const static char* const adj_mcast_ip6_nodes[] =
402 {
403     "ip6-rewrite-mcast",
404     NULL,
405 };
406
407 const static char* const * const adj_mcast_nodes[DPO_PROTO_NUM] =
408 {
409     [DPO_PROTO_IP4]  = adj_mcast_ip4_nodes,
410     [DPO_PROTO_IP6]  = adj_mcast_ip6_nodes,
411     [DPO_PROTO_MPLS] = NULL,
412 };
413
414 /**
415  * @brief The per-protocol VLIB graph nodes that are assigned to a mcast
416  *        object.
417  *
418  * this means that these graph nodes are ones from which a mcast is the
419  * parent object in the DPO-graph.
420  */
421 const static char* const adj_mcast_midchain_ip4_nodes[] =
422 {
423     "ip4-mcast-midchain",
424     NULL,
425 };
426 const static char* const adj_mcast_midchain_ip6_nodes[] =
427 {
428     "ip6-mcast-midchain",
429     NULL,
430 };
431
432 const static char* const * const adj_mcast_midchain_nodes[DPO_PROTO_NUM] =
433 {
434     [DPO_PROTO_IP4]  = adj_mcast_midchain_ip4_nodes,
435     [DPO_PROTO_IP6]  = adj_mcast_midchain_ip6_nodes,
436     [DPO_PROTO_MPLS] = NULL,
437 };
438
439 /**
440  * @brief Return the size of the adj DB.
441  * This is only for testing purposes so an efficient implementation is not needed
442  */
443 u32
444 adj_mcast_db_size (void)
445 {
446     u32 n_adjs, sw_if_index;
447     fib_protocol_t proto;
448
449     n_adjs = 0;
450     for (proto = FIB_PROTOCOL_IP4; proto <= FIB_PROTOCOL_IP6; proto++)
451     {
452         for (sw_if_index = 0;
453              sw_if_index < vec_len(adj_mcasts[proto]);
454              sw_if_index++)
455         {
456             if (ADJ_INDEX_INVALID != adj_mcasts[proto][sw_if_index])
457             {
458                 n_adjs++;
459             }
460         }
461     }
462     
463     return (n_adjs);
464 }
465
466 void
467 adj_mcast_module_init (void)
468 {
469     dpo_register(DPO_ADJACENCY_MCAST,
470                  &adj_mcast_dpo_vft,
471                  adj_mcast_nodes);
472     dpo_register(DPO_ADJACENCY_MCAST_MIDCHAIN,
473                  &adj_mcast_midchain_dpo_vft,
474                  adj_mcast_midchain_nodes);
475 }