From bc130288a30891729196d08f6fab47c4786c5b60 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 26 Mar 2021 15:43:14 +0100 Subject: [PATCH] qemu: command: Introduce JSON equivalent of qemuBuildDeviceAddressStr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Upcoming patches will start converting the formatting of arguments for -device from a string to JSON so that we can keep proper types around when using it via QMP. This means we will need an equivalet for the device address builder function. 'qemuBuildDeviceAddressProps' provides equal functionality, but the output differs for fields where a number is expected, where we've previously formatted a hex value but now end up with a decimal value per JSON standard. For given address types I've selected an example device and used '-device $DEV,help' to obtain the current types recognized by qemu: Note that 'bus' is not shown below, but it's already a string so we can keep using it as a string. VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI (virtio-balloon-pci) acpi-index= - (default: 0) addr= - Slot and optional function number, example: 06.0 or 06 (default: -1) multifunction= - on/off (default: false) Note that 'addr' is here defined as 'int32' but in fact internally in qemu is an alternate type between a number and a string so we can keep using strings here. VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB (usb-tablet) port= VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO (spapr-vty) reg= - (default: 4294967295) VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW (virtio-blk-cww) devno= - Identifier of an I/O device in the channel subsystem, example: fe.1.23ab VIR_DOMAIN_DEVICE_ADDRESS_TYPE_ISA (isa-serial) iobase= - (default: 4294967295) irq= - (default: 4294967295) VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DIMM (pc-dimm) slot= - (default: -1) addr= - (default: 0) Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/qemu/qemu_command.c | 108 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 4f4b24aa66..0c2cf8f1ca 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -416,7 +416,7 @@ qemuBuildDeviceAddressPCIStr(virBuffer *buf, } -static int +static int G_GNUC_UNUSED qemuBuildDeviceAddressStr(virBuffer *buf, const virDomainDef *domainDef, virDomainDeviceInfo *info) @@ -484,6 +484,112 @@ qemuBuildDeviceAddressStr(virBuffer *buf, } +static int G_GNUC_UNUSED +qemuBuildDeviceAddressProps(virJSONValue *props, + const virDomainDef *domainDef, + virDomainDeviceInfo *info) +{ + switch ((virDomainDeviceAddressType) info->type) { + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI: { + g_autofree char *pciaddr = NULL; + g_autofree char *bus = qemuBuildDeviceAddressPCIGetBus(domainDef, info); + + if (!bus) + return -1; + + if (info->addr.pci.function != 0) + pciaddr = g_strdup_printf("0x%x.0x%x", info->addr.pci.slot, info->addr.pci.function); + else + pciaddr = g_strdup_printf("0x%x", info->addr.pci.slot); + + if (virJSONValueObjectAdd(props, + "s:bus", bus, + "T:multifunction", info->addr.pci.multi, + "s:addr", pciaddr, + "p:acpi-index", info->acpiIndex, + NULL) < 0) + return -1; + + return 0; + } + break; + + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB: { + const char *contAlias = NULL; + g_auto(virBuffer) port = VIR_BUFFER_INITIALIZER; + g_autofree char *bus = NULL; + + if (!(contAlias = virDomainControllerAliasFind(domainDef, + VIR_DOMAIN_CONTROLLER_TYPE_USB, + info->addr.usb.bus))) + return -1; + + bus = g_strdup_printf("%s.0", contAlias); + + virDomainUSBAddressPortFormatBuf(&port, info->addr.usb.port); + + if (virJSONValueObjectAdd(props, + "s:bus", bus, + "S:port", virBufferCurrentContent(&port), + NULL) < 0) + return -1; + + return 0; + } + + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO: + if (info->addr.spaprvio.has_reg) { + if (virJSONValueObjectAdd(props, + "P:reg", info->addr.spaprvio.reg, + NULL) < 0) + return -1; + } + return 0; + + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW: { + g_autofree char *devno = g_strdup_printf("%x.%x.%04x", + info->addr.ccw.cssid, + info->addr.ccw.ssid, + info->addr.ccw.devno); + + if (virJSONValueObjectAdd(props, "s:devno", devno, NULL) < 0) + return -1; + + return 0; + } + + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_ISA: + if (virJSONValueObjectAdd(props, + "u:iobase", info->addr.isa.iobase, + "p:irq", info->addr.isa.irq, + NULL) < 0) + return -1; + + return 0; + + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DIMM: + if (virJSONValueObjectAdd(props, + "u:slot", info->addr.dimm.slot, + "P:addr", info->addr.dimm.base, + NULL) < 0) + return -1; + + return 0; + + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE: + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE: + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_SERIAL: + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCID: + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390: + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO: + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_UNASSIGNED: + case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_LAST: + default: + return 0; + } +} + + /** * qemuBuildVirtioDevStr * @buf: virBuffer * to append the built string