[v4] lib/net: fix tcp/udp cksum with padding data

Message ID 20231213043732.2113867-1-kaiwenx.deng@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series [v4] lib/net: fix tcp/udp cksum with padding data |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-abi-testing success Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS

Commit Message

Kaiwen Deng Dec. 13, 2023, 4:37 a.m. UTC
  IEEE 802 packets may have a minimum size limit. The data fields
should be padded when necessary. In some cases, the padding data
is not zero.

In 'rte_ipv4_udptcp_cksum_mbuf()', as payload length
"mbuf->pkt_len - l4_off" is used, which includes padding and if
padding is not zero it will end up producing wrong checksum.

This patch will use IP header to get the payload size to calculate
tcp/udp checksum.

Fixes: d178f693bbfe ("net: add UDP/TCP checksum in mbuf segments")
Cc: stable@dpdk.org

Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
---
 lib/net/rte_ip.h | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
  

Comments

Morten Brørup Dec. 13, 2023, 7:36 a.m. UTC | #1
> From: Kaiwen Deng [mailto:kaiwenx.deng@intel.com]
> Sent: Wednesday, 13 December 2023 05.38
> 
> IEEE 802 packets may have a minimum size limit. The data fields
> should be padded when necessary. In some cases, the padding data
> is not zero.
> 
> In 'rte_ipv4_udptcp_cksum_mbuf()', as payload length
> "mbuf->pkt_len - l4_off" is used, which includes padding and if
> padding is not zero it will end up producing wrong checksum.
> 
> This patch will use IP header to get the payload size to calculate
> tcp/udp checksum.
> 
> Fixes: d178f693bbfe ("net: add UDP/TCP checksum in mbuf segments")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>

Please fix indentation, refer to the Coding Style:
https://doc.dpdk.org/guides/contributing/coding_style.html#c-indentation

Note that line length up to 100 characters is acceptable:
https://doc.dpdk.org/guides/contributing/coding_style.html#general-guidelines

To some degree, indentation is a matter of taste. Just stay within the boundaries of the Coding Style.

> ---
>  lib/net/rte_ip.h | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
> index 6fa98a5a0f..bcdce723a5 100644
> --- a/lib/net/rte_ip.h
> +++ b/lib/net/rte_ip.h
> @@ -419,11 +419,15 @@ __rte_ipv4_udptcp_cksum_mbuf(const struct
> rte_mbuf *m,
>  {
>  	uint16_t raw_cksum;
>  	uint32_t cksum;
> +	uint16_t len;
> 
> -	if (l4_off > m->pkt_len)
> -		return 0;
> +	if (unlikely(l4_off > m->pkt_len))
> +		return 0; /* invalid params, return a dummy value */
> +
> +	len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
> +				(uint16_t)rte_ipv4_hdr_len(ipv4_hdr);

Please fix indentation of this continuation line.

> 
> -	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off,
> &raw_cksum))
> +	if (rte_raw_cksum_mbuf(m, l4_off, len, &raw_cksum))
>  		return 0;
> 
>  	cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
> @@ -663,10 +667,11 @@ __rte_ipv6_udptcp_cksum_mbuf(const struct
> rte_mbuf *m,
>  	uint16_t raw_cksum;
>  	uint32_t cksum;
> 
> -	if (l4_off > m->pkt_len)
> -		return 0;
> +	if (unlikely(l4_off > m->pkt_len))
> +		return 0; /* invalid params, return a dummy value */
> 
> -	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off,
> &raw_cksum))
> +	if (rte_raw_cksum_mbuf(m, l4_off,
> +		rte_be_to_cpu_16(ipv6_hdr->payload_len), &raw_cksum))

Please fix indentation of this continuation line.

>  		return 0;
> 
>  	cksum = raw_cksum + rte_ipv6_phdr_cksum(ipv6_hdr, 0);
> --
> 2.34.1

With indentation fixed,

Reviewed-by: Morten Brørup <mb@smartsharesystems.com>
  

Patch

diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
index 6fa98a5a0f..bcdce723a5 100644
--- a/lib/net/rte_ip.h
+++ b/lib/net/rte_ip.h
@@ -419,11 +419,15 @@  __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
 {
 	uint16_t raw_cksum;
 	uint32_t cksum;
+	uint16_t len;
 
-	if (l4_off > m->pkt_len)
-		return 0;
+	if (unlikely(l4_off > m->pkt_len))
+		return 0; /* invalid params, return a dummy value */
+
+	len = rte_be_to_cpu_16(ipv4_hdr->total_length) -
+				(uint16_t)rte_ipv4_hdr_len(ipv4_hdr);
 
-	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
+	if (rte_raw_cksum_mbuf(m, l4_off, len, &raw_cksum))
 		return 0;
 
 	cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
@@ -663,10 +667,11 @@  __rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
 	uint16_t raw_cksum;
 	uint32_t cksum;
 
-	if (l4_off > m->pkt_len)
-		return 0;
+	if (unlikely(l4_off > m->pkt_len))
+		return 0; /* invalid params, return a dummy value */
 
-	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
+	if (rte_raw_cksum_mbuf(m, l4_off,
+		rte_be_to_cpu_16(ipv6_hdr->payload_len), &raw_cksum))
 		return 0;
 
 	cksum = raw_cksum + rte_ipv6_phdr_cksum(ipv6_hdr, 0);