[PATCH 5/6] net: add smaller IPv4 cksum function for simple cases
Morten Brørup
mb at smartsharesystems.com
Thu Oct 17 19:15:10 CEST 2024
> +/**
> + * Process the IPv4 checksum of an IPv4 header without any extensions.
> + *
> + * The checksum field does NOT have to be set by the caller, the field
> + * is skipped by the calculation.
> + *
> + * @param ipv4_hdr
> + * The pointer to the contiguous IPv4 header.
> + * @return
> + * The complemented checksum to set in the IP packet.
> + */
> +__rte_experimental
> +static inline uint16_t
> +rte_ipv4_cksum_simple(const struct rte_ipv4_hdr *ipv4_hdr)
> +{
> + const uint16_t *v16_h;
> + uint32_t ip_cksum;
> +
> + /*
> + * Compute the sum of successive 16-bit words of the IPv4 header,
> + * skipping the checksum field of the header.
> + */
> + v16_h = (const unaligned_uint16_t *)&ipv4_hdr->version_ihl;
> + ip_cksum = v16_h[0] + v16_h[1] + v16_h[2] + v16_h[3] +
> + v16_h[4] + v16_h[6] + v16_h[7] + v16_h[8] + v16_h[9];
> +
> + /* reduce 32 bit checksum to 16 bits and complement it */
> + ip_cksum = (ip_cksum & 0xffff) + (ip_cksum >> 16);
> + ip_cksum = (ip_cksum & 0xffff) + (ip_cksum >> 16);
> + ip_cksum = (~ip_cksum) & 0x0000FFFF;
> + return (ip_cksum == 0) ? 0xFFFF : (uint16_t) ip_cksum;
The zero exception does not apply to the checksum stored in the IP header, only to the checksum in the UDP header.
> +}
Besides that, for the series,
Acked-by: Morten Brørup <mb at smartsharesystems.com>
More information about the dev
mailing list