Add (not run by default) selftest that measures latency while flushing a large hash:net set contends with concurrent packet-path insertions (via "-j SET --add-set"). Signed-off-by: Florian Westphal --- Artifact from LLM trying to convince me that rhashtable conversion sucks and makes flush too slow. Since this doesn't hurt and might come in useful later, sending this so its archived on lore. I'm not sure its worth applying it, ever. .../testing/selftests/net/netfilter/Makefile | 5 +- tools/testing/selftests/net/netfilter/config | 3 + .../net/netfilter/ipset_flush_perf.sh | 105 ++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/net/netfilter/ipset_flush_perf.sh diff --git a/tools/testing/selftests/net/netfilter/Makefile b/tools/testing/selftests/net/netfilter/Makefile index df3c20c90f5d..0a732ce12c81 100644 --- a/tools/testing/selftests/net/netfilter/Makefile +++ b/tools/testing/selftests/net/netfilter/Makefile @@ -45,7 +45,10 @@ TEST_PROGS := \ xt_string.sh \ # end of TEST_PROGS -TEST_PROGS_EXTENDED = nft_concat_range_perf.sh +TEST_PROGS_EXTENDED = \ + ipset_flush_perf.sh \ + nft_concat_range_perf.sh \ +# end of TEST_PROGS_EXTENDED TEST_GEN_FILES = \ audit_logread \ diff --git a/tools/testing/selftests/net/netfilter/config b/tools/testing/selftests/net/netfilter/config index 629b2b29ed1a..d6047a8db318 100644 --- a/tools/testing/selftests/net/netfilter/config +++ b/tools/testing/selftests/net/netfilter/config @@ -32,6 +32,8 @@ CONFIG_IP_NF_MATCH_RPFILTER=m CONFIG_IP_NF_NAT=m CONFIG_IP_NF_RAW=m CONFIG_IP_SCTP=m +CONFIG_IP_SET=m +CONFIG_IP_SET_HASH_NET=m CONFIG_IPV6=y CONFIG_IPV6_MULTIPLE_TABLES=y CONFIG_IPV6_TUNNEL=m @@ -53,6 +55,7 @@ CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m CONFIG_NETFILTER_XT_MATCH_STATE=m CONFIG_NETFILTER_XT_MATCH_STRING=m CONFIG_NETFILTER_XT_NAT=m +CONFIG_NETFILTER_XT_SET=m CONFIG_NETFILTER_XT_TARGET_REDIRECT=m CONFIG_NET_IPIP=m CONFIG_NET_L3_MASTER_DEV=y diff --git a/tools/testing/selftests/net/netfilter/ipset_flush_perf.sh b/tools/testing/selftests/net/netfilter/ipset_flush_perf.sh new file mode 100755 index 000000000000..72dc8966a537 --- /dev/null +++ b/tools/testing/selftests/net/netfilter/ipset_flush_perf.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Measures ipset flush latency on a large hash:net set, both idle and while +# the set is concurrently being inserted into via a real "-j SET --add-set +# ... src" iptables rule, and reports flush latency and ping RTT so a +# reader can judge the impact of concurrent insert load from real numbers. +# +# This is NOT a regression test. + +source lib.sh + +checktool "ipset -v" "run test without ipset" +checktool "iptables --version" "run test without iptables" +checktool "ping -V" "run test without ping" + +entries=200000 +duration=10 +[ "$KSFT_MACHINE_SLOW" = "yes" ] && entries=20000 + +pingout=$(mktemp) + +cleanup() { + cleanup_all_ns + rm -f "$pingout" +} +trap cleanup EXIT + +setup_ns nsclient + +# Mostly /32 entries, like a dynamic IP block/allow list (fail2ban-style), +# plus a handful of wider prefixes so h->rnets[] tracks more than one CIDR +# length. +gen_restore() +{ + local set="$1" + local n="$2" + + awk -v set="$set" -v n="$n" 'BEGIN { + print "create " set " hash:net family inet maxelem " (n + 64) " -exist" + for (i = 0; i < n; i++) + print "add " set " 10." int(i/65536)%256 "." int(i/256)%256 "." i%256 "/32" + for (i = 0; i < 16; i++) { + print "add " set " 172.16." i ".0/24" + print "add " set " 172." i ".0.0/16" + } + print "add " set " 11.0.0.0/8" + print "add " set " 12.0.0.0/8" + }' +} + +populate() +{ + gen_restore perfset "$entries" | ip netns exec "$nsclient" ipset restore +} + +flush_once() +{ + local t0 t1 + + t0=$(date +%s%N) + ip netns exec "$nsclient" ipset flush perfset + t1=$(date +%s%N) + echo $(( (t1 - t0) / 1000000 )) +} + +echo "INFO: populating hash:net set with $entries /32 entries (+ a few wider prefixes)" +if ! populate; then + echo "SKIP: could not create/populate ipset" + exit $ksft_skip +fi + +base_ms=$(flush_once) +echo "INFO: flush of a freshly populated, otherwise idle set took ${base_ms}ms" + +populate + +if ! ip netns exec "$nsclient" iptables -t raw -A OUTPUT -j SET --add-set perfset src; then + echo "SKIP: could not load the SET target (xt_set)" + exit $ksft_skip +fi + +echo "INFO: flushing the set back-to-back for ${duration}s while flooding it with inserts via -j SET" +ip netns exec "$nsclient" ping -f -q -w "$duration" 127.0.0.1 > "$pingout" 2>&1 & +pingpid=$! + +max_ms=0 +count=0 +end=$(( $(date +%s) + duration )) +while [ "$(date +%s)" -lt "$end" ]; do + ms=$(flush_once) + count=$((count + 1)) + [ "$ms" -gt "$max_ms" ] && max_ms="$ms" + populate +done + +wait "$pingpid" + +echo "INFO: ping -f summary while the set was being repeatedly flushed:" +sed -n '/packets transmitted/,$p' "$pingout" + +echo "INFO: completed $count flush+repopulate cycles in ${duration}s under concurrent insert load" +echo "INFO: max flush latency under load: ${max_ms}ms (idle baseline: ${base_ms}ms)" + +exit 0 -- 2.56.0