selftests: virtio_net: add initial tests
authorJiri Pirko <jiri@nvidia.com>
Wed, 24 Apr 2024 10:40:49 +0000 (12:40 +0200)
committerPaolo Abeni <pabeni@redhat.com>
Fri, 26 Apr 2024 11:26:53 +0000 (13:26 +0200)
Introduce initial tests for virtio_net driver. Focus on feature testing
leveraging previously introduced debugfs feature filtering
infrastructure. Add very basic ping and F_MAC feature tests.

To run this, do:
$ make -C tools/testing/selftests/ TARGETS=drivers/net/virtio_net/ run_tests

Run it on a system with 2 virtio_net devices connected back-to-back
on the hypervisor.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
Reviewed-by: Petr Machata <petrm@nvidia.com>
Tested-by: Benjamin Poirier <bpoirier@nvidia.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
MAINTAINERS
tools/testing/selftests/Makefile
tools/testing/selftests/drivers/net/virtio_net/Makefile [new file with mode: 0644]
tools/testing/selftests/drivers/net/virtio_net/basic_features.sh [new file with mode: 0755]
tools/testing/selftests/drivers/net/virtio_net/config [new file with mode: 0644]
tools/testing/selftests/drivers/net/virtio_net/virtio_net_common.sh [new file with mode: 0644]

index 209143bac37ab1be607228a4004dba236572d789..ab89edc6974d17670575651e136c27f70065eea9 100644 (file)
@@ -23466,6 +23466,7 @@ F:      include/linux/virtio*.h
 F:     include/linux/vringh.h
 F:     include/uapi/linux/virtio_*.h
 F:     tools/virtio/
+F:     tools/testing/selftests/drivers/net/virtio_net/
 
 VIRTIO CRYPTO DRIVER
 M:     Gonglei <arei.gonglei@huawei.com>
index c785b6256a452acaf097578b2fe9c7306da1db40..2c940e9c4cedfe218bd616102d452cf50b5282a8 100644 (file)
@@ -20,6 +20,7 @@ TARGETS += drivers/s390x/uvdevice
 TARGETS += drivers/net
 TARGETS += drivers/net/bonding
 TARGETS += drivers/net/team
+TARGETS += drivers/net/virtio_net
 TARGETS += dt
 TARGETS += efivarfs
 TARGETS += exec
diff --git a/tools/testing/selftests/drivers/net/virtio_net/Makefile b/tools/testing/selftests/drivers/net/virtio_net/Makefile
new file mode 100644 (file)
index 0000000..7ec7cd3
--- /dev/null
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0+ OR MIT
+
+TEST_PROGS = basic_features.sh \
+        #
+
+TEST_FILES = \
+        virtio_net_common.sh \
+        #
+
+TEST_INCLUDES = \
+        ../../../net/forwarding/lib.sh \
+        ../../../net/lib.sh \
+        #
+
+include ../../../lib.mk
diff --git a/tools/testing/selftests/drivers/net/virtio_net/basic_features.sh b/tools/testing/selftests/drivers/net/virtio_net/basic_features.sh
new file mode 100755 (executable)
index 0000000..cf8cf81
--- /dev/null
@@ -0,0 +1,131 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+# See virtio_net_common.sh comments for more details about assumed setup
+
+ALL_TESTS="
+       initial_ping_test
+       f_mac_test
+"
+
+source virtio_net_common.sh
+
+lib_dir=$(dirname "$0")
+source "$lib_dir"/../../../net/forwarding/lib.sh
+
+h1=${NETIFS[p1]}
+h2=${NETIFS[p2]}
+
+h1_create()
+{
+       simple_if_init $h1 $H1_IPV4/24 $H1_IPV6/64
+}
+
+h1_destroy()
+{
+       simple_if_fini $h1 $H1_IPV4/24 $H1_IPV6/64
+}
+
+h2_create()
+{
+       simple_if_init $h2 $H2_IPV4/24 $H2_IPV6/64
+}
+
+h2_destroy()
+{
+       simple_if_fini $h2 $H2_IPV4/24 $H2_IPV6/64
+}
+
+initial_ping_test()
+{
+       setup_cleanup
+       setup_prepare
+       ping_test $h1 $H2_IPV4 " simple"
+}
+
+f_mac_test()
+{
+       RET=0
+       local test_name="mac feature filtered"
+
+       virtio_feature_present $h1 $VIRTIO_NET_F_MAC
+       if [ $? -ne 0 ]; then
+               log_test_skip "$test_name" "Device $h1 is missing feature $VIRTIO_NET_F_MAC."
+               return 0
+       fi
+       virtio_feature_present $h1 $VIRTIO_NET_F_MAC
+       if [ $? -ne 0 ]; then
+               log_test_skip "$test_name" "Device $h2 is missing feature $VIRTIO_NET_F_MAC."
+               return 0
+       fi
+
+       setup_cleanup
+       setup_prepare
+
+       grep -q 0 /sys/class/net/$h1/addr_assign_type
+       check_err $? "Permanent address assign type for $h1 is not set"
+       grep -q 0 /sys/class/net/$h2/addr_assign_type
+       check_err $? "Permanent address assign type for $h2 is not set"
+
+       setup_cleanup
+       virtio_filter_feature_add $h1 $VIRTIO_NET_F_MAC
+       virtio_filter_feature_add $h2 $VIRTIO_NET_F_MAC
+       setup_prepare
+
+       grep -q 0 /sys/class/net/$h1/addr_assign_type
+       check_fail $? "Permanent address assign type for $h1 is set when F_MAC feature is filtered"
+       grep -q 0 /sys/class/net/$h2/addr_assign_type
+       check_fail $? "Permanent address assign type for $h2 is set when F_MAC feature is filtered"
+
+       ping_do $h1 $H2_IPV4
+       check_err $? "Ping failed"
+
+       log_test "$test_name"
+}
+
+setup_prepare()
+{
+       virtio_device_rebind $h1
+       virtio_device_rebind $h2
+       wait_for_dev $h1
+       wait_for_dev $h2
+
+       vrf_prepare
+
+       h1_create
+       h2_create
+}
+
+setup_cleanup()
+{
+       h2_destroy
+       h1_destroy
+
+       vrf_cleanup
+
+       virtio_filter_features_clear $h1
+       virtio_filter_features_clear $h2
+       virtio_device_rebind $h1
+       virtio_device_rebind $h2
+       wait_for_dev $h1
+       wait_for_dev $h2
+}
+
+cleanup()
+{
+       pre_cleanup
+       setup_cleanup
+}
+
+check_driver $h1 "virtio_net"
+check_driver $h2 "virtio_net"
+check_virtio_debugfs $h1
+check_virtio_debugfs $h2
+
+trap cleanup EXIT
+
+setup_prepare
+
+tests_run
+
+exit "$EXIT_STATUS"
diff --git a/tools/testing/selftests/drivers/net/virtio_net/config b/tools/testing/selftests/drivers/net/virtio_net/config
new file mode 100644 (file)
index 0000000..f35de05
--- /dev/null
@@ -0,0 +1,2 @@
+CONFIG_VIRTIO_NET=y
+CONFIG_VIRTIO_DEBUG=y
diff --git a/tools/testing/selftests/drivers/net/virtio_net/virtio_net_common.sh b/tools/testing/selftests/drivers/net/virtio_net/virtio_net_common.sh
new file mode 100644 (file)
index 0000000..57bd805
--- /dev/null
@@ -0,0 +1,99 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+# This assumes running on a host with two virtio interfaces connected
+# back to back. Example script to do such wire-up of tap devices would
+# look like this:
+#
+# =======================================================================================================
+# #!/bin/bash
+#
+# DEV1="$1"
+# DEV2="$2"
+#
+# sudo tc qdisc add dev $DEV1 clsact
+# sudo tc qdisc add dev $DEV2 clsact
+# sudo tc filter add dev $DEV1 ingress protocol all pref 1 matchall action mirred egress redirect dev $DEV2
+# sudo tc filter add dev $DEV2 ingress protocol all pref 1 matchall action mirred egress redirect dev $DEV1
+# sudo ip link set $DEV1 up
+# sudo ip link set $DEV2 up
+# =======================================================================================================
+
+REQUIRE_MZ="no"
+NETIF_CREATE="no"
+NETIF_FIND_DRIVER="virtio_net"
+NUM_NETIFS=2
+
+H1_IPV4="192.0.2.1"
+H2_IPV4="192.0.2.2"
+H1_IPV6="2001:db8:1::1"
+H2_IPV6="2001:db8:1::2"
+
+VIRTIO_NET_F_MAC=5
+
+virtio_device_get()
+{
+       local dev=$1; shift
+       local device_path="/sys/class/net/$dev/device/"
+
+       basename `realpath $device_path`
+}
+
+virtio_device_rebind()
+{
+       local dev=$1; shift
+       local device=`virtio_device_get $dev`
+
+       echo "$device" > /sys/bus/virtio/drivers/virtio_net/unbind
+       echo "$device" > /sys/bus/virtio/drivers/virtio_net/bind
+}
+
+virtio_debugfs_get()
+{
+       local dev=$1; shift
+       local device=`virtio_device_get $dev`
+
+       echo /sys/kernel/debug/virtio/$device/
+}
+
+check_virtio_debugfs()
+{
+       local dev=$1; shift
+       local debugfs=`virtio_debugfs_get $dev`
+
+       if [ ! -f "$debugfs/device_features" ] ||
+          [ ! -f "$debugfs/filter_feature_add"  ] ||
+          [ ! -f "$debugfs/filter_feature_del"  ] ||
+          [ ! -f "$debugfs/filter_features"  ] ||
+          [ ! -f "$debugfs/filter_features_clear"  ]; then
+               echo "SKIP: not possible to access debugfs for $dev"
+               exit $ksft_skip
+       fi
+}
+
+virtio_feature_present()
+{
+       local dev=$1; shift
+       local feature=$1; shift
+       local debugfs=`virtio_debugfs_get $dev`
+
+       cat $debugfs/device_features |grep "^$feature$" &> /dev/null
+       return $?
+}
+
+virtio_filter_features_clear()
+{
+       local dev=$1; shift
+       local debugfs=`virtio_debugfs_get $dev`
+
+       echo "1" > $debugfs/filter_features_clear
+}
+
+virtio_filter_feature_add()
+{
+       local dev=$1; shift
+       local feature=$1; shift
+       local debugfs=`virtio_debugfs_get $dev`
+
+       echo "$feature" > $debugfs/filter_feature_add
+}