Generic load balancer for multiple WAN links

So you have two or more ADSL lines and want to use them all?
… or maybe you’re stealing your neighbor’s wifi and you have more than one network available?
… or you have cloned your SIM card and want to use multiple 3G connections simultaneously?

You can easily setup your Linux box to route multiple connections using iproute2, no matter how many WAN links you have! I have made a script to automate the process, it also comes with an optional failover watchdog which will monitor all the WAN links and automatically disable those which fail, re-enabling them when the connection is back.

Configuration is simple -one column for each WAN link-, you just need to have a separate interface for each link. If you want to use only one physical interface I recommend using a different VLAN on each WAN link. Edit the script to configure it as follows:

#
# Specify each WAN link in a separate column, example:
#
# In this example we have 3 wan links (vlanXXX interfaces) attached to a single
# physical interface because we use a vlan-enabled switch between the balancer
# machine and the ADSL routers we want to balance. The weight parameter should
# be kept to a low integer, in this case the ADSL line connected to vlan101 and
# vlan102 is 4Mbps and the ADSL line connected to vlan100 is 8Mbps (twice fast)
# so the WEIGHT value in vlan100 is 2 because it is two times faster.
#
# WANIFACE=\" vlan101 vlan100 vlan102\"
# GATEWAYS=\" 192.168.1.1 192.168.0.1 192.168.2.1\"
# NETWORKS=\" 192.168.1.0/24 192.168.0.0/24 192.168.2.0/24\"
# WEIGHTS=\" 1 2 1\"
#
# quick formula to calculate the weight: (LINKSPEED/MINSPEED)*NUM_LINKS
#
# If you don't want to use vlans, you should then use a separate physical
# interface for each link. IP aliasing on the same interface is not supported.
#

The script will set up the default route to be a multipath route, this will balance routes over the multiple WAN links and cache them depending on the destination address (so often used sites will always be sent over the same link). The weight parameters can be adjusted for each link in case you don’t have the same speed on each.

13 Responses to Generic load balancer for multiple WAN links

After using this for a while, I have found a small but annoying bug when using a multipath default route for load balancing over multiple links. Apparently, after each route cache flush traffic from existing connections is routed through a different interface (I noticed it because this causes some streaming music connections to cut after 10 minutes playing).

As a workaround, you can set the sysctl value ‘net.ipv4.route.secret_interval=3600‘ (defaults to 600), and ‘net.ipv4.route.gc_elasticity=80‘ (defaults to 8). With these values, the error will only happen once an hour instead of once every 10 minutes, and the garbage collector will not prune routes which are long time on the cache, so it will be more difficult to trigger the problem.

If you run into problems with the route cache flushes, this article has very good information about all the sysctl parameters used in different kernel versions, and how everything works and can be fine tuned.

I tried your script and I am having trouble getting it to work. I have two networks configured but it only appears to be working on one device. For example I can ping a site fine, try again and get nothing, ping another fine, then nothing.

Both of the networks have the same gateway- 192.168.0.1, which I have a feeling may be causing the problem? It looks like when the routing tables are being created, the default gateway is specified by IP only. Should the device be specified here as well?? Any feedback would be great. Thanks. -Jason

You are right, the gateway can’t be the same. You must use different network addresses on each interface: if using 192.168.0.0/24 in one network, you can use 192.168.1.0/24 in the other, or if you prefer, you can do subnetting with 2 hosts using /30 netmasks for peer-to-peer networking.

you need to have separate interfaces for each link, so won’t work if the gateways are on the same network (seen through the same interface). Either use VLANs on the same NIC, or use separate ethernet adapters one for each link.

I ‘m a rookie in Linux networking and I would like to use this script to load-balance between an ADSL connection and a 3G/UMTS one. Which information from ifconfig regarding the two interfaces (eth0, ppp0) am I supposed to fill in order to get the script working? Any help highly appreciated!

read the script comments, it’s all in there, basically you have to fill in the interface name, gateway IP address, network address with netmask (in CIDR notation), and the weight you want to assign to each link.

I’ve got one problem with generic balanser – ftp connections.
My company has 2 WAN links but only one is a Symetric link. I would like to pass all my outgoing and incomming connections to external ftp serwers by eth2 interface where i plug my symetric link. How I Can do this? I have to write iptables rules?

Additional, I woudl like to use generic balanser to route all ftp connections when eth2 link will fail.