LCOV - code coverage report
Current view: top level - lib/net - rte_ip4.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 25 44 56.8 %
Date: 2025-10-01 17:51:42 Functions: 2 5 40.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 13 48 27.1 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 1982, 1986, 1990, 1993
       3                 :            :  *      The Regents of the University of California.
       4                 :            :  * Copyright(c) 2010-2014 Intel Corporation.
       5                 :            :  * Copyright(c) 2014 6WIND S.A.
       6                 :            :  * All rights reserved.
       7                 :            :  */
       8                 :            : 
       9                 :            : #ifndef _RTE_IP4_H_
      10                 :            : #define _RTE_IP4_H_
      11                 :            : 
      12                 :            : /**
      13                 :            :  * @file
      14                 :            :  *
      15                 :            :  * IPv4-related defines
      16                 :            :  */
      17                 :            : 
      18                 :            : #include <stdint.h>
      19                 :            : 
      20                 :            : #ifdef RTE_EXEC_ENV_WINDOWS
      21                 :            : #include <ws2tcpip.h>
      22                 :            : #else
      23                 :            : #include <sys/socket.h>
      24                 :            : #include <sys/types.h>
      25                 :            : #include <netinet/in.h>
      26                 :            : #include <arpa/inet.h>
      27                 :            : #include <netinet/ip.h>
      28                 :            : #include <netinet/ip6.h>
      29                 :            : #endif
      30                 :            : 
      31                 :            : #include <rte_byteorder.h>
      32                 :            : #include <rte_cksum.h>
      33                 :            : #include <rte_mbuf.h>
      34                 :            : 
      35                 :            : #ifdef __cplusplus
      36                 :            : extern "C" {
      37                 :            : #endif
      38                 :            : 
      39                 :            : /**
      40                 :            :  * IPv4 Header
      41                 :            :  */
      42                 :            : struct __rte_aligned(2) __rte_packed_begin rte_ipv4_hdr {
      43                 :            :         __extension__
      44                 :            :         union {
      45                 :            :                 uint8_t version_ihl;    /**< version and header length */
      46                 :            :                 struct {
      47                 :            : #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
      48                 :            :                         uint8_t ihl:4;     /**< header length */
      49                 :            :                         uint8_t version:4; /**< version */
      50                 :            : #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
      51                 :            :                         uint8_t version:4; /**< version */
      52                 :            :                         uint8_t ihl:4;     /**< header length */
      53                 :            : #endif
      54                 :            :                 };
      55                 :            :         };
      56                 :            :         uint8_t  type_of_service;       /**< type of service */
      57                 :            :         rte_be16_t total_length;        /**< length of packet */
      58                 :            :         rte_be16_t packet_id;           /**< packet ID */
      59                 :            :         rte_be16_t fragment_offset;     /**< fragmentation offset */
      60                 :            :         uint8_t  time_to_live;          /**< time to live */
      61                 :            :         uint8_t  next_proto_id;         /**< protocol ID */
      62                 :            :         rte_be16_t hdr_checksum;        /**< header checksum */
      63                 :            :         rte_be32_t src_addr;            /**< source address */
      64                 :            :         rte_be32_t dst_addr;            /**< destination address */
      65                 :            : } __rte_packed_end;
      66                 :            : 
      67                 :            : /** Create IPv4 address */
      68                 :            : #define RTE_IPV4(a, b, c, d) (((uint32_t)((a) & 0xff) << 24) | ((uint32_t)((b) & 0xff) << 16) | \
      69                 :            :         ((uint32_t)((c) & 0xff) << 8) | ((uint32_t)((d) & 0xff)))
      70                 :            : 
      71                 :            : /** Maximal IPv4 packet length (including a header) */
      72                 :            : #define RTE_IPV4_MAX_PKT_LEN        65535
      73                 :            : 
      74                 :            : /** Internet header length mask for version_ihl field */
      75                 :            : #define RTE_IPV4_HDR_IHL_MASK   (0x0f)
      76                 :            : /**
      77                 :            :  * Internet header length field multiplier (IHL field specifies overall header
      78                 :            :  * length in number of 4-byte words)
      79                 :            :  */
      80                 :            : #define RTE_IPV4_IHL_MULTIPLIER (4)
      81                 :            : 
      82                 :            : /* Type of Service fields */
      83                 :            : #define RTE_IPV4_HDR_DSCP_MASK  (0xfc)
      84                 :            : #define RTE_IPV4_HDR_ECN_MASK   (0x03)
      85                 :            : #define RTE_IPV4_HDR_ECN_CE     RTE_IPV4_HDR_ECN_MASK
      86                 :            : 
      87                 :            : /* Fragment Offset * Flags. */
      88                 :            : #define RTE_IPV4_HDR_DF_SHIFT   14
      89                 :            : #define RTE_IPV4_HDR_MF_SHIFT   13
      90                 :            : #define RTE_IPV4_HDR_FO_SHIFT   3
      91                 :            : 
      92                 :            : #define RTE_IPV4_HDR_DF_FLAG    (1 << RTE_IPV4_HDR_DF_SHIFT)
      93                 :            : #define RTE_IPV4_HDR_MF_FLAG    (1 << RTE_IPV4_HDR_MF_SHIFT)
      94                 :            : 
      95                 :            : #define RTE_IPV4_HDR_OFFSET_MASK        ((1 << RTE_IPV4_HDR_MF_SHIFT) - 1)
      96                 :            : 
      97                 :            : #define RTE_IPV4_HDR_OFFSET_UNITS       8
      98                 :            : 
      99                 :            : /* IPv4 options */
     100                 :            : #define RTE_IPV4_HDR_OPT_EOL       0
     101                 :            : #define RTE_IPV4_HDR_OPT_NOP       1
     102                 :            : #define RTE_IPV4_HDR_OPT_COPIED(v) ((v) & 0x80)
     103                 :            : #define RTE_IPV4_HDR_OPT_MAX_LEN   40
     104                 :            : 
     105                 :            : /*
     106                 :            :  * IPv4 address types
     107                 :            :  */
     108                 :            : #define RTE_IPV4_ANY              ((uint32_t)0x00000000) /**< 0.0.0.0 */
     109                 :            : #define RTE_IPV4_LOOPBACK         ((uint32_t)0x7f000001) /**< 127.0.0.1 */
     110                 :            : #define RTE_IPV4_BROADCAST        ((uint32_t)0xe0000000) /**< 224.0.0.0 */
     111                 :            : #define RTE_IPV4_ALLHOSTS_GROUP   ((uint32_t)0xe0000001) /**< 224.0.0.1 */
     112                 :            : #define RTE_IPV4_ALLRTRS_GROUP    ((uint32_t)0xe0000002) /**< 224.0.0.2 */
     113                 :            : #define RTE_IPV4_MAX_LOCAL_GROUP  ((uint32_t)0xe00000ff) /**< 224.0.0.255 */
     114                 :            : 
     115                 :            : /*
     116                 :            :  * IPv4 Multicast-related macros
     117                 :            :  */
     118                 :            : #define RTE_IPV4_MIN_MCAST \
     119                 :            :         RTE_IPV4(224, 0, 0, 0)          /**< Minimal IPv4-multicast address */
     120                 :            : #define RTE_IPV4_MAX_MCAST \
     121                 :            :         RTE_IPV4(239, 255, 255, 255)    /**< Maximum IPv4 multicast address */
     122                 :            : 
     123                 :            : #define RTE_IS_IPV4_MCAST(x) \
     124                 :            :         ((x) >= RTE_IPV4_MIN_MCAST && (x) <= RTE_IPV4_MAX_MCAST)
     125                 :            :         /**< check if IPv4 address is multicast */
     126                 :            : 
     127                 :            : /* IPv4 default fields values */
     128                 :            : #define RTE_IPV4_MIN_IHL    (0x5)
     129                 :            : #define RTE_IPV4_VHL_DEF    ((IPVERSION << 4) | RTE_IPV4_MIN_IHL)
     130                 :            : 
     131                 :            : /**
     132                 :            :  * Get the length of an IPv4 header.
     133                 :            :  *
     134                 :            :  * @param ipv4_hdr
     135                 :            :  *   Pointer to the IPv4 header.
     136                 :            :  * @return
     137                 :            :  *   The length of the IPv4 header (with options if present) in bytes.
     138                 :            :  */
     139                 :            : static inline uint8_t
     140                 :            : rte_ipv4_hdr_len(const struct rte_ipv4_hdr *ipv4_hdr)
     141                 :            : {
     142   [ +  -  -  - ]:       4151 :         return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
     143                 :            :                 RTE_IPV4_IHL_MULTIPLIER);
     144                 :            : }
     145                 :            : 
     146                 :            : /**
     147                 :            :  * Process the IPv4 checksum of an IPv4 header.
     148                 :            :  *
     149                 :            :  * The checksum field must be set to 0 by the caller.
     150                 :            :  *
     151                 :            :  * @param ipv4_hdr
     152                 :            :  *   The pointer to the contiguous IPv4 header.
     153                 :            :  * @return
     154                 :            :  *   The complemented checksum to set in the IP packet.
     155                 :            :  */
     156                 :            : static inline uint16_t
     157                 :          0 : rte_ipv4_cksum(const struct rte_ipv4_hdr *ipv4_hdr)
     158                 :            : {
     159                 :            :         uint16_t cksum;
     160   [ -  +  -  + ]:       4128 :         cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
     161   [ -  +  #  # ]:          4 :         return (uint16_t)~cksum;
     162                 :            : }
     163                 :            : 
     164                 :            : /**
     165                 :            :  * @warning
     166                 :            :  * @b EXPERIMENTAL: this API may change without prior notice.
     167                 :            :  *
     168                 :            :  * Process the IPv4 checksum of an IPv4 header without any extensions.
     169                 :            :  *
     170                 :            :  * The checksum field does NOT have to be set by the caller, the field
     171                 :            :  * is skipped by the calculation.
     172                 :            :  *
     173                 :            :  * @param ipv4_hdr
     174                 :            :  *   The pointer to the contiguous IPv4 header.
     175                 :            :  * @return
     176                 :            :  *   The complemented checksum to set in the IP packet.
     177                 :            :  */
     178                 :            : __rte_experimental
     179                 :            : static inline uint16_t
     180                 :          0 : rte_ipv4_cksum_simple(const struct rte_ipv4_hdr *ipv4_hdr)
     181                 :            : {
     182                 :            :         const uint16_t *v16_h;
     183                 :            :         uint32_t ip_cksum;
     184                 :            : 
     185                 :            :         /*
     186                 :            :          * Compute the sum of successive 16-bit words of the IPv4 header,
     187                 :            :          * skipping the checksum field of the header.
     188                 :            :          */
     189                 :            :         v16_h = (const uint16_t *)ipv4_hdr;
     190                 :          0 :         ip_cksum = v16_h[0] + v16_h[1] + v16_h[2] + v16_h[3] +
     191                 :          0 :                 v16_h[4] + v16_h[6] + v16_h[7] + v16_h[8] + v16_h[9];
     192                 :            : 
     193                 :            :         /* reduce 32 bit checksum to 16 bits and complement it */
     194                 :          0 :         ip_cksum = (ip_cksum & 0xffff) + (ip_cksum >> 16);
     195                 :          0 :         ip_cksum = (ip_cksum & 0xffff) + (ip_cksum >> 16);
     196                 :          0 :         return (uint16_t)(~ip_cksum);
     197                 :            : }
     198                 :            : 
     199                 :            : /**
     200                 :            :  * Process the pseudo-header checksum of an IPv4 header.
     201                 :            :  *
     202                 :            :  * The checksum field must be set to 0 by the caller.
     203                 :            :  *
     204                 :            :  * Depending on the ol_flags, the pseudo-header checksum expected by the
     205                 :            :  * drivers is not the same. For instance, when TSO is enabled, the IP
     206                 :            :  * payload length must not be included in the packet.
     207                 :            :  *
     208                 :            :  * When ol_flags is 0, it computes the standard pseudo-header checksum.
     209                 :            :  *
     210                 :            :  * @param ipv4_hdr
     211                 :            :  *   The pointer to the contiguous IPv4 header.
     212                 :            :  * @param ol_flags
     213                 :            :  *   The ol_flags of the associated mbuf.
     214                 :            :  * @return
     215                 :            :  *   The non-complemented checksum to set in the L4 header.
     216                 :            :  */
     217                 :            : static inline uint16_t
     218                 :          9 : rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
     219                 :            : {
     220                 :            :         struct ipv4_psd_header {
     221                 :            :                 uint32_t src_addr; /* IP address of source host. */
     222                 :            :                 uint32_t dst_addr; /* IP address of destination host. */
     223                 :            :                 uint8_t  zero;     /* zero. */
     224                 :            :                 uint8_t  proto;    /* L4 protocol type. */
     225                 :            :                 uint16_t len;      /* L4 length. */
     226                 :            :         } psd_hdr;
     227                 :            : 
     228                 :            :         uint32_t l3_len;
     229                 :            : 
     230                 :          9 :         psd_hdr.src_addr = ipv4_hdr->src_addr;
     231                 :          9 :         psd_hdr.dst_addr = ipv4_hdr->dst_addr;
     232                 :          9 :         psd_hdr.zero = 0;
     233                 :          9 :         psd_hdr.proto = ipv4_hdr->next_proto_id;
     234         [ -  + ]:          9 :         if (ol_flags & (RTE_MBUF_F_TX_TCP_SEG | RTE_MBUF_F_TX_UDP_SEG)) {
     235                 :          0 :                 psd_hdr.len = 0;
     236                 :            :         } else {
     237         [ -  + ]:         18 :                 l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
     238                 :          9 :                 psd_hdr.len = rte_cpu_to_be_16((uint16_t)(l3_len -
     239                 :            :                         rte_ipv4_hdr_len(ipv4_hdr)));
     240                 :            :         }
     241                 :          9 :         return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
     242                 :            : }
     243                 :            : 
     244                 :            : /**
     245                 :            :  * @internal Calculate the non-complemented IPv4 L4 checksum
     246                 :            :  */
     247                 :            : static inline uint16_t
     248                 :          9 : __rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
     249                 :            : {
     250                 :            :         uint32_t cksum;
     251                 :            :         uint32_t l3_len, l4_len;
     252                 :            :         uint8_t ip_hdr_len;
     253                 :            : 
     254                 :            :         ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
     255         [ -  + ]:         18 :         l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
     256         [ +  - ]:          9 :         if (l3_len < ip_hdr_len)
     257                 :            :                 return 0;
     258                 :            : 
     259                 :          9 :         l4_len = l3_len - ip_hdr_len;
     260                 :            : 
     261                 :          9 :         cksum = rte_raw_cksum(l4_hdr, l4_len);
     262                 :          9 :         cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
     263                 :            : 
     264                 :          9 :         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
     265                 :            : 
     266                 :          9 :         return (uint16_t)cksum;
     267                 :            : }
     268                 :            : 
     269                 :            : /**
     270                 :            :  * Process the IPv4 UDP or TCP checksum.
     271                 :            :  *
     272                 :            :  * The layer 4 checksum must be set to 0 in the L4 header by the caller.
     273                 :            :  *
     274                 :            :  * @param ipv4_hdr
     275                 :            :  *   The pointer to the contiguous IPv4 header.
     276                 :            :  * @param l4_hdr
     277                 :            :  *   The pointer to the beginning of the L4 header.
     278                 :            :  * @return
     279                 :            :  *   The complemented checksum to set in the L4 header.
     280                 :            :  */
     281                 :            : static inline uint16_t
     282                 :            : rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void *l4_hdr)
     283                 :            : {
     284                 :          3 :         uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
     285                 :            : 
     286                 :          3 :         cksum = ~cksum;
     287                 :            : 
     288                 :            :         /*
     289                 :            :          * Per RFC 768: If the computed checksum is zero for UDP,
     290                 :            :          * it is transmitted as all ones
     291                 :            :          * (the equivalent in one's complement arithmetic).
     292                 :            :          */
     293   [ -  +  -  -  :          3 :         if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
          -  +  -  -  #  
                #  #  # ]
     294                 :            :                 cksum = 0xffff;
     295                 :            : 
     296                 :            :         return cksum;
     297                 :            : }
     298                 :            : 
     299                 :            : /**
     300                 :            :  * @internal Calculate the non-complemented IPv4 L4 checksum of a packet
     301                 :            :  */
     302                 :            : static inline uint16_t
     303                 :          0 : __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
     304                 :            :                              const struct rte_ipv4_hdr *ipv4_hdr,
     305                 :            :                              uint16_t l4_off)
     306                 :            : {
     307                 :            :         uint16_t raw_cksum;
     308                 :            :         uint32_t cksum;
     309                 :            :         uint16_t len;
     310                 :            : 
     311         [ #  # ]:          0 :         if (unlikely(l4_off > m->pkt_len))
     312                 :            :                 return 0; /* invalid params, return a dummy value */
     313                 :            : 
     314         [ #  # ]:          0 :         len = rte_be_to_cpu_16(ipv4_hdr->total_length) - (uint16_t)rte_ipv4_hdr_len(ipv4_hdr);
     315                 :            : 
     316         [ #  # ]:          0 :         if (rte_raw_cksum_mbuf(m, l4_off, len, &raw_cksum))
     317                 :            :                 return 0;
     318                 :            : 
     319                 :          0 :         cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
     320                 :            : 
     321                 :          0 :         cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
     322                 :            : 
     323                 :          0 :         return (uint16_t)cksum;
     324                 :            : }
     325                 :            : 
     326                 :            : /**
     327                 :            :  * Compute the IPv4 UDP/TCP checksum of a packet.
     328                 :            :  *
     329                 :            :  * @param m
     330                 :            :  *   The pointer to the mbuf.
     331                 :            :  * @param ipv4_hdr
     332                 :            :  *   The pointer to the contiguous IPv4 header.
     333                 :            :  * @param l4_off
     334                 :            :  *   The offset in bytes to start L4 checksum.
     335                 :            :  * @return
     336                 :            :  *   The complemented checksum to set in the L4 header.
     337                 :            :  */
     338                 :            : static inline uint16_t
     339                 :            : rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
     340                 :            :                            const struct rte_ipv4_hdr *ipv4_hdr, uint16_t l4_off)
     341                 :            : {
     342                 :          0 :         uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
     343                 :            : 
     344                 :          0 :         cksum = ~cksum;
     345                 :            : 
     346                 :            :         /*
     347                 :            :          * Per RFC 768: If the computed checksum is zero for UDP,
     348                 :            :          * it is transmitted as all ones
     349                 :            :          * (the equivalent in one's complement arithmetic).
     350                 :            :          */
     351   [ #  #  #  # ]:          0 :         if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
     352                 :            :                 cksum = 0xffff;
     353                 :            : 
     354                 :            :         return cksum;
     355                 :            : }
     356                 :            : 
     357                 :            : /**
     358                 :            :  * Validate the IPv4 UDP or TCP checksum.
     359                 :            :  *
     360                 :            :  * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0
     361                 :            :  * (i.e. no checksum).
     362                 :            :  *
     363                 :            :  * @param ipv4_hdr
     364                 :            :  *   The pointer to the contiguous IPv4 header.
     365                 :            :  * @param l4_hdr
     366                 :            :  *   The pointer to the beginning of the L4 header.
     367                 :            :  * @return
     368                 :            :  *   Return 0 if the checksum is correct, else -1.
     369                 :            :  */
     370                 :            : static inline int
     371                 :            : rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
     372                 :            :                              const void *l4_hdr)
     373                 :            : {
     374                 :          6 :         uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
     375                 :            : 
     376   [ -  +  +  -  :          6 :         if (cksum != 0xffff)
                   +  - ]
     377                 :          0 :                 return -1;
     378                 :            : 
     379                 :            :         return 0;
     380                 :            : }
     381                 :            : 
     382                 :            : /**
     383                 :            :  * Verify the IPv4 UDP/TCP checksum of a packet.
     384                 :            :  *
     385                 :            :  * In case of UDP, the caller must first check if udp_hdr->dgram_cksum is 0
     386                 :            :  * (i.e. no checksum).
     387                 :            :  *
     388                 :            :  * @param m
     389                 :            :  *   The pointer to the mbuf.
     390                 :            :  * @param ipv4_hdr
     391                 :            :  *   The pointer to the contiguous IPv4 header.
     392                 :            :  * @param l4_off
     393                 :            :  *   The offset in bytes to start L4 checksum.
     394                 :            :  * @return
     395                 :            :  *   Return 0 if the checksum is correct, else -1.
     396                 :            :  */
     397                 :            : static inline int
     398                 :            : rte_ipv4_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m,
     399                 :            :                                   const struct rte_ipv4_hdr *ipv4_hdr,
     400                 :            :                                   uint16_t l4_off)
     401                 :            : {
     402                 :            :         uint16_t cksum = __rte_ipv4_udptcp_cksum_mbuf(m, ipv4_hdr, l4_off);
     403                 :            : 
     404                 :            :         if (cksum != 0xffff)
     405                 :            :                 return -1;
     406                 :            : 
     407                 :            :         return 0;
     408                 :            : }
     409                 :            : 
     410                 :            : #ifdef __cplusplus
     411                 :            : }
     412                 :            : #endif
     413                 :            : 
     414                 :            : #endif /* _RTE_IP_H_ */

Generated by: LCOV version 1.14