0

The following is my network topology:

+                                +
 |                                |
 |                                |
 |                                |
 |     +------------------+       |
 |     |                  |       |
 +-----+    firewall      +-------+
  eth0 |                  | eth1
       +--------+---------+
                | eth4
                |
                |
          +-----+---------+
          |               |
+---------+   switch      +---+
|         |               |   |
|         +-+-+-+-+-+-+-+-+   |
|         | | | | | | | |     |
+         + + + + + + + +     +

              10 Vlans

I use extension statistic and and connection mark to load balancing for LAN network. But module mark and statistic are not work well. My iptables as the following:

#!/bin/sh
#
# delete all existing rules.
#
IPT='/sbin/iptables'

LAN_IF='eth4'

WAN_IF='eth0'
OPT_IF='eth1'
LAN_NET='192.168.10.0/24'


VLAN1_NET='192.168.101.0/24'
VLAN2_NET='192.168.102.0/24'
VLAN3_NET='192.168.103.0/24'
VLAN4_NET='192.168.104.0/24'
VLAN5_NET='192.168.105.0/24'
VLAN6_NET='192.168.106.0/24'
VLAN7_NET='192.168.107.0/24'



$IPT -F
$IPT -t nat -F
$IPT -t mangle -F
$IPT -X

#$IPT -A INPUT -j LOG --log-level 4 --log-prefix 'NETFILTER'
#$IPT -A OUTPUT -j LOG --log-level 4 --log-prefix 'NETFILTER'
$IPT -A FORWARD -j LOG --log-level 4 --log-prefix 'NETFILTER '


$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP


# Always accept loopback traffic
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT




# Allow for lan net
$IPT -A OUTPUT -o $LAN_IF -j ACCEPT
$IPT -A INPUT -i $LAN_IF -j ACCEPT

# Allow from local to internet
$IPT -A OUTPUT -o $WAN_IF -j ACCEPT
$IPT -A OUTPUT -o $OPT_IF -j ACCEPT

# Allow established connections, and those not coming from the outside
$IPT -A INPUT -s $LAN_NET -p icmp -j ACCEPT
$IPT -A OUTPUT -s $LAN_NET -p icmp -j ACCEPT


$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -m state --state NEW -i $LAN_IF -j ACCEPT

# Allow forward both WANT and OPT
$IPT -A FORWARD -i $WAN_IF -o $LAN_IF -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A FORWARD -i $OPT_IF -o $LAN_IF -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow outgoing connections from the LAN side.
$IPT -A FORWARD -s $LAN_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN7_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN6_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN5_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN4_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN3_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN2_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN1_NET -o $WAN_IF -j ACCEPT

#$IPT -A FORWARD -s $LAN_NET -o $OPT_IF -j ACCEPT
# Allow outgoing connections from the LAN side.
$IPT -A FORWARD -s $LAN_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN5_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN6_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN7_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN4_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN3_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN2_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN1_NET -o $OPT_IF -j ACCEPT




$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN1_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN1_NET -j ACCEPT

$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN2_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN2_NET -j ACCEPT

$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN3_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN3_NET -j ACCEPT

$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN4_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN4_NET -j ACCEPT

$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN5_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN5_NET -j ACCEPT

$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN6_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN6_NET -j ACCEPT

$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN7_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN7_NET -j ACCEPT



# Masquerade.
$IPT -t nat -A POSTROUTING -o $WAN_IF -j MASQUERADE
$IPT -t nat -A POSTROUTING -o $OPT_IF -j MASQUERADE



# load balancing
$IPT -A PREROUTING -t mangle -j CONNMARK --restore-mark
$IPT -A PREROUTING -t mangle -m mark ! --mark 0 -j ACCEPT

$IPT -A PREROUTING -p icmp -t mangle -m statistic --mode nth --every 2 --packet 0 -j MARK --set-mark 2
$IPT -A PREROUTING -p icmp -t mangle -m statistic --mode nth --every 2 --packet 1 -j MARK --set-mark 3
$IPT -A PREROUTING -t mangle -j CONNMARK --save-mark


# Enable routing.
echo 1 > /proc/sys/net/ipv4/ip_forward

I debug by command: cat /var/log/messages |grep 0x2 |wc -l and cat /var/log/messages |grep 0x3 |wc -l

But the number of packets marked 0x2 and packets marked 0x3 are not balance. Why does it happen ?

  • How many do you get of each to get a better understanding of your problem? – Julie Pelletier May 7 '16 at 2:16
0

The load balancing part of your script says :

  • If I already know this connection, just let it go the same way than previously
  • If I don't, send it half of the time on one interface, half of the time on the other one

So you have an equal number of connection going on each interface.

But some connections only have 3 packets, when other ones have 1000, the packet count will not be equal. Furthermore, if you check the number of opened connections, it might be inequal, because some connections last longer.

In order to load balance packets, you would have to delete these lines :

$IPT -A PREROUTING -t mangle -j CONNMARK --restore-mark
$IPT -A PREROUTING -t mangle -m mark ! --mark 0 -j ACCEPT
$IPT -A PREROUTING -t mangle -j CONNMARK --save-mark

It would be a terrible idea though, since your connections would have different ip source and half of the packet wouldn't hit destination services because of asymmetric routing. And balance packet doesn't balance bits send since. Even balance bits send doesn't balance bits received.

In my opinion, you should just let your script as it is now, and the more traffic there is, the more balanced your links will be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.