Loadbalance traffic over multiple ADSL lines

##(c) Pieter E Smit 2013 - GPL3.
##(c) 2015 Add notes on creation of routing tables.
#Note1: create tables e.g. ppp1, ppp2 in /etc/iproute2/rt_tables
#Note2: define pppX interface number with "unit X" option in ppp config. 
##Routing
ipt=iptables
prio=1
#Set connmark depending on dsl interface used.
for i in {1..5}; do
  int="ppp$i"
  mark="$i"
  $ipt -t mangle -I POSTROUTING 1 -o $int -m state --state NEW -j CONNMARK --set-mark $mark
  $ipt -t mangle -I PREROUTING 1 -i $int -m state --state NEW -j CONNMARK --set-mark $mark
  $ipt -t mangle -I INPUT      1 -i $int -m state --state NEW -j CONNMARK --set-mark $mark
  #Update route
  #Also done in ppp ip-up script as we cant add route if int does not exist.
  ip route replace default dev $int table $int 2> /dev/null

  ##THIS is LOCAL lan route## Update to local subnet.
  ip route replace 192.168.0.0/16 dev eth0 table $int

  #set routing rule to pick routing table depending on fwmark, table name=$int see /etc/iproute2/rt_tables
  ip rule del fwmark $mark table $int 2> /dev/null
  ip rule add fwmark $mark table $int prio $prio
  #

done
  $ipt -t mangle -I PREROUTING 1 -j CONNMARK --restore-mark

#Debug command
#   iptables -L -nv -t mangle

#(c)Pieter E Smit 2013 GPL3
#adds all ppp interfaces to default route.
command="ip route replace default scope global" 
for i in `ifconfig | grep ppp | cut  -d " " -f 1` ;  do
  command="$command nexthop dev $i weight 1" ;
  #Add default route to each ppp interface's own routing table. /etc/iproute2/rt_table
  ip route replace default dev $i table $i
  done 

$command

# crontab -e 
10  */1 * * * if /sbin/ifconfig ppp1 > /dev/null; then true ; else /sbin/ifup --force ppp1 ; fi
20  */1 * * * if /sbin/ifconfig ppp2 > /dev/null; then true ; else /sbin/ifup --force ppp2 ; fi
30  */1 * * * if /sbin/ifconfig ppp3 > /dev/null; then true ; else /sbin/ifup --force ppp3 ; fi
40  */1 * * * if /sbin/ifconfig ppp4 > /dev/null; then true ; else /sbin/ifup --force ppp4 ; fi
50  */1 * * * if /sbin/ifconfig ppp5 > /dev/null; then true ; else /sbin/ifup --force ppp5 ; fi

...

linux/LoadBalanceMultipleADSLlines (last edited 2015-07-31 11:33:00 by PieterSmit)