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 #ifndef CLIB_MARCH_VARIANT
31 /* Feature graph node names */
32 static char *l2output_feat_names[] = {
33 #define _(sym,name) name,
39 l2output_get_feat_names (void)
41 return l2output_feat_names;
45 format_l2_output_features (u8 * s, va_list * args)
47 static char *display_names[] = {
48 #define _(sym,name) #sym,
52 u32 feature_bitmap = va_arg (*args, u32);
53 u32 verbose = va_arg (*args, u32);
55 if (feature_bitmap == 0)
57 s = format (s, " none configured");
62 for (i = L2OUTPUT_N_FEAT - 1; i >= 0; i--)
64 if (feature_bitmap & (1 << i))
68 format (s, "%17s (%s)\n", display_names[i],
69 l2output_feat_names[i]);
71 s = format (s, "%s ", l2output_feat_names[i]);
78 l2output_main_t l2output_main;
83 /* per-pkt trace data */
87 u8 raw[12]; /* raw data */
90 /* packet trace format function */
92 format_l2output_trace (u8 * s, va_list * args)
94 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
95 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
96 l2output_trace_t *t = va_arg (*args, l2output_trace_t *);
98 s = format (s, "l2-output: sw_if_index %d dst %U src %U data "
99 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
101 format_ethernet_address, t->dst,
102 format_ethernet_address, t->src,
103 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
104 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
105 t->raw[10], t->raw[11]);
111 static char *l2output_error_strings[] = {
112 #define _(sym,string) string,
113 foreach_l2output_error
118 * Check for split horizon violations.
119 * Return 0 if split horizon check passes, otherwise return non-zero.
120 * Packets should not be transmitted out an interface with the same
121 * split-horizon group as the input interface, except if the @c shg is 0
122 * in which case the check always passes.
124 static_always_inline void
125 split_horizon_violation (vlib_node_runtime_t * node, u8 shg,
126 vlib_buffer_t * b, u16 * next)
128 if (shg != vnet_buffer (b)->l2.shg)
130 next[0] = L2OUTPUT_NEXT_DROP;
131 b->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
134 static_always_inline void
135 l2output_process_batch_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
136 l2_output_config_t * config,
137 vlib_buffer_t ** b, i16 * cdo, u16 * next,
138 u32 n_left, int l2_efp, int l2_vtr, int l2_pbb,
139 int shg_set, int update_feature_bitmap)
143 vlib_prefetch_buffer_header (b[4], LOAD);
144 vlib_prefetch_buffer_header (b[5], LOAD);
145 vlib_prefetch_buffer_header (b[6], LOAD);
146 vlib_prefetch_buffer_header (b[7], LOAD);
148 /* prefetch eth headers only if we need to touch them */
149 if (l2_vtr || l2_pbb || shg_set)
151 CLIB_PREFETCH (b[4]->data + cdo[4], CLIB_CACHE_LINE_BYTES, LOAD);
152 CLIB_PREFETCH (b[5]->data + cdo[5], CLIB_CACHE_LINE_BYTES, LOAD);
153 CLIB_PREFETCH (b[6]->data + cdo[6], CLIB_CACHE_LINE_BYTES, LOAD);
154 CLIB_PREFETCH (b[7]->data + cdo[7], CLIB_CACHE_LINE_BYTES, LOAD);
157 if (update_feature_bitmap)
159 vnet_buffer (b[0])->l2.feature_bitmap = config->feature_bitmap;
160 vnet_buffer (b[1])->l2.feature_bitmap = config->feature_bitmap;
161 vnet_buffer (b[2])->l2.feature_bitmap = config->feature_bitmap;
162 vnet_buffer (b[3])->l2.feature_bitmap = config->feature_bitmap;
168 for (i = 0; i < 4; i++)
170 u32 failed1 = l2_efp &&
171 l2_efp_filter_process (b[i], &(config->input_vtr));
172 u32 failed2 = l2_vtr_process (b[i], &(config->output_vtr));
173 if (PREDICT_FALSE (failed1 | failed2))
175 next[i] = L2OUTPUT_NEXT_DROP;
177 b[i]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
179 b[i]->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
187 for (i = 0; i < 4; i++)
188 if (l2_pbb_process (b[i], &(config->output_pbb_vtr)))
190 next[i] = L2OUTPUT_NEXT_DROP;
191 b[i]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
197 split_horizon_violation (node, config->shg, b[0], next);
198 split_horizon_violation (node, config->shg, b[1], next + 1);
199 split_horizon_violation (node, config->shg, b[2], next + 2);
200 split_horizon_violation (node, config->shg, b[3], next + 3);
211 if (update_feature_bitmap)
212 vnet_buffer (b[0])->l2.feature_bitmap = config->feature_bitmap;
216 u32 failed1 = l2_efp &&
217 l2_efp_filter_process (b[0], &(config->input_vtr));
218 u32 failed2 = l2_vtr_process (b[0], &(config->output_vtr));
219 if (PREDICT_FALSE (failed1 | failed2))
221 *next = L2OUTPUT_NEXT_DROP;
223 b[0]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
225 b[0]->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
229 if (l2_pbb && l2_pbb_process (b[0], &(config->output_pbb_vtr)))
231 next[0] = L2OUTPUT_NEXT_DROP;
232 b[0]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
236 split_horizon_violation (node, config->shg, b[0], next);
245 static_always_inline void
246 l2output_set_buffer_error (vlib_buffer_t ** b, u32 n_left, vlib_error_t error)
250 vlib_prefetch_buffer_header (b[4], LOAD);
251 vlib_prefetch_buffer_header (b[5], LOAD);
252 vlib_prefetch_buffer_header (b[6], LOAD);
253 vlib_prefetch_buffer_header (b[7], LOAD);
254 b[0]->error = b[1]->error = b[2]->error = b[3]->error = error;
266 static_always_inline void
267 l2output_process_batch (vlib_main_t * vm, vlib_node_runtime_t * node,
268 l2_output_config_t * config, vlib_buffer_t ** b,
269 i16 * cdo, u16 * next, u32 n_left, int l2_efp,
270 int l2_vtr, int l2_pbb)
272 u32 feature_bitmap = config->feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT;
273 if (config->shg == 0 && feature_bitmap == 0)
275 if ((l2_efp | l2_vtr | l2_pbb) == 0)
277 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
278 l2_efp, l2_vtr, l2_pbb, 0, 0);
280 else if (config->shg == 0)
281 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
282 l2_efp, l2_vtr, l2_pbb, 0, 1);
283 else if (feature_bitmap == 0)
284 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
285 l2_efp, l2_vtr, l2_pbb, 1, 0);
287 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
288 l2_efp, l2_vtr, l2_pbb, 1, 1);
291 VLIB_NODE_FN (l2output_node) (vlib_main_t * vm,
292 vlib_node_runtime_t * node,
293 vlib_frame_t * frame)
296 l2output_main_t *msm = &l2output_main;
297 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
298 u16 nexts[VLIB_FRAME_SIZE];
299 u32 sw_if_indices[VLIB_FRAME_SIZE], *sw_if_index;
300 i16 cur_data_offsets[VLIB_FRAME_SIZE], *cdo;
301 l2_output_config_t *config;
304 from = vlib_frame_vector_args (frame);
305 n_left = frame->n_vectors; /* number of packets to process */
307 vlib_get_buffers (vm, from, bufs, n_left);
309 sw_if_index = sw_if_indices;
310 cdo = cur_data_offsets;
312 /* extract data from buffer metadata */
315 /* Prefetch the buffer header for the N+2 loop iteration */
316 vlib_prefetch_buffer_header (b[4], LOAD);
317 vlib_prefetch_buffer_header (b[5], LOAD);
318 vlib_prefetch_buffer_header (b[6], LOAD);
319 vlib_prefetch_buffer_header (b[7], LOAD);
321 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
322 cdo[0] = b[0]->current_data;
323 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
324 cdo[1] = b[1]->current_data;
325 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
326 cdo[2] = b[2]->current_data;
327 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
328 cdo[3] = b[3]->current_data;
338 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
339 cdo[0] = b[0]->current_data;
348 n_left = frame->n_vectors;
351 u16 count, new_next, *next;
352 u16 off = frame->n_vectors - n_left;
357 vlib_prefetch_buffer_header (b[0], LOAD);
358 vlib_prefetch_buffer_header (b[1], LOAD);
359 vlib_prefetch_buffer_header (b[2], LOAD);
360 vlib_prefetch_buffer_header (b[3], LOAD);
363 sw_if_index = sw_if_indices + off;
364 cdo = cur_data_offsets + off;
367 count = clib_count_equal_u32 (sw_if_index, n_left);
370 config = vec_elt_at_index (msm->configs, sw_if_index[0]);
371 feature_bitmap = config->feature_bitmap;
372 if (PREDICT_FALSE ((feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT) != 0))
373 new_next = feat_bitmap_get_next_node_index
374 (l2output_main.l2_out_feat_next, feature_bitmap);
376 new_next = vec_elt (l2output_main.output_node_index_vec,
378 clib_memset_u16 (nexts + off, new_next, count);
380 if (new_next == L2OUTPUT_NEXT_DROP)
382 l2output_set_buffer_error
383 (b, count, node->errors[L2OUTPUT_ERROR_MAPPING_DROP]);
388 if (config->out_vtr_flag && config->output_vtr.push_and_pop_bytes)
390 if (feature_bitmap & L2OUTPUT_FEAT_EFP_FILTER)
391 l2output_process_batch (vm, node, config, b, cdo, next, count,
396 l2output_process_batch (vm, node, config, b, cdo, next, count,
401 else if (config->out_vtr_flag &&
402 config->output_pbb_vtr.push_and_pop_bytes)
403 l2output_process_batch (vm, node, config, b, cdo, next, count,
408 l2output_process_batch (vm, node, config, b, cdo, next, count,
415 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
417 n_left = frame->n_vectors; /* number of packets to process */
422 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
424 ethernet_header_t *h;
425 l2output_trace_t *t =
426 vlib_add_trace (vm, node, b[0], sizeof (*t));
427 t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
428 h = vlib_buffer_get_current (b[0]);
429 clib_memcpy_fast (t->src, h->src_address, 6);
430 clib_memcpy_fast (t->dst, h->dst_address, 6);
431 clib_memcpy_fast (t->raw, &h->type, sizeof (t->raw));
439 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
440 vlib_node_increment_counter (vm, l2output_node.index,
441 L2OUTPUT_ERROR_L2OUTPUT, frame->n_vectors);
443 return frame->n_vectors;
447 VLIB_REGISTER_NODE (l2output_node) = {
449 .vector_size = sizeof (u32),
450 .format_trace = format_l2output_trace,
451 .type = VLIB_NODE_TYPE_INTERNAL,
453 .n_errors = ARRAY_LEN(l2output_error_strings),
454 .error_strings = l2output_error_strings,
456 .n_next_nodes = L2OUTPUT_N_NEXT,
458 /* edit / add dispositions here */
460 [L2OUTPUT_NEXT_DROP] = "error-drop",
461 [L2OUTPUT_NEXT_BAD_INTF] = "l2-output-bad-intf",
467 #define foreach_l2output_bad_intf_error \
468 _(DROP, "L2 output to interface not in L2 mode or deleted")
470 static char *l2output_bad_intf_error_strings[] = {
471 #define _(sym,string) string,
472 foreach_l2output_bad_intf_error
478 #define _(sym,str) L2OUTPUT_BAD_INTF_ERROR_##sym,
479 foreach_l2output_bad_intf_error
481 L2OUTPUT_BAD_INTF_N_ERROR,
482 } l2output_bad_intf_error_t;
486 * Output node for interfaces/tunnels which was in L2 mode but were changed
487 * to L3 mode or possibly deleted thereafter. On changing forwarding mode
488 * of any tunnel/interface from L2 to L3, its entry in l2_output_main table
489 * next_nodes.output_node_index_vec[sw_if_index] MUST be set to the value of
490 * L2OUTPUT_NEXT_BAD_INTF. Thus, if there are stale entries in the L2FIB for
491 * this sw_if_index, l2-output will send packets for this sw_if_index to the
492 * l2-output-bad-intf node which just setup the proper drop reason before
493 * sending packets to the error-drop node to drop the packet. Then, stale L2FIB
494 * entries for deleted tunnels won't cause possible packet or memory corruption.
497 VLIB_NODE_FN (l2output_bad_intf_node) (vlib_main_t * vm,
498 vlib_node_runtime_t * node,
499 vlib_frame_t * frame)
501 u32 n_left_from, *from, *to_next;
502 l2output_next_t next_index = 0;
504 from = vlib_frame_vector_args (frame);
505 n_left_from = frame->n_vectors; /* number of packets to process */
507 while (n_left_from > 0)
511 /* get space to enqueue frame to graph node "next_index" */
512 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
514 while (n_left_from >= 4 && n_left_to_next >= 2)
517 vlib_buffer_t *b0, *b1;
519 to_next[0] = bi0 = from[0];
520 to_next[1] = bi1 = from[1];
525 b0 = vlib_get_buffer (vm, bi0);
526 b1 = vlib_get_buffer (vm, bi1);
527 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
528 b1->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
531 while (n_left_from > 0 && n_left_to_next > 0)
542 b0 = vlib_get_buffer (vm, bi0);
543 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
546 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
549 return frame->n_vectors;
553 VLIB_REGISTER_NODE (l2output_bad_intf_node) = {
554 .name = "l2-output-bad-intf",
555 .vector_size = sizeof (u32),
556 .type = VLIB_NODE_TYPE_INTERNAL,
558 .n_errors = ARRAY_LEN(l2output_bad_intf_error_strings),
559 .error_strings = l2output_bad_intf_error_strings,
563 /* edit / add dispositions here */
570 static clib_error_t *
571 l2output_init (vlib_main_t * vm)
573 l2output_main_t *mp = &l2output_main;
576 mp->vnet_main = vnet_get_main ();
578 /* Create the config vector */
579 vec_validate (mp->configs, 100);
580 /* Until we hook up the CLI config, just create 100 sw interface entries and zero them */
582 /* Initialize the feature next-node indexes */
583 feat_bitmap_init_next_nodes (vm,
586 l2output_get_feat_names (),
587 mp->l2_out_feat_next);
589 /* Initialize the output node mapping table */
590 vec_validate_init_empty (mp->output_node_index_vec, 100,
596 VLIB_INIT_FUNCTION (l2output_init);
599 #ifndef CLIB_MARCH_VARIANT
600 /** Create a mapping in the next node mapping table for the given sw_if_index. */
602 l2output_create_output_node_mapping (vlib_main_t * vlib_main,
603 vnet_main_t * vnet_main, u32 sw_if_index)
605 vnet_hw_interface_t *hw0 =
606 vnet_get_sup_hw_interface (vnet_main, sw_if_index);
608 /* dynamically create graph node arc */
609 u32 next = vlib_node_add_next (vlib_main, l2output_node.index,
610 hw0->output_node_index);
611 l2output_main.output_node_index_vec[sw_if_index] = next;
614 /* Get a pointer to the config for the given interface */
616 l2output_intf_config (u32 sw_if_index)
618 l2output_main_t *mp = &l2output_main;
620 vec_validate (mp->configs, sw_if_index);
621 return vec_elt_at_index (mp->configs, sw_if_index);
624 /** Enable (or disable) the feature in the bitmap for the given interface. */
626 l2output_intf_bitmap_enable (u32 sw_if_index,
627 l2output_feat_masks_t feature_bitmap, u32 enable)
629 l2output_main_t *mp = &l2output_main;
630 l2_output_config_t *config;
632 vec_validate (mp->configs, sw_if_index);
633 config = vec_elt_at_index (mp->configs, sw_if_index);
637 config->feature_bitmap |= feature_bitmap;
641 config->feature_bitmap &= ~feature_bitmap;
647 * fd.io coding-style-patch-verification: ON
650 * eval: (c-set-style "gnu")