FD.io VPP  v16.06
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 
31 // Send a packet from L2 processing to L3 via the BVI interface.
32 // Set next0 to the proper L3 input node.
33 // Return an error if the packet isn't what we expect.
34 
38  vlib_buffer_t * b0,
39  u32 bvi_sw_if_index,
40  next_by_ethertype_t * l3_next,
41  u32 * next0)
42 {
43  u8 l2_len;
44  u16 ethertype;
45  u8 * l3h;
46  ethernet_header_t * e0;
48 
49  e0 = vlib_buffer_get_current (b0);
50  hi = vnet_get_sup_hw_interface (vnet_main, bvi_sw_if_index);
51 
52  // Perform L3 my-mac filter
53  if ((!ethernet_address_cast(e0->dst_address)) &&
54  (!eth_mac_equal((u8 *)e0, hi->hw_address))) {
55  return TO_BVI_ERR_BAD_MAC;
56  }
57 
58  // Save L2 header position which may be changed due to packet replication
59  vnet_buffer (b0)->ethernet.start_of_ethernet_header = b0->current_data;
60 
61  // Strip L2 header
62  l2_len = vnet_buffer(b0)->l2.l2_len;
63  vlib_buffer_advance (b0, l2_len);
64 
65  l3h = vlib_buffer_get_current (b0);
66  ethertype = clib_net_to_host_u16(*(u16 *)(l3h - 2));
67 
68  // Set the input interface to be the BVI interface
69  vnet_buffer(b0)->sw_if_index[VLIB_RX] = bvi_sw_if_index;
70  vnet_buffer(b0)->sw_if_index[VLIB_TX] = ~0;
71 
72  // Go to appropriate L3 input node
73  if (ethertype == ETHERNET_TYPE_IP4) {
74  *next0 = l3_next->input_next_ip4;
75  } else if (ethertype == ETHERNET_TYPE_IP6) {
76  *next0 = l3_next->input_next_ip6;
77  } else {
78  // uncommon ethertype, check table
79  u32 i0;
80 
81  i0 = sparse_vec_index (l3_next->input_next_by_type, ethertype);
82  *next0 = vec_elt (l3_next->input_next_by_type, i0);
83 
84  if (i0 == SPARSE_VEC_INVALID_INDEX) {
85  return TO_BVI_ERR_ETHERTYPE;
86  }
87  }
88 
89  // increment BVI RX interface stat
93  vlib_main->cpu_index,
94  vnet_buffer(b0)->sw_if_index[VLIB_RX],
95  1,
96  vlib_buffer_length_in_chain (vlib_main, b0));
97  return TO_BVI_ERR_OK;
98 }
99 
100 void
103  u32 node_index);
104 #endif
always_inline u32 eth_mac_equal(u8 *mac1, u8 *mac2)
Definition: ethernet.h:477
vmrglw vmrglh hi
void l2bvi_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: l2_bvi.c:27
#define TO_BVI_ERR_BAD_MAC
Definition: l2_bvi.h:28
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
vnet_interface_main_t interface_main
Definition: vnet.h:62
ethernet_type_t
Definition: packet.h:43
#define TO_BVI_ERR_ETHERTYPE
Definition: l2_bvi.h:29
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, u32 *next0)
Definition: l2_bvi.h:36
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:77
#define static_always_inline
Definition: clib.h:85
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:458
u8 dst_address[6]
Definition: packet.h:51
always_inline void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Definition: counter.h:210
always_inline 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:112
u32 cpu_index
Definition: main.h:159
int vlib_main(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:1538
always_inline uword ethernet_address_cast(u8 *a)
Definition: packet.h:63
vnet_main_t vnet_main
Definition: misc.c:42
always_inline vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:300
#define vec_elt(v, i)
Get vector value at index i.
Definition: defs.h:46
unsigned short u16
Definition: types.h:57
always_inline void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:197
unsigned char u8
Definition: types.h:56
#define TO_BVI_ERR_OK
Definition: l2_bvi.h:27
Definition: defs.h:45
u16 * input_next_by_type
Definition: ethernet.h:177
#define SPARSE_VEC_INVALID_INDEX
Definition: sparse_vec.h:65
always_inline uword sparse_vec_index(void *v, uword sparse_index)
Definition: sparse_vec.h:150