Difference between revisions of "Routing Tricks"

From SlackWiki
Jump to: navigation, search
(CREATED THIS PAGE)
 
m (Weighted Routing: FIXED the screwed up link. And added the category)
 
Line 78: Line 78:
 
If you call the script without any parameters, it tries to balance eth0 and ppp0 (because this works in my case ).
 
If you call the script without any parameters, it tries to balance eth0 and ppp0 (because this works in my case ).
  
[http://www.linuxquestions.org/questions/slackware-14/script-to-load-balance-two-isps-with-ip-route-and-ip-rules-672602 HERE IS HIS ORIGINAL POSTING]
+
[http://www.linuxquestions.org/questions/slackware-14/script-to-load-balance-two-isps-with-ip-route-and-ip-rules-672602/ HERE IS HIS ORIGINAL POSTING]
  
 
(ALSO, I copied a copy of his permission on my DISCUSSION page)
 
(ALSO, I copied a copy of his permission on my DISCUSSION page)
 +
 +
[[Category:Tips]]

Latest revision as of 01:34, 9 January 2010

Weighted Routing

(aka Load Balancing, Net Balancing)

What is Weighted Routing Simply put, distributing network traffic over multiple paths based on load.

EXAMPLE 1: You have two DSL connections incoming and you distribute your connection traffic between them.

EXAMPLE 2: You have a 1.5Mbs DSL connection and a 3MBs cable connection, therefore you set up your routing to send 1/3 of the traffic out the DSL connection and 2/3 out the cable connection.


How do I do it?

Niels Horn has written a slick little script to do the weighted routing for you. He has given me permission to copy it here.


#!/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(at symbol)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 ).

HERE IS HIS ORIGINAL POSTING

(ALSO, I copied a copy of his permission on my DISCUSSION page)