LCOV - code coverage report
Current view: top level - lib/net - rte_net.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 81 242 33.5 %
Date: 2026-10-01 18:19:24 Functions: 3 4 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 54 213 25.4 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2016 6WIND S.A.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdint.h>
       6                 :            : 
       7                 :            : #include <eal_export.h>
       8                 :            : #include <rte_mbuf.h>
       9                 :            : #include <rte_mbuf_ptype.h>
      10                 :            : #include <rte_byteorder.h>
      11                 :            : #include <rte_ether.h>
      12                 :            : #include <rte_ip.h>
      13                 :            : #include <rte_tcp.h>
      14                 :            : #include <rte_udp.h>
      15                 :            : #include <rte_sctp.h>
      16                 :            : #include <rte_gre.h>
      17                 :            : #include <rte_mpls.h>
      18                 :            : #include <rte_geneve.h>
      19                 :            : #include <rte_vxlan.h>
      20                 :            : #include <rte_gtp.h>
      21                 :            : #include <rte_net.h>
      22                 :            : #include <rte_os_shim.h>
      23                 :            : 
      24                 :            : /* get l3 packet type from ip6 next protocol */
      25                 :            : static uint32_t
      26                 :            : ptype_l3_ip6(uint8_t ip6_proto)
      27                 :            : {
      28                 :            :         static const uint32_t ip6_ext_proto_map[256] = {
      29                 :            :                 [IPPROTO_HOPOPTS] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
      30                 :            :                 [IPPROTO_ROUTING] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
      31                 :            :                 [IPPROTO_FRAGMENT] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
      32                 :            :                 [IPPROTO_ESP] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
      33                 :            :                 [IPPROTO_AH] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
      34                 :            :                 [IPPROTO_DSTOPTS] = RTE_PTYPE_L3_IPV6_EXT - RTE_PTYPE_L3_IPV6,
      35                 :            :         };
      36                 :            : 
      37                 :          1 :         return RTE_PTYPE_L3_IPV6 + ip6_ext_proto_map[ip6_proto];
      38                 :            : }
      39                 :            : 
      40                 :            : /* get l3 packet type from ip version and header length */
      41                 :            : static uint32_t
      42                 :            : ptype_l3_ip(uint8_t ipv_ihl)
      43                 :            : {
      44                 :            :         static const uint32_t ptype_l3_ip_proto_map[256] = {
      45                 :            :                 [0x45] = RTE_PTYPE_L3_IPV4,
      46                 :            :                 [0x46] = RTE_PTYPE_L3_IPV4_EXT,
      47                 :            :                 [0x47] = RTE_PTYPE_L3_IPV4_EXT,
      48                 :            :                 [0x48] = RTE_PTYPE_L3_IPV4_EXT,
      49                 :            :                 [0x49] = RTE_PTYPE_L3_IPV4_EXT,
      50                 :            :                 [0x4A] = RTE_PTYPE_L3_IPV4_EXT,
      51                 :            :                 [0x4B] = RTE_PTYPE_L3_IPV4_EXT,
      52                 :            :                 [0x4C] = RTE_PTYPE_L3_IPV4_EXT,
      53                 :            :                 [0x4D] = RTE_PTYPE_L3_IPV4_EXT,
      54                 :            :                 [0x4E] = RTE_PTYPE_L3_IPV4_EXT,
      55                 :            :                 [0x4F] = RTE_PTYPE_L3_IPV4_EXT,
      56                 :            :         };
      57                 :            : 
      58                 :         11 :         return ptype_l3_ip_proto_map[ipv_ihl];
      59                 :            : }
      60                 :            : 
      61                 :            : /* get l4 packet type from proto */
      62                 :            : static uint32_t
      63                 :            : ptype_l4(uint8_t proto)
      64                 :            : {
      65                 :            :         static const uint32_t ptype_l4_proto[256] = {
      66                 :            :                 [IPPROTO_UDP] = RTE_PTYPE_L4_UDP,
      67                 :            :                 [IPPROTO_TCP] = RTE_PTYPE_L4_TCP,
      68                 :            :                 [IPPROTO_SCTP] = RTE_PTYPE_L4_SCTP,
      69                 :            :         };
      70                 :            : 
      71                 :         11 :         return ptype_l4_proto[proto];
      72                 :            : }
      73                 :            : 
      74                 :            : /* get inner l3 packet type from ip6 next protocol */
      75                 :            : static uint32_t
      76                 :            : ptype_inner_l3_ip6(uint8_t ip6_proto)
      77                 :            : {
      78                 :            :         static const uint32_t ptype_inner_ip6_ext_proto_map[256] = {
      79                 :            :                 [IPPROTO_HOPOPTS] = RTE_PTYPE_INNER_L3_IPV6_EXT -
      80                 :            :                         RTE_PTYPE_INNER_L3_IPV6,
      81                 :            :                 [IPPROTO_ROUTING] = RTE_PTYPE_INNER_L3_IPV6_EXT -
      82                 :            :                         RTE_PTYPE_INNER_L3_IPV6,
      83                 :            :                 [IPPROTO_FRAGMENT] = RTE_PTYPE_INNER_L3_IPV6_EXT -
      84                 :            :                         RTE_PTYPE_INNER_L3_IPV6,
      85                 :            :                 [IPPROTO_ESP] = RTE_PTYPE_INNER_L3_IPV6_EXT -
      86                 :            :                         RTE_PTYPE_INNER_L3_IPV6,
      87                 :            :                 [IPPROTO_AH] = RTE_PTYPE_INNER_L3_IPV6_EXT -
      88                 :            :                         RTE_PTYPE_INNER_L3_IPV6,
      89                 :            :                 [IPPROTO_DSTOPTS] = RTE_PTYPE_INNER_L3_IPV6_EXT -
      90                 :            :                         RTE_PTYPE_INNER_L3_IPV6,
      91                 :            :         };
      92                 :            : 
      93                 :          0 :         return RTE_PTYPE_INNER_L3_IPV6 +
      94                 :          0 :                 ptype_inner_ip6_ext_proto_map[ip6_proto];
      95                 :            : }
      96                 :            : 
      97                 :            : /* get inner l3 packet type from ip version and header length */
      98                 :            : static uint32_t
      99                 :            : ptype_inner_l3_ip(uint8_t ipv_ihl)
     100                 :            : {
     101                 :            :         static const uint32_t ptype_inner_l3_ip_proto_map[256] = {
     102                 :            :                 [0x45] = RTE_PTYPE_INNER_L3_IPV4,
     103                 :            :                 [0x46] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     104                 :            :                 [0x47] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     105                 :            :                 [0x48] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     106                 :            :                 [0x49] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     107                 :            :                 [0x4A] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     108                 :            :                 [0x4B] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     109                 :            :                 [0x4C] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     110                 :            :                 [0x4D] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     111                 :            :                 [0x4E] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     112                 :            :                 [0x4F] = RTE_PTYPE_INNER_L3_IPV4_EXT,
     113                 :            :         };
     114                 :            : 
     115                 :          0 :         return ptype_inner_l3_ip_proto_map[ipv_ihl];
     116                 :            : }
     117                 :            : 
     118                 :            : /* get inner l4 packet type from proto */
     119                 :            : static uint32_t
     120                 :            : ptype_inner_l4(uint8_t proto)
     121                 :            : {
     122                 :            :         static const uint32_t ptype_inner_l4_proto[256] = {
     123                 :            :                 [IPPROTO_UDP] = RTE_PTYPE_INNER_L4_UDP,
     124                 :            :                 [IPPROTO_TCP] = RTE_PTYPE_INNER_L4_TCP,
     125                 :            :                 [IPPROTO_SCTP] = RTE_PTYPE_INNER_L4_SCTP,
     126                 :            :         };
     127                 :            : 
     128                 :          0 :         return ptype_inner_l4_proto[proto];
     129                 :            : }
     130                 :            : 
     131                 :            : /* get the tunnel packet type if any, update proto and off. */
     132                 :            : static uint32_t
     133                 :          6 : ptype_tunnel_without_udp(uint16_t *proto, const struct rte_mbuf *m,
     134                 :            :         uint32_t *off)
     135                 :            : {
     136   [ -  -  -  + ]:          6 :         switch (*proto) {
     137                 :          0 :         case IPPROTO_GRE: {
     138                 :            :                 static const uint8_t opt_len[16] = {
     139                 :            :                         [0x0] = 4,
     140                 :            :                         [0x1] = 8,
     141                 :            :                         [0x2] = 8,
     142                 :            :                         [0x8] = 8,
     143                 :            :                         [0x3] = 12,
     144                 :            :                         [0x9] = 12,
     145                 :            :                         [0xa] = 12,
     146                 :            :                         [0xb] = 16,
     147                 :            :                 };
     148                 :            :                 const struct rte_gre_hdr *gh;
     149                 :            :                 struct rte_gre_hdr gh_copy;
     150                 :            :                 uint16_t flags;
     151                 :            : 
     152         [ #  # ]:          0 :                 gh = rte_pktmbuf_read(m, *off, sizeof(*gh), &gh_copy);
     153         [ #  # ]:          0 :                 if (unlikely(gh == NULL))
     154                 :            :                         return 0;
     155                 :            : 
     156         [ #  # ]:          0 :                 flags = rte_be_to_cpu_16(*(const uint16_t *)gh);
     157                 :          0 :                 flags >>= 12;
     158         [ #  # ]:          0 :                 if (opt_len[flags] == 0)
     159                 :            :                         return 0;
     160                 :            : 
     161                 :          0 :                 *off += opt_len[flags];
     162                 :          0 :                 *proto = gh->proto;
     163         [ #  # ]:          0 :                 if (*proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_TEB))
     164                 :            :                         return RTE_PTYPE_TUNNEL_NVGRE;
     165                 :            :                 else
     166                 :          0 :                         return RTE_PTYPE_TUNNEL_GRE;
     167                 :            :         }
     168                 :            :         case IPPROTO_IPIP:
     169                 :          0 :                 *proto = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
     170                 :          0 :                 return RTE_PTYPE_TUNNEL_IP;
     171                 :            :         case IPPROTO_IPV6:
     172                 :          0 :                 *proto = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
     173                 :          0 :                 return RTE_PTYPE_TUNNEL_IP; /* IP is also valid for IPv6 */
     174                 :            :         default:
     175                 :            :                 return 0;
     176                 :            :         }
     177                 :            : }
     178                 :            : 
     179                 :            : /* get the tunnel packet type with UDP port if any, update proto and off. */
     180                 :            : static uint32_t
     181                 :          3 : ptype_tunnel_with_udp(uint16_t *proto, const struct rte_mbuf *m,
     182                 :            :                         uint32_t *off, struct rte_net_hdr_lens *hdr_lens)
     183                 :            : {
     184                 :            :         const struct rte_udp_hdr *uh;
     185                 :            :         struct rte_udp_hdr uh_copy;
     186                 :            :         uint16_t port_no;
     187                 :            : 
     188         [ +  - ]:          3 :         uh = rte_pktmbuf_read(m, *off, sizeof(*uh), &uh_copy);
     189         [ +  - ]:          3 :         if (unlikely(uh == NULL))
     190                 :            :                 return 0;
     191                 :            : 
     192                 :          3 :         *off += sizeof(*uh);
     193   [ -  +  -  + ]:          6 :         if (rte_be_to_cpu_16(uh->src_port) == RTE_GTPC_UDP_PORT)
     194         [ #  # ]:          0 :                 port_no = rte_be_to_cpu_16(uh->src_port);
     195                 :            :         else
     196         [ -  + ]:          3 :                 port_no = rte_be_to_cpu_16(uh->dst_port);
     197   [ -  -  -  -  :          3 :         switch (port_no) {
                      + ]
     198                 :          0 :         case RTE_VXLAN_DEFAULT_PORT: {
     199                 :          0 :                 *off += sizeof(struct rte_vxlan_hdr);
     200                 :          0 :                 hdr_lens->tunnel_len = sizeof(struct rte_vxlan_hdr);
     201                 :          0 :                 hdr_lens->inner_l2_len = RTE_ETHER_VXLAN_HLEN;
     202                 :          0 :                 *proto = RTE_VXLAN_GPE_TYPE_ETH; /* just for eth header parse. */
     203                 :          0 :                 return RTE_PTYPE_TUNNEL_VXLAN;
     204                 :            :         }
     205         [ #  # ]:          0 :         case RTE_VXLAN_GPE_DEFAULT_PORT: {
     206                 :            :                 const struct rte_vxlan_gpe_hdr *vgh;
     207                 :            :                 struct rte_vxlan_gpe_hdr vgh_copy;
     208                 :            :                 vgh = rte_pktmbuf_read(m, *off, sizeof(*vgh), &vgh_copy);
     209         [ #  # ]:          0 :                 if (unlikely(vgh == NULL))
     210                 :            :                         return 0;
     211                 :          0 :                 *off += sizeof(struct rte_vxlan_gpe_hdr);
     212                 :          0 :                 hdr_lens->tunnel_len = sizeof(struct rte_vxlan_gpe_hdr);
     213                 :          0 :                 hdr_lens->inner_l2_len = RTE_ETHER_VXLAN_GPE_HLEN;
     214                 :          0 :                 *proto = vgh->proto;
     215                 :            : 
     216                 :          0 :                 return RTE_PTYPE_TUNNEL_VXLAN_GPE;
     217                 :            :         }
     218         [ #  # ]:          0 :         case RTE_GTPC_UDP_PORT:
     219                 :            :         case RTE_GTPU_UDP_PORT: {
     220                 :            :                 const struct rte_gtp_hdr *gh;
     221                 :            :                 struct rte_gtp_hdr gh_copy;
     222                 :            :                 uint32_t gtp_len;
     223                 :            :                 gh = rte_pktmbuf_read(m, *off, sizeof(*gh), &gh_copy);
     224         [ #  # ]:          0 :                 if (unlikely(gh == NULL))
     225                 :            :                         return 0;
     226                 :            :                 gtp_len = sizeof(*gh);
     227         [ #  # ]:          0 :                 if (gh->e || gh->s || gh->pn)
     228                 :            :                         gtp_len += sizeof(struct rte_gtp_hdr_ext_word);
     229                 :            :                 /*
     230                 :            :                  * Check message type. If message type is 0xff, it is
     231                 :            :                  * a GTP data packet. If not, it is a GTP control packet
     232                 :            :                  */
     233                 :          0 :                 *off += gtp_len;
     234         [ #  # ]:          0 :                 if (gh->msg_type == 0xff) {
     235                 :            :                         const uint8_t *l3_byte;
     236                 :            :                         uint8_t l3_copy, ip_ver;
     237                 :            : 
     238                 :            :                         /* read first byte of l3 header */
     239                 :            :                         l3_byte = rte_pktmbuf_read(m, *off, sizeof(uint8_t), &l3_copy);
     240         [ #  # ]:          0 :                         if (unlikely(l3_byte == NULL))
     241                 :          0 :                                 return 0;
     242                 :            : 
     243                 :          0 :                         ip_ver = *l3_byte & 0xf0;
     244         [ #  # ]:          0 :                         if (ip_ver == RTE_GTP_TYPE_IPV4)
     245                 :          0 :                                 *proto = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
     246         [ #  # ]:          0 :                         else if (ip_ver == RTE_GTP_TYPE_IPV6)
     247                 :          0 :                                 *proto = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
     248                 :            :                         else
     249                 :          0 :                                 *proto = 0;
     250                 :            :                 } else {
     251                 :          0 :                         *proto = 0;
     252                 :            :                 }
     253                 :          0 :                 hdr_lens->inner_l2_len = gtp_len + sizeof(struct rte_udp_hdr);
     254                 :          0 :                 hdr_lens->tunnel_len = gtp_len;
     255         [ #  # ]:          0 :                 if (port_no == RTE_GTPC_UDP_PORT)
     256                 :            :                         return RTE_PTYPE_TUNNEL_GTPC;
     257                 :            :                 else if (port_no == RTE_GTPU_UDP_PORT)
     258                 :          0 :                         return RTE_PTYPE_TUNNEL_GTPU;
     259                 :            :                 return 0;
     260                 :            :         }
     261         [ #  # ]:          0 :         case RTE_GENEVE_DEFAULT_PORT: {
     262                 :            :                 const struct rte_geneve_hdr *gnh;
     263                 :            :                 struct rte_geneve_hdr gnh_copy;
     264                 :            :                 uint16_t geneve_len;
     265                 :            :                 gnh = rte_pktmbuf_read(m, *off, sizeof(*gnh), &gnh_copy);
     266         [ #  # ]:          0 :                 if (unlikely(gnh == NULL))
     267                 :            :                         return 0;
     268                 :          0 :                 geneve_len = sizeof(*gnh) + gnh->opt_len * 4;
     269                 :          0 :                 *off += geneve_len;
     270                 :          0 :                 hdr_lens->tunnel_len = geneve_len;
     271                 :          0 :                 hdr_lens->inner_l2_len = sizeof(struct rte_udp_hdr) + geneve_len;
     272                 :          0 :                 *proto = gnh->proto;
     273         [ #  # ]:          0 :                 if (gnh->proto == 0)
     274                 :          0 :                         *proto = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
     275                 :            :                 return RTE_PTYPE_TUNNEL_GENEVE;
     276                 :            :         }
     277                 :            :         default:
     278                 :            :                 return 0;
     279                 :            :         }
     280                 :            : }
     281                 :            : 
     282                 :            : /* parse ipv6 extended headers, update offset and return next proto */
     283                 :            : RTE_EXPORT_SYMBOL(rte_net_skip_ip6_ext)
     284                 :            : int
     285                 :          0 : rte_net_skip_ip6_ext(uint16_t proto, const struct rte_mbuf *m, uint32_t *off,
     286                 :            :         int *frag)
     287                 :            : {
     288                 :            :         struct ext_hdr {
     289                 :            :                 uint8_t next_hdr;
     290                 :            :                 uint8_t len;
     291                 :            :         };
     292                 :            :         const struct ext_hdr *xh;
     293                 :            :         struct ext_hdr xh_copy;
     294                 :            :         unsigned int i;
     295                 :            : 
     296                 :          0 :         *frag = 0;
     297                 :            : 
     298                 :            : #define MAX_EXT_HDRS 5
     299         [ #  # ]:          0 :         for (i = 0; i < MAX_EXT_HDRS; i++) {
     300   [ #  #  #  # ]:          0 :                 switch (proto) {
     301                 :          0 :                 case IPPROTO_HOPOPTS:
     302                 :            :                 case IPPROTO_ROUTING:
     303                 :            :                 case IPPROTO_DSTOPTS:
     304         [ #  # ]:          0 :                         xh = rte_pktmbuf_read(m, *off, sizeof(*xh),
     305                 :            :                                 &xh_copy);
     306         [ #  # ]:          0 :                         if (xh == NULL)
     307                 :            :                                 return -1;
     308                 :          0 :                         *off += (xh->len + 1) * 8;
     309                 :          0 :                         proto = xh->next_hdr;
     310                 :            :                         break;
     311                 :          0 :                 case IPPROTO_FRAGMENT:
     312         [ #  # ]:          0 :                         xh = rte_pktmbuf_read(m, *off, sizeof(*xh),
     313                 :            :                                 &xh_copy);
     314         [ #  # ]:          0 :                         if (xh == NULL)
     315                 :            :                                 return -1;
     316                 :          0 :                         *off += 8;
     317                 :          0 :                         proto = xh->next_hdr;
     318                 :          0 :                         *frag = 1;
     319                 :          0 :                         return proto; /* this is always the last ext hdr */
     320                 :            :                 case IPPROTO_NONE:
     321                 :            :                         return 0;
     322                 :            :                 default:
     323                 :            :                         return proto;
     324                 :            :                 }
     325                 :            :         }
     326                 :            :         return -1;
     327                 :            : }
     328                 :            : 
     329                 :            : /* limit number of supported VLAN headers */
     330                 :            : #define RTE_NET_VLAN_MAX_DEPTH 8
     331                 :            : 
     332                 :            : /* parse mbuf data to get packet type */
     333                 :            : RTE_EXPORT_SYMBOL(rte_net_get_ptype)
     334                 :         12 : uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
     335                 :            :         struct rte_net_hdr_lens *hdr_lens, uint32_t layers)
     336                 :            : {
     337                 :            :         struct rte_net_hdr_lens local_hdr_lens;
     338                 :            :         const struct rte_ether_hdr *eh;
     339                 :            :         struct rte_ether_hdr eh_copy;
     340                 :            :         uint32_t pkt_type = RTE_PTYPE_L2_ETHER;
     341                 :         12 :         uint32_t off = 0, vlan_depth = 0;
     342                 :            :         uint16_t proto;
     343                 :            :         int ret;
     344                 :            : 
     345         [ -  + ]:         12 :         if (hdr_lens == NULL)
     346                 :            :                 hdr_lens = &local_hdr_lens;
     347                 :            : 
     348                 :            :         eh = rte_pktmbuf_read(m, off, sizeof(*eh), &eh_copy);
     349         [ +  - ]:         12 :         if (unlikely(eh == NULL))
     350                 :            :                 return 0;
     351                 :         12 :         proto = eh->ether_type;
     352                 :         12 :         off = sizeof(*eh);
     353                 :         12 :         hdr_lens->l2_len = off;
     354                 :            : 
     355         [ +  - ]:         12 :         if ((layers & RTE_PTYPE_L2_MASK) == 0)
     356                 :            :                 return 0;
     357                 :            : 
     358         [ +  + ]:         12 :         if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
     359                 :         11 :                 goto l3; /* fast path if packet is IPv4 */
     360                 :            : 
     361   [ -  +  -  + ]:          1 :         while (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) ||
     362                 :            :                proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_QINQ)) {
     363                 :            :                 const struct rte_vlan_hdr *vh;
     364                 :            :                 struct rte_vlan_hdr vh_copy;
     365                 :            : 
     366         [ #  # ]:          0 :                 if (++vlan_depth > RTE_NET_VLAN_MAX_DEPTH)
     367                 :          0 :                         return 0;
     368                 :            : 
     369                 :            :                 /*
     370                 :            :                  * It is the outermost tag that determines whether
     371                 :            :                  * the packet will be classified as VLAN or QinQ.
     372                 :            :                  */
     373         [ #  # ]:          0 :                 if (vlan_depth == 1) {
     374                 :            :                         /* Override previously set 'RTE_PTYPE_L2_ETHER'. */
     375                 :            :                         pkt_type = (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) ?
     376         [ #  # ]:          0 :                                         RTE_PTYPE_L2_ETHER_VLAN :
     377                 :            :                                         RTE_PTYPE_L2_ETHER_QINQ);
     378                 :            :                 }
     379                 :            : 
     380         [ #  # ]:          0 :                 vh = rte_pktmbuf_read(m, off, sizeof(*vh), &vh_copy);
     381         [ #  # ]:          0 :                 if (unlikely(vh == NULL))
     382                 :          0 :                         return pkt_type;
     383                 :          0 :                 off += sizeof(*vh);
     384                 :          0 :                 hdr_lens->l2_len += sizeof(*vh);
     385                 :          0 :                 proto = vh->eth_proto;
     386                 :            :         }
     387                 :            : 
     388   [ +  -  -  + ]:          1 :         if ((proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_MPLS)) ||
     389                 :            :                 (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_MPLSM))) {
     390                 :            :                 unsigned int i;
     391                 :            :                 const struct rte_mpls_hdr *mh;
     392                 :            :                 struct rte_mpls_hdr mh_copy;
     393                 :            : 
     394                 :            : #define MAX_MPLS_HDR 5
     395         [ #  # ]:          0 :                 for (i = 0; i < MAX_MPLS_HDR; i++) {
     396         [ #  # ]:          0 :                         mh = rte_pktmbuf_read(m, off + (i * sizeof(*mh)),
     397                 :            :                                 sizeof(*mh), &mh_copy);
     398         [ #  # ]:          0 :                         if (unlikely(mh == NULL))
     399                 :            :                                 return pkt_type;
     400                 :            :                 }
     401                 :            :                 if (i == MAX_MPLS_HDR)
     402                 :            :                         return pkt_type;
     403                 :            :                 pkt_type = RTE_PTYPE_L2_ETHER_MPLS;
     404                 :            :                 hdr_lens->l2_len += (sizeof(*mh) * i);
     405                 :            :                 return pkt_type;
     406                 :            :         }
     407                 :            : 
     408                 :          1 : l3:
     409         [ +  - ]:         12 :         if ((layers & RTE_PTYPE_L3_MASK) == 0)
     410                 :            :                 return pkt_type;
     411                 :            : 
     412         [ +  + ]:         12 :         if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
     413                 :            :                 const struct rte_ipv4_hdr *ip4h;
     414                 :            :                 struct rte_ipv4_hdr ip4h_copy;
     415                 :            : 
     416         [ +  - ]:         11 :                 ip4h = rte_pktmbuf_read(m, off, sizeof(*ip4h), &ip4h_copy);
     417         [ +  - ]:         11 :                 if (unlikely(ip4h == NULL))
     418                 :          1 :                         return pkt_type;
     419                 :            : 
     420         [ +  - ]:         11 :                 pkt_type |= ptype_l3_ip(ip4h->version_ihl);
     421                 :         11 :                 hdr_lens->l3_len = rte_ipv4_hdr_len(ip4h);
     422                 :         11 :                 off += hdr_lens->l3_len;
     423                 :            : 
     424         [ +  - ]:         11 :                 if ((layers & RTE_PTYPE_L4_MASK) == 0)
     425                 :            :                         return pkt_type;
     426                 :            : 
     427         [ +  + ]:         11 :                 if (ip4h->fragment_offset & rte_cpu_to_be_16(
     428                 :            :                                 RTE_IPV4_HDR_OFFSET_MASK | RTE_IPV4_HDR_MF_FLAG)) {
     429                 :          1 :                         pkt_type |= RTE_PTYPE_L4_FRAG;
     430                 :          1 :                         hdr_lens->l4_len = 0;
     431                 :          1 :                         return pkt_type;
     432                 :            :                 }
     433                 :         10 :                 proto = ip4h->next_proto_id;
     434                 :         10 :                 pkt_type |= ptype_l4(proto);
     435         [ +  - ]:          1 :         } else if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
     436                 :            :                 const struct rte_ipv6_hdr *ip6h;
     437                 :            :                 struct rte_ipv6_hdr ip6h_copy;
     438                 :          1 :                 int frag = 0;
     439                 :            : 
     440         [ +  - ]:          1 :                 ip6h = rte_pktmbuf_read(m, off, sizeof(*ip6h), &ip6h_copy);
     441         [ +  - ]:          1 :                 if (unlikely(ip6h == NULL))
     442                 :          0 :                         return pkt_type;
     443                 :            : 
     444                 :          1 :                 proto = ip6h->proto;
     445                 :          1 :                 hdr_lens->l3_len = sizeof(*ip6h);
     446                 :          1 :                 off += hdr_lens->l3_len;
     447                 :          1 :                 pkt_type |= ptype_l3_ip6(proto);
     448         [ -  + ]:          1 :                 if ((pkt_type & RTE_PTYPE_L3_MASK) == RTE_PTYPE_L3_IPV6_EXT) {
     449                 :          0 :                         ret = rte_net_skip_ip6_ext(proto, m, &off, &frag);
     450         [ #  # ]:          0 :                         if (ret < 0)
     451                 :            :                                 return pkt_type;
     452                 :          0 :                         proto = ret;
     453                 :          0 :                         hdr_lens->l3_len = off - hdr_lens->l2_len;
     454                 :            :                 }
     455         [ +  - ]:          1 :                 if (proto == 0)
     456                 :            :                         return pkt_type;
     457                 :            : 
     458         [ +  - ]:          1 :                 if ((layers & RTE_PTYPE_L4_MASK) == 0)
     459                 :            :                         return pkt_type;
     460                 :            : 
     461         [ -  + ]:          1 :                 if (frag) {
     462                 :          0 :                         pkt_type |= RTE_PTYPE_L4_FRAG;
     463                 :          0 :                         hdr_lens->l4_len = 0;
     464                 :          0 :                         return pkt_type;
     465                 :            :                 }
     466                 :          1 :                 pkt_type |= ptype_l4(proto);
     467                 :            :         }
     468                 :            : 
     469         [ +  + ]:         11 :         if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP) {
     470                 :          3 :                 hdr_lens->l4_len = sizeof(struct rte_udp_hdr);
     471         [ +  - ]:          3 :                 if ((layers & RTE_PTYPE_TUNNEL_MASK) == 0)
     472                 :            :                         return pkt_type;
     473                 :          3 :                 pkt_type |= ptype_tunnel_with_udp(&proto, m, &off, hdr_lens);
     474         [ +  + ]:          8 :         } else if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP) {
     475                 :            :                 const struct rte_tcp_hdr *th;
     476                 :            :                 struct rte_tcp_hdr th_copy;
     477                 :            : 
     478         [ +  - ]:          2 :                 th = rte_pktmbuf_read(m, off, sizeof(*th), &th_copy);
     479         [ -  + ]:          2 :                 if (unlikely(th == NULL))
     480                 :          0 :                         return pkt_type & (RTE_PTYPE_L2_MASK |
     481                 :            :                                 RTE_PTYPE_L3_MASK);
     482                 :          2 :                 hdr_lens->l4_len = (th->data_off & 0xf0) >> 2;
     483                 :          2 :                 return pkt_type;
     484         [ -  + ]:          6 :         } else if ((pkt_type & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP) {
     485                 :          0 :                 hdr_lens->l4_len = sizeof(struct rte_sctp_hdr);
     486                 :          0 :                 return pkt_type;
     487                 :            :         } else {
     488                 :          6 :                 uint32_t prev_off = off;
     489                 :            : 
     490                 :          6 :                 hdr_lens->l4_len = 0;
     491                 :            : 
     492         [ +  - ]:          6 :                 if ((layers & RTE_PTYPE_TUNNEL_MASK) == 0)
     493                 :            :                         return pkt_type;
     494                 :            : 
     495                 :          6 :                 pkt_type |= ptype_tunnel_without_udp(&proto, m, &off);
     496                 :          6 :                 hdr_lens->tunnel_len = off - prev_off;
     497                 :          6 :                 hdr_lens->inner_l2_len = off - prev_off;
     498                 :            :         }
     499                 :            : 
     500                 :            :         /* same job for inner header: we need to duplicate the code
     501                 :            :          * because the packet types do not have the same value.
     502                 :            :          */
     503         [ +  - ]:          9 :         if ((layers & RTE_PTYPE_INNER_L2_MASK) == 0)
     504                 :            :                 return pkt_type;
     505                 :            : 
     506         [ +  - ]:          9 :         if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_TEB) ||
     507         [ -  + ]:          9 :                 proto == rte_cpu_to_be_16(RTE_GENEVE_TYPE_ETH) ||
     508                 :            :                 proto == RTE_VXLAN_GPE_TYPE_ETH) {
     509         [ #  # ]:          0 :                 eh = rte_pktmbuf_read(m, off, sizeof(*eh), &eh_copy);
     510         [ #  # ]:          0 :                 if (unlikely(eh == NULL))
     511                 :            :                         return pkt_type;
     512                 :          0 :                 pkt_type |= RTE_PTYPE_INNER_L2_ETHER;
     513                 :          0 :                 proto = eh->ether_type;
     514                 :          0 :                 off += sizeof(*eh);
     515                 :          0 :                 hdr_lens->inner_l2_len += sizeof(*eh);
     516                 :            :         }
     517                 :            : 
     518         [ -  + ]:          9 :         if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
     519                 :            :                 const struct rte_vlan_hdr *vh;
     520                 :            :                 struct rte_vlan_hdr vh_copy;
     521                 :            : 
     522                 :          0 :                 pkt_type &= ~RTE_PTYPE_INNER_L2_MASK;
     523                 :          0 :                 pkt_type |= RTE_PTYPE_INNER_L2_ETHER_VLAN;
     524         [ #  # ]:          0 :                 vh = rte_pktmbuf_read(m, off, sizeof(*vh), &vh_copy);
     525         [ #  # ]:          0 :                 if (unlikely(vh == NULL))
     526                 :          0 :                         return pkt_type;
     527                 :          0 :                 off += sizeof(*vh);
     528                 :          0 :                 hdr_lens->inner_l2_len += sizeof(*vh);
     529                 :          0 :                 proto = vh->eth_proto;
     530         [ -  + ]:          9 :         } else if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_QINQ)) {
     531                 :            :                 const struct rte_vlan_hdr *vh;
     532                 :            :                 struct rte_vlan_hdr vh_copy;
     533                 :            : 
     534                 :          0 :                 pkt_type &= ~RTE_PTYPE_INNER_L2_MASK;
     535                 :          0 :                 pkt_type |= RTE_PTYPE_INNER_L2_ETHER_QINQ;
     536         [ #  # ]:          0 :                 vh = rte_pktmbuf_read(m, off + sizeof(*vh), sizeof(*vh),
     537                 :            :                         &vh_copy);
     538         [ #  # ]:          0 :                 if (unlikely(vh == NULL))
     539                 :          0 :                         return pkt_type;
     540                 :          0 :                 off += 2 * sizeof(*vh);
     541                 :          0 :                 hdr_lens->inner_l2_len += 2 * sizeof(*vh);
     542                 :          0 :                 proto = vh->eth_proto;
     543                 :            :         }
     544                 :            : 
     545         [ +  - ]:          9 :         if ((layers & RTE_PTYPE_INNER_L3_MASK) == 0)
     546                 :            :                 return pkt_type;
     547                 :            : 
     548   [ +  -  -  + ]:          9 :         if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) ||
     549                 :          0 :                 proto == RTE_VXLAN_GPE_TYPE_IPV4) {
     550                 :            :                 const struct rte_ipv4_hdr *ip4h;
     551                 :            :                 struct rte_ipv4_hdr ip4h_copy;
     552                 :            : 
     553         [ #  # ]:          0 :                 ip4h = rte_pktmbuf_read(m, off, sizeof(*ip4h), &ip4h_copy);
     554         [ #  # ]:          0 :                 if (unlikely(ip4h == NULL))
     555                 :          0 :                         return pkt_type;
     556                 :            : 
     557         [ #  # ]:          0 :                 pkt_type |= ptype_inner_l3_ip(ip4h->version_ihl);
     558                 :          0 :                 hdr_lens->inner_l3_len = rte_ipv4_hdr_len(ip4h);
     559                 :          0 :                 off += hdr_lens->inner_l3_len;
     560                 :            : 
     561         [ #  # ]:          0 :                 if ((layers & RTE_PTYPE_INNER_L4_MASK) == 0)
     562                 :            :                         return pkt_type;
     563         [ #  # ]:          0 :                 if (ip4h->fragment_offset &
     564                 :            :                                 rte_cpu_to_be_16(RTE_IPV4_HDR_OFFSET_MASK |
     565                 :            :                                         RTE_IPV4_HDR_MF_FLAG)) {
     566                 :          0 :                         pkt_type |= RTE_PTYPE_INNER_L4_FRAG;
     567                 :          0 :                         hdr_lens->inner_l4_len = 0;
     568                 :          0 :                         return pkt_type;
     569                 :            :                 }
     570                 :          0 :                 proto = ip4h->next_proto_id;
     571                 :          0 :                 pkt_type |= ptype_inner_l4(proto);
     572   [ +  -  -  + ]:          9 :         } else if (proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) ||
     573                 :            :                         proto == RTE_VXLAN_GPE_TYPE_IPV6) {
     574                 :            :                 const struct rte_ipv6_hdr *ip6h;
     575                 :            :                 struct rte_ipv6_hdr ip6h_copy;
     576                 :          0 :                 int frag = 0;
     577                 :            : 
     578         [ #  # ]:          0 :                 ip6h = rte_pktmbuf_read(m, off, sizeof(*ip6h), &ip6h_copy);
     579         [ #  # ]:          0 :                 if (unlikely(ip6h == NULL))
     580                 :          0 :                         return pkt_type;
     581                 :            : 
     582                 :          0 :                 proto = ip6h->proto;
     583                 :          0 :                 hdr_lens->inner_l3_len = sizeof(*ip6h);
     584                 :          0 :                 off += hdr_lens->inner_l3_len;
     585                 :          0 :                 pkt_type |= ptype_inner_l3_ip6(proto);
     586         [ #  # ]:          0 :                 if ((pkt_type & RTE_PTYPE_INNER_L3_MASK) ==
     587                 :            :                                 RTE_PTYPE_INNER_L3_IPV6_EXT) {
     588                 :            :                         uint32_t prev_off;
     589                 :            : 
     590                 :            :                         prev_off = off;
     591                 :          0 :                         ret = rte_net_skip_ip6_ext(proto, m, &off, &frag);
     592         [ #  # ]:          0 :                         if (ret < 0)
     593                 :            :                                 return pkt_type;
     594                 :          0 :                         proto = ret;
     595                 :          0 :                         hdr_lens->inner_l3_len += off - prev_off;
     596                 :            :                 }
     597         [ #  # ]:          0 :                 if (proto == 0)
     598                 :            :                         return pkt_type;
     599                 :            : 
     600         [ #  # ]:          0 :                 if ((layers & RTE_PTYPE_INNER_L4_MASK) == 0)
     601                 :            :                         return pkt_type;
     602                 :            : 
     603         [ #  # ]:          0 :                 if (frag) {
     604                 :          0 :                         pkt_type |= RTE_PTYPE_INNER_L4_FRAG;
     605                 :          0 :                         hdr_lens->inner_l4_len = 0;
     606                 :          0 :                         return pkt_type;
     607                 :            :                 }
     608                 :          0 :                 pkt_type |= ptype_inner_l4(proto);
     609                 :            :         }
     610                 :            : 
     611         [ -  + ]:          9 :         if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) == RTE_PTYPE_INNER_L4_UDP) {
     612                 :          0 :                 hdr_lens->inner_l4_len = sizeof(struct rte_udp_hdr);
     613         [ -  + ]:          9 :         } else if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) ==
     614                 :            :                         RTE_PTYPE_INNER_L4_TCP) {
     615                 :            :                 const struct rte_tcp_hdr *th;
     616                 :            :                 struct rte_tcp_hdr th_copy;
     617                 :            : 
     618         [ #  # ]:          0 :                 th = rte_pktmbuf_read(m, off, sizeof(*th), &th_copy);
     619         [ #  # ]:          0 :                 if (unlikely(th == NULL))
     620                 :          0 :                         return pkt_type & (RTE_PTYPE_INNER_L2_MASK |
     621                 :            :                                 RTE_PTYPE_INNER_L3_MASK);
     622                 :          0 :                 hdr_lens->inner_l4_len = (th->data_off & 0xf0) >> 2;
     623         [ -  + ]:          9 :         } else if ((pkt_type & RTE_PTYPE_INNER_L4_MASK) ==
     624                 :            :                         RTE_PTYPE_INNER_L4_SCTP) {
     625                 :          0 :                 hdr_lens->inner_l4_len = sizeof(struct rte_sctp_hdr);
     626                 :            :         } else {
     627                 :          9 :                 hdr_lens->inner_l4_len = 0;
     628                 :            :         }
     629                 :            : 
     630                 :            :         return pkt_type;
     631                 :            : }

Generated by: LCOV version 1.14