bugfix: rte_raw_checksum

Message ID 20200527134009.19444-1-guohongzhi1@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series bugfix: rte_raw_checksum |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-nxp-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-testing warning Testing issues
ci/travis-robot success Travis build: passed
ci/Intel-compilation success Compilation OK

Commit Message

Hongzhi Guo May 27, 2020, 1:40 p.m. UTC
  From: Hongzhi Guo <guohongzhi1@huawei.com>

__rte_raw_cksum should consider Big Endian.

Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
---
 lib/librte_net/rte_ip.h | 4 ++++
 1 file changed, 4 insertions(+)
  

Comments

Morten Brørup May 27, 2020, 2:58 p.m. UTC | #1
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of guohongzhi
> Sent: Wednesday, May 27, 2020 3:40 PM
> 
> From: Hongzhi Guo <guohongzhi1@huawei.com>
> 
> __rte_raw_cksum should consider Big Endian.
> 
> Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
> ---
>  lib/librte_net/rte_ip.h | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/lib/librte_net/rte_ip.h b/lib/librte_net/rte_ip.h
> index 1ceb7b7..eb863d4 100644
> --- a/lib/librte_net/rte_ip.h
> +++ b/lib/librte_net/rte_ip.h
> @@ -140,7 +140,11 @@ __rte_raw_cksum(const void *buf, size_t len,
> uint32_t sum)
> 
>  	/* if length is in odd bytes */
>  	if (len == 1)
> +#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
> +		sum += *((const uint8_t *)u16_buf) << 8;
> +#else
>  		sum += *((const uint8_t *)u16_buf);
> +#endif
> 
>  	return sum;
>  }
> --
> 2.21.0.windows.1
> 
> 

Acked-by: Morten Brørup <mb@smartsharesystems.com>
  
Thomas Monjalon June 24, 2020, 12:21 p.m. UTC | #2
27/05/2020 15:40, guohongzhi:
> From: Hongzhi Guo <guohongzhi1@huawei.com>
> 
> __rte_raw_cksum should consider Big Endian.

We need to explain the logic in the commit log.

> Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
> ---
> +#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
> +		sum += *((const uint8_t *)u16_buf) << 8;
> +#else
>  		sum += *((const uint8_t *)u16_buf);
> +#endif

*((const uint8_t *)u16_buf) should be an uint8_t.
What is the expected behaviour of shifting 8 bits of a byte?
  
Morten Brørup June 24, 2020, 1 p.m. UTC | #3
> From: Thomas Monjalon [mailto:thomas@monjalon.net]
> Sent: Wednesday, June 24, 2020 2:22 PM
> 
> 27/05/2020 15:40, guohongzhi:
> > From: Hongzhi Guo <guohongzhi1@huawei.com>
> >
> > __rte_raw_cksum should consider Big Endian.
> 
> We need to explain the logic in the commit log.

Having grown up with big endian CPUs, reading the final byte like this is obvious to me. I struggle understanding the little endian way of reading the last byte. (Not really anymore, but back when little endian was unfamiliar to me I would have struggled.)

An RFC (I can't remember which) describes why the same checksum calculation code works on both big and little endian CPUs. Is it this explanation you are asking for?

> 
> > Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
> > ---
> > +#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
> > +		sum += *((const uint8_t *)u16_buf) << 8;
> > +#else
> >  		sum += *((const uint8_t *)u16_buf);
> > +#endif
> 
> *((const uint8_t *)u16_buf) should be an uint8_t.
> What is the expected behaviour of shifting 8 bits of a byte?

Yes, the value will be an uint8_t type. But the shift operation will cause the compiler to promote the type to int before shifting it.
  
Thomas Monjalon June 24, 2020, 3:04 p.m. UTC | #4
24/06/2020 15:00, Morten Brørup:
> > From: Thomas Monjalon [mailto:thomas@monjalon.net]
> > Sent: Wednesday, June 24, 2020 2:22 PM
> > 
> > 27/05/2020 15:40, guohongzhi:
> > > From: Hongzhi Guo <guohongzhi1@huawei.com>
> > >
> > > __rte_raw_cksum should consider Big Endian.
> > 
> > We need to explain the logic in the commit log.
> 
> Having grown up with big endian CPUs, reading the final byte like this is obvious to me. I struggle understanding the little endian way of reading the last byte. (Not really anymore, but back when little endian was unfamiliar to me I would have struggled.)
> 
> An RFC (I can't remember which) describes why the same checksum calculation code works on both big and little endian CPUs. Is it this explanation you are asking for?

This explanation may be interesting.


> > > Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
> > > ---
> > > +#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
> > > +		sum += *((const uint8_t *)u16_buf) << 8;
> > > +#else
> > >  		sum += *((const uint8_t *)u16_buf);
> > > +#endif
> > 
> > *((const uint8_t *)u16_buf) should be an uint8_t.
> > What is the expected behaviour of shifting 8 bits of a byte?
> 
> Yes, the value will be an uint8_t type. But the shift operation will cause the compiler to promote the type to int before shifting it.

This is the explanation I was looking for :-)
  
Morten Brørup June 24, 2020, 3:11 p.m. UTC | #5
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> Sent: Wednesday, June 24, 2020 5:04 PM
> 
> 24/06/2020 15:00, Morten Brørup:
> > > From: Thomas Monjalon [mailto:thomas@monjalon.net]
> > > Sent: Wednesday, June 24, 2020 2:22 PM
> > >
> > > 27/05/2020 15:40, guohongzhi:
> > > > From: Hongzhi Guo <guohongzhi1@huawei.com>
> > > >
> > > > __rte_raw_cksum should consider Big Endian.
> > >
> > > We need to explain the logic in the commit log.
> >
> > Having grown up with big endian CPUs, reading the final byte like
> this is obvious to me. I struggle understanding the little endian way
> of reading the last byte. (Not really anymore, but back when little
> endian was unfamiliar to me I would have struggled.)
> >
> > An RFC (I can't remember which) describes why the same checksum
> calculation code works on both big and little endian CPUs. Is it this
> explanation you are asking for?
> 
> This explanation may be interesting.
> 

RFC 1071, especially chapter 3.

Please note that big endian is considered "Normal" order in the RFC. :-)

> 
> > > > Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
> > > > ---
> > > > +#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
> > > > +		sum += *((const uint8_t *)u16_buf) << 8;
> > > > +#else
> > > >  		sum += *((const uint8_t *)u16_buf);
> > > > +#endif
> > >
> > > *((const uint8_t *)u16_buf) should be an uint8_t.
> > > What is the expected behaviour of shifting 8 bits of a byte?
> >
> > Yes, the value will be an uint8_t type. But the shift operation will
> cause the compiler to promote the type to int before shifting it.
> 
> This is the explanation I was looking for :-)
> 
>
  
Olivier Matz July 6, 2020, 7:36 a.m. UTC | #6
Hi Hongzhi,

I suggest the following title instead:

  net: fix checksum on big endian CPUs

On Wed, Jun 24, 2020 at 05:11:19PM +0200, Morten Brørup wrote:
> > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> > Sent: Wednesday, June 24, 2020 5:04 PM
> > 
> > 24/06/2020 15:00, Morten Brørup:
> > > > From: Thomas Monjalon [mailto:thomas@monjalon.net]
> > > > Sent: Wednesday, June 24, 2020 2:22 PM
> > > >
> > > > 27/05/2020 15:40, guohongzhi:
> > > > > From: Hongzhi Guo <guohongzhi1@huawei.com>
> > > > >
> > > > > __rte_raw_cksum should consider Big Endian.
> > > >
> > > > We need to explain the logic in the commit log.

Here is a suggestion of commit log:

  With current code, the checksum of odd-length buffers is wrong on
  big endian CPUs: the last byte is not properly summed to the
  accumulator.

  Fix this by left-shifting the remaining byte by 8. For instance,
  if the last byte is 0x42, we should add 0x4200 to the accumulator
  on big endian CPUs.

  This change is similar to what is suggested in Errata 3133 of
  RFC 1071.

Can you please submit a new version with the 2 changes above?

> > >
> > > Having grown up with big endian CPUs, reading the final byte like
> > this is obvious to me. I struggle understanding the little endian way
> > of reading the last byte. (Not really anymore, but back when little
> > endian was unfamiliar to me I would have struggled.)
> > >
> > > An RFC (I can't remember which) describes why the same checksum
> > calculation code works on both big and little endian CPUs. Is it this
> > explanation you are asking for?
> > 
> > This explanation may be interesting.
> > 
> 
> RFC 1071, especially chapter 3.
> 
> Please note that big endian is considered "Normal" order in the RFC. :-)

There is an errata for this RFC about the C code:
see https://www.rfc-editor.org/errata/eid3133

> > 
> > > > > Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
> > > > > ---
> > > > > +#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
> > > > > +		sum += *((const uint8_t *)u16_buf) << 8;
> > > > > +#else
> > > > >  		sum += *((const uint8_t *)u16_buf);
> > > > > +#endif
> > > >
> > > > *((const uint8_t *)u16_buf) should be an uint8_t.
> > > > What is the expected behaviour of shifting 8 bits of a byte?
> > >
> > > Yes, the value will be an uint8_t type. But the shift operation will
> > cause the compiler to promote the type to int before shifting it.
> > 
> > This is the explanation I was looking for :-)
> > 
> > 
>
  
Olivier Matz July 6, 2020, 7:46 a.m. UTC | #7
On Mon, Jul 06, 2020 at 09:36:25AM +0200, Olivier Matz wrote:
> Hi Hongzhi,
> 
> I suggest the following title instead:
> 
>   net: fix checksum on big endian CPUs
> 
> On Wed, Jun 24, 2020 at 05:11:19PM +0200, Morten Brørup wrote:
> > > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> > > Sent: Wednesday, June 24, 2020 5:04 PM
> > > 
> > > 24/06/2020 15:00, Morten Brørup:
> > > > > From: Thomas Monjalon [mailto:thomas@monjalon.net]
> > > > > Sent: Wednesday, June 24, 2020 2:22 PM
> > > > >
> > > > > 27/05/2020 15:40, guohongzhi:
> > > > > > From: Hongzhi Guo <guohongzhi1@huawei.com>
> > > > > >
> > > > > > __rte_raw_cksum should consider Big Endian.
> > > > >
> > > > > We need to explain the logic in the commit log.
> 
> Here is a suggestion of commit log:
> 
>   With current code, the checksum of odd-length buffers is wrong on
>   big endian CPUs: the last byte is not properly summed to the
>   accumulator.
> 
>   Fix this by left-shifting the remaining byte by 8. For instance,
>   if the last byte is 0x42, we should add 0x4200 to the accumulator
>   on big endian CPUs.
> 
>   This change is similar to what is suggested in Errata 3133 of
>   RFC 1071.
> 
> Can you please submit a new version with the 2 changes above?

One more thing, please also add:

  Fixes: 6006818cfb26 ("net: new checksum functions")
  Cc: stable@dpdk.org

Thanks
Olivier

> 
> > > >
> > > > Having grown up with big endian CPUs, reading the final byte like
> > > this is obvious to me. I struggle understanding the little endian way
> > > of reading the last byte. (Not really anymore, but back when little
> > > endian was unfamiliar to me I would have struggled.)
> > > >
> > > > An RFC (I can't remember which) describes why the same checksum
> > > calculation code works on both big and little endian CPUs. Is it this
> > > explanation you are asking for?
> > > 
> > > This explanation may be interesting.
> > > 
> > 
> > RFC 1071, especially chapter 3.
> > 
> > Please note that big endian is considered "Normal" order in the RFC. :-)
> 
> There is an errata for this RFC about the C code:
> see https://www.rfc-editor.org/errata/eid3133
> 
> > > 
> > > > > > Signed-off-by: Hongzhi Guo <guohongzhi1@huawei.com>
> > > > > > ---
> > > > > > +#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
> > > > > > +		sum += *((const uint8_t *)u16_buf) << 8;
> > > > > > +#else
> > > > > >  		sum += *((const uint8_t *)u16_buf);
> > > > > > +#endif
> > > > >
> > > > > *((const uint8_t *)u16_buf) should be an uint8_t.
> > > > > What is the expected behaviour of shifting 8 bits of a byte?
> > > >
> > > > Yes, the value will be an uint8_t type. But the shift operation will
> > > cause the compiler to promote the type to int before shifting it.
> > > 
> > > This is the explanation I was looking for :-)
> > > 
> > > 
> >
  

Patch

diff --git a/lib/librte_net/rte_ip.h b/lib/librte_net/rte_ip.h
index 1ceb7b7..eb863d4 100644
--- a/lib/librte_net/rte_ip.h
+++ b/lib/librte_net/rte_ip.h
@@ -140,7 +140,11 @@  __rte_raw_cksum(const void *buf, size_t len, uint32_t sum)
 
 	/* if length is in odd bytes */
 	if (len == 1)
+#if (RTE_BYTE_ORDER == RTE_BIG_ENDIAN)
+		sum += *((const uint8_t *)u16_buf) << 8;
+#else
 		sum += *((const uint8_t *)u16_buf);
+#endif
 
 	return sum;
 }