UDP packet dropping
Moderators: RoccoMarco, lbednarz, tfAteba
UDP packet dropping
First, I'm using a custom STM32F407V board with a LAN8720A ethernet PHY transceiver.
Everything seems to work fine in a "normal" scenario in which I'm sending one 324 byte packet to the board every 20ms. All of them are handled and none is lost.
However, if I send 20 UDP packets with no delay between them some are lost, starting at the 7th or 8th. I do this intentionally to reproduce the error, which I have occasionally encountered when due to bad network conditions the packets pile up somewhere and are received in a burst when the link is restored.
Now, I know that the STM32F407V has limited memory and processing power and that under certain extreme conditions packets are going to be dropped, however, I wasn't expecting to face this problem with just a 20 x 324 byte packet burst. First to blame is usually the MAC driver or LWIP configuration: do they have enough buffering?. I think so, I tried the following and it made no change (always the 7th or 8th packet is the first to be dropped):
Increase MAC driver number of receive buffers:
#define STM32_MAC_RECEIVE_BUFFERS 32
Increase lwIP buffering
Here I tried increasing MEMP_NUM_PBUF, MEMP_NUM_UDP_PCB, and PBUF_POOL_SIZE to absurdly high values.
So I'm not sure here what's the limit that is being reached: I'm about to delve in the internals of the STM32 MAC driver, but I think that even if lwIP and my code were not able to cope with t he incoming packet burst, a large number of MAC driver buffers should be enough to store them until they can be processed. Shouldn't it?.
Any suggestions highly welcome.
Everything seems to work fine in a "normal" scenario in which I'm sending one 324 byte packet to the board every 20ms. All of them are handled and none is lost.
However, if I send 20 UDP packets with no delay between them some are lost, starting at the 7th or 8th. I do this intentionally to reproduce the error, which I have occasionally encountered when due to bad network conditions the packets pile up somewhere and are received in a burst when the link is restored.
Now, I know that the STM32F407V has limited memory and processing power and that under certain extreme conditions packets are going to be dropped, however, I wasn't expecting to face this problem with just a 20 x 324 byte packet burst. First to blame is usually the MAC driver or LWIP configuration: do they have enough buffering?. I think so, I tried the following and it made no change (always the 7th or 8th packet is the first to be dropped):
Increase MAC driver number of receive buffers:
#define STM32_MAC_RECEIVE_BUFFERS 32
Increase lwIP buffering
Here I tried increasing MEMP_NUM_PBUF, MEMP_NUM_UDP_PCB, and PBUF_POOL_SIZE to absurdly high values.
So I'm not sure here what's the limit that is being reached: I'm about to delve in the internals of the STM32 MAC driver, but I think that even if lwIP and my code were not able to cope with t he incoming packet burst, a large number of MAC driver buffers should be enough to store them until they can be processed. Shouldn't it?.
Any suggestions highly welcome.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: UDP packet dropping
Being UDP packets the MAC should be able to buffer them regardless of what lwIP does. It is strange because the buffering is handled in HW by the MAC DMA.
I would verify if ST's own Ethernet driver has the same behavior, if not then it must be a problem in our MAC driver.
How can I test this? is there a program I can use to send those UDP packets?
Giovanni
I would verify if ST's own Ethernet driver has the same behavior, if not then it must be a problem in our MAC driver.
How can I test this? is there a program I can use to send those UDP packets?
Giovanni
Re: UDP packet dropping
This is a simple linux program that will allow you to send a burst of UDP packets:
https://dl.dropbox.com/u/35035041/flooder.zip
For example: I initialize lwIP, with STM32 MAC configured with 20 receive buffers, send a 12 packet burst with "./main -f 12" and usually packets 9th-10th are dropped.
I'm right now looking into the problem and I too agree that it must be a problem in the low level driver. That or my hardware is somewhat fouled up (unlikely, but I've learned not to discard causes too soon). I'm doing as described above and immediately stop the CPU and look in the MAC low level driver receive buffers, but I'm seeing mangled data (not only for the dropped packets, which seem to be there, but for all of them). Doesn't make sense unless the low level driver has gone nuts and is overwriting data or something. That or lwIP is mangling the data after the hardware buffer is read, which I find unlikely (but then again I don't know how lwIP internals exactly work).
I'm trying to rule out lwIP itself, so I have tried to not use it at all and initialize the MAC driver myself. I've tried the following without success:
macStart(ÐD1);
while (TRUE) {
macPollLinkStatus(ÐD1);
chThdSleepMilliseconds(ÐD1);
}
Any ideas on how to use the MAC driver properly without lwIP ?.
Note that disabling lwIP also disables the ARP mechanism, so you need to bypass it at the host PC to use my UDP flooding program:
sudo arp -s 192.168.0.20 11:22:33:44:55:66
(obviously you must use your system MAC adress instead of 11:22:33...)
https://dl.dropbox.com/u/35035041/flooder.zip
For example: I initialize lwIP, with STM32 MAC configured with 20 receive buffers, send a 12 packet burst with "./main -f 12" and usually packets 9th-10th are dropped.
I'm right now looking into the problem and I too agree that it must be a problem in the low level driver. That or my hardware is somewhat fouled up (unlikely, but I've learned not to discard causes too soon). I'm doing as described above and immediately stop the CPU and look in the MAC low level driver receive buffers, but I'm seeing mangled data (not only for the dropped packets, which seem to be there, but for all of them). Doesn't make sense unless the low level driver has gone nuts and is overwriting data or something. That or lwIP is mangling the data after the hardware buffer is read, which I find unlikely (but then again I don't know how lwIP internals exactly work).
I'm trying to rule out lwIP itself, so I have tried to not use it at all and initialize the MAC driver myself. I've tried the following without success:
macStart(ÐD1);
while (TRUE) {
macPollLinkStatus(ÐD1);
chThdSleepMilliseconds(ÐD1);
}
Any ideas on how to use the MAC driver properly without lwIP ?.
Note that disabling lwIP also disables the ARP mechanism, so you need to bypass it at the host PC to use my UDP flooding program:
sudo arp -s 192.168.0.20 11:22:33:44:55:66
(obviously you must use your system MAC adress instead of 11:22:33...)
Re: UDP packet dropping
Slightly off-topic, but related to the MAC driver:
STM32F4xx (maybe also other STM32?) has a grand total of 192KB SRAM divided as follows:
a) 64KB CCM
b) 112KB + 16KB
(b) is memory accessible only by the CPU (i.e. no DMA).
(b) is divided in two areas separetely connected to the bus matrix, which means the last 16KB can be directly accessed by the Ethernet and USB peripherals without interering any other operations to the other 112KB.
So, placing the MAC low level driver buffers in that special 16KB SRAM area results in a performance improvement.
Hence I would like to request a feature: modify the MAC low level driver so that the user can somehow specify the location of the buffers, most likely just passing a pointer, since any compiler specific extension or linker manipulation would be very unportable.
STM32F4xx (maybe also other STM32?) has a grand total of 192KB SRAM divided as follows:
a) 64KB CCM
b) 112KB + 16KB
(b) is memory accessible only by the CPU (i.e. no DMA).
(b) is divided in two areas separetely connected to the bus matrix, which means the last 16KB can be directly accessed by the Ethernet and USB peripherals without interering any other operations to the other 112KB.
So, placing the MAC low level driver buffers in that special 16KB SRAM area results in a performance improvement.
Hence I would like to request a feature: modify the MAC low level driver so that the user can somehow specify the location of the buffers, most likely just passing a pointer, since any compiler specific extension or linker manipulation would be very unportable.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: UDP packet dropping
Hi,
There is another user reporting a problem with the Ethernet driver, I wonder if it is related to the problem you are experiencing:
viewtopic.php?f=2&t=845
About memory allocation, I will look into a solution, probably using a buffer declaration macro that can be overridden externally (adding an __attribute__ or a #pragma, not sure if a #pragma works if defined inside a macro however).
Giovanni
There is another user reporting a problem with the Ethernet driver, I wonder if it is related to the problem you are experiencing:
viewtopic.php?f=2&t=845
About memory allocation, I will look into a solution, probably using a buffer declaration macro that can be overridden externally (adding an __attribute__ or a #pragma, not sure if a #pragma works if defined inside a macro however).
Giovanni
Re: UDP packet dropping
Mmm... could be related, though the 5s periodicity of his problem makes me think it has something to do with the link status polling, which is exactly 5s.
BTW, I entered a feature request to make this configurable. It's dead easy. In my case the first link status check always returns down, though it becomes up very shortly after. The large 5s polling interval means in practice that I have always to wait for 5s before a second check is performed.
I've managed to make the MAC work alone without any lwIP code. Just call macStart and then (apparently necessary) macPollLinkStatus periodically, and results are a bit disturbing (remember, I'm using 20 receive buffers, and I'm sending a burst of 12 UDP packets, also, I'm manually populating the ARP cache in my linux box to get it to work without lwIP):
- I get 14 receive interrupts, though only 12 packets are sent from my linux box (in which I'm capturing net traffic with wireshark just to be sure).
- All 12 sent packets are received, I can see them in the low level driver buffers.
- All 20 low level driver buffers contain data.
The latest doesn't make sense. I'm still to analyze and manually decode the data in the buffers that do not contain the UDP packets I sent from my linux box, but even if it was some sort of other broadcast traffic in my network, having counted only 14 interrupts and seeing data in all 20 buffers is really fishy.
BTW, I entered a feature request to make this configurable. It's dead easy. In my case the first link status check always returns down, though it becomes up very shortly after. The large 5s polling interval means in practice that I have always to wait for 5s before a second check is performed.
I've managed to make the MAC work alone without any lwIP code. Just call macStart and then (apparently necessary) macPollLinkStatus periodically, and results are a bit disturbing (remember, I'm using 20 receive buffers, and I'm sending a burst of 12 UDP packets, also, I'm manually populating the ARP cache in my linux box to get it to work without lwIP):
- I get 14 receive interrupts, though only 12 packets are sent from my linux box (in which I'm capturing net traffic with wireshark just to be sure).
- All 12 sent packets are received, I can see them in the low level driver buffers.
- All 20 low level driver buffers contain data.
The latest doesn't make sense. I'm still to analyze and manually decode the data in the buffers that do not contain the UDP packets I sent from my linux box, but even if it was some sort of other broadcast traffic in my network, having counted only 14 interrupts and seeing data in all 20 buffers is really fishy.
Re: UDP packet dropping
Progress report:
The MAC peripheral continues to work while the CPU is stopped, interrupts are NOT triggered but DMA transfers do happen. This means that the mangled data I was seeing in some low level driver buffers were the result of broadcast packets in my network overwriting my test UDP packets. Similarly the incoherent interrupt count (more buffers used than interrupts counted) was caused by this. To make things worse, the MAC peripheral continues working but the EWARM IDE won't update the watch variables. Now fortunately I figured out all that, and with that in mind, it seems ALL packets I send are correctly received and stored by the low level driver.
I added also a counter of processed received packets in lwipthread, just in case for some reason the notification mechanism was malfunctioning and lwipthread was just skipping packets properly received by the MAC. This is not the case. I send 14 UDP packets and I see exactly the same amount of low level interrupts and lwipthread receive iterations. Check.
So apparently lwIP itself is discarding my UDP packets. That's where I'm looking next.
The MAC peripheral continues to work while the CPU is stopped, interrupts are NOT triggered but DMA transfers do happen. This means that the mangled data I was seeing in some low level driver buffers were the result of broadcast packets in my network overwriting my test UDP packets. Similarly the incoherent interrupt count (more buffers used than interrupts counted) was caused by this. To make things worse, the MAC peripheral continues working but the EWARM IDE won't update the watch variables. Now fortunately I figured out all that, and with that in mind, it seems ALL packets I send are correctly received and stored by the low level driver.
I added also a counter of processed received packets in lwipthread, just in case for some reason the notification mechanism was malfunctioning and lwipthread was just skipping packets properly received by the MAC. This is not the case. I send 14 UDP packets and I see exactly the same amount of low level interrupts and lwipthread receive iterations. Check.
So apparently lwIP itself is discarding my UDP packets. That's where I'm looking next.
Re: UDP packet dropping
FIXED:
This is the culprit (in lwipopts.h):
#define TCPIP_MBOX_SIZE 4
Combined with the fact that my lwipthread priority is higher than the internal lwIP tcpip thread. As a result, when a burst comes and packets get stored by the MAC low level driver and lwipthread enqueues them into the tcpip thread mailbox, but tcpip thread doesn't have a chance to process them before the mailbox overflows.
Solutions:
a) Increase TCPIP_MBOX_SIZE
b) Set lwipthread priority lower than any of the internal lwIP threads.
I'm going with (b), because lwipthread doesn't do much more than taking incoming packets from the low level driver and feeding them to the lwip internal threads. If, say, tcpip thread is not able to keep up processing packets it is not going to matter anyway if it is not allowing lwipthread to execute, and the low level driver is then responsible for storing the incoming packets until they can be handled.
This is the culprit (in lwipopts.h):
#define TCPIP_MBOX_SIZE 4
Combined with the fact that my lwipthread priority is higher than the internal lwIP tcpip thread. As a result, when a burst comes and packets get stored by the MAC low level driver and lwipthread enqueues them into the tcpip thread mailbox, but tcpip thread doesn't have a chance to process them before the mailbox overflows.
Solutions:
a) Increase TCPIP_MBOX_SIZE
b) Set lwipthread priority lower than any of the internal lwIP threads.
I'm going with (b), because lwipthread doesn't do much more than taking incoming packets from the low level driver and feeding them to the lwip internal threads. If, say, tcpip thread is not able to keep up processing packets it is not going to matter anyway if it is not allowing lwipthread to execute, and the low level driver is then responsible for storing the incoming packets until they can be handled.
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: UDP packet dropping
Good findings, thanks.
I am going to integrate lwIP 1.4.1 over the Christmas vacations and this is very useful, it would be nice to add a better http server too, the current one is too basic.
Giovanni
I am going to integrate lwIP 1.4.1 over the Christmas vacations and this is very useful, it would be nice to add a better http server too, the current one is too basic.
Giovanni
Re: UDP packet dropping
I think it is worth mentioning this somewhere in the documentation, surely it will save someone sometime a good few hours of head baging against the wall.
In the end it all boils down to this:
1- In general, it is advisable to assign lwipthread a lower priority than the internal lwIP threads.
2- Make TCPIP_MBOX_SIZE large, it's cheap in terms of memory (only sizeof(msg_t) bytes per entry). The following will put you in the safe side always:
#define TCPIP_MBOX_SIZE MEMP_NUM_PBUF
Because low level data is placed in a PBUF chain in order to pass it up to the tcpip thread, so you'll run out of PBUFs before you can fill the mailbox.
In the end it all boils down to this:
1- In general, it is advisable to assign lwipthread a lower priority than the internal lwIP threads.
2- Make TCPIP_MBOX_SIZE large, it's cheap in terms of memory (only sizeof(msg_t) bytes per entry). The following will put you in the safe side always:
#define TCPIP_MBOX_SIZE MEMP_NUM_PBUF
Because low level data is placed in a PBUF chain in order to pass it up to the tcpip thread, so you'll run out of PBUFs before you can fill the mailbox.