packets: Do not assume that IPv4, TCP, or ARP headers are 32-bit aligned.
authorBen Pfaff <blp@nicira.com>
Thu, 15 Aug 2013 17:47:39 +0000 (10:47 -0700)
committerBen Pfaff <blp@nicira.com>
Thu, 15 Aug 2013 19:34:54 +0000 (12:34 -0700)
commit11ff30bdd5e003a26851f7aab43498842700dffd
tree7502057213339ff8ed89b3276b7cfbdbf9a6d261
parent02e51c6a0fffe8db84a0d4c39d9dd26f2f36a68c
packets: Do not assume that IPv4, TCP, or ARP headers are 32-bit aligned.

Ethernet headers are 14 bytes long, so when the beginning of such a header
is 32-bit aligned, the following data is misaligned.  The usual trick to
fix that is to start the Ethernet header on an odd-numbered 16-bit
boundary.  That trick works OK for Open vSwitch, but there are two
problems:

   - OVS doesn't use that trick everywhere.  Maybe it should, but it's
     difficult to make sure that it does consistently because the CPUs
     most commonly used with OVS don't care about misalignment, so we
     only find problems when porting.

   - Some protocols (GRE, VXLAN) don't use that trick, so in such a case
     one can properly align the inner or outer L3/L4/L7 but not both.  (OVS
     userspace doesn't directly deal with such protocols yet, so this is
     just future-proofing.)

   - OpenFlow uses the alignment trick in a few places but not all of them.

This commit starts the adoption of what I hope will be a more robust way
to avoid misalignment problems and the resulting bus errors on RISC
architectures.  Instead of trying to ensure that 32-bit quantities are
always aligned, we always read them as if they were misaligned.  To ensure
that they are read this way, we change their types from 32-bit types to
pairs of 16-bit types.  (I don't know of any protocols that offset the
next header by an odd number of bytes, so a 16-bit alignment assumption
seems OK.)

The same would be necessary for 64-bit types in protocol headers, but we
don't yet have any protocol definitions with 64-bit types.

IPv6 protocol headers need the same treatment, but for those we rely on
structs provided by system headers, so I'll leave them for an upcoming
patch.

Signed-off-by: Ben Pfaff <blp@nicira.com>
include/openvswitch/types.h
lib/flow.c
lib/packets.c
lib/packets.h
lib/unaligned.h