From: Florian Westphal Date: Thu, 2 Jan 2025 17:08:30 +0000 (+0100) Subject: tests: shell: add cgroupv2 socket match test case X-Git-Tag: v1.1.2~106 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9fbc77c127e7d55e02f7f8a13f5ed003be8f51aa;p=thirdparty%2Fnftables.git tests: shell: add cgroupv2 socket match test case This is a test case for nft_socket cgroupv2 matching, including support for matching inside a cgroupv2 mount space added in kernel commit 7f3287db6543 ("netfilter: nft_socket: make cgroupsv2 matching work with namespaces"). Test is thus run twice, once in the initial namespace and once with a changed cgroupv2 root. In case we can't create a cgroup or the 2nd half (unshared re-run) fails, indicate SKIP. Signed-off-by: Florian Westphal --- diff --git a/tests/shell/testcases/packetpath/cgroupv2 b/tests/shell/testcases/packetpath/cgroupv2 new file mode 100755 index 00000000..20bd18ae --- /dev/null +++ b/tests/shell/testcases/packetpath/cgroupv2 @@ -0,0 +1,143 @@ +#!/bin/bash + +doit="$1" +rc=0 + +# Create hierarchy: +# / -> nft-test1a/nft-test2a +# | `nft-test2b +# `--> nft-test1b/nft-test2a +# test1b/nft-test2a will remain empty and +# should never match, it only exists so we +# can create cgroupv2 match rules. + +if ! socat -h > /dev/null ; then + echo "socat tool is missing" + exit 77 +fi + +if [ ! -r /sys/fs/cgroup/cgroup.procs ] ;then + echo "cgroup filesystem not available" + exit 77 +fi + +cleanup() +{ + echo $$ > "/sys/fs/cgroup/cgroup.procs" + + rmdir "/sys/fs/cgroup/nft-test1a/nft-test2a" + rmdir "/sys/fs/cgroup/nft-test1a/nft-test2b" + rmdir "/sys/fs/cgroup/nft-test1b/nft-test2a" + rmdir "/sys/fs/cgroup/nft-test1a" + rmdir "/sys/fs/cgroup/nft-test1b" + + # nft list is broken after cgroupv2 removal, as nft + # can't find the human-readable names anymore. + $NFT delete table inet testcgrpv2 +} + +do_initial_setup() +{ + trap cleanup EXIT + ip link set lo up + + mkdir -p "/sys/fs/cgroup/nft-test1a/nft-test2a" || exit 1 + mkdir -p "/sys/fs/cgroup/nft-test1b/nft-test2a" || exit 1 + + mkdir "/sys/fs/cgroup/nft-test1a/nft-test2b" || exit 1 + + # After this, we can create cgroupv2 rules for the these cgroups. + # test1a and test2a should match while test1b/test2b should not: +$NFT -f - < "/sys/fs/cgroup/nft-test1a/nft-test2a/cgroup.procs" || exit 2 + socat -u STDIN TCP:127.0.0.1:8880,connect-timeout=4 < /dev/null > /dev/null + + test_counters "a1,a2" 1 1 + + echo $$ > "/sys/fs/cgroup/nft-test1a/cgroup.procs" || exit 2 + socat -u STDIN TCP:127.0.0.1:8880,connect-timeout=4 < /dev/null > /dev/null + test_counters "a1 only" 1 0 +} + + +if [ "$doit" != "setup-done" ];then + mkdir -p "/sys/fs/cgroup/nft-test1a" || exit 77 + + do_initial_setup + run_test + + if [ $rc -ne 0 ]; then + exit $rc + fi + + echo "Re-running test with changed cgroup root" + echo $$ > "/sys/fs/cgroup/nft-test1a/cgroup.procs" || exit 2 + unshare --fork --pid --mount -n -C $0 "setup-done" + rc=$? +else + want_inode=$(stat --printf=%i "/sys/fs/cgroup/nft-test1a/") + mount -t cgroup2 cgroup2 /sys/fs/cgroup + + # /sys/fs/cgroup/ should now match "/sys/fs/cgroup/nft-test1a/cgroup.procs" + rootinode=$(stat --printf=%i "/sys/fs/cgroup/") + + if [ $want_inode -ne $rootinode ] ;then + echo "Failed to remount cgroupv2 fs, wanted inode $want_inode as root node, but got $rootinode" + exit 77 + fi + + do_initial_setup + run_test +fi + +exit $rc diff --git a/tests/shell/testcases/packetpath/dumps/cgroupv2.nodump b/tests/shell/testcases/packetpath/dumps/cgroupv2.nodump new file mode 100644 index 00000000..e69de29b