blob: 7abe462054e52bfa50f526d7a18348c1fcb6f8ed [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Lo7185c3b2016-06-04 00:02:37 -040028#define TO_BVI_ERR_BAD_MAC 1
Ed Warnickecb9cada2015-12-08 15:45:58 -070029#define TO_BVI_ERR_ETHERTYPE 2
30
Dave Barach97d8dc22016-08-15 15:31:15 -040031/**
32 * Send a packet from L2 processing to L3 via the BVI interface.
33 * Set next0 to the proper L3 input node.
34 * Return an error if the packet isn't what we expect.
35 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070036
Dave Barach97d8dc22016-08-15 15:31:15 -040037static_always_inline u32
Ed Warnickecb9cada2015-12-08 15:45:58 -070038l2_to_bvi (vlib_main_t * vlib_main,
Dave Barach97d8dc22016-08-15 15:31:15 -040039 vnet_main_t * vnet_main,
40 vlib_buffer_t * b0,
Neale Rannsc25eb452018-09-12 06:53:03 -040041 u32 bvi_sw_if_index, next_by_ethertype_t * l3_next, u16 * next0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070042{
Dave Barach97d8dc22016-08-15 15:31:15 -040043 /* Perform L3 my-mac filter */
Eyal Baribb376882018-08-23 12:16:41 +030044 ethernet_header_t *e0 = vlib_buffer_get_current (b0);
45 if (!ethernet_address_cast (e0->dst_address))
Dave Barach97d8dc22016-08-15 15:31:15 -040046 {
Eyal Baribb376882018-08-23 12:16:41 +030047 vnet_hw_interface_t *hi =
48 vnet_get_sup_hw_interface (vnet_main, bvi_sw_if_index);
49 if (!eth_mac_equal (e0->dst_address, hi->hw_address))
50 return TO_BVI_ERR_BAD_MAC;
Dave Barach97d8dc22016-08-15 15:31:15 -040051 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Dave Barach97d8dc22016-08-15 15:31:15 -040053 /* Save L2 header position which may be changed due to packet replication */
Damjan Marion072401e2017-07-13 18:53:27 +020054 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Dave Barach97d8dc22016-08-15 15:31:15 -040056 /* Strip L2 header */
Eyal Baribb376882018-08-23 12:16:41 +030057 u8 l2_len = vnet_buffer (b0)->l2.l2_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 vlib_buffer_advance (b0, l2_len);
59
Eyal Baribb376882018-08-23 12:16:41 +030060 u8 *l3h = vlib_buffer_get_current (b0);
61 u16 ethertype = clib_net_to_host_u16 (*(u16 *) (l3h - 2));
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
Dave Barach97d8dc22016-08-15 15:31:15 -040063 /* Set the input interface to be the BVI interface */
64 vnet_buffer (b0)->sw_if_index[VLIB_RX] = bvi_sw_if_index;
65 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Dave Barach97d8dc22016-08-15 15:31:15 -040067 /* Go to appropriate L3 input node */
68 if (ethertype == ETHERNET_TYPE_IP4)
69 {
70 *next0 = l3_next->input_next_ip4;
71 }
72 else if (ethertype == ETHERNET_TYPE_IP6)
73 {
74 *next0 = l3_next->input_next_ip6;
75 }
76 else
77 {
78 /* uncommon ethertype, check table */
Eyal Baribb376882018-08-23 12:16:41 +030079 u32 i0 = sparse_vec_index (l3_next->input_next_by_type, ethertype);
Dave Barach97d8dc22016-08-15 15:31:15 -040080 *next0 = vec_elt (l3_next->input_next_by_type, i0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
Dave Barach97d8dc22016-08-15 15:31:15 -040082 if (i0 == SPARSE_VEC_INVALID_INDEX)
83 {
84 return TO_BVI_ERR_ETHERTYPE;
85 }
86 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Dave Barach97d8dc22016-08-15 15:31:15 -040088 /* increment BVI RX interface stat */
89 vlib_increment_combined_counter
90 (vnet_main->interface_main.combined_sw_if_counters
91 + VNET_INTERFACE_COUNTER_RX,
Eyal Baribb376882018-08-23 12:16:41 +030092 vlib_main->thread_index, bvi_sw_if_index,
Dave Barach97d8dc22016-08-15 15:31:15 -040093 1, vlib_buffer_length_in_chain (vlib_main, b0));
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 return TO_BVI_ERR_OK;
95}
96
Ed Warnickecb9cada2015-12-08 15:45:58 -070097void
98l2bvi_register_input_type (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -040099 ethernet_type_t type, u32 node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100#endif
Dave Barach97d8dc22016-08-15 15:31:15 -0400101
102/*
103 * fd.io coding-style-patch-verification: ON
104 *
105 * Local Variables:
106 * eval: (c-set-style "gnu")
107 * End:
108 */