lwip-devel
[Top][All Lists]
Advanced

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

[lwip-devel] RFC: making packet handling more platform-independent


From: Atte Kojo
Subject: [lwip-devel] RFC: making packet handling more platform-independent
Date: Fri, 12 Mar 2004 10:55:43 +0200

The current implementations of IP, UDP and TCP make the assumption that
data in packets can be addressed byte by byte. But this is only true for
processors with byte addressing. I had to change a lot of code to get
lwIP working on a processor with no byte addressing. 

There was some discussion about this when talking about conversion
issues in cheksum implementation in last october.

I would propose that all accesses to packet data would be made through
macros, so that when porting lwIP to some architecture with esoteric
limitations in addressing only certain headers would have to be changed.

Or, to go a bit further, these macros could be defined in terms of a few
elementary macros, say PACK and UNPACK (16- and 32-bit versions). All
packet header structs would be defined as collections of u8_t arrays.
When accessing elements of the headers the PACK/UNPACK macros would
handle converting between native representation and u(8|16|32)_t. This
would also remove the need for ntoh/hton functions.

So when porting only thing that needs changing is the PACK/UNPACK
macros.

With word addressing, this scheme wastes memory, but is somewhat faster
(and a lot simpler) than packing and extracting data. With byte
addressing only change is that the ntoh/hton functions are hidden behind
the macros.

How does this sound like?

Here's a small snippet from tcp.h to illustrate what I mean:

---(snip)--------------------------------------------------------------

...

PACK_STRUCT_BEGIN
struct tcp_hdr {
  PACK_STRUCT_FIELD(u8_t src[2]);
  PACK_STRUCT_FIELD(u8_t dest[2]);
  PACK_STRUCT_FIELD(u8_t seqno[4]);
  PACK_STRUCT_FIELD(u8_t ackno[4]);
  PACK_STRUCT_FIELD(u8_t offset);
  PACK_STRUCT_FIELD(u8_t flags);
  PACK_STRUCT_FIELD(u8_t wnd[2]);
  PACK_STRUCT_FIELD(u8_t chksum[2]);
  PACK_STRUCT_FIELD(u8_t urgp[2]);
} PACK_STRUCT_STRUCT;
PACK_STRUCT_END
 
#define TCPH_SRC(hdr_)    PACK16((hdr_)->src)
#define TCPH_DEST(hdr_)   PACK16((hdr_)->dest)
#define TCPH_SEQNO(hdr_)  PACK32((hdr_)->seqno)
#define TCPH_ACKNO(hdr_)  PACK32((hdr_)->ackno)
#define TCPH_OFFSET(hdr_) ((hdr_)->offset >> 4)
...
 
#define TCPH_SRC_SET(hdr_, src_)       UNPACK16((hdr_)->src, src_)
#define TCPH_DEST_SET(hdr_, dest_)     UNPACK16((hdr_)->dest, dest_)
#define TCPH_SEQNO_SET(hdr_, seqno_)   UNPACK32((hdr_)->seqno, seqno_)
#define TCPH_ACKNO_SET(hdr_, ackno_)   UNPACK32((hdr_)->ackno, ackno_)
#define TCPH_OFFSET_SET(hdr_, offset_) ((hdr_)->offset = (offset_) << 4)
...
 
---(snip)---------------------------------------------------------------

And the PACK/UNPACK macros (24-bit processor with no byte addressing)
from arch/cc.h:

---(snip)----------------------------------------------------------------

#define PACK16(ptr_) (((u16_t)(ptr_)[0] << 8) | (u16_t)(ptr_)[1])
#define UNPACK16(dst_, val_) do { \
  (dst_)[1] = (val_) & 0xff; \
  (dst_)[0] = ((val_) >> 8) & 0xff; \
} while (0)

...

(32-bit versions are similar)

-- 
As part of the conversion, computer specialists rewrote 1,500 programs;
a process that traditionally requires some debugging. 
-- USA Today, referring to the Internal Revenue Service conversion to a
new computer system.





reply via email to

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