2 * l2_vtr.h : layer 2 vlan tag rewrite 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 #ifndef included_vnet_l2_vtr_h
19 #define included_vnet_l2_vtr_h
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vnet/ethernet/packet.h>
24 #include <vnet/l2/l2_vtr.h>
26 /* VTR config options for API and CLI support */
41 * Per-interface vlan tag rewrite configuration
42 * There will be one instance of this struct for each sw_if_index
43 * for both input vtr and output vtr
50 * Up to two vlan tags to push.
51 * if there is only one vlan tag to push, it is in tags[1].
53 ethernet_vlan_header_tv_t tags[2];
61 u8 push_bytes; /* number of bytes to push for up to 2 vlans (0,4,8) */
62 u8 pop_bytes; /* number of bytes to pop for up to 2 vlans (0,4,8) */
64 u16 push_and_pop_bytes; /* if 0 then the feature is disabled */
70 * Perform the configured tag rewrite on the packet.
71 * Return 0 if ok, 1 if packet should be dropped (e.g. tried to pop
75 l2_vtr_process (vlib_buffer_t * b0, vtr_config_t * config)
81 eth = vlib_buffer_get_current (b0);
83 /* copy the 12B dmac and smac to a temporary location */
84 temp_8 = *((u64 *) eth);
85 temp_4 = *((u32 *) (eth + 8));
87 /* adjust for popped tags */
88 eth += config->pop_bytes;
90 /* if not enough tags to pop then drop packet */
91 if (PREDICT_FALSE ((vnet_buffer (b0)->l2.l2_len - 12) < config->pop_bytes))
96 /* copy the 2 new tags to the start of the packet */
97 *((u64 *) (eth + 12 - 8)) = config->raw_tags;
99 /* TODO: set cos bits */
101 /* adjust for pushed tags: */
102 eth -= config->push_bytes;
104 /* copy the 12 dmac and smac back to the packet */
105 *((u64 *) eth) = temp_8;
106 *((u32 *) (eth + 8)) = temp_4;
109 vnet_buffer (b0)->l2.l2_len +=
110 (word) config->push_bytes - (word) config->pop_bytes;
112 /* Update vlan tag count */
113 ethernet_buffer_adjust_vlan_count_by_bytes (b0,
114 (word) config->push_bytes -
115 (word) config->pop_bytes);
117 /* Update packet len */
118 vlib_buffer_advance (b0,
119 (word) config->pop_bytes - (word) config->push_bytes);
126 * Perform the egress pre-vlan tag rewrite EFP Filter check.
127 * The post-vlan tag rewrite check is a separate graph node.
129 * This check insures that a packet being output to an interface
130 * (before output vtr is performed) has vlan tags that match those
131 * on a packet received from that interface (after vtr has been performed).
132 * This means verifying that any tags pushed by input vtr are present
135 * Return 0 if ok, 1 if packet should be dropped.
136 * This function should be passed the input vtr config for the interface.
139 l2_efp_filter_process (vlib_buffer_t * b0, vtr_config_t * in_config)
145 eth = vlib_buffer_get_current (b0);
148 * If there are 2 tags pushed, they must match config->tags[0] and
150 * If there is one tag pushed, it must match config->tag[1].
151 * If there are 0 tags pushed, the check passes.
154 /* mask for two vlan id and ethertypes, no cos bits */
155 tag_mask = clib_net_to_host_u64 (0xFFFF0FFFFFFF0FFF);
156 /* mask for one vlan id and ethertype, no cos bits */
158 (in_config->push_bytes ==
159 4) ? clib_net_to_host_u64 (0xFFFF0FFF) : tag_mask;
160 /* mask for always match */
161 tag_mask = (in_config->push_bytes == 0) ? 0 : tag_mask;
164 * Read 8B from the packet, getting the proper set of vlan tags
165 * For 0 push bytes, the address doesn't matter since the mask
166 * clears the data to 0.
168 packet_tags = *((u64 *) (eth + 4 + in_config->push_bytes));
170 /* Check if the packet tags match the configured tags */
171 return (packet_tags & tag_mask) != in_config->raw_tags;
176 * Configure vtag tag rewrite on the given interface.
177 * Return 1 if there is an error, 0 if ok
179 u32 l2vtr_configure (vlib_main_t * vlib_main,
180 vnet_main_t * vnet_main,
182 u32 vtr_op, u32 push_dot1q, u32 vtr_tag1, u32 vtr_tag2);
185 * Get vtag tag rewrite on the given interface.
186 * Return 1 if there is an error, 0 if ok
188 u32 l2vtr_get (vlib_main_t * vlib_main,
189 vnet_main_t * vnet_main,
192 u32 * push_dot1q, u32 * vtr_tag1, u32 * vtr_tag2);
194 #endif /* included_vnet_l2_vtr_h */
198 * fd.io coding-style-patch-verification: ON
201 * eval: (c-set-style "gnu")