DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] app/test-pmd: fix L4 checksum with padding data
@ 2023-08-04  8:28 Kaiwen Deng
  2023-11-02 19:20 ` Ferruh Yigit
                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Kaiwen Deng @ 2023-08-04  8:28 UTC (permalink / raw)
  To: dev
  Cc: stable, qiming.yang, yidingx.zhou, Kaiwen Deng, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

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. Testpmd does not trim these IP packets to the true
length of the frame, so errors will occur when calculating TCP
or UDP checksum.

This commit fixes this issue by triming IP packets to the true
length of the frame in testpmd.

Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum engine")
Cc: stable@dpdk.org

Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
---
 app/test-pmd/csumonly.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c
index 7af635e3f7..58b72b714a 100644
--- a/app/test-pmd/csumonly.c
+++ b/app/test-pmd/csumonly.c
@@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
 	uint16_t nb_rx;
 	uint16_t nb_prep;
 	uint16_t i;
+	uint16_t pad_len;
 	uint64_t rx_ol_flags, tx_ol_flags;
 	uint64_t tx_offloads;
 	uint32_t rx_bad_ip_csum;
 	uint32_t rx_bad_l4_csum;
 	uint32_t rx_bad_outer_l4_csum;
 	uint32_t rx_bad_outer_ip_csum;
+	uint32_t l3_off;
 	struct testpmd_offload_info info;
 
 	/* receive a burst of packet */
@@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
 			l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
 		}
 
+		if (info.is_tunnel) {
+			l3_off = info.outer_l2_len +
+					info.outer_l3_len +
+					info.l2_len;
+		} else {
+			l3_off = info.l2_len;
+		}
+		switch (info.ethertype) {
+		case _htons(RTE_ETHER_TYPE_IPV4):
+			pad_len = rte_pktmbuf_data_len(m) -
+					(l3_off +
+					rte_be_to_cpu_16(
+					((struct rte_ipv4_hdr *)l3_hdr)->total_length));
+			break;
+		case _htons(RTE_ETHER_TYPE_IPV6):
+			pad_len = rte_pktmbuf_data_len(m) -
+					(l3_off +
+					rte_be_to_cpu_16(
+					((struct rte_ipv6_hdr *)l3_hdr)->payload_len));
+			break;
+		default:
+			pad_len = 0;
+			break;
+		}
+
+		if (pad_len) {
+			rte_pktmbuf_data_len(m) = rte_pktmbuf_data_len(m) - pad_len;
+			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
+		}
+
 		/* step 2: depending on user command line configuration,
 		 * recompute checksum either in software or flag the
 		 * mbuf to offload the calculation to the NIC. If TSO
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-08-04  8:28 [PATCH] app/test-pmd: fix L4 checksum with padding data Kaiwen Deng
@ 2023-11-02 19:20 ` Ferruh Yigit
  2023-11-03  2:49   ` Deng, KaiwenX
  2023-11-16 22:58   ` Stephen Hemminger
  2023-11-17  1:13 ` Ferruh Yigit
  2023-12-07  8:53 ` [PATCH v2] app/test-pmd: fix tcp/udp cksum " Kaiwen Deng
  2 siblings, 2 replies; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-02 19:20 UTC (permalink / raw)
  To: Kaiwen Deng, dev
  Cc: stable, qiming.yang, yidingx.zhou, Aman Singh, Yuying Zhang,
	Olivier Matz, Pablo de Lara

On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> 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. Testpmd does not trim these IP packets to the true
> length of the frame, so errors will occur when calculating TCP
> or UDP checksum.
> 

Hi Kaiwen,

I am trying to understand the problem, what is the testcase that has
checksum error?

Are the received mbuf data_len & pkt_len wrong? Instead of trying to fix
the mbuf during forwarding, can we fix where packet generated?

> This commit fixes this issue by triming IP packets to the true
> length of the frame in testpmd.
> 
> Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum engine")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
> ---
>  app/test-pmd/csumonly.c | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c
> index 7af635e3f7..58b72b714a 100644
> --- a/app/test-pmd/csumonly.c
> +++ b/app/test-pmd/csumonly.c
> @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
>  	uint16_t nb_rx;
>  	uint16_t nb_prep;
>  	uint16_t i;
> +	uint16_t pad_len;
>  	uint64_t rx_ol_flags, tx_ol_flags;
>  	uint64_t tx_offloads;
>  	uint32_t rx_bad_ip_csum;
>  	uint32_t rx_bad_l4_csum;
>  	uint32_t rx_bad_outer_l4_csum;
>  	uint32_t rx_bad_outer_ip_csum;
> +	uint32_t l3_off;
>  	struct testpmd_offload_info info;
>  
>  	/* receive a burst of packet */
> @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
>  			l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
>  		}
>  
> +		if (info.is_tunnel) {
> +			l3_off = info.outer_l2_len +
> +					info.outer_l3_len +
> +					info.l2_len;
> +		} else {
> +			l3_off = info.l2_len;
> +		}
> +		switch (info.ethertype) {
> +		case _htons(RTE_ETHER_TYPE_IPV4):
> +			pad_len = rte_pktmbuf_data_len(m) -
> +					(l3_off +
> +					rte_be_to_cpu_16(
> +					((struct rte_ipv4_hdr *)l3_hdr)->total_length));
> +			break;
> +		case _htons(RTE_ETHER_TYPE_IPV6):
> +			pad_len = rte_pktmbuf_data_len(m) -
> +					(l3_off +
> +					rte_be_to_cpu_16(
> +					((struct rte_ipv6_hdr *)l3_hdr)->payload_len));
> +			break;
> +		default:
> +			pad_len = 0;
> +			break;
> +		}
> +
> +		if (pad_len) {
> +			rte_pktmbuf_data_len(m) = rte_pktmbuf_data_len(m) - pad_len;
> +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
> +		}
> +
>  		/* step 2: depending on user command line configuration,
>  		 * recompute checksum either in software or flag the
>  		 * mbuf to offload the calculation to the NIC. If TSO


^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-02 19:20 ` Ferruh Yigit
@ 2023-11-03  2:49   ` Deng, KaiwenX
  2023-11-03  4:03     ` Ferruh Yigit
  2023-11-16 22:58   ` Stephen Hemminger
  1 sibling, 1 reply; 29+ messages in thread
From: Deng, KaiwenX @ 2023-11-03  2:49 UTC (permalink / raw)
  To: Ferruh Yigit, dev
  Cc: stable, Yang, Qiming, Zhou, YidingX, Singh, Aman Deep, Zhang,
	Yuying, Matz, Olivier, De Lara Guarch, Pablo



> -----Original Message-----
> From: Ferruh Yigit <ferruh.yigit@amd.com>
> Sent: Friday, November 3, 2023 3:20 AM
> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> <pablo.de.lara.guarch@intel.com>
> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> 
> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> > 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.
> > Testpmd does not trim these IP packets to the true length of the
> > frame, so errors will occur when calculating TCP or UDP checksum.
> >
> 
> Hi Kaiwen,
> 
> I am trying to understand the problem, what is the testcase that has checksum
> error?
> 
> Are the received mbuf data_len & pkt_len wrong? Instead of trying to fix the
> mbuf during forwarding, can we fix where packet generated?
> 
Hi Ferruh,

In effect, the packet is padded by the switch. 
IEEE 802 packets may have a minimum size limit. The data fields should 
be padded by switch when necessary. In some switches, the padding data is not zero. 

Csumonly doesn't trim these packets to the true length of the frame. 
In csumonly, the received mbuf data_len is the true length of the packet plus the padding data len.
Therefore, padding data is included in the checksum calculation.
When the padding data is not zero, the checksum is wrong.

> > This commit fixes this issue by triming IP packets to the true length
> > of the frame in testpmd.
> >
> > Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum
> > engine")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
> > ---
> >  app/test-pmd/csumonly.c | 32 ++++++++++++++++++++++++++++++++
> >  1 file changed, 32 insertions(+)
> >
> > diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c index
> > 7af635e3f7..58b72b714a 100644
> > --- a/app/test-pmd/csumonly.c
> > +++ b/app/test-pmd/csumonly.c
> > @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct
> fwd_stream *fs)
> >  	uint16_t nb_rx;
> >  	uint16_t nb_prep;
> >  	uint16_t i;
> > +	uint16_t pad_len;
> >  	uint64_t rx_ol_flags, tx_ol_flags;
> >  	uint64_t tx_offloads;
> >  	uint32_t rx_bad_ip_csum;
> >  	uint32_t rx_bad_l4_csum;
> >  	uint32_t rx_bad_outer_l4_csum;
> >  	uint32_t rx_bad_outer_ip_csum;
> > +	uint32_t l3_off;
> >  	struct testpmd_offload_info info;
> >
> >  	/* receive a burst of packet */
> > @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct fwd_stream
> *fs)
> >  			l3_hdr = (char *)l3_hdr + info.outer_l3_len +
> info.l2_len;
> >  		}
> >
> > +		if (info.is_tunnel) {
> > +			l3_off = info.outer_l2_len +
> > +					info.outer_l3_len +
> > +					info.l2_len;
> > +		} else {
> > +			l3_off = info.l2_len;
> > +		}
> > +		switch (info.ethertype) {
> > +		case _htons(RTE_ETHER_TYPE_IPV4):
> > +			pad_len = rte_pktmbuf_data_len(m) -
> > +					(l3_off +
> > +					rte_be_to_cpu_16(
> > +					((struct rte_ipv4_hdr *)l3_hdr)-
> >total_length));
> > +			break;
> > +		case _htons(RTE_ETHER_TYPE_IPV6):
> > +			pad_len = rte_pktmbuf_data_len(m) -
> > +					(l3_off +
> > +					rte_be_to_cpu_16(
> > +					((struct rte_ipv6_hdr *)l3_hdr)-
> >payload_len));
> > +			break;
> > +		default:
> > +			pad_len = 0;
> > +			break;
> > +		}
> > +
> > +		if (pad_len) {
> > +			rte_pktmbuf_data_len(m) =
> rte_pktmbuf_data_len(m) - pad_len;
> > +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
> > +		}
> > +
> >  		/* step 2: depending on user command line configuration,
> >  		 * recompute checksum either in software or flag the
> >  		 * mbuf to offload the calculation to the NIC. If TSO


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-03  2:49   ` Deng, KaiwenX
@ 2023-11-03  4:03     ` Ferruh Yigit
  2023-11-14  2:19       ` Deng, KaiwenX
  0 siblings, 1 reply; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-03  4:03 UTC (permalink / raw)
  To: Deng, KaiwenX, dev
  Cc: stable, Yang, Qiming, Zhou, YidingX, Singh, Aman Deep, Zhang,
	Yuying, Matz, Olivier, De Lara Guarch, Pablo

On 11/3/2023 2:49 AM, Deng, KaiwenX wrote:
> 
> 
>> -----Original Message-----
>> From: Ferruh Yigit <ferruh.yigit@amd.com>
>> Sent: Friday, November 3, 2023 3:20 AM
>> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
>> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
>> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
>> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
>> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
>> <pablo.de.lara.guarch@intel.com>
>> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
>>
>> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
>>> 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.
>>> Testpmd does not trim these IP packets to the true length of the
>>> frame, so errors will occur when calculating TCP or UDP checksum.
>>>
>>
>> Hi Kaiwen,
>>
>> I am trying to understand the problem, what is the testcase that has checksum
>> error?
>>
>> Are the received mbuf data_len & pkt_len wrong? Instead of trying to fix the
>> mbuf during forwarding, can we fix where packet generated?
>>
> Hi Ferruh,
> 
> In effect, the packet is padded by the switch. 
> IEEE 802 packets may have a minimum size limit. The data fields should 
> be padded by switch when necessary. In some switches, the padding data is not zero. 
> 
> Csumonly doesn't trim these packets to the true length of the frame. 
> In csumonly, the received mbuf data_len is the true length of the packet plus the padding data len.
> Therefore, padding data is included in the checksum calculation.
> When the padding data is not zero, the checksum is wrong.
> 

Thanks for clarification.

Even some non-zero padding added, it will calculate the csum
successfully, but I assume in this case csum becomes different than
expected csum and test fails?

In this case why not fix the generated packets, and make them compatible
to minimum size requirement? What is generating packets?


>>> This commit fixes this issue by triming IP packets to the true length
>>> of the frame in testpmd.
>>>
>>> Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum
>>> engine")
>>> Cc: stable@dpdk.org
>>>
>>> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
>>> ---
>>>  app/test-pmd/csumonly.c | 32 ++++++++++++++++++++++++++++++++
>>>  1 file changed, 32 insertions(+)
>>>
>>> diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c index
>>> 7af635e3f7..58b72b714a 100644
>>> --- a/app/test-pmd/csumonly.c
>>> +++ b/app/test-pmd/csumonly.c
>>> @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct
>> fwd_stream *fs)
>>>  	uint16_t nb_rx;
>>>  	uint16_t nb_prep;
>>>  	uint16_t i;
>>> +	uint16_t pad_len;
>>>  	uint64_t rx_ol_flags, tx_ol_flags;
>>>  	uint64_t tx_offloads;
>>>  	uint32_t rx_bad_ip_csum;
>>>  	uint32_t rx_bad_l4_csum;
>>>  	uint32_t rx_bad_outer_l4_csum;
>>>  	uint32_t rx_bad_outer_ip_csum;
>>> +	uint32_t l3_off;
>>>  	struct testpmd_offload_info info;
>>>
>>>  	/* receive a burst of packet */
>>> @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct fwd_stream
>> *fs)
>>>  			l3_hdr = (char *)l3_hdr + info.outer_l3_len +
>> info.l2_len;
>>>  		}
>>>
>>> +		if (info.is_tunnel) {
>>> +			l3_off = info.outer_l2_len +
>>> +					info.outer_l3_len +
>>> +					info.l2_len;
>>> +		} else {
>>> +			l3_off = info.l2_len;
>>> +		}
>>> +		switch (info.ethertype) {
>>> +		case _htons(RTE_ETHER_TYPE_IPV4):
>>> +			pad_len = rte_pktmbuf_data_len(m) -
>>> +					(l3_off +
>>> +					rte_be_to_cpu_16(
>>> +					((struct rte_ipv4_hdr *)l3_hdr)-
>>> total_length));
>>> +			break;
>>> +		case _htons(RTE_ETHER_TYPE_IPV6):
>>> +			pad_len = rte_pktmbuf_data_len(m) -
>>> +					(l3_off +
>>> +					rte_be_to_cpu_16(
>>> +					((struct rte_ipv6_hdr *)l3_hdr)-
>>> payload_len));
>>> +			break;
>>> +		default:
>>> +			pad_len = 0;
>>> +			break;
>>> +		}
>>> +
>>> +		if (pad_len) {
>>> +			rte_pktmbuf_data_len(m) =
>> rte_pktmbuf_data_len(m) - pad_len;
>>> +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
>>> +		}
>>> +
>>>  		/* step 2: depending on user command line configuration,
>>>  		 * recompute checksum either in software or flag the
>>>  		 * mbuf to offload the calculation to the NIC. If TSO
> 


^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-03  4:03     ` Ferruh Yigit
@ 2023-11-14  2:19       ` Deng, KaiwenX
  2023-11-14 19:09         ` Ferruh Yigit
  0 siblings, 1 reply; 29+ messages in thread
From: Deng, KaiwenX @ 2023-11-14  2:19 UTC (permalink / raw)
  To: Ferruh Yigit, dev
  Cc: stable, Yang, Qiming, Zhou, YidingX, Singh, Aman Deep, Zhang,
	Yuying, Matz, Olivier, De Lara Guarch, Pablo



> -----Original Message-----
> From: Ferruh Yigit <ferruh.yigit@amd.com>
> Sent: Friday, November 3, 2023 12:03 PM
> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> <pablo.de.lara.guarch@intel.com>
> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> 
> On 11/3/2023 2:49 AM, Deng, KaiwenX wrote:
> >
> >
> >> -----Original Message-----
> >> From: Ferruh Yigit <ferruh.yigit@amd.com>
> >> Sent: Friday, November 3, 2023 3:20 AM
> >> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
> >> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou,
> >> YidingX <yidingx.zhou@intel.com>; Singh, Aman Deep
> >> <aman.deep.singh@intel.com>; Zhang, Yuying <yuying.zhang@intel.com>;
> >> Matz, Olivier <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> >> <pablo.de.lara.guarch@intel.com>
> >> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> >>
> >> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> >>> 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.
> >>> Testpmd does not trim these IP packets to the true length of the
> >>> frame, so errors will occur when calculating TCP or UDP checksum.
> >>>
> >>
> >> Hi Kaiwen,
> >>
> >> I am trying to understand the problem, what is the testcase that has
> >> checksum error?
> >>
> >> Are the received mbuf data_len & pkt_len wrong? Instead of trying to
> >> fix the mbuf during forwarding, can we fix where packet generated?
> >>
> > Hi Ferruh,
> >
> > In effect, the packet is padded by the switch.
> > IEEE 802 packets may have a minimum size limit. The data fields should
> > be padded by switch when necessary. In some switches, the padding data is
> not zero.
> >
> > Csumonly doesn't trim these packets to the true length of the frame.
> > In csumonly, the received mbuf data_len is the true length of the packet plus
> the padding data len.
> > Therefore, padding data is included in the checksum calculation.
> > When the padding data is not zero, the checksum is wrong.
> >
Hi,
Sorry for late reply.
The minimum frame length specified by IEEE 802.3 is 64 bytes. In practice, 
there are many packets less than 64 bytes that are padding through the switch. 

We found this issue because some customers found that their packets could not 
calculate checksum correctly, they would send some packets less than 64 bytes, 
but our app didn't strip the padding data for such packets.
> 
> Thanks for clarification.
> 
> Even some non-zero padding added, it will calculate the csum successfully, but
> I assume in this case csum becomes different than expected csum and test
> fails?
> 
> In this case why not fix the generated packets, and make them compatible to
> minimum size requirement? What is generating packets?
> 
> 
> >>> This commit fixes this issue by triming IP packets to the true
> >>> length of the frame in testpmd.
> >>>
> >>> Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in
> >>> checksum
> >>> engine")
> >>> Cc: stable@dpdk.org
> >>>
> >>> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
> >>> ---
> >>>  app/test-pmd/csumonly.c | 32
> ++++++++++++++++++++++++++++++++
> >>>  1 file changed, 32 insertions(+)
> >>>
> >>> diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c index
> >>> 7af635e3f7..58b72b714a 100644
> >>> --- a/app/test-pmd/csumonly.c
> >>> +++ b/app/test-pmd/csumonly.c
> >>> @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct
> >> fwd_stream *fs)
> >>>  	uint16_t nb_rx;
> >>>  	uint16_t nb_prep;
> >>>  	uint16_t i;
> >>> +	uint16_t pad_len;
> >>>  	uint64_t rx_ol_flags, tx_ol_flags;
> >>>  	uint64_t tx_offloads;
> >>>  	uint32_t rx_bad_ip_csum;
> >>>  	uint32_t rx_bad_l4_csum;
> >>>  	uint32_t rx_bad_outer_l4_csum;
> >>>  	uint32_t rx_bad_outer_ip_csum;
> >>> +	uint32_t l3_off;
> >>>  	struct testpmd_offload_info info;
> >>>
> >>>  	/* receive a burst of packet */
> >>> @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct
> fwd_stream
> >> *fs)
> >>>  			l3_hdr = (char *)l3_hdr + info.outer_l3_len +
> >> info.l2_len;
> >>>  		}
> >>>
> >>> +		if (info.is_tunnel) {
> >>> +			l3_off = info.outer_l2_len +
> >>> +					info.outer_l3_len +
> >>> +					info.l2_len;
> >>> +		} else {
> >>> +			l3_off = info.l2_len;
> >>> +		}
> >>> +		switch (info.ethertype) {
> >>> +		case _htons(RTE_ETHER_TYPE_IPV4):
> >>> +			pad_len = rte_pktmbuf_data_len(m) -
> >>> +					(l3_off +
> >>> +					rte_be_to_cpu_16(
> >>> +					((struct rte_ipv4_hdr *)l3_hdr)-
> >>> total_length));
> >>> +			break;
> >>> +		case _htons(RTE_ETHER_TYPE_IPV6):
> >>> +			pad_len = rte_pktmbuf_data_len(m) -
> >>> +					(l3_off +
> >>> +					rte_be_to_cpu_16(
> >>> +					((struct rte_ipv6_hdr *)l3_hdr)-
> >>> payload_len));
> >>> +			break;
> >>> +		default:
> >>> +			pad_len = 0;
> >>> +			break;
> >>> +		}
> >>> +
> >>> +		if (pad_len) {
> >>> +			rte_pktmbuf_data_len(m) =
> >> rte_pktmbuf_data_len(m) - pad_len;
> >>> +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
> >>> +		}
> >>> +
> >>>  		/* step 2: depending on user command line configuration,
> >>>  		 * recompute checksum either in software or flag the
> >>>  		 * mbuf to offload the calculation to the NIC. If TSO
> >


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-14  2:19       ` Deng, KaiwenX
@ 2023-11-14 19:09         ` Ferruh Yigit
  2023-11-16  7:02           ` Deng, KaiwenX
  0 siblings, 1 reply; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-14 19:09 UTC (permalink / raw)
  To: Deng, KaiwenX, dev
  Cc: stable, Yang, Qiming, Zhou, YidingX, Singh, Aman Deep, Zhang,
	Yuying, Matz, Olivier, De Lara Guarch, Pablo

On 11/14/2023 2:19 AM, Deng, KaiwenX wrote:
> 
> 
>> -----Original Message-----
>> From: Ferruh Yigit <ferruh.yigit@amd.com>
>> Sent: Friday, November 3, 2023 12:03 PM
>> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
>> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
>> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
>> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
>> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
>> <pablo.de.lara.guarch@intel.com>
>> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
>>
>> On 11/3/2023 2:49 AM, Deng, KaiwenX wrote:
>>>
>>>
>>>> -----Original Message-----
>>>> From: Ferruh Yigit <ferruh.yigit@amd.com>
>>>> Sent: Friday, November 3, 2023 3:20 AM
>>>> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
>>>> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou,
>>>> YidingX <yidingx.zhou@intel.com>; Singh, Aman Deep
>>>> <aman.deep.singh@intel.com>; Zhang, Yuying <yuying.zhang@intel.com>;
>>>> Matz, Olivier <olivier.matz@6wind.com>; De Lara Guarch, Pablo
>>>> <pablo.de.lara.guarch@intel.com>
>>>> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
>>>>
>>>> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
>>>>> 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.
>>>>> Testpmd does not trim these IP packets to the true length of the
>>>>> frame, so errors will occur when calculating TCP or UDP checksum.
>>>>>
>>>>
>>>> Hi Kaiwen,
>>>>
>>>> I am trying to understand the problem, what is the testcase that has
>>>> checksum error?
>>>>
>>>> Are the received mbuf data_len & pkt_len wrong? Instead of trying to
>>>> fix the mbuf during forwarding, can we fix where packet generated?
>>>>
>>> Hi Ferruh,
>>>
>>> In effect, the packet is padded by the switch.
>>> IEEE 802 packets may have a minimum size limit. The data fields should
>>> be padded by switch when necessary. In some switches, the padding data is
>> not zero.
>>>
>>> Csumonly doesn't trim these packets to the true length of the frame.
>>> In csumonly, the received mbuf data_len is the true length of the packet plus
>> the padding data len.
>>> Therefore, padding data is included in the checksum calculation.
>>> When the padding data is not zero, the checksum is wrong.
>>>
> Hi,
> Sorry for late reply.
> The minimum frame length specified by IEEE 802.3 is 64 bytes. In practice, 
> there are many packets less than 64 bytes that are padding through the switch. 
> 
> We found this issue because some customers found that their packets could not 
> calculate checksum correctly, they would send some packets less than 64 bytes, 
> but our app didn't strip the padding data for such packets.
>

OK, so switch in between is padding packets to make them compatible with
standard.

From DPDK application perspective received packet is 64 bytes, right?
Problem happens because where verifies the checksum gets different
checksum that expected, but this is because packet is modified in
between by the networking setup.
I am not sure about trying to fix this in the testpmd.

Why not send packets that are >= 64 bytes from sender side, or configure
switch to not add padding or maybe use different switch?


>>
>> Thanks for clarification.
>>
>> Even some non-zero padding added, it will calculate the csum successfully, but
>> I assume in this case csum becomes different than expected csum and test
>> fails?
>>
>> In this case why not fix the generated packets, and make them compatible to
>> minimum size requirement? What is generating packets?
>>
>>
>>>>> This commit fixes this issue by triming IP packets to the true
>>>>> length of the frame in testpmd.
>>>>>
>>>>> Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in
>>>>> checksum
>>>>> engine")
>>>>> Cc: stable@dpdk.org
>>>>>
>>>>> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
>>>>> ---
>>>>>  app/test-pmd/csumonly.c | 32
>> ++++++++++++++++++++++++++++++++
>>>>>  1 file changed, 32 insertions(+)
>>>>>
>>>>> diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c index
>>>>> 7af635e3f7..58b72b714a 100644
>>>>> --- a/app/test-pmd/csumonly.c
>>>>> +++ b/app/test-pmd/csumonly.c
>>>>> @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct
>>>> fwd_stream *fs)
>>>>>  	uint16_t nb_rx;
>>>>>  	uint16_t nb_prep;
>>>>>  	uint16_t i;
>>>>> +	uint16_t pad_len;
>>>>>  	uint64_t rx_ol_flags, tx_ol_flags;
>>>>>  	uint64_t tx_offloads;
>>>>>  	uint32_t rx_bad_ip_csum;
>>>>>  	uint32_t rx_bad_l4_csum;
>>>>>  	uint32_t rx_bad_outer_l4_csum;
>>>>>  	uint32_t rx_bad_outer_ip_csum;
>>>>> +	uint32_t l3_off;
>>>>>  	struct testpmd_offload_info info;
>>>>>
>>>>>  	/* receive a burst of packet */
>>>>> @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct
>> fwd_stream
>>>> *fs)
>>>>>  			l3_hdr = (char *)l3_hdr + info.outer_l3_len +
>>>> info.l2_len;
>>>>>  		}
>>>>>
>>>>> +		if (info.is_tunnel) {
>>>>> +			l3_off = info.outer_l2_len +
>>>>> +					info.outer_l3_len +
>>>>> +					info.l2_len;
>>>>> +		} else {
>>>>> +			l3_off = info.l2_len;
>>>>> +		}
>>>>> +		switch (info.ethertype) {
>>>>> +		case _htons(RTE_ETHER_TYPE_IPV4):
>>>>> +			pad_len = rte_pktmbuf_data_len(m) -
>>>>> +					(l3_off +
>>>>> +					rte_be_to_cpu_16(
>>>>> +					((struct rte_ipv4_hdr *)l3_hdr)-
>>>>> total_length));
>>>>> +			break;
>>>>> +		case _htons(RTE_ETHER_TYPE_IPV6):
>>>>> +			pad_len = rte_pktmbuf_data_len(m) -
>>>>> +					(l3_off +
>>>>> +					rte_be_to_cpu_16(
>>>>> +					((struct rte_ipv6_hdr *)l3_hdr)-
>>>>> payload_len));
>>>>> +			break;
>>>>> +		default:
>>>>> +			pad_len = 0;
>>>>> +			break;
>>>>> +		}
>>>>> +
>>>>> +		if (pad_len) {
>>>>> +			rte_pktmbuf_data_len(m) =
>>>> rte_pktmbuf_data_len(m) - pad_len;
>>>>> +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
>>>>> +		}
>>>>> +
>>>>>  		/* step 2: depending on user command line configuration,
>>>>>  		 * recompute checksum either in software or flag the
>>>>>  		 * mbuf to offload the calculation to the NIC. If TSO
>>>
> 


^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-14 19:09         ` Ferruh Yigit
@ 2023-11-16  7:02           ` Deng, KaiwenX
  0 siblings, 0 replies; 29+ messages in thread
From: Deng, KaiwenX @ 2023-11-16  7:02 UTC (permalink / raw)
  To: Ferruh Yigit, dev
  Cc: stable, Yang, Qiming, Zhou, YidingX, Singh, Aman Deep, Zhang,
	Yuying, Matz, Olivier, De Lara Guarch, Pablo



> -----Original Message-----
> From: Ferruh Yigit <ferruh.yigit@amd.com>
> Sent: Wednesday, November 15, 2023 3:10 AM
> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> <pablo.de.lara.guarch@intel.com>
> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> 
> On 11/14/2023 2:19 AM, Deng, KaiwenX wrote:
> >
> >
> >> -----Original Message-----
> >> From: Ferruh Yigit <ferruh.yigit@amd.com>
> >> Sent: Friday, November 3, 2023 12:03 PM
> >> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
> >> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou,
> >> YidingX <yidingx.zhou@intel.com>; Singh, Aman Deep
> >> <aman.deep.singh@intel.com>; Zhang, Yuying <yuying.zhang@intel.com>;
> >> Matz, Olivier <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> >> <pablo.de.lara.guarch@intel.com>
> >> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> >>
> >> On 11/3/2023 2:49 AM, Deng, KaiwenX wrote:
> >>>
> >>>
> >>>> -----Original Message-----
> >>>> From: Ferruh Yigit <ferruh.yigit@amd.com>
> >>>> Sent: Friday, November 3, 2023 3:20 AM
> >>>> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
> >>>> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou,
> >>>> YidingX <yidingx.zhou@intel.com>; Singh, Aman Deep
> >>>> <aman.deep.singh@intel.com>; Zhang, Yuying
> >>>> <yuying.zhang@intel.com>; Matz, Olivier <olivier.matz@6wind.com>;
> >>>> De Lara Guarch, Pablo <pablo.de.lara.guarch@intel.com>
> >>>> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding
> >>>> data
> >>>>
> >>>> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> >>>>> 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.
> >>>>> Testpmd does not trim these IP packets to the true length of the
> >>>>> frame, so errors will occur when calculating TCP or UDP checksum.
> >>>>>
> >>>>
> >>>> Hi Kaiwen,
> >>>>
> >>>> I am trying to understand the problem, what is the testcase that
> >>>> has checksum error?
> >>>>
> >>>> Are the received mbuf data_len & pkt_len wrong? Instead of trying
> >>>> to fix the mbuf during forwarding, can we fix where packet generated?
> >>>>
> >>> Hi Ferruh,
> >>>
> >>> In effect, the packet is padded by the switch.
> >>> IEEE 802 packets may have a minimum size limit. The data fields
> >>> should be padded by switch when necessary. In some switches, the
> >>> padding data is
> >> not zero.
> >>>
> >>> Csumonly doesn't trim these packets to the true length of the frame.
> >>> In csumonly, the received mbuf data_len is the true length of the
> >>> packet plus
> >> the padding data len.
> >>> Therefore, padding data is included in the checksum calculation.
> >>> When the padding data is not zero, the checksum is wrong.
> >>>
> > Hi,
> > Sorry for late reply.
> > The minimum frame length specified by IEEE 802.3 is 64 bytes. In
> > practice, there are many packets less than 64 bytes that are padding through
> the switch.
> >
> > We found this issue because some customers found that their packets
> > could not calculate checksum correctly, they would send some packets
> > less than 64 bytes, but our app didn't strip the padding data for such
> packets.
> >
> 
> OK, so switch in between is padding packets to make them compatible with
> standard.
> 
> From DPDK application perspective received packet is 64 bytes, right?
> Problem happens because where verifies the checksum gets different
> checksum that expected, but this is because packet is modified in between by
> the networking setup.
> I am not sure about trying to fix this in the testpmd.
> 
> Why not send packets that are >= 64 bytes from sender side, or configure
> switch to not add padding or maybe use different switch?
> 
If we send a 40 bytes UDP packet, it will be padded to 64 bytes as it passes through 
the switch, whereas the Linux kernel stack strips out the padding data as it receives 
the packet. 
I think maybe DPDK applications should be aligned with the Linux kernel. 
Otherwise the csumonly application only supports packets above 64 bytes.
> 
> >>
> >> Thanks for clarification.
> >>
> >> Even some non-zero padding added, it will calculate the csum
> >> successfully, but I assume in this case csum becomes different than
> >> expected csum and test fails?
> >>
> >> In this case why not fix the generated packets, and make them
> >> compatible to minimum size requirement? What is generating packets?
> >>
> >>
> >>>>> This commit fixes this issue by triming IP packets to the true
> >>>>> length of the frame in testpmd.
> >>>>>
> >>>>> Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in
> >>>>> checksum
> >>>>> engine")
> >>>>> Cc: stable@dpdk.org
> >>>>>
> >>>>> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
> >>>>> ---
> >>>>>  app/test-pmd/csumonly.c | 32
> >> ++++++++++++++++++++++++++++++++
> >>>>>  1 file changed, 32 insertions(+)
> >>>>>
> >>>>> diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c
> >>>>> index 7af635e3f7..58b72b714a 100644
> >>>>> --- a/app/test-pmd/csumonly.c
> >>>>> +++ b/app/test-pmd/csumonly.c
> >>>>> @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct
> >>>> fwd_stream *fs)
> >>>>>  	uint16_t nb_rx;
> >>>>>  	uint16_t nb_prep;
> >>>>>  	uint16_t i;
> >>>>> +	uint16_t pad_len;
> >>>>>  	uint64_t rx_ol_flags, tx_ol_flags;
> >>>>>  	uint64_t tx_offloads;
> >>>>>  	uint32_t rx_bad_ip_csum;
> >>>>>  	uint32_t rx_bad_l4_csum;
> >>>>>  	uint32_t rx_bad_outer_l4_csum;
> >>>>>  	uint32_t rx_bad_outer_ip_csum;
> >>>>> +	uint32_t l3_off;
> >>>>>  	struct testpmd_offload_info info;
> >>>>>
> >>>>>  	/* receive a burst of packet */
> >>>>> @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct
> >> fwd_stream
> >>>> *fs)
> >>>>>  			l3_hdr = (char *)l3_hdr + info.outer_l3_len +
> >>>> info.l2_len;
> >>>>>  		}
> >>>>>
> >>>>> +		if (info.is_tunnel) {
> >>>>> +			l3_off = info.outer_l2_len +
> >>>>> +					info.outer_l3_len +
> >>>>> +					info.l2_len;
> >>>>> +		} else {
> >>>>> +			l3_off = info.l2_len;
> >>>>> +		}
> >>>>> +		switch (info.ethertype) {
> >>>>> +		case _htons(RTE_ETHER_TYPE_IPV4):
> >>>>> +			pad_len = rte_pktmbuf_data_len(m) -
> >>>>> +					(l3_off +
> >>>>> +					rte_be_to_cpu_16(
> >>>>> +					((struct rte_ipv4_hdr *)l3_hdr)-
> >>>>> total_length));
> >>>>> +			break;
> >>>>> +		case _htons(RTE_ETHER_TYPE_IPV6):
> >>>>> +			pad_len = rte_pktmbuf_data_len(m) -
> >>>>> +					(l3_off +
> >>>>> +					rte_be_to_cpu_16(
> >>>>> +					((struct rte_ipv6_hdr *)l3_hdr)-
> >>>>> payload_len));
> >>>>> +			break;
> >>>>> +		default:
> >>>>> +			pad_len = 0;
> >>>>> +			break;
> >>>>> +		}
> >>>>> +
> >>>>> +		if (pad_len) {
> >>>>> +			rte_pktmbuf_data_len(m) =
> >>>> rte_pktmbuf_data_len(m) - pad_len;
> >>>>> +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
> >>>>> +		}
> >>>>> +
> >>>>>  		/* step 2: depending on user command line configuration,
> >>>>>  		 * recompute checksum either in software or flag the
> >>>>>  		 * mbuf to offload the calculation to the NIC. If TSO
> >>>
> >


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-02 19:20 ` Ferruh Yigit
  2023-11-03  2:49   ` Deng, KaiwenX
@ 2023-11-16 22:58   ` Stephen Hemminger
  2023-11-17  0:50     ` Ferruh Yigit
  1 sibling, 1 reply; 29+ messages in thread
From: Stephen Hemminger @ 2023-11-16 22:58 UTC (permalink / raw)
  To: Ferruh Yigit
  Cc: Kaiwen Deng, dev, stable, qiming.yang, yidingx.zhou, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

On Thu, 2 Nov 2023 19:20:07 +0000
Ferruh Yigit <ferruh.yigit@amd.com> wrote:

> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> > 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. Testpmd does not trim these IP packets to the true
> > length of the frame, so errors will occur when calculating TCP
> > or UDP checksum.
> >   
> 
> Hi Kaiwen,
> 
> I am trying to understand the problem, what is the testcase that has
> checksum error?
> 
> Are the received mbuf data_len & pkt_len wrong? Instead of trying to fix
> the mbuf during forwarding, can we fix where packet generated?

The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len
which maybe larger than the actual data. The real issue is there and
in rte_ip.h checksum code. The correct fix would be to use l3_len instead.

It also looks like test-pmd is not validating the IP header.
Both parse_ipv4() and parse_ipv6() should check if packet was truncated.
Same for both UDP and TCP lengths.


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-16 22:58   ` Stephen Hemminger
@ 2023-11-17  0:50     ` Ferruh Yigit
  2023-11-17  3:28       ` Stephen Hemminger
  2023-11-20  9:21       ` Deng, KaiwenX
  0 siblings, 2 replies; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-17  0:50 UTC (permalink / raw)
  To: Stephen Hemminger
  Cc: Kaiwen Deng, dev, stable, qiming.yang, yidingx.zhou, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

On 11/16/2023 10:58 PM, Stephen Hemminger wrote:
> On Thu, 2 Nov 2023 19:20:07 +0000
> Ferruh Yigit <ferruh.yigit@amd.com> wrote:
> 
>> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
>>> 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. Testpmd does not trim these IP packets to the true
>>> length of the frame, so errors will occur when calculating TCP
>>> or UDP checksum.
>>>   
>>
>> Hi Kaiwen,
>>
>> I am trying to understand the problem, what is the testcase that has
>> checksum error?
>>
>> Are the received mbuf data_len & pkt_len wrong? Instead of trying to fix
>> the mbuf during forwarding, can we fix where packet generated?
> 
> The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len
> which maybe larger than the actual data. The real issue is there and
> in rte_ip.h checksum code. The correct fix would be to use l3_len instead.
> 

I see, you are right.

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.


I agree using 'l3_len' instead is correct fix.

But this requires ABI/API change,
plus do we have any reason to keep the padding, discarding it as this
patch does is also simpler alternative.


Other alternative can be to zero the padding bytes. I guess standard
doesn't enforce them to be zero, but we can do this to remove its impact
on checksum calculation.


@Kaiwen, did you able to test this with HW offload, what is the behavior
of the HW, does is remove padding bytes?


> It also looks like test-pmd is not validating the IP header.
> Both parse_ipv4() and parse_ipv6() should check if packet was truncated.
> Same for both UDP and TCP lengths.
> 



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-08-04  8:28 [PATCH] app/test-pmd: fix L4 checksum with padding data Kaiwen Deng
  2023-11-02 19:20 ` Ferruh Yigit
@ 2023-11-17  1:13 ` Ferruh Yigit
  2023-11-20  9:52   ` Deng, KaiwenX
  2023-12-07  8:53 ` [PATCH v2] app/test-pmd: fix tcp/udp cksum " Kaiwen Deng
  2 siblings, 1 reply; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-17  1:13 UTC (permalink / raw)
  To: Kaiwen Deng, dev
  Cc: stable, qiming.yang, yidingx.zhou, Aman Singh, Yuying Zhang,
	Olivier Matz, Pablo de Lara

On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> 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. Testpmd does not trim these IP packets to the true
> length of the frame, so errors will occur when calculating TCP
> or UDP checksum.
> 
> This commit fixes this issue by triming IP packets to the true
> length of the frame in testpmd.
> 
> Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum engine")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
> ---
>  app/test-pmd/csumonly.c | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c
> index 7af635e3f7..58b72b714a 100644
> --- a/app/test-pmd/csumonly.c
> +++ b/app/test-pmd/csumonly.c
> @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
>  	uint16_t nb_rx;
>  	uint16_t nb_prep;
>  	uint16_t i;
> +	uint16_t pad_len;
>  	uint64_t rx_ol_flags, tx_ol_flags;
>  	uint64_t tx_offloads;
>  	uint32_t rx_bad_ip_csum;
>  	uint32_t rx_bad_l4_csum;
>  	uint32_t rx_bad_outer_l4_csum;
>  	uint32_t rx_bad_outer_ip_csum;
> +	uint32_t l3_off;
>  	struct testpmd_offload_info info;
>  
>  	/* receive a burst of packet */
> @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
>  			l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
>  		}
>  
> +		if (info.is_tunnel) {
> +			l3_off = info.outer_l2_len +
> +					info.outer_l3_len +
> +					info.l2_len;
>

I don't know much about tunnel code but is above calculation correct for
all tunnel protocols, like for the case inner packet over UDP, should
outer l4_len also added etc...


> +		} else {
> +			l3_off = info.l2_len;
> +		}
> +		switch (info.ethertype) {
> +		case _htons(RTE_ETHER_TYPE_IPV4):
> +			pad_len = rte_pktmbuf_data_len(m) -
> +					(l3_off +
> +					rte_be_to_cpu_16(
> +					((struct rte_ipv4_hdr *)l3_hdr)->total_length));
> +			break;
> +		case _htons(RTE_ETHER_TYPE_IPV6):
> +			pad_len = rte_pktmbuf_data_len(m) -
> +					(l3_off +
> +					rte_be_to_cpu_16(
> +					((struct rte_ipv6_hdr *)l3_hdr)->payload_len));
>

As far as I remember ipv6 payload_len doesn't contain the header length,
so pad_len calculation should be different than ipv4 one,
like "l4_off + l3_hdr->payload_len", did you verify this code with ipv6?


> +			break;
> +		default:
> +			pad_len = 0;
> +			break;
> +		}
> +
> +		if (pad_len) {
> +			rte_pktmbuf_data_len(m) = rte_pktmbuf_data_len(m) - pad_len;
> +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
>

Can't received mbuf be multi-segment mbuf, as far as I can see checksum
calculation API takes this possibility into account. If so need to check
that possibility here before updating 'pkt_len'



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17  0:50     ` Ferruh Yigit
@ 2023-11-17  3:28       ` Stephen Hemminger
  2023-11-17  9:29         ` Ferruh Yigit
  2023-11-20  9:21       ` Deng, KaiwenX
  1 sibling, 1 reply; 29+ messages in thread
From: Stephen Hemminger @ 2023-11-17  3:28 UTC (permalink / raw)
  To: Ferruh Yigit
  Cc: Kaiwen Deng, dev, stable, qiming.yang, yidingx.zhou, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

On Fri, 17 Nov 2023 00:50:16 +0000
Ferruh Yigit <ferruh.yigit@amd.com> wrote:

> >> Hi Kaiwen,
> >>
> >> I am trying to understand the problem, what is the testcase that has
> >> checksum error?
> >>
> >> Are the received mbuf data_len & pkt_len wrong? Instead of trying to fix
> >> the mbuf during forwarding, can we fix where packet generated?  
> > 
> > The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len
> > which maybe larger than the actual data. The real issue is there and
> > in rte_ip.h checksum code. The correct fix would be to use l3_len instead.
> >   
> 
> I see, you are right.
> 
> 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.
> 
> 
> I agree using 'l3_len' instead is correct fix.
> 
> But this requires ABI/API change,
> plus do we have any reason to keep the padding, discarding it as this
> patch does is also simpler alternative.


Possibly an API version to change the args would work to fix.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17  3:28       ` Stephen Hemminger
@ 2023-11-17  9:29         ` Ferruh Yigit
  2023-11-17 12:11           ` Morten Brørup
  2023-11-17 16:22           ` Stephen Hemminger
  0 siblings, 2 replies; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-17  9:29 UTC (permalink / raw)
  To: Stephen Hemminger
  Cc: Kaiwen Deng, dev, stable, qiming.yang, yidingx.zhou, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

On 11/17/2023 3:28 AM, Stephen Hemminger wrote:
> On Fri, 17 Nov 2023 00:50:16 +0000
> Ferruh Yigit <ferruh.yigit@amd.com> wrote:
> 
>>>> Hi Kaiwen,
>>>>
>>>> I am trying to understand the problem, what is the testcase that has
>>>> checksum error?
>>>>
>>>> Are the received mbuf data_len & pkt_len wrong? Instead of trying to fix
>>>> the mbuf during forwarding, can we fix where packet generated?  
>>>
>>> The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len
>>> which maybe larger than the actual data. The real issue is there and
>>> in rte_ip.h checksum code. The correct fix would be to use l3_len instead.
>>>   
>>
>> I see, you are right.
>>
>> 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.
>>
>>
>> I agree using 'l3_len' instead is correct fix.
>>
>> But this requires ABI/API change,
>> plus do we have any reason to keep the padding, discarding it as this
>> patch does is also simpler alternative.
> 
> 
> Possibly an API version to change the args would work to fix.
>

rte_ipv4_udptcp_cksum_mbuf() and rte_ipv6_udptcp_cksum_mbuf() are inline
functions, unfortunately we can't version them.

But those functions already gets IP header as parameter, can't we use IP
header to get the payload size? If so this can be fixed without updating
API.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17  9:29         ` Ferruh Yigit
@ 2023-11-17 12:11           ` Morten Brørup
  2023-11-17 16:23             ` Stephen Hemminger
  2023-11-17 16:22           ` Stephen Hemminger
  1 sibling, 1 reply; 29+ messages in thread
From: Morten Brørup @ 2023-11-17 12:11 UTC (permalink / raw)
  To: Ferruh Yigit, Stephen Hemminger, Kaiwen Deng
  Cc: dev, stable, qiming.yang, yidingx.zhou, Aman Singh, Yuying Zhang,
	Olivier Matz, Pablo de Lara

> From: Ferruh Yigit [mailto:ferruh.yigit@amd.com]
> Sent: Friday, 17 November 2023 10.30
> 
> On 11/17/2023 3:28 AM, Stephen Hemminger wrote:
> > On Fri, 17 Nov 2023 00:50:16 +0000
> > Ferruh Yigit <ferruh.yigit@amd.com> wrote:
> >
> >>>> Hi Kaiwen,
> >>>>
> >>>> I am trying to understand the problem, what is the testcase that
> has
> >>>> checksum error?
> >>>>
> >>>> Are the received mbuf data_len & pkt_len wrong? Instead of trying
> to fix
> >>>> the mbuf during forwarding, can we fix where packet generated?
> >>>
> >>> The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len
> >>> which maybe larger than the actual data. The real issue is there
> and
> >>> in rte_ip.h checksum code. The correct fix would be to use l3_len
> instead.
> >>>
> >>
> >> I see, you are right.
> >>
> >> 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.
> >>
> >>
> >> I agree using 'l3_len' instead is correct fix.
> >>
> >> But this requires ABI/API change,
> >> plus do we have any reason to keep the padding, discarding it as
> this
> >> patch does is also simpler alternative.
> >
> >
> > Possibly an API version to change the args would work to fix.
> >
> 
> rte_ipv4_udptcp_cksum_mbuf() and rte_ipv6_udptcp_cksum_mbuf() are
> inline
> functions, unfortunately we can't version them.
> 
> But those functions already gets IP header as parameter, can't we use
> IP
> header to get the payload size? If so this can be fixed without
> updating
> API.

If rte_ipv4_udptcp_cksum_mbuf() - or any other function in the DPDK Network Headers library - includes Ethernet padding (which may be non-zero) when calculating the TCP/UDP checksum of an IPv4 packet, it is a bug, and must be fixed there.

Our test cases should use random padding to catch bugs like this.

And I just realized that Ethernet padding may be added to any IP packet, so don't assume that this bug only applies to small packets.


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17  9:29         ` Ferruh Yigit
  2023-11-17 12:11           ` Morten Brørup
@ 2023-11-17 16:22           ` Stephen Hemminger
  2023-11-20 10:47             ` Ferruh Yigit
  1 sibling, 1 reply; 29+ messages in thread
From: Stephen Hemminger @ 2023-11-17 16:22 UTC (permalink / raw)
  To: Ferruh Yigit
  Cc: Kaiwen Deng, dev, stable, qiming.yang, yidingx.zhou, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

On Fri, 17 Nov 2023 09:29:41 +0000
Ferruh Yigit <ferruh.yigit@amd.com> wrote:

> >> I agree using 'l3_len' instead is correct fix.
> >>
> >> But this requires ABI/API change,
> >> plus do we have any reason to keep the padding, discarding it as this
> >> patch does is also simpler alternative.  
> > 
> > 
> > Possibly an API version to change the args would work to fix.
> >  
> 
> rte_ipv4_udptcp_cksum_mbuf() and rte_ipv6_udptcp_cksum_mbuf() are inline
> functions, unfortunately we can't version them.
> 
> But those functions already gets IP header as parameter, can't we use IP
> header to get the payload size? If so this can be fixed without updating
> API.

Inlines are easier. Just make a fixed new function and make sure the old
one is not used.  They shouldn't have been inline in the first place.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17 12:11           ` Morten Brørup
@ 2023-11-17 16:23             ` Stephen Hemminger
  0 siblings, 0 replies; 29+ messages in thread
From: Stephen Hemminger @ 2023-11-17 16:23 UTC (permalink / raw)
  To: Morten Brørup
  Cc: Ferruh Yigit, Kaiwen Deng, dev, stable, qiming.yang,
	yidingx.zhou, Aman Singh, Yuying Zhang, Olivier Matz,
	Pablo de Lara

On Fri, 17 Nov 2023 13:11:50 +0100
Morten Brørup <mb@smartsharesystems.com> wrote:

> > rte_ipv4_udptcp_cksum_mbuf() and rte_ipv6_udptcp_cksum_mbuf() are
> > inline
> > functions, unfortunately we can't version them.
> > 
> > But those functions already gets IP header as parameter, can't we use
> > IP
> > header to get the payload size? If so this can be fixed without
> > updating
> > API.  
> 
> If rte_ipv4_udptcp_cksum_mbuf() - or any other function in the DPDK Network Headers library - includes Ethernet padding (which may be non-zero) when calculating the TCP/UDP checksum of an IPv4 packet, it is a bug, and must be fixed there.
> 
> Our test cases should use random padding to catch bugs like this.
> 
> And I just realized that Ethernet padding may be added to any IP packet, so don't assume that this bug only applies to small packets.

Agree. And test code needs lots more header checks it is way too trusting that mbuf is valid.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17  0:50     ` Ferruh Yigit
  2023-11-17  3:28       ` Stephen Hemminger
@ 2023-11-20  9:21       ` Deng, KaiwenX
  2023-11-20 10:46         ` Ferruh Yigit
  1 sibling, 1 reply; 29+ messages in thread
From: Deng, KaiwenX @ 2023-11-20  9:21 UTC (permalink / raw)
  To: Ferruh Yigit, Stephen Hemminger
  Cc: dev, stable, Yang,  Qiming, Zhou, YidingX, Singh, Aman Deep,
	Zhang, Yuying, Matz, Olivier, De Lara Guarch, Pablo



> -----Original Message-----
> From: Ferruh Yigit <ferruh.yigit@amd.com>
> Sent: Friday, November 17, 2023 8:50 AM
> To: Stephen Hemminger <stephen@networkplumber.org>
> Cc: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org;
> stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> <pablo.de.lara.guarch@intel.com>
> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> 
> On 11/16/2023 10:58 PM, Stephen Hemminger wrote:
> > On Thu, 2 Nov 2023 19:20:07 +0000
> > Ferruh Yigit <ferruh.yigit@amd.com> wrote:
> >
> >> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> >>> 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. Testpmd does not trim these IP packets to the true length
> >>> of the frame, so errors will occur when calculating TCP or UDP
> >>> checksum.
> >>>
> >>
> >> Hi Kaiwen,
> >>
> >> I am trying to understand the problem, what is the testcase that has
> >> checksum error?
> >>
> >> Are the received mbuf data_len & pkt_len wrong? Instead of trying to
> >> fix the mbuf during forwarding, can we fix where packet generated?
> >
> > The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len which
> > maybe larger than the actual data. The real issue is there and in
> > rte_ip.h checksum code. The correct fix would be to use l3_len instead.
> >
> 
> I see, you are right.
> 
> 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.
> 
> 
> I agree using 'l3_len' instead is correct fix.
> 
> But this requires ABI/API change,
> plus do we have any reason to keep the padding, discarding it as this patch
> does is also simpler alternative.
> 
> 
> Other alternative can be to zero the padding bytes. I guess standard doesn't
> enforce them to be zero, but we can do this to remove its impact on checksum
> calculation.
I'm not sure if this is ok, it feels like it would reduce performance. 
I can try this alternative if needed.
> 
> 
> @Kaiwen, did you able to test this with HW offload, what is the behavior of
> the HW, does is remove padding bytes?
> 
I tested the HW offload case and the same tcp/udp checksum error occurs when padding is not 0, 
But if change pkt_len to the true length of the frame, the checksum is correct.
> 
> > It also looks like test-pmd is not validating the IP header.
> > Both parse_ipv4() and parse_ipv6() should check if packet was truncated.
> > Same for both UDP and TCP lengths.
> >
> 


^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17  1:13 ` Ferruh Yigit
@ 2023-11-20  9:52   ` Deng, KaiwenX
  0 siblings, 0 replies; 29+ messages in thread
From: Deng, KaiwenX @ 2023-11-20  9:52 UTC (permalink / raw)
  To: Ferruh Yigit, dev
  Cc: stable, Yang, Qiming, Zhou, YidingX, Singh, Aman Deep, Zhang,
	Yuying, Matz, Olivier, De Lara Guarch, Pablo



> -----Original Message-----
> From: Ferruh Yigit <ferruh.yigit@amd.com>
> Sent: Friday, November 17, 2023 9:14 AM
> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org
> Cc: stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> <pablo.de.lara.guarch@intel.com>
> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> 
> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> > 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.
> > Testpmd does not trim these IP packets to the true length of the
> > frame, so errors will occur when calculating TCP or UDP checksum.
> >
> > This commit fixes this issue by triming IP packets to the true length
> > of the frame in testpmd.
> >
> > Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum
> > engine")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
> > ---
> >  app/test-pmd/csumonly.c | 32 ++++++++++++++++++++++++++++++++
> >  1 file changed, 32 insertions(+)
> >
> > diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c index
> > 7af635e3f7..58b72b714a 100644
> > --- a/app/test-pmd/csumonly.c
> > +++ b/app/test-pmd/csumonly.c
> > @@ -853,12 +853,14 @@ pkt_burst_checksum_forward(struct
> fwd_stream *fs)
> >  	uint16_t nb_rx;
> >  	uint16_t nb_prep;
> >  	uint16_t i;
> > +	uint16_t pad_len;
> >  	uint64_t rx_ol_flags, tx_ol_flags;
> >  	uint64_t tx_offloads;
> >  	uint32_t rx_bad_ip_csum;
> >  	uint32_t rx_bad_l4_csum;
> >  	uint32_t rx_bad_outer_l4_csum;
> >  	uint32_t rx_bad_outer_ip_csum;
> > +	uint32_t l3_off;
> >  	struct testpmd_offload_info info;
> >
> >  	/* receive a burst of packet */
> > @@ -980,6 +982,36 @@ pkt_burst_checksum_forward(struct fwd_stream
> *fs)
> >  			l3_hdr = (char *)l3_hdr + info.outer_l3_len +
> info.l2_len;
> >  		}
> >
> > +		if (info.is_tunnel) {
> > +			l3_off = info.outer_l2_len +
> > +					info.outer_l3_len +
> > +					info.l2_len;
> >
> 
> I don't know much about tunnel code but is above calculation correct for all
> tunnel protocols, like for the case inner packet over UDP, should outer l4_len
> also added etc...
According to the comments, these tunnel packets are supported.

* (1) Supported packets are:
 *   Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
 *   Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
 *           UDP|TCP|SCTP
 *   Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / Ether / IP|IP6 /
 *           UDP|TCP|SCTP
 *   Ether / (vlan) / outer IP|IP6 / outer UDP / VXLAN-GPE / IP|IP6 /
 *           UDP|TCP|SCTP
 *   Ether / (vlan) / outer IP / outer UDP / GTP / IP|IP6 / UDP|TCP|SCTP
 *   Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
 *   Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
 *   Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
> 
> 
> > +		} else {
> > +			l3_off = info.l2_len;
> > +		}
> > +		switch (info.ethertype) {
> > +		case _htons(RTE_ETHER_TYPE_IPV4):
> > +			pad_len = rte_pktmbuf_data_len(m) -
> > +					(l3_off +
> > +					rte_be_to_cpu_16(
> > +					((struct rte_ipv4_hdr *)l3_hdr)-
> >total_length));
> > +			break;
> > +		case _htons(RTE_ETHER_TYPE_IPV6):
> > +			pad_len = rte_pktmbuf_data_len(m) -
> > +					(l3_off +
> > +					rte_be_to_cpu_16(
> > +					((struct rte_ipv6_hdr *)l3_hdr)-
> >payload_len));
> >
> 
> As far as I remember ipv6 payload_len doesn't contain the header length, so
> pad_len calculation should be different than ipv4 one, like "l4_off + l3_hdr-
> >payload_len", did you verify this code with ipv6?
You're right, I didn't notice that and didn't test it adequately. I'll fix that.
> 
> 
> > +			break;
> > +		default:
> > +			pad_len = 0;
> > +			break;
> > +		}
> > +
> > +		if (pad_len) {
> > +			rte_pktmbuf_data_len(m) =
> rte_pktmbuf_data_len(m) - pad_len;
> > +			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
> >
> 
> Can't received mbuf be multi-segment mbuf, as far as I can see checksum
> calculation API takes this possibility into account. If so need to check that
> possibility here before updating 'pkt_len'
You are right.

Thanks
> 


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-20  9:21       ` Deng, KaiwenX
@ 2023-11-20 10:46         ` Ferruh Yigit
  2023-11-22  3:04           ` Deng, KaiwenX
  0 siblings, 1 reply; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-20 10:46 UTC (permalink / raw)
  To: Deng, KaiwenX, Stephen Hemminger, Morten Brørup
  Cc: dev, stable, Yang, Qiming, Zhou, YidingX, Singh, Aman Deep,
	Zhang, Yuying, Matz, Olivier, De Lara Guarch, Pablo

On 11/20/2023 9:21 AM, Deng, KaiwenX wrote:
> 
> 
>> -----Original Message-----
>> From: Ferruh Yigit <ferruh.yigit@amd.com>
>> Sent: Friday, November 17, 2023 8:50 AM
>> To: Stephen Hemminger <stephen@networkplumber.org>
>> Cc: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org;
>> stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
>> <yidingx.zhou@intel.com>; Singh, Aman Deep <aman.deep.singh@intel.com>;
>> Zhang, Yuying <yuying.zhang@intel.com>; Matz, Olivier
>> <olivier.matz@6wind.com>; De Lara Guarch, Pablo
>> <pablo.de.lara.guarch@intel.com>
>> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
>>
>> On 11/16/2023 10:58 PM, Stephen Hemminger wrote:
>>> On Thu, 2 Nov 2023 19:20:07 +0000
>>> Ferruh Yigit <ferruh.yigit@amd.com> wrote:
>>>
>>>> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
>>>>> 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. Testpmd does not trim these IP packets to the true length
>>>>> of the frame, so errors will occur when calculating TCP or UDP
>>>>> checksum.
>>>>>
>>>>
>>>> Hi Kaiwen,
>>>>
>>>> I am trying to understand the problem, what is the testcase that has
>>>> checksum error?
>>>>
>>>> Are the received mbuf data_len & pkt_len wrong? Instead of trying to
>>>> fix the mbuf during forwarding, can we fix where packet generated?
>>>
>>> The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len which
>>> maybe larger than the actual data. The real issue is there and in
>>> rte_ip.h checksum code. The correct fix would be to use l3_len instead.
>>>
>>
>> I see, you are right.
>>
>> 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.
>>
>>
>> I agree using 'l3_len' instead is correct fix.
>>
>> But this requires ABI/API change,
>> plus do we have any reason to keep the padding, discarding it as this patch
>> does is also simpler alternative.
>>
>>
>> Other alternative can be to zero the padding bytes. I guess standard doesn't
>> enforce them to be zero, but we can do this to remove its impact on checksum
>> calculation.
> I'm not sure if this is ok, it feels like it would reduce performance. 
> I can try this alternative if needed.
>

Yes impacts performance, so not a good alternative, please scratch it.

As discussion with Stephen and Morten, consensus is to fix SW functions
that calculates checksum.
'rte_ipv4_udptcp_cksum_mbuf()' & 'rte_ipv6_udptcp_cksum_mbuf()'.

Instead of using packet_len, those functions can use packet length,
which will make the checksum correct.


Can you please send a patch to fix those functions? I think this can be
done without changing function fingerprint, so without causing any
ABI/API break.


>>
>>
>> @Kaiwen, did you able to test this with HW offload, what is the behavior of
>> the HW, does is remove padding bytes?
>>
> I tested the HW offload case and the same tcp/udp checksum error occurs when padding is not 0, 
> But if change pkt_len to the true length of the frame, the checksum is correct.
>

I was expecting HW not impacted, since padding is part of the spec, my
assumption would be HW only take the actual payload size into account,
instead of buffer size.

Can you please double check? Which HW you are testing with, can you
please add maintainer of that HW to this discussion?

If HW requires padding to be removed, we may go with your solution.


>>
>>> It also looks like test-pmd is not validating the IP header.
>>> Both parse_ipv4() and parse_ipv6() should check if packet was truncated.
>>> Same for both UDP and TCP lengths.
>>>
>>
> 


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-17 16:22           ` Stephen Hemminger
@ 2023-11-20 10:47             ` Ferruh Yigit
  0 siblings, 0 replies; 29+ messages in thread
From: Ferruh Yigit @ 2023-11-20 10:47 UTC (permalink / raw)
  To: Stephen Hemminger
  Cc: Kaiwen Deng, dev, stable, qiming.yang, yidingx.zhou, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

On 11/17/2023 4:22 PM, Stephen Hemminger wrote:
> On Fri, 17 Nov 2023 09:29:41 +0000
> Ferruh Yigit <ferruh.yigit@amd.com> wrote:
> 
>>>> I agree using 'l3_len' instead is correct fix.
>>>>
>>>> But this requires ABI/API change,
>>>> plus do we have any reason to keep the padding, discarding it as this
>>>> patch does is also simpler alternative.  
>>>
>>>
>>> Possibly an API version to change the args would work to fix.
>>>  
>>
>> rte_ipv4_udptcp_cksum_mbuf() and rte_ipv6_udptcp_cksum_mbuf() are inline
>> functions, unfortunately we can't version them.
>>
>> But those functions already gets IP header as parameter, can't we use IP
>> header to get the payload size? If so this can be fixed without updating
>> API.
> 
> Inlines are easier. Just make a fixed new function and make sure the old
> one is not used.  They shouldn't have been inline in the first place.
>

I guess inlines as because of performance concerns, since it is in datapath.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH] app/test-pmd: fix L4 checksum with padding data
  2023-11-20 10:46         ` Ferruh Yigit
@ 2023-11-22  3:04           ` Deng, KaiwenX
  0 siblings, 0 replies; 29+ messages in thread
From: Deng, KaiwenX @ 2023-11-22  3:04 UTC (permalink / raw)
  To: Ferruh Yigit, Stephen Hemminger, Morten Brørup, Zhang, Qi Z
  Cc: dev, stable, Yang,  Qiming, Zhou, YidingX, Singh, Aman Deep,
	Zhang, Yuying, Matz, Olivier, De Lara Guarch, Pablo



> -----Original Message-----
> From: Ferruh Yigit <ferruh.yigit@amd.com>
> Sent: Monday, November 20, 2023 6:46 PM
> To: Deng, KaiwenX <kaiwenx.deng@intel.com>; Stephen Hemminger
> <stephen@networkplumber.org>; Morten Brørup
> <mb@smartsharesystems.com>
> Cc: dev@dpdk.org; stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>;
> Zhou, YidingX <yidingx.zhou@intel.com>; Singh, Aman Deep
> <aman.deep.singh@intel.com>; Zhang, Yuying <yuying.zhang@intel.com>;
> Matz, Olivier <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> <pablo.de.lara.guarch@intel.com>
> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> 
> On 11/20/2023 9:21 AM, Deng, KaiwenX wrote:
> >
> >
> >> -----Original Message-----
> >> From: Ferruh Yigit <ferruh.yigit@amd.com>
> >> Sent: Friday, November 17, 2023 8:50 AM
> >> To: Stephen Hemminger <stephen@networkplumber.org>
> >> Cc: Deng, KaiwenX <kaiwenx.deng@intel.com>; dev@dpdk.org;
> >> stable@dpdk.org; Yang, Qiming <qiming.yang@intel.com>; Zhou, YidingX
> >> <yidingx.zhou@intel.com>; Singh, Aman Deep
> >> <aman.deep.singh@intel.com>; Zhang, Yuying <yuying.zhang@intel.com>;
> >> Matz, Olivier <olivier.matz@6wind.com>; De Lara Guarch, Pablo
> >> <pablo.de.lara.guarch@intel.com>
> >> Subject: Re: [PATCH] app/test-pmd: fix L4 checksum with padding data
> >>
> >> On 11/16/2023 10:58 PM, Stephen Hemminger wrote:
> >>> On Thu, 2 Nov 2023 19:20:07 +0000
> >>> Ferruh Yigit <ferruh.yigit@amd.com> wrote:
> >>>
> >>>> On 8/4/2023 9:28 AM, Kaiwen Deng wrote:
> >>>>> 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. Testpmd does not trim these IP packets to the true
> >>>>> length of the frame, so errors will occur when calculating TCP or
> >>>>> UDP checksum.
> >>>>>
> >>>>
> >>>> Hi Kaiwen,
> >>>>
> >>>> I am trying to understand the problem, what is the testcase that
> >>>> has checksum error?
> >>>>
> >>>> Are the received mbuf data_len & pkt_len wrong? Instead of trying
> >>>> to fix the mbuf during forwarding, can we fix where packet generated?
> >>>
> >>> The root cause is that get_udptcp_cksum_mbuf is using m->pkt_len
> >>> which maybe larger than the actual data. The real issue is there and
> >>> in rte_ip.h checksum code. The correct fix would be to use l3_len instead.
> >>>
> >>
> >> I see, you are right.
> >>
> >> 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.
> >>
> >>
> >> I agree using 'l3_len' instead is correct fix.
> >>
> >> But this requires ABI/API change,
> >> plus do we have any reason to keep the padding, discarding it as this
> >> patch does is also simpler alternative.
> >>
> >>
> >> Other alternative can be to zero the padding bytes. I guess standard
> >> doesn't enforce them to be zero, but we can do this to remove its
> >> impact on checksum calculation.
> > I'm not sure if this is ok, it feels like it would reduce performance.
> > I can try this alternative if needed.
> >
> 
> Yes impacts performance, so not a good alternative, please scratch it.
> 
> As discussion with Stephen and Morten, consensus is to fix SW functions that
> calculates checksum.
> 'rte_ipv4_udptcp_cksum_mbuf()' & 'rte_ipv6_udptcp_cksum_mbuf()'.
> 
> Instead of using packet_len, those functions can use packet length, which will
> make the checksum correct.
> 
> 
> Can you please send a patch to fix those functions? I think this can be done
> without changing function fingerprint, so without causing any ABI/API break.
> 
> 
> >>
> >>
> >> @Kaiwen, did you able to test this with HW offload, what is the
> >> behavior of the HW, does is remove padding bytes?
> >>
> > I tested the HW offload case and the same tcp/udp checksum error
> > occurs when padding is not 0, But if change pkt_len to the true length of the
> frame, the checksum is correct.
> >
> 
> I was expecting HW not impacted, since padding is part of the spec, my
> assumption would be HW only take the actual payload size into account,
> instead of buffer size.
> 
> Can you please double check? Which HW you are testing with, can you please
> add maintainer of that HW to this discussion?

I've tested hw offloads for udp and tcp with Intel E810. The hardware takes the size 

of the buffer into account when calculating the udp/tcp checksum, not the size of the 

actual payload.

Hi @Zhang, Qi Z,

Can you help confirm if this behavior is normal in hw?

Thanks!
> 
> If HW requires padding to be removed, we may go with your solution.
> 
> 
> >>
> >>> It also looks like test-pmd is not validating the IP header.
> >>> Both parse_ipv4() and parse_ipv6() should check if packet was truncated.
> >>> Same for both UDP and TCP lengths.
> >>>
> >>
> >


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v2] app/test-pmd: fix tcp/udp cksum with padding data
  2023-08-04  8:28 [PATCH] app/test-pmd: fix L4 checksum with padding data Kaiwen Deng
  2023-11-02 19:20 ` Ferruh Yigit
  2023-11-17  1:13 ` Ferruh Yigit
@ 2023-12-07  8:53 ` Kaiwen Deng
  2023-12-07 14:35   ` Ferruh Yigit
  2023-12-12  2:16   ` [PATCH v3] lib/net: " Kaiwen Deng
  2 siblings, 2 replies; 29+ messages in thread
From: Kaiwen Deng @ 2023-12-07  8:53 UTC (permalink / raw)
  To: dev
  Cc: stable, qiming.yang, yidingx.zhou, Kaiwen Deng, Aman Singh,
	Yuying Zhang, Olivier Matz, Pablo de Lara

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. Testpmd does not trim these IP packets to the true
length of the frame, so errors will occur when calculating TCP
or UDP checksum.

This commit fixes this issue by triming IP packets to the true
length of the frame in testpmd.

Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum engine")
Cc: stable@dpdk.org

Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
---
 app/test-pmd/csumonly.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c
index 21210aff43..f7c8af96d7 100644
--- a/app/test-pmd/csumonly.c
+++ b/app/test-pmd/csumonly.c
@@ -860,12 +860,14 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
 	uint16_t nb_rx;
 	uint16_t nb_prep;
 	uint16_t i;
+	uint16_t pad_len;
 	uint64_t rx_ol_flags, tx_ol_flags;
 	uint64_t tx_offloads;
 	uint32_t rx_bad_ip_csum;
 	uint32_t rx_bad_l4_csum;
 	uint32_t rx_bad_outer_l4_csum;
 	uint32_t rx_bad_outer_ip_csum;
+	uint32_t l3_off;
 	struct testpmd_offload_info info;
 
 	/* receive a burst of packet */
@@ -987,6 +989,36 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
 			l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
 		}
 
+		if (info.is_tunnel) {
+			l3_off = info.outer_l2_len +
+					info.outer_l3_len +
+					info.l2_len;
+		} else {
+			l3_off = info.l2_len;
+		}
+		switch (info.ethertype) {
+		case _htons(RTE_ETHER_TYPE_IPV4):
+			pad_len = rte_pktmbuf_data_len(m) -
+					(l3_off +
+					rte_be_to_cpu_16(
+					((struct rte_ipv4_hdr *)l3_hdr)->total_length));
+			break;
+		case _htons(RTE_ETHER_TYPE_IPV6):
+			pad_len = rte_pktmbuf_data_len(m) -
+					(l3_off + info.l3_len +
+					rte_be_to_cpu_16(
+					((struct rte_ipv6_hdr *)l3_hdr)->payload_len));
+			break;
+		default:
+			pad_len = 0;
+			break;
+		}
+
+		if (pad_len) {
+			rte_pktmbuf_data_len(m) = rte_pktmbuf_data_len(m) - pad_len;
+			rte_pktmbuf_pkt_len(m) = rte_pktmbuf_data_len(m);
+		}
+
 		/* step 2: depending on user command line configuration,
 		 * recompute checksum either in software or flag the
 		 * mbuf to offload the calculation to the NIC. If TSO
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v2] app/test-pmd: fix tcp/udp cksum with padding data
  2023-12-07  8:53 ` [PATCH v2] app/test-pmd: fix tcp/udp cksum " Kaiwen Deng
@ 2023-12-07 14:35   ` Ferruh Yigit
  2023-12-12  2:16   ` [PATCH v3] lib/net: " Kaiwen Deng
  1 sibling, 0 replies; 29+ messages in thread
From: Ferruh Yigit @ 2023-12-07 14:35 UTC (permalink / raw)
  To: Kaiwen Deng, dev
  Cc: stable, qiming.yang, yidingx.zhou, Aman Singh, Yuying Zhang,
	Olivier Matz, Pablo de Lara

On 12/7/2023 8:53 AM, Kaiwen Deng wrote:
> 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. Testpmd does not trim these IP packets to the true
> length of the frame, so errors will occur when calculating TCP
> or UDP checksum.
> 
> This commit fixes this issue by triming IP packets to the true
> length of the frame in testpmd.
> 
> Fixes: 03d17e4d0179 ("app/testpmd: do not change IP addrs in checksum engine")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Kaiwen Deng <kaiwenx.deng@intel.com>
> 

I think decision was to fix 'rte_ipv4_udptcp_cksum_mbuf()' &
'rte_ipv6_udptcp_cksum_mbuf()'.


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v3] lib/net: fix tcp/udp cksum with padding data
  2023-12-07  8:53 ` [PATCH v2] app/test-pmd: fix tcp/udp cksum " Kaiwen Deng
  2023-12-07 14:35   ` Ferruh Yigit
@ 2023-12-12  2:16   ` Kaiwen Deng
  2023-12-12  8:10     ` Morten Brørup
  2023-12-13  4:37     ` [PATCH v4] " Kaiwen Deng
  1 sibling, 2 replies; 29+ messages in thread
From: Kaiwen Deng @ 2023-12-12  2:16 UTC (permalink / raw)
  To: dev
  Cc: stable, qiming.yang, yidingx.zhou, Kaiwen Deng, Xiaoyun Li,
	Aman Singh, Ferruh Yigit

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 | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
index 6fa98a5a0f..c503a2b57f 100644
--- a/lib/net/rte_ip.h
+++ b/lib/net/rte_ip.h
@@ -423,7 +423,10 @@ __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf *m,
 	if (l4_off > m->pkt_len)
 		return 0;
 
-	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
+	uint16_t 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, len, &raw_cksum))
 		return 0;
 
 	cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
@@ -666,7 +669,9 @@ __rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf *m,
 	if (l4_off > m->pkt_len)
 		return 0;
 
-	if (rte_raw_cksum_mbuf(m, l4_off, m->pkt_len - l4_off, &raw_cksum))
+	uint16_t len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
+
+	if (rte_raw_cksum_mbuf(m, l4_off, len, &raw_cksum))
 		return 0;
 
 	cksum = raw_cksum + rte_ipv6_phdr_cksum(ipv6_hdr, 0);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH v3] lib/net: fix tcp/udp cksum with padding data
  2023-12-12  2:16   ` [PATCH v3] lib/net: " Kaiwen Deng
@ 2023-12-12  8:10     ` Morten Brørup
  2023-12-13  4:37     ` [PATCH v4] " Kaiwen Deng
  1 sibling, 0 replies; 29+ messages in thread
From: Morten Brørup @ 2023-12-12  8:10 UTC (permalink / raw)
  To: Kaiwen Deng, dev
  Cc: stable, qiming.yang, yidingx.zhou, Xiaoyun Li, Aman Singh, Ferruh Yigit

> From: Kaiwen Deng [mailto:kaiwenx.deng@intel.com]
> Sent: Tuesday, 12 December 2023 03.16
> 
> 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 | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
> index 6fa98a5a0f..c503a2b57f 100644
> --- a/lib/net/rte_ip.h
> +++ b/lib/net/rte_ip.h
> @@ -423,7 +423,10 @@ __rte_ipv4_udptcp_cksum_mbuf(const struct rte_mbuf
> *m,
>  	if (l4_off > m->pkt_len)
>  		return 0;

It's not directly related, but while you are at it, please also add unlikely to the above check:

-	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))
> +	uint16_t 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, len, &raw_cksum))

Please declare "uint16_t len;" with the other variables at the top of the function, and only set its value here. (It's allowed to declare here, but please follow this function's existing convention of where variables are declared.)

>  		return 0;
> 
>  	cksum = raw_cksum + rte_ipv4_phdr_cksum(ipv4_hdr, 0);
> @@ -666,7 +669,9 @@ __rte_ipv6_udptcp_cksum_mbuf(const struct rte_mbuf
> *m,
>  	if (l4_off > m->pkt_len)
>  		return 0;

Again not directly related, but please also add unlikely to this comparison:

-	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))
> +	uint16_t len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
> +
> +	if (rte_raw_cksum_mbuf(m, l4_off, len, &raw_cksum))

No need for "len" variable here, just use rte_be_to_cpu_16(ipv6_hdr->payload_len) directly:

-	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);
> --
> 2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v4] lib/net: fix tcp/udp cksum with padding data
  2023-12-12  2:16   ` [PATCH v3] lib/net: " Kaiwen Deng
  2023-12-12  8:10     ` Morten Brørup
@ 2023-12-13  4:37     ` Kaiwen Deng
  2023-12-13  7:36       ` Morten Brørup
  2023-12-14  9:22       ` [PATCH v5] " Kaiwen Deng
  1 sibling, 2 replies; 29+ messages in thread
From: Kaiwen Deng @ 2023-12-13  4:37 UTC (permalink / raw)
  To: dev
  Cc: stable, qiming.yang, yidingx.zhou, Kaiwen Deng, Xiaoyun Li,
	Ferruh Yigit, Aman Singh

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(-)

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);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH v4] lib/net: fix tcp/udp cksum with padding data
  2023-12-13  4:37     ` [PATCH v4] " Kaiwen Deng
@ 2023-12-13  7:36       ` Morten Brørup
  2023-12-14  9:22       ` [PATCH v5] " Kaiwen Deng
  1 sibling, 0 replies; 29+ messages in thread
From: Morten Brørup @ 2023-12-13  7:36 UTC (permalink / raw)
  To: Kaiwen Deng, dev
  Cc: stable, qiming.yang, yidingx.zhou, Xiaoyun Li, Ferruh Yigit, Aman Singh

> 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>


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v5] lib/net: fix tcp/udp cksum with padding data
  2023-12-13  4:37     ` [PATCH v4] " Kaiwen Deng
  2023-12-13  7:36       ` Morten Brørup
@ 2023-12-14  9:22       ` Kaiwen Deng
  2023-12-14 11:20         ` Morten Brørup
  1 sibling, 1 reply; 29+ messages in thread
From: Kaiwen Deng @ 2023-12-14  9:22 UTC (permalink / raw)
  To: dev
  Cc: stable, qiming.yang, yidingx.zhou, Kaiwen Deng, Aman Singh,
	Ferruh Yigit, Xiaoyun Li

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 | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
index 6fa98a5a0f..0d103d4127 100644
--- a/lib/net/rte_ip.h
+++ b/lib/net/rte_ip.h
@@ -419,11 +419,14 @@ __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 +666,10 @@ __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);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 29+ messages in thread

* RE: [PATCH v5] lib/net: fix tcp/udp cksum with padding data
  2023-12-14  9:22       ` [PATCH v5] " Kaiwen Deng
@ 2023-12-14 11:20         ` Morten Brørup
  2024-02-19  1:10           ` Thomas Monjalon
  0 siblings, 1 reply; 29+ messages in thread
From: Morten Brørup @ 2023-12-14 11:20 UTC (permalink / raw)
  To: Kaiwen Deng, dev
  Cc: stable, qiming.yang, yidingx.zhou, Aman Singh, Ferruh Yigit, Xiaoyun Li

> From: Kaiwen Deng [mailto:kaiwenx.deng@intel.com]
> Sent: Thursday, 14 December 2023 10.23
> 
> 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>
> ---

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


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v5] lib/net: fix tcp/udp cksum with padding data
  2023-12-14 11:20         ` Morten Brørup
@ 2024-02-19  1:10           ` Thomas Monjalon
  0 siblings, 0 replies; 29+ messages in thread
From: Thomas Monjalon @ 2024-02-19  1:10 UTC (permalink / raw)
  To: Kaiwen Deng
  Cc: dev, stable, qiming.yang, yidingx.zhou, Aman Singh, Ferruh Yigit,
	Xiaoyun Li, Morten Brørup

14/12/2023 12:20, Morten Brørup:
> > From: Kaiwen Deng [mailto:kaiwenx.deng@intel.com]
> > Sent: Thursday, 14 December 2023 10.23
> > 
> > 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>
> 
> Reviewed-by: Morten Brørup <mb@smartsharesystems.com>

Applied, thanks.




^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2024-02-19  1:10 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-04  8:28 [PATCH] app/test-pmd: fix L4 checksum with padding data Kaiwen Deng
2023-11-02 19:20 ` Ferruh Yigit
2023-11-03  2:49   ` Deng, KaiwenX
2023-11-03  4:03     ` Ferruh Yigit
2023-11-14  2:19       ` Deng, KaiwenX
2023-11-14 19:09         ` Ferruh Yigit
2023-11-16  7:02           ` Deng, KaiwenX
2023-11-16 22:58   ` Stephen Hemminger
2023-11-17  0:50     ` Ferruh Yigit
2023-11-17  3:28       ` Stephen Hemminger
2023-11-17  9:29         ` Ferruh Yigit
2023-11-17 12:11           ` Morten Brørup
2023-11-17 16:23             ` Stephen Hemminger
2023-11-17 16:22           ` Stephen Hemminger
2023-11-20 10:47             ` Ferruh Yigit
2023-11-20  9:21       ` Deng, KaiwenX
2023-11-20 10:46         ` Ferruh Yigit
2023-11-22  3:04           ` Deng, KaiwenX
2023-11-17  1:13 ` Ferruh Yigit
2023-11-20  9:52   ` Deng, KaiwenX
2023-12-07  8:53 ` [PATCH v2] app/test-pmd: fix tcp/udp cksum " Kaiwen Deng
2023-12-07 14:35   ` Ferruh Yigit
2023-12-12  2:16   ` [PATCH v3] lib/net: " Kaiwen Deng
2023-12-12  8:10     ` Morten Brørup
2023-12-13  4:37     ` [PATCH v4] " Kaiwen Deng
2023-12-13  7:36       ` Morten Brørup
2023-12-14  9:22       ` [PATCH v5] " Kaiwen Deng
2023-12-14 11:20         ` Morten Brørup
2024-02-19  1:10           ` Thomas Monjalon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).