2 * l2_efp_filter.c : layer 2 egress EFP Filter 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/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/l2/feat_bitmap.h>
23 #include <vnet/l2/l2_output.h>
24 #include <vnet/ethernet/ethernet.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/cache.h>
31 * @brief EFP-filter - Ethernet Flow Point Filter.
33 * It is possible to transmit a packet out a subinterface with VLAN tags
34 * that are not compatible with that subinterface. In other words, if that
35 * packet arrived on the output port, it would not be classified as coming
36 * from the output subinterface. This can happen in various ways: through
37 * misconfiguration, by putting subinterfaces with different VLAN encaps in
38 * the same bridge-domain, etc. The EFP Filter Check detects such packets
39 * and drops them. It consists of two checks, one that verifies the packet
40 * prior to output VLAN tag rewrite and one that verifies the packet after
46 /* Next nodes for L2 output features */
47 u32 l2_out_feat_next[32];
49 /* convenience variables */
50 vlib_main_t *vlib_main;
51 vnet_main_t *vnet_main;
52 } l2_efp_filter_main_t;
57 /* per-pkt trace data */
60 u8 raw[12]; /* raw data (vlans) */
62 } l2_efp_filter_trace_t;
64 /* packet trace format function */
66 format_l2_efp_filter_trace (u8 * s, va_list * args)
68 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
69 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
70 l2_efp_filter_trace_t *t = va_arg (*args, l2_efp_filter_trace_t *);
72 s = format (s, "l2-output-vtr: sw_if_index %d dst %U src %U data "
73 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
75 format_ethernet_address, t->dst,
76 format_ethernet_address, t->src,
77 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
78 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
79 t->raw[10], t->raw[11]);
83 l2_efp_filter_main_t l2_efp_filter_main;
85 static vlib_node_registration_t l2_efp_filter_node;
87 #define foreach_l2_efp_filter_error \
88 _(L2_EFP_FILTER, "L2 EFP filter packets") \
89 _(DROP, "L2 EFP filter post-rewrite drops")
93 #define _(sym,str) L2_EFP_FILTER_ERROR_##sym,
94 foreach_l2_efp_filter_error
96 L2_EFP_FILTER_N_ERROR,
97 } l2_efp_filter_error_t;
99 static char *l2_efp_filter_error_strings[] = {
100 #define _(sym,string) string,
101 foreach_l2_efp_filter_error
107 L2_EFP_FILTER_NEXT_DROP,
108 L2_EFP_FILTER_N_NEXT,
109 } l2_efp_filter_next_t;
113 * Extract fields from the packet that will be used in interface
116 static_always_inline void
117 extract_keys (vnet_main_t * vnet_main,
120 u32 * port_sw_if_index0,
121 u16 * first_ethertype0,
122 u16 * outer_id0, u16 * inner_id0, u32 * match_flags0)
124 ethernet_header_t *e0;
125 ethernet_vlan_header_t *h0;
130 vnet_get_sup_sw_interface (vnet_main, sw_if_index0)->sw_if_index;
132 e0 = vlib_buffer_get_current (b0);
133 h0 = (ethernet_vlan_header_t *) (e0 + 1);
135 *first_ethertype0 = clib_net_to_host_u16 (e0->type);
136 *outer_id0 = clib_net_to_host_u16 (h0[0].priority_cfi_and_id);
137 *inner_id0 = clib_net_to_host_u16 (h0[1].priority_cfi_and_id);
139 tag_len = vnet_buffer (b0)->l2.l2_len - sizeof (ethernet_header_t);
140 tag_num = tag_len / sizeof (ethernet_vlan_header_t);
141 *match_flags0 = eth_create_valid_subint_match_flags (tag_num);
145 * EFP filtering is a basic switch feature which prevents an interface from
146 * transmitting a packet that doesn't match the interface's ingress match
147 * criteria. The check has two parts, one performed before egress vlan tag
148 * rewrite and one after.
150 * The pre-rewrite check insures the packet matches what an ingress packet looks
151 * like after going through the interface's ingress tag rewrite operation. Only
152 * pushed tags are compared. So:
153 * - if the ingress vlan tag rewrite pushes no tags (or is not enabled),
154 * any packet passes the filter
155 * - if the ingress vlan tag rewrite pushes one tag,
156 * the packet must have at least one tag, and the outer tag must match the pushed tag
157 * - if the ingress vlan tag rewrite pushes two tags,
158 * the packet must have at least two tags, and the outer two tags must match the pushed tags
160 * The pre-rewrite check is performed in the l2-output node.
162 * The post-rewrite check insures the packet matches what an ingress packet looks
163 * like before going through the interface's ingress tag rewrite operation. It verifies
164 * that such a packet arriving on the wire at this port would be classified as arriving
165 * an input interface equal to the packet's output interface. This can be done by running
166 * the output packet's vlan tags and output port through the interface classification,
167 * and checking if the resulting interface matches the output interface.
169 * The post-rewrite check is performed here.
173 l2_efp_filter_node_fn (vlib_main_t * vm,
174 vlib_node_runtime_t * node, vlib_frame_t * frame)
176 u32 n_left_from, *from, *to_next;
177 l2_efp_filter_next_t next_index;
178 l2_efp_filter_main_t *msm = &l2_efp_filter_main;
179 vlib_node_t *n = vlib_get_node (vm, l2_efp_filter_node.index);
180 u32 node_counter_base_index = n->error_heap_index;
181 vlib_error_main_t *em = &vm->error_main;
183 from = vlib_frame_vector_args (frame);
184 n_left_from = frame->n_vectors; /* number of packets to process */
185 next_index = node->cached_next_index;
187 while (n_left_from > 0)
191 /* get space to enqueue frame to graph node "next_index" */
192 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
194 while (n_left_from >= 6 && n_left_to_next >= 2)
197 vlib_buffer_t *b0, *b1;
199 u32 sw_if_index0, sw_if_index1;
200 u16 first_ethertype0, first_ethertype1;
201 u16 outer_id0, inner_id0, outer_id1, inner_id1;
202 u32 match_flags0, match_flags1;
203 u32 port_sw_if_index0, subint_sw_if_index0, port_sw_if_index1,
205 vnet_hw_interface_t *hi0, *hi1;
206 main_intf_t *main_intf0, *main_intf1;
207 vlan_intf_t *vlan_intf0, *vlan_intf1;
208 qinq_intf_t *qinq_intf0, *qinq_intf1;
210 __attribute__ ((unused)) u32 matched0, matched1;
213 /* Prefetch next iteration. */
215 vlib_buffer_t *p2, *p3, *p4, *p5;
216 __attribute__ ((unused)) u32 sw_if_index2, sw_if_index3;
218 p2 = vlib_get_buffer (vm, from[2]);
219 p3 = vlib_get_buffer (vm, from[3]);
220 p4 = vlib_get_buffer (vm, from[4]);
221 p5 = vlib_get_buffer (vm, from[5]);
223 /* Prefetch the buffer header and packet for the N+2 loop iteration */
224 vlib_prefetch_buffer_header (p4, LOAD);
225 vlib_prefetch_buffer_header (p5, LOAD);
227 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
228 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
231 * Prefetch the input config for the N+1 loop iteration
232 * This depends on the buffer header above
234 sw_if_index2 = vnet_buffer (p2)->sw_if_index[VLIB_TX];
235 sw_if_index3 = vnet_buffer (p3)->sw_if_index[VLIB_TX];
238 * CLIB_PREFETCH (vec_elt_at_index(l2output_main.configs, sw_if_index2), CLIB_CACHE_LINE_BYTES, LOAD);
239 * CLIB_PREFETCH (vec_elt_at_index(l2output_main.configs, sw_if_index3), CLIB_CACHE_LINE_BYTES, LOAD);
243 /* speculatively enqueue b0 and b1 to the current next frame */
244 /* bi is "buffer index", b is pointer to the buffer */
245 to_next[0] = bi0 = from[0];
246 to_next[1] = bi1 = from[1];
252 b0 = vlib_get_buffer (vm, bi0);
253 b1 = vlib_get_buffer (vm, bi1);
255 /* TX interface handles */
256 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
257 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
259 /* process 2 packets */
260 em->counters[node_counter_base_index +
261 L2_EFP_FILTER_ERROR_L2_EFP_FILTER] += 2;
263 /* Determine next node */
264 next0 = vnet_l2_feature_next (b0, msm->l2_out_feat_next,
265 L2OUTPUT_FEAT_EFP_FILTER);
266 next1 = vnet_l2_feature_next (b1, msm->l2_out_feat_next,
267 L2OUTPUT_FEAT_EFP_FILTER);
269 /* perform the efp filter check on two packets */
271 extract_keys (msm->vnet_main,
276 &outer_id0, &inner_id0, &match_flags0);
278 extract_keys (msm->vnet_main,
283 &outer_id1, &inner_id1, &match_flags1);
285 eth_vlan_table_lookups (ðernet_main,
292 &main_intf0, &vlan_intf0, &qinq_intf0);
294 eth_vlan_table_lookups (ðernet_main,
301 &main_intf1, &vlan_intf1, &qinq_intf1);
303 matched0 = eth_identify_subint (hi0,
309 &subint_sw_if_index0,
312 matched1 = eth_identify_subint (hi1,
318 &subint_sw_if_index1,
321 if (PREDICT_FALSE (sw_if_index0 != subint_sw_if_index0))
324 next0 = L2_EFP_FILTER_NEXT_DROP;
325 b0->error = node->errors[L2_EFP_FILTER_ERROR_DROP];
328 if (PREDICT_FALSE (sw_if_index1 != subint_sw_if_index1))
331 next1 = L2_EFP_FILTER_NEXT_DROP;
332 b1->error = node->errors[L2_EFP_FILTER_ERROR_DROP];
335 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
337 if (b0->flags & VLIB_BUFFER_IS_TRACED)
339 ethernet_header_t *h0 = vlib_buffer_get_current (b0);
340 l2_efp_filter_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 ethernet_header_t *h1 = vlib_buffer_get_current (b1);
350 l2_efp_filter_trace_t *t =
351 vlib_add_trace (vm, node, b1, sizeof (*t));
352 t->sw_if_index = sw_if_index1;
353 clib_memcpy (t->src, h1->src_address, 6);
354 clib_memcpy (t->dst, h1->dst_address, 6);
355 clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
359 /* verify speculative enqueues, maybe switch current next frame */
360 /* if next0==next1==next_index then nothing special needs to be done */
361 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
362 to_next, n_left_to_next,
363 bi0, bi1, next0, next1);
366 while (n_left_from > 0 && n_left_to_next > 0)
372 u16 first_ethertype0;
373 u16 outer_id0, inner_id0;
375 u32 port_sw_if_index0, subint_sw_if_index0;
376 vnet_hw_interface_t *hi0;
377 main_intf_t *main_intf0;
378 vlan_intf_t *vlan_intf0;
379 qinq_intf_t *qinq_intf0;
381 __attribute__ ((unused)) u32 matched0;
384 /* speculatively enqueue b0 to the current next frame */
392 b0 = vlib_get_buffer (vm, bi0);
393 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
395 /* process 1 packet */
396 em->counters[node_counter_base_index +
397 L2_EFP_FILTER_ERROR_L2_EFP_FILTER] += 1;
399 /* Determine next node */
400 next0 = vnet_l2_feature_next (b0, msm->l2_out_feat_next,
401 L2OUTPUT_FEAT_EFP_FILTER);
403 /* perform the efp filter check on one packet */
405 extract_keys (msm->vnet_main,
410 &outer_id0, &inner_id0, &match_flags0);
412 eth_vlan_table_lookups (ðernet_main,
419 &main_intf0, &vlan_intf0, &qinq_intf0);
421 matched0 = eth_identify_subint (hi0,
427 &subint_sw_if_index0,
430 if (PREDICT_FALSE (sw_if_index0 != subint_sw_if_index0))
433 next0 = L2_EFP_FILTER_NEXT_DROP;
434 b0->error = node->errors[L2_EFP_FILTER_ERROR_DROP];
437 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
438 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
440 ethernet_header_t *h0 = vlib_buffer_get_current (b0);
441 l2_efp_filter_trace_t *t =
442 vlib_add_trace (vm, node, b0, sizeof (*t));
443 t->sw_if_index = sw_if_index0;
444 clib_memcpy (t->src, h0->src_address, 6);
445 clib_memcpy (t->dst, h0->dst_address, 6);
446 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
449 /* verify speculative enqueue, maybe switch current next frame */
450 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
451 to_next, n_left_to_next,
455 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
458 return frame->n_vectors;
463 VLIB_REGISTER_NODE (l2_efp_filter_node,static) = {
464 .function = l2_efp_filter_node_fn,
465 .name = "l2-efp-filter",
466 .vector_size = sizeof (u32),
467 .format_trace = format_l2_efp_filter_trace,
468 .type = VLIB_NODE_TYPE_INTERNAL,
470 .n_errors = ARRAY_LEN(l2_efp_filter_error_strings),
471 .error_strings = l2_efp_filter_error_strings,
473 .n_next_nodes = L2_EFP_FILTER_N_NEXT,
475 /* edit / add dispositions here */
477 [L2_EFP_FILTER_NEXT_DROP] = "error-drop",
482 VLIB_NODE_FUNCTION_MULTIARCH (l2_efp_filter_node, l2_efp_filter_node_fn)
483 clib_error_t *l2_efp_filter_init (vlib_main_t * vm)
485 l2_efp_filter_main_t *mp = &l2_efp_filter_main;
488 mp->vnet_main = vnet_get_main ();
490 /* Initialize the feature next-node indexes */
491 feat_bitmap_init_next_nodes (vm,
492 l2_efp_filter_node.index,
494 l2output_get_feat_names (),
495 mp->l2_out_feat_next);
500 VLIB_INIT_FUNCTION (l2_efp_filter_init);
503 /** Enable/disable the EFP Filter check on the subinterface. */
505 l2_efp_filter_configure (vnet_main_t * vnet_main, u32 sw_if_index, u32 enable)
507 /* set the interface flag */
508 l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_EFP_FILTER, enable);
513 * Set subinterface egress efp filter enable/disable.
515 * set interface l2 efp-filter <interface> [disable]]
517 static clib_error_t *
518 int_l2_efp_filter (vlib_main_t * vm,
519 unformat_input_t * input, vlib_cli_command_t * cmd)
521 vnet_main_t *vnm = vnet_get_main ();
522 clib_error_t *error = 0;
526 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
528 error = clib_error_return (0, "unknown interface `%U'",
529 format_unformat_error, input);
534 if (unformat (input, "disable"))
539 /* enable/disable the feature */
540 l2_efp_filter_configure (vnm, sw_if_index, enable);
548 * EFP filtering is a basic switch feature which prevents an interface from
549 * transmitting a packet that doesn't match the interface's ingress match
550 * criteria. The check has two parts, one performed before egress vlan tag
551 * rewrite and one after. This command enables or disables the EFP filtering
552 * for a given sub-interface.
555 * Example of how to enable a Layer 2 efp-filter on a sub-interface:
556 * @cliexcmd{set interface l2 efp-filter GigabitEthernet0/8/0.200}
557 * Example of how to disable a Layer 2 efp-filter on a sub-interface:
558 * @cliexcmd{set interface l2 efp-filter GigabitEthernet0/8/0.200 disable}
561 VLIB_CLI_COMMAND (int_l2_efp_filter_cli, static) = {
562 .path = "set interface l2 efp-filter",
563 .short_help = "set interface l2 efp-filter <interface> [disable]",
564 .function = int_l2_efp_filter,
570 * fd.io coding-style-patch-verification: ON
573 * eval: (c-set-style "gnu")