Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * ethernet_node.c: ethernet packet processing |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | #include <vnet/pg/pg.h> |
| 42 | #include <vnet/ethernet/ethernet.h> |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 43 | #include <vnet/ethernet/p2p_ethernet.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | #include <vppinfra/sparse_vec.h> |
| 45 | #include <vnet/l2/l2_bvi.h> |
| 46 | |
| 47 | |
| 48 | #define foreach_ethernet_input_next \ |
| 49 | _ (PUNT, "error-punt") \ |
| 50 | _ (DROP, "error-drop") \ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 51 | _ (LLC, "llc-input") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 53 | typedef enum |
| 54 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | #define _(s,n) ETHERNET_INPUT_NEXT_##s, |
| 56 | foreach_ethernet_input_next |
| 57 | #undef _ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 58 | ETHERNET_INPUT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | } ethernet_input_next_t; |
| 60 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 61 | typedef struct |
| 62 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | u8 packet_data[32]; |
| 64 | } ethernet_input_trace_t; |
| 65 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 66 | static u8 * |
| 67 | format_ethernet_input_trace (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | { |
| 69 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 70 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 71 | ethernet_input_trace_t *t = va_arg (*va, ethernet_input_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | s = format (s, "%U", format_ethernet_header, t->packet_data); |
| 74 | |
| 75 | return s; |
| 76 | } |
| 77 | |
| 78 | vlib_node_registration_t ethernet_input_node; |
| 79 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 80 | typedef enum |
| 81 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | ETHERNET_INPUT_VARIANT_ETHERNET, |
| 83 | ETHERNET_INPUT_VARIANT_ETHERNET_TYPE, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | ETHERNET_INPUT_VARIANT_NOT_L2, |
| 85 | } ethernet_input_variant_t; |
| 86 | |
| 87 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | // Parse the ethernet header to extract vlan tags and innermost ethertype |
| 89 | static_always_inline void |
| 90 | parse_header (ethernet_input_variant_t variant, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 91 | vlib_buffer_t * b0, |
| 92 | u16 * type, |
| 93 | u16 * orig_type, |
| 94 | u16 * outer_id, u16 * inner_id, u32 * match_flags) |
| 95 | { |
Chris Luke | 194ebc5 | 2016-04-25 14:26:55 -0400 | [diff] [blame] | 96 | u8 vlan_count; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 98 | if (variant == ETHERNET_INPUT_VARIANT_ETHERNET |
| 99 | || variant == ETHERNET_INPUT_VARIANT_NOT_L2) |
| 100 | { |
| 101 | ethernet_header_t *e0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 103 | e0 = (void *) (b0->data + b0->current_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 105 | vnet_buffer (b0)->l2_hdr_offset = b0->current_data; |
Steven | 35de3b3 | 2017-12-03 23:40:54 -0800 | [diff] [blame] | 106 | b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 108 | vlib_buffer_advance (b0, sizeof (e0[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 110 | *type = clib_net_to_host_u16 (e0->type); |
| 111 | } |
| 112 | else if (variant == ETHERNET_INPUT_VARIANT_ETHERNET_TYPE) |
| 113 | { |
| 114 | // here when prior node was LLC/SNAP processing |
| 115 | u16 *e0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 117 | e0 = (void *) (b0->data + b0->current_data); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 119 | vlib_buffer_advance (b0, sizeof (e0[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 121 | *type = clib_net_to_host_u16 (e0[0]); |
| 122 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
| 124 | // save for distinguishing between dot1q and dot1ad later |
| 125 | *orig_type = *type; |
| 126 | |
| 127 | // default the tags to 0 (used if there is no corresponding tag) |
| 128 | *outer_id = 0; |
| 129 | *inner_id = 0; |
| 130 | |
| 131 | *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_0_TAG; |
Chris Luke | 194ebc5 | 2016-04-25 14:26:55 -0400 | [diff] [blame] | 132 | vlan_count = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | |
| 134 | // check for vlan encaps |
Damjan Marion | b94bdad | 2016-09-19 11:32:03 +0200 | [diff] [blame] | 135 | if (ethernet_frame_is_tagged (*type)) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 136 | { |
| 137 | ethernet_vlan_header_t *h0; |
| 138 | u16 tag; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 140 | *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_1_TAG; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
| 142 | h0 = (void *) (b0->data + b0->current_data); |
| 143 | |
| 144 | tag = clib_net_to_host_u16 (h0->priority_cfi_and_id); |
| 145 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 146 | *outer_id = tag & 0xfff; |
Neale Ranns | 30d0fd4 | 2017-05-30 07:30:04 -0700 | [diff] [blame] | 147 | if (0 == *outer_id) |
| 148 | *match_flags &= ~SUBINT_CONFIG_MATCH_1_TAG; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 150 | *type = clib_net_to_host_u16 (h0->type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | |
| 152 | vlib_buffer_advance (b0, sizeof (h0[0])); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 153 | vlan_count = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 155 | if (*type == ETHERNET_TYPE_VLAN) |
| 156 | { |
| 157 | // Double tagged packet |
| 158 | *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_2_TAG; |
| 159 | |
| 160 | h0 = (void *) (b0->data + b0->current_data); |
| 161 | |
| 162 | tag = clib_net_to_host_u16 (h0->priority_cfi_and_id); |
| 163 | |
| 164 | *inner_id = tag & 0xfff; |
| 165 | |
| 166 | *type = clib_net_to_host_u16 (h0->type); |
| 167 | |
| 168 | vlib_buffer_advance (b0, sizeof (h0[0])); |
| 169 | vlan_count = 2; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 170 | if (*type == ETHERNET_TYPE_VLAN) |
| 171 | { |
| 172 | // More than double tagged packet |
| 173 | *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_3_TAG; |
Eyal Bari | 6f7ebf9 | 2017-06-13 12:09:37 +0300 | [diff] [blame] | 174 | |
| 175 | vlib_buffer_advance (b0, sizeof (h0[0])); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 176 | vlan_count = 3; // "unknown" number, aka, 3-or-more |
| 177 | } |
| 178 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | } |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 180 | ethernet_buffer_set_vlan_count (b0, vlan_count); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Determine the subinterface for this packet, given the result of the |
| 184 | // vlan table lookups and vlan header parsing. Check the most specific |
| 185 | // matches first. |
| 186 | static_always_inline void |
| 187 | identify_subint (vnet_hw_interface_t * hi, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 188 | vlib_buffer_t * b0, |
| 189 | u32 match_flags, |
| 190 | main_intf_t * main_intf, |
| 191 | vlan_intf_t * vlan_intf, |
| 192 | qinq_intf_t * qinq_intf, |
| 193 | u32 * new_sw_if_index, u8 * error0, u32 * is_l2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | { |
| 195 | u32 matched; |
| 196 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 197 | matched = eth_identify_subint (hi, b0, match_flags, |
| 198 | main_intf, vlan_intf, qinq_intf, |
| 199 | new_sw_if_index, error0, is_l2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 201 | if (matched) |
| 202 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 204 | // Perform L3 my-mac filter |
| 205 | // A unicast packet arriving on an L3 interface must have a dmac matching the interface mac. |
| 206 | // This is required for promiscuous mode, else we will forward packets we aren't supposed to. |
| 207 | if (!(*is_l2)) |
| 208 | { |
| 209 | ethernet_header_t *e0; |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 210 | e0 = (void *) (b0->data + vnet_buffer (b0)->l2_hdr_offset); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 212 | if (!(ethernet_address_cast (e0->dst_address))) |
| 213 | { |
| 214 | if (!eth_mac_equal ((u8 *) e0, hi->hw_address)) |
| 215 | { |
| 216 | *error0 = ETHERNET_ERROR_L3_MAC_MISMATCH; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // Check for down subinterface |
| 222 | *error0 = (*new_sw_if_index) != ~0 ? (*error0) : ETHERNET_ERROR_DOWN; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static_always_inline void |
| 227 | determine_next_node (ethernet_main_t * em, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 228 | ethernet_input_variant_t variant, |
| 229 | u32 is_l20, |
| 230 | u32 type0, vlib_buffer_t * b0, u8 * error0, u8 * next0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | { |
Damjan Marion | faf9a5a | 2018-01-15 17:57:00 +0100 | [diff] [blame] | 232 | u32 eth_start = vnet_buffer (b0)->l2_hdr_offset; |
| 233 | vnet_buffer (b0)->l2.l2_len = b0->current_data - eth_start; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 234 | if (PREDICT_FALSE (*error0 != ETHERNET_ERROR_NONE)) |
| 235 | { |
| 236 | // some error occurred |
| 237 | *next0 = ETHERNET_INPUT_NEXT_DROP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | } |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 239 | else if (is_l20) |
| 240 | { |
| 241 | *next0 = em->l2_next; |
| 242 | // record the L2 len and reset the buffer so the L2 header is preserved |
Eyal Bari | 6f7ebf9 | 2017-06-13 12:09:37 +0300 | [diff] [blame] | 243 | ASSERT (vnet_buffer (b0)->l2.l2_len == |
| 244 | ethernet_buffer_header_size (b0)); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 245 | vlib_buffer_advance (b0, -ethernet_buffer_header_size (b0)); |
| 246 | |
| 247 | // check for common IP/MPLS ethertypes |
| 248 | } |
| 249 | else if (type0 == ETHERNET_TYPE_IP4) |
| 250 | { |
| 251 | *next0 = em->l3_next.input_next_ip4; |
| 252 | } |
| 253 | else if (type0 == ETHERNET_TYPE_IP6) |
| 254 | { |
| 255 | *next0 = em->l3_next.input_next_ip6; |
| 256 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 257 | else if (type0 == ETHERNET_TYPE_MPLS) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 258 | { |
| 259 | *next0 = em->l3_next.input_next_mpls; |
| 260 | |
| 261 | } |
| 262 | else if (em->redirect_l3) |
| 263 | { |
| 264 | // L3 Redirect is on, the cached common next nodes will be |
| 265 | // pointing to the redirect node, catch the uncommon types here |
| 266 | *next0 = em->redirect_l3_next; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | // uncommon ethertype, check table |
| 271 | u32 i0; |
| 272 | i0 = sparse_vec_index (em->l3_next.input_next_by_type, type0); |
| 273 | *next0 = vec_elt (em->l3_next.input_next_by_type, i0); |
| 274 | *error0 = |
| 275 | i0 == |
| 276 | SPARSE_VEC_INVALID_INDEX ? ETHERNET_ERROR_UNKNOWN_TYPE : *error0; |
| 277 | |
| 278 | // The table is not populated with LLC values, so check that now. |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 279 | // If variant is variant_ethernet then we came from LLC processing. Don't |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 280 | // go back there; drop instead using by keeping the drop/bad table result. |
| 281 | if ((type0 < 0x600) && (variant == ETHERNET_INPUT_VARIANT_ETHERNET)) |
| 282 | { |
| 283 | *next0 = ETHERNET_INPUT_NEXT_LLC; |
| 284 | } |
| 285 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Eyal Bari | 9a1ae1a | 2017-06-13 08:42:35 +0300 | [diff] [blame] | 288 | static_always_inline int |
| 289 | ethernet_frame_is_any_tagged (u16 type0, u16 type1) |
| 290 | { |
| 291 | #if __SSE4_2__ |
Damjan Marion | 547ecf6 | 2018-02-06 19:01:28 +0100 | [diff] [blame] | 292 | const __m128i ethertype_mask = _mm_set_epi16 ((u16) ETHERNET_TYPE_VLAN, |
| 293 | (u16) ETHERNET_TYPE_DOT1AD, |
| 294 | (u16) ETHERNET_TYPE_VLAN_9100, |
| 295 | (u16) ETHERNET_TYPE_VLAN_9200, |
Eyal Bari | 9a1ae1a | 2017-06-13 08:42:35 +0300 | [diff] [blame] | 296 | /* duplicate for type1 */ |
Damjan Marion | 547ecf6 | 2018-02-06 19:01:28 +0100 | [diff] [blame] | 297 | (u16) ETHERNET_TYPE_VLAN, |
| 298 | (u16) ETHERNET_TYPE_DOT1AD, |
| 299 | (u16) ETHERNET_TYPE_VLAN_9100, |
| 300 | (u16) |
Eyal Bari | 9a1ae1a | 2017-06-13 08:42:35 +0300 | [diff] [blame] | 301 | ETHERNET_TYPE_VLAN_9200); |
| 302 | |
| 303 | __m128i r = |
| 304 | _mm_set_epi16 (type0, type0, type0, type0, type1, type1, type1, type1); |
| 305 | r = _mm_cmpeq_epi16 (ethertype_mask, r); |
| 306 | return !_mm_test_all_zeros (r, r); |
| 307 | #else |
Christophe Fontaine | 38cfe98 | 2017-07-10 15:21:10 +0200 | [diff] [blame] | 308 | return ethernet_frame_is_tagged (type0) || ethernet_frame_is_tagged (type1); |
Eyal Bari | 9a1ae1a | 2017-06-13 08:42:35 +0300 | [diff] [blame] | 309 | #endif |
| 310 | } |
| 311 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | static_always_inline uword |
| 313 | ethernet_input_inline (vlib_main_t * vm, |
| 314 | vlib_node_runtime_t * node, |
| 315 | vlib_frame_t * from_frame, |
| 316 | ethernet_input_variant_t variant) |
| 317 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 318 | vnet_main_t *vnm = vnet_get_main (); |
| 319 | ethernet_main_t *em = ðernet_main; |
| 320 | vlib_node_runtime_t *error_node; |
| 321 | u32 n_left_from, next_index, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | u32 stats_sw_if_index, stats_n_packets, stats_n_bytes; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 323 | u32 thread_index = vlib_get_thread_index (); |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 324 | u32 cached_sw_if_index = ~0; |
| 325 | u32 cached_is_l2 = 0; /* shut up gcc */ |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 326 | vnet_hw_interface_t *hi = NULL; /* used for main interface only */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 327 | |
| 328 | if (variant != ETHERNET_INPUT_VARIANT_ETHERNET) |
| 329 | error_node = vlib_node_get_runtime (vm, ethernet_input_node.index); |
| 330 | else |
| 331 | error_node = node; |
| 332 | |
| 333 | from = vlib_frame_vector_args (from_frame); |
| 334 | n_left_from = from_frame->n_vectors; |
| 335 | |
| 336 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 337 | vlib_trace_frame_buffers_only (vm, node, |
| 338 | from, |
| 339 | n_left_from, |
| 340 | sizeof (from[0]), |
| 341 | sizeof (ethernet_input_trace_t)); |
| 342 | |
| 343 | next_index = node->cached_next_index; |
| 344 | stats_sw_if_index = node->runtime_data[0]; |
| 345 | stats_n_packets = stats_n_bytes = 0; |
| 346 | |
| 347 | while (n_left_from > 0) |
| 348 | { |
| 349 | u32 n_left_to_next; |
| 350 | |
| 351 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 352 | |
| 353 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 354 | { |
| 355 | u32 bi0, bi1; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 356 | vlib_buffer_t *b0, *b1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | u8 next0, next1, error0, error1; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 358 | u16 type0, orig_type0, type1, orig_type1; |
| 359 | u16 outer_id0, inner_id0, outer_id1, inner_id1; |
| 360 | u32 match_flags0, match_flags1; |
| 361 | u32 old_sw_if_index0, new_sw_if_index0, len0, old_sw_if_index1, |
| 362 | new_sw_if_index1, len1; |
| 363 | vnet_hw_interface_t *hi0, *hi1; |
| 364 | main_intf_t *main_intf0, *main_intf1; |
| 365 | vlan_intf_t *vlan_intf0, *vlan_intf1; |
| 366 | qinq_intf_t *qinq_intf0, *qinq_intf1; |
| 367 | u32 is_l20, is_l21; |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 368 | ethernet_header_t *e0, *e1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | |
| 370 | /* Prefetch next iteration. */ |
| 371 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 372 | vlib_buffer_t *b2, *b3; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | |
| 374 | b2 = vlib_get_buffer (vm, from[2]); |
| 375 | b3 = vlib_get_buffer (vm, from[3]); |
| 376 | |
| 377 | vlib_prefetch_buffer_header (b2, STORE); |
| 378 | vlib_prefetch_buffer_header (b3, STORE); |
| 379 | |
| 380 | CLIB_PREFETCH (b2->data, sizeof (ethernet_header_t), LOAD); |
| 381 | CLIB_PREFETCH (b3->data, sizeof (ethernet_header_t), LOAD); |
| 382 | } |
| 383 | |
| 384 | bi0 = from[0]; |
| 385 | bi1 = from[1]; |
| 386 | to_next[0] = bi0; |
| 387 | to_next[1] = bi1; |
| 388 | from += 2; |
| 389 | to_next += 2; |
| 390 | n_left_to_next -= 2; |
| 391 | n_left_from -= 2; |
| 392 | |
| 393 | b0 = vlib_get_buffer (vm, bi0); |
| 394 | b1 = vlib_get_buffer (vm, bi1); |
| 395 | |
| 396 | error0 = error1 = ETHERNET_ERROR_NONE; |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 397 | e0 = vlib_buffer_get_current (b0); |
| 398 | type0 = clib_net_to_host_u16 (e0->type); |
| 399 | e1 = vlib_buffer_get_current (b1); |
| 400 | type1 = clib_net_to_host_u16 (e1->type); |
| 401 | |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 402 | /* Speed-path for the untagged case */ |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 403 | if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET |
Eyal Bari | 9a1ae1a | 2017-06-13 08:42:35 +0300 | [diff] [blame] | 404 | && !ethernet_frame_is_any_tagged (type0, type1))) |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 405 | { |
| 406 | main_intf_t *intf0; |
| 407 | subint_config_t *subint0; |
| 408 | u32 sw_if_index0, sw_if_index1; |
| 409 | |
| 410 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 411 | sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
| 412 | is_l20 = cached_is_l2; |
| 413 | |
| 414 | /* This is probably wholly unnecessary */ |
| 415 | if (PREDICT_FALSE (sw_if_index0 != sw_if_index1)) |
| 416 | goto slowpath; |
| 417 | |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 418 | /* Now sw_if_index0 == sw_if_index1 */ |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 419 | if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0)) |
| 420 | { |
| 421 | cached_sw_if_index = sw_if_index0; |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 422 | hi = vnet_get_sup_hw_interface (vnm, sw_if_index0); |
| 423 | intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index); |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 424 | subint0 = &intf0->untagged_subint; |
| 425 | cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2; |
| 426 | } |
John Lo | 7714b30 | 2016-12-20 16:59:02 -0500 | [diff] [blame] | 427 | |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 428 | vnet_buffer (b0)->l2_hdr_offset = b0->current_data; |
| 429 | vnet_buffer (b1)->l2_hdr_offset = b1->current_data; |
Steven | 35de3b3 | 2017-12-03 23:40:54 -0800 | [diff] [blame] | 430 | vnet_buffer (b0)->l3_hdr_offset = |
| 431 | vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t); |
| 432 | vnet_buffer (b1)->l3_hdr_offset = |
| 433 | vnet_buffer (b1)->l2_hdr_offset + sizeof (ethernet_header_t); |
| 434 | b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID | |
| 435 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
| 436 | b1->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID | |
| 437 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
John Lo | 7714b30 | 2016-12-20 16:59:02 -0500 | [diff] [blame] | 438 | |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 439 | if (PREDICT_TRUE (is_l20 != 0)) |
| 440 | { |
| 441 | next0 = em->l2_next; |
| 442 | vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t); |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 443 | next1 = em->l2_next; |
| 444 | vnet_buffer (b1)->l2.l2_len = sizeof (ethernet_header_t); |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 445 | } |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 446 | else |
| 447 | { |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 448 | if (!ethernet_address_cast (e0->dst_address) && |
Hongjun Ni | 9e3e361 | 2017-04-26 18:40:55 +0800 | [diff] [blame] | 449 | (hi->hw_address != 0) && |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 450 | !eth_mac_equal ((u8 *) e0, hi->hw_address)) |
| 451 | error0 = ETHERNET_ERROR_L3_MAC_MISMATCH; |
| 452 | if (!ethernet_address_cast (e1->dst_address) && |
Hongjun Ni | 9e3e361 | 2017-04-26 18:40:55 +0800 | [diff] [blame] | 453 | (hi->hw_address != 0) && |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 454 | !eth_mac_equal ((u8 *) e1, hi->hw_address)) |
| 455 | error1 = ETHERNET_ERROR_L3_MAC_MISMATCH; |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 456 | determine_next_node (em, variant, 0, type0, b0, |
| 457 | &error0, &next0); |
| 458 | vlib_buffer_advance (b0, sizeof (ethernet_header_t)); |
| 459 | determine_next_node (em, variant, 0, type1, b1, |
| 460 | &error1, &next1); |
| 461 | vlib_buffer_advance (b1, sizeof (ethernet_header_t)); |
| 462 | } |
| 463 | goto ship_it01; |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 464 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 466 | /* Slow-path for the tagged case */ |
| 467 | slowpath: |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 468 | parse_header (variant, |
| 469 | b0, |
| 470 | &type0, |
| 471 | &orig_type0, &outer_id0, &inner_id0, &match_flags0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 473 | parse_header (variant, |
| 474 | b1, |
| 475 | &type1, |
| 476 | &orig_type1, &outer_id1, &inner_id1, &match_flags1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 478 | old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 479 | old_sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 481 | eth_vlan_table_lookups (em, |
| 482 | vnm, |
| 483 | old_sw_if_index0, |
| 484 | orig_type0, |
| 485 | outer_id0, |
| 486 | inner_id0, |
| 487 | &hi0, |
| 488 | &main_intf0, &vlan_intf0, &qinq_intf0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 490 | eth_vlan_table_lookups (em, |
| 491 | vnm, |
| 492 | old_sw_if_index1, |
| 493 | orig_type1, |
| 494 | outer_id1, |
| 495 | inner_id1, |
| 496 | &hi1, |
| 497 | &main_intf1, &vlan_intf1, &qinq_intf1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 498 | |
| 499 | identify_subint (hi0, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 500 | b0, |
| 501 | match_flags0, |
| 502 | main_intf0, |
| 503 | vlan_intf0, |
| 504 | qinq_intf0, &new_sw_if_index0, &error0, &is_l20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | |
| 506 | identify_subint (hi1, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 507 | b1, |
| 508 | match_flags1, |
| 509 | main_intf1, |
| 510 | vlan_intf1, |
| 511 | qinq_intf1, &new_sw_if_index1, &error1, &is_l21); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 513 | // Save RX sw_if_index for later nodes |
| 514 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = |
| 515 | error0 != |
| 516 | ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0; |
| 517 | vnet_buffer (b1)->sw_if_index[VLIB_RX] = |
| 518 | error1 != |
| 519 | ETHERNET_ERROR_NONE ? old_sw_if_index1 : new_sw_if_index1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 520 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 521 | // Check if there is a stat to take (valid and non-main sw_if_index for pkt 0 or pkt 1) |
| 522 | if (((new_sw_if_index0 != ~0) |
| 523 | && (new_sw_if_index0 != old_sw_if_index0)) |
| 524 | || ((new_sw_if_index1 != ~0) |
| 525 | && (new_sw_if_index1 != old_sw_if_index1))) |
| 526 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 527 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 528 | len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 529 | - vnet_buffer (b0)->l2_hdr_offset; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 530 | len1 = vlib_buffer_length_in_chain (vm, b1) + b1->current_data |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 531 | - vnet_buffer (b1)->l2_hdr_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 533 | stats_n_packets += 2; |
| 534 | stats_n_bytes += len0 + len1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 536 | if (PREDICT_FALSE |
| 537 | (!(new_sw_if_index0 == stats_sw_if_index |
| 538 | && new_sw_if_index1 == stats_sw_if_index))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 539 | { |
| 540 | stats_n_packets -= 2; |
| 541 | stats_n_bytes -= len0 + len1; |
| 542 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 543 | if (new_sw_if_index0 != old_sw_if_index0 |
| 544 | && new_sw_if_index0 != ~0) |
| 545 | vlib_increment_combined_counter (vnm-> |
| 546 | interface_main.combined_sw_if_counters |
| 547 | + |
| 548 | VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 549 | thread_index, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 550 | new_sw_if_index0, 1, |
| 551 | len0); |
| 552 | if (new_sw_if_index1 != old_sw_if_index1 |
| 553 | && new_sw_if_index1 != ~0) |
| 554 | vlib_increment_combined_counter (vnm-> |
| 555 | interface_main.combined_sw_if_counters |
| 556 | + |
| 557 | VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 558 | thread_index, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 559 | new_sw_if_index1, 1, |
| 560 | len1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | |
| 562 | if (new_sw_if_index0 == new_sw_if_index1) |
| 563 | { |
| 564 | if (stats_n_packets > 0) |
| 565 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 566 | vlib_increment_combined_counter |
| 567 | (vnm->interface_main.combined_sw_if_counters |
| 568 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 569 | thread_index, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 570 | stats_sw_if_index, |
| 571 | stats_n_packets, stats_n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | stats_n_packets = stats_n_bytes = 0; |
| 573 | } |
| 574 | stats_sw_if_index = new_sw_if_index0; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 579 | if (variant == ETHERNET_INPUT_VARIANT_NOT_L2) |
| 580 | is_l20 = is_l21 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 581 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 582 | determine_next_node (em, variant, is_l20, type0, b0, &error0, |
| 583 | &next0); |
| 584 | determine_next_node (em, variant, is_l21, type1, b1, &error1, |
| 585 | &next1); |
Steven | 35de3b3 | 2017-12-03 23:40:54 -0800 | [diff] [blame] | 586 | vnet_buffer (b0)->l3_hdr_offset = vnet_buffer (b0)->l2_hdr_offset + |
| 587 | vnet_buffer (b0)->l2.l2_len; |
| 588 | vnet_buffer (b1)->l3_hdr_offset = vnet_buffer (b1)->l2_hdr_offset + |
| 589 | vnet_buffer (b1)->l2.l2_len; |
| 590 | b0->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
| 591 | b1->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 592 | |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 593 | ship_it01: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 594 | b0->error = error_node->errors[error0]; |
| 595 | b1->error = error_node->errors[error1]; |
| 596 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 597 | // verify speculative enqueue |
| 598 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next, |
| 599 | n_left_to_next, bi0, bi1, next0, |
| 600 | next1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | while (n_left_from > 0 && n_left_to_next > 0) |
| 604 | { |
| 605 | u32 bi0; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 606 | vlib_buffer_t *b0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 607 | u8 error0, next0; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 608 | u16 type0, orig_type0; |
| 609 | u16 outer_id0, inner_id0; |
| 610 | u32 match_flags0; |
| 611 | u32 old_sw_if_index0, new_sw_if_index0, len0; |
| 612 | vnet_hw_interface_t *hi0; |
| 613 | main_intf_t *main_intf0; |
| 614 | vlan_intf_t *vlan_intf0; |
| 615 | qinq_intf_t *qinq_intf0; |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 616 | ethernet_header_t *e0; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 617 | u32 is_l20; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 619 | // Prefetch next iteration |
| 620 | if (n_left_from > 1) |
| 621 | { |
| 622 | vlib_buffer_t *p2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 623 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 624 | p2 = vlib_get_buffer (vm, from[1]); |
| 625 | vlib_prefetch_buffer_header (p2, STORE); |
| 626 | CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD); |
| 627 | } |
| 628 | |
| 629 | bi0 = from[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | to_next[0] = bi0; |
| 631 | from += 1; |
| 632 | to_next += 1; |
| 633 | n_left_from -= 1; |
| 634 | n_left_to_next -= 1; |
| 635 | |
| 636 | b0 = vlib_get_buffer (vm, bi0); |
| 637 | |
| 638 | error0 = ETHERNET_ERROR_NONE; |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 639 | e0 = vlib_buffer_get_current (b0); |
| 640 | type0 = clib_net_to_host_u16 (e0->type); |
| 641 | |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 642 | /* Speed-path for the untagged case */ |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 643 | if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET |
| 644 | && !ethernet_frame_is_tagged (type0))) |
| 645 | { |
| 646 | main_intf_t *intf0; |
| 647 | subint_config_t *subint0; |
| 648 | u32 sw_if_index0; |
| 649 | |
| 650 | sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 651 | is_l20 = cached_is_l2; |
| 652 | |
| 653 | if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0)) |
| 654 | { |
| 655 | cached_sw_if_index = sw_if_index0; |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 656 | hi = vnet_get_sup_hw_interface (vnm, sw_if_index0); |
| 657 | intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index); |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 658 | subint0 = &intf0->untagged_subint; |
| 659 | cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2; |
| 660 | } |
John Lo | 7714b30 | 2016-12-20 16:59:02 -0500 | [diff] [blame] | 661 | |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 662 | vnet_buffer (b0)->l2_hdr_offset = b0->current_data; |
Steven | 35de3b3 | 2017-12-03 23:40:54 -0800 | [diff] [blame] | 663 | vnet_buffer (b0)->l3_hdr_offset = |
| 664 | vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t); |
| 665 | b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID | |
| 666 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
John Lo | 7714b30 | 2016-12-20 16:59:02 -0500 | [diff] [blame] | 667 | |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 668 | if (PREDICT_TRUE (is_l20 != 0)) |
| 669 | { |
| 670 | next0 = em->l2_next; |
| 671 | vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t); |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 672 | } |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 673 | else |
| 674 | { |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 675 | if (!ethernet_address_cast (e0->dst_address) && |
Hongjun Ni | 9e3e361 | 2017-04-26 18:40:55 +0800 | [diff] [blame] | 676 | (hi->hw_address != 0) && |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 677 | !eth_mac_equal ((u8 *) e0, hi->hw_address)) |
| 678 | error0 = ETHERNET_ERROR_L3_MAC_MISMATCH; |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 679 | determine_next_node (em, variant, 0, type0, b0, |
| 680 | &error0, &next0); |
| 681 | vlib_buffer_advance (b0, sizeof (ethernet_header_t)); |
| 682 | } |
| 683 | goto ship_it0; |
Dave Barach | cfba1e2 | 2016-11-16 10:23:50 -0500 | [diff] [blame] | 684 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 685 | |
John Lo | cc53285 | 2016-12-14 15:42:45 -0500 | [diff] [blame] | 686 | /* Slow-path for the tagged case */ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 687 | parse_header (variant, |
| 688 | b0, |
| 689 | &type0, |
| 690 | &orig_type0, &outer_id0, &inner_id0, &match_flags0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 691 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 692 | old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 693 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 694 | eth_vlan_table_lookups (em, |
| 695 | vnm, |
| 696 | old_sw_if_index0, |
| 697 | orig_type0, |
| 698 | outer_id0, |
| 699 | inner_id0, |
| 700 | &hi0, |
| 701 | &main_intf0, &vlan_intf0, &qinq_intf0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 702 | |
| 703 | identify_subint (hi0, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 704 | b0, |
| 705 | match_flags0, |
| 706 | main_intf0, |
| 707 | vlan_intf0, |
| 708 | qinq_intf0, &new_sw_if_index0, &error0, &is_l20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 709 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 710 | // Save RX sw_if_index for later nodes |
| 711 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = |
| 712 | error0 != |
| 713 | ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 715 | // Increment subinterface stats |
| 716 | // Note that interface-level counters have already been incremented |
| 717 | // prior to calling this function. Thus only subinterface counters |
| 718 | // are incremented here. |
| 719 | // |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 720 | // Interface level counters include packets received on the main |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 721 | // interface and all subinterfaces. Subinterface level counters |
| 722 | // include only those packets received on that subinterface |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 723 | // Increment stats if the subint is valid and it is not the main intf |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 724 | if ((new_sw_if_index0 != ~0) |
| 725 | && (new_sw_if_index0 != old_sw_if_index0)) |
| 726 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 728 | len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 729 | - vnet_buffer (b0)->l2_hdr_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 730 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 731 | stats_n_packets += 1; |
| 732 | stats_n_bytes += len0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 733 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 734 | // Batch stat increments from the same subinterface so counters |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 735 | // don't need to be incremented for every packet. |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 736 | if (PREDICT_FALSE (new_sw_if_index0 != stats_sw_if_index)) |
| 737 | { |
| 738 | stats_n_packets -= 1; |
| 739 | stats_n_bytes -= len0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 741 | if (new_sw_if_index0 != ~0) |
| 742 | vlib_increment_combined_counter |
| 743 | (vnm->interface_main.combined_sw_if_counters |
| 744 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 745 | thread_index, new_sw_if_index0, 1, len0); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 746 | if (stats_n_packets > 0) |
| 747 | { |
| 748 | vlib_increment_combined_counter |
| 749 | (vnm->interface_main.combined_sw_if_counters |
| 750 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 751 | thread_index, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 752 | stats_sw_if_index, stats_n_packets, stats_n_bytes); |
| 753 | stats_n_packets = stats_n_bytes = 0; |
| 754 | } |
| 755 | stats_sw_if_index = new_sw_if_index0; |
| 756 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 759 | if (variant == ETHERNET_INPUT_VARIANT_NOT_L2) |
| 760 | is_l20 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 762 | determine_next_node (em, variant, is_l20, type0, b0, &error0, |
| 763 | &next0); |
Steven | 35de3b3 | 2017-12-03 23:40:54 -0800 | [diff] [blame] | 764 | vnet_buffer (b0)->l3_hdr_offset = vnet_buffer (b0)->l2_hdr_offset + |
| 765 | vnet_buffer (b0)->l2.l2_len; |
| 766 | b0->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | |
John Lo | 1904c47 | 2017-03-10 17:15:22 -0500 | [diff] [blame] | 768 | ship_it0: |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 769 | b0->error = error_node->errors[error0]; |
| 770 | |
| 771 | // verify speculative enqueue |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 772 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 773 | to_next, n_left_to_next, |
| 774 | bi0, next0); |
| 775 | } |
| 776 | |
| 777 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 778 | } |
| 779 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 780 | // Increment any remaining batched stats |
| 781 | if (stats_n_packets > 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 782 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 783 | vlib_increment_combined_counter |
| 784 | (vnm->interface_main.combined_sw_if_counters |
| 785 | + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 786 | thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 787 | node->runtime_data[0] = stats_sw_if_index; |
| 788 | } |
| 789 | |
| 790 | return from_frame->n_vectors; |
| 791 | } |
| 792 | |
| 793 | static uword |
| 794 | ethernet_input (vlib_main_t * vm, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 795 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 796 | { |
| 797 | return ethernet_input_inline (vm, node, from_frame, |
| 798 | ETHERNET_INPUT_VARIANT_ETHERNET); |
| 799 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 800 | |
| 801 | static uword |
| 802 | ethernet_input_type (vlib_main_t * vm, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 803 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 804 | { |
| 805 | return ethernet_input_inline (vm, node, from_frame, |
| 806 | ETHERNET_INPUT_VARIANT_ETHERNET_TYPE); |
| 807 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 808 | |
| 809 | static uword |
| 810 | ethernet_input_not_l2 (vlib_main_t * vm, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 811 | vlib_node_runtime_t * node, vlib_frame_t * from_frame) |
| 812 | { |
| 813 | return ethernet_input_inline (vm, node, from_frame, |
| 814 | ETHERNET_INPUT_VARIANT_NOT_L2); |
| 815 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 816 | |
| 817 | |
| 818 | // Return the subinterface config struct for the given sw_if_index |
| 819 | // Also return via parameter the appropriate match flags for the |
| 820 | // configured number of tags. |
| 821 | // On error (unsupported or not ethernet) return 0. |
| 822 | static subint_config_t * |
| 823 | ethernet_sw_interface_get_config (vnet_main_t * vnm, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 824 | u32 sw_if_index, |
| 825 | u32 * flags, u32 * unsupported) |
| 826 | { |
| 827 | ethernet_main_t *em = ðernet_main; |
| 828 | vnet_hw_interface_t *hi; |
| 829 | vnet_sw_interface_t *si; |
| 830 | main_intf_t *main_intf; |
| 831 | vlan_table_t *vlan_table; |
| 832 | qinq_table_t *qinq_table; |
| 833 | subint_config_t *subint = 0; |
| 834 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 835 | hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 836 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 837 | if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index)) |
| 838 | { |
| 839 | *unsupported = 0; |
| 840 | goto done; // non-ethernet interface |
| 841 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 842 | |
| 843 | // ensure there's an entry for the main intf (shouldn't really be necessary) |
| 844 | vec_validate (em->main_intfs, hi->hw_if_index); |
| 845 | main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index); |
| 846 | |
| 847 | // Locate the subint for the given ethernet config |
| 848 | si = vnet_get_sw_interface (vnm, sw_if_index); |
| 849 | |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 850 | if (si->type == VNET_SW_INTERFACE_TYPE_P2P) |
| 851 | { |
| 852 | p2p_ethernet_main_t *p2pm = &p2p_main; |
| 853 | u32 p2pe_sw_if_index = |
| 854 | p2p_ethernet_lookup (hi->hw_if_index, si->p2p.client_mac); |
| 855 | if (p2pe_sw_if_index == ~0) |
| 856 | { |
| 857 | pool_get (p2pm->p2p_subif_pool, subint); |
| 858 | si->p2p.pool_index = subint - p2pm->p2p_subif_pool; |
| 859 | } |
| 860 | else |
| 861 | subint = vec_elt_at_index (p2pm->p2p_subif_pool, si->p2p.pool_index); |
| 862 | *flags = SUBINT_CONFIG_P2P; |
| 863 | } |
| 864 | else if (si->sub.eth.flags.default_sub) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 865 | { |
| 866 | subint = &main_intf->default_subint; |
| 867 | *flags = SUBINT_CONFIG_MATCH_0_TAG | |
| 868 | SUBINT_CONFIG_MATCH_1_TAG | |
| 869 | SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG; |
| 870 | } |
| 871 | else if ((si->sub.eth.flags.no_tags) || (si->sub.eth.raw_flags == 0)) |
| 872 | { |
| 873 | // if no flags are set then this is a main interface |
| 874 | // so treat as untagged |
| 875 | subint = &main_intf->untagged_subint; |
| 876 | *flags = SUBINT_CONFIG_MATCH_0_TAG; |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | // one or two tags |
| 881 | // first get the vlan table |
| 882 | if (si->sub.eth.flags.dot1ad) |
| 883 | { |
| 884 | if (main_intf->dot1ad_vlans == 0) |
| 885 | { |
| 886 | // Allocate a vlan table from the pool |
| 887 | pool_get (em->vlan_pool, vlan_table); |
| 888 | main_intf->dot1ad_vlans = vlan_table - em->vlan_pool; |
| 889 | } |
| 890 | else |
| 891 | { |
| 892 | // Get ptr to existing vlan table |
| 893 | vlan_table = |
| 894 | vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans); |
| 895 | } |
| 896 | } |
| 897 | else |
| 898 | { // dot1q |
| 899 | if (main_intf->dot1q_vlans == 0) |
| 900 | { |
| 901 | // Allocate a vlan table from the pool |
| 902 | pool_get (em->vlan_pool, vlan_table); |
| 903 | main_intf->dot1q_vlans = vlan_table - em->vlan_pool; |
| 904 | } |
| 905 | else |
| 906 | { |
| 907 | // Get ptr to existing vlan table |
| 908 | vlan_table = |
| 909 | vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (si->sub.eth.flags.one_tag) |
| 914 | { |
| 915 | *flags = si->sub.eth.flags.exact_match ? |
| 916 | SUBINT_CONFIG_MATCH_1_TAG : |
| 917 | (SUBINT_CONFIG_MATCH_1_TAG | |
| 918 | SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG); |
| 919 | |
| 920 | if (si->sub.eth.flags.outer_vlan_id_any) |
| 921 | { |
| 922 | // not implemented yet |
| 923 | *unsupported = 1; |
| 924 | goto done; |
| 925 | } |
| 926 | else |
| 927 | { |
| 928 | // a single vlan, a common case |
| 929 | subint = |
| 930 | &vlan_table->vlans[si->sub.eth. |
| 931 | outer_vlan_id].single_tag_subint; |
| 932 | } |
| 933 | |
| 934 | } |
| 935 | else |
| 936 | { |
| 937 | // Two tags |
| 938 | *flags = si->sub.eth.flags.exact_match ? |
| 939 | SUBINT_CONFIG_MATCH_2_TAG : |
| 940 | (SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG); |
| 941 | |
| 942 | if (si->sub.eth.flags.outer_vlan_id_any |
| 943 | && si->sub.eth.flags.inner_vlan_id_any) |
| 944 | { |
| 945 | // not implemented yet |
| 946 | *unsupported = 1; |
| 947 | goto done; |
| 948 | } |
| 949 | |
| 950 | if (si->sub.eth.flags.inner_vlan_id_any) |
| 951 | { |
| 952 | // a specific outer and "any" inner |
| 953 | // don't need a qinq table for this |
| 954 | subint = |
| 955 | &vlan_table->vlans[si->sub.eth. |
| 956 | outer_vlan_id].inner_any_subint; |
| 957 | if (si->sub.eth.flags.exact_match) |
| 958 | { |
| 959 | *flags = SUBINT_CONFIG_MATCH_2_TAG; |
| 960 | } |
| 961 | else |
| 962 | { |
| 963 | *flags = SUBINT_CONFIG_MATCH_2_TAG | |
| 964 | SUBINT_CONFIG_MATCH_3_TAG; |
| 965 | } |
| 966 | } |
| 967 | else |
| 968 | { |
| 969 | // a specific outer + specifc innner vlan id, a common case |
| 970 | |
| 971 | // get the qinq table |
| 972 | if (vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs == 0) |
| 973 | { |
| 974 | // Allocate a qinq table from the pool |
| 975 | pool_get (em->qinq_pool, qinq_table); |
| 976 | vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs = |
| 977 | qinq_table - em->qinq_pool; |
| 978 | } |
| 979 | else |
| 980 | { |
| 981 | // Get ptr to existing qinq table |
| 982 | qinq_table = |
| 983 | vec_elt_at_index (em->qinq_pool, |
| 984 | vlan_table->vlans[si->sub. |
| 985 | eth.outer_vlan_id]. |
| 986 | qinqs); |
| 987 | } |
| 988 | subint = &qinq_table->vlans[si->sub.eth.inner_vlan_id].subint; |
| 989 | } |
| 990 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 993 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 994 | return subint; |
| 995 | } |
| 996 | |
| 997 | clib_error_t * |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 998 | ethernet_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 999 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1000 | subint_config_t *subint; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1001 | u32 dummy_flags; |
| 1002 | u32 dummy_unsup; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1003 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1004 | |
| 1005 | // Find the config for this subinterface |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1006 | subint = |
| 1007 | ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags, |
| 1008 | &dummy_unsup); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1009 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1010 | if (subint == 0) |
| 1011 | { |
| 1012 | // not implemented yet or not ethernet |
| 1013 | goto done; |
| 1014 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1015 | |
| 1016 | subint->sw_if_index = |
| 1017 | ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? sw_if_index : ~0); |
| 1018 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1019 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1020 | return error; |
| 1021 | } |
| 1022 | |
| 1023 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_sw_interface_up_down); |
| 1024 | |
| 1025 | |
| 1026 | // Set the L2/L3 mode for the subinterface |
| 1027 | void |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1028 | ethernet_sw_interface_set_l2_mode (vnet_main_t * vnm, u32 sw_if_index, u32 l2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1029 | { |
| 1030 | subint_config_t *subint; |
| 1031 | u32 dummy_flags; |
| 1032 | u32 dummy_unsup; |
| 1033 | int is_port; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1034 | vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1035 | |
| 1036 | is_port = !(sw->type == VNET_SW_INTERFACE_TYPE_SUB); |
| 1037 | |
| 1038 | // Find the config for this subinterface |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1039 | subint = |
| 1040 | ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags, |
| 1041 | &dummy_unsup); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1042 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1043 | if (subint == 0) |
| 1044 | { |
| 1045 | // unimplemented or not ethernet |
| 1046 | goto done; |
| 1047 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1048 | |
| 1049 | // Double check that the config we found is for our interface (or the interface is down) |
| 1050 | ASSERT ((subint->sw_if_index == sw_if_index) | (subint->sw_if_index == ~0)); |
| 1051 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1052 | if (l2) |
| 1053 | { |
| 1054 | subint->flags |= SUBINT_CONFIG_L2; |
| 1055 | if (is_port) |
| 1056 | subint->flags |= |
| 1057 | SUBINT_CONFIG_MATCH_0_TAG | SUBINT_CONFIG_MATCH_1_TAG |
| 1058 | | SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG; |
| 1059 | } |
| 1060 | else |
| 1061 | { |
| 1062 | subint->flags &= ~SUBINT_CONFIG_L2; |
| 1063 | if (is_port) |
| 1064 | subint->flags &= |
| 1065 | ~(SUBINT_CONFIG_MATCH_1_TAG | SUBINT_CONFIG_MATCH_2_TAG |
| 1066 | | SUBINT_CONFIG_MATCH_3_TAG); |
| 1067 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1068 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1069 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1070 | return; |
| 1071 | } |
| 1072 | |
Christian Dechamplain (cdechamp) | 07aecbb | 2016-04-05 10:40:38 -0400 | [diff] [blame] | 1073 | /* |
| 1074 | * Set the L2/L3 mode for the subinterface regardless of port |
| 1075 | */ |
| 1076 | void |
| 1077 | ethernet_sw_interface_set_l2_mode_noport (vnet_main_t * vnm, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1078 | u32 sw_if_index, u32 l2) |
Christian Dechamplain (cdechamp) | 07aecbb | 2016-04-05 10:40:38 -0400 | [diff] [blame] | 1079 | { |
| 1080 | subint_config_t *subint; |
| 1081 | u32 dummy_flags; |
| 1082 | u32 dummy_unsup; |
| 1083 | |
| 1084 | /* Find the config for this subinterface */ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1085 | subint = |
| 1086 | ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags, |
| 1087 | &dummy_unsup); |
Christian Dechamplain (cdechamp) | 07aecbb | 2016-04-05 10:40:38 -0400 | [diff] [blame] | 1088 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1089 | if (subint == 0) |
| 1090 | { |
| 1091 | /* unimplemented or not ethernet */ |
| 1092 | goto done; |
| 1093 | } |
Christian Dechamplain (cdechamp) | 07aecbb | 2016-04-05 10:40:38 -0400 | [diff] [blame] | 1094 | |
| 1095 | /* |
| 1096 | * Double check that the config we found is for our interface (or the |
| 1097 | * interface is down) |
| 1098 | */ |
| 1099 | ASSERT ((subint->sw_if_index == sw_if_index) | (subint->sw_if_index == ~0)); |
| 1100 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1101 | if (l2) |
| 1102 | { |
| 1103 | subint->flags |= SUBINT_CONFIG_L2; |
| 1104 | } |
| 1105 | else |
| 1106 | { |
| 1107 | subint->flags &= ~SUBINT_CONFIG_L2; |
| 1108 | } |
Christian Dechamplain (cdechamp) | 07aecbb | 2016-04-05 10:40:38 -0400 | [diff] [blame] | 1109 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1110 | done: |
Christian Dechamplain (cdechamp) | 07aecbb | 2016-04-05 10:40:38 -0400 | [diff] [blame] | 1111 | return; |
| 1112 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1113 | |
| 1114 | static clib_error_t * |
| 1115 | ethernet_sw_interface_add_del (vnet_main_t * vnm, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1116 | u32 sw_if_index, u32 is_create) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1117 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1118 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1119 | subint_config_t *subint; |
| 1120 | u32 match_flags; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1121 | u32 unsupported = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1122 | |
| 1123 | // Find the config for this subinterface |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1124 | subint = |
| 1125 | ethernet_sw_interface_get_config (vnm, sw_if_index, &match_flags, |
| 1126 | &unsupported); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1127 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1128 | if (subint == 0) |
| 1129 | { |
| 1130 | // not implemented yet or not ethernet |
| 1131 | if (unsupported) |
| 1132 | { |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 1133 | // this is the NYI case |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1134 | error = clib_error_return (0, "not implemented yet"); |
| 1135 | } |
| 1136 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1137 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1138 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1139 | if (!is_create) |
| 1140 | { |
| 1141 | subint->flags = 0; |
| 1142 | return error; |
| 1143 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1144 | |
| 1145 | // Initialize the subint |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1146 | if (subint->flags & SUBINT_CONFIG_VALID) |
| 1147 | { |
| 1148 | // Error vlan already in use |
| 1149 | error = clib_error_return (0, "vlan is already in use"); |
| 1150 | } |
| 1151 | else |
| 1152 | { |
| 1153 | // Note that config is L3 by defaulty |
| 1154 | subint->flags = SUBINT_CONFIG_VALID | match_flags; |
| 1155 | subint->sw_if_index = ~0; // because interfaces are initially down |
| 1156 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1157 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1158 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1159 | return error; |
| 1160 | } |
| 1161 | |
| 1162 | VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ethernet_sw_interface_add_del); |
| 1163 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1164 | static char *ethernet_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1165 | #define ethernet_error(n,c,s) s, |
| 1166 | #include "error.def" |
| 1167 | #undef ethernet_error |
| 1168 | }; |
| 1169 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1170 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1171 | VLIB_REGISTER_NODE (ethernet_input_node) = { |
| 1172 | .function = ethernet_input, |
| 1173 | .name = "ethernet-input", |
| 1174 | /* Takes a vector of packets. */ |
| 1175 | .vector_size = sizeof (u32), |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1176 | .n_errors = ETHERNET_N_ERROR, |
| 1177 | .error_strings = ethernet_error_strings, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1178 | .n_next_nodes = ETHERNET_INPUT_N_NEXT, |
| 1179 | .next_nodes = { |
| 1180 | #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n, |
| 1181 | foreach_ethernet_input_next |
| 1182 | #undef _ |
| 1183 | }, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1184 | .format_buffer = format_ethernet_header_with_length, |
| 1185 | .format_trace = format_ethernet_input_trace, |
| 1186 | .unformat_buffer = unformat_ethernet_header, |
| 1187 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1188 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1189 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1190 | /* *INDENT-OFF* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 1191 | VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_node, ethernet_input) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1192 | /* *INDENT-ON* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 1193 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1194 | /* *INDENT-OFF* */ |
| 1195 | VLIB_REGISTER_NODE (ethernet_input_type_node, static) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1196 | .function = ethernet_input_type, |
| 1197 | .name = "ethernet-input-type", |
| 1198 | /* Takes a vector of packets. */ |
| 1199 | .vector_size = sizeof (u32), |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1200 | .n_next_nodes = ETHERNET_INPUT_N_NEXT, |
| 1201 | .next_nodes = { |
| 1202 | #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n, |
| 1203 | foreach_ethernet_input_next |
| 1204 | #undef _ |
| 1205 | }, |
| 1206 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1207 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1208 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1209 | /* *INDENT-OFF* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 1210 | VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_type_node, ethernet_input_type) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1211 | /* *INDENT-ON* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 1212 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1213 | /* *INDENT-OFF* */ |
| 1214 | VLIB_REGISTER_NODE (ethernet_input_not_l2_node, static) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1215 | .function = ethernet_input_not_l2, |
| 1216 | .name = "ethernet-input-not-l2", |
| 1217 | /* Takes a vector of packets. */ |
| 1218 | .vector_size = sizeof (u32), |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1219 | .n_next_nodes = ETHERNET_INPUT_N_NEXT, |
| 1220 | .next_nodes = { |
| 1221 | #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n, |
| 1222 | foreach_ethernet_input_next |
| 1223 | #undef _ |
| 1224 | }, |
| 1225 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1226 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1227 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 1228 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1229 | /* *INDENT-OFF* */ |
| 1230 | VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_not_l2_node, |
| 1231 | ethernet_input_not_l2) |
| 1232 | /* *INDENT-ON* */ |
| 1233 | |
| 1234 | |
| 1235 | void |
| 1236 | ethernet_set_rx_redirect (vnet_main_t * vnm, |
| 1237 | vnet_hw_interface_t * hi, u32 enable) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1238 | { |
| 1239 | // Insure all packets go to ethernet-input (i.e. untagged ipv4 packets |
| 1240 | // don't go directly to ip4-input) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1241 | vnet_hw_interface_rx_redirect_to_node |
| 1242 | (vnm, hi->hw_if_index, enable ? ethernet_input_node.index : ~0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | /* |
| 1247 | * Initialization and registration for the next_by_ethernet structure |
| 1248 | */ |
| 1249 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1250 | clib_error_t * |
| 1251 | next_by_ethertype_init (next_by_ethertype_t * l3_next) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1252 | { |
| 1253 | l3_next->input_next_by_type = sparse_vec_new |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1254 | ( /* elt bytes */ sizeof (l3_next->input_next_by_type[0]), |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1255 | /* bits in index */ BITS (((ethernet_header_t *) 0)->type)); |
| 1256 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1257 | vec_validate (l3_next->sparse_index_by_input_next_index, |
| 1258 | ETHERNET_INPUT_NEXT_DROP); |
| 1259 | vec_validate (l3_next->sparse_index_by_input_next_index, |
| 1260 | ETHERNET_INPUT_NEXT_PUNT); |
| 1261 | l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_DROP] = |
| 1262 | SPARSE_VEC_INVALID_INDEX; |
| 1263 | l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_PUNT] = |
| 1264 | SPARSE_VEC_INVALID_INDEX; |
| 1265 | |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 1266 | /* |
| 1267 | * Make sure we don't wipe out an ethernet registration by mistake |
Dave Barach | 1f49ed6 | 2016-02-24 11:29:06 -0500 | [diff] [blame] | 1268 | * Can happen if init function ordering constraints are missing. |
| 1269 | */ |
| 1270 | if (CLIB_DEBUG > 0) |
| 1271 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1272 | ethernet_main_t *em = ðernet_main; |
| 1273 | ASSERT (em->next_by_ethertype_register_called == 0); |
Dave Barach | 1f49ed6 | 2016-02-24 11:29:06 -0500 | [diff] [blame] | 1274 | } |
| 1275 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | // Add an ethertype -> next index mapping to the structure |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1280 | clib_error_t * |
| 1281 | next_by_ethertype_register (next_by_ethertype_t * l3_next, |
| 1282 | u32 ethertype, u32 next_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1283 | { |
| 1284 | u32 i; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1285 | u16 *n; |
| 1286 | ethernet_main_t *em = ðernet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1287 | |
Dave Barach | 1f49ed6 | 2016-02-24 11:29:06 -0500 | [diff] [blame] | 1288 | if (CLIB_DEBUG > 0) |
| 1289 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1290 | ethernet_main_t *em = ðernet_main; |
Dave Barach | 1f49ed6 | 2016-02-24 11:29:06 -0500 | [diff] [blame] | 1291 | em->next_by_ethertype_register_called = 1; |
| 1292 | } |
| 1293 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1294 | /* Setup ethernet type -> next index sparse vector mapping. */ |
| 1295 | n = sparse_vec_validate (l3_next->input_next_by_type, ethertype); |
| 1296 | n[0] = next_index; |
| 1297 | |
| 1298 | /* Rebuild next index -> sparse index inverse mapping when sparse vector |
| 1299 | is updated. */ |
| 1300 | vec_validate (l3_next->sparse_index_by_input_next_index, next_index); |
| 1301 | for (i = 1; i < vec_len (l3_next->input_next_by_type); i++) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1302 | l3_next-> |
| 1303 | sparse_index_by_input_next_index[l3_next->input_next_by_type[i]] = i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1304 | |
| 1305 | // do not allow the cached next index's to be updated if L3 |
| 1306 | // redirect is enabled, as it will have overwritten them |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1307 | if (!em->redirect_l3) |
| 1308 | { |
| 1309 | // Cache common ethertypes directly |
| 1310 | if (ethertype == ETHERNET_TYPE_IP4) |
| 1311 | { |
| 1312 | l3_next->input_next_ip4 = next_index; |
| 1313 | } |
| 1314 | else if (ethertype == ETHERNET_TYPE_IP6) |
| 1315 | { |
| 1316 | l3_next->input_next_ip6 = next_index; |
| 1317 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 1318 | else if (ethertype == ETHERNET_TYPE_MPLS) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1319 | { |
| 1320 | l3_next->input_next_mpls = next_index; |
| 1321 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1322 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1327 | static clib_error_t * |
| 1328 | ethernet_input_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1329 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1330 | ethernet_main_t *em = ðernet_main; |
| 1331 | __attribute__ ((unused)) vlan_table_t *invalid_vlan_table; |
| 1332 | __attribute__ ((unused)) qinq_table_t *invalid_qinq_table; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1333 | |
| 1334 | ethernet_setup_node (vm, ethernet_input_node.index); |
| 1335 | ethernet_setup_node (vm, ethernet_input_type_node.index); |
| 1336 | ethernet_setup_node (vm, ethernet_input_not_l2_node.index); |
| 1337 | |
| 1338 | next_by_ethertype_init (&em->l3_next); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1339 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1340 | // Initialize pools and vector for vlan parsing |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1341 | vec_validate (em->main_intfs, 10); // 10 main interfaces |
| 1342 | pool_alloc (em->vlan_pool, 10); |
| 1343 | pool_alloc (em->qinq_pool, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1344 | |
| 1345 | // The first vlan pool will always be reserved for an invalid table |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1346 | pool_get (em->vlan_pool, invalid_vlan_table); // first id = 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1347 | // The first qinq pool will always be reserved for an invalid table |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1348 | pool_get (em->qinq_pool, invalid_qinq_table); // first id = 0 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | VLIB_INIT_FUNCTION (ethernet_input_init); |
| 1354 | |
| 1355 | void |
| 1356 | ethernet_register_input_type (vlib_main_t * vm, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1357 | ethernet_type_t type, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1358 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1359 | ethernet_main_t *em = ðernet_main; |
| 1360 | ethernet_type_info_t *ti; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1361 | u32 i; |
| 1362 | |
| 1363 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1364 | clib_error_t *error = vlib_call_init_function (vm, ethernet_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1365 | if (error) |
| 1366 | clib_error_report (error); |
| 1367 | } |
| 1368 | |
| 1369 | ti = ethernet_get_type_info (em, type); |
| 1370 | ti->node_index = node_index; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1371 | ti->next_index = vlib_node_add_next (vm, |
| 1372 | ethernet_input_node.index, node_index); |
| 1373 | i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1374 | ASSERT (i == ti->next_index); |
| 1375 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1376 | i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1377 | ASSERT (i == ti->next_index); |
| 1378 | |
| 1379 | // Add the L3 node for this ethertype to the next nodes structure |
| 1380 | next_by_ethertype_register (&em->l3_next, type, ti->next_index); |
| 1381 | |
| 1382 | // Call the registration functions for other nodes that want a mapping |
| 1383 | l2bvi_register_input_type (vm, type, node_index); |
| 1384 | } |
| 1385 | |
| 1386 | void |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1387 | ethernet_register_l2_input (vlib_main_t * vm, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1388 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1389 | ethernet_main_t *em = ðernet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1390 | u32 i; |
| 1391 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1392 | em->l2_next = |
| 1393 | vlib_node_add_next (vm, ethernet_input_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1394 | |
Damjan Marion | 607de1a | 2016-08-16 22:53:54 +0200 | [diff] [blame] | 1395 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1396 | * Even if we never use these arcs, we have to align the next indices... |
| 1397 | */ |
| 1398 | i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index); |
| 1399 | |
| 1400 | ASSERT (i == em->l2_next); |
| 1401 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1402 | i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1403 | ASSERT (i == em->l2_next); |
| 1404 | } |
| 1405 | |
| 1406 | // Register a next node for L3 redirect, and enable L3 redirect |
| 1407 | void |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1408 | ethernet_register_l3_redirect (vlib_main_t * vm, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1409 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1410 | ethernet_main_t *em = ðernet_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1411 | u32 i; |
| 1412 | |
| 1413 | em->redirect_l3 = 1; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1414 | em->redirect_l3_next = vlib_node_add_next (vm, |
| 1415 | ethernet_input_node.index, |
| 1416 | node_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1417 | /* |
| 1418 | * Change the cached next nodes to the redirect node |
| 1419 | */ |
| 1420 | em->l3_next.input_next_ip4 = em->redirect_l3_next; |
| 1421 | em->l3_next.input_next_ip6 = em->redirect_l3_next; |
| 1422 | em->l3_next.input_next_mpls = em->redirect_l3_next; |
| 1423 | |
| 1424 | /* |
| 1425 | * Even if we never use these arcs, we have to align the next indices... |
| 1426 | */ |
| 1427 | i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index); |
| 1428 | |
| 1429 | ASSERT (i == em->redirect_l3_next); |
jerryian | ff82ed6 | 2016-12-05 17:13:00 +0800 | [diff] [blame] | 1430 | |
| 1431 | i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index); |
| 1432 | |
| 1433 | ASSERT (i == em->redirect_l3_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1434 | } |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1435 | |
| 1436 | /* |
| 1437 | * fd.io coding-style-patch-verification: ON |
| 1438 | * |
| 1439 | * Local Variables: |
| 1440 | * eval: (c-set-style "gnu") |
| 1441 | * End: |
| 1442 | */ |