Skip to contentSkip to conversation

netfilter: xtables: add cluster match

PATCH In review Pablo Neira Ayuso 3 participants 4 messages Feb 16, 2009

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 netfilter: xtables: add cluster match

This patch adds the iptables cluster match. This match can be used to deploy gateway and back-end load-sharing clusters.

Assuming that all the nodes see all packets (see below for an example on how to do that if your switch does not allow this), the cluster match decides if this node has to handle a packet given:

	jhash(source IP) % total_nodes == node_id

For related connections, the master conntrack is used. The following is an example of its use to deploy a gateway cluster composed of two nodes (where this is the node 1):

iptables -I PREROUTING -t mangle -i eth1 -m cluster \
	--cluster-total-nodes 2 --cluster-local-node 1 \
	--cluster-proc-name eth1 -j MARK --set-mark 0xffff
iptables -A PREROUTING -t mangle -i eth1 \
	-m mark ! --mark 0xffff -j DROP
iptables -A PREROUTING -t mangle -i eth2 -m cluster \
	--cluster-total-nodes 2 --cluster-local-node 1 \
	--cluster-proc-name eth2 -j MARK --set-mark 0xffff
iptables -A PREROUTING -t mangle -i eth2 \
	-m mark ! --mark 0xffff -j DROP

And the following commands to make all nodes see the same packets:

ip maddr add 01:00:5e:00:01:01 dev eth1
ip maddr add 01:00:5e:00:01:02 dev eth2

arptables -I OUTPUT -o eth1 --h-length 6 \
	-j mangle --mangle-mac-s 01:00:5e:00:01:01
arptables -I INPUT -i eth1 --h-length 6 \
	--destination-mac 01:00:5e:00:01:01 \
	-j mangle --mangle-mac-d 00:zz:yy:xx:5a:27
arptables -I OUTPUT -o eth2 --h-length 6 \
	-j mangle --mangle-mac-s 01:00:5e:00:01:02
arptables -I INPUT -i eth2 --h-length 6 \
	--destination-mac 01:00:5e:00:01:02 \
	-j mangle --mangle-mac-d 00:zz:yy:xx:5a:27

In the case of TCP connections, pickup facility has to be disabled to avoid marking TCP ACK packets coming in the reply direction as valid.

echo 0 > /proc/sys/net/netfilter/nf_conntrack_tcp_loose

The match also provides a /proc entry under:

/proc/sys/net/netfilter/cluster/$PROC_NAME

where PROC_NAME is set via --cluster-proc-name. This is useful to include possible cluster reconfigurations via fail-over scripts. Assuming that this is the node 1, if node 2 is down, you can add node 2 to your node-mask as follows:

echo +2 > /proc/sys/net/netfilter/cluster/$PROC_NAME

BTW, some final notes:

* This match mangles the skbuff pkt_type in case that it detects
PACKET_MULTICAST for a non-multicast address. This may be done in a PKTTYPE target for this sole purpose.
* This match supersedes the CLUSTERIP target.

---

 include/linux/netfilter/xt_cluster.h |   21 ++
 net/netfilter/Kconfig                |   16 +
 net/netfilter/Makefile               |    1 
 net/netfilter/xt_cluster.c           |  368 ++++++++++++++++++++++++++++++++++
 4 files changed, 406 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/netfilter/xt_cluster.h
 create mode 100644 net/netfilter/xt_cluster.c
include/linux/netfilter/xt_cluster.h new file +21 -0
new file mode 100644
index 0000000..2cfc24e
@@ -0,0 +1,21 @@
+#ifndef _XT_CLUSTER_MATCH_H
+#define _XT_CLUSTER_MATCH_H
+
+struct proc_dir_entry;
+
+enum xt_cluster_flags {
+	XT_CLUSTER_F_INV = (1 << 0),
+};
+
+struct xt_cluster_match_info {
+	u_int16_t		total_nodes;
+	u_int16_t		node_id;
+	u_int32_t		hash_seed;
+	char			proc_name[16];
+	u_int32_t		flags;
+
+	/* Used internally by the kernel */
+	void			*data __attribute__((aligned(8)));
+};
+
+#endif /* _XT_CLUSTER_MATCH_H */
net/netfilter/Kconfig +16 -0
index c2bac9c..77b6405 100644
@@ -488,6 +488,22 @@ config NETFILTER_XT_TARGET_TCPOPTSTRIP
 	  This option adds a "TCPOPTSTRIP" target, which allows you to strip
 	  TCP options from TCP packets.
 
+config NETFILTER_XT_MATCH_CLUSTER
+	tristate '"cluster" match support'
+	depends on NF_CONNTRACK
+	depends on NETFILTER_ADVANCED
+	---help---
+	  This option allows you to build work-load-sharing clusters of
+	  network servers/stateful firewalls without having a dedicated
+	  load-balancing router/server/switch. Basically, this match returns
+	  true when the packet must be handled by this cluster node. Thus,
+	  all nodes see all packets and this match decides which node handles
+	  what packets. The work-load sharing algorithm is based on source
+	  address hashing.
+
+	  If you say Y or M here, try `iptables -m cluster --help` for
+	  more information.
+
 config NETFILTER_XT_MATCH_COMMENT
 	tristate  '"comment" match support'
 	depends on NETFILTER_ADVANCED
net/netfilter/Makefile +1 -0
index da3d909..960399a 100644
@@ -57,6 +57,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP) += xt_TCPOPTSTRIP.o
 obj-$(CONFIG_NETFILTER_XT_TARGET_TRACE) += xt_TRACE.o
 
 # matches
+obj-$(CONFIG_NETFILTER_XT_MATCH_CLUSTER) += xt_cluster.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_CONNBYTES) += xt_connbytes.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_CONNLIMIT) += xt_connlimit.o
net/netfilter/xt_cluster.c new file +368 -0

368 lines not on this page · raw message

author maintainer
#1 lore raw

net/netfilter/xt_cluster.c line 160

Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/xt_cluster.c new file +4 -0
+static int xt_cluster_seq_show(struct seq_file *s, void *v)
+{
+	unsigned long *mask = v;
+	seq_printf(s, "0x%8lx\n", *mask);
                         ^^^

Damn, this needs the dot before the 8 to fill with zero the empty spaces. Jan's suggestion was wrong and I forgot to check this. I'll send the patch again.

pablo@debian:~$ sudo cat /proc/net/netfilter/cluster/eth2
0x       1
Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers
#2 lore raw
Quoted 10 lines · Pablo Neira Ayuso

net/netfilter/xt_cluster.c line 160

Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/xt_cluster.c new file +4 -0
+static int xt_cluster_seq_show(struct seq_file *s, void *v)
+{
+	unsigned long *mask = v;
+	seq_printf(s, "0x%8lx\n", *mask);
                        ^^^

Damn, this needs the dot before the 8 to fill with zero the empty spaces. Jan's suggestion was wrong and I forgot to check this. I'll send the patch again.

Wait wait, let me requote myself:

Quoted 2 lines

'.' does not make sense with non-string,non-floating point numbers (though it is a stdc feature it seems). I'd say "0x%08lx", for clarity.

The second '0' in "0x%08lx" is missing.

#3 lore raw
Quoted 13 lines · Jan Engelhardt
Quoted 10 lines · Pablo Neira Ayuso

net/netfilter/xt_cluster.c line 160

Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/xt_cluster.c new file +4 -0
+static int xt_cluster_seq_show(struct seq_file *s, void *v)
+{
+	unsigned long *mask = v;
+	seq_printf(s, "0x%8lx\n", *mask);
                        ^^^

Damn, this needs the dot before the 8 to fill with zero the empty spaces. Jan's suggestion was wrong and I forgot to check this. I'll send the patch again.

Quoted 5 lines

Wait wait, let me requote myself:

Quoted 1 line

'.' does not make sense with non-string,non-floating point numbers (though it is a stdc feature it seems). I'd say "0x%08lx", for clarity.

The second '0' in "0x%08lx" is missing.

Lets discuss the need for this interface first before fixing it :) It looks like a way to get around using (potentially slow) rule replacement, which doesn't make sense to add on a per-module base.