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;
43 l2output_main_t l2output_main;
47 /* per-pkt trace data */
51 u8 raw[12]; /* raw data */
54 /* packet trace format function */
56 format_l2output_trace (u8 * s, va_list * args)
58 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60 l2output_trace_t *t = va_arg (*args, l2output_trace_t *);
62 s = format (s, "l2-output: sw_if_index %d dst %U src %U data "
63 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
65 format_ethernet_address, t->dst,
66 format_ethernet_address, t->src,
67 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
68 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
69 t->raw[10], t->raw[11]);
75 static char *l2output_error_strings[] = {
76 #define _(sym,string) string,
77 foreach_l2output_error
82 * Check for split horizon violations.
83 * Return 0 if split horizon check passes, otherwise return non-zero.
84 * Packets should not be transmitted out an interface with the same
85 * split-horizon group as the input interface, except if the @c shg is 0
86 * in which case the check always passes.
88 static_always_inline u32
89 split_horizon_violation (u8 shg1, u8 shg2)
91 if (PREDICT_TRUE (shg1 == 0))
101 /** Determine the next L2 node based on the output feature bitmap */
102 static_always_inline void
103 l2_output_dispatch (vlib_buffer_t * b0, vlib_node_runtime_t * node,
104 u32 * cached_sw_if_index, u32 * cached_next_index,
105 u32 sw_if_index, u32 feature_bitmap, u32 * next0)
108 * The output feature bitmap always have at least the L2 output bit set
109 * for a normal L2 interface (or 0 if the interface is changed from L2
110 * to L3 mode). So if the feature bitmap is 0 or just have L2 output bits set,
111 * we know there is no more feature and will just output packets on interface.
112 * Otherwise, get the index of the next feature node.
114 if (PREDICT_FALSE ((feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT) != 0))
116 /* Save bitmap for the next feature graph nodes */
117 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
119 /* Determine the next node */
121 feat_bitmap_get_next_node_index (l2output_main.l2_out_feat_next,
127 * There are no features. Send packet to TX node for sw_if_index0
128 * This is a little tricky in that the output interface next node indexes
129 * are not precomputed at init time.
132 if (sw_if_index == *cached_sw_if_index)
134 /* We hit in the one-entry cache. Use it. */
135 *next0 = *cached_next_index;
139 /* Look up the output TX node for the sw_if_index */
140 *next0 = vec_elt (l2output_main.output_node_index_vec, sw_if_index);
142 if (PREDICT_FALSE (*next0 == L2OUTPUT_NEXT_DROP))
143 b0->error = node->errors[L2OUTPUT_ERROR_MAPPING_DROP];
145 /* Update the one-entry cache */
146 *cached_sw_if_index = sw_if_index;
147 *cached_next_index = *next0;
152 static_always_inline void
153 l2output_vtr (vlib_node_runtime_t * node, l2_output_config_t * config,
154 u32 feature_bitmap, vlib_buffer_t * b, u32 * next)
156 if (PREDICT_FALSE (config->out_vtr_flag))
158 /* Perform pre-vtr EFP filter check if configured */
159 if (config->output_vtr.push_and_pop_bytes)
162 * Perform output vlan tag rewrite and the pre-vtr EFP filter check.
163 * The EFP Filter only needs to be run if there is an output VTR
164 * configured. The flag for the post-vtr EFP Filter node is used
165 * to trigger the pre-vtr check as well.
167 u32 failed1 = (feature_bitmap & L2OUTPUT_FEAT_EFP_FILTER)
168 && (l2_efp_filter_process (b, &(config->input_vtr)));
169 u32 failed2 = l2_vtr_process (b, &(config->output_vtr));
171 if (PREDICT_FALSE (failed1 | failed2))
173 *next = L2OUTPUT_NEXT_DROP;
176 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
180 b->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
184 // perform the PBB rewrite
185 else if (config->output_pbb_vtr.push_and_pop_bytes)
187 u32 failed = l2_pbb_process (b, &(config->output_pbb_vtr));
188 if (PREDICT_FALSE (failed))
190 *next = L2OUTPUT_NEXT_DROP;
191 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
198 static_always_inline uword
199 l2output_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
200 vlib_frame_t * frame, int do_trace)
202 u32 n_left_from, *from, *to_next;
203 l2output_next_t next_index;
204 l2output_main_t *msm = &l2output_main;
205 u32 cached_sw_if_index;
206 u32 cached_next_index;
208 /* Invalidate cache */
209 cached_sw_if_index = ~0;
210 cached_next_index = ~0; /* warning be gone */
212 from = vlib_frame_vector_args (frame);
213 n_left_from = frame->n_vectors; /* number of packets to process */
214 next_index = node->cached_next_index;
216 while (n_left_from > 0)
220 /* get space to enqueue frame to graph node "next_index" */
221 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
223 while (n_left_from >= 8 && n_left_to_next >= 4)
225 u32 bi0, bi1, bi2, bi3;
226 vlib_buffer_t *b0, *b1, *b2, *b3;
227 u32 next0, next1, next2, next3;
228 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
229 ethernet_header_t *h0, *h1, *h2, *h3;
230 l2_output_config_t *config0, *config1, *config2, *config3;
231 u32 feature_bitmap0, feature_bitmap1;
232 u32 feature_bitmap2, feature_bitmap3;
234 /* Prefetch next iteration. */
236 vlib_buffer_t *p4, *p5, *p6, *p7;
238 p4 = vlib_get_buffer (vm, from[4]);
239 p5 = vlib_get_buffer (vm, from[5]);
240 p6 = vlib_get_buffer (vm, from[6]);
241 p7 = vlib_get_buffer (vm, from[7]);
243 /* Prefetch the buffer header for the N+2 loop iteration */
244 vlib_prefetch_buffer_header (p4, LOAD);
245 vlib_prefetch_buffer_header (p5, LOAD);
246 vlib_prefetch_buffer_header (p6, LOAD);
247 vlib_prefetch_buffer_header (p7, LOAD);
250 /* speculatively enqueue b0 and b1 to the current next frame */
251 /* bi is "buffer index", b is pointer to the buffer */
252 to_next[0] = bi0 = from[0];
253 to_next[1] = bi1 = from[1];
254 to_next[2] = bi2 = from[2];
255 to_next[3] = bi3 = from[3];
261 b0 = vlib_get_buffer (vm, bi0);
262 b1 = vlib_get_buffer (vm, bi1);
263 b2 = vlib_get_buffer (vm, bi2);
264 b3 = vlib_get_buffer (vm, bi3);
266 /* TX interface handles */
267 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
268 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
269 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
270 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
272 vlib_node_increment_counter (vm, l2output_node.index,
273 L2OUTPUT_ERROR_L2OUTPUT, 4);
275 /* Get config for the output interface */
276 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
277 config1 = vec_elt_at_index (msm->configs, sw_if_index1);
278 config2 = vec_elt_at_index (msm->configs, sw_if_index2);
279 config3 = vec_elt_at_index (msm->configs, sw_if_index3);
282 * Get features from the config
283 * TODO: mask out any non-applicable features
285 feature_bitmap0 = config0->feature_bitmap;
286 feature_bitmap1 = config1->feature_bitmap;
287 feature_bitmap2 = config2->feature_bitmap;
288 feature_bitmap3 = config3->feature_bitmap;
290 /* Determine next node */
291 l2_output_dispatch (b0, node, &cached_sw_if_index,
292 &cached_next_index, sw_if_index0,
293 feature_bitmap0, &next0);
294 l2_output_dispatch (b1, node, &cached_sw_if_index,
295 &cached_next_index, sw_if_index1,
296 feature_bitmap1, &next1);
297 l2_output_dispatch (b2, node, &cached_sw_if_index,
298 &cached_next_index, sw_if_index2,
299 feature_bitmap2, &next2);
300 l2_output_dispatch (b3, node, &cached_sw_if_index,
301 &cached_next_index, sw_if_index3,
302 feature_bitmap3, &next3);
304 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
305 l2output_vtr (node, config1, feature_bitmap1, b1, &next1);
306 l2output_vtr (node, config2, feature_bitmap2, b2, &next2);
307 l2output_vtr (node, config3, feature_bitmap3, b3, &next3);
311 h0 = vlib_buffer_get_current (b0);
312 h1 = vlib_buffer_get_current (b1);
313 h2 = vlib_buffer_get_current (b2);
314 h3 = vlib_buffer_get_current (b3);
315 if (b0->flags & VLIB_BUFFER_IS_TRACED)
317 l2output_trace_t *t =
318 vlib_add_trace (vm, node, b0, sizeof (*t));
319 t->sw_if_index = sw_if_index0;
320 clib_memcpy (t->src, h0->src_address, 6);
321 clib_memcpy (t->dst, h0->dst_address, 6);
322 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
324 if (b1->flags & VLIB_BUFFER_IS_TRACED)
326 l2output_trace_t *t =
327 vlib_add_trace (vm, node, b1, sizeof (*t));
328 t->sw_if_index = sw_if_index1;
329 clib_memcpy (t->src, h1->src_address, 6);
330 clib_memcpy (t->dst, h1->dst_address, 6);
331 clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
333 if (b2->flags & VLIB_BUFFER_IS_TRACED)
335 l2output_trace_t *t =
336 vlib_add_trace (vm, node, b2, sizeof (*t));
337 t->sw_if_index = sw_if_index2;
338 clib_memcpy (t->src, h2->src_address, 6);
339 clib_memcpy (t->dst, h2->dst_address, 6);
340 clib_memcpy (t->raw, &h2->type, sizeof (t->raw));
342 if (b3->flags & VLIB_BUFFER_IS_TRACED)
344 l2output_trace_t *t =
345 vlib_add_trace (vm, node, b3, sizeof (*t));
346 t->sw_if_index = sw_if_index3;
347 clib_memcpy (t->src, h3->src_address, 6);
348 clib_memcpy (t->dst, h3->dst_address, 6);
349 clib_memcpy (t->raw, &h3->type, sizeof (t->raw));
354 * Perform the split horizon check
355 * The check can only fail for non-zero shg's
357 if (PREDICT_FALSE (config0->shg + config1->shg +
358 config2->shg + config3->shg))
360 /* one of the checks might fail, check both */
361 if (split_horizon_violation
362 (config0->shg, vnet_buffer (b0)->l2.shg))
364 next0 = L2OUTPUT_NEXT_DROP;
365 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
367 if (split_horizon_violation
368 (config1->shg, vnet_buffer (b1)->l2.shg))
370 next1 = L2OUTPUT_NEXT_DROP;
371 b1->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
373 if (split_horizon_violation
374 (config2->shg, vnet_buffer (b2)->l2.shg))
376 next2 = L2OUTPUT_NEXT_DROP;
377 b2->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
379 if (split_horizon_violation
380 (config3->shg, vnet_buffer (b3)->l2.shg))
382 next3 = L2OUTPUT_NEXT_DROP;
383 b3->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
387 /* verify speculative enqueues, maybe switch current next frame */
388 /* if next0==next1==next_index then nothing special needs to be done */
389 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
390 to_next, n_left_to_next,
392 next0, next1, next2, next3);
395 while (n_left_from > 0 && n_left_to_next > 0)
401 ethernet_header_t *h0;
402 l2_output_config_t *config0;
405 /* speculatively enqueue b0 to the current next frame */
413 b0 = vlib_get_buffer (vm, bi0);
415 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
417 vlib_node_increment_counter (vm, l2output_node.index,
418 L2OUTPUT_ERROR_L2OUTPUT, 1);
420 /* Get config for the output interface */
421 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
424 * Get features from the config
425 * TODO: mask out any non-applicable features
427 feature_bitmap0 = config0->feature_bitmap;
429 /* Determine next node */
430 l2_output_dispatch (b0, node, &cached_sw_if_index,
431 &cached_next_index, sw_if_index0,
432 feature_bitmap0, &next0);
434 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
436 if (do_trace && PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
438 l2output_trace_t *t =
439 vlib_add_trace (vm, node, b0, sizeof (*t));
440 t->sw_if_index = sw_if_index0;
441 h0 = vlib_buffer_get_current (b0);
442 clib_memcpy (t->src, h0->src_address, 6);
443 clib_memcpy (t->dst, h0->dst_address, 6);
444 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
447 /* Perform the split horizon check */
449 (split_horizon_violation
450 (config0->shg, vnet_buffer (b0)->l2.shg)))
452 next0 = L2OUTPUT_NEXT_DROP;
453 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
456 /* verify speculative enqueue, maybe switch current next frame */
457 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
458 to_next, n_left_to_next,
462 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
465 return frame->n_vectors;
469 l2output_node_fn (vlib_main_t * vm,
470 vlib_node_runtime_t * node, vlib_frame_t * frame)
472 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
473 return l2output_node_inline (vm, node, frame, 1 /* do_trace */ );
474 return l2output_node_inline (vm, node, frame, 0 /* do_trace */ );
478 VLIB_REGISTER_NODE (l2output_node) = {
479 .function = l2output_node_fn,
481 .vector_size = sizeof (u32),
482 .format_trace = format_l2output_trace,
483 .type = VLIB_NODE_TYPE_INTERNAL,
485 .n_errors = ARRAY_LEN(l2output_error_strings),
486 .error_strings = l2output_error_strings,
488 .n_next_nodes = L2OUTPUT_N_NEXT,
490 /* edit / add dispositions here */
492 [L2OUTPUT_NEXT_DROP] = "error-drop",
493 [L2OUTPUT_NEXT_BAD_INTF] = "l2-output-bad-intf",
497 VLIB_NODE_FUNCTION_MULTIARCH (l2output_node, l2output_node_fn);
501 #define foreach_l2output_bad_intf_error \
502 _(DROP, "L2 output to interface not in L2 mode or deleted")
504 static char *l2output_bad_intf_error_strings[] = {
505 #define _(sym,string) string,
506 foreach_l2output_bad_intf_error
512 #define _(sym,str) L2OUTPUT_BAD_INTF_ERROR_##sym,
513 foreach_l2output_bad_intf_error
515 L2OUTPUT_BAD_INTF_N_ERROR,
516 } l2output_bad_intf_error_t;
520 * Output node for interfaces/tunnels which was in L2 mode but were changed
521 * to L3 mode or possibly deleted thereafter. On changing forwarding mode
522 * of any tunnel/interface from L2 to L3, its entry in l2_output_main table
523 * next_nodes.output_node_index_vec[sw_if_index] MUST be set to the value of
524 * L2OUTPUT_NEXT_BAD_INTF. Thus, if there are stale entries in the L2FIB for
525 * this sw_if_index, l2-output will send packets for this sw_if_index to the
526 * l2-output-bad-intf node which just setup the proper drop reason before
527 * sending packets to the error-drop node to drop the packet. Then, stale L2FIB
528 * entries for delted tunnels won't cause possible packet or memory corrpution.
530 static vlib_node_registration_t l2output_bad_intf_node;
533 l2output_bad_intf_node_fn (vlib_main_t * vm,
534 vlib_node_runtime_t * node, vlib_frame_t * frame)
536 u32 n_left_from, *from, *to_next;
537 l2output_next_t next_index = 0;
539 from = vlib_frame_vector_args (frame);
540 n_left_from = frame->n_vectors; /* number of packets to process */
542 while (n_left_from > 0)
546 /* get space to enqueue frame to graph node "next_index" */
547 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
549 while (n_left_from >= 4 && n_left_to_next >= 2)
552 vlib_buffer_t *b0, *b1;
554 to_next[0] = bi0 = from[0];
555 to_next[1] = bi1 = from[1];
560 b0 = vlib_get_buffer (vm, bi0);
561 b1 = vlib_get_buffer (vm, bi1);
562 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
563 b1->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
566 while (n_left_from > 0 && n_left_to_next > 0)
577 b0 = vlib_get_buffer (vm, bi0);
578 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
581 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
584 return frame->n_vectors;
588 VLIB_REGISTER_NODE (l2output_bad_intf_node,static) = {
589 .function = l2output_bad_intf_node_fn,
590 .name = "l2-output-bad-intf",
591 .vector_size = sizeof (u32),
592 .type = VLIB_NODE_TYPE_INTERNAL,
594 .n_errors = ARRAY_LEN(l2output_bad_intf_error_strings),
595 .error_strings = l2output_bad_intf_error_strings,
599 /* edit / add dispositions here */
605 VLIB_NODE_FUNCTION_MULTIARCH (l2output_bad_intf_node, l2output_bad_intf_node_fn);
608 static clib_error_t *
609 l2output_init (vlib_main_t * vm)
611 l2output_main_t *mp = &l2output_main;
614 mp->vnet_main = vnet_get_main ();
616 /* Create the config vector */
617 vec_validate (mp->configs, 100);
618 /* Until we hook up the CLI config, just create 100 sw interface entries and zero them */
620 /* Initialize the feature next-node indexes */
621 feat_bitmap_init_next_nodes (vm,
624 l2output_get_feat_names (),
625 mp->l2_out_feat_next);
627 /* Initialize the output node mapping table */
628 vec_validate_init_empty (mp->output_node_index_vec, 100,
634 VLIB_INIT_FUNCTION (l2output_init);
637 /** Create a mapping in the next node mapping table for the given sw_if_index. */
639 l2output_create_output_node_mapping (vlib_main_t * vlib_main,
640 vnet_main_t * vnet_main, u32 sw_if_index)
642 vnet_hw_interface_t *hw0 =
643 vnet_get_sup_hw_interface (vnet_main, sw_if_index);
645 /* dynamically create graph node arc */
646 u32 next = vlib_node_add_next (vlib_main, l2output_node.index,
647 hw0->output_node_index);
648 l2output_main.output_node_index_vec[sw_if_index] = next;
651 /* Get a pointer to the config for the given interface */
653 l2output_intf_config (u32 sw_if_index)
655 l2output_main_t *mp = &l2output_main;
657 vec_validate (mp->configs, sw_if_index);
658 return vec_elt_at_index (mp->configs, sw_if_index);
661 /** Enable (or disable) the feature in the bitmap for the given interface. */
663 l2output_intf_bitmap_enable (u32 sw_if_index, u32 feature_bitmap, u32 enable)
665 l2output_main_t *mp = &l2output_main;
666 l2_output_config_t *config;
668 vec_validate (mp->configs, sw_if_index);
669 config = vec_elt_at_index (mp->configs, sw_if_index);
673 config->feature_bitmap |= feature_bitmap;
677 config->feature_bitmap &= ~feature_bitmap;
682 * fd.io coding-style-patch-verification: ON
685 * eval: (c-set-style "gnu")