lbalance patch
Subsystems (2)
Greetings;
lbalance is a match target intended for load balancing that uses current bandwidth usage (as opposed to randomicity, or round-robin rotation, or number of NAT connections) as the underlying choice criterion. Assuming that eth0 has twice as much bandwidth as eth1:
# Measure downstream bandwidth by accumulating statistics into slots 0 and 1:
iptables -t mangle -A PREROUTING -i eth0 -m lbalance --record 0 --weight 128
iptables -t mangle -A PREROUTING -i eth1 -m lbalance --record 1 --weight 64
# Choose the route with the lowest current usage:
... -m lbalance --test 0 -j ... # slot 0 has the lowest usage, therefore pick eth0
... -m lbalance --test 1 -j ... # likewise for eth1
This example measures downstream bandwidth because the first two rules are placed in mangle/PREROUTING. Upstream bandwidth or joint bandwidth could be measured instead by placing the rules elsewhere. Current statistics are gathered in /proc/net/ipt_lbalance:
host# cat /proc/net/ipt_lbalance
0 153921
1 140291
host#
These values are exponentially decayed byte counts with a 1-second half-life (i.e. roughly equivalent to instantaneous Bps.)
Please let me know if this builds/works for you.
Regards,
Luca Raggi
extensions/.lbalance-test +3 -0
@@ -0,0 +1,3 @@ +#!/bin/sh +# True if lbalance is applied. +[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_lbalance.h ] && echo lbalance
extensions/.lbalance-test~ +1 -1
@@ -1,3 +1,3 @@ #!/bin/sh # True if nth is applied. -[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_nth.h ] && echo nth +[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_lbalance.h ] && echo lbalance
extensions/libipt_lbalance.c +176 -0
176 lines not on this page · raw message
include/linux/netfilter_ipv4/ipt_lbalance.h +28 -0
@@ -0,0 +1,28 @@ +#ifndef _IPT_LBALANCE_H +#define _IPT_LBALANCE_H + +#include <linux/param.h> +#include <linux/types.h> + +#ifndef IPT_LBALANCE_COUNTERS /* max is 255 due to u_int8_t below... */ +#define IPT_LBALANCE_COUNTERS 128 +#endif + +#ifndef IPT_LBALANCE_HALFLIFE +/* halflife in microseconds: */ +#define IPT_LBALANCE_HALFLIFE 1000000 +#endif + +#define IPT_LBALANCE_FUNC_SET 1 +#define IPT_LBALANCE_FUNC_TEST 2 + +#define IPT_DEFAULT_WEIGHT 128 + +struct ipt_lbalance_info { +u_int8_t function; +u_int8_t slot; +u_int8_t weight; +u_int8_t reserved;/* set to zero */ +}; + +#endif /*_IPT_LBALANCE_H*/
net/ipv4/netfilter/ipt_lbalance.c +356 -0
356 lines not on this page · raw message
net/ipv4/netfilter/Config.in +1 -0
@@ -32,6 +32,7 @@ dep_tristate ' LENGTH match support' CONFIG_IP_NF_MATCH_LENGTH $CONFIG_IP_NF_IPTABLES dep_tristate ' TTL match support' CONFIG_IP_NF_MATCH_TTL $CONFIG_IP_NF_IPTABLES dep_tristate ' tcpmss match support' CONFIG_IP_NF_MATCH_TCPMSS $CONFIG_IP_NF_IPTABLES + dep_tristate ' lbalance match support' CONFIG_IP_NF_MATCH_LBALANCE $CONFIG_IP_NF_IPTABLES if [ "$CONFIG_IP_NF_CONNTRACK" != "n" ]; then dep_tristate ' Helper match support' CONFIG_IP_NF_MATCH_HELPER $CONFIG_IP_NF_IPTABLES fi
net/ipv4/netfilter/Makefile +1 -0
@@ -83,6 +83,7 @@ obj-$(CONFIG_IP_NF_MATCH_CONNTRACK) += ipt_conntrack.o obj-$(CONFIG_IP_NF_MATCH_UNCLEAN) += ipt_unclean.o obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o +obj-$(CONFIG_IP_NF_MATCH_LBALANCE) += ipt_lbalance.o # targets obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o