[libvirt] [PATCH 0/6] qemu: command: Replace vlan= with netdev=

Most VMs libvirt knows how to launch will have qemu network config like: -netdev $NETDEV_OPTS,id=netdev1 -device e1000,id=netdev1 However for machine types with built-in platform network devices, we currently do: -net nic,model=e1000,vlan=1 -net $NETDEV_OPTS,vlan=1 However for the platform case, all qemu versions we support can do: -netdev $NETDEV_OPTS,id=netdev1 -net nic,model=e1000,netdev=netdev1 Which simplifies our code, and is more future proof as qemu has deprecated the vlan= option. This series switches to the netdev= method, and performs some cleanups in related code. Thanks, Cole Cole Robinson (6): qemu: command: Make qemuBuildNicStr static qemu: command: Rename BuildNicStr to BuildLegacyNicStr qemu: command: remove unused LegacyNicStr arg 'prefix' qemu: command: replace vlan= with netdev= for legacy nic qemu: Remove vlan function arguments qemu: command: vhost: cleanup error reporting src/qemu/qemu_command.c | 99 ++++++------------- src/qemu/qemu_command.h | 8 -- src/qemu/qemu_hotplug.c | 3 +- .../arm-vexpressa9-basic.args | 4 +- 4 files changed, 33 insertions(+), 81 deletions(-) -- 2.17.1

It doesn't have any external callers Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 2 +- src/qemu/qemu_command.h | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 20c6ac2a04..4625851dc5 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3352,7 +3352,7 @@ qemuBuildMemoryDeviceStr(virDomainMemoryDefPtr mem) } -char * +static char * qemuBuildNicStr(virDomainNetDefPtr net, const char *prefix, int vlan) diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h index da75645ac5..0bcbf3018b 100644 --- a/src/qemu/qemu_command.h +++ b/src/qemu/qemu_command.h @@ -92,11 +92,6 @@ char *qemuBuildHostNetStr(virDomainNetDefPtr net, char **vhostfd, size_t vhostfdSize); -/* Legacy, pre device support */ -char *qemuBuildNicStr(virDomainNetDefPtr net, - const char *prefix, - int vlan); - /* Current, best practice */ char *qemuBuildNicDevStr(virDomainDefPtr def, virDomainNetDefPtr net, -- 2.17.1

On 06/19/2018 06:40 PM, Cole Robinson wrote:
It doesn't have any external callers
Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 2 +- src/qemu/qemu_command.h | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-)
Nit pick, I prefer s/qemu: command:/qemu_command/ in $SUBJ, but at the same time don't care that much. It's a matter of preference after all. Michal

Makes it less ambiguous Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 4625851dc5..f7038a8c5e 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3353,9 +3353,9 @@ qemuBuildMemoryDeviceStr(virDomainMemoryDefPtr mem) static char * -qemuBuildNicStr(virDomainNetDefPtr net, - const char *prefix, - int vlan) +qemuBuildLegacyNicStr(virDomainNetDefPtr net, + const char *prefix, + int vlan) { char *str; char macaddr[VIR_MAC_STRING_BUFLEN]; @@ -8517,7 +8517,7 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver, goto cleanup; virCommandAddArgList(cmd, "-device", nic, NULL); } else { - if (!(nic = qemuBuildNicStr(net, "nic,", vlan))) + if (!(nic = qemuBuildLegacyNicStr(net, "nic,", vlan))) goto cleanup; virCommandAddArgList(cmd, "-net", nic, NULL); -- 2.17.1

Hardcode the only string that's passed in Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index f7038a8c5e..31a0b7761a 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3354,15 +3354,13 @@ qemuBuildMemoryDeviceStr(virDomainMemoryDefPtr mem) static char * qemuBuildLegacyNicStr(virDomainNetDefPtr net, - const char *prefix, int vlan) { char *str; char macaddr[VIR_MAC_STRING_BUFLEN]; ignore_value(virAsprintf(&str, - "%smacaddr=%s,vlan=%d%s%s%s%s", - prefix ? prefix : "", + "nic,macaddr=%s,vlan=%d%s%s%s%s", virMacAddrFormat(&net->mac, macaddr), vlan, (net->model ? ",model=" : ""), @@ -8517,7 +8515,7 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver, goto cleanup; virCommandAddArgList(cmd, "-device", nic, NULL); } else { - if (!(nic = qemuBuildLegacyNicStr(net, "nic,", vlan))) + if (!(nic = qemuBuildLegacyNicStr(net, vlan))) goto cleanup; virCommandAddArgList(cmd, "-net", nic, NULL); -- 2.17.1

VMs with hardcoded platform network devices are forced to use old style '-net nic' command line config. Current we use qemu's vlan option to hook this with the '-netdev' host side of things. However since qemu 1.2 there is '-net nic,netdev=X' option for explicitly referencing a netdev ID, which is more inline with typical VM commandlines, so let's switch to that Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 52 ++++++------------- .../arm-vexpressa9-basic.args | 4 +- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 31a0b7761a..a2687c5693 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3354,15 +3354,15 @@ qemuBuildMemoryDeviceStr(virDomainMemoryDefPtr mem) static char * qemuBuildLegacyNicStr(virDomainNetDefPtr net, - int vlan) + int vlan ATTRIBUTE_UNUSED) { char *str; char macaddr[VIR_MAC_STRING_BUFLEN]; ignore_value(virAsprintf(&str, - "nic,macaddr=%s,vlan=%d%s%s%s%s", + "nic,macaddr=%s,netdev=host%s%s%s%s%s", virMacAddrFormat(&net->mac, macaddr), - vlan, + net->info.alias, (net->model ? ",model=" : ""), (net->model ? net->model : ""), (net->info.alias ? ",name=" : ""), @@ -3374,7 +3374,7 @@ qemuBuildLegacyNicStr(virDomainNetDefPtr net, char * qemuBuildNicDevStr(virDomainDefPtr def, virDomainNetDefPtr net, - int vlan, + int vlan ATTRIBUTE_UNUSED, unsigned int bootindex, size_t vhostfdSize, virQEMUCapsPtr qemuCaps) @@ -3523,10 +3523,7 @@ qemuBuildNicDevStr(virDomainDefPtr def, virBufferAsprintf(&buf, ",host_mtu=%u", net->mtu); } - if (vlan == -1) - virBufferAsprintf(&buf, ",netdev=host%s", net->info.alias); - else - virBufferAsprintf(&buf, ",vlan=%d", vlan); + virBufferAsprintf(&buf, ",netdev=host%s", net->info.alias); virBufferAsprintf(&buf, ",id=%s", net->info.alias); virBufferAsprintf(&buf, ",mac=%s", virMacAddrFormat(&net->mac, macaddr)); @@ -3555,7 +3552,7 @@ qemuBuildNicDevStr(virDomainDefPtr def, char * qemuBuildHostNetStr(virDomainNetDefPtr net, virQEMUDriverPtr driver, - int vlan, + int vlan ATTRIBUTE_UNUSED, char **tapfd, size_t tapfdSize, char **vhostfd, @@ -3670,13 +3667,7 @@ qemuBuildHostNetStr(virDomainNetDefPtr net, break; } - if (vlan >= 0) { - virBufferAsprintf(&buf, "vlan=%d,", vlan); - if (net->info.alias) - virBufferAsprintf(&buf, "name=host%s,", net->info.alias); - } else { - virBufferAsprintf(&buf, "id=host%s,", net->info.alias); - } + virBufferAsprintf(&buf, "id=host%s,", net->info.alias); if (is_tap) { if (vhostfdSize) { @@ -8494,22 +8485,20 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver, goto cleanup; } + if (!(host = qemuBuildHostNetStr(net, driver, + vlan, + tapfdName, tapfdSize, + vhostfdName, vhostfdSize))) + goto cleanup; + virCommandAddArgList(cmd, "-netdev", host, NULL); + /* Possible combinations: * - * 1. Old way: -net nic,model=e1000,vlan=1 -net tap,vlan=1 - * 2. New way: -netdev type=tap,id=netdev1 -device e1000,id=netdev1 - * - * NB: The backend and frontend are reversed above + * Old way: -netdev type=tap,id=netdev1 \ + * -net nic,model=e1000,netdev=netdev1 + * New way: -netdev type=tap,id=netdev1 -device e1000,id=netdev1 */ - if (qemuDomainSupportsNicdev(def, net)) { - if (!(host = qemuBuildHostNetStr(net, driver, - vlan, - tapfdName, tapfdSize, - vhostfdName, vhostfdSize))) - goto cleanup; - virCommandAddArgList(cmd, "-netdev", host, NULL); - if (!(nic = qemuBuildNicDevStr(def, net, vlan, bootindex, vhostfdSize, qemuCaps))) goto cleanup; @@ -8518,13 +8507,6 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver, if (!(nic = qemuBuildLegacyNicStr(net, vlan))) goto cleanup; virCommandAddArgList(cmd, "-net", nic, NULL); - - if (!(host = qemuBuildHostNetStr(net, driver, - vlan, - tapfdName, tapfdSize, - vhostfdName, vhostfdSize))) - goto cleanup; - virCommandAddArgList(cmd, "-net", host, NULL); } ret = 0; diff --git a/tests/qemuxml2argvdata/arm-vexpressa9-basic.args b/tests/qemuxml2argvdata/arm-vexpressa9-basic.args index d9689b8ea8..1821ed4d38 100644 --- a/tests/qemuxml2argvdata/arm-vexpressa9-basic.args +++ b/tests/qemuxml2argvdata/arm-vexpressa9-basic.args @@ -27,7 +27,7 @@ server,nowait \ -dtb /arm.dtb \ -usb \ -drive file=/arm.raw,format=raw,if=sd,index=0 \ --net nic,macaddr=52:54:00:09:a4:37,vlan=0,model=lan9118,name=net0 \ --net user,vlan=0,name=hostnet0 \ +-netdev user,id=hostnet0 \ +-net nic,macaddr=52:54:00:09:a4:37,netdev=hostnet0,model=lan9118,name=net0 \ -chardev pty,id=charserial0 \ -serial chardev:charserial0 -- 2.17.1

On 06/19/2018 06:40 PM, Cole Robinson wrote:
VMs with hardcoded platform network devices are forced to use old style '-net nic' command line config. Current we use qemu's vlan
s/Current/Currently/ perhaps?
option to hook this with the '-netdev' host side of things.
However since qemu 1.2 there is '-net nic,netdev=X' option for explicitly referencing a netdev ID, which is more inline with typical VM commandlines, so let's switch to that
Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 52 ++++++------------- .../arm-vexpressa9-basic.args | 4 +- 2 files changed, 19 insertions(+), 37 deletions(-)
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 31a0b7761a..a2687c5693 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3354,15 +3354,15 @@ qemuBuildMemoryDeviceStr(virDomainMemoryDefPtr mem)
static char * qemuBuildLegacyNicStr(virDomainNetDefPtr net, - int vlan) + int vlan ATTRIBUTE_UNUSED)
Might as well drop this. Looking into the future at 5/6 and you're doing just that. My suggestion is to merge 5/6 into this one, but whatever you prefer.
{ char *str; char macaddr[VIR_MAC_STRING_BUFLEN];
ignore_value(virAsprintf(&str, - "nic,macaddr=%s,vlan=%d%s%s%s%s", + "nic,macaddr=%s,netdev=host%s%s%s%s%s", virMacAddrFormat(&net->mac, macaddr), - vlan, + net->info.alias, (net->model ? ",model=" : ""), (net->model ? net->model : ""), (net->info.alias ? ",name=" : ""),
Michal

They are all unused now Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 23 +++++------------------ src/qemu/qemu_command.h | 3 --- src/qemu/qemu_hotplug.c | 3 +-- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index a2687c5693..f2dbb3fadd 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -3353,8 +3353,7 @@ qemuBuildMemoryDeviceStr(virDomainMemoryDefPtr mem) static char * -qemuBuildLegacyNicStr(virDomainNetDefPtr net, - int vlan ATTRIBUTE_UNUSED) +qemuBuildLegacyNicStr(virDomainNetDefPtr net) { char *str; char macaddr[VIR_MAC_STRING_BUFLEN]; @@ -3374,7 +3373,6 @@ qemuBuildLegacyNicStr(virDomainNetDefPtr net, char * qemuBuildNicDevStr(virDomainDefPtr def, virDomainNetDefPtr net, - int vlan ATTRIBUTE_UNUSED, unsigned int bootindex, size_t vhostfdSize, virQEMUCapsPtr qemuCaps) @@ -3552,7 +3550,6 @@ qemuBuildNicDevStr(virDomainDefPtr def, char * qemuBuildHostNetStr(virDomainNetDefPtr net, virQEMUDriverPtr driver, - int vlan ATTRIBUTE_UNUSED, char **tapfd, size_t tapfdSize, char **vhostfd, @@ -8194,7 +8191,6 @@ qemuBuildVhostuserCommandLine(virQEMUDriverPtr driver, } if (!(netdev = qemuBuildHostNetStr(net, driver, - -1, NULL, 0, NULL, 0))) goto error; @@ -8210,7 +8206,7 @@ qemuBuildVhostuserCommandLine(virQEMUDriverPtr driver, virCommandAddArg(cmd, netdev); VIR_FREE(netdev); - if (!(nic = qemuBuildNicDevStr(def, net, -1, bootindex, + if (!(nic = qemuBuildNicDevStr(def, net, bootindex, queues, qemuCaps))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Error generating NIC -device string")); @@ -8239,7 +8235,6 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver, virDomainDefPtr def, virDomainNetDefPtr net, virQEMUCapsPtr qemuCaps, - int vlan, unsigned int bootindex, virNetDevVPortProfileOp vmop, bool standalone, @@ -8486,7 +8481,6 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver, } if (!(host = qemuBuildHostNetStr(net, driver, - vlan, tapfdName, tapfdSize, vhostfdName, vhostfdSize))) goto cleanup; @@ -8499,12 +8493,12 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver, * New way: -netdev type=tap,id=netdev1 -device e1000,id=netdev1 */ if (qemuDomainSupportsNicdev(def, net)) { - if (!(nic = qemuBuildNicDevStr(def, net, vlan, bootindex, + if (!(nic = qemuBuildNicDevStr(def, net, bootindex, vhostfdSize, qemuCaps))) goto cleanup; virCommandAddArgList(cmd, "-device", nic, NULL); } else { - if (!(nic = qemuBuildLegacyNicStr(net, vlan))) + if (!(nic = qemuBuildLegacyNicStr(net))) goto cleanup; virCommandAddArgList(cmd, "-net", nic, NULL); } @@ -8577,16 +8571,9 @@ qemuBuildNetCommandLine(virQEMUDriverPtr driver, for (i = 0; i < def->nnets; i++) { virDomainNetDefPtr net = def->nets[i]; - int vlan; - - /* VLANs are not used with -netdev and -device, so don't record them */ - if (qemuDomainSupportsNicdev(def, net)) - vlan = -1; - else - vlan = i; if (qemuBuildInterfaceCommandLine(driver, logManager, cmd, def, net, - qemuCaps, vlan, bootNet, vmop, + qemuCaps, bootNet, vmop, standalone, nnicindexes, nicindexes, chardevStdioLogd) < 0) diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h index 0bcbf3018b..c78282eb09 100644 --- a/src/qemu/qemu_command.h +++ b/src/qemu/qemu_command.h @@ -83,10 +83,8 @@ qemuBuildChrDeviceStr(char **deviceStr, virDomainChrDefPtr chr, virQEMUCapsPtr qemuCaps); -/* With vlan == -1, use netdev syntax, else old hostnet */ char *qemuBuildHostNetStr(virDomainNetDefPtr net, virQEMUDriverPtr driver, - int vlan, char **tapfd, size_t tapfdSize, char **vhostfd, @@ -95,7 +93,6 @@ char *qemuBuildHostNetStr(virDomainNetDefPtr net, /* Current, best practice */ char *qemuBuildNicDevStr(virDomainDefPtr def, virDomainNetDefPtr net, - int vlan, unsigned int bootindex, size_t vhostfdSize, virQEMUCapsPtr qemuCaps); diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 7c0c366b99..7a1bbc7c8c 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -994,7 +994,6 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver, } if (!(netstr = qemuBuildHostNetStr(net, driver, - -1, tapfdName, tapfdSize, vhostfdName, vhostfdSize))) goto cleanup; @@ -1027,7 +1026,7 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver, for (i = 0; i < vhostfdSize; i++) VIR_FORCE_CLOSE(vhostfd[i]); - if (!(nicstr = qemuBuildNicDevStr(vm->def, net, -1, 0, + if (!(nicstr = qemuBuildNicDevStr(vm->def, net, 0, queueSize, priv->qemuCaps))) goto try_remove; -- 2.17.1

- Switch to cleanup: label and share free calls - Don't overwrite qemuBuildNicDevStr error Signed-off-by: Cole Robinson <crobinso@redhat.com> --- src/qemu/qemu_command.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index f2dbb3fadd..1ffcb5b1ae 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -8147,11 +8147,12 @@ qemuBuildVhostuserCommandLine(virQEMUDriverPtr driver, char *netdev = NULL; unsigned int queues = net->driver.virtio.queues; char *nic = NULL; + int ret = -1; if (!qemuDomainSupportsNicdev(def, net)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Nicdev support unavailable")); - goto error; + goto cleanup; } switch ((virDomainChrType)net->data.vhostuser->type) { @@ -8160,7 +8161,7 @@ qemuBuildVhostuserCommandLine(virQEMUDriverPtr driver, net->data.vhostuser, net->info.alias, qemuCaps, false, chardevStdioLogd))) - goto error; + goto cleanup; break; case VIR_DOMAIN_CHR_TYPE_NULL: @@ -8179,7 +8180,7 @@ qemuBuildVhostuserCommandLine(virQEMUDriverPtr driver, virReportError(VIR_ERR_INTERNAL_ERROR, _("vhost-user type '%s' not supported"), virDomainChrTypeToString(net->data.vhostuser->type)); - goto error; + goto cleanup; } if (queues > 1 && @@ -8187,45 +8188,38 @@ qemuBuildVhostuserCommandLine(virQEMUDriverPtr driver, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("multi-queue is not supported for vhost-user " "with this QEMU binary")); - goto error; + goto cleanup; } if (!(netdev = qemuBuildHostNetStr(net, driver, NULL, 0, NULL, 0))) - goto error; + goto cleanup; if (virNetDevOpenvswitchGetVhostuserIfname(net->data.vhostuser->data.nix.path, &net->ifname) < 0) - goto error; + goto cleanup; virCommandAddArg(cmd, "-chardev"); virCommandAddArg(cmd, chardev); - VIR_FREE(chardev); virCommandAddArg(cmd, "-netdev"); virCommandAddArg(cmd, netdev); - VIR_FREE(netdev); if (!(nic = qemuBuildNicDevStr(def, net, bootindex, queues, qemuCaps))) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("Error generating NIC -device string")); - goto error; + goto cleanup; } virCommandAddArgList(cmd, "-device", nic, NULL); - VIR_FREE(nic); - virObjectUnref(cfg); - return 0; - - error: + ret = 0; + cleanup: virObjectUnref(cfg); VIR_FREE(netdev); VIR_FREE(chardev); VIR_FREE(nic); - return -1; + return ret; } static int -- 2.17.1

On 06/19/2018 06:40 PM, Cole Robinson wrote:
Most VMs libvirt knows how to launch will have qemu network config like:
-netdev $NETDEV_OPTS,id=netdev1 -device e1000,id=netdev1
However for machine types with built-in platform network devices, we currently do:
-net nic,model=e1000,vlan=1 -net $NETDEV_OPTS,vlan=1
However for the platform case, all qemu versions we support can do:
-netdev $NETDEV_OPTS,id=netdev1 -net nic,model=e1000,netdev=netdev1
Which simplifies our code, and is more future proof as qemu has deprecated the vlan= option. This series switches to the netdev= method, and performs some cleanups in related code.
Thanks, Cole
Cole Robinson (6): qemu: command: Make qemuBuildNicStr static qemu: command: Rename BuildNicStr to BuildLegacyNicStr qemu: command: remove unused LegacyNicStr arg 'prefix' qemu: command: replace vlan= with netdev= for legacy nic qemu: Remove vlan function arguments qemu: command: vhost: cleanup error reporting
src/qemu/qemu_command.c | 99 ++++++------------- src/qemu/qemu_command.h | 8 -- src/qemu/qemu_hotplug.c | 3 +- .../arm-vexpressa9-basic.args | 4 +- 4 files changed, 33 insertions(+), 81 deletions(-)
ACK series. Michal
participants (2)
-
Cole Robinson
-
Michal Privoznik