2 * l2_output.c : layer 2 output packet processing
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>
24 #include <vppinfra/error.h>
25 #include <vppinfra/hash.h>
26 #include <vnet/l2/feat_bitmap.h>
27 #include <vnet/l2/l2_output.h>
30 /* Feature graph node names */
31 static char *l2output_feat_names[] = {
32 #define _(sym,name) name,
38 l2output_get_feat_names (void)
40 return l2output_feat_names;
44 format_l2_output_features (u8 * s, va_list * args)
46 static char *display_names[] = {
47 #define _(sym,name) #sym,
51 u32 feature_bitmap = va_arg (*args, u32);
53 if (feature_bitmap == 0)
55 s = format (s, " none configured");
60 for (i = L2OUTPUT_N_FEAT - 1; i >= 0; i--)
61 if (feature_bitmap & (1 << i))
62 s = format (s, "%10s (%s)\n", display_names[i], l2output_feat_names[i]);
66 l2output_main_t l2output_main;
70 /* per-pkt trace data */
74 u8 raw[12]; /* raw data */
77 /* packet trace format function */
79 format_l2output_trace (u8 * s, va_list * args)
81 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
82 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
83 l2output_trace_t *t = va_arg (*args, l2output_trace_t *);
85 s = format (s, "l2-output: sw_if_index %d dst %U src %U data "
86 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
88 format_ethernet_address, t->dst,
89 format_ethernet_address, t->src,
90 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
91 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
92 t->raw[10], t->raw[11]);
98 static char *l2output_error_strings[] = {
99 #define _(sym,string) string,
100 foreach_l2output_error
105 * Check for split horizon violations.
106 * Return 0 if split horizon check passes, otherwise return non-zero.
107 * Packets should not be transmitted out an interface with the same
108 * split-horizon group as the input interface, except if the @c shg is 0
109 * in which case the check always passes.
111 static_always_inline u32
112 split_horizon_violation (u8 shg1, u8 shg2)
114 if (PREDICT_TRUE (shg1 == 0))
124 /** Determine the next L2 node based on the output feature bitmap */
125 static_always_inline void
126 l2_output_dispatch (vlib_buffer_t * b0, vlib_node_runtime_t * node,
127 u32 * cached_sw_if_index, u32 * cached_next_index,
128 u32 sw_if_index, u32 feature_bitmap, u32 * next0)
131 * The output feature bitmap always have at least the L2 output bit set
132 * for a normal L2 interface (or 0 if the interface is changed from L2
133 * to L3 mode). So if the feature bitmap is 0 or just have L2 output bits set,
134 * we know there is no more feature and will just output packets on interface.
135 * Otherwise, get the index of the next feature node.
137 if (PREDICT_FALSE ((feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT) != 0))
139 /* Save bitmap for the next feature graph nodes */
140 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
142 /* Determine the next node */
144 feat_bitmap_get_next_node_index (l2output_main.l2_out_feat_next,
150 * There are no features. Send packet to TX node for sw_if_index0
151 * This is a little tricky in that the output interface next node indexes
152 * are not precomputed at init time.
155 if (sw_if_index == *cached_sw_if_index)
157 /* We hit in the one-entry cache. Use it. */
158 *next0 = *cached_next_index;
162 /* Look up the output TX node for the sw_if_index */
163 *next0 = vec_elt (l2output_main.output_node_index_vec, sw_if_index);
165 if (PREDICT_FALSE (*next0 == L2OUTPUT_NEXT_DROP))
166 b0->error = node->errors[L2OUTPUT_ERROR_MAPPING_DROP];
168 /* Update the one-entry cache */
169 *cached_sw_if_index = sw_if_index;
170 *cached_next_index = *next0;
175 static_always_inline void
176 l2output_vtr (vlib_node_runtime_t * node, l2_output_config_t * config,
177 u32 feature_bitmap, vlib_buffer_t * b, u32 * next)
179 if (PREDICT_FALSE (config->out_vtr_flag))
181 /* Perform pre-vtr EFP filter check if configured */
182 if (config->output_vtr.push_and_pop_bytes)
185 * Perform output vlan tag rewrite and the pre-vtr EFP filter check.
186 * The EFP Filter only needs to be run if there is an output VTR
187 * configured. The flag for the post-vtr EFP Filter node is used
188 * to trigger the pre-vtr check as well.
190 u32 failed1 = (feature_bitmap & L2OUTPUT_FEAT_EFP_FILTER)
191 && (l2_efp_filter_process (b, &(config->input_vtr)));
192 u32 failed2 = l2_vtr_process (b, &(config->output_vtr));
194 if (PREDICT_FALSE (failed1 | failed2))
196 *next = L2OUTPUT_NEXT_DROP;
199 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
203 b->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
207 // perform the PBB rewrite
208 else if (config->output_pbb_vtr.push_and_pop_bytes)
210 u32 failed = l2_pbb_process (b, &(config->output_pbb_vtr));
211 if (PREDICT_FALSE (failed))
213 *next = L2OUTPUT_NEXT_DROP;
214 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
221 static_always_inline uword
222 l2output_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
223 vlib_frame_t * frame, int do_trace)
225 u32 n_left_from, *from, *to_next;
226 l2output_next_t next_index;
227 l2output_main_t *msm = &l2output_main;
228 u32 cached_sw_if_index;
229 u32 cached_next_index;
231 /* Invalidate cache */
232 cached_sw_if_index = ~0;
233 cached_next_index = ~0; /* warning be gone */
235 from = vlib_frame_vector_args (frame);
236 n_left_from = frame->n_vectors; /* number of packets to process */
237 next_index = node->cached_next_index;
239 while (n_left_from > 0)
243 /* get space to enqueue frame to graph node "next_index" */
244 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
246 while (n_left_from >= 8 && n_left_to_next >= 4)
248 u32 bi0, bi1, bi2, bi3;
249 vlib_buffer_t *b0, *b1, *b2, *b3;
250 u32 next0, next1, next2, next3;
251 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
252 ethernet_header_t *h0, *h1, *h2, *h3;
253 l2_output_config_t *config0, *config1, *config2, *config3;
254 u32 feature_bitmap0, feature_bitmap1;
255 u32 feature_bitmap2, feature_bitmap3;
257 /* Prefetch next iteration. */
259 vlib_buffer_t *p4, *p5, *p6, *p7;
261 p4 = vlib_get_buffer (vm, from[4]);
262 p5 = vlib_get_buffer (vm, from[5]);
263 p6 = vlib_get_buffer (vm, from[6]);
264 p7 = vlib_get_buffer (vm, from[7]);
266 /* Prefetch the buffer header for the N+2 loop iteration */
267 vlib_prefetch_buffer_header (p4, LOAD);
268 vlib_prefetch_buffer_header (p5, LOAD);
269 vlib_prefetch_buffer_header (p6, LOAD);
270 vlib_prefetch_buffer_header (p7, LOAD);
273 /* speculatively enqueue b0 and b1 to the current next frame */
274 /* bi is "buffer index", b is pointer to the buffer */
275 to_next[0] = bi0 = from[0];
276 to_next[1] = bi1 = from[1];
277 to_next[2] = bi2 = from[2];
278 to_next[3] = bi3 = from[3];
284 b0 = vlib_get_buffer (vm, bi0);
285 b1 = vlib_get_buffer (vm, bi1);
286 b2 = vlib_get_buffer (vm, bi2);
287 b3 = vlib_get_buffer (vm, bi3);
289 /* TX interface handles */
290 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
291 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
292 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
293 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
295 vlib_node_increment_counter (vm, l2output_node.index,
296 L2OUTPUT_ERROR_L2OUTPUT, 4);
298 /* Get config for the output interface */
299 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
300 config1 = vec_elt_at_index (msm->configs, sw_if_index1);
301 config2 = vec_elt_at_index (msm->configs, sw_if_index2);
302 config3 = vec_elt_at_index (msm->configs, sw_if_index3);
305 * Get features from the config
306 * TODO: mask out any non-applicable features
308 feature_bitmap0 = config0->feature_bitmap;
309 feature_bitmap1 = config1->feature_bitmap;
310 feature_bitmap2 = config2->feature_bitmap;
311 feature_bitmap3 = config3->feature_bitmap;
313 /* Determine next node */
314 l2_output_dispatch (b0, node, &cached_sw_if_index,
315 &cached_next_index, sw_if_index0,
316 feature_bitmap0, &next0);
317 l2_output_dispatch (b1, node, &cached_sw_if_index,
318 &cached_next_index, sw_if_index1,
319 feature_bitmap1, &next1);
320 l2_output_dispatch (b2, node, &cached_sw_if_index,
321 &cached_next_index, sw_if_index2,
322 feature_bitmap2, &next2);
323 l2_output_dispatch (b3, node, &cached_sw_if_index,
324 &cached_next_index, sw_if_index3,
325 feature_bitmap3, &next3);
327 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
328 l2output_vtr (node, config1, feature_bitmap1, b1, &next1);
329 l2output_vtr (node, config2, feature_bitmap2, b2, &next2);
330 l2output_vtr (node, config3, feature_bitmap3, b3, &next3);
334 h0 = vlib_buffer_get_current (b0);
335 h1 = vlib_buffer_get_current (b1);
336 h2 = vlib_buffer_get_current (b2);
337 h3 = vlib_buffer_get_current (b3);
338 if (b0->flags & VLIB_BUFFER_IS_TRACED)
340 l2output_trace_t *t =
341 vlib_add_trace (vm, node, b0, sizeof (*t));
342 t->sw_if_index = sw_if_index0;
343 clib_memcpy (t->src, h0->src_address, 6);
344 clib_memcpy (t->dst, h0->dst_address, 6);
345 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
347 if (b1->flags & VLIB_BUFFER_IS_TRACED)
349 l2output_trace_t *t =
350 vlib_add_trace (vm, node, b1, sizeof (*t));
351 t->sw_if_index = sw_if_index1;
352 clib_memcpy (t->src, h1->src_address, 6);
353 clib_memcpy (t->dst, h1->dst_address, 6);
354 clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
356 if (b2->flags & VLIB_BUFFER_IS_TRACED)
358 l2output_trace_t *t =
359 vlib_add_trace (vm, node, b2, sizeof (*t));
360 t->sw_if_index = sw_if_index2;
361 clib_memcpy (t->src, h2->src_address, 6);
362 clib_memcpy (t->dst, h2->dst_address, 6);
363 clib_memcpy (t->raw, &h2->type, sizeof (t->raw));
365 if (b3->flags & VLIB_BUFFER_IS_TRACED)
367 l2output_trace_t *t =
368 vlib_add_trace (vm, node, b3, sizeof (*t));
369 t->sw_if_index = sw_if_index3;
370 clib_memcpy (t->src, h3->src_address, 6);
371 clib_memcpy (t->dst, h3->dst_address, 6);
372 clib_memcpy (t->raw, &h3->type, sizeof (t->raw));
377 * Perform the split horizon check
378 * The check can only fail for non-zero shg's
380 if (PREDICT_FALSE (config0->shg + config1->shg +
381 config2->shg + config3->shg))
383 /* one of the checks might fail, check both */
384 if (split_horizon_violation
385 (config0->shg, vnet_buffer (b0)->l2.shg))
387 next0 = L2OUTPUT_NEXT_DROP;
388 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
390 if (split_horizon_violation
391 (config1->shg, vnet_buffer (b1)->l2.shg))
393 next1 = L2OUTPUT_NEXT_DROP;
394 b1->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
396 if (split_horizon_violation
397 (config2->shg, vnet_buffer (b2)->l2.shg))
399 next2 = L2OUTPUT_NEXT_DROP;
400 b2->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
402 if (split_horizon_violation
403 (config3->shg, vnet_buffer (b3)->l2.shg))
405 next3 = L2OUTPUT_NEXT_DROP;
406 b3->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
410 /* verify speculative enqueues, maybe switch current next frame */
411 /* if next0==next1==next_index then nothing special needs to be done */
412 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
413 to_next, n_left_to_next,
415 next0, next1, next2, next3);
418 while (n_left_from > 0 && n_left_to_next > 0)
424 ethernet_header_t *h0;
425 l2_output_config_t *config0;
428 /* speculatively enqueue b0 to the current next frame */
436 b0 = vlib_get_buffer (vm, bi0);
438 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
440 vlib_node_increment_counter (vm, l2output_node.index,
441 L2OUTPUT_ERROR_L2OUTPUT, 1);
443 /* Get config for the output interface */
444 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
447 * Get features from the config
448 * TODO: mask out any non-applicable features
450 feature_bitmap0 = config0->feature_bitmap;
452 /* Determine next node */
453 l2_output_dispatch (b0, node, &cached_sw_if_index,
454 &cached_next_index, sw_if_index0,
455 feature_bitmap0, &next0);
457 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
459 if (do_trace && PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
461 l2output_trace_t *t =
462 vlib_add_trace (vm, node, b0, sizeof (*t));
463 t->sw_if_index = sw_if_index0;
464 h0 = vlib_buffer_get_current (b0);
465 clib_memcpy (t->src, h0->src_address, 6);
466 clib_memcpy (t->dst, h0->dst_address, 6);
467 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
470 /* Perform the split horizon check */
472 (split_horizon_violation
473 (config0->shg, vnet_buffer (b0)->l2.shg)))
475 next0 = L2OUTPUT_NEXT_DROP;
476 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
479 /* verify speculative enqueue, maybe switch current next frame */
480 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
481 to_next, n_left_to_next,
485 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
488 return frame->n_vectors;
492 l2output_node_fn (vlib_main_t * vm,
493 vlib_node_runtime_t * node, vlib_frame_t * frame)
495 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
496 return l2output_node_inline (vm, node, frame, 1 /* do_trace */ );
497 return l2output_node_inline (vm, node, frame, 0 /* do_trace */ );
501 VLIB_REGISTER_NODE (l2output_node) = {
502 .function = l2output_node_fn,
504 .vector_size = sizeof (u32),
505 .format_trace = format_l2output_trace,
506 .type = VLIB_NODE_TYPE_INTERNAL,
508 .n_errors = ARRAY_LEN(l2output_error_strings),
509 .error_strings = l2output_error_strings,
511 .n_next_nodes = L2OUTPUT_N_NEXT,
513 /* edit / add dispositions here */
515 [L2OUTPUT_NEXT_DROP] = "error-drop",
516 [L2OUTPUT_NEXT_BAD_INTF] = "l2-output-bad-intf",
520 VLIB_NODE_FUNCTION_MULTIARCH (l2output_node, l2output_node_fn);
524 #define foreach_l2output_bad_intf_error \
525 _(DROP, "L2 output to interface not in L2 mode or deleted")
527 static char *l2output_bad_intf_error_strings[] = {
528 #define _(sym,string) string,
529 foreach_l2output_bad_intf_error
535 #define _(sym,str) L2OUTPUT_BAD_INTF_ERROR_##sym,
536 foreach_l2output_bad_intf_error
538 L2OUTPUT_BAD_INTF_N_ERROR,
539 } l2output_bad_intf_error_t;
543 * Output node for interfaces/tunnels which was in L2 mode but were changed
544 * to L3 mode or possibly deleted thereafter. On changing forwarding mode
545 * of any tunnel/interface from L2 to L3, its entry in l2_output_main table
546 * next_nodes.output_node_index_vec[sw_if_index] MUST be set to the value of
547 * L2OUTPUT_NEXT_BAD_INTF. Thus, if there are stale entries in the L2FIB for
548 * this sw_if_index, l2-output will send packets for this sw_if_index to the
549 * l2-output-bad-intf node which just setup the proper drop reason before
550 * sending packets to the error-drop node to drop the packet. Then, stale L2FIB
551 * entries for delted tunnels won't cause possible packet or memory corrpution.
553 static vlib_node_registration_t l2output_bad_intf_node;
556 l2output_bad_intf_node_fn (vlib_main_t * vm,
557 vlib_node_runtime_t * node, vlib_frame_t * frame)
559 u32 n_left_from, *from, *to_next;
560 l2output_next_t next_index = 0;
562 from = vlib_frame_vector_args (frame);
563 n_left_from = frame->n_vectors; /* number of packets to process */
565 while (n_left_from > 0)
569 /* get space to enqueue frame to graph node "next_index" */
570 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
572 while (n_left_from >= 4 && n_left_to_next >= 2)
575 vlib_buffer_t *b0, *b1;
577 to_next[0] = bi0 = from[0];
578 to_next[1] = bi1 = from[1];
583 b0 = vlib_get_buffer (vm, bi0);
584 b1 = vlib_get_buffer (vm, bi1);
585 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
586 b1->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
589 while (n_left_from > 0 && n_left_to_next > 0)
600 b0 = vlib_get_buffer (vm, bi0);
601 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
604 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
607 return frame->n_vectors;
611 VLIB_REGISTER_NODE (l2output_bad_intf_node,static) = {
612 .function = l2output_bad_intf_node_fn,
613 .name = "l2-output-bad-intf",
614 .vector_size = sizeof (u32),
615 .type = VLIB_NODE_TYPE_INTERNAL,
617 .n_errors = ARRAY_LEN(l2output_bad_intf_error_strings),
618 .error_strings = l2output_bad_intf_error_strings,
622 /* edit / add dispositions here */
628 VLIB_NODE_FUNCTION_MULTIARCH (l2output_bad_intf_node, l2output_bad_intf_node_fn);
631 static clib_error_t *
632 l2output_init (vlib_main_t * vm)
634 l2output_main_t *mp = &l2output_main;
637 mp->vnet_main = vnet_get_main ();
639 /* Create the config vector */
640 vec_validate (mp->configs, 100);
641 /* Until we hook up the CLI config, just create 100 sw interface entries and zero them */
643 /* Initialize the feature next-node indexes */
644 feat_bitmap_init_next_nodes (vm,
647 l2output_get_feat_names (),
648 mp->l2_out_feat_next);
650 /* Initialize the output node mapping table */
651 vec_validate_init_empty (mp->output_node_index_vec, 100,
657 VLIB_INIT_FUNCTION (l2output_init);
660 /** Create a mapping in the next node mapping table for the given sw_if_index. */
662 l2output_create_output_node_mapping (vlib_main_t * vlib_main,
663 vnet_main_t * vnet_main, u32 sw_if_index)
665 vnet_hw_interface_t *hw0 =
666 vnet_get_sup_hw_interface (vnet_main, sw_if_index);
668 /* dynamically create graph node arc */
669 u32 next = vlib_node_add_next (vlib_main, l2output_node.index,
670 hw0->output_node_index);
671 l2output_main.output_node_index_vec[sw_if_index] = next;
674 /* Get a pointer to the config for the given interface */
676 l2output_intf_config (u32 sw_if_index)
678 l2output_main_t *mp = &l2output_main;
680 vec_validate (mp->configs, sw_if_index);
681 return vec_elt_at_index (mp->configs, sw_if_index);
684 /** Enable (or disable) the feature in the bitmap for the given interface. */
686 l2output_intf_bitmap_enable (u32 sw_if_index, u32 feature_bitmap, u32 enable)
688 l2output_main_t *mp = &l2output_main;
689 l2_output_config_t *config;
691 vec_validate (mp->configs, sw_if_index);
692 config = vec_elt_at_index (mp->configs, sw_if_index);
696 config->feature_bitmap |= feature_bitmap;
700 config->feature_bitmap &= ~feature_bitmap;
705 * fd.io coding-style-patch-verification: ON
708 * eval: (c-set-style "gnu")