2 * l2_flood.c : layer 2 flooding
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vnet/ethernet/ethernet.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>
28 #include <vppinfra/error.h>
29 #include <vppinfra/hash.h>
34 * @brief Ethernet Flooding.
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.
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().
51 /* Next nodes for each feature */
52 u32 feat_next_node_index[32];
54 /* next node index for the L3 input node of each ethertype */
55 next_by_ethertype_t l3_next;
57 /* convenience variables */
58 vlib_main_t *vlib_main;
59 vnet_main_t *vnet_main;
61 /* per-cpu vector of cloned packets */
63 l2_flood_member_t ***members;
75 /* packet trace format function */
77 format_l2flood_trace (u8 * s, va_list * args)
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 *);
83 s = format (s, "l2-flood: sw_if_index %d dst %U src %U bd_index %d",
85 format_ethernet_address, t->dst,
86 format_ethernet_address, t->src, t->bd_index);
90 l2flood_main_t l2flood_main;
92 static vlib_node_registration_t l2flood_node;
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")
103 #define _(sym,str) L2FLOOD_ERROR_##sym,
104 foreach_l2flood_error
109 static char *l2flood_error_strings[] = {
110 #define _(sym,string) string,
111 foreach_l2flood_error
117 L2FLOOD_NEXT_L2_OUTPUT,
123 * Perform flooding on one packet
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.
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.
138 l2flood_node_fn (vlib_main_t * vm,
139 vlib_node_runtime_t * node, vlib_frame_t * frame)
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;
146 from = vlib_frame_vector_args (frame);
147 n_left_from = frame->n_vectors;
148 next_index = node->cached_next_index;
150 while (n_left_from > 0)
154 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
156 while (n_left_from > 0 && n_left_to_next > 0)
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;
167 /* speculatively enqueue b0 to the current next frame */
171 next0 = L2FLOOD_NEXT_L2_OUTPUT;
173 b0 = vlib_get_buffer (vm, bi0);
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];
181 vec_validate (msm->members[thread_index],
182 vec_len (bd_config->members));
184 vec_reset_length (msm->members[thread_index]);
186 /* Find first members that passes the reflection and SHG checks */
187 for (mi = bd_config->flood_count - 1; mi >= 0; mi--)
189 member = &bd_config->members[mi];
190 if ((member->sw_if_index != sw_if_index0) &&
191 (!in_shg || (member->shg != in_shg)))
193 vec_add1 (msm->members[thread_index], member);
197 n_clones = vec_len (msm->members[thread_index]);
201 /* No members to flood to */
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);
212 else if (n_clones > 1)
214 vec_validate (msm->clones[thread_index], n_clones);
215 vec_reset_length (msm->clones[thread_index]);
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)
223 n_cloned = vlib_buffer_clone (vm, bi0,
224 msm->clones[thread_index],
226 VLIB_BUFFER_CLONE_HEAD_SIZE);
228 if (PREDICT_FALSE (n_cloned != n_clones))
230 b0->error = node->errors[L2FLOOD_ERROR_REPL_FAIL];
234 * for all but the last clone, these are not BVI bound
236 for (clone0 = 0; clone0 < n_cloned - 1; clone0++)
238 member = msm->members[thread_index][clone0];
239 ci0 = msm->clones[thread_index][clone0];
240 c0 = vlib_get_buffer (vm, ci0);
246 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
247 (b0->flags & VLIB_BUFFER_IS_TRACED)))
249 ethernet_header_t *h0;
253 vlib_buffer_copy_trace_flag (vm, b0, ci0);
255 t = vlib_add_trace (vm, node, c0, sizeof (*t));
256 h0 = vlib_buffer_get_current (c0);
257 t->sw_if_index = sw_if_index0;
258 t->bd_index = vnet_buffer (c0)->l2.bd_index;
259 clib_memcpy_fast (t->src, h0->src_address, 6);
260 clib_memcpy_fast (t->dst, h0->dst_address, 6);
263 /* Do normal L2 forwarding */
264 vnet_buffer (c0)->sw_if_index[VLIB_TX] =
267 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
268 to_next, n_left_to_next,
270 if (PREDICT_FALSE (0 == n_left_to_next))
272 vlib_put_next_frame (vm, node, next_index,
274 vlib_get_next_frame (vm, node, next_index, to_next,
278 member = msm->members[thread_index][clone0];
279 ci0 = msm->clones[thread_index][clone0];
285 member = msm->members[thread_index][0];
289 * the last clone that might go to a BVI
291 c0 = vlib_get_buffer (vm, ci0);
297 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
298 (b0->flags & VLIB_BUFFER_IS_TRACED)))
300 ethernet_header_t *h0;
304 vlib_buffer_copy_trace_flag (vm, b0, ci0);
306 t = vlib_add_trace (vm, node, c0, sizeof (*t));
307 h0 = vlib_buffer_get_current (c0);
308 t->sw_if_index = sw_if_index0;
309 t->bd_index = vnet_buffer (c0)->l2.bd_index;
310 clib_memcpy_fast (t->src, h0->src_address, 6);
311 clib_memcpy_fast (t->dst, h0->dst_address, 6);
315 /* Forward packet to the current member */
316 if (PREDICT_FALSE (member->flags & L2_FLOOD_MEMBER_BVI))
318 /* Do BVI processing */
322 c0, member->sw_if_index, &msm->l3_next, &next0);
324 if (PREDICT_FALSE (rc != TO_BVI_ERR_OK))
326 if (rc == TO_BVI_ERR_BAD_MAC)
328 c0->error = node->errors[L2FLOOD_ERROR_BVI_BAD_MAC];
330 else if (rc == TO_BVI_ERR_ETHERTYPE)
332 c0->error = node->errors[L2FLOOD_ERROR_BVI_ETHERTYPE];
334 next0 = L2FLOOD_NEXT_DROP;
339 /* Do normal L2 forwarding */
340 vnet_buffer (c0)->sw_if_index[VLIB_TX] = member->sw_if_index;
343 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
344 to_next, n_left_to_next,
346 if (PREDICT_FALSE (0 == n_left_to_next))
348 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
349 vlib_get_next_frame (vm, node, next_index,
350 to_next, n_left_to_next);
354 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
357 vlib_node_increment_counter (vm, node->node_index,
358 L2FLOOD_ERROR_L2FLOOD, frame->n_vectors);
360 return frame->n_vectors;
365 VLIB_REGISTER_NODE (l2flood_node,static) = {
366 .function = l2flood_node_fn,
368 .vector_size = sizeof (u32),
369 .format_trace = format_l2flood_trace,
370 .type = VLIB_NODE_TYPE_INTERNAL,
372 .n_errors = ARRAY_LEN(l2flood_error_strings),
373 .error_strings = l2flood_error_strings,
375 .n_next_nodes = L2FLOOD_N_NEXT,
377 /* edit / add dispositions here */
379 [L2FLOOD_NEXT_L2_OUTPUT] = "l2-output",
380 [L2FLOOD_NEXT_DROP] = "error-drop",
384 VLIB_NODE_FUNCTION_MULTIARCH (l2flood_node, l2flood_node_fn)
388 l2flood_init (vlib_main_t * vm)
390 l2flood_main_t *mp = &l2flood_main;
393 mp->vnet_main = vnet_get_main ();
395 vec_validate (mp->clones, vlib_num_workers ());
396 vec_validate (mp->members, vlib_num_workers ());
398 /* Initialize the feature next-node indexes */
399 feat_bitmap_init_next_nodes (vm,
402 l2input_get_feat_names (),
403 mp->feat_next_node_index);
408 VLIB_INIT_FUNCTION (l2flood_init);
412 /** Add the L3 input node for this ethertype to the next nodes structure. */
414 l2flood_register_input_type (vlib_main_t * vm,
415 ethernet_type_t type, u32 node_index)
417 l2flood_main_t *mp = &l2flood_main;
420 next_index = vlib_node_add_next (vm, l2flood_node.index, node_index);
422 next_by_ethertype_register (&mp->l3_next, type, next_index);
427 * Set subinterface flood enable/disable.
429 * set interface l2 flood <interface> [disable]
431 static clib_error_t *
432 int_flood (vlib_main_t * vm,
433 unformat_input_t * input, vlib_cli_command_t * cmd)
435 vnet_main_t *vnm = vnet_get_main ();
436 clib_error_t *error = 0;
440 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
442 error = clib_error_return (0, "unknown interface `%U'",
443 format_unformat_error, input);
448 if (unformat (input, "disable"))
453 /* set the interface flag */
454 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_FLOOD, enable);
461 * Layer 2 flooding can be enabled and disabled on each
462 * interface and on each bridge-domain. Use this command to
463 * manage interfaces. It is enabled by default.
466 * Example of how to enable flooding:
467 * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0}
468 * Example of how to disable flooding:
469 * @cliexcmd{set interface l2 flood GigabitEthernet0/8/0 disable}
472 VLIB_CLI_COMMAND (int_flood_cli, static) = {
473 .path = "set interface l2 flood",
474 .short_help = "set interface l2 flood <interface> [disable]",
475 .function = int_flood,
480 * fd.io coding-style-patch-verification: ON
483 * eval: (c-set-style "gnu")