FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
l2_bvi.h
Go to the documentation of this file.
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
28 #define TO_BVI_ERR_BAD_MAC 1
29 #define TO_BVI_ERR_ETHERTYPE 2
30 
33  ethernet_interface_t * ei, u8 have_sec_dmac)
34 {
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  {
44  if (ethernet_mac_address_equal (dmac, sec_addr->mac.bytes))
45  return TO_BVI_ERR_OK;
46  }
47  }
48 
49  return TO_BVI_ERR_BAD_MAC;
50 }
51 
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  */
57 
61  vlib_buffer_t * b0,
62  u32 bvi_sw_if_index, next_by_ethertype_t * l3_next, u16 * next0)
63 {
65 
66  /* Perform L3 my-mac filter */
69  {
72  ethernet_interface_t *ei = ethernet_get_interface (em, hi->hw_if_index);
73  u32 rv;
74 
75  if (PREDICT_FALSE (ei && (vec_len (ei->secondary_addrs) > 0)))
77  1 /* have_sec_dmac */ );
78  else
80  0 /* have_sec_dmac */ );
81 
82  if (rv != TO_BVI_ERR_OK)
83  return rv;
84  }
85 
86  /* Save L2 header position which may be changed due to packet replication */
87  vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
88 
89  /* Strip L2 header */
90  u8 l2_len = vnet_buffer (b0)->l2.l2_len;
91  vlib_buffer_advance (b0, l2_len);
92 
93  u8 *l3h = vlib_buffer_get_current (b0);
94  u16 ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2));
95 
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;
99 
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 */
112  u32 i0 = sparse_vec_index (l3_next->input_next_by_type, ethertype);
113  *next0 = vec_elt (l3_next->input_next_by_type, i0);
114 
115  if (i0 == SPARSE_VEC_INVALID_INDEX)
116  {
117  return TO_BVI_ERR_ETHERTYPE;
118  }
119  }
120 
121  /* increment BVI RX interface stat */
125  vlib_main->thread_index, bvi_sw_if_index,
127  return TO_BVI_ERR_OK;
128 }
129 
130 void
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 
138 #endif
139 
140 /*
141  * fd.io coding-style-patch-verification: ON
142  *
143  * Local Variables:
144  * eval: (c-set-style "gnu")
145  * End:
146  */
vlib.h
next_by_ethertype_t::input_next_by_type
u16 * input_next_by_type
Definition: ethernet.h:269
mac
vl_api_mac_address_t mac
Definition: l2.api:559
l2_bvi_delete
int l2_bvi_delete(u32 sw_if_index)
Definition: l2_bvi.c:210
TO_BVI_ERR_BAD_MAC
#define TO_BVI_ERR_BAD_MAC
Definition: l2_bvi.h:28
ethernet_header_t::dst_address
u8 dst_address[6]
Definition: packet.h:55
next_by_ethertype_t::input_next_ip4
u32 input_next_ip4
Definition: ethernet.h:273
TO_BVI_ERR_ETHERTYPE
#define TO_BVI_ERR_ETHERTYPE
Definition: l2_bvi.h:29
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
node_index
node node_index
Definition: interface_output.c:420
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
bvi_sw_if_index
vl_api_interface_index_t bvi_sw_if_index
Definition: gbp.api:37
SPARSE_VEC_INVALID_INDEX
#define SPARSE_VEC_INVALID_INDEX
Definition: sparse_vec.h:68
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
next_by_ethertype_t
Definition: ethernet.h:265
ethernet.h
vec_elt
#define vec_elt(v, i)
Get vector value at index i.
Definition: vec_bootstrap.h:210
ethernet_mac_address_equal
static int ethernet_mac_address_equal(const u8 *a, const u8 *b)
Definition: mac_address.h:85
vlib_buffer_t::current_data
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:119
vlib_buffer_advance
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
ethernet_interface_address
Definition: ethernet.h:136
next_by_ethertype_t::input_next_ip6
u32 input_next_ip6
Definition: ethernet.h:274
ethernet_main_t_
Definition: ethernet.h:289
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
l2_bvi_create
int l2_bvi_create(u32 instance, const mac_address_t *mac, u32 *sw_if_index)
Definition: l2_bvi.c:136
static_always_inline
#define static_always_inline
Definition: clib.h:112
ethernet_header_t
Definition: packet.h:52
sparse_vec_index
static uword sparse_vec_index(void *v, uword sparse_index)
Definition: sparse_vec.h:161
sparse_vec.h
ethernet_type_t
ethernet_type_t
Definition: packet.h:45
ethernet_interface
Definition: ethernet.h:147
mac_address_t_::bytes
u8 bytes[6]
Definition: mac_address.h:25
VNET_INTERFACE_COUNTER_RX
@ VNET_INTERFACE_COUNTER_RX
Definition: interface.h:914
vnet_interface_main_t::combined_sw_if_counters
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:1023
ethernet_get_interface
ethernet_interface_t * ethernet_get_interface(ethernet_main_t *em, u32 hw_if_index)
Definition: interface.c:982
l2_input.h
ethernet_interface_address::mac
mac_address_t mac
Definition: ethernet.h:140
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
ethernet_main
ethernet_main_t ethernet_main
Definition: init.c:45
ethernet_address_cast
static uword ethernet_address_cast(const u8 *a)
Definition: packet.h:67
u32
unsigned int u32
Definition: types.h:88
vnet_get_sup_hw_interface
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:92
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
instance
u32 instance
Definition: gre.api:51
mac_address_t_
Definition: mac_address.h:21
l2_to_bvi_dmac_check
static_always_inline u32 l2_to_bvi_dmac_check(vnet_hw_interface_t *hi, u8 *dmac, ethernet_interface_t *ei, u8 have_sec_dmac)
Definition: l2_bvi.h:32
vnet_main
vnet_main_t vnet_main
Definition: misc.c:43
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
rv
int __clib_unused rv
Definition: application.c:491
l2bvi_register_input_type
void l2bvi_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: l2_bvi.c:29
vlib_main
int vlib_main(vlib_main_t *volatile vm, unformat_input_t *input)
Definition: main.c:1914
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
ethernet_interface::secondary_addrs
ethernet_interface_address_t * secondary_addrs
Definition: ethernet.h:177
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
l2_to_bvi
static_always_inline u32 l2_to_bvi(vlib_main_t *vlib_main, vnet_main_t *vnet_main, vlib_buffer_t *b0, u32 bvi_sw_if_index, next_by_ethertype_t *l3_next, u16 *next0)
Send a packet from L2 processing to L3 via the BVI interface.
Definition: l2_bvi.h:59
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
TO_BVI_ERR_OK
#define TO_BVI_ERR_OK
Definition: l2_bvi.h:27
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81
vlib_increment_combined_counter
vlib_increment_combined_counter(ccm, ti, sw_if_index, n_buffers, n_bytes)
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111