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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_hdr_offset_parser_h
17 #define included_hdr_offset_parser_h
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>
27 #define foreach_gho_flag \
39 _( 11, IPIP6_TUNNEL) \
42 typedef enum gho_flag_t_
44 #define _(bit, name) GHO_F_##name = (1 << bit),
49 #define GHO_F_TUNNEL (GHO_F_VXLAN_TUNNEL | \
50 GHO_F_GENEVE_TUNNEL | \
52 GHO_F_IPIP6_TUNNEL | \
55 #define GHO_F_OUTER_HDR (GHO_F_OUTER_IP4 | \
60 #define GHO_F_INNER_HDR (GHO_F_IP4 | \
67 i16 outer_l2_hdr_offset;
68 i16 outer_l3_hdr_offset;
69 i16 outer_l4_hdr_offset;
78 } generic_header_offset_t;
80 static_always_inline u8 *
81 format_generic_header_offset (u8 * s, va_list * args)
83 generic_header_offset_t *gho = va_arg (*args, generic_header_offset_t *);
85 s = format (s, "\n\t");
86 if (gho->gho_flags & GHO_F_TUNNEL)
88 if (gho->gho_flags & GHO_F_VXLAN_TUNNEL)
89 s = format (s, "vxlan-tunnel ");
90 else if (gho->gho_flags & GHO_F_IPIP_TUNNEL)
91 s = format (s, "ipip-tunnel ");
92 else if (gho->gho_flags & GHO_F_GRE_TUNNEL)
93 s = format (s, "gre-tunnel ");
94 else if (gho->gho_flags & GHO_F_GENEVE_TUNNEL)
95 s = format (s, "geneve-tunnel ");
97 if (gho->gho_flags & GHO_F_OUTER_IP4)
98 s = format (s, "outer-ipv4 ");
99 else if (gho->gho_flags & GHO_F_OUTER_IP6)
100 s = format (s, "outer-ipv6 ");
102 if (gho->gho_flags & GHO_F_OUTER_UDP)
103 s = format (s, "outer-udp ");
104 else if (gho->gho_flags & GHO_F_OUTER_TCP)
105 s = format (s, "outer-tcp ");
107 s = format (s, "outer-hdr-sz %u outer-l2-hdr-offset %d "
108 "outer-l3-hdr-offset %d outer-l4-hdr-offset %d "
109 "outer-l4-hdr-sz %u\n\t",
110 gho->outer_hdr_sz, gho->outer_l2_hdr_offset,
111 gho->outer_l3_hdr_offset, gho->outer_l4_hdr_offset,
112 gho->outer_l4_hdr_sz);
115 if (gho->gho_flags & GHO_F_IP4)
116 s = format (s, "ipv4 ");
117 else if (gho->gho_flags & GHO_F_IP6)
118 s = format (s, "ipv6 ");
120 if (gho->gho_flags & GHO_F_TCP)
121 s = format (s, "tcp ");
122 else if (gho->gho_flags & GHO_F_UDP)
123 s = format (s, "udp ");
125 s = format (s, "hdr-sz %u l2-hdr-offset %d "
126 "l3-hdr-offset %d l4-hdr-offset %d "
128 gho->hdr_sz, gho->l2_hdr_offset, gho->l3_hdr_offset,
129 gho->l4_hdr_offset, gho->l4_hdr_sz);
134 static_always_inline void
135 vnet_get_inner_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
137 if ((gho->gho_flags & GHO_F_TUNNEL)
138 && (gho->gho_flags & GHO_F_OUTER_HDR)
139 && (b0->current_data == gho->outer_l2_hdr_offset))
140 vlib_buffer_advance (b0, gho->outer_hdr_sz);
143 static_always_inline void
144 vnet_get_outer_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
146 if ((gho->gho_flags & GHO_F_TUNNEL)
147 && (gho->gho_flags & GHO_F_OUTER_HDR)
148 && (b0->current_data == gho->l2_hdr_offset))
149 vlib_buffer_advance (b0, -gho->outer_hdr_sz);
152 static_always_inline void
153 vnet_geneve_inner_header_parser_inline (vlib_buffer_t * b0,
154 generic_header_offset_t * gho)
156 /* not supported yet */
157 if ((gho->gho_flags & GHO_F_GENEVE_TUNNEL) == 0)
163 static_always_inline void
164 vnet_gre_inner_header_parser_inline (vlib_buffer_t * b0,
165 generic_header_offset_t * gho)
167 /* not supported yet */
168 if ((gho->gho_flags & GHO_F_GRE_TUNNEL) == 0)
174 static_always_inline void
175 vnet_ipip_inner_header_parser_inline (vlib_buffer_t * b0,
176 generic_header_offset_t * gho)
178 if ((gho->gho_flags & (GHO_F_IPIP_TUNNEL | GHO_F_IPIP6_TUNNEL)) == 0)
184 gho->outer_l2_hdr_offset = gho->l2_hdr_offset;
185 gho->outer_l3_hdr_offset = gho->l3_hdr_offset;
186 gho->outer_l4_hdr_offset = gho->l4_hdr_offset;
187 gho->outer_l4_hdr_sz = gho->l4_hdr_sz;
188 gho->outer_hdr_sz = gho->hdr_sz;
190 gho->l2_hdr_offset = 0;
191 gho->l3_hdr_offset = 0;
192 gho->l4_hdr_offset = 0;
196 if (gho->gho_flags & GHO_F_IP4)
198 gho->gho_flags |= GHO_F_OUTER_IP4;
200 else if (gho->gho_flags & GHO_F_IP6)
202 gho->gho_flags |= GHO_F_OUTER_IP6;
205 gho->gho_flags &= ~GHO_F_INNER_HDR;
207 vnet_get_inner_header (b0, gho);
209 gho->l2_hdr_offset = b0->current_data;
210 gho->l3_hdr_offset = 0;
212 if (PREDICT_TRUE (gho->gho_flags & GHO_F_IPIP_TUNNEL))
214 ip4_header_t *ip4 = (ip4_header_t *) vlib_buffer_get_current (b0);
215 gho->l4_hdr_offset = ip4_header_bytes (ip4);
216 l4_proto = ip4->protocol;
217 gho->gho_flags |= GHO_F_IP4;
219 else if (PREDICT_TRUE (gho->gho_flags & GHO_F_IPIP6_TUNNEL))
221 ip6_header_t *ip6 = (ip6_header_t *) vlib_buffer_get_current (b0);
222 /* FIXME IPv6 EH traversal */
223 gho->l4_hdr_offset = sizeof (ip6_header_t);
224 l4_proto = ip6->protocol;
225 gho->gho_flags |= GHO_F_IP6;
227 if (l4_proto == IP_PROTOCOL_TCP)
229 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
231 l4_hdr_sz = tcp_header_bytes (tcp);
233 gho->gho_flags |= GHO_F_TCP;
236 else if (l4_proto == IP_PROTOCOL_UDP)
238 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
240 l4_hdr_sz = sizeof (*udp);
242 gho->gho_flags |= GHO_F_UDP;
245 gho->l4_hdr_sz = l4_hdr_sz;
246 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
248 vnet_get_outer_header (b0, gho);
251 static_always_inline void
252 vnet_vxlan_inner_header_parser_inline (vlib_buffer_t * b0,
253 generic_header_offset_t * gho)
258 if ((gho->gho_flags & GHO_F_VXLAN_TUNNEL) == 0)
261 gho->outer_l2_hdr_offset = gho->l2_hdr_offset;
262 gho->outer_l3_hdr_offset = gho->l3_hdr_offset;
263 gho->outer_l4_hdr_offset = gho->l4_hdr_offset;
264 gho->outer_l4_hdr_sz = gho->l4_hdr_sz;
265 gho->outer_hdr_sz = gho->hdr_sz;
267 gho->l2_hdr_offset = 0;
268 gho->l3_hdr_offset = 0;
269 gho->l4_hdr_offset = 0;
273 if (gho->gho_flags & GHO_F_IP4)
275 gho->gho_flags |= GHO_F_OUTER_IP4;
277 else if (gho->gho_flags & GHO_F_IP6)
279 gho->gho_flags |= GHO_F_OUTER_IP6;
282 if (gho->gho_flags & GHO_F_UDP)
284 gho->gho_flags |= GHO_F_OUTER_UDP;
287 gho->gho_flags &= ~GHO_F_INNER_HDR;
289 vnet_get_inner_header (b0, gho);
291 gho->l2_hdr_offset = b0->current_data;
293 ethernet_header_t *eh = (ethernet_header_t *) vlib_buffer_get_current (b0);
294 u16 ethertype = clib_net_to_host_u16 (eh->type);
295 u16 l2hdr_sz = sizeof (ethernet_header_t);
297 if (ethernet_frame_is_tagged (ethertype))
299 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
301 ethertype = clib_net_to_host_u16 (vlan->type);
302 l2hdr_sz += sizeof (*vlan);
303 if (ethertype == ETHERNET_TYPE_VLAN)
306 ethertype = clib_net_to_host_u16 (vlan->type);
307 l2hdr_sz += sizeof (*vlan);
311 gho->l3_hdr_offset = l2hdr_sz;
313 if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
316 (ip4_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
317 gho->l4_hdr_offset = gho->l3_hdr_offset + ip4_header_bytes (ip4);
318 l4_proto = ip4->protocol;
319 gho->gho_flags |= GHO_F_IP4;
321 else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
324 (ip6_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
325 /* FIXME IPv6 EH traversal */
326 gho->l4_hdr_offset = gho->l3_hdr_offset + sizeof (ip6_header_t);
327 l4_proto = ip6->protocol;
328 gho->gho_flags |= GHO_F_IP6;
330 if (l4_proto == IP_PROTOCOL_TCP)
332 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
334 l4_hdr_sz = tcp_header_bytes (tcp);
336 gho->gho_flags |= GHO_F_TCP;
339 else if (l4_proto == IP_PROTOCOL_UDP)
341 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
343 l4_hdr_sz = sizeof (*udp);
345 gho->gho_flags |= GHO_F_UDP;
348 gho->l4_hdr_sz = l4_hdr_sz;
349 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
351 vnet_get_outer_header (b0, gho);
354 static_always_inline void
355 vnet_generic_inner_header_parser_inline (vlib_buffer_t * b0,
356 generic_header_offset_t * gho)
359 if (gho->gho_flags & GHO_F_VXLAN_TUNNEL)
360 vnet_vxlan_inner_header_parser_inline (b0, gho);
361 else if (gho->gho_flags & (GHO_F_IPIP_TUNNEL | GHO_F_IPIP6_TUNNEL))
362 vnet_ipip_inner_header_parser_inline (b0, gho);
363 else if (gho->gho_flags & GHO_F_GRE_TUNNEL)
364 vnet_gre_inner_header_parser_inline (b0, gho);
365 else if (gho->gho_flags & GHO_F_GENEVE_TUNNEL)
366 vnet_geneve_inner_header_parser_inline (b0, gho);
369 static_always_inline void
370 vnet_generic_outer_header_parser_inline (vlib_buffer_t * b0,
371 generic_header_offset_t * gho,
372 int is_l2, int is_ip4, int is_ip6)
379 ASSERT (!(is_ip4 && is_ip6));
383 ethernet_header_t *eh =
384 (ethernet_header_t *) vlib_buffer_get_current (b0);
385 ethertype = clib_net_to_host_u16 (eh->type);
386 l2hdr_sz = sizeof (ethernet_header_t);
388 if (ethernet_frame_is_tagged (ethertype))
390 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
392 ethertype = clib_net_to_host_u16 (vlan->type);
393 l2hdr_sz += sizeof (*vlan);
394 if (ethertype == ETHERNET_TYPE_VLAN)
397 ethertype = clib_net_to_host_u16 (vlan->type);
398 l2hdr_sz += sizeof (*vlan);
403 l2hdr_sz = vnet_buffer (b0)->ip.save_rewrite_length;
405 gho->l2_hdr_offset = b0->current_data;
406 gho->l3_hdr_offset = l2hdr_sz;
408 if (PREDICT_TRUE (is_ip4))
411 (ip4_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
412 gho->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
413 l4_proto = ip4->protocol;
414 gho->gho_flags |= GHO_F_IP4;
416 else if (PREDICT_TRUE (is_ip6))
419 (ip6_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
420 /* FIXME IPv6 EH traversal */
421 gho->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
422 l4_proto = ip6->protocol;
423 gho->gho_flags |= GHO_F_IP6;
425 if (l4_proto == IP_PROTOCOL_TCP)
427 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
429 l4_hdr_sz = tcp_header_bytes (tcp);
431 gho->gho_flags |= GHO_F_TCP;
433 else if (l4_proto == IP_PROTOCOL_UDP)
435 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
437 l4_hdr_sz = sizeof (*udp);
439 gho->gho_flags |= GHO_F_UDP;
441 if (UDP_DST_PORT_vxlan == clib_net_to_host_u16 (udp->dst_port))
443 gho->gho_flags |= GHO_F_VXLAN_TUNNEL;
444 gho->hdr_sz += sizeof (vxlan_header_t);
446 else if (UDP_DST_PORT_geneve == clib_net_to_host_u16 (udp->dst_port))
448 gho->gho_flags |= GHO_F_GENEVE_TUNNEL;
451 else if (l4_proto == IP_PROTOCOL_IP_IN_IP)
454 gho->gho_flags |= GHO_F_IPIP_TUNNEL;
456 else if (l4_proto == IP_PROTOCOL_IPV6)
459 gho->gho_flags |= GHO_F_IPIP6_TUNNEL;
461 else if (l4_proto == IP_PROTOCOL_GRE)
464 gho->gho_flags |= GHO_F_GRE_TUNNEL;
467 gho->l4_hdr_sz = l4_hdr_sz;
468 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
471 static_always_inline void
472 vnet_generic_header_offset_parser (vlib_buffer_t * b0,
473 generic_header_offset_t * gho, int is_l2,
474 int is_ip4, int is_ip6)
476 vnet_generic_outer_header_parser_inline (b0, gho, is_l2, is_ip4, is_ip6);
478 if (gho->gho_flags & GHO_F_TUNNEL)
480 vnet_generic_inner_header_parser_inline (b0, gho);
484 #endif /* included_hdr_offset_parser_h */
487 * fd.io coding-style-patch-verification: ON
490 * eval: (c-set-style "gnu")