[lwip-users] FreeRTOS / lwip multiple connections

2,734 views
Skip to first unread message

Christoph Kronen

unread,
Aug 28, 2011, 12:48:26 PM8/28/11
to lwip-...@nongnu.org
Hi all,

I am using FreeRTOS 7.0.1 on a Sam7X512 and upgraded the contributed port lwIP 1.3.0 to 1.3.2.

The "lwIP Embedded Webserver Demo" is what I took as a starting point. I removed the webserver functionality, now I have a task that

  • checks for incoming data (nonblocking)
  • checks a Queue for outgoing data (nonblocking)
  • wait 1 ms (to prevent 100% cpu usage)
  • repeat :-)

This works pretty well, I get around 1400 kbyte/s RX and TX.
Now to my question: Is it possible/difficult/easy to transform this functionality to accept multiple connections on the same port ? At the moment I am using the netconn API, so I guess I have to switch to the Socket API ?

I read through Richards thread on his "FreeRTOS/lwip Win32 simulator" project and it sounds as if it is quite difficult to have multiple connections in a multithreaded environment. Unfortunately I could not find other helpful topics or contributed examples.

I am rather new to FreeRTOS and lwIP, so please excuse me if I am missing something obvious :-)

Please give me some comments / suggestions / tips / pushes into the right direction.

Thank you very much !

Christoph

FreeRTOS Info

unread,
Aug 28, 2011, 2:43:15 PM8/28/11
to Mailing list for lwIP users
Is this the project you are referring to?

http://interactive.freertos.org/entries/20290712-freertos-win32-project-with-lwip-web-server

If so, look at the command interpreter rather than the web server. It
uses sockets, and although it is structure to only process one
connection at a time, I think converting it to allow multiple
connections should be simple (?).

> * checks for incoming data (nonblocking)
> * checks a Queue for outgoing data (nonblocking)
> * wait 1 ms (to prevent 100% cpu usage)
> * repeat :-)

This does not sound like a good solution, unless you are running at the
lowest (idle) task priority. At any other priority, you are still going
to be using most of the CPU time even when there is no processing to
perform. A better solution would be to block the TCP/IP processing
until a TCP/IP related event occurs - a semaphore or queue can be used
to signal the event, so the task blocks on the queue. An event can be
external, like a packet arriving, or internal, like a buffer becoming
free, or an ARP timer event, or data to be sent, etc.

Regards,
Richard.

+ http://www.FreeRTOS.org
Designed for Microcontrollers.
More than 7000 downloads per month.

On 28/08/2011 17:48, Christoph Kronen wrote:
> Hi all,
>
> I am using FreeRTOS 7.0.1 on a Sam7X512 and upgraded the contributed
> port lwIP 1.3.0 to 1.3.2.
>
> The "lwIP Embedded Webserver Demo" is what I took as a starting point. I
> removed the webserver functionality, now I have a task that
>

> * checks for incoming data (nonblocking)
> * checks a Queue for outgoing data (nonblocking)
> * wait 1 ms (to prevent 100% cpu usage)
> * repeat :-)


>
>
> This works pretty well, I get around 1400 kbyte/s RX and TX.
> Now to my question: Is it possible/difficult/easy to transform this
> functionality to accept multiple connections on the same port ? At the
> moment I am using the netconn API, so I guess I have to switch to the
> Socket API ?
>
> I read through Richards thread on his "FreeRTOS/lwip Win32 simulator"
> project and it sounds as if it is quite difficult to have multiple
> connections in a multithreaded environment. Unfortunately I could not
> find other helpful topics or contributed examples.
>
> I am rather new to FreeRTOS and lwIP, so please excuse me if I am
> missing something obvious :-)
>
> Please give me some comments / suggestions / tips / pushes into the
> right direction.
>
> Thank you very much !
>
> Christoph
>
>

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

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

vincent cui

unread,
Aug 29, 2011, 11:06:52 PM8/29/11
to Mailing list for lwIP users
Hi :

I programmed a web server with socket api to do multi-connnection from more client. It seems there is a problem
After it works some days, the system will halt.
Does anyone meet this ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

Yugal Kishore Gupta

unread,
Aug 29, 2011, 11:19:22 PM8/29/11
to Mailing list for lwIP users
I have heard about this problem where critical section helped. I don't
remember the exact problem and solution.

Thanks & Regards,
Yugal Kishore Gupta

CYBER G. INDIA PVT. LTD, (A Unit of CYBER GROUP, INC)
Logix Technopark
Tower B, 5th Floor
Sector 127, NOIDA, U.P., INDIA 201301
OT 91-120-4643106/ Cell: 91-9818566224
ygu...@cygrp.com / www.cygrp.com

vincent cui

unread,
Aug 29, 2011, 11:28:07 PM8/29/11
to Mailing list for lwIP users

Hi Chistoph:

 

 

The "lwIP Embedded Webserver Demo" is what I took as a starting point. I removed the webserver functionality, now I have a task that

  • checks for incoming data (nonblocking)
  • checks a Queue for outgoing data (nonblocking)
  • wait 1 ms (to prevent 100% cpu usage)
  • repeat :-)

 

the LwIP webserver demo code is following, I don’t know how do  you infer it is noblocking for incoming data and outgoing ….

Where to wait 1 ms .

 

void  WebServer_Handler(void *pdata)

{

         struct netconn  *__pstConn, *__pstNewConn;

                                                       

    __pstConn = netconn_new(NETCONN_TCP);

    netconn_bind(__pstConn, NULL,80);

    netconn_listen(__pstConn);

 

         for(;;)

         {

                   __pstNewConn = netconn_accept(__pstConn);

                  

                   if(__pstNewConn != NULL)

                   {                          

                            vHandler_HTTP(__pstNewConn);

                            while(netconn_delete(__pstNewConn) != ERR_OK)

                            {

                                     vTaskDelay(10);

                            }

                   }

    }

}

 

 

 

Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

 

Christoph Kronen

unread,
Aug 30, 2011, 3:31:00 AM8/30/11
to Mailing list for lwIP users
Hi Vincent,

I changed the receiving process to be nonblocking, it wasn't like this in the first place. The code below has the same entry point as your "vHandler_HTTP(__pstNewConn);", it gets called when the connection is already established.

My problem was the following: I needed to check for new data received via Ethernet as well as new data from a Sendqueue in the same task. So netconn_recv() could not be blocking. In another topic, Simon Goldschmitt suggested to use lwIP 1.3.1 and to set "LWIP_SO_RCVTIMEO" to make netconn_recv() nonblocking. Unfortunately this crashed my Sam7X all the time.

So what I do now, is to peek into the receive mailbox and only if there is data I start the netconn_recv() process. Probably horrible programming, but it works for now.

Richard suggested to use semaphores on TCP events, that sounds much cleaner and I will definitely try to do it that way. But then I would need a FreeRTOS function like "WaitForMultipleObjects", since the Task needs to wake up for either a TCP event semaphore OR a new element in my SendQueue.

The CommandInterpreter from Richards socket example (thanks !) works for my Sam7X and I am in the process of rewriting it to have the same functionality as the code below.

@Vincent: You said your webserver is able to have multiple connections, could you give me a tip on that ? Do you have a small code example for "multiconnection socket" part ?

Thank you very much,

Christoph

static void vProcessSpeedtest( struct netconn *pxNetCon )
{
struct netbuf *pxRxBuffer;
struct pbuf *dummybuffer;
unsigned char *pcRxString;
xTCP_TXBuff * TXPointer = 0;
unsigned short usLength;
    while (1)
    {
        // Peek into the Receive mailbox
        if ( pdTRUE == xQueuePeek((pxNetCon->recvmbox),&dummybuffer,0))
        {
            pxRxBuffer = netconn_recv( pxNetCon );
            if( pxRxBuffer != NULL )
            {
                do
                {
                    netbuf_data( pxRxBuffer, ( void * ) &pcRxString, &usLength );
                  
                    //use the incoming data at this point

                }   while ( netbuf_next(pxRxBuffer) >=0 );
                netbuf_delete( pxRxBuffer );
            }
            else break;
        }
        // Check the Transmit Queue for Data and send it
        if ( xQueueReceive( xTXPointerQueue, &TXPointer, 0 ))
        {
            netconn_write(pxNetCon, TXPointer->data, TXPointer->length, NETCONN_NOFLAG);
            vFreeTCPBuffer(TXPointer);
        }
        vTaskDelay( 1 / portTICK_RATE_MS );
    }
    netconn_close( pxNetCon );
}


-------- Original-Nachricht --------
Datum: Tue, 30 Aug 2011 03:28:07 +0000
Von: vincent cui <vince...@enlogic.com>
An: Mailing list for lwIP users <lwip-...@nongnu.org>
Betreff: Re: [lwip-users] FreeRTOS / lwip multiple connections

vincent cui

unread,
Aug 30, 2011, 3:39:04 AM8/30/11
to Mailing list for lwIP users

Hi Christoph:

 

Indeed, I write the web server  with socket api in lwIP. It responses http request with a dynamic web page ( fetch interval 1 second).

After it run, I open 10 IE to access the web serve (I make sure 10 clients could access the web server at same time )r, and keep that in pressure testing…

 it had works in 3 days now , but I don’t think it is enough.

vincent cui

unread,
Aug 30, 2011, 6:04:12 AM8/30/11
to Mailing list for lwIP users
Dear:

Who know how to configure TCP if there is 10 client access web server base on lwip at same time.
I test it by open 10 IE, my web server halt after some hour later .
It I open 1 IE, the web server works well.

Does anyone help to solve the trouble ?

vincent cui

unread,
Aug 30, 2011, 10:04:58 AM8/30/11
to Mailing list for lwIP users

Christoph:

 

Deal with mult-connection correctly, there is 3 way.

1.       Using raw api

2.       Using netconn API, but you have to create a  task to handle the HTTP request, then back to main thread to start accept next request.

3.       Using socket, you must set it with non-blocking and select it

 

I use the 3th method. The following code must be add to listen socket.

 

Int arg = 0x1;

lwip_ioctl(listhensocket, FIONBIO, &arg);

 

but I found that the listen socket cann’t work once it is set with nonblocking…

now, I remove it, it works … hope someone explain why

other, you also need call lwIP_shutdown and lowip_close to terminate socket in order to be avoid to CLOSE_WAIT .

Kieran Mansley

unread,
Aug 30, 2011, 11:18:37 AM8/30/11
to Mailing list for lwIP users
On Tue, 2011-08-30 at 14:04 +0000, vincent cui wrote:
> Deal with mult-connection correctly, there is 3 way.

There are lots of ways!

> 3. Using socket, you must set it with non-blocking and select it
>
> I use the 3th method. The following code must be add to listen socket.
>
>
> Int arg = 0x1;
>
> lwip_ioctl(listhensocket, FIONBIO, &arg);

I'm not sure why you need your listening socket to be non-blocking.

> but I found that the listen socket cann’t work once it is set with
> nonblocking…
> now, I remove it, it works … hope someone explain why

I'm not sure if lwIP supports non-blocking listening sockets. I'd have
to check the source to be sure and I'm short on time.

> other, you also need call lwIP_shutdown and lowip_close to terminate
> socket in order to be avoid to CLOSE_WAIT .

No, either should be fine if you use them correctly. There are some
small differences between lwIP's shutdown and close and standard
shutdown and close, but we're working towards getting them more
standardised where possible.

Kieran

vincent cui

unread,
Aug 30, 2011, 8:18:57 PM8/30/11
to Mailing list for lwIP users
K:
You mean I need make accept socket to be non-blocking , right ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com
-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Kieran Mansley
Sent: 2011年8月30日 23:19
To: Mailing list for lwIP users
Subject: Re: [lwip-users] FreeRTOS / lwip multiple connections

vincent cui

unread,
Aug 31, 2011, 11:16:27 PM8/31/11
to Mailing list for lwIP users

H chiston:

 

I redesign my webserver to support mult-connection.  I create a new task to deal with HTTP request, it will delete it by itself.

The main task will go to accept new connection.

Kieran Mansley

unread,
Sep 1, 2011, 3:22:21 PM9/1/11
to Mailing list for lwIP users

On 31 Aug 2011, at 01:18, vincent cui wrote:

> K:
> You mean I need make accept socket to be non-blocking , right ?

No, I'm not sure why you need any of your sockets to be non-blocking.

vincent cui

unread,
Sep 1, 2011, 8:12:29 PM9/1/11
to Mailing list for lwIP users
HI :

I want to create a semaphore (value is 10) to deal with max connection . but freertos only provide mutex and binary semaphore...
Is there other way to come it true ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Kieran Mansley
Sent: 2011年9月2日 3:22
To: Mailing list for lwIP users
Subject: Re: [lwip-users] FreeRTOS / lwip multiple connections


vincent cui

unread,
Sep 1, 2011, 10:39:52 PM9/1/11
to Mailing list for lwIP users
Hi:

Anyone ever use LWIP_KEEP_ALIVE function in lwip1.3.2 ? I enable it , but it doesn't work, is there bug ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Kieran Mansley
Sent: 2011年9月2日 3:22
To: Mailing list for lwIP users
Subject: Re: [lwip-users] FreeRTOS / lwip multiple connections


vincent cui

unread,
Sep 1, 2011, 11:06:02 PM9/1/11
to Mailing list for lwIP users
HI:

#if LWIP_TCP_KEEPALIVE
lwip_setsockopt(clientfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&mytcp_keepalive, sizeof(mytcp_keepalive));
lwip_setsockopt(clientfd, IPPROTO_TCP, TCP_KEEPIDLE, (void *)&mytcp_keepIdle, sizeof(mytcp_keepIdle));
lwip_setsockopt(clientfd, IPPROTO_TCP, TCP_KEEPINTVL, (void *)&mytcp_keepIntvl, sizeof(mytcp_keepIntvl));
lwip_setsockopt(clientfd, IPPROTO_TCP, TCP_KEEPCNT, (void *)&mytcp_keepCount, sizeof(mytcp_keepCount));
#endif

Above code enable socket keepalive function ... it really works

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Kieran Mansley
Sent: 2011年9月2日 3:22
To: Mailing list for lwIP users
Subject: Re: [lwip-users] FreeRTOS / lwip multiple connections


vincent cui

unread,
Sep 2, 2011, 1:26:12 AM9/2/11
to Mailing list for lwIP users
Hi;

When unplug internet cable, how lwip deal it ? I want to show message to user in LCD to notice user !

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Kieran Mansley
Sent: 2011年9月2日 3:22
To: Mailing list for lwIP users
Subject: Re: [lwip-users] FreeRTOS / lwip multiple connections


ake.fo...@nibe.se

unread,
Sep 2, 2011, 2:49:57 AM9/2/11
to Mailing list for lwIP users
Hi!

I think that is something you define in your ethernet driver. My approach is to send a signal to a separate task to handle the cable-plug/unplug events. In the task I currently disable dhcp (if active) on cable-unplug signal and reenable it on cable-connect. it might be good to set an auto-ip when unplugged as well, but I'm not sure. Maybe it should be handled with the netif_set_up/down-functions instead, I'm still experimenting...

/Åke

-----------------------------
Åke Forslund
0433-273296
NIBE AB
Box 14
S-285 21  Markaryd
Tel +46-(0)433-273296



From: vincent cui <vince...@enlogic.com>
To: Mailing list for lwIP users <lwip-...@nongnu.org>
Date: 2011-09-02 07:27
Subject: [lwip-users] how lwip know internet cable is unplug
Sent by: lwip-users-bounces+ake.forslund=nib...@nongnu.org


vincent cui

unread,
Sep 2, 2011, 3:06:51 AM9/2/11
to Mailing list for lwIP users

Dear:

 

I define a semaphore used to send signal to tell upper  app….the semaphore is controlled by ethernet driver..

 

Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

 

Simon Goldschmidt

unread,
Sep 2, 2011, 3:54:49 AM9/2/11
to Mailing list for lwIP users
ake.fo...@nibe.se wrote:
> Maybe it should be
> handled with the netif_set_up/down-functions

Nearly: it should call netif_set_link_up/down (watch out for threading issues!), which in turn calls user-specified callbacks. This has to be enabled using a define (I think it's turned off by default).

Simon
--
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!
Jetzt informieren: http://www.gmx.net/de/go/freephone

vincent cui

unread,
Sep 2, 2011, 3:57:06 AM9/2/11
to Mailing list for lwIP users
Simon:'

Good, it is easy to deal with.
Thank you very much.

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Simon Goldschmidt
Sent: 2011年9月2日 15:55
To: Mailing list for lwIP users

FreeRTOS Info

unread,
Sep 2, 2011, 3:59:22 AM9/2/11
to Mailing list for lwIP users, vincent cui

On 02/09/2011 01:12, vincent cui wrote:
> HI :
>
> I want to create a semaphore (value is 10) to deal with max connection . but freertos only provide mutex and binary semaphore...
> Is there other way to come it true ?

Yes. http://www.freertos.org/CreateCounting.html


Regards,
Richard.

+ http://www.FreeRTOS.org
Designed for Microcontrollers.
More than 7000 downloads per month.

>
> ï»?Vincent Cui

vincent cui

unread,
Sep 2, 2011, 3:59:20 AM9/2/11
to FreeRTOS Info, Mailing list for lwIP users
Thank you

锘?Vincent Cui

vincent cui

unread,
Sep 2, 2011, 4:02:02 AM9/2/11
to Mailing list for lwIP users
Simon:

I will try to play chargen.c and chargen.h., the readme show that test it by telnet ip 19 and see pattern in screen,
But it is only one connection . how to test 5 or 10 muli-connection ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Simon Goldschmidt
Sent: 2011年9月2日 15:55
To: Mailing list for lwIP users

Stephane Lesage

unread,
Sep 2, 2011, 4:55:17 AM9/2/11
to Mailing list for lwIP users

> When unplug internet cable, how lwip deal it ? I want to show message
> to user in LCD to notice user !

Hi,
Here's what I do (NOSYS=0)

In my PHY driver init:
bfphy_msg = tcpip_callbackmsg_new(bfphy_interrupt_callback, netif);

In the interrupt:
tcpip_trycallback(bfphy_msg);

In the callback, executing in the tcpip_thread context:

void bfphy_interrupt_callback(void* ctx)
{
struct netif* netif = (struct netif*) ctx;

Read PHY registers (simple function with mutex protection in my
case)
Set Full-duplex, RMII 10/100 parameters, etc...

// Link state
if (stat & PHY_MODESTAT_AUTONEG_COMPLETE)
netif_set_link_up(netif);
else
netif_set_link_down(netif);
}

Now you can either:
- use the netif callbacks to get the information
- regularly poll with netif_is_link_up(netif);

Kieran Mansley

unread,
Sep 2, 2011, 7:02:34 AM9/2/11
to lwip-...@nongnu.org
On Fri, 2011-09-02 at 05:26 +0000, vincent cui wrote:
> When unplug internet cable, how lwip deal it ?

It doesn't. Whether the link is up or not is of no real interest to a
TCP/IP stack. It will just try to send packets, and the lower layers
(e.g. the driver you're using) have to deal with the link status.

Simon Goldschmidt

unread,
Sep 2, 2011, 7:06:13 AM9/2/11
to Mailing list for lwIP users

Kieran Mansley <kie...@recoil.org> wrote:
> An: lwip-...@nongnu.org
> Betreff: Re: [lwip-users] how lwip know internet cable is unplug

> On Fri, 2011-09-02 at 05:26 +0000, vincent cui wrote:
> > When unplug internet cable, how lwip deal it ?
>
> It doesn't. Whether the link is up or not is of no real interest to a
> TCP/IP stack. It will just try to send packets, and the lower layers
> (e.g. the driver you're using) have to deal with the link status.

That's only half the truth. At least dhcp/AutoIP use this information: they restart address negotiation after the link status has been changed to 'up' (using netif_set_link_up()). After all, this is a feature which should be implemented when using dhcp/AutoIP or the lwIP device will continue to use an address of one subnet when plugged into another subnet until the original lease would expire.

Simon
--
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!
Jetzt informieren: http://www.gmx.net/de/go/freephone

_______________________________________________

ake.fo...@nibe.se

unread,
Sep 2, 2011, 8:03:04 AM9/2/11
to Mailing list for lwIP users
Thanks for the tip. set_link_up/down looks pretty safe to use from my rx-thread (rx handled by a separate thread triggered by eth-interrupt). Is it the callback that need to be protected from threding issues?

/Åke
-----------------------------
Åke Forslund
0433-273296
NIBE AB
Box 14
S-285 21  Markaryd
Tel +46-(0)433-273296



From: "Simon Goldschmidt" <gold...@gmx.de>
To: Mailing list for lwIP users <lwip-...@nongnu.org>
Date: 2011-09-02 09:55
Subject: Re: [lwip-users] how lwip know internet cable is unplug
Sent by: lwip-users-bounces+ake.forslund=nib...@nongnu.org





Simon Goldschmidt

unread,
Sep 2, 2011, 8:15:37 AM9/2/11
to Mailing list for lwIP users

ake.fo...@nibe.se wrote:
> Thanks for the tip. set_link_up/down looks pretty safe to use from my
> rx-thread (rx handled by a separate thread triggered by eth-interrupt). Is
> it the callback that need to be protected from threding issues?

Even if it is now, it is only so by chance and you shouldn't rely on it. Such mixed threading code is a no-go, you only risk producing bugs that are *very* hard to track down later. Just do it right now and be safe: it's just the lwIP principle that things like this are not protected from concurrent access by the lwIP core code.

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

ake.fo...@nibe.se

unread,
Sep 2, 2011, 8:25:20 AM9/2/11
to Mailing list for lwIP users
I'm not quite sure what you're suggesting. Is it to put all calls that may change states of netif, dhcp etc. in a single thread or is it the use of a separate thread to handle RX, or something else?

-----------------------------
Åke Forslund
0433-273296
NIBE AB
Box 14
S-285 21  Markaryd
Tel +46-(0)433-273296



From: "Simon Goldschmidt" <gold...@gmx.de>
To: Mailing list for lwIP users <lwip-...@nongnu.org>
Date: 2011-09-02 14:15
Subject: Re: [lwip-users] how lwip know internet cable is unplug
Sent by: lwip-users-bounces+ake.forslund=nib...@nongnu.org





Simon Goldschmidt

unread,
Sep 2, 2011, 8:32:19 AM9/2/11
to Mailing list for lwIP users
ake.fo...@nibe.se wrote:
> I'm not quite sure what you're suggesting. Is it to put all calls that may
> change states of netif, dhcp etc. in a single thread or is it the use of a
> separate thread to handle RX, or something else?

I'm just repeating the mantra that lwIP core code is not thread protected (with very little exceptions, mostly regarding memory management functions) and either all code using lwIP functions/variables must be used from one thread only or you have to protect it yourself (e.g. by using a semaphore or something like that).

E.g. if you have RX in one thread and TX in another thread, you can be most sure that you will get problems at some point (e.g. corrupted state, corrupted pbuf-ref or corrupted lists).

I have to point this out to prevent other users reading this list from making such a mistake (that can be hard to debug). And your statement "set_link_up/down looks pretty safe to use from my rx-thread" can sound like these functions are thread-safe to new users (as it did, to me).

ake.fo...@nibe.se

unread,
Sep 2, 2011, 8:46:50 AM9/2/11
to Mailing list for lwIP users
Ok, I now I get it.

So protection around my calls it is, but I don't quite see how RX and TX can be handled in the same thread since the TX-function is registered with lwip in the netif-structure and RX isn't.

OK I see now that there is an input-pointer in the netif-structure as well but it's not referenced in the driver-template (ethernetif.c) so I invented something on my own...I'll have to dig through the examples one more time and look how they have done it...

/Åke
-----------------------------
Åke Forslund
0433-273296
NIBE AB
Box 14
S-285 21  Markaryd
Tel +46-(0)433-273296



From: "Simon Goldschmidt" <gold...@gmx.de>
To: Mailing list for lwIP users <lwip-...@nongnu.org>
Date: 2011-09-02 14:32
Subject: Re: [lwip-users] how lwip know internet cable is unplug
Sent by: lwip-users-bounces+ake.forslund=nib...@nongnu.org





ake.fo...@nibe.se

unread,
Sep 2, 2011, 8:51:01 AM9/2/11
to Mailing list for lwIP users
Ok I clearly can't read my own code anymore. I do use it but inside the drivers _input()-function so I'm still at a loss how I'm supposed to handle RX and TX in the same thread...

I'll try to be more coherent next time

Thanks for all the help

Simon Goldschmidt

unread,
Sep 2, 2011, 9:29:29 AM9/2/11
to Mailing list for lwIP users

ake.fo...@nibe.se wrote:
> So protection around my calls it is, but I don't quite see how RX and TX
> can be handled in the same thread since the TX-function is registered with
> lwip in the netif-structure and RX isn't.

By using tcpip_input() as the input function. This passes received packets into the tcpip_thread before actually feeding them into the stack.

> OK I see now that there is an input-pointer in the netif-structure as well
> but it's not referenced in the driver-template (ethernetif.c) so I
> invented something on my own...I'll have to dig through the examples one
> more time and look how they have done it...

It *is*: it's not set there but it is used. The code that sets it is in netif.c (the function being set as netif->input has to be passed to netif_add()).

Simon
--
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!
Jetzt informieren: http://www.gmx.net/de/go/freephone

_______________________________________________

ake.fo...@nibe.se

unread,
Sep 2, 2011, 9:40:08 AM9/2/11
to Mailing list for lwIP users
Thanks for the advice,

I seem to have gotten it right when I wrote the code. I use tcpip_input() for handling input, so that part should be safe. I'll go through my calls into lwip and add protection.

Thank you for your help and clarification.

Best regards
/Åke
-----------------------------
Åke Forslund
0433-273296
NIBE AB
Box 14
S-285 21  Markaryd
Tel +46-(0)433-273296



From: "Simon Goldschmidt" <gold...@gmx.de>
To: Mailing list for lwIP users <lwip-...@nongnu.org>
Date: 2011-09-02 15:29
Subject: Re: [lwip-users] how lwip know internet cable is unplug
Sent by: lwip-users-bounces+ake.forslund=nib...@nongnu.org





vincent cui

unread,
Sep 4, 2011, 11:23:11 PM9/4/11
to Mailing list for lwIP users
All

I write a simple routine to deal with it , it works well,
static uint32_t ETH_GetNetifUporDown (void)
{
return (ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BSR) & PHY_Linked_Status) ? 1:0;
}

Why do you use callback to handle it ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com
-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Kieran Mansley
Sent: 2011年9月2日 19:03
To: lwip-...@nongnu.org
Subject: Re: [lwip-users] how lwip know internet cable is unplug

vincent cui

unread,
Sep 5, 2011, 2:21:05 AM9/5/11
to Mailing list for lwIP users

HI Ake and Simon:

 

I set callback function as following, but have to call netif_set_link_up/down to trigger the callback function.

Once cable is unplugged / plugged, the netif_set_link_down/up  should be called by one times. I don’t know how to do this…

I can use the ETH_GetNetifUporDown function to get the Ethernet link status (not lwIP link status), but where to put it ?

 

Would you help me 

 

 

static uint32_t ETH_GetNetifUporDown (void)

{

         return (ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BSR) & PHY_Linked_Status) ? 1:0;

}

------------------------------------------------------------------------------------------------------------------------

void lwip_link_callback(struct netif *netif )

{

         if (netif_is_link_up(netif))

         {

                   printf ("link status go to up \r\n");

                   dhcp_start(netif);

                   netif_set_up(netif);

         }

         else

         {

                  printf ("link status go to down \r\n");

                   dhcp_stop(netif);

                   netif_set_down(netif);

         }

}

 

Set the call back function in lnit_lwip()

 

   netif_add(&netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);

 

    netif_set_default(&netif);

 

netif_set_link_callback(&netif, lwip_link_callback);

 

------------------------------------------------------------------------------------------------------------------------

 

Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

 

vincent cui

unread,
Sep 6, 2011, 7:14:26 PM9/6/11
to Mailing list for lwIP users

>>>> My approach is to send a signal to a separate task to handle the cable-plug/unplug events

 

Ake:

 

For your approach, who send a signal to the task ? the driver ?  my driver can get the link status by polling, not interrupt, so it takes more cpu time..I stop using it

And try to find another one to replace… maybe it need HW sensor support.

Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

 

ake.fo...@nibe.se

unread,
Sep 7, 2011, 2:26:15 AM9/7/11
to Mailing list for lwIP users
Hi Vincent,

my application is not time critical and due to an error on the PCB the interrupt line from my ethernet controller is not connected.

Right now my sollution works like this:

my driver is initied via *_init() and creates the task that periodically polls my ethernet controller for new frames and events. When cable plug/unplug occurs a function is called (this function is registred as a callback to keep the project structure sane). This function then safely executes netif_set_link_up/down().

The task polling the ethernet controller is run every ~44 ms. I'm not sure what sort of response time you are hoping to achieve but for my purposes it seem to work fine.

/Åke

-----------------------------
Åke Forslund
0433-273296
NIBE AB
Box 14
S-285 21  Markaryd
Tel +46-(0)433-273296



From: vincent cui <vince...@enlogic.com>
To:
Mailing list for lwIP users <lwip-...@nongnu.org>
Date: 2011-09-07 01:15
Subject:
Re: [lwip-users] how lwip know internet cable is unplug
Sent by: lwip-users-bounces+ake.forslund=nib...@nongnu.org


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

vincent cui

unread,
Sep 7, 2011, 2:44:25 AM9/7/11
to Mailing list for lwIP users

Alk

 

Thank you for your reply …. Now, I know your method to deal with ….but it is not a good idea with polling …

I ask my HW guys support LED_LINK pin with interrupt. So I can receive the event at real time

ake.fo...@nibe.se

unread,
Sep 7, 2011, 2:51:52 AM9/7/11
to Mailing list for lwIP users
I agree that polling isn't the best way of dealing with it. If your ethernet-controller really doesn't support interrupt on link-status change the hardware hack you propose might be the only way to go. What controller is it you're working with?

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

vincent cui

unread,
Sep 7, 2011, 3:07:50 AM9/7/11
to Mailing list for lwIP users

I use STM32F107 board, Cortex M3…

 DP83848c,,,,

ake.fo...@nibe.se

unread,
Sep 7, 2011, 3:30:15 AM9/7/11
to Mailing list for lwIP users
The DP83848c seem to have IRQ on link status change (data sheet page 53). Why can't you use the normal IRQ?

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

vincent cui

unread,
Sep 7, 2011, 3:34:27 AM9/7/11
to Mailing list for lwIP users

The normal IRQ is Ethernet_IRQ ?  

Simon Goldschmidt

unread,
Sep 7, 2011, 3:45:56 AM9/7/11
to Mailing list for lwIP users
vincent cui <vince...@enlogic.com> wrote:
> Thank you for your reply …. Now, I know your method to deal with ….but
> it is not a good idea with polling ...

I agree that polling for RXframes isn't optimal, but link-change events shouldn't be too high-priority, so you could check/process them in a very low-priority task (which is what I did before having the interrupt).

Simon
--
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!
Jetzt informieren: http://www.gmx.net/de/go/freephone

_______________________________________________

ake.fo...@nibe.se

unread,
Sep 7, 2011, 3:48:53 AM9/7/11
to Mailing list for lwIP users
Yes (The interrupt output-pin that is muxed with the Power-down-signal on the DP83848c)

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

vincent cui

unread,
Sep 7, 2011, 3:49:40 AM9/7/11
to Mailing list for lwIP users

I just check the Ethernet driver,,, it doesn’t enable the link_status in INT CTRL register… so , I never get it

vincent cui

unread,
Sep 7, 2011, 4:21:45 AM9/7/11
to Mailing list for lwIP users
Simon:

You mean that I could polling the link status in a low priority task, the task does
1. disable dhcp if dhcp is enable in link loss case
2. enable dhcp if dhcp is disable in link recover case.

The link status from loss to recover will happen one time at boot time...so the dhcp enable will put the task too ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Simon Goldschmidt
Sent: 2011年9月7日 15:46
To: Mailing list for lwIP users
Subject: Re: [lwip-users] how lwip know internet cable is unplug

Simon Goldschmidt

unread,
Sep 7, 2011, 4:30:13 AM9/7/11
to Mailing list for lwIP users
vincent cui <vince...@enlogic.com>

> You mean that I could polling the link status in a low priority task, the
> task does
> 1. disable dhcp if dhcp is enable in link loss case
> 2. enable dhcp if dhcp is disable in link recover case.

You don't need to do that: lwIP handles calling dhcp itself if you just call netif_set_link_up/down.

> The link status from loss to recover will happen one time at boot
> time...so the dhcp enable will put the task too ?

No, I initialized it once after booting so that change didn't happen after botting.

vincent cui

unread,
Sep 7, 2011, 4:36:03 AM9/7/11
to Mailing list for lwIP users
But if booting, there is no cable plugged in, how about ?

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Simon Goldschmidt
Sent: 2011年9月7日 16:30
To: Mailing list for lwIP users
Subject: Re: [lwip-users] how lwip know internet cable is unplug

vincent cui

unread,
Sep 7, 2011, 4:40:13 AM9/7/11
to Mailing list for lwIP users
netif_set_link_up/down will do something about dhcp, but it also call link_callback function.

I plan do something about dhcp in the link callback function . as you said, it is redundence

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

-----Original Message-----
From: lwip-users-bounces+vincent.cui=enlog...@nongnu.org [mailto:lwip-users-bounces+vincent.cui=enlog...@nongnu.org] On Behalf Of Simon Goldschmidt
Sent: 2011年9月7日 16:30
To: Mailing list for lwIP users
Subject: Re: [lwip-users] how lwip know internet cable is unplug

Bill Auerbach

unread,
Sep 7, 2011, 10:07:01 AM9/7/11
to Mailing list for lwIP users

Vincent,

 

I have to poll the link state as well on 2 platforms but it’s a simple PHY I/O read and it takes very little time. I do that as part of my incoming packet check when there are no packets to process.  If there are packets to process obviously the link is up. J

 

Bill

vincent cui

unread,
Sep 7, 2011, 8:29:41 PM9/7/11
to Mailing list for lwIP users

Bill

 

Would you tell me where to check the incoming package ?

 

Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: vince...@enlogic.com
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 200233
http://www.enlogic.com

 

Reply all
Reply to author
Forward
0 new messages