Adding DHCP, AutoIP and IGMP to LWIP support

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
trepidacious
Posts: 58
Joined: Mon Jan 21, 2013 3:36 pm
Been thanked: 1 time

Adding DHCP, AutoIP and IGMP to LWIP support

Postby trepidacious » Mon Jan 21, 2013 4:22 pm

I've just started working with ChibiOS on the Olimex P407 board - great fun so far!

I've attached patch files to enable DHCP, AutoIP and IGMP via LWIP, in the hope this could be added to ChibiOS 2.4...
ChibiOS2.4.3-AddDHCP-IGMP-AutoIP-20130121-1407-patch.zip
Patch for ChibiOS
(4.2 KiB) Downloaded 360 times

This allows the board to attempt to receive an address from a DHCP server, but fall back to a link-local address using AutoIP if no DHCP server is found. The IGMP support allows for subscribing to multicast messages, and so could be used for implementing a UDP discovery protocol like ZeroConf or upnp to allow finding the board after it gets an address.

The changes are pretty minimal, just calling additional init methods, then setting up some more "tmr" methods to be called regularly from the lwipthread. There is also a small change in cc.h to add a LWIP_RAND define. In order for this to compile, there are also changes to lwipopts.h to enable the various LWIP features.

The patches are against 2.4.3, one file for changes to ChibiOS, then another to change lwipopts.h in the P407-LWIP demo.

I'm not sure what the best way is to allow for configuration of the changes, in case they are not wanted - it seems like there could be additional settings in lwipthread_opts. Alternatively, the new code could be controlled by the existing defines like LWIP_DHCP, LWIP_AUTOIP and LWIP_IGMP.

There is one additional change that is needed for the IGMP support to work - the MAC needs to be configured to pass through multicast packets. The simplest way of doing this seems to be from code in the appropriate demo, adding:

Code: Select all

ETH->MACFFR    = ETH_MACFFR_PAM;

This seems to work when called before the "netconn_join_leave_group" function that is required to join a group.

I've tried the IGMP support, and multicast packets are received - the DHCP and AutoIP also seem to work very well, the board will swap between networks repeatedly, moving from DHCP to AutoIP and back as appropriate.

User avatar
Giovanni
Site Admin
Posts: 13086
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 759 times
Been thanked: 637 times
Contact:

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby Giovanni » Mon Jan 21, 2013 4:28 pm

Hi,

Thanks for the contribution, I will look into adding that flag directly into the MAC driver. I assume it is safe since you tested it.

Giovanni

trepidacious
Posts: 58
Joined: Mon Jan 21, 2013 3:36 pm
Been thanked: 1 time

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby trepidacious » Mon Jan 21, 2013 6:08 pm

Great - I think the only downside for enabling that flag is that more packets will reach LWIP rather than getting thrown away by the MAC. I would imagine that would only be a problem if there is a lot of broadcast/multicast traffic on the network.

User avatar
Giovanni
Site Admin
Posts: 13086
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 759 times
Been thanked: 637 times
Contact:

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby Giovanni » Mon Jan 21, 2013 6:38 pm

I could add a configuration option for that, that way it would be optional.

Giovanni

trepidacious
Posts: 58
Joined: Mon Jan 21, 2013 3:36 pm
Been thanked: 1 time

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby trepidacious » Tue Jan 22, 2013 11:45 am

That sounds good. Would configuration options work for selecting whether to run the DHCP, AutoIP and IGMP timers as well? I think they are all pretty much independently useful.

rubenswerk
Posts: 104
Joined: Wed Feb 22, 2012 11:39 am
Location: Austria

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby rubenswerk » Tue Jan 22, 2013 3:06 pm

I also tried out DHCP and AutoIP. I didn't have to add any tmr methods for that. Just activated the modules in the options file, and start via dhcp_start(). That's it.
I could see the increasing time between DHCP retries. 2secs, 4secs, 8secs...
I used the cooperation mode, which switches to AutoIP after a configurable number of DHCP tries.

But there was one problem:
When I started the board without a network link, and then establish the connection after 1 minute, the DHCP tries were already over and only AutoIP was used as fallback solution. I would have expected that the DHCP requests start when the link goes up, but not when the board is powered.

When the network cable was plugged from the very beginning, everything was fine.

trepidacious
Posts: 58
Joined: Mon Jan 21, 2013 3:36 pm
Been thanked: 1 time

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby trepidacious » Tue Jan 22, 2013 6:30 pm

From a quick read through the header files, I think the tmr methods should be called - for example in autoip.h, it states:
"Has to be called in loop every AUTOIP_TMR_INTERVAL milliseconds"
I think the same applies for the DHCP and IGMP timers.

I also used the DHCP/AutoIP cooperation mode, this seems to work well.

I've tried your test case of starting the board with the cable unplugged for 1 minute, then connecting the cable - this works for me, with a DHCP address assigned within a few seconds, then I start seeing the multicast traffic I send from the board, with the DHCP assigned source address. When I was doing earlier testing I tried swapping the network connection between two different networks, one with DHCP and one without, and found that the board would swap between AutoIP assigned addresses and DHCP assigned addresses multiple times. This is noticeable both from multicast/broadcast data and from some serial output when a separate thread notices that the interface has a new address assigned.

The only edge case I am not certain about is if DHCP assigns a new address without the network link going down and up - in this case I have not tested that the IGMP code will rejoin any groups it was part of, however this may well work (quite difficult to check!)

rubenswerk
Posts: 104
Joined: Wed Feb 22, 2012 11:39 am
Location: Austria

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby rubenswerk » Tue Feb 05, 2013 5:08 pm

Hello,
please have a look at sys_timeouts_init() in lwip/timers.c
This function is called from lwip_init() if LWIP_TIMERS is defined. This is the case if an OS is used.
sys_timeouts_init sets up timers for DHCP coarse and fine, as well as AutoIP.
However, as described above, I had some problems when switching between the modes.
For my tests I set LWIP_DHCP_AUTOIP_COOP_TRIES to 4. With the default value 9 it would take very long until switching to AutoIP (the interval between DHCP retries doubles each time).

trepidacious
Posts: 58
Joined: Mon Jan 21, 2013 3:36 pm
Been thanked: 1 time

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby trepidacious » Thu Feb 07, 2013 6:38 pm

Hi,
Ah, I see what you mean! I'll try my code with the extra tmr calls removed, that would be much simpler. In that case the only changes needed to lwipthread would be to remove:

Code: Select all

netif_set_up(&thisif);

And replace with:

Code: Select all

netifapi_dhcp_start(&thisif);
tcpip_callback_with_block((tcpip_callback_fn) igmp_start, &thisif, 0);

(I'm not quite sure whether the tcpip_callback thing is needed)
Does that match what you did when you ran into problems with the mode switching?

I also reduced the number of tries, in my case to 5.

trepidacious
Posts: 58
Joined: Mon Jan 21, 2013 3:36 pm
Been thanked: 1 time

Re: Adding DHCP, AutoIP and IGMP to LWIP support

Postby trepidacious » Wed May 29, 2013 3:46 pm

I finally got round to retrying this and you're completely correct - the additional tmr calls are not required. I've set the LWIP_DHCP_AUTOIP_COOP_TRIES to 3, and this gives about a 16 second delay before DHCP gives up and AUTOIP takes over, which seems reasonable. This works fine for multiple changes between DHCP and AUTOIP.


Return to “User Projects”

Who is online

Users browsing this forum: No registered users and 1 guest