Intel® IXP400 Software

Endianness in Intel® IXP400 Software

Little:0x8

Big:0x0

The following provides another example of endianness causing the code to be interpreted differently on BE versus LE machines:

int myString[2] = { 0x61626364,0}; /* hex values for ascii */ Printf(“%s\n”, (char *)&myString);

Depending on the endianness of the processor the code is executing on, the result is:

Little:“dcba”

Big:“abcd”

27.3.1.2Network Stacks and Protocols

Little-Endian Machines: Running a network protocol stack on a Little-Endian processor can degrade performance due to formatting translation. If a network protocol stack is to be run on a Little-Endian processor, at run time it will have to reorder the bytes of every multi-byte data field within the various layers' headers.

Big-Endian Machines: Running a network protocol stack on a Big-Endian processor does not degrade performance due to formatting translation. If the stack will run on a Big-Endian processor, there is nothing to worry about; the endianness of the processor inherently matches the format of standard network data ordering.

27.3.1.3Shared Data Example: LE Re-Ordering Data for BE Network Traffic

By using a macro conversion routine, the data access is re-ordered as needed to properly interpret data moving between a network (which is using Big-Endian or network order) and a host machine, which may be Little-Endian.

Basic Assumptions:

TCP/IP defines the network byte order as Big-Endian.

Little-Endian machines must byte swap accesses to 16-/32-bit data types (IP address, checksum, etc.).

Example: We want to assign the value of the IP source address field in the header of an IP packet to a 32-bit value we will call “src.” Here is the code, which features a macro to translate.

u_long src = ntohl(ip->ip_src.s_addr);

Here is what the macro ntohl() looks like in actual code:

–ntohl()

 

{

 

#if (_BYTE_ORDER == _BIG_ENDIAN)

 

#define ntohl(x)

(x)

#else

#define ntohl(x)((((x) & 0x000000ff) << 24) \

(((x) & 0x0000ff00) << 8) \

(((x) & 0x00ff0000) >> 8) \

(((x) & 0xff000000) >> 24))

#endif

}

April 2005

IXP400 Software Version 2.0

Programmer’s Guide

344

Document Number: 252539, Revision: 007

 

Page 344
Image 344
Intel IXP400 manual Network Stacks and Protocols, Here is what the macro ntohl looks like in actual code