<HTML ><HEAD ><TITLE >Routing for multiple uplinks/providers</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Linux Advanced Routing & Traffic Control HOWTO" HREF="index.html"><LINK REL="UP" TITLE="Rules - routing policy database" HREF="lartc.rpdb.html"><LINK REL="PREVIOUS" TITLE="Simple source policy routing" HREF="lartc.rpdb.simple.html"><LINK REL="NEXT" TITLE="GRE and other tunnels" HREF="lartc.tunnel.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Linux Advanced Routing & Traffic Control HOWTO</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="lartc.rpdb.simple.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 4. Rules - routing policy database</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="lartc.tunnel.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="LARTC.RPDB.MULTIPLE-LINKS" ></A >4.2. Routing for multiple uplinks/providers</H1 ><P >A common configuration is the following, in which there are two providers that connect a local network (or even a single machine) to the big Internet. <TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" > ________ +------------+ / | | | +-------------+ Provider 1 +------- __ | | | / ___/ \_ +------+-------+ +------------+ | _/ \__ | if1 | / / \ | | | | Local network -----+ Linux router | | Internet \_ __/ | | | \__ __/ | if2 | \ \___/ +------+-------+ +------------+ | | | | \ +-------------+ Provider 2 +------- | | | +------------+ \________</PRE ></FONT ></TD ></TR ></TABLE > There are usually two questions given this setup. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN258" ></A >4.2.1. Split access</H2 ><P > The first is how to route answers to packets coming in over a particular provider, say Provider 1, back out again over that same provider. </P ><P > Let us first set some symbolical names. Let <B CLASS="COMMAND" >$IF1</B > be the name of the first interface (if1 in the picture above) and <B CLASS="COMMAND" >$IF2</B > the name of the second interface. Then let <B CLASS="COMMAND" >$IP1</B > be the IP address associated with <B CLASS="COMMAND" >$IF1</B > and <B CLASS="COMMAND" >$IP2</B > the IP address associated with <B CLASS="COMMAND" >$IF2</B >. Next, let <B CLASS="COMMAND" >$P1</B > be the IP address of the gateway at Provider 1, and <B CLASS="COMMAND" >$P2</B > the IP address of the gateway at provider 2. Finally, let <B CLASS="COMMAND" >$P1_NET</B > be the IP network <B CLASS="COMMAND" >$P1</B > is in, and <B CLASS="COMMAND" >$P2_NET</B > the IP network <B CLASS="COMMAND" >$P2</B > is in. </P ><P > One creates two additional routing tables, say <B CLASS="COMMAND" >T1</B > and <B CLASS="COMMAND" >T2</B >. These are added in /etc/iproute2/rt_tables. Then you set up routing in these tables as follows: </P ><P > <TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" > ip route add $P1_NET dev $IF1 src $IP1 table T1 ip route add default via $P1 table T1 ip route add $P2_NET dev $IF2 src $IP2 table T2 ip route add default via $P2 table T2 </PRE ></FONT ></TD ></TR ></TABLE > Nothing spectacular, just build a route to the gateway and build a default route via that gateway, as you would do in the case of a single upstream provider, but put the routes in a separate table per provider. Note that the network route suffices, as it tells you how to find any host in that network, which includes the gateway, as specified above. </P ><P > Next you set up the main routing table. It is a good idea to route things to the direct neighbour through the interface connected to that neighbour. Note the `src' arguments, they make sure the right outgoing IP address is chosen. <TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" > ip route add $P1_NET dev $IF1 src $IP1 ip route add $P2_NET dev $IF2 src $IP2 </PRE ></FONT ></TD ></TR ></TABLE > Then, your preference for default route: <TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" > ip route add default via $P1 </PRE ></FONT ></TD ></TR ></TABLE > Next, you set up the routing rules. These actually choose what routing table to route with. You want to make sure that you route out a given interface if you already have the corresponding source address: <TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" > ip rule add from $IP1 table T1 ip rule add from $IP2 table T2 </PRE ></FONT ></TD ></TR ></TABLE > This set of commands makes sure all answers to traffic coming in on a particular interface get answered from that interface. </P ><P > Now, this is just the very basic setup. It will work for all processes running on the router itself, and for the local network, if it is masqueraded. If it is not, then you either have IP space from both providers or you are going to want to masquerade to one of the two providers. In both cases you will want to add rules selecting which provider to route out from based on the IP address of the machine in the local network. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN284" ></A >4.2.2. Load balancing</H2 ><P > The second question is how to balance traffic going out over the two providers. This is actually not hard if you already have set up split access as above. </P ><P > Instead of choosing one of the two providers as your default route, you now set up the default route to be a multipath route. In the default kernel this will balance routes over the two providers. It is done as follows (once more building on the example in the section on split-access): <TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" > ip route add default scope global nexthop via $P1 dev $IF1 weight 1 \ nexthop via $P2 dev $IF2 weight 1 </PRE ></FONT ></TD ></TR ></TABLE > This will balance the routes over both providers. The <B CLASS="COMMAND" >weight</B > parameters can be tweaked to favor one provider over the other. </P ><P > Note that balancing will not be perfect, as it is route based, and routes are cached. This means that routes to often-used sites will always be over the same provider. </P ><P > Furthermore, if you really want to do this, you probably also want to look at Julian Anastasov's patches at <A HREF="http://www.linuxvirtualserver.org/~julian/#routes" TARGET="_top" >http://www.linuxvirtualserver.org/~julian/#routes </A >, Julian's route patch page. They will make things nicer to work with. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="lartc.rpdb.simple.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="lartc.tunnel.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Simple source policy routing</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="lartc.rpdb.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >GRE and other tunnels</TD ></TR ></TABLE ></DIV ></BODY ></HTML >