Initial commit of vpp code.
[vpp.git] / vnet / vnet / l2 / l2_bvi.h
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_TAGGED    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
35 static_always_inline u32 
36 l2_to_bvi (vlib_main_t * vlib_main,
37            vnet_main_t * vnet_main,
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
47   // Save L2 header position which may be changed due to packet replication
48   vnet_buffer (b0)->ethernet.start_of_ethernet_header = b0->current_data;
49
50   // Strip L2 header
51   l2_len = vnet_buffer(b0)->l2.l2_len;
52   vlib_buffer_advance (b0, l2_len);
53
54   l3h = vlib_buffer_get_current (b0);
55   ethertype = clib_net_to_host_u16(*(u16 *)(l3h - 2));
56
57   // Set the input interface to be the BVI interface
58   vnet_buffer(b0)->sw_if_index[VLIB_RX] = bvi_sw_if_index;
59   vnet_buffer(b0)->sw_if_index[VLIB_TX] = ~0;
60
61   // Go to appropriate L3 input node
62   if (ethertype == ETHERNET_TYPE_IP4) {
63     *next0 = l3_next->input_next_ip4;
64   } else if (ethertype == ETHERNET_TYPE_IP6) {
65     *next0 = l3_next->input_next_ip6;
66   } else {
67     // uncommon ethertype, check table
68     u32 i0;
69
70     i0 = sparse_vec_index (l3_next->input_next_by_type, ethertype);
71     *next0 = vec_elt (l3_next->input_next_by_type, i0);
72
73     if (i0 == SPARSE_VEC_INVALID_INDEX) {
74       return TO_BVI_ERR_ETHERTYPE;
75     } 
76   }
77
78   // increment BVI RX interface stat
79   vlib_increment_combined_counter 
80       (vnet_main->interface_main.combined_sw_if_counters
81        + VNET_INTERFACE_COUNTER_RX,
82        vlib_main->cpu_index, 
83        vnet_buffer(b0)->sw_if_index[VLIB_RX],
84        1,
85        vlib_buffer_length_in_chain (vlib_main, b0));
86   return TO_BVI_ERR_OK;
87 }
88
89
90 // Prepare a packet that was sent to the BVI interface for L2 processing.
91
92 static_always_inline void 
93 bvi_to_l2 (vlib_main_t * vlib_main,
94            vnet_main_t * vnet_main,
95            u32 cpu_index,
96            vlib_buffer_t * b0,
97            u32 bvi_sw_if_index)
98 {
99   // Set the input interface to be the BVI interface
100   vnet_buffer(b0)->sw_if_index[VLIB_RX] = bvi_sw_if_index;
101   vnet_buffer(b0)->sw_if_index[VLIB_TX] = ~0;
102
103   // Update l2_len in packet which is expected by l2 path, 
104   // including l2 tag push/pop code on output
105   vnet_update_l2_len(b0);
106
107   // increment BVI TX interface stat
108   vlib_increment_combined_counter 
109       (vnet_main->interface_main.combined_sw_if_counters
110        + VNET_INTERFACE_COUNTER_TX,
111        cpu_index,
112        bvi_sw_if_index,
113        1,
114        vlib_buffer_length_in_chain (vlib_main, b0));
115 }
116
117
118 void
119 l2bvi_register_input_type (vlib_main_t * vm,
120                            ethernet_type_t type,
121                            u32 node_index);
122 #endif