vlib: copy trace_handle in vlib_buffer_copy/clone() functions
[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 extern l2flood_main_t l2flood_main;
91
92 #ifndef CLIB_MARCH_VARIANT
93 l2flood_main_t l2flood_main;
94 #endif /* CLIB_MARCH_VARIANT */
95
96 #define foreach_l2flood_error                                   \
97 _(L2FLOOD,           "L2 flood packets")                        \
98 _(REPL_FAIL,         "L2 replication failures")                 \
99 _(NO_MEMBERS,        "L2 replication complete")                 \
100 _(BVI_BAD_MAC,       "BVI L3 mac mismatch")                     \
101 _(BVI_ETHERTYPE,     "BVI packet with unhandled ethertype")
102
103 typedef enum
104 {
105 #define _(sym,str) L2FLOOD_ERROR_##sym,
106   foreach_l2flood_error
107 #undef _
108     L2FLOOD_N_ERROR,
109 } l2flood_error_t;
110
111 static char *l2flood_error_strings[] = {
112 #define _(sym,string) string,
113   foreach_l2flood_error
114 #undef _
115 };
116
117 typedef enum
118 {
119   L2FLOOD_NEXT_L2_OUTPUT,
120   L2FLOOD_NEXT_DROP,
121   L2FLOOD_N_NEXT,
122 } l2flood_next_t;
123
124 /*
125  * Perform flooding on one packet
126  *
127  * Due to the way BVI processing can modify the packet, the BVI interface
128  * (if present) must be processed last in the replication. The member vector
129  * is arranged so that the BVI interface is always the first element.
130  * Flooding walks the vector in reverse.
131  *
132  * BVI processing causes the packet to go to L3 processing. This strips the
133  * L2 header, which is fine because the replication infrastructure restores
134  * it. However L3 processing can trigger larger changes to the packet. For
135  * example, an ARP request could be turned into an ARP reply, an ICMP request
136  * could be turned into an ICMP reply. If BVI processing is not performed
137  * last, the modified packet would be replicated to the remaining members.
138  */
139 VLIB_NODE_FN (l2flood_node) (vlib_main_t * vm,
140                              vlib_node_runtime_t * node, vlib_frame_t * frame)
141 {
142   u32 n_left_from, *from, *to_next;
143   l2flood_next_t next_index;
144   l2flood_main_t *msm = &l2flood_main;
145   u32 thread_index = vm->thread_index;
146
147   from = vlib_frame_vector_args (frame);
148   n_left_from = frame->n_vectors;
149   next_index = node->cached_next_index;
150
151   while (n_left_from > 0)
152     {
153       u32 n_left_to_next;
154
155       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
156
157       while (n_left_from > 0 && n_left_to_next > 0)
158         {
159           u16 n_clones, n_cloned, clone0;
160           l2_bridge_domain_t *bd_config;
161           u32 sw_if_index0, bi0, ci0;
162           l2_flood_member_t *member;
163           vlib_buffer_t *b0, *c0;
164           u16 next0;
165           u8 in_shg;
166           i32 mi;
167
168           /* speculatively enqueue b0 to the current next frame */
169           bi0 = from[0];
170           from += 1;
171           n_left_from -= 1;
172           next0 = L2FLOOD_NEXT_L2_OUTPUT;
173
174           b0 = vlib_get_buffer (vm, bi0);
175
176           /* Get config for the bridge domain interface */
177           bd_config = vec_elt_at_index (l2input_main.bd_configs,
178                                         vnet_buffer (b0)->l2.bd_index);
179           in_shg = vnet_buffer (b0)->l2.shg;
180           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
181
182           vec_validate (msm->members[thread_index],
183                         vec_len (bd_config->members));
184
185           vec_reset_length (msm->members[thread_index]);
186
187           /* Find first members that passes the reflection and SHG checks */
188           for (mi = bd_config->flood_count - 1; mi >= 0; mi--)
189             {
190               member = &bd_config->members[mi];
191               if ((member->sw_if_index != sw_if_index0) &&
192                   (!in_shg || (member->shg != in_shg)))
193                 {
194                   vec_add1 (msm->members[thread_index], member);
195                 }
196             }
197
198           n_clones = vec_len (msm->members[thread_index]);
199
200           if (0 == n_clones)
201             {
202               /* No members to flood to */
203               to_next[0] = bi0;
204               to_next += 1;
205               n_left_to_next -= 1;
206
207               b0->error = node->errors[L2FLOOD_ERROR_NO_MEMBERS];
208               vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
209                                                to_next, n_left_to_next,
210                                                bi0, L2FLOOD_NEXT_DROP);
211               continue;
212             }
213           else if (n_clones > 1)
214             {
215               vec_validate (msm->clones[thread_index], n_clones);
216               vec_reset_length (msm->clones[thread_index]);
217
218               /*
219                * the header offset needs to be large enough to incorporate
220                * all the L3 headers that could be touched when doing BVI
221                * processing. So take the current l2 length plus 2 * IPv6
222                * headers (for tunnel encap)
223                */
224               n_cloned = vlib_buffer_clone (vm, bi0,
225                                             msm->clones[thread_index],
226                                             n_clones,
227                                             VLIB_BUFFER_CLONE_HEAD_SIZE);
228
229               if (PREDICT_FALSE (n_cloned != n_clones))
230                 {
231                   b0->error = node->errors[L2FLOOD_ERROR_REPL_FAIL];
232                 }
233
234               /*
235                * for all but the last clone, these are not BVI bound
236                */
237               for (clone0 = 0; clone0 < n_cloned - 1; clone0++)
238                 {
239                   member = msm->members[thread_index][clone0];
240                   ci0 = msm->clones[thread_index][clone0];
241                   c0 = vlib_get_buffer (vm, ci0);
242
243                   to_next[0] = ci0;
244                   to_next += 1;
245                   n_left_to_next -= 1;
246
247                   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
248                                      (b0->flags & VLIB_BUFFER_IS_TRACED)))
249                     {
250                       ethernet_header_t *h0;
251                       l2flood_trace_t *t;
252
253                       t = vlib_add_trace (vm, node, c0, sizeof (*t));
254                       h0 = vlib_buffer_get_current (c0);
255                       t->sw_if_index = sw_if_index0;
256                       t->bd_index = vnet_buffer (c0)->l2.bd_index;
257                       clib_memcpy_fast (t->src, h0->src_address, 6);
258                       clib_memcpy_fast (t->dst, h0->dst_address, 6);
259                     }
260
261                   /* Do normal L2 forwarding */
262                   vnet_buffer (c0)->sw_if_index[VLIB_TX] =
263                     member->sw_if_index;
264
265                   vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
266                                                    to_next, n_left_to_next,
267                                                    ci0, next0);
268                   if (PREDICT_FALSE (0 == n_left_to_next))
269                     {
270                       vlib_put_next_frame (vm, node, next_index,
271                                            n_left_to_next);
272                       vlib_get_next_frame (vm, node, next_index, to_next,
273                                            n_left_to_next);
274                     }
275                 }
276               member = msm->members[thread_index][clone0];
277               ci0 = msm->clones[thread_index][clone0];
278             }
279           else
280             {
281               /* one clone */
282               ci0 = bi0;
283               member = msm->members[thread_index][0];
284             }
285
286           /*
287            * the last clone that might go to a BVI
288            */
289           c0 = vlib_get_buffer (vm, ci0);
290
291           to_next[0] = ci0;
292           to_next += 1;
293           n_left_to_next -= 1;
294
295           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
296                              (b0->flags & VLIB_BUFFER_IS_TRACED)))
297             {
298               ethernet_header_t *h0;
299               l2flood_trace_t *t;
300
301               t = vlib_add_trace (vm, node, c0, sizeof (*t));
302               h0 = vlib_buffer_get_current (c0);
303               t->sw_if_index = sw_if_index0;
304               t->bd_index = vnet_buffer (c0)->l2.bd_index;
305               clib_memcpy_fast (t->src, h0->src_address, 6);
306               clib_memcpy_fast (t->dst, h0->dst_address, 6);
307             }
308
309
310           /* Forward packet to the current member */
311           if (PREDICT_FALSE (member->flags & L2_FLOOD_MEMBER_BVI))
312             {
313               /* Do BVI processing */
314               u32 rc;
315               rc = l2_to_bvi (vm,
316                               msm->vnet_main,
317                               c0, member->sw_if_index, &msm->l3_next, &next0);
318
319               if (PREDICT_FALSE (rc != TO_BVI_ERR_OK))
320                 {
321                   if (rc == TO_BVI_ERR_BAD_MAC)
322                     {
323                       c0->error = node->errors[L2FLOOD_ERROR_BVI_BAD_MAC];
324                     }
325                   else if (rc == TO_BVI_ERR_ETHERTYPE)
326                     {
327                       c0->error = node->errors[L2FLOOD_ERROR_BVI_ETHERTYPE];
328                     }
329                   next0 = L2FLOOD_NEXT_DROP;
330                 }
331             }
332           else
333             {
334               /* Do normal L2 forwarding */
335               vnet_buffer (c0)->sw_if_index[VLIB_TX] = member->sw_if_index;
336             }
337
338           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
339                                            to_next, n_left_to_next,
340                                            ci0, next0);
341           if (PREDICT_FALSE (0 == n_left_to_next))
342             {
343               vlib_put_next_frame (vm, node, next_index, n_left_to_next);
344               vlib_get_next_frame (vm, node, next_index,
345                                    to_next, n_left_to_next);
346             }
347         }
348
349       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
350     }
351
352   vlib_node_increment_counter (vm, node->node_index,
353                                L2FLOOD_ERROR_L2FLOOD, frame->n_vectors);
354
355   return frame->n_vectors;
356 }
357
358
359 /* *INDENT-OFF* */
360 VLIB_REGISTER_NODE (l2flood_node) = {
361   .name = "l2-flood",
362   .vector_size = sizeof (u32),
363   .format_trace = format_l2flood_trace,
364   .type = VLIB_NODE_TYPE_INTERNAL,
365
366   .n_errors = ARRAY_LEN(l2flood_error_strings),
367   .error_strings = l2flood_error_strings,
368
369   .n_next_nodes = L2FLOOD_N_NEXT,
370
371   /* edit / add dispositions here */
372   .next_nodes = {
373         [L2FLOOD_NEXT_L2_OUTPUT] = "l2-output",
374         [L2FLOOD_NEXT_DROP] = "error-drop",
375   },
376 };
377 /* *INDENT-ON* */
378
379 #ifndef CLIB_MARCH_VARIANT
380 clib_error_t *
381 l2flood_init (vlib_main_t * vm)
382 {
383   l2flood_main_t *mp = &l2flood_main;
384
385   mp->vlib_main = vm;
386   mp->vnet_main = vnet_get_main ();
387
388   vec_validate (mp->clones, vlib_num_workers ());
389   vec_validate (mp->members, vlib_num_workers ());
390
391   /* Initialize the feature next-node indexes */
392   feat_bitmap_init_next_nodes (vm,
393                                l2flood_node.index,
394                                L2INPUT_N_FEAT,
395                                l2input_get_feat_names (),
396                                mp->feat_next_node_index);
397
398   return NULL;
399 }
400
401 VLIB_INIT_FUNCTION (l2flood_init);
402
403
404
405 /** Add the L3 input node for this ethertype to the next nodes structure. */
406 void
407 l2flood_register_input_type (vlib_main_t * vm,
408                              ethernet_type_t type, u32 node_index)
409 {
410   l2flood_main_t *mp = &l2flood_main;
411   u32 next_index;
412
413   next_index = vlib_node_add_next (vm, l2flood_node.index, node_index);
414
415   next_by_ethertype_register (&mp->l3_next, type, next_index);
416 }
417 #endif /* CLIB_MARCH_VARIANT */
418
419
420 /**
421  * Set subinterface flood enable/disable.
422  * The CLI format is:
423  * set interface l2 flood <interface> [disable]
424  */
425 static clib_error_t *
426 int_flood (vlib_main_t * vm,
427            unformat_input_t * input, vlib_cli_command_t * cmd)
428 {
429   vnet_main_t *vnm = vnet_get_main ();
430   clib_error_t *error = 0;
431   u32 sw_if_index;
432   u32 enable;
433
434   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
435     {
436       error = clib_error_return (0, "unknown interface `%U'",
437                                  format_unformat_error, input);
438       goto done;
439     }
440
441   enable = 1;
442   if (unformat (input, "disable"))
443     {
444       enable = 0;
445     }
446
447   /* set the interface flag */
448   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_FLOOD, enable);
449
450 done:
451   return error;
452 }
453
454 /*?
455  * Layer 2 flooding can be enabled and disabled on each
456  * interface and on each bridge-domain. Use this command to
457  * manage interfaces. It is enabled by default.
458  *
459  * @cliexpar
460  * Example of how to enable flooding:
461  * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0}
462  * Example of how to disable flooding:
463  * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0 disable}
464 ?*/
465 /* *INDENT-OFF* */
466 VLIB_CLI_COMMAND (int_flood_cli, static) = {
467   .path = "set interface l2 flood",
468   .short_help = "set interface l2 flood <interface> [disable]",
469   .function = int_flood,
470 };
471 /* *INDENT-ON* */
472
473 /*
474  * fd.io coding-style-patch-verification: ON
475  *
476  * Local Variables:
477  * eval: (c-set-style "gnu")
478  * End:
479  */