Skip to contentSkip to conversation

vl: transform QemuOpts device to JSON syntax device

PATCH In review Zhenzhong Duan 4 participants 7 messages Feb 24, 2022 – Feb 25, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vl: transform QemuOpts device to JSON syntax device

While there are mixed use of traditional -device option and JSON syntax option, QEMU reports conflict, e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device '{"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}' \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

It breaks with:

qemu-kvm: -device {"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}: PCI: slot 2 function 0 not available for virtio-scsi-pci, in use by virtio-scsi-pci

But if we reformat first -device same as the second, so only same kind of option for all the devices, it succeeds, vice versa. e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device virtio-scsi-pci,id=scsi0,bus=pci.0,addr=02.0 \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

Succeed!

Because both kind of options are inserted into their own list and break the order in QEMU command line during BDF auto assign. Fix it by transform QemuOpts into JSON syntax and insert in JSON device list, so the order in QEMU command line kept.

---
 softmmu/vl.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
softmmu/vl.c +14 -9
index 1fe028800fdf..3def40b5405e 100644
@@ -3394,21 +3394,26 @@ void qemu_init(int argc, char **argv, char **envp)
                 qdict_put_str(machine_opts_dict, "usb", "on");
                 add_device_config(DEV_USB, optarg);
                 break;
-            case QEMU_OPTION_device:
+            case QEMU_OPTION_device: {
+                QObject *obj;
                 if (optarg[0] == '{') {
-                    QObject *obj = qobject_from_json(optarg, &error_fatal);
-                    DeviceOption *opt = g_new0(DeviceOption, 1);
-                    opt->opts = qobject_to(QDict, obj);
-                    loc_save(&opt->loc);
-                    assert(opt->opts != NULL);
-                    QTAILQ_INSERT_TAIL(&device_opts, opt, next);
+                    obj = qobject_from_json(optarg, &error_fatal);
                 } else {
-                    if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
-                                                 optarg, true)) {
+                    opts = qemu_opts_parse_noisily(qemu_find_opts("device"),
+                                                   optarg, true);
+                    if (!opts) {
                         exit(1);
                     }
+                    obj = QOBJECT(qemu_opts_to_qdict(opts, NULL));
+                    qemu_opts_del(opts);
                 }
+                DeviceOption *opt = g_new0(DeviceOption, 1);
+                opt->opts = qobject_to(QDict, obj);
+                loc_save(&opt->loc);
+                assert(opt->opts != NULL);
+                QTAILQ_INSERT_TAIL(&device_opts, opt, next);
                 break;
+            }
             case QEMU_OPTION_smp:
                 machine_parse_property_opt(qemu_find_opts("smp-opts"),
                                            "smp", optarg);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Zhenzhong Duan

While there are mixed use of traditional -device option and JSON syntax option, QEMU reports conflict, e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device '{"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}' \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

Why are you attempting to mix JSON and non-JSON syntax at the same time ? The expectation is that any mgmt app adopting JSON syntax will do so universally and not mix old and new syntax. So in practice the scenario above is not one that QEMU ever intended to have used by apps.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw
Quoted 8 lines · Daniel P. Berrangé
Quoted 6 lines · Zhenzhong Duan

While there are mixed use of traditional -device option and JSON syntax option, QEMU reports conflict, e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device '{"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}' \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

Quoted 5 lines

Why are you attempting to mix JSON and non-JSON syntax at the same time ? The expectation is that any mgmt app adopting JSON syntax will do so universally and not mix old and new syntax. So in practice the scenario above is not one that QEMU ever intended to have used by apps.

Based on the previous post they are using some 'qemu:commandline' overrides with the legacy syntax with new libvirt which uses JSON syntax:

<qemu:commandline>
<qemu:arg value='-netdev'/>

    <qemu:arg value='user,id=mynet0,hostfwd=tcp::44483-:22'/>
    <qemu:arg value='-device'/>

<qemu:arg value='virtio-net-pci,netdev=mynet0,mac=00:16:3E:68:00:10,romfile='/>
</qemu:commandline>

I suggested that they should add the required functionality to libvirt instead of trying to hack qemu:

https://lists.gnu.org/archive/html/qemu-devel/2022-02/msg05068.html

#3 lore raw
Quoted 23 lines · Peter Krempa
Quoted 8 lines · Daniel P. Berrangé
Quoted 6 lines · Zhenzhong Duan

While there are mixed use of traditional -device option and JSON syntax option, QEMU reports conflict, e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device '{"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}' \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

Quoted 5 lines

Why are you attempting to mix JSON and non-JSON syntax at the same time ? The expectation is that any mgmt app adopting JSON syntax will do so universally and not mix old and new syntax. So in practice the scenario above is not one that QEMU ever intended to have used by apps.

Based on the previous post they are using some 'qemu:commandline' overrides with the legacy syntax with new libvirt which uses JSON syntax:

<qemu:commandline>
<qemu:arg value='-netdev'/>

    <qemu:arg value='user,id=mynet0,hostfwd=tcp::44483-:22'/>
    <qemu:arg value='-device'/>
Quoted 7 lines

<qemu:arg value='virtio-net-pci,netdev=mynet0,mac=00:16:3E:68:00:10,romfile='/>
</qemu:commandline>

I suggested that they should add the required functionality to libvirt instead of trying to hack qemu:

https://lists.gnu.org/archive/html/qemu-devel/2022-02/msg05068.html

Or the other answer is to switch to use JSON syntax in the above command line passthrough config.

Or to specify the PCI address so it doesn't clash with other devices already present.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#4 lore raw
Quoted 43 lines

-----Original Message-----
From: Daniel P. Berrangé <[email protected]>
Sent: Thursday, February 24, 2022 5:14 PM
To: Peter Krempa <[email protected]>
Cc: Duan, Zhenzhong <[email protected]>; qemu- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] vl: transform QemuOpts device to JSON syntax device

Quoted 7 lines · Peter Krempa
Quoted 6 lines · Daniel P. Berrangé
Quoted 5 lines · Zhenzhong Duan

While there are mixed use of traditional -device option and JSON syntax option, QEMU reports conflict, e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device '{"driver":"virtio-scsi-

pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}' \

Quoted 23 lines
Quoted 7 lines
Quoted 1 line

-device virtio-scsi-pci,id=scsi1,bus=pci.0

Why are you attempting to mix JSON and non-JSON syntax at the same time ? The expectation is that any mgmt app adopting JSON syntax will do so universally and not mix old and new syntax. So in practice the scenario above is not one that QEMU ever intended to have used by apps.

Based on the previous post they are using some 'qemu:commandline' overrides with the legacy syntax with new libvirt which uses JSON syntax:

<qemu:commandline>
<qemu:arg value='-netdev'/>

    <qemu:arg value='user,id=mynet0,hostfwd=tcp::44483-:22'/>
    <qemu:arg value='-device'/>

<qemu:arg
value='virtio-net-pci,netdev=mynet0,mac=00:16:3E:68:00:10,romfile='/> </qemu:commandline>

I suggested that they should add the required functionality to libvirt instead of trying to hack qemu:

Quoted 7 lines
Quoted 1 line

https://lists.gnu.org/archive/html/qemu-devel/2022-02/msg05068.html

Or the other answer is to switch to use JSON syntax in the above command line passthrough config.

Or to specify the PCI address so it doesn't clash with other devices already present.

Thanks Daniel and Peter for comments.
Clear on my side. I'll synchronize your suggestions to my colleague.

Regards
Zhenzhong

#5 lore raw
In reply to Zhenzhong Duan

Am 24.02.2022 um 07:06 hat Zhenzhong Duan geschrieben:

Quoted 18 lines

While there are mixed use of traditional -device option and JSON syntax option, QEMU reports conflict, e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device '{"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}' \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

It breaks with:

qemu-kvm: -device {"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}: PCI: slot 2 function 0 not available for virtio-scsi-pci, in use by virtio-scsi-pci

But if we reformat first -device same as the second, so only same kind of option for all the devices, it succeeds, vice versa. e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device virtio-scsi-pci,id=scsi0,bus=pci.0,addr=02.0 \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

Quoted 8 lines

Succeed!

Because both kind of options are inserted into their own list and break the order in QEMU command line during BDF auto assign. Fix it by transform QemuOpts into JSON syntax and insert in JSON device list, so the order in QEMU command line kept.

This patch is incorrect and breaks several cases, which are the reason why the QemuOpts path hasn't been changed yet.

For example, after this patch, help doesn't work any more:

$ build/qemu-system-x86_64 -device help
qemu-system-x86_64: -device help: 'help' is not a valid device model name

Any non-string property doesn't work any more in non-JSON syntax:

$ $ build/qemu-system-x86_64 -blockdev null-co,node-name=disk -device virtio-blk,drive=disk,physical_block_size=4096
qemu-system-x86_64: -device virtio-blk,drive=disk,physical_block_size=4096: Parameter 'physical_block_size' expects uint64

There may be more cases that are broken with this patch.

Kevin

#6 lore raw
Quoted 49 lines

-----Original Message-----
From: Kevin Wolf <[email protected]>
Sent: Thursday, February 24, 2022 7:31 PM
To: Duan, Zhenzhong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] vl: transform QemuOpts device to JSON syntax device

Am 24.02.2022 um 07:06 hat Zhenzhong Duan geschrieben:

Quoted 5 lines

While there are mixed use of traditional -device option and JSON syntax option, QEMU reports conflict, e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device '{"driver":"virtio-scsi-

pci","id":"scsi0","bus":"pci.0","addr":"0x02.0"}' \

Quoted 16 lines

-device virtio-scsi-pci,id=scsi1,bus=pci.0

It breaks with:

qemu-kvm: -device {"driver":"virtio-scsi-pci","id":"scsi0","bus":"pci.0","addr":"0x02.0" }: PCI: slot 2 function 0 not available for virtio-scsi-pci, in use by virtio-scsi-pci

But if we reformat first -device same as the second, so only same kind of option for all the devices, it succeeds, vice versa. e.x:

/usr/libexec/qemu-kvm -nodefaults \
-device virtio-scsi-pci,id=scsi0,bus=pci.0,addr=02.0 \
-device virtio-scsi-pci,id=scsi1,bus=pci.0

Quoted 8 lines

Succeed!

Because both kind of options are inserted into their own list and break the order in QEMU command line during BDF auto assign. Fix it by transform QemuOpts into JSON syntax and insert in JSON device list, so the order in QEMU command line kept.

This patch is incorrect and breaks several cases, which are the reason why the QemuOpts path hasn't been changed yet.

For example, after this patch, help doesn't work any more:

$ build/qemu-system-x86_64 -device help
qemu-system-x86_64: -device help: 'help' is not a valid device model name
Quoted 8 lines

Any non-string property doesn't work any more in non-JSON syntax:

$ $ build/qemu-system-x86_64 -blockdev null-co,node-name=disk -device
virtio-blk,drive=disk,physical_block_size=4096
qemu-system-x86_64: -device virtio-blk,drive=disk,physical_block_size=4096:
Parameter 'physical_block_size' expects uint64

There may be more cases that are broken with this patch.

Ah, yes. Thanks for point out. I should have learned more before writing this patch. Sorry for the noise. And we will try the libvirt maintainer suggested way on this issue.

Regards
Zhenzhong