PBUFs are used to hold data which is received or transmitted as IP packets. Rather than copy data repeatedly, the system passes pointers to these structures. In order to avoid heavy heap use, these buffers are preallocated.
Collections of buffers are handled by the QUEUE data structures.
The function of PBUFs is to hold data that will be received or transmitted to a device. Headers are added to application data. The worst case occurs for Ethernet systems:
Ethernet header
IP header
TCP, UDP or other protocol header(s)
Application data
Traditionally, TCP/IP stacks based on the BSD implementations use separate buffers for each data area. In order to avoid additional heap allocations and data copying, PowerNet uses a single buffer and moves pointers as the PBUF is moved up and down the TCP/IP stack.
struct pbuf_hdr \ -- size ; size of pbuf_hdr structure
PBUF buffer header definition. The first four fields
(10 bytes) are common with the obsolete BF_HDR definition.
PSIZE pbuf_hdr + equ pbuf \ -- n
Size of complete pbuf structure.
$3FFF equ BUFFSIZEMASK \ -- mask
The buffer size is held in the low 14 bits of the 16 bit
type field. This permits a maximum data size of BUFFSIZEMASK
bytes.
$C000 equ BUFFTYPEMASK \ -- mask
The buffer type is held in the top 2 bits of the 16 bit
type field.
$0000 equ DATABUFFER \ -- n
Set into the type bits for I/O buffers.
$4000 equ PBUFFER \ -- n
Set into the type bits for PBUFs.
: !pb_size \ n *pb -- ; set buffer size
Set the size of a buffer and mark it as a PBUF.
: PBHdrStart \ *pbuf -- addr ; of packet header
Return the address of the current header.
: PBHdrLen \ *pbuf -- len ; of packet header
Return the length of the current header.
: PBDataStart \ *pbuf -- addr ; of data
Return the address of the current data.
: PBDataLen \ *pbuf -- len ; of data
Return the length of the current data.
: !PBDataLen \ len *pbuf -- ; of data
Set the length of the current data.
: !*PBData \ offset addr --
Set offset to buffer data.
: bumpPBuffData \ n *pbuf --
Moves the data pointer/count along the buffer,
incrementing the address, decrementing the count.
: unbumpPBuffData \ n *pbuf --
Moves the data pointer/count back up the buffer,
decrementing the address, incrementing the count.
: PBinit \ *pb --
Initialise a PBUF.
: AllocPbuf \ -- *pb|0
ALLOCATE
and initialise a PBUF from the heap and return
the PBUF address. If the buffer cannot be allocated, 0 is
returned without waiting.
0 value TxPbuf \ -- addr
Points to the dedicated transmit PBUF.
0 value NextTxPbuf \ -- addr
Holds TxPbuf
when the transmit PBUF is available, or
zero when not available.
: InitIOQueues \ --
Initialise the predefined queues and their I/O buffers
and PBUFs.
: GetPbuf \ -- *pb|0
Get a PBUF from the free PBUF queue and return
the PBUF address. If there are no free buffers, 0 is
returned without waiting.
: GetTxPbuf \ -- *pb|0
Get a PBUF from the free PBUF queue and return the PBUF address.
If there are no free buffers, try the dedicated transmit PBUF.
0 equ QBdiags? \ -- n
When this equate is set non-zero, additional test code is
compiled and included in FREEQB
below.
variable qbug \ -- addr
When set to non-zero, FREEQB
will display diagnostics if
the buffer is bad.
PBUFFER PSIZE or equ PBcheck \ -- x
Check value for a PBUF.
: FreeQB \ *pb/bf --
Place a PBUF or I/O buffer on the relevant free queue.
N.B. FreeQB
cleans up the buffers before requeueing
them.
: FreeQB \ *pb/bf --
Place a PBUF or I/O buffer on the relevant free queue.
N.B. FreeQB
cleans up the buffers before requeueing
them.