lwip-users
[Top][All Lists]
Advanced

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

RE: [lwip-users] Question on structure packing


From: Robert Brown
Subject: RE: [lwip-users] Question on structure packing
Date: Tue, 20 Apr 2004 19:31:12 -0400

I had similar problems with a buggy compiler.  I made a quick and dirty work 
around to avoid the need for packed structures.  My target was an ARM7 with a 
RTK8019AS ethernet device.  There are a few changes required:

/*-----------------------------------------------------------------------------------*/
/*
 * Explanation of the contortions performed on input and where everything is.  
 *
 * The lwIP stack uses C-structs to read/write protocol headers in an 
 * incoming/outgoing data segment.  The definition of these structures
 * relies on packing which allows header fields to be read/written by name even
 * when they are not word-aligned.
 * 
 * For some reason the ARM compiler used for this project does not 
 * handle packed structures very well.  We avoid this problem by introducing 
 * pad fields into the data structures.  The 2-octet pad field between the
 * Ethernet header and the IP header is added by the Ethernet device driver for 
the
 * network interface. 
 *
 * This pad field forces the IP header to be word-aligned so packing is not 
 * required.  
 *
 *
 *
 **********************************
 * Ethernet and IP headers with pading added in driver:
 *
 *         src mac          dest mac     type   pad
 *                    |                 |     |     |
 *   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
 *              |           |           |           |
 *    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 
 *
 *    v_hl                    ttl
 *    tos   len   id   offst proto chksm   src ip
 *        |     |     |     |     |     |           |
 *   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
 *              |           |           |           |
 *   16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
 *
 *      dest ip   IP payload (headers, data, etc.) 
 *              |  
 *   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
 *              |           |           |           |
 *   32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 
 *
 *
 *
 **********************************
 * Special case for ARP packets:
 * For ARP packets additional pad fields are required after 
 * each Ethernet header field as illustrated below.
 * 
 *         src mac          dest mac     type   pad
 *                    |                 |     |     |
 *   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
 *              |           |           |           |
 *    0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 
 *
 *               proto
 *   hw          len
 *   type  proto hwlen opcode   shwaddr        pad1   
 *        |     |     |     |                 |    |
 *   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
 *              |           |           |           |
 *   16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
 *
 *    sipaddr         dhwaddr       pad2  dipaddr  
 *              |                 |     |           |
 *   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
 *              |           |           |           |
 *   32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 
 *
 */


1. For IP add two octets of "padding" between the ethernet header and the IP 
header to ensure that the IP header is 4 byte aligned as follows:

PACK_STRUCT_BEGIN
struct eth_hdr {
  PACK_STRUCT_FIELD(struct eth_addr dest);
  PACK_STRUCT_FIELD(struct eth_addr src);
  PACK_STRUCT_FIELD(u16_t type);
  PACK_STRUCT_FIELD(u16_t pad);  //<----added
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END

2. For ARP add two "pad" elements to word-align the IP addresses as follows:

PACK_STRUCT_BEGIN
/** the ARP message */
struct etharp_hdr {
  PACK_STRUCT_FIELD(struct eth_hdr ethhdr);
  PACK_STRUCT_FIELD(u16_t hwtype);
  PACK_STRUCT_FIELD(u16_t proto);
  PACK_STRUCT_FIELD(u16_t _hwlen_protolen);
  PACK_STRUCT_FIELD(u16_t opcode);
  PACK_STRUCT_FIELD(struct eth_addr shwaddr);
  PACK_STRUCT_FIELD(u16_t pad1);  //<----added
  PACK_STRUCT_FIELD(struct ip_addr sipaddr);
  PACK_STRUCT_FIELD(struct eth_addr dhwaddr);
  PACK_STRUCT_FIELD(u16_t pad2);  //<----added
  PACK_STRUCT_FIELD(struct ip_addr dipaddr);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END

5. Modify etharp_arp_input() and etharp_query() in etharp.c to remove padding 
from outgoing ARP replies and ARP queries, respectively.  Following is a snip 
of code that may serve the purpose.

        /* Remove padding.  It is valid to assume that for
         * an ARP reply there is only one pbuf in action.  */
        {
                unsigned char *payload;
                unsigned char *packed_payload;
                int i;

                payload = (unsigned char *) &(hdr->sipaddr);
                packed_payload = payload - 2;
        
                /* Remove hdr->pad1 until we encounter hdr->pad2 */
                for (i = 0; i < 10; i++)
                {
                        *packed_payload++ = *payload++;
                }
                /* Reduce the length of the packet by 2 */
                p->len -= 2;
                p->tot_len -= 2;
                /* Advance past hdr->pad2  */
                payload += 2;
                /* Remove hdr->pad2 and cover the last for bytes with ethernet 
padding */
                for (i = 0; i < 8; i++)
                {
                        *packed_payload++ = *payload++;
                }
                /* Reduce the length of the packet by 2 */
                p->len -= 2;
                p->tot_len -= 2;
        }
      /* return ARP reply */
      netif->linkoutput(netif, p);

4. Modify your ethernet driver to skip the padding octets when reading/writing 
from/to the hardware


I think this is everything we changed to get it to work.  

Regards,
Rob




-----Original Message-----
From: address@hidden
[mailto:address@hidden Behalf
Of John BODDIE
Sent: Tuesday, April 20, 2004 12:12 PM
To: 'Mailing list for lwIP users'
Subject: RE: [lwip-users] Question on structure packing


Hi Tony,

I'm currently working on an STi5516/17 Eval System with a DB558 ethernet STEM 
module.

Would there be any chance of getting a copy of your modified sources?

Regards,

John

-----Original Message-----
From: address@hidden [mailto:address@hidden On Behalf Of Mountifield, Tony
Sent: 20 April 2004 16:48
To: Mailing list for lwIP users
Subject: RE: [lwip-users] Question on structure packing


Hi John,

I am doing exactly the same exercise for the STi5516 evaluation system, and 
found exactly the same problem. I have it working now, although still some 
tidying to do, but had to rewrite the struture elements as arrays of bytes 
instead of u16 and u32, and use macros to access them.

Since you're at ST, you probably know more about the issues than I do. I found 
that if I did a longword access with a pointer that was not a multiple of four, 
it fetched an aligned longword anyway. e.g.: if the pointer value was NNN0, 
NNN1, NNN2 or NNN3, it would fetch the longword consisting of those bytes, 
NNN0/1/2/3. So you can't even play clever games with pointers, and have instead 
to fetch a byte at a time and stick them together.

Cheers,
Tony

> -----Original Message-----
> From: John BODDIE [mailto:address@hidden
> Sent: 20 April 2004 16:30
> To: address@hidden
> Subject: [lwip-users] Question on structure packing
> 
> 
> Hi,
> 
> I am currently porting lwIP 0.7.1 to run under OS20 on a
> 32-bit STMicro ST20. Unfortunately the ST20 compiler does not 
> support structure packing leading to all sorts of data 
> corruption problems.
> 
> Does anyone know of anyway around the issue of structure packing?
> 
> Regards,
> 
> John
> 
> 
> 
> _______________________________________________
> lwip-users mailing list
> address@hidden http://mail.gnu.org/mailman/listinfo/lwip-users
> 


***********************************************************************************
This email, its content and any attachments is PRIVATE AND CONFIDENTIAL to 
TANDBERG Television. If received in error please notify the sender and destroy 
the original message and attachments.

www.tandbergtv.com
***********************************************************************************




_______________________________________________
lwip-users mailing list
address@hidden
http://mail.gnu.org/mailman/listinfo/lwip-users





reply via email to

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