pigeond pigeond

pigeon's blog - the pigeon concerto in D minor


Bonding – switching network interface without losing connections
2007/02/12 17:48:19
linux software

There’s wireless setup at my wife’s home in Taiwan. But whenever I’m upstairs in our room I prefer using the wired connection. While when I’m downstairs I have no choice as there’s no extra ethernet port.

Switching between the two networks is not a big deal. And with the help of ifplugd, the switching is automatic. But after all, they are two different interfaces. It would be good if I could switch between them without losing any existing connections. After a bit of googling around, I’ve found one solution: Ethernet channel bonding.

From the Linux source code Documentation/networking/bonding.txt: “The Linux bonding driver provides a method for aggregating multiple network interfaces into a single logical “bonded” interface”. In my case, I’m bonding the eth0 (wired) and the eth2 (wireless) together.

Under Debian, the bonding driver is built as a module bonding. I’ve also installed the ifenslave. So I put the bonding driver into my /etc/modules so it’s loaded at system startup:

bonding mode=1 miimon=500

where mode=1 sets the bonding mode to active-backup. The bonding driver also supports a few different load-balancing modes, which you might want to check out as well. miimon=500 sets the link check interval in milliseconds.

There’s also a primary parameter which you could specify which slave interface will always be used if it is available (e.g. primary=eth0).

The bonding driver gives you a new network interface bond0. Now to setup bond0, eth0 and eth2 in /etc/network/interfaces:

# the wired interface
iface eth0 inet manual
    hostname xxxxxxx

# the wireless interface
iface eth2 inet manual
    wireless-key s:xxxxxxxxxxxxx restricted
    wireless-essid xxxxxxxxxxxx
    wireless-mode managed

# the bonding interface, eth0 + eth2
iface bond0 inet dhcp
    pre-up ifconfig bond0 up
    pre-up ifenslave bond0 eth0 eth2
    pre-down ifenslave -d bond0 eth0 eth2

bond0 is set to use DHCP, while eth0 and eth2 are not, as they will act as slave interfaces. bond0 is the master. The 2nd pre-up command for bond0 attaches both eth0 and eth2 as slaves to the bonding device. Note that it is possible to configure a bonding device via sysfs, which is also documented in Documentation/network/bonding.txt.

While ifplugd takes care of eth0 going up and down, I also want to turn off the wireless completely when the wired network is plugged in. So I added my own script /etc/ifplugd/action.d/bonding:

#!/bin/sh
set -e

if [ ! -f /proc/net/bonding/bond0 ]; then
    exit
fi

case "$1" in
eth0)
    case "$2" in
    up)
        /sbin/ifdown eth2
        /sbin/ifconfig eth2 down
        /sbin/iwconfig eth2 txpower off
        ;;

    down)
        /sbin/iwconfig eth2 txpower auto
        /sbin/ifup eth2
        /sbin/ifconfig eth2 up
        ;;
    esac

    # I need to do this sometimes, so I've put it in
    # or could someone comment on this?
    ifdown bond0
    ifup bond0

    ;;
esac

This script will turn off the wireless radio for me when my eth0 wired network is up.

I’m relying on ifplugd to bring up bond0, which will then bring up eth0 and eth2. Hence in /etc/network/interfaces I did not set any of the interfaces to be auto. I also added -l to the arguments of ifplugd in /etc/default/ifplugd:

ARGS="-q -f -u0 -d10 -w -I -l"

so that it will run the “down” script on ifplugd startup if no cable is detected. That way my script will bring up my wireless eth2 if eth0 is not plugged, say at boot time.

With this setup, the bonding driver automatically switches to use eth0 or eth2, whichever is brought up. In my setup, eth0 and eth2 won’t be up at the same time. If you use the primary parameter, the primary slave interface will always be used, even if the other slaves become available.

/proc/net/bonding/bond0 shows the status of the bonding interface. For example I’m now on the wireless network:

Ethernet Channel Bonding Driver: v3.0.3 (March 23, 2006)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth2
MII Status: up
MII Polling Interval (ms): 500
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: down
Link Failure Count: 0
Permanent HW addr: 00:11:2f:9c:57:25

Slave Interface: eth2
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:11:2f:9c:57:25

It’s pretty obvious that the currently active slave is my eth2 wireless. I can run something like watch -n 1 cat /proc/net/bonding/bond0, and as I plug the ethernet cable back, the active slave will become eth0. You can also find out the currently active slave via /sys/class/net/bond0/bonding/active_slave.

For completeness, here is the output of ifconfig:

bond0     Link encap:Ethernet  HWaddr 00:11:2F:9C:57:25
          inet addr:192.168.11.3  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::211:2fff:fe9c:5725/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:329306 errors:0 dropped:0 overruns:0 frame:0
          TX packets:329010 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:732552585 (698.6 MiB)  TX bytes:204431117 (194.9 MiB)

eth0      Link encap:Ethernet  HWaddr 00:11:2F:9C:57:25
          UP BROADCAST SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:303936 errors:0 dropped:0 overruns:0 frame:0
          TX packets:304693 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:245381818 (234.0 MiB)  TX bytes:27905638 (26.6 MiB)
          Interrupt:5 Base address:0xd800

eth2      Link encap:Ethernet  HWaddr 00:11:2F:9C:57:25
          inet6 addr: fe80::211:2fff:fe9c:5725/64 Scope:Link
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:25370 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24317 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:487170767 (464.6 MiB)  TX bytes:176525479 (168.3 MiB)
          Interrupt:5 Base address:0xe000 Memory:ff9fe000-ff9fefff

which shows bond0 being the master, and eth0 and eth2 being slaves.

It almost makes plugging and unplugging the ethernet cable fun. I can just sit there and keep replugging the cable alllllll day… :)

I’ve been using this setup for at least a week now and has been working very well. Please let me know if there’s anything I’ve missed, or any even better way of setting this up.





“I know kung fu…”
2007/02/11 20:36:47
linux software

And so does everyone else. Simply apt-get install ipkungfu.

I’ve finally switched my gateway box from my 12 years old Pentium (I) 166MHz gateway box (still running Debian potato, kernel 2.2. This is Linux for you, when it works, it can work forever…), to my 5-6 years old Netwinder.

I was also very glad that the Netwinder is now (or still) officially supported by Debian. I had Debian potato installed on it for years. Upgrading it was painless.

My old gateway box was running such an old kernel, and the firewall was one of those copied-from-others script using ipchains. This time I decided to use a better way to manage my firewall rules. After a bit of research and trying things out, I’ve chosen ipkungfu. Again, it was painless to setup.

The only not-so-good bit was, I did the machine switching-over only 1 day before I went away. So as it turned out, my net connection went down in about 2 days time. I always have some scripts checking if the net is up or not, and it would reset everything (bringing down/up the interface, renewing IP, relogging on, etc). But looks like that wasn’t helping either. So I thought it might be one of those cable-modem-needed-to-be-reset situation.

I asked my uncle to power cycle the cable modem for me. And just to be safe I asked him to do the same for the gateway box too. As expected my net connection went up again. And I was happy, for a while.

Who would have thought, it went down again. Suspiciously, it also only stayed up for about 2 days. My instinct told me it wasn’t a coincident. A few days later my uncle again power cycled the modem.

I got on and check the logs again and found that my firewall was blocking (and logging) a lot of IGMP packets from some mysterious 10.x.x.x (Telstra internal network instruments) IP. It’s strange though, I haven’t seen these logs when I was testing the net. So apparently just the bpalogin client heartbeat is not enough. Seems that without the IGMP packet response (or how-ever IGMP works), after a certain timeout period (2 days?) the other end terminates the net connection and stops leasing IP, even if the cable modem is on or even there are existing network connections. So of course, I’ve then updated my firewall rules.

Lesson learnt, testing the net for 1 day is not enough, cos it will go down in 2 days ;)

Once again, many thanks to my uncle for the trouble.


permlink :: trackback :: Comments Off



blog top 24 tags
api bonding debian engrish fglive fgmap flightgear google ISP laptop lca2007 lca2008 linux live map mpserver navaids network qotd quote of the day taiwan vim windows wine
Search
 
Web pigeond.net









this site is
created with vim



nearly all pages in this site are validated as
Valid HTML 4.01!

css validated by
Valid CSS!


pigeond.net copyright 2001 - 2014
designed, created, and coded by pigeon at pigeond dot net
all rights reserved