LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 09-26-2008, 06:51 PM   #1
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Rep: Reputation: 90
Lightbulb Script to Load Balance two ISPs with ip route & ip rules


Hi to all,

A week ago today I received a GSM usb-modem for my laptop which I use during the day at work.
With a colleague (also Slackware-fan) I discussed the possibility to use the usb-modem at home in combination with my adsl-modem and combine the two bandwidths.

My setup is the following:
1) Local network with:
- Desktop running Slackware 12.1-current
- Laptop running Slackware 12.1-current
- Some other desktops varying from Slackware 4.0 to 12.1
- Huawei MT880 adsl modem/router with a 1Mbps downstream / 320kbps upstream line

2) ZTE622 GSM usb modem reaching about 1.7Mbps downstream / 350kbps upstream where I live that I configured both on my laptop and primary desktop. (*)

(*) The modem is not fully supported by the 2.6.24 kernels, but there is a patch to option.c to make it work 100% at full speed.

After some research I found several solutions using iptables, but my desktop is not a router so I wasn't able to use the 'PREROUTING' chain to mangle packets as most sites suggest.

Messing around a bit with 'ip route' and 'ip rule' I managed to write a script that creates two new routing tables and defines the rules to alternate between the two ISPs.
It is not the most elegant solution, as routes are cached so once you have a connection to one destination, it will continue to use the same route (and thus the same ISP).
So, for FTP / HTTP downloads of large files there is no improvement other than using the other ISP for other traffic while you're downloading.

But if you use bittorrent (or KTorrent, etc.) you will have several connections at the same time and will notice a substantial gain in download speed.

Most information I found on this site: Linux Advanced Routing & Traffic Control

For those interested, this is my script:
Code:
#!/bin/bash
#
# bal_local		Load-balance internet connection over two local links
#
# Version:		1.0.0 - Fri, Sep 26, 2008
#
# Author:		Niels Horn <niels.horn@gmail.com>
#

# Set devices:
DEV1=${1-eth0}	# default eth0
DEV2=${2-ppp0}	# default ppp0

# Get IP addresses of our devices:
ip1=`ifconfig $DEV1 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
ip2=`ifconfig $DEV2 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`

# Get default gateway for our devices:
gw1=`route -n | grep $DEV1 | grep '^0.0.0.0' | awk '{ print $2 }'`
gw2=`route -n | grep $DEV2 | grep '^0.0.0.0' | awk '{ print $2 }'`

echo "$DEV1: IP=$ip1 GW=$gw1"
echo "$DEV2: IP=$ip2 GW=$gw2"

### Definition of routes ###

# Check if tables exists, if not -> create them:
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
	echo "251	rt_dev1" >> /etc/iproute2/rt_tables
fi
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
	echo "252	rt_dev2" >> /etc/iproute2/rt_tables
fi

# Define routing tables:
ip route add default via $gw1 table rt_dev1
ip route add default via $gw2 table rt_dev2

# Create rules:
ip rule add from $ip1 table rt_dev1
ip rule add from $ip2 table rt_dev2

# If we already have a 'nexthop' route, delete it:
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
	ip route del default scope global
fi

# Balance links based on routes:
ip route add default scope global nexthop via $gw1 dev $DEV1 weight 1 nexthop via $gw2 dev $DEV2 weight 1

# Flush cache table:
ip route flush cache

# All done...
To use the script, copy it to /usr/local/bin, make it executable with 'chmod +x' and call it with:
Code:
bal_local <dev1> <dev2>
filling in <dev1> and <dev2> with your network-devices.
If you call the script without any parameters, it tries to balance eth0 and ppp0 (because this works in my case )

As a test I started downloading one of the Slackware 12.1 CDs and reached a total download speed of over 250KB/sec with BitTorrent, using about 50 connections simultaneously.

I know there are things that can / should be improved in the script, but, hey, it works for now!

Happy Slacking,

Niels
 
Old 09-28-2008, 04:22 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
good info, maybe you'd rather write this up as a LinuxAnswer?
 
Old 09-28-2008, 10:03 AM   #3
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Original Poster
Rep: Reputation: 90
Thanks, I just submitted the article!

Niels
 
Old 01-07-2010, 06:31 AM   #4
muthubalajirm
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
re:Script to Load Balance two ISPs with ip route & ip rules

hi,
I am intrested to know whether the packets transfer through both the interfaces were even or nearly the same ,say balanced.If you could do iptraf and check the general statistics and reply back would be great help.
Thanks and regards
Balaji rm
 
Old 01-07-2010, 08:35 AM   #5
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Original Poster
Rep: Reputation: 90
@muthubalajirm:

Welcome to LinuxQuestions.org!

I'm not using the dual-link setup every day, but the packages are equally balanced between the two links.

Regards,
 
Old 01-09-2010, 02:39 AM   #6
muthubalajirm
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
thanks for your reply.
I am now intrested to load balance equally the traffic through two interfaces because the load balance is realised for large file downloads only and want to find a way to do away with iproute cache.It would be a great help if you could help me out to find a way where in i can achieve it.
Thanks and regards
balaji rm
 
Old 01-09-2010, 04:51 AM   #7
Mark Pettit
Member
 
Registered: Dec 2008
Location: Cape Town, South Africa
Distribution: Slackware 14.2 64 Multi-Lib
Posts: 573

Rep: Reputation: 265Reputation: 265Reputation: 265
@niels.horn : Down here at the bottom end of Africa, we have the rather sad situation where local traffic (ie South African) is fairly cheap, while international (ie via undersea cable to Eu/Us) is expensive. We have weird ISP packages which are capped to say 5 GB and then you get moved into an area where you get 30 GB local, but zero international. So we end up taking out 2 packages and then manually change the ADSL router when you know you're doing one and not the other. But that's a real fag.

Seems to me that your script could somehow be modified to run 2 RP-PPPOE sessions to a bridged ADSL router, and then using some routing rules switch packets according to local and international. You seem to be skilled with networking and switching - do you have any suggestions for mod'ing your script to make that happen ?
 
Old 01-09-2010, 06:07 AM   #8
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Original Poster
Rep: Reputation: 90
@Mark Pettit: It definitely can be done, but unfortunately I do not have the time at the moment to create a script for you.

You can switch gateways based on the destination IP / network, after discovering if this IP is national or international.
It shouldn't be too difficult to discover the range of IP addresses for South Africa (I found this site) and then make some rules for the routing table.

Take a look at the Linux Advanced Routing & Traffic Control site I mentioned in the original post for some instructions on how to do that.
 
Old 01-09-2010, 01:31 PM   #9
Mark Pettit
Member
 
Registered: Dec 2008
Location: Cape Town, South Africa
Distribution: Slackware 14.2 64 Multi-Lib
Posts: 573

Rep: Reputation: 265Reputation: 265Reputation: 265
Hi Niels. Sorry you got the impression I was asking you to write a script. The suggestions I asked for were probably forthcoming in your answer - especially the link to the LAR&TC - which seems quite complete, altho' pretty advanced. It will take a chunk of reading and experimentation to wade thru' it ;-) Thanx.
 
Old 01-09-2010, 02:30 PM   #10
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Original Poster
Rep: Reputation: 90
@Mark Pettit: No worries I actually like doing these things, but I'm too caught up with other tasks at the moment.

When you get a script working, I'd like to see the result!
 
Old 01-10-2010, 09:08 PM   #11
muthubalajirm
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
thanks for your reply.
I am now intrested to load balance equally the traffic through two interfaces because the load balance is realised for large file downloads only and want to find a way to do away with iproute cache.It would be a great help if you could help me out to find a way where in i can achieve it.
Thanks and regards
balaji rm
 
Old 01-02-2014, 11:49 PM   #12
lstein
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
Kernel patches needed for load balancing

Just a note that this solution works fine for a single machine, but if you are using load balancing on a gateway/router that is NAT-ing multiple machines, then you will need to build a custom kernel using the patches located at http://www.ssi.bg/~ja/
 
Old 08-07-2015, 02:13 AM   #13
atux_null
Member
 
Registered: Mar 2015
Distribution: debian 10, ubuntu 20.04, centos 7 & 8
Posts: 63

Rep: Reputation: Disabled
how do you make the NAT in that script?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Load Balance NFS possible? Swakoo Linux - General 3 04-19-2011 09:08 AM
ip route + ip tables + 2 lan ip's + 2 isps only 1 issue! exodist Linux - Networking 1 04-13-2008 10:59 PM
Need to work out rewrite rules with proxy-balance mago Linux - Server 0 03-10-2008 07:56 PM
squid load balance ghabsh Linux - Server 1 03-24-2007 03:45 AM
CPU Load Balance - Who knows? davi_cabral Linux - General 3 01-02-2006 04:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 05:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration