Home > FreeBSD > PF: Load Balancing

PF: Load Balancing

December 11, 2009 Leave a comment Go to comments

The following example balances outgoing traffic across two Internet

connections:

lan_net = “192.168.0.0/24”
int_if  = “dc0”
ext_if1 = “fxp0”
ext_if2 = “fxp1”
ext_gw1 = “68.146.224.1”
ext_gw2 = “142.59.76.1”

pass in on $int_if route-to
{ ($ext_if1 $ext_gw1), ($ext_if2 $ext_gw2) } round-robin
from $lan_net to any keep state

The route-to option is used on traffic coming in on the
internal interface to specify the outgoing network interfaces
that traffic will be balanced across along with their respective
gateways. Note that the route-to option must be present on
each filter rule that traffic is to be balanced for. Return
packets will be routed back to the same external interface that they
exited (this is done by the ISPs) and will be routed back to the
internal network normally.

To ensure that packets with a source address belonging to
$ext_if1 are always routed to $ext_gw1 (and similarly
for $ext_if2 and $ext_gw2), the following two lines
should be included in the ruleset:

pass out on $ext_if1 route-to ($ext_if2 $ext_gw2) from $ext_if2
to any
pass out on $ext_if2 route-to ($ext_if1 $ext_gw1) from $ext_if1
to any

Finally, NAT can also be used on each outgoing interface:

nat on $ext_if1 from $lan_net to any -> ($ext_if1)
nat on $ext_if2 from $lan_net to any -> ($ext_if2)

A complete example that load balances outgoing traffic might look
something like this:

lan_net = “192.168.0.0/24”
int_if = “dc0”
ext_if1 = “fxp0”
ext_if2 = “fxp1”
ext_gw1 = “68.146.224.1”
ext_gw2 = “142.59.76.1”

# nat outgoing connections on each internet interface
nat on $ext_if1 from $lan_net to any -> ($ext_if1)
nat on $ext_if2 from $lan_net to any -> ($ext_if2)

# default deny
block in from any to any
block out from any to any

# pass all outgoing packets on internal interface
pass out on $int_if from any to $lan_net
# pass in quick any packets destined for the gateway itself
pass in quick on $int_if from $lan_net to $int_if
# load balance outgoing tcp traffic from internal network.
pass in on $int_if route-to
{ ($ext_if1 $ext_gw1), ($ext_if2 $ext_gw2) } round-robin
proto tcp from $lan_net to any flags S/SA modulate state
# load balance outgoing udp and icmp traffic from internal network
pass in on $int_if route-to
{ ($ext_if1 $ext_gw1), ($ext_if2 $ext_gw2) } round-robin
proto { udp, icmp } from $lan_net to any keep state

# general “pass out” rules for external interfaces
pass out on $ext_if1 proto tcp from any to any flags S/SA modulate state
pass out on $ext_if1 proto { udp, icmp } from any to any keep state
pass out on $ext_if2 proto tcp from any to any flags S/SA modulate state
pass out on $ext_if2 proto { udp, icmp } from any to any keep state

# route packets from any IPs on $ext_if1 to $ext_gw1 and the same for
# $ext_if2 and $ext_gw2
pass out on $ext_if1 route-to ($ext_if2 $ext_gw2) from $ext_if2 to any
pass out on $ext_if2 route-to ($ext_if1 $ext_gw1) from $ext_if1 to any

Advertisements
Categories: FreeBSD
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: