2 * l2_vtr.c : layer 2 vlan tag rewrite configuration
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/l2_input.h>
23 #include <vnet/l2/l2_output.h>
24 #include <vnet/l2/feat_bitmap.h>
25 #include <vnet/l2/l2_vtr.h>
26 #include <vnet/l2/l2_input_vtr.h>
27 #include <vnet/l2/l2_output.h>
29 #include <vppinfra/error.h>
34 * @brief Ethernet VLAN Tag Rewrite.
36 * VLAN tag rewrite provides the ability to change the VLAN tags on a packet.
37 * Existing tags can be popped, new tags can be pushed, and existing tags can
38 * be swapped with new tags. The rewrite feature is attached to a subinterface
39 * as input and output operations. The input operation is explicitly configured.
40 * The output operation is the symmetric opposite and is automatically derived
41 * from the input operation.
44 /** Just a placeholder; ensures file is not eliminated by linker. */
46 l2_vtr_init (vlib_main_t * vm)
51 VLIB_INIT_FUNCTION (l2_vtr_init);
54 l2pbb_configure (vlib_main_t * vlib_main,
55 vnet_main_t * vnet_main, u32 sw_if_index, u32 vtr_op,
56 u8 * b_dmac, u8 * b_smac,
57 u16 b_vlanid, u32 i_sid, u16 vlan_outer_tag)
62 l2_output_config_t *config = 0;
63 vnet_hw_interface_t *hi;
64 hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
68 error = VNET_API_ERROR_INVALID_INTERFACE;
72 // Config for this interface should be already initialized
73 ptr_config_t *in_config;
74 ptr_config_t *out_config;
75 config = vec_elt_at_index (l2output_main.configs, sw_if_index);
76 in_config = &(config->input_pbb_vtr);
77 out_config = &(config->output_pbb_vtr);
79 in_config->pop_bytes = 0;
80 in_config->push_bytes = 0;
81 out_config->pop_bytes = 0;
82 out_config->push_bytes = 0;
83 enable = (vtr_op != L2_VTR_DISABLED);
88 if (vtr_op == L2_VTR_POP_2)
90 in_config->pop_bytes = sizeof (ethernet_pbb_header_packed_t);
92 else if (vtr_op == L2_VTR_PUSH_2)
94 clib_memcpy (in_config->macs_tags.b_dst_address, b_dmac,
95 sizeof (in_config->macs_tags.b_dst_address));
96 clib_memcpy (in_config->macs_tags.b_src_address, b_smac,
97 sizeof (in_config->macs_tags.b_src_address));
98 in_config->macs_tags.b_type =
99 clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AD);
100 in_config->macs_tags.priority_dei_id =
101 clib_net_to_host_u16 (b_vlanid & 0xFFF);
102 in_config->macs_tags.i_type =
103 clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AH);
104 in_config->macs_tags.priority_dei_uca_res_sid =
105 clib_net_to_host_u32 (i_sid & 0xFFFFF);
106 in_config->push_bytes = sizeof (ethernet_pbb_header_packed_t);
108 else if (vtr_op == L2_VTR_TRANSLATE_2_2)
114 * Construct the output tag-rewrite config
116 * The push/pop values are always reversed
118 out_config->raw_data = in_config->raw_data;
119 out_config->pop_bytes = in_config->push_bytes;
120 out_config->push_bytes = in_config->pop_bytes;
123 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VTR, enable);
125 config->out_vtr_flag = (u8) enable;
127 /* output vtr enable is checked explicitly in l2_output */
132 * Configure vtag tag rewrite on the given interface.
133 * Return 1 if there is an error, 0 if ok
136 l2vtr_configure (vlib_main_t * vlib_main, vnet_main_t * vnet_main, u32 sw_if_index, u32 vtr_op, u32 push_dot1q, /* ethertype of first pushed tag is dot1q/dot1ad */
137 u32 vtr_tag1, /* first pushed tag */
138 u32 vtr_tag2) /* second pushed tag */
140 vnet_hw_interface_t *hi;
141 vnet_sw_interface_t *si;
144 l2_output_config_t *config;
145 vtr_config_t *in_config;
146 vtr_config_t *out_config;
152 hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
153 if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index))
155 error = VNET_API_ERROR_INVALID_INTERFACE; /* non-ethernet interface */
159 /* Init the config for this interface */
160 vec_validate (l2output_main.configs, sw_if_index);
161 config = vec_elt_at_index (l2output_main.configs, sw_if_index);
162 in_config = &(config->input_vtr);
163 out_config = &(config->output_vtr);
164 in_config->raw_tags = 0;
165 out_config->raw_tags = 0;
167 /* Get the configured tags for the interface */
168 si = vnet_get_sw_interface (vnet_main, sw_if_index);
169 hw_no_tags = (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
171 /* Construct the input tag-rewrite config */
174 clib_net_to_host_u16 (push_dot1q ? ETHERNET_TYPE_VLAN :
175 ETHERNET_TYPE_DOT1AD);
176 push_inner_et = clib_net_to_host_u16 (ETHERNET_TYPE_VLAN);
177 vtr_tag1 = clib_net_to_host_u16 (vtr_tag1);
178 vtr_tag2 = clib_net_to_host_u16 (vtr_tag2);
180 /* Determine number of vlan tags with explictly configured values */
182 if (hw_no_tags || si->sub.eth.flags.no_tags)
186 else if (si->sub.eth.flags.one_tag)
189 if (si->sub.eth.flags.outer_vlan_id_any)
194 else if (si->sub.eth.flags.two_tags)
197 if (si->sub.eth.flags.inner_vlan_id_any)
201 if (si->sub.eth.flags.outer_vlan_id_any)
209 case L2_VTR_DISABLED:
210 in_config->push_and_pop_bytes = 0;
216 /* Need one or two tags */
217 error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT;
220 in_config->pop_bytes = 4;
221 in_config->push_bytes = 0;
227 error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; /* Need two tags */
230 in_config->pop_bytes = 8;
231 in_config->push_bytes = 0;
233 out_config->push_bytes = in_config->pop_bytes;
234 out_config->pop_bytes = in_config->push_bytes;
238 in_config->pop_bytes = 0;
239 in_config->push_bytes = 4;
240 in_config->tags[1].priority_cfi_and_id = vtr_tag1;
241 in_config->tags[1].type = push_outer_et;
245 in_config->pop_bytes = 0;
246 in_config->push_bytes = 8;
247 in_config->tags[0].priority_cfi_and_id = vtr_tag1;
248 in_config->tags[0].type = push_outer_et;
249 in_config->tags[1].priority_cfi_and_id = vtr_tag2;
250 in_config->tags[1].type = push_inner_et;
253 case L2_VTR_TRANSLATE_1_1:
256 error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; /* Need one or two tags */
259 in_config->pop_bytes = 4;
260 in_config->push_bytes = 4;
261 in_config->tags[1].priority_cfi_and_id = vtr_tag1;
262 in_config->tags[1].type = push_outer_et;
265 case L2_VTR_TRANSLATE_1_2:
268 error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; /* Need one or two tags */
271 in_config->pop_bytes = 4;
272 in_config->push_bytes = 8;
273 in_config->tags[0].priority_cfi_and_id = vtr_tag1;
274 in_config->tags[0].type = push_outer_et;
275 in_config->tags[1].priority_cfi_and_id = vtr_tag2;
276 in_config->tags[1].type = push_inner_et;
279 case L2_VTR_TRANSLATE_2_1:
282 error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; /* Need two tags */
285 in_config->pop_bytes = 8;
286 in_config->push_bytes = 4;
287 in_config->tags[1].priority_cfi_and_id = vtr_tag1;
288 in_config->tags[1].type = push_outer_et;
291 case L2_VTR_TRANSLATE_2_2:
294 error = VNET_API_ERROR_INVALID_VLAN_TAG_COUNT; /* Need two tags */
297 in_config->pop_bytes = 8;
298 in_config->push_bytes = 8;
299 in_config->tags[0].priority_cfi_and_id = vtr_tag1;
300 in_config->tags[0].type = push_outer_et;
301 in_config->tags[1].priority_cfi_and_id = vtr_tag2;
302 in_config->tags[1].type = push_inner_et;
307 * Construct the output tag-rewrite config
309 * The push/pop values are always reversed
311 out_config->push_bytes = in_config->pop_bytes;
312 out_config->pop_bytes = in_config->push_bytes;
314 /* Any pushed tags are derived from the subinterface config */
316 clib_net_to_host_u16 (si->sub.eth.flags.dot1ad ? ETHERNET_TYPE_DOT1AD :
318 push_inner_et = clib_net_to_host_u16 (ETHERNET_TYPE_VLAN);
319 vtr_tag1 = clib_net_to_host_u16 (si->sub.eth.outer_vlan_id);
320 vtr_tag2 = clib_net_to_host_u16 (si->sub.eth.inner_vlan_id);
322 if (out_config->push_bytes == 4)
324 out_config->tags[1].priority_cfi_and_id = vtr_tag1;
325 out_config->tags[1].type = push_outer_et;
327 else if (out_config->push_bytes == 8)
329 out_config->tags[0].priority_cfi_and_id = vtr_tag1;
330 out_config->tags[0].type = push_outer_et;
331 out_config->tags[1].priority_cfi_and_id = vtr_tag2;
332 out_config->tags[1].type = push_inner_et;
335 /* set the interface enable flags */
336 enable = (vtr_op != L2_VTR_DISABLED);
337 config->out_vtr_flag = (u8) enable;
338 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VTR, enable);
339 /* output vtr enable is checked explicitly in l2_output */
346 * Get vtag tag rewrite on the given interface.
347 * Return 1 if there is an error, 0 if ok
350 l2vtr_get (vlib_main_t * vlib_main, vnet_main_t * vnet_main, u32 sw_if_index, u32 * vtr_op, u32 * push_dot1q, /* ethertype of first pushed tag is dot1q/dot1ad */
351 u32 * vtr_tag1, /* first pushed tag */
352 u32 * vtr_tag2) /* second pushed tag */
354 vnet_hw_interface_t *hi;
356 vtr_config_t *in_config;
358 if (!vtr_op || !push_dot1q || !vtr_tag1 || !vtr_tag2)
360 clib_warning ("invalid arguments");
361 error = VNET_API_ERROR_INVALID_ARGUMENT;
365 *vtr_op = L2_VTR_DISABLED;
370 hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
371 if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index))
373 /* non-ethernet interface */
377 if (sw_if_index >= vec_len (l2output_main.configs))
379 /* no specific config (return disabled) */
383 /* Get the config for this interface */
385 &(vec_elt_at_index (l2output_main.configs, sw_if_index)->input_vtr);
388 if (in_config->push_and_pop_bytes == 0)
393 /* find out vtr_op */
394 switch (in_config->pop_bytes)
397 switch (in_config->push_bytes)
403 *vtr_op = L2_VTR_PUSH_1;
405 clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
407 (ETHERNET_TYPE_VLAN ==
408 clib_host_to_net_u16 (in_config->tags[1].type));
411 *vtr_op = L2_VTR_PUSH_2;
413 clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
415 clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
417 (ETHERNET_TYPE_VLAN ==
418 clib_host_to_net_u16 (in_config->tags[0].type));
421 clib_warning ("invalid push_bytes count: %d",
422 in_config->push_bytes);
423 error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
429 switch (in_config->push_bytes)
432 *vtr_op = L2_VTR_POP_1;
435 *vtr_op = L2_VTR_TRANSLATE_1_1;
437 clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
439 (ETHERNET_TYPE_VLAN ==
440 clib_host_to_net_u16 (in_config->tags[1].type));
443 *vtr_op = L2_VTR_TRANSLATE_1_2;
445 clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
447 clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
449 (ETHERNET_TYPE_VLAN ==
450 clib_host_to_net_u16 (in_config->tags[0].type));
453 clib_warning ("invalid push_bytes count: %d",
454 in_config->push_bytes);
455 error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
461 switch (in_config->push_bytes)
464 *vtr_op = L2_VTR_POP_2;
467 *vtr_op = L2_VTR_TRANSLATE_2_1;
469 clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
471 (ETHERNET_TYPE_VLAN ==
472 clib_host_to_net_u16 (in_config->tags[1].type));
475 *vtr_op = L2_VTR_TRANSLATE_2_2;
477 clib_host_to_net_u16 (in_config->tags[0].priority_cfi_and_id);
479 clib_host_to_net_u16 (in_config->tags[1].priority_cfi_and_id);
481 (ETHERNET_TYPE_VLAN ==
482 clib_host_to_net_u16 (in_config->tags[0].type));
485 clib_warning ("invalid push_bytes count: %d",
486 in_config->push_bytes);
487 error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
493 clib_warning ("invalid pop_bytes count: %d", in_config->pop_bytes);
494 error = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
503 * Set subinterface vtr enable/disable.
505 * set interface l2 tag-rewrite <interface> [disable | pop 1 | pop 2 | push {dot1q|dot1ad} <tag> [<tag>]]
507 * "push" can also be replaced by "translate-{1|2}-{1|2}"
509 static clib_error_t *
510 int_l2_vtr (vlib_main_t * vm,
511 unformat_input_t * input, vlib_cli_command_t * cmd)
513 vnet_main_t *vnm = vnet_get_main ();
514 clib_error_t *error = 0;
518 u32 tag1 = 0, tag2 = 0;
520 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
522 error = clib_error_return (0, "unknown interface `%U'",
523 format_unformat_error, input);
527 vtr_op = L2_VTR_DISABLED;
529 if (unformat (input, "disable"))
531 vtr_op = L2_VTR_DISABLED;
533 else if (unformat (input, "pop 1"))
535 vtr_op = L2_VTR_POP_1;
537 else if (unformat (input, "pop 2"))
539 vtr_op = L2_VTR_POP_2;
542 else if (unformat (input, "push dot1q %d %d", &tag1, &tag2))
544 vtr_op = L2_VTR_PUSH_2;
547 else if (unformat (input, "push dot1ad %d %d", &tag1, &tag2))
549 vtr_op = L2_VTR_PUSH_2;
552 else if (unformat (input, "push dot1q %d", &tag1))
554 vtr_op = L2_VTR_PUSH_1;
557 else if (unformat (input, "push dot1ad %d", &tag1))
559 vtr_op = L2_VTR_PUSH_1;
562 else if (unformat (input, "translate 1-1 dot1q %d", &tag1))
564 vtr_op = L2_VTR_TRANSLATE_1_1;
567 else if (unformat (input, "translate 1-1 dot1ad %d", &tag1))
569 vtr_op = L2_VTR_TRANSLATE_1_1;
572 else if (unformat (input, "translate 2-1 dot1q %d", &tag1))
574 vtr_op = L2_VTR_TRANSLATE_2_1;
577 else if (unformat (input, "translate 2-1 dot1ad %d", &tag1))
579 vtr_op = L2_VTR_TRANSLATE_2_1;
582 else if (unformat (input, "translate 2-2 dot1q %d %d", &tag1, &tag2))
584 vtr_op = L2_VTR_TRANSLATE_2_2;
587 else if (unformat (input, "translate 2-2 dot1ad %d %d", &tag1, &tag2))
589 vtr_op = L2_VTR_TRANSLATE_2_2;
592 else if (unformat (input, "translate 1-2 dot1q %d %d", &tag1, &tag2))
594 vtr_op = L2_VTR_TRANSLATE_1_2;
597 else if (unformat (input, "translate 1-2 dot1ad %d %d", &tag1, &tag2))
599 vtr_op = L2_VTR_TRANSLATE_1_2;
605 clib_error_return (0,
606 "expecting [disable | pop 1 | pop 2 | push {dot1q|dot1ah} <tag> [<tag>]\n"
607 " | translate {1|2}-{1|2} {dot1q|dot1ah} <tag> [<tag>]] but got `%U'",
608 format_unformat_error, input);
612 if (l2vtr_configure (vm, vnm, sw_if_index, vtr_op, push_dot1q, tag1, tag2))
615 clib_error_return (0,
616 "vlan tag rewrite is not compatible with interface");
625 * VLAN tag rewrite provides the ability to change the VLAN tags on a packet.
626 * Existing tags can be popped, new tags can be pushed, and existing tags can
627 * be swapped with new tags. The rewrite feature is attached to a subinterface
628 * as input and output operations. The input operation is explicitly configured.
629 * The output operation is the symmetric opposite and is automatically derived
630 * from the input operation.
632 * <b>POP:</b> For pop operations, the subinterface encapsulation (the vlan
633 * tags specified when it was created) must have at least the number of popped
634 * tags. e.g. the \"pop 2\" operation would be rejected on a single-vlan interface.
635 * The output tag-rewrite operation for pops is to push the specified number of
636 * vlan tags onto the packet. The pushed tag values are the ones in the
637 * subinterface encapsulation.
639 * <b>PUSH:</b> For push operations, the ethertype is also specified. The
640 * output tag-rewrite operation for pushes is to pop the same number of tags
641 * off the packet. If the packet doesn't have enough tags it is dropped.
646 * By default a subinterface has no tag-rewrite. To return a subinterface to
648 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 disable}
650 * To pop vlan tags off packets received from a subinterface, use:
651 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 pop 1}
652 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 pop 2}
654 * To push one or two vlan tags onto packets received from an interface, use:
655 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 push dot1q 100}
656 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 push dot1ad 100 150}
658 * Tags can also be translated, which is basically a combination of a pop and push.
659 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 1-1 dot1ad 100}
660 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 2-2 dot1ad 100 150}
661 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 1-2 dot1q 100}
662 * @cliexcmd{set interface l2 tag-rewrite GigabitEthernet0/8/0.200 translate 2-1 dot1q 100 150}
664 * To display the VLAN Tag settings, show the associate bridge-domain:
665 * @cliexstart{show bridge-domain 200 detail}
666 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
667 * 200 1 on on on on off N/A
669 * Interface Index SHG BVI VLAN-Tag-Rewrite
670 * GigabitEthernet0/8/0.200 5 0 - trans-1-1 dot1ad 100
671 * GigabitEthernet0/9/0.200 4 0 - none
672 * GigabitEthernet0/a/0.200 6 0 - none
677 VLIB_CLI_COMMAND (int_l2_vtr_cli, static) = {
678 .path = "set interface l2 tag-rewrite",
679 .short_help = "set interface l2 tag-rewrite <interface> [disable | pop {1|2} | push {dot1q|dot1ad} <tag> <tag>]",
680 .function = int_l2_vtr,
685 * Set subinterface pbb vtr enable/disable.
687 * set interface l2 pbb-tag-rewrite <interface> [disable | pop | push | translate_pbb_stag <outer_tag> dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]]
689 static clib_error_t *
690 int_l2_pbb_vtr (vlib_main_t * vm,
691 unformat_input_t * input, vlib_cli_command_t * cmd)
693 vnet_main_t *vnm = vnet_get_main ();
694 clib_error_t *error = 0;
695 u32 sw_if_index, tmp;
696 u32 vtr_op = L2_VTR_DISABLED;
700 u8 dmac_set = 0, smac_set = 0;
704 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
707 (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
709 else if (unformat (input, "disable"))
710 vtr_op = L2_VTR_DISABLED;
711 else if (vtr_op == L2_VTR_DISABLED && unformat (input, "pop"))
712 vtr_op = L2_VTR_POP_2;
713 else if (vtr_op == L2_VTR_DISABLED && unformat (input, "push"))
714 vtr_op = L2_VTR_PUSH_2;
715 else if (vtr_op == L2_VTR_DISABLED
716 && unformat (input, "translate_pbb_stag %d", &outer_tag))
717 vtr_op = L2_VTR_TRANSLATE_2_1;
718 else if (unformat (input, "dmac %U", unformat_ethernet_address, dmac))
720 else if (unformat (input, "smac %U", unformat_ethernet_address, smac))
722 else if (unformat (input, "b_vlanid %d", &tmp))
724 else if (unformat (input, "s_id %d", &s_id))
728 error = clib_error_return (0,
729 "expecting [disable | pop | push | translate_pbb_stag <outer_tag>\n"
730 "dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]]");
735 if ((vtr_op == L2_VTR_PUSH_2 || vtr_op == L2_VTR_TRANSLATE_2_1)
736 && (!dmac_set || !smac_set || s_id == ~0))
738 error = clib_error_return (0,
739 "expecting dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]");
744 (vm, vnm, sw_if_index, vtr_op, dmac, smac, b_vlanid, s_id, outer_tag))
747 clib_error_return (0,
748 "pbb tag rewrite is not compatible with interface");
757 VLIB_CLI_COMMAND (int_l2_pbb_vtr_cli, static) = {
758 .path = "set interface l2 pbb-tag-rewrite",
759 .short_help = "set interface l2 pbb-tag-rewrite <interface> [disable | pop | push | translate_pbb_stag <outer_tag> dmac <address> smac <address> s_id <nn> [b_vlanid <nn>]]",
760 .function = int_l2_pbb_vtr,
765 * fd.io coding-style-patch-verification: ON
768 * eval: (c-set-style "gnu")