[lwip-users] UDP socket thread safe problem

265 views
Skip to first unread message

Darius Babrauskas

unread,
Feb 23, 2012, 3:34:23 AM2/23/12
to lwip-...@nongnu.org
Hello,
 
I have old project with LWIP v.1.2. for AVR32.
 
Problem:
 
2  threads  A and B . Each treads have 1 UDP socket with No( 0 and 1 ).  Each thread have self timeout struct.
 
Each socket using lwip_select function for detect receive data and timeouts. A thread using 1 second timeout, B using 10 seconds timeout.
 
 
Problem:
A or B thread  corrupting timeouts other thread.  If working one thread (or other sleeping) then all ok.  But if both tread using lwip_select, then timeouts begun work strange. Example.: B timeout  must be 10 second, but it triger after  1 second  , then 5 times 10seconds, then  9 seconds or 14 seconds. A timeouts receiving real data, but timeout  begun sometimes 0-1ms. So, data not received, because real must about after  60ms.
 
I found like post.
 
 

"conn->callback is called from api_msg.c in the core-receive-callbacks (recv_raw/recv_udp/recv_tcp) but also from netconn_recv, when data was received by the

application. This is definitively not thread-safe. "

I understud that it old version. Porting last lwip version - possible more problems.

So.  What advise? (maybe need use in one thread raw  udp function ? )

P.S. Thread C using tcp socket with select function, but it working ok.

 

 

 

 
 
 
 
 
 

Simon Goldschmidt

unread,
Feb 23, 2012, 4:58:42 AM2/23/12
to Mailing list for lwIP users
"Darius Babrauskas" <dar...@tipas.lt> wrote:
> I have old project with LWIP v.1.2. for AVR32.

lwIP 1.2.0 is very old and *is known* to have threading problems. Please upgrade to the latest version.

Simon
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

_______________________________________________
lwip-users mailing list
lwip-...@nongnu.org
https://lists.nongnu.org/mailman/listinfo/lwip-users

Kieran Mansley

unread,
Mar 6, 2012, 2:58:16 PM3/6/12
to Mailing list for lwIP users

On 23 Feb 2012, at 08:34, Darius Babrauskas wrote:

> I have old project with LWIP v.1.2. for AVR32.
>
> Problem:
>
> 2 threads A and B . Each treads have 1 UDP socket with No( 0 and 1 ). Each thread have self timeout struct.

I think if you don't want to update to the latest version of lwIP you will probably have to serialise your use of lwIP sockets into a single thread.

Kieran

Darius Babrauskas

unread,
Mar 7, 2012, 3:50:21 AM3/7/12
to Mailing list for lwIP users
>>serialise your use of lwIP sockets into a single thread.
Yes I think about this idea. But I do not know, how 2 udp socket working in
one thread with select function. Because in my system used FreeRtos and
Lwip.
select function using FreeRtos task timeout struct. Maybe 2 udp socket can't
work with select function in one thread?

Currently I replace udp socket in one thread with RAW api functions. Now
select function in other thread working ok. But tread with RAW udp
connection work not good. Because I get first respond in call_back function
after 10s. (because send IP not in arp table and send function packet is
queueing)


LWIP options

#define SYS_LIGHTWEIGHT_PROT 1

#define ARP_QUEUEING 1

Maybe need manualy refresh arp request quene?

Simon Goldschmidt

unread,
Mar 23, 2012, 5:03:06 AM3/23/12
to Mailing list for lwIP users
"Darius Babrauskas" <dar...@tipas.lt> wrote:
> Problem are, that etharp_request very fast - and it executed before udp
> packet queued. So queued packet not send, when get arp response. It send
> later, maybe after arp timer, or when receiving other packet.

That bug doesn't exist in 1.4.0. It even doesn't really exist in 1.2.0: the actual bug is that the netif driver passes incoming packets into the ARP module from its input context instead of getting into tcpip_thread first (this was however a bug in the example ethernetif.c, so many people copied it).

> To resolve this problem, posible need use semaphores to protect arp table.

No, the complete stack (all modules including UDP, IP, ARP and others) need to be accessed from one thread at a time only. While this is fixed in 1.4.0 by letting the tcpip_thread check if input packets need to be passed to ARP (the netif driver just calls netif->input(p) for all packets), you can implement this in your 1.2.0 driver by calling tcpip_callback() to get the ARP input packets into tcpip_thread.

Upgrading to 1.4.0 (or 1.4.1 RC1) is of course the cleaner solution - 1.2.0 is really old and includes many other bugs that are fixed by now.


Simon

--
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a

Simon Goldschmidt

unread,
Mar 23, 2012, 9:12:26 AM3/23/12
to Mailing list for lwIP users
"Darius Babrauskas" <dar...@tipas.lt> wrote:
> But how raw api? It must working in tcpip_thread context?
> In LWIP 1.2v my aplication main task using udp_sendto function, which
> internaly use etharp_output, which use etharp_query which using not
> protected ARP table.
> So problem leave same. 2 task using one not protected APR table.

No, both your lwIP port and your application have to ensure you are not using lwIP from more than one contexs at the same time (where context means thread or ISR). How you do this is up to you: you can use a mutex or process RX packets and timers from the same main loop that eventually calls udp_output().

Thread-safety is not something which is provided by lwIP itself: the developer creating the port or application is free to chose which type of concurrency protection he wants to use.

Simon
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

_______________________________________________

Darius Babrauskas

unread,
Mar 23, 2012, 11:14:01 AM3/23/12
to Mailing list for lwIP users
Thank you, Simon.

Darius

----- Original Message -----
From: "Simon Goldschmidt" <gold...@gmx.de>
To: "Mailing list for lwIP users" <lwip-...@nongnu.org>

Reply all
Reply to author
Forward
0 new messages