Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D21532DF3E8 for ; Fri, 20 Jun 2025 16:47:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750438064; cv=none; b=sGPnWlMJWwzqUwZKrpSj2IYn49GxlXpmDS0Qvm1EgY1bJBwveY89TT3M2MNTE9eBCKIMIjiqMY4HPzBngkj5pe1iIg4laXREhWv+Cr4Ct+L1ByBnoLuXGIfC3sd/fq5t5xxfbGa/KqbjkBbl97QlhSatRB4RUCV/En8UZwhP7iA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750438064; c=relaxed/simple; bh=5xZObnCiwuB34iqLVb1nVJvdFDZFpwIWpPYWvFByI1g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NGB8wHj+63SHkTVXI8ysjpbuGm32kCnMaH9rEHFZetrL/H+z/Amr5FVW/IOUAyJCW5PhnITI55WQaUSfcpuw7P4KWsQPe+NWeMBkp+ybFznrelUeU7rhh8MjhUQAs5RRYUvF+aJ8P6/q7AyqHSicwv2kU800eYIToyk4H8nDOp0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eKJR1BW1; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eKJR1BW1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750438062; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=h/by31EtfziCQvgYWmEywAEhvgKYYlXWvTMAVPw5/Pc=; b=eKJR1BW1NlT/zvtdVSp+xL4c/ChqkTbGXY6w+YCLNaQQtbifTlIeNDNIIfDwfIcu4u7uSU xch7IKWZGfbSGr51+BAmHo75tIRqImEQomve+/6eRJk/wIW0bJIeq47n1Gakx8pkuMqdid JpsguLJKFilYxW4sOyDsTOiV8xE2b5A= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-695-EF-gksIHOCCPDkQ4imniDw-1; Fri, 20 Jun 2025 12:47:35 -0400 X-MC-Unique: EF-gksIHOCCPDkQ4imniDw-1 X-Mimecast-MFC-AGG-ID: EF-gksIHOCCPDkQ4imniDw_1750438054 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AD6E81800297; Fri, 20 Jun 2025 16:47:34 +0000 (UTC) Received: from yiche-laptop.redhat.com (unknown [10.72.112.97]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3066C19560A3; Fri, 20 Jun 2025 16:47:32 +0000 (UTC) From: Yi Chen To: netfilter-devel@vger.kernel.org Cc: Florian Westphal Subject: [PATCH v2] tests: shell: Verify limit statement with new test case. Date: Sat, 21 Jun 2025 00:47:30 +0800 Message-ID: <20250620164730.50041-1-yiche@redhat.com> In-Reply-To: <20250617104128.27188-1-yiche@redhat.com> References: <20250617104128.27188-1-yiche@redhat.com> Precedence: bulk X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 1. Add rate_limit test case. 2. Consolidate frequently used functions in helper/lib.sh 3. Introduce NFT_TEST_LIBRARY_FILE variable to source helper/lib.sh in tests. 4. Replace sleep with wait_local_port_listen(). 5. Other fixes: nft->$NFT; add dumps/*.nodump files Signed-off-by: Yi Chen --- tests/shell/helpers/lib.sh | 51 +++++++ tests/shell/helpers/test-wrapper.sh | 1 + .../testcases/packetpath/dumps/nat_ftp.nodump | 0 .../packetpath/dumps/rate_limit.nodump | 0 tests/shell/testcases/packetpath/flowtables | 32 +--- tests/shell/testcases/packetpath/nat_ftp | 32 ++-- tests/shell/testcases/packetpath/rate_limit | 137 ++++++++++++++++++ 7 files changed, 211 insertions(+), 42 deletions(-) create mode 100755 tests/shell/helpers/lib.sh create mode 100644 tests/shell/testcases/packetpath/dumps/nat_ftp.nodump create mode 100644 tests/shell/testcases/packetpath/dumps/rate_limit.nodump create mode 100755 tests/shell/testcases/packetpath/rate_limit diff --git a/tests/shell/helpers/lib.sh b/tests/shell/helpers/lib.sh new file mode 100755 index 00000000..4e01c957 --- /dev/null +++ b/tests/shell/helpers/lib.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# Frequently used functions. + +wait_local_port_listen() +{ + local listener_ns="${1}" + local port="${2}" + local protocol="${3}" + local pattern + local i + + pattern=":$(printf "%04X" "${port}") " + + # for tcp protocol additionally check the socket state + [ ${protocol} = "tcp" ] && pattern="${pattern}0A" + for i in $(seq 10); do + if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \ + /proc/net/"${protocol}"* | grep -q "${pattern}"; then + break + fi + sleep 0.1 + done +} + +assert_pass() +{ + local ret=$? + if [ $ret != 0 ]; then + echo "FAIL: ${@}" + if type -t failout; then + failout + fi + exit 1 + else + echo "PASS: ${@}" + fi +} +assert_fail() +{ + local ret=$? + if [ $ret == 0 ]; then + echo "FAIL: ${@}" + if type -t failout; then + failout + fi + exit 1 + else + echo "PASS: ${@}" + fi +} diff --git a/tests/shell/helpers/test-wrapper.sh b/tests/shell/helpers/test-wrapper.sh index 4a7e8b7b..6ec4e030 100755 --- a/tests/shell/helpers/test-wrapper.sh +++ b/tests/shell/helpers/test-wrapper.sh @@ -36,6 +36,7 @@ TESTDIR="$(dirname "$TEST")" START_TIME="$(cut -d ' ' -f1 /proc/uptime)" export TMPDIR="$NFT_TEST_TESTTMPDIR" +export NFT_TEST_LIBRARY_FILE="$NFT_TEST_BASEDIR/helpers/lib.sh" CLEANUP_UMOUNT_VAR_RUN=n diff --git a/tests/shell/testcases/packetpath/dumps/nat_ftp.nodump b/tests/shell/testcases/packetpath/dumps/nat_ftp.nodump new file mode 100644 index 00000000..e69de29b diff --git a/tests/shell/testcases/packetpath/dumps/rate_limit.nodump b/tests/shell/testcases/packetpath/dumps/rate_limit.nodump new file mode 100644 index 00000000..e69de29b diff --git a/tests/shell/testcases/packetpath/flowtables b/tests/shell/testcases/packetpath/flowtables index b68c5dd4..01a775a1 100755 --- a/tests/shell/testcases/packetpath/flowtables +++ b/tests/shell/testcases/packetpath/flowtables @@ -3,6 +3,8 @@ # NFT_TEST_REQUIRES(NFT_TEST_HAVE_socat) # NFT_TEST_SKIP(NFT_TEST_SKIP_slow) +. $NFT_TEST_LIBRARY_FILE + rnd=$(mktemp -u XXXXXXXX) R="flowtable-router-$rnd" C="flowtable-client-$rnd" @@ -17,29 +19,10 @@ cleanup() } trap cleanup EXIT -assert_pass() -{ - local ret=$? - if [ $ret != 0 ] - then - echo "FAIL: ${@}" - ip netns exec $R cat /proc/net/nf_conntrack - exit 1 - else - echo "PASS: ${@}" - fi -} -assert_fail() +# call back when assert failed +failout() { - local ret=$? - if [ $ret == 0 ] - then - echo "FAIL: ${@}" - ip netns exec $R cat /proc/net/nf_conntrack - exit 1 - else - echo "PASS: ${@}" - fi + ip netns exec $R cat /proc/net/nf_conntrack } ip netns add $R @@ -67,7 +50,7 @@ sleep 3 ip netns exec $C ping -q -6 2001:db8:ffff:22::1 -c1 assert_pass "topo initialization" -ip netns exec $R nft -f - <> /tmp/pipefile-$rnd ; assert_pass "send a packet" diff --git a/tests/shell/testcases/packetpath/nat_ftp b/tests/shell/testcases/packetpath/nat_ftp index 327047b8..4b9f6eb0 100755 --- a/tests/shell/testcases/packetpath/nat_ftp +++ b/tests/shell/testcases/packetpath/nat_ftp @@ -4,6 +4,7 @@ # NFT_TEST_REQUIRES(NFT_TEST_HAVE_curl) # NFT_TEST_REQUIRES(NFT_TEST_HAVE_vsftpd) +. $NFT_TEST_LIBRARY_FILE cleanup() { for i in $R $C $S;do @@ -14,22 +15,15 @@ cleanup() } trap cleanup EXIT -assert_pass() +# call back when assert failed +failout() { - local ret=$? - if [ $ret != 0 ] - then - echo "FAIL: ${@}" - ip netns exec $R nft list ruleset - tcpdump -nnr ${PCAP} - test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack - ip netns exec $R conntrack -S - ip netns exec $R conntrack -L - ip netns exec $S ss -nitepal - exit 1 - else - echo "PASS: ${@}" - fi + ip netns exec $R $NFT list ruleset + tcpdump -nnr ${PCAP} + test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack + ip netns exec $R conntrack -S + ip netns exec $R conntrack -L + ip netns exec $S ss -nitepal } rnd=$(mktemp -u XXXXXXXX) @@ -82,7 +76,7 @@ assert_pass "topo initialization" reload_ruleset() { ip netns exec $R conntrack -F 2> /dev/null - ip netns exec $R nft -f - <<-EOF + ip netns exec $R $NFT -f - <<-EOF flush ruleset table ip6 ftp_helper_nat_test { ct helper ftp-standard { @@ -138,7 +132,8 @@ pam_service_name=vsftpd background=YES EOF ip netns exec $S vsftpd ${FTPCONF} -sleep 1 +wait_local_port_listen $S 21 tcp + ip netns exec $S ss -6ltnp | grep -q '*:21' assert_pass "start vsftpd server" @@ -147,7 +142,7 @@ assert_pass "start vsftpd server" reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! -sleep 1 +sleep 0.5 ip netns exec $C curl --no-progress-meter --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp passive mode " @@ -164,6 +159,7 @@ reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! +sleep 0.5 ip netns exec $C curl --no-progress-meter -P - --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp active mode " diff --git a/tests/shell/testcases/packetpath/rate_limit b/tests/shell/testcases/packetpath/rate_limit new file mode 100755 index 00000000..84ee5621 --- /dev/null +++ b/tests/shell/testcases/packetpath/rate_limit @@ -0,0 +1,137 @@ +#!/bin/bash + +# NFT_TEST_REQUIRES(NFT_TEST_HAVE_socat) +# + +. $NFT_TEST_LIBRARY_FILE + +cleanup() +{ + for i in $C $S;do + kill $(ip netns pid $i) 2>/dev/null + ip netns del $i + done +} +trap cleanup EXIT + +rnd=$(mktemp -u XXXXXXXX) +C="ratelimit-client-$rnd" +S="ratelimit-server-$rnd" + +ip_sc=10.167.1.1 +ip_cs=10.167.1.2 +ip1_cs=10.167.1.3 + +ip netns add $S +ip netns add $C + +ip link add s_c netns $S type veth peer name c_s netns $C +ip -net $S link set s_c up +ip -net $C link set c_s up +ip -net $S link set lo up +ip -net $C link set lo up +ip -net $S addr add ${ip_sc}/24 dev s_c +ip -net $C addr add ${ip_cs}/24 dev c_s +ip -net $C addr add ${ip1_cs}/24 dev c_s +ip netns exec $C ping ${ip_sc} -c1 +assert_pass "topo initialization" + +ip netns exec $S $NFT -f - < /dev/null & +wait_local_port_listen $S 80 tcp + +for port in {1..5};do + ip netns exec $C socat -u - TCP:${ip_sc}:80,connect-timeout=1 <<< 'AAA' + assert_pass "tcp connection burst 5 accept" +done +ip netns exec $C socat -u - TCP:${ip_sc}:80,reuseport,connect-timeout=1 <<< 'AAA' +assert_fail "tcp connection burst 5 up to limit reject" + +ip netns exec $S $NFT flush chain filter in_tcp +assert_pass result "flush chain" + +ip netns exec $S $NFT flush set filter http1 +assert_pass result "flush set" + +ip netns exec $S $NFT add rule filter in_tcp iifname s_c tcp dport 80 ct state new add @http1 { tcp dport . ip saddr limit rate over 1/second burst 1 packets} counter reject +assert_pass result "add rule limit rate over 1/second burst 1" +ip netns exec $S $NFT add rule filter in_tcp iifname s_c tcp dport 80 counter accept + +sleep 1 +ip netns exec $C socat -u - TCP:${ip_sc}:80,reuseport,connect-timeout=1 <<< 'AAA' +assert_pass result "tcp connection limit rate 1/sec burst 1 accept" + +ip netns exec $C socat -u - TCP:${ip_sc}:80,reuseport,connect-timeout=1 <<< 'AAA' +assert_fail result "tcp connection limit rate 1/sec burst 1 reject" + +sleep 1 +ip netns exec $C socat -u - TCP:${ip_sc}:80,reuseport,connect-timeout=1 <<< 'AAA' +assert_pass result "tcp connection limit rate 1/sec burst 1 accept" -- 2.49.0