Commit dba86b7d authored by Paolo Abeni's avatar Paolo Abeni

Merge branch 'selftests-virtio_net-introduce-initial-testing-infrastructure'

Jiri Pirko says:

====================
selftests: virtio_net: introduce initial testing infrastructure

This patchset aims at introducing very basic initial infrastructure
for virtio_net testing, namely it focuses on virtio feature testing.

The first patch adds support for debugfs for virtio devices, allowing
user to filter features to pretend to be driver that is not capable
of the filtered feature.

Example:
$ cat /sys/bus/virtio/devices/virtio0/features
1110010111111111111101010000110010000000100000000000000000000000
$ echo "5" >/sys/kernel/debug/virtio/virtio0/filter_feature_add
$ cat /sys/kernel/debug/virtio/virtio0/filter_features
5
$ echo "virtio0" > /sys/bus/virtio/drivers/virtio_net/unbind
$ echo "virtio0" > /sys/bus/virtio/drivers/virtio_net/bind
$ cat /sys/bus/virtio/devices/virtio0/features
1110000111111111111101010000110010000000100000000000000000000000

Leverage that in the last patch that lays ground for virtio_net
selftests testing, including very basic F_MAC feature test.

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

It is assumed, as with lot of other selftests in the net group,
that there are netdevices connected back-to-back. In this case,
two virtio_net devices connected back to back. If you use "tap" qemu
netdevice type, to configure this loop on a hypervisor, one may use
this script:

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

Another possibility is to use virtme-ng like this:
$ vng --network=loop
or directly:
$ vng --network=loop -- make -C tools/testing/selftests/ TARGETS=drivers/net/virtio_net/ run_tests

"loop" network type will take care of creating two "hubport" qemu netdevs
putting them into a single hub.

To do it manually with qemu, pass following command line options:
-nic hubport,hubid=1,id=nd0,model=virtio-net-pci
-nic hubport,hubid=1,id=nd1,model=virtio-net-pci
====================

Link: https://lore.kernel.org/r/20240424104049.3935572-1-jiri@resnulli.usSigned-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parents fc48de77 ccfaed04
...@@ -23466,6 +23466,7 @@ F: include/linux/virtio*.h ...@@ -23466,6 +23466,7 @@ F: include/linux/virtio*.h
F: include/linux/vringh.h F: include/linux/vringh.h
F: include/uapi/linux/virtio_*.h F: include/uapi/linux/virtio_*.h
F: tools/virtio/ F: tools/virtio/
F: tools/testing/selftests/drivers/net/virtio_net/
VIRTIO CRYPTO DRIVER VIRTIO CRYPTO DRIVER
M: Gonglei <arei.gonglei@huawei.com> M: Gonglei <arei.gonglei@huawei.com>
......
...@@ -178,4 +178,14 @@ config VIRTIO_DMA_SHARED_BUFFER ...@@ -178,4 +178,14 @@ config VIRTIO_DMA_SHARED_BUFFER
This option adds a flavor of dma buffers that are backed by This option adds a flavor of dma buffers that are backed by
virtio resources. virtio resources.
config VIRTIO_DEBUG
bool "Debug facilities"
depends on VIRTIO
help
Enable this to expose debug facilities over debugfs.
This allows to debug features, to see what features the device
advertises and to set filter for features used by driver.
If unsure, say N.
endif # VIRTIO_MENU endif # VIRTIO_MENU
...@@ -13,3 +13,4 @@ obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o ...@@ -13,3 +13,4 @@ obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
obj-$(CONFIG_VIRTIO_VDPA) += virtio_vdpa.o obj-$(CONFIG_VIRTIO_VDPA) += virtio_vdpa.o
obj-$(CONFIG_VIRTIO_MEM) += virtio_mem.o obj-$(CONFIG_VIRTIO_MEM) += virtio_mem.o
obj-$(CONFIG_VIRTIO_DMA_SHARED_BUFFER) += virtio_dma_buf.o obj-$(CONFIG_VIRTIO_DMA_SHARED_BUFFER) += virtio_dma_buf.o
obj-$(CONFIG_VIRTIO_DEBUG) += virtio_debug.o
...@@ -274,6 +274,9 @@ static int virtio_dev_probe(struct device *_d) ...@@ -274,6 +274,9 @@ static int virtio_dev_probe(struct device *_d)
else else
dev->features = driver_features_legacy & device_features; dev->features = driver_features_legacy & device_features;
/* When debugging, user may filter some features by hand. */
virtio_debug_device_filter_features(dev);
/* Transport features always preserved to pass to finalize_features. */ /* Transport features always preserved to pass to finalize_features. */
for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
if (device_features & (1ULL << i)) if (device_features & (1ULL << i))
...@@ -465,6 +468,8 @@ int register_virtio_device(struct virtio_device *dev) ...@@ -465,6 +468,8 @@ int register_virtio_device(struct virtio_device *dev)
/* Acknowledge that we've seen the device. */ /* Acknowledge that we've seen the device. */
virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
virtio_debug_device_init(dev);
/* /*
* device_add() causes the bus infrastructure to look for a matching * device_add() causes the bus infrastructure to look for a matching
* driver. * driver.
...@@ -496,6 +501,7 @@ void unregister_virtio_device(struct virtio_device *dev) ...@@ -496,6 +501,7 @@ void unregister_virtio_device(struct virtio_device *dev)
int index = dev->index; /* save for after device release */ int index = dev->index; /* save for after device release */
device_unregister(&dev->dev); device_unregister(&dev->dev);
virtio_debug_device_exit(dev);
ida_free(&virtio_index_ida, index); ida_free(&virtio_index_ida, index);
} }
EXPORT_SYMBOL_GPL(unregister_virtio_device); EXPORT_SYMBOL_GPL(unregister_virtio_device);
...@@ -590,11 +596,13 @@ static int virtio_init(void) ...@@ -590,11 +596,13 @@ static int virtio_init(void)
{ {
if (bus_register(&virtio_bus) != 0) if (bus_register(&virtio_bus) != 0)
panic("virtio bus registration failed"); panic("virtio bus registration failed");
virtio_debug_init();
return 0; return 0;
} }
static void __exit virtio_exit(void) static void __exit virtio_exit(void)
{ {
virtio_debug_exit();
bus_unregister(&virtio_bus); bus_unregister(&virtio_bus);
ida_destroy(&virtio_index_ida); ida_destroy(&virtio_index_ida);
} }
......
// SPDX-License-Identifier: GPL-2.0-or-later
#include <linux/virtio.h>
#include <linux/virtio_config.h>
#include <linux/debugfs.h>
static struct dentry *virtio_debugfs_dir;
static int virtio_debug_device_features_show(struct seq_file *s, void *data)
{
struct virtio_device *dev = s->private;
u64 device_features;
unsigned int i;
device_features = dev->config->get_features(dev);
for (i = 0; i < BITS_PER_LONG_LONG; i++) {
if (device_features & (1ULL << i))
seq_printf(s, "%u\n", i);
}
return 0;
}
DEFINE_SHOW_ATTRIBUTE(virtio_debug_device_features);
static int virtio_debug_filter_features_show(struct seq_file *s, void *data)
{
struct virtio_device *dev = s->private;
unsigned int i;
for (i = 0; i < BITS_PER_LONG_LONG; i++) {
if (dev->debugfs_filter_features & (1ULL << i))
seq_printf(s, "%u\n", i);
}
return 0;
}
DEFINE_SHOW_ATTRIBUTE(virtio_debug_filter_features);
static int virtio_debug_filter_features_clear(void *data, u64 val)
{
struct virtio_device *dev = data;
if (val == 1)
dev->debugfs_filter_features = 0;
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(virtio_debug_filter_features_clear_fops, NULL,
virtio_debug_filter_features_clear, "%llu\n");
static int virtio_debug_filter_feature_add(void *data, u64 val)
{
struct virtio_device *dev = data;
if (val >= BITS_PER_LONG_LONG)
return -EINVAL;
dev->debugfs_filter_features |= BIT_ULL_MASK(val);
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(virtio_debug_filter_feature_add_fops, NULL,
virtio_debug_filter_feature_add, "%llu\n");
static int virtio_debug_filter_feature_del(void *data, u64 val)
{
struct virtio_device *dev = data;
if (val >= BITS_PER_LONG_LONG)
return -EINVAL;
dev->debugfs_filter_features &= ~BIT_ULL_MASK(val);
return 0;
}
DEFINE_DEBUGFS_ATTRIBUTE(virtio_debug_filter_feature_del_fops, NULL,
virtio_debug_filter_feature_del, "%llu\n");
void virtio_debug_device_init(struct virtio_device *dev)
{
dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->dev),
virtio_debugfs_dir);
debugfs_create_file("device_features", 0400, dev->debugfs_dir, dev,
&virtio_debug_device_features_fops);
debugfs_create_file("filter_features", 0400, dev->debugfs_dir, dev,
&virtio_debug_filter_features_fops);
debugfs_create_file("filter_features_clear", 0200, dev->debugfs_dir, dev,
&virtio_debug_filter_features_clear_fops);
debugfs_create_file("filter_feature_add", 0200, dev->debugfs_dir, dev,
&virtio_debug_filter_feature_add_fops);
debugfs_create_file("filter_feature_del", 0200, dev->debugfs_dir, dev,
&virtio_debug_filter_feature_del_fops);
}
EXPORT_SYMBOL_GPL(virtio_debug_device_init);
void virtio_debug_device_filter_features(struct virtio_device *dev)
{
dev->features &= ~dev->debugfs_filter_features;
}
EXPORT_SYMBOL_GPL(virtio_debug_device_filter_features);
void virtio_debug_device_exit(struct virtio_device *dev)
{
debugfs_remove_recursive(dev->debugfs_dir);
}
EXPORT_SYMBOL_GPL(virtio_debug_device_exit);
void virtio_debug_init(void)
{
virtio_debugfs_dir = debugfs_create_dir("virtio", NULL);
}
EXPORT_SYMBOL_GPL(virtio_debug_init);
void virtio_debug_exit(void)
{
debugfs_remove_recursive(virtio_debugfs_dir);
}
EXPORT_SYMBOL_GPL(virtio_debug_exit);
...@@ -126,6 +126,8 @@ struct virtio_admin_cmd { ...@@ -126,6 +126,8 @@ struct virtio_admin_cmd {
* @vqs: the list of virtqueues for this device. * @vqs: the list of virtqueues for this device.
* @features: the features supported by both driver and device. * @features: the features supported by both driver and device.
* @priv: private pointer for the driver's use. * @priv: private pointer for the driver's use.
* @debugfs_dir: debugfs directory entry.
* @debugfs_filter_features: features to be filtered set by debugfs.
*/ */
struct virtio_device { struct virtio_device {
int index; int index;
...@@ -141,6 +143,10 @@ struct virtio_device { ...@@ -141,6 +143,10 @@ struct virtio_device {
struct list_head vqs; struct list_head vqs;
u64 features; u64 features;
void *priv; void *priv;
#ifdef CONFIG_VIRTIO_DEBUG
struct dentry *debugfs_dir;
u64 debugfs_filter_features;
#endif
}; };
#define dev_to_virtio(_dev) container_of_const(_dev, struct virtio_device, dev) #define dev_to_virtio(_dev) container_of_const(_dev, struct virtio_device, dev)
...@@ -237,4 +243,33 @@ void virtqueue_dma_sync_single_range_for_cpu(struct virtqueue *_vq, dma_addr_t a ...@@ -237,4 +243,33 @@ void virtqueue_dma_sync_single_range_for_cpu(struct virtqueue *_vq, dma_addr_t a
void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq, dma_addr_t addr, void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq, dma_addr_t addr,
unsigned long offset, size_t size, unsigned long offset, size_t size,
enum dma_data_direction dir); enum dma_data_direction dir);
#ifdef CONFIG_VIRTIO_DEBUG
void virtio_debug_device_init(struct virtio_device *dev);
void virtio_debug_device_exit(struct virtio_device *dev);
void virtio_debug_device_filter_features(struct virtio_device *dev);
void virtio_debug_init(void);
void virtio_debug_exit(void);
#else
static inline void virtio_debug_device_init(struct virtio_device *dev)
{
}
static inline void virtio_debug_device_exit(struct virtio_device *dev)
{
}
static inline void virtio_debug_device_filter_features(struct virtio_device *dev)
{
}
static inline void virtio_debug_init(void)
{
}
static inline void virtio_debug_exit(void)
{
}
#endif
#endif /* _LINUX_VIRTIO_H */ #endif /* _LINUX_VIRTIO_H */
...@@ -20,6 +20,7 @@ TARGETS += drivers/s390x/uvdevice ...@@ -20,6 +20,7 @@ TARGETS += drivers/s390x/uvdevice
TARGETS += drivers/net TARGETS += drivers/net
TARGETS += drivers/net/bonding TARGETS += drivers/net/bonding
TARGETS += drivers/net/team TARGETS += drivers/net/team
TARGETS += drivers/net/virtio_net
TARGETS += dt TARGETS += dt
TARGETS += efivarfs TARGETS += efivarfs
TARGETS += exec TARGETS += exec
......
# 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
#!/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"
CONFIG_VIRTIO_NET=y
CONFIG_VIRTIO_DEBUG=y
#!/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
}
...@@ -84,6 +84,43 @@ declare -A NETIFS=( ...@@ -84,6 +84,43 @@ declare -A NETIFS=(
# e.g. a low-power board. # e.g. a low-power board.
: "${KSFT_MACHINE_SLOW:=no}" : "${KSFT_MACHINE_SLOW:=no}"
##############################################################################
# Find netifs by test-specified driver name
driver_name_get()
{
local dev=$1; shift
local driver_path="/sys/class/net/$dev/device/driver"
if [[ -L $driver_path ]]; then
basename `realpath $driver_path`
fi
}
netif_find_driver()
{
local ifnames=`ip -j link show | jq -r ".[].ifname"`
local count=0
for ifname in $ifnames
do
local driver_name=`driver_name_get $ifname`
if [[ ! -z $driver_name && $driver_name == $NETIF_FIND_DRIVER ]]; then
count=$((count + 1))
NETIFS[p$count]="$ifname"
fi
done
}
# Whether to find netdevice according to the driver speficied by the importer
: "${NETIF_FIND_DRIVER:=}"
if [[ $NETIF_FIND_DRIVER ]]; then
unset NETIFS
declare -A NETIFS
netif_find_driver
fi
net_forwarding_dir=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")") net_forwarding_dir=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
if [[ -f $net_forwarding_dir/forwarding.config ]]; then if [[ -f $net_forwarding_dir/forwarding.config ]]; then
...@@ -246,6 +283,18 @@ if [[ "$(id -u)" -ne 0 ]]; then ...@@ -246,6 +283,18 @@ if [[ "$(id -u)" -ne 0 ]]; then
exit $ksft_skip exit $ksft_skip
fi fi
check_driver()
{
local dev=$1; shift
local expected=$1; shift
local driver_name=`driver_name_get $dev`
if [[ $driver_name != $expected ]]; then
echo "SKIP: expected driver $expected for $dev, got $driver_name instead"
exit $ksft_skip
fi
}
if [[ "$CHECK_TC" = "yes" ]]; then if [[ "$CHECK_TC" = "yes" ]]; then
check_tc_version check_tc_version
fi fi
...@@ -689,6 +738,19 @@ setup_wait() ...@@ -689,6 +738,19 @@ setup_wait()
sleep $WAIT_TIME sleep $WAIT_TIME
} }
wait_for_dev()
{
local dev=$1; shift
local timeout=${1:-$WAIT_TIMEOUT}; shift
slowwait $timeout ip link show dev $dev &> /dev/null
if (( $? )); then
check_err 1
log_test wait_for_dev "Interface $dev did not appear."
exit $EXIT_STATUS
fi
}
cmd_jq() cmd_jq()
{ {
local cmd=$1 local cmd=$1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment