Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: virtio_net: add initial tests

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>

authored by

Jiri Pirko and committed by
Paolo Abeni
ccfaed04 dae9dd5f

+249
+1
MAINTAINERS
··· 23466 23466 F: include/linux/vringh.h 23467 23467 F: include/uapi/linux/virtio_*.h 23468 23468 F: tools/virtio/ 23469 + F: tools/testing/selftests/drivers/net/virtio_net/ 23469 23470 23470 23471 VIRTIO CRYPTO DRIVER 23471 23472 M: Gonglei <arei.gonglei@huawei.com>
+1
tools/testing/selftests/Makefile
··· 20 20 TARGETS += drivers/net 21 21 TARGETS += drivers/net/bonding 22 22 TARGETS += drivers/net/team 23 + TARGETS += drivers/net/virtio_net 23 24 TARGETS += dt 24 25 TARGETS += efivarfs 25 26 TARGETS += exec
+15
tools/testing/selftests/drivers/net/virtio_net/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0+ OR MIT 2 + 3 + TEST_PROGS = basic_features.sh \ 4 + # 5 + 6 + TEST_FILES = \ 7 + virtio_net_common.sh \ 8 + # 9 + 10 + TEST_INCLUDES = \ 11 + ../../../net/forwarding/lib.sh \ 12 + ../../../net/lib.sh \ 13 + # 14 + 15 + include ../../../lib.mk
+131
tools/testing/selftests/drivers/net/virtio_net/basic_features.sh
··· 1 + #!/bin/bash 2 + # SPDX-License-Identifier: GPL-2.0 3 + 4 + # See virtio_net_common.sh comments for more details about assumed setup 5 + 6 + ALL_TESTS=" 7 + initial_ping_test 8 + f_mac_test 9 + " 10 + 11 + source virtio_net_common.sh 12 + 13 + lib_dir=$(dirname "$0") 14 + source "$lib_dir"/../../../net/forwarding/lib.sh 15 + 16 + h1=${NETIFS[p1]} 17 + h2=${NETIFS[p2]} 18 + 19 + h1_create() 20 + { 21 + simple_if_init $h1 $H1_IPV4/24 $H1_IPV6/64 22 + } 23 + 24 + h1_destroy() 25 + { 26 + simple_if_fini $h1 $H1_IPV4/24 $H1_IPV6/64 27 + } 28 + 29 + h2_create() 30 + { 31 + simple_if_init $h2 $H2_IPV4/24 $H2_IPV6/64 32 + } 33 + 34 + h2_destroy() 35 + { 36 + simple_if_fini $h2 $H2_IPV4/24 $H2_IPV6/64 37 + } 38 + 39 + initial_ping_test() 40 + { 41 + setup_cleanup 42 + setup_prepare 43 + ping_test $h1 $H2_IPV4 " simple" 44 + } 45 + 46 + f_mac_test() 47 + { 48 + RET=0 49 + local test_name="mac feature filtered" 50 + 51 + virtio_feature_present $h1 $VIRTIO_NET_F_MAC 52 + if [ $? -ne 0 ]; then 53 + log_test_skip "$test_name" "Device $h1 is missing feature $VIRTIO_NET_F_MAC." 54 + return 0 55 + fi 56 + virtio_feature_present $h1 $VIRTIO_NET_F_MAC 57 + if [ $? -ne 0 ]; then 58 + log_test_skip "$test_name" "Device $h2 is missing feature $VIRTIO_NET_F_MAC." 59 + return 0 60 + fi 61 + 62 + setup_cleanup 63 + setup_prepare 64 + 65 + grep -q 0 /sys/class/net/$h1/addr_assign_type 66 + check_err $? "Permanent address assign type for $h1 is not set" 67 + grep -q 0 /sys/class/net/$h2/addr_assign_type 68 + check_err $? "Permanent address assign type for $h2 is not set" 69 + 70 + setup_cleanup 71 + virtio_filter_feature_add $h1 $VIRTIO_NET_F_MAC 72 + virtio_filter_feature_add $h2 $VIRTIO_NET_F_MAC 73 + setup_prepare 74 + 75 + grep -q 0 /sys/class/net/$h1/addr_assign_type 76 + check_fail $? "Permanent address assign type for $h1 is set when F_MAC feature is filtered" 77 + grep -q 0 /sys/class/net/$h2/addr_assign_type 78 + check_fail $? "Permanent address assign type for $h2 is set when F_MAC feature is filtered" 79 + 80 + ping_do $h1 $H2_IPV4 81 + check_err $? "Ping failed" 82 + 83 + log_test "$test_name" 84 + } 85 + 86 + setup_prepare() 87 + { 88 + virtio_device_rebind $h1 89 + virtio_device_rebind $h2 90 + wait_for_dev $h1 91 + wait_for_dev $h2 92 + 93 + vrf_prepare 94 + 95 + h1_create 96 + h2_create 97 + } 98 + 99 + setup_cleanup() 100 + { 101 + h2_destroy 102 + h1_destroy 103 + 104 + vrf_cleanup 105 + 106 + virtio_filter_features_clear $h1 107 + virtio_filter_features_clear $h2 108 + virtio_device_rebind $h1 109 + virtio_device_rebind $h2 110 + wait_for_dev $h1 111 + wait_for_dev $h2 112 + } 113 + 114 + cleanup() 115 + { 116 + pre_cleanup 117 + setup_cleanup 118 + } 119 + 120 + check_driver $h1 "virtio_net" 121 + check_driver $h2 "virtio_net" 122 + check_virtio_debugfs $h1 123 + check_virtio_debugfs $h2 124 + 125 + trap cleanup EXIT 126 + 127 + setup_prepare 128 + 129 + tests_run 130 + 131 + exit "$EXIT_STATUS"
+2
tools/testing/selftests/drivers/net/virtio_net/config
··· 1 + CONFIG_VIRTIO_NET=y 2 + CONFIG_VIRTIO_DEBUG=y
+99
tools/testing/selftests/drivers/net/virtio_net/virtio_net_common.sh
··· 1 + #!/bin/bash 2 + # SPDX-License-Identifier: GPL-2.0 3 + 4 + # This assumes running on a host with two virtio interfaces connected 5 + # back to back. Example script to do such wire-up of tap devices would 6 + # look like this: 7 + # 8 + # ======================================================================================================= 9 + # #!/bin/bash 10 + # 11 + # DEV1="$1" 12 + # DEV2="$2" 13 + # 14 + # sudo tc qdisc add dev $DEV1 clsact 15 + # sudo tc qdisc add dev $DEV2 clsact 16 + # sudo tc filter add dev $DEV1 ingress protocol all pref 1 matchall action mirred egress redirect dev $DEV2 17 + # sudo tc filter add dev $DEV2 ingress protocol all pref 1 matchall action mirred egress redirect dev $DEV1 18 + # sudo ip link set $DEV1 up 19 + # sudo ip link set $DEV2 up 20 + # ======================================================================================================= 21 + 22 + REQUIRE_MZ="no" 23 + NETIF_CREATE="no" 24 + NETIF_FIND_DRIVER="virtio_net" 25 + NUM_NETIFS=2 26 + 27 + H1_IPV4="192.0.2.1" 28 + H2_IPV4="192.0.2.2" 29 + H1_IPV6="2001:db8:1::1" 30 + H2_IPV6="2001:db8:1::2" 31 + 32 + VIRTIO_NET_F_MAC=5 33 + 34 + virtio_device_get() 35 + { 36 + local dev=$1; shift 37 + local device_path="/sys/class/net/$dev/device/" 38 + 39 + basename `realpath $device_path` 40 + } 41 + 42 + virtio_device_rebind() 43 + { 44 + local dev=$1; shift 45 + local device=`virtio_device_get $dev` 46 + 47 + echo "$device" > /sys/bus/virtio/drivers/virtio_net/unbind 48 + echo "$device" > /sys/bus/virtio/drivers/virtio_net/bind 49 + } 50 + 51 + virtio_debugfs_get() 52 + { 53 + local dev=$1; shift 54 + local device=`virtio_device_get $dev` 55 + 56 + echo /sys/kernel/debug/virtio/$device/ 57 + } 58 + 59 + check_virtio_debugfs() 60 + { 61 + local dev=$1; shift 62 + local debugfs=`virtio_debugfs_get $dev` 63 + 64 + if [ ! -f "$debugfs/device_features" ] || 65 + [ ! -f "$debugfs/filter_feature_add" ] || 66 + [ ! -f "$debugfs/filter_feature_del" ] || 67 + [ ! -f "$debugfs/filter_features" ] || 68 + [ ! -f "$debugfs/filter_features_clear" ]; then 69 + echo "SKIP: not possible to access debugfs for $dev" 70 + exit $ksft_skip 71 + fi 72 + } 73 + 74 + virtio_feature_present() 75 + { 76 + local dev=$1; shift 77 + local feature=$1; shift 78 + local debugfs=`virtio_debugfs_get $dev` 79 + 80 + cat $debugfs/device_features |grep "^$feature$" &> /dev/null 81 + return $? 82 + } 83 + 84 + virtio_filter_features_clear() 85 + { 86 + local dev=$1; shift 87 + local debugfs=`virtio_debugfs_get $dev` 88 + 89 + echo "1" > $debugfs/filter_features_clear 90 + } 91 + 92 + virtio_filter_feature_add() 93 + { 94 + local dev=$1; shift 95 + local feature=$1; shift 96 + local debugfs=`virtio_debugfs_get $dev` 97 + 98 + echo "$feature" > $debugfs/filter_feature_add 99 + }