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 \
41 typedef enum gho_flag_t_
43 #define _(bit, name) GHO_F_##name = (1 << bit),
48 #define GHO_F_TUNNEL (GHO_F_VXLAN_TUNNEL | \
49 GHO_F_GENEVE_TUNNEL | \
53 #define GHO_F_OUTER_HDR (GHO_F_OUTER_IP4 | \
58 #define GHO_F_INNER_HDR (GHO_F_IP4 | \
65 i16 outer_l2_hdr_offset;
66 i16 outer_l3_hdr_offset;
67 i16 outer_l4_hdr_offset;
76 } generic_header_offset_t;
78 static_always_inline u8 *
79 format_generic_header_offset (u8 * s, va_list * args)
81 generic_header_offset_t *gho = va_arg (*args, generic_header_offset_t *);
83 s = format (s, "\n\t");
84 if (gho->gho_flags & GHO_F_TUNNEL)
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 ");
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 ");
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 ");
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);
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 ");
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 ");
123 s = format (s, "hdr-sz %u l2-hdr-offset %d "
124 "l3-hdr-offset %d l4-hdr-offset %d "
126 gho->hdr_sz, gho->l2_hdr_offset, gho->l3_hdr_offset,
127 gho->l4_hdr_offset, gho->l4_hdr_sz);
132 static_always_inline void
133 vnet_get_inner_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
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);
141 static_always_inline void
142 vnet_get_outer_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
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);
150 static_always_inline void
151 vnet_geneve_inner_header_parser_inline (vlib_buffer_t * b0,
152 generic_header_offset_t * gho)
154 /* not supported yet */
155 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)
172 static_always_inline void
173 vnet_ipip_inner_header_parser_inline (vlib_buffer_t * b0,
174 generic_header_offset_t * gho)
176 /* not supported yet */
177 if ((gho->gho_flags & GHO_F_IPIP_TUNNEL) == 0)
183 static_always_inline void
184 vnet_vxlan_inner_header_parser_inline (vlib_buffer_t * b0,
185 generic_header_offset_t * gho)
190 if ((gho->gho_flags & GHO_F_VXLAN_TUNNEL) == 0)
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;
199 gho->l2_hdr_offset = 0;
200 gho->l3_hdr_offset = 0;
201 gho->l4_hdr_offset = 0;
205 if (gho->gho_flags & GHO_F_IP4)
207 gho->gho_flags |= GHO_F_OUTER_IP4;
209 else if (gho->gho_flags & GHO_F_IP6)
211 gho->gho_flags |= GHO_F_OUTER_IP6;
214 if (gho->gho_flags & GHO_F_UDP)
216 gho->gho_flags |= GHO_F_OUTER_UDP;
219 gho->gho_flags &= ~GHO_F_INNER_HDR;
221 vnet_get_inner_header (b0, gho);
223 gho->l2_hdr_offset = b0->current_data;
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);
229 if (ethernet_frame_is_tagged (ethertype))
231 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
233 ethertype = clib_net_to_host_u16 (vlan->type);
234 l2hdr_sz += sizeof (*vlan);
235 if (ethertype == ETHERNET_TYPE_VLAN)
238 ethertype = clib_net_to_host_u16 (vlan->type);
239 l2hdr_sz += sizeof (*vlan);
243 gho->l3_hdr_offset = l2hdr_sz;
245 if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_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;
253 else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_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;
262 if (l4_proto == IP_PROTOCOL_TCP)
264 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
266 l4_hdr_sz = tcp_header_bytes (tcp);
268 gho->gho_flags |= GHO_F_TCP;
271 else if (l4_proto == IP_PROTOCOL_UDP)
273 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
275 l4_hdr_sz = sizeof (*udp);
277 gho->gho_flags |= GHO_F_UDP;
280 gho->l4_hdr_sz = l4_hdr_sz;
281 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
283 vnet_get_outer_header (b0, gho);
286 static_always_inline void
287 vnet_generic_inner_header_parser_inline (vlib_buffer_t * b0,
288 generic_header_offset_t * gho)
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);
301 static_always_inline void
302 vnet_generic_outer_header_parser_inline (vlib_buffer_t * b0,
303 generic_header_offset_t * gho)
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);
312 if (ethernet_frame_is_tagged (ethertype))
314 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
316 ethertype = clib_net_to_host_u16 (vlan->type);
317 l2hdr_sz += sizeof (*vlan);
318 if (ethertype == ETHERNET_TYPE_VLAN)
321 ethertype = clib_net_to_host_u16 (vlan->type);
322 l2hdr_sz += sizeof (*vlan);
326 gho->l2_hdr_offset = b0->current_data;
327 gho->l3_hdr_offset = l2hdr_sz;
329 if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_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;
337 else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_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;
346 if (l4_proto == IP_PROTOCOL_TCP)
348 tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
350 l4_hdr_sz = tcp_header_bytes (tcp);
352 gho->gho_flags |= GHO_F_TCP;
354 else if (l4_proto == IP_PROTOCOL_UDP)
356 udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
358 l4_hdr_sz = sizeof (*udp);
360 gho->gho_flags |= GHO_F_UDP;
362 if (UDP_DST_PORT_vxlan == clib_net_to_host_u16 (udp->dst_port))
364 gho->gho_flags |= GHO_F_VXLAN_TUNNEL;
365 gho->hdr_sz += sizeof (vxlan_header_t);
367 else if (UDP_DST_PORT_geneve == clib_net_to_host_u16 (udp->dst_port))
369 gho->gho_flags |= GHO_F_GENEVE_TUNNEL;
372 else if ((l4_proto == IP_PROTOCOL_IP_IN_IP)
373 || (l4_proto == IP_PROTOCOL_IPV6))
376 gho->gho_flags |= GHO_F_IPIP_TUNNEL;
378 else if (l4_proto == IP_PROTOCOL_GRE)
381 gho->gho_flags |= GHO_F_GRE_TUNNEL;
384 gho->l4_hdr_sz = l4_hdr_sz;
385 gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
388 static_always_inline void
389 vnet_generic_header_offset_parser (vlib_buffer_t * b0,
390 generic_header_offset_t * gho)
392 vnet_generic_outer_header_parser_inline (b0, gho);
394 if (gho->gho_flags & GHO_F_TUNNEL)
396 vnet_generic_inner_header_parser_inline (b0, gho);
400 #endif /* included_hdr_offset_parser_h */
403 * fd.io coding-style-patch-verification: ON
406 * eval: (c-set-style "gnu")