1.11M Members

alignment in structs

 
0
 

i am receiving a struct from the switch:

/* Packet received on port (datapath -> controller). */
struct ofp_packet_in {
    struct ofp_header header;
    uint32_t buffer_id;     /* ID assigned by datapath. */
    uint16_t total_len;     /* Full length of frame. */
    uint16_t in_port;       /* Port on which frame was received. */
    uint8_t reason;         /* Reason packet is being sent (one of OFPR_*) */
    uint8_t pad;
    uint8_t data[0];        /* Ethernet frame, halfway through 32-bit word,
                               so the IP header is 32-bit aligned.  The
                               amount of data is inferred from the length
                               field in the header.  Because of padding,
                               offsetof(struct ofp_packet_in, data) ==
                               sizeof(struct ofp_packet_in) - 2. */
};
assert(sizeof(struct ofp_packet_in) == 20);

after inspecting the length field in the header i have successfully received the data in a separate char array. the data starts with 2 bytes of padding and the next 14 bytes contain the ethernet header. now the problem lies with the IP header alignment. if i consider the the next 4-bytes after ethernet header to be padded and extract the IP header using standard iphdr struct and cast it to the data array with an offset of 16-bytes:

struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
	__u8	ihl:4,
		version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
	__u8	version:4,
  		ihl:4;
#else
#error	"Please fix <asm/byteorder.h>"
#endif
	__u8	tos;
	__be16	tot_len;
	__be16	id;
	__be16	frag_off;
	__u8	ttl;
	__u8	protocol;
	__sum16	check;
	__be32	saddr;
	__be32	daddr;
};

i am getting the src IP to be zero. how is this IP header aligned in this case? the data output in hex is shown below:

---------Data---Starts----

ff ff ff ff 50 54 00 00 00 03 08 06 00 01 08 00 06 04 00 01 50 54 00 00 00 03 c0 a8 64 01 00 00 00 00 00 00 c0 a8 64 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

--------Data---Ends-----

Destination MAC: FF FF 50 54 00 00 
Source MAC: 00 03 08 06 00 01 
protocol: 08 00

nw_tos: 84
tot_len:0
id:3
frag off:49320
ttl:100
nw_proto:1
checksum:0
src IP address i.e nw_src: 0
dest IP address i.e. nw_dst: 40151232

thanks.

 
1
 

Change the packing factor to 1 and the alignment problem will go away. #pragma pack(1) will not put holes in the structure. The pragma is compiler dependent, some compilers may do it differently.

#pragma pack(1)
// your structure goes here
#pragma pack() // revert to default
 
0
 

Change the packing factor to 1 and the alignment problem will go away. #pragma pack(1) will not put holes in the structure. The pragma is compiler dependent, some compilers may do it differently.

#pragma pack(1)
// your structure goes here
#pragma pack() // revert to default

thnanx alot. it resolved my issue

Question Answered as of 2 Years Ago by Ancient Dragon
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: