lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] Re: [lwip] Packed structures


From: Adam Dunkels
Subject: [lwip-users] Re: [lwip] Packed structures
Date: Thu, 09 Jan 2003 00:07:01 -0000

On Wednesday 02 January 2002 18.55, you wrote:
> > I guess this depends on the compiler, but in my experience it is only the
> > arp_hdr that needs to be packet. The other headers are aligned in such a
> > way that 16-bit word accesses are on a 16-bit word boundary, 32-bit
> > accesses are on a 32-bit boundary, etc. I guess the protocol designers
> > had this in mind when they designed the header layouts.
>
> I will try that out, I hope it will not be aligned on 32bit boundaries.

Incoming packets can be properly aligned by the device driver since the ARP 
header always is 14 bytes long. By leaving two bytes at the top of the pbuf, 
the IP header will always be 32-bit aligned. Since the IP header without IP 
options always is 20 bytes, the TCP header will be aligned as well and since 
the TCP header without options is 20 bytes, the TCP data will be aligned. (Of 
course, the TCP data might be misaligned anyway if the sender has segmented 
it in such a way.)

Outgoing packets should always be aligned on 32-bit boundaries by setting the 
MEM_ALIGNMENT option to 4 in lwipopts.h and the PBUF_LINK_HLEN to 16 (the 
length of the aligned Ethernet header). Note that I haven't tested this in a 
system that actually traps misaligned memory references.

> > When it comes to the best way to pack the structure, I would think that
> > something along the lines of a PACK_STRUCT macro and a PACK_STRUCT_FIELD
> > macro would be the way to go. The actual #definition of the macro could
> > be put in the arch/cc.h file and the structs that needs packing could be
> > defined as:
> >
> > PACK_STRUCT(
> > struct a_struct_that_needs_to_be_packed {
> >    PACK_STRUCT_FIELD(u8_t a);
> >    PACK_STRUCT_FIELD(u32_t b);
> > }
> > )
> >
> > But it would looks rather cluttred...
>
> But it would only be for very few headers. The only problem I see with this
> is that some compilers don't like #'s in #defines.

You are right. Perhaps a scheme with two macros, PACK_STRUCT_PREAMBLE and 
PACK_STRUCT_POSTAMBLE or something like that, would be better.

/adam
-- 
Adam Dunkels <address@hidden>
http://www.sics.se/~adam
[This message was sent through the lwip discussion list.]




reply via email to

[Prev in Thread] Current Thread [Next in Thread]