linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ioc3: fix incorrect use of htons/ntohs
@ 2014-11-30 10:40 Lino Sanfilippo
  2014-12-01  4:09 ` Ben Hutchings
  0 siblings, 1 reply; 5+ messages in thread
From: Lino Sanfilippo @ 2014-11-30 10:40 UTC (permalink / raw)
  To: ralf; +Cc: linux-mips, netdev, linux-kernel, Lino Sanfilippo

The protocol type in the ip header struct is a single byte variable. So there
is no need to swap bytes depending on host endianness.

Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
---

Please note that I could not test this, since I dont have access to the
concerning hardware.

 drivers/net/ethernet/sgi/ioc3-eth.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/sgi/ioc3-eth.c b/drivers/net/ethernet/sgi/ioc3-eth.c
index 7a254da..0bb303d 100644
--- a/drivers/net/ethernet/sgi/ioc3-eth.c
+++ b/drivers/net/ethernet/sgi/ioc3-eth.c
@@ -540,8 +540,7 @@ static void ioc3_tcpudp_checksum(struct sk_buff *skb, uint32_t hwsum, int len)
 
 	/* Same as tx - compute csum of pseudo header  */
 	csum = hwsum +
-	       (ih->tot_len - (ih->ihl << 2)) +
-	       htons((uint16_t)ih->protocol) +
+	       (ih->tot_len - (ih->ihl << 2)) + ih->protocol +
 	       (ih->saddr >> 16) + (ih->saddr & 0xffff) +
 	       (ih->daddr >> 16) + (ih->daddr & 0xffff);
 
@@ -1417,7 +1416,7 @@ static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
 	 */
 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
 		const struct iphdr *ih = ip_hdr(skb);
-		const int proto = ntohs(ih->protocol);
+		const int proto = ih->protocol;
 		unsigned int csoff;
 		uint32_t csum, ehsum;
 		uint16_t *eh;
-- 
1.9.1


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

* Re: [PATCH] ioc3: fix incorrect use of htons/ntohs
  2014-11-30 10:40 [PATCH] ioc3: fix incorrect use of htons/ntohs Lino Sanfilippo
@ 2014-12-01  4:09 ` Ben Hutchings
  2014-12-02  1:31   ` Lino Sanfilippo
  2014-12-15 18:14   ` Ralf Baechle
  0 siblings, 2 replies; 5+ messages in thread
From: Ben Hutchings @ 2014-12-01  4:09 UTC (permalink / raw)
  To: Lino Sanfilippo; +Cc: ralf, linux-mips, netdev, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2826 bytes --]

On Sun, 2014-11-30 at 11:40 +0100, Lino Sanfilippo wrote:
> The protocol type in the ip header struct is a single byte variable. So there
> is no need to swap bytes depending on host endianness.
> 
> Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
> ---
> 
> Please note that I could not test this, since I dont have access to the
> concerning hardware.
> 
>  drivers/net/ethernet/sgi/ioc3-eth.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/ethernet/sgi/ioc3-eth.c b/drivers/net/ethernet/sgi/ioc3-eth.c
> index 7a254da..0bb303d 100644
> --- a/drivers/net/ethernet/sgi/ioc3-eth.c
> +++ b/drivers/net/ethernet/sgi/ioc3-eth.c
> @@ -540,8 +540,7 @@ static void ioc3_tcpudp_checksum(struct sk_buff *skb, uint32_t hwsum, int len)
>  
>  	/* Same as tx - compute csum of pseudo header  */
>  	csum = hwsum +
> -	       (ih->tot_len - (ih->ihl << 2)) +
> -	       htons((uint16_t)ih->protocol) +
> +	       (ih->tot_len - (ih->ihl << 2)) + ih->protocol +
>  	       (ih->saddr >> 16) + (ih->saddr & 0xffff) +
>  	       (ih->daddr >> 16) + (ih->daddr & 0xffff);
>

The pseudo-header is specified as:

                     +--------+--------+--------+--------+
                     |           Source Address          |
                     +--------+--------+--------+--------+
                     |         Destination Address       |
                     +--------+--------+--------+--------+
                     |  zero  |  PTCL  |    TCP Length   |
                     +--------+--------+--------+--------+

The current code zero-extends the protocol number to produce the 5th
16-bit word of the pseudo-header, then uses htons() to put it in
big-endian order, consistent with the other fields.  (Yes, it's doing
addition on big-endian words; this works even on little-endian machines
due to the way the checksum is specified.)

The driver should not be doing this at all, though.  It should set
skb->csum = hwsum; skb->ip_summed = CHECKSUM_COMPLETE; and let the
network stack adjust the hardware checksum.

> @@ -1417,7 +1416,7 @@ static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
>  	 */
>  	if (skb->ip_summed == CHECKSUM_PARTIAL) {
>  		const struct iphdr *ih = ip_hdr(skb);
> -		const int proto = ntohs(ih->protocol);
> +		const int proto = ih->protocol;
>  		unsigned int csoff;
>  		uint32_t csum, ehsum;
>  		uint16_t *eh;

This should logically be __be16 proto = htons(ih->protocol), but the
current version should work in practice.

However, the driver should really use skb->csum_start and
skb->csum_offset to work out where the checksum belongs and which bytes
to cancel out.

Ben.

-- 
Ben Hutchings
The first rule of tautology club is the first rule of tautology club.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 811 bytes --]

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

* Re: [PATCH] ioc3: fix incorrect use of htons/ntohs
  2014-12-01  4:09 ` Ben Hutchings
@ 2014-12-02  1:31   ` Lino Sanfilippo
  2014-12-15 18:14   ` Ralf Baechle
  1 sibling, 0 replies; 5+ messages in thread
From: Lino Sanfilippo @ 2014-12-02  1:31 UTC (permalink / raw)
  To: Ben Hutchings; +Cc: ralf, linux-mips, netdev, linux-kernel

On 01.12.2014 05:09, Ben Hutchings wrote:
> On Sun, 2014-11-30 at 11:40 +0100, Lino Sanfilippo wrote:
>> The protocol type in the ip header struct is a single byte variable. So there
>> is no need to swap bytes depending on host endianness.
>> 
>> Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
>> ---
>> 
>> Please note that I could not test this, since I dont have access to the
>> concerning hardware.
>> 
>>  drivers/net/ethernet/sgi/ioc3-eth.c | 5 ++---
>>  1 file changed, 2 insertions(+), 3 deletions(-)
>> 
>> diff --git a/drivers/net/ethernet/sgi/ioc3-eth.c b/drivers/net/ethernet/sgi/ioc3-eth.c
>> index 7a254da..0bb303d 100644
>> --- a/drivers/net/ethernet/sgi/ioc3-eth.c
>> +++ b/drivers/net/ethernet/sgi/ioc3-eth.c
>> @@ -540,8 +540,7 @@ static void ioc3_tcpudp_checksum(struct sk_buff *skb, uint32_t hwsum, int len)
>>  
>>  	/* Same as tx - compute csum of pseudo header  */
>>  	csum = hwsum +
>> -	       (ih->tot_len - (ih->ihl << 2)) +
>> -	       htons((uint16_t)ih->protocol) +
>> +	       (ih->tot_len - (ih->ihl << 2)) + ih->protocol +
>>  	       (ih->saddr >> 16) + (ih->saddr & 0xffff) +
>>  	       (ih->daddr >> 16) + (ih->daddr & 0xffff);
>>
> 
> The pseudo-header is specified as:
> 
>                      +--------+--------+--------+--------+
>                      |           Source Address          |
>                      +--------+--------+--------+--------+
>                      |         Destination Address       |
>                      +--------+--------+--------+--------+
>                      |  zero  |  PTCL  |    TCP Length   |
>                      +--------+--------+--------+--------+
> 
> The current code zero-extends the protocol number to produce the 5th
> 16-bit word of the pseudo-header, then uses htons() to put it in
> big-endian order, consistent with the other fields.  (Yes, it's doing
> addition on big-endian words; this works even on little-endian machines
> due to the way the checksum is specified.)
> 
> The driver should not be doing this at all, though.  It should set
> skb->csum = hwsum; skb->ip_summed = CHECKSUM_COMPLETE; and let the
> network stack adjust the hardware checksum.
> 
>> @@ -1417,7 +1416,7 @@ static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
>>  	 */
>>  	if (skb->ip_summed == CHECKSUM_PARTIAL) {
>>  		const struct iphdr *ih = ip_hdr(skb);
>> -		const int proto = ntohs(ih->protocol);
>> +		const int proto = ih->protocol;
>>  		unsigned int csoff;
>>  		uint32_t csum, ehsum;
>>  		uint16_t *eh;
> 
> This should logically be __be16 proto = htons(ih->protocol), but the
> current version should work in practice.
> 
> However, the driver should really use skb->csum_start and
> skb->csum_offset to work out where the checksum belongs and which bytes
> to cancel out.
> 
> Ben.
> 

Hi Ben,

youre right, the use of htons/ntohs is correct in this case. So thank
you for the explanation and please ignore that patch.

Regards,
Lino


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

* Re: [PATCH] ioc3: fix incorrect use of htons/ntohs
  2014-12-01  4:09 ` Ben Hutchings
  2014-12-02  1:31   ` Lino Sanfilippo
@ 2014-12-15 18:14   ` Ralf Baechle
  2014-12-15 21:09     ` Ben Hutchings
  1 sibling, 1 reply; 5+ messages in thread
From: Ralf Baechle @ 2014-12-15 18:14 UTC (permalink / raw)
  To: Ben Hutchings; +Cc: Lino Sanfilippo, linux-mips, netdev, linux-kernel

On Mon, Dec 01, 2014 at 04:09:36AM +0000, Ben Hutchings wrote:

> >  	/* Same as tx - compute csum of pseudo header  */
> >  	csum = hwsum +
> > -	       (ih->tot_len - (ih->ihl << 2)) +
> > -	       htons((uint16_t)ih->protocol) +
> > +	       (ih->tot_len - (ih->ihl << 2)) + ih->protocol +
> >  	       (ih->saddr >> 16) + (ih->saddr & 0xffff) +
> >  	       (ih->daddr >> 16) + (ih->daddr & 0xffff);
> >
> 
> The pseudo-header is specified as:
> 
>                      +--------+--------+--------+--------+
>                      |           Source Address          |
>                      +--------+--------+--------+--------+
>                      |         Destination Address       |
>                      +--------+--------+--------+--------+
>                      |  zero  |  PTCL  |    TCP Length   |
>                      +--------+--------+--------+--------+
> 
> The current code zero-extends the protocol number to produce the 5th
> 16-bit word of the pseudo-header, then uses htons() to put it in
> big-endian order, consistent with the other fields.  (Yes, it's doing
> addition on big-endian words; this works even on little-endian machines
> due to the way the checksum is specified.)
> 
> The driver should not be doing this at all, though.  It should set
> skb->csum = hwsum; skb->ip_summed = CHECKSUM_COMPLETE; and let the
> network stack adjust the hardware checksum.

Really?  The IOC3 isn't the exactly the smartest NIC around; it does add up
everything and the kitchen sink, that is ethernet headers, IP headers and
on RX the frame's trailing CRC.  All that needs to be subtracted in software
which is what this does.  I think others NICs are all smarted and don't
need this particular piece of magic.

I agree with your other comment wrt. to htons().

  Ralf

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

* Re: [PATCH] ioc3: fix incorrect use of htons/ntohs
  2014-12-15 18:14   ` Ralf Baechle
@ 2014-12-15 21:09     ` Ben Hutchings
  0 siblings, 0 replies; 5+ messages in thread
From: Ben Hutchings @ 2014-12-15 21:09 UTC (permalink / raw)
  To: Ralf Baechle; +Cc: Lino Sanfilippo, linux-mips, netdev, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2676 bytes --]

On Mon, 2014-12-15 at 19:14 +0100, Ralf Baechle wrote:
> On Mon, Dec 01, 2014 at 04:09:36AM +0000, Ben Hutchings wrote:
> 
> > >  	/* Same as tx - compute csum of pseudo header  */
> > >  	csum = hwsum +
> > > -	       (ih->tot_len - (ih->ihl << 2)) +
> > > -	       htons((uint16_t)ih->protocol) +
> > > +	       (ih->tot_len - (ih->ihl << 2)) + ih->protocol +
> > >  	       (ih->saddr >> 16) + (ih->saddr & 0xffff) +
> > >  	       (ih->daddr >> 16) + (ih->daddr & 0xffff);
> > >
> > 
> > The pseudo-header is specified as:
> > 
> >                      +--------+--------+--------+--------+
> >                      |           Source Address          |
> >                      +--------+--------+--------+--------+
> >                      |         Destination Address       |
> >                      +--------+--------+--------+--------+
> >                      |  zero  |  PTCL  |    TCP Length   |
> >                      +--------+--------+--------+--------+
> > 
> > The current code zero-extends the protocol number to produce the 5th
> > 16-bit word of the pseudo-header, then uses htons() to put it in
> > big-endian order, consistent with the other fields.  (Yes, it's doing
> > addition on big-endian words; this works even on little-endian machines
> > due to the way the checksum is specified.)
> > 
> > The driver should not be doing this at all, though.  It should set
> > skb->csum = hwsum; skb->ip_summed = CHECKSUM_COMPLETE; and let the
> > network stack adjust the hardware checksum.
> 
> Really?  The IOC3 isn't the exactly the smartest NIC around; it does add up
> everything and the kitchen sink, that is ethernet headers, IP headers and
> on RX the frame's trailing CRC.

That is almost exactly what CHECKSUM_COMPLETE means on receive; only the
CRC would need to be subtracted.  Then the driver can validate
{TCP,UDP}/IPv{4,6} checksums without any header parsing.

> All that needs to be subtracted in software
> which is what this does.  I think others NICs are all smarted and don't
> need this particular piece of magic.

It may not be smart, but that allows it to cover more cases than most
smart network controllers!

On transmit, the driver should:
- Calculate the partial checksum of data up to offset csum_start
- Subtract this from the checksum at offset (csum_start + csum_offset)
It should set the NETIF_F_GEN_CSUM feature flag rather than
NETIF_F_IP_CSUM.  Then it will be able to generate {TCP,UDP}/IPv{4,6}
checksums.

Ben.

> I agree with your other comment wrt. to htons().


-- 
Ben Hutchings
The two most common things in the universe are hydrogen and stupidity.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 811 bytes --]

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

end of thread, other threads:[~2014-12-15 23:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-30 10:40 [PATCH] ioc3: fix incorrect use of htons/ntohs Lino Sanfilippo
2014-12-01  4:09 ` Ben Hutchings
2014-12-02  1:31   ` Lino Sanfilippo
2014-12-15 18:14   ` Ralf Baechle
2014-12-15 21:09     ` Ben Hutchings

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