Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * l2_vtr.h : layer 2 vlan tag rewrite processing |
| 3 | * |
| 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: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #ifndef included_vnet_l2_vtr_h |
| 19 | #define included_vnet_l2_vtr_h |
| 20 | |
| 21 | #include <vlib/vlib.h> |
| 22 | #include <vnet/vnet.h> |
Damjan Marion | 864d857 | 2019-09-19 18:43:42 +0200 | [diff] [blame] | 23 | #include <vnet/ethernet/ethernet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | #include <vnet/ethernet/packet.h> |
| 25 | #include <vnet/l2/l2_vtr.h> |
| 26 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 27 | /* VTR config options for API and CLI support */ |
| 28 | typedef enum |
| 29 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | L2_VTR_DISABLED, |
| 31 | L2_VTR_PUSH_1, |
| 32 | L2_VTR_PUSH_2, |
| 33 | L2_VTR_POP_1, |
| 34 | L2_VTR_POP_2, |
| 35 | L2_VTR_TRANSLATE_1_1, |
| 36 | L2_VTR_TRANSLATE_1_2, |
| 37 | L2_VTR_TRANSLATE_2_1, |
| 38 | L2_VTR_TRANSLATE_2_2 |
| 39 | } l2_vtr_op_t; |
| 40 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 41 | /** |
| 42 | * Per-interface vlan tag rewrite configuration |
| 43 | * There will be one instance of this struct for each sw_if_index |
| 44 | * for both input vtr and output vtr |
| 45 | */ |
| 46 | typedef struct |
| 47 | { |
| 48 | union |
| 49 | { |
| 50 | /* |
| 51 | * Up to two vlan tags to push. |
| 52 | * if there is only one vlan tag to push, it is in tags[1]. |
| 53 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | ethernet_vlan_header_tv_t tags[2]; |
| 55 | u64 raw_tags; |
| 56 | }; |
| 57 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 58 | union |
| 59 | { |
| 60 | struct |
| 61 | { |
| 62 | u8 push_bytes; /* number of bytes to push for up to 2 vlans (0,4,8) */ |
| 63 | u8 pop_bytes; /* number of bytes to pop for up to 2 vlans (0,4,8) */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | }; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 65 | u16 push_and_pop_bytes; /* if 0 then the feature is disabled */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | }; |
| 67 | } vtr_config_t; |
| 68 | |
| 69 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 70 | /** |
| 71 | * Perform the configured tag rewrite on the packet. |
| 72 | * Return 0 if ok, 1 if packet should be dropped (e.g. tried to pop |
| 73 | * too many tags) |
| 74 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | always_inline u32 |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 76 | l2_vtr_process (vlib_buffer_t * b0, vtr_config_t * config) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 78 | u8 *eth; |
Andrew Yourtchenko | 33e1243 | 2024-09-17 15:01:44 +0200 | [diff] [blame] | 79 | u8 save_macs[12]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
| 81 | eth = vlib_buffer_get_current (b0); |
| 82 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 83 | /* copy the 12B dmac and smac to a temporary location */ |
Andrew Yourtchenko | 33e1243 | 2024-09-17 15:01:44 +0200 | [diff] [blame] | 84 | clib_memcpy_fast (save_macs, eth, sizeof (save_macs)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 86 | /* adjust for popped tags */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | eth += config->pop_bytes; |
| 88 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 89 | /* if not enough tags to pop then drop packet */ |
| 90 | if (PREDICT_FALSE ((vnet_buffer (b0)->l2.l2_len - 12) < config->pop_bytes)) |
| 91 | { |
| 92 | return 1; |
| 93 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 95 | /* copy the 2 new tags to the start of the packet */ |
Andrew Yourtchenko | 33e1243 | 2024-09-17 15:01:44 +0200 | [diff] [blame] | 96 | clib_memcpy_fast (eth + 12 - 8, &config->raw_tags, |
| 97 | sizeof (config->raw_tags)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 99 | /* TODO: set cos bits */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 101 | /* adjust for pushed tags: */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | eth -= config->push_bytes; |
| 103 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 104 | /* copy the 12 dmac and smac back to the packet */ |
Andrew Yourtchenko | 33e1243 | 2024-09-17 15:01:44 +0200 | [diff] [blame] | 105 | clib_memcpy_fast (eth, save_macs, sizeof (save_macs)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | |
Neale Ranns | 4c151b5 | 2017-10-24 07:55:53 -0700 | [diff] [blame] | 107 | /* Update l2 parameters */ |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 108 | vnet_buffer (b0)->l2.l2_len += |
| 109 | (word) config->push_bytes - (word) config->pop_bytes; |
Neale Ranns | 4c151b5 | 2017-10-24 07:55:53 -0700 | [diff] [blame] | 110 | vnet_buffer (b0)->l2_hdr_offset -= |
| 111 | (word) config->push_bytes - (word) config->pop_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 113 | /* Update vlan tag count */ |
| 114 | ethernet_buffer_adjust_vlan_count_by_bytes (b0, |
| 115 | (word) config->push_bytes - |
| 116 | (word) config->pop_bytes); |
Chris Luke | 194ebc5 | 2016-04-25 14:26:55 -0400 | [diff] [blame] | 117 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 118 | /* Update packet len */ |
| 119 | vlib_buffer_advance (b0, |
| 120 | (word) config->pop_bytes - (word) config->push_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 125 | /* |
| 126 | * Perform the egress pre-vlan tag rewrite EFP Filter check. |
| 127 | * The post-vlan tag rewrite check is a separate graph node. |
| 128 | * |
| 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 |
| 133 | * on the packet. |
| 134 | * |
| 135 | * Return 0 if ok, 1 if packet should be dropped. |
| 136 | * This function should be passed the input vtr config for the interface. |
| 137 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | always_inline u8 |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 139 | l2_efp_filter_process (vlib_buffer_t * b0, vtr_config_t * in_config) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | { |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 141 | u8 *eth; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | u64 packet_tags; |
| 143 | u64 tag_mask; |
| 144 | |
| 145 | eth = vlib_buffer_get_current (b0); |
| 146 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 147 | /* |
| 148 | * If there are 2 tags pushed, they must match config->tags[0] and |
| 149 | * config->tags[1]. |
| 150 | * If there is one tag pushed, it must match config->tag[1]. |
| 151 | * If there are 0 tags pushed, the check passes. |
| 152 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 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 */ |
| 157 | tag_mask = |
| 158 | (in_config->push_bytes == |
| 159 | 4) ? clib_net_to_host_u64 (0xFFFF0FFF) : tag_mask; |
| 160 | /* mask for always match */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | tag_mask = (in_config->push_bytes == 0) ? 0 : tag_mask; |
| 162 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 163 | /* |
| 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. |
| 167 | */ |
| 168 | packet_tags = *((u64 *) (eth + 4 + in_config->push_bytes)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 170 | /* Check if the packet tags match the configured tags */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | return (packet_tags & tag_mask) != in_config->raw_tags; |
| 172 | } |
| 173 | |
Pavel Kotucek | 95300d1 | 2016-08-26 16:11:36 +0200 | [diff] [blame] | 174 | typedef struct |
| 175 | { |
| 176 | union |
| 177 | { |
| 178 | ethernet_pbb_header_t macs_tags; |
| 179 | struct |
| 180 | { |
| 181 | u64 data1; |
| 182 | u64 data2; |
| 183 | u16 data3; |
| 184 | u32 data4; |
| 185 | } raw_data; |
| 186 | }; |
| 187 | union |
| 188 | { |
| 189 | struct |
| 190 | { |
| 191 | u8 push_bytes; /* number of bytes to push pbb tags */ |
| 192 | u8 pop_bytes; /* number of bytes to pop pbb tags */ |
| 193 | }; |
| 194 | u16 push_and_pop_bytes; /* if 0 then the feature is disabled */ |
| 195 | }; |
| 196 | } ptr_config_t; |
| 197 | |
| 198 | always_inline u32 |
| 199 | l2_pbb_process (vlib_buffer_t * b0, ptr_config_t * config) |
| 200 | { |
| 201 | u8 *eth = vlib_buffer_get_current (b0); |
| 202 | |
| 203 | if (config->pop_bytes > 0) |
| 204 | { |
| 205 | ethernet_pbb_header_packed_t *ph = (ethernet_pbb_header_packed_t *) eth; |
| 206 | |
| 207 | // drop packet without PBB header or with wrong I-tag or B-tag |
| 208 | if (clib_net_to_host_u16 (ph->priority_dei_id) != |
| 209 | clib_net_to_host_u16 (config->macs_tags.priority_dei_id) |
| 210 | || clib_net_to_host_u32 (ph->priority_dei_uca_res_sid) != |
| 211 | clib_net_to_host_u32 (config->macs_tags.priority_dei_uca_res_sid)) |
| 212 | return 1; |
| 213 | |
| 214 | eth += config->pop_bytes; |
| 215 | } |
| 216 | |
| 217 | if (config->push_bytes > 0) |
| 218 | { |
| 219 | eth -= config->push_bytes; |
| 220 | // copy the B-DA (6B), B-SA (6B), B-TAG (4B), I-TAG (6B) |
| 221 | *((u64 *) eth) = config->raw_data.data1; |
| 222 | *((u64 *) (eth + 8)) = config->raw_data.data2; |
| 223 | *((u16 *) (eth + 16)) = config->raw_data.data3; |
| 224 | *((u32 *) (eth + 18)) = config->raw_data.data4; |
| 225 | } |
| 226 | |
| 227 | /* Update l2_len */ |
| 228 | vnet_buffer (b0)->l2.l2_len += |
| 229 | (word) config->push_bytes - (word) config->pop_bytes; |
| 230 | /* Update packet len */ |
| 231 | vlib_buffer_advance (b0, |
| 232 | (word) config->pop_bytes - (word) config->push_bytes); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | u32 l2pbb_configure (vlib_main_t * vlib_main, |
| 238 | vnet_main_t * vnet_main, u32 sw_if_index, u32 vtr_op, |
| 239 | u8 * b_dmac, u8 * b_smac, |
| 240 | u16 b_vlanid, u32 i_sid, u16 vlan_outer_tag); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 242 | /** |
| 243 | * Configure vtag tag rewrite on the given interface. |
| 244 | * Return 1 if there is an error, 0 if ok |
| 245 | */ |
| 246 | u32 l2vtr_configure (vlib_main_t * vlib_main, |
| 247 | vnet_main_t * vnet_main, |
| 248 | u32 sw_if_index, |
| 249 | u32 vtr_op, u32 push_dot1q, u32 vtr_tag1, u32 vtr_tag2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 251 | /** |
| 252 | * Get vtag tag rewrite on the given interface. |
| 253 | * Return 1 if there is an error, 0 if ok |
| 254 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | u32 l2vtr_get (vlib_main_t * vlib_main, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 256 | vnet_main_t * vnet_main, |
| 257 | u32 sw_if_index, |
| 258 | u32 * vtr_op, |
| 259 | u32 * push_dot1q, u32 * vtr_tag1, u32 * vtr_tag2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 261 | /** |
| 262 | * Get pbb tag rewrite on the given interface. |
| 263 | * Return 1 if there is an error, 0 if ok |
| 264 | */ |
| 265 | u32 l2pbb_get (vlib_main_t * vlib_main, |
| 266 | vnet_main_t * vnet_main, |
| 267 | u32 sw_if_index, |
| 268 | u32 * vtr_op, |
| 269 | u16 * outer_tag, |
Gabriel Ganne | f7f2a9f | 2017-03-06 15:19:40 +0100 | [diff] [blame] | 270 | ethernet_header_t * eth_hdr, u16 * b_vlanid, u32 * i_sid); |
Pavel Kotucek | 65e8457 | 2017-01-16 17:01:56 +0100 | [diff] [blame] | 271 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 272 | #endif /* included_vnet_l2_vtr_h */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * fd.io coding-style-patch-verification: ON |
| 277 | * |
| 278 | * Local Variables: |
| 279 | * eval: (c-set-style "gnu") |
| 280 | * End: |
| 281 | */ |