Skip to contentSkip to conversation

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

PATCH Superseded Oleksandr Tyshchenko 3 participants 8 messages Jun 1, 2022 – Jun 10, 2022

v9 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v9 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 (49dd52fb1311dadab29f6634d0bc1f4c022c357a tools/xenstore: fix event sending in introduce_domain()) 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]/
https://lore.kernel.org/xen-devel/[email protected]/

[3] https://github.com/otyshchenko1/xen/commits/libxl_virtio4
[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              | 121 +++-
 tools/libs/light/libxl_device.c           |  62 +-
 tools/libs/light/libxl_disk.c             | 140 ++++-
 tools/libs/light/libxl_internal.h         |   2 +
 tools/libs/light/libxl_types.idl          |  18 +
 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, 923 insertions(+), 482 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v9 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

Changes V8 -> V9:
- update (and harden) checks in libxl__device_disk_setdefault(), return error in case of incorrect settings of specification
and transport
- remove both asserts in device_disk_add()
- update virtio related code in libxl__disk_from_xenstore(),
do not fail if specification node is absent, replace
open-coded checks of fetched specification and transport by libxl_disk_specification_from_string() and libxl_disk_transport_from_string() respectively
- s/libxl_device_disk_get_path/libxl__device_disk_get_path
- add a comment for virtio-mmio parameters in struct 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             | 140 ++++-
 tools/libs/light/libxl_internal.h         |   2 +
 tools/libs/light/libxl_types.idl          |  18 +
 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, 797 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 +133 -7

209 lines not on this page · raw message

tools/libs/light/libxl_internal.h +2 -0
index bdef5a6..cb9e8b3 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 +18 -0
index 2a42da2..858e32b 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,12 @@ 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),
+    # Note that virtio-mmio parameters (irq and base) are for internal use
+    # by libxl and can't be modified.
+    ("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 e5e6b2d..f55915e 100644
@@ -297,6 +297,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 V9 1/2] libxl: Add support for Virtio disk configuration

Quoted 5 lines · Oleksandr Tyshchenko
tools/libs/light/libxl_disk.c +0 -0
index a5ca778..e90bc25 100644

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

Quoted 10 lines
tools/libs/light/libxl_disk.c +7 -0
@@ -163,6 +163,25 @@ 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;
+
+    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;

Could you return ERROR_INVAL instead?

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

Quoted 8 lines
tools/libs/light/libxl_disk.c +8 -0
+    }
+
+    /* 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;

Same here.

Quoted 34 lines
tools/libs/light/libxl_disk.c +27 -0
+        }
+        disk->transport = LIBXL_DISK_TRANSPORT_MMIO;
+    }
+
     /* 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) {
@@ -575,6 +660,41 @@ cleanup:
     return rc;
 }
 
+static int libxl__device_disk_get_path(libxl__gc *gc, uint32_t domid,
+                                       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.
+     */
+    *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;

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

Quoted 10 lines
tools/libs/light/libxl_disk.c +10 -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;

I still don't like this implementation of get_path() which return a different answer depending on the environment which can change from one call to the next. I think get_path() was introduced when the path for the kind of device didn't correspond to the common path which other kind of devices uses. And when get_path() is implemented, it always returns the same answer (see libxl_pci.c for the only implementation).

I don't really know how to deal with a type of device that have two different frontend kind at the moment. (But maybe there's something in libxl_usb.c which could be useful as a potential example, but one of the kind doesn't use xenstore so is probably easier to deal with.). So I guess we are stuck with an implementation of get_path() which may return a different answer depending on thing outside of libxl's full control.

So, could you at least make it much harder to have libxl's view of a guest in a weird state? I mean:
- always check both xenstore paths
-> if both exist, return an error
-> if only one exist, return that one.
-> default to the "vbd" kind, otherwise.

That would be better than the current implementation which returns the "virtio" path by default.

Thanks,

Signature · 1 line
Anthony PERARD
author
#3 lore raw

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

On 10.06.22 18:12, Anthony PERARD wrote:

Hello Anthony

Quoted 9 lines · Oleksandr Tyshchenko
Quoted 8 lines
tools/libs/light/libxl_disk.c +0 -0
index a5ca778..e90bc25 100644
@@ -163,6 +163,25 @@ 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 8 lines

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

Quoted 7 lines
tools/libs/light/libxl_disk.c +7 -0
+    if (disk->specification == LIBXL_DISK_SPECIFICATION_UNKNOWN)
+        disk->specification = LIBXL_DISK_SPECIFICATION_XEN;
+
+    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;

Could you return ERROR_INVAL instead?

yes

Quoted 10 lines

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

Quoted 9 lines
tools/libs/light/libxl_disk.c +8 -0
+    }
+
+    /* 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;

Same here.

yes

Quoted 59 lines
Quoted 35 lines
tools/libs/light/libxl_disk.c +27 -0
+        }
+        disk->transport = LIBXL_DISK_TRANSPORT_MMIO;
+    }
+
      /* 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) {
@@ -575,6 +660,41 @@ cleanup:
      return rc;
  }
 
+static int libxl__device_disk_get_path(libxl__gc *gc, uint32_t domid,
+                                       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.
+     */
+    *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;

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

Quoted 10 lines
tools/libs/light/libxl_disk.c +10 -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;

I still don't like this implementation of get_path() which return a different answer depending on the environment which can change from one call to the next. I think get_path() was introduced when the path for the kind of device didn't correspond to the common path which other kind of devices uses. And when get_path() is implemented, it always returns the same answer (see libxl_pci.c for the only implementation).

I don't really know how to deal with a type of device that have two different frontend kind at the moment. (But maybe there's something in libxl_usb.c which could be useful as a potential example, but one of the kind doesn't use xenstore so is probably easier to deal with.). So I guess we are stuck with an implementation of get_path() which may return a different answer depending on thing outside of libxl's full control.

Quoted 6 lines

So, could you at least make it much harder to have libxl's view of a guest in a weird state? I mean:
- always check both xenstore paths
-> if both exist, return an error
-> if only one exist, return that one.
-> default to the "vbd" kind, otherwise.

I think I got your idea, will try to do this way

Quoted 3 lines

That would be better than the current implementation which returns the "virtio" path by default.

agree,

making virtio by default wasn't the original intention, it was entirely involuntarily))

Quoted 3 lines

Thanks,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
author
#4 lore raw

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

On 10.06.22 20:18, Oleksandr wrote:

Hello Anthony

Quoted 103 lines · Anthony PERARD

Hello Anthony

Quoted 10 lines · Oleksandr Tyshchenko
Quoted 9 lines
tools/libs/light/libxl_disk.c +0 -0
index a5ca778..e90bc25 100644
@@ -163,6 +163,25 @@ 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)
Quoted 8 lines

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

Quoted 7 lines
tools/libs/light/libxl_disk.c +6 -0
+        disk->specification = LIBXL_DISK_SPECIFICATION_XEN;
+
+    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;

Could you return ERROR_INVAL instead?

yes

Quoted 11 lines

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

Quoted 10 lines
tools/libs/light/libxl_disk.c +8 -0
+    }
+
+    /* 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;

Same here.

yes

Quoted 61 lines
Quoted 37 lines
tools/libs/light/libxl_disk.c +4 -0
+        }
+        disk->transport = LIBXL_DISK_TRANSPORT_MMIO;
+    }
+

      /* 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) {

tools/libs/light/libxl_disk.c +7 -0
@@ -575,6 +660,41 @@ cleanup:
       return rc;
   }
   +static int libxl__device_disk_get_path(libxl__gc *gc, uint32_t domid,
+                                       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. +     */
+    *path = GCSPRINTF("%s/device/%s",
+                      libxl__xs_libxl_path(gc, domid),
+ libxl__device_kind_to_string(LIBXL__DEVICE_KIND_VBD));

tools/libs/light/libxl_disk.c +7 -0
+
+    rc = libxl__xs_read_checked(gc, XBT_NULL, *path, &dir);
+    if (rc)
+        return rc;
+
+    if (dir)
+        return 0;

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

Quoted 10 lines
tools/libs/light/libxl_disk.c +10 -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;

I still don't like this implementation of get_path() which return a different answer depending on the environment which can change from one call to the next. I think get_path() was introduced when the path for the kind of device didn't correspond to the common path which other kind of devices uses. And when get_path() is implemented, it always returns the same answer (see libxl_pci.c for the only implementation).

I don't really know how to deal with a type of device that have two different frontend kind at the moment. (But maybe there's something in libxl_usb.c which could be useful as a potential example, but one of the kind doesn't use xenstore so is probably easier to deal with.). So I guess we are stuck with an implementation of get_path() which may return a different answer depending on thing outside of libxl's full control.

Quoted 8 lines
Quoted 6 lines

So, could you at least make it much harder to have libxl's view of a guest in a weird state? I mean:
- always check both xenstore paths
   -> if both exist, return an error
   -> if only one exist, return that one.
   -> default to the "vbd" kind, otherwise.

I think I got your idea, will try to do this way

I hope that this (it seems working) function follows your suggestion. If no objections, I will use it for V10.

static int libxl__device_disk_get_path(libxl__gc *gc, uint32_t domid, char **path)
{
    const char *xen_dir, *virtio_dir;
    char *xen_path, *virtio_path;
    int rc;

    xen_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, xen_path, &xen_dir); if (rc)
        return rc;

    virtio_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, virtio_path, &virtio_dir); if (rc)
        return rc;

    if (xen_dir && virtio_dir) {
        LOGD(ERROR, domid, "Invalid configuration, both xen and virtio paths are present"); return ERROR_INVAL;
    } else if (virtio_dir)
        *path = virtio_path;
    else
        *path = xen_path;

    return 0;
}

Quoted 14 lines
Quoted 5 lines

That would be better than the current implementation which returns the "virtio" path by default.

agree,

making virtio by default wasn't the original intention, it was entirely involuntarily))

Quoted 3 lines

Thanks,

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#5 lore raw

PATCH v9 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"

Changes V8 -> V9:
- Stefano already gave his Reviewed-by, I dropped it due to the changes
- remove the second set of parentheses for check in alloc_virtio_mmio_base()
- clarify the updating of "nr_spis" right after num_disks loop in libxl__arch_domain_prepare_config() and add a comment on top of it
- use GCSPRINTF() instead of using a buffer of a static size calculated by hand in make_virtio_mmio_node()

---
 tools/libs/light/libxl_arm.c  | 121 +++++++++++++++++++++++++++++++++++++++++-
 xen/include/public/arch-arm.h |   7 +++
 2 files changed, 126 insertions(+), 2 deletions(-)
tools/libs/light/libxl_arm.c +119 -2
index eef1de0..9be9b2a 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,35 @@ 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);
+        }
+    }
+
+    /*
+     * Every virtio-mmio device uses one emulated SPI. If Virtio devices are
+     * present, make sure that we allocate enough SPIs for them.
+     * The resulting "nr_spis" needs to cover the highest possible SPI.
+     */
+    if (virtio_enabled)
+        nr_spis = max(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 +129,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 +865,37 @@ 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;
+    const char *name = GCSPRINTF("virtio@%"PRIx64, base);
+
+    res = fdt_begin_node(fdt, name);
+    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 +1097,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 +1204,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 v9 2/2
#6 lore raw

Re: [PATCH V9 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 265 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"

Changes V8 -> V9:
- Stefano already gave his Reviewed-by, I dropped it due to the changes
- remove the second set of parentheses for check in alloc_virtio_mmio_base()
- clarify the updating of "nr_spis" right after num_disks loop in libxl__arch_domain_prepare_config() and add a comment on top of it
- use GCSPRINTF() instead of using a buffer of a static size calculated by hand in make_virtio_mmio_node()

---
 tools/libs/light/libxl_arm.c  | 121 +++++++++++++++++++++++++++++++++++++++++-
 xen/include/public/arch-arm.h |   7 +++
 2 files changed, 126 insertions(+), 2 deletions(-)
tools/libs/light/libxl_arm.c +119 -2
index eef1de0..9be9b2a 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,35 @@ 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);
+        }
+    }
+
+    /*
+     * Every virtio-mmio device uses one emulated SPI. If Virtio devices are
+     * present, make sure that we allocate enough SPIs for them.
+     * The resulting "nr_spis" needs to cover the highest possible SPI.
+     */
+    if (virtio_enabled)
+        nr_spis = max(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 +129,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 +865,37 @@ 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;
+    const char *name = GCSPRINTF("virtio@%"PRIx64, base);
+
+    res = fdt_begin_node(fdt, name);
+    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 +1097,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 +1204,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 v9 2/2
#7 lore raw

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

In reply to Oleksandr Tyshchenko
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

Thanks,

Signature · 1 line
Anthony PERARD