FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
l2_flood.c
Go to the documentation of this file.
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 */
56 
57  /* convenience variables */
60 
61  /* per-cpu vector of cloned packets */
65 
66 typedef struct
67 {
68  u8 src[6];
69  u8 dst[6];
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,
87  return s;
88 }
89 
91 
92 #ifndef CLIB_MARCH_VARIANT
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,
107 #undef _
110 
111 static char *l2flood_error_strings[] = {
112 #define _(sym,string) string,
114 #undef _
115 };
116 
117 typedef enum
118 {
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  */
141 {
142  u32 n_left_from, *from, *to_next;
143  l2flood_next_t next_index;
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 */
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 
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,
227 
228  vec_set_len (msm->clones[thread_index], n_cloned);
229 
230  if (PREDICT_FALSE (n_cloned != n_clones))
231  {
232  b0->error = node->errors[L2FLOOD_ERROR_REPL_FAIL];
233  /* Worst-case, no clones, consume the original buf */
234  if (n_cloned == 0)
235  {
236  ci0 = bi0;
237  member = msm->members[thread_index][0];
238  goto use_original_buffer;
239  }
240  }
241 
242  /*
243  * for all but the last clone, these are not BVI bound
244  */
245  for (clone0 = 0; clone0 < n_cloned - 1; clone0++)
246  {
247  member = msm->members[thread_index][clone0];
248  ci0 = msm->clones[thread_index][clone0];
249  c0 = vlib_get_buffer (vm, ci0);
250 
251  to_next[0] = ci0;
252  to_next += 1;
253  n_left_to_next -= 1;
254 
255  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
256  (b0->flags & VLIB_BUFFER_IS_TRACED)))
257  {
258  ethernet_header_t *h0;
259  l2flood_trace_t *t;
260 
261  t = vlib_add_trace (vm, node, c0, sizeof (*t));
262  h0 = vlib_buffer_get_current (c0);
263  t->sw_if_index = sw_if_index0;
264  t->bd_index = vnet_buffer (c0)->l2.bd_index;
265  clib_memcpy_fast (t->src, h0->src_address, 6);
266  clib_memcpy_fast (t->dst, h0->dst_address, 6);
267  }
268 
269  /* Do normal L2 forwarding */
270  vnet_buffer (c0)->sw_if_index[VLIB_TX] =
271  member->sw_if_index;
272 
273  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
274  to_next, n_left_to_next,
275  ci0, next0);
276  if (PREDICT_FALSE (0 == n_left_to_next))
277  {
278  vlib_put_next_frame (vm, node, next_index,
279  n_left_to_next);
280  vlib_get_next_frame (vm, node, next_index, to_next,
281  n_left_to_next);
282  }
283  }
284  member = msm->members[thread_index][clone0];
285  ci0 = msm->clones[thread_index][clone0];
286  }
287  else
288  {
289  /* one clone */
290  ci0 = bi0;
291  member = msm->members[thread_index][0];
292  }
293 
294  use_original_buffer:
295  /*
296  * the last clone that might go to a BVI
297  */
298  c0 = vlib_get_buffer (vm, ci0);
299 
300  to_next[0] = ci0;
301  to_next += 1;
302  n_left_to_next -= 1;
303 
304  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
305  (b0->flags & VLIB_BUFFER_IS_TRACED)))
306  {
307  ethernet_header_t *h0;
308  l2flood_trace_t *t;
309 
310  t = vlib_add_trace (vm, node, c0, sizeof (*t));
311  h0 = vlib_buffer_get_current (c0);
312  t->sw_if_index = sw_if_index0;
313  t->bd_index = vnet_buffer (c0)->l2.bd_index;
314  clib_memcpy_fast (t->src, h0->src_address, 6);
315  clib_memcpy_fast (t->dst, h0->dst_address, 6);
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* */
368  .name = "l2-flood",
369  .vector_size = sizeof (u32),
370  .format_trace = format_l2flood_trace,
372 
373  .n_errors = ARRAY_LEN(l2flood_error_strings),
374  .error_strings = l2flood_error_strings,
375 
376  .n_next_nodes = L2FLOOD_N_NEXT,
377 
378  /* edit / add dispositions here */
379  .next_nodes = {
380  [L2FLOOD_NEXT_L2_OUTPUT] = "l2-output",
381  [L2FLOOD_NEXT_DROP] = "error-drop",
382  },
383 };
384 /* *INDENT-ON* */
385 
386 #ifndef CLIB_MARCH_VARIANT
387 clib_error_t *
389 {
391 
392  mp->vlib_main = vm;
393  mp->vnet_main = vnet_get_main ();
394 
397 
398  /* Initialize the feature next-node indexes */
400  l2flood_node.index,
404 
405  return NULL;
406 }
407 
409 
410 
411 
412 /** Add the L3 input node for this ethertype to the next nodes structure. */
413 void
415  ethernet_type_t type, u32 node_index)
416 {
418  u32 next_index;
419 
420  next_index = vlib_node_add_next (vm, l2flood_node.index, node_index);
421 
422  next_by_ethertype_register (&mp->l3_next, type, next_index);
423 }
424 #endif /* CLIB_MARCH_VARIANT */
425 
426 
427 /**
428  * Set subinterface flood enable/disable.
429  * The CLI format is:
430  * set interface l2 flood <interface> [disable]
431  */
432 static clib_error_t *
434  unformat_input_t * input, vlib_cli_command_t * cmd)
435 {
436  vnet_main_t *vnm = vnet_get_main ();
437  clib_error_t *error = 0;
439  u32 enable;
440 
441  if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
442  {
443  error = clib_error_return (0, "unknown interface `%U'",
444  format_unformat_error, input);
445  goto done;
446  }
447 
448  enable = 1;
449  if (unformat (input, "disable"))
450  {
451  enable = 0;
452  }
453 
454  /* set the interface flag */
455  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_FLOOD, enable);
456 
457 done:
458  return error;
459 }
460 
461 /*?
462  * Layer 2 flooding can be enabled and disabled on each
463  * interface and on each bridge-domain. Use this command to
464  * manage interfaces. It is enabled by default.
465  *
466  * @cliexpar
467  * Example of how to enable flooding:
468  * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0}
469  * Example of how to disable flooding:
470  * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0 disable}
471 ?*/
472 /* *INDENT-OFF* */
473 VLIB_CLI_COMMAND (int_flood_cli, static) = {
474  .path = "set interface l2 flood",
475  .short_help = "set interface l2 flood <interface> [disable]",
476  .function = int_flood,
477 };
478 /* *INDENT-ON* */
479 
480 /*
481  * fd.io coding-style-patch-verification: ON
482  *
483  * Local Variables:
484  * eval: (c-set-style "gnu")
485  * End:
486  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:440
u32 feat_next_node_index[32]
Definition: l2_flood.c:52
u32 ** clones
Definition: l2_flood.c:62
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
vnet_main_t * vnet_main
Definition: l2_flood.c:59
l2flood_next_t
Definition: l2_flood.c:117
#define vec_set_len(v, l)
Set vector length to a user-defined value.
#define CLIB_UNUSED(x)
Definition: clib.h:82
#define VLIB_BUFFER_CLONE_HEAD_SIZE
Definition: buffer.h:61
#define TO_BVI_ERR_BAD_MAC
Definition: l2_bvi.h:28
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define NULL
Definition: clib.h:58
ethernet_type_t
Definition: packet.h:45
#define TO_BVI_ERR_ETHERTYPE
Definition: l2_bvi.h:29
u8 src_address[6]
Definition: packet.h:56
u32 thread_index
Definition: main.h:218
vlib_main_t * vlib_main
Definition: l2_flood.c:58
l2flood_main_t l2flood_main
Definition: l2_flood.c:93
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
vl_api_address_t src
Definition: gre.api:60
void l2flood_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Add the L3 input node for this ethertype to the next nodes structure.
Definition: l2_flood.c:414
l2_flood_member_t * members
Definition: l2_bd.h:86
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
static char * l2flood_error_strings[]
Definition: l2_flood.c:111
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
#define VLIB_NODE_FN(node)
Definition: node.h:202
#define L2_FLOOD_MEMBER_BVI
Definition: l2_bd.h:51
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1092
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
u8 dst_address[6]
Definition: packet.h:55
static u8 * format_l2flood_trace(u8 *s, va_list *args)
Definition: l2_flood.c:77
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
vl_api_address_t dst
Definition: gre.api:61
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
vlib_main_t * vm
Definition: in2out_ed.c:1810
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
clib_error_t * next_by_ethertype_register(next_by_ethertype_t *l3_next, u32 ethertype, u32 next_index)
Definition: node.c:2198
static u16 vlib_buffer_clone(vlib_main_t *vm, u32 src_buffer, u32 *buffers, u16 n_buffers, u16 head_end_offset)
Create multiple clones of buffer and store them in the supplied array.
vlib_node_registration_t l2flood_node
(constructor) VLIB_REGISTER_NODE (l2flood_node)
Definition: l2_flood.c:367
static void feat_bitmap_init_next_nodes(vlib_main_t *vm, u32 node_index, u32 num_features, char **feat_names, u32 *next_nodes)
Initialize the feature next-node indexes of a graph node.
Definition: feat_bitmap.h:43
l2_flood_member_t *** members
Definition: l2_flood.c:63
#define ARRAY_LEN(x)
Definition: clib.h:62
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:456
char ** l2input_get_feat_names(void)
Return an array of strings containing graph node names of each feature.
Definition: l2_input.c:62
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
signed int i32
Definition: types.h:77
u32 l2input_intf_bitmap_enable(u32 sw_if_index, l2input_feat_masks_t feature_bitmap, u32 enable)
Enable (or disable) the feature in the bitmap for the given interface.
Definition: l2_input.c:538
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
Definition: defs.h:47
l2input_main_t l2input_main
Definition: l2_input.c:128
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define foreach_l2flood_error
Definition: l2_flood.c:96
VLIB buffer representation.
Definition: buffer.h:102
l2flood_error_t
Definition: l2_flood.c:103
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
l2_bridge_domain_t * bd_configs
Definition: l2_input.h:65
#define vnet_buffer(b)
Definition: buffer.h:408
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static u32 vlib_num_workers()
Definition: threads.h:372
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
u32 sw_if_index
Definition: l2_bd.h:55
#define TO_BVI_ERR_OK
Definition: l2_bvi.h:27
next_by_ethertype_t l3_next
Definition: l2_flood.c:55
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
static clib_error_t * int_flood(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Set subinterface flood enable/disable.
Definition: l2_flood.c:433
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
Definition: defs.h:46
static_always_inline u32 l2_to_bvi(vlib_main_t *vlib_main, vnet_main_t *vnet_main, vlib_buffer_t *b0, u32 bvi_sw_if_index, next_by_ethertype_t *l3_next, u16 *next0)
Send a packet from L2 processing to L3 via the BVI interface.
Definition: l2_bvi.h:38
clib_error_t * l2flood_init(vlib_main_t *vm)
Definition: l2_flood.c:388