[dpdk-dev] [PATCH 3/4] net: introduce functions to verify L4 checksums
Morten Brørup
mb at smartsharesystems.com
Tue Apr 27 17:07:04 CEST 2021
> From: Olivier Matz [mailto:olivier.matz at 6wind.com]
> Sent: Tuesday, April 27, 2021 3:58 PM
>
> Since commit d5df2ae0428a ("net: fix unneeded replacement of TCP
> checksum 0"), the functions rte_ipv4_udptcp_cksum() and
> rte_ipv6_udptcp_cksum() can return either 0x0000 or 0xffff when used to
> verify a packet containing a valid checksum.
>
> Since these functions should be used to calculate the checksum to set
> in
> a packet, introduce 2 new helpers for checksum verification. They
> return
> 0 if the checksum is valid in the packet.
>
> Use this new helper in net/tap driver.
>
> Signed-off-by: Olivier Matz <olivier.matz at 6wind.com>
> ---
> drivers/net/tap/rte_eth_tap.c | 7 +-
> lib/net/rte_ip.h | 124 +++++++++++++++++++++++++++-------
> 2 files changed, 104 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/net/tap/rte_eth_tap.c
> b/drivers/net/tap/rte_eth_tap.c
> index 71282e8065..b14d5a1d55 100644
> --- a/drivers/net/tap/rte_eth_tap.c
> +++ b/drivers/net/tap/rte_eth_tap.c
> @@ -365,11 +365,12 @@ tap_verify_csum(struct rte_mbuf *mbuf)
> return;
> }
> }
> - cksum = rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr);
> + cksum_ok = !rte_ipv4_udptcp_cksum_verify(l3_hdr,
> + l4_hdr);
> } else { /* l3 == RTE_PTYPE_L3_IPV6, checked above */
> - cksum = rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr);
> + cksum_ok = !rte_ipv6_udptcp_cksum_verify(l3_hdr,
> + l4_hdr);
> }
> - cksum_ok = (cksum == 0) || (cksum == 0xffff);
> mbuf->ol_flags |= cksum_ok ?
> PKT_RX_L4_CKSUM_GOOD : PKT_RX_L4_CKSUM_BAD;
> }
> diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
> index 8c189009b0..ef84bcc5bf 100644
> --- a/lib/net/rte_ip.h
> +++ b/lib/net/rte_ip.h
> @@ -344,20 +344,10 @@ rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr
> *ipv4_hdr, uint64_t ol_flags)
> }
>
> /**
> - * Process the IPv4 UDP or TCP checksum.
> - *
> - * The IP and layer 4 checksum must be set to 0 in the packet by
> - * the caller.
> - *
> - * @param ipv4_hdr
> - * The pointer to the contiguous IPv4 header.
> - * @param l4_hdr
> - * The pointer to the beginning of the L4 header.
> - * @return
> - * The complemented checksum to set in the IP packet.
> + * @internal Calculate the non-complemented IPv4 L4 checksum
> */
> static inline uint16_t
> -rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void
> *l4_hdr)
> +__rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const
> void *l4_hdr)
> {
> uint32_t cksum;
> uint32_t l3_len, l4_len;
> @@ -374,16 +364,62 @@ rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr
> *ipv4_hdr, const void *l4_hdr)
> cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
>
> cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
> - cksum = (~cksum) & 0xffff;
> +
> + return (uint16_t)cksum;
> +}
> +
> +/**
> + * Process the IPv4 UDP or TCP checksum.
> + *
> + * The IP and layer 4 checksum must be set to 0 in the packet by
> + * the caller.
> + *
> + * @param ipv4_hdr
> + * The pointer to the contiguous IPv4 header.
> + * @param l4_hdr
> + * The pointer to the beginning of the L4 header.
> + * @return
> + * The complemented checksum to set in the IP packet.
> + */
> +static inline uint16_t
> +rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr *ipv4_hdr, const void
> *l4_hdr)
> +{
> + uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
> +
> + cksum = ~cksum;
> +
> /*
> - * Per RFC 768:If the computed checksum is zero for UDP,
> + * Per RFC 768: If the computed checksum is zero for UDP,
> * it is transmitted as all ones
> * (the equivalent in one's complement arithmetic).
> */
> if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
> cksum = 0xffff;
>
> - return (uint16_t)cksum;
> + return cksum;
> +}
The GCC static branch predictor treats the above comparison as likely. Playing around with Godbolt, I came up with this alternative:
if (likely(cksum != 0)) return cksum;
if (ipv4_hdr->next_proto_id == IPPROTO_UDP) return 0xffff;
return 0;
> +
> +/**
> + * Validate the IPv4 UDP or TCP checksum.
> + *
> + * @param ipv4_hdr
> + * The pointer to the contiguous IPv4 header.
> + * @param l4_hdr
> + * The pointer to the beginning of the L4 header.
> + * @return
> + * Return 0 if the checksum is correct, else -1.
> + */
> +__rte_experimental
> +static inline int
> +rte_ipv4_udptcp_cksum_verify(const struct rte_ipv4_hdr *ipv4_hdr,
> + const void *l4_hdr)
> +{
> + uint16_t cksum = __rte_ipv4_udptcp_cksum(ipv4_hdr, l4_hdr);
> +
> + if (cksum != 0xffff)
> + return -1;
The GCC static branch predictor treats the above comparison as likely, so I would prefer unlikely() around it.
> +
> + return 0;
> }
>
> /**
> @@ -448,6 +484,25 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr
> *ipv6_hdr, uint64_t ol_flags)
> return __rte_raw_cksum_reduce(sum);
> }
>
> +/**
> + * @internal Calculate the non-complemented IPv4 L4 checksum
> + */
> +static inline uint16_t
> +__rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const
> void *l4_hdr)
> +{
> + uint32_t cksum;
> + uint32_t l4_len;
> +
> + l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
> +
> + cksum = rte_raw_cksum(l4_hdr, l4_len);
> + cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
> +
> + cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
> +
> + return (uint16_t)cksum;
> +}
> +
> /**
> * Process the IPv6 UDP or TCP checksum.
> *
> @@ -464,16 +519,10 @@ rte_ipv6_phdr_cksum(const struct rte_ipv6_hdr
> *ipv6_hdr, uint64_t ol_flags)
> static inline uint16_t
> rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr *ipv6_hdr, const void
> *l4_hdr)
> {
> - uint32_t cksum;
> - uint32_t l4_len;
> -
> - l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
> + uint16_t cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
>
> - cksum = rte_raw_cksum(l4_hdr, l4_len);
> - cksum += rte_ipv6_phdr_cksum(ipv6_hdr, 0);
> + cksum = ~cksum;
>
> - cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
> - cksum = (~cksum) & 0xffff;
> /*
> * Per RFC 768: If the computed checksum is zero for UDP,
> * it is transmitted as all ones
> @@ -482,7 +531,34 @@ rte_ipv6_udptcp_cksum(const struct rte_ipv6_hdr
> *ipv6_hdr, const void *l4_hdr)
> if (cksum == 0 && ipv6_hdr->proto == IPPROTO_UDP)
> cksum = 0xffff;
Same comment about GCC static branch prediction as above.
>
> - return (uint16_t)cksum;
> + return cksum;
> +}
> +
> +/**
> + * Validate the IPv6 UDP or TCP checksum.
> + *
> + * The function accepts a 0 checksum, since it can exceptionally
> happen. See 8.1
> + * (Upper-Layer Checksums) in RFC 8200.
> + *
> + * @param ipv6_hdr
> + * The pointer to the contiguous IPv6 header.
> + * @param l4_hdr
> + * The pointer to the beginning of the L4 header.
> + * @return
> + * Return 0 if the checksum is correct, else -1.
> + */
> +__rte_experimental
> +static inline int
> +rte_ipv6_udptcp_cksum_verify(const struct rte_ipv6_hdr *ipv6_hdr,
> + const void *l4_hdr)
> +{
> + uint16_t cksum;
> +
> + cksum = __rte_ipv6_udptcp_cksum(ipv6_hdr, l4_hdr);
> + if (cksum != 0xffff)
> + return -1;
Same comment about GCC static branch prediction as above.
> +
> + return 0;
> }
>
> /** IPv6 fragment extension header. */
> --
> 2.29.2
>
With or without my suggested modifications:
Acked-by: Morten Brørup <mb at smartsharesystems.com>
Without my suggested modifications:
Reviewed-by: Morten Brørup <mb at smartsharesystems.com>
More information about the dev
mailing list