All of lore.kernel.org
 help / color / mirror / Atom feed
* Purpose of using __be16 inside a data structure?
@ 2011-08-09  7:25 陳國成
  2011-08-24  5:16 ` Adam Lee
  2011-10-24 19:54 ` Kevin O'Gorman
  0 siblings, 2 replies; 7+ messages in thread
From: 陳國成 @ 2011-08-09  7:25 UTC (permalink / raw)
  To: kernelnewbies

Hi all,

Please tell me the difference between using unsigned short and __be16 to
declear a variable in a data structure.
For example, in include/net/inet_sock.h, struct inet_sock is decleared as

struct inet_sock {
        __be16                  inet_dport;
};

not

 struct inet_sock {
        unsigned short                  inet_dport;
};

I think it has something to do with endianness. But I see that both __le16
and __be16 are unsigned short when using gcc with -E option for
little-endian and big-endian platform. Can someone give me more information?

* little-endian
typedef unsigned short __u16;
typedef __u16 __le16;
typedef __u16 __be16;

* big-endian
typedef unsigned short __u16;
typedef __u16 __le16;
typedef __u16 __be16;
Regards,
GC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110809/f270b41a/attachment-0001.html 

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

* Purpose of using __be16 inside a data structure?
  2011-08-09  7:25 Purpose of using __be16 inside a data structure? 陳國成
@ 2011-08-24  5:16 ` Adam Lee
  2011-08-25  5:41   ` 陳國成
  2011-10-24 19:54 ` Kevin O'Gorman
  1 sibling, 1 reply; 7+ messages in thread
From: Adam Lee @ 2011-08-24  5:16 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Aug 09, 2011 at 03:25:52PM +0800, ??? wrote:
> Please tell me the difference between using unsigned short and __be16 to
> declear a variable in a data structure.
> For example, in include/net/inet_sock.h, struct inet_sock is decleared as
> ?
> struct inet_sock {
> ??????? __be16????????????????? inet_dport;
> };
> ?
> not
> ?
> struct inet_sock {
> ????????unsigned short????????????????? inet_dport;
> };
> ?
> I think it has something to do with endianness. But I see that both __le16 and
> __be16 are unsigned short when using gcc with -E option?for little-endian and
> big-endian platform. Can someone give me more information?
> ?
> * little-endian
> typedef unsigned short __u16;
> typedef __u16 __le16;
> typedef __u16 __be16;
> ?
> * big-endian
> typedef unsigned short __u16;
> typedef __u16 __le16;
> typedef __u16 __be16;
>

Which platform? The atomic element size is 16-bit? Maybe you should take
a look at __be32 and __le32.

FYI: This is in the kernel types.h

----
#ifdef __CHECKER__
#define __bitwise__ __attribute__((bitwise))
#else
#define __bitwise__
#endif
#ifdef __CHECK_ENDIAN__
#define __bitwise __bitwise__
#else
#define __bitwise
#endif

typedef __u16 __bitwise __le16;
typedef __u16 __bitwise __be16;
----

-- 
Regards,
Adam Lee
--------------------------------------------------
E-mail: adam8157 at gmail.com
Website: http://www.adam8157.info
--------------------------------------------------

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

* Purpose of using __be16 inside a data structure?
  2011-08-24  5:16 ` Adam Lee
@ 2011-08-25  5:41   ` 陳國成
  0 siblings, 0 replies; 7+ messages in thread
From: 陳國成 @ 2011-08-25  5:41 UTC (permalink / raw)
  To: kernelnewbies

Hi Adam,

1. Which platform?

* little-endian
ARM

* big-endian
MIPS

2. The atomic element size is 16-bit?
No idea about this question.

3. Maybe you should take a look at __be32 and __le32.

* little-endian
# 1 "arch/arm/include/asm/types.h" 1
# 1 "include/asm-generic/int-ll64.h" 1
# 17 "include/asm-generic/int-ll64.h"
typedef __signed__ char __s8;
typedef unsigned char __u8;

typedef __signed__ short __s16;
typedef unsigned short __u16;

typedef __signed__ int __s32;
typedef unsigned int __u32;

# 168 "include/linux/types.h"
typedef __u16 __le16;
typedef __u16 __be16;
typedef __u32 __le32;
typedef __u32 __be32;


* big-endian
# 21 "arch/mips/include/asm/types.h"
# 1 "include/asm-generic/int-ll64.h" 1
# 17 "include/asm-generic/int-ll64.h"
typedef __signed__ char __s8;
typedef unsigned char __u8;

typedef __signed__ short __s16;
typedef unsigned short __u16;

typedef __signed__ int __s32;
typedef unsigned int __u32;

# 168 "include/linux/types.h"
typedef __u16 __le16;
typedef __u16 __be16;
typedef __u32 __le32;
typedef __u32 __be32;


I'm using Linux 2.6.30.

Regards,
GC


2011/8/24 Adam Lee <adam8157@gmail.com>:
> On Tue, Aug 09, 2011 at 03:25:52PM +0800, ??? wrote:
>> Please tell me the difference between using unsigned short and __be16 to
>> declear a variable in a data structure.
>> For example, in include/net/inet_sock.h, struct inet_sock is decleared as
>>
>> struct inet_sock {
>>         __be16                  inet_dport;
>> };
>>
>> not
>>
>> struct inet_sock {
>>         unsigned short                  inet_dport;
>> };
>>
>> I think it has something to do with endianness. But I see that both __le16 and
>> __be16 are unsigned short when using gcc with -E option for little-endian and
>> big-endian platform. Can someone give me more information?
>>
>> * little-endian
>> typedef unsigned short __u16;
>> typedef __u16 __le16;
>> typedef __u16 __be16;
>>
>> * big-endian
>> typedef unsigned short __u16;
>> typedef __u16 __le16;
>> typedef __u16 __be16;
>>
>
> Which platform? The atomic element size is 16-bit? Maybe you should take
> a look at __be32 and __le32.
>
> FYI: This is in the kernel types.h
>
> ----
> #ifdef __CHECKER__
> #define __bitwise__ __attribute__((bitwise))
> #else
> #define __bitwise__
> #endif
> #ifdef __CHECK_ENDIAN__
> #define __bitwise __bitwise__
> #else
> #define __bitwise
> #endif
>
> typedef __u16 __bitwise __le16;
> typedef __u16 __bitwise __be16;
> ----
>
> --
> Regards,
> Adam Lee
> --------------------------------------------------
> E-mail: adam8157 at gmail.com
> Website: http://www.adam8157.info
> --------------------------------------------------
>

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

* Purpose of using __be16 inside a data structure?
  2011-08-09  7:25 Purpose of using __be16 inside a data structure? 陳國成
  2011-08-24  5:16 ` Adam Lee
@ 2011-10-24 19:54 ` Kevin O'Gorman
  2011-10-25 14:25   ` Jonathan Neuschäfer
  1 sibling, 1 reply; 7+ messages in thread
From: Kevin O'Gorman @ 2011-10-24 19:54 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Aug 9, 2011 at 12:25 AM, ??? <gcchen.org@gmail.com> wrote:

> Hi all,
>
> Please tell me the difference between using unsigned short and __be16 to
> declear a variable in a data structure.
>
> The purpose of this and similar artificial types is to allow the code to be
written in an architecture-independent way.  The name is chosen to reflect
the behavior (signedness, capacity, precision, etc) that is desired, and the
headers for each architecture typedef it to a standard C type that
accomplishes this goal.  From the name, I expect this to be a 16-bit type.
It may have other attributes, such as a specific endianness (unusual except
in networking), or specific signedness.  Except on unusual or antique
architectures, I would expect this one to resolve to "short" or "unsigned
short", but other possibilities exist.

What you should usually do is to find the typedef of __be16 in the headers
and see if there is a clue to what use this type is intended for.

-- 
Kevin O'Gorman, PhD
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20111024/6af5e1e4/attachment.html 

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

* Purpose of using __be16 inside a data structure?
  2011-10-24 19:54 ` Kevin O'Gorman
@ 2011-10-25 14:25   ` Jonathan Neuschäfer
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Neuschäfer @ 2011-10-25 14:25 UTC (permalink / raw)
  To: kernelnewbies

On Mon, Oct 24, 2011 at 12:54:41PM -0700, Kevin O'Gorman wrote:
> On Tue, Aug 9, 2011 at 12:25 AM, ??? <gcchen.org@gmail.com> wrote:
> 
> > Hi all,
> >
> > Please tell me the difference between using unsigned short and __be16 to
> > declear a variable in a data structure.
[...]
> accomplishes this goal.  From the name, I expect this to be a 16-bit type.
> It may have other attributes, such as a specific endianness (unusual except

Yes, it's for 16-bit big-endian data.

--
Jonathan Neusch?fer

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

* Purpose of using __be16 inside a data structure?
  2011-08-14  0:41 陳國成
@ 2011-08-14  6:21 ` Michael Blizek
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Blizek @ 2011-08-14  6:21 UTC (permalink / raw)
  To: kernelnewbies

Hi!

On 08:41 Sun 14 Aug     , ?????? wrote:
> Hi all,
> 
> Please tell me the difference between using unsigned short and __be16
> to declear a variable in a data structure.
> For example, in include/net/inet_sock.h, struct inet_sock is decleared as
> 
> struct inet_sock {
> ??????? __be16????????????????? inet_dport;
> };
> 
> not
> 
> struct inet_sock {
> ????????unsigned short????????????????? inet_dport;
> };

This is mostly to avoid confusion about where byte order conversation has to
be done. When reading "unsigned short" you would normally expect the variable
to contain host byte order.

	-Michi
-- 
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com

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

* Purpose of using __be16 inside a data structure?
@ 2011-08-14  0:41 陳國成
  2011-08-14  6:21 ` Michael Blizek
  0 siblings, 1 reply; 7+ messages in thread
From: 陳國成 @ 2011-08-14  0:41 UTC (permalink / raw)
  To: kernelnewbies

Hi all,

Please tell me the difference between using unsigned short and __be16
to declear a variable in a data structure.
For example, in include/net/inet_sock.h, struct inet_sock is decleared as

struct inet_sock {
??????? __be16????????????????? inet_dport;
};

not

struct inet_sock {
????????unsigned short????????????????? inet_dport;
};

I think it has something to do with endianness. But I see that both
__le16 and __be16 are unsigned short when using gcc with -E option?for
little-endian and big-endian platform. Can someone give me more
information?

* little-endian
typedef unsigned short __u16;
typedef __u16 __le16;
typedef __u16 __be16;

* big-endian
typedef unsigned short __u16;
typedef __u16 __le16;
typedef __u16 __be16;

Regards,
GC

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

end of thread, other threads:[~2011-10-25 14:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-09  7:25 Purpose of using __be16 inside a data structure? 陳國成
2011-08-24  5:16 ` Adam Lee
2011-08-25  5:41   ` 陳國成
2011-10-24 19:54 ` Kevin O'Gorman
2011-10-25 14:25   ` Jonathan Neuschäfer
2011-08-14  0:41 陳國成
2011-08-14  6:21 ` Michael Blizek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.