Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * l2_bvi.h : layer 2 Bridged Virtual Interface |
| 3 | * |
| 4 | * Copyright (c) 2013 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef included_l2bvi_h |
| 19 | #define included_l2bvi_h |
| 20 | |
| 21 | #include <vlib/vlib.h> |
| 22 | #include <vnet/ethernet/ethernet.h> |
| 23 | #include <vppinfra/sparse_vec.h> |
| 24 | |
| 25 | #include <vnet/l2/l2_input.h> |
| 26 | |
| 27 | #define TO_BVI_ERR_OK 0 |
John Lo | 7185c3b | 2016-06-04 00:02:37 -0400 | [diff] [blame] | 28 | #define TO_BVI_ERR_BAD_MAC 1 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | #define TO_BVI_ERR_ETHERTYPE 2 |
| 30 | |
Matthew Smith | adf2fe0 | 2020-08-31 14:55:17 -0500 | [diff] [blame] | 31 | static_always_inline u32 |
| 32 | l2_to_bvi_dmac_check (vnet_hw_interface_t * hi, u8 * dmac, |
| 33 | ethernet_interface_t * ei, u8 have_sec_dmac) |
| 34 | { |
Benoît Ganne | b44c77d | 2020-10-20 16:24:17 +0200 | [diff] [blame] | 35 | ethernet_interface_address_t *sec_addr; |
Matthew Smith | adf2fe0 | 2020-08-31 14:55:17 -0500 | [diff] [blame] | 36 | |
| 37 | if (ethernet_mac_address_equal (dmac, hi->hw_address)) |
| 38 | return TO_BVI_ERR_OK; |
| 39 | |
| 40 | if (have_sec_dmac) |
| 41 | { |
| 42 | vec_foreach (sec_addr, ei->secondary_addrs) |
| 43 | { |
Benoît Ganne | b44c77d | 2020-10-20 16:24:17 +0200 | [diff] [blame] | 44 | if (ethernet_mac_address_equal (dmac, sec_addr->mac.bytes)) |
Matthew Smith | adf2fe0 | 2020-08-31 14:55:17 -0500 | [diff] [blame] | 45 | return TO_BVI_ERR_OK; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return TO_BVI_ERR_BAD_MAC; |
| 50 | } |
| 51 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 52 | /** |
| 53 | * Send a packet from L2 processing to L3 via the BVI interface. |
| 54 | * Set next0 to the proper L3 input node. |
| 55 | * Return an error if the packet isn't what we expect. |
| 56 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 58 | static_always_inline u32 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | l2_to_bvi (vlib_main_t * vlib_main, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 60 | vnet_main_t * vnet_main, |
| 61 | vlib_buffer_t * b0, |
Neale Ranns | c25eb45 | 2018-09-12 06:53:03 -0400 | [diff] [blame] | 62 | u32 bvi_sw_if_index, next_by_ethertype_t * l3_next, u16 * next0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | { |
Matthew Smith | adf2fe0 | 2020-08-31 14:55:17 -0500 | [diff] [blame] | 64 | ethernet_main_t *em = ðernet_main; |
| 65 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 66 | /* Perform L3 my-mac filter */ |
Eyal Bari | bb37688 | 2018-08-23 12:16:41 +0300 | [diff] [blame] | 67 | ethernet_header_t *e0 = vlib_buffer_get_current (b0); |
| 68 | if (!ethernet_address_cast (e0->dst_address)) |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 69 | { |
Eyal Bari | bb37688 | 2018-08-23 12:16:41 +0300 | [diff] [blame] | 70 | vnet_hw_interface_t *hi = |
| 71 | vnet_get_sup_hw_interface (vnet_main, bvi_sw_if_index); |
Matthew Smith | adf2fe0 | 2020-08-31 14:55:17 -0500 | [diff] [blame] | 72 | ethernet_interface_t *ei = ethernet_get_interface (em, hi->hw_if_index); |
| 73 | u32 rv; |
| 74 | |
Matthew Smith | 78681de | 2020-09-10 10:09:01 -0500 | [diff] [blame] | 75 | if (PREDICT_FALSE (ei && (vec_len (ei->secondary_addrs) > 0))) |
Matthew Smith | adf2fe0 | 2020-08-31 14:55:17 -0500 | [diff] [blame] | 76 | rv = l2_to_bvi_dmac_check (hi, e0->dst_address, ei, |
| 77 | 1 /* have_sec_dmac */ ); |
| 78 | else |
| 79 | rv = l2_to_bvi_dmac_check (hi, e0->dst_address, ei, |
| 80 | 0 /* have_sec_dmac */ ); |
| 81 | |
| 82 | if (rv != TO_BVI_ERR_OK) |
| 83 | return rv; |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 84 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 86 | /* Save L2 header position which may be changed due to packet replication */ |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 87 | vnet_buffer (b0)->l2_hdr_offset = b0->current_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 89 | /* Strip L2 header */ |
Eyal Bari | bb37688 | 2018-08-23 12:16:41 +0300 | [diff] [blame] | 90 | u8 l2_len = vnet_buffer (b0)->l2.l2_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | vlib_buffer_advance (b0, l2_len); |
| 92 | |
Eyal Bari | bb37688 | 2018-08-23 12:16:41 +0300 | [diff] [blame] | 93 | u8 *l3h = vlib_buffer_get_current (b0); |
| 94 | u16 ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 96 | /* Set the input interface to be the BVI interface */ |
| 97 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = bvi_sw_if_index; |
| 98 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 100 | /* Go to appropriate L3 input node */ |
| 101 | if (ethertype == ETHERNET_TYPE_IP4) |
| 102 | { |
| 103 | *next0 = l3_next->input_next_ip4; |
| 104 | } |
| 105 | else if (ethertype == ETHERNET_TYPE_IP6) |
| 106 | { |
| 107 | *next0 = l3_next->input_next_ip6; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | /* uncommon ethertype, check table */ |
Eyal Bari | bb37688 | 2018-08-23 12:16:41 +0300 | [diff] [blame] | 112 | u32 i0 = sparse_vec_index (l3_next->input_next_by_type, ethertype); |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 113 | *next0 = vec_elt (l3_next->input_next_by_type, i0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 115 | if (i0 == SPARSE_VEC_INVALID_INDEX) |
| 116 | { |
| 117 | return TO_BVI_ERR_ETHERTYPE; |
| 118 | } |
| 119 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 121 | /* increment BVI RX interface stat */ |
| 122 | vlib_increment_combined_counter |
| 123 | (vnet_main->interface_main.combined_sw_if_counters |
| 124 | + VNET_INTERFACE_COUNTER_RX, |
Eyal Bari | bb37688 | 2018-08-23 12:16:41 +0300 | [diff] [blame] | 125 | vlib_main->thread_index, bvi_sw_if_index, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 126 | 1, vlib_buffer_length_in_chain (vlib_main, b0)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | return TO_BVI_ERR_OK; |
| 128 | } |
| 129 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | void |
| 131 | l2bvi_register_input_type (vlib_main_t * vm, |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 132 | ethernet_type_t type, u32 node_index); |
Neale Ranns | 192b13f | 2019-03-15 02:16:20 -0700 | [diff] [blame] | 133 | |
| 134 | extern int l2_bvi_create (u32 instance, const mac_address_t * mac, |
| 135 | u32 * sw_if_index); |
| 136 | extern int l2_bvi_delete (u32 sw_if_index); |
| 137 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | #endif |
Dave Barach | 97d8dc2 | 2016-08-15 15:31:15 -0400 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * fd.io coding-style-patch-verification: ON |
| 142 | * |
| 143 | * Local Variables: |
| 144 | * eval: (c-set-style "gnu") |
| 145 | * End: |
| 146 | */ |