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_local.h>
23 #include <vnet/udp/udp_packet.h>
24 #include <vnet/tcp/tcp_packet.h>
25 #include <vnet/vnet.h>
26 #include <vnet/vxlan/vxlan_packet.h>
28 #define foreach_gho_flag \
40 _( 11, IPIP6_TUNNEL) \
43 typedef enum gho_flag_t_
45 #define _(bit, name) GHO_F_##name = (1 << bit),
50 #define GHO_F_TUNNEL (GHO_F_VXLAN_TUNNEL | \
51 GHO_F_GENEVE_TUNNEL | \
53 GHO_F_IPIP6_TUNNEL | \
56 #define GHO_F_OUTER_HDR (GHO_F_OUTER_IP4 | \
61 #define GHO_F_INNER_HDR (GHO_F_IP4 | \
68 i16 outer_l2_hdr_offset;
69 i16 outer_l3_hdr_offset;
70 i16 outer_l4_hdr_offset;
79 } generic_header_offset_t;
81 static_always_inline u8 *
82 format_generic_header_offset (u8 * s, va_list * args)
84 generic_header_offset_t *gho = va_arg (*args, generic_header_offset_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)
161 static_always_inline void
162 vnet_gre_inner_header_parser_inline (vlib_buffer_t * b0,
163 generic_header_offset_t * gho)
165 /* not supported yet */
166 if ((gho->gho_flags & GHO_F_GRE_TUNNEL) == 0)
170 static_always_inline void
171 vnet_ipip_inner_header_parser_inline (vlib_buffer_t * b0,
172 generic_header_offset_t * gho)
174 if ((gho->gho_flags & (GHO_F_IPIP_TUNNEL | GHO_F_IPIP6_TUNNEL)) == 0)
180 gho->outer_l2_hdr_offset = gho->l2_hdr_offset;
181 gho->outer_l3_hdr_offset = gho->l3_hdr_offset;
182 gho->outer_l4_hdr_offset = gho->l4_hdr_offset;
183 gho->outer_l4_hdr_sz = gho->l4_hdr_sz;
184 gho->outer_hdr_sz = gho->hdr_sz;
186 gho->l2_hdr_offset = 0;
187 gho->l3_hdr_offset = 0;
188 gho->l4_hdr_offset = 0;
192 if (gho->gho_flags & GHO_F_IP4)
194 gho->gho_flags |= GHO_F_OUTER_IP4;
196 else if (gho->gho_flags & GHO_F_IP6)
198 gho->gho_flags |= GHO_F_OUTER_IP6;
201 gho->gho_flags &= ~GHO_F_INNER_HDR;
203 vnet_get_inner_header (b0, gho);
205 gho->l2_hdr_offset = b0->current_data;
206 gho->l3_hdr_offset = 0;
208 if (PREDICT_TRUE (gho->gho_flags & GHO_F_IPIP_TUNNEL))
210 ip4_header_t *ip4 = (ip4_header_t *) vlib_buffer_get_current (b0);
211 gho->l4_hdr_offset = ip4_header_bytes (ip4);
212 l4_proto = ip4->protocol;
213 gho->gho_flags |= GHO_F_IP4;
215 else if (PREDICT_TRUE (gho->gho_flags & GHO_F_IPIP6_TUNNEL))
217 ip6_header_t *ip6 = (ip6_header_t *) vlib_buffer_get_current (b0);
218 /* FIXME IPv6 EH traversal */
219 gho->l4_hdr_offset = sizeof (ip6_header_t);
220 l4_proto = ip6->protocol;
221 gho->gho_flags |= GHO_F_IP6;
223 if (l4_proto == IP_PROTOCOL_TCP)
225 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
227 l4_hdr_sz = tcp_header_bytes (tcp);
229 gho->gho_flags |= GHO_F_TCP;
232 else if (l4_proto == IP_PROTOCOL_UDP)
234 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
236 l4_hdr_sz = sizeof (*udp);
238 gho->gho_flags |= GHO_F_UDP;
241 gho->l4_hdr_sz = l4_hdr_sz;
242 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
244 vnet_get_outer_header (b0, gho);
247 static_always_inline void
248 vnet_vxlan_inner_header_parser_inline (vlib_buffer_t * b0,
249 generic_header_offset_t * gho)
254 if ((gho->gho_flags & GHO_F_VXLAN_TUNNEL) == 0)
257 gho->outer_l2_hdr_offset = gho->l2_hdr_offset;
258 gho->outer_l3_hdr_offset = gho->l3_hdr_offset;
259 gho->outer_l4_hdr_offset = gho->l4_hdr_offset;
260 gho->outer_l4_hdr_sz = gho->l4_hdr_sz;
261 gho->outer_hdr_sz = gho->hdr_sz;
263 gho->l2_hdr_offset = 0;
264 gho->l3_hdr_offset = 0;
265 gho->l4_hdr_offset = 0;
269 if (gho->gho_flags & GHO_F_IP4)
271 gho->gho_flags |= GHO_F_OUTER_IP4;
273 else if (gho->gho_flags & GHO_F_IP6)
275 gho->gho_flags |= GHO_F_OUTER_IP6;
278 if (gho->gho_flags & GHO_F_UDP)
280 gho->gho_flags |= GHO_F_OUTER_UDP;
283 gho->gho_flags &= ~GHO_F_INNER_HDR;
285 vnet_get_inner_header (b0, gho);
287 gho->l2_hdr_offset = b0->current_data;
289 ethernet_header_t *eh = (ethernet_header_t *) vlib_buffer_get_current (b0);
290 u16 ethertype = clib_net_to_host_u16 (eh->type);
291 u16 l2hdr_sz = sizeof (ethernet_header_t);
293 if (ethernet_frame_is_tagged (ethertype))
295 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
297 ethertype = clib_net_to_host_u16 (vlan->type);
298 l2hdr_sz += sizeof (*vlan);
299 if (ethertype == ETHERNET_TYPE_VLAN)
302 ethertype = clib_net_to_host_u16 (vlan->type);
303 l2hdr_sz += sizeof (*vlan);
307 gho->l3_hdr_offset = l2hdr_sz;
309 if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
312 (ip4_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
313 gho->l4_hdr_offset = gho->l3_hdr_offset + ip4_header_bytes (ip4);
314 l4_proto = ip4->protocol;
315 gho->gho_flags |= GHO_F_IP4;
317 else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
320 (ip6_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
321 /* FIXME IPv6 EH traversal */
322 gho->l4_hdr_offset = gho->l3_hdr_offset + sizeof (ip6_header_t);
323 l4_proto = ip6->protocol;
324 gho->gho_flags |= GHO_F_IP6;
326 if (l4_proto == IP_PROTOCOL_TCP)
328 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
330 l4_hdr_sz = tcp_header_bytes (tcp);
332 gho->gho_flags |= GHO_F_TCP;
335 else if (l4_proto == IP_PROTOCOL_UDP)
337 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
339 l4_hdr_sz = sizeof (*udp);
341 gho->gho_flags |= GHO_F_UDP;
344 gho->l4_hdr_sz = l4_hdr_sz;
345 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
347 vnet_get_outer_header (b0, gho);
350 static_always_inline void
351 vnet_generic_inner_header_parser_inline (vlib_buffer_t * b0,
352 generic_header_offset_t * gho)
355 if (gho->gho_flags & GHO_F_VXLAN_TUNNEL)
356 vnet_vxlan_inner_header_parser_inline (b0, gho);
357 else if (gho->gho_flags & (GHO_F_IPIP_TUNNEL | GHO_F_IPIP6_TUNNEL))
358 vnet_ipip_inner_header_parser_inline (b0, gho);
359 else if (gho->gho_flags & GHO_F_GRE_TUNNEL)
360 vnet_gre_inner_header_parser_inline (b0, gho);
361 else if (gho->gho_flags & GHO_F_GENEVE_TUNNEL)
362 vnet_geneve_inner_header_parser_inline (b0, gho);
365 static_always_inline void
366 vnet_generic_outer_header_parser_inline (vlib_buffer_t * b0,
367 generic_header_offset_t * gho,
368 int is_l2, int is_ip4, int is_ip6)
375 ASSERT (!(is_ip4 && is_ip6));
379 ethernet_header_t *eh =
380 (ethernet_header_t *) vlib_buffer_get_current (b0);
381 ethertype = clib_net_to_host_u16 (eh->type);
382 l2hdr_sz = sizeof (ethernet_header_t);
384 if (ethernet_frame_is_tagged (ethertype))
386 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
388 ethertype = clib_net_to_host_u16 (vlan->type);
389 l2hdr_sz += sizeof (*vlan);
390 if (ethertype == ETHERNET_TYPE_VLAN)
393 ethertype = clib_net_to_host_u16 (vlan->type);
394 l2hdr_sz += sizeof (*vlan);
399 l2hdr_sz = vnet_buffer (b0)->ip.save_rewrite_length;
401 gho->l2_hdr_offset = b0->current_data;
402 gho->l3_hdr_offset = l2hdr_sz;
404 if (PREDICT_TRUE (is_ip4))
407 (ip4_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
408 gho->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
409 l4_proto = ip4->protocol;
410 gho->gho_flags |= GHO_F_IP4;
412 else if (PREDICT_TRUE (is_ip6))
415 (ip6_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
416 /* FIXME IPv6 EH traversal */
417 gho->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
418 l4_proto = ip6->protocol;
419 gho->gho_flags |= GHO_F_IP6;
421 if (l4_proto == IP_PROTOCOL_TCP)
423 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
425 l4_hdr_sz = tcp_header_bytes (tcp);
427 gho->gho_flags |= GHO_F_TCP;
429 else if (l4_proto == IP_PROTOCOL_UDP)
431 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
433 l4_hdr_sz = sizeof (*udp);
435 gho->gho_flags |= GHO_F_UDP;
437 if (UDP_DST_PORT_vxlan == clib_net_to_host_u16 (udp->dst_port))
439 gho->gho_flags |= GHO_F_VXLAN_TUNNEL;
440 gho->hdr_sz += sizeof (vxlan_header_t);
442 else if (UDP_DST_PORT_geneve == clib_net_to_host_u16 (udp->dst_port))
444 gho->gho_flags |= GHO_F_GENEVE_TUNNEL;
447 else if (l4_proto == IP_PROTOCOL_IP_IN_IP)
450 gho->gho_flags |= GHO_F_IPIP_TUNNEL;
452 else if (l4_proto == IP_PROTOCOL_IPV6)
455 gho->gho_flags |= GHO_F_IPIP6_TUNNEL;
457 else if (l4_proto == IP_PROTOCOL_GRE)
460 gho->gho_flags |= GHO_F_GRE_TUNNEL;
463 gho->l4_hdr_sz = l4_hdr_sz;
464 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
467 static_always_inline void
468 vnet_generic_header_offset_parser (vlib_buffer_t * b0,
469 generic_header_offset_t * gho, int is_l2,
470 int is_ip4, int is_ip6)
472 vnet_generic_outer_header_parser_inline (b0, gho, is_l2, is_ip4, is_ip6);
474 if (gho->gho_flags & GHO_F_TUNNEL)
476 vnet_generic_inner_header_parser_inline (b0, gho);
480 #endif /* included_hdr_offset_parser_h */
483 * fd.io coding-style-patch-verification: ON
486 * eval: (c-set-style "gnu")