Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * l2_input.c : layer 2 input packet 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 | #include <vlib/vlib.h> |
| 19 | #include <vnet/vnet.h> |
| 20 | #include <vnet/pg/pg.h> |
| 21 | #include <vnet/ethernet/ethernet.h> |
| 22 | #include <vnet/ethernet/packet.h> |
| 23 | #include <vnet/ip/ip_packet.h> |
| 24 | #include <vnet/ip/ip4_packet.h> |
| 25 | #include <vnet/ip/ip6_packet.h> |
| 26 | #include <vnet/fib/fib_node.h> |
| 27 | #include <vnet/ethernet/arp_packet.h> |
| 28 | #include <vlib/cli.h> |
| 29 | #include <vnet/l2/l2_input.h> |
| 30 | #include <vnet/l2/l2_output.h> |
| 31 | #include <vnet/l2/feat_bitmap.h> |
| 32 | #include <vnet/l2/l2_bvi.h> |
| 33 | #include <vnet/l2/l2_fib.h> |
| 34 | #include <vnet/l2/l2_bd.h> |
| 35 | |
| 36 | #include <vppinfra/error.h> |
| 37 | #include <vppinfra/hash.h> |
| 38 | #include <vppinfra/cache.h> |
| 39 | |
| 40 | /** |
| 41 | * @file |
| 42 | * @brief Interface Input Mode (Layer 2 Cross-Connect or Bridge / Layer 3). |
| 43 | * |
| 44 | * This file contains the CLI Commands that modify the input mode of an |
| 45 | * interface. For interfaces in a Layer 2 cross-connect, all packets |
| 46 | * received on one interface will be transmitted to the other. For |
| 47 | * interfaces in a bridge-domain, packets will be forwarded to other |
| 48 | * interfaces in the same bridge-domain based on destination mac address. |
| 49 | * For interfaces in Layer 3 mode, the packets will be routed. |
| 50 | */ |
| 51 | |
| 52 | typedef struct |
| 53 | { |
| 54 | /* per-pkt trace data */ |
| 55 | u8 dst_and_src[12]; |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 56 | u32 sw_if_index; |
| 57 | u32 feat_mask; |
| 58 | } l2input_trace_t; |
| 59 | |
| 60 | /* packet trace format function */ |
| 61 | static u8 * |
| 62 | format_l2input_trace (u8 * s, va_list * args) |
| 63 | { |
| 64 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 65 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 66 | l2input_trace_t *t = va_arg (*args, l2input_trace_t *); |
| 67 | |
| 68 | s = format (s, "l2-input: sw_if_index %d dst %U src %U [%U]", |
| 69 | t->sw_if_index, |
| 70 | format_ethernet_address, t->dst_and_src, |
| 71 | format_ethernet_address, t->dst_and_src + 6, |
| 72 | format_l2_input_feature_bitmap, t->feat_mask, 0); |
| 73 | return s; |
| 74 | } |
| 75 | |
| 76 | extern l2input_main_t l2input_main; |
| 77 | |
| 78 | #ifndef CLIB_MARCH_VARIANT |
| 79 | l2input_main_t l2input_main; |
| 80 | #endif /* CLIB_MARCH_VARIANT */ |
| 81 | |
| 82 | #define foreach_l2input_error \ |
| 83 | _(L2INPUT, "L2 input packets") \ |
| 84 | _(DROP, "L2 input drops") |
| 85 | |
| 86 | typedef enum |
| 87 | { |
| 88 | #define _(sym,str) L2INPUT_ERROR_##sym, |
| 89 | foreach_l2input_error |
| 90 | #undef _ |
| 91 | L2INPUT_N_ERROR, |
| 92 | } l2input_error_t; |
| 93 | |
| 94 | static char *l2input_error_strings[] = { |
| 95 | #define _(sym,string) string, |
| 96 | foreach_l2input_error |
| 97 | #undef _ |
| 98 | }; |
| 99 | |
| 100 | typedef enum |
| 101 | { /* */ |
| 102 | L2INPUT_NEXT_LEARN, |
| 103 | L2INPUT_NEXT_FWD, |
| 104 | L2INPUT_NEXT_DROP, |
| 105 | L2INPUT_N_NEXT, |
| 106 | } l2input_next_t; |
| 107 | |
| 108 | static_always_inline void |
| 109 | classify_and_dispatch (l2input_main_t * msm, vlib_buffer_t * b0, u16 * next0) |
| 110 | { |
| 111 | /* |
| 112 | * Load L2 input feature struct |
| 113 | * Load bridge domain struct |
| 114 | * Parse ethernet header to determine unicast/mcast/broadcast |
| 115 | * take L2 input stat |
| 116 | * classify packet as IP/UDP/TCP, control, other |
| 117 | * mask feature bitmap |
| 118 | * go to first node in bitmap |
| 119 | * Later: optimize VTM |
| 120 | * |
| 121 | * For L2XC, |
| 122 | * set tx sw-if-handle |
| 123 | */ |
| 124 | |
| 125 | u32 feat_mask = ~0; |
| 126 | u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX]; |
| 127 | ethernet_header_t *h0 = vlib_buffer_get_current (b0); |
| 128 | |
| 129 | /* Get config for the input interface */ |
| 130 | l2_input_config_t *config = vec_elt_at_index (msm->configs, sw_if_index0); |
| 131 | |
| 132 | /* Save split horizon group */ |
| 133 | vnet_buffer (b0)->l2.shg = config->shg; |
| 134 | |
| 135 | /* determine layer2 kind for stat and mask */ |
| 136 | if (PREDICT_FALSE (ethernet_address_cast (h0->dst_address))) |
| 137 | { |
| 138 | u8 *l3h0 = (u8 *) h0 + vnet_buffer (b0)->l2.l2_len; |
| 139 | |
| 140 | #define get_u16(addr) ( *((u16 *)(addr)) ) |
| 141 | u16 ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2)); |
| 142 | u8 protocol = ((ip6_header_t *) l3h0)->protocol; |
| 143 | |
| 144 | /* Disable bridge forwarding (flooding will execute instead if not xconnect) */ |
| 145 | feat_mask &= ~(L2INPUT_FEAT_FWD | |
| 146 | L2INPUT_FEAT_UU_FLOOD | |
| 147 | L2INPUT_FEAT_UU_FWD | L2INPUT_FEAT_GBP_FWD); |
| 148 | |
| 149 | if (ethertype != ETHERNET_TYPE_ARP) |
| 150 | feat_mask &= ~(L2INPUT_FEAT_ARP_UFWD); |
| 151 | |
| 152 | /* Disable ARP-term for non-ARP and non-ICMP6 packet */ |
| 153 | if (ethertype != ETHERNET_TYPE_ARP && |
| 154 | (ethertype != ETHERNET_TYPE_IP6 || protocol != IP_PROTOCOL_ICMP6)) |
| 155 | feat_mask &= ~(L2INPUT_FEAT_ARP_TERM); |
| 156 | /* |
| 157 | * For packet from BVI - set SHG of ARP request or ICMPv6 neighbor |
| 158 | * solicitation packet from BVI to 0 so it can also flood to VXLAN |
| 159 | * tunnels or other ports with the same SHG as that of the BVI. |
| 160 | */ |
| 161 | else if (PREDICT_FALSE (vnet_buffer (b0)->sw_if_index[VLIB_TX] == |
| 162 | L2INPUT_BVI)) |
| 163 | { |
| 164 | if (ethertype == ETHERNET_TYPE_ARP) |
| 165 | { |
| 166 | ethernet_arp_header_t *arp0 = (ethernet_arp_header_t *) l3h0; |
| 167 | if (arp0->opcode == |
| 168 | clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request)) |
| 169 | vnet_buffer (b0)->l2.shg = 0; |
| 170 | } |
| 171 | else /* must be ICMPv6 */ |
| 172 | { |
| 173 | ip6_header_t *iph0 = (ip6_header_t *) l3h0; |
| 174 | icmp6_neighbor_solicitation_or_advertisement_header_t *ndh0; |
| 175 | ndh0 = ip6_next_header (iph0); |
| 176 | if (ndh0->icmp.type == ICMP6_neighbor_solicitation) |
| 177 | vnet_buffer (b0)->l2.shg = 0; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | /* |
| 184 | * For packet from BVI - set SHG of unicast packet from BVI to 0 so it |
| 185 | * is not dropped on output to VXLAN tunnels or other ports with the |
| 186 | * same SHG as that of the BVI. |
| 187 | */ |
| 188 | if (PREDICT_FALSE (vnet_buffer (b0)->sw_if_index[VLIB_TX] == |
| 189 | L2INPUT_BVI)) |
| 190 | vnet_buffer (b0)->l2.shg = 0; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | if (l2_input_is_bridge (config)) |
| 195 | { |
| 196 | /* Do bridge-domain processing */ |
| 197 | /* save BD ID for next feature graph nodes */ |
| 198 | vnet_buffer (b0)->l2.bd_index = config->bd_index; |
| 199 | |
| 200 | /* Save bridge domain and interface seq_num */ |
| 201 | vnet_buffer (b0)->l2.l2fib_sn = l2_fib_mk_seq_num |
| 202 | (config->bd_seq_num, config->seq_num); |
| 203 | vnet_buffer (b0)->l2.bd_age = config->bd_mac_age; |
| 204 | |
| 205 | /* |
| 206 | * Process bridge domain feature enables. |
| 207 | * To perform learning/flooding/forwarding, the corresponding bit |
| 208 | * must be enabled in both the input interface config and in the |
| 209 | * bridge domain config. In the bd_bitmap, bits for features other |
| 210 | * than learning/flooding/forwarding should always be set. |
| 211 | */ |
| 212 | feat_mask = feat_mask & config->bd_feature_bitmap; |
| 213 | } |
| 214 | else if (l2_input_is_xconnect (config)) |
| 215 | { |
| 216 | /* Set the output interface */ |
| 217 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = config->output_sw_if_index; |
| 218 | } |
| 219 | else |
| 220 | feat_mask = L2INPUT_FEAT_DROP; |
| 221 | |
| 222 | /* mask out features from bitmap using packet type and bd config */ |
| 223 | u32 feature_bitmap = config->feature_bitmap & feat_mask; |
| 224 | |
| 225 | /* save for next feature graph nodes */ |
| 226 | vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap; |
| 227 | |
| 228 | /* Determine the next node */ |
| 229 | *next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index, |
| 230 | feature_bitmap); |
| 231 | } |
| 232 | |
| 233 | static_always_inline uword |
| 234 | l2input_node_inline (vlib_main_t * vm, |
| 235 | vlib_node_runtime_t * node, vlib_frame_t * frame, |
| 236 | int do_trace) |
| 237 | { |
| 238 | u32 n_left, *from; |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 239 | l2input_main_t *msm = &l2input_main; |
| 240 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 241 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 242 | |
| 243 | from = vlib_frame_vector_args (frame); |
| 244 | n_left = frame->n_vectors; /* number of packets to process */ |
Neale Ranns | 47a3d99 | 2020-09-29 15:38:51 +0000 | [diff] [blame] | 245 | |
| 246 | vlib_get_buffers (vm, from, bufs, n_left); |
| 247 | |
| 248 | while (n_left > 0) |
| 249 | { |
| 250 | while (n_left >= 8) |
| 251 | { |
| 252 | u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3; |
| 253 | |
| 254 | /* Prefetch next iteration. */ |
| 255 | { |
| 256 | /* Prefetch the buffer header and packet for the N+2 loop iteration */ |
| 257 | vlib_prefetch_buffer_header (b[4], LOAD); |
| 258 | vlib_prefetch_buffer_header (b[5], LOAD); |
| 259 | vlib_prefetch_buffer_header (b[6], LOAD); |
| 260 | vlib_prefetch_buffer_header (b[7], LOAD); |
| 261 | |
| 262 | CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 263 | CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 264 | CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 265 | CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE); |
| 266 | } |
| 267 | |
| 268 | classify_and_dispatch (msm, b[0], &next[0]); |
| 269 | classify_and_dispatch (msm, b[1], &next[1]); |
| 270 | classify_and_dispatch (msm, b[2], &next[2]); |
| 271 | classify_and_dispatch (msm, b[3], &next[3]); |
| 272 | |
| 273 | if (do_trace) |
| 274 | { |
| 275 | /* RX interface handles */ |
| 276 | sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX]; |
| 277 | sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX]; |
| 278 | sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX]; |
| 279 | sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX]; |
| 280 | |
| 281 | if (b[0]->flags & VLIB_BUFFER_IS_TRACED) |
| 282 | { |
| 283 | ethernet_header_t *h0 = vlib_buffer_get_current (b[0]); |
| 284 | l2input_trace_t *t = |
| 285 | vlib_add_trace (vm, node, b[0], sizeof (*t)); |
| 286 | t->sw_if_index = sw_if_index0; |
| 287 | t->feat_mask = vnet_buffer (b[0])->l2.feature_bitmap; |
| 288 | clib_memcpy_fast (t->dst_and_src, h0->dst_address, |
| 289 | sizeof (h0->dst_address) + |
| 290 | sizeof (h0->src_address)); |
| 291 | } |
| 292 | if (b[1]->flags & VLIB_BUFFER_IS_TRACED) |
| 293 | { |
| 294 | ethernet_header_t *h1 = vlib_buffer_get_current (b[1]); |
| 295 | l2input_trace_t *t = |
| 296 | vlib_add_trace (vm, node, b[1], sizeof (*t)); |
| 297 | t->sw_if_index = sw_if_index1; |
| 298 | t->feat_mask = vnet_buffer (b[1])->l2.feature_bitmap; |
| 299 | clib_memcpy_fast (t->dst_and_src, h1->dst_address, |
| 300 | sizeof (h1->dst_address) + |
| 301 | sizeof (h1->src_address)); |
| 302 | } |
| 303 | if (b[2]->flags & VLIB_BUFFER_IS_TRACED) |
| 304 | { |
| 305 | ethernet_header_t *h2 = vlib_buffer_get_current (b[2]); |
| 306 | l2input_trace_t *t = |
| 307 | vlib_add_trace (vm, node, b[2], sizeof (*t)); |
| 308 | t->sw_if_index = sw_if_index2; |
| 309 | t->feat_mask = vnet_buffer (b[2])->l2.feature_bitmap; |
| 310 | clib_memcpy_fast (t->dst_and_src, h2->dst_address, |
| 311 | sizeof (h2->dst_address) + |
| 312 | sizeof (h2->src_address)); |
| 313 | } |
| 314 | if (b[3]->flags & VLIB_BUFFER_IS_TRACED) |
| 315 | { |
| 316 | ethernet_header_t *h3 = vlib_buffer_get_current (b[3]); |
| 317 | l2input_trace_t *t = |
| 318 | vlib_add_trace (vm, node, b[3], sizeof (*t)); |
| 319 | t->sw_if_index = sw_if_index3; |
| 320 | t->feat_mask = vnet_buffer (b[3])->l2.feature_bitmap; |
| 321 | clib_memcpy_fast (t->dst_and_src, h3->dst_address, |
| 322 | sizeof (h3->dst_address) + |
| 323 | sizeof (h3->src_address)); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | b += 4; |
| 328 | n_left -= 4; |
| 329 | next += 4; |
| 330 | } |
| 331 | |
| 332 | while (n_left > 0) |
| 333 | { |
| 334 | classify_and_dispatch (msm, b[0], &next[0]); |
| 335 | |
| 336 | if (do_trace && PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 337 | { |
| 338 | ethernet_header_t *h0 = vlib_buffer_get_current (b[0]); |
| 339 | l2input_trace_t *t = |
| 340 | vlib_add_trace (vm, node, b[0], sizeof (*t)); |
| 341 | t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX]; |
| 342 | t->feat_mask = vnet_buffer (b[0])->l2.feature_bitmap; |
| 343 | clib_memcpy_fast (t->dst_and_src, h0->dst_address, |
| 344 | sizeof (h0->dst_address) + |
| 345 | sizeof (h0->src_address)); |
| 346 | } |
| 347 | |
| 348 | b += 1; |
| 349 | next += 1; |
| 350 | n_left -= 1; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | vlib_node_increment_counter (vm, l2input_node.index, |
| 355 | L2INPUT_ERROR_L2INPUT, frame->n_vectors); |
| 356 | |
| 357 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 358 | |
| 359 | return frame->n_vectors; |
| 360 | } |
| 361 | |
| 362 | VLIB_NODE_FN (l2input_node) (vlib_main_t * vm, |
| 363 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
| 364 | { |
| 365 | if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE))) |
| 366 | return l2input_node_inline (vm, node, frame, 1 /* do_trace */ ); |
| 367 | return l2input_node_inline (vm, node, frame, 0 /* do_trace */ ); |
| 368 | } |
| 369 | |
| 370 | /* *INDENT-OFF* */ |
| 371 | VLIB_REGISTER_NODE (l2input_node) = { |
| 372 | .name = "l2-input", |
| 373 | .vector_size = sizeof (u32), |
| 374 | .format_trace = format_l2input_trace, |
| 375 | .format_buffer = format_ethernet_header_with_length, |
| 376 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 377 | |
| 378 | .n_errors = ARRAY_LEN(l2input_error_strings), |
| 379 | .error_strings = l2input_error_strings, |
| 380 | |
| 381 | .n_next_nodes = L2INPUT_N_NEXT, |
| 382 | |
| 383 | /* edit / add dispositions here */ |
| 384 | .next_nodes = { |
| 385 | [L2INPUT_NEXT_LEARN] = "l2-learn", |
| 386 | [L2INPUT_NEXT_FWD] = "l2-fwd", |
| 387 | [L2INPUT_NEXT_DROP] = "error-drop", |
| 388 | }, |
| 389 | }; |
| 390 | /* *INDENT-ON* */ |
| 391 | |
| 392 | /* |
| 393 | * fd.io coding-style-patch-verification: ON |
| 394 | * |
| 395 | * Local Variables: |
| 396 | * eval: (c-set-style "gnu") |
| 397 | * End: |
| 398 | */ |