On 6.4.11 11:38 , Riccardo Manfrin wrote:
> I'm writing the reception mechanism for an embedded device equipped
> with a "generic" radio interface.
> My question regards the implementation of the reception passing
> through the various layers of the stack. Assume that different users
> (e.g. UDP clients) can request a reception, by submitting a buffer,
> the buffer length and a rx callback. The application should allowed to
> allocate the buffers statically or dynamically depending on its needs.
> When a packet is received at the lower layer, the layer doesn't know
> who such packet belongs to (because it hasn't parsed it yet).
> Therefore the first operation is to copy the packet from the radio
> buffer to the first available buffer in the list of buffers submitted
> for reception (precisely: the one with the smallest length allowing
> the full storage of the packet).
> While climbing up the stack layers with packet parsing, a layer finds
> out that the packet belonged to a different user than the one whose
> buffer had been used for temporarily holding the packet (e.g. UDP port
> mismatching).
> In such case the layer could swap the buffers of the two users
> involved in the mismatching OR memcopy the data from the wrong buffer
> to the right one.
> While swapping is a computational efficient choice it requires buffers
> to have the same properties in terms of
> * length,
> * type of allocation (static/dynamic).
> Indeed when releasing the buffers to the users, those should be
> allowed to assume to be using the same buffer they had submitted for
> reception (rather reasonable). If this assumption doesn't hold because
> of swapping, the only way to cope with it is to have the above
> described characteristics for all buffers (same length and allocation
> type).
>
> My question is if linux has a smart way to do these kind of jobs (or
> eventually if you know any).
>
This is the classical networking buffer problem.
The common solutions are:
1. There is a separate buffer for the receiver, and its contents are
copied to the client buffer when the proper client is found. The
receive buffer is then recycled to the receiver.
2. There is a generic buffer pool. The receiver picks a buffer from
the pool and passes it to the proper user when the user is decoded
from the message. It is the the responsibility of the user to
return the buffer to the common pool.
Both methods have memory space problems if the message lengths
vary wildly, so that a single length for the buffer won't do.
An often used solution is to use pretty small buffers with
chaining pointers, so that a longer message is split between
shorter buffers which are chained together.
--
Tauno Voipio
tauno voipio (at) iki fi
|