Difference between revisions of "Source based routing"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
m (Robot: Automated text replacement (-VE +container))
m (Added some info that helped me recently)
Line 3: Line 3:
 
<pre>
 
<pre>
 
# /sbin/ip rule add from $IP table $TBL
 
# /sbin/ip rule add from $IP table $TBL
# /sbin/ip route add default dev eth0 via $GW table $TBL
+
# /sbin/ip route add default dev $ETH via $GW table $TBL
 +
# /sbin/ip route add $NET dev $ETH table $TBL
 +
 
 
</pre>
 
</pre>
  
where  
+
where
 
* <code>$IP</code> is an IP address which should use non-default gateway. You can also use netmask here, e.g. <code>10.9.8.0/24</code> will mean that all containers with addresses like 10.9.8.x will be routed through the given gateway.
 
* <code>$IP</code> is an IP address which should use non-default gateway. You can also use netmask here, e.g. <code>10.9.8.0/24</code> will mean that all containers with addresses like 10.9.8.x will be routed through the given gateway.
 +
* <code>$NET</code> is an IP network which should be routed straight, that is, not through the gateway. E.g. <code>10.9.8.0/24</code>.
 +
* <code>$ETH</code> is the network interface to route to, e.g. <code>eth0</code>.
 
* <code>$GW</code> is a gateway for this IP address.
 
* <code>$GW</code> is a gateway for this IP address.
 
* <code>$TBL</code> is any free table number, I use numbers from 6 upwards. Note that tables 250-255 are usually reserved (see <code>/etc/iproute2/rt_tables</code>).
 
* <code>$TBL</code> is any free table number, I use numbers from 6 upwards. Note that tables 250-255 are usually reserved (see <code>/etc/iproute2/rt_tables</code>).

Revision as of 12:01, 16 April 2008

Sometimes you have more than one router in your network, and want different containers to use different routers. For that, you need to set up source-based routing on the host system. For example:

# /sbin/ip rule add from $IP table $TBL
# /sbin/ip route add default dev $ETH via $GW table $TBL
# /sbin/ip route add $NET dev $ETH table $TBL

where

  • $IP is an IP address which should use non-default gateway. You can also use netmask here, e.g. 10.9.8.0/24 will mean that all containers with addresses like 10.9.8.x will be routed through the given gateway.
  • $NET is an IP network which should be routed straight, that is, not through the gateway. E.g. 10.9.8.0/24.
  • $ETH is the network interface to route to, e.g. eth0.
  • $GW is a gateway for this IP address.
  • $TBL is any free table number, I use numbers from 6 upwards. Note that tables 250-255 are usually reserved (see /etc/iproute2/rt_tables).

Note that every new non-standard gateway will require another table number.

For more details on routing rules, see man ip.

See also