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:

set -e

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

case "$1" in
    case "$2" in
        /sbin/ifdown eth2
        /sbin/ifconfig eth2 down
        /sbin/iwconfig eth2 txpower off

        /sbin/iwconfig eth2 txpower auto
        /sbin/ifup eth2
        /sbin/ifconfig eth2 up

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


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:  Bcast:  Mask:
          inet6 addr: fe80::211:2fff:fe9c:5725/64 Scope:Link
          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
          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.

5 comments to “Bonding – switching network interface without losing connections”

2007/10/21 20:58:00

hi there!!!

I like your article. I’m actually trying to do the same things with my vaio: ipw3945(eth1)+sky2(eth0), in order to gain more “mobility”. But once I got the interface enslaved, the bond0 interface cannot get dhcp any longer: it’s switching between state up and down almost every second. My distribution is gentoo. Thank a lot in advance, and again GOOD JOB!!!!!

Beaker replies:
2010/03/24 01:25:02

Can we email and not use forums I have to activate java for? How to contact the author of the article via email?

2010/03/24 01:30:29

At least I don’t have to register again into one more forum to post :-)

I don’t understand how ifplugd should be able to detect plugging of cables on eth0 and react to it by reconfiguring bond0. Which nic do you actually control with ifplug? You mention both, eth0 and bond0 in the text which does not make sense, because at the same time you write that bond0 will bring up eth0 itself. Who brings up eth0? Is it bond0 or is it ifplugd which controls bond0?

Does your configuration work with different networks on the fly? I.e. is it possible in a different network to plug cable again and get the different ip off dhcp?

2010/09/09 00:22:37

[...] [...]

Debian-Installation mit zwei Netzwerkkarten ausfallsicher(er) machen « think eMeidi
2013/07/26 04:26:34

[...] Bonding – switching network interface without losing connections (aus dem Jahr 2007, d.h. veraltet. Meine ursprüngliche Idee war es, die zweite Netzwerkverbindung über einen USB-WiFi-Adapter (Zyxel NWD2205) herzustellen) [...]

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
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