BD ARP entry use common API types
[vpp.git] / src / vnet / l2 / l2_vtr.h
1 /*
2  * l2_vtr.h : layer 2 vlan tag rewrite 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_vtr_h
19 #define included_vnet_l2_vtr_h
20
21 #include <vlib/vlib.h>
22 #include <vnet/vnet.h>
23 #include <vnet/ethernet/packet.h>
24 #include <vnet/l2/l2_vtr.h>
25
26 /* VTR config options for API and CLI support */
27 typedef enum
28 {
29   L2_VTR_DISABLED,
30   L2_VTR_PUSH_1,
31   L2_VTR_PUSH_2,
32   L2_VTR_POP_1,
33   L2_VTR_POP_2,
34   L2_VTR_TRANSLATE_1_1,
35   L2_VTR_TRANSLATE_1_2,
36   L2_VTR_TRANSLATE_2_1,
37   L2_VTR_TRANSLATE_2_2
38 } l2_vtr_op_t;
39
40 /**
41  * Per-interface vlan tag rewrite configuration
42  * There will be one instance of this struct for each sw_if_index
43  * for both input vtr and output vtr
44  */
45 typedef struct
46 {
47   union
48   {
49     /*
50      * Up to two vlan tags to push.
51      * if there is only one vlan tag to push, it is in tags[1].
52      */
53     ethernet_vlan_header_tv_t tags[2];
54     u64 raw_tags;
55   };
56
57   union
58   {
59     struct
60     {
61       u8 push_bytes;            /* number of bytes to push for up to 2 vlans (0,4,8) */
62       u8 pop_bytes;             /* number of bytes to pop for up to 2 vlans (0,4,8) */
63     };
64     u16 push_and_pop_bytes;     /* if 0 then the feature is disabled */
65   };
66 } vtr_config_t;
67
68
69 /**
70  * Perform the configured tag rewrite on the packet.
71  * Return 0 if ok, 1 if packet should be dropped (e.g. tried to pop
72  * too many tags)
73  */
74 always_inline u32
75 l2_vtr_process (vlib_buffer_t * b0, vtr_config_t * config)
76 {
77   u64 temp_8;
78   u32 temp_4;
79   u8 *eth;
80
81   eth = vlib_buffer_get_current (b0);
82
83   /* copy the 12B dmac and smac to a temporary location */
84   temp_8 = *((u64 *) eth);
85   temp_4 = *((u32 *) (eth + 8));
86
87   /* adjust for popped tags */
88   eth += config->pop_bytes;
89
90   /* if not enough tags to pop then drop packet */
91   if (PREDICT_FALSE ((vnet_buffer (b0)->l2.l2_len - 12) < config->pop_bytes))
92     {
93       return 1;
94     }
95
96   /* copy the 2 new tags to the start of the packet  */
97   *((u64 *) (eth + 12 - 8)) = config->raw_tags;
98
99   /* TODO: set cos bits */
100
101   /* adjust for pushed tags: */
102   eth -= config->push_bytes;
103
104   /* copy the 12 dmac and smac back to the packet */
105   *((u64 *) eth) = temp_8;
106   *((u32 *) (eth + 8)) = temp_4;
107
108   /* Update l2 parameters */
109   vnet_buffer (b0)->l2.l2_len +=
110     (word) config->push_bytes - (word) config->pop_bytes;
111   vnet_buffer (b0)->l2_hdr_offset -=
112     (word) config->push_bytes - (word) config->pop_bytes;
113
114   /* Update vlan tag count */
115   ethernet_buffer_adjust_vlan_count_by_bytes (b0,
116                                               (word) config->push_bytes -
117                                               (word) config->pop_bytes);
118
119   /* Update packet len */
120   vlib_buffer_advance (b0,
121                        (word) config->pop_bytes - (word) config->push_bytes);
122
123   return 0;
124 }
125
126
127 /*
128  *  Perform the egress pre-vlan tag rewrite EFP Filter check.
129  * The post-vlan tag rewrite check is a separate graph node.
130  *
131  *  This check insures that a packet being output to an interface
132  * (before output vtr is performed) has vlan tags that match those
133  * on a packet received from that interface (after vtr has been performed).
134  * This means verifying that any tags pushed by input vtr are present
135  * on the packet.
136  *
137  *  Return 0 if ok, 1 if packet should be dropped.
138  * This function should be passed the input vtr config for the interface.
139  */
140 always_inline u8
141 l2_efp_filter_process (vlib_buffer_t * b0, vtr_config_t * in_config)
142 {
143   u8 *eth;
144   u64 packet_tags;
145   u64 tag_mask;
146
147   eth = vlib_buffer_get_current (b0);
148
149   /*
150    * If there are 2 tags pushed, they must match config->tags[0] and
151    * config->tags[1].
152    * If there is one tag pushed, it must match config->tag[1].
153    * If there are 0 tags pushed, the check passes.
154    */
155
156   /* mask for two vlan id and ethertypes, no cos bits */
157   tag_mask = clib_net_to_host_u64 (0xFFFF0FFFFFFF0FFF);
158   /* mask for one vlan id and ethertype, no cos bits */
159   tag_mask =
160     (in_config->push_bytes ==
161      4) ? clib_net_to_host_u64 (0xFFFF0FFF) : tag_mask;
162   /* mask for always match */
163   tag_mask = (in_config->push_bytes == 0) ? 0 : tag_mask;
164
165   /*
166    * Read 8B from the packet, getting the proper set of vlan tags
167    * For 0 push bytes, the address doesn't matter since the mask
168    * clears the data to 0.
169    */
170   packet_tags = *((u64 *) (eth + 4 + in_config->push_bytes));
171
172   /* Check if the packet tags match the configured tags */
173   return (packet_tags & tag_mask) != in_config->raw_tags;
174 }
175
176 typedef struct
177 {
178   union
179   {
180     ethernet_pbb_header_t macs_tags;
181     struct
182     {
183       u64 data1;
184       u64 data2;
185       u16 data3;
186       u32 data4;
187     } raw_data;
188   };
189   union
190   {
191     struct
192     {
193       u8 push_bytes;            /* number of bytes to push pbb tags */
194       u8 pop_bytes;             /* number of bytes to pop pbb tags */
195     };
196     u16 push_and_pop_bytes;     /* if 0 then the feature is disabled */
197   };
198 } ptr_config_t;
199
200 always_inline u32
201 l2_pbb_process (vlib_buffer_t * b0, ptr_config_t * config)
202 {
203   u8 *eth = vlib_buffer_get_current (b0);
204
205   if (config->pop_bytes > 0)
206     {
207       ethernet_pbb_header_packed_t *ph = (ethernet_pbb_header_packed_t *) eth;
208
209       // drop packet without PBB header or with wrong I-tag or B-tag
210       if (clib_net_to_host_u16 (ph->priority_dei_id) !=
211           clib_net_to_host_u16 (config->macs_tags.priority_dei_id)
212           || clib_net_to_host_u32 (ph->priority_dei_uca_res_sid) !=
213           clib_net_to_host_u32 (config->macs_tags.priority_dei_uca_res_sid))
214         return 1;
215
216       eth += config->pop_bytes;
217     }
218
219   if (config->push_bytes > 0)
220     {
221       eth -= config->push_bytes;
222       // copy the B-DA (6B), B-SA (6B), B-TAG (4B), I-TAG (6B)
223       *((u64 *) eth) = config->raw_data.data1;
224       *((u64 *) (eth + 8)) = config->raw_data.data2;
225       *((u16 *) (eth + 16)) = config->raw_data.data3;
226       *((u32 *) (eth + 18)) = config->raw_data.data4;
227     }
228
229   /* Update l2_len */
230   vnet_buffer (b0)->l2.l2_len +=
231     (word) config->push_bytes - (word) config->pop_bytes;
232   /* Update packet len */
233   vlib_buffer_advance (b0,
234                        (word) config->pop_bytes - (word) config->push_bytes);
235
236   return 0;
237 }
238
239 u32 l2pbb_configure (vlib_main_t * vlib_main,
240                      vnet_main_t * vnet_main, u32 sw_if_index, u32 vtr_op,
241                      u8 * b_dmac, u8 * b_smac,
242                      u16 b_vlanid, u32 i_sid, u16 vlan_outer_tag);
243
244 /**
245  * Configure vtag tag rewrite on the given interface.
246  * Return 1 if there is an error, 0 if ok
247  */
248 u32 l2vtr_configure (vlib_main_t * vlib_main,
249                      vnet_main_t * vnet_main,
250                      u32 sw_if_index,
251                      u32 vtr_op, u32 push_dot1q, u32 vtr_tag1, u32 vtr_tag2);
252
253 /**
254  * Get vtag tag rewrite on the given interface.
255  * Return 1 if there is an error, 0 if ok
256  */
257 u32 l2vtr_get (vlib_main_t * vlib_main,
258                vnet_main_t * vnet_main,
259                u32 sw_if_index,
260                u32 * vtr_op,
261                u32 * push_dot1q, u32 * vtr_tag1, u32 * vtr_tag2);
262
263 /**
264  * Get pbb tag rewrite on the given interface.
265  * Return 1 if there is an error, 0 if ok
266  */
267 u32 l2pbb_get (vlib_main_t * vlib_main,
268                vnet_main_t * vnet_main,
269                u32 sw_if_index,
270                u32 * vtr_op,
271                u16 * outer_tag,
272                ethernet_header_t * eth_hdr, u16 * b_vlanid, u32 * i_sid);
273
274 #endif /* included_vnet_l2_vtr_h */
275
276
277 /*
278  * fd.io coding-style-patch-verification: ON
279  *
280  * Local Variables:
281  * eval: (c-set-style "gnu")
282  * End:
283  */