Skip to contentSkip to conversation

docs: admin-guide: add valsa driver documentation

PATCH In review Ivan Orlov 3 participants 9 messages May 13, 2023 – May 15, 2023

Subsystems (4)
lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

PATCH 1/3 docs: admin-guide: add valsa driver documentation

Add documentation for the new Virtual ALSA driver. It covers all possible usage cases: errors and delay injections, random and pattern-based data generation, playback and ioctl redefinition functionalities testing.

We have a lot of different virtual media drivers, which can be used for testing of the userspace applications and media subsystem middle layer. However, all of them are aimed at testing the video functionality and simulating the video devices. For audio devices we have only snd-dummy module, which is good in simulating the correct behavior of an ALSA device. I decided to write a tool, which would help to test the userspace ALSA programs (and the PCM middle layer as well) under unusual circumstances to figure out how they would behave. So I came up with this Virtual ALSA Driver.

This new Virtual ALSA Driver has several features which can be useful during the userspace ALSA applications testing/fuzzing, or testing/fuzzing of the PCM middle layer. Not all of them can be implemented using the existing virtual drivers (like dummy or loopback). Here is what can this driver do:

- Simulate both capture and playback processes
- Check the playback stream for containing the looped pattern
- Generate random or pattern-based capture data
- Inject delays into the playback and capturing processes
- Inject errors during the PCM callbacks

Also, this driver can check the playback stream for containing the predefined pattern, which is used in the corresponding selftest to check the PCM middle layer data transferring functionality. Additionally, this driver redefines the default RESET ioctl, and the selftest covers this PCM API functionality as well.

---
 Documentation/admin-guide/index.rst |   1 +
 Documentation/admin-guide/valsa.rst | 114 ++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+)
 create mode 100644 Documentation/admin-guide/valsa.rst
Documentation/admin-guide/index.rst +1 -0
index 43ea35613dfc..328cc59275a1 100644
@@ -131,6 +131,7 @@ configure specific aspects of kernel behavior to your liking.
    thunderbolt
    ufs
    unicode
+   valsa
    vga-softcursor
    video-output
    xfs
Documentation/admin-guide/valsa.rst new file +114 -0

114 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/3 ALSA: Implement the new virtual driver

We have a lot of different virtual media drivers, which can be used for testing of the userspace applications and media subsystem middle layer. However, all of them are aimed at testing the video functionality and simulating the video devices. For audio devices we have only snd-dummy module, which is good in simulating the correct behavior of an ALSA device. I decided to write a tool, which would help to test the userspace ALSA programs (and the PCM middle layer as well) under unusual circumstances to figure out how they would behave. So I came up with this Virtual ALSA Driver.

This new Virtual ALSA Driver has several features which can be useful during the userspace ALSA applications testing/fuzzing, or testing/fuzzing of the PCM middle layer. Not all of them can be implemented using the existing virtual drivers (like dummy or loopback). Here is what can this driver do:

- Simulate both capture and playback processes
- Generate random or pattern-based capture data
- Inject delays into the playback and capturing processes
- Inject errors during the PCM callbacks

Also, this driver can check the playback stream for containing the predefined pattern, which is used in the corresponding selftest to check the PCM middle layer data transferring functionality. Additionally, this driver redefines the default RESET ioctl, and the selftest covers this PCM API functionality as well.

Pattern-based capture stream data generation works in the following way:
user can set the pattern by writing to the 'fill_pattern' debugfs file. After that, the capture stream in case of reading will be filled with this pattern (for example, if the pattern is 'abc', the capture stream will contain 'abcabcabc...'). The pattern itself can be up to 4096 bytes long.

After all, I think this driver would be a good start, and I believe in the future we will see more virtual sound drivers.

---
 MAINTAINERS            |   7 +
 sound/drivers/Kconfig  |  15 ++
 sound/drivers/Makefile |   2 +
 sound/drivers/valsa.c  | 508 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 532 insertions(+)
 create mode 100644 sound/drivers/valsa.c
MAINTAINERS +7 -0
index 7e0b87d5aa2e..6dcb09d5f3d7 100644
@@ -22411,6 +22411,13 @@ S:	Maintained
 F:	drivers/nvdimm/virtio_pmem.c
 F:	drivers/nvdimm/nd_virtio.c
 
+VIRTUAL ALSA DRIVER
+M:	Ivan Orlov <[email protected]>
+S:	Maintained
+F:	Documentation/admin-guide/valsa.rst
+F:	sound/drivers/valsa.c
+F:	tools/testing/selftests/alsa/valsa-test.sh
+
 VIRTUAL BOX GUEST DEVICE DRIVER
 M:	Hans de Goede <[email protected]>
 M:	Arnd Bergmann <[email protected]>
sound/drivers/Kconfig +15 -0
index be3009746f3a..fa7d6f22ff06 100644
@@ -109,6 +109,21 @@ config SND_ALOOP
 	  To compile this driver as a module, choose M here: the module
 	  will be called snd-aloop.
 
+config SND_VALSA
+	tristate "Virtual ALSA driver"
+	select SND_PCM
+	help
+	  Say 'Y' or 'M' to include support for the Virtual ALSA driver. This
+	  driver is aimed at extended testing of the userspace applications
+	  which use the ALSA API, as well as the ALSA middle layer testing.
+
+	  It can generate random or pattern-based data into the capture stream,
+	  check the playback stream for containing the selected pattern, inject
+	  time delays during capture/playback, inject errors into the PCM
+	  callbacks, redefine the RESET ioctl operation to perform the PCM middle
+	  layer testing and inject errors during the PCM callbacks. You can find
+	  the corresponding selftest in the 'alsa' selftests folder.
+
 config SND_VIRMIDI
 	tristate "Virtual MIDI soundcard"
 	depends on SND_SEQUENCER
sound/drivers/Makefile +2 -0
index b60303180a1b..229eeb788995 100644
@@ -8,6 +8,7 @@ snd-dummy-objs := dummy.o
 snd-aloop-objs := aloop.o
 snd-mtpav-objs := mtpav.o
 snd-mts64-objs := mts64.o
+snd-valsa-objs := valsa.o
 snd-portman2x4-objs := portman2x4.o
 snd-serial-u16550-objs := serial-u16550.o
 snd-serial-generic-objs := serial-generic.o
@@ -17,6 +18,7 @@ snd-virmidi-objs := virmidi.o
 obj-$(CONFIG_SND_DUMMY) += snd-dummy.o
 obj-$(CONFIG_SND_ALOOP) += snd-aloop.o
 obj-$(CONFIG_SND_VIRMIDI) += snd-virmidi.o
+obj-$(CONFIG_SND_VALSA) += snd-valsa.o
 obj-$(CONFIG_SND_SERIAL_U16550) += snd-serial-u16550.o
 obj-$(CONFIG_SND_SERIAL_GENERIC) += snd-serial-generic.o
 obj-$(CONFIG_SND_MTPAV) += snd-mtpav.o
sound/drivers/valsa.c new file +508 -0

508 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 3/3 selftests: ALSA: Add test for the 'valsa' driver

In reply to Ivan Orlov

This test covers the new Virtual ALSA driver, including the capturing, playback and ioctl redefinition functionalities. This test is also helpful as an usage example of the 'valsa' driver.

We have a lot of different virtual media drivers, which can be used for testing of the userspace applications and media subsystem middle layer. However, all of them are aimed at testing the video functionality and simulating the video devices. For audio devices we have only snd-dummy module, which is good in simulating the correct behavior of an ALSA device. I decided to write a tool, which would help to test the userspace ALSA programs (and the PCM middle layer as well) under unusual circumstances to figure out how they would behave. So I came up with this Virtual ALSA Driver.

This new Virtual ALSA Driver has several features which can be useful during the userspace ALSA applications testing/fuzzing, or testing/fuzzing of the PCM middle layer. Not all of them can be implemented using the existing virtual drivers (like dummy or loopback). Here is what can this driver do:

- Simulate both capture and playback processes
- Generate random or pattern-based capture data
- Check the playback stream for containing the looped pattern
- Inject delays into the playback and capturing processes
- Inject errors during the PCM callbacks

Also, this driver can check the playback stream for containing the predefined pattern, which is used in the corresponding selftest to check the PCM middle layer data transferring functionality. Additionally, this driver redefines the default RESET ioctl, and the selftest covers this PCM API functionality as well.

After all, I think this driver would be a good start, and I believe in the future we will see more virtual sound drivers.

---
 tools/testing/selftests/alsa/valsa-test.sh | 55 ++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100755 tools/testing/selftests/alsa/valsa-test.sh
tools/testing/selftests/alsa/valsa-test.sh new file +55 -0
new file mode 100755
index 000000000000..872dfac138c2
@@ -0,0 +1,55 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Test for the 'valsa' virtual driver. It tests capture and playback functionalities,
+# as well as the 'reset' ioctl redefinition.
+
+uid=$(id -u)
+if [ $uid -ne 0 ]; then
+	echo "$0: Must be run as root"
+	exit 1
+fi
+
+if ! which modprobe > /dev/null 2>&1; then
+	echo "$0: You need modprobe installed"
+        exit 4
+fi
+
+if ! modinfo snd-valsa > /dev/null 2>&1; then
+	echo "$0: You must have the following enabled in your kernel:"
+	echo "CONFIG_SND_ALSAV=m"
+	exit 4
+fi
+
+modprobe snd-valsa
+
+if [ ! -e /sys/kernel/debug/valsa/pc_test ]; then
+	mount -t debugfs none /sys/kernel/debug
+
+	if [ ! -e /sys/kernel/debug/valsa/pc_test ]; then
+		echo "$0: Error mounting debugfs"
+		exit 4
+	fi
+fi
+
+success="1"
+arecord -D hw:CARD=valsa,DEV=0 -c 1 --buffer-size=8192 -f S16_LE -r 8000 --duration=4 out.wav
+
+if [[ $(< /sys/kernel/debug/valsa/ioctl_test) == "$success" ]]; then
+	echo "$0: ioctl test: success"
+else
+	echo "$0: ioctl test: fail"
+fi
+
+aplay -D hw:CARD=valsa,DEV=0 -c 1 --buffer-size=8192 -f S16_LE -r 8000 out.wav
+
+if [[ $(< /sys/kernel/debug/valsa/pc_test) == "$success" ]]; then
+	echo "$0: playback test: success"
+else
+	echo "$0: playback test: fail"
+fi
+
+rm -rf out.wav
+if ! rmmod snd-valsa > /dev/null 2>&1; then
+	echo "$0: Can't remove 'valsa' module: disable PulseAudio and unload it manually"
+fi
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/3] ALSA: Implement the new virtual driver

In reply to Ivan Orlov
Quoted 34 lines · Ivan Orlov

We have a lot of different virtual media drivers, which can be used for testing of the userspace applications and media subsystem middle layer. However, all of them are aimed at testing the video functionality and simulating the video devices. For audio devices we have only snd-dummy module, which is good in simulating the correct behavior of an ALSA device. I decided to write a tool, which would help to test the userspace ALSA programs (and the PCM middle layer as well) under unusual circumstances to figure out how they would behave. So I came up with this Virtual ALSA Driver.

This new Virtual ALSA Driver has several features which can be useful during the userspace ALSA applications testing/fuzzing, or testing/fuzzing of the PCM middle layer. Not all of them can be implemented using the existing virtual drivers (like dummy or loopback). Here is what can this driver do:

- Simulate both capture and playback processes
- Generate random or pattern-based capture data
- Inject delays into the playback and capturing processes
- Inject errors during the PCM callbacks

Also, this driver can check the playback stream for containing the predefined pattern, which is used in the corresponding selftest to check the PCM middle layer data transferring functionality. Additionally, this driver redefines the default RESET ioctl, and the selftest covers this PCM API functionality as well.

Pattern-based capture stream data generation works in the following way:
user can set the pattern by writing to the 'fill_pattern' debugfs file. After that, the capture stream in case of reading will be filled with this pattern (for example, if the pattern is 'abc', the capture stream will contain 'abcabcabc...'). The pattern itself can be up to 4096 bytes long.

Quoted 4 lines

After all, I think this driver would be a good start, and I believe in the future we will see more virtual sound drivers.

The idea is interesting, and it's a definitely good thing to have.

I wonder, though, whether it could be better provided as an extention to the existing snd-dummy driver. The advantage of extending snd-dummy driver would be that it already supports different formats, etc. OTOH, if we create an individual driver, the pro side is the simpleness of the code.

I'm inclined to go with a separate driver, but I'm open about this. Maybe Jaroslav and Mark have some opinions?

About this patch set: the driver name should be a bit more specific, as this isn't a generic virtual driver that is used for general purpose but it's only for testing. And it's only for testing PCM. So, a name like snd-test-pcm would be more appropriate, IMO.

And, we want the coverage of different formats, channels, rates and accesses (interleaved vs non-interleaved). How can we extend somehow more for that?

thanks,

Takashi

#7 lore raw
In reply to Ivan Orlov
Quoted 33 lines · Ivan Orlov

Add documentation for the new Virtual ALSA driver. It covers all possible usage cases: errors and delay injections, random and pattern-based data generation, playback and ioctl redefinition functionalities testing.

We have a lot of different virtual media drivers, which can be used for testing of the userspace applications and media subsystem middle layer. However, all of them are aimed at testing the video functionality and simulating the video devices. For audio devices we have only snd-dummy module, which is good in simulating the correct behavior of an ALSA device. I decided to write a tool, which would help to test the userspace ALSA programs (and the PCM middle layer as well) under unusual circumstances to figure out how they would behave. So I came up with this Virtual ALSA Driver.

This new Virtual ALSA Driver has several features which can be useful during the userspace ALSA applications testing/fuzzing, or testing/fuzzing of the PCM middle layer. Not all of them can be implemented using the existing virtual drivers (like dummy or loopback). Here is what can this driver do:

- Simulate both capture and playback processes
- Check the playback stream for containing the looped pattern
- Generate random or pattern-based capture data
- Inject delays into the playback and capturing processes
- Inject errors during the PCM callbacks

Also, this driver can check the playback stream for containing the predefined pattern, which is used in the corresponding selftest to check the PCM middle layer data transferring functionality. Additionally, this driver redefines the default RESET ioctl, and the selftest covers this PCM API functionality as well.

Quoted 4 lines
---
 Documentation/admin-guide/index.rst |   1 +
 Documentation/admin-guide/valsa.rst | 114 ++++++++++++++++++++++++++++

We have already subdirectories for the sound stuff (Documentation/sound/*), and this should go to there, I suppose (unless there is somewhere dedicated for each selftest scenario).

thanks,

Takashi

author
#3 lore raw

Re: [PATCH 2/3] ALSA: Implement the new virtual driver

In reply to Takashi Iwai
Quoted 16 lines · Takashi Iwai

The idea is interesting, and it's a definitely good thing to have.

I wonder, though, whether it could be better provided as an extention to the existing snd-dummy driver. The advantage of extending snd-dummy driver would be that it already supports different formats, etc. OTOH, if we create an individual driver, the pro side is the simpleness of the code.

I'm inclined to go with a separate driver, but I'm open about this. Maybe Jaroslav and Mark have some opinions?

About this patch set: the driver name should be a bit more specific, as this isn't a generic virtual driver that is used for general purpose but it's only for testing. And it's only for testing PCM. So, a name like snd-test-pcm would be more appropriate, IMO.

Quoted 8 lines

And, we want the coverage of different formats, channels, rates and accesses (interleaved vs non-interleaved). How can we extend somehow more for that?

thanks,

Takashi

Hello Takashi! Thank you for your reply. I fully agree with the naming issue, and I'll change it in the future versions of the patch set in case we choose to have it as a separate driver. I also prefer this option because in my opinion the use cases of these drivers are a little bit different. Also, I believe I can extend the driver to support different formats, channels and accesses in the near future.

Additionally, implementing these changes would be a perfect task for the end of the Linux Kernel Mentorship program I'm going through :) However, I'm open to other views on this, and I'm ready to move the functionality from my driver to the snd-dummy in case we prefer this option.

Thanks again for considering my changes!

Kind regards,
Ivan Orlov.

author
#8 lore raw
In reply to Takashi Iwai
Quoted 3 lines · Takashi Iwai

We have already subdirectories for the sound stuff (Documentation/sound/*), and this should go to there, I suppose (unless there is somewhere dedicated for each selftest scenario).

Alright, I'll move the documentation to the sound subfolder. Thank you!

Kind regards,
Ivan Orlov.

maintainer
#5 lore raw

Re: [PATCH 3/3] selftests: ALSA: Add test for the 'valsa' driver

In reply to Ivan Orlov

tools/testing/selftests/alsa/valsa-test.sh line 11

Quoted 5 lines · Ivan Orlov
tools/testing/selftests/alsa/valsa-test.sh new file +5 -0
+uid=$(id -u)
+if [ $uid -ne 0 ]; then
+	echo "$0: Must be run as root"
+	exit 1
+fi

It is not an error to run the selftest as a non-root user, the test should be skipped.

Quoted 1 line

+modprobe snd-valsa

We don't check if the module was already loaded.

tools/testing/selftests/alsa/valsa-test.sh line 33

Quoted 8 lines
tools/testing/selftests/alsa/valsa-test.sh new file +8 -0
+if [ ! -e /sys/kernel/debug/valsa/pc_test ]; then
+	mount -t debugfs none /sys/kernel/debug
+
+	if [ ! -e /sys/kernel/debug/valsa/pc_test ]; then
+		echo "$0: Error mounting debugfs"
+		exit 4
+	fi
+fi

This will unconditionally attempt to mount debugfs in a standard location and won't clean up after itself, if the system didn't have debugfs mounted for some reason then this will leave it sitting there.

tools/testing/selftests/alsa/valsa-test.sh line 42

Quoted 8 lines
tools/testing/selftests/alsa/valsa-test.sh new file +8 -0
+success="1"
+arecord -D hw:CARD=valsa,DEV=0 -c 1 --buffer-size=8192 -f S16_LE -r 8000 --duration=4 out.wav
+
+if [[ $(< /sys/kernel/debug/valsa/ioctl_test) == "$success" ]]; then
+	echo "$0: ioctl test: success"
+else
+	echo "$0: ioctl test: fail"
+fi

Would it not be better to have a C program that actually calls the ioctl rather than a custom debugfs thing that may or may not be wired up to do the same thing as an ioctl would? It seems like other than whatever this ioctl test actually does this is all just a simplified version of the existing pcm-test.

author
#6 lore raw

Re: [PATCH 3/3] selftests: ALSA: Add test for the 'valsa' driver

Quoted 10 lines · Mark Brown

tools/testing/selftests/alsa/valsa-test.sh line 11

Quoted 5 lines · Ivan Orlov
tools/testing/selftests/alsa/valsa-test.sh new file +5 -0
+uid=$(id -u)
+if [ $uid -ne 0 ]; then
+	echo "$0: Must be run as root"
+	exit 1
+fi

It is not an error to run the selftest as a non-root user, the test should be skipped.

Alright, thanks!

Quoted 4 lines
Quoted 1 line

+modprobe snd-valsa

We don't check if the module was already loaded.

Quoted 12 lines

tools/testing/selftests/alsa/valsa-test.sh line 33

Quoted 8 lines
tools/testing/selftests/alsa/valsa-test.sh new file +8 -0
+if [ ! -e /sys/kernel/debug/valsa/pc_test ]; then
+	mount -t debugfs none /sys/kernel/debug
+
+	if [ ! -e /sys/kernel/debug/valsa/pc_test ]; then
+		echo "$0: Error mounting debugfs"
+		exit 4
+	fi
+fi

This will unconditionally attempt to mount debugfs in a standard location and won't clean up after itself, if the system didn't have debugfs mounted for some reason then this will leave it sitting there.

Yes, I agree... I'll remove this in the future versions, I think the approach with skipping in case of missing debugfs would be better.

By the way, I used the 'fpa' test as an example, and it looks like it should be fixed as well...

Quoted 5 lines

Would it not be better to have a C program that actually calls the ioctl rather than a custom debugfs thing that may or may not be wired up to do the same thing as an ioctl would? It seems like other than whatever this ioctl test actually does this is all just a simplified version of the existing pcm-test.

Well, the idea was to test the playback buffer consistency - the driver has the functionality of testing the playback buffer for containing the looped pattern (and if the buffer doesn't contain the looped pattern the test fails). So, firstly we get the capture buffer with this pattern (via arecord), and then send it to the driver for the test (via aplay).

The pcm-test (as I understand) performs only time checks, not the checks of the data played/captured, and I think it is pointless to test the time again. But I agree, that C test implementation would be better - using this approach I can perform additional buffer checks and cover more functionality of the driver.

Thank you very much for the review!

Kind regards,
Ivan Orlov.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees