Redirect the ingress of veth1, where the flowtable takes the reply direction, to ifb0 with a pfifo qdisc, as an ingress shaper does, and check that the flow is offloaded in both directions, that ifb0 sees the whole transfer and that a netdev base chain on veth1 sees none of it, for IPv4 and IPv6. Then forward veth1's TCP to ifb0 from a netdev base chain instead, and check that the transfer completes, that ifb0 carried it and that ifb0 does not loop the packets back to that chain. Signed-off-by: Julius Bairaktaris --- tools/testing/selftests/net/netfilter/config | 5 + .../selftests/net/netfilter/nft_flowtable.sh | 159 ++++++++++++++++++ 2 files changed, 164 insertions(+) diff --git a/tools/testing/selftests/net/netfilter/config b/tools/testing/selftests/net/netfilter/config index 629b2b29ed1a..49dfdbc14042 100644 --- a/tools/testing/selftests/net/netfilter/config +++ b/tools/testing/selftests/net/netfilter/config @@ -38,8 +38,11 @@ CONFIG_IPV6_TUNNEL=m CONFIG_IP_VS=m CONFIG_IP_VS_PROTO_TCP=y CONFIG_IP_VS_RR=m +CONFIG_IFB=m CONFIG_MACVLAN=m CONFIG_NAMESPACES=y +CONFIG_NET_ACT_MIRRED=m +CONFIG_NET_CLS_MATCHALL=m CONFIG_NET_CLS_U32=m CONFIG_NETDEVSIM=m CONFIG_NETFILTER=y @@ -59,6 +62,7 @@ CONFIG_NET_L3_MASTER_DEV=y CONFIG_NET_NS=y CONFIG_NET_PKTGEN=m CONFIG_NET_SCH_HTB=m +CONFIG_NET_SCH_INGRESS=m CONFIG_NET_SCH_NETEM=m CONFIG_NET_VRF=y CONFIG_NF_CONNTRACK=m @@ -90,6 +94,7 @@ CONFIG_NFT_FIB_INET=m CONFIG_NFT_FIB_IPV4=m CONFIG_NFT_FIB_IPV6=m CONFIG_NFT_FLOW_OFFLOAD=m +CONFIG_NFT_FWD_NETDEV=m CONFIG_NFT_LIMIT=m CONFIG_NFT_LOG=m CONFIG_NFT_MASQ=m diff --git a/tools/testing/selftests/net/netfilter/nft_flowtable.sh b/tools/testing/selftests/net/netfilter/nft_flowtable.sh index 449c518bd947..f2ac48081558 100755 --- a/tools/testing/selftests/net/netfilter/nft_flowtable.sh +++ b/tools/testing/selftests/net/netfilter/nft_flowtable.sh @@ -847,10 +847,169 @@ ip -net "$nsr1" addr add dead:1::1/64 dev veth0 nodad ip -net "$nsr1" link set up dev veth0 } +# Bytes the root qdisc of ifb0 has sent. +ifb_sent() +{ + ip netns exec "$nsr1" tc -s qdisc show dev ifb0 root | + sed -n 's/.*Sent \([0-9]*\) bytes.*/\1/p' | head -n 1 +} + +ifb_add() +{ + if ! ip -net "$nsr1" link add ifb0 type ifb 2>/dev/null; then + echo "SKIP: ifb not available" + [ "$ret" -eq 0 ] && ret=$ksft_skip + return 1 + fi + ip -net "$nsr1" link set ifb0 up + ip netns exec "$nsr1" tc qdisc add dev ifb0 root pfifo +} + +# An ingress shaper redirects $dev's ingress to ifb0, which hands the +# packets back after its qdisc: the flow must be offloaded and ifb0 must +# have seen the file. +ifb_shaper_run() +{ + local dev=$1 + local fs min sent + + ifb_add || return + + if ! ip netns exec "$nsr1" tc qdisc add dev "$dev" \ + handle ffff: ingress || + ! ip netns exec "$nsr1" tc filter add dev "$dev" parent ffff: \ + matchall action mirred egress redirect dev ifb0; then + echo "SKIP: could not redirect $dev to ifb0" + [ "$ret" -eq 0 ] && ret=$ksft_skip + ip netns exec "$nsr1" tc qdisc del dev "$dev" handle ffff: \ + ingress 2>/dev/null + ip -net "$nsr1" link delete ifb0 + return + fi + + # The base chains are above tc: they do not see what tc redirected, + # neither before ifb nor after it hands the packets back. + ip netns exec "$nsr1" nft -f - <&2 + ret=1 + fi + sent=$(($(ifb_sent) - sent)) + if [ "$sent" -lt "$min" ]; then + echo "FAIL: IPv$f ifb0 saw $sent bytes," \ + "expected at least $min" 1>&2 + ret=1 + else + echo "PASS: IPv$f ifb0 shaped the ingress of $dev" + fi + done + + if ip netns exec "$nsr1" nft list counter netdev ifbcount seen | + grep -q "packets 0 "; then + echo "PASS: base chain on $dev did not see the" \ + "redirected packets" + else + echo "FAIL: base chain on $dev saw packets tc redirected" \ + "to ifb0" 1>&2 + ip netns exec "$nsr1" nft list counter netdev ifbcount seen 1>&2 + ret=1 + fi + ip netns exec "$nsr1" nft delete table netdev ifbcount + + ip netns exec "$nsr1" tc qdisc del dev "$dev" handle ffff: ingress + ip -net "$nsr1" link delete ifb0 +} + +# A netdev base chain forwards $dev's ingress to ifb0, which hands the +# packets back: they must not reach the base chain again, or they loop. +ifb_fwd_run() +{ + local dev=$1 + local fs sent + + ifb_add || return + + if ! ip netns exec "$nsr1" nft -f - <&2 + ret=1 + fi + sent=$(ifb_sent) + if [ "$sent" -lt "$fs" ]; then + echo "FAIL: ifb0 sent $sent bytes for a $fs byte file:" \ + "nft fwd to ifb0 did not redirect" 1>&2 + ret=1 + elif [ "$sent" -gt $((fs * 4)) ]; then + echo "FAIL: ifb0 sent $sent bytes for a $fs byte file:" \ + "redirect loop" 1>&2 + ret=1 + else + echo "PASS: no redirect loop through ifb0 ($sent bytes for $fs)" + fi + + ip netns exec "$nsr1" nft delete table netdev ifbfwd + ip netns exec "$nsr1" nft reset counters table inet filter > /dev/null + ip -net "$nsr1" link delete ifb0 +} + +# Another test: +# Shape the ingress of veth1, where the flowtable takes the reply +# direction, through ifb, as an ingress shaper does; then forward it to ifb +# from a netdev base chain instead. +test_ifb() { +ip -net "$ns2" route replace default via 10.0.2.1 +ip -6 -net "$ns2" route replace default via dead:2::1 + +ifb_shaper_run veth1 +ifb_fwd_run veth1 + +ip -net "$ns2" route del default via 10.0.2.1 +ip -6 -net "$ns2" route del default via dead:2::1 +} + test_ipip test_bridge +test_ifb + KEY_SHA="0x"$(ps -af | sha1sum | cut -d " " -f 1) KEY_AES="0x"$(ps -af | md5sum | cut -d " " -f 1) SPI1=$RANDOM -- 2.53.0