STM32 MAC driver buffering optimization

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

STM32 MAC driver buffering optimization

Post by iggarpe »

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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 MAC driver buffering optimization

Post by Giovanni »

If I understood well your idea, this would require some kind of preparation before receiving a frame. This not how the STM32 MAC works, it expects a ready descriptor and frame buffer when a frame is incoming, there is no notification to the driver about incoming frames, only about stored frames. If a descriptor is not available when the frame is arriving then it is discarded.

Giovanni
iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: STM32 MAC driver buffering optimization

Post by iggarpe »

But at the time you are notified (by means of an interrupt) that a frame has arrived, can you determine which will be the descriptor to be used next?. Because if that is possible, then all you have to do is see how large the received frame was and set up that descriptor pointing to the next (aligned) byte.

That scheme would have a drawback: the next descriptor has to be prepared BEFORE the MAC starts using it, and I'm thinking that at 100Mbps in certain cases maybe there is not enough time. But then you could still implement it using two separate 8KB circular buffers. At the time of interrupt corresponding to arrival of packet N, you would prepare descriptor for packet N+2 pointing to the next byte.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 MAC driver buffering optimization

Post by Giovanni »

I think there would be several problems:
1) Added complexity in the ISR.
2) Allocating variable length buffers from an ISR would be a problem (fragmentation, non constant allocation time).
3) I don't know the size of the frame is going to arrive next, so how much space to allocate? if the allocation is of fixed size blocks then allocate them statically would be still better.

Giovanni
iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: STM32 MAC driver buffering optimization

Post by iggarpe »

1) Added complexity in the ISR.


Yep. But probably not as much as you think (see below). However think of the significant amount of RAM saving for applications that require very high throughput or coping with bad network scenarios.

2) Allocating variable length buffers from an ISR would be a problem (fragmentation, non constant allocation time).


Not really. That would happen if low level buffer deallocation was left to the application level (which BTW is tempting because that would mean true zero copy reception).

Currently lwipthread is the only consumer of the low level driver buffers, and does consume them in strict order, so you can use just a tail pointer for that.

Picture this: a large, say, 16KB buffer and two inbdex pointers, HEAD = TAIL = 0:

1- We make sure there are at least 4+1522 bytes free from HEAD to TAIL, accounting for (not allowing) wraparound at end of buffer (but see note below).
2- Initialize next descriptor pointing to buffer[HEAD+4] with size 1522.
3- Wait for reception. Say we receive a 324 byte frame.
4- buffer[HEAD] = 324 (store received length), then HEAD = HEAD + 4 + 324.
5- goto 1

The reader thread just needs to grab length from buffer[TAIL], get data from buffer[TAIL+4], and advance TAIL = TAIL + 4 + length.

The key here is to advance HEAD and set up next descriptor once you KNOW the size of the last received frame. Now, is this feasible or usefull at all in packet burst scenarios ?. Is it possible that the next frame starts arriving BEFORE the interrutp is triggered or has time to set up the next descriptor ?. (remember that the STM32 MAC has a 2KB FIFO).

I think this scheme can be implemented even in a scenario in which the next DMA descriptor is already in use by the time the ISR code is executed, (and thus we cannot "adjust" the buffer address of that descriptor): just use TWO circular buffers alternatively. One for even frames and the other for odd frames. In both cases, by the time you receive frame N you "adjust" the buffer address of descriptor for frame N+2, there should be time for that.

NOTE: I think you could even use 1522 bytes of circular buffer wrapping around the end of the buffer, just need to use two chained DMA descriptors. I think the MAC DMA supports it.

3) I don't know the size of the frame is going to arrive next, so how much space to allocate? if the allocation is of fixed size blocks then allocate them statically would be still better.


As I described above, you actually "allocate" 1522 bytes, quotation marks because you don't actually allocate them but just make sure they are available in the circular byte buffer.



P.S: I just realized that true zero-copy transmission is in theory possible, since the STM32 MAC supports multidescriptor frames. Just have to set up a descriptor chain that mimics the PBUF chain !!!.

After a quick read of the MAC manual, I think that modifying the descriptor list on the fly (while transmission is enabled) is not possible, but still you can set up a large enough descriptor list where each descriptor does not have an associated static 1522 byte buffer but instead it is set on demmand.

There's a big catch here: according to the system architecture diagram the MAC DMA cannot read from flash memory, so a pbuf would have to be first checked for addresses belonging to flash and if found, use the "old" method of copying first data to a static SRAM buffer.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32 MAC driver buffering optimization

Post by Giovanni »

I considered zero copy and have an idea about it but it requires for pbufs chains to be handled in the driver directly (and pointing to MAC phisical static buffers) and released by lwIP which would return the chain to the driver.

This would require changes in lwIP I think because the pbufs would be released to the driver. Basically the pbufs handling would be demanded to the driver that would implement it in a way optimized for its architecture.

I don't know if lwIP 1.4.1 can support this, 1.4.0 was way too entangled with its own pbufs and I gave up on this.

Giovanni
iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: STM32 MAC driver buffering optimization

Post by iggarpe »

I think all you need is a special type of PBUF_ROM (maybe PBUF_RAM) that is recognized and handled in pbuf_free (for example, just a callback). Also, you may want to enable LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT.

Zero copy for transmission is probably not worth the trouble. I can't think of an application in which transmit performance is not enough with the current implementation. Reception is a whole different story, because it's the network (or the remote host) imposing on you and you just have to cope with it. In my case I have to cope with one hundred 324 byte packets arriving in a single burst, and that's just not possible with the current implementation. The memory waste is enormous: first, each low level driver buffer wastes (1522 - 324) bytes, then I have to allocate 100 low level driver buffers AND 100 lwIP PBUFs, despite the fact that any one of those 100 packets can be in either side but not in both.

I just realized that the low level driver wasting due to buffer size is easy to fix: use six times as much descriptors and buffers, 256 bytes each. The performance penalty is low (the MAC DMA needs to read six instead of just one descriptor per frame in the worst case which is a 1522 byte frame), and the gains are huge: I can store three times as many packets in the low level layer with the same amount of memory. Implementation is dead easy, just need to modify the data copy function to account for the case where a frame is stored across several descriptors.

And also, for zero copy on reception, I think it's doable like this:

- Just for coherency, let's use buffers of size PBUF_POOL_BUFSIZE (590 bytes in my case).
- Use a static FIXED array of N descriptors.
- Use a static FIXED array of N buffers.
- Initially assign one buffer to each descriptor.
- When a frame is received, build a special PBUF_ROM chain with the used buffers and pass it along to the tcpip thread. The descriptors remain "disabled".
- Via a pbuf_free hook, when one of those special PBUFs is freed take the buffers and assign them to "disabled" descriptors, then enable them.

Advantages:

1- Uses a fixed circularly linked list of descriptors. I'm not sure if the MAC DMA would support a mutating list, for one thing the documentation says that the list head pointer register can only be changed while reception is disabled.

2- Zero copy.

3- Unifies low level driver and lwIP reception buffering.

The later is of uttermost importance to me. As I explained at the beginning, I would need then only a single 100 x PBUF_POOL_BUFSIZE pool.

I think I'll give it a go.
Post Reply