gso: add vxlan tunnel support
[vpp.git] / src / vnet / gso / hdr_offset_parser.h
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef included_hdr_offset_parser_h
17 #define included_hdr_offset_parser_h
18
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/ip/ip4_packet.h>
21 #include <vnet/ip/ip6_packet.h>
22 #include <vnet/udp/udp.h>
23 #include <vnet/udp/udp_packet.h>
24 #include <vnet/vnet.h>
25 #include <vnet/vxlan/vxlan_packet.h>
26
27 #define foreach_gho_flag        \
28   _( 0, IP4)                    \
29   _( 1, IP6)                    \
30   _( 2, TCP)                    \
31   _( 3, UDP)                    \
32   _( 4, OUTER_IP4)              \
33   _( 5, OUTER_IP6)              \
34   _( 6, OUTER_TCP)              \
35   _( 7, OUTER_UDP)              \
36   _( 8, VXLAN_TUNNEL)           \
37   _( 9, GRE_TUNNEL)             \
38   _( 10, IPIP_TUNNEL)           \
39   _( 11, GENEVE_TUNNEL)
40
41 typedef enum gho_flag_t_
42 {
43 #define _(bit, name) GHO_F_##name  = (1 << bit),
44   foreach_gho_flag
45 #undef _
46 } gho_flag_t;
47
48 #define GHO_F_TUNNEL (GHO_F_VXLAN_TUNNEL |  \
49                       GHO_F_GENEVE_TUNNEL | \
50                       GHO_F_IPIP_TUNNEL |   \
51                       GHO_F_GRE_TUNNEL)
52
53 #define GHO_F_OUTER_HDR (GHO_F_OUTER_IP4 | \
54                          GHO_F_OUTER_IP6 | \
55                          GHO_F_OUTER_TCP | \
56                          GHO_F_OUTER_UDP)
57
58 #define GHO_F_INNER_HDR (GHO_F_IP4 | \
59                          GHO_F_IP6 | \
60                          GHO_F_UDP | \
61                          GHO_F_TCP)
62
63 typedef struct
64 {
65   i16 outer_l2_hdr_offset;
66   i16 outer_l3_hdr_offset;
67   i16 outer_l4_hdr_offset;
68   u16 outer_l4_hdr_sz;
69   u16 outer_hdr_sz;
70   i16 l2_hdr_offset;
71   i16 l3_hdr_offset;
72   i16 l4_hdr_offset;
73   u16 l4_hdr_sz;
74   u16 hdr_sz;
75   gho_flag_t gho_flags;
76 } generic_header_offset_t;
77
78 static_always_inline u8 *
79 format_generic_header_offset (u8 * s, va_list * args)
80 {
81   generic_header_offset_t *gho = va_arg (*args, generic_header_offset_t *);
82
83   s = format (s, "\n\t");
84   if (gho->gho_flags & GHO_F_TUNNEL)
85     {
86       if (gho->gho_flags & GHO_F_VXLAN_TUNNEL)
87         s = format (s, "vxlan-tunnel ");
88       else if (gho->gho_flags & GHO_F_IPIP_TUNNEL)
89         s = format (s, "ipip-tunnel ");
90       else if (gho->gho_flags & GHO_F_GRE_TUNNEL)
91         s = format (s, "gre-tunnel ");
92       else if (gho->gho_flags & GHO_F_GENEVE_TUNNEL)
93         s = format (s, "geneve-tunnel ");
94
95       if (gho->gho_flags & GHO_F_OUTER_IP4)
96         s = format (s, "outer-ipv4 ");
97       else if (gho->gho_flags & GHO_F_OUTER_IP6)
98         s = format (s, "outer-ipv6 ");
99
100       if (gho->gho_flags & GHO_F_OUTER_UDP)
101         s = format (s, "outer-udp ");
102       else if (gho->gho_flags & GHO_F_OUTER_TCP)
103         s = format (s, "outer-tcp ");
104
105       s = format (s, "outer-hdr-sz %u outer-l2-hdr-offset %d "
106                   "outer-l3-hdr-offset %d outer-l4-hdr-offset %d "
107                   "outer-l4-hdr-sz %u\n\t",
108                   gho->outer_hdr_sz, gho->outer_l2_hdr_offset,
109                   gho->outer_l3_hdr_offset, gho->outer_l4_hdr_offset,
110                   gho->outer_l4_hdr_sz);
111     }
112
113   if (gho->gho_flags & GHO_F_IP4)
114     s = format (s, "ipv4 ");
115   else if (gho->gho_flags & GHO_F_IP6)
116     s = format (s, "ipv6 ");
117
118   if (gho->gho_flags & GHO_F_TCP)
119     s = format (s, "tcp ");
120   else if (gho->gho_flags & GHO_F_UDP)
121     s = format (s, "udp ");
122
123   s = format (s, "hdr-sz %u l2-hdr-offset %d "
124               "l3-hdr-offset %d l4-hdr-offset %d "
125               "l4-hdr-sz %u",
126               gho->hdr_sz, gho->l2_hdr_offset, gho->l3_hdr_offset,
127               gho->l4_hdr_offset, gho->l4_hdr_sz);
128
129   return s;
130 }
131
132 static_always_inline void
133 vnet_get_inner_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
134 {
135   if ((gho->gho_flags & GHO_F_TUNNEL)
136       && (gho->gho_flags & GHO_F_OUTER_HDR)
137       && (b0->current_data == gho->outer_l2_hdr_offset))
138     vlib_buffer_advance (b0, gho->outer_hdr_sz);
139 }
140
141 static_always_inline void
142 vnet_get_outer_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
143 {
144   if ((gho->gho_flags & GHO_F_TUNNEL)
145       && (gho->gho_flags & GHO_F_OUTER_HDR)
146       && (b0->current_data == gho->l2_hdr_offset))
147     vlib_buffer_advance (b0, -gho->outer_hdr_sz);
148 }
149
150 static_always_inline void
151 vnet_geneve_inner_header_parser_inline (vlib_buffer_t * b0,
152                                         generic_header_offset_t * gho)
153 {
154   /* not supported yet */
155   if ((gho->gho_flags & GHO_F_GENEVE_TUNNEL) == 0)
156     return;
157
158   ASSERT (0);
159 }
160
161 static_always_inline void
162 vnet_gre_inner_header_parser_inline (vlib_buffer_t * b0,
163                                      generic_header_offset_t * gho)
164 {
165   /* not supported yet */
166   if ((gho->gho_flags & GHO_F_GRE_TUNNEL) == 0)
167     return;
168
169   ASSERT (0);
170 }
171
172 static_always_inline void
173 vnet_ipip_inner_header_parser_inline (vlib_buffer_t * b0,
174                                       generic_header_offset_t * gho)
175 {
176   /* not supported yet */
177   if ((gho->gho_flags & GHO_F_IPIP_TUNNEL) == 0)
178     return;
179
180   ASSERT (0);
181 }
182
183 static_always_inline void
184 vnet_vxlan_inner_header_parser_inline (vlib_buffer_t * b0,
185                                        generic_header_offset_t * gho)
186 {
187   u8 l4_proto = 0;
188   u8 l4_hdr_sz = 0;
189
190   if ((gho->gho_flags & GHO_F_VXLAN_TUNNEL) == 0)
191     return;
192
193   gho->outer_l2_hdr_offset = gho->l2_hdr_offset;
194   gho->outer_l3_hdr_offset = gho->l3_hdr_offset;
195   gho->outer_l4_hdr_offset = gho->l4_hdr_offset;
196   gho->outer_l4_hdr_sz = gho->l4_hdr_sz;
197   gho->outer_hdr_sz = gho->hdr_sz;
198
199   gho->l2_hdr_offset = 0;
200   gho->l3_hdr_offset = 0;
201   gho->l4_hdr_offset = 0;
202   gho->l4_hdr_sz = 0;
203   gho->hdr_sz = 0;
204
205   if (gho->gho_flags & GHO_F_IP4)
206     {
207       gho->gho_flags |= GHO_F_OUTER_IP4;
208     }
209   else if (gho->gho_flags & GHO_F_IP6)
210     {
211       gho->gho_flags |= GHO_F_OUTER_IP6;
212     }
213
214   if (gho->gho_flags & GHO_F_UDP)
215     {
216       gho->gho_flags |= GHO_F_OUTER_UDP;
217     }
218
219   gho->gho_flags &= ~GHO_F_INNER_HDR;
220
221   vnet_get_inner_header (b0, gho);
222
223   gho->l2_hdr_offset = b0->current_data;
224
225   ethernet_header_t *eh = (ethernet_header_t *) vlib_buffer_get_current (b0);
226   u16 ethertype = clib_net_to_host_u16 (eh->type);
227   u16 l2hdr_sz = sizeof (ethernet_header_t);
228
229   if (ethernet_frame_is_tagged (ethertype))
230     {
231       ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
232
233       ethertype = clib_net_to_host_u16 (vlan->type);
234       l2hdr_sz += sizeof (*vlan);
235       if (ethertype == ETHERNET_TYPE_VLAN)
236         {
237           vlan++;
238           ethertype = clib_net_to_host_u16 (vlan->type);
239           l2hdr_sz += sizeof (*vlan);
240         }
241     }
242
243   gho->l3_hdr_offset = l2hdr_sz;
244
245   if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
246     {
247       ip4_header_t *ip4 =
248         (ip4_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
249       gho->l4_hdr_offset = gho->l3_hdr_offset + ip4_header_bytes (ip4);
250       l4_proto = ip4->protocol;
251       gho->gho_flags |= GHO_F_IP4;
252     }
253   else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
254     {
255       ip6_header_t *ip6 =
256         (ip6_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
257       /* FIXME IPv6 EH traversal */
258       gho->l4_hdr_offset = gho->l3_hdr_offset + sizeof (ip6_header_t);
259       l4_proto = ip6->protocol;
260       gho->gho_flags |= GHO_F_IP6;
261     }
262   if (l4_proto == IP_PROTOCOL_TCP)
263     {
264       tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
265                                             gho->l4_hdr_offset);
266       l4_hdr_sz = tcp_header_bytes (tcp);
267
268       gho->gho_flags |= GHO_F_TCP;
269
270     }
271   else if (l4_proto == IP_PROTOCOL_UDP)
272     {
273       udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
274                                             gho->l4_hdr_offset);
275       l4_hdr_sz = sizeof (*udp);
276
277       gho->gho_flags |= GHO_F_UDP;
278     }
279
280   gho->l4_hdr_sz = l4_hdr_sz;
281   gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
282
283   vnet_get_outer_header (b0, gho);
284 }
285
286 static_always_inline void
287 vnet_generic_inner_header_parser_inline (vlib_buffer_t * b0,
288                                          generic_header_offset_t * gho)
289 {
290
291   if (gho->gho_flags & GHO_F_VXLAN_TUNNEL)
292     vnet_vxlan_inner_header_parser_inline (b0, gho);
293   else if (gho->gho_flags & GHO_F_IPIP_TUNNEL)
294     vnet_ipip_inner_header_parser_inline (b0, gho);
295   else if (gho->gho_flags & GHO_F_GRE_TUNNEL)
296     vnet_gre_inner_header_parser_inline (b0, gho);
297   else if (gho->gho_flags & GHO_F_GENEVE_TUNNEL)
298     vnet_geneve_inner_header_parser_inline (b0, gho);
299 }
300
301 static_always_inline void
302 vnet_generic_outer_header_parser_inline (vlib_buffer_t * b0,
303                                          generic_header_offset_t * gho)
304 {
305   u8 l4_proto = 0;
306   u8 l4_hdr_sz = 0;
307
308   ethernet_header_t *eh = (ethernet_header_t *) vlib_buffer_get_current (b0);
309   u16 ethertype = clib_net_to_host_u16 (eh->type);
310   u16 l2hdr_sz = sizeof (ethernet_header_t);
311
312   if (ethernet_frame_is_tagged (ethertype))
313     {
314       ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
315
316       ethertype = clib_net_to_host_u16 (vlan->type);
317       l2hdr_sz += sizeof (*vlan);
318       if (ethertype == ETHERNET_TYPE_VLAN)
319         {
320           vlan++;
321           ethertype = clib_net_to_host_u16 (vlan->type);
322           l2hdr_sz += sizeof (*vlan);
323         }
324     }
325
326   gho->l2_hdr_offset = b0->current_data;
327   gho->l3_hdr_offset = l2hdr_sz;
328
329   if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
330     {
331       ip4_header_t *ip4 =
332         (ip4_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
333       gho->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
334       l4_proto = ip4->protocol;
335       gho->gho_flags |= GHO_F_IP4;
336     }
337   else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
338     {
339       ip6_header_t *ip6 =
340         (ip6_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
341       /* FIXME IPv6 EH traversal */
342       gho->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
343       l4_proto = ip6->protocol;
344       gho->gho_flags |= GHO_F_IP6;
345     }
346   if (l4_proto == IP_PROTOCOL_TCP)
347     {
348       tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
349                                             gho->l4_hdr_offset);
350       l4_hdr_sz = tcp_header_bytes (tcp);
351
352       gho->gho_flags |= GHO_F_TCP;
353     }
354   else if (l4_proto == IP_PROTOCOL_UDP)
355     {
356       udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
357                                             gho->l4_hdr_offset);
358       l4_hdr_sz = sizeof (*udp);
359
360       gho->gho_flags |= GHO_F_UDP;
361
362       if (UDP_DST_PORT_vxlan == clib_net_to_host_u16 (udp->dst_port))
363         {
364           gho->gho_flags |= GHO_F_VXLAN_TUNNEL;
365           gho->hdr_sz += sizeof (vxlan_header_t);
366         }
367       else if (UDP_DST_PORT_geneve == clib_net_to_host_u16 (udp->dst_port))
368         {
369           gho->gho_flags |= GHO_F_GENEVE_TUNNEL;
370         }
371     }
372   else if ((l4_proto == IP_PROTOCOL_IP_IN_IP)
373            || (l4_proto == IP_PROTOCOL_IPV6))
374     {
375       l4_hdr_sz = 0;
376       gho->gho_flags |= GHO_F_IPIP_TUNNEL;
377     }
378   else if (l4_proto == IP_PROTOCOL_GRE)
379     {
380       l4_hdr_sz = 0;
381       gho->gho_flags |= GHO_F_GRE_TUNNEL;
382     }
383
384   gho->l4_hdr_sz = l4_hdr_sz;
385   gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
386 }
387
388 static_always_inline void
389 vnet_generic_header_offset_parser (vlib_buffer_t * b0,
390                                    generic_header_offset_t * gho)
391 {
392   vnet_generic_outer_header_parser_inline (b0, gho);
393
394   if (gho->gho_flags & GHO_F_TUNNEL)
395     {
396       vnet_generic_inner_header_parser_inline (b0, gho);
397     }
398 }
399
400 #endif /* included_hdr_offset_parser_h */
401
402 /*
403  * fd.io coding-style-patch-verification: ON
404  *
405  * Local Variables:
406  * eval: (c-set-style "gnu")
407  * End:
408  */