Skip to contentSkip to conversation

virtio_test: A module for testing virtio via userspace

PATCH/RFC Christian Bornträger 4 participants 9 messages Jun 19, 2009 – Nov 15, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello Rusty,

this is a result of a two month internship about virtio testing.

From: Adrian Schneider <[email protected]>
From: Tim Hofmann <[email protected]>
From: Christian Ehrhardt <[email protected]>
From: Christian Borntraeger <[email protected]>

This patch introduces a prototype for a virtio_test module. This module can be bound to any virtio device via sysfs bind/unbind feature, e.g:

$ echo virtio1 > /sys/bus/virtio/drivers/virtio_rng/unbind
$ modprobe virtio_test

On probe this module registers to all virtqueues and creates a character device for every virtio device. (/dev/viotest<number>).
The character device offers ioctls to allow a userspace application to submit virtio operations like addbuf, kick and getbuf. It also offers ioctls to get information about the device and to query the amount of occurred callbacks (or wait synchronously on callbacks).

The driver currently lacks the following planned features:

o  userspace tooling for fuzzing (a prototype exists)
o  feature bit support
o  support arbitrary pointer mode in add_buf (e.g. test how qemu deals with

iovecs pointing beyond the guest memory size)
o priority binding with other virtio drivers (e.g. if virtio_blk and virtio_test are compiled into the kernel, virtio_blk should get all block devices by default on hotplug)

I would like to get feedback on

o  the general idea of a virtio_test module
o  the user interface ioctls
o  further ideas and comments 
---
 drivers/virtio/Kconfig       |   12 
 drivers/virtio/Makefile      |    2 
 drivers/virtio/virtio_test.c |  710 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/Kbuild         |    1 
 include/linux/virtio_test.h  |  146 ++++++++
 5 files changed, 871 insertions(+)
drivers/virtio/Kconfig +12 -0
Index: linux-2.6/drivers/virtio/Kconfig
===================================================================
@@ -33,3 +33,15 @@ config VIRTIO_BALLOON
 
 	 If unsure, say M.
 
+config VIRTIO_TEST
+	tristate "Virtio test driver (EXPERIMENTAL)"
+	select VIRTIO
+	select VIRTIO_RING
+	---help---
+	 This driver supports testing arbitrary virtio devices. The drivers
+	 offers IOCTLs to run add_buf/get_buf etc. from userspace. You can
+	 bind/unbind any unused virtio device to this driver via sysfs. Each
+	 bound device will get a /dev/viotest* device node.
+
+	 If unsure, say M.
+
drivers/virtio/Makefile +2 -0
Index: linux-2.6/drivers/virtio/Makefile
===================================================================
@@ -2,3 +2,5 @@ obj-$(CONFIG_VIRTIO) += virtio.o
 obj-$(CONFIG_VIRTIO_RING) += virtio_ring.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
+obj-$(CONFIG_VIRTIO_TEST) += virtio_test.o
+
drivers/virtio/virtio_test.c new file +710 -0

710 lines not on this page · raw message

include/linux/Kbuild +1 -0
Index: linux-2.6/include/linux/Kbuild
===================================================================
@@ -365,6 +365,7 @@ unifdef-y += virtio_console.h
 unifdef-y += virtio_pci.h
 unifdef-y += virtio_ring.h
 unifdef-y += virtio_rng.h
+unifdef-y += virtio_test.h
 unifdef-y += vt.h
 unifdef-y += wait.h
 unifdef-y += wanrouter.h
include/linux/virtio_test.h new file +146 -0

146 lines not on this page · raw message

#1 lore raw
Quoted 3 lines · Christian Bornträger

Hello Rusty,

this is a result of a two month internship about virtio testing.

Interesting!

Quoted 5 lines

I would like to get feedback on

o  the general idea of a virtio_test module
o  the user interface ioctls
o  further ideas and comments

Not mugging real drivers would be a requirement, I think.

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 39

Quoted 4 lines
drivers/virtio/Kconfig +4 -0
+config VIRTIO_TEST
+	tristate "Virtio test driver (EXPERIMENTAL)"
+	select VIRTIO
+	select VIRTIO_RING

Perhaps these should be depends? Plus, depends on EXPERIMENTAL.

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 46

Quoted 1 line
drivers/virtio/Kconfig +1 -0
+	 If unsure, say M.

That's "N" I think.

drivers/virtio/virtio_test.c line 385

Quoted 6 lines
drivers/virtio/virtio_test.c new file +6 -0
+	case VIOTEST_IOCGETBUF:
+		ret = do_get_buf(vtest, (struct viotest_getbuf __user *) arg);
+		break;
+	case VIOTEST_IOCGETCBS:
+		ret = get_callbacks(vtest, (struct viotest_cbinfo __user *) arg);
+		break;

Generally the point of callbacks is to tell you you have new buffers; in fact you're insulated from callbacks which don't show new buffers. So I'm not sure these two need to be separate?

In which case, a read/write interface starts to make sense (write for addbuf and kick, read for get_buf). That fits nicely with O_NONBLOCK and poll().

Cheers,
Rusty.

#4 lore raw
In reply to Christian Bornträger
Quoted 3 lines · Christian Bornträger

Hello Rusty,

this is a result of a two month internship about virtio testing.

Interesting!

Quoted 5 lines

I would like to get feedback on

o  the general idea of a virtio_test module
o  the user interface ioctls
o  further ideas and comments

Not mugging real drivers would be a requirement, I think.

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 39

Quoted 4 lines
drivers/virtio/Kconfig +4 -0
+config VIRTIO_TEST
+	tristate "Virtio test driver (EXPERIMENTAL)"
+	select VIRTIO
+	select VIRTIO_RING

Perhaps these should be depends? Plus, depends on EXPERIMENTAL.

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 46

Quoted 1 line
drivers/virtio/Kconfig +1 -0
+	 If unsure, say M.

That's "N" I think.

drivers/virtio/virtio_test.c line 385

Quoted 6 lines
drivers/virtio/virtio_test.c new file +6 -0
+	case VIOTEST_IOCGETBUF:
+		ret = do_get_buf(vtest, (struct viotest_getbuf __user *) arg);
+		break;
+	case VIOTEST_IOCGETCBS:
+		ret = get_callbacks(vtest, (struct viotest_cbinfo __user *) arg);
+		break;

Generally the point of callbacks is to tell you you have new buffers; in fact you're insulated from callbacks which don't show new buffers. So I'm not sure these two need to be separate?

In which case, a read/write interface starts to make sense (write for addbuf and kick, read for get_buf). That fits nicely with O_NONBLOCK and poll().

Cheers,
Rusty.

#2 lore raw
In reply to Rusty Russell

Am Mittwoch 24 Juni 2009 05:40:34 schrieb Rusty Russell:

Quoted 5 lines
Quoted 3 lines
o  the general idea of a virtio_test module
o  the user interface ioctls
o  further ideas and comments

Not mugging real drivers would be a requirement, I think.

Ok, I try to find a proper way to avoid that virtio_test binds to devices that have real drivers available. That patch to virtio_dev_match should be relatively easy.
The open question I have: Should virtio_test bind to a device if no other driver is (yet) available?

Quoted 10 lines

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 39

Quoted 4 lines
drivers/virtio/Kconfig +4 -0
+config VIRTIO_TEST
+	tristate "Virtio test driver (EXPERIMENTAL)"
+	select VIRTIO
+	select VIRTIO_RING

Perhaps these should be depends? Plus, depends on EXPERIMENTAL.

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 46

Quoted 1 line
drivers/virtio/Kconfig +1 -0
+	 If unsure, say M.

That's "N" I think.

Yes.

Quoted 14 lines

drivers/virtio/virtio_test.c line 385

Quoted 7 lines
drivers/virtio/virtio_test.c new file +6 -0
+	case VIOTEST_IOCGETBUF:
+		ret = do_get_buf(vtest, (struct viotest_getbuf __user *) arg);
+		break;
+	case VIOTEST_IOCGETCBS:
+		ret = get_callbacks(vtest, (struct viotest_cbinfo __user *) arg);
+		break;

Generally the point of callbacks is to tell you you have new buffers; in fact you're insulated from callbacks which don't show new buffers. So I'm not sure these two need to be separate?
In which case, a read/write interface starts to make sense (write for addbuf and kick, read for get_buf). That fits nicely with O_NONBLOCK and poll().

Hmm - makes sense. I will try to propose a 2nd version of the interface. The interface must handle multiple virtqueues per device, should allow non-blocking mode etc. Lets see what ideas come to my mind.

Thanks for the comments

Christian

#3 lore raw
In reply to Rusty Russell

Am Mittwoch 24 Juni 2009 05:40:34 schrieb Rusty Russell:

Quoted 5 lines
Quoted 3 lines
o  the general idea of a virtio_test module
o  the user interface ioctls
o  further ideas and comments

Not mugging real drivers would be a requirement, I think.

Ok, I try to find a proper way to avoid that virtio_test binds to devices that have real drivers available. That patch to virtio_dev_match should be relatively easy.
The open question I have: Should virtio_test bind to a device if no other driver is (yet) available?

Quoted 10 lines

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 39

Quoted 4 lines
drivers/virtio/Kconfig +4 -0
+config VIRTIO_TEST
+	tristate "Virtio test driver (EXPERIMENTAL)"
+	select VIRTIO
+	select VIRTIO_RING

Perhaps these should be depends? Plus, depends on EXPERIMENTAL.

drivers/virtio/Kconfig config VIRTIO_BALLOON · line 46

Quoted 1 line
drivers/virtio/Kconfig +1 -0
+	 If unsure, say M.

That's "N" I think.

Yes.

Quoted 14 lines

drivers/virtio/virtio_test.c line 385

Quoted 7 lines
drivers/virtio/virtio_test.c new file +6 -0
+	case VIOTEST_IOCGETBUF:
+		ret = do_get_buf(vtest, (struct viotest_getbuf __user *) arg);
+		break;
+	case VIOTEST_IOCGETCBS:
+		ret = get_callbacks(vtest, (struct viotest_cbinfo __user *) arg);
+		break;

Generally the point of callbacks is to tell you you have new buffers; in fact you're insulated from callbacks which don't show new buffers. So I'm not sure these two need to be separate?
In which case, a read/write interface starts to make sense (write for addbuf and kick, read for get_buf). That fits nicely with O_NONBLOCK and poll().

Hmm - makes sense. I will try to propose a 2nd version of the interface. The interface must handle multiple virtqueues per device, should allow non-blocking mode etc. Lets see what ideas come to my mind.

Thanks for the comments

Christian

#5 lore raw
In reply to Christian Bornträger

Hi,

The original email is from 2009, so I assume you don't have it in your inboxes already. Here is the original email:

https://lore.kernel.org/all/[email protected]/

Quoted 11 lines

This patch introduces a prototype for a virtio_test module. This module can be bound to any virtio device via sysfs bind/unbind feature, e.g:

$ echo virtio1 > /sys/bus/virtio/drivers/virtio_rng/unbind
$ modprobe virtio_test

On probe this module registers to all virtqueues and creates a character device for every virtio device. (/dev/viotest<number>).
The character device offers ioctls to allow a userspace application to submit virtio operations like addbuf, kick and getbuf. It also offers ioctls to get information about the device and to query the amount of occurred callbacks (or wait synchronously on callbacks).

As far as I understand the test driver was never merged and I can't find any similar testing drivers. I am looking for a test module that allows to create a transient virtio device that can be used to activate a virtio driver are communicate with it as if from the host.

Does such thing exist already?
Or how are virtio transports/drivers tested/fuzzed nowadays?

Thanks

#6 lore raw

Am 12.11.22 um 17:19 schrieb Dmitry Vyukov:

Quoted 19 lines

Hi,

The original email is from 2009, so I assume you don't have it in your inboxes already. Here is the original email:

https://lore.kernel.org/all/[email protected]/

Quoted 11 lines

This patch introduces a prototype for a virtio_test module. This module can be bound to any virtio device via sysfs bind/unbind feature, e.g:

$ echo virtio1 > /sys/bus/virtio/drivers/virtio_rng/unbind
$ modprobe virtio_test

On probe this module registers to all virtqueues and creates a character device for every virtio device. (/dev/viotest<number>).
The character device offers ioctls to allow a userspace application to submit virtio operations like addbuf, kick and getbuf. It also offers ioctls to get information about the device and to query the amount of occurred callbacks (or wait synchronously on callbacks).

Quoted 7 lines

As far as I understand the test driver was never merged and I can't find any similar testing drivers. I am looking for a test module that allows to create a transient virtio device that can be used to activate a virtio driver are communicate with it as if from the host.

Does such thing exist already?
Or how are virtio transports/drivers tested/fuzzed nowadays?

Right, the driver was never merged.
Adding Michael as todays virtio maintainer for ideas how to proceed.

#7 lore raw
In reply to Dmitry Vyukov
Quoted 7 lines · Dmitry Vyukov

I am looking for a test module that allows
to create a transient virtio device that can be used to activate a virtio driver are communicate with it as if from the host.
Does such thing exist already?
Or how are virtio transports/drivers tested/fuzzed nowadays?

Thanks

Just coding it up in qemu is probably easiest. This is how we test most things.

Signature · 6 lines
MST

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#8 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 6 lines · Dmitry Vyukov

I am looking for a test module that allows
to create a transient virtio device that can be used to activate a virtio driver are communicate with it as if from the host.
Does such thing exist already?
Or how are virtio transports/drivers tested/fuzzed nowadays?

Quoted 4 lines
Quoted 1 line

Thanks

Just coding it up in qemu is probably easiest. This is how we test most things.

This works for some testing scenarios, but has important downsides:
- fixed number of global virtio devices, so tests are not
hermetic/parallel and proper fuzzing is impossible
- tests running inside of the kernel can't control the device
behavior, so lots of scenarios are untestable/unfuzzable
- not suitable for most CI/fuzzing systems that run in clouds (nested
virt is very slow)
- require special setup per test suite (not scalable for CI/fuzzing
systems that test all of kernel)

A better and flexible approach to stub devices is to implement them inside of the kernel and allow creation of new transient instances (e.g. /dev/net/tun). Such stubs allow proper fuzzing, allow self-contained tests, allow the test to control stub behavior and are compatible with all machines (cloud, physical hw).

Is my understanding of how such in-kernel stub device can be implemented correct?
A stub driver could create struct virtio_device and call register_virtio_device() directly skipping all of the bus/probing code.
The virtio_device implementation will be parallel to virtio_mmio/pci and implement its own virtio_config_ops and notify/kick callback. This will allow us to test all of the virtio device drivers (console, balloon, virtio sound/gpu, etc), but not the virtio_mmio/pci nor the real probing code.

Is there a reasonable way to also test virtio_mmio/pci/probing from within the kernel?