Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to handle large buffers in a layered protocol stack?

I am working on a simple protocol stack for a small embedded system (multidrop, rs485 type stuff). In this stack, losely models after OSI layers:

  1. Application
  2. Network
  3. Datalink
  4. physical (serial driver)

Each layer has its own header / footer portion that wraps the payload of the layer above it.

I will be using my own buffer pool of statically allocated fixed sized blocks to store the binary packets. (No malloc / free in this app.)

In other API's I have seen that the data is usually passed as a const pointer with an associated length. In this way the data would need a copy operation at each layer as the payload of the layer above is placed in a newly allocated buffer for the current layer.

For a three layer stack this would be 2 copy operations and 3 allocated buffers.

Is there a better way to do this and still maintain clean separation of the protocol layers?

To better anchor the discussion, lets say the packets are typically around 2k and the processor is a small 8 bit micro running at 8Mhz.

like image 755
JeffV Avatar asked Aug 31 '09 00:08

JeffV


1 Answers

You could avoid the copies by having each layer request an empty buffer from the next lower layer, rather than allocating one itself:

  • Application Layer requests buffer length LA from Network Layer.
  • Network Layer requests buffer length LA+LN from Datalink Layer.
  • Datalink Layer requests buffer length LA+LN+LD from Physical Layer.
  • Physical Layer pulls a buffer from the buffer pool.
  • Physical Layer returns buffer + phdr_len to Datalink Layer.
  • Datalink Layer returns buffer + phdr_len + dhdr_len to Network Layer.
  • Network Layer returns buffer + phdr_len + dhdr_len + nhdr_len to Application Layer.
  • Application Layer fills out data in provided buffer, and calls Network Layer to transmit.
  • Network Layer prepends header and calls Datalink Layer to transmit.
  • Datalink Layer prepends header and calls Physical Layer to transmit.
  • Physical Layer prepends header and passes to hardware.
like image 72
caf Avatar answered Sep 21 '22 22:09

caf