Demonstrating CAN support in Renode with SocketCAN and Wireshark
Published:
Topics: Open networking, Open simulation
The CAN (Controller Area Network) bus in its various flavors is used in environments such as automotive and space where a high level of reliability is required. Antmicro’s open source Renode simulation framework lets you build simulation environments which include multiple devices connected into a simulated CAN network, use Wireshark to inspect resulting traffic thanks to a built-in integration, and perform complex tests without the hassle of building dedicated hardware rigs.
We’ve already featured a sample scenario simulating a typical CAN network in our Tesla Roadster article earlier this year. To provide an even better starting point for working with CAN device networks, in this article we will describe how to set up a CAN network and how to connect it to a virtual CAN interface on your host with the recently developed Renode SocketCAN bridge. SocketCAN is essentially a Linux implementation of CAN protocols allowing you to use CAN connections as a network interface from the OS perspective, and it is now available as yet another method of host-guest communication provided by Renode.
Setting up a CAN network in Renode
To illustrate how easy it is to set up a CAN network in Renode, we created a demo that utilizes the Zephyr ISO-TP sample running on two connected STM32H753 machines.
To begin with, install Renode. For example, you can use the portable package for Linux:
mkdir renode_portable
wget https://builds.renode.io/renode-latest.linux-portable.tar.gz
tar xf renode-latest.linux-portable.tar.gz -C renode_portable --strip-components=1
Then open the Renode Monitor window:
$ ./renode_portable/renode
The first step is to create the CAN Bus itself using a virtual “hub” device, using the following command in the Renode Monitor:
(monitor) emulation CreateCANHub "canHub"
Then we set the bin
variable with a link to the chosen binary. You can use a precompiled demo by providing a URL:
(monitor) set bin @https://dl.antmicro.com/projects/renode/nucleo_h743zi--zephyr-samples-subsys-canbus-isotp.elf-s_1554512-549bcf52da77937f5c7a86dd407d3e9599e40938
To set up the simulated STM32H753 machines used in this demo, include the platform description and load the binary:
(monitor) mach create “machine-0”
(machine-0) include @platforms/cpus/stm32h753.repl
(machine-0) sysbus LoadELF $bin
The next step is to connect the CAN peripheral on the simulated STM32H753 to the shared bus between the machines:
(machine-0) connector Connect sysbus.fdcan1 canHub
At this point, we also show the UART console window so that we can display the output of the demo and to provide confirmation of the completed CAN transmission:
(machine-0) showAnalyzer sysbus.usart3
Then repeat these steps to set up the second machine:
(machine-0) mach create "machine-1"
(machine-1) include @platforms/cpus/stm32h753.repl
(machine-1) sysbus LoadELF $bin
(machine-1) connector Connect sysbus.fdcan1 canHub
Renode provides determinism of execution even in multi-node systems, e.g. by delaying some communication. In this particular scenario it’s beneficial to synchronize nodes more often than what Renode offers by default, but it can be easily adjusted. Below, we can see that for our demo, it is set to 25 virtual microseconds. Please note that more frequent synchronization may affect the overall performance.
(machine-1) emulation SetGlobalQuantum "0.000025"
Finally, to open Wireshark for the purposes of logging the CAN traffic, call:
(machine-1) emulation LogCANTraffic
The full demo can be seen below, running in Renode. The sample software prints out a log upon the completion of the transmission. The received data is a large payload with ASCII art showing the “ISO-TP” text and a small payload with a short text, and both payloads are also printed out.
External integration with Wireshark and hosts
Renode already features Wireshark integration for other protocols, so it was a logical extension to include the ability to inspect internal CAN network traffic for the purpose of identifying communication problems in highly complex setups.
The integration itself required us to translate our internal CAN frame representation to SocketCAN in order to pass traffic to Wireshark. This is done by communicating directly with Wireshark via the packet capture protocol (pcap), allowing us to avoid routing communication via the host machine, reducing the overhead and simplifying the setup.
The integration starts Wireshark from within the Renode framework and without any need for additional configuration, eliminating the need for the user to select which interface should be inspected upon launch.
Linux host integration is also supported in Renode, meaning that simulated networks can also be connected to the host’s vcan interface, allowing for seamless integration into existing configurations, tooling and workflows as just another vcan
interface.
In order to create a virtual CAN bus between Renode and the host machine, you’ll need to connect a virtual CAN interface on the host side to the SocketCAN bridge in Renode.
On the host side, ensure that the vcan
module is loaded first:
# modprobe vcan
Then, to set up the virtual CAN Bus, named vcan0
, run the following commands:
# ip link add dev vcan0 type vcan
# ip link set up vcan0
To create the CAN bridge to the host and connect it with the interface named vcan0
as well as to connect it to the internal network, use the following commands in the Renode Monitor:
(machine-1) machine CreateSocketCANBridge "socketcan" "vcan0"
(machine-1) connector Connect socketcan canHub
A SocketCAN bridge demo is also available on the Renode GitHub page.
Enabling fully deterministic testing environments for developing safety critical applications
Renode makes it possible to create and test multi-node systems that use a range of devices, SoCs and peripherals, connected using protocols such as Ethernet, Bluetooth and CAN.
By using Renode, developers can quickly create and test complex heterogeneous systems that meet safety and security requirements in high-tech industries like automotive and space where reliable and thorough testing is paramount. By analyzing and recording traffic between different nodes on different buses for the purposes of regression testing, it is also possible to identify and solve communication problems in complex heterogeneous setups to iron out issues early in the development cycle and advance system integration.
If you would like to take advantage of the fully deterministic and highly modular development environment Renode provides to develop, simulate and test your next system, get in touch at contact@antmicro.com.