From nobody Thu Nov 6 19:53:33 2025 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542813293585775.2447275208812; Wed, 21 Nov 2018 07:14:53 -0800 (PST) Received: from localhost ([::1]:39687 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPUDC-0007b4-4o for importer@patchew.org; Wed, 21 Nov 2018 10:14:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46112) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPUB3-0005Tg-Tl for qemu-devel@nongnu.org; Wed, 21 Nov 2018 10:12:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gPUB1-0003ey-KD for qemu-devel@nongnu.org; Wed, 21 Nov 2018 10:12:29 -0500 Received: from smtp03.citrix.com ([162.221.156.55]:55928) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gPUAu-0003ST-Ms; Wed, 21 Nov 2018 10:12:20 -0500 X-IronPort-AV: E=Sophos;i="5.56,261,1539648000"; d="scan'208";a="71205794" From: Paul Durrant To: , , Date: Wed, 21 Nov 2018 15:11:56 +0000 Message-ID: <20181121151211.15997-4-paul.durrant@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181121151211.15997-1-paul.durrant@citrix.com> References: <20181121151211.15997-1-paul.durrant@citrix.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 162.221.156.55 Subject: [Qemu-devel] [PATCH 03/18] xen: introduce 'xen-qdisk' X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Anthony Perard , Paul Durrant , Stefano Stabellini , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch adds a new XenDevice: 'xen-qdisk' [1]. This will eventually replace the 'xen_disk' legacy PV backend but it is illustrative to build up the implementation incrementally, along with the XenBus/XenDevice framework. Subsequent patches will therefore add to this device's implementation as new features are added to the framework. After this patch has been applied it is possible to instantiate a new 'xen-qdisk' device with a single 'vdev' parameter, which accepts values adhering to the Xen VBD naming scheme [2]. For example, a command-line instantiation of a xen-qdisk can be done with an argument similar to the following: -device xen-qdisk,vdev=3Dhda The implementation of the vdev parameter formulates the appropriate VBD number for use in the PV protocol. [1] The name 'qdisk' as always been the name given to the QEMU implementation of the Xen PV block protocol backend implementation [2] http://xenbits.xen.org/gitweb/?p=3Dxen.git;a=3Dblob;f=3Ddocs/man/xen-vb= d-interface.markdown.7 Signed-off-by: Paul Durrant --- Cc: Kevin Wolf Cc: Max Reitz Cc: Stefano Stabellini Cc: Anthony Perard --- MAINTAINERS | 2 +- hw/block/Makefile.objs | 1 + hw/block/trace-events | 4 + hw/block/xen-qdisk.c | 256 +++++++++++++++++++++++++++++++++++++++++= ++++ include/hw/xen/xen-qdisk.h | 38 +++++++ 5 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 hw/block/xen-qdisk.c create mode 100644 include/hw/xen/xen-qdisk.h diff --git a/MAINTAINERS b/MAINTAINERS index 1032406c56..10f048a503 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -389,7 +389,7 @@ F: hw/9pfs/xen-9p-backend.c F: hw/char/xen_console.c F: hw/display/xenfb.c F: hw/net/xen_nic.c -F: hw/block/xen_* +F: hw/block/xen* F: hw/xen/ F: hw/xenpv/ F: hw/i386/xen/ diff --git a/hw/block/Makefile.objs b/hw/block/Makefile.objs index 53ce5751ae..bcdd36d318 100644 --- a/hw/block/Makefile.objs +++ b/hw/block/Makefile.objs @@ -5,6 +5,7 @@ common-obj-$(CONFIG_NAND) +=3D nand.o common-obj-$(CONFIG_PFLASH_CFI01) +=3D pflash_cfi01.o common-obj-$(CONFIG_PFLASH_CFI02) +=3D pflash_cfi02.o common-obj-$(CONFIG_XEN) +=3D xen_disk.o +common-obj-$(CONFIG_XEN) +=3D xen-qdisk.o common-obj-$(CONFIG_ECC) +=3D ecc.o common-obj-$(CONFIG_ONENAND) +=3D onenand.o common-obj-$(CONFIG_NVME_PCI) +=3D nvme.o diff --git a/hw/block/trace-events b/hw/block/trace-events index 335c092450..fd3c126ac1 100644 --- a/hw/block/trace-events +++ b/hw/block/trace-events @@ -127,3 +127,7 @@ xen_disk_init(char *name) "%s" xen_disk_connect(char *name) "%s" xen_disk_disconnect(char *name) "%s" xen_disk_free(char *name) "%s" + +# hw/block/xen-qdisk.c +xen_qdisk_realize(uint32_t disk, uint32_t partition) "d%up%u" +xen_qdisk_unrealize(uint32_t disk, uint32_t partition) "d%up%u" diff --git a/hw/block/xen-qdisk.c b/hw/block/xen-qdisk.c new file mode 100644 index 0000000000..72122073f7 --- /dev/null +++ b/hw/block/xen-qdisk.c @@ -0,0 +1,256 @@ +/* + * Copyright (c) Citrix Systems Inc. + * All rights reserved. + */ + +#include "qemu/osdep.h" +#include "qemu/cutils.h" +#include "qapi/error.h" +#include "qapi/visitor.h" +#include "hw/hw.h" +#include "hw/xen/xen-qdisk.h" +#include "trace.h" + +static void xen_qdisk_realize(XenDevice *xendev, Error **errp) +{ + XenQdiskDevice *qdiskdev =3D XEN_QDISK_DEVICE(xendev); + XenQdiskVdev *vdev =3D &qdiskdev->vdev; + + if (!vdev->valid) { + error_setg(errp, "vdev property not set"); + return; + } + + trace_xen_qdisk_realize(vdev->disk, vdev->partition); +} + +static void xen_qdisk_unrealize(XenDevice *xendev, Error **errp) +{ + XenQdiskDevice *qdiskdev =3D XEN_QDISK_DEVICE(xendev); + XenQdiskVdev *vdev =3D &qdiskdev->vdev; + + trace_xen_qdisk_unrealize(vdev->disk, vdev->partition); +} + +static char *disk_to_vbd_name(unsigned int disk) +{ + unsigned int len =3D DIV_ROUND_UP(disk, 26); + char *name =3D g_malloc0(len + 1); + + do { + name[len--] =3D 'a' + (disk % 26); + disk /=3D 26; + } while (disk !=3D 0); + assert(len =3D=3D 0); + + return name; +} + +static void xen_qdisk_get_vdev(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + DeviceState *dev =3D DEVICE(obj); + Property *prop =3D opaque; + XenQdiskVdev *vdev =3D qdev_get_prop_ptr(dev, prop); + char *str; + + switch (vdev->type) { + case XEN_QDISK_VDEV_TYPE_DP: + str =3D g_strdup_printf("d%lup%lu", vdev->disk, vdev->partition); + break; + + case XEN_QDISK_VDEV_TYPE_XVD: + case XEN_QDISK_VDEV_TYPE_HD: + case XEN_QDISK_VDEV_TYPE_SD: { + char *name =3D disk_to_vbd_name(vdev->disk); + + str =3D g_strdup_printf("%s%s%lu", + (vdev->type =3D=3D XEN_QDISK_VDEV_TYPE_XVD) ? + "xvd" : + (vdev->type =3D=3D XEN_QDISK_VDEV_TYPE_HD) ? + "hd" : + "sd", + name, vdev->partition); + g_free(name); + break; + } + default: + error_setg(errp, "invalid vdev type"); + return; + } + + visit_type_str(v, name, &str, errp); + g_free(str); +} + +static unsigned int vbd_name_to_disk(const char *name, const char **endp) +{ + unsigned int disk =3D 0; + + while (*name !=3D '\0') { + if (!g_ascii_isalpha(*name) || !g_ascii_islower(*name)) { + break; + } + + disk *=3D 26; + disk +=3D *name++ - 'a'; + } + *endp =3D name; + + return disk; +} + +static void xen_qdisk_set_vdev(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + DeviceState *dev =3D DEVICE(obj); + Property *prop =3D opaque; + XenQdiskVdev *vdev =3D qdev_get_prop_ptr(dev, prop); + Error *local_err =3D NULL; + char *str, *p; + const char *end; + + if (dev->realized) { + qdev_prop_set_after_realize(dev, name, errp); + return; + } + + visit_type_str(v, name, &str, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + + p =3D strchr(str, 'd'); + if (!p) { + goto invalid; + } + + *p++ =3D '\0'; + if (*str =3D=3D '\0') { + vdev->type =3D XEN_QDISK_VDEV_TYPE_DP; + } else if (strcmp(str, "xv") =3D=3D 0) { + vdev->type =3D XEN_QDISK_VDEV_TYPE_XVD; + } else if (strcmp(str, "h") =3D=3D 0) { + vdev->type =3D XEN_QDISK_VDEV_TYPE_HD; + } else if (strcmp(str, "s") =3D=3D 0) { + vdev->type =3D XEN_QDISK_VDEV_TYPE_SD; + } else { + goto invalid; + } + + if (vdev->type =3D=3D XEN_QDISK_VDEV_TYPE_DP) { + if (qemu_strtoul(p, &end, 10, &vdev->disk)) { + goto invalid; + } + + if (*end =3D=3D 'p') { + p =3D (char *) ++end; + if (*end =3D=3D '\0') { + goto invalid; + } + } + } else { + vdev->disk =3D vbd_name_to_disk(p, &end); + } + + if (*end !=3D '\0') { + p =3D (char *)end; + + if (qemu_strtoul(p, &end, 10, &vdev->partition)) { + goto invalid; + } + + if (*end !=3D '\0') { + goto invalid; + } + } else { + vdev->partition =3D 0; + } + + switch (vdev->type) { + case XEN_QDISK_VDEV_TYPE_DP: + case XEN_QDISK_VDEV_TYPE_XVD: + if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) { + vdev->number =3D (202 << 8) | (vdev->disk << 4) | + vdev->partition; + } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) { + vdev->number =3D (1 << 28) | (vdev->disk << 8) | + vdev->partition; + } else { + goto invalid; + } + break; + + case XEN_QDISK_VDEV_TYPE_HD: + if ((vdev->disk =3D=3D 0 || vdev->disk =3D=3D 1) && + vdev->partition < (1 << 4)) { + vdev->number =3D (3 << 8) | (vdev->disk << 6) | vdev->partitio= n; + } else if ((vdev->disk =3D=3D 2 || vdev->disk =3D=3D 3) && + vdev->partition < (1 << 4)) { + vdev->number =3D (22 << 8) | ((vdev->disk - 2) << 6) | + vdev->partition; + } else { + goto invalid; + } + break; + + case XEN_QDISK_VDEV_TYPE_SD: + if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) { + vdev->number =3D (8 << 8) | (vdev->disk << 4) | vdev->partitio= n; + } else { + goto invalid; + } + break; + + default: + goto invalid; + } + + g_free(str); + vdev->valid =3D true; + return; + +invalid: + error_setg(errp, "invalid virtual disk specifier"); + g_free(str); +} + +const PropertyInfo xen_qdisk_prop_vdev =3D { + .name =3D "str", + .description =3D "Virtual Disk specifier: d*p*/xvd*/hd*/sd*", + .get =3D xen_qdisk_get_vdev, + .set =3D xen_qdisk_set_vdev, +}; + +static Property xen_qdisk_props[] =3D { + DEFINE_PROP("vdev", XenQdiskDevice, vdev, + xen_qdisk_prop_vdev, XenQdiskVdev), + DEFINE_PROP_END_OF_LIST() +}; + +static void xen_qdisk_class_init(ObjectClass *class, void *data) +{ + DeviceClass *dev_class =3D DEVICE_CLASS(class); + XenDeviceClass *xendev_class =3D XEN_DEVICE_CLASS(class); + + xendev_class->realize =3D xen_qdisk_realize; + xendev_class->unrealize =3D xen_qdisk_unrealize; + + dev_class->desc =3D "Xen Qdisk Device"; + dev_class->props =3D xen_qdisk_props; +} + +static const TypeInfo xen_qdisk_type_info =3D { + .name =3D TYPE_XEN_QDISK_DEVICE, + .parent =3D TYPE_XEN_DEVICE, + .instance_size =3D sizeof(XenQdiskDevice), + .class_init =3D xen_qdisk_class_init, +}; + +static void xen_qdisk_register_types(void) +{ + type_register_static(&xen_qdisk_type_info); +} + +type_init(xen_qdisk_register_types) diff --git a/include/hw/xen/xen-qdisk.h b/include/hw/xen/xen-qdisk.h new file mode 100644 index 0000000000..ade0866037 --- /dev/null +++ b/include/hw/xen/xen-qdisk.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) Citrix Systems Inc. + * All rights reserved. + */ + +#ifndef HW_XEN_QDISK_H +#define HW_XEN_QDISK_H + +#include "hw/xen/xen-bus.h" + +typedef enum XenQdiskVdevType { + XEN_QDISK_VDEV_TYPE_DP, + XEN_QDISK_VDEV_TYPE_XVD, + XEN_QDISK_VDEV_TYPE_HD, + XEN_QDISK_VDEV_TYPE_SD, + XEN_QDISK_VDEV_TYPE__MAX +} XenQdiskVdevType; + +typedef struct XenQdiskVdev { + XenQdiskVdevType type; + unsigned long disk; + unsigned long partition; + unsigned long number; + bool valid; +} XenQdiskVdev; + +#define TYPE_XEN_QDISK_DEVICE "xen-qdisk" +#define XEN_QDISK_DEVICE(obj) \ + OBJECT_CHECK(XenQdiskDevice, (obj), TYPE_XEN_QDISK_DEVICE) + +typedef struct XenQdiskDevice XenQdiskDevice; + +struct XenQdiskDevice { + XenDevice xendev; + XenQdiskVdev vdev; +}; + +#endif /* HW_XEN_QDISK_H */ --=20 2.11.0