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/replication.h>
27 #include <vnet/l2/l2_fib.h>
29 #include <vppinfra/error.h>
30 #include <vppinfra/hash.h>
34 * Flooding uses the packet replication infrastructure to send a copy of the
35 * packet to each member interface. Logically the replication infrastructure
36 * expects two graph nodes: a prep node that initiates replication and sends the
37 * packet to the first destination, and a recycle node that is passed the packet
38 * after it has been transmitted.
40 * To decrease the amount of code, l2 flooding implements both functions in
41 * the same graph node. This node can tell if is it being called as the "prep"
42 * or "recycle" using replication_is_recycled().
48 // Next nodes for each feature
49 u32 feat_next_node_index[32];
51 // next node index for the L3 input node of each ethertype
52 next_by_ethertype_t l3_next;
54 /* convenience variables */
55 vlib_main_t * vlib_main;
56 vnet_main_t * vnet_main;
67 /* packet trace format function */
68 static u8 * format_l2flood_trace (u8 * s, va_list * args)
70 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
72 l2flood_trace_t * t = va_arg (*args, l2flood_trace_t *);
74 s = format (s, "l2-flood: sw_if_index %d dst %U src %U bd_index %d",
76 format_ethernet_address, t->dst,
77 format_ethernet_address, t->src,
82 l2flood_main_t l2flood_main;
84 static vlib_node_registration_t l2flood_node;
86 #define foreach_l2flood_error \
87 _(L2FLOOD, "L2 flood packets") \
88 _(REPL_FAIL, "L2 replication failures") \
89 _(NO_MEMBERS, "L2 replication complete") \
90 _(BVI_TAGGED, "BVI packet with vlan tag") \
91 _(BVI_ETHERTYPE, "BVI packet with unhandled ethertype")
94 #define _(sym,str) L2FLOOD_ERROR_##sym,
100 static char * l2flood_error_strings[] = {
101 #define _(sym,string) string,
102 foreach_l2flood_error
107 L2FLOOD_NEXT_L2_OUTPUT,
113 * Perform flooding on one packet
115 * Due to the way BVI processing can modify the packet, the BVI interface
116 * (if present) must be processed last in the replication. The member vector
117 * is arranged so that the BVI interface is always the first element.
118 * Flooding walks the vector in reverse.
120 * BVI processing causes the packet to go to L3 processing. This strips the
121 * L2 header, which is fine because the replication infrastructure restores
122 * it. However L3 processing can trigger larger changes to the packet. For
123 * example, an ARP request could be turned into an ARP reply, an ICMP request
124 * could be turned into an ICMP reply. If BVI processing is not performed
125 * last, the modified packet would be replicated to the remaining members.
128 static_always_inline void
129 l2flood_process (vlib_main_t * vm,
130 vlib_node_runtime_t * node,
131 l2flood_main_t * msm,
135 l2fib_entry_key_t * key0,
137 l2fib_entry_result_t * result0,
141 l2_bridge_domain_t *bd_config;
142 l2_flood_member_t * members;
143 i32 current_member; // signed
144 replication_context_t * ctx;
145 u8 in_shg = vnet_buffer(b0)->l2.shg;
147 if (!replication_is_recycled(b0)) {
149 // Do flood "prep node" processing
151 // Get config for the bridge domain interface
152 bd_index0 = vnet_buffer(b0)->l2.bd_index;
153 bd_config = vec_elt_at_index(l2input_main.bd_configs, bd_index0);
154 members = bd_config->members;
156 // Find first member that passes the reflection and SHG checks
157 current_member = vec_len(members) - 1;
158 while ((current_member >= 0) &&
159 ((members[current_member].sw_if_index == sw_if_index0) ||
160 (in_shg && members[current_member].shg == in_shg))) {
164 if (current_member < 0) {
165 // No members to flood to
166 *next0 = L2FLOOD_NEXT_DROP;
167 b0->error = node->errors[L2FLOOD_ERROR_NO_MEMBERS];
171 if ((current_member > 0) &&
172 ((current_member > 1) ||
173 ((members[0].sw_if_index != sw_if_index0) &&
174 (!in_shg || members[0].shg != in_shg)))) {
175 // If more than one member then initiate replication
176 ctx = replication_prep (vm, b0, l2flood_node.index, 1 /* l2_packet */);
177 ctx->feature_replicas = (u64) members;
178 ctx->feature_counter = current_member;
183 // Do flood "recycle node" processing
185 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_REPL_FAIL))
187 (void)replication_recycle (vm, b0, 1 /* is_last */);
188 *next0 = L2FLOOD_NEXT_DROP;
189 b0->error = node->errors[L2FLOOD_ERROR_REPL_FAIL];
193 ctx = replication_get_ctx (b0);
194 replication_clear_recycled (b0);
196 members = (l2_flood_member_t *) ctx->feature_replicas;
197 current_member = (i32)ctx->feature_counter - 1;
199 // Find next member that passes the reflection and SHG check
200 while ((current_member >= 0) &&
201 ((members[current_member].sw_if_index == sw_if_index0) ||
202 (in_shg && members[current_member].shg == in_shg))) {
206 if (current_member < 0) {
207 // No more members to flood to.
208 // Terminate replication and drop packet.
210 replication_recycle (vm, b0, 1 /* is_last */);
212 *next0 = L2FLOOD_NEXT_DROP;
213 // Ideally we woudn't bump a counter here, just silently complete
214 b0->error = node->errors[L2FLOOD_ERROR_NO_MEMBERS];
218 // Restore packet and context and continue replication
219 ctx->feature_counter = current_member;
220 replication_recycle (vm, b0,
221 ((current_member == 0) || /*is_last */
222 ((current_member == 1) &&
223 ((members[0].sw_if_index == sw_if_index0) ||
224 (in_shg && members[0].shg == in_shg)))));
227 // Forward packet to the current member
229 if (PREDICT_TRUE(members[current_member].flags == L2_FLOOD_MEMBER_NORMAL)) {
230 // Do normal L2 forwarding
231 vnet_buffer(b0)->sw_if_index[VLIB_TX] = members[current_member].sw_if_index;
232 *next0 = L2FLOOD_NEXT_L2_OUTPUT;
240 members[current_member].sw_if_index,
244 if (PREDICT_FALSE(rc)) {
245 if (rc == TO_BVI_ERR_TAGGED) {
246 b0->error = node->errors[L2FLOOD_ERROR_BVI_TAGGED];
247 *next0 = L2FLOOD_NEXT_DROP;
248 } else if (rc == TO_BVI_ERR_ETHERTYPE) {
249 b0->error = node->errors[L2FLOOD_ERROR_BVI_ETHERTYPE];
250 *next0 = L2FLOOD_NEXT_DROP;
259 l2flood_node_fn (vlib_main_t * vm,
260 vlib_node_runtime_t * node,
261 vlib_frame_t * frame)
263 u32 n_left_from, * from, * to_next;
264 l2flood_next_t next_index;
265 l2flood_main_t * msm = &l2flood_main;
266 vlib_node_t *n = vlib_get_node (vm, l2flood_node.index);
267 u32 node_counter_base_index = n->error_heap_index;
268 vlib_error_main_t * em = &vm->error_main;
270 from = vlib_frame_vector_args (frame);
271 n_left_from = frame->n_vectors; /* number of packets to process */
272 next_index = node->cached_next_index;
274 while (n_left_from > 0)
278 /* get space to enqueue frame to graph node "next_index" */
279 vlib_get_next_frame (vm, node, next_index,
280 to_next, n_left_to_next);
282 while (n_left_from >= 6 && n_left_to_next >= 2)
285 vlib_buffer_t * b0, * b1;
287 u32 sw_if_index0, sw_if_index1;
288 ethernet_header_t * h0, * h1;
289 l2fib_entry_key_t key0, key1;
290 l2fib_entry_result_t result0, result1;
291 u32 bucket0, bucket1;
293 /* Prefetch next iteration. */
295 vlib_buffer_t * p2, * p3, * p4, * p5;
297 p2 = vlib_get_buffer (vm, from[2]);
298 p3 = vlib_get_buffer (vm, from[3]);
299 p4 = vlib_get_buffer (vm, from[4]);
300 p5 = vlib_get_buffer (vm, from[5]);
302 // Prefetch the buffer header for the N+2 loop iteration
303 vlib_prefetch_buffer_header (p4, LOAD);
304 vlib_prefetch_buffer_header (p5, LOAD);
306 // Prefetch the replication context for the N+1 loop iteration
307 // This depends on the buffer header above
308 replication_prefetch_ctx (p2);
309 replication_prefetch_ctx (p3);
311 // Prefetch the packet for the N+1 loop iteration
312 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
313 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
316 /* speculatively enqueue b0 and b1 to the current next frame */
317 /* bi is "buffer index", b is pointer to the buffer */
318 to_next[0] = bi0 = from[0];
319 to_next[1] = bi1 = from[1];
325 b0 = vlib_get_buffer (vm, bi0);
326 b1 = vlib_get_buffer (vm, bi1);
328 /* RX interface handles */
329 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
330 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
332 /* Process 2 x pkts */
334 h0 = vlib_buffer_get_current (b0);
335 h1 = vlib_buffer_get_current (b1);
338 em->counters[node_counter_base_index + L2FLOOD_ERROR_L2FLOOD] += 2;
340 l2flood_process (vm, node, msm, &em->counters[node_counter_base_index],
341 b0, sw_if_index0, &key0, &bucket0, &result0, &next0);
343 l2flood_process (vm, node, msm, &em->counters[node_counter_base_index],
344 b1, sw_if_index1, &key1, &bucket1, &result1, &next1);
346 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
348 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
350 l2flood_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
351 t->sw_if_index = sw_if_index0;
352 t->bd_index = vnet_buffer(b0)->l2.bd_index;
353 memcpy(t->src, h0->src_address, 6);
354 memcpy(t->dst, h0->dst_address, 6);
356 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
358 l2flood_trace_t *t = vlib_add_trace (vm, node, b1, sizeof (*t));
359 t->sw_if_index = sw_if_index1;
360 t->bd_index = vnet_buffer(b1)->l2.bd_index;
361 memcpy(t->src, h1->src_address, 6);
362 memcpy(t->dst, h1->dst_address, 6);
366 /* verify speculative enqueues, maybe switch current next frame */
367 /* if next0==next1==next_index then nothing special needs to be done */
368 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
369 to_next, n_left_to_next,
370 bi0, bi1, next0, next1);
373 while (n_left_from > 0 && n_left_to_next > 0)
379 ethernet_header_t * h0;
380 l2fib_entry_key_t key0;
381 l2fib_entry_result_t result0;
384 /* speculatively enqueue b0 to the current next frame */
392 b0 = vlib_get_buffer (vm, bi0);
394 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
396 h0 = vlib_buffer_get_current (b0);
399 em->counters[node_counter_base_index + L2FLOOD_ERROR_L2FLOOD] += 1;
401 l2flood_process (vm, node, msm, &em->counters[node_counter_base_index],
402 b0, sw_if_index0, &key0, &bucket0, &result0, &next0);
404 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) &&
405 (b0->flags & VLIB_BUFFER_IS_TRACED)))
407 l2flood_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
408 t->sw_if_index = sw_if_index0;
409 t->bd_index = vnet_buffer(b0)->l2.bd_index;
410 memcpy(t->src, h0->src_address, 6);
411 memcpy(t->dst, h0->dst_address, 6);
414 /* verify speculative enqueue, maybe switch current next frame */
415 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
416 to_next, n_left_to_next,
420 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
423 return frame->n_vectors;
427 VLIB_REGISTER_NODE (l2flood_node,static) = {
428 .function = l2flood_node_fn,
430 .vector_size = sizeof (u32),
431 .format_trace = format_l2flood_trace,
432 .type = VLIB_NODE_TYPE_INTERNAL,
434 .n_errors = ARRAY_LEN(l2flood_error_strings),
435 .error_strings = l2flood_error_strings,
437 .n_next_nodes = L2FLOOD_N_NEXT,
439 /* edit / add dispositions here */
441 [L2FLOOD_NEXT_L2_OUTPUT] = "l2-output",
442 [L2FLOOD_NEXT_DROP] = "error-drop",
446 clib_error_t *l2flood_init (vlib_main_t *vm)
448 l2flood_main_t * mp = &l2flood_main;
451 mp->vnet_main = vnet_get_main();
453 // Initialize the feature next-node indexes
454 feat_bitmap_init_next_nodes(vm,
457 l2input_get_feat_names(),
458 mp->feat_next_node_index);
463 VLIB_INIT_FUNCTION (l2flood_init);
467 // Add the L3 input node for this ethertype to the next nodes structure
469 l2flood_register_input_type (vlib_main_t * vm,
470 ethernet_type_t type,
473 l2flood_main_t * mp = &l2flood_main;
476 next_index = vlib_node_add_next (vm,
480 next_by_ethertype_register (&mp->l3_next, type, next_index);
484 // set subinterface flood enable/disable
485 // The CLI format is:
486 // set interface l2 flood <interface> [disable]
487 static clib_error_t *
488 int_flood (vlib_main_t * vm,
489 unformat_input_t * input,
490 vlib_cli_command_t * cmd)
492 vnet_main_t * vnm = vnet_get_main();
493 clib_error_t * error = 0;
497 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
499 error = clib_error_return (0, "unknown interface `%U'",
500 format_unformat_error, input);
505 if (unformat (input, "disable")) {
509 // set the interface flag
510 l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_FLOOD, enable);
516 VLIB_CLI_COMMAND (int_flood_cli, static) = {
517 .path = "set interface l2 flood",
518 .short_help = "set interface l2 flood <interface> [disable]",
519 .function = int_flood,