Skip to contentSkip to conversation

tests: add atomic chain replace test

PATCH In review Florian Westphal 2 participants 2 messages Mar 3, 2025 – Mar 5, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 tests: add atomic chain replace test

Add a test that replaces one base chain and check that no filtered packets make it through, i.e. that the 'old chain' doesn't disappear before new one is active.

---
 .../testcases/transactions/atomic_replace.sh  | 73 +++++++++++++++++++
 .../dumps/atomic_replace.sh.nodump            |  0
 2 files changed, 73 insertions(+)
 create mode 100755 tests/shell/testcases/transactions/atomic_replace.sh
 create mode 100644 tests/shell/testcases/transactions/dumps/atomic_replace.sh.nodump
tests/shell/testcases/transactions/atomic_replace.sh new file +73 -0
new file mode 100755
index 000000000000..dce178602a6f
@@ -0,0 +1,73 @@
+#!/bin/bash
+
+set -e
+
+rnd=$(mktemp -u XXXXXXXX)
+ns="nft-atomic-$rnd"
+pid1=""
+pid2=""
+duration=8
+
+cleanup()
+{
+	kill "$pid1" "$pid2"
+	ip netns del "$ns"
+}
+
+trap cleanup EXIT
+
+ip netns add "$ns" || exit 111
+ip -net "$ns" link set lo up
+
+ip netns exec "$ns" ping 127.0.0.1 -q -c 1
+
+ip netns exec "$ns" $NFT -f - <<EOF
+table ip t {
+	set s {
+		type ipv4_addr
+		elements = { 127.0.0.1 }
+	}
+
+	chain input {
+		type filter hook input priority 0; policy accept;
+		ip protocol icmp counter
+	}
+
+	chain output {
+		type filter hook output priority 0; policy accept;
+		ip protocol icmp ip daddr @s drop
+	}
+}
+EOF
+
+ip netns exec "$ns" ping -f 127.0.0.1 &
+pid1=$!
+ip netns exec "$ns" ping -f 127.0.0.1 &
+pid2=$!
+
+time_now=$(date +%s)
+time_stop=$((time_now + duration))
+repl=0
+
+while [ $time_now -lt $time_stop ]; do
+ip netns exec "$ns" $NFT -f - <<EOF
+flush chain ip t output
+table ip t {
+	chain output {
+		type filter hook output priority 0; policy accept;
+		ip protocol icmp ip daddr @s drop
+	}
+}
+EOF
+	repl=$((repl+1))
+
+	# do at least 100 replaces and stop after $duration seconds.
+	if [ $((repl % 101)) -eq 100 ];then
+		time_now=$(date +%s)
+	fi
+done
+
+# must match, all icmp packets dropped in output.
+ip netns exec "$ns" $NFT list chain ip t input | grep "counter packets 0"
+
+echo "Completed $repl chain replacements"
tests/shell/testcases/transactions/dumps/atomic_replace.sh.nodump new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
Signature · 1 line
2.48.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Florian Westphal

Add a test that replaces one base chain and check that no filtered packets make it through, i.e. that the 'old chain' doesn't disappear before new one is active.

Quoted 93 lines
---
 .../testcases/transactions/atomic_replace.sh  | 73 +++++++++++++++++++
 .../dumps/atomic_replace.sh.nodump            |  0
 2 files changed, 73 insertions(+)
 create mode 100755 tests/shell/testcases/transactions/atomic_replace.sh
 create mode 100644 tests/shell/testcases/transactions/dumps/atomic_replace.sh.nodump
tests/shell/testcases/transactions/atomic_replace.sh new file +73 -0
new file mode 100755
index 000000000000..dce178602a6f
@@ -0,0 +1,73 @@
+#!/bin/bash
+
+set -e
+
+rnd=$(mktemp -u XXXXXXXX)
+ns="nft-atomic-$rnd"
+pid1=""
+pid2=""
+duration=8
+
+cleanup()
+{
+	kill "$pid1" "$pid2"
+	ip netns del "$ns"
+}
+
+trap cleanup EXIT
+
+ip netns add "$ns" || exit 111
+ip -net "$ns" link set lo up
+
+ip netns exec "$ns" ping 127.0.0.1 -q -c 1
+
+ip netns exec "$ns" $NFT -f - <<EOF
+table ip t {
+	set s {
+		type ipv4_addr
+		elements = { 127.0.0.1 }
+	}
+
+	chain input {
+		type filter hook input priority 0; policy accept;
+		ip protocol icmp counter
+	}
+
+	chain output {
+		type filter hook output priority 0; policy accept;
+		ip protocol icmp ip daddr @s drop
+	}
+}
+EOF
+
+ip netns exec "$ns" ping -f 127.0.0.1 &
+pid1=$!
+ip netns exec "$ns" ping -f 127.0.0.1 &
+pid2=$!
+
+time_now=$(date +%s)
+time_stop=$((time_now + duration))
+repl=0
+
+while [ $time_now -lt $time_stop ]; do
+ip netns exec "$ns" $NFT -f - <<EOF
+flush chain ip t output
+table ip t {
+	chain output {
+		type filter hook output priority 0; policy accept;
+		ip protocol icmp ip daddr @s drop
+	}
+}
+EOF
+	repl=$((repl+1))
+
+	# do at least 100 replaces and stop after $duration seconds.
+	if [ $((repl % 101)) -eq 100 ];then
+		time_now=$(date +%s)
+	fi
+done
+
+# must match, all icmp packets dropped in output.
+ip netns exec "$ns" $NFT list chain ip t input | grep "counter packets 0"
+
+echo "Completed $repl chain replacements"
tests/shell/testcases/transactions/dumps/atomic_replace.sh.nodump new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
Signature · 1 line
2.48.1