qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [RFC 1/2] linux-headers: add virtio_can.h
@ 2025-10-31 15:56 Matias Ezequiel Vara Larsen
  2025-10-31 15:56 ` [RFC 2/2] Add vhost-user-can and vhost-user-can-pci devices Matias Ezequiel Vara Larsen
  2025-10-31 16:15 ` [RFC 1/2] linux-headers: add virtio_can.h Albert Esteve
  0 siblings, 2 replies; 4+ messages in thread
From: Matias Ezequiel Vara Larsen @ 2025-10-31 15:56 UTC (permalink / raw)
  To: qemu-devel
  Cc: Michael S. Tsirkin, Vikram Garhwal, Jason Wang, Paolo Bonzini,
	Francisco Iglesias, Cornelia Huck, Pavel Pisa,
	Matias Ezequiel Vara Larsen

Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
---
Note that virtio_can.h is not merged yet and it is part of the series
https://lore.kernel.org/all/aQJRnX7OpFRY%2F1+H@fedora/
---
 include/standard-headers/linux/virtio_can.h | 78 +++++++++++++++++++++
 1 file changed, 78 insertions(+)
 create mode 100644 include/standard-headers/linux/virtio_can.h

diff --git a/include/standard-headers/linux/virtio_can.h b/include/standard-headers/linux/virtio_can.h
new file mode 100644
index 0000000000..6380ef7bce
--- /dev/null
+++ b/include/standard-headers/linux/virtio_can.h
@@ -0,0 +1,78 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (C) 2021-2023 OpenSynergy GmbH
+ * Copyright Red Hat, Inc. 2025
+ */
+#ifndef _LINUX_VIRTIO_VIRTIO_CAN_H
+#define _LINUX_VIRTIO_VIRTIO_CAN_H
+
+#include <standard-headers/linux/types.h>
+#include <standard-headers/linux/virtio_types.h>
+#include <standard-headers/linux/virtio_ids.h>
+#include <standard-headers/linux/virtio_config.h>
+
+/* Feature bit numbers */
+#define VIRTIO_CAN_F_CAN_CLASSIC        0
+#define VIRTIO_CAN_F_CAN_FD             1
+#define VIRTIO_CAN_F_RTR_FRAMES         2
+#define VIRTIO_CAN_F_LATE_TX_ACK        3
+
+/* CAN Result Types */
+#define VIRTIO_CAN_RESULT_OK            0
+#define VIRTIO_CAN_RESULT_NOT_OK        1
+
+/* CAN flags to determine type of CAN Id */
+#define VIRTIO_CAN_FLAGS_EXTENDED       0x8000
+#define VIRTIO_CAN_FLAGS_FD             0x4000
+#define VIRTIO_CAN_FLAGS_RTR            0x2000
+
+#define VIRTIO_CAN_MAX_DLEN    64 // this is like CANFD_MAX_DLEN
+
+struct virtio_can_config {
+#define VIRTIO_CAN_S_CTRL_BUSOFF (1u << 0) /* Controller BusOff */
+	/* CAN controller status */
+	__le16 status;
+};
+
+/* TX queue message types */
+struct virtio_can_tx_out {
+#define VIRTIO_CAN_TX                   0x0001
+	__le16 msg_type;
+	__le16 length; /* 0..8 CC, 0..64 CAN-FD, 0..2048 CAN-XL, 12 bits */
+	__u8 reserved_classic_dlc; /* If CAN classic length = 8 then DLC can be 8..15 */
+	__u8 padding;
+	__le16 reserved_xl_priority; /* May be needed for CAN XL priority */
+	__le32 flags;
+	__le32 can_id;
+	__u8 sdu[] __counted_by(length);
+};
+
+struct virtio_can_tx_in {
+	__u8 result;
+};
+
+/* RX queue message types */
+struct virtio_can_rx {
+#define VIRTIO_CAN_RX                   0x0101
+	__le16 msg_type;
+	__le16 length; /* 0..8 CC, 0..64 CAN-FD, 0..2048 CAN-XL, 12 bits */
+	__u8 reserved_classic_dlc; /* If CAN classic length = 8 then DLC can be 8..15 */
+	__u8 padding;
+	__le16 reserved_xl_priority; /* May be needed for CAN XL priority */
+	__le32 flags;
+	__le32 can_id;
+	__u8 sdu[] __counted_by(length);
+};
+
+/* Control queue message types */
+struct virtio_can_control_out {
+#define VIRTIO_CAN_SET_CTRL_MODE_START  0x0201
+#define VIRTIO_CAN_SET_CTRL_MODE_STOP   0x0202
+	__le16 msg_type;
+};
+
+struct virtio_can_control_in {
+	__u8 result;
+};
+
+#endif /* #ifndef _LINUX_VIRTIO_VIRTIO_CAN_H */
-- 
2.42.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [RFC 2/2] Add vhost-user-can and vhost-user-can-pci devices
  2025-10-31 15:56 [RFC 1/2] linux-headers: add virtio_can.h Matias Ezequiel Vara Larsen
@ 2025-10-31 15:56 ` Matias Ezequiel Vara Larsen
  2025-10-31 16:15 ` [RFC 1/2] linux-headers: add virtio_can.h Albert Esteve
  1 sibling, 0 replies; 4+ messages in thread
From: Matias Ezequiel Vara Larsen @ 2025-10-31 15:56 UTC (permalink / raw)
  To: qemu-devel
  Cc: Michael S. Tsirkin, Vikram Garhwal, Jason Wang, Paolo Bonzini,
	Francisco Iglesias, Cornelia Huck, Pavel Pisa, Timos Ampelikiotis,
	Matias Ezequiel Vara Larsen

From: Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>

Add vhost-user-can device, which has been tested with latest
vhost-device-can (see
https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-can) and
the virtio-can driver v6 (see
https://lore.kernel.org/all/aQJRnX7OpFRY%2F1+H@fedora/).

Signed-off-by: Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
Tested-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
---
 hw/net/Kconfig                  |  5 +++
 hw/net/can/meson.build          |  3 ++
 hw/net/can/vhost-user-can-pci.c | 69 +++++++++++++++++++++++++++++++++
 hw/net/can/vhost-user-can.c     | 65 +++++++++++++++++++++++++++++++
 include/hw/net/vhost-user-can.h | 26 +++++++++++++
 5 files changed, 168 insertions(+)
 create mode 100644 hw/net/can/vhost-user-can-pci.c
 create mode 100644 hw/net/can/vhost-user-can.c
 create mode 100644 include/hw/net/vhost-user-can.h

diff --git a/hw/net/Kconfig b/hw/net/Kconfig
index 2b513d6895..c6fe5b4376 100644
--- a/hw/net/Kconfig
+++ b/hw/net/Kconfig
@@ -160,3 +160,8 @@ config CAN_CTUCANFD_PCI
     default y if PCI_DEVICES
     depends on PCI && CAN_CTUCANFD
     select CAN_BUS
+
+config VHOST_USER_CAN
+    bool
+    default y
+    depends on VIRTIO && VHOST_USER
diff --git a/hw/net/can/meson.build b/hw/net/can/meson.build
index 7382344628..1796a89820 100644
--- a/hw/net/can/meson.build
+++ b/hw/net/can/meson.build
@@ -4,5 +4,8 @@ system_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_pcm3680_pci.c'))
 system_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_mioe3680_pci.c'))
 system_ss.add(when: 'CONFIG_CAN_CTUCANFD', if_true: files('ctucan_core.c'))
 system_ss.add(when: 'CONFIG_CAN_CTUCANFD_PCI', if_true: files('ctucan_pci.c'))
+system_ss.add(when: 'CONFIG_VHOST_USER_CAN', if_true: files('vhost-user-can.c'))
+system_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_CAN'],
+                      if_true: files('vhost-user-can-pci.c'))
 system_ss.add(when: 'CONFIG_XLNX_ZYNQMP', if_true: files('xlnx-zynqmp-can.c'))
 system_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files('xlnx-versal-canfd.c'))
diff --git a/hw/net/can/vhost-user-can-pci.c b/hw/net/can/vhost-user-can-pci.c
new file mode 100644
index 0000000000..e525d28ab1
--- /dev/null
+++ b/hw/net/can/vhost-user-can-pci.c
@@ -0,0 +1,69 @@
+/*
+ * Vhost-user CAN virtio device PCI glue
+ *
+ * Copyright (c) 2024 Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/net/vhost-user-can.h"
+#include "hw/virtio/virtio-pci.h"
+
+struct VHostUserCanPCI {
+    VirtIOPCIProxy parent_obj;
+    VHostUserCan vdev;
+};
+
+typedef struct VHostUserCanPCI VHostUserCanPCI;
+
+#define TYPE_VHOST_USER_CAN_PCI "vhost-user-can-pci-base"
+
+DECLARE_INSTANCE_CHECKER(VHostUserCanPCI, VHOST_USER_CAN_PCI,
+                         TYPE_VHOST_USER_CAN_PCI)
+
+static void vhost_user_can_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
+{
+    VHostUserCanPCI *dev = VHOST_USER_CAN_PCI(vpci_dev);
+    DeviceState *vdev = DEVICE(&dev->vdev);
+
+    vpci_dev->nvectors = 1;
+
+    qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
+}
+
+static void vhost_user_can_pci_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+    k->realize = vhost_user_can_pci_realize;
+    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
+    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    pcidev_k->device_id = 0; /* Set by virtio-pci based on virtio id */
+    pcidev_k->revision = 0x00;
+    pcidev_k->class_id = PCI_CLASS_SERIAL_CANBUS;
+}
+
+static void vhost_user_can_pci_instance_init(Object *obj)
+{
+    VHostUserCanPCI *dev = VHOST_USER_CAN_PCI(obj);
+
+    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
+                                TYPE_VHOST_USER_CAN);
+}
+
+static const VirtioPCIDeviceTypeInfo vhost_user_can_pci_info = {
+    .base_name = TYPE_VHOST_USER_CAN_PCI,
+    .non_transitional_name = "vhost-user-can-pci",
+    .instance_size = sizeof(VHostUserCanPCI),
+    .instance_init = vhost_user_can_pci_instance_init,
+    .class_init = vhost_user_can_pci_class_init,
+};
+
+static void vhost_user_can_pci_register(void)
+{
+    virtio_pci_types_register(&vhost_user_can_pci_info);
+}
+
+type_init(vhost_user_can_pci_register);
diff --git a/hw/net/can/vhost-user-can.c b/hw/net/can/vhost-user-can.c
new file mode 100644
index 0000000000..acab1f64ee
--- /dev/null
+++ b/hw/net/can/vhost-user-can.c
@@ -0,0 +1,65 @@
+/*
+ * Vhost-user CAN virtio device
+ *
+ * Copyright (c) 2024 Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
+ *
+ * Simple wrapper of the generic vhost-user-device.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "hw/qdev-properties.h"
+#include "hw/virtio/virtio-bus.h"
+#include "hw/net/vhost-user-can.h"
+#include "standard-headers/linux/virtio_ids.h"
+#include "standard-headers/linux/virtio_can.h"
+
+static const VMStateDescription vu_can_vmstate = {
+    .name = "vhost-user-can",
+    .unmigratable = 1,
+};
+
+static const Property vcan_properties[] = {
+    DEFINE_PROP_CHR("chardev", VHostUserBase, chardev),
+};
+
+static void vu_can_base_realize(DeviceState *dev, Error **errp)
+{
+    VHostUserBase *vub = VHOST_USER_BASE(dev);
+    VHostUserBaseClass *vubs = VHOST_USER_BASE_GET_CLASS(dev);
+
+    vub->virtio_id = VIRTIO_ID_CAN;
+    vub->num_vqs = 3;
+    vub->config_size = sizeof(struct virtio_can_config);
+
+    vubs->parent_realize(dev, errp);
+}
+
+static void vu_can_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VHostUserBaseClass *vubc = VHOST_USER_BASE_CLASS(klass);
+
+    dc->vmsd = &vu_can_vmstate;
+    device_class_set_props(dc, vcan_properties);
+    device_class_set_parent_realize(dc, vu_can_base_realize,
+                                    &vubc->parent_realize);
+
+    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
+}
+
+static const TypeInfo vu_can_info = {
+    .name = TYPE_VHOST_USER_CAN,
+    .parent = TYPE_VHOST_USER_BASE,
+    .instance_size = sizeof(VHostUserCan),
+    .class_init = vu_can_class_init,
+};
+
+static void vu_can_register_types(void)
+{
+    type_register_static(&vu_can_info);
+}
+
+type_init(vu_can_register_types)
diff --git a/include/hw/net/vhost-user-can.h b/include/hw/net/vhost-user-can.h
new file mode 100644
index 0000000000..38bc261ce7
--- /dev/null
+++ b/include/hw/net/vhost-user-can.h
@@ -0,0 +1,26 @@
+/*
+ * Vhost-user Can virtio device
+ *
+ * Copyright (c) 2024 Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef QEMU_VHOST_USER_CAN_H
+#define QEMU_VHOST_USER_CAN_H
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/vhost.h"
+#include "hw/virtio/vhost-user.h"
+#include "hw/virtio/vhost-user-base.h"
+
+#define TYPE_VHOST_USER_CAN "vhost-user-can"
+OBJECT_DECLARE_SIMPLE_TYPE(VHostUserCan, VHOST_USER_CAN)
+
+struct VHostUserCan {
+    /*< private >*/
+    VHostUserBase parent;
+    /*< public >*/
+};
+
+#endif /* QEMU_VHOST_USER_CAN_H */
-- 
2.42.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RFC 1/2] linux-headers: add virtio_can.h
  2025-10-31 15:56 [RFC 1/2] linux-headers: add virtio_can.h Matias Ezequiel Vara Larsen
  2025-10-31 15:56 ` [RFC 2/2] Add vhost-user-can and vhost-user-can-pci devices Matias Ezequiel Vara Larsen
@ 2025-10-31 16:15 ` Albert Esteve
  2025-10-31 16:33   ` Cornelia Huck
  1 sibling, 1 reply; 4+ messages in thread
From: Albert Esteve @ 2025-10-31 16:15 UTC (permalink / raw)
  To: Matias Ezequiel Vara Larsen
  Cc: qemu-devel, Michael S. Tsirkin, Vikram Garhwal, Jason Wang,
	Paolo Bonzini, Francisco Iglesias, Cornelia Huck, Pavel Pisa

On Fri, Oct 31, 2025 at 5:00 PM Matias Ezequiel Vara Larsen
<mvaralar@redhat.com> wrote:
>
> Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
> ---
> Note that virtio_can.h is not merged yet and it is part of the series
> https://lore.kernel.org/all/aQJRnX7OpFRY%2F1+H@fedora/

Hi Matias,

As far as I know, standard-headers must be updated with
`./scripts/update-linux-headers.sh`, so unless exceptions can be it'll
have to wait until the kernel patch is merged for this to be accepted.


> ---
>  include/standard-headers/linux/virtio_can.h | 78 +++++++++++++++++++++
>  1 file changed, 78 insertions(+)
>  create mode 100644 include/standard-headers/linux/virtio_can.h
>
> diff --git a/include/standard-headers/linux/virtio_can.h b/include/standard-headers/linux/virtio_can.h
> new file mode 100644
> index 0000000000..6380ef7bce
> --- /dev/null
> +++ b/include/standard-headers/linux/virtio_can.h
> @@ -0,0 +1,78 @@
> +/* SPDX-License-Identifier: BSD-3-Clause */
> +/*
> + * Copyright (C) 2021-2023 OpenSynergy GmbH
> + * Copyright Red Hat, Inc. 2025
> + */
> +#ifndef _LINUX_VIRTIO_VIRTIO_CAN_H
> +#define _LINUX_VIRTIO_VIRTIO_CAN_H
> +
> +#include <standard-headers/linux/types.h>
> +#include <standard-headers/linux/virtio_types.h>
> +#include <standard-headers/linux/virtio_ids.h>
> +#include <standard-headers/linux/virtio_config.h>
> +
> +/* Feature bit numbers */
> +#define VIRTIO_CAN_F_CAN_CLASSIC        0
> +#define VIRTIO_CAN_F_CAN_FD             1
> +#define VIRTIO_CAN_F_RTR_FRAMES         2
> +#define VIRTIO_CAN_F_LATE_TX_ACK        3
> +
> +/* CAN Result Types */
> +#define VIRTIO_CAN_RESULT_OK            0
> +#define VIRTIO_CAN_RESULT_NOT_OK        1
> +
> +/* CAN flags to determine type of CAN Id */
> +#define VIRTIO_CAN_FLAGS_EXTENDED       0x8000
> +#define VIRTIO_CAN_FLAGS_FD             0x4000
> +#define VIRTIO_CAN_FLAGS_RTR            0x2000
> +
> +#define VIRTIO_CAN_MAX_DLEN    64 // this is like CANFD_MAX_DLEN
> +
> +struct virtio_can_config {
> +#define VIRTIO_CAN_S_CTRL_BUSOFF (1u << 0) /* Controller BusOff */
> +       /* CAN controller status */
> +       __le16 status;
> +};
> +
> +/* TX queue message types */
> +struct virtio_can_tx_out {
> +#define VIRTIO_CAN_TX                   0x0001
> +       __le16 msg_type;
> +       __le16 length; /* 0..8 CC, 0..64 CAN-FD, 0..2048 CAN-XL, 12 bits */
> +       __u8 reserved_classic_dlc; /* If CAN classic length = 8 then DLC can be 8..15 */
> +       __u8 padding;
> +       __le16 reserved_xl_priority; /* May be needed for CAN XL priority */
> +       __le32 flags;
> +       __le32 can_id;
> +       __u8 sdu[] __counted_by(length);
> +};
> +
> +struct virtio_can_tx_in {
> +       __u8 result;
> +};
> +
> +/* RX queue message types */
> +struct virtio_can_rx {
> +#define VIRTIO_CAN_RX                   0x0101
> +       __le16 msg_type;
> +       __le16 length; /* 0..8 CC, 0..64 CAN-FD, 0..2048 CAN-XL, 12 bits */
> +       __u8 reserved_classic_dlc; /* If CAN classic length = 8 then DLC can be 8..15 */
> +       __u8 padding;
> +       __le16 reserved_xl_priority; /* May be needed for CAN XL priority */
> +       __le32 flags;
> +       __le32 can_id;
> +       __u8 sdu[] __counted_by(length);
> +};
> +
> +/* Control queue message types */
> +struct virtio_can_control_out {
> +#define VIRTIO_CAN_SET_CTRL_MODE_START  0x0201
> +#define VIRTIO_CAN_SET_CTRL_MODE_STOP   0x0202
> +       __le16 msg_type;
> +};
> +
> +struct virtio_can_control_in {
> +       __u8 result;
> +};
> +
> +#endif /* #ifndef _LINUX_VIRTIO_VIRTIO_CAN_H */
> --
> 2.42.0
>
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC 1/2] linux-headers: add virtio_can.h
  2025-10-31 16:15 ` [RFC 1/2] linux-headers: add virtio_can.h Albert Esteve
@ 2025-10-31 16:33   ` Cornelia Huck
  0 siblings, 0 replies; 4+ messages in thread
From: Cornelia Huck @ 2025-10-31 16:33 UTC (permalink / raw)
  To: Albert Esteve, Matias Ezequiel Vara Larsen
  Cc: qemu-devel, Michael S. Tsirkin, Vikram Garhwal, Jason Wang,
	Paolo Bonzini, Francisco Iglesias, Pavel Pisa

On Fri, Oct 31 2025, Albert Esteve <aesteve@redhat.com> wrote:

> On Fri, Oct 31, 2025 at 5:00 PM Matias Ezequiel Vara Larsen
> <mvaralar@redhat.com> wrote:
>>
>> Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
>> ---
>> Note that virtio_can.h is not merged yet and it is part of the series
>> https://lore.kernel.org/all/aQJRnX7OpFRY%2F1+H@fedora/
>
> Hi Matias,
>
> As far as I know, standard-headers must be updated with
> `./scripts/update-linux-headers.sh`, so unless exceptions can be it'll
> have to wait until the kernel patch is merged for this to be accepted.

It's fine to have it as a placeholder patch for review (this series is
marked as RFC anyway) -- I'd recommend to put "Placeholder for headers
update, do not merge" more prominently in there, though.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-10-31 16:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-31 15:56 [RFC 1/2] linux-headers: add virtio_can.h Matias Ezequiel Vara Larsen
2025-10-31 15:56 ` [RFC 2/2] Add vhost-user-can and vhost-user-can-pci devices Matias Ezequiel Vara Larsen
2025-10-31 16:15 ` [RFC 1/2] linux-headers: add virtio_can.h Albert Esteve
2025-10-31 16:33   ` Cornelia Huck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).