Skip to contentSkip to conversation

Virtio support for toolstack on Arm (Was "IOREQ feature (+ virtio-mmio) on Arm")

PATCH Superseded Oleksandr Tyshchenko 3 participants 10 messages May 3, 2022 – May 20, 2022

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 cover Virtio support for toolstack on Arm (Was "IOREQ feature (+ virtio-mmio) on Arm")

From: Oleksandr Tyshchenko <[email protected]>

Hello all.

The purpose of this patch series is to add missing virtio-mmio bits to Xen toolstack on Arm. The Virtio support for toolstack [1] was postponed as the main target was to upstream IOREQ/DM support on Arm in the first place. Now, we already have IOREQ support in, so we can resume Virtio enabling work. You can find previous discussions at [2].

Patch series [3] is based on recent "staging" branch (fa6dc0879ffd3dffffaea2837953c7a8761a9ba0 page_alloc: assert IRQs are enabled in heap alloc/free) and tested on Renesas Salvator-X board + H3 ES3.0 SoC (Arm64) with virtio-mmio based virtio-disk backend [4] running in Dom0 (or Driver domain) and unmodified Linux Guest running on existing virtio-blk driver (frontend). No issues were observed. Guest domain 'reboot/destroy' use-cases work properly.

Any feedback/help would be highly appreciated.

[1]
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
[2]
https://lists.xenproject.org/archives/html/xen-devel/2021-01/msg02403.html
https://lists.xenproject.org/archives/html/xen-devel/2021-01/msg02536.html
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/
https://lore.kernel.org/xen-devel/[email protected]/

[3] https://github.com/otyshchenko1/xen/commits/libxl_virtio3
[4] https://github.com/otyshchenko1/virtio-disk/commits/virtio_grant

Julien Grall (1):
libxl: Introduce basic virtio-mmio support on Arm

Oleksandr Tyshchenko (1):
libxl: Add support for Virtio disk configuration

 docs/man/xl-disk-configuration.5.pod.in   |  38 +-
 tools/golang/xenlight/helpers.gen.go      |   8 +
 tools/golang/xenlight/types.gen.go        |  18 +
 tools/include/libxl.h                     |   7 +
 tools/libs/light/libxl_arm.c              | 118 +++-
 tools/libs/light/libxl_device.c           |  62 +-
 tools/libs/light/libxl_disk.c             | 136 ++++-
 tools/libs/light/libxl_internal.h         |   2 +
 tools/libs/light/libxl_types.idl          |  16 +
 tools/libs/light/libxl_types_internal.idl |   1 +
 tools/libs/light/libxl_utils.c            |   2 +
 tools/libs/util/libxlu_disk_l.c           | 959 +++++++++++++++---------------
 tools/libs/util/libxlu_disk_l.h           |   2 +-
 tools/libs/util/libxlu_disk_l.l           |   9 +
 tools/xl/xl_block.c                       |  11 +
 xen/include/public/arch-arm.h             |   7 +
 16 files changed, 914 insertions(+), 482 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v8 1/2 libxl: Add support for Virtio disk configuration

From: Oleksandr Tyshchenko <[email protected]>

This patch adds basic support for configuring and assisting virtio-mmio based virtio-disk backend (emulator) which is intended to run out of Qemu and could be run in any domain.
Although the Virtio block device is quite different from traditional Xen PV block device (vbd) from the toolstack's point of view:
- as the frontend is virtio-blk which is not a Xenbus driver, nothing written to Xenstore are fetched by the frontend currently ("vdev" is not passed to the frontend). But this might need to be revised in future, so frontend data might be written to Xenstore in order to support hotplugging virtio devices or passing the backend domain id on arch where the device-tree is not available.
- the ring-ref/event-channel are not used for the backend<->frontend communication, the proposed IPC for Virtio is IOREQ/DM
it is still a "block device" and ought to be integrated in existing "disk" handling. So, re-use (and adapt) "disk" parsing/configuration logic to deal with Virtio devices as well.

For the immediate purpose and an ability to extend that support for other use-cases in future (Qemu, virtio-pci, etc) perform the following actions:
- Add new disk backend type (LIBXL_DISK_BACKEND_OTHER) and reflect that in the configuration
- Introduce new disk "specification" and "transport" fields to struct libxl_device_disk. Both are written to the Xenstore. The transport field is only used for the specification "virtio" and it assumes only "mmio" value for now.
- Introduce new "specification" option with "xen" communication protocol being default value.
- Add new device kind (LIBXL__DEVICE_KIND_VIRTIO_DISK) as current one (LIBXL__DEVICE_KIND_VBD) doesn't fit into Virtio disk model

An example of domain configuration for Virtio disk:
disk = [ 'phy:/dev/mmcblk0p3, xvda1, backendtype=other, specification=virtio']

Nothing has changed for default Xen disk configuration.

Please note, this patch is not enough for virtio-disk to work on Xen (Arm), as for every Virtio device (including disk) we need to allocate Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree. The subsequent patch will add these missing bits. For the current patch, the default "irq" and "base" are just written to the Xenstore. This is not an ideal splitting, but this way we avoid breaking the bisectability.

---
Changes RFC -> V1:
- no changes

Changes V1 -> V2:
- rebase according to the new location of libxl_virtio_disk.c

Changes V2 -> V3:
- no changes

Changes V3 -> V4:
- rebase according to the new argument for DEFINE_DEVICE_TYPE_STRUCT

Changes V4 -> V5:
- split the changes, change the order of the patches
- update patch description
- don't introduce new "vdisk" configuration option with own parsing logic, re-use Xen PV block "disk" parsing/configuration logic for the virtio-disk
- introduce "virtio" flag and document it's usage
- add LIBXL_HAVE_DEVICE_DISK_VIRTIO
- update libxlu_disk_l.[ch]
- drop num_disks variable/MAX_VIRTIO_DISKS
- drop Wei's T-b

Changes V5 -> V6:
- rebase on current staging
- use "%"PRIu64 instead of %lu for disk->base in device_disk_add()
- update *.gen.go files

Changes V6 -> V7:
- rebase on current staging
- update *.gen.go files and libxlu_disk_l.[ch] files
- update patch description
- rework significantly to support more flexible configuration and have more generic basic implementation for being able to extend that for other use-cases (virtio-pci, qemu, etc).

Changes V7 -> V8:
- update *.gen.go files and libxlu_disk_l.[ch] files
- update patch description and comments in the code
- use "specification" config option instead of "protocol"
- update libxl_types.idl and code according to new fields in libxl_device_disk

---
 docs/man/xl-disk-configuration.5.pod.in   |  38 +-
 tools/golang/xenlight/helpers.gen.go      |   8 +
 tools/golang/xenlight/types.gen.go        |  18 +
 tools/include/libxl.h                     |   7 +
 tools/libs/light/libxl_device.c           |  62 +-
 tools/libs/light/libxl_disk.c             | 136 ++++-
 tools/libs/light/libxl_internal.h         |   2 +
 tools/libs/light/libxl_types.idl          |  16 +
 tools/libs/light/libxl_types_internal.idl |   1 +
 tools/libs/light/libxl_utils.c            |   2 +
 tools/libs/util/libxlu_disk_l.c           | 959 +++++++++++++++---------------
 tools/libs/util/libxlu_disk_l.h           |   2 +-
 tools/libs/util/libxlu_disk_l.l           |   9 +
 tools/xl/xl_block.c                       |  11 +
 14 files changed, 791 insertions(+), 480 deletions(-)
docs/man/xl-disk-configuration.5.pod.in +34 -4
index 71d0e86..487ffef 100644
@@ -232,7 +232,7 @@ Specifies the backend implementation to use
 
 =item Supported values
 
-phy, qdisk
+phy, qdisk, other
 
 =item Mandatory
 
@@ -244,11 +244,13 @@ Automatically determine which backend to use.
 
 =back
 
-This does not affect the guest's view of the device.  It controls
-which software implementation of the Xen backend driver is used.
+It controls which software implementation of the backend driver is used.
+Depending on the "specification" option this may affect the guest's view
+of the device.
 
 Not all backend drivers support all combinations of other options.
-For example, "phy" does not support formats other than "raw".
+For example, "phy" and "other" do not support formats other than "raw" and
+"other" does not support specifications other than "virtio".
 Normally this option should not be specified, in which case libxl will
 automatically determine the most suitable backend.
 
@@ -344,8 +346,36 @@ can be used to disable "hole punching" for file based backends which
 were intentionally created non-sparse to avoid fragmentation of the
 file.
 
+=item B<specification>=I<SPECIFICATION>
+
+=over 4
+
+=item Description
+
+Specifies the communication protocol (specification) to use for the chosen
+"backendtype" option
+
+=item Supported values
+
+xen, virtio
+
+=item Mandatory
+
+No
+
+=item Default value
+
+xen
+
 =back
 
+Besides forcing toolstack to use specific backend implementation, this also
+affects the guest's view of the device. For example, "virtio" requires
+Virtio frontend driver (virtio-blk) to be used. Please note, the virtual
+device (vdev) is not passed to the guest in that case, but it still must be
+specified for the internal purposes.
+
+=back
 
 =head1 COLO Parameters
 
tools/golang/xenlight/helpers.gen.go +8 -0
index b746ff1..00f10b9 100644
@@ -1751,6 +1751,10 @@ x.DirectIoSafe = bool(xc.direct_io_safe)
 if err := x.DiscardEnable.fromC(&xc.discard_enable);err != nil {
 return fmt.Errorf("converting field DiscardEnable: %v", err)
 }
+x.Specification = DiskSpecification(xc.specification)
+x.Transport = DiskTransport(xc.transport)
+x.Irq = uint32(xc.irq)
+x.Base = uint64(xc.base)
 if err := x.ColoEnable.fromC(&xc.colo_enable);err != nil {
 return fmt.Errorf("converting field ColoEnable: %v", err)
 }
@@ -1788,6 +1792,10 @@ xc.direct_io_safe = C.bool(x.DirectIoSafe)
 if err := x.DiscardEnable.toC(&xc.discard_enable); err != nil {
 return fmt.Errorf("converting field DiscardEnable: %v", err)
 }
+xc.specification = C.libxl_disk_specification(x.Specification)
+xc.transport = C.libxl_disk_transport(x.Transport)
+xc.irq = C.uint32_t(x.Irq)
+xc.base = C.uint64_t(x.Base)
 if err := x.ColoEnable.toC(&xc.colo_enable); err != nil {
 return fmt.Errorf("converting field ColoEnable: %v", err)
 }
tools/golang/xenlight/types.gen.go +18 -0
index b1e84d5..cc52936 100644
@@ -99,6 +99,20 @@ DiskBackendUnknown DiskBackend = 0
 DiskBackendPhy DiskBackend = 1
 DiskBackendTap DiskBackend = 2
 DiskBackendQdisk DiskBackend = 3
+DiskBackendOther DiskBackend = 4
+)
+
+type DiskSpecification int
+const(
+DiskSpecificationUnknown DiskSpecification = 0
+DiskSpecificationXen DiskSpecification = 1
+DiskSpecificationVirtio DiskSpecification = 2
+)
+
+type DiskTransport int
+const(
+DiskTransportUnknown DiskTransport = 0
+DiskTransportMmio DiskTransport = 1
 )
 
 type NicType int
@@ -643,6 +657,10 @@ Readwrite int
 IsCdrom int
 DirectIoSafe bool
 DiscardEnable Defbool
+Specification DiskSpecification
+Transport DiskTransport
+Irq uint32
+Base uint64
 ColoEnable Defbool
 ColoRestoreEnable Defbool
 ColoHost string
tools/include/libxl.h +7 -0
index 51a9b6c..cd8067b 100644
@@ -528,6 +528,13 @@
 #define LIBXL_HAVE_MAX_GRANT_VERSION 1
 
 /*
+ * LIBXL_HAVE_DEVICE_DISK_SPECIFICATION indicates that 'specification' and
+ * 'transport' fields (of libxl_disk_specification and libxl_disk_transport
+ * types respectively) are present in libxl_device_disk.
+ */
+#define LIBXL_HAVE_DEVICE_DISK_SPECIFICATION 1
+
+/*
  * libxl ABI compatibility
  *
  * The only guarantee which libxl makes regarding ABI compatibility
tools/libs/light/libxl_device.c +58 -4

111 lines not on this page · raw message

tools/libs/light/libxl_disk.c +129 -7

211 lines not on this page · raw message

tools/libs/light/libxl_internal.h +2 -0
index a26daec..534dc71 100644
@@ -1493,6 +1493,8 @@ _hidden char * libxl__domain_pvcontrol_read(libxl__gc *gc,
 
 /* from xl_device */
 _hidden char *libxl__device_disk_string_of_backend(libxl_disk_backend backend);
+_hidden char *libxl__device_disk_string_of_specification(libxl_disk_specification specification);
+_hidden char *libxl__device_disk_string_of_transport(libxl_disk_transport transport);
 _hidden char *libxl__device_disk_string_of_format(libxl_disk_format format);
 _hidden const char *libxl__qemu_disk_format_string(libxl_disk_format format);
 _hidden int libxl__device_disk_set_backend(libxl__gc*, libxl_device_disk*);
tools/libs/light/libxl_types.idl +16 -0
index 2a42da2..f783cac 100644
@@ -130,6 +130,18 @@ libxl_disk_backend = Enumeration("disk_backend", [
     (1, "PHY"),
     (2, "TAP"),
     (3, "QDISK"),
+    (4, "OTHER"),
+    ])
+
+libxl_disk_specification = Enumeration("disk_specification", [
+    (0, "UNKNOWN"),
+    (1, "XEN"),
+    (2, "VIRTIO"),
+    ])
+
+libxl_disk_transport = Enumeration("disk_transport", [
+    (0, "UNKNOWN"),
+    (1, "MMIO"),
     ])
 
 libxl_nic_type = Enumeration("nic_type", [
@@ -704,6 +716,10 @@ libxl_device_disk = Struct("device_disk", [
     ("is_cdrom", integer),
     ("direct_io_safe", bool),
     ("discard_enable", libxl_defbool),
+    ("specification", libxl_disk_specification),
+    ("transport", libxl_disk_transport),
+    ("irq", uint32),
+    ("base", uint64),
     # Note that the COLO configuration settings should be considered unstable.
     # They may change incompatibly in future versions of Xen.
     ("colo_enable", libxl_defbool),
tools/libs/light/libxl_types_internal.idl +1 -0
index 3593e21..8f71980 100644
@@ -32,6 +32,7 @@ libxl__device_kind = Enumeration("device_kind", [
     (14, "PVCALLS"),
     (15, "VSND"),
     (16, "VINPUT"),
+    (17, "VIRTIO_DISK"),
     ])
 
 libxl__console_backend = Enumeration("console_backend", [
tools/libs/light/libxl_utils.c +2 -0
index b91c2ca..eb816dd 100644
@@ -303,6 +303,8 @@ int libxl_string_to_backend(libxl_ctx *ctx, char *s, libxl_disk_backend *backend
         *backend = LIBXL_DISK_BACKEND_TAP;
     } else if (!strcmp(s, "qdisk")) {
         *backend = LIBXL_DISK_BACKEND_QDISK;
+    } else if (!strcmp(s, "other")) {
+        *backend = LIBXL_DISK_BACKEND_OTHER;
     } else if (!strcmp(s, "tap")) {
         p = strchr(s, ':');
         if (!p) {
tools/libs/util/libxlu_disk_l.c +495 -464

1,244 lines not on this page · raw message

tools/libs/util/libxlu_disk_l.h +1 -1
index 6abeecf..509aad6 100644
@@ -694,7 +694,7 @@ extern int yylex (yyscan_t yyscanner);
 #undef yyTABLES_NAME
 #endif
 
-#line 291 "libxlu_disk_l.l"
+#line 300 "libxlu_disk_l.l"
 
 #line 699 "libxlu_disk_l.h"
 #undef xlu__disk_yyIN_HEADER
tools/libs/util/libxlu_disk_l.l +9 -0
index 3bd639a..47b8ee0 100644
@@ -122,9 +122,17 @@ static void setbackendtype(DiskParseContext *dpc, const char *str) {
     if (     !strcmp(str,"phy"))   DSET(dpc,backend,BACKEND,str,PHY);
     else if (!strcmp(str,"tap"))   DSET(dpc,backend,BACKEND,str,TAP);
     else if (!strcmp(str,"qdisk")) DSET(dpc,backend,BACKEND,str,QDISK);
+    else if (!strcmp(str,"other")) DSET(dpc,backend,BACKEND,str,OTHER);
     else xlu__disk_err(dpc,str,"unknown value for backendtype");
 }
 
+/* Sets ->specification from the string.  IDL should provide something for this. */
+static void setspecification(DiskParseContext *dpc, const char *str) {
+    if      (!strcmp(str,"xen"))    DSET(dpc,specification,SPECIFICATION,str,XEN);
+    else if (!strcmp(str,"virtio")) DSET(dpc,specification,SPECIFICATION,str,VIRTIO);
+    else xlu__disk_err(dpc,str,"unknown value for specification");
+}
+
 /* Sets ->colo-port from the string.  COLO need this. */
 static void setcoloport(DiskParseContext *dpc, const char *str) {
     int port = atoi(str);
@@ -192,6 +200,7 @@ devtype=[^,]*,?	{ xlu__disk_err(DPC,yytext,"unknown value for type"); }
 access=[^,]*,?	{ STRIP(','); setaccess(DPC, FROMEQUALS); }
 backend=[^,]*,? { STRIP(','); SAVESTRING("backend", backend_domname, FROMEQUALS); }
 backendtype=[^,]*,? { STRIP(','); setbackendtype(DPC,FROMEQUALS); }
+specification=[^,]*,? { STRIP(','); setspecification(DPC,FROMEQUALS); }
 
 vdev=[^,]*,?	{ STRIP(','); SAVESTRING("vdev", vdev, FROMEQUALS); }
 script=[^,]*,?	{ STRIP(','); SAVESTRING("script", script, FROMEQUALS); }
tools/xl/xl_block.c +11 -0
index 70eed43..f2b0ff5 100644
@@ -50,6 +50,11 @@ int main_blockattach(int argc, char **argv)
         return 0;
     }
 
+    if (disk.specification != LIBXL_DISK_SPECIFICATION_XEN) {
+        fprintf(stderr, "block-attach is only supported for specification xen\n");
+        return 1;
+    }
+
     if (libxl_device_disk_add(ctx, fe_domid, &disk, 0)) {
         fprintf(stderr, "libxl_device_disk_add failed.\n");
         return 1;
@@ -119,6 +124,12 @@ int main_blockdetach(int argc, char **argv)
         fprintf(stderr, "Error: Device %s not connected.\n", argv[optind+1]);
         return 1;
     }
+
+    if (disk.specification != LIBXL_DISK_SPECIFICATION_XEN) {
+        fprintf(stderr, "block-detach is only supported for specification xen\n");
+        return 1;
+    }
+
     rc = !force ? libxl_device_disk_safe_remove(ctx, domid, &disk, 0) :
         libxl_device_disk_destroy(ctx, domid, &disk, 0);
     if (rc) {
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH V8 1/2] libxl: Add support for Virtio disk configuration

Quoted 19 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch adds basic support for configuring and assisting virtio-mmio based virtio-disk backend (emulator) which is intended to run out of Qemu and could be run in any domain.
Although the Virtio block device is quite different from traditional Xen PV block device (vbd) from the toolstack's point of view:
- as the frontend is virtio-blk which is not a Xenbus driver, nothing written to Xenstore are fetched by the frontend currently ("vdev" is not passed to the frontend). But this might need to be revised in future, so frontend data might be written to Xenstore in order to support hotplugging virtio devices or passing the backend domain id on arch where the device-tree is not available.
- the ring-ref/event-channel are not used for the backend<->frontend communication, the proposed IPC for Virtio is IOREQ/DM
it is still a "block device" and ought to be integrated in existing "disk" handling. So, re-use (and adapt) "disk" parsing/configuration logic to deal with Virtio devices as well.

Quoted 13 lines

For the immediate purpose and an ability to extend that support for other use-cases in future (Qemu, virtio-pci, etc) perform the following actions:
- Add new disk backend type (LIBXL_DISK_BACKEND_OTHER) and reflect that in the configuration
- Introduce new disk "specification" and "transport" fields to struct libxl_device_disk. Both are written to the Xenstore. The transport field is only used for the specification "virtio" and it assumes only "mmio" value for now.
- Introduce new "specification" option with "xen" communication protocol being default value.
- Add new device kind (LIBXL__DEVICE_KIND_VIRTIO_DISK) as current one (LIBXL__DEVICE_KIND_VBD) doesn't fit into Virtio disk model

Is this still an issue? Since v5, the "disk/vbd" kind is used. Also see my comment about libxl_device_disk_get_path() regarding this.

Quoted 26 lines

An example of domain configuration for Virtio disk:
disk = [ 'phy:/dev/mmcblk0p3, xvda1, backendtype=other, specification=virtio']

Nothing has changed for default Xen disk configuration.

Please note, this patch is not enough for virtio-disk to work on Xen (Arm), as for every Virtio device (including disk) we need to allocate Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree. The subsequent patch will add these missing bits. For the current patch, the default "irq" and "base" are just written to the Xenstore. This is not an ideal splitting, but this way we avoid breaking the bisectability.

---

tools/libs/light/libxl_disk.c +2 -0
index a5ca778..7fd98ce 100644
@@ -163,6 +163,19 @@ static int libxl__device_disk_setdefault(libxl__gc *gc, uint32_t domid,
     rc = libxl__resolve_domid(gc, disk->backend_domname, &disk->backend_domid);
     if (rc < 0) return rc;
 
+    if (disk->specification == LIBXL_DISK_SPECIFICATION_UNKNOWN)
+        disk->specification = LIBXL_DISK_SPECIFICATION_XEN;

tools/libs/light/libxl_disk.c static int libxl__device_disk_setdefault(libxl__gc *gc, uint32_t domid, · line 177

Quoted 10 lines
tools/libs/light/libxl_disk.c +10 -0
+
+    /*
+     * The transport field is only used for the specification "virtio" and
+     * it assumes only "mmio" value for now. When there will be a need to add
+     * "pci" support, we will need to remove the enforcement here and
+     * respective assert(s) down the code and let the toolstack to decide
+     * the transport to use.
+     */
+    if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO)
+        disk->transport = LIBXL_DISK_TRANSPORT_MMIO;

Could you check that disk->transport is unset when specification==xen ? And probably return ERROR_INVAL in this case.

Also, I don't think you should overwrite the value set by an application in _setdefault(). If specification==virtio, check first that transport as a supported value (unknown or mmio) then you can then you can set the transport value expected by virtio if it wasn't set by the application. ( An example of this is done the function already when enforcing qdisk for cdroms. )

tools/libs/light/libxl_disk.c static void device_disk_add(libxl__egc *egc, uint32_t domid, · line 340

Quoted 12 lines
tools/libs/light/libxl_disk.c +5 -0
+
     /* Force Qdisk backend for CDROM devices of guests with a device model. */
     if (disk->is_cdrom != 0 &&
         libxl__domain_type(gc, domid) == LIBXL_DOMAIN_TYPE_HVM) {
@@ -317,6 +334,11 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
             goto out;
         }
 
+        assert((disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO &&
+                disk->backend == LIBXL_DISK_BACKEND_OTHER) ||
+               (disk->specification != LIBXL_DISK_SPECIFICATION_VIRTIO &&
+                disk->backend != LIBXL_DISK_BACKEND_OTHER));

I'm not sure whether this assert() is useful. The value should already be correct as we call _setdefault(). It seems like an unnecessary potential crash at this point.

Quoted 19 lines
tools/libs/light/libxl_disk.c +7 -0
         switch (disk->backend) {
             case LIBXL_DISK_BACKEND_PHY:
                 dev = disk->pdev_path;
@@ -330,7 +352,14 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
 
                 assert(device->backend_kind == LIBXL__DEVICE_KIND_VBD);
                 break;
+            case LIBXL_DISK_BACKEND_OTHER:
+                dev = disk->pdev_path;
+
+                flexarray_append(back, "params");
+                flexarray_append(back, dev);
 
+                assert(device->backend_kind == LIBXL__DEVICE_KIND_VIRTIO_DISK);
+                break;
             case LIBXL_DISK_BACKEND_TAP:
                 LOG(ERROR, "blktap is not supported");
                 rc = ERROR_FAIL;

tools/libs/light/libxl_disk.c static void device_disk_add(libxl__egc *egc, uint32_t domid, · line 421

Quoted 7 lines
tools/libs/light/libxl_disk.c +4 -0
@@ -386,6 +415,15 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
         flexarray_append_pair(back, "discard-enable",
                               libxl_defbool_val(disk->discard_enable) ?
                               "1" : "0");
+        flexarray_append(back, "specification");
+        flexarray_append(back, libxl__device_disk_string_of_specification(disk->specification));
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            assert(disk->transport == LIBXL_DISK_TRANSPORT_MMIO);

I don't think this is a good assert(). If an application sets the wrong "transport" for virtio, it should get an error, not crash. But I believe in this case that _setdefault() could already check that "transport" is correct, so there's probably no need to check the transport value here.

Quoted 9 lines
tools/libs/light/libxl_disk.c +5 -0
+            flexarray_append(back, "transport");
+            flexarray_append(back, libxl__device_disk_string_of_transport(disk->transport));
+            flexarray_append_pair(back, "base", GCSPRINTF("%"PRIu64, disk->base));
+            flexarray_append_pair(back, "irq", GCSPRINTF("%u", disk->irq));
+        }
 
         flexarray_append(front, "backend-id");
         flexarray_append(front, GCSPRINTF("%d", disk->backend_domid));

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 576

Quoted 7 lines
tools/libs/light/libxl_disk.c +4 -0
@@ -532,6 +570,49 @@ static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path,
     }
     libxl_string_to_backend(ctx, tmp, &(disk->backend));
 
+    tmp = libxl__xs_read(gc, XBT_NULL,
+                         GCSPRINTF("%s/specification", libxl_path));
+    if (!tmp) {
+        LOG(ERROR, "Missing xenstore node %s/specification", libxl_path);

This mean that we introduce an hard error if we deal with a previous version of libxl that didn't write this path. But we know that it meant specification=xen, so could we default to "xen" when the node is missing, rather than return an error? (It's mostly useful for developper at this point as creating a vm with one version of libxl and keep managing it with a newer version isn't really possible for now.)

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 584

Quoted 8 lines
tools/libs/light/libxl_disk.c +8 -0
+        goto cleanup;
+    }
+    if (!strcmp(tmp, "xen"))
+        disk->specification = LIBXL_DISK_SPECIFICATION_XEN;
+    else if (!strcmp(tmp, "virtio"))
+        disk->specification = LIBXL_DISK_SPECIFICATION_VIRTIO;
+    else
+        disk->specification = LIBXL_DISK_SPECIFICATION_UNKNOWN;

That's a reimplementation of a generated function, libxl_disk_specification_from_string() I believe ;-)

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 596

Quoted 12 lines
tools/libs/light/libxl_disk.c +12 -0
+
+    if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+        tmp = libxl__xs_read(gc, XBT_NULL,
+                             GCSPRINTF("%s/transport", libxl_path));
+        if (!tmp) {
+            LOG(ERROR, "Missing xenstore node %s/transport", libxl_path);
+            goto cleanup;
+        }
+        if (!strcmp(tmp, "mmio"))
+            disk->transport = LIBXL_DISK_TRANSPORT_MMIO;
+        else
+            disk->transport = LIBXL_DISK_TRANSPORT_UNKNOWN;

That's libxl_disk_transport_from_string() I think.

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 597

Quoted 1 line
tools/libs/light/libxl_disk.c +1 -0
+        assert(disk->transport == LIBXL_DISK_TRANSPORT_MMIO);

Could you return an error instead of assert() here?

Quoted 22 lines
tools/libs/light/libxl_disk.c +18 -0
+
+        tmp = libxl__xs_read(gc, XBT_NULL,
+                             GCSPRINTF("%s/base", libxl_path));
+        if (!tmp) {
+            LOG(ERROR, "Missing xenstore node %s/base", libxl_path);
+            goto cleanup;
+        }
+        disk->base = strtoul(tmp, NULL, 10);
+
+        tmp = libxl__xs_read(gc, XBT_NULL,
+                             GCSPRINTF("%s/irq", libxl_path));
+        if (!tmp) {
+            LOG(ERROR, "Missing xenstore node %s/irq", libxl_path);
+            goto cleanup;
+        }
+        disk->irq = strtoul(tmp, NULL, 10);
+    }
+
     disk->vdev = xs_read(ctx->xsh, XBT_NULL,
                          GCSPRINTF("%s/dev", libxl_path), &len);
     if (!disk->vdev) {

tools/libs/light/libxl_disk.c cleanup: · line 659

Quoted 4 lines
tools/libs/light/libxl_disk.c +1 -0
@@ -575,6 +656,41 @@ cleanup:
     return rc;
 }
 
+static int libxl_device_disk_get_path(libxl__gc *gc, uint32_t domid,

FYI, "libxl_" prefix is for functions exported by the library. You could use "libxl__" for functions used within the library or no prefix in this case as it is static.

tools/libs/light/libxl_disk.c cleanup: · line 671

Quoted 12 lines
tools/libs/light/libxl_disk.c +12 -0
+                                      char **path)
+{
+    const char *dir;
+    int rc;
+
+    /*
+     * As we don't know exactly what device kind to be used here, guess it
+     * by checking the presence of the corresponding path in Xenstore.
+     * First, try to read path for vbd device (default) and if not exists
+     * read path for virtio_disk device. This will work as long as both Xen PV
+     * and Virtio disk devices are not assigned to the same guest.
+     */

That mean, we can't have both virtio-disk and pv-disk, and there's going to be weird error with device disappearing if one try to add a pv-disk after adding a virtio-disk.

Also, I don't know whether the function is called before a first device is added, but it could be a potential issue as it would return an error in that case.

The path generated here are path that are only used by libxl, so it probably doesn't matter which path is used, as long as there's a unique path for a device implementation. (The different path might have been useful in v4 of the series when there was a libxl-virtio-disk implementation.)

So, is there a reason to have different path? Can we simply get rid of this function?

Maybe this is related to the path that a frontend would see, and we probably don't want to have a pv-disk front-end trying to connect to a virtio backend as it doesn't going to work. I wonder if the "/libxl/*/device/" needs to use the same "device kind" name as the ones seen by a guest. I didn't investigated that.

Quoted 30 lines
tools/libs/light/libxl_disk.c +22 -0
+    *path = GCSPRINTF("%s/device/%s",
+                      libxl__xs_libxl_path(gc, domid),
+                      libxl__device_kind_to_string(LIBXL__DEVICE_KIND_VBD));
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL, *path, &dir);
+    if (rc)
+        return rc;
+
+    if (dir)
+        return 0;
+
+    *path = GCSPRINTF("%s/device/%s",
+                      libxl__xs_libxl_path(gc, domid),
+                      libxl__device_kind_to_string(LIBXL__DEVICE_KIND_VIRTIO_DISK));
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL, *path, &dir);
+    if (rc)
+        return rc;
+
+    return 0;
+}
+
 int libxl_vdev_to_device_disk(libxl_ctx *ctx, uint32_t domid,
                               const char *vdev, libxl_device_disk *disk)
 {
tools/libs/light/libxl_types.idl +0 -0
index 2a42da2..f783cac 100644

tools/libs/light/libxl_types.idl libxl_device_disk = Struct("device_disk", [ · line 720

Quoted 5 lines
tools/libs/light/libxl_types.idl +2 -0
@@ -704,6 +716,10 @@ libxl_device_disk = Struct("device_disk", [
     ("is_cdrom", integer),
     ("direct_io_safe", bool),
     ("discard_enable", libxl_defbool),
+    ("specification", libxl_disk_specification),
+    ("transport", libxl_disk_transport),

Could you add a comment here about "irq" and "base", that say that they are for internal use by libxl and can't be modified? Is it possible that in the future, an application like libvirt could potentially change those two values and have libxl use them?

tools/libs/light/libxl_types.idl libxl_device_disk = Struct("device_disk", [ · line 725

Quoted 5 lines
tools/libs/light/libxl_types.idl +2 -0
+    ("irq", uint32),
+    ("base", uint64),
     # Note that the COLO configuration settings should be considered unstable.
     # They may change incompatibly in future versions of Xen.
     ("colo_enable", libxl_defbool),

Thanks,

Signature · 1 line
Anthony PERARD
author
#3 lore raw

Re: [PATCH V8 1/2] libxl: Add support for Virtio disk configuration

On 18.05.22 13:52, Anthony PERARD wrote:

Hello Anthony

Quoted 33 lines · Oleksandr Tyshchenko
Quoted 19 lines

From: Oleksandr Tyshchenko <[email protected]>

This patch adds basic support for configuring and assisting virtio-mmio based virtio-disk backend (emulator) which is intended to run out of Qemu and could be run in any domain.
Although the Virtio block device is quite different from traditional Xen PV block device (vbd) from the toolstack's point of view:
- as the frontend is virtio-blk which is not a Xenbus driver, nothing written to Xenstore are fetched by the frontend currently ("vdev" is not passed to the frontend). But this might need to be revised in future, so frontend data might be written to Xenstore in order to support hotplugging virtio devices or passing the backend domain id on arch where the device-tree is not available.
- the ring-ref/event-channel are not used for the backend<->frontend communication, the proposed IPC for Virtio is IOREQ/DM
it is still a "block device" and ought to be integrated in existing "disk" handling. So, re-use (and adapt) "disk" parsing/configuration logic to deal with Virtio devices as well.

Quoted 13 lines

For the immediate purpose and an ability to extend that support for other use-cases in future (Qemu, virtio-pci, etc) perform the following actions:
- Add new disk backend type (LIBXL_DISK_BACKEND_OTHER) and reflect that in the configuration
- Introduce new disk "specification" and "transport" fields to struct libxl_device_disk. Both are written to the Xenstore. The transport field is only used for the specification "virtio" and it assumes only "mmio" value for now.
- Introduce new "specification" option with "xen" communication protocol being default value.
- Add new device kind (LIBXL__DEVICE_KIND_VIRTIO_DISK) as current one (LIBXL__DEVICE_KIND_VBD) doesn't fit into Virtio disk model

Quoted 1 line

Is this still an issue?

Yes, it is.

Both LIBXL__DEVICE_KIND_VBD and LIBXL__DEVICE_KIND_VIRTIO_DISK are for disk devices, but they are quite different from toolstack PoV.

They use different specifications/transports. The latter is for virtio-blk driver which is not xenbus driver. The way how both Virtio backend and frontend get configuration and communicate with each

are completely different from Xen PV drivers.

Quoted 2 lines

Since v5, the "disk/vbd" kind is used. Also see
my comment about libxl_device_disk_get_path() regarding this.

ok

Quoted 26 lines
Quoted 26 lines

An example of domain configuration for Virtio disk:
disk = [ 'phy:/dev/mmcblk0p3, xvda1, backendtype=other, specification=virtio']

Nothing has changed for default Xen disk configuration.

Please note, this patch is not enough for virtio-disk to work on Xen (Arm), as for every Virtio device (including disk) we need to allocate Virtio MMIO params (IRQ and memory region) and pass them to the backend, also update Guest device-tree. The subsequent patch will add these missing bits. For the current patch, the default "irq" and "base" are just written to the Xenstore. This is not an ideal splitting, but this way we avoid breaking the bisectability.

---

tools/libs/light/libxl_disk.c +0 -0
index a5ca778..7fd98ce 100644
@@ -163,6 +163,19 @@ static int libxl__device_disk_setdefault(libxl__gc *gc, uint32_t domid,
      rc = libxl__resolve_domid(gc, disk->backend_domname, &disk->backend_domid);
      if (rc < 0) return rc;
 
Quoted 14 lines

tools/libs/light/libxl_disk.c static int libxl__device_disk_setdefault(libxl__gc *gc, uint32_t domid, · line 177

Quoted 12 lines
tools/libs/light/libxl_disk.c +12 -0
+    if (disk->specification == LIBXL_DISK_SPECIFICATION_UNKNOWN)
+        disk->specification = LIBXL_DISK_SPECIFICATION_XEN;
+
+    /*
+     * The transport field is only used for the specification "virtio" and
+     * it assumes only "mmio" value for now. When there will be a need to add
+     * "pci" support, we will need to remove the enforcement here and
+     * respective assert(s) down the code and let the toolstack to decide
+     * the transport to use.
+     */
+    if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO)
+        disk->transport = LIBXL_DISK_TRANSPORT_MMIO;

Could you check that disk->transport is unset when specification==xen ? And probably return ERROR_INVAL in this case.

yes, will do

Quoted 7 lines

Also, I don't think you should overwrite the value set by an application in _setdefault(). If specification==virtio, check first that transport as a supported value (unknown or mmio) then you can then you can set the transport value expected by virtio if it wasn't set by the application. ( An example of this is done the function already when enforcing qdisk for cdroms. )

I got it, will do

I transform to something like that:

    if (disk->specification == LIBXL_DISK_SPECIFICATION_XEN && disk->transport != LIBXL_DISK_TRANSPORT_UNKNOWN) {
        LOGD(ERROR, domid, "Transport is only supported for specification virtio"); return ERROR_FAIL;
    }

    /* Force transport mmio for specification virtio for now */
    if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
        if (!(disk->transport == LIBXL_DISK_TRANSPORT_UNKNOWN || disk->transport == LIBXL_DISK_TRANSPORT_MMIO)) { LOGD(ERROR, domid, "Unsupported transport for specification virtio"); return ERROR_FAIL;
        }
        disk->transport = LIBXL_DISK_TRANSPORT_MMIO;
    }

Quoted 9 lines
Quoted 9 lines
tools/libs/light/libxl_disk.c +1 -0
+
      /* Force Qdisk backend for CDROM devices of guests with a device model. */
      if (disk->is_cdrom != 0 &&
          libxl__domain_type(gc, domid) == LIBXL_DOMAIN_TYPE_HVM) {
@@ -317,6 +334,11 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
              goto out;
          }
 
Quoted 7 lines

tools/libs/light/libxl_disk.c static void device_disk_add(libxl__egc *egc, uint32_t domid, · line 340

Quoted 4 lines
tools/libs/light/libxl_disk.c +4 -0
+        assert((disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO &&
+                disk->backend == LIBXL_DISK_BACKEND_OTHER) ||
+               (disk->specification != LIBXL_DISK_SPECIFICATION_VIRTIO &&
+                disk->backend != LIBXL_DISK_BACKEND_OTHER));

I'm not sure whether this assert() is useful. The value should already be correct as we call _setdefault(). It seems like an unnecessary potential crash at this point.

agree, will drop

Quoted 23 lines
Quoted 23 lines
tools/libs/light/libxl_disk.c +7 -0
          switch (disk->backend) {
              case LIBXL_DISK_BACKEND_PHY:
                  dev = disk->pdev_path;
@@ -330,7 +352,14 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
 
                  assert(device->backend_kind == LIBXL__DEVICE_KIND_VBD);
                  break;
+            case LIBXL_DISK_BACKEND_OTHER:
+                dev = disk->pdev_path;
+
+                flexarray_append(back, "params");
+                flexarray_append(back, dev);
 
+                assert(device->backend_kind == LIBXL__DEVICE_KIND_VIRTIO_DISK);
+                break;
              case LIBXL_DISK_BACKEND_TAP:
                  LOG(ERROR, "blktap is not supported");
                  rc = ERROR_FAIL;
@@ -386,6 +415,15 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
          flexarray_append_pair(back, "discard-enable",
                                libxl_defbool_val(disk->discard_enable) ?
                                "1" : "0");
Quoted 8 lines

tools/libs/light/libxl_disk.c static void device_disk_add(libxl__egc *egc, uint32_t domid, · line 421

Quoted 4 lines
tools/libs/light/libxl_disk.c +4 -0
+        flexarray_append(back, "specification");
+        flexarray_append(back, libxl__device_disk_string_of_specification(disk->specification));
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            assert(disk->transport == LIBXL_DISK_TRANSPORT_MMIO);

I don't think this is a good assert(). If an application sets the wrong "transport" for virtio, it should get an error, not crash. But I believe in this case that _setdefault() could already check that "transport" is correct, so there's probably no need to check the transport value here.

ok, I can drop this. The reason why I put this assert is to draw developer's attention (who will implement pci transport in future) to the need

to update code below.

Quoted 13 lines
Quoted 13 lines
tools/libs/light/libxl_disk.c +5 -0
+            flexarray_append(back, "transport");
+            flexarray_append(back, libxl__device_disk_string_of_transport(disk->transport));
+            flexarray_append_pair(back, "base", GCSPRINTF("%"PRIu64, disk->base));
+            flexarray_append_pair(back, "irq", GCSPRINTF("%u", disk->irq));
+        }
 
          flexarray_append(front, "backend-id");
          flexarray_append(front, GCSPRINTF("%d", disk->backend_domid));
@@ -532,6 +570,49 @@ static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path,
      }
      libxl_string_to_backend(ctx, tmp, &(disk->backend));
 
Quoted 10 lines

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 576

Quoted 4 lines
tools/libs/light/libxl_disk.c +4 -0
+    tmp = libxl__xs_read(gc, XBT_NULL,
+                         GCSPRINTF("%s/specification", libxl_path));
+    if (!tmp) {
+        LOG(ERROR, "Missing xenstore node %s/specification", libxl_path);

This mean that we introduce an hard error if we deal with a previous version of libxl that didn't write this path. But we know that it meant specification=xen, so could we default to "xen" when the node is missing, rather than return an error? (It's mostly useful for developper at this point as creating a vm with one version of libxl and keep managing it with a newer version isn't really possible for now.)

agree, will do. But maybe it is worth leaving debug print here?

LOG(DEBUG, "Missing xenstore node %s/specification, assuming specification xen", libxl_path);

Quoted 11 lines

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 584

Quoted 9 lines
tools/libs/light/libxl_disk.c +8 -0
+        goto cleanup;
+    }
+    if (!strcmp(tmp, "xen"))
+        disk->specification = LIBXL_DISK_SPECIFICATION_XEN;
+    else if (!strcmp(tmp, "virtio"))
+        disk->specification = LIBXL_DISK_SPECIFICATION_VIRTIO;
+    else
+        disk->specification = LIBXL_DISK_SPECIFICATION_UNKNOWN;

That's a reimplementation of a generated function, libxl_disk_specification_from_string() I believe ;-)

indeed, will reuse

Taking into the account both comments, the resulting code can look something like that:

    tmp = libxl__xs_read(gc, XBT_NULL,
                         GCSPRINTF("%s/specification", libxl_path));
    if (!tmp) {
        LOG(DEBUG, "Missing xenstore node %s/specification, assuming specification xen", libxl_path); disk->specification = LIBXL_DISK_SPECIFICATION_XEN;
    } else {
        rc = libxl_disk_specification_from_string(tmp, &disk->specification);
        if (rc) {
            LOG(ERROR, "Unable to parse xenstore node %s/specification", libxl_path); goto cleanup;
        }
    }

Quoted 13 lines

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 596

Quoted 13 lines
tools/libs/light/libxl_disk.c +12 -0
+
+    if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+        tmp = libxl__xs_read(gc, XBT_NULL,
+                             GCSPRINTF("%s/transport", libxl_path));
+        if (!tmp) {
+            LOG(ERROR, "Missing xenstore node %s/transport", libxl_path);
+            goto cleanup;
+        }
+        if (!strcmp(tmp, "mmio"))
+            disk->transport = LIBXL_DISK_TRANSPORT_MMIO;
+        else
+            disk->transport = LIBXL_DISK_TRANSPORT_UNKNOWN;
Quoted 1 line

That's libxl_disk_transport_from_string() I think.

indeed, will reuse

Quoted 3 lines

tools/libs/light/libxl_disk.c static int libxl__disk_from_xenstore(libxl__gc *gc, const char *libxl_path, · line 597

Quoted 2 lines
tools/libs/light/libxl_disk.c +1 -0
+        assert(disk->transport == LIBXL_DISK_TRANSPORT_MMIO);

Could you return an error instead of assert() here?

yes, will do

Quoted 26 lines
Quoted 26 lines
tools/libs/light/libxl_disk.c +18 -0
+
+        tmp = libxl__xs_read(gc, XBT_NULL,
+                             GCSPRINTF("%s/base", libxl_path));
+        if (!tmp) {
+            LOG(ERROR, "Missing xenstore node %s/base", libxl_path);
+            goto cleanup;
+        }
+        disk->base = strtoul(tmp, NULL, 10);
+
+        tmp = libxl__xs_read(gc, XBT_NULL,
+                             GCSPRINTF("%s/irq", libxl_path));
+        if (!tmp) {
+            LOG(ERROR, "Missing xenstore node %s/irq", libxl_path);
+            goto cleanup;
+        }
+        disk->irq = strtoul(tmp, NULL, 10);
+    }
+
      disk->vdev = xs_read(ctx->xsh, XBT_NULL,
                           GCSPRINTF("%s/dev", libxl_path), &len);
      if (!disk->vdev) {
@@ -575,6 +656,41 @@ cleanup:
      return rc;
  }
 
Quoted 4 lines

tools/libs/light/libxl_disk.c cleanup: · line 659

Quoted 1 line
tools/libs/light/libxl_disk.c +1 -0
+static int libxl_device_disk_get_path(libxl__gc *gc, uint32_t domid,

FYI, "libxl_" prefix is for functions exported by the library. You could use "libxl__" for functions used within the library or no prefix in this case as it is static.

I got it, will do

Quoted 16 lines

tools/libs/light/libxl_disk.c cleanup: · line 671

Quoted 13 lines
tools/libs/light/libxl_disk.c +12 -0
+                                      char **path)
+{
+    const char *dir;
+    int rc;
+
+    /*
+     * As we don't know exactly what device kind to be used here, guess it
+     * by checking the presence of the corresponding path in Xenstore.
+     * First, try to read path for vbd device (default) and if not exists
+     * read path for virtio_disk device. This will work as long as both Xen PV
+     * and Virtio disk devices are not assigned to the same guest.
+     */

That mean, we can't have both virtio-disk and pv-disk, and there's going to be weird error with device disappearing if one try to add a pv-disk after adding a virtio-disk.

I was thinking this wouldn't be a big issue (at least on production). I might be wrong, but what is a reason to assign to the guest the virtual devices of the *same* type (block device) using different virtualization techniques (Xen PV and Virtio)?

Quoted 5 lines

Also, I don't know whether the function is called before a first device is added, but it could be a potential issue as it would return an error in that case.

Quoted 7 lines

The path generated here are path that are only used by libxl, so it probably doesn't matter which path is used, as long as there's a unique path for a device implementation. (The different path might have been useful in v4 of the series when there was a libxl-virtio-disk implementation.)

So, is there a reason to have different path?

I think, the completely different guest's view of the virtualized device is a reason. If virtio-disk and pv-disk was only different in software implementation of the backend, I would reuse LIBXL__DEVICE_KIND_VBD for virtio-disk. But their frontends are completely different. Or I really misunderstood the idea of device_kind?

Quoted 2 lines

Can we simply get rid of
this function?

Without it, libxl_vdev_to_device_disk() won't work for virtio-disk.

Quoted 4 lines

Maybe this is related to the path that a frontend would see, and we probably don't want to have a pv-disk front-end trying to connect to a virtio backend as it doesn't going to work.

Yes, exactly

Quoted 33 lines

I wonder if the
"/libxl/*/device/" needs to use the same "device kind" name as the ones seen by a guest. I didn't investigated that.

Quoted 29 lines
tools/libs/light/libxl_disk.c +22 -0
+    *path = GCSPRINTF("%s/device/%s",
+                      libxl__xs_libxl_path(gc, domid),
+                      libxl__device_kind_to_string(LIBXL__DEVICE_KIND_VBD));
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL, *path, &dir);
+    if (rc)
+        return rc;
+
+    if (dir)
+        return 0;
+
+    *path = GCSPRINTF("%s/device/%s",
+                      libxl__xs_libxl_path(gc, domid),
+                      libxl__device_kind_to_string(LIBXL__DEVICE_KIND_VIRTIO_DISK));
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL, *path, &dir);
+    if (rc)
+        return rc;
+
+    return 0;
+}
+
  int libxl_vdev_to_device_disk(libxl_ctx *ctx, uint32_t domid,
                                const char *vdev, libxl_device_disk *disk)
  {
tools/libs/light/libxl_types.idl +0 -0
index 2a42da2..f783cac 100644
Quoted 8 lines

tools/libs/light/libxl_types.idl libxl_device_disk = Struct("device_disk", [ · line 720

Quoted 6 lines
tools/libs/light/libxl_types.idl +2 -0
@@ -704,6 +716,10 @@ libxl_device_disk = Struct("device_disk", [
      ("is_cdrom", integer),
      ("direct_io_safe", bool),
      ("discard_enable", libxl_defbool),
+    ("specification", libxl_disk_specification),
+    ("transport", libxl_disk_transport),

Could you add a comment here about "irq" and "base", that say that they are for internal use by libxl and can't be modified?

yes, will do

Quoted 3 lines

Is it possible
that in the future, an application like libvirt could potentially change those two values and have libxl use them?

I am not sure I understand that the libvirt is for, but I don't see a need for any external application to touch these fields. These are allocated internally in libxl_arm.c.

Quoted 8 lines

tools/libs/light/libxl_types.idl libxl_device_disk = Struct("device_disk", [ · line 725

Quoted 6 lines
tools/libs/light/libxl_types.idl +2 -0
+    ("irq", uint32),
+    ("base", uint64),
      # Note that the COLO configuration settings should be considered unstable.
      # They may change incompatibly in future versions of Xen.
      ("colo_enable", libxl_defbool),

Thanks,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#4 lore raw

PATCH v8 2/2 libxl: Introduce basic virtio-mmio support on Arm

In reply to Oleksandr Tyshchenko

From: Julien Grall <[email protected]>

This patch introduces helpers to allocate Virtio MMIO params (IRQ and memory region) and create specific device node in the Guest device-tree with allocated params. In order to deal with multiple Virtio devices, reserve corresponding ranges. For now, we reserve 1MB for memory regions and 10 SPIs.

As these helpers should be used for every Virtio device attached to the Guest, call them for Virtio disk(s).

Please note, with statically allocated Virtio IRQs there is a risk of a clash with a physical IRQs of passthrough devices. For the first version, it's fine, but we should consider allocating the Virtio IRQs automatically. Thankfully, we know in advance which IRQs will be used for passthrough to be able to choose non-clashed ones.

---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"

Changes RFC -> V1:
- was squashed with:
"[RFC PATCH V1 09/12] libxl: Handle virtio-mmio irq in more correct way" "[RFC PATCH V1 11/12] libxl: Insert "dma-coherent" property into virtio-mmio device node" "[RFC PATCH V1 12/12] libxl: Fix duplicate memory node in DT"
- move VirtIO MMIO #define-s to xen/include/public/arch-arm.h

Changes V1 -> V2:
- update the author of a patch

Changes V2 -> V3:
- no changes

Changes V3 -> V4:
- no changes

Changes V4 -> V5:
- split the changes, change the order of the patches
- drop an extra "virtio" configuration option
- update patch description
- use CONTAINER_OF instead of own implementation
- reserve ranges for Virtio MMIO params and put them in correct location
- create helpers to allocate Virtio MMIO params, add corresponding sanity-сhecks
- add comment why MMIO size 0x200 is chosen
- update debug print
- drop Wei's T-b

Changes V5 -> V6:
- rebase on current staging

Changes V6 -> V7:
- rebase on current staging
- add T-b and R-b tags
- update according to the recent changes to "libxl: Add support for Virtio disk configuration"

Changes V7 -> V8:
- drop T-b and R-b tags
- make virtio_mmio_base/irq global variables to be local in libxl__arch_domain_prepare_config() and initialize them at the beginning of the function, then rework alloc_virtio_mmio_base/irq() to take a pointer to virtio_mmio_base/irq variables as an argument
- update according to the recent changes to
"libxl: Add support for Virtio disk configuration"

---
 tools/libs/light/libxl_arm.c  | 118 +++++++++++++++++++++++++++++++++++++++++-
 xen/include/public/arch-arm.h |   7 +++
 2 files changed, 123 insertions(+), 2 deletions(-)
tools/libs/light/libxl_arm.c +116 -2
index eef1de0..37403a2 100644
@@ -8,6 +8,46 @@
 #include <assert.h>
 #include <xen/device_tree_defs.h>
 
+/*
+ * There is no clear requirements for the total size of Virtio MMIO region.
+ * The size of control registers is 0x100 and device-specific configuration
+ * registers starts at the offset 0x100, however it's size depends on the device
+ * and the driver. Pick the biggest known size at the moment to cover most
+ * of the devices (also consider allowing the user to configure the size via
+ * config file for the one not conforming with the proposed value).
+ */
+#define VIRTIO_MMIO_DEV_SIZE   xen_mk_ullong(0x200)
+
+static uint64_t alloc_virtio_mmio_base(libxl__gc *gc, uint64_t *virtio_mmio_base)
+{
+    uint64_t base = *virtio_mmio_base;
+
+    /* Make sure we have enough reserved resources */
+    if ((base + VIRTIO_MMIO_DEV_SIZE >
+        GUEST_VIRTIO_MMIO_BASE + GUEST_VIRTIO_MMIO_SIZE)) {
+        LOG(ERROR, "Ran out of reserved range for Virtio MMIO BASE 0x%"PRIx64"\n",
+            base);
+        return 0;
+    }
+    *virtio_mmio_base += VIRTIO_MMIO_DEV_SIZE;
+
+    return base;
+}
+
+static uint32_t alloc_virtio_mmio_irq(libxl__gc *gc, uint32_t *virtio_mmio_irq)
+{
+    uint32_t irq = *virtio_mmio_irq;
+
+    /* Make sure we have enough reserved resources */
+    if (irq > GUEST_VIRTIO_MMIO_SPI_LAST) {
+        LOG(ERROR, "Ran out of reserved range for Virtio MMIO IRQ %u\n", irq);
+        return 0;
+    }
+    (*virtio_mmio_irq)++;
+
+    return irq;
+}
+
 static const char *gicv_to_string(libxl_gic_version gic_version)
 {
     switch (gic_version) {
@@ -26,8 +66,10 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
     uint32_t nr_spis = 0;
     unsigned int i;
-    uint32_t vuart_irq;
-    bool vuart_enabled = false;
+    uint32_t vuart_irq, virtio_irq = 0;
+    bool vuart_enabled = false, virtio_enabled = false;
+    uint64_t virtio_mmio_base = GUEST_VIRTIO_MMIO_BASE;
+    uint32_t virtio_mmio_irq = GUEST_VIRTIO_MMIO_SPI_FIRST;
 
     /*
      * If pl011 vuart is enabled then increment the nr_spis to allow allocation
@@ -39,6 +81,30 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         vuart_enabled = true;
     }
 
+    for (i = 0; i < d_config->num_disks; i++) {
+        libxl_device_disk *disk = &d_config->disks[i];
+
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
+            if (!disk->base)
+                return ERROR_FAIL;
+
+            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
+            if (!disk->irq)
+                return ERROR_FAIL;
+
+            if (virtio_irq < disk->irq)
+                virtio_irq = disk->irq;
+            virtio_enabled = true;
+
+            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
+                disk->vdev, disk->irq, disk->base);
+        }
+    }
+
+    if (virtio_enabled)
+        nr_spis += (virtio_irq - 32) + 1;
+
     for (i = 0; i < d_config->b_info.num_irqs; i++) {
         uint32_t irq = d_config->b_info.irqs[i];
         uint32_t spi;
@@ -58,6 +124,13 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             return ERROR_FAIL;
         }
 
+        /* The same check as for vpl011 */
+        if (virtio_enabled &&
+            (irq >= GUEST_VIRTIO_MMIO_SPI_FIRST && irq <= virtio_irq)) {
+            LOG(ERROR, "Physical IRQ %u conflicting with Virtio MMIO IRQ range\n", irq);
+            return ERROR_FAIL;
+        }
+
         if (irq < 32)
             continue;
 
@@ -787,6 +860,39 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
     return 0;
 }
 
+
+static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
+                                 uint64_t base, uint32_t irq)
+{
+    int res;
+    gic_interrupt intr;
+    /* Placeholder for virtio@ + a 64-bit number + \0 */
+    char buf[24];
+
+    snprintf(buf, sizeof(buf), "virtio@%"PRIx64, base);
+    res = fdt_begin_node(fdt, buf);
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "virtio,mmio");
+    if (res) return res;
+
+    res = fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS, GUEST_ROOT_SIZE_CELLS,
+                            1, base, VIRTIO_MMIO_DEV_SIZE);
+    if (res) return res;
+
+    set_interrupt(intr, irq, 0xf, DT_IRQ_TYPE_EDGE_RISING);
+    res = fdt_property_interrupts(gc, fdt, &intr, 1);
+    if (res) return res;
+
+    res = fdt_property(fdt, "dma-coherent", NULL, 0);
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return 0;
+}
+
 static const struct arch_info *get_arch_info(libxl__gc *gc,
                                              const struct xc_dom_image *dom)
 {
@@ -988,6 +1094,7 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
     size_t fdt_size = 0;
     int pfdt_size = 0;
     libxl_domain_build_info *const info = &d_config->b_info;
+    unsigned int i;
 
     const libxl_version_info *vers;
     const struct arch_info *ainfo;
@@ -1094,6 +1201,13 @@ next_resize:
         if (d_config->num_pcidevs)
             FDT( make_vpci_node(gc, fdt, ainfo, dom) );
 
+        for (i = 0; i < d_config->num_disks; i++) {
+            libxl_device_disk *disk = &d_config->disks[i];
+
+            if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO)
+                FDT( make_virtio_mmio_node(gc, fdt, disk->base, disk->irq) );
+        }
+
         if (pfdt)
             FDT( copy_partial_fdt(gc, fdt, pfdt) );
 
xen/include/public/arch-arm.h +7 -0
index ab05fe1..c8b6058 100644
@@ -407,6 +407,10 @@ typedef uint64_t xen_callback_t;
 
 /* Physical Address Space */
 
+/* Virtio MMIO mappings */
+#define GUEST_VIRTIO_MMIO_BASE   xen_mk_ullong(0x02000000)
+#define GUEST_VIRTIO_MMIO_SIZE   xen_mk_ullong(0x00100000)
+
 /*
  * vGIC mappings: Only one set of mapping is used by the guest.
  * Therefore they can overlap.
@@ -493,6 +497,9 @@ typedef uint64_t xen_callback_t;
 
 #define GUEST_VPL011_SPI        32
 
+#define GUEST_VIRTIO_MMIO_SPI_FIRST   33
+#define GUEST_VIRTIO_MMIO_SPI_LAST    43
+
 /* PSCI functions */
 #define PSCI_cpu_suspend 0
 #define PSCI_cpu_off     1
Signature · 1 line
2.7.4
Reviewed-by on v8 2/2
#5 lore raw

Re: [PATCH V8 2/2] libxl: Introduce basic virtio-mmio support on Arm

Quoted 18 lines · Oleksandr Tyshchenko

From: Julien Grall <[email protected]>

This patch introduces helpers to allocate Virtio MMIO params (IRQ and memory region) and create specific device node in the Guest device-tree with allocated params. In order to deal with multiple Virtio devices, reserve corresponding ranges. For now, we reserve 1MB for memory regions and 10 SPIs.

As these helpers should be used for every Virtio device attached to the Guest, call them for Virtio disk(s).

Please note, with statically allocated Virtio IRQs there is a risk of a clash with a physical IRQs of passthrough devices. For the first version, it's fine, but we should consider allocating the Virtio IRQs automatically. Thankfully, we know in advance which IRQs will be used for passthrough to be able to choose non-clashed ones.

Quoted 2 lines
Quoted 254 lines

---
Please note, this is a split/cleanup/hardening of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"

Changes RFC -> V1:
- was squashed with:
"[RFC PATCH V1 09/12] libxl: Handle virtio-mmio irq in more correct way" "[RFC PATCH V1 11/12] libxl: Insert "dma-coherent" property into virtio-mmio device node" "[RFC PATCH V1 12/12] libxl: Fix duplicate memory node in DT"
- move VirtIO MMIO #define-s to xen/include/public/arch-arm.h

Changes V1 -> V2:
- update the author of a patch

Changes V2 -> V3:
- no changes

Changes V3 -> V4:
- no changes

Changes V4 -> V5:
- split the changes, change the order of the patches
- drop an extra "virtio" configuration option
- update patch description
- use CONTAINER_OF instead of own implementation
- reserve ranges for Virtio MMIO params and put them in correct location
- create helpers to allocate Virtio MMIO params, add corresponding sanity-сhecks
- add comment why MMIO size 0x200 is chosen
- update debug print
- drop Wei's T-b

Changes V5 -> V6:
- rebase on current staging

Changes V6 -> V7:
- rebase on current staging
- add T-b and R-b tags
- update according to the recent changes to "libxl: Add support for Virtio disk configuration"

Changes V7 -> V8:
- drop T-b and R-b tags
- make virtio_mmio_base/irq global variables to be local in libxl__arch_domain_prepare_config() and initialize them at the beginning of the function, then rework alloc_virtio_mmio_base/irq() to take a pointer to virtio_mmio_base/irq variables as an argument
- update according to the recent changes to
"libxl: Add support for Virtio disk configuration"

---
 tools/libs/light/libxl_arm.c  | 118 +++++++++++++++++++++++++++++++++++++++++-
 xen/include/public/arch-arm.h |   7 +++
 2 files changed, 123 insertions(+), 2 deletions(-)
tools/libs/light/libxl_arm.c +116 -2
index eef1de0..37403a2 100644
@@ -8,6 +8,46 @@
 #include <assert.h>
 #include <xen/device_tree_defs.h>
 
+/*
+ * There is no clear requirements for the total size of Virtio MMIO region.
+ * The size of control registers is 0x100 and device-specific configuration
+ * registers starts at the offset 0x100, however it's size depends on the device
+ * and the driver. Pick the biggest known size at the moment to cover most
+ * of the devices (also consider allowing the user to configure the size via
+ * config file for the one not conforming with the proposed value).
+ */
+#define VIRTIO_MMIO_DEV_SIZE   xen_mk_ullong(0x200)
+
+static uint64_t alloc_virtio_mmio_base(libxl__gc *gc, uint64_t *virtio_mmio_base)
+{
+    uint64_t base = *virtio_mmio_base;
+
+    /* Make sure we have enough reserved resources */
+    if ((base + VIRTIO_MMIO_DEV_SIZE >
+        GUEST_VIRTIO_MMIO_BASE + GUEST_VIRTIO_MMIO_SIZE)) {
+        LOG(ERROR, "Ran out of reserved range for Virtio MMIO BASE 0x%"PRIx64"\n",
+            base);
+        return 0;
+    }
+    *virtio_mmio_base += VIRTIO_MMIO_DEV_SIZE;
+
+    return base;
+}
+
+static uint32_t alloc_virtio_mmio_irq(libxl__gc *gc, uint32_t *virtio_mmio_irq)
+{
+    uint32_t irq = *virtio_mmio_irq;
+
+    /* Make sure we have enough reserved resources */
+    if (irq > GUEST_VIRTIO_MMIO_SPI_LAST) {
+        LOG(ERROR, "Ran out of reserved range for Virtio MMIO IRQ %u\n", irq);
+        return 0;
+    }
+    (*virtio_mmio_irq)++;
+
+    return irq;
+}
+
 static const char *gicv_to_string(libxl_gic_version gic_version)
 {
     switch (gic_version) {
@@ -26,8 +66,10 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
     uint32_t nr_spis = 0;
     unsigned int i;
-    uint32_t vuart_irq;
-    bool vuart_enabled = false;
+    uint32_t vuart_irq, virtio_irq = 0;
+    bool vuart_enabled = false, virtio_enabled = false;
+    uint64_t virtio_mmio_base = GUEST_VIRTIO_MMIO_BASE;
+    uint32_t virtio_mmio_irq = GUEST_VIRTIO_MMIO_SPI_FIRST;
 
     /*
      * If pl011 vuart is enabled then increment the nr_spis to allow allocation
@@ -39,6 +81,30 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         vuart_enabled = true;
     }
 
+    for (i = 0; i < d_config->num_disks; i++) {
+        libxl_device_disk *disk = &d_config->disks[i];
+
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
+            if (!disk->base)
+                return ERROR_FAIL;
+
+            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
+            if (!disk->irq)
+                return ERROR_FAIL;
+
+            if (virtio_irq < disk->irq)
+                virtio_irq = disk->irq;
+            virtio_enabled = true;
+
+            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
+                disk->vdev, disk->irq, disk->base);
+        }
+    }
+
+    if (virtio_enabled)
+        nr_spis += (virtio_irq - 32) + 1;
+
     for (i = 0; i < d_config->b_info.num_irqs; i++) {
         uint32_t irq = d_config->b_info.irqs[i];
         uint32_t spi;
@@ -58,6 +124,13 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             return ERROR_FAIL;
         }
 
+        /* The same check as for vpl011 */
+        if (virtio_enabled &&
+            (irq >= GUEST_VIRTIO_MMIO_SPI_FIRST && irq <= virtio_irq)) {
+            LOG(ERROR, "Physical IRQ %u conflicting with Virtio MMIO IRQ range\n", irq);
+            return ERROR_FAIL;
+        }
+
         if (irq < 32)
             continue;
 
@@ -787,6 +860,39 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
     return 0;
 }
 
+
+static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
+                                 uint64_t base, uint32_t irq)
+{
+    int res;
+    gic_interrupt intr;
+    /* Placeholder for virtio@ + a 64-bit number + \0 */
+    char buf[24];
+
+    snprintf(buf, sizeof(buf), "virtio@%"PRIx64, base);
+    res = fdt_begin_node(fdt, buf);
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "virtio,mmio");
+    if (res) return res;
+
+    res = fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS, GUEST_ROOT_SIZE_CELLS,
+                            1, base, VIRTIO_MMIO_DEV_SIZE);
+    if (res) return res;
+
+    set_interrupt(intr, irq, 0xf, DT_IRQ_TYPE_EDGE_RISING);
+    res = fdt_property_interrupts(gc, fdt, &intr, 1);
+    if (res) return res;
+
+    res = fdt_property(fdt, "dma-coherent", NULL, 0);
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return 0;
+}
+
 static const struct arch_info *get_arch_info(libxl__gc *gc,
                                              const struct xc_dom_image *dom)
 {
@@ -988,6 +1094,7 @@ static int libxl__prepare_dtb(libxl__gc *gc, libxl_domain_config *d_config,
     size_t fdt_size = 0;
     int pfdt_size = 0;
     libxl_domain_build_info *const info = &d_config->b_info;
+    unsigned int i;
 
     const libxl_version_info *vers;
     const struct arch_info *ainfo;
@@ -1094,6 +1201,13 @@ next_resize:
         if (d_config->num_pcidevs)
             FDT( make_vpci_node(gc, fdt, ainfo, dom) );
 
+        for (i = 0; i < d_config->num_disks; i++) {
+            libxl_device_disk *disk = &d_config->disks[i];
+
+            if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO)
+                FDT( make_virtio_mmio_node(gc, fdt, disk->base, disk->irq) );
+        }
+
         if (pfdt)
             FDT( copy_partial_fdt(gc, fdt, pfdt) );
 
xen/include/public/arch-arm.h +7 -0
index ab05fe1..c8b6058 100644
@@ -407,6 +407,10 @@ typedef uint64_t xen_callback_t;
 
 /* Physical Address Space */
 
+/* Virtio MMIO mappings */
+#define GUEST_VIRTIO_MMIO_BASE   xen_mk_ullong(0x02000000)
+#define GUEST_VIRTIO_MMIO_SIZE   xen_mk_ullong(0x00100000)
+
 /*
  * vGIC mappings: Only one set of mapping is used by the guest.
  * Therefore they can overlap.
@@ -493,6 +497,9 @@ typedef uint64_t xen_callback_t;
 
 #define GUEST_VPL011_SPI        32
 
+#define GUEST_VIRTIO_MMIO_SPI_FIRST   33
+#define GUEST_VIRTIO_MMIO_SPI_LAST    43
+
 /* PSCI functions */
 #define PSCI_cpu_suspend 0
 #define PSCI_cpu_off     1
Signature · 1 line
2.7.4
#6 lore raw

Re: [PATCH V8 2/2] libxl: Introduce basic virtio-mmio support on Arm

In reply to Oleksandr Tyshchenko
Quoted 36 lines · Oleksandr Tyshchenko

From: Julien Grall <[email protected]>

This patch introduces helpers to allocate Virtio MMIO params (IRQ and memory region) and create specific device node in the Guest device-tree with allocated params. In order to deal with multiple Virtio devices, reserve corresponding ranges. For now, we reserve 1MB for memory regions and 10 SPIs.

As these helpers should be used for every Virtio device attached to the Guest, call them for Virtio disk(s).

Please note, with statically allocated Virtio IRQs there is a risk of a clash with a physical IRQs of passthrough devices. For the first version, it's fine, but we should consider allocating the Virtio IRQs automatically. Thankfully, we know in advance which IRQs will be used for passthrough to be able to choose non-clashed ones.

---

tools/libs/light/libxl_arm.c +7 -0
index eef1de0..37403a2 100644
@@ -8,6 +8,46 @@
 #include <assert.h>
 #include <xen/device_tree_defs.h>
 
+/*
+ * There is no clear requirements for the total size of Virtio MMIO region.
+ * The size of control registers is 0x100 and device-specific configuration
+ * registers starts at the offset 0x100, however it's size depends on the device
+ * and the driver. Pick the biggest known size at the moment to cover most
+ * of the devices (also consider allowing the user to configure the size via
+ * config file for the one not conforming with the proposed value).

tools/libs/light/libxl_arm.c line 27

Quoted 10 lines
tools/libs/light/libxl_arm.c +10 -0
+ */
+#define VIRTIO_MMIO_DEV_SIZE   xen_mk_ullong(0x200)
+
+static uint64_t alloc_virtio_mmio_base(libxl__gc *gc, uint64_t *virtio_mmio_base)
+{
+    uint64_t base = *virtio_mmio_base;
+
+    /* Make sure we have enough reserved resources */
+    if ((base + VIRTIO_MMIO_DEV_SIZE >
+        GUEST_VIRTIO_MMIO_BASE + GUEST_VIRTIO_MMIO_SIZE)) {

Could you remove the second set of parentheses? I'd like the compiler to warn us if there's an assignment.

Quoted 30 lines
tools/libs/light/libxl_arm.c +17 -2
@@ -26,8 +66,10 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
     uint32_t nr_spis = 0;
     unsigned int i;
-    uint32_t vuart_irq;
-    bool vuart_enabled = false;
+    uint32_t vuart_irq, virtio_irq = 0;
+    bool vuart_enabled = false, virtio_enabled = false;
+    uint64_t virtio_mmio_base = GUEST_VIRTIO_MMIO_BASE;
+    uint32_t virtio_mmio_irq = GUEST_VIRTIO_MMIO_SPI_FIRST;
 
     /*
      * If pl011 vuart is enabled then increment the nr_spis to allow allocation
@@ -39,6 +81,30 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         vuart_enabled = true;
     }
 
+    for (i = 0; i < d_config->num_disks; i++) {
+        libxl_device_disk *disk = &d_config->disks[i];
+
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
+            if (!disk->base)
+                return ERROR_FAIL;
+
+            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
+            if (!disk->irq)
+                return ERROR_FAIL;
+
+            if (virtio_irq < disk->irq)

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 106

Quoted 10 lines
tools/libs/light/libxl_arm.c +10 -0
+                virtio_irq = disk->irq;
+            virtio_enabled = true;
+
+            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
+                disk->vdev, disk->irq, disk->base);
+        }
+    }
+
+    if (virtio_enabled)
+        nr_spis += (virtio_irq - 32) + 1;

Is it possible to update "nr_spis" inside the loop? The added value seems to be "number of virtio device + 1", so updating "nr_spis" and adding +1 after the loop could work, right?

Also, what is "32"? Is it "GUEST_VIRTIO_MMIO_SPI_FIRST - 1" ?

Quoted 8 lines
tools/libs/light/libxl_arm.c +1 -0
+
     for (i = 0; i < d_config->b_info.num_irqs; i++) {
         uint32_t irq = d_config->b_info.irqs[i];
         uint32_t spi;
@@ -787,6 +860,39 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
     return 0;
 }
 

tools/libs/light/libxl_arm.c static int make_vpci_node(libxl__gc *gc, void *fdt, · line 872

Quoted 10 lines
tools/libs/light/libxl_arm.c +10 -0
+
+static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
+                                 uint64_t base, uint32_t irq)
+{
+    int res;
+    gic_interrupt intr;
+    /* Placeholder for virtio@ + a 64-bit number + \0 */
+    char buf[24];
+
+    snprintf(buf, sizeof(buf), "virtio@%"PRIx64, base);

Could you use GCSPRINTF() here instead of using a buffer of a static size calculated by hand which is potentially wrong? Also, the return value of snprintf isn't checked so the string could be truncated without warning. So I think GCSPRINTF is better than a static buffer.

The rest of the patch looks fine.

Thanks,

Signature · 1 line
Anthony PERARD
author
#7 lore raw

Re: [PATCH V8 2/2] libxl: Introduce basic virtio-mmio support on Arm

On 18.05.22 14:05, Anthony PERARD wrote:

Hello Anthony

Quoted 41 lines · Oleksandr Tyshchenko
Quoted 40 lines

From: Julien Grall <[email protected]>

This patch introduces helpers to allocate Virtio MMIO params (IRQ and memory region) and create specific device node in the Guest device-tree with allocated params. In order to deal with multiple Virtio devices, reserve corresponding ranges. For now, we reserve 1MB for memory regions and 10 SPIs.

As these helpers should be used for every Virtio device attached to the Guest, call them for Virtio disk(s).

Please note, with statically allocated Virtio IRQs there is a risk of a clash with a physical IRQs of passthrough devices. For the first version, it's fine, but we should consider allocating the Virtio IRQs automatically. Thankfully, we know in advance which IRQs will be used for passthrough to be able to choose non-clashed ones.

---

tools/libs/light/libxl_arm.c +11 -0
index eef1de0..37403a2 100644
@@ -8,6 +8,46 @@
  #include <assert.h>
  #include <xen/device_tree_defs.h>
 
+/*
+ * There is no clear requirements for the total size of Virtio MMIO region.
+ * The size of control registers is 0x100 and device-specific configuration
+ * registers starts at the offset 0x100, however it's size depends on the device
+ * and the driver. Pick the biggest known size at the moment to cover most
+ * of the devices (also consider allowing the user to configure the size via
+ * config file for the one not conforming with the proposed value).
+ */
+#define VIRTIO_MMIO_DEV_SIZE   xen_mk_ullong(0x200)
+
+static uint64_t alloc_virtio_mmio_base(libxl__gc *gc, uint64_t *virtio_mmio_base)
Quoted 8 lines

tools/libs/light/libxl_arm.c line 27

Quoted 6 lines
tools/libs/light/libxl_arm.c +6 -0
+{
+    uint64_t base = *virtio_mmio_base;
+
+    /* Make sure we have enough reserved resources */
+    if ((base + VIRTIO_MMIO_DEV_SIZE >
+        GUEST_VIRTIO_MMIO_BASE + GUEST_VIRTIO_MMIO_SIZE)) {

Could you remove the second set of parentheses? I'd like the compiler to warn us if there's an assignment.

ok

Quoted 34 lines
Quoted 34 lines
tools/libs/light/libxl_arm.c +20 -2
@@ -26,8 +66,10 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
  {
      uint32_t nr_spis = 0;
      unsigned int i;
-    uint32_t vuart_irq;
-    bool vuart_enabled = false;
+    uint32_t vuart_irq, virtio_irq = 0;
+    bool vuart_enabled = false, virtio_enabled = false;
+    uint64_t virtio_mmio_base = GUEST_VIRTIO_MMIO_BASE;
+    uint32_t virtio_mmio_irq = GUEST_VIRTIO_MMIO_SPI_FIRST;
 
      /*
       * If pl011 vuart is enabled then increment the nr_spis to allow allocation
@@ -39,6 +81,30 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
          vuart_enabled = true;
      }
 
+    for (i = 0; i < d_config->num_disks; i++) {
+        libxl_device_disk *disk = &d_config->disks[i];
+
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
+            if (!disk->base)
+                return ERROR_FAIL;
+
+            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
+            if (!disk->irq)
+                return ERROR_FAIL;
+
+            if (virtio_irq < disk->irq)
+                virtio_irq = disk->irq;
+            virtio_enabled = true;
+
Quoted 8 lines

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 106

Quoted 7 lines
tools/libs/light/libxl_arm.c +7 -0
+            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
+                disk->vdev, disk->irq, disk->base);
+        }
+    }
+
+    if (virtio_enabled)
+        nr_spis += (virtio_irq - 32) + 1;

Is it possible to update "nr_spis" inside the loop?

yes, but ...

Quoted 2 lines

The added value
seems to be "number of virtio device + 1",

   ... not really ...

Quoted 2 lines

so updating "nr_spis" and
adding +1 after the loop could work, right?

   ... from my understanding, we cannot just increment nr_spis by "one" inside a loop, we need to calculate it.

Something like that (not tested):

       uint32_t spi;

       ...

       spi = irq - 32;
       if (nr_spis <= spi)
           nr_spis = spi + 1;

Shall I update "nr_spis" inside the loop?

Are you asking because of eliminating "virtio_enabled" and/or "virtio_irq" locals? They are used down the code.

Quoted 2 lines

Also, what is "32"? Is it "GUEST_VIRTIO_MMIO_SPI_FIRST - 1" ?

Although currently "GUEST_VIRTIO_MMIO_SPI_FIRST - 1" = "32", we cannot rely on this (I mean to use "GUEST_VIRTIO_MMIO_SPI_FIRST - 1"

instead of "32"),  because "32" has yet another meaning. This is an offset for SPI, the values before 32 are for private IRQs (PPI, SGI).

Quoted 9 lines
Quoted 9 lines
tools/libs/light/libxl_arm.c +1 -0
+
      for (i = 0; i < d_config->b_info.num_irqs; i++) {
          uint32_t irq = d_config->b_info.irqs[i];
          uint32_t spi;
@@ -787,6 +860,39 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
      return 0;
  }
 
Quoted 14 lines

tools/libs/light/libxl_arm.c static int make_vpci_node(libxl__gc *gc, void *fdt, · line 872

Quoted 10 lines
tools/libs/light/libxl_arm.c +10 -0
+
+static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
+                                 uint64_t base, uint32_t irq)
+{
+    int res;
+    gic_interrupt intr;
+    /* Placeholder for virtio@ + a 64-bit number + \0 */
+    char buf[24];
+
+    snprintf(buf, sizeof(buf), "virtio@%"PRIx64, base);

Could you use GCSPRINTF() here instead of using a buffer of a static size calculated by hand which is potentially wrong? Also, the return value of snprintf isn't checked so the string could be truncated without warning. So I think GCSPRINTF is better than a static buffer.

I got it, thank you for detailed explanation, will use.

Quoted 4 lines

The rest of the patch looks fine.

Thank you.

Quoted 3 lines

Thanks,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#8 lore raw

Re: [PATCH V8 2/2] libxl: Introduce basic virtio-mmio support on Arm

Quoted 54 lines · Oleksandr
Quoted 17 lines · Anthony PERARD
Quoted 16 lines · Oleksandr Tyshchenko
tools/libs/light/libxl_arm.c +16 -0
+    for (i = 0; i < d_config->num_disks; i++) {
+        libxl_device_disk *disk = &d_config->disks[i];
+
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
+            if (!disk->base)
+                return ERROR_FAIL;
+
+            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
+            if (!disk->irq)
+                return ERROR_FAIL;
+
+            if (virtio_irq < disk->irq)
+                virtio_irq = disk->irq;
+            virtio_enabled = true;
+
Quoted 8 lines

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 106

Quoted 7 lines
tools/libs/light/libxl_arm.c +7 -0
+            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
+                disk->vdev, disk->irq, disk->base);
+        }
+    }
+
+    if (virtio_enabled)
+        nr_spis += (virtio_irq - 32) + 1;

Is it possible to update "nr_spis" inside the loop?

yes, but ...

Quoted 2 lines

The added value
seems to be "number of virtio device + 1",

   ... not really ...

Quoted 2 lines

so updating "nr_spis" and
adding +1 after the loop could work, right?

   ... from my understanding, we cannot just increment nr_spis by "one" inside a loop, we need to calculate it.

Something like that (not tested):

       uint32_t spi;

       ...

       spi = irq - 32;
       if (nr_spis <= spi)
           nr_spis = spi + 1;

Quoted 4 lines

Shall I update "nr_spis" inside the loop?

Are you asking because of eliminating "virtio_enabled" and/or "virtio_irq" locals? They are used down the code.

I'm asking because the calculation doesn't really make sense to me yet. At the moment "virtio_irq-32+1" happen to be the "number of disk + 1" and we have "nr_spis += " which I don't think makes sense with the "+1".

Doesn't "nr_spis" only need to be the highest irq value for the devices we're adding? (Maybe with +1) (also -32 because I think I understand what 32 stand for now) (also, the "num_irqs" loop just after this loop seems to do exactly that)

But I think what this line of code needs the most is a comment.

Quoted 7 lines
Quoted 1 line

Also, what is "32"? Is it "GUEST_VIRTIO_MMIO_SPI_FIRST - 1" ?

Although currently "GUEST_VIRTIO_MMIO_SPI_FIRST - 1" = "32", we cannot rely on this (I mean to use "GUEST_VIRTIO_MMIO_SPI_FIRST - 1"

instead of "32"),  because "32" has yet another meaning. This is an offset for SPI, the values before 32 are for private IRQs (PPI, SGI).

Do you think it could be possible to name that value? I've seen other use of 32 in the same function that have probably the same meaning. But if you don't have a good name, I guess we can also live a bit longer with a plain "32".

Cheers,

Signature · 1 line
Anthony PERARD
author
#9 lore raw

Re: [PATCH V8 2/2] libxl: Introduce basic virtio-mmio support on Arm

On 20.05.22 18:22, Anthony PERARD wrote:

Hello Anthony

Quoted 52 lines · Oleksandr
Quoted 51 lines · Anthony PERARD
Quoted 17 lines · Oleksandr Tyshchenko
Quoted 16 lines
tools/libs/light/libxl_arm.c +16 -0
+    for (i = 0; i < d_config->num_disks; i++) {
+        libxl_device_disk *disk = &d_config->disks[i];
+
+        if (disk->specification == LIBXL_DISK_SPECIFICATION_VIRTIO) {
+            disk->base = alloc_virtio_mmio_base(gc, &virtio_mmio_base);
+            if (!disk->base)
+                return ERROR_FAIL;
+
+            disk->irq = alloc_virtio_mmio_irq(gc, &virtio_mmio_irq);
+            if (!disk->irq)
+                return ERROR_FAIL;
+
+            if (virtio_irq < disk->irq)
+                virtio_irq = disk->irq;
+            virtio_enabled = true;
+
Quoted 8 lines

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 106

Quoted 7 lines
tools/libs/light/libxl_arm.c +7 -0
+            LOG(DEBUG, "Allocate Virtio MMIO params for Vdev %s: IRQ %u BASE 0x%"PRIx64,
+                disk->vdev, disk->irq, disk->base);
+        }
+    }
+
+    if (virtio_enabled)
+        nr_spis += (virtio_irq - 32) + 1;

Is it possible to update "nr_spis" inside the loop?

yes, but ...

Quoted 2 lines

The added value
seems to be "number of virtio device + 1",

   ... not really ...

Quoted 2 lines

so updating "nr_spis" and
adding +1 after the loop could work, right?

   ... from my understanding, we cannot just increment nr_spis by "one"
inside a loop, we need to calculate it.

Something like that (not tested):

       uint32_t spi;

       ...

       spi = irq - 32;
       if (nr_spis <= spi)
           nr_spis = spi + 1;

Quoted 7 lines
Quoted 4 lines

Shall I update "nr_spis" inside the loop?

Are you asking because of eliminating "virtio_enabled" and/or "virtio_irq" locals? They are used down the code.

I'm asking because the calculation doesn't really make sense to me yet. At the moment "virtio_irq-32+1" happen to be the "number of disk + 1" and we have "nr_spis += " which I don't think makes sense with the "+1".

I see

Quoted 5 lines

Doesn't "nr_spis" only need to be the highest irq value for the devices we're adding? (Maybe with +1) (also -32 because I think I understand what 32 stand for now) (also, the "num_irqs" loop just after this loop seems to do exactly that)

I also think the same, the "nr_spis" needs to cover the highest SPI.

Quoted 2 lines

But I think what this line of code needs the most is a comment.

ok

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

Also, what is "32"? Is it "GUEST_VIRTIO_MMIO_SPI_FIRST - 1" ?

Although currently "GUEST_VIRTIO_MMIO_SPI_FIRST - 1" = "32", we cannot rely on this (I mean to use "GUEST_VIRTIO_MMIO_SPI_FIRST - 1"

instead of "32"),  because "32" has yet another meaning. This is an offset for SPI, the values before 32 are for private IRQs (PPI, SGI).

Do you think it could be possible to name that value? I've seen other use of 32 in the same function that have probably the same meaning.

yes, all uses of "32" within current function have the same meaning.

Quoted 3 lines

But
if you don't have a good name, I guess we can also live a bit longer with a plain "32".

Except here in toolstack, this plain "32" is used in a few places in hypervisor code also.

I don't have a plan to convert this value into appropriate #define everywhere.

But, I can introduce local to this file #define NR_LOCAL_IRQS 32 and change in current function.

Shall I?

Quoted 3 lines

Cheers,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko