Apr 13, 2022 – José Ignacio Tornos Martínez |
|
Nowadays, there are more and more devices and modern operating systems must try to support all types and several of them with every integration, with every release. Maintaining a large number of devices is difficult, expensive and also hard to test, specially for plug-and-play devices, like USB devices. Therefore, it is necessary to create a mechanism to facilitate the maintenance and testing of old and new USB devices. And this is where USB device emulation comes in. In that way, a complete framework including a big bunch of emulated and validated USB devices will allow easier integration and release. The area of application would be very wide: earlier bug search/detection even during development, automatic tests, continuous integration, …
|
USB/IP project (http://usbip.sourceforge.net/) allows sharing the USB devices connected to a local machine so that it can be managed by another machine connected to the network by means of a TCP/IP connection. Then USB/IP project consist of two parts:
The procedure is valid for Linux and Windows, here I will focus only on Linux. The idea behind emulation is to replace the local device support to remotize or host with an application that behaves in the same way. In this way we can emulate devices with software applications that follow the commented USB/IP protocol specification. In the following points I will describe how to configure and run the remote support and how to connect to our USB emulated device.
|
Remote support is divided in two parts:
At this point, it is important to remark that the device emulators, after configuration by user space application, will communicate directly with the kernel space. Local support has a very similar structure but as I have mentioned, as our goal is the device emulation we will do not talk about this. Let's analyze every part of remote support.
|
First of all, in order to get the functionality we need to compile the Linux Kernel with the following options:
These options enable the USB/IP virtual host controller driver, which is run on the remote machine. Normal USB drivers need to be also included because they will be probed and configured in the same way from virtual host controller drivers. Besides there are other important configuration options:
These options define the number of ports per USB/IP virtual host controller and the number of USB/IP virtual host controllers as if adding physical host controllers. These are the default values if CONFIG_USBIP_VHCI_HCD is enabled, increase if necessary. The commented options and kernel modules are already included in some Linux distributions like Fedora. Let’s see an example of available virtual USB buses and ports that we will use later. Default and real resources in example equipment:
Now, we will load the module vhci-hcd into the system (default configuration for CONFIG_USBIP_VHCI_HC_PORTS and CONFIG_USBIP_VHCI_NR_HCS):
The remote USB/IP virtual host controller driver will only use the configured virtualized resources. Of course, emulated devices will work in the same way.
|
The other necessary part in the USB/IP project is the user space tool usbip and needs to be used to configure the referred kernel space on both sides, although, in the same way, we only focus on the remote side, since the local side will be represented by the emulator. That is, usbip tool will configure the USB/IP virtual controller (tcp client) in kernel space to connect to the device emulator (tcp server) in order to establish a direct connection between them for USB configuration, events, data, ... exchange. The tool is independent of the type of device and is able to provide information about available and reserved resources (see more information in the examples below). The local USB/IP virtual host controller needs to specify the pair bus-port that will be remotized to allow remote access, it will be the same for emulated devices, but in this case, this pair can be anything because there is no real device and resource reservation is not necessary. This tool is found on the Linux Kernel repository in order to be totally synchronized with it. Location of the tool on the Linux Kernel repository: ./tools/usb/usbip Some distributions like Fedora provides the usbip package with this tool. If package is not provided, follow the instruction in the available README file to compile and install. Suitable rpm package can also be generated from the usbip-emulator repo https://github.com/jtornosm/USBIP-Virtual-USB-Device.git:
|
Emulators are generated in python and c. I have started with c development (I will focus on this part), but the same could be done in python. For c development, compile emulation tools from the repo https://github.com/jtornosm/USBIP-Virtual-USB-Device.git:
All the supported devices emulated at this moment will be generated: rpm package (usbip-emulator) can be also generated with:
As examples, Vendor and Product IDs are hardcoded in the code. Following three examples to show how emulation works. We are using the same equipment for the emulator and remote USB/IP but they could run in different equipment. Besides, we are reserving different resources so all the devices could be emulated at the same time.
|
From one terminal, let's emulate the hso device: (“1-1” is the pair bus-port for usb on the local machine, as we are emulating, it could be anything. It is only important because usbip tool will have to use the same name to request the emulated device)
From another terminal connect to the emulator: (localhost because emulator is running in the same equipment and the same name for pair bus-port as the emulator)
Now we can check that the new device is present: (as we saw previously, for this example machine, bus 3 is virtualized)
In order to release resources:
And we can check that the device is released:
After this, we can emulate again or stop the emulated device from the first terminal (i.e. with Ctrl-C).
|
From one terminal, let's emulate the cdc-ether device (root permission is required because raw socket needs to bind to specified interface for data plane): (“1-1” is the pair bus-port for usb on the local machine, as we are emulating, it could be anything. It is only important because usbip tool will have to use the same name to request the emulated device)
From another terminal connect to the emulator: (localhost because emulator is running in the same equipment and the same name for pair bus-port as the emulator)
Now we can check that the new device is present: (as we saw previously, for this example machine, bus 3 is virtualized)
For this example, we can also test the data plane. (ip forwarding is disabled in both sides) First, we can configure the ip address in the emulated device:
Second, for example, from other directly ethernet connected machine (real or virtual) we can configure a macvlan interface in the same subnet to send/receive traffic (ping, iperf, …):
In order to release resources:
And we can check that the device is released:
And of course, traffic from the other machine is not working:
After this, we can emulate again or stop the emulated device from the first terminal (i.e. with Ctrl-C).
|
From one terminal, let's emulate the bluetooth device: (“1-1” is the pair bus-port for usb on the local machine, as we are emulating, it could be anything. It is only important because usbip tool will have to use the same name to request the emulated device)
From another terminal connect to the emulator: (localhost because emulator is running in the same equipment and the same name for pair bus-port as the emulator)
Now we can check that the new device is present: (as we saw previously, for this example machine, bus 3 is virtualized)
And we can turn off and turn on the emulated bluetooth device, detecting several fake bluetooth devices: (at this moment, fake bluetooth devices are not emulated/simulated so we can not set up)
In order to release resources:
And we can check that the device is released:
And of course, device is not detected (as before emulation):
After this, we can emulate again or stop the emulated device from the first terminal (i.e. with Ctrl-C).
|
When the real hardware and/or final device is not used to test, we can always feel insecure about the results, and this is the biggest hurdle that we will have to overcome to check the correct operation of the devices by means of emulation. So, in order to be confident, emulation must be as close as possible to the real hardware and in order to get the most real emulation every aspect of the device must be covered (or at least the necessary ones if they are not related with other aspects). In fact, for a correct test, we must not modify the driver, that is, we must only emulate the physical layer, so that the driver is not able to know if the device is real or emulated. Starting to test with the real hardware device is a very good idea to get a reference to build the emulator with the same features. For the case of USB devices, the device emulator building is easier because of the existing remotization procedure that complies with all the characteristics mentioned above.
|
USB device emulation is the best way to integrate and test the related features in an efficient, automatic and easy way. But, in order to be confident about the emulation procedure, device emulators need to be previously validated to confirm that they work in the same way as real hardware. Of course, the USB device emulator is not the same as the real hardware device, but the commented procedure, thanks to the tested remotization of the device, is very close to the real scenario and can help a lot to improve our release and testing processes. Finally, I would like to comment that one of the best advantages of using software emulators is that we will be able to cause specific behaviors, in a simple way, that would be very difficult to reproduce with real hardware, and this could help to find issues and be more robust.
|
|