STM32 MAC driver buffering optimization
Posted: Thu Dec 20, 2012 2:55 am
I'm implementing an application on a STM32F407V which is very demanding in network performance. I've had quite a bit of trouble debugging a packet drop problem that occured during incoming packet bursts, which is a situation my application must be able to handle to a certain extent.
One of the problems I'm facing is the limited amount of SRAM, and during the above mentioned debugging I've noticed that the incoming packet buffering in STM32 mac_lld.c wastes a lot of RAM if packets are much smaller than the largest possible ethernet frame (1522 bytes).
The STM32 low level driver just allocates a static array [N][1522] plus [N] DMA descriptors. When a new packet arrives it is stored in one of the 1522 byte chunks. A 324 byte packet in my case wastes almos 80% of the buffer SRAM. That's terrible because the more packets I'm able to buffer the better my application will work.
So, I ask to those more familiar with the STM32 MAC internals: would it be possible to fix this?.
I'm thinking somethig along the lines of this:
- Instead of a fixed set of DMA descriptors, set them up as packets are received.
- The receive buffer would be a continuous large chunk of memory (for example 16KB) used as a circular byte buffer with some restrictions. Data is written at the head and read from the tail.
- When preparing for reception of a new packet, set up the descriptor to point to a continuous chunk of 1522 bytes starting at the head. This chunk cannot wrap around, so if 1522 bytes are not available from head to end of buffer, move the head pointer to beginning of buffer. In any case, make sure there are at least 1522 bytes between the head and the tail.
- When a packet is received, see how much data was actually received and move the head pointer accordingly.
- Prepare reception of the next packet.
Actually some extra space would need to be allocated in front of each chunk to store the packet size once it's been received.
In summary, the scheme is to prepare reception of the next packet as if it was to be 1522 bytes long, then upon reception, advance the buffer head only the actual amount of data received.
I don't know because I'm not yet familiar with the STM32 MAC driver, but in case the next descriptor is automatically loaded when a packet is received (and thus it's been already loaded by the time we get to know how much data was actually received), the proposed scheme can still be implemented by using two 8KB buffers instead of a single 16KB one. At any given time the current descriptor points to one buffer and the next descriptor points to the other.
One of the problems I'm facing is the limited amount of SRAM, and during the above mentioned debugging I've noticed that the incoming packet buffering in STM32 mac_lld.c wastes a lot of RAM if packets are much smaller than the largest possible ethernet frame (1522 bytes).
The STM32 low level driver just allocates a static array [N][1522] plus [N] DMA descriptors. When a new packet arrives it is stored in one of the 1522 byte chunks. A 324 byte packet in my case wastes almos 80% of the buffer SRAM. That's terrible because the more packets I'm able to buffer the better my application will work.
So, I ask to those more familiar with the STM32 MAC internals: would it be possible to fix this?.
I'm thinking somethig along the lines of this:
- Instead of a fixed set of DMA descriptors, set them up as packets are received.
- The receive buffer would be a continuous large chunk of memory (for example 16KB) used as a circular byte buffer with some restrictions. Data is written at the head and read from the tail.
- When preparing for reception of a new packet, set up the descriptor to point to a continuous chunk of 1522 bytes starting at the head. This chunk cannot wrap around, so if 1522 bytes are not available from head to end of buffer, move the head pointer to beginning of buffer. In any case, make sure there are at least 1522 bytes between the head and the tail.
- When a packet is received, see how much data was actually received and move the head pointer accordingly.
- Prepare reception of the next packet.
Actually some extra space would need to be allocated in front of each chunk to store the packet size once it's been received.
In summary, the scheme is to prepare reception of the next packet as if it was to be 1522 bytes long, then upon reception, advance the buffer head only the actual amount of data received.
I don't know because I'm not yet familiar with the STM32 MAC driver, but in case the next descriptor is automatically loaded when a packet is received (and thus it's been already loaded by the time we get to know how much data was actually received), the proposed scheme can still be implemented by using two 8KB buffers instead of a single 16KB one. At any given time the current descriptor points to one buffer and the next descriptor points to the other.