Skip to contentSkip to conversation

conntrackd: add ip netns test script

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Dec 24, 2020 – Dec 27, 2020 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 conntrackd: add ip netns test script

This patch adds a script that creates a ip netns testbed. The network topology looks like this:

          veth0---veth0
 host     nsr1     ns2
 veth0----veth0
 ns1      veth2
            |
          veth0
          nsr2

* ns1 and ns2 are clients to generate traffic
* nsr1 and nsr2 run conntrackd to synchronize states
* nsr1 is the primary gateway
- veth2 is used to synchronize states
* nsr2 is the backup gateway
- veth0 is used to synchronize states

To set up the testbed:

% sudo ./conntrackd-netns-test.sh start

To test your testbed works, from ns2:

% sudo ip netns exec ns2 nc -l -p 8080

From ns1:

	% sudo ip netns exec ns1 nc -vvv 10.0.1.2 8080

From nsr1:

% sudo ip netns exec nsr1 conntrackd -s -C conntrackd-nsr1.conf cache internal:
current active connections: 1
[...]
cache external:
current active connections: 0

From nsr2:

% sudo ip netns exec nsr1 conntrackd -s -C conntrackd-nsr2.conf cache internal:
current active connections: 0
[...]
cache external:
current active connections: 1

To stop it:

% sudo ./conntrackd-netns-test.sh stop

---
 .../conntrackd/netns/conntrackd-netns-test.sh |  85 ++++
 tests/conntrackd/netns/conntrackd-nsr1.conf   | 413 ++++++++++++++++++
 tests/conntrackd/netns/conntrackd-nsr2.conf   | 413 ++++++++++++++++++
 tests/conntrackd/netns/ruleset-nsr1.nft       |   6 +
 4 files changed, 917 insertions(+)
 create mode 100755 tests/conntrackd/netns/conntrackd-netns-test.sh
 create mode 100644 tests/conntrackd/netns/conntrackd-nsr1.conf
 create mode 100644 tests/conntrackd/netns/conntrackd-nsr2.conf
 create mode 100644 tests/conntrackd/netns/ruleset-nsr1.nft
tests/conntrackd/netns/conntrackd-netns-test.sh new file +85 -0
new file mode 100755
index 000000000000..a0885c6437e5
@@ -0,0 +1,85 @@
+#!/bin/bash
+
+#
+# testbed network topology
+#
+#          veth0---veth0
+# host     nsr1     ns2
+# veth0----veth0
+# ns1      veth2
+#            |
+#          veth0
+#          nsr2
+#
+# ns1 and ns2 are clients to generate traffic
+# nsr1 and nsr2 run conntrackd to synchronize states
+# nsr1 is the primary gateway
+# - veth2 is used to synchronize states
+# nsr2 is the backup gateway
+# - veth0 is used to synchronize states
+#
+
+if [ $UID -ne 0 ]
+then
+	echo "You must be root to run this test script"
+	exit 0
+fi
+
+start () {
+	ip netns add ns1
+	ip netns add ns2
+	ip netns add nsr1
+	ip netns add nsr2
+
+	ip link add veth0 netns ns1 type veth peer name veth1 netns nsr1
+	ip link add veth0 netns nsr1 type veth peer name veth0 netns ns2
+	ip link add veth2 netns nsr1 type veth peer name veth0 netns nsr2
+
+	ip -net ns1 addr add 192.168.10.2/24 dev veth0
+	ip -net ns1 link set up dev veth0
+	ip -net ns1 ro add 10.0.1.0/24 via 192.168.10.1 dev veth0
+
+	ip -net nsr1 addr add 10.0.1.1/24 dev veth0
+	ip -net nsr1 addr add 192.168.10.1/24 dev veth1
+	ip -net nsr1 link set up dev veth0
+	ip -net nsr1 link set up dev veth1
+	ip -net nsr1 route add default via 192.168.10.2
+	ip netns exec nsr1 sysctl net.ipv4.ip_forward=1
+
+	ip -net nsr1 addr add 192.168.100.2/24 dev veth2
+	ip -net nsr1 link set up dev veth2
+	ip -net nsr2 addr add 192.168.100.3/24 dev veth0
+	ip -net nsr2 link set up dev veth0
+
+	ip -net ns2 addr add 10.0.1.2/24 dev veth0
+	ip -net ns2 link set up dev veth0
+	ip -net ns2 route add default via 10.0.1.1
+
+	echo 1 > /proc/sys/net/netfilter/nf_log_all_netns
+
+	ip netns exec nsr1 nft -f ruleset-nsr1.nft
+	ip netns exec nsr1 conntrackd -C conntrackd-nsr1.conf -d
+	ip netns exec nsr2 conntrackd -C conntrackd-nsr2.conf -d
+}
+
+stop () {
+	ip netns del ns1
+	ip netns del ns2
+	ip netns del nsr1
+	ip netns del nsr2
+	killall -15 conntrackd
+}
+
+case $1 in
+start)
+	start
+	;;
+stop)
+	stop
+	;;
+*)
+	echo "$0 [start|stop]"
+	;;
+esac
+
+exit 0
tests/conntrackd/netns/conntrackd-nsr1.conf new file +413 -0

413 lines not on this page · raw message

tests/conntrackd/netns/conntrackd-nsr2.conf new file +413 -0

413 lines not on this page · raw message

tests/conntrackd/netns/ruleset-nsr1.nft new file +6 -0
new file mode 100644
index 000000000000..bd6f1b4df6dd
@@ -0,0 +1,6 @@
+table ip filter {
+	chain postrouting {
+		type nat hook postrouting priority srcnat; policy accept;
+		oif veth0 masquerade
+	}
+}
Signature · 1 line
2.20.1
#1 lore raw
Quoted 3 lines · Pablo Neira Ayuso

This patch adds a script that creates a ip netns testbed. The network topology looks like this:

You can probably drop all those comments in the config file.

author maintainer
#2 lore raw
Quoted 6 lines · Arturo Borrero Gonzalez
Quoted 3 lines · Pablo Neira Ayuso

This patch adds a script that creates a ip netns testbed. The network topology looks like this:

You can probably drop all those comments in the config file.

I'll do before applying, thanks.