L2-flood: no clone for 1 replication
[vpp.git] / src / vnet / l2 / l2_flood.c
1 /*
2  * l2_flood.c : layer 2 flooding
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vlib/cli.h>
23 #include <vnet/l2/l2_input.h>
24 #include <vnet/l2/feat_bitmap.h>
25 #include <vnet/l2/l2_bvi.h>
26 #include <vnet/l2/l2_fib.h>
27
28 #include <vppinfra/error.h>
29 #include <vppinfra/hash.h>
30
31
32 /**
33  * @file
34  * @brief Ethernet Flooding.
35  *
36  * Flooding uses the packet replication infrastructure to send a copy of the
37  * packet to each member interface. Logically the replication infrastructure
38  * expects two graph nodes: a prep node that initiates replication and sends the
39  * packet to the first destination, and a recycle node that is passed the packet
40  * after it has been transmitted.
41  *
42  * To decrease the amount of code, l2 flooding implements both functions in
43  * the same graph node. This node can tell if is it being called as the "prep"
44  * or "recycle" using replication_is_recycled().
45  */
46
47
48 typedef struct
49 {
50
51   /* Next nodes for each feature */
52   u32 feat_next_node_index[32];
53
54   /* next node index for the L3 input node of each ethertype */
55   next_by_ethertype_t l3_next;
56
57   /* convenience variables */
58   vlib_main_t *vlib_main;
59   vnet_main_t *vnet_main;
60
61   /* per-cpu vector of cloned packets */
62   u32 **clones;
63   l2_flood_member_t ***members;
64 } l2flood_main_t;
65
66 typedef struct
67 {
68   u8 src[6];
69   u8 dst[6];
70   u32 sw_if_index;
71   u16 bd_index;
72 } l2flood_trace_t;
73
74
75 /* packet trace format function */
76 static u8 *
77 format_l2flood_trace (u8 * s, va_list * args)
78 {
79   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
80   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
81   l2flood_trace_t *t = va_arg (*args, l2flood_trace_t *);
82
83   s = format (s, "l2-flood: sw_if_index %d dst %U src %U bd_index %d",
84               t->sw_if_index,
85               format_ethernet_address, t->dst,
86               format_ethernet_address, t->src, t->bd_index);
87   return s;
88 }
89
90 l2flood_main_t l2flood_main;
91
92 static vlib_node_registration_t l2flood_node;
93
94 #define foreach_l2flood_error                                   \
95 _(L2FLOOD,           "L2 flood packets")                        \
96 _(REPL_FAIL,         "L2 replication failures")                 \
97 _(NO_MEMBERS,        "L2 replication complete")                 \
98 _(BVI_BAD_MAC,       "BVI L3 mac mismatch")                     \
99 _(BVI_ETHERTYPE,     "BVI packet with unhandled ethertype")
100
101 typedef enum
102 {
103 #define _(sym,str) L2FLOOD_ERROR_##sym,
104   foreach_l2flood_error
105 #undef _
106     L2FLOOD_N_ERROR,
107 } l2flood_error_t;
108
109 static char *l2flood_error_strings[] = {
110 #define _(sym,string) string,
111   foreach_l2flood_error
112 #undef _
113 };
114
115 typedef enum
116 {
117   L2FLOOD_NEXT_L2_OUTPUT,
118   L2FLOOD_NEXT_DROP,
119   L2FLOOD_N_NEXT,
120 } l2flood_next_t;
121
122 /*
123  * Perform flooding on one packet
124  *
125  * Due to the way BVI processing can modify the packet, the BVI interface
126  * (if present) must be processed last in the replication. The member vector
127  * is arranged so that the BVI interface is always the first element.
128  * Flooding walks the vector in reverse.
129  *
130  * BVI processing causes the packet to go to L3 processing. This strips the
131  * L2 header, which is fine because the replication infrastructure restores
132  * it. However L3 processing can trigger larger changes to the packet. For
133  * example, an ARP request could be turned into an ARP reply, an ICMP request
134  * could be turned into an ICMP reply. If BVI processing is not performed
135  * last, the modified packet would be replicated to the remaining members.
136  */
137 static uword
138 l2flood_node_fn (vlib_main_t * vm,
139                  vlib_node_runtime_t * node, vlib_frame_t * frame)
140 {
141   u32 n_left_from, *from, *to_next;
142   l2flood_next_t next_index;
143   l2flood_main_t *msm = &l2flood_main;
144   u32 thread_index = vm->thread_index;
145
146   from = vlib_frame_vector_args (frame);
147   n_left_from = frame->n_vectors;
148   next_index = node->cached_next_index;
149
150   while (n_left_from > 0)
151     {
152       u32 n_left_to_next;
153
154       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
155
156       while (n_left_from > 0 && n_left_to_next > 0)
157         {
158           u16 n_clones, n_cloned, clone0;
159           l2_bridge_domain_t *bd_config;
160           u32 sw_if_index0, bi0, ci0;
161           l2_flood_member_t *member;
162           vlib_buffer_t *b0, *c0;
163           u16 next0;
164           u8 in_shg;
165           i32 mi;
166
167           /* speculatively enqueue b0 to the current next frame */
168           bi0 = from[0];
169           from += 1;
170           n_left_from -= 1;
171           next0 = L2FLOOD_NEXT_L2_OUTPUT;
172
173           b0 = vlib_get_buffer (vm, bi0);
174
175           /* Get config for the bridge domain interface */
176           bd_config = vec_elt_at_index (l2input_main.bd_configs,
177                                         vnet_buffer (b0)->l2.bd_index);
178           in_shg = vnet_buffer (b0)->l2.shg;
179           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
180
181           vec_validate (msm->members[thread_index],
182                         vec_len (bd_config->members));
183
184           vec_reset_length (msm->members[thread_index]);
185
186           /* Find first members that passes the reflection and SHG checks */
187           for (mi = bd_config->flood_count - 1; mi >= 0; mi--)
188             {
189               member = &bd_config->members[mi];
190               if ((member->sw_if_index != sw_if_index0) &&
191                   (!in_shg || (member->shg != in_shg)))
192                 {
193                   vec_add1 (msm->members[thread_index], member);
194                 }
195             }
196
197           n_clones = vec_len (msm->members[thread_index]);
198
199           if (0 == n_clones)
200             {
201               /* No members to flood to */
202               to_next[0] = bi0;
203               to_next += 1;
204               n_left_to_next -= 1;
205
206               b0->error = node->errors[L2FLOOD_ERROR_NO_MEMBERS];
207               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
208                                                to_next, n_left_to_next,
209                                                bi0, L2FLOOD_NEXT_DROP);
210               continue;
211             }
212           else if (n_clones > 1)
213             {
214               vec_validate (msm->clones[thread_index], n_clones);
215               vec_reset_length (msm->clones[thread_index]);
216
217               /*
218                * the header offset needs to be large enough to incorporate
219                * all the L3 headers that could be touched when doing BVI
220                * processing. So take the current l2 length plus 2 * IPv6
221                * headers (for tunnel encap)
222                */
223               n_cloned = vlib_buffer_clone (vm, bi0,
224                                             msm->clones[thread_index],
225                                             n_clones,
226                                             (vnet_buffer (b0)->l2.l2_len +
227                                              sizeof (udp_header_t) +
228                                              2 * sizeof (ip6_header_t)));
229
230               if (PREDICT_FALSE (n_cloned != n_clones))
231                 {
232                   b0->error = node->errors[L2FLOOD_ERROR_REPL_FAIL];
233                 }
234
235               /*
236                * for all but the last clone, these are not BVI bound
237                */
238               for (clone0 = 0; clone0 < n_cloned - 1; clone0++)
239                 {
240                   member = msm->members[thread_index][clone0];
241                   ci0 = msm->clones[thread_index][clone0];
242                   c0 = vlib_get_buffer (vm, ci0);
243
244                   to_next[0] = ci0;
245                   to_next += 1;
246                   n_left_to_next -= 1;
247
248                   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
249                                      (b0->flags & VLIB_BUFFER_IS_TRACED)))
250                     {
251                       ethernet_header_t *h0;
252                       l2flood_trace_t *t;
253
254                       if (c0 != b0)
255                         vlib_buffer_copy_trace_flag (vm, b0, ci0);
256
257                       t = vlib_add_trace (vm, node, c0, sizeof (*t));
258                       h0 = vlib_buffer_get_current (c0);
259                       t->sw_if_index = sw_if_index0;
260                       t->bd_index = vnet_buffer (c0)->l2.bd_index;
261                       clib_memcpy (t->src, h0->src_address, 6);
262                       clib_memcpy (t->dst, h0->dst_address, 6);
263                     }
264
265                   /* Do normal L2 forwarding */
266                   vnet_buffer (c0)->sw_if_index[VLIB_TX] =
267                     member->sw_if_index;
268
269                   vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
270                                                    to_next, n_left_to_next,
271                                                    ci0, next0);
272                   if (PREDICT_FALSE (0 == n_left_to_next))
273                     {
274                       vlib_put_next_frame (vm, node, next_index,
275                                            n_left_to_next);
276                       vlib_get_next_frame (vm, node, next_index, to_next,
277                                            n_left_to_next);
278                     }
279                 }
280               member = msm->members[thread_index][clone0];
281               ci0 = msm->clones[thread_index][clone0];
282             }
283           else
284             {
285               /* one clone */
286               ci0 = bi0;
287               member = msm->members[thread_index][0];
288             }
289
290           /*
291            * the last clone that might go to a BVI
292            */
293           c0 = vlib_get_buffer (vm, ci0);
294
295           to_next[0] = ci0;
296           to_next += 1;
297           n_left_to_next -= 1;
298
299           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
300                              (b0->flags & VLIB_BUFFER_IS_TRACED)))
301             {
302               ethernet_header_t *h0;
303               l2flood_trace_t *t;
304
305               if (c0 != b0)
306                 vlib_buffer_copy_trace_flag (vm, b0, ci0);
307
308               t = vlib_add_trace (vm, node, c0, sizeof (*t));
309               h0 = vlib_buffer_get_current (c0);
310               t->sw_if_index = sw_if_index0;
311               t->bd_index = vnet_buffer (c0)->l2.bd_index;
312               clib_memcpy (t->src, h0->src_address, 6);
313               clib_memcpy (t->dst, h0->dst_address, 6);
314             }
315
316
317           /* Forward packet to the current member */
318           if (PREDICT_FALSE (member->flags & L2_FLOOD_MEMBER_BVI))
319             {
320               /* Do BVI processing */
321               u32 rc;
322               rc = l2_to_bvi (vm,
323                               msm->vnet_main,
324                               c0, member->sw_if_index, &msm->l3_next, &next0);
325
326               if (PREDICT_FALSE (rc != TO_BVI_ERR_OK))
327                 {
328                   if (rc == TO_BVI_ERR_BAD_MAC)
329                     {
330                       c0->error = node->errors[L2FLOOD_ERROR_BVI_BAD_MAC];
331                     }
332                   else if (rc == TO_BVI_ERR_ETHERTYPE)
333                     {
334                       c0->error = node->errors[L2FLOOD_ERROR_BVI_ETHERTYPE];
335                     }
336                   next0 = L2FLOOD_NEXT_DROP;
337                 }
338             }
339           else
340             {
341               /* Do normal L2 forwarding */
342               vnet_buffer (c0)->sw_if_index[VLIB_TX] = member->sw_if_index;
343             }
344
345           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
346                                            to_next, n_left_to_next,
347                                            ci0, next0);
348           if (PREDICT_FALSE (0 == n_left_to_next))
349             {
350               vlib_put_next_frame (vm, node, next_index, n_left_to_next);
351               vlib_get_next_frame (vm, node, next_index,
352                                    to_next, n_left_to_next);
353             }
354         }
355
356       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
357     }
358
359   vlib_node_increment_counter (vm, node->node_index,
360                                L2FLOOD_ERROR_L2FLOOD, frame->n_vectors);
361
362   return frame->n_vectors;
363 }
364
365
366 /* *INDENT-OFF* */
367 VLIB_REGISTER_NODE (l2flood_node,static) = {
368   .function = l2flood_node_fn,
369   .name = "l2-flood",
370   .vector_size = sizeof (u32),
371   .format_trace = format_l2flood_trace,
372   .type = VLIB_NODE_TYPE_INTERNAL,
373
374   .n_errors = ARRAY_LEN(l2flood_error_strings),
375   .error_strings = l2flood_error_strings,
376
377   .n_next_nodes = L2FLOOD_N_NEXT,
378
379   /* edit / add dispositions here */
380   .next_nodes = {
381         [L2FLOOD_NEXT_L2_OUTPUT] = "l2-output",
382         [L2FLOOD_NEXT_DROP] = "error-drop",
383   },
384 };
385
386 VLIB_NODE_FUNCTION_MULTIARCH (l2flood_node, l2flood_node_fn)
387 /* *INDENT-ON* */
388
389 clib_error_t *
390 l2flood_init (vlib_main_t * vm)
391 {
392   l2flood_main_t *mp = &l2flood_main;
393
394   mp->vlib_main = vm;
395   mp->vnet_main = vnet_get_main ();
396
397   vec_validate (mp->clones, vlib_num_workers ());
398   vec_validate (mp->members, vlib_num_workers ());
399
400   /* Initialize the feature next-node indexes */
401   feat_bitmap_init_next_nodes (vm,
402                                l2flood_node.index,
403                                L2INPUT_N_FEAT,
404                                l2input_get_feat_names (),
405                                mp->feat_next_node_index);
406
407   return NULL;
408 }
409
410 VLIB_INIT_FUNCTION (l2flood_init);
411
412
413
414 /** Add the L3 input node for this ethertype to the next nodes structure. */
415 void
416 l2flood_register_input_type (vlib_main_t * vm,
417                              ethernet_type_t type, u32 node_index)
418 {
419   l2flood_main_t *mp = &l2flood_main;
420   u32 next_index;
421
422   next_index = vlib_node_add_next (vm, l2flood_node.index, node_index);
423
424   next_by_ethertype_register (&mp->l3_next, type, next_index);
425 }
426
427
428 /**
429  * Set subinterface flood enable/disable.
430  * The CLI format is:
431  * set interface l2 flood <interface> [disable]
432  */
433 static clib_error_t *
434 int_flood (vlib_main_t * vm,
435            unformat_input_t * input, vlib_cli_command_t * cmd)
436 {
437   vnet_main_t *vnm = vnet_get_main ();
438   clib_error_t *error = 0;
439   u32 sw_if_index;
440   u32 enable;
441
442   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
443     {
444       error = clib_error_return (0, "unknown interface `%U'",
445                                  format_unformat_error, input);
446       goto done;
447     }
448
449   enable = 1;
450   if (unformat (input, "disable"))
451     {
452       enable = 0;
453     }
454
455   /* set the interface flag */
456   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_FLOOD, enable);
457
458 done:
459   return error;
460 }
461
462 /*?
463  * Layer 2 flooding can be enabled and disabled on each
464  * interface and on each bridge-domain. Use this command to
465  * manage interfaces. It is enabled by default.
466  *
467  * @cliexpar
468  * Example of how to enable flooding:
469  * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0}
470  * Example of how to disable flooding:
471  * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0 disable}
472 ?*/
473 /* *INDENT-OFF* */
474 VLIB_CLI_COMMAND (int_flood_cli, static) = {
475   .path = "set interface l2 flood",
476   .short_help = "set interface l2 flood <interface> [disable]",
477   .function = int_flood,
478 };
479 /* *INDENT-ON* */
480
481 /*
482  * fd.io coding-style-patch-verification: ON
483  *
484  * Local Variables:
485  * eval: (c-set-style "gnu")
486  * End:
487  */