linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* ntohs/ntohl and bitops
@ 2006-01-10 22:02 Ulrich Drepper
  2006-01-11  8:00 ` David S. Miller
  0 siblings, 1 reply; 13+ messages in thread
From: Ulrich Drepper @ 2006-01-10 22:02 UTC (permalink / raw)
  To: Linux Kernel, David S. Miller

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

I just saw this in a patch:

+               if (ntohs(ih->frag_off) & IP_OFFSET)
+                       return EBT_NOMATCH;

This isn't optimal, it requires a byte switch little endian machines.
The compiler isn't smart enough.  It would be better to use

     if (ih->frag_off & ntohs(IP_OFFSET))

where the byte-swap can be done at compile time.  This is kind of ugly,
I guess, so maybe a dedicate macro

    net_host_bit_p(ih->frag_off, IP_OFFSET)

??

-- 
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

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

* Re: ntohs/ntohl and bitops
  2006-01-10 22:02 ntohs/ntohl and bitops Ulrich Drepper
@ 2006-01-11  8:00 ` David S. Miller
  2006-01-11  8:13   ` Arjan van de Ven
  2006-01-12  1:04   ` Pavel Machek
  0 siblings, 2 replies; 13+ messages in thread
From: David S. Miller @ 2006-01-11  8:00 UTC (permalink / raw)
  To: drepper; +Cc: linux-kernel

From: Ulrich Drepper <drepper@redhat.com>
Date: Tue, 10 Jan 2006 14:02:52 -0800

> I just saw this in a patch:
> 
> +               if (ntohs(ih->frag_off) & IP_OFFSET)
> +                       return EBT_NOMATCH;
> 
> This isn't optimal, it requires a byte switch little endian machines.
> The compiler isn't smart enough.  It would be better to use
> 
>      if (ih->frag_off & ntohs(IP_OFFSET))
> 
> where the byte-swap can be done at compile time.  This is kind of ugly,
> I guess, so maybe a dedicate macro
> 
>     net_host_bit_p(ih->frag_off, IP_OFFSET)

The first suggestion isn't considered ugly, and the best form is:

	if (ih->frag_off & __constant_htons(IP_OFFSET))

I'll fix that up when I get a chance, thanks for catching it Uli.

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

* Re: ntohs/ntohl and bitops
  2006-01-11  8:00 ` David S. Miller
@ 2006-01-11  8:13   ` Arjan van de Ven
  2006-01-11  8:36     ` David S. Miller
  2006-01-11  8:36     ` Ulrich Drepper
  2006-01-12  1:04   ` Pavel Machek
  1 sibling, 2 replies; 13+ messages in thread
From: Arjan van de Ven @ 2006-01-11  8:13 UTC (permalink / raw)
  To: David S. Miller; +Cc: drepper, linux-kernel

On Wed, 2006-01-11 at 00:00 -0800, David S. Miller wrote:
> From: Ulrich Drepper <drepper@redhat.com>
> Date: Tue, 10 Jan 2006 14:02:52 -0800
> 
> > I just saw this in a patch:
> > 
> > +               if (ntohs(ih->frag_off) & IP_OFFSET)
> > +                       return EBT_NOMATCH;
> > 
> > This isn't optimal, it requires a byte switch little endian machines.
> > The compiler isn't smart enough.  It would be better to use
> > 
> >      if (ih->frag_off & ntohs(IP_OFFSET))
> > 
> > where the byte-swap can be done at compile time.  This is kind of ugly,
> > I guess, so maybe a dedicate macro
> > 
> >     net_host_bit_p(ih->frag_off, IP_OFFSET)
> 
> The first suggestion isn't considered ugly, and the best form is:
> 
> 	if (ih->frag_off & __constant_htons(IP_OFFSET))

why this __constant_htons and not just plain htons ??
htons() gets auto-remapped to that anyway via the builtin "is this a
constant" thing...... and to be honest htons() is more readable.



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

* Re: ntohs/ntohl and bitops
  2006-01-11  8:13   ` Arjan van de Ven
@ 2006-01-11  8:36     ` David S. Miller
  2006-01-11  8:36     ` Ulrich Drepper
  1 sibling, 0 replies; 13+ messages in thread
From: David S. Miller @ 2006-01-11  8:36 UTC (permalink / raw)
  To: arjan; +Cc: drepper, linux-kernel

From: Arjan van de Ven <arjan@infradead.org>
Date: Wed, 11 Jan 2006 09:13:11 +0100

> > The first suggestion isn't considered ugly, and the best form is:
> > 
> > 	if (ih->frag_off & __constant_htons(IP_OFFSET))
> 
> why this __constant_htons and not just plain htons ??
> htons() gets auto-remapped to that anyway via the builtin "is this a
> constant" thing...... and to be honest htons() is more readable.

You're right.

We use the __constant_*() things for structure initialization
which needs to be compile time.

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

* Re: ntohs/ntohl and bitops
  2006-01-11  8:13   ` Arjan van de Ven
  2006-01-11  8:36     ` David S. Miller
@ 2006-01-11  8:36     ` Ulrich Drepper
  2006-01-11  8:44       ` David S. Miller
  1 sibling, 1 reply; 13+ messages in thread
From: Ulrich Drepper @ 2006-01-11  8:36 UTC (permalink / raw)
  To: Arjan van de Ven; +Cc: David S. Miller, linux-kernel

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

Arjan van de Ven wrote:
> why this __constant_htons and not just plain htons ??
> htons() gets auto-remapped to that anyway via the builtin "is this a
> constant" thing...... and to be honest htons() is more readable.

Indeed, why the need for the uglification?

Anyway, candidates for this kind of transformation:

net/ipx/af_ipx.c:1450:  if (ntohs(addr->sipx_port) <
IPX_MIN_EPHEMERAL_SOCKET &&
net/atm/br2684.c:308:   if (ntohs(eth->h_proto) >= 1536)
net/ipv6/netfilter/ip6t_LOG.c:114:                      if
(ntohs(fh->frag_off) & 0xFFF8)
net/ipv6/exthdrs_core.c:89:                     if (ntohs(*fp) & ~0x7)
net/ipv4/ipmr.c:1182:   if (skb->len+encap > dst_mtu(&rt->u.dst) &&
(ntohs(iph->frag_off) & IP_DF)) {
net/ipv4/netfilter/ip_conntrack_helper_pptp.c:710:      if
(ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
net/ipv4/netfilter/ipt_LOG.c:70:        if (ntohs(ih->frag_off) & IP_CE)
net/ipv4/netfilter/ipt_LOG.c:72:        if (ntohs(ih->frag_off) & IP_DF)
net/ipv4/netfilter/ipt_LOG.c:74:        if (ntohs(ih->frag_off) & IP_MF)
net/ipv4/netfilter/ipt_LOG.c:78:        if (ntohs(ih->frag_off) & IP_OFFSET)
net/ipv4/netfilter/ipt_LOG.c:108:               if (ntohs(ih->frag_off)
& IP_OFFSET)
net/ipv4/netfilter/ipt_LOG.c:180:               if (ntohs(ih->frag_off)
& IP_OFFSET)
net/ipv4/netfilter/ipt_LOG.c:221:               if (ntohs(ih->frag_off)
& IP_OFFSET)
net/ipv4/netfilter/ipt_LOG.c:286:               if (ntohs(ih->frag_off)
& IP_OFFSET)
net/ipv4/netfilter/ipt_LOG.c:311:               if (ntohs(ih->frag_off)
& IP_OFFSET)
net/bridge/netfilter/ebt_ip.c:55:               if (ntohs(ih->frag_off)
& IP_OFFSET)
net/sunrpc/auth_gss/svcauth_gss.c:793:  if (ntohl(svc_getu32(argv)) !=
RPC_GSS_VERSION)
net/sunrpc/auth_gss/svcauth_gss.c:823:          if
(ntohl(svc_getu32(argv)) != RPC_AUTH_NULL)
net/sunrpc/auth_gss/svcauth_gss.c:825:          if
(ntohl(svc_getu32(argv)) != 0)
net/bluetooth/bnep/core.c:343:  if (ntohs(s->eh.h_proto) == 0x8100) {


(svcauth_gss.c:825 is a good one).



For consistency reasons, ntohs should be changed to htons here:

net/ipv6/netfilter/ip6t_eui64.c:43:    if (eth_hdr(skb)->h_proto ==
ntohs(ETH_P_IPV6)) {


Around net/ipv4/ipconfig.c:376 you might want to store the transformed
ic_myaddr in a variable instead of repeating the ntohl call.

-- 
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

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

* Re: ntohs/ntohl and bitops
  2006-01-11  8:36     ` Ulrich Drepper
@ 2006-01-11  8:44       ` David S. Miller
  2006-01-11  9:48         ` Jakub Jelinek
  0 siblings, 1 reply; 13+ messages in thread
From: David S. Miller @ 2006-01-11  8:44 UTC (permalink / raw)
  To: drepper; +Cc: arjan, linux-kernel

From: Ulrich Drepper <drepper@redhat.com>
Date: Wed, 11 Jan 2006 00:36:11 -0800

> Anyway, candidates for this kind of transformation:
> 
> net/ipx/af_ipx.c:1450:  if (ntohs(addr->sipx_port) <
> IPX_MIN_EPHEMERAL_SOCKET &&

Does it work for comparisons?  F.e.:

     if (val < ntohs(VAL))




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

* Re: ntohs/ntohl and bitops
  2006-01-11  8:44       ` David S. Miller
@ 2006-01-11  9:48         ` Jakub Jelinek
  2006-01-11 18:35           ` Jan Engelhardt
  0 siblings, 1 reply; 13+ messages in thread
From: Jakub Jelinek @ 2006-01-11  9:48 UTC (permalink / raw)
  To: David S. Miller; +Cc: drepper, arjan, linux-kernel

On Wed, Jan 11, 2006 at 12:44:18AM -0800, David S. Miller wrote:
> From: Ulrich Drepper <drepper@redhat.com>
> Date: Wed, 11 Jan 2006 00:36:11 -0800
> 
> > Anyway, candidates for this kind of transformation:
> > 
> > net/ipx/af_ipx.c:1450:  if (ntohs(addr->sipx_port) <
> > IPX_MIN_EPHEMERAL_SOCKET &&
> 
> Does it work for comparisons?  F.e.:
> 
>      if (val < ntohs(VAL))

No, only for ==, !=, &, |, ~.

	Jakub

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

* Re: ntohs/ntohl and bitops
  2006-01-11  9:48         ` Jakub Jelinek
@ 2006-01-11 18:35           ` Jan Engelhardt
  2006-01-11 18:45             ` Ulrich Drepper
  0 siblings, 1 reply; 13+ messages in thread
From: Jan Engelhardt @ 2006-01-11 18:35 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: David S. Miller, drepper, arjan, linux-kernel

>> Does it work for comparisons?  F.e.:
>> 
>>      if (val < ntohs(VAL))
>
>No, only for ==, !=, &, |, ~.

And ^?


Jan Engelhardt
-- 

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

* Re: ntohs/ntohl and bitops
  2006-01-11 18:35           ` Jan Engelhardt
@ 2006-01-11 18:45             ` Ulrich Drepper
  2006-01-11 20:19               ` David S. Miller
  0 siblings, 1 reply; 13+ messages in thread
From: Ulrich Drepper @ 2006-01-11 18:45 UTC (permalink / raw)
  To: Jan Engelhardt; +Cc: Jakub Jelinek, David S. Miller, arjan, linux-kernel

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

Jan Engelhardt wrote:
> And ^?

^ is fine.

And just to be complete: I sent DaveM the correct line for that file in
question, I just C&Ped the wrong line my scripts produced.  The mail
seem not to make it to lkml.  In fact, none of my mail originated from
the gmail account ever made it.  I don't know what filter doesn't like me.

-- 
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

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

* Re: ntohs/ntohl and bitops
  2006-01-11 18:45             ` Ulrich Drepper
@ 2006-01-11 20:19               ` David S. Miller
  0 siblings, 0 replies; 13+ messages in thread
From: David S. Miller @ 2006-01-11 20:19 UTC (permalink / raw)
  To: drepper; +Cc: jengelh, jakub, arjan, linux-kernel

From: Ulrich Drepper <drepper@redhat.com>
Date: Wed, 11 Jan 2006 10:45:00 -0800

> And just to be complete: I sent DaveM the correct line for that file in
> question, I just C&Ped the wrong line my scripts produced.  The mail
> seem not to make it to lkml.  In fact, none of my mail originated from
> the gmail account ever made it.  I don't know what filter doesn't like me.

This one:

m/^Content-Transfer-Encoding:\s*base64/i

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

* Re: ntohs/ntohl and bitops
  2006-01-11  8:00 ` David S. Miller
  2006-01-11  8:13   ` Arjan van de Ven
@ 2006-01-12  1:04   ` Pavel Machek
  2006-01-14  9:52     ` YOSHIFUJI Hideaki / 吉藤英明
  2006-01-15  1:46     ` David S. Miller
  1 sibling, 2 replies; 13+ messages in thread
From: Pavel Machek @ 2006-01-12  1:04 UTC (permalink / raw)
  To: David S. Miller; +Cc: drepper, linux-kernel


On Wed 11-01-06 00:00:20, David S. Miller wrote:
> From: Ulrich Drepper <drepper@redhat.com>
> Date: Tue, 10 Jan 2006 14:02:52 -0800
> 
> > I just saw this in a patch:
> > 
> > +               if (ntohs(ih->frag_off) & IP_OFFSET)
> > +                       return EBT_NOMATCH;
> > 
> > This isn't optimal, it requires a byte switch little endian machines.
> > The compiler isn't smart enough.  It would be better to use
> > 
> >      if (ih->frag_off & ntohs(IP_OFFSET))
> > 
> > where the byte-swap can be done at compile time.  This is kind of ugly,
> > I guess, so maybe a dedicate macro
> > 
> >     net_host_bit_p(ih->frag_off, IP_OFFSET)
> 
> The first suggestion isn't considered ugly, and the best form is:
> 
> 	if (ih->frag_off & __constant_htons(IP_OFFSET))
> 
> I'll fix that up when I get a chance, thanks for catching it Uli.

Could you possibly 
#define IP_OFFSET htons(1234)
?

Noone should need it in native endianity, no?

-- 
Thanks, Sharp!

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

* Re: ntohs/ntohl and bitops
  2006-01-12  1:04   ` Pavel Machek
@ 2006-01-14  9:52     ` YOSHIFUJI Hideaki / 吉藤英明
  2006-01-15  1:46     ` David S. Miller
  1 sibling, 0 replies; 13+ messages in thread
From: YOSHIFUJI Hideaki / 吉藤英明 @ 2006-01-14  9:52 UTC (permalink / raw)
  To: pavel; +Cc: davem, drepper, linux-kernel, yoshfuji

In article <20060112010406.GA2367@ucw.cz> (at Thu, 12 Jan 2006 01:04:06 +0000), Pavel Machek <pavel@ucw.cz> says:

> 
> On Wed 11-01-06 00:00:20, David S. Miller wrote:
> > From: Ulrich Drepper <drepper@redhat.com>
> > Date: Tue, 10 Jan 2006 14:02:52 -0800
> > 
> > > I just saw this in a patch:
> > > 
> > > +               if (ntohs(ih->frag_off) & IP_OFFSET)
> > > +                       return EBT_NOMATCH;
> > > 
> > > This isn't optimal, it requires a byte switch little endian machines.
> > > The compiler isn't smart enough.  It would be better to use
> > > 
> > >      if (ih->frag_off & ntohs(IP_OFFSET))
:
> Could you possibly 
> #define IP_OFFSET htons(1234)
> ?

In this case, you should use __constant_htons().
I still prefer:
   if (ih->frag_off & htons(IP_OFFSET))
though.

--yoshfuji

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

* Re: ntohs/ntohl and bitops
  2006-01-12  1:04   ` Pavel Machek
  2006-01-14  9:52     ` YOSHIFUJI Hideaki / 吉藤英明
@ 2006-01-15  1:46     ` David S. Miller
  1 sibling, 0 replies; 13+ messages in thread
From: David S. Miller @ 2006-01-15  1:46 UTC (permalink / raw)
  To: pavel; +Cc: drepper, linux-kernel

From: Pavel Machek <pavel@ucw.cz>
Date: Thu, 12 Jan 2006 01:04:06 +0000

> Could you possibly 
> #define IP_OFFSET htons(1234)
> ?
> 
> Noone should need it in native endianity, no?

That's definitely going to be error prone.
And I bet the BSD headers define it in cpu endainness
as well.

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

end of thread, other threads:[~2006-01-15  1:50 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-10 22:02 ntohs/ntohl and bitops Ulrich Drepper
2006-01-11  8:00 ` David S. Miller
2006-01-11  8:13   ` Arjan van de Ven
2006-01-11  8:36     ` David S. Miller
2006-01-11  8:36     ` Ulrich Drepper
2006-01-11  8:44       ` David S. Miller
2006-01-11  9:48         ` Jakub Jelinek
2006-01-11 18:35           ` Jan Engelhardt
2006-01-11 18:45             ` Ulrich Drepper
2006-01-11 20:19               ` David S. Miller
2006-01-12  1:04   ` Pavel Machek
2006-01-14  9:52     ` YOSHIFUJI Hideaki / 吉藤英明
2006-01-15  1:46     ` David S. Miller

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