l2-rw: Packet rewrite feature for L2 bridges
[vpp.git] / vnet / vnet / l2 / l2_input.h
1 /*
2  * l2_input.h : layer 2 input packet processing
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_vnet_l2_input_h
19 #define included_vnet_l2_input_h
20
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vnet/l2/l2_bd.h>
24 #include <vnet/ethernet/packet.h>
25 #include <vnet/ip/ip.h>
26
27 // Per-subinterface L2 feature configuration
28
29 typedef struct {
30
31   union {
32     u16 bd_index;                 // bridge domain id
33     u32 output_sw_if_index;    // for xconnect
34   };
35
36   // Interface mode. If both are 0, this interface is in L3 mode
37   u8  xconnect;
38   u8  bridge;
39
40   // this is the bvi interface for the bridge-domain
41   u8  bvi;  
42
43   // config for which input features are configured on this interface
44   u32 feature_bitmap;
45
46   // some of these flags are also in the feature bitmap
47   u8  learn_enable;  
48   u8  fwd_enable;    
49   u8  flood_enable;
50
51   // split horizon group
52   u8  shg;
53
54 } l2_input_config_t;
55
56
57 typedef struct {
58
59   // Next nodes for the feature bitmap
60   u32 feat_next_node_index[32];
61
62   /* config vector indexed by sw_if_index */
63   l2_input_config_t *configs;
64
65   /* bridge domain config vector indexed by BD ID */
66   l2_bridge_domain_t *bd_configs;
67
68   /* convenience variables */
69   vlib_main_t * vlib_main;
70   vnet_main_t * vnet_main;
71 } l2input_main_t;
72
73 extern l2input_main_t l2input_main;
74
75 static_always_inline l2_bridge_domain_t * 
76 l2input_bd_config_from_index (l2input_main_t * l2im, u32 bd_index)
77 {
78     l2_bridge_domain_t * bd_config;
79
80     bd_config = vec_elt_at_index (l2im->bd_configs, bd_index);
81     return bd_is_valid (bd_config) ? bd_config : NULL;
82 }
83
84 // L2 input features
85
86 // Mappings from feature ID to graph node name
87 #define foreach_l2input_feat \
88  _(DROP,          "feature-bitmap-drop")        \
89  _(CLASSIFY,      "l2-classify")                \
90  _(XCONNECT,      "l2-output")                  \
91  _(IPIW,          "feature-bitmap-drop")        \
92  _(FLOOD,         "l2-flood")                   \
93  _(ARP_TERM,      "arp-term-l2bd")              \
94  _(UU_FLOOD,      "l2-flood")                   \
95  _(FWD,           "l2-fwd")                     \
96  _(RW,            "l2-rw")                      \
97  _(LEARN,         "l2-learn")                   \
98  _(VTR,           "l2-input-vtr")               \
99  _(VPATH,         "vpath-input-l2")             \
100  _(CTRL_PKT,      "feature-bitmap-drop")        \
101  _(L2PT,          "feature-bitmap-drop")        \
102  _(IGMP_SNOOP,    "feature-bitmap-drop")        \
103  _(MLD_SNOOP,     "feature-bitmap-drop")        \
104  _(DHCP_SNOOP,    "feature-bitmap-drop")        \
105  _(DAI,           "feature-bitmap-drop")        \
106  _(IPSG,          "feature-bitmap-drop")        \
107  _(ACL,           "l2-input-acl")               \
108  _(QOS,           "feature-bitmap-drop")        \
109  _(CFM,           "feature-bitmap-drop")        \
110  _(SPAN,          "feature-bitmap-drop")
111
112 // Feature bitmap positions
113 typedef enum {
114 #define _(sym,str) L2INPUT_FEAT_##sym##_BIT,
115   foreach_l2input_feat
116 #undef _
117   L2INPUT_N_FEAT,
118 } l2input_feat_t;
119
120 // Feature bit masks
121 typedef enum {
122 #define _(sym,str) L2INPUT_FEAT_##sym = (1<<L2INPUT_FEAT_##sym##_BIT),
123   foreach_l2input_feat
124 #undef _
125 } l2input_feat_masks_t;
126            
127 // Return an array of strings containing graph node names of each feature
128 char **l2input_get_feat_names(void);
129
130
131 static_always_inline u8 bd_feature_flood (l2_bridge_domain_t * bd_config)
132 {
133     return ((bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD) ==
134             L2INPUT_FEAT_FLOOD);
135 }
136
137 static_always_inline u8 bd_feature_uu_flood (l2_bridge_domain_t * bd_config)
138 {
139     return ((bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD) ==
140             L2INPUT_FEAT_UU_FLOOD);
141 }
142
143 static_always_inline u8 bd_feature_forward (l2_bridge_domain_t * bd_config)
144 {
145     return ((bd_config->feature_bitmap & L2INPUT_FEAT_FWD) ==
146             L2INPUT_FEAT_FWD);
147 }
148
149 static_always_inline u8 bd_feature_learn (l2_bridge_domain_t * bd_config)
150 {
151     return ((bd_config->feature_bitmap & L2INPUT_FEAT_LEARN) ==
152             L2INPUT_FEAT_LEARN);
153 }
154
155 static_always_inline u8 bd_feature_arp_term (l2_bridge_domain_t * bd_config)
156 {
157     return ((bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM) ==
158             L2INPUT_FEAT_ARP_TERM);
159 }
160
161 // Masks for eliminating features that do not apply to a packet
162
163 #define IP4_FEAT_MASK  ~(L2INPUT_FEAT_CTRL_PKT    | \
164                          L2INPUT_FEAT_MLD_SNOOP   | \
165                          L2INPUT_FEAT_L2PT        | \
166                          L2INPUT_FEAT_CFM         | \
167                          L2INPUT_FEAT_DAI)
168
169 #define IP6_FEAT_MASK  ~(L2INPUT_FEAT_CTRL_PKT    | \
170                          L2INPUT_FEAT_IGMP_SNOOP  | \
171                          L2INPUT_FEAT_L2PT        | \
172                          L2INPUT_FEAT_CFM         | \
173                          L2INPUT_FEAT_DAI)
174
175 #define IP_UDP_TCP_FEAT_MASK  ~(L2INPUT_FEAT_CTRL_PKT   | \
176                                 L2INPUT_FEAT_L2PT       | \
177                                 L2INPUT_FEAT_IGMP_SNOOP | \
178                                 L2INPUT_FEAT_MLD_SNOOP  | \
179                                 L2INPUT_FEAT_DHCP_SNOOP | \
180                                 L2INPUT_FEAT_CFM        | \
181                                 L2INPUT_FEAT_DAI)
182
183 #define MPLS_FEAT_MASK  ~(L2INPUT_FEAT_CTRL_PKT   | \
184                           L2INPUT_FEAT_L2PT       | \
185                           L2INPUT_FEAT_IGMP_SNOOP | \
186                           L2INPUT_FEAT_MLD_SNOOP  | \
187                           L2INPUT_FEAT_DHCP_SNOOP | \
188                           L2INPUT_FEAT_CFM        | \
189                           L2INPUT_FEAT_DAI)
190
191
192 // Get a pointer to the config for the given interface
193 l2_input_config_t * l2input_intf_config (u32 sw_if_index);
194
195 // Enable (or disable) the feature in the bitmap for the given interface
196 u32 l2input_intf_bitmap_enable (u32 sw_if_index,
197                                  u32 feature_bitmap,
198                                  u32 enable);
199
200 //Sets modifies flags from a bridge domain
201 u32 l2input_set_bridge_features(u32 bd_index,
202                                 u32 feat_mask,
203                                 u32 feat_value);
204
205
206 #define MODE_L3        0
207 #define MODE_L2_BRIDGE 1
208 #define MODE_L2_XC     2
209 #define MODE_L2_CLASSIFY 3
210
211 #define MODE_ERROR_ETH        1
212 #define MODE_ERROR_BVI_DEF    2
213
214 u32 set_int_l2_mode (vlib_main_t * vm,
215                      vnet_main_t * vnet_main,
216                      u32 mode,
217                      u32 sw_if_index,
218                      u32 bd_index,
219                      u32 bvi,  
220                      u32 shg,   
221                      u32 xc_sw_if_index);
222
223 static inline void
224 vnet_update_l2_len (vlib_buffer_t * b)
225 {
226   ethernet_header_t * eth;
227   u16 ethertype;
228
229   /* point at currrent l2 hdr */
230   eth = vlib_buffer_get_current (b);
231
232   /* 
233    * l2-output pays no attention to this
234    * but the tag push/pop code on an l2 subif needs it.
235    * 
236    * Determine l2 header len, check for up to 2 vlans 
237    */
238   vnet_buffer(b)->l2.l2_len = sizeof(ethernet_header_t);
239   ethertype = clib_net_to_host_u16(eth->type);
240   if ((ethertype == ETHERNET_TYPE_VLAN) ||
241       (ethertype == ETHERNET_TYPE_DOT1AD) ||
242       (ethertype == ETHERNET_TYPE_VLAN_9100) ||
243       (ethertype == ETHERNET_TYPE_VLAN_9200)) {    
244     ethernet_vlan_header_t * vlan;
245     vnet_buffer(b)->l2.l2_len += sizeof (*vlan);
246     vlan = (void *) (eth+1);
247     ethertype = clib_net_to_host_u16 (vlan->type);
248     if (ethertype == ETHERNET_TYPE_VLAN) {
249       vnet_buffer(b)->l2.l2_len += sizeof (*vlan);
250     }
251   }
252 }
253
254 /*
255  * Compute flow hash of an ethernet packet, use 5-tuple hash if L3 packet 
256  * is ip4 or ip6. Otherwise hash on smac/dmac/etype. 
257  * The vlib buffer current pointer is expected to be at ethernet header 
258  * and vnet l2.l2_len is exppected to be setup already.
259  */
260 static inline u32 vnet_l2_compute_flow_hash (vlib_buffer_t *b) 
261 {
262   ethernet_header_t * eh = vlib_buffer_get_current(b);
263   u8 * l3h = (u8 *)eh + vnet_buffer(b)->l2.l2_len;
264   u16 ethertype = clib_net_to_host_u16(*(u16 *)(l3h - 2));
265
266   if (ethertype == ETHERNET_TYPE_IP4) 
267     return ip4_compute_flow_hash((ip4_header_t *) l3h, IP_FLOW_HASH_DEFAULT);
268   else if (ethertype == ETHERNET_TYPE_IP6) 
269     return ip6_compute_flow_hash((ip6_header_t *) l3h, IP_FLOW_HASH_DEFAULT);
270   else
271     {
272       u32 a, b, c;
273       u32 * ap = (u32 *) &eh->dst_address[2];
274       u32 * bp = (u32 *) &eh->src_address[2];
275       a = * ap;
276       b = * bp;
277       c = ethertype;
278       hash_v3_mix32 (a, b, c);
279       hash_v3_finalize32 (a, b, c);
280       return c;
281     }
282 }
283
284 #endif
285