CAN SJA100 controller emulation and SocketCAN based host CAN bus access
Subsystems (2)
cover CAN SJA100 controller emulation and SocketCAN based host CAN bus access
Hello everybody,
I have invested some time to wrap-up and get back on the track 2013 GSoC Jin Yang work on QEMU CAN bus emulation done in frame of RTEMS project preparation for future RTEMS CAN bus infrastructure preparation.
I think that code is important for much wider community.
RTEMS page for original QEMU 1.4 work
http://www.rtems.org/wiki/index.php/Qemu_simulations
Be warned, page is not yet updated for redesigned CAN infrastructure conecept
The patch series consists of
- CAN bus simple SJA1000 PCI card emulation for QEMU
Files:
- include/net/can_emu.h
- basic CAN bus related types. Those which could possibly clash with Linux kernel prepended by "qemu_".
- prototypes for CAN buses naming and clients registration
- original Jin Yang approach uses chardev, but that does not map well to little different semantics of CAN messages distribution. I have considered common vlan code but I have not found how to use it with CAN well too.
- hw/net/can_core.c
- implementation of CAN messages delivery
- support to connect into real host CAN bus network for case of Linux SocketCAN
- hw/net/can_sja1000.h
- declarations of SJA1000 CAN controller registers and connection type independent part of API
- hw/net/can_sja1000.c
- SJA1000 CAN controller registers and registers model implementation - hard part implemented by Jin Yang
- hw/net/can_pci.c
- connection of above infrastructure to the minimal PCI card with only one mmio BAR and no bridge interrupts setup and control. Unfortuantelly, I am not aware of any such straightforward card but it is great for testing and drivers porting. Used vendor and product ID are random ones chosen by Jin Yang, if there is some consensus that work worth for integration then I suggest to ask RedHat for unique PCI ID donation
- CAN bus Kvaser PCI CAN-S (single SJA1000 channel)
Files:
- hw/net/can_kvaser_pci.c
Implementation of
Testing:
I have implemented first simple single SJA1000 memory mapped PCI board support for LinCAN
Setup real or virtual can in host system
modprobe can-raw
modprobe vcan
ip link add dev can0 type vcan
ip link set can0 up
QEMU run with some subset of next parameters
$QEMU -enable-kvm -kernel /boot/vmlinuz-3.2.0-4-amd64 \
-initrd ramdisk.cpio \
-virtfs local,path=shareddir,security_model=none,mount_tag=shareddir \
-vga cirrus \
-device can_pci,model=SJA1000,canbus=canbus0,host=can0 \
-nographic -append "console=ttyS0"Run in the guest system
modprobe lincan hw=pcisja1000mm io=0
mdev -s
sendburst
readburst
Test with Kvaser
$QEMU -enable-kvm -kernel /boot/vmlinuz-3.2.0-4-amd64 \
-initrd ramdisk.cpio \
-virtfs local,path=shareddir,security_model=none,mount_tag=shareddir \
-vga cirrus \
-device kvaser_pci,canbus=canbus0,host=can0 \
-nographic -append "console=ttyS0"Run in the guest system
modprobe lincan hw=pcican-s io=0
mdev -s
SocketCAN in the guest system
No load lincan or rmmod lincan
Then SocketCAN in inner system setup
modprobe can-raw
modprobe kvaser-pci
ip link set can0 type can bitrate 1000000
ip link set can0 up
cangen can0 -e -I 123 -g 1000 -D 11223344DEADBEEF -L 8 candump can0
Visualization in host / outer system possible by QCANalyzer for example
http://ortcan.sourceforge.net/qcanalyzer/
Code is really hot and more or less proof of concept but worked for me stable for some while.
There is no buffering in CAN bus model for now so messages can be easily lost or overwritten. No CAN filters are in real use for now. Some filters union and application to the SocketCAN can be usesfull for future. But separation of CAN bus model out of chardev and or vlan allows to implement CAN specific things more easily.
Best wishes,
Pavel
--
Pavel Pisa
e-mail: pisa-/N2ztlQkxE7Ub/[email protected]
www: http://cmp.felk.cvut.cz/~pisa
university: http://dce.fel.cvut.cz/
company: http://www.pikron.com/
A message that is not in the archive (sent off-list or not yet received): 201405092010.19482.pisa-/N2ztlQkxE7Ub/[email protected]
PATCH 1/2 CAN bus simple SJA1000 PCI card emulation for QEMU
The work is based on Jin Yang GSoC 2013 work funded by Google and mentored in frame of RTEMS project GSoC slot donated to QEMU.
Update from QEMU-1.4 version and architecture cleanup by Pavel Pisa (Czech Technical University in Prague).
The core SJA1000 support is independent of provided PCI board. The simple core CAN bus infrastructure is independent as well.
Connection to the real host CAN bus network through SocketCAN network interface is available for Linux host system as well.
--- default-configs/pci.mak | 2 + hw/net/Makefile.objs | 4 + hw/net/can_core.c | 350 +++++++++++++++++++ hw/net/can_pci.c | 238 +++++++++++++ hw/net/can_sja1000.c | 878 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/net/can_sja1000.h | 163 +++++++++ include/net/can_emu.h | 123 +++++++ 7 files changed, 1758 insertions(+) create mode 100644 hw/net/can_core.c create mode 100644 hw/net/can_pci.c create mode 100644 hw/net/can_sja1000.c create mode 100644 hw/net/can_sja1000.h create mode 100644 include/net/can_emu.h
default-configs/pci.mak +2 -0
@@ -30,3 +30,5 @@ CONFIG_IPACK=y CONFIG_WDT_IB6300ESB=y CONFIG_PCI_TESTDEV=y CONFIG_NVME_PCI=y +CONFIG_CAN_SJA1000=y +CONFIG_CAN_PCI=y
hw/net/Makefile.objs +4 -0
@@ -24,6 +24,10 @@ common-obj-$(CONFIG_CADENCE) += cadence_gem.o common-obj-$(CONFIG_STELLARIS_ENET) += stellaris_enet.o common-obj-$(CONFIG_LANCE) += lance.o +obj-$(CONFIG_CAN_SJA1000) += can_core.o +obj-$(CONFIG_CAN_SJA1000) += can_sja1000.o +obj-$(CONFIG_CAN_PCI) += can_pci.o + obj-$(CONFIG_ETRAXFS) += etraxfs_eth.o obj-$(CONFIG_COLDFIRE) += mcf_fec.o obj-$(CONFIG_MILKYMIST) += milkymist-minimac2.o
hw/net/can_core.c new file +350 -0
350 lines not on this page · raw message
hw/net/can_pci.c new file +238 -0
238 lines not on this page · raw message
hw/net/can_sja1000.c new file +878 -0
878 lines not on this page · raw message
hw/net/can_sja1000.h new file +163 -0
163 lines not on this page · raw message
include/net/can_emu.h new file +123 -0
123 lines not on this page · raw message
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH 1/2] CAN bus simple SJA1000 PCI card emulation for QEMU
Hi Pavel,
Quoted 16 lines · Pavel Pisa
The work is based on Jin Yang GSoC 2013 work funded by Google and mentored in frame of RTEMS project GSoC slot donated to QEMU.
Update from QEMU-1.4 version and architecture cleanup by Pavel Pisa (Czech Technical University in Prague).
The core SJA1000 support is independent of provided PCI board. The simple core CAN bus infrastructure is independent as well.
Connection to the real host CAN bus network through SocketCAN network interface is available for Linux host system as well.
Signed-off-by Pavel Pisa <[email protected]>
Quoted 9 lines
--- default-configs/pci.mak | 2 + hw/net/Makefile.objs | 4 + hw/net/can_core.c | 350 +++++++++++++++++++ hw/net/can_pci.c | 238 +++++++++++++ hw/net/can_sja1000.c | 878 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/net/can_sja1000.h | 163 +++++++++ include/net/can_emu.h | 123 +++++++ 7 files changed, 1758 insertions(+)
That's a big patch. And it seems to add a new API/framework, then new users of that API. Can you add your core as a single patch, then incrementally bring your devices stuff as subsequent patches? My guess is this should be about 3 patches - are there any circular deps, requiring you to bring your three c files all at once or is there a logical order you can add them for ease of review?
Regards,
Peter
Re: [Qemu-devel] [PATCH 1/2] CAN bus simple SJA1000 PCI card emulation for QEMU
Am 12.05.2014 11:01, schrieb Peter Crosthwaite:
Quoted 4 lines · Pavel Pisa
Quoted 3 lines
The work is based on Jin Yang GSoC 2013 work funded by Google and mentored in frame of RTEMS project GSoC slot donated to QEMU.
Should/can that be recorded in form of his Signed-off-by before yours?
Quoted 22 lines
Quoted 12 lines
Update from QEMU-1.4 version and architecture cleanup by Pavel Pisa (Czech Technical University in Prague).
The core SJA1000 support is independent of provided PCI board. The simple core CAN bus infrastructure is independent as well.
Connection to the real host CAN bus network through SocketCAN network interface is available for Linux host system as well.
Signed-off-by Pavel Pisa <[email protected]>Quoted 9 lines
--- default-configs/pci.mak | 2 + hw/net/Makefile.objs | 4 + hw/net/can_core.c | 350 +++++++++++++++++++ hw/net/can_pci.c | 238 +++++++++++++ hw/net/can_sja1000.c | 878 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/net/can_sja1000.h | 163 +++++++++ include/net/can_emu.h | 123 +++++++ 7 files changed, 1758 insertions(+)
Quoted 6 lines
That's a big patch. And it seems to add a new API/framework, then new users of that API. Can you add your core as a single patch, then incrementally bring your devices stuff as subsequent patches? My guess is this should be about 3 patches - are there any circular deps, requiring you to bring your three c files all at once or is there a logical order you can add them for ease of review?
True. But before you resend, I'd like to hear Stefan H.'s view of whether CAN should go into hw/net/ at all or into, e.g., hw/can/.
Independently of the placement, it's always an interesting question of who will maintain this new infrastructure - I don't see any new MAINTAINERS entries getting added in either patch (NB: a diffstat in 0/2 would've been nice) for patch review.
Regards,
Andreas
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
PATCH 2/2 CAN bus Kvaser PCI CAN-S (single SJA1000 channel) emulation added.
In reply to Pavel Pisa--- hw/net/Makefile.objs | 1 + hw/net/can_kvaser_pci.c | 355 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 356 insertions(+) create mode 100644 hw/net/can_kvaser_pci.c
hw/net/Makefile.objs +1 -0
@@ -27,6 +27,7 @@ common-obj-$(CONFIG_LANCE) += lance.o obj-$(CONFIG_CAN_SJA1000) += can_core.o obj-$(CONFIG_CAN_SJA1000) += can_sja1000.o obj-$(CONFIG_CAN_PCI) += can_pci.o +obj-$(CONFIG_CAN_PCI) += can_kvaser_pci.o obj-$(CONFIG_ETRAXFS) += etraxfs_eth.o obj-$(CONFIG_COLDFIRE) += mcf_fec.o
hw/net/can_kvaser_pci.c new file +355 -0
355 lines not on this page · raw message
Signature · 1 line
1.9.1
On Fri, May 9, 2014 at 8:10 PM, Pavel Pisa <[email protected]> wrote:
Please run the patches through scripts/checkpatch.pl and address the warnings.
This patch doesn't use QEMU's network layer. Perhaps it should but I'm not familiar with CAN. The QEMU network layer implements a point-to-point model where NetClientState instances can send/receive packets. It seems a subset of this was reimplemented for CAN but I don't see much unique behavior in the core CAN code. Why didn't you use the QEMU network layer?
Quoted 13 lines
- CAN bus simple SJA1000 PCI card emulation for QEMU
Files:
-
include/net/can_emu.h
- basic CAN bus related types. Those which could possibly clash with Linux kernel prepended by "qemu_".
- prototypes for CAN buses naming and clients registration
- original Jin Yang approach uses chardev, but that does not map well to little different semantics of CAN messages distribution. I have considered common vlan code but I have not found how to use it with CAN well too.
Quoted 4 lines
-
hw/net/can_core.c
- implementation of CAN messages delivery
- support to connect into real host CAN bus network for case of Linux SocketCAN
The network layer implements (poorly) a flow control mechanism so that devices with limited buffers, like a USB network card, can pause receive until the guest has drained the receive buffer. I don't see that in the CAN code, so is it okay to drop frames?
About the Linux socket file descriptor:
1. It should be non-blocking because QEMU is an event-driven program. We must never block waiting to read/write data from/to a socket.
2. Missing EINTR error handling in case a signal interrupted a read(2)/write(2) system call.
In the QEMU network layer the can_core.c behavior would be produced using a hub with a tap or socket netdev. The broadcast code in the hub netdev is separate from the Linux-specific tap code. I think you can implement a CAN socket netdev and use the hub to broadcast.
Quoted 8 lines
-
hw/net/can_sja1000.h
- declarations of SJA1000 CAN controller registers and connection type independent part of API-
hw/net/can_sja1000.c
- SJA1000 CAN controller registers and registers model implementation - hard part implemented by Jin Yang
Quoted 9 lines
-
hw/net/can_pci.c
- connection of above infrastructure to the minimal PCI card with only one mmio BAR and no bridge interrupts setup and control. Unfortuantelly, I am not aware of any such straightforward card but it is great for testing and drivers porting. Used vendor and product ID are random ones chosen by Jin Yang, if there is some consensus that work worth for integration then I suggest to ask RedHat for unique PCI ID donation
What is the relationship between can_pci.c and the emulated Kvaser device?
Implementing one real device would be very important to establish that this CAN implementation can model real devices.
Stefan
Hello all,
the first much thanks to Stefan, Andreas and Peter for reply.
Next, should I keep linux-can in the talk
(it is against list cross-posting rule) or all potentially interrested participants agree to stay/follow this and future CAN+QEMU related topics on QEMU list?
I would remove RTEMS in the next post. Even that it is one of potential users, original "investor", internals of the emulation are out of this project scope.
Quoted 7 lines · On Monday 12 of May 2014 11:18:09 Andreas Färber wrote:
Am 12.05.2014 11:01, schrieb Peter Crosthwaite:
Quoted 4 lines · Pavel Pisa
Quoted 3 lines
The work is based on Jin Yang GSoC 2013 work funded by Google and mentored in frame of RTEMS project GSoC slot donated to QEMU.
Should/can that be recorded in form of his Signed-off-by before yours?
I try to do something with that. I would preffer to add him but I would like to have his confirmation beforehand. The GSoC 2013 work is available in his GitHub repo including negotiated final licence inclusion
https://github.com/Jin-Yang/QEMU-1.4.2
I was against to bothering you by patches for old QEMU version so I have postponed communication till I have time to bring it to the latest GIT. But there is significant redesign which unties changes from hardcoded modifications to QEMU character driver (original approach) and changes whole patches architecture substantially including omission of some functionality for beginning and probably introduction of new errors. So I think that I am not authorized to provide Signed-off-by Jin Yang on my own. But original work status and license is declared by Jin Yang and GSoC 2013.
Quoted 9 lines
Quoted 6 lines
That's a big patch. And it seems to add a new API/framework, then new users of that API. Can you add your core as a single patch, then incrementally bring your devices stuff as subsequent patches? My guess is this should be about 3 patches - are there any circular deps, requiring you to bring your three c files all at once or is there a logical order you can add them for ease of review?
True. But before you resend, I'd like to hear Stefan H.'s view of whether CAN should go into hw/net/ at all or into, e.g., hw/can/.
That is significant question and my sending of the patch series is mainly base to start discussion about this and support architecture. I need to learn mnore about QEMU internals and possible mechanisms to use from QEMU experts as well.
Quoted 4 lines
Independently of the placement, it's always an interesting question of who will maintain this new infrastructure - I don't see any new
MAINTAINERSentries getting added in either patch (NB: a diffstat in 0/2 would've been nice) for patch review.
I have personal interrest in CAN and control systems etc. which can be tracked more than 15 years back. But I have no funding for this/QEMU work and some of my other projects available. On the other hand, our group on Department of Control Enginnering of Czech Technical University have participated in many serious projects and we contribute to (ideally all open) CAN ecosystems.
CAN at Czech Technical University
https://rtime.felk.cvut.cz/can/
links our group projects and some small part of our university other departments CAN projects, which we have got some notice about.
I cannot speak for our group head if he provide support/allows to maintain QEMU CAN project by us. But I personally have interrest for this and I would apply for maintainership. But I/personally can provide only my spare time thought I have a problems with fast responses and time in general.
But I am keeping many open project alive or at least compilable for 15 years and more already.
https://www.ohloh.net/accounts/ppisa/positions
Quoted 11 lines · On Tuesday 13 of May 2014 14:29:08 Stefan Hajnoczi wrote:
On Fri, May 9, 2014 at 8:10 PM, Pavel Pisa <[email protected]> wrote:
Please run the patches through
scripts/checkpatch.pland address the warnings.This patch doesn't use QEMU's network layer. Perhaps it should but I'm not familiar with CAN. The QEMU network layer implements a point-to-point model where NetClientState instances can send/receive packets. It seems a subset of this was reimplemented for CAN but I don't see much unique behavior in the core CAN code. Why didn't you use the QEMU network layer?
Yes, that is main question. It would be great to use QEMU infrastructure for broadcasting messages/frames. I am not enough familiar with it and I would need some help to find how it could be used for CAN.
But on the other hand, there is question, if real behavior and messages ordering should be modelled for CAN or if we consider only some +/- working solution. If the behave of real CAN network is required later, it can be very hard to model it by infrastructure designed for other data flow.
In the full model case, each CanBusClient should publish its list of CAN communication objects ready for Tx and Rx. For Tx usually ordered by message IDs (representing priority in arbitration process). Global clients order for given bus should be recomputed for each change on clients list by client's change activated bus arbitration process.
That process should then select next message for transmition. Then it should be delivered to all CanBusClien's Rx communication objects which filters match CAN message ID.
The main problem (when compared to the real bus) is, that real bus does transfer rate limiting (max 1M bit/s - stuffing etc.). But in QEMU faster messages flow can overflow Rx buffers in situations which cannot lead to the overflow in real situation. So there should be some mechanism to check Rx object availability in clients. Than there should be mechanism to postpone data exchange in such case for at least time equivalent to real message presence on a wire (we have implemented computation for exact time required for message from its actual data including stuffing and CRC done for our industrial partner in other project).
The message Tx attempt expiration should be postponed for even longer when it is not accepted/confirmed by any target and error should be reported back to corresponding Tx object.
Some controllers provide "overload frame" response which should/could be included/emulated as well.
Controllers should evaluate and count Tx and Rx errors counts and change state appropriately.
I am not sure if all that can be emulated by network QEMU subsystem or if required changes are acceptable.
I do not know if it worth to consider all this at all.
My actual goal was to have something simple which works and can be used for drivers implementors (i.e. RTEMS) to get into state when first message is sent and received and then switch to real HW.
And yes, it would be great to have all this implemented to can do drivers testing and correctness assessment in QEMU.
I am able to think about infrastructure which allows such extensions in future. But real implementation is muti-man-year project which needs contribution from more people.
So at the end, I am not sure if net or not-net. I expected some objections to addition of subsystem. If you agree with hw/can than I would incline to separation of code which needs to follow CAN infrastructure development caused changes together.
Quoted 18 lines
Quoted 13 lines
- CAN bus simple SJA1000 PCI card emulation for QEMU
Files:
-
include/net/can_emu.h
- basic CAN bus related types. Those which could possibly clash with Linux kernel prepended by "qemu_".
- prototypes for CAN buses naming and clients registration
- original Jin Yang approach uses chardev, but that does not map well to little different semantics of CAN messages distribution. I have considered common vlan code but I have not found how to use it with CAN well too.Quoted 4 lines
-
hw/net/can_core.c
- implementation of CAN messages delivery
- support to connect into real host CAN bus network for case of Linux SocketCAN
Quoted 4 lines
The network layer implements (poorly) a flow control mechanism so that devices with limited buffers, like a USB network card, can pause receive until the guest has drained the receive buffer. I don't see that in the CAN code, so is it okay to drop frames?
There is no such mechanism in our implementation now. But it is desirable in longer run an there was some minimal solution in original Jin Yang's char device code.
Quoted 5 lines
About the Linux socket file descriptor:
1. It should be non-blocking because QEMU is an event-driven program. We must never block waiting to read/write data from/to a socket.
2. Missing EINTR error handling in case a signal interrupted a read(2)/write(2) system call.
Yes, that are next steps or other option is to start delivery thread for each CAN bus. Which could be natural solution for real bus behavior simulation.
Are there some reasons against use of separate thread for this purpose?
Even such thread could call actual syscall nonblockin to can timeout and report errors. But it could be simpler to program and parallelize mesages delivery with emulated CPU run.
Quoted 4 lines
In the QEMU network layer the
can_core.cbehavior would be produced using a hub with a tap or socket netdev. The broadcast code in the hub netdev is separate from the Linux-specific tap code. I think you can implement a CAN socket netdev and use the hub to broadcast.
I need to learn more about QEMU internals and reusable features. Thank for suggestions.
Quoted 18 lines
Quoted 8 lines
-
hw/net/can_sja1000.h
- declarations of SJA1000 CAN controller registers and connection type independent part of API-
hw/net/can_sja1000.c
- SJA1000 CAN controller registers and registers model implementation - hard part implemented by Jin YangQuoted 9 lines
-
hw/net/can_pci.c
- connection of above infrastructure to the minimal PCI card with only one mmio BAR and no bridge interrupts setup and control. Unfortuantelly, I am not aware of any such straightforward card but it is great for testing and drivers porting. Used vendor and product ID are random ones chosen by Jin Yang, if there is some consensus that work
worth for integration then I suggest to ask RedHat for unique PCI ID donation
Quoted 4 lines
What is the relationship between
can_pci.cand the emulated Kvaser device?Implementing one real device would be very important to establish that this CAN implementation can model real devices.
There is broad range of CAN controllers, we have selected one famous CAN discrete controller chip SJA1000. Problem is, that it predates PCI and is intended to be used with 8051 like multiplexed bus.
It is still one of the most used chips on addons cards. CAN is common on SoC and MCUs and they use many different derivations or completely different registers models. But there is really very little other CAN controller chips available for addon cards (OKI, discontinued Intel, ...). So SJA1000 is reasonable choice for our QEMU effort. But it has to be used in combination with some PCI-local bus bridge. But these bridges are quite strange, sometimes reused chip for old ISDN solutions etc. So they need special configuration to access local bus connected CAN controller and mainly to setup routing of interrupts.
Emulation of PCI-local bus bridge complicates both sides in action - QEMU emulation side and developed CAN drivers side. That is why we have "designed" basic card with clean implementation directly mapping SJA1000 chip into PCI memory BAR with direct, level triggered routing of interrupt to corresponding "PCI board" A pin.
When we have confirmed that this works together with modified LinCAN driver I have started to implement model corresponding to real PCI CAN card. I have selected Kvaser card because we pose more of these at university/so I can check real contents of other configuration/support spaces/BARs on real device. The advantage of that selection is that driver for this card is included in mainline Linux in CAN/SocketCAN subsystem. This board can have up to four SJA1000 chips in one of its BARs, The board routes interrupts through bride interrupt control/masking and configuration, which has to be emulated as well.
It even maps chips into I/O space instead of MMIO.
I think that for drivers writers or even can infrastructure developers (userspace and higher level tools) it is advantage to have simple solution with corresponding driver in a guest kernel (something like virtio). On the other hand, concrete real hardware driver testing requires exact hardware complexity emulation and allows to use unmodified OS and available drivers.
But if you think, that this artificial simple card design is not good idea for QEMU, then it can be discarded and only Kvaser (when it works now) included.
The decisions for further development
Should be minimal working solution included in the QEMU mainline in short term?
(months .. or rather wait for agreement on final infrastructure, may be years because of our other load and complexity of full model task)
Is preferred approach to open CAN QEMU fork on GitHub? Etc...
I consider as a good result, that my actual attempt and QEMU offered infrastructure allows to to provide minimal testable solution with no QEMU modifications (except Makefiles). So I hope that is is reasonable start and our effort would be of some use for others.
Best wishes,
Pavel
PS: I am not sure if I will be online till Monday
Hi, all
I just read this e-mail today. didn't check the e-mail for a long time, because a little busy on the graduation stuffs. I still want to do something on this project. So, i would like to working on anything i can help.
Thanks,
Jin Yang
From: Pavel Pisa
Date: 2014-05-15 21:53
To: Stefan Hajnoczi; AndreasFärber; Peter Crosthwaite
CC: [email protected]; Oliver Hartkopp A; jinyang.sia; Stefan Weil; rtems-devel; linux-can
Subject: Re: [Qemu-devel] [PATCH 0/2] CAN SJA100 controller emulation and SocketCAN based host CAN bus access Hello all,
the first much thanks to Stefan, Andreas and Peter for reply.
Next, should I keep linux-can in the talk
(it is against list cross-posting rule) or all potentially interrested participants agree to stay/follow this and future CAN+QEMU related topics on QEMU list?
I would remove RTEMS in the next post. Even that it is one of potential users, original "investor", internals of the emulation are out of this project scope.
Quoted 7 lines · On Monday 12 of May 2014 11:18:09 Andreas Färber wrote:
Am 12.05.2014 11:01, schrieb Peter Crosthwaite:
Quoted 4 lines · Pavel Pisa
Quoted 3 lines
The work is based on Jin Yang GSoC 2013 work funded by Google and mentored in frame of RTEMS project GSoC slot donated to QEMU.
Should/can that be recorded in form of his Signed-off-by before yours?
I try to do something with that. I would preffer to add him but I would like to have his confirmation beforehand. The GSoC 2013 work is available in his GitHub repo including negotiated final licence inclusion
https://github.com/Jin-Yang/QEMU-1.4.2
I was against to bothering you by patches for old QEMU version so I have postponed communication till I have time to bring it to the latest GIT. But there is significant redesign which unties changes from hardcoded modifications to QEMU character driver (original approach) and changes whole patches architecture substantially including omission of some functionality for beginning and probably introduction of new errors. So I think that I am not authorized to provide Signed-off-by Jin Yang on my own. But original work status and license is declared by Jin Yang and GSoC 2013.
Quoted 9 lines
Quoted 6 lines
That's a big patch. And it seems to add a new API/framework, then new users of that API. Can you add your core as a single patch, then incrementally bring your devices stuff as subsequent patches? My guess is this should be about 3 patches - are there any circular deps, requiring you to bring your three c files all at once or is there a logical order you can add them for ease of review?
True. But before you resend, I'd like to hear Stefan H.'s view of whether CAN should go into hw/net/ at all or into, e.g., hw/can/.
That is significant question and my sending of the patch series is mainly base to start discussion about this and support architecture. I need to learn mnore about QEMU internals and possible mechanisms to use from QEMU experts as well.
Quoted 4 lines
Independently of the placement, it's always an interesting question of who will maintain this new infrastructure - I don't see any new
MAINTAINERSentries getting added in either patch (NB: a diffstat in 0/2 would've been nice) for patch review.
I have personal interrest in CAN and control systems etc. which can be tracked more than 15 years back. But I have no funding for this/QEMU work and some of my other projects available. On the other hand, our group on Department of Control Enginnering of Czech Technical University have participated in many serious projects and we contribute to (ideally all open) CAN ecosystems.
CAN at Czech Technical University
https://rtime.felk.cvut.cz/can/
links our group projects and some small part of our university other departments CAN projects, which we have got some notice about.
I cannot speak for our group head if he provide support/allows to maintain QEMU CAN project by us. But I personally have interrest for this and I would apply for maintainership. But I/personally can provide only my spare time thought I have a problems with fast responses and time in general.
But I am keeping many open project alive or at least compilable for 15 years and more already.
https://www.ohloh.net/accounts/ppisa/positions
Quoted 11 lines · On Tuesday 13 of May 2014 14:29:08 Stefan Hajnoczi wrote:
On Fri, May 9, 2014 at 8:10 PM, Pavel Pisa <[email protected]> wrote:
Please run the patches through
scripts/checkpatch.pland address the warnings.This patch doesn't use QEMU's network layer. Perhaps it should but I'm not familiar with CAN. The QEMU network layer implements a point-to-point model where NetClientState instances can send/receive packets. It seems a subset of this was reimplemented for CAN but I don't see much unique behavior in the core CAN code. Why didn't you use the QEMU network layer?
Yes, that is main question. It would be great to use QEMU infrastructure for broadcasting messages/frames. I am not enough familiar with it and I would need some help to find how it could be used for CAN.
But on the other hand, there is question, if real behavior and messages ordering should be modelled for CAN or if we consider only some +/- working solution. If the behave of real CAN network is required later, it can be very hard to model it by infrastructure designed for other data flow.
In the full model case, each CanBusClient should publish its list of CAN communication objects ready for Tx and Rx. For Tx usually ordered by message IDs (representing priority in arbitration process). Global clients order for given bus should be recomputed for each change on clients list by client's change activated bus arbitration process.
That process should then select next message for transmition. Then it should be delivered to all CanBusClien's Rx communication objects which filters match CAN message ID.
The main problem (when compared to the real bus) is, that real bus does transfer rate limiting (max 1M bit/s - stuffing etc.). But in QEMU faster messages flow can overflow Rx buffers in situations which cannot lead to the overflow in real situation. So there should be some mechanism to check Rx object availability in clients. Than there should be mechanism to postpone data exchange in such case for at least time equivalent to real message presence on a wire (we have implemented computation for exact time required for message from its actual data including stuffing and CRC done for our industrial partner in other project).
The message Tx attempt expiration should be postponed for even longer when it is not accepted/confirmed by any target and error should be reported back to corresponding Tx object.
Some controllers provide "overload frame" response which should/could be included/emulated as well.
Controllers should evaluate and count Tx and Rx errors counts and change state appropriately.
I am not sure if all that can be emulated by network QEMU subsystem or if required changes are acceptable.
I do not know if it worth to consider all this at all.
My actual goal was to have something simple which works and can be used for drivers implementors (i.e. RTEMS) to get into state when first message is sent and received and then switch to real HW.
And yes, it would be great to have all this implemented to can do drivers testing and correctness assessment in QEMU.
I am able to think about infrastructure which allows such extensions in future. But real implementation is muti-man-year project which needs contribution from more people.
So at the end, I am not sure if net or not-net. I expected some objections to addition of subsystem. If you agree with hw/can than I would incline to separation of code which needs to follow CAN infrastructure development caused changes together.
Quoted 18 lines
Quoted 13 lines
- CAN bus simple SJA1000 PCI card emulation for QEMU
Files:
-
include/net/can_emu.h
- basic CAN bus related types. Those which could possibly clash with Linux kernel prepended by "qemu_".
- prototypes for CAN buses naming and clients registration
- original Jin Yang approach uses chardev, but that does not map well to little different semantics of CAN messages distribution. I have considered common vlan code but I have not found how to use it with CAN well too.Quoted 4 lines
-
hw/net/can_core.c
- implementation of CAN messages delivery
- support to connect into real host CAN bus network for case of Linux SocketCAN
Quoted 4 lines
The network layer implements (poorly) a flow control mechanism so that devices with limited buffers, like a USB network card, can pause receive until the guest has drained the receive buffer. I don't see that in the CAN code, so is it okay to drop frames?
There is no such mechanism in our implementation now. But it is desirable in longer run an there was some minimal solution in original Jin Yang's char device code.
Quoted 5 lines
About the Linux socket file descriptor:
1. It should be non-blocking because QEMU is an event-driven program. We must never block waiting to read/write data from/to a socket.
2. Missing EINTR error handling in case a signal interrupted a read(2)/write(2) system call.
Yes, that are next steps or other option is to start delivery thread for each CAN bus. Which could be natural solution for real bus behavior simulation.
Are there some reasons against use of separate thread for this purpose?
Even such thread could call actual syscall nonblockin to can timeout and report errors. But it could be simpler to program and parallelize mesages delivery with emulated CPU run.
Quoted 4 lines
In the QEMU network layer the
can_core.cbehavior would be produced using a hub with a tap or socket netdev. The broadcast code in the hub netdev is separate from the Linux-specific tap code. I think you can implement a CAN socket netdev and use the hub to broadcast.
I need to learn more about QEMU internals and reusable features. Thank for suggestions.
Quoted 18 lines
Quoted 8 lines
-
hw/net/can_sja1000.h
- declarations of SJA1000 CAN controller registers and connection type independent part of API-
hw/net/can_sja1000.c
- SJA1000 CAN controller registers and registers model implementation - hard part implemented by Jin YangQuoted 9 lines
-
hw/net/can_pci.c
- connection of above infrastructure to the minimal PCI card with only one mmio BAR and no bridge interrupts setup and control. Unfortuantelly, I am not aware of any such straightforward card but it is great for testing and drivers porting. Used vendor and product ID are random ones chosen by Jin Yang, if there is some consensus that work
worth for integration then I suggest to ask RedHat for unique PCI ID donation
Quoted 4 lines
What is the relationship between
can_pci.cand the emulated Kvaser device?Implementing one real device would be very important to establish that this CAN implementation can model real devices.
There is broad range of CAN controllers, we have selected one famous CAN discrete controller chip SJA1000. Problem is, that it predates PCI and is intended to be used with 8051 like multiplexed bus.
It is still one of the most used chips on addons cards. CAN is common on SoC and MCUs and they use many different derivations or completely different registers models. But there is really very little other CAN controller chips available for addon cards (OKI, discontinued Intel, ...). So SJA1000 is reasonable choice for our QEMU effort. But it has to be used in combination with some PCI-local bus bridge. But these bridges are quite strange, sometimes reused chip for old ISDN solutions etc. So they need special configuration to access local bus connected CAN controller and mainly to setup routing of interrupts.
Emulation of PCI-local bus bridge complicates both sides in action - QEMU emulation side and developed CAN drivers side. That is why we have "designed" basic card with clean implementation directly mapping SJA1000 chip into PCI memory BAR with direct, level triggered routing of interrupt to corresponding "PCI board" A pin.
When we have confirmed that this works together with modified LinCAN driver I have started to implement model corresponding to real PCI CAN card. I have selected Kvaser card because we pose more of these at university/so I can check real contents of other configuration/support spaces/BARs on real device. The advantage of that selection is that driver for this card is included in mainline Linux in CAN/SocketCAN subsystem. This board can have up to four SJA1000 chips in one of its BARs, The board routes interrupts through bride interrupt control/masking and configuration, which has to be emulated as well.
It even maps chips into I/O space instead of MMIO.
I think that for drivers writers or even can infrastructure developers (userspace and higher level tools) it is advantage to have simple solution with corresponding driver in a guest kernel (something like virtio). On the other hand, concrete real hardware driver testing requires exact hardware complexity emulation and allows to use unmodified OS and available drivers.
But if you think, that this artificial simple card design is not good idea for QEMU, then it can be discarded and only Kvaser (when it works now) included.
The decisions for further development
Should be minimal working solution included in the QEMU mainline in short term?
(months .. or rather wait for agreement on final infrastructure, may be years because of our other load and complexity of full model task)
Is preferred approach to open CAN QEMU fork on GitHub? Etc...
I consider as a good result, that my actual attempt and QEMU offered infrastructure allows to to provide minimal testable solution with no QEMU modifications (except Makefiles). So I hope that is is reasonable start and our effort would be of some use for others.
Best wishes,
Pavel
PS: I am not sure if I will be online till Monday
_______________________________________________
rtems-devel mailing list [email protected]
http://www.rtems.org/mailman/listinfo/rtems-devel
Quoted 10 lines · Pavel Pisa
The decisions for further development
Should be minimal working solution included in the QEMU mainline in short term?
(months .. or rather wait for agreement on final infrastructure, may be years because of our other load and complexity of full model task)Is preferred approach to open CAN QEMU fork on GitHub? Etc...
It sounds like there is doubt about whether anyone has enough time to implement CAN more fully. I'm not thrilled about reviewing patches if it's a partial implementation with few end users - something like that can be kept out-of-tree until someone with enough resources can polish it and push it upstream.
A few more comments about the network subsystem:
The QEMU network subsystem doesn't emulate Ethernet. It's just a way to connect an emulated NIC with a host netdev (tap, socket, etc) with a few extra services like link down/up, flow control, etc.
In the CAN world it would connect a CAN controller (e.g. Kvaser PCI) with a host device (e.g. using libcan or whatever). I don't think it's necessary to emulate bus arbitration in this model, that should happen via the host device (which is talking to an real CAN bus).
The question is whether the network subsystem's send/receive model works or whether you need something CAN-specific like publishing RX/TX objects along with their metadata (IDs, priorities, deadlines, etc). That would be a major difference and it would probably make sense it implement it as a separate subsystem.
Stefan
Hello Stefan,
Quoted 9 lines · On Friday 23 of May 2014 11:42:25 Stefan Hajnoczi wrote:
Quoted 8 lines · Pavel Pisa
The decisions for further development
Should be minimal working solution included in the QEMU mainline in short term?
(months .. or rather wait for agreement on final infrastructure, may be years because of our other load and complexity of full model task)
Quoted 8 lines
Quoted 2 lines
Is preferred approach to open CAN QEMU fork on GitHub? Etc...
It sounds like there is doubt about whether anyone has enough time to implement CAN more fully. I'm not thrilled about reviewing patches if it's a partial implementation with few end users - something like that can be kept out-of-tree until someone with enough resources can polish it and push it upstream.
I can maintain only my available time for this work for now. A demand for state equivalent to QEMU standard network subsystem developed already for 10 years by many people is quite strict requirement for discussion about this project.
I hope that inform/release early approach can help to join forces and comments from QEMU experts helps to not invest into dead ends.
As for the users, CAN is fundamental communication backbone of allmost all (expect 99%) todays vehicles. Without this support QEMU is useless for development or testing of automotive control units (today quiet often based on PowerPC or ARM). There is expected some movement from CAN to other standards in future (FlexRay, Flexible Datarate CAN and may it be some variant of Real-Time ETHERNET). But FD CAN is extension of CAN which can be integrated into same infrastructure and other two are much more complex and I do not expect that they would phase out CAN completely. There are 6 to 10 or more CAN busses in the today cars and for smaller MCUs it is much more cost and resources effective than others options. CAN will be used there for decades. CAN is used in many industry automation and robotic systems, railway vehicles etc. as well. This is prevalent bus (not counting UART and board local I2C and SPI) interface integrated to todays MCUs. CAN is not found on mobile chips where USB often is present. But even some of chips used even in mobile devices (i.e. i.MX6) have CAN bus integrated.
So there is HUGE base of potential users who would gain from such support. Other missing part for industrial control is support of data acquisition cards emulation.
We have started some effort even in this direction
https://rtime.felk.cvut.cz/hw/index.php/Humusoft_MF6xx#QEMU_Emulation_of_MF624
So I think that our contribution can be really valuable.
Quoted 10 lines
A few more comments about the network subsystem:
The QEMU network subsystem doesn't emulate Ethernet. It's just a way to connect an emulated NIC with a host netdev (tap, socket, etc) with a few extra services like link down/up, flow control, etc.
In the CAN world it would connect a CAN controller (e.g. Kvaser PCI) with a host device (e.g. using libcan or whatever). I don't think it's necessary to emulate bus arbitration in this model, that should happen via the host device (which is talking to an real CAN bus).
This is not so easy. CAN has critical property of priority based deterministic arbitration. I can imagine that such property could be directly mapped to the host in the case of SocketCAN. This would result in mapping each communication object of emulated controller (one Tx one Rx in the case of SJA1000 but up to 32 or 128 for C_CAN, HCAN2, FlexCAN, etc.) to one socket on the host side. But such model would be quite resources demanding a there is question what to do with unfortunate users of Windows where CAN API is usually proprietary for each card vendor. The demand of CAN support on host side would break one significant reason to use QEMU. Because QEMU should allow to do development for CAN equipped guests even on systems without CAN support. For Linux, there is VCAN for such case although and there are approaches (not with single standard yet) to tunnel CAN over ETHERNET.
On the other hand, including full CAN bus emulation allows to use QEMU directly even on host without CAN support and even to emulate some standard CAN devices directly in QEMU
or by some pluging which could be simpler than requirement to setup VCAN (usually root access required) or some daemons communicating over TCP IP or raw ETHERNET frames/VLANs. I think that for I2C, SPI and some other embedded targets support it is implemented that way in QEMU already.
But I am not sure what is the best approach now.
Quoted 5 lines
The question is whether the network subsystem's send/receive model works or whether you need something CAN-specific like publishing RX/TX objects along with their metadata (IDs, priorities, deadlines, etc). That would be a major difference and it would probably make sense it implement it as a separate subsystem.
As I have re-analyzed above, important properties can
be preserved through pass through to the host side but complexity would show somewhere else. As for future (and may it be quite demanded) FD CAN support, this would result in problems to use SocketCAN on actual distributions stable kernels because FD CAN support matures only in actual development ones.
Anyway, I would be happy if some perspective approach can be found. I understand that you want stable and long term maintainable and supported solution and that we should prove usefulness of our approach. But I do not like classical situation where there is no infomation about effort target and more teams start independently from different old stable branches and do not care about mainlining or following what is opinion of core project members. Such project are usually good for some research funding final presentation and then they hide in black hole. I have found some pledge to develop complex-complete CAN emulation framework on the net in one research project from 1010. But I have not found any code or documentation about it. This is waste of work and money. That is why I want to find agreement where to put code. We have many public GITs at our department server but this is project which expects more contributors so I prefer some more neutral location, GitHub (or SF.net) and some agreement to direct people to look there before they start from zero again
so that at least they consider to reuse work already done. I.e. such reuse at least happened in case of our LinCAN and later alternative wining SocketCAN development.
I would be happy if you are not against use of qemu-devel list at least to coordinate/help with changes required to keep separate branch/fork compatible/in-sync with core QEMU changes.
Thanks for comments and providing your options how to proceeded with our effort.
Best wishes,
Pavel