# alignment requirement when compiling TCP/IP implementation



## baihaoyuan (Aug 21, 2011)

Hi

I am reading TCP/IP Illustrated, Vol. 2 by Stevens. In page 215, it says 

'In C, the easiest way to process data located in an untyped area of memory is to overlay a structure on the area of memory and process the structure members instead of raw bytes...'

In the source code, it's like this:


```
struct ip *ip;
struct mbuf *m;

...

ip = mtod(m, struct ip*);

//then use ip member w/o parsing the header
ip->ip_sum ..
```

My question is struct ip has the same bytes layout of ip protocol header only when alignment is 1, but this would hurt performance, or even not allowed on certain CPUs

Can someone help me understand this?

Thanks,
Baihao


----------



## expl (Aug 21, 2011)

Packet duplication to an aligned structure would hurt performance more than reading unaligned data using register shifting.


----------

