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