1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
8 #include <rte_mbuf_ptype.h>
9 #include <rte_byteorder.h>
10 #include <rte_ether.h>
19 /* get l3 packet type from ip6 next protocol */
21 ptype_l3_ip6(uint8_t ip6_proto)
23 static const uint32_t ip6_ext_proto_map[256] = {
24 [IPPROTO_HOPOPTS] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
25 [IPPROTO_ROUTING] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
26 [IPPROTO_FRAGMENT] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
27 [IPPROTO_ESP] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
28 [IPPROTO_AH] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
29 [IPPROTO_DSTOPTS] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
32 return RTE_PTYPE_L3_IPV6 + ip6_ext_proto_map[ip6_proto];
35 /* get l3 packet type from ip version and header length */
37 ptype_l3_ip(uint8_t ipv_ihl)
39 static const uint32_t ptype_l3_ip_proto_map[256] = {
40 [0x45] = RTE_PTYPE_L3_IPV4,
41 [0x46] = RTE_PTYPE_L3_IPV4_EXT,
42 [0x47] = RTE_PTYPE_L3_IPV4_EXT,
43 [0x48] = RTE_PTYPE_L3_IPV4_EXT,
44 [0x49] = RTE_PTYPE_L3_IPV4_EXT,
45 [0x4A] = RTE_PTYPE_L3_IPV4_EXT,
46 [0x4B] = RTE_PTYPE_L3_IPV4_EXT,
47 [0x4C] = RTE_PTYPE_L3_IPV4_EXT,
48 [0x4D] = RTE_PTYPE_L3_IPV4_EXT,
49 [0x4E] = RTE_PTYPE_L3_IPV4_EXT,
50 [0x4F] = RTE_PTYPE_L3_IPV4_EXT,
53 return ptype_l3_ip_proto_map[ipv_ihl];
56 /* get l4 packet type from proto */
58 ptype_l4(uint8_t proto)
60 static const uint32_t ptype_l4_proto[256] = {
61 [IPPROTO_UDP] = RTE_PTYPE_L4_UDP,
62 [IPPROTO_TCP] = RTE_PTYPE_L4_TCP,
63 [IPPROTO_SCTP] = RTE_PTYPE_L4_SCTP,
66 return ptype_l4_proto[proto];
69 /* get inner l3 packet type from ip6 next protocol */
71 ptype_inner_l3_ip6(uint8_t ip6_proto)
73 static const uint32_t ptype_inner_ip6_ext_proto_map[256] = {
74 [IPPROTO_HOPOPTS] = RTE_PTYPE_INNER_L3_IPV6_EXT -
75 RTE_PTYPE_INNER_L3_IPV6,
76 [IPPROTO_ROUTING] = RTE_PTYPE_INNER_L3_IPV6_EXT -
77 RTE_PTYPE_INNER_L3_IPV6,
78 [IPPROTO_FRAGMENT] = RTE_PTYPE_INNER_L3_IPV6_EXT -
79 RTE_PTYPE_INNER_L3_IPV6,
80 [IPPROTO_ESP] = RTE_PTYPE_INNER_L3_IPV6_EXT -
81 RTE_PTYPE_INNER_L3_IPV6,
82 [IPPROTO_AH] = RTE_PTYPE_INNER_L3_IPV6_EXT -
83 RTE_PTYPE_INNER_L3_IPV6,
84 [IPPROTO_DSTOPTS] = RTE_PTYPE_INNER_L3_IPV6_EXT -
85 RTE_PTYPE_INNER_L3_IPV6,
88 return RTE_PTYPE_INNER_L3_IPV6 +
89 ptype_inner_ip6_ext_proto_map[ip6_proto];
92 /* get inner l3 packet type from ip version and header length */
94 ptype_inner_l3_ip(uint8_t ipv_ihl)
96 static const uint32_t ptype_inner_l3_ip_proto_map[256] = {
97 [0x45] = RTE_PTYPE_INNER_L3_IPV4,
98 [0x46] = RTE_PTYPE_INNER_L3_IPV4_EXT,
99 [0x47] = RTE_PTYPE_INNER_L3_IPV4_EXT,
100 [0x48] = RTE_PTYPE_INNER_L3_IPV4_EXT,
101 [0x49] = RTE_PTYPE_INNER_L3_IPV4_EXT,
102 [0x4A] = RTE_PTYPE_INNER_L3_IPV4_EXT,
103 [0x4B] = RTE_PTYPE_INNER_L3_IPV4_EXT,
104 [0x4C] = RTE_PTYPE_INNER_L3_IPV4_EXT,
105 [0x4D] = RTE_PTYPE_INNER_L3_IPV4_EXT,
106 [0x4E] = RTE_PTYPE_INNER_L3_IPV4_EXT,
107 [0x4F] = RTE_PTYPE_INNER_L3_IPV4_EXT,
110 return ptype_inner_l3_ip_proto_map[ipv_ihl];
113 /* get inner l4 packet type from proto */
115 ptype_inner_l4(uint8_t proto)
117 static const uint32_t ptype_inner_l4_proto[256] = {
118 [IPPROTO_UDP] = RTE_PTYPE_INNER_L4_UDP,
119 [IPPROTO_TCP] = RTE_PTYPE_INNER_L4_TCP,
120 [IPPROTO_SCTP] = RTE_PTYPE_INNER_L4_SCTP,
123 return ptype_inner_l4_proto[proto];
126 /* get the tunnel packet type if any, update proto and off. */
128 ptype_tunnel(uint16_t *proto, const struct rte_mbuf *m,
133 static const uint8_t opt_len[16] = {
143 const struct rte_gre_hdr *gh;
144 struct rte_gre_hdr gh_copy;
147 gh = rte_pktmbuf_read(m, *off, sizeof(*gh), &gh_copy);
148 if (unlikely(gh == NULL))
151 flags = rte_be_to_cpu_16(*(const uint16_t *)gh);
153 if (opt_len[flags] == 0)
156 *off += opt_len[flags];
158 if (*proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_TEB))
159 return RTE_PTYPE_TUNNEL_NVGRE;
161 return RTE_PTYPE_TUNNEL_GRE;
164 *proto = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
165 return RTE_PTYPE_TUNNEL_IP;
167 *proto = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
168 return RTE_PTYPE_TUNNEL_IP; /* IP is also valid for IPv6 */
174 /* parse ipv6 extended headers, update offset and return next proto */
176 rte_net_skip_ip6_ext(uint16_t proto, const struct rte_mbuf *m, uint32_t *off,
183 const struct ext_hdr *xh;
184 struct ext_hdr xh_copy;
189 #define MAX_EXT_HDRS 5
190 for (i = 0; i < MAX_EXT_HDRS; i++) {
192 case IPPROTO_HOPOPTS:
193 case IPPROTO_ROUTING:
194 case IPPROTO_DSTOPTS:
195 xh = rte_pktmbuf_read(m, *off, sizeof(*xh),
199 *off += (xh->len + 1) * 8;
200 proto = xh->next_hdr;
202 case IPPROTO_FRAGMENT:
203 xh = rte_pktmbuf_read(m, *off, sizeof(*xh),
208 proto = xh->next_hdr;
210 return proto; /* this is always the last ext hdr */
220 /* parse mbuf data to get packet type */
221 uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
222 struct rte_net_hdr_lens *hdr_lens, uint32_t layers)
224 struct rte_net_hdr_lens local_hdr_lens;
225 const struct rte_ether_hdr *eh;
226 struct rte_ether_hdr eh_copy;
227 uint32_t pkt_type = RTE_PTYPE_L2_ETHER;
232 if (hdr_lens == NULL)
233 hdr_lens = &local_hdr_lens;
235 eh = rte_pktmbuf_read(m, off, sizeof(*eh), &eh_copy);
236 if (unlikely(eh == NULL))
238 proto = eh->ether_type;
240 hdr_lens->l2_len = off;
242 if ((layers & RTE_PTYPE_L2_MASK) == 0)
245 if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
246 goto l3; /* fast path if packet is IPv4 */
248 if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
249 const struct rte_vlan_hdr *vh;
250 struct rte_vlan_hdr vh_copy;
252 pkt_type = RTE_PTYPE_L2_ETHER_VLAN;
253 vh = rte_pktmbuf_read(m, off, sizeof(*vh), &vh_copy);
254 if (unlikely(vh == NULL))
257 hdr_lens->l2_len += sizeof(*vh);
258 proto = vh->eth_proto;
259 } else if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_QINQ)) {
260 const struct rte_vlan_hdr *vh;
261 struct rte_vlan_hdr vh_copy;
263 pkt_type = RTE_PTYPE_L2_ETHER_QINQ;
264 vh = rte_pktmbuf_read(m, off + sizeof(*vh), sizeof(*vh),
266 if (unlikely(vh == NULL))
268 off += 2 * sizeof(*vh);
269 hdr_lens->l2_len += 2 * sizeof(*vh);
270 proto = vh->eth_proto;
271 } else if ((proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_MPLS)) ||
272 (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_MPLSM))) {
274 const struct rte_mpls_hdr *mh;
275 struct rte_mpls_hdr mh_copy;
277 #define MAX_MPLS_HDR 5
278 for (i = 0; i < MAX_MPLS_HDR; i++) {
279 mh = rte_pktmbuf_read(m, off + (i * sizeof(*mh)),
280 sizeof(*mh), &mh_copy);
281 if (unlikely(mh == NULL))
284 if (i == MAX_MPLS_HDR)
286 pkt_type = RTE_PTYPE_L2_ETHER_MPLS;
287 hdr_lens->l2_len += (sizeof(*mh) * i);
292 if ((layers & RTE_PTYPE_L3_MASK) == 0)
295 if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
296 const struct rte_ipv4_hdr *ip4h;
297 struct rte_ipv4_hdr ip4h_copy;
299 ip4h = rte_pktmbuf_read(m, off, sizeof(*ip4h), &ip4h_copy);
300 if (unlikely(ip4h == NULL))
303 pkt_type |= ptype_l3_ip(ip4h->version_ihl);
304 hdr_lens->l3_len = rte_ipv4_hdr_len(ip4h);
305 off += hdr_lens->l3_len;
307 if ((layers & RTE_PTYPE_L4_MASK) == 0)
310 if (ip4h->fragment_offset & rte_cpu_to_be_16(
311 RTE_IPV4_HDR_OFFSET_MASK | RTE_IPV4_HDR_MF_FLAG)) {
312 pkt_type |= RTE_PTYPE_L4_FRAG;
313 hdr_lens->l4_len = 0;
316 proto = ip4h->next_proto_id;
317 pkt_type |= ptype_l4(proto);
318 } else if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
319 const struct rte_ipv6_hdr *ip6h;
320 struct rte_ipv6_hdr ip6h_copy;
323 ip6h = rte_pktmbuf_read(m, off, sizeof(*ip6h), &ip6h_copy);
324 if (unlikely(ip6h == NULL))
328 hdr_lens->l3_len = sizeof(*ip6h);
329 off += hdr_lens->l3_len;
330 pkt_type |= ptype_l3_ip6(proto);
331 if ((pkt_type & RTE_PTYPE_L3_MASK) == RTE_PTYPE_L3_IPV6_EXT) {
332 ret = rte_net_skip_ip6_ext(proto, m, &off, &frag);
336 hdr_lens->l3_len = off - hdr_lens->l2_len;
341 if ((layers & RTE_PTYPE_L4_MASK) == 0)
345 pkt_type |= RTE_PTYPE_L4_FRAG;
346 hdr_lens->l4_len = 0;
349 pkt_type |= ptype_l4(proto);
352 if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP) {
353 hdr_lens->l4_len = sizeof(struct rte_udp_hdr);
355 } else if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP) {
356 const struct rte_tcp_hdr *th;
357 struct rte_tcp_hdr th_copy;
359 th = rte_pktmbuf_read(m, off, sizeof(*th), &th_copy);
360 if (unlikely(th == NULL))
361 return pkt_type & (RTE_PTYPE_L2_MASK |
363 hdr_lens->l4_len = (th->data_off & 0xf0) >> 2;
365 } else if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP) {
366 hdr_lens->l4_len = sizeof(struct rte_sctp_hdr);
369 uint32_t prev_off = off;
371 hdr_lens->l4_len = 0;
373 if ((layers & RTE_PTYPE_TUNNEL_MASK) == 0)
376 pkt_type |= ptype_tunnel(&proto, m, &off);
377 hdr_lens->tunnel_len = off - prev_off;
380 /* same job for inner header: we need to duplicate the code
381 * because the packet types do not have the same value.
383 if ((layers & RTE_PTYPE_INNER_L2_MASK) == 0)
386 hdr_lens->inner_l2_len = 0;
387 if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_TEB)) {
388 eh = rte_pktmbuf_read(m, off, sizeof(*eh), &eh_copy);
389 if (unlikely(eh == NULL))
391 pkt_type |= RTE_PTYPE_INNER_L2_ETHER;
392 proto = eh->ether_type;
394 hdr_lens->inner_l2_len = sizeof(*eh);
397 if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
398 const struct rte_vlan_hdr *vh;
399 struct rte_vlan_hdr vh_copy;
401 pkt_type &= ~RTE_PTYPE_INNER_L2_MASK;
402 pkt_type |= RTE_PTYPE_INNER_L2_ETHER_VLAN;
403 vh = rte_pktmbuf_read(m, off, sizeof(*vh), &vh_copy);
404 if (unlikely(vh == NULL))
407 hdr_lens->inner_l2_len += sizeof(*vh);
408 proto = vh->eth_proto;
409 } else if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_QINQ)) {
410 const struct rte_vlan_hdr *vh;
411 struct rte_vlan_hdr vh_copy;
413 pkt_type &= ~RTE_PTYPE_INNER_L2_MASK;
414 pkt_type |= RTE_PTYPE_INNER_L2_ETHER_QINQ;
415 vh = rte_pktmbuf_read(m, off + sizeof(*vh), sizeof(*vh),
417 if (unlikely(vh == NULL))
419 off += 2 * sizeof(*vh);
420 hdr_lens->inner_l2_len += 2 * sizeof(*vh);
421 proto = vh->eth_proto;
424 if ((layers & RTE_PTYPE_INNER_L3_MASK) == 0)
427 if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
428 const struct rte_ipv4_hdr *ip4h;
429 struct rte_ipv4_hdr ip4h_copy;
431 ip4h = rte_pktmbuf_read(m, off, sizeof(*ip4h), &ip4h_copy);
432 if (unlikely(ip4h == NULL))
435 pkt_type |= ptype_inner_l3_ip(ip4h->version_ihl);
436 hdr_lens->inner_l3_len = rte_ipv4_hdr_len(ip4h);
437 off += hdr_lens->inner_l3_len;
439 if ((layers & RTE_PTYPE_INNER_L4_MASK) == 0)
441 if (ip4h->fragment_offset &
442 rte_cpu_to_be_16(RTE_IPV4_HDR_OFFSET_MASK |
443 RTE_IPV4_HDR_MF_FLAG)) {
444 pkt_type |= RTE_PTYPE_INNER_L4_FRAG;
445 hdr_lens->inner_l4_len = 0;
448 proto = ip4h->next_proto_id;
449 pkt_type |= ptype_inner_l4(proto);
450 } else if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
451 const struct rte_ipv6_hdr *ip6h;
452 struct rte_ipv6_hdr ip6h_copy;
455 ip6h = rte_pktmbuf_read(m, off, sizeof(*ip6h), &ip6h_copy);
456 if (unlikely(ip6h == NULL))
460 hdr_lens->inner_l3_len = sizeof(*ip6h);
461 off += hdr_lens->inner_l3_len;
462 pkt_type |= ptype_inner_l3_ip6(proto);
463 if ((pkt_type & RTE_PTYPE_INNER_L3_MASK) ==
464 RTE_PTYPE_INNER_L3_IPV6_EXT) {
468 ret = rte_net_skip_ip6_ext(proto, m, &off, &frag);
472 hdr_lens->inner_l3_len += off - prev_off;
477 if ((layers & RTE_PTYPE_INNER_L4_MASK) == 0)
481 pkt_type |= RTE_PTYPE_INNER_L4_FRAG;
482 hdr_lens->inner_l4_len = 0;
485 pkt_type |= ptype_inner_l4(proto);
488 if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) == RTE_PTYPE_INNER_L4_UDP) {
489 hdr_lens->inner_l4_len = sizeof(struct rte_udp_hdr);
490 } else if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) ==
491 RTE_PTYPE_INNER_L4_TCP) {
492 const struct rte_tcp_hdr *th;
493 struct rte_tcp_hdr th_copy;
495 th = rte_pktmbuf_read(m, off, sizeof(*th), &th_copy);
496 if (unlikely(th == NULL))
497 return pkt_type & (RTE_PTYPE_INNER_L2_MASK |
498 RTE_PTYPE_INNER_L3_MASK);
499 hdr_lens->inner_l4_len = (th->data_off & 0xf0) >> 2;
500 } else if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) ==
501 RTE_PTYPE_INNER_L4_SCTP) {
502 hdr_lens->inner_l4_len = sizeof(struct rte_sctp_hdr);
504 hdr_lens->inner_l4_len = 0;