Re: [libvirt] [PATCH 1/1] Add shrink flag to blockresize command

2019-10-15 Thread Han Han
On Tue, Oct 15, 2019 at 10:46 PM Cole Robinson  wrote:

> On 10/5/19 2:56 PM, martinsson.pat...@gmail.com wrote:
> > From: patchon 
> >
>
> You probably want to fix this to match your signed-off-by name.
>
> > These commits simply adds the '--shrink' flag to the blockresize
>
I agree that an options should be added here to avoid undesirable disk
shrinking.
Because I have broken guest OS image for several times due to the shrinking
of
blockresize.

> > command to prevent accidental shrinking of a block device. This
> > behaviour is already present on the vol-resize command and it
> > makes sense to mimic that behaviour.
> >
>
> I don't have an opinion on whether the feature should be added at the
> API level, I will leave that to others. CCing pkrempa
>
> But as implemented it seems problematic to add a flag that changes the
> semantics of the API, essentially requiring a new flag to get the old
> behavior. I see the argument that this is a data safety issue, but maybe
> apps are depending on this already? I don't know enough about the usage
> to guess either way
>
> I disagree the check of disk shrink is added to API level because it
changes
the behavior of blockresize API and may cause unexpected error in uplayer
products.

It is better to add the check to codes of **virsh** to avoid undesirable
shrinking
in cmdline user. And we can use '--force' to make blockresize always.
The procedure will be:

if (new size < capacity && no --force)
print error("Can't shrink capacity below current unless --force
specified")
else
call blockresize

And also we can add comments to blockresize API to avoid uplayer use
undesirable disk shrinking.

> > Only implemented in the qemu-driver atm.
> >
> > Signed-off-by: Patrik Martinsson 
> > ---
> >  src/qemu/qemu_driver.c | 15 ++-
> >  tools/virsh-domain.c   |  7 +++
> >  tools/virsh.pod|  9 -
> >  3 files changed, 29 insertions(+), 2 deletions(-)
> >
> > diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
> > index 95fe844c34..4e34eec796 100644
> > --- a/src/qemu/qemu_driver.c
> > +++ b/src/qemu/qemu_driver.c
> > @@ -11265,8 +11265,10 @@ qemuDomainBlockResize(virDomainPtr dom,
> >  char *device = NULL;
> >  const char *nodename = NULL;
> >  virDomainDiskDefPtr disk = NULL;
> > +virDomainBlockInfo info;
> >
> > -virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES, -1);
> > +virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES |
> > +  VIR_STORAGE_VOL_RESIZE_SHRINK, -1);
> >
>
> We shouldn't mix and match flag prefix names. Each public API should be
> coupled with its own set of flag names. So you would want to call this
> VIR_DOMAIN_BLOCK_RESIZE_SHRINK, and add it to
> include/libvirt/libvirt-domain.h and add it to documentation in
> libvirt-domain.c
>
> - Cole
>
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
>


-- 
Best regards,
---
Han Han
Quality Engineer
Redhat.

Email: h...@redhat.com
Phone: +861065339333
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH v3] util: Block SIGPIPE until execve in child process

2019-10-15 Thread Eric Blake

On 10/15/19 9:43 PM, Eric Blake wrote:

On 10/15/19 9:25 PM, Wang Yechao wrote:

Libvirtd has set SIGPIPE to ignored, and virFork resets all signal
handlers to the defaults. But child process may write logs to
stderr/stdout, that may generate SIGPIPE if journald has stopped.

So block SIGPIPE in virFork, and unblock it before execve.


How does that help?  If writing to stderr hits EOF, it will queue up a 
SIGPIPE to be delivered as soon as you unblock SIGPIPE.


The correct fix is to not unblock SIGPIPE until after any point at which 
you would be performing I/O that must not fail due to SIGPIPE, rather 
than messing with signal masks to just delay when SIGPIPE is delivered.


Let's word this better:

The correct fix is to keep SIGPIPE ignored until after any point at 
which you would be performing I/O that must not cause SIGPIPE, reverting 
SIGPIPE back to SIG_DFL at the last possible moment.  Messing with 
signal masks merely delays when SIGPIPE is delivered, rather than 
avoiding it.


--
Eric Blake, Principal Software Engineer
Red Hat, Inc.   +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH 2/3] conf/network_conf: use virStringParseYesNo helper

2019-10-15 Thread Mao Zhongyi
A function virStringParseYesNo was added to convert
string 'yes' to true and 'no' to false, so use this
helper to replace 'STREQ(.*, \"yes\")' and
'STREQ(.*, \"no\")' as it allows us to drop several
repetitive if-then-else string->bool conversion blocks.

Cc: g...@czarc.net
Cc: crobi...@redhat.com
Cc: berra...@redhat.com
Cc: g.sho1...@gmail.com

Signed-off-by: Mao Zhongyi 
Signed-off-by: Zhang Shengju 
---
 src/conf/network_conf.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 75ec5ccc27..9954c3d25f 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1682,9 +1682,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
  */
 ipv6nogwStr = virXPathString("string(./@ipv6)", ctxt);
 if (ipv6nogwStr) {
-if (STREQ(ipv6nogwStr, "yes")) {
-def->ipv6nogw = true;
-} else if (STRNEQ(ipv6nogwStr, "no")) {
+if (virStringParseYesNo(ipv6nogwStr, >ipv6nogw) < 0) {
 virReportError(VIR_ERR_XML_ERROR,
_("Invalid ipv6 setting '%s' in network '%s'"),
ipv6nogwStr, def->name);
-- 
2.17.1



--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 0/3] use virStringParseYesNo helper

2019-10-15 Thread Mao Zhongyi
A function virStringParseYesNo was added to convert
string 'yes' to true and 'no' to false, so use this
helper to replace 'STREQ(.*, \"yes\")' and
'STREQ(.*, \"no\")' as it allows us to drop several
repetitive if-then-else string->bool conversion blocks.

Cc: abolo...@redhat.com
Cc: berra...@redhat.com
Cc: crobi...@redhat.com
Cc: g...@czarc.net
Cc: g.sho1...@gmail.com
Cc: jdene...@redhat.com
Cc: la...@laine.org
Cc: mklet...@redhat.com
Cc: phrd...@redhat.com

Mao Zhongyi (3):
  conf/domain_conf: use virStringParseYesNo helper
  conf/network_conf: use virStringParseYesNo helper
  qemu/qemu_migration_params: use virStringParseYesNo helper

 src/conf/domain_conf.c   | 30 ++
 src/conf/network_conf.c  |  4 +---
 src/qemu/qemu_migration_params.c |  6 +-
 3 files changed, 16 insertions(+), 24 deletions(-)

-- 
2.17.1



--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 1/3] conf/domain_conf: use virStringParseYesNo helper

2019-10-15 Thread Mao Zhongyi
This helper performs a conversion from a "yes|no" string
to a corresponding boolean, and several conversions were
already done, but there are still some omissions.

For most of the remaining usages in domain_conf.c only
"yes" is explicitly checked for. This means all other
values are implicitly handled as 'false'. In this case,
use virStringParseYesNo, but if it returns -1, don't
raise an error but set the bool value to false.

Cc: crobi...@redhat.com
Cc: berra...@redhat.com
Cc: abolo...@redhat.com
Cc: la...@laine.org
Cc: phrd...@redhat.com
Cc: mklet...@redhat.com
Cc: g.sho1...@gmail.com

Signed-off-by: Mao Zhongyi 
Signed-off-by: Zhang Shengju 
---
 src/conf/domain_conf.c | 30 ++
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 10d6bf0eea..7420658726 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7601,8 +7601,8 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr node,
 }
 
 if ((autoAddress = virXMLPropString(node, "autoAddress"))) {
-if (STREQ(autoAddress, "yes"))
-usbsrc->autoAddress = true;
+if (virStringParseYesNo(autoAddress, >autoAddress) < 0)
+usbsrc->autoAddress = false;
 }
 
 /* Product can validly be 0, so we need some extra help to determine
@@ -8168,8 +8168,8 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
  * (e.g. ) with type='hostdev')
  */
 if ((managed = virXMLPropString(node, "managed")) != NULL) {
-if (STREQ(managed, "yes"))
-def->managed = true;
+if (virStringParseYesNo(managed, >managed) < 0)
+def->managed = false;
 }
 
 sgio = virXMLPropString(node, "sgio");
@@ -13807,9 +13807,7 @@ 
virDomainGraphicsListenDefParseXML(virDomainGraphicsListenDefPtr def,
 
 if (autoGenerated &&
 flags & VIR_DOMAIN_DEF_PARSE_STATUS) {
-if (STREQ(autoGenerated, "yes")) {
-def->autoGenerated = true;
-} else if (STRNEQ(autoGenerated, "no")) {
+if (virStringParseYesNo(autoGenerated, >autoGenerated) < 0) {
 virReportError(VIR_ERR_XML_ERROR,
_("Invalid autoGenerated value: %s"),
autoGenerated);
@@ -13939,12 +13937,11 @@ 
virDomainGraphicsDefParseXMLVNC(virDomainGraphicsDefPtr def,
 }
 
 if (autoport) {
-if (STREQ(autoport, "yes")) {
-if (flags & VIR_DOMAIN_DEF_PARSE_INACTIVE)
-def->data.vnc.port = 0;
-def->data.vnc.autoport = true;
-} else {
+if (virStringParseYesNo(autoport, >data.vnc.autoport) < 0) {
 def->data.vnc.autoport = false;
+} else {
+if (def->data.vnc.autoport && flags & 
VIR_DOMAIN_DEF_PARSE_INACTIVE)
+def->data.vnc.port = 0;
 }
 }
 
@@ -13958,8 +13955,9 @@ virDomainGraphicsDefParseXMLVNC(virDomainGraphicsDefPtr 
def,
 }
 }
 
-if (websocketGenerated && STREQ(websocketGenerated, "yes"))
-def->data.vnc.websocketGenerated = true;
+if (websocketGenerated &&
+virStringParseYesNo(websocketGenerated, 
>data.vnc.websocketGenerated))
+def->data.vnc.websocketGenerated = false;
 
 if (sharePolicy) {
 int policy =
@@ -15479,8 +15477,8 @@ virDomainVideoDefParseXML(virDomainXMLOptionPtr xmlopt,
 heads = virXMLPropString(cur, "heads");
 
 if ((primary = virXMLPropString(cur, "primary")) != NULL) {
-if (STREQ(primary, "yes"))
-def->primary = true;
+if (virStringParseYesNo(primary, >primary) < 0)
+def->primary = false;
 VIR_FREE(primary);
 }
 
-- 
2.17.1



--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 3/3] qemu/qemu_migration_params: use virStringParseYesNo helper

2019-10-15 Thread Mao Zhongyi
A function virStringParseYesNo was added to convert
string 'yes' to true and 'no' to false, so use this
helper to replace 'STREQ(.*, \"yes\")' and
'STREQ(.*, \"no\")' as it allows us to drop several
repetitive if-then-else string->bool conversion blocks.

Cc: jdene...@redhat.com
Cc: g.sho1...@gmail.com
Cc: crobi...@redhat.com

Signed-off-by: Mao Zhongyi 
Signed-off-by: Zhang Shengju 
---
 src/qemu/qemu_migration_params.c | 6 +-
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_params.c
index 85fa8f8de5..acd42042fe 100644
--- a/src/qemu/qemu_migration_params.c
+++ b/src/qemu/qemu_migration_params.c
@@ -1326,11 +1326,7 @@ qemuMigrationParamsParse(xmlXPathContextPtr ctxt,
 break;
 
 case QEMU_MIGRATION_PARAM_TYPE_BOOL:
-if (STREQ(value, "yes"))
-pv->value.b = true;
-else if (STREQ(value, "no"))
-pv->value.b = false;
-else
+if (virStringParseYesNo(value, >value.b) < 0)
 rc = -1;
 break;
 
-- 
2.17.1



--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3] util: Block SIGPIPE until execve in child process

2019-10-15 Thread Eric Blake

On 10/15/19 9:25 PM, Wang Yechao wrote:

Libvirtd has set SIGPIPE to ignored, and virFork resets all signal
handlers to the defaults. But child process may write logs to
stderr/stdout, that may generate SIGPIPE if journald has stopped.

So block SIGPIPE in virFork, and unblock it before execve.


How does that help?  If writing to stderr hits EOF, it will queue up a 
SIGPIPE to be delivered as soon as you unblock SIGPIPE.


The correct fix is to not unblock SIGPIPE until after any point at which 
you would be performing I/O that must not fail due to SIGPIPE, rather 
than messing with signal masks to just delay when SIGPIPE is delivered.


--
Eric Blake, Principal Software Engineer
Red Hat, Inc.   +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 00/30] storagefile, security: qcow2 data_file support

2019-10-15 Thread Han Han
On Wed, Oct 16, 2019 at 1:04 AM Cole Robinson  wrote:

> On 10/15/19 3:56 AM, Han Han wrote:
> > Hello Cole, one issue is found:
> > The qcow2 data file XTTRs is not cleaned on external snapshot when
> > -blockdev is not enabled
> >
> > Versions:
> > libvirt v5.8.0-134-g9d03e9adf1
> > qemu-kvm-4.1.0-13.module+el8.1.0+4313+ef76ec61.x86_64
> >
> > Steps:
> > 1. Convert a OS image to qcow2 data file:
> > # qemu-img convert -O qcow2 -o
> > data_file=/var/lib/libvirt/images/pc-data.raw,data_file_raw=on
> > /var/lib/libvirt/images/pc.qcow2 /var/lib/libvirt/images/pc-data.qcow2
> >
> > 2. Build and start libvirt source, start libvirt daemon:
> > # make clean && CC=/usr/lib64/ccache/cc ./autogen.sh&&./configure
> > --without-libssh --build=x86_64-redhat-linux-gnu
> > --host=x86_64-redhat-linux-gnu --program-prefix=
> > --disable-dependency-tracking --prefix=/usr --exec-prefix=/usr
> > --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc
> > --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64
> > --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/var/lib
> > --mandir=/usr/share/man --infodir=/usr/share/info --with-qemu
> > --without-openvz --without-lxc --without-vbox --without-libxl
> > --with-sasl --with-polkit --with-libvirtd --without-phyp --with-esx
> > --without-hyperv --without-vmware --without-xenapi --without-vz
> > --without-bhyve --with-interface --with-network --with-storage-fs
> > --with-storage-lvm --with-storage-iscsi --with-storage-iscsi-direct
> > --with-storage-scsi --with-storage-disk --with-storage-mpath
> > --with-storage-rbd --without-storage-sheepdog --with-storage-gluster
> > --without-storage-zfs --without-storage-vstorage --with-numactl
> > --with-numad --with-capng --without-fuse --with-netcf --with-selinux
> > --with-selinux-mount=/sys/fs/selinux --without-apparmor --without-hal
> > --with-udev --with-yajl --with-sanlock --with-libpcap --with-macvtap
> > --with-audit --with-dtrace --with-driver-modules --with-firewalld
> > --with-firewalld-zone --without-wireshark-dissector --without-pm-utils
> > --with-nss-plugin '--with-packager=Unknown, 2019-08-19-12:13:01,
> > lab.rhel8.me ' --with-packager-version=1.el8
> > --with-qemu-user=qemu --with-qemu-group=qemu
> > --with-tls-priority=@LIBVIRT,SYSTEM --enable-werror
> > --enable-expensive-tests --with-init-script=systemd
> > --without-login-shell && make -j8
> > # LD_PRELOAD="$(find src -name '*.so.*'|tr '\n' ' ')" src/.libs/virtlogd
> > # LD_PRELOAD="$(find src -name '*.so.*'|tr '\n' ' ')" LIBVIRT_DEBUG=3
> > LIBVIRT_LOG_FILTERS="1:util 1:qemu 1:security"
> > LIBVIRT_LOG_OUTPUTS="1:file:/tmp/libvirt_daemon.log" src/.libs/libvirtd
> >
> > 3. Define and start an VM with the qcow2 data file. Note that the
> > -blockdev is not enabled
> > # virsh define pc-data.xml
> > # virsh start pc-data
> >
> > 4. Create snapshot and check the data file XATTRs:
> > # virsh snapshot-create-as pc-data s1 --no-metadata --disk-only
> > # getfattr -m - -d /var/lib/libvirt/images/pc-data.raw
> > getfattr: Removing leading '/' from absolute path names
> > # file: var/lib/libvirt/images/pc-data.raw
> > security.selinux="unconfined_u:object_r:svirt_image_t:s0:c775,c1011"
> > trusted.libvirt.security.dac="+107:+107"
> > trusted.libvirt.security.ref_dac="1"
> > trusted.libvirt.security.ref_selinux="1"
> >
> trusted.libvirt.security.selinux="unconfined_u:object_r:svirt_image_t:s0:c284,c367"
> > trusted.libvirt.security.timestamp_dac="1563328069"
> > trusted.libvirt.security.timestamp_selinux="1563328069"
> >
> > Shutdown the VM. The XATTRs of data file is not changed.
> > It is not expected. The XTTRs should not contain *.libvirt.*
> >
> > Issue is not reproduced with -blockdev enabled:
> > 
> > ...
> >   
> > 
> > 
> >   
> > 
> >
> > See the libvirt daemon log and vm xml in attachment.
>
> Nice catch! I will need to dig into this to figure out where the issue
> is. Can you put this info into an upstream bug report in
>
Sure. https://bugzilla.redhat.com/show_bug.cgi?id=1762135

> product=Virtualization Tools  and I will get to it when I can
>
> Thanks,
> Cole
>


-- 
Best regards,
---
Han Han
Quality Engineer
Redhat.

Email: h...@redhat.com
Phone: +861065339333
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v3] util: Block SIGPIPE until execve in child process

2019-10-15 Thread Wang Yechao
Libvirtd has set SIGPIPE to ignored, and virFork resets all signal
handlers to the defaults. But child process may write logs to
stderr/stdout, that may generate SIGPIPE if journald has stopped.

So block SIGPIPE in virFork, and unblock it before execve.

Signed-off-by: Wang Yechao 
---
v1 patch:
https://www.redhat.com/archives/libvir-list/2019-October/msg00720.html

Changes in v2:
 -  use pthread_sigmask to block SIGPIPE

Changes in v3:
 -  pass SIG_UNBLOCK(not SIG_SETMASK) to pthread_sigmask when unlock SIGPIPE

---
 src/util/vircommand.c | 17 +
 1 file changed, 17 insertions(+)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 79e1e87..bd3a582 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -321,6 +321,15 @@ virFork(void)
 virDispatchError(NULL);
 _exit(EXIT_CANCELED);
 }
+
+sigemptyset();
+sigaddset(, SIGPIPE);
+if (pthread_sigmask(SIG_BLOCK, , NULL) != 0) {
+virReportSystemError(errno, "%s", _("cannot block SIGPIPE"));
+virDispatchError(NULL);
+_exit(EXIT_CANCELED);
+}
+
 }
 return pid;
 }
@@ -553,6 +562,7 @@ virExec(virCommandPtr cmd)
 struct sigaction waxon, waxoff;
 VIR_AUTOFREE(gid_t *) groups = NULL;
 int ngroups;
+sigset_t set;
 
 if (cmd->args[0][0] != '/') {
 if (!(binary = binarystr = virFindFileInPath(cmd->args[0]))) {
@@ -792,6 +802,13 @@ virExec(virCommandPtr cmd)
 /* Close logging again to ensure no FDs leak to child */
 virLogReset();
 
+sigemptyset();
+sigaddset(, SIGPIPE);
+if (pthread_sigmask(SIG_UNBLOCK, , NULL) != 0) {
+virReportSystemError(errno, "%s", _("cannot unblock SIGPIPE"));
+goto fork_error;
+}
+
 if (cmd->env)
 execve(binary, cmd->args, cmd->env);
 else
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 1/3] qemu_process: use VIR_AUTOFREE()

2019-10-15 Thread Daniel Henrique Barboza
Change all strings and scalar pointers to use VIR_AUTOFREE(),
clearing up the 'cleanup' labels when possible.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_process.c | 118 ++--
 1 file changed, 40 insertions(+), 78 deletions(-)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 9c50c4a1d8..f4b3144154 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -106,7 +106,7 @@ qemuProcessRemoveDomainStatus(virQEMUDriverPtr driver,
   virDomainObjPtr vm)
 {
 char ebuf[1024];
-char *file = NULL;
+VIR_AUTOFREE(char *) file = NULL;
 qemuDomainObjPrivatePtr priv = vm->privateData;
 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
 int ret = -1;
@@ -117,7 +117,6 @@ qemuProcessRemoveDomainStatus(virQEMUDriverPtr driver,
 if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR)
 VIR_WARN("Failed to remove domain XML for %s: %s",
  vm->def->name, virStrerror(errno, ebuf, sizeof(ebuf)));
-VIR_FREE(file);
 
 if (priv->pidfile &&
 unlink(priv->pidfile) < 0 &&
@@ -1513,7 +1512,7 @@ qemuProcessHandleBlockThreshold(qemuMonitorPtr mon 
G_GNUC_UNUSED,
 virDomainDiskDefPtr disk;
 virStorageSourcePtr src;
 unsigned int idx;
-char *dev = NULL;
+VIR_AUTOFREE(char *) dev = NULL;
 const char *path = NULL;
 
 virObjectLock(vm);
@@ -1526,11 +1525,9 @@ qemuProcessHandleBlockThreshold(qemuMonitorPtr mon 
G_GNUC_UNUSED,
 if (virStorageSourceIsLocalStorage(src))
 path = src->path;
 
-if ((dev = qemuDomainDiskBackingStoreGetName(disk, src, idx))) {
+if ((dev = qemuDomainDiskBackingStoreGetName(disk, src, idx)))
 event = virDomainEventBlockThresholdNewFromObj(vm, dev, path,
threshold, excess);
-VIR_FREE(dev);
-}
 }
 
 virObjectUnlock(vm);
@@ -2065,7 +2062,7 @@ static int
 qemuProcessReportLogError(qemuDomainLogContextPtr logCtxt,
   const char *msgprefix)
 {
-char *logmsg = NULL;
+VIR_AUTOFREE(char *) logmsg = NULL;
 size_t max;
 
 max = VIR_ERROR_MAX_LENGTH - 1;
@@ -2082,7 +2079,6 @@ qemuProcessReportLogError(qemuDomainLogContextPtr logCtxt,
 else
 virReportError(VIR_ERR_INTERNAL_ERROR, _("%s: %s"), msgprefix, logmsg);
 
-VIR_FREE(logmsg);
 return 0;
 }
 
@@ -2102,9 +2098,8 @@ qemuProcessLookupPTYs(virDomainChrDefPtr *devices,
   int count,
   virHashTablePtr info)
 {
-char *id = NULL;
+VIR_AUTOFREE(char *) id = NULL;
 size_t i;
-int ret = -1;
 
 for (i = 0; i < count; i++) {
 virDomainChrDefPtr chr = devices[i];
@@ -2123,7 +2118,7 @@ qemuProcessLookupPTYs(virDomainChrDefPtr *devices,
  */
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("no assigned pty for device %s"), id);
-goto cleanup;
+return -1;
 } else {
 /* 'info chardev' had no pty path for this chardev,
  * but the log output had, so we're fine
@@ -2134,14 +2129,11 @@ qemuProcessLookupPTYs(virDomainChrDefPtr *devices,
 
 VIR_FREE(chr->source->data.file.path);
 if (VIR_STRDUP(chr->source->data.file.path, entry->ptyPath) < 0)
-goto cleanup;
+return -1;
 }
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(id);
-return ret;
+return 0;
 }
 
 static int
@@ -2193,8 +2185,7 @@ qemuProcessRefreshChannelVirtioState(virQEMUDriverPtr 
driver,
 int agentReason = VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_CHANNEL;
 qemuMonitorChardevInfoPtr entry;
 virObjectEventPtr event = NULL;
-char *id = NULL;
-int ret = -1;
+VIR_AUTOFREE(char *) id = NULL;
 
 if (booted)
 agentReason = 
VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_DOMAIN_STARTED;
@@ -2205,7 +2196,7 @@ qemuProcessRefreshChannelVirtioState(virQEMUDriverPtr 
driver,
 
 VIR_FREE(id);
 if (virAsprintf(, "char%s", chr->info.alias) < 0)
-goto cleanup;
+return -1;
 
 /* port state not reported */
 if (!(entry = virHashLookup(info, id)) ||
@@ -,10 +2213,7 @@ qemuProcessRefreshChannelVirtioState(virQEMUDriverPtr 
driver,
 }
 }
 
-ret = 0;
- cleanup:
-VIR_FREE(id);
-return ret;
+return 0;
 }
 
 
@@ -2645,7 +2633,7 @@ qemuProcessSetupPid(virDomainObjPtr vm,
 virCgroupPtr cgroup = NULL;
 virBitmapPtr use_cpumask = NULL;
 VIR_AUTOPTR(virBitmap) hostcpumap = NULL;
-char *mem_mask = NULL;
+VIR_AUTOFREE(char *) mem_mask = NULL;
 int ret = -1;
 
 if ((period || quota) &&
@@ -2722,7 +2710,6 @@ qemuProcessSetupPid(virDomainObjPtr vm,
 
 ret = 0;
  cleanup:
-

[libvirt] [PATCH v2 2/3] qemu_process: use VIR_AUTOUNREF()

2019-10-15 Thread Daniel Henrique Barboza
Change all feasible vir*Ptr pointers to be AUTOUNREF(), clearing
up the 'cleanup' labels when possible.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_process.c | 316 +++-
 1 file changed, 116 insertions(+), 200 deletions(-)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index f4b3144154..72919c370d 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -108,11 +108,10 @@ qemuProcessRemoveDomainStatus(virQEMUDriverPtr driver,
 char ebuf[1024];
 VIR_AUTOFREE(char *) file = NULL;
 qemuDomainObjPrivatePtr priv = vm->privateData;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
-int ret = -1;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 
 if (virAsprintf(, "%s/%s.xml", cfg->stateDir, vm->def->name) < 0)
-goto cleanup;
+return -1;
 
 if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR)
 VIR_WARN("Failed to remove domain XML for %s: %s",
@@ -124,10 +123,7 @@ qemuProcessRemoveDomainStatus(virQEMUDriverPtr driver,
 VIR_WARN("Failed to remove PID file for %s: %s",
  vm->def->name, virStrerror(errno, ebuf, sizeof(ebuf)));
 
-ret = 0;
- cleanup:
-virObjectUnref(cfg);
-return ret;
+return 0;
 }
 
 
@@ -407,7 +403,7 @@ qemuProcessHandleReset(qemuMonitorPtr mon G_GNUC_UNUSED,
 virQEMUDriverPtr driver = opaque;
 virObjectEventPtr event;
 qemuDomainObjPrivatePtr priv;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 int ret = -1;
 
 virObjectLock(vm);
@@ -444,7 +440,6 @@ qemuProcessHandleReset(qemuMonitorPtr mon G_GNUC_UNUSED,
  cleanup:
 virObjectUnlock(vm);
 virObjectEventStateQueue(driver->domainEventState, event);
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -463,7 +458,7 @@ qemuProcessFakeReboot(void *opaque)
 virDomainObjPtr vm = opaque;
 qemuDomainObjPrivatePtr priv = vm->privateData;
 virQEMUDriverPtr driver = priv->driver;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 virDomainRunningReason reason = VIR_DOMAIN_RUNNING_BOOTED;
 int ret = -1, rc;
 
@@ -513,7 +508,6 @@ qemuProcessFakeReboot(void *opaque)
 if (ret == -1)
 ignore_value(qemuProcessKill(vm, VIR_QEMU_PROCESS_KILL_FORCE));
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 }
 
 
@@ -576,7 +570,7 @@ qemuProcessHandleShutdown(qemuMonitorPtr mon G_GNUC_UNUSED,
 virQEMUDriverPtr driver = opaque;
 qemuDomainObjPrivatePtr priv;
 virObjectEventPtr event = NULL;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 int detail = 0;
 
 VIR_DEBUG("vm=%p", vm);
@@ -633,7 +627,6 @@ qemuProcessHandleShutdown(qemuMonitorPtr mon G_GNUC_UNUSED,
  unlock:
 virObjectUnlock(vm);
 virObjectEventStateQueue(driver->domainEventState, event);
-virObjectUnref(cfg);
 
 return 0;
 }
@@ -648,7 +641,7 @@ qemuProcessHandleStop(qemuMonitorPtr mon G_GNUC_UNUSED,
 virObjectEventPtr event = NULL;
 virDomainPausedReason reason;
 virDomainEventSuspendedDetailType detail;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 qemuDomainObjPrivatePtr priv = vm->privateData;
 
 virObjectLock(vm);
@@ -694,7 +687,6 @@ qemuProcessHandleStop(qemuMonitorPtr mon G_GNUC_UNUSED,
 
 virObjectUnlock(vm);
 virObjectEventStateQueue(driver->domainEventState, event);
-virObjectUnref(cfg);
 
 return 0;
 }
@@ -707,7 +699,7 @@ qemuProcessHandleResume(qemuMonitorPtr mon G_GNUC_UNUSED,
 {
 virQEMUDriverPtr driver = opaque;
 virObjectEventPtr event = NULL;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 qemuDomainObjPrivatePtr priv;
 virDomainRunningReason reason = VIR_DOMAIN_RUNNING_UNPAUSED;
 virDomainEventResumedDetailType eventDetail;
@@ -740,7 +732,6 @@ qemuProcessHandleResume(qemuMonitorPtr mon G_GNUC_UNUSED,
 
 virObjectUnlock(vm);
 virObjectEventStateQueue(driver->domainEventState, event);
-virObjectUnref(cfg);
 return 0;
 }
 
@@ -752,7 +743,7 @@ qemuProcessHandleRTCChange(qemuMonitorPtr mon G_GNUC_UNUSED,
 {
 virQEMUDriverPtr driver = opaque;
 virObjectEventPtr event = NULL;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 
 virObjectLock(vm);
 
@@ -784,7 +775,6 @@ qemuProcessHandleRTCChange(qemuMonitorPtr mon G_GNUC_UNUSED,
 virObjectUnlock(vm);
 
 virObjectEventStateQueue(driver->domainEventState, event);
- 

[libvirt] [PATCH v2 0/3] qemu_process: use VIR_AUTO*/g_auto* all around

2019-10-15 Thread Daniel Henrique Barboza
The usual AUTOFREE() and AUTOUNREF() changes that allows
for a bit of cleanup. A new patch was added to convert
most of the VIR_AUTO* macros to GLib g_auto* ones.


v1: https://www.redhat.com/archives/libvir-list/2019-September/msg01465.html 

Daniel Henrique Barboza (3):
  qemu_process: use VIR_AUTOFREE()
  qemu_process: use VIR_AUTOUNREF()
  qemu_process.c: use GLib macros

 src/qemu/qemu_process.c | 444 +++-
 1 file changed, 161 insertions(+), 283 deletions(-)

-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 3/3] qemu_process.c: use GLib macros

2019-10-15 Thread Daniel Henrique Barboza
Previous patches added VIR_AUTO* facilities in qemu_process.c to
cleanup virObjectUnref() and VIR_FREE() calls. This patch now
converts those VIR_AUTO* macros to their GLib alternatives,
g_autoptr and g_autofree.

The following functions weren't changed in this patch:

- qemuProcessUpdateCPU - error when trying to use g_autoptr with
virDomainCapsCPUModelsPtr;
- qemuProcessLaunch - error when trying to use g_autoptr with
qemuDomainLogContextPtr;
- qemuProcessQMPConnectMonitor - error when trying to use g_autoptr
with virDomainXMLOptionPtr.

The rest of the file was converted to GLib auto* macros.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_process.c | 136 
 1 file changed, 68 insertions(+), 68 deletions(-)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 72919c370d..7cf47207da 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -106,9 +106,9 @@ qemuProcessRemoveDomainStatus(virQEMUDriverPtr driver,
   virDomainObjPtr vm)
 {
 char ebuf[1024];
-VIR_AUTOFREE(char *) file = NULL;
+g_autofree char *file = NULL;
 qemuDomainObjPrivatePtr priv = vm->privateData;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 
 if (virAsprintf(, "%s/%s.xml", cfg->stateDir, vm->def->name) < 0)
 return -1;
@@ -403,7 +403,7 @@ qemuProcessHandleReset(qemuMonitorPtr mon G_GNUC_UNUSED,
 virQEMUDriverPtr driver = opaque;
 virObjectEventPtr event;
 qemuDomainObjPrivatePtr priv;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 int ret = -1;
 
 virObjectLock(vm);
@@ -458,7 +458,7 @@ qemuProcessFakeReboot(void *opaque)
 virDomainObjPtr vm = opaque;
 qemuDomainObjPrivatePtr priv = vm->privateData;
 virQEMUDriverPtr driver = priv->driver;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 virDomainRunningReason reason = VIR_DOMAIN_RUNNING_BOOTED;
 int ret = -1, rc;
 
@@ -570,7 +570,7 @@ qemuProcessHandleShutdown(qemuMonitorPtr mon G_GNUC_UNUSED,
 virQEMUDriverPtr driver = opaque;
 qemuDomainObjPrivatePtr priv;
 virObjectEventPtr event = NULL;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 int detail = 0;
 
 VIR_DEBUG("vm=%p", vm);
@@ -641,7 +641,7 @@ qemuProcessHandleStop(qemuMonitorPtr mon G_GNUC_UNUSED,
 virObjectEventPtr event = NULL;
 virDomainPausedReason reason;
 virDomainEventSuspendedDetailType detail;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 qemuDomainObjPrivatePtr priv = vm->privateData;
 
 virObjectLock(vm);
@@ -699,7 +699,7 @@ qemuProcessHandleResume(qemuMonitorPtr mon G_GNUC_UNUSED,
 {
 virQEMUDriverPtr driver = opaque;
 virObjectEventPtr event = NULL;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 qemuDomainObjPrivatePtr priv;
 virDomainRunningReason reason = VIR_DOMAIN_RUNNING_UNPAUSED;
 virDomainEventResumedDetailType eventDetail;
@@ -743,7 +743,7 @@ qemuProcessHandleRTCChange(qemuMonitorPtr mon G_GNUC_UNUSED,
 {
 virQEMUDriverPtr driver = opaque;
 virObjectEventPtr event = NULL;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 
 virObjectLock(vm);
 
@@ -788,7 +788,7 @@ qemuProcessHandleWatchdog(qemuMonitorPtr mon G_GNUC_UNUSED,
 virQEMUDriverPtr driver = opaque;
 virObjectEventPtr watchdogEvent = NULL;
 virObjectEventPtr lifecycleEvent = NULL;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 
 virObjectLock(vm);
 watchdogEvent = virDomainEventWatchdogNewFromObj(vm, action);
@@ -856,7 +856,7 @@ qemuProcessHandleIOError(qemuMonitorPtr mon G_GNUC_UNUSED,
 const char *srcPath;
 const char *devAlias;
 virDomainDiskDefPtr disk;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 
 virObjectLock(vm);
 
@@ -923,7 +923,7 @@ qemuProcessHandleBlockJob(qemuMonitorPtr mon G_GNUC_UNUSED,
 virQEMUDriverPtr driver = opaque;
 struct qemuProcessEvent *processEvent = NULL;
 virDomainDiskDefPtr disk;
-VIR_AUTOUNREF(qemuBlockJobDataPtr) job = NULL;
+g_autoptr(qemuBlockJobData) job = 

[libvirt] [PATCH v3 03/10] qemu_driver: use VIR_AUTOUNREF() with virQEMUDriverConfigPtr 3/3

2019-10-15 Thread Daniel Henrique Barboza
virQEMUDriverConfigPtr can be auto-unref for the great majority
of the uses made in qemu_driver, sparing us a virObjectUnref()
call and sometimes a whole 'cleanup' label.

This patch changes virQEMUDriverConfigPtr declarations to
use VIR_AUTOUNREF(). 'cleanup' labels were deleted when
applicable.

This is the last part of this change. All but one* instance of
virQEMUDriverConfigPtr were changed to use VIR_AUTOUNREF().
'cleanup' labels were deleted when applicable.

* qemuStateInitialize: we can't auto-unref the pointer since we're
initializing the qemu_driver object with it.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 51 ++
 1 file changed, 17 insertions(+), 34 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 9f9443f493..d9effd7f34 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -10150,7 +10150,7 @@ qemuDomainSetNumaParameters(virDomainPtr dom,
 virDomainDefPtr persistentDef;
 virDomainObjPtr vm = NULL;
 int ret = -1;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 qemuDomainObjPrivatePtr priv;
 virBitmapPtr nodeset = NULL;
 virDomainNumatuneMemMode config_mode;
@@ -10262,7 +10262,6 @@ qemuDomainSetNumaParameters(virDomainPtr dom,
  cleanup:
 virBitmapFree(nodeset);
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -10367,7 +10366,7 @@ qemuDomainSetPerfEvents(virDomainPtr dom,
 virQEMUDriverPtr driver = dom->conn->privateData;
 size_t i;
 virDomainObjPtr vm = NULL;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 qemuDomainObjPrivatePtr priv;
 virDomainDefPtr def;
 virDomainDefPtr persistentDef;
@@ -10459,7 +10458,6 @@ qemuDomainSetPerfEvents(virDomainPtr dom,
 
  cleanup:
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -10646,7 +10644,7 @@ qemuDomainSetSchedulerParametersFlags(virDomainPtr dom,
 long long value_l;
 int ret = -1;
 int rc;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 virCapsPtr caps = NULL;
 qemuDomainObjPrivatePtr priv;
 virObjectEventPtr event = NULL;
@@ -10940,7 +10938,6 @@ qemuDomainSetSchedulerParametersFlags(virDomainPtr dom,
 if (eventNparams)
 virTypedParamsFree(eventParams, eventNparams);
 virObjectUnref(caps);
-virObjectUnref(cfg);
 return ret;
 }
 #undef SCHED_RANGE_CHECK
@@ -11605,7 +11602,7 @@ qemuDomainSetInterfaceParameters(virDomainPtr dom,
 int ret = -1;
 virDomainNetDefPtr net = NULL, persistentNet = NULL;
 virNetDevBandwidthPtr bandwidth = NULL, newBandwidth = NULL;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 bool inboundSpecified = false, outboundSpecified = false;
 int actualType;
 bool qosSupported = true;
@@ -11800,7 +11797,6 @@ qemuDomainSetInterfaceParameters(virDomainPtr dom,
 virNetDevBandwidthFree(bandwidth);
 virNetDevBandwidthFree(newBandwidth);
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -12062,7 +12058,7 @@ qemuDomainMemoryPeek(virDomainPtr dom,
 char *tmp = NULL;
 int fd = -1, ret = -1;
 qemuDomainObjPrivatePtr priv;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 
 virCheckFlags(VIR_MEMORY_VIRTUAL | VIR_MEMORY_PHYSICAL, -1);
 
@@ -12133,7 +12129,6 @@ qemuDomainMemoryPeek(virDomainPtr dom,
 unlink(tmp);
 VIR_FREE(tmp);
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -12350,7 +12345,7 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
 virDomainObjPtr vm;
 int ret = -1;
 virDomainDiskDefPtr disk;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 qemuBlockStatsPtr entry = NULL;
 
 virCheckFlags(0, -1);
@@ -12439,7 +12434,6 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
  cleanup:
 VIR_FREE(entry);
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -12911,7 +12905,7 @@ qemuDomainMigratePrepare3Params(virConnectPtr dconn,
 unsigned int flags)
 {
 virQEMUDriverPtr driver = dconn->privateData;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 virDomainDefPtr def = NULL;
 const char *dom_xml = NULL;
 const char *dname = NULL;
@@ -12985,7 +12979,6 @@ qemuDomainMigratePrepare3Params(virConnectPtr dconn,
 VIR_FREE(migrate_disks);
 VIR_FREE(origname);
 virDomainDefFree(def);
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -14709,7 +14702,7 @@ 
qemuDomainSnapshotCreateInactiveExternal(virQEMUDriverPtr driver,
 virCommandPtr cmd = NULL;
 const char *qemuImgPath;
 virBitmapPtr 

[libvirt] [PATCH v3 04/10] qemu_driver: use VIR_AUTOUNREF() with virCapsPtr

2019-10-15 Thread Daniel Henrique Barboza
virCapsPtr can be auto-unref for the great majority
of the uses made in qemu_driver, sparing us a virObjectUnref()
call and sometimes a whole 'cleanup' label.

This patch changes virCapsPtr declarations to use VIR_AUTOUNREF().
'cleanup' labels were deleted when possible.

Suggested-by: Erik Skultety 
Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 113 ++---
 1 file changed, 37 insertions(+), 76 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d9effd7f34..99923b0ab6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -396,7 +396,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
   VIR_DOMAIN_SNAPSHOT_PARSE_DISKS |
   VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL);
 int ret = -1;
-virCapsPtr caps = NULL;
+VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 int direrr;
 qemuDomainObjPrivatePtr priv;
 
@@ -495,7 +495,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
  cleanup:
 VIR_DIR_CLOSE(dir);
 VIR_FREE(snapDir);
-virObjectUnref(caps);
 virObjectUnlock(vm);
 return ret;
 }
@@ -516,7 +515,7 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 virDomainMomentObjPtr current = NULL;
 unsigned int flags = VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE;
 int ret = -1;
-virCapsPtr caps = NULL;
+VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 int direrr;
 qemuDomainObjPrivatePtr priv;
 
@@ -606,7 +605,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
  cleanup:
 VIR_DIR_CLOSE(dir);
 VIR_FREE(chkDir);
-virObjectUnref(caps);
 virObjectUnlock(vm);
 return ret;
 }
@@ -1072,13 +1070,13 @@ static int
 qemuStateReload(void)
 {
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
-virCapsPtr caps = NULL;
+VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 
 if (!qemu_driver)
 return 0;
 
 if (!(caps = virQEMUDriverGetCapabilities(qemu_driver, false)))
-goto cleanup;
+return 0;
 
 cfg = virQEMUDriverGetConfig(qemu_driver);
 virDomainObjListLoadAllConfigs(qemu_driver->domains,
@@ -1086,8 +1084,6 @@ qemuStateReload(void)
cfg->autostartDir, false,
caps, qemu_driver->xmlopt,
qemuNotifyLoadDomain, qemu_driver);
- cleanup:
-virObjectUnref(caps);
 return 0;
 }
 
@@ -1356,21 +1352,15 @@ qemuConnectGetMaxVcpus(virConnectPtr conn 
G_GNUC_UNUSED, const char *type)
 
 static char *qemuConnectGetCapabilities(virConnectPtr conn) {
 virQEMUDriverPtr driver = conn->privateData;
-virCapsPtr caps = NULL;
-char *xml = NULL;
+VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 
 if (virConnectGetCapabilitiesEnsureACL(conn) < 0)
 return NULL;
 
 if (!(caps = virQEMUDriverGetCapabilities(driver, true)))
-goto cleanup;
-
-xml = virCapabilitiesFormatXML(caps);
-virObjectUnref(caps);
-
- cleanup:
+return NULL;
 
-return xml;
+return virCapabilitiesFormatXML(caps);
 }
 
 
@@ -1716,27 +1706,22 @@ static int qemuDomainIsUpdated(virDomainPtr dom)
 static int qemuConnectGetVersion(virConnectPtr conn, unsigned long *version)
 {
 virQEMUDriverPtr driver = conn->privateData;
-int ret = -1;
 unsigned int qemuVersion = 0;
-virCapsPtr caps = NULL;
+VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 
 if (virConnectGetVersionEnsureACL(conn) < 0)
 return -1;
 
 if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
-goto cleanup;
+return -1;
 
 if (virQEMUCapsGetDefaultVersion(caps,
  driver->qemuCapsCache,
  ) < 0)
-goto cleanup;
+return -1;
 
 *version = qemuVersion;
-ret = 0;
-
- cleanup:
-virObjectUnref(caps);
-return ret;
+return 0;
 }
 
 
@@ -1789,7 +1774,7 @@ static virDomainPtr qemuDomainCreateXML(virConnectPtr 
conn,
 virObjectEventPtr event = NULL;
 virObjectEventPtr event2 = NULL;
 unsigned int start_flags = VIR_QEMU_PROCESS_START_COLD;
-virCapsPtr caps = NULL;
+VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 unsigned int parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE |
VIR_DOMAIN_DEF_PARSE_ABI_UPDATE;
 
@@ -1864,7 +1849,6 @@ static virDomainPtr qemuDomainCreateXML(virConnectPtr 
conn,
 virDomainObjEndAPI();
 virObjectEventStateQueue(driver->domainEventState, event);
 virObjectEventStateQueue(driver->domainEventState, event2);
-virObjectUnref(caps);
 virNWFilterUnlockFilterUpdates();
 return dom;
 }
@@ -3324,7 +3308,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver,
 int ret = -1;
 virObjectEventPtr event = NULL;
 qemuDomainObjPrivatePtr priv = vm->privateData;
-virCapsPtr caps;
+VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 virQEMUSaveDataPtr data = NULL;
 qemuDomainSaveCookiePtr cookie = NULL;
 
@@ -3439,7 

[libvirt] [PATCH v3 05/10] qemu_driver: use VIR_AUTOUNREF() with more pointer types

2019-10-15 Thread Daniel Henrique Barboza
This patch uses VIR_AUTOUNREF() with the following pointer types:

- virQEMUCapsPtr
- virConnect
- qemuDomainSaveCookiePtr
- virDomainCapsPtr
- qemuBlockJobDataPtr*
- virNetworkPtr
- virSecurityManagerPtr

'cleanup' labels were deleted when possible.

* instances being cleaned up with qemuBlockJobStartupFinalize()
weren't changed, since qemuBlockJobStartupFinalize() will
unref the object after qemuBlockJobUnregister().

Suggested-by: Erik Skultety 
Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 64 +++---
 1 file changed, 22 insertions(+), 42 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 99923b0ab6..d95c5c5b81 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -305,8 +305,8 @@ static int
 qemuSecurityInit(virQEMUDriverPtr driver)
 {
 char **names;
-virSecurityManagerPtr mgr = NULL;
-virSecurityManagerPtr stack = NULL;
+VIR_AUTOUNREF(virSecurityManagerPtr) mgr = NULL;
+VIR_AUTOUNREF(virSecurityManagerPtr) stack = NULL;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 unsigned int flags = 0;
 
@@ -372,8 +372,6 @@ qemuSecurityInit(virQEMUDriverPtr driver)
  error:
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Failed to initialize security drivers"));
-virObjectUnref(stack);
-virObjectUnref(mgr);
 return -1;
 }
 
@@ -1098,7 +1096,7 @@ static int
 qemuStateStop(void)
 {
 int ret = -1;
-virConnectPtr conn;
+VIR_AUTOUNREF(virConnectPtr) conn = NULL;
 int numDomains = 0;
 size_t i;
 int state;
@@ -1142,7 +1140,6 @@ qemuStateStop(void)
 VIR_FREE(domains);
 }
 VIR_FREE(flags);
-virObjectUnref(conn);
 
 return ret;
 }
@@ -3310,7 +3307,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver,
 qemuDomainObjPrivatePtr priv = vm->privateData;
 VIR_AUTOUNREF(virCapsPtr) caps = NULL;
 virQEMUSaveDataPtr data = NULL;
-qemuDomainSaveCookiePtr cookie = NULL;
+VIR_AUTOUNREF(qemuDomainSaveCookiePtr) cookie = NULL;
 
 if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
 goto cleanup;
@@ -3419,7 +3416,6 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver,
 qemuDomainRemoveInactiveJob(driver, vm);
 
  cleanup:
-virObjectUnref(cookie);
 VIR_FREE(xml);
 virQEMUSaveDataFree(data);
 virObjectEventStateQueue(driver->domainEventState, event);
@@ -6866,7 +6862,7 @@ qemuDomainSaveImageStartVM(virConnectPtr conn,
 char *errbuf = NULL;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 virQEMUSaveHeaderPtr header = >header;
-qemuDomainSaveCookiePtr cookie = NULL;
+VIR_AUTOUNREF(qemuDomainSaveCookiePtr) cookie = NULL;
 
 if (virSaveCookieParseString(data->cookie, (virObjectPtr *),
  
virDomainXMLOptionGetSaveCookie(driver->xmlopt)) < 0)
@@ -6981,7 +6977,6 @@ qemuDomainSaveImageStartVM(virConnectPtr conn,
 ret = 0;
 
  cleanup:
-virObjectUnref(cookie);
 virCommandFree(cmd);
 VIR_FREE(errbuf);
 if (qemuSecurityRestoreSavedStateLabel(driver, vm, path) < 0)
@@ -13588,7 +13583,7 @@ qemuConnectCompareHypervisorCPU(virConnectPtr conn,
 int ret = VIR_CPU_COMPARE_ERROR;
 virQEMUDriverPtr driver = conn->privateData;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
-virQEMUCapsPtr qemuCaps = NULL;
+VIR_AUTOUNREF(virQEMUCapsPtr) qemuCaps = NULL;
 bool failIncompatible;
 virCPUDefPtr hvCPU;
 virCPUDefPtr cpu = NULL;
@@ -13642,7 +13637,6 @@ qemuConnectCompareHypervisorCPU(virConnectPtr conn,
 
  cleanup:
 virCPUDefFree(cpu);
-virObjectUnref(qemuCaps);
 return ret;
 }
 
@@ -13802,7 +13796,7 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
 virQEMUDriverPtr driver = conn->privateData;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 virCPUDefPtr *cpus = NULL;
-virQEMUCapsPtr qemuCaps = NULL;
+VIR_AUTOUNREF(virQEMUCapsPtr) qemuCaps = NULL;
 virArch arch;
 virDomainVirtType virttype;
 virDomainCapsCPUModelsPtr cpuModels;
@@ -13881,7 +13875,6 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
  cleanup:
 virCPUDefListFree(cpus);
 virCPUDefFree(cpu);
-virObjectUnref(qemuCaps);
 virStringListFree(features);
 
 return cpustr;
@@ -17689,7 +17682,7 @@ qemuDomainBlockJobAbort(virDomainPtr dom,
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 bool pivot = !!(flags & VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT);
 bool async = !!(flags & VIR_DOMAIN_BLOCK_JOB_ABORT_ASYNC);
-qemuBlockJobDataPtr job = NULL;
+VIR_AUTOUNREF(qemuBlockJobDataPtr) job = NULL;
 virDomainObjPtr vm;
 qemuDomainObjPrivatePtr priv = NULL;
 bool blockdev = false;
@@ -17790,7 +17783,6 @@ qemuDomainBlockJobAbort(virDomainPtr dom,
 qemuDomainObjEndJob(driver, vm);
 
  cleanup:
-   

[libvirt] [PATCH v3 10/10] qemu_driver.c: use GLib macros

2019-10-15 Thread Daniel Henrique Barboza
Previous patches added VIR_AUTO* facilities in qemu_driver.c to
cleanup virObjectUnref() and VIR_FREE() calls. This patch now
converts those VIR_AUTO* macros to their GLib alternatives,
g_autoptr and g_autofree.

After this patch, VIR_AUTO* macros are being used only in three
functions:

- qemuSecurityInit
- qemuDomainSaveInternal
- qemuDomainSaveImageStartVM

The rest of the file was converted to GLib macros.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 366 -
 1 file changed, 183 insertions(+), 183 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 6f283d2de3..046f68ad40 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -202,7 +202,7 @@ qemuAutostartDomain(virDomainObjPtr vm,
 {
 virQEMUDriverPtr driver = opaque;
 int flags = 0;
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
+g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
 int ret = -1;
 
 if (cfg->autoStartBypassCache)
@@ -254,7 +254,7 @@ qemuSecurityChownCallback(const virStorageSource *src,
 int save_errno = 0;
 int ret = -1;
 int rv;
-VIR_AUTOUNREF(virStorageSourcePtr) cpy = NULL;
+g_autoptr(virStorageSource) cpy = NULL;
 
 rv = virStorageFileSupportsSecurityDriver(src);
 if (rv <= 0)
@@ -381,7 +381,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
void *data)
 {
 char *baseDir = (char *)data;
-VIR_AUTOFREE(char *) snapDir = NULL;
+g_autofree char *snapDir = NULL;
 DIR *dir = NULL;
 struct dirent *entry;
 virDomainSnapshotDefPtr def = NULL;
@@ -392,7 +392,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
   VIR_DOMAIN_SNAPSHOT_PARSE_DISKS |
   VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL);
 int ret = -1;
-VIR_AUTOUNREF(virCapsPtr) caps = NULL;
+g_autoptr(virCaps) caps = NULL;
 int direrr;
 qemuDomainObjPrivatePtr priv;
 
@@ -418,8 +418,8 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
 goto cleanup;
 
 while ((direrr = virDirRead(dir, , NULL)) > 0) {
-VIR_AUTOFREE(char *) xmlStr = NULL;
-VIR_AUTOFREE(char *) fullpath = NULL;
+g_autofree char *xmlStr = NULL;
+g_autofree char *fullpath = NULL;
 
 /* NB: ignoring errors, so one malformed config doesn't
kill the whole process */
@@ -497,7 +497,7 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
  void *data)
 {
 char *baseDir = (char *)data;
-VIR_AUTOFREE(char *) chkDir = NULL;
+g_autofree char *chkDir = NULL;
 DIR *dir = NULL;
 struct dirent *entry;
 virDomainCheckpointDefPtr def = NULL;
@@ -505,7 +505,7 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 virDomainMomentObjPtr current = NULL;
 unsigned int flags = VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE;
 int ret = -1;
-VIR_AUTOUNREF(virCapsPtr) caps = NULL;
+g_autoptr(virCaps) caps = NULL;
 int direrr;
 qemuDomainObjPrivatePtr priv;
 
@@ -530,8 +530,8 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 goto cleanup;
 
 while ((direrr = virDirRead(dir, , NULL)) > 0) {
-VIR_AUTOFREE(char *) xmlStr = NULL;
-VIR_AUTOFREE(char *) fullpath = NULL;
+g_autofree char *xmlStr = NULL;
+g_autofree char *fullpath = NULL;
 
 /* NB: ignoring errors, so one malformed config doesn't
kill the whole process */
@@ -648,11 +648,11 @@ qemuStateInitialize(bool privileged,
 virStateInhibitCallback callback,
 void *opaque)
 {
-VIR_AUTOFREE(char *) driverConf = NULL;
+g_autofree char *driverConf = NULL;
 virQEMUDriverConfigPtr cfg;
 uid_t run_uid = -1;
 gid_t run_gid = -1;
-VIR_AUTOFREE(char *) memoryBackingPath = NULL;
+g_autofree char *memoryBackingPath = NULL;
 bool autostart = true;
 size_t i;
 
@@ -816,7 +816,7 @@ qemuStateInitialize(bool privileged,
 goto error;
 
 if (privileged) {
-VIR_AUTOFREE(char *) channeldir = NULL;
+g_autofree char *channeldir = NULL;
 
 if (chown(cfg->libDir, cfg->user, cfg->group) < 0) {
 virReportSystemError(errno,
@@ -921,7 +921,7 @@ qemuStateInitialize(bool privileged,
  * it, since we can't assume the root mount point has permissions that
  * will let our spawned QEMU instances use it. */
 for (i = 0; i < cfg->nhugetlbfs; i++) {
-VIR_AUTOFREE(char *) hugepagePath = NULL;
+g_autofree char *hugepagePath = NULL;
 
 hugepagePath = qemuGetBaseHugepagePath(>hugetlbfs[i]);
 
@@ -1048,8 +1048,8 @@ static void qemuNotifyLoadDomain(virDomainObjPtr vm, int 
newVM, void *opaque)
 static int
 qemuStateReload(void)
 {
-VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
-VIR_AUTOUNREF(virCapsPtr) caps = NULL;
+g_autoptr(virQEMUDriverConfig) cfg = NULL;
+g_autoptr(virCaps) caps = 

[libvirt] [PATCH v3 08/10] qemu_driver: use VIR_AUTOFREE() with strings 3/3

2019-10-15 Thread Daniel Henrique Barboza
Using VIR_AUTOFREE() in all strings of qemu_driver.c make the code
a bit tidier and smaller, sparing VIR_FREE() calls and sometimes a
whole 'cleanup' label.

This is the last part of this change.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 99 +++---
 1 file changed, 34 insertions(+), 65 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index f3fccc0843..fd7c8bb1be 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11956,7 +11956,7 @@ qemuDomainMemoryPeek(virDomainPtr dom,
 {
 virQEMUDriverPtr driver = dom->conn->privateData;
 virDomainObjPtr vm;
-char *tmp = NULL;
+VIR_AUTOFREE(char *) tmp = NULL;
 int fd = -1, ret = -1;
 qemuDomainObjPrivatePtr priv;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
@@ -12028,7 +12028,6 @@ qemuDomainMemoryPeek(virDomainPtr dom,
 VIR_FORCE_CLOSE(fd);
 if (tmp)
 unlink(tmp);
-VIR_FREE(tmp);
 virDomainObjEndAPI();
 return ret;
 }
@@ -12194,7 +12193,7 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
 int ret = -1;
 int fd = -1;
 struct stat sb;
-char *buf = NULL;
+VIR_AUTOFREE(char *) buf = NULL;
 ssize_t len;
 
 if ((rc = qemuDomainStorageOpenStat(driver, cfg, vm, src, , ,
@@ -12230,7 +12229,6 @@ qemuStorageLimitsRefresh(virQEMUDriverPtr driver,
 ret = 1;
 
  cleanup:
-VIR_FREE(buf);
 qemuDomainStorageCloseStat(src, );
 return ret;
 }
@@ -12451,7 +12449,7 @@ qemuDomainMigratePrepareTunnel(virConnectPtr dconn,
 {
 virQEMUDriverPtr driver = dconn->privateData;
 virDomainDefPtr def = NULL;
-char *origname = NULL;
+VIR_AUTOFREE(char *) origname = NULL;
 qemuMigrationParamsPtr migParams = NULL;
 int ret = -1;
 
@@ -12486,7 +12484,6 @@ qemuDomainMigratePrepareTunnel(virConnectPtr dconn,
 
  cleanup:
 qemuMigrationParamsFree(migParams);
-VIR_FREE(origname);
 virDomainDefFree(def);
 return ret;
 }
@@ -12508,7 +12505,7 @@ qemuDomainMigratePrepare2(virConnectPtr dconn,
 {
 virQEMUDriverPtr driver = dconn->privateData;
 virDomainDefPtr def = NULL;
-char *origname = NULL;
+VIR_AUTOFREE(char *) origname = NULL;
 qemuMigrationParamsPtr migParams = NULL;
 int ret = -1;
 
@@ -12553,7 +12550,6 @@ qemuDomainMigratePrepare2(virConnectPtr dconn,
 
  cleanup:
 qemuMigrationParamsFree(migParams);
-VIR_FREE(origname);
 virDomainDefFree(def);
 return ret;
 }
@@ -12754,7 +12750,7 @@ qemuDomainMigratePrepare3(virConnectPtr dconn,
 {
 virQEMUDriverPtr driver = dconn->privateData;
 virDomainDefPtr def = NULL;
-char *origname = NULL;
+VIR_AUTOFREE(char *) origname = NULL;
 qemuMigrationParamsPtr migParams = NULL;
 int ret = -1;
 
@@ -12789,7 +12785,6 @@ qemuDomainMigratePrepare3(virConnectPtr dconn,
 
  cleanup:
 qemuMigrationParamsFree(migParams);
-VIR_FREE(origname);
 virDomainDefFree(def);
 return ret;
 }
@@ -12815,7 +12810,7 @@ qemuDomainMigratePrepare3Params(virConnectPtr dconn,
 int nbdPort = 0;
 int nmigrate_disks;
 const char **migrate_disks = NULL;
-char *origname = NULL;
+VIR_AUTOFREE(char *) origname = NULL;
 qemuMigrationParamsPtr migParams = NULL;
 int ret = -1;
 
@@ -12878,7 +12873,6 @@ qemuDomainMigratePrepare3Params(virConnectPtr dconn,
  cleanup:
 qemuMigrationParamsFree(migParams);
 VIR_FREE(migrate_disks);
-VIR_FREE(origname);
 virDomainDefFree(def);
 return ret;
 }
@@ -12898,7 +12892,7 @@ qemuDomainMigratePrepareTunnel3(virConnectPtr dconn,
 {
 virQEMUDriverPtr driver = dconn->privateData;
 virDomainDefPtr def = NULL;
-char *origname = NULL;
+VIR_AUTOFREE(char *) origname = NULL;
 qemuMigrationParamsPtr migParams = NULL;
 int ret = -1;
 
@@ -12927,7 +12921,6 @@ qemuDomainMigratePrepareTunnel3(virConnectPtr dconn,
 
  cleanup:
 qemuMigrationParamsFree(migParams);
-VIR_FREE(origname);
 virDomainDefFree(def);
 return ret;
 }
@@ -12947,7 +12940,7 @@ qemuDomainMigratePrepareTunnel3Params(virConnectPtr 
dconn,
 virDomainDefPtr def = NULL;
 const char *dom_xml = NULL;
 const char *dname = NULL;
-char *origname = NULL;
+VIR_AUTOFREE(char *) origname = NULL;
 qemuMigrationParamsPtr migParams = NULL;
 int ret = -1;
 
@@ -12986,7 +12979,6 @@ qemuDomainMigratePrepareTunnel3Params(virConnectPtr 
dconn,
 
  cleanup:
 qemuMigrationParamsFree(migParams);
-VIR_FREE(origname);
 virDomainDefFree(def);
 return ret;
 }
@@ -13308,7 +13300,7 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev,
 unsigned domain = 0, bus = 0, slot = 0, function = 0;
 int ret = -1;
 virNodeDeviceDefPtr def = NULL;
-char *xml = NULL;
+VIR_AUTOFREE(char *) xml = NULL;
 bool vfio = qemuHostdevHostSupportsPassthroughVFIO();
 virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
 
@@ -13358,7 +13350,6 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr 

[libvirt] [PATCH v3 06/10] qemu_driver: use VIR_AUTOFREE() with strings 1/3

2019-10-15 Thread Daniel Henrique Barboza
Using VIR_AUTOFREE() in all strings of qemu_driver.c make the code
a bit tidier and smaller, sparing VIR_FREE() calls and sometimes a
whole 'cleanup' label.

This is a huge change due to the amount of char * declared in
this file, thus let's split it in 3. This is the first part.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 103 +
 1 file changed, 33 insertions(+), 70 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d95c5c5b81..f887a79ecd 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -381,11 +381,9 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
void *data)
 {
 char *baseDir = (char *)data;
-char *snapDir = NULL;
+VIR_AUTOFREE(char *) snapDir = NULL;
 DIR *dir = NULL;
 struct dirent *entry;
-char *xmlStr;
-char *fullpath;
 virDomainSnapshotDefPtr def = NULL;
 virDomainMomentObjPtr snap = NULL;
 virDomainMomentObjPtr current = NULL;
@@ -420,6 +418,9 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
 goto cleanup;
 
 while ((direrr = virDirRead(dir, , NULL)) > 0) {
+VIR_AUTOFREE(char *) xmlStr = NULL;
+VIR_AUTOFREE(char *) fullpath = NULL;
+
 /* NB: ignoring errors, so one malformed config doesn't
kill the whole process */
 VIR_INFO("Loading snapshot file '%s'", entry->d_name);
@@ -435,7 +436,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
 virReportSystemError(errno,
  _("Failed to read snapshot file %s"),
  fullpath);
-VIR_FREE(fullpath);
 continue;
 }
 
@@ -448,8 +448,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to parse snapshot XML from file '%s'"),
fullpath);
-VIR_FREE(fullpath);
-VIR_FREE(xmlStr);
 continue;
 }
 
@@ -463,9 +461,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
vm->def->name);
 current = snap;
 }
-
-VIR_FREE(fullpath);
-VIR_FREE(xmlStr);
 }
 if (direrr < 0)
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -492,7 +487,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
 ret = 0;
  cleanup:
 VIR_DIR_CLOSE(dir);
-VIR_FREE(snapDir);
 virObjectUnlock(vm);
 return ret;
 }
@@ -503,11 +497,9 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
  void *data)
 {
 char *baseDir = (char *)data;
-char *chkDir = NULL;
+VIR_AUTOFREE(char *) chkDir = NULL;
 DIR *dir = NULL;
 struct dirent *entry;
-char *xmlStr;
-char *fullpath;
 virDomainCheckpointDefPtr def = NULL;
 virDomainMomentObjPtr chk = NULL;
 virDomainMomentObjPtr current = NULL;
@@ -538,6 +530,9 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 goto cleanup;
 
 while ((direrr = virDirRead(dir, , NULL)) > 0) {
+VIR_AUTOFREE(char *) xmlStr = NULL;
+VIR_AUTOFREE(char *) fullpath = NULL;
+
 /* NB: ignoring errors, so one malformed config doesn't
kill the whole process */
 VIR_INFO("Loading checkpoint file '%s'", entry->d_name);
@@ -553,7 +548,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 virReportSystemError(errno,
  _("Failed to read checkpoint file %s"),
  fullpath);
-VIR_FREE(fullpath);
 continue;
 }
 
@@ -566,8 +560,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to parse checkpoint XML from file '%s'"),
fullpath);
-VIR_FREE(fullpath);
-VIR_FREE(xmlStr);
 virObjectUnref(def);
 continue;
 }
@@ -575,9 +567,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 chk = virDomainCheckpointAssignDef(vm->checkpoints, def);
 if (chk == NULL)
 virObjectUnref(def);
-
-VIR_FREE(fullpath);
-VIR_FREE(xmlStr);
 }
 if (direrr < 0)
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -602,7 +591,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
 ret = 0;
  cleanup:
 VIR_DIR_CLOSE(dir);
-VIR_FREE(chkDir);
 virObjectUnlock(vm);
 return ret;
 }
@@ -660,12 +648,11 @@ qemuStateInitialize(bool privileged,
 virStateInhibitCallback callback,
 void *opaque)
 {
-char *driverConf = NULL;
+VIR_AUTOFREE(char *) driverConf = NULL;
 virQEMUDriverConfigPtr cfg;
 uid_t run_uid = -1;
 gid_t run_gid = -1;
-char *hugepagePath = NULL;
-char *memoryBackingPath = NULL;
+VIR_AUTOFREE(char *) memoryBackingPath = NULL;
 bool autostart = true;
 size_t i;
 
@@ 

[libvirt] [PATCH v3 07/10] qemu_driver: use VIR_AUTOFREE() with strings 2/3

2019-10-15 Thread Daniel Henrique Barboza
Using VIR_AUTOFREE() in all strings of qemu_driver.c make the code
a bit tidier and smaller, sparing VIR_FREE() calls and sometimes a
whole 'cleanup' label.

This is a huge change due to the amount of char * declared in
this file, thus let's split it in 3. This is the second part.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 86 +++---
 1 file changed, 30 insertions(+), 56 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index f887a79ecd..f3fccc0843 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4108,11 +4108,11 @@ processWatchdogEvent(virQEMUDriverPtr driver,
 {
 int ret;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
-char *dumpfile = getAutoDumpPath(driver, vm);
+VIR_AUTOFREE(char *) dumpfile = getAutoDumpPath(driver, vm);
 unsigned int flags = VIR_DUMP_MEMORY_ONLY;
 
 if (!dumpfile)
-goto cleanup;
+return;
 
 switch (action) {
 case VIR_DOMAIN_WATCHDOG_ACTION_DUMP:
@@ -4120,7 +4120,7 @@ processWatchdogEvent(virQEMUDriverPtr driver,
QEMU_ASYNC_JOB_DUMP,
VIR_DOMAIN_JOB_OPERATION_DUMP,
flags) < 0) {
-goto cleanup;
+return;
 }
 
 if (virDomainObjCheckActive(vm) < 0)
@@ -4141,14 +4141,11 @@ processWatchdogEvent(virQEMUDriverPtr driver,
"%s", _("Resuming after dump failed"));
 break;
 default:
-goto cleanup;
+return;
 }
 
  endjob:
 qemuDomainObjEndAsyncJob(driver, vm);
-
- cleanup:
-VIR_FREE(dumpfile);
 }
 
 static int
@@ -4158,18 +4155,16 @@ doCoreDumpToAutoDumpPath(virQEMUDriverPtr driver,
 {
 int ret = -1;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
-char *dumpfile = getAutoDumpPath(driver, vm);
+VIR_AUTOFREE(char *) dumpfile = getAutoDumpPath(driver, vm);
 
 if (!dumpfile)
-goto cleanup;
+return -1;
 
 flags |= cfg->autoDumpBypassCache ? VIR_DUMP_BYPASS_CACHE: 0;
 if ((ret = doCoreDump(driver, vm, dumpfile, flags,
   VIR_DOMAIN_CORE_DUMP_FORMAT_RAW)) < 0)
 virReportError(VIR_ERR_OPERATION_FAILED,
"%s", _("Dump failed"));
- cleanup:
-VIR_FREE(dumpfile);
 return ret;
 }
 
@@ -4179,14 +4174,11 @@ qemuProcessGuestPanicEventInfo(virQEMUDriverPtr driver,
virDomainObjPtr vm,
qemuMonitorEventPanicInfoPtr info)
 {
-char *msg = qemuMonitorGuestPanicEventInfoFormatMsg(info);
-char *timestamp = virTimeStringNow();
+VIR_AUTOFREE(char *) msg = qemuMonitorGuestPanicEventInfoFormatMsg(info);
+VIR_AUTOFREE(char *) timestamp = virTimeStringNow();
 
 if (msg && timestamp)
 qemuDomainLogAppendMessage(driver, vm, "%s: panic %s\n", timestamp, 
msg);
-
-VIR_FREE(timestamp);
-VIR_FREE(msg);
 }
 
 
@@ -5082,7 +5074,7 @@ qemuDomainPinVcpuLive(virDomainObjPtr vm,
 virDomainVcpuDefPtr vcpuinfo;
 qemuDomainObjPrivatePtr priv = vm->privateData;
 virCgroupPtr cgroup_vcpu = NULL;
-char *str = NULL;
+VIR_AUTOFREE(char *) str = NULL;
 virObjectEventPtr event = NULL;
 char paramField[VIR_TYPED_PARAM_FIELD_LENGTH] = "";
 virTypedParameterPtr eventParams = NULL;
@@ -5146,7 +5138,6 @@ qemuDomainPinVcpuLive(virDomainObjPtr vm,
  cleanup:
 virBitmapFree(tmpmap);
 virCgroupFree(_vcpu);
-VIR_FREE(str);
 virObjectEventStateQueue(driver->domainEventState, event);
 return ret;
 }
@@ -5287,7 +5278,7 @@ qemuDomainPinEmulator(virDomainPtr dom,
 virBitmapPtr pcpumap = NULL;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 virObjectEventPtr event = NULL;
-char *str = NULL;
+VIR_AUTOFREE(char *) str = NULL;
 virTypedParameterPtr eventParams = NULL;
 int eventNparams = 0;
 int eventMaxparams = 0;
@@ -5376,7 +5367,6 @@ qemuDomainPinEmulator(virDomainPtr dom,
 if (cgroup_emulator)
 virCgroupFree(_emulator);
 virObjectEventStateQueue(driver->domainEventState, event);
-VIR_FREE(str);
 virBitmapFree(pcpumap);
 virDomainObjEndAPI();
 return ret;
@@ -5758,7 +5748,7 @@ qemuDomainPinIOThread(virDomainPtr dom,
 virCgroupPtr cgroup_iothread = NULL;
 virObjectEventPtr event = NULL;
 char paramField[VIR_TYPED_PARAM_FIELD_LENGTH] = "";
-char *str = NULL;
+VIR_AUTOFREE(char *) str = NULL;
 virTypedParameterPtr eventParams = NULL;
 int eventNparams = 0;
 int eventMaxparams = 0;
@@ -5870,7 +5860,6 @@ qemuDomainPinIOThread(virDomainPtr dom,
 if (cgroup_iothread)
 virCgroupFree(_iothread);
 virObjectEventStateQueue(driver->domainEventState, event);
-VIR_FREE(str);
 virBitmapFree(pcpumap);
 virDomainObjEndAPI();
 return ret;
@@ -5882,7 +5871,7 @@ 

[libvirt] [PATCH v3 01/10] qemu_driver: use VIR_AUTOUNREF() with virQEMUDriverConfigPtr 1/3

2019-10-15 Thread Daniel Henrique Barboza
virQEMUDriverConfigPtr can be auto-unref for the great majority
of the uses made in qemu_driver, sparing us a virObjectUnref()
call and sometimes a whole 'cleanup' label.

This patch changes virQEMUDriverConfigPtr declarations to
use VIR_AUTOUNREF(). 'cleanup' labels were deleted when
applicable. Since there are a lot of references to change,
let's do it in 3 steps.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 94 +-
 1 file changed, 29 insertions(+), 65 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 6ce6348593..1bd9609761 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -202,7 +202,7 @@ qemuAutostartDomain(virDomainObjPtr vm,
 {
 virQEMUDriverPtr driver = opaque;
 int flags = 0;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 int ret = -1;
 
 if (cfg->autoStartBypassCache)
@@ -234,7 +234,6 @@ qemuAutostartDomain(virDomainObjPtr vm,
 ret = 0;
  cleanup:
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -308,7 +307,7 @@ qemuSecurityInit(virQEMUDriverPtr driver)
 char **names;
 virSecurityManagerPtr mgr = NULL;
 virSecurityManagerPtr stack = NULL;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 unsigned int flags = 0;
 
 if (cfg->securityDefaultConfined)
@@ -368,7 +367,6 @@ qemuSecurityInit(virQEMUDriverPtr driver)
 }
 
 driver->securityManager = stack;
-virObjectUnref(cfg);
 return 0;
 
  error:
@@ -376,7 +374,6 @@ qemuSecurityInit(virQEMUDriverPtr driver)
_("Failed to initialize security drivers"));
 virObjectUnref(stack);
 virObjectUnref(mgr);
-virObjectUnref(cfg);
 return -1;
 }
 
@@ -1074,7 +1071,7 @@ static void qemuNotifyLoadDomain(virDomainObjPtr vm, int 
newVM, void *opaque)
 static int
 qemuStateReload(void)
 {
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 virCapsPtr caps = NULL;
 
 if (!qemu_driver)
@@ -1090,7 +1087,6 @@ qemuStateReload(void)
caps, qemu_driver->xmlopt,
qemuNotifyLoadDomain, qemu_driver);
  cleanup:
-virObjectUnref(cfg);
 virObjectUnref(caps);
 return 0;
 }
@@ -1112,7 +1108,7 @@ qemuStateStop(void)
 int state;
 virDomainPtr *domains = NULL;
 unsigned int *flags = NULL;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(qemu_driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = 
virQEMUDriverGetConfig(qemu_driver);
 
 if (!(conn = virConnectOpen(cfg->uri)))
 goto cleanup;
@@ -1151,7 +1147,6 @@ qemuStateStop(void)
 }
 VIR_FREE(flags);
 virObjectUnref(conn);
-virObjectUnref(cfg);
 
 return ret;
 }
@@ -1209,20 +1204,16 @@ qemuStateCleanup(void)
 static int
 qemuConnectURIProbe(char **uri)
 {
-virQEMUDriverConfigPtr cfg = NULL;
-int ret = -1;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 
 if (qemu_driver == NULL)
 return 0;
 
 cfg = virQEMUDriverGetConfig(qemu_driver);
 if (VIR_STRDUP(*uri, cfg->uri) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
- cleanup:
-virObjectUnref(cfg);
-return ret;
+return 0;
 }
 
 static virDrvOpenStatus qemuConnectOpen(virConnectPtr conn,
@@ -1887,7 +1878,7 @@ static int qemuDomainSuspend(virDomainPtr dom)
 qemuDomainObjPrivatePtr priv;
 virDomainPausedReason reason;
 int state;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 
 if (!(vm = qemuDomainObjFromDomain(dom)))
 return -1;
@@ -1930,7 +1921,6 @@ static int qemuDomainSuspend(virDomainPtr dom)
  cleanup:
 virDomainObjEndAPI();
 
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -1942,7 +1932,7 @@ static int qemuDomainResume(virDomainPtr dom)
 int ret = -1;
 int state;
 int reason;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 
 if (!(vm = qemuDomainObjFromDomain(dom)))
 return -1;
@@ -1988,7 +1978,6 @@ static int qemuDomainResume(virDomainPtr dom)
 
  cleanup:
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -2369,7 +2358,7 @@ static int qemuDomainSetMemoryFlags(virDomainPtr dom, 
unsigned long newmem,
 virDomainDefPtr def;
 virDomainDefPtr persistentDef;
 int ret = -1, r;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 
 virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
   VIR_DOMAIN_AFFECT_CONFIG |
@@ -2474,7 +2463,6 @@ static int qemuDomainSetMemoryFlags(virDomainPtr dom, 
unsigned long newmem,
 
  cleanup:
 virDomainObjEndAPI();
-virObjectUnref(cfg);
 

[libvirt] [PATCH v3 09/10] qemu_driver: VIR_AUTOFREE on other scalar pointers

2019-10-15 Thread Daniel Henrique Barboza
This patch uses VIR_AUTOFREE on instances of 'unsigned int *'
and 'unsigned long long *' found in qemu_driver.c.

Suggested-by: Erik Skultety 
Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index fd7c8bb1be..6f283d2de3 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1082,7 +1082,7 @@ qemuStateStop(void)
 size_t i;
 int state;
 virDomainPtr *domains = NULL;
-unsigned int *flags = NULL;
+VIR_AUTOFREE(unsigned int *) flags = NULL;
 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = 
virQEMUDriverGetConfig(qemu_driver);
 
 if (!(conn = virConnectOpen(cfg->uri)))
@@ -1120,7 +1120,6 @@ qemuStateStop(void)
 virObjectUnref(domains[i]);
 VIR_FREE(domains);
 }
-VIR_FREE(flags);
 
 return ret;
 }
@@ -20715,7 +20714,7 @@ qemuDomainGetStatsVcpu(virQEMUDriverPtr driver,
 size_t i;
 int ret = -1;
 virVcpuInfoPtr cpuinfo = NULL;
-unsigned long long *cpuwait = NULL;
+VIR_AUTOFREE(unsigned long long *) cpuwait = NULL;
 
 if (virTypedParamListAddUInt(params, virDomainDefGetVcpus(dom->def),
  "vcpu.current") < 0)
@@ -20780,7 +20779,6 @@ qemuDomainGetStatsVcpu(virQEMUDriverPtr driver,
 
  cleanup:
 VIR_FREE(cpuinfo);
-VIR_FREE(cpuwait);
 return ret;
 }
 
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 02/10] qemu_driver: use VIR_AUTOUNREF() with virQEMUDriverConfigPtr 2/3

2019-10-15 Thread Daniel Henrique Barboza
virQEMUDriverConfigPtr can be auto-unref for the great majority
of the uses made in qemu_driver, sparing us a virObjectUnref()
call and sometimes a whole 'cleanup' label.

This patch changes virQEMUDriverConfigPtr declarations to
use VIR_AUTOUNREF(). 'cleanup' labels were deleted when
applicable.

Since there are a lot of references to change, let's do it in
3 steps. This is step 2.

Note: there is a g_autofree GLib macro being used in
qemuDomainUndefineFlags. In this particular instance we're
using g_autoptr() instead of VIR_AUTOUNREF() to not mix-up VIR_*
and GLib macros in that method.

Signed-off-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 81 ++
 1 file changed, 26 insertions(+), 55 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 1bd9609761..9f9443f493 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4571,7 +4571,7 @@ processNicRxFilterChangedEvent(virQEMUDriverPtr driver,
virDomainObjPtr vm,
const char *devAlias)
 {
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 qemuDomainObjPrivatePtr priv = vm->privateData;
 virDomainDeviceDef dev;
 virDomainNetDefPtr def;
@@ -4670,7 +4670,6 @@ processNicRxFilterChangedEvent(virQEMUDriverPtr driver,
  cleanup:
 virNetDevRxFilterFree(hostFilter);
 virNetDevRxFilterFree(guestFilter);
-virObjectUnref(cfg);
 }
 
 
@@ -4680,7 +4679,7 @@ processSerialChangedEvent(virQEMUDriverPtr driver,
   const char *devAlias,
   bool connected)
 {
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 virDomainChrDeviceState newstate;
 virObjectEventPtr event = NULL;
 virDomainDeviceDef dev;
@@ -4713,7 +4712,7 @@ processSerialChangedEvent(virQEMUDriverPtr driver,
 }
 
 if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0)
-goto cleanup;
+return;
 
 if (!virDomainObjIsActive(vm)) {
 VIR_DEBUG("Domain is not running");
@@ -4754,9 +4753,6 @@ processSerialChangedEvent(virQEMUDriverPtr driver,
 
  endjob:
 qemuDomainObjEndJob(driver, vm);
-
- cleanup:
-virObjectUnref(cfg);
 }
 
 
@@ -5029,21 +5025,20 @@ qemuDomainSetVcpusMax(virQEMUDriverPtr driver,
   virDomainDefPtr persistentDef,
   unsigned int nvcpus)
 {
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 unsigned int topologycpus;
-int ret = -1;
 
 if (def) {
 virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
_("maximum vcpu count of a live domain can't be 
modified"));
-goto cleanup;
+return -1;
 }
 
 if (virDomainNumaGetCPUCountTotal(persistentDef->numa) > nvcpus) {
 virReportError(VIR_ERR_INVALID_ARG, "%s",
_("Number of CPUs in  exceeds the desired "
  "maximum vcpu count"));
-goto cleanup;
+return -1;
 }
 
 if (virDomainDefGetVcpusTopology(persistentDef, ) == 0 &&
@@ -5052,23 +5047,19 @@ qemuDomainSetVcpusMax(virQEMUDriverPtr driver,
  * setting may be corrected via this API */
 virReportError(VIR_ERR_INVALID_ARG, "%s",
_("CPU topology doesn't match the desired vcpu count"));
-goto cleanup;
+return -1;
 }
 
 /* ordering information may become invalid, thus clear it */
 virDomainDefVcpuOrderClear(persistentDef);
 
 if (virDomainDefSetVcpusMax(persistentDef, nvcpus, driver->xmlopt) < 0)
-goto cleanup;
+return -1;
 
 if (virDomainSaveConfig(cfg->configDir, driver->caps, persistentDef) < 0)
-goto cleanup;
-
-ret = 0;
+return -1;
 
- cleanup:
-virObjectUnref(cfg);
-return ret;
+return 0;
 }
 
 
@@ -5233,7 +5224,7 @@ qemuDomainPinVcpuFlags(virDomainPtr dom,
 int ret = -1;
 virBitmapPtr pcpumap = NULL;
 virDomainVcpuDefPtr vcpuinfo = NULL;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 
 virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
   VIR_DOMAIN_AFFECT_CONFIG, -1);
@@ -5290,7 +5281,6 @@ qemuDomainPinVcpuFlags(virDomainPtr dom,
  cleanup:
 virDomainObjEndAPI();
 virBitmapFree(pcpumap);
-virObjectUnref(cfg);
 return ret;
 }
 
@@ -5353,7 +5343,7 @@ qemuDomainPinEmulator(virDomainPtr dom,
 int ret = -1;
 qemuDomainObjPrivatePtr priv;
 virBitmapPtr pcpumap = NULL;
-virQEMUDriverConfigPtr cfg = NULL;
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL;
 virObjectEventPtr event = NULL;
 char *str = NULL;
 virTypedParameterPtr 

[libvirt] [PATCH v3 00/10] use VIR_AUTO*/g_auto* all around qemu_driver.c

2019-10-15 Thread Daniel Henrique Barboza
changes from v2:
- rebased with newer master (67e72053c1)
- added an extra patch to convert the existing VIR_AUTO* macros
to g_auto* ones, all at once, to avoid the case where a method
will have both VIR_AUTO* and g_auto* macros at the same time.


Note: the conversion in patch 10 wasn't 100% due to a handful of
methods that I was unable to use g_autoptr. Take for example
the method qemuDomainSaveInternal:
--
qemuDomainObjPrivatePtr priv = vm->privateData;
VIR_AUTOUNREF(virCapsPtr) caps = NULL;
virQEMUSaveDataPtr data = NULL;
VIR_AUTOUNREF(qemuDomainSaveCookiePtr) cookie = NULL;
--

Changing the 'cookie' variable to use g_autoptr() causes an error:

make[3]: Entering directory '/home/danielhb/kvm-project/libvirt/src'
  CC   qemu/libvirt_driver_qemu_impl_la-qemu_driver.lo
In file included from /usr/lib64/glib-2.0/include/glibconfig.h:9,
 from /usr/include/glib-2.0/glib/gtypes.h:32,
 from /usr/include/glib-2.0/glib/galloca.h:32,
 from /usr/include/glib-2.0/glib.h:30,
 from ./internal.h:31,
 from qemu/qemu_agent.h:24,
 from qemu/qemu_driver.c:40:
qemu/qemu_driver.c: In function 'qemuDomainSaveInternal':
qemu/qemu_driver.c:3282:15: error: unknown type name 
'qemuDomainSaveCookie_autoptr'
 3282 | g_autoptr(qemuDomainSaveCookie) cookie = NULL;


I tried doing it with the 'Ptr' in the name, same error:


  CC   qemu/libvirt_driver_qemu_impl_la-qemu_driver.lo
In file included from /usr/lib64/glib-2.0/include/glibconfig.h:9,
 from /usr/include/glib-2.0/glib/gtypes.h:32,
 from /usr/include/glib-2.0/glib/galloca.h:32,
 from /usr/include/glib-2.0/glib.h:30,
 from ./internal.h:31,
 from qemu/qemu_agent.h:24,
 from qemu/qemu_driver.c:40:
qemu/qemu_driver.c: In function 'qemuDomainSaveInternal':
qemu/qemu_driver.c:3282:15: error: unknown type name 
'qemuDomainSaveCookiePtr_autoptr'
 3282 | g_autoptr(qemuDomainSaveCookiePtr) cookie = NULL;


Similar situation happens with qemuDomainSaveImageStartVM and with
qemuSecurityInit methods. They are mentioned in the commit message
of patch 10 for reference. These methods are still using VIR_AUTO*
macros.


changes from v1:
- addressed review concerns made by Erik
- added more cleanups, as suggested by Erik

v2: https://www.redhat.com/archives/libvir-list/2019-September/msg01452.html
v1: https://www.redhat.com/archives/libvir-list/2019-September/msg00719.html

Daniel Henrique Barboza (10):
  qemu_driver: use VIR_AUTOUNREF() with virQEMUDriverConfigPtr 1/3
  qemu_driver: use VIR_AUTOUNREF() with virQEMUDriverConfigPtr 2/3
  qemu_driver: use VIR_AUTOUNREF() with virQEMUDriverConfigPtr 3/3
  qemu_driver: use VIR_AUTOUNREF() with virCapsPtr
  qemu_driver: use VIR_AUTOUNREF() with more pointer types
  qemu_driver: use VIR_AUTOFREE() with strings 1/3
  qemu_driver: use VIR_AUTOFREE() with strings 2/3
  qemu_driver: use VIR_AUTOFREE() with strings 3/3
  qemu_driver: VIR_AUTOFREE on other scalar pointers
  qemu_driver.c: use GLib macros

 src/qemu/qemu_driver.c | 755 ++---
 1 file changed, 259 insertions(+), 496 deletions(-)

-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [RFC 0/1] convert virStorageSource to GObject

2019-10-15 Thread Daniel Henrique Barboza



On 10/15/19 2:15 PM, Daniel P. Berrangé wrote:

On Tue, Oct 15, 2019 at 10:51:39AM -0300, Daniel Henrique Barboza wrote:


On 10/15/19 9:55 AM, Daniel P. Berrangé wrote:

On Tue, Oct 15, 2019 at 09:42:45AM -0300, Daniel Henrique Barboza wrote:

I was hoping to quickly re-send the qemu_driver cleanups I've
sent some time ago, now using Glib. I started by attempting to
change the first VIR_AUTOUNREF() call in qemu_driver.c to
g_autoptr(), which happens to be a virStorageSourcePtr type,
then I realized that it wasn't that simple.

It should be that simple with this commit:

commit 667ff797e8eb8d82f30ab430216a8d2eef6b915a
Author: Daniel P. Berrangé 
Date:   Fri Oct 4 17:14:10 2019 +0100

  src: add support for g_autoptr with virObject instances

we should be able to use g_autoptr for any virObject, without
having to lock-step convert to GObject.

What actual problem did you find ?

I failed to notice this commit. Just tried it again and it worked.

What happened yesterday was that I attempted to do a simple
VIR_AUTOUNREF -> g_autopt replace, faced compile errors and then, since
I didn't notice this commit about, I assumed "I guess I need to convert
this guy to GObject".

In fact, the compile error happened because g_autoptr() does not operate
with a 'Ptr' type - something that I learned only during the conversion
process.

Yeah, you need to drop the 'Ptr' suffix in the type name when
converting to g_autoptr, as it adds the pointer itself.



This is being sent as RFC because x-I am aware that docs/hacking.html
mentions that we shouldn't mix up certain GLib macros with Libvirt
ones, thus I am uncertain of whether I have messed up or not.
'make check' works, did a few sanity checks with libvirtd as
well.

Yes, the need to not mix  g_auto* with VIR_AUTO*, is why I did commit
667ff797e8eb8d82f30ab430216a8d2eef6b915a to let you use g_autoptr
with virObject, without first converting to GObject.

What if there are other object types in the same function  using the VIR
macros?
For example, inside qemu_driver.c: qemuDomainBlockCopyCommon:


     VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg =
virQEMUDriverGetConfig(driver);
     const char *format = NULL;
     bool mirror_reuse = !!(flags & VIR_DOMAIN_BLOCK_COPY_REUSE_EXT);
     bool mirror_shallow = !!(flags & VIR_DOMAIN_BLOCK_COPY_SHALLOW);
     bool existing = mirror_reuse;
     qemuBlockJobDataPtr job = NULL;
     VIR_AUTOUNREF(virStorageSourcePtr) mirror = mirrorsrc;
     bool blockdev = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV);
     bool mirror_initialized = false;
     VIR_AUTOPTR(qemuBlockStorageSourceChainData) data = NULL;
     VIR_AUTOPTR(qemuBlockStorageSourceChainData) crdata = NULL;


Let's say that I change the virStorageSourcePtr up there to

    g_autoptr(virStorageSource) mirror = mirrorsrc;


As long as there are no VIR macros acting in the 'mirror' variable, is it to
use g_autoptr
there even when everyone else is using VIR_AUTO* macros?

You should change all variables in the method at the same time.
Both the VIR_AUTOUNEF calls here can use g_autoptr, as can the
two VIR_AUTOPTR calls.


Thanks for clarifying. I was going to re-send the patches adding GLib
macros instead of VIR_AUTO* ones, which would end up breaking this
rule because these patches are changing stuff in smaller steps.

What I'll end up is to basically re-send them as they are now, but with an
extra patch to change everything to GLib at once. That way we'll stay
compliant every step of the way.




DHB




Regards,
Daniel


--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH 10/10] tools: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 tools/virsh.c  | 7 ++-
 tools/virt-admin.c | 7 ++-
 2 files changed, 4 insertions(+), 10 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 4aae877160..a3553ddd36 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -93,7 +93,7 @@ virshCatchDisconnect(virConnectPtr conn,
 virErrorPtr error;
 char *uri;
 
-error = virSaveLastError();
+virErrorPreserveLast();
 uri = virConnectGetURI(conn);
 
 switch ((virConnectCloseReason) reason) {
@@ -114,10 +114,7 @@ virshCatchDisconnect(virConnectPtr conn,
 vshError(ctl, _(str), NULLSTR(uri));
 VIR_FREE(uri);
 
-if (error) {
-virSetError(error);
-virFreeError(error);
-}
+virErrorRestore();
 disconnected++;
 vshEventDone(ctl);
 }
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index c0cc0999cb..a820e7241d 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -124,7 +124,7 @@ vshAdmCatchDisconnect(virAdmConnectPtr conn G_GNUC_UNUSED,
 if (reason == VIR_CONNECT_CLOSE_REASON_CLIENT)
 return;
 
-error = virSaveLastError();
+virErrorPreserveLast();
 uri = virAdmConnectGetURI(conn);
 
 switch ((virConnectCloseReason) reason) {
@@ -146,10 +146,7 @@ vshAdmCatchDisconnect(virAdmConnectPtr conn G_GNUC_UNUSED,
 vshError(ctl, _(str), NULLSTR(uri));
 VIR_FREE(uri);
 
-if (error) {
-virSetError(error);
-virFreeError(error);
-}
+virErrorRestore();
 }
 
 static int
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 05/10] qemu: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/qemu/qemu_cgroup.c   | 14 +++---
 src/qemu/qemu_command.c  | 12 ++---
 src/qemu/qemu_domain.c   |  7 +--
 src/qemu/qemu_driver.c   | 36 ++
 src/qemu/qemu_migration.c| 85 +++-
 src/qemu/qemu_migration_params.c |  9 ++--
 src/qemu/qemu_monitor.c  | 19 +++
 src/qemu/qemu_process.c  |  7 +--
 8 files changed, 72 insertions(+), 117 deletions(-)

diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index f1776a7c0b..8b915d124c 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -1149,12 +1149,11 @@ qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
 
  error:
 if (period) {
-virErrorPtr saved = virSaveLastError();
+virErrorPtr saved;
+
+virErrorPreserveLast();
 ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
-if (saved) {
-virSetError(saved);
-virFreeError(saved);
-}
+virErrorRestore();
 }
 
 return -1;
@@ -1362,10 +1361,9 @@ 
qemuCgroupEmulatorAllNodesRestore(qemuCgroupEmulatorAllNodesDataPtr data)
 if (!data)
 return;
 
-err = virSaveLastError();
+virErrorPreserveLast();
 virCgroupSetCpusetMems(data->emulatorCgroup, data->emulatorMemMask);
-virSetError(err);
-virFreeError(err);
+virErrorRestore();
 
 qemuCgroupEmulatorAllNodesDataFree(data);
 }
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index c2a19689c5..2e5e2a75bf 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -8644,10 +8644,11 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver,
 ret = 0;
  cleanup:
 if (ret < 0) {
-virErrorPtr saved_err = virSaveLastError();
+virErrorPtr saved_err;
+
+virErrorPreserveLast(_err);
 virDomainConfNWFilterTeardown(net);
-virSetError(saved_err);
-virFreeError(saved_err);
+virErrorRestore(_err);
 }
 for (i = 0; vhostfd && i < vhostfdSize; i++) {
 if (ret < 0)
@@ -8730,11 +8731,10 @@ qemuBuildNetCommandLine(virQEMUDriverPtr driver,
  error:
 /* free up any resources in the network driver
  * but don't overwrite the original error */
-originalError = virSaveLastError();
+virErrorPreserveLast();
 for (i = 0; last_good_net != -1 && i <= last_good_net; i++)
 virDomainConfNWFilterTeardown(def->nets[i]);
-virSetError(originalError);
-virFreeError(originalError);
+virErrorRestore();
 return -1;
 }
 
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index c638077aa8..43aec2e3d7 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -9174,7 +9174,7 @@ void qemuDomainObjTaint(virQEMUDriverPtr driver,
 /* We don't care about errors logging taint info, so
  * preserve original error, and clear any error that
  * is raised */
-orig_err = virSaveLastError();
+virErrorPreserveLast(_err);
 
 if (!(timestamp = virTimeStringNow()))
 goto cleanup;
@@ -9198,10 +9198,7 @@ void qemuDomainObjTaint(virQEMUDriverPtr driver,
 
  cleanup:
 VIR_FREE(timestamp);
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 }
 
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 6ce6348593..c662676be3 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3436,7 +3436,8 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver,
  endjob:
 if (ret < 0) {
 if (was_running && virDomainObjIsActive(vm)) {
-virErrorPtr save_err = virSaveLastError();
+virErrorPtr save_err;
+virErrorPreserveLast(_err);
 if (qemuProcessStartCPUs(driver, vm,
  VIR_DOMAIN_RUNNING_SAVE_CANCELED,
  QEMU_ASYNC_JOB_SAVE) < 0) {
@@ -3446,8 +3447,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver,
  VIR_DOMAIN_EVENT_SUSPENDED,
  
VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR));
 }
-virSetError(save_err);
-virFreeError(save_err);
+virErrorRestore(_err);
 }
 }
 qemuDomainObjEndAsyncJob(driver, vm);
@@ -6729,7 +6729,9 @@ qemuDomainSaveImageUpdateDef(virQEMUDriverPtr driver,
 goto cleanup;
 
 if (!virDomainDefCheckABIStability(def, newdef_migr, driver->xmlopt)) {
-virErrorPtr err = virSaveLastError();
+virErrorPtr save_err;
+
+virErrorPreserveLast(_err);
 
 /* Due to a bug in older 

[libvirt] [PATCH 00/10] Clean up error preservation/restoration

2019-10-15 Thread John Ferlan
I was going through some old git trees and found this one started before
I 'altered' my Red Hat career path. Seeing as no one has picked it up from
https://wiki.libvirt.org/page/BiteSizedTasks - figured I may as well post
it rather than throw it away ;-)

The change essentially alters virSaveLastError ... virSetError and
virFreeError paths to use virErrorPreserveLast & virErrorRestore.
 

John Ferlan (10):
  conf: Use consistent error preservation and restoration calls
  src: Use consistent error preservation and restoration calls
  libxl: Use consistent error preservation and restoration calls
  lxc: Use consistent error preservation and restoration calls
  qemu: Use consistent error preservation and restoration calls
  remote: Use consistent error preservation and restoration calls
  storage: Use consistent error preservation and restoration calls
  util: Use consistent error preservation and restoration calls
  vz: Use consistent error preservation and restoration calls
  tools: Use consistent error preservation and restoration calls

 src/conf/domain_conf.c|  7 +--
 src/libvirt-stream.c  | 36 +---
 src/libxl/libxl_migration.c   | 18 ++
 src/lxc/lxc_driver.c  |  9 ++-
 src/lxc/lxc_process.c |  7 +--
 src/qemu/qemu_cgroup.c| 14 ++---
 src/qemu/qemu_command.c   | 12 ++--
 src/qemu/qemu_domain.c|  7 +--
 src/qemu/qemu_driver.c| 36 +---
 src/qemu/qemu_migration.c | 85 +--
 src/qemu/qemu_migration_params.c  |  9 ++-
 src/qemu/qemu_monitor.c   | 19 +++---
 src/qemu/qemu_process.c   |  7 +--
 src/remote/remote_daemon_stream.c | 17 +++---
 src/storage/storage_backend_disk.c|  5 +-
 src/storage/storage_backend_logical.c |  5 +-
 src/storage/storage_driver.c  |  8 +--
 src/util/vircgroup.c  | 18 +++---
 src/util/virfirewall.c|  6 +-
 src/vz/vz_driver.c|  9 +--
 tools/virsh.c |  7 +--
 tools/virt-admin.c|  7 +--
 22 files changed, 137 insertions(+), 211 deletions(-)

-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 09/10] vz: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/vz/vz_driver.c | 9 +++--
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c
index 917acdae57..b4049261ae 100644
--- a/src/vz/vz_driver.c
+++ b/src/vz/vz_driver.c
@@ -3206,7 +3206,7 @@ vzDomainMigratePerformP2P(virDomainObjPtr dom,
  */
 if (uri && virTypedParamsReplaceString(, ,
VIR_MIGRATE_PARAM_URI, uri) < 0) {
-orig_err = virSaveLastError();
+virErrorPreserveLast(_err);
 goto finish;
 }
 
@@ -3216,7 +3216,7 @@ vzDomainMigratePerformP2P(virDomainObjPtr dom,
 cookieoutlen = 0;
 if (vzDomainMigratePerformStep(dom, driver, params, nparams, cookiein,
cookieinlen, flags) < 0) {
-orig_err = virSaveLastError();
+virErrorPreserveLast(_err);
 goto finish;
 }
 
@@ -3242,10 +3242,7 @@ vzDomainMigratePerformP2P(virDomainObjPtr dom,
 /* confirm step is NOOP thus no need to call it */
 
  done:
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 VIR_FREE(dom_xml);
 VIR_FREE(uri);
 VIR_FREE(cookiein);
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 08/10] util: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/util/vircgroup.c   | 18 --
 src/util/virfirewall.c |  6 --
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index c6e4cf2dde..4c83e37ec4 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1168,13 +1168,12 @@ virCgroupNewMachineSystemd(const char *name,
 }
 
 if (virCgroupAddProcess(*group, pidleader) < 0) {
-virErrorPtr saved = virSaveLastError();
+virErrorPtr saved;
+
+virErrorPreserveLast();
 virCgroupRemove(*group);
 virCgroupFree(group);
-if (saved) {
-virSetError(saved);
-virFreeError(saved);
-}
+virErrorRestore();
 }
 
 return 0;
@@ -1220,13 +1219,12 @@ virCgroupNewMachineManual(const char *name,
 goto cleanup;
 
 if (virCgroupAddProcess(*group, pidleader) < 0) {
-virErrorPtr saved = virSaveLastError();
+virErrorPtr saved;
+
+virErrorPreserveLast();
 virCgroupRemove(*group);
 virCgroupFree(group);
-if (saved) {
-virSetError(saved);
-virFreeError(saved);
-}
+virErrorRestore();
 }
 
  done:
diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
index 0d4bfae8f8..62f404afd6 100644
--- a/src/util/virfirewall.c
+++ b/src/util/virfirewall.c
@@ -838,7 +838,9 @@ virFirewallApply(virFirewallPtr firewall)
 if (virFirewallApplyGroup(firewall, i) < 0) {
 VIR_DEBUG("Rolling back groups up to %zu for %p", i, firewall);
 size_t first = i;
-VIR_AUTOPTR(virError) saved_error = virSaveLastError();
+virErrorPtr saved_error;
+
+virErrorPreserveLast(_error);
 
 /*
  * Look at any inheritance markers to figure out
@@ -858,7 +860,7 @@ virFirewallApply(virFirewallPtr firewall)
 virFirewallRollbackGroup(firewall, j);
 }
 
-virSetError(saved_error);
+virErrorRestore(_error);
 VIR_DEBUG("Done rolling back groups for %p", firewall);
 goto cleanup;
 }
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 07/10] storage: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/storage/storage_backend_disk.c| 5 ++---
 src/storage/storage_backend_logical.c | 5 ++---
 src/storage/storage_driver.c  | 8 +++-
 3 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/src/storage/storage_backend_disk.c 
b/src/storage/storage_backend_disk.c
index 5bf704bcc8..d2f7b94224 100644
--- a/src/storage/storage_backend_disk.c
+++ b/src/storage/storage_backend_disk.c
@@ -918,10 +918,9 @@ virStorageBackendDiskCreateVol(virStoragePoolObjPtr pool,
 /* Best effort to remove the partition. Ignore any errors
  * since we could be calling this with vol->target.path == NULL
  */
-save_err = virSaveLastError();
+virErrorPreserveLast(_err);
 ignore_value(virStorageBackendDiskDeleteVol(pool, vol, 0));
-virSetError(save_err);
-virFreeError(save_err);
+virErrorRestore(_err);
 return -1;
 }
 
diff --git a/src/storage/storage_backend_logical.c 
b/src/storage/storage_backend_logical.c
index 6e468b3579..bfedb06a91 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -969,10 +969,9 @@ virStorageBackendLogicalCreateVol(virStoragePoolObjPtr 
pool,
 return 0;
 
  error:
-err = virSaveLastError();
+virErrorPreserveLast();
 virStorageBackendLogicalDeleteVol(pool, vol, 0);
-virSetError(err);
-virFreeError(err);
+virErrorRestore();
 return -1;
 }
 
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 6516b0943d..e8551ba57e 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -81,18 +81,16 @@ storagePoolRefreshFailCleanup(virStorageBackendPtr backend,
   virStoragePoolObjPtr obj,
   const char *stateFile)
 {
-virErrorPtr orig_err = virSaveLastError();
+virErrorPtr orig_err;
 
+virErrorPreserveLast(_err);
 virStoragePoolObjClearVols(obj);
 
 if (stateFile)
 unlink(stateFile);
 if (backend->stopPool)
 backend->stopPool(obj);
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 }
 
 
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 04/10] lxc: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/lxc/lxc_driver.c  | 9 -
 src/lxc/lxc_process.c | 7 ++-
 2 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 5c7a9140b2..204a3ed522 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -1857,12 +1857,11 @@ static int lxcSetVcpuBWLive(virCgroupPtr cgroup, 
unsigned long long period,
 
  error:
 if (period) {
-virErrorPtr saved = virSaveLastError();
+virErrorPtr saved;
+
+virErrorPreserveLast();
 virCgroupSetCpuCfsPeriod(cgroup, old_period);
-if (saved) {
-virSetError(saved);
-virFreeError(saved);
-}
+virErrorRestore();
 }
 
 return -1;
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index da0b055b85..d9939f102d 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -1568,7 +1568,7 @@ int virLXCProcessStart(virConnectPtr conn,
 rc = -1;
 }
 if (rc != 0) {
-err = virSaveLastError();
+virErrorPreserveLast();
 virLXCProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED);
 }
 virCommandFree(cmd);
@@ -1582,10 +1582,7 @@ int virLXCProcessStart(virConnectPtr conn,
 virObjectUnref(cfg);
 virObjectUnref(caps);
 
-if (err) {
-virSetError(err);
-virFreeError(err);
-}
+virErrorRestore();
 
 return rc;
 }
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 03/10] libxl: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/libxl/libxl_migration.c | 18 ++
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/src/libxl/libxl_migration.c b/src/libxl/libxl_migration.c
index 8a41e9374d..770a300316 100644
--- a/src/libxl/libxl_migration.c
+++ b/src/libxl/libxl_migration.c
@@ -1049,7 +1049,7 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver,
 if (uri_out) {
 if (virTypedParamsReplaceString(, ,
 VIR_MIGRATE_PARAM_URI, uri_out) < 
0) {
-orig_err = virSaveLastError();
+virErrorPreserveLast(_err);
 goto finish;
 }
 } else {
@@ -1067,7 +1067,7 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver,
  uri_out, NULL, flags);
 if (ret < 0) {
 notify_source = false;
-orig_err = virSaveLastError();
+virErrorPreserveLast(_err);
 }
 
 cancelled = (ret < 0);
@@ -1094,7 +1094,7 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver,
  * one we need to preserve it in case confirm3 overwrites
  */
 if (!orig_err)
-orig_err = virSaveLastError();
+virErrorPreserveLast(_err);
 
  confirm:
 if (notify_source) {
@@ -1119,10 +1119,7 @@ libxlDoMigrateSrcP2P(libxlDriverPrivatePtr driver,
 ret = -1;
 }
 
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 
 VIR_FREE(cookieout);
 VIR_FREE(dom_xml);
@@ -1200,15 +1197,12 @@ libxlDomainMigrationSrcPerformP2P(libxlDriverPrivatePtr 
driver,
 }
 
  cleanup:
-orig_err = virSaveLastError();
+virErrorPreserveLast(_err);
 virObjectUnlock(vm);
 virObjectUnref(dconn);
 virObjectUnref(cfg);
 virObjectLock(vm);
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 return ret;
 }
 
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 06/10] remote: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/remote/remote_daemon_stream.c | 17 +
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/remote/remote_daemon_stream.c 
b/src/remote/remote_daemon_stream.c
index d7fcb1bf42..c4f14a27ef 100644
--- a/src/remote/remote_daemon_stream.c
+++ b/src/remote/remote_daemon_stream.c
@@ -229,15 +229,18 @@ daemonStreamEvent(virStreamPtr st, int events, void 
*opaque)
 int ret;
 virNetMessagePtr msg;
 virNetMessageError rerr;
-virErrorPtr origErr = virSaveLastError();
+virErrorPtr origErr;
+
+virErrorPreserveLast();
 
 memset(, 0, sizeof(rerr));
 stream->closed = true;
 virStreamEventRemoveCallback(stream->st);
 virStreamAbort(stream->st);
 if (origErr && origErr->code != VIR_ERR_OK) {
-virSetError(origErr);
+virErrorRestore();
 } else {
+virFreeError(origErr);
 if (events & VIR_STREAM_EVENT_HANGUP)
 virReportError(VIR_ERR_RPC,
"%s", _("stream had unexpected termination"));
@@ -245,7 +248,6 @@ daemonStreamEvent(virStreamPtr st, int events, void *opaque)
 virReportError(VIR_ERR_RPC,
"%s", _("stream had I/O failure"));
 }
-virFreeError(origErr);
 
 msg = virNetMessageNew(false);
 if (!msg) {
@@ -549,7 +551,9 @@ daemonStreamHandleWriteData(virNetServerClientPtr client,
 return 1;
 } else if (ret < 0) {
 virNetMessageError rerr;
-virErrorPtr err = virSaveLastError();
+virErrorPtr err;
+
+virErrorPreserveLast();
 
 memset(, 0, sizeof(rerr));
 
@@ -558,10 +562,7 @@ daemonStreamHandleWriteData(virNetServerClientPtr client,
 virStreamEventRemoveCallback(stream->st);
 virStreamAbort(stream->st);
 
-if (err) {
-virSetError(err);
-virFreeError(err);
-}
+virErrorRestore();
 
 return virNetServerProgramSendReplyError(stream->prog,
  client,
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 01/10] conf: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/conf/domain_conf.c | 7 ++-
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 10d6bf0eea..188ea80c3d 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -23624,17 +23624,14 @@ virDomainDefCheckABIStabilityFlags(virDomainDefPtr 
src,
 return true;
 
  error:
-err = virSaveLastError();
+virErrorPreserveLast();
 
 strSrc = virDomainDefFormat(src, NULL, 0);
 strDst = virDomainDefFormat(dst, NULL, 0);
 VIR_DEBUG("XMLs that failed stability check were: src=\"%s\", dst=\"%s\"",
   NULLSTR(strSrc), NULLSTR(strDst));
 
-if (err) {
-virSetError(err);
-virFreeError(err);
-}
+virErrorRestore();
 return false;
 }
 
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 02/10] src: Use consistent error preservation and restoration calls

2019-10-15 Thread John Ferlan
Provide some consistency over error message variable name and usage
when saving error messages across possible other errors or possibility
of resetting of the last error.

Instead of virSaveLastError paired up with virSetError and virFreeError,
we should use the newer virErrorPreserveLast and virRestoreError.

Signed-off-by: John Ferlan 
---
 src/libvirt-stream.c | 36 
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/src/libvirt-stream.c b/src/libvirt-stream.c
index ef83696bcd..fb646d0aef 100644
--- a/src/libvirt-stream.c
+++ b/src/libvirt-stream.c
@@ -622,12 +622,11 @@ virStreamSendAll(virStreamPtr stream,
 VIR_FREE(bytes);
 
 if (ret != 0) {
-virErrorPtr orig_err = virSaveLastError();
+virErrorPtr orig_err;
+
+virErrorPreserveLast(_err);
 virStreamAbort(stream);
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 virDispatchError(stream->conn);
 }
 
@@ -794,12 +793,11 @@ int virStreamSparseSendAll(virStreamPtr stream,
 VIR_FREE(bytes);
 
 if (ret != 0) {
-virErrorPtr orig_err = virSaveLastError();
+virErrorPtr orig_err;
+
+virErrorPreserveLast(_err);
 virStreamAbort(stream);
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 virDispatchError(stream->conn);
 }
 
@@ -900,12 +898,11 @@ virStreamRecvAll(virStreamPtr stream,
 VIR_FREE(bytes);
 
 if (ret != 0) {
-virErrorPtr orig_err = virSaveLastError();
+virErrorPtr orig_err;
+
+virErrorPreserveLast(_err);
 virStreamAbort(stream);
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 virDispatchError(stream->conn);
 }
 
@@ -1034,12 +1031,11 @@ virStreamSparseRecvAll(virStreamPtr stream,
 VIR_FREE(bytes);
 
 if (ret != 0) {
-virErrorPtr orig_err = virSaveLastError();
+virErrorPtr orig_err;
+
+virErrorPreserveLast(_err);
 virStreamAbort(stream);
-if (orig_err) {
-virSetError(orig_err);
-virFreeError(orig_err);
-}
+virErrorRestore(_err);
 virDispatchError(stream->conn);
 }
 
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 7/8] examples: Use G_N_ELEMENTS instead of ARRAY_CARDINALITY

2019-10-15 Thread Daniel P . Berrangé
On Tue, Oct 15, 2019 at 04:10:55PM +0200, Andrea Bolognani wrote:
> On Tue, 2019-10-15 at 14:21 +0200, Ján Tomko wrote:
> > We try to keep the example programs independent of libraries
> > other than libvirt.
> > 
> > Rename the locally defined ARRAY_CARDINALITY macro to G_N_ELEMENTS
> > which GLib provides, even though we don't actually include GLib.
> 
> [...]
> 
> > -#define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array)))
> > +#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
> 
> In this case, and in this case only, I think we should leave the name
> alone.
> 
> When we use GLib and simply provide our own implementation when using
> a version that's too old, the GLib name is obviously accurate, but in
> this specific case I think it's misleading and we should stick with
> the current one.

We do have the option to have our example programs use GLib instead
of plain C. If people are using libvirt from plain C, I'm inclined
to suggest they'd be wise to use GLib for the same reasons that it
is beneficial for libvirt to use GLib.

If they're not using GLib, they likely have enough C knowledge to
figure out what they must do from our examples anyway.

Regards,
Daniel
-- 
|: 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 :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [RFC 0/1] convert virStorageSource to GObject

2019-10-15 Thread Daniel P . Berrangé
On Tue, Oct 15, 2019 at 10:51:39AM -0300, Daniel Henrique Barboza wrote:
> 
> 
> On 10/15/19 9:55 AM, Daniel P. Berrangé wrote:
> > On Tue, Oct 15, 2019 at 09:42:45AM -0300, Daniel Henrique Barboza wrote:
> > > I was hoping to quickly re-send the qemu_driver cleanups I've
> > > sent some time ago, now using Glib. I started by attempting to
> > > change the first VIR_AUTOUNREF() call in qemu_driver.c to
> > > g_autoptr(), which happens to be a virStorageSourcePtr type,
> > > then I realized that it wasn't that simple.
> > It should be that simple with this commit:
> > 
> >commit 667ff797e8eb8d82f30ab430216a8d2eef6b915a
> >Author: Daniel P. Berrangé 
> >Date:   Fri Oct 4 17:14:10 2019 +0100
> > 
> >  src: add support for g_autoptr with virObject instances
> > 
> > we should be able to use g_autoptr for any virObject, without
> > having to lock-step convert to GObject.
> > 
> > What actual problem did you find ?
> 
> I failed to notice this commit. Just tried it again and it worked.
> 
> What happened yesterday was that I attempted to do a simple
> VIR_AUTOUNREF -> g_autopt replace, faced compile errors and then, since
> I didn't notice this commit about, I assumed "I guess I need to convert
> this guy to GObject".
> 
> In fact, the compile error happened because g_autoptr() does not operate
> with a 'Ptr' type - something that I learned only during the conversion
> process.

Yeah, you need to drop the 'Ptr' suffix in the type name when
converting to g_autoptr, as it adds the pointer itself.


> > > This is being sent as RFC because x-I am aware that docs/hacking.html
> > > mentions that we shouldn't mix up certain GLib macros with Libvirt
> > > ones, thus I am uncertain of whether I have messed up or not.
> > > 'make check' works, did a few sanity checks with libvirtd as
> > > well.
> > Yes, the need to not mix  g_auto* with VIR_AUTO*, is why I did commit
> > 667ff797e8eb8d82f30ab430216a8d2eef6b915a to let you use g_autoptr
> > with virObject, without first converting to GObject.
> 
> What if there are other object types in the same function  using the VIR
> macros?
> For example, inside qemu_driver.c: qemuDomainBlockCopyCommon:
> 
> 
>     VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg =
> virQEMUDriverGetConfig(driver);
>     const char *format = NULL;
>     bool mirror_reuse = !!(flags & VIR_DOMAIN_BLOCK_COPY_REUSE_EXT);
>     bool mirror_shallow = !!(flags & VIR_DOMAIN_BLOCK_COPY_SHALLOW);
>     bool existing = mirror_reuse;
>     qemuBlockJobDataPtr job = NULL;
>     VIR_AUTOUNREF(virStorageSourcePtr) mirror = mirrorsrc;
>     bool blockdev = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV);
>     bool mirror_initialized = false;
>     VIR_AUTOPTR(qemuBlockStorageSourceChainData) data = NULL;
>     VIR_AUTOPTR(qemuBlockStorageSourceChainData) crdata = NULL;
> 
> 
> Let's say that I change the virStorageSourcePtr up there to
> 
>    g_autoptr(virStorageSource) mirror = mirrorsrc;
> 
> 
> As long as there are no VIR macros acting in the 'mirror' variable, is it to
> use g_autoptr
> there even when everyone else is using VIR_AUTO* macros?

You should change all variables in the method at the same time.
Both the VIR_AUTOUNEF calls here can use g_autoptr, as can the
two VIR_AUTOPTR calls.

Regards,
Daniel
-- 
|: 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 :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH v2 4/6] qemu: consolidate video validation

2019-10-15 Thread Cole Robinson
On 10/11/19 5:27 PM, Jonathon Jongsma wrote:
> Move video validation logic from qemuProcessStartValidateVideo() to
> qemuDomainDeviceDefValidateVideo() (which is in fact called from the
> aforementioned function).
> 

As mentioned in the other response, this patch adds a lot of test suite
breakage. By moving more validation from process start time to XML parse
time, it exposes that many test suite test cases do not have the full
list of capabilities specified that it would need to actually boot the
XML under test.

Most of the example failures are in qemuxml2xml test. Run
VIR_TEST_DEBUG=1 ./tests/qemuxml2xmltest to see each failure and the
associated error message. Most of these cases will just be adding the
associated CIRRUS or VGA qemuCaps to the list in qemuxml2xmltest.c. You
can also look at the same test case in qemuxml2argvtest.c and copy the
caps list from there.

Also this patch should be broken up even more IMO. Some ideas:

* Add a qemuDomainDeviceDefValidateVideoModel from the existing code
qemu_domain.c code, which should be a no-op
* Extend it to handle the vhostuser validation
* Move the video->accel validation to qemuDomainDeviceDefValidateVideo.
it may need to grow an extra check for backend != vhostuser because we
will be de-nesting it
* Move the remaining model validation to
qemuDomainDeviceDefValidateVideoModel

Some of that will also help split up the test suite breakage, rather
than requiring you to adjust them all at once in one patch.

Thanks,
Cole

> Signed-off-by: Jonathon Jongsma 
> ---
>  src/qemu/qemu_domain.c  | 172 +---
>  src/qemu/qemu_process.c |  62 ---
>  2 files changed, 107 insertions(+), 127 deletions(-)
> 
> diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
> index bc455e7da3..def90a0f7d 100644
> --- a/src/qemu/qemu_domain.c
> +++ b/src/qemu/qemu_domain.c
> @@ -5706,83 +5706,125 @@ qemuDomainDeviceDefValidateHostdev(const 
> virDomainHostdevDef *hostdev,
>  
>  
>  static int
> -qemuDomainDeviceDefValidateVideo(const virDomainVideoDef *video)
> +qemuDomainDeviceDefValidateVideo(const virDomainVideoDef *video,
> + virQEMUCapsPtr qemuCaps)
>  {
> -switch ((virDomainVideoType) video->type) {
> -case VIR_DOMAIN_VIDEO_TYPE_NONE:
> -return 0;
> -case VIR_DOMAIN_VIDEO_TYPE_XEN:
> -case VIR_DOMAIN_VIDEO_TYPE_VBOX:
> -case VIR_DOMAIN_VIDEO_TYPE_PARALLELS:
> -case VIR_DOMAIN_VIDEO_TYPE_GOP:
> -case VIR_DOMAIN_VIDEO_TYPE_DEFAULT:
> -virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> -   _("video type '%s' is not supported with QEMU"),
> -   virDomainVideoTypeToString(video->type));
> -return -1;
> -case VIR_DOMAIN_VIDEO_TYPE_VGA:
> -case VIR_DOMAIN_VIDEO_TYPE_CIRRUS:
> -case VIR_DOMAIN_VIDEO_TYPE_VMVGA:
> -case VIR_DOMAIN_VIDEO_TYPE_QXL:
> -case VIR_DOMAIN_VIDEO_TYPE_VIRTIO:
> -case VIR_DOMAIN_VIDEO_TYPE_BOCHS:
> -case VIR_DOMAIN_VIDEO_TYPE_RAMFB:
> -case VIR_DOMAIN_VIDEO_TYPE_LAST:
> -break;
> -}
> -
> -if (!video->primary &&
> -video->type != VIR_DOMAIN_VIDEO_TYPE_QXL &&
> -video->type != VIR_DOMAIN_VIDEO_TYPE_VIRTIO) {
> -virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> -   _("video type '%s' is only valid as primary "
> - "video device"),
> -   virDomainVideoTypeToString(video->type));
> -return -1;
> -}
> -
> -if (video->accel && video->accel->accel2d == VIR_TRISTATE_SWITCH_ON) {
> -virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
> -   _("qemu does not support the accel2d setting"));
> -return -1;
> -}
> -
> -if (video->type == VIR_DOMAIN_VIDEO_TYPE_QXL) {
> -if (video->vram > (UINT_MAX / 1024)) {
> -virReportError(VIR_ERR_OVERFLOW,
> -   _("value for 'vram' must be less than '%u'"),
> -   UINT_MAX / 1024);
> +if (video->backend == VIR_DOMAIN_VIDEO_BACKEND_TYPE_VHOSTUSER) {
> +if (video->type == VIR_DOMAIN_VIDEO_TYPE_VIRTIO &&
> +!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VHOST_USER_GPU)) {
> +virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
> +   _("this QEMU does not support 'vhost-user' video 
> device"));
> +return -1;
> +}
> +} else {
> +switch ((virDomainVideoType) video->type) {
> +case VIR_DOMAIN_VIDEO_TYPE_NONE:
> +return 0;
> +case VIR_DOMAIN_VIDEO_TYPE_XEN:
> +case VIR_DOMAIN_VIDEO_TYPE_VBOX:
> +case VIR_DOMAIN_VIDEO_TYPE_PARALLELS:
> +case VIR_DOMAIN_VIDEO_TYPE_GOP:
> +case VIR_DOMAIN_VIDEO_TYPE_DEFAULT:
> +virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> +   _("video type '%s' is not supported with 
> QEMU"),
> +

Re: [libvirt] [PATCH 00/30] storagefile, security: qcow2 data_file support

2019-10-15 Thread Cole Robinson
On 10/15/19 3:56 AM, Han Han wrote:
> Hello Cole, one issue is found:
> The qcow2 data file XTTRs is not cleaned on external snapshot when
> -blockdev is not enabled
> 
> Versions:
> libvirt v5.8.0-134-g9d03e9adf1
> qemu-kvm-4.1.0-13.module+el8.1.0+4313+ef76ec61.x86_64
> 
> Steps:
> 1. Convert a OS image to qcow2 data file:
> # qemu-img convert -O qcow2 -o
> data_file=/var/lib/libvirt/images/pc-data.raw,data_file_raw=on
> /var/lib/libvirt/images/pc.qcow2 /var/lib/libvirt/images/pc-data.qcow2
> 
> 2. Build and start libvirt source, start libvirt daemon:
> # make clean && CC=/usr/lib64/ccache/cc ./autogen.sh&&./configure
> --without-libssh --build=x86_64-redhat-linux-gnu
> --host=x86_64-redhat-linux-gnu --program-prefix=
> --disable-dependency-tracking --prefix=/usr --exec-prefix=/usr
> --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc
> --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64
> --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/var/lib
> --mandir=/usr/share/man --infodir=/usr/share/info --with-qemu
> --without-openvz --without-lxc --without-vbox --without-libxl
> --with-sasl --with-polkit --with-libvirtd --without-phyp --with-esx
> --without-hyperv --without-vmware --without-xenapi --without-vz
> --without-bhyve --with-interface --with-network --with-storage-fs
> --with-storage-lvm --with-storage-iscsi --with-storage-iscsi-direct
> --with-storage-scsi --with-storage-disk --with-storage-mpath
> --with-storage-rbd --without-storage-sheepdog --with-storage-gluster
> --without-storage-zfs --without-storage-vstorage --with-numactl
> --with-numad --with-capng --without-fuse --with-netcf --with-selinux
> --with-selinux-mount=/sys/fs/selinux --without-apparmor --without-hal
> --with-udev --with-yajl --with-sanlock --with-libpcap --with-macvtap
> --with-audit --with-dtrace --with-driver-modules --with-firewalld
> --with-firewalld-zone --without-wireshark-dissector --without-pm-utils
> --with-nss-plugin '--with-packager=Unknown, 2019-08-19-12:13:01,
> lab.rhel8.me ' --with-packager-version=1.el8
> --with-qemu-user=qemu --with-qemu-group=qemu
> --with-tls-priority=@LIBVIRT,SYSTEM --enable-werror
> --enable-expensive-tests --with-init-script=systemd
> --without-login-shell && make -j8
> # LD_PRELOAD="$(find src -name '*.so.*'|tr '\n' ' ')" src/.libs/virtlogd
> # LD_PRELOAD="$(find src -name '*.so.*'|tr '\n' ' ')" LIBVIRT_DEBUG=3
> LIBVIRT_LOG_FILTERS="1:util 1:qemu 1:security"
> LIBVIRT_LOG_OUTPUTS="1:file:/tmp/libvirt_daemon.log" src/.libs/libvirtd
> 
> 3. Define and start an VM with the qcow2 data file. Note that the
> -blockdev is not enabled
> # virsh define pc-data.xml
> # virsh start pc-data
> 
> 4. Create snapshot and check the data file XATTRs:
> # virsh snapshot-create-as pc-data s1 --no-metadata --disk-only
> # getfattr -m - -d /var/lib/libvirt/images/pc-data.raw
> getfattr: Removing leading '/' from absolute path names
> # file: var/lib/libvirt/images/pc-data.raw
> security.selinux="unconfined_u:object_r:svirt_image_t:s0:c775,c1011"
> trusted.libvirt.security.dac="+107:+107"
> trusted.libvirt.security.ref_dac="1"
> trusted.libvirt.security.ref_selinux="1"
> trusted.libvirt.security.selinux="unconfined_u:object_r:svirt_image_t:s0:c284,c367"
> trusted.libvirt.security.timestamp_dac="1563328069"
> trusted.libvirt.security.timestamp_selinux="1563328069"
> 
> Shutdown the VM. The XATTRs of data file is not changed.
> It is not expected. The XTTRs should not contain *.libvirt.*
> 
> Issue is not reproduced with -blockdev enabled:
> 
> ...
>   
>     
>     
>   
> 
> 
> See the libvirt daemon log and vm xml in attachment.

Nice catch! I will need to dig into this to figure out where the issue
is. Can you put this info into an upstream bug report in
product=Virtualization Tools  and I will get to it when I can

Thanks,
Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH v2 2/6] qemu: use VIR_AUTOUNREF in qemuDomainDeviceDefValidate()

2019-10-15 Thread Cole Robinson
On 10/11/19 5:27 PM, Jonathon Jongsma wrote:
> This allows us to simplify the function and avoid jumping to 'cleanup'.
> 
> Signed-off-by: Jonathon Jongsma 
> ---
>  src/qemu/qemu_domain.c | 13 +
>  1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
> index dc7568fe18..516ae7e444 100644
> --- a/src/qemu/qemu_domain.c
> +++ b/src/qemu/qemu_domain.c
> @@ -7197,8 +7197,8 @@ qemuDomainDeviceDefValidate(const virDomainDeviceDef 
> *dev,
>  {
>  int ret = 0;
>  virQEMUDriverPtr driver = opaque;
> -virQEMUCapsPtr qemuCaps = NULL;
> -virDomainCapsPtr domCaps = NULL;
> +VIR_AUTOUNREF(virQEMUCapsPtr) qemuCaps = NULL;
> +VIR_AUTOUNREF(virDomainCapsPtr) domCaps = NULL;
>  
>  if (!(qemuCaps = virQEMUCapsCacheLookup(driver->qemuCapsCache,
>  def->emulator)))
> @@ -7208,13 +7208,13 @@ qemuDomainDeviceDefValidate(const virDomainDeviceDef 
> *dev,
> def->os.machine,
> def->os.arch,
> def->virtType)))
> -goto cleanup;
> +return -1;
> 

This patch actually uncovers a bug in the domcaps caching,
qemumemlocktest is failing. I sent a fix now and CCd you. Previously
even when this function failed, we would return 0; So this patch has a
side effect besides the cleanup.

I think you should move patch #3 before this and have it contain all the
bug fixing here. Then do the VIR_AUTOFREE cleanup on top, which should
be a no-op. Without cleaning up the function, the bug fixing will look a
bit weird:

if (!(qemuCaps = virQEMUBlah())) {
ret = -1;
goto cleanup;
}

That's due to this function being a little funky because it's using
'ret' as both the function return code, and to capture the return value
of functions we are calling. Elsewhere in libvirt we will use a separate
'int rc' to capture return values. Then the code is more like:

int ret = -1;
int rc;

if (foo() < 0)
goto cleanup;

if ((rc = bar()) < 0) {
ret = rc;
goto cleanup;
}

switch()...

ret = rc;
 cleanup:
return ret;


But after the VIR_AUTOFREE cleanup that distinction probably doesn't
matter, so if the bug fix is a bit ugly than IMO that's fine because the
VIR_AUTOFREE conversion will clean it up anyways

But, did you run the test suite on this series? There are the errors I
hit here, but there's also tons of qemuxml2xml breakage with caused by
the video validation move. Please make sure you run 'make check' and
'make syntax-check' on every commit in a series before sending. I'll
reply to the other patches with more info.

Thanks,
Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH] qemu: caps: Use unique key for domCaps caching

2019-10-15 Thread Cole Robinson
When searching qemuCaps->domCapsCache for existing domCaps data,
we check for a matching pair of arch+virttype+machine+emulator. However
for the hash table key we only use the machine string. So if the
cache already contains:

  x86_64 + kvm + pc + /usr/bin/qemu-kvm

But a new VM is defined with

  x86_64 + qemu + pc + /usr/bin/qemu-kvm

We correctly fail to find matching cached domCaps, but then attempt
to use a colliding key with virHashAddEntry

Fix this by building a hash key from the 4 values, not just machine

Signed-off-by: Cole Robinson 
---
qemu_domain.c validation should be affected, but it is covered up
by another bug, fixed here:
https://www.redhat.com/archives/libvir-list/2019-October/msg00708.html

 src/qemu/qemu_conf.c | 11 ++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 08cd784054..64ac8cbdd3 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1396,6 +1396,8 @@ virQEMUDriverGetDomainCapabilities(virQEMUDriverPtr 
driver,
 domCaps = virHashSearch(domCapsCache,
 virQEMUDriverSearchDomcaps, , NULL);
 if (!domCaps) {
+VIR_AUTOFREE(char *) key = NULL;
+
 /* hash miss, build new domcaps */
 if (!(domCaps = virDomainCapsNew(data.path, data.machine,
  data.arch, data.virttype)))
@@ -1406,7 +1408,14 @@ virQEMUDriverGetDomainCapabilities(virQEMUDriverPtr 
driver,
   cfg->firmwares, cfg->nfirmwares) < 0)
 return NULL;
 
-if (virHashAddEntry(domCapsCache, machine, domCaps) < 0)
+if (virAsprintf(, "%d:%d:%s:%s",
+data.arch,
+data.virttype,
+NULLSTR(data.machine),
+NULLSTR(data.path)) < 0)
+return NULL;
+
+if (virHashAddEntry(domCapsCache, key, domCaps) < 0)
 return NULL;
 }
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 30/31] qemuxml2*test: Add test cases for default CPU models on x86_64

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 ...-default-cpu-kvm-pc-4.2.x86_64-latest.args | 36 +++
 .../x86_64-default-cpu-kvm-pc-4.2.xml | 24 
 ...default-cpu-kvm-q35-4.2.x86_64-latest.args | 41 +
 .../x86_64-default-cpu-kvm-q35-4.2.xml| 24 
 ...-default-cpu-tcg-pc-4.2.x86_64-latest.args | 36 +++
 .../x86_64-default-cpu-tcg-pc-4.2.xml | 24 
 ...default-cpu-tcg-q35-4.2.x86_64-latest.args | 41 +
 .../x86_64-default-cpu-tcg-q35-4.2.xml| 24 
 tests/qemuxml2argvtest.c  |  4 ++
 ...4-default-cpu-kvm-pc-4.2.x86_64-latest.xml | 37 
 ...-default-cpu-kvm-q35-4.2.x86_64-latest.xml | 60 +++
 ...4-default-cpu-tcg-pc-4.2.x86_64-latest.xml | 37 
 ...-default-cpu-tcg-q35-4.2.x86_64-latest.xml | 60 +++
 tests/qemuxml2xmltest.c   |  4 ++
 14 files changed, 452 insertions(+)
 create mode 100644 
tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.x86_64-latest.args
 create mode 100644 tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.xml
 create mode 100644 
tests/qemuxml2argvdata/x86_64-default-cpu-kvm-q35-4.2.x86_64-latest.args
 create mode 100644 tests/qemuxml2argvdata/x86_64-default-cpu-kvm-q35-4.2.xml
 create mode 100644 
tests/qemuxml2argvdata/x86_64-default-cpu-tcg-pc-4.2.x86_64-latest.args
 create mode 100644 tests/qemuxml2argvdata/x86_64-default-cpu-tcg-pc-4.2.xml
 create mode 100644 
tests/qemuxml2argvdata/x86_64-default-cpu-tcg-q35-4.2.x86_64-latest.args
 create mode 100644 tests/qemuxml2argvdata/x86_64-default-cpu-tcg-q35-4.2.xml
 create mode 100644 
tests/qemuxml2xmloutdata/x86_64-default-cpu-kvm-pc-4.2.x86_64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/x86_64-default-cpu-kvm-q35-4.2.x86_64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/x86_64-default-cpu-tcg-pc-4.2.x86_64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/x86_64-default-cpu-tcg-q35-4.2.x86_64-latest.xml

diff --git 
a/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.x86_64-latest.args 
b/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.x86_64-latest.args
new file mode 100644
index 00..c7bef11f22
--- /dev/null
+++ b/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.x86_64-latest.args
@@ -0,0 +1,36 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-guest \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-guest/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-guest/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-guest/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name guest=guest,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-guest/master-key.aes \
+-machine pc-i440fx-4.2,accel=kvm,usb=off,dump-guest-core=off \
+-m 4096 \
+-overcommit mem-lock=off \
+-smp 4,sockets=4,cores=1,threads=1 \
+-uuid 1ccfd97d-5eb4-478a-bbe6-88d254c16db7 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-boot strict=on \
+-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 \
+-drive file=/var/lib/libvirt/images/guest.qcow2,format=qcow2,if=none,\
+id=drive-virtio-disk0 \
+-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x2,drive=drive-virtio-disk0,\
+id=virtio-disk0,bootindex=1 \
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.xml 
b/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.xml
new file mode 100644
index 00..ce432ef52f
--- /dev/null
+++ b/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-pc-4.2.xml
@@ -0,0 +1,24 @@
+
+  guest
+  1ccfd97d-5eb4-478a-bbe6-88d254c16db7
+  4194304
+  4194304
+  4
+  
+hvm
+
+  
+  
+
+
+  
+  
+  
+/usr/bin/qemu-system-x86_64
+
+  
+  
+  
+
+  
+
diff --git 
a/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-q35-4.2.x86_64-latest.args 
b/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-q35-4.2.x86_64-latest.args
new file mode 100644
index 00..c65d29fc32
--- /dev/null
+++ b/tests/qemuxml2argvdata/x86_64-default-cpu-kvm-q35-4.2.x86_64-latest.args
@@ -0,0 +1,41 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-guest \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-guest/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-guest/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-guest/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name guest=guest,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-guest/master-key.aes \
+-machine pc-q35-4.2,accel=kvm,usb=off,dump-guest-core=off \
+-m 4096 \
+-overcommit mem-lock=off \
+-smp 

[libvirt] [PATCH v2 31/31] qemu: Store default CPU in domain XML

2019-10-15 Thread Jiri Denemark
When starting a domain without a CPU model specified in the domain XML,
QEMU will choose a default one. Which is fine unless the domain gets
migrated to another host because libvirt doesn't perform any CPU ABI
checks and the virtual CPU provided by QEMU on the destination host can
differ from the one on the source host.

With QEMU 4.2.0 we can probe for the default CPU model used by QEMU for
a particular machine type and store it in the domain XML. This way the
chosen CPU model is more visible to users and libvirt will make sure
the guest will see the exact same CPU after migration.

Architecture specific notes
- aarch64: We only set the default CPU for TCG domains as KVM requires
  -cpu host to work.

- ppc64: (to be checked with QEMU developers) Default CPU data reported
  by QEMU is unusable for KVM. QEMU would effectively use -cpu host by
  default and it even rewrites typename of the current host CPU model to
  host-powerpc64-cpu. The default CPU type reported by query-machines is
  still power*-powerpc64-cpu, which may not even exist due to the
  changed typename. For example, on a Power8 host, power8 CPU model is
  of host-powerpc64-cpu type (according to query-cpu-definitions), while
  the default CPU type name for pseries-3.1 machine type is reported as
  power8_v2.0-powerpc64-cpu. What's even worse, pseries-4.2 says the
  default CPU type is power9_v2.0-powerpc64-cpu, which cannot be started
  on older host at all. So again, we only set the default CPU for TCG
  domains.

- s390x: (to be checked with QEMU developers) The default CPU is said to
  be "qemu", which works fine for TCG domains, but it doesn't work on
  KVM because QEMU complains that some features requested in the CPU
  model are not available.

- x86_64: The default CPU model (qemu64) is not runnable on any host
  with KVM, but in contrast to s390x QEMU just disables unavailable
  features and starts happily.

https://bugzilla.redhat.com/show_bug.cgi?id=1598151
https://bugzilla.redhat.com/show_bug.cgi?id=1598162
---

Notes:
This patch should not be merged yet as we need to confirm what to do
on s390x and ppc64 architectures.

Version 2:
- new test cases

 src/qemu/qemu_domain.c| 56 +++
 ...fault-cpu-tcg-virt-4.2.aarch64-latest.args |  1 +
 .../disk-cache.x86_64-latest.args |  1 +
 .../disk-cdrom-network.x86_64-latest.args |  1 +
 .../disk-cdrom-tray.x86_64-latest.args|  1 +
 .../disk-copy_on_read.x86_64-latest.args  |  1 +
 .../disk-detect-zeroes.x86_64-latest.args |  1 +
 .../disk-floppy-q35-2_11.x86_64-latest.args   |  1 +
 .../disk-floppy-q35-2_9.x86_64-latest.args|  1 +
 .../os-firmware-bios.x86_64-latest.args   |  1 +
 ...os-firmware-efi-secboot.x86_64-latest.args |  1 +
 .../os-firmware-efi.x86_64-latest.args|  1 +
 ...ault-cpu-tcg-pseries-2.7.ppc64-latest.args |  1 +
 ...ault-cpu-tcg-pseries-3.1.ppc64-latest.args |  1 +
 ...ault-cpu-tcg-pseries-4.2.ppc64-latest.args |  1 +
 ...t-cpu-tcg-ccw-virtio-4.2.s390x-latest.args |  1 +
 .../tpm-emulator-tpm2-enc.x86_64-latest.args  |  1 +
 .../tpm-emulator-tpm2.x86_64-latest.args  |  1 +
 .../tpm-emulator.x86_64-latest.args   |  1 +
 .../tseg-explicit-size.x86_64-latest.args |  1 +
 .../vhost-vsock-auto.x86_64-latest.args   |  1 +
 .../vhost-vsock.x86_64-latest.args|  1 +
 ...-default-cpu-kvm-pc-4.2.x86_64-latest.args |  1 +
 ...default-cpu-kvm-q35-4.2.x86_64-latest.args |  1 +
 ...-default-cpu-tcg-pc-4.2.x86_64-latest.args |  1 +
 ...default-cpu-tcg-q35-4.2.x86_64-latest.args |  1 +
 ...efault-cpu-tcg-virt-4.2.aarch64-latest.xml |  3 +
 .../os-firmware-bios.x86_64-latest.xml|  3 +
 .../os-firmware-efi-secboot.x86_64-latest.xml |  3 +
 .../os-firmware-efi.x86_64-latest.xml |  3 +
 ...fault-cpu-tcg-pseries-2.7.ppc64-latest.xml |  3 +
 ...fault-cpu-tcg-pseries-3.1.ppc64-latest.xml |  3 +
 ...fault-cpu-tcg-pseries-4.2.ppc64-latest.xml |  3 +
 ...lt-cpu-tcg-ccw-virtio-4.2.s390x-latest.xml |  3 +
 .../tpm-emulator-tpm2-enc.x86_64-latest.xml   |  3 +
 .../tpm-emulator-tpm2.x86_64-latest.xml   |  3 +
 .../tpm-emulator.x86_64-latest.xml|  3 +
 .../tpm-passthrough-crb.x86_64-latest.xml |  3 +
 .../tpm-passthrough.x86_64-latest.xml |  3 +
 ...4-default-cpu-kvm-pc-4.2.x86_64-latest.xml |  3 +
 ...-default-cpu-kvm-q35-4.2.x86_64-latest.xml |  3 +
 ...4-default-cpu-tcg-pc-4.2.x86_64-latest.xml |  3 +
 ...-default-cpu-tcg-q35-4.2.x86_64-latest.xml |  3 +
 43 files changed, 132 insertions(+)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 9dcba4ef38..438f003186 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -4470,6 +4470,59 @@ qemuDomainDefVcpusPostParse(virDomainDefPtr def)
 }
 
 
+static int
+qemuDomainDefSetDefaultCPU(virDomainDefPtr def,
+   virQEMUCapsPtr qemuCaps)
+{
+VIR_AUTOPTR(virCPUDef) newCPU = NULL;
+virCPUDefPtr cpu = def->cpu;
+

[libvirt] [PATCH v2 26/31] conf: Define VIR_AUTOPTR for virCPUDef

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- no change

 src/conf/cpu_conf.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
index 30904fab95..230e75f077 100644
--- a/src/conf/cpu_conf.h
+++ b/src/conf/cpu_conf.h
@@ -151,6 +151,7 @@ virCPUDefFreeModel(virCPUDefPtr def);
 
 void
 virCPUDefFree(virCPUDefPtr def);
+VIR_DEFINE_AUTOPTR_FUNC(virCPUDef, virCPUDefFree);
 
 int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
 virCPUDefCopyModel(virCPUDefPtr dst,
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 29/31] qemuxml2*test: Add test cases for default CPU models on s390x

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 ...t-cpu-kvm-ccw-virtio-4.2.s390x-latest.args | 31 +++
 .../s390-default-cpu-kvm-ccw-virtio-4.2.xml   | 16 ++
 ...t-cpu-tcg-ccw-virtio-4.2.s390x-latest.args | 31 +++
 .../s390-default-cpu-tcg-ccw-virtio-4.2.xml   | 16 ++
 tests/qemuxml2argvtest.c  |  2 ++
 ...lt-cpu-kvm-ccw-virtio-4.2.s390x-latest.xml | 23 ++
 ...lt-cpu-tcg-ccw-virtio-4.2.s390x-latest.xml | 23 ++
 tests/qemuxml2xmltest.c   |  2 ++
 8 files changed, 144 insertions(+)
 create mode 100644 
tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.s390x-latest.args
 create mode 100644 
tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.xml
 create mode 100644 
tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.s390x-latest.args
 create mode 100644 
tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.xml
 create mode 100644 
tests/qemuxml2xmloutdata/s390-default-cpu-kvm-ccw-virtio-4.2.s390x-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/s390-default-cpu-tcg-ccw-virtio-4.2.s390x-latest.xml

diff --git 
a/tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.s390x-latest.args 
b/tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.s390x-latest.args
new file mode 100644
index 00..d45a87e6ec
--- /dev/null
+++ 
b/tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.s390x-latest.args
@@ -0,0 +1,31 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-test \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-test/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-test/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-test/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-s390x \
+-name guest=test,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-test/master-key.aes \
+-machine s390-ccw-virtio-4.2,accel=kvm,usb=off,dump-guest-core=off \
+-m 256 \
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid 9aa4b45c-b9dd-45ef-91fe-862b27b4231f \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-boot strict=on \
+-device virtio-balloon-ccw,id=balloon0,devno=fe.0. \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.xml 
b/tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.xml
new file mode 100644
index 00..405b53db7d
--- /dev/null
+++ b/tests/qemuxml2argvdata/s390-default-cpu-kvm-ccw-virtio-4.2.xml
@@ -0,0 +1,16 @@
+
+  test
+  9aa4b45c-b9dd-45ef-91fe-862b27b4231f
+  262144
+  262144
+  
+hvm
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-s390x
+  
+
diff --git 
a/tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.s390x-latest.args 
b/tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.s390x-latest.args
new file mode 100644
index 00..3d31148613
--- /dev/null
+++ 
b/tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.s390x-latest.args
@@ -0,0 +1,31 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-test \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-test/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-test/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-test/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-s390x \
+-name guest=test,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-test/master-key.aes \
+-machine s390-ccw-virtio-4.2,accel=tcg,usb=off,dump-guest-core=off \
+-m 256 \
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid 9aa4b45c-b9dd-45ef-91fe-862b27b4231f \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-boot strict=on \
+-device virtio-balloon-ccw,id=balloon0,devno=fe.0. \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.xml 
b/tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.xml
new file mode 100644
index 00..c6c92bb08a
--- /dev/null
+++ b/tests/qemuxml2argvdata/s390-default-cpu-tcg-ccw-virtio-4.2.xml
@@ -0,0 +1,16 @@
+
+  test
+  9aa4b45c-b9dd-45ef-91fe-862b27b4231f
+  262144
+  262144
+  
+hvm
+  
+  
+  destroy
+  restart
+  destroy
+  
+/usr/bin/qemu-system-s390x
+  
+
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 7fb84fc950..e19f30ff15 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -3042,6 +3042,8 @@ mymain(void)
 

[libvirt] [PATCH v2 23/31] qemu: Introduce virQEMUCapsGetMachineDefaultCPU

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- fix crash with CPUs without type

 src/qemu/qemu_capabilities.c | 36 
 src/qemu/qemu_capabilities.h |  3 +++
 2 files changed, 39 insertions(+)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 33cd3491b2..dd7ea9ae86 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2179,6 +2179,42 @@ bool virQEMUCapsGetMachineHotplugCpus(virQEMUCapsPtr 
qemuCaps,
 }
 
 
+const char *
+virQEMUCapsGetMachineDefaultCPU(virQEMUCapsPtr qemuCaps,
+const char *name,
+virDomainVirtType type)
+{
+qemuMonitorCPUDefsPtr defs;
+const char *cpuType = NULL;
+size_t i;
+
+if (type == VIR_DOMAIN_VIRT_KVM)
+defs = qemuCaps->kvmCPUModels;
+else
+defs = qemuCaps->tcgCPUModels;
+
+if (!name || !defs)
+return NULL;
+
+for (i = 0; i < qemuCaps->nmachineTypes; i++) {
+if (STREQ(qemuCaps->machineTypes[i].name, name)) {
+cpuType = qemuCaps->machineTypes[i].defaultCPU;
+break;
+}
+}
+
+if (!cpuType)
+return NULL;
+
+for (i = 0; i < defs->ncpus; i++) {
+if (STREQ_NULLABLE(defs->cpus[i].type, cpuType))
+return defs->cpus[i].name;
+}
+
+return NULL;
+}
+
+
 /**
  * virQEMUCapsSetGICCapabilities:
  * @qemuCaps: QEMU capabilities
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 923706c3bf..f5133d083d 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -607,6 +607,9 @@ int virQEMUCapsGetMachineMaxCpus(virQEMUCapsPtr qemuCaps,
  const char *name);
 bool virQEMUCapsGetMachineHotplugCpus(virQEMUCapsPtr qemuCaps,
   const char *name);
+const char *virQEMUCapsGetMachineDefaultCPU(virQEMUCapsPtr qemuCaps,
+const char *name,
+virDomainVirtType type);
 int virQEMUCapsGetMachineTypesCaps(virQEMUCapsPtr qemuCaps,
size_t *nmachines,
virCapsGuestMachinePtr **machines);
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 24/31] qemu: Drop unused virQEMUCapsGetDefaultMachine

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_capabilities.c | 12 
 src/qemu/qemu_capabilities.h |  1 -
 2 files changed, 13 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index dd7ea9ae86..bd54c1751c 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2133,18 +2133,6 @@ const char 
*virQEMUCapsGetCanonicalMachine(virQEMUCapsPtr qemuCaps,
 return name;
 }
 
-const char *
-virQEMUCapsGetDefaultMachine(virQEMUCapsPtr qemuCaps)
-{
-size_t i;
-
-for (i = 0; i < qemuCaps->nmachineTypes; i++) {
-if (qemuCaps->machineTypes[i].qemuDefault)
-return qemuCaps->machineTypes[i].name;
-}
-
-return NULL;
-}
 
 int virQEMUCapsGetMachineMaxCpus(virQEMUCapsPtr qemuCaps,
  const char *name)
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index f5133d083d..feb6a9ecd0 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -602,7 +602,6 @@ bool virQEMUCapsIsCPUModeSupported(virQEMUCapsPtr qemuCaps,
virCPUMode mode);
 const char *virQEMUCapsGetCanonicalMachine(virQEMUCapsPtr qemuCaps,
const char *name);
-const char *virQEMUCapsGetDefaultMachine(virQEMUCapsPtr qemuCaps);
 int virQEMUCapsGetMachineMaxCpus(virQEMUCapsPtr qemuCaps,
  const char *name);
 bool virQEMUCapsGetMachineHotplugCpus(virQEMUCapsPtr qemuCaps,
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 15/31] qemu: Introduce virQEMUCapsCPUDefsToModels

2019-10-15 Thread Jiri Denemark
The function translates qemuMonitorCPUDefsPtr (used by QEMU caps probing
code) into virDomainCapsCPUModelsPtr used by domain capabilities.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- trivial rebase

 src/qemu/qemu_capabilities.c | 71 +---
 1 file changed, 42 insertions(+), 29 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index e38ad03ab5..87ac9bacdc 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1887,6 +1887,41 @@ virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
 }
 
 
+static virDomainCapsCPUModelsPtr
+virQEMUCapsCPUDefsToModels(qemuMonitorCPUDefsPtr defs,
+   const char **modelWhitelist,
+   const char **modelBlacklist)
+{
+VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+size_t i;
+
+if (!(cpuModels = virDomainCapsCPUModelsNew(defs->ncpus)))
+return NULL;
+
+for (i = 0; i < defs->ncpus; i++) {
+qemuMonitorCPUDefInfoPtr cpu = defs->cpus + i;
+virDomainCapsCPUUsable usable = VIR_DOMCAPS_CPU_USABLE_UNKNOWN;
+
+if (modelWhitelist && !virStringListHasString(modelWhitelist, 
cpu->name))
+continue;
+
+if (modelBlacklist && virStringListHasString(modelBlacklist, 
cpu->name))
+continue;
+
+if (cpu->usable == VIR_TRISTATE_BOOL_YES)
+usable = VIR_DOMCAPS_CPU_USABLE_YES;
+else if (cpu->usable == VIR_TRISTATE_BOOL_NO)
+usable = VIR_DOMCAPS_CPU_USABLE_NO;
+
+if (virDomainCapsCPUModelsAdd(cpuModels, cpu->name,
+  usable, cpu->blockers) < 0)
+return NULL;
+}
+
+VIR_RETURN_PTR(cpuModels);
+}
+
+
 virDomainCapsCPUModelsPtr
 virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
  virDomainVirtType type,
@@ -2458,19 +2493,15 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
virDomainCapsCPUModelsPtr *cpuModels)
 {
 VIR_AUTOPTR(qemuMonitorCPUDefs) defs = NULL;
-virDomainCapsCPUModelsPtr models = NULL;
 size_t i;
-int ret = -1;
 
 *cpuModels = NULL;
 
 if (qemuMonitorGetCPUDefinitions(mon, ) < 0)
 return -1;
 
-if (!defs) {
-ret = 0;
-goto cleanup;
-}
+if (!defs)
+return 0;
 
 /* QEMU 2.11 for Power renamed all CPU models to lower case, we need to
  * translate them back to libvirt's upper case model names. */
@@ -2479,7 +2510,7 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 char **name;
 
 if (virCPUGetModels(arch, ) < 0)
-goto cleanup;
+return -1;
 
 for (name = libvirtModels; name && *name; name++) {
 for (i = 0; i < defs->ncpus; i++) {
@@ -2488,33 +2519,15 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 
 VIR_FREE(defs->cpus[i].name);
 if (VIR_STRDUP(defs->cpus[i].name, *name) < 0)
-goto cleanup;
+return -1;
 }
 }
 }
 
-if (!(models = virDomainCapsCPUModelsNew(defs->ncpus)))
-goto cleanup;
-
-for (i = 0; i < defs->ncpus; i++) {
-virDomainCapsCPUUsable usable = VIR_DOMCAPS_CPU_USABLE_UNKNOWN;
-
-if (defs->cpus[i].usable == VIR_TRISTATE_BOOL_YES)
-usable = VIR_DOMCAPS_CPU_USABLE_YES;
-else if (defs->cpus[i].usable == VIR_TRISTATE_BOOL_NO)
-usable = VIR_DOMCAPS_CPU_USABLE_NO;
-
-if (virDomainCapsCPUModelsAddSteal(models, >cpus[i].name, usable,
-   >cpus[i].blockers) < 0)
-goto cleanup;
-}
-
-VIR_STEAL_PTR(*cpuModels, models);
-ret = 0;
+if (!(*cpuModels = virQEMUCapsCPUDefsToModels(defs, NULL, NULL)))
+return -1;
 
- cleanup:
-virObjectUnref(models);
-return ret;
+return 0;
 }
 
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 04/31] conf: Drop nameLen parameter from virDomainCapsCPUModelsAdd

2019-10-15 Thread Jiri Denemark
All callers use nameLen == -1 anyway.

Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/conf/domain_capabilities.c | 7 +++
 src/conf/domain_capabilities.h | 1 -
 src/qemu/qemu_capabilities.c   | 2 +-
 tests/cputest.c| 4 ++--
 4 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/conf/domain_capabilities.c b/src/conf/domain_capabilities.c
index 3224ec3f52..ab8ebb650d 100644
--- a/src/conf/domain_capabilities.c
+++ b/src/conf/domain_capabilities.c
@@ -171,7 +171,7 @@ virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr old)
 
 for (i = 0; i < old->nmodels; i++) {
 if (virDomainCapsCPUModelsAdd(cpuModels,
-  old->models[i].name, -1,
+  old->models[i].name,
   old->models[i].usable,
   old->models[i].blockers) < 0)
 goto error;
@@ -204,7 +204,7 @@ virDomainCapsCPUModelsFilter(virDomainCapsCPUModelsPtr old,
 continue;
 
 if (virDomainCapsCPUModelsAdd(cpuModels,
-  old->models[i].name, -1,
+  old->models[i].name,
   old->models[i].usable,
   old->models[i].blockers) < 0)
 goto error;
@@ -242,14 +242,13 @@ virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr 
cpuModels,
 int
 virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr cpuModels,
   const char *name,
-  ssize_t nameLen,
   virDomainCapsCPUUsable usable,
   char **blockers)
 {
 VIR_AUTOFREE(char *) nameCopy = NULL;
 VIR_AUTOSTRINGLIST blockersCopy = NULL;
 
-if (VIR_STRNDUP(nameCopy, name, nameLen) < 0)
+if (VIR_STRDUP(nameCopy, name) < 0)
 return -1;
 
 if (virStringListCopy(, (const char **)blockers) < 0)
diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h
index 3a92194602..e3bd8d423e 100644
--- a/src/conf/domain_capabilities.h
+++ b/src/conf/domain_capabilities.h
@@ -204,7 +204,6 @@ int 
virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr cpuModels,
char ***blockers);
 int virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr cpuModels,
   const char *name,
-  ssize_t nameLen,
   virDomainCapsCPUUsable usable,
   char **blockers);
 virDomainCapsCPUModelPtr
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index efd71d69c5..14939fc91d 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1879,7 +1879,7 @@ virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
 }
 
 for (i = 0; i < count; i++) {
-if (virDomainCapsCPUModelsAdd(cpus, name[i], -1, usable, NULL) < 0)
+if (virDomainCapsCPUModelsAdd(cpus, name[i], usable, NULL) < 0)
 return -1;
 }
 
diff --git a/tests/cputest.c b/tests/cputest.c
index e3937c6fd6..7f47e3b601 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -863,7 +863,7 @@ cpuTestUpdateLive(const void *arg)
 usable = hvModel->usable;
 }
 
-if (virDomainCapsCPUModelsAdd(models, expected->model, -1,
+if (virDomainCapsCPUModelsAdd(models, expected->model,
   usable, blockers) < 0)
 goto cleanup;
 
@@ -973,7 +973,7 @@ cpuTestInitModels(const char **list)
 return NULL;
 
 for (model = list; *model; model++) {
-if (virDomainCapsCPUModelsAdd(cpus, *model, -1,
+if (virDomainCapsCPUModelsAdd(cpus, *model,
   VIR_DOMCAPS_CPU_USABLE_UNKNOWN, NULL) < 
0)
 goto error;
 }
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 14/31] qemu: Use VIR_AUTOFREE in virQEMUCapsLoadCPUModels

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_capabilities.c | 58 +++-
 1 file changed, 24 insertions(+), 34 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 442445aad9..e38ad03ab5 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3474,15 +3474,10 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps,
  virDomainVirtType type)
 {
 virDomainCapsCPUModelsPtr cpus = NULL;
-xmlNodePtr *nodes = NULL;
-char *str = NULL;
+VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
 size_t i;
 int n;
-int ret = -1;
 xmlNodePtr node;
-xmlNodePtr *blockerNodes = NULL;
-char **blockers = NULL;
-int nblockers;
 
 if (type == VIR_DOMAIN_VIRT_KVM)
 n = virXPathNodeSet("./cpu[@type='kvm']", ctxt, );
@@ -3492,16 +3487,14 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps,
 if (n < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse qemu capabilities cpus"));
-goto cleanup;
+return -1;
 }
 
-if (n == 0) {
-ret = 0;
-goto cleanup;
-}
+if (n == 0)
+return 0;
 
 if (!(cpus = virDomainCapsCPUModelsNew(n)))
-goto cleanup;
+return -1;
 
 if (type == VIR_DOMAIN_VIRT_KVM)
 qemuCaps->kvmCPUModels = cpus;
@@ -3510,19 +3503,24 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps,
 
 for (i = 0; i < n; i++) {
 int usable = VIR_DOMCAPS_CPU_USABLE_UNKNOWN;
-
-if ((str = virXMLPropString(nodes[i], "usable")) &&
-(usable = virDomainCapsCPUUsableTypeFromString(str)) < 0) {
+VIR_AUTOFREE(char *) strUsable = NULL;
+VIR_AUTOFREE(char *) name = NULL;
+VIR_AUTOFREE(xmlNodePtr *) blockerNodes = NULL;
+VIR_AUTOSTRINGLIST blockers = NULL;
+int nblockers;
+
+if ((strUsable = virXMLPropString(nodes[i], "usable")) &&
+(usable = virDomainCapsCPUUsableTypeFromString(strUsable)) < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
-   _("unknown value '%s' in attribute 'usable'"), str);
-goto cleanup;
+   _("unknown value '%s' in attribute 'usable'"),
+   strUsable);
+return -1;
 }
-VIR_FREE(str);
 
-if (!(str = virXMLPropString(nodes[i], "name"))) {
+if (!(name = virXMLPropString(nodes[i], "name"))) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing cpu name in QEMU capabilities cache"));
-goto cleanup;
+return -1;
 }
 
 node = ctxt->node;
@@ -3533,38 +3531,30 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps,
 if (nblockers < 0) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse CPU blockers in QEMU 
capabilities"));
-goto cleanup;
+return -1;
 }
 
 if (nblockers > 0) {
 size_t j;
 
 if (VIR_ALLOC_N(blockers, nblockers + 1) < 0)
-goto cleanup;
+return -1;
 
 for (j = 0; j < nblockers; j++) {
 if (!(blockers[j] = virXMLPropString(blockerNodes[j], 
"name"))) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing blocker name in QEMU "
  "capabilities cache"));
-goto cleanup;
+return -1;
 }
 }
-VIR_FREE(blockerNodes);
 }
 
-if (virDomainCapsCPUModelsAddSteal(cpus, , usable, ) < 0)
-goto cleanup;
+if (virDomainCapsCPUModelsAddSteal(cpus, , usable, ) < 0)
+return -1;
 }
 
-ret = 0;
-
- cleanup:
-VIR_FREE(nodes);
-VIR_FREE(str);
-VIR_FREE(blockerNodes);
-virStringListFree(blockers);
-return ret;
+return 0;
 }
 
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 10/31] qemu: Introduce qemuMonitorCPUDefs struct

2019-10-15 Thread Jiri Denemark
It is a container for a CPU models list (qemuMonitorCPUDefInfo) and a
number of elements in this list.

Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- v1 reviewed by Ján Tomko, but the patch had to be changed because
  of the previous patch

 src/qemu/qemu_capabilities.c | 30 +--
 src/qemu/qemu_monitor.c  | 39 +++--
 src/qemu/qemu_monitor.h  | 14 +++--
 src/qemu/qemu_monitor_json.c | 56 ++--
 src/qemu/qemu_monitor_json.h |  2 +-
 tests/qemumonitorjsontest.c  | 38 +---
 6 files changed, 93 insertions(+), 86 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 6fa5e06edb..59af3ab6d3 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2457,18 +2457,17 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
virArch arch,
virDomainCapsCPUModelsPtr *cpuModels)
 {
+VIR_AUTOPTR(qemuMonitorCPUDefs) defs = NULL;
 virDomainCapsCPUModelsPtr models = NULL;
-qemuMonitorCPUDefInfoPtr *cpus = NULL;
-int ncpus = 0;
 size_t i;
 int ret = -1;
 
 *cpuModels = NULL;
 
-if ((ncpus = qemuMonitorGetCPUDefinitions(mon, )) < 0)
+if (qemuMonitorGetCPUDefinitions(mon, ) < 0)
 return -1;
 
-if (ncpus == 0) {
+if (!defs) {
 ret = 0;
 goto cleanup;
 }
@@ -2483,30 +2482,30 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 goto cleanup;
 
 for (name = libvirtModels; name && *name; name++) {
-for (i = 0; i < ncpus; i++) {
-if (STRCASENEQ(cpus[i]->name, *name))
+for (i = 0; i < defs->ncpus; i++) {
+if (STRCASENEQ(defs->cpus[i]->name, *name))
 continue;
 
-VIR_FREE(cpus[i]->name);
-if (VIR_STRDUP(cpus[i]->name, *name) < 0)
+VIR_FREE(defs->cpus[i]->name);
+if (VIR_STRDUP(defs->cpus[i]->name, *name) < 0)
 goto cleanup;
 }
 }
 }
 
-if (!(models = virDomainCapsCPUModelsNew(ncpus)))
+if (!(models = virDomainCapsCPUModelsNew(defs->ncpus)))
 goto cleanup;
 
-for (i = 0; i < ncpus; i++) {
+for (i = 0; i < defs->ncpus; i++) {
 virDomainCapsCPUUsable usable = VIR_DOMCAPS_CPU_USABLE_UNKNOWN;
 
-if (cpus[i]->usable == VIR_TRISTATE_BOOL_YES)
+if (defs->cpus[i]->usable == VIR_TRISTATE_BOOL_YES)
 usable = VIR_DOMCAPS_CPU_USABLE_YES;
-else if (cpus[i]->usable == VIR_TRISTATE_BOOL_NO)
+else if (defs->cpus[i]->usable == VIR_TRISTATE_BOOL_NO)
 usable = VIR_DOMCAPS_CPU_USABLE_NO;
 
-if (virDomainCapsCPUModelsAddSteal(models, [i]->name, usable,
-   [i]->blockers) < 0)
+if (virDomainCapsCPUModelsAddSteal(models, >cpus[i]->name, 
usable,
+   >cpus[i]->blockers) < 0)
 goto cleanup;
 }
 
@@ -2514,9 +2513,6 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 ret = 0;
 
  cleanup:
-for (i = 0; i < ncpus; i++)
-qemuMonitorCPUDefInfoFree(cpus[i]);
-VIR_FREE(cpus);
 virObjectUnref(models);
 return ret;
 }
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 49f9159315..d17387d27f 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3538,25 +3538,48 @@ qemuMonitorMachineInfoFree(qemuMonitorMachineInfoPtr 
machine)
 
 int
 qemuMonitorGetCPUDefinitions(qemuMonitorPtr mon,
- qemuMonitorCPUDefInfoPtr **cpus)
+ qemuMonitorCPUDefsPtr *cpuDefs)
 {
-VIR_DEBUG("cpus=%p", cpus);
+VIR_DEBUG("cpuDefs=%p", cpuDefs);
 
 QEMU_CHECK_MONITOR(mon);
 
-return qemuMonitorJSONGetCPUDefinitions(mon, cpus);
+return qemuMonitorJSONGetCPUDefinitions(mon, cpuDefs);
 }
 
 
 void
-qemuMonitorCPUDefInfoFree(qemuMonitorCPUDefInfoPtr cpu)
+qemuMonitorCPUDefsFree(qemuMonitorCPUDefsPtr defs)
 {
-if (!cpu)
+size_t i;
+
+if (!defs)
 return;
 
-virStringListFree(cpu->blockers);
-VIR_FREE(cpu->name);
-VIR_FREE(cpu);
+for (i = 0; i < defs->ncpus; i++) {
+virStringListFree(defs->cpus[i]->blockers);
+VIR_FREE(defs->cpus[i]->name);
+VIR_FREE(defs->cpus[i]);
+}
+
+VIR_FREE(defs->cpus);
+VIR_FREE(defs);
+}
+
+
+qemuMonitorCPUDefsPtr
+qemuMonitorCPUDefsNew(size_t count)
+{
+VIR_AUTOPTR(qemuMonitorCPUDefs) defs = NULL;
+
+if (VIR_ALLOC(defs) < 0)
+return NULL;
+
+if (count > 0 && VIR_ALLOC_N(defs->cpus, count) < 0)
+return NULL;
+
+defs->ncpus = count;
+VIR_RETURN_PTR(defs);
 }
 
 
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 536ba7893b..359bbfca7f 100644
--- a/src/qemu/qemu_monitor.h
+++ 

[libvirt] [PATCH v2 06/31] qemu: Filter models in virQEMUCapsGetCPUDefinitions

2019-10-15 Thread Jiri Denemark
Some callers of virQEMUCapsGetCPUDefinitions will need to filter the
returned list of CPU models. Let's add the filtering parameters directly
to virQEMUCapsGetCPUDefinitions to avoid copying the CPU models list
twice.

Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_capabilities.c | 11 ---
 src/qemu/qemu_capabilities.h |  4 +++-
 src/qemu/qemu_driver.c   |  2 +-
 src/qemu/qemu_process.c  |  2 +-
 tests/cputest.c  |  2 +-
 5 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index a40bf99aa3..e6a7ee4f14 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1889,7 +1889,9 @@ virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
 
 virDomainCapsCPUModelsPtr
 virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
- virDomainVirtType type)
+ virDomainVirtType type,
+ const char **modelWhitelist,
+ const char **modelBlacklist)
 {
 virDomainCapsCPUModelsPtr cpuModels;
 
@@ -1901,6 +1903,9 @@ virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
 if (!cpuModels)
 return NULL;
 
+if (modelWhitelist || modelBlacklist)
+return virDomainCapsCPUModelsFilter(cpuModels, modelWhitelist, 
modelBlacklist);
+
 return virDomainCapsCPUModelsCopy(cpuModels);
 }
 
@@ -3142,7 +3147,7 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
 if (!(data = virQEMUCapsGetCPUModelX86Data(qemuCaps, model, migratable)))
 goto cleanup;
 
-cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type);
+cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type, NULL, NULL);
 
 if (cpuDecode(cpu, data, cpuModels) < 0)
 goto cleanup;
@@ -3231,7 +3236,7 @@ virQEMUCapsInitHostCPUModel(virQEMUCapsPtr qemuCaps,
 
 VIR_DEBUG("No host CPU model info from QEMU; probing host CPU 
directly");
 
-cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type);
+cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type, NULL, NULL);
 hostCPU = virQEMUCapsProbeHostCPU(hostArch, cpuModels);
 
 if (!hostCPU ||
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 10f0ce2654..67dccc522e 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -570,7 +570,9 @@ int virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
  size_t count,
  virDomainCapsCPUUsable usable);
 virDomainCapsCPUModelsPtr virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
-   virDomainVirtType type);
+   virDomainVirtType type,
+   const char 
**modelWhitelist,
+   const char 
**modelBlacklist);
 virDomainCapsCPUModelsPtr virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
  virArch arch);
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 15c69667a5..802f07fe92 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13939,7 +13939,7 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
 if (!qemuCaps)
 goto cleanup;
 
-if (!(cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, virttype)) ||
+if (!(cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, virttype, NULL, 
NULL)) ||
 cpuModels->nmodels == 0) {
 virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
_("QEMU '%s' does not support any CPU models for "
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 58192612b0..f37acab9e4 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -6136,7 +6136,7 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
  
VIR_QEMU_CAPS_HOST_CPU_MIGRATABLE)) < 0)
 return -1;
 
-cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType);
+cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType, 
NULL, NULL);
 
 if (virCPUTranslate(def->os.arch, def->cpu, cpuModels) < 0)
 return -1;
diff --git a/tests/cputest.c b/tests/cputest.c
index 3aa8fd7bf3..4428d451cf 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -550,7 +550,7 @@ cpuTestGetCPUModels(const struct data *data,
 if (!(qemuCaps = cpuTestMakeQEMUCaps(data)))
 return -1;
 
-*models = virQEMUCapsGetCPUDefinitions(qemuCaps, VIR_DOMAIN_VIRT_KVM);
+*models = virQEMUCapsGetCPUDefinitions(qemuCaps, VIR_DOMAIN_VIRT_KVM, 
NULL, NULL);
 
 virObjectUnref(qemuCaps);
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com

[libvirt] [PATCH v2 11/31] qemu: Flatten qemuMonitorCPUDefs.cpus

2019-10-15 Thread Jiri Denemark
Let's store qemuMonitorCPUDefInfo directly in the array of CPUs in
qemuMonitorCPUDefs rather then using an array of pointers.

Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- trivial rebase

 src/qemu/qemu_capabilities.c | 14 +++---
 src/qemu/qemu_monitor.c  |  5 ++---
 src/qemu/qemu_monitor.h  |  2 +-
 src/qemu/qemu_monitor_json.c |  7 +--
 tests/qemumonitorjsontest.c  |  8 
 5 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 59af3ab6d3..442445aad9 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2483,11 +2483,11 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 
 for (name = libvirtModels; name && *name; name++) {
 for (i = 0; i < defs->ncpus; i++) {
-if (STRCASENEQ(defs->cpus[i]->name, *name))
+if (STRCASENEQ(defs->cpus[i].name, *name))
 continue;
 
-VIR_FREE(defs->cpus[i]->name);
-if (VIR_STRDUP(defs->cpus[i]->name, *name) < 0)
+VIR_FREE(defs->cpus[i].name);
+if (VIR_STRDUP(defs->cpus[i].name, *name) < 0)
 goto cleanup;
 }
 }
@@ -2499,13 +2499,13 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 for (i = 0; i < defs->ncpus; i++) {
 virDomainCapsCPUUsable usable = VIR_DOMCAPS_CPU_USABLE_UNKNOWN;
 
-if (defs->cpus[i]->usable == VIR_TRISTATE_BOOL_YES)
+if (defs->cpus[i].usable == VIR_TRISTATE_BOOL_YES)
 usable = VIR_DOMCAPS_CPU_USABLE_YES;
-else if (defs->cpus[i]->usable == VIR_TRISTATE_BOOL_NO)
+else if (defs->cpus[i].usable == VIR_TRISTATE_BOOL_NO)
 usable = VIR_DOMCAPS_CPU_USABLE_NO;
 
-if (virDomainCapsCPUModelsAddSteal(models, >cpus[i]->name, 
usable,
-   >cpus[i]->blockers) < 0)
+if (virDomainCapsCPUModelsAddSteal(models, >cpus[i].name, usable,
+   >cpus[i].blockers) < 0)
 goto cleanup;
 }
 
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index d17387d27f..87a9d06d45 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3557,9 +3557,8 @@ qemuMonitorCPUDefsFree(qemuMonitorCPUDefsPtr defs)
 return;
 
 for (i = 0; i < defs->ncpus; i++) {
-virStringListFree(defs->cpus[i]->blockers);
-VIR_FREE(defs->cpus[i]->name);
-VIR_FREE(defs->cpus[i]);
+virStringListFree(defs->cpus[i].blockers);
+VIR_FREE(defs->cpus[i].name);
 }
 
 VIR_FREE(defs->cpus);
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 359bbfca7f..aa2a0e392a 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -1105,7 +1105,7 @@ typedef struct _qemuMonitorCPUDefs qemuMonitorCPUDefs;
 typedef qemuMonitorCPUDefs *qemuMonitorCPUDefsPtr;
 struct _qemuMonitorCPUDefs {
 size_t ncpus;
-qemuMonitorCPUDefInfoPtr *cpus;
+qemuMonitorCPUDefInfoPtr cpus;
 };
 
 int qemuMonitorGetCPUDefinitions(qemuMonitorPtr mon,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 664f1b225e..de75c46dc1 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -5560,12 +5560,7 @@ qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon,
 for (i = 0; i < defs->ncpus; i++) {
 virJSONValuePtr child = virJSONValueArrayGet(data, i);
 const char *tmp;
-qemuMonitorCPUDefInfoPtr cpu;
-
-if (VIR_ALLOC(cpu) < 0)
-return -1;
-
-defs->cpus[i] = cpu;
+qemuMonitorCPUDefInfoPtr cpu = defs->cpus + i;
 
 if (!(tmp = virJSONValueObjectGetString(child, "name"))) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index 7b8f63b11e..18ccf271f5 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -463,16 +463,16 @@ testQemuMonitorJSONGetCPUDefinitions(const void *opaque)
 
 #define CHECK_FULL(i, wantname, Usable) \
 do { \
-if (STRNEQ(defs->cpus[i]->name, (wantname))) { \
+if (STRNEQ(defs->cpus[i].name, (wantname))) { \
 virReportError(VIR_ERR_INTERNAL_ERROR, \
"name %s is not %s", \
-   defs->cpus[i]->name, (wantname)); \
+   defs->cpus[i].name, (wantname)); \
 return -1; \
 } \
-if (defs->cpus[i]->usable != (Usable)) { \
+if (defs->cpus[i].usable != (Usable)) { \
 virReportError(VIR_ERR_INTERNAL_ERROR, \
"%s: expecting usable flag %d, got %d", \
-   defs->cpus[i]->name, Usable, 
defs->cpus[i]->usable); \
+   defs->cpus[i].name, Usable, defs->cpus[i].usable); \
 return 

[libvirt] [PATCH v2 28/31] qemuxml2*test: Add test cases for default CPU models on ppc64

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 ...ault-cpu-kvm-pseries-2.7.ppc64-latest.args | 38 
 .../ppc64-default-cpu-kvm-pseries-2.7.xml | 22 +
 ...ault-cpu-kvm-pseries-3.1.ppc64-latest.args | 38 
 .../ppc64-default-cpu-kvm-pseries-3.1.xml | 22 +
 ...ault-cpu-kvm-pseries-4.2.ppc64-latest.args | 38 
 .../ppc64-default-cpu-kvm-pseries-4.2.xml | 22 +
 ...ault-cpu-tcg-pseries-2.7.ppc64-latest.args | 38 
 .../ppc64-default-cpu-tcg-pseries-2.7.xml | 22 +
 ...ault-cpu-tcg-pseries-3.1.ppc64-latest.args | 38 
 .../ppc64-default-cpu-tcg-pseries-3.1.xml | 22 +
 ...ault-cpu-tcg-pseries-4.2.ppc64-latest.args | 38 
 .../ppc64-default-cpu-tcg-pseries-4.2.xml | 22 +
 tests/qemuxml2argvtest.c  |  6 +++
 ...fault-cpu-kvm-pseries-2.7.ppc64-latest.xml | 45 +++
 ...fault-cpu-kvm-pseries-3.1.ppc64-latest.xml | 45 +++
 ...fault-cpu-kvm-pseries-4.2.ppc64-latest.xml | 45 +++
 ...fault-cpu-tcg-pseries-2.7.ppc64-latest.xml | 45 +++
 ...fault-cpu-tcg-pseries-3.1.ppc64-latest.xml | 45 +++
 ...fault-cpu-tcg-pseries-4.2.ppc64-latest.xml | 45 +++
 tests/qemuxml2xmltest.c   |  6 +++
 20 files changed, 642 insertions(+)
 create mode 100644 
tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-2.7.ppc64-latest.args
 create mode 100644 tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-2.7.xml
 create mode 100644 
tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-3.1.ppc64-latest.args
 create mode 100644 tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-3.1.xml
 create mode 100644 
tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-4.2.ppc64-latest.args
 create mode 100644 tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-4.2.xml
 create mode 100644 
tests/qemuxml2argvdata/ppc64-default-cpu-tcg-pseries-2.7.ppc64-latest.args
 create mode 100644 tests/qemuxml2argvdata/ppc64-default-cpu-tcg-pseries-2.7.xml
 create mode 100644 
tests/qemuxml2argvdata/ppc64-default-cpu-tcg-pseries-3.1.ppc64-latest.args
 create mode 100644 tests/qemuxml2argvdata/ppc64-default-cpu-tcg-pseries-3.1.xml
 create mode 100644 
tests/qemuxml2argvdata/ppc64-default-cpu-tcg-pseries-4.2.ppc64-latest.args
 create mode 100644 tests/qemuxml2argvdata/ppc64-default-cpu-tcg-pseries-4.2.xml
 create mode 100644 
tests/qemuxml2xmloutdata/ppc64-default-cpu-kvm-pseries-2.7.ppc64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/ppc64-default-cpu-kvm-pseries-3.1.ppc64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/ppc64-default-cpu-kvm-pseries-4.2.ppc64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/ppc64-default-cpu-tcg-pseries-2.7.ppc64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/ppc64-default-cpu-tcg-pseries-3.1.ppc64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/ppc64-default-cpu-tcg-pseries-4.2.ppc64-latest.xml

diff --git 
a/tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-2.7.ppc64-latest.args 
b/tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-2.7.ppc64-latest.args
new file mode 100644
index 00..d264659d49
--- /dev/null
+++ b/tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-2.7.ppc64-latest.args
@@ -0,0 +1,38 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-guest \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-guest/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-guest/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-guest/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-ppc64 \
+-name guest=guest,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-guest/master-key.aes \
+-machine pseries-2.7,accel=kvm,usb=off,dump-guest-core=off \
+-m 4096 \
+-overcommit mem-lock=off \
+-smp 4,sockets=4,cores=1,threads=1 \
+-uuid 1ccfd97d-5eb4-478a-bbe6-88d254c16db7 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-boot strict=on \
+-device pci-ohci,id=usb,bus=pci.0,addr=0x1 \
+-drive file=/var/lib/libvirt/images/guest.qcow2,format=qcow2,if=none,\
+id=drive-virtio-disk0 \
+-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x2,drive=drive-virtio-disk0,\
+id=virtio-disk0,bootindex=1 \
+-chardev pty,id=charserial0 \
+-device spapr-vty,chardev=charserial0,id=serial0,reg=0x3000 \
+-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3 \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-2.7.xml 
b/tests/qemuxml2argvdata/ppc64-default-cpu-kvm-pseries-2.7.xml
new file mode 100644
index 00..9aac1815c4
--- /dev/null
+++ 

[libvirt] [PATCH v2 19/31] conf: Drop unused virDomainCapsCPUModelsFilter

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- no change

 src/conf/domain_capabilities.c | 33 -
 src/conf/domain_capabilities.h |  3 ---
 src/libvirt_private.syms   |  1 -
 3 files changed, 37 deletions(-)

diff --git a/src/conf/domain_capabilities.c b/src/conf/domain_capabilities.c
index ab8ebb650d..a13463a6e7 100644
--- a/src/conf/domain_capabilities.c
+++ b/src/conf/domain_capabilities.c
@@ -185,39 +185,6 @@ virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr old)
 }
 
 
-virDomainCapsCPUModelsPtr
-virDomainCapsCPUModelsFilter(virDomainCapsCPUModelsPtr old,
- const char **models,
- const char **blacklist)
-{
-virDomainCapsCPUModelsPtr cpuModels;
-size_t i;
-
-if (!(cpuModels = virDomainCapsCPUModelsNew(0)))
-return NULL;
-
-for (i = 0; i < old->nmodels; i++) {
-if (models && !virStringListHasString(models, old->models[i].name))
-continue;
-
-if (blacklist && virStringListHasString(blacklist, 
old->models[i].name))
-continue;
-
-if (virDomainCapsCPUModelsAdd(cpuModels,
-  old->models[i].name,
-  old->models[i].usable,
-  old->models[i].blockers) < 0)
-goto error;
-}
-
-return cpuModels;
-
- error:
-virObjectUnref(cpuModels);
-return NULL;
-}
-
-
 int
 virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr cpuModels,
char **name,
diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h
index e3bd8d423e..bdd0bd9253 100644
--- a/src/conf/domain_capabilities.h
+++ b/src/conf/domain_capabilities.h
@@ -195,9 +195,6 @@ virDomainCapsPtr virDomainCapsNew(const char *path,
 
 virDomainCapsCPUModelsPtr virDomainCapsCPUModelsNew(size_t nmodels);
 virDomainCapsCPUModelsPtr virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr 
old);
-virDomainCapsCPUModelsPtr 
virDomainCapsCPUModelsFilter(virDomainCapsCPUModelsPtr old,
-   const char **models,
-   const char **blacklist);
 int virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr cpuModels,
char **name,
virDomainCapsCPUUsable usable,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1af18b880b..641b1f93bd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -193,7 +193,6 @@ virDomainAuditVcpu;
 virDomainCapsCPUModelsAdd;
 virDomainCapsCPUModelsAddSteal;
 virDomainCapsCPUModelsCopy;
-virDomainCapsCPUModelsFilter;
 virDomainCapsCPUModelsGet;
 virDomainCapsCPUModelsNew;
 virDomainCapsCPUUsableTypeFromString;
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 27/31] qemuxml2*test: Add test cases for default CPU models on aarch64

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 ...fault-cpu-kvm-virt-4.2.aarch64-latest.args | 38 +++
 .../aarch64-default-cpu-kvm-virt-4.2.xml  | 20 ++
 ...fault-cpu-tcg-virt-4.2.aarch64-latest.args | 38 +++
 .../aarch64-default-cpu-tcg-virt-4.2.xml  | 20 ++
 tests/qemuxml2argvtest.c  |  3 ++
 ...efault-cpu-kvm-virt-4.2.aarch64-latest.xml | 38 +++
 ...efault-cpu-tcg-virt-4.2.aarch64-latest.xml | 38 +++
 tests/qemuxml2xmltest.c   |  3 ++
 8 files changed, 198 insertions(+)
 create mode 100644 
tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.aarch64-latest.args
 create mode 100644 tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.xml
 create mode 100644 
tests/qemuxml2argvdata/aarch64-default-cpu-tcg-virt-4.2.aarch64-latest.args
 create mode 100644 tests/qemuxml2argvdata/aarch64-default-cpu-tcg-virt-4.2.xml
 create mode 100644 
tests/qemuxml2xmloutdata/aarch64-default-cpu-kvm-virt-4.2.aarch64-latest.xml
 create mode 100644 
tests/qemuxml2xmloutdata/aarch64-default-cpu-tcg-virt-4.2.aarch64-latest.xml

diff --git 
a/tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.aarch64-latest.args 
b/tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.aarch64-latest.args
new file mode 100644
index 00..fe33d5410a
--- /dev/null
+++ 
b/tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.aarch64-latest.args
@@ -0,0 +1,38 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-guest \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-guest/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-guest/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-guest/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-aarch64 \
+-name guest=guest,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-guest/master-key.aes \
+-machine virt-4.2,accel=kvm,usb=off,dump-guest-core=off,gic-version=3 \
+-m 4096 \
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid 1ccfd97d-5eb4-478a-bbe6-88d254c16db7 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-boot strict=on \
+-device 
pcie-root-port,port=0x8,chassis=1,id=pci.1,bus=pcie.0,multifunction=on,\
+addr=0x1 \
+-device pcie-root-port,port=0x9,chassis=2,id=pci.2,bus=pcie.0,addr=0x1.0x1 \
+-drive file=/var/lib/libvirt/images/guest.qcow2,format=qcow2,if=none,\
+id=drive-virtio-disk0 \
+-device virtio-blk-pci,scsi=off,bus=pci.1,addr=0x0,drive=drive-virtio-disk0,\
+id=virtio-disk0,bootindex=1 \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.xml 
b/tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.xml
new file mode 100644
index 00..1267306ebf
--- /dev/null
+++ b/tests/qemuxml2argvdata/aarch64-default-cpu-kvm-virt-4.2.xml
@@ -0,0 +1,20 @@
+
+  guest
+  1ccfd97d-5eb4-478a-bbe6-88d254c16db7
+  4194304
+  4194304
+  1
+  
+hvm
+
+  
+  
+  
+/usr/bin/qemu-system-aarch64
+
+  
+  
+  
+
+  
+
diff --git 
a/tests/qemuxml2argvdata/aarch64-default-cpu-tcg-virt-4.2.aarch64-latest.args 
b/tests/qemuxml2argvdata/aarch64-default-cpu-tcg-virt-4.2.aarch64-latest.args
new file mode 100644
index 00..5ecbe1fdd6
--- /dev/null
+++ 
b/tests/qemuxml2argvdata/aarch64-default-cpu-tcg-virt-4.2.aarch64-latest.args
@@ -0,0 +1,38 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/tmp/lib/domain--1-guest \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/tmp/lib/domain--1-guest/.local/share \
+XDG_CACHE_HOME=/tmp/lib/domain--1-guest/.cache \
+XDG_CONFIG_HOME=/tmp/lib/domain--1-guest/.config \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-aarch64 \
+-name guest=guest,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-guest/master-key.aes \
+-machine virt-4.2,accel=tcg,usb=off,dump-guest-core=off,gic-version=2 \
+-m 4096 \
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid 1ccfd97d-5eb4-478a-bbe6-88d254c16db7 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-boot strict=on \
+-device 
pcie-root-port,port=0x8,chassis=1,id=pci.1,bus=pcie.0,multifunction=on,\
+addr=0x1 \
+-device pcie-root-port,port=0x9,chassis=2,id=pci.2,bus=pcie.0,addr=0x1.0x1 \
+-drive file=/var/lib/libvirt/images/guest.qcow2,format=qcow2,if=none,\
+id=drive-virtio-disk0 \
+-device virtio-blk-pci,scsi=off,bus=pci.1,addr=0x0,drive=drive-virtio-disk0,\
+id=virtio-disk0,bootindex=1 \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-msg timestamp=on

[libvirt] [PATCH v2 22/31] qemu: Probe for default CPU types

2019-10-15 Thread Jiri Denemark
QEMU 4.2.0 will report default CPU types used by each machine type and
we will want to start using it.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- no change except for updated test results

 src/qemu/qemu_capabilities.c  | 15 ++-
 src/qemu/qemu_monitor.c   |  1 +
 src/qemu/qemu_monitor.h   |  1 +
 src/qemu/qemu_monitor_json.c  | 12 +++
 .../caps_4.2.0.aarch64.xml| 94 +--
 .../qemucapabilitiesdata/caps_4.2.0.ppc64.xml | 58 ++--
 .../qemucapabilitiesdata/caps_4.2.0.s390x.xml | 28 +++---
 .../caps_4.2.0.x86_64.xml | 92 +-
 8 files changed, 163 insertions(+), 138 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 2a32d575c9..33cd3491b2 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -556,6 +556,7 @@ struct virQEMUCapsMachineType {
 unsigned int maxCpus;
 bool hotplugCpus;
 bool qemuDefault;
+char *defaultCPU;
 };
 
 typedef struct _virQEMUCapsHostCPUData virQEMUCapsHostCPUData;
@@ -1638,7 +1639,8 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps)
 ret->nmachineTypes = qemuCaps->nmachineTypes;
 for (i = 0; i < qemuCaps->nmachineTypes; i++) {
 if (VIR_STRDUP(ret->machineTypes[i].name, 
qemuCaps->machineTypes[i].name) < 0 ||
-VIR_STRDUP(ret->machineTypes[i].alias, 
qemuCaps->machineTypes[i].alias) < 0)
+VIR_STRDUP(ret->machineTypes[i].alias, 
qemuCaps->machineTypes[i].alias) < 0 ||
+VIR_STRDUP(ret->machineTypes[i].defaultCPU, 
qemuCaps->machineTypes[i].defaultCPU) < 0)
 goto error;
 ret->machineTypes[i].maxCpus = qemuCaps->machineTypes[i].maxCpus;
 ret->machineTypes[i].hotplugCpus = 
qemuCaps->machineTypes[i].hotplugCpus;
@@ -1673,6 +1675,7 @@ void virQEMUCapsDispose(void *obj)
 for (i = 0; i < qemuCaps->nmachineTypes; i++) {
 VIR_FREE(qemuCaps->machineTypes[i].name);
 VIR_FREE(qemuCaps->machineTypes[i].alias);
+VIR_FREE(qemuCaps->machineTypes[i].defaultCPU);
 }
 VIR_FREE(qemuCaps->machineTypes);
 
@@ -2406,7 +2409,8 @@ virQEMUCapsProbeQMPMachineTypes(virQEMUCapsPtr qemuCaps,
 mach = &(qemuCaps->machineTypes[qemuCaps->nmachineTypes++]);
 
 if (VIR_STRDUP(mach->alias, machines[i]->alias) < 0 ||
-VIR_STRDUP(mach->name, machines[i]->name) < 0)
+VIR_STRDUP(mach->name, machines[i]->name) < 0 ||
+VIR_STRDUP(mach->defaultCPU, machines[i]->defaultCPU) < 0)
 goto cleanup;
 
 mach->maxCpus = machines[i]->maxCpus;
@@ -3843,6 +3847,8 @@ virQEMUCapsLoadCache(virArch hostArch,
 if (STREQ_NULLABLE(str, "yes"))
 qemuCaps->machineTypes[i].qemuDefault = true;
 VIR_FREE(str);
+
+qemuCaps->machineTypes[i].defaultCPU = virXMLPropString(nodes[i], 
"defaultCPU");
 }
 }
 VIR_FREE(nodes);
@@ -4116,6 +4122,11 @@ virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps)
   qemuCaps->machineTypes[i].maxCpus);
 if (qemuCaps->machineTypes[i].qemuDefault)
 virBufferAddLit(, " default='yes'");
+
+if (qemuCaps->machineTypes[i].defaultCPU)
+virBufferEscapeString(, " defaultCPU='%s'",
+  qemuCaps->machineTypes[i].defaultCPU);
+
 virBufferAddLit(, "/>\n");
 }
 
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 6056219674..6f2d759a78 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3532,6 +3532,7 @@ qemuMonitorMachineInfoFree(qemuMonitorMachineInfoPtr 
machine)
 return;
 VIR_FREE(machine->name);
 VIR_FREE(machine->alias);
+VIR_FREE(machine->defaultCPU);
 VIR_FREE(machine);
 }
 
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index f2af1badd4..c099321ad1 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -1085,6 +1085,7 @@ struct _qemuMonitorMachineInfo {
 char *alias;
 unsigned int maxCpus;
 bool hotplugCpus;
+char *defaultCPU;
 };
 
 int qemuMonitorGetMachines(qemuMonitorPtr mon,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index bc30884223..1309b6a781 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -5504,6 +5504,18 @@ int qemuMonitorJSONGetMachines(qemuMonitorPtr mon,
 
 ignore_value(virJSONValueObjectGetBoolean(child, "hotpluggable-cpus",
   >hotplugCpus));
+
+if (virJSONValueObjectHasKey(child, "default-cpu-type")) {
+if (!(tmp = virJSONValueObjectGetString(child, 
"default-cpu-type"))) {
+virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+   _("query-machines reply has malformed "
+ "'default-cpu-type' data"));
+ 

[libvirt] [PATCH v2 08/31] qemu: Use VIR_AUTOPTR in qemuMonitorJSONGetCPUDefinitions

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_monitor_json.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index d3447dd2f3..418bdcec92 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -5527,8 +5527,8 @@ qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon,
  qemuMonitorCPUDefInfoPtr **cpus)
 {
 int ret = -1;
-virJSONValuePtr cmd;
-virJSONValuePtr reply = NULL;
+VIR_AUTOPTR(virJSONValue) cmd = NULL;
+VIR_AUTOPTR(virJSONValue) reply = NULL;
 virJSONValuePtr data;
 qemuMonitorCPUDefInfoPtr *cpulist = NULL;
 size_t n = 0;
@@ -5630,8 +5630,6 @@ qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon,
 qemuMonitorCPUDefInfoFree(cpulist[i]);
 VIR_FREE(cpulist);
 }
-virJSONValueFree(cmd);
-virJSONValueFree(reply);
 return ret;
 }
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 25/31] qemu: Use VIR_AUTOUNREF in qemuDomainDefPostParse

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_domain.c | 28 
 1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index c638077aa8..9dcba4ef38 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -4617,55 +4617,51 @@ qemuDomainDefPostParse(virDomainDefPtr def,
void *parseOpaque)
 {
 virQEMUDriverPtr driver = opaque;
-virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
+VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver);
 /* Note that qemuCaps may be NULL when this function is called. This
  * function shall not fail in that case. It will be re-run on VM startup
  * with the capabilities populated. */
 virQEMUCapsPtr qemuCaps = parseOpaque;
-int ret = -1;
 
 if (def->os.bootloader || def->os.bootloaderArgs) {
 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("bootloader is not supported by QEMU"));
-goto cleanup;
+return -1;
 }
 
 if (!def->os.machine) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing machine type"));
-goto cleanup;
+return -1;
 }
 
 if (qemuDomainNVRAMPathGenerate(cfg, def) < 0)
-goto cleanup;
+return -1;
 
 if (qemuDomainDefAddDefaultDevices(def, qemuCaps) < 0)
-goto cleanup;
+return -1;
 
 if (qemuCanonicalizeMachine(def, qemuCaps) < 0)
-goto cleanup;
+return -1;
 
 qemuDomainDefEnableDefaultFeatures(def, qemuCaps);
 
 if (qemuDomainRecheckInternalPaths(def, cfg, parseFlags) < 0)
-goto cleanup;
+return -1;
 
 if (qemuSecurityVerify(driver->securityManager, def) < 0)
-goto cleanup;
+return -1;
 
 if (qemuDomainDefVcpusPostParse(def) < 0)
-goto cleanup;
+return -1;
 
 if (qemuDomainDefCPUPostParse(def) < 0)
-goto cleanup;
+return -1;
 
 if (qemuDomainDefTsegPostParse(def, qemuCaps) < 0)
-goto cleanup;
+return -1;
 
-ret = 0;
- cleanup:
-virObjectUnref(cfg);
-return ret;
+return 0;
 }
 
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 21/31] qemu: Store typename from query-cpu-definitions in qemuCaps

2019-10-15 Thread Jiri Denemark
We need to create a mapping between CPU model names and their
corresponding QOM types.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- ignore empty typename strings

 src/qemu/qemu_capabilities.c  |   3 +
 src/qemu/qemu_monitor.c   |   2 +
 src/qemu/qemu_monitor.h   |   1 +
 src/qemu/qemu_monitor_json.c  |   5 +
 .../caps_2.10.0.aarch64.xml   | 126 ++---
 .../caps_2.10.0.ppc64.xml | 512 -
 .../caps_2.10.0.s390x.xml | 264 -
 .../caps_2.10.0.x86_64.xml| 132 ++---
 .../caps_2.11.0.s390x.xml | 264 -
 .../caps_2.11.0.x86_64.xml| 136 ++---
 .../caps_2.12.0.aarch64.xml   | 136 ++---
 .../caps_2.12.0.ppc64.xml | 516 +-
 .../caps_2.12.0.s390x.xml | 264 -
 .../caps_2.12.0.x86_64.xml| 180 +++---
 .../qemucapabilitiesdata/caps_2.9.0.ppc64.xml | 512 -
 .../qemucapabilitiesdata/caps_2.9.0.s390x.xml | 256 -
 .../caps_2.9.0.x86_64.xml | 128 ++---
 .../qemucapabilitiesdata/caps_3.0.0.ppc64.xml | 516 +-
 .../qemucapabilitiesdata/caps_3.0.0.s390x.xml | 272 -
 .../caps_3.0.0.x86_64.xml | 184 +++
 .../qemucapabilitiesdata/caps_3.1.0.ppc64.xml | 516 +-
 .../caps_3.1.0.x86_64.xml | 196 +++
 .../caps_4.0.0.aarch64.xml| 148 ++---
 .../qemucapabilitiesdata/caps_4.0.0.ppc64.xml | 516 +-
 .../qemucapabilitiesdata/caps_4.0.0.s390x.xml | 284 +-
 .../caps_4.0.0.x86_64.xml | 196 +++
 .../caps_4.1.0.x86_64.xml | 400 +++---
 .../caps_4.2.0.aarch64.xml| 148 ++---
 .../qemucapabilitiesdata/caps_4.2.0.ppc64.xml | 516 +-
 .../qemucapabilitiesdata/caps_4.2.0.s390x.xml | 300 +-
 .../caps_4.2.0.x86_64.xml | 400 +++---
 31 files changed, 4020 insertions(+), 4009 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index a274cef120..2a32d575c9 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3548,6 +3548,8 @@ virQEMUCapsLoadCPUModels(virQEMUCapsPtr qemuCaps,
 return -1;
 }
 
+cpu->type = virXMLPropString(nodes[i], "typename");
+
 node = ctxt->node;
 ctxt->node = nodes[i];
 nblockers = virXPathNodeSet("./blocker", ctxt, );
@@ -4010,6 +4012,7 @@ virQEMUCapsFormatCPUModels(virQEMUCapsPtr qemuCaps,
 
 virBufferAsprintf(buf, "name);
+virBufferEscapeString(buf, " typename='%s'", cpu->type);
 if (cpu->usable) {
 virBufferAsprintf(buf, " usable='%s'",
   virDomainCapsCPUUsableTypeToString(cpu->usable));
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 8d86112f30..6056219674 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3559,6 +3559,7 @@ qemuMonitorCPUDefsFree(qemuMonitorCPUDefsPtr defs)
 for (i = 0; i < defs->ncpus; i++) {
 virStringListFree(defs->cpus[i].blockers);
 VIR_FREE(defs->cpus[i].name);
+VIR_FREE(defs->cpus[i].type);
 }
 
 VIR_FREE(defs->cpus);
@@ -3605,6 +3606,7 @@ qemuMonitorCPUDefsCopy(qemuMonitorCPUDefsPtr *dst,
 cpuDst->usable = cpuSrc->usable;
 
 if (VIR_STRDUP(cpuDst->name, cpuSrc->name) < 0 ||
+VIR_STRDUP(cpuDst->type, cpuSrc->type) < 0 ||
 virStringListCopy(>blockers,
   (const char **)cpuSrc->blockers) < 0)
 return -1;
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 4f355c760a..f2af1badd4 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -1098,6 +1098,7 @@ typedef qemuMonitorCPUDefInfo *qemuMonitorCPUDefInfoPtr;
 struct _qemuMonitorCPUDefInfo {
 virTristateBool usable;
 char *name;
+char *type;
 char **blockers; /* NULL-terminated string list */
 };
 
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index de75c46dc1..bc30884223 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -5571,6 +5571,11 @@ qemuMonitorJSONGetCPUDefinitions(qemuMonitorPtr mon,
 if (VIR_STRDUP(cpu->name, tmp) < 0)
 return -1;
 
+if ((tmp = virJSONValueObjectGetString(child, "typename")) &&
+*tmp &&
+VIR_STRDUP(cpu->type, tmp) < 0)
+return -1;
+
 if (virJSONValueObjectHasKey(child, "unavailable-features")) {
 virJSONValuePtr blockers;
 size_t j;
...
diff --git a/tests/qemucapabilitiesdata/caps_4.2.0.x86_64.xml 
b/tests/qemucapabilitiesdata/caps_4.2.0.x86_64.xml
index ab884239fe..176613fa2a 100644
--- 

[libvirt] [PATCH v2 20/31] conf: Drop virDomainCapsCPUModelsAddSteal

2019-10-15 Thread Jiri Denemark
Both virDomainCapsCPUModelsAdd and virDomainCapsCPUModelsAddSteal are so
simple we can just squash the code in a single function.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- no change

 src/conf/domain_capabilities.c | 33 ++---
 src/conf/domain_capabilities.h |  4 
 src/libvirt_private.syms   |  1 -
 3 files changed, 10 insertions(+), 28 deletions(-)

diff --git a/src/conf/domain_capabilities.c b/src/conf/domain_capabilities.c
index a13463a6e7..dadb2a1183 100644
--- a/src/conf/domain_capabilities.c
+++ b/src/conf/domain_capabilities.c
@@ -185,27 +185,6 @@ virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr old)
 }
 
 
-int
-virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr cpuModels,
-   char **name,
-   virDomainCapsCPUUsable usable,
-   char ***blockers)
-{
-if (VIR_RESIZE_N(cpuModels->models, cpuModels->nmodels_max,
- cpuModels->nmodels, 1) < 0)
-return -1;
-
-cpuModels->models[cpuModels->nmodels].usable = usable;
-VIR_STEAL_PTR(cpuModels->models[cpuModels->nmodels].name, *name);
-
-if (blockers)
-VIR_STEAL_PTR(cpuModels->models[cpuModels->nmodels].blockers, 
*blockers);
-
-cpuModels->nmodels++;
-return 0;
-}
-
-
 int
 virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr cpuModels,
   const char *name,
@@ -214,6 +193,7 @@ virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr 
cpuModels,
 {
 VIR_AUTOFREE(char *) nameCopy = NULL;
 VIR_AUTOSTRINGLIST blockersCopy = NULL;
+virDomainCapsCPUModelPtr cpu;
 
 if (VIR_STRDUP(nameCopy, name) < 0)
 return -1;
@@ -221,10 +201,17 @@ virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr 
cpuModels,
 if (virStringListCopy(, (const char **)blockers) < 0)
 return -1;
 
-if (virDomainCapsCPUModelsAddSteal(cpuModels, ,
-   usable, ) < 0)
+if (VIR_RESIZE_N(cpuModels->models, cpuModels->nmodels_max,
+ cpuModels->nmodels, 1) < 0)
 return -1;
 
+cpu = cpuModels->models + cpuModels->nmodels;
+cpuModels->nmodels++;
+
+cpu->usable = usable;
+VIR_STEAL_PTR(cpu->name, nameCopy);
+VIR_STEAL_PTR(cpu->blockers, blockersCopy);
+
 return 0;
 }
 
diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h
index bdd0bd9253..8b220e3bf8 100644
--- a/src/conf/domain_capabilities.h
+++ b/src/conf/domain_capabilities.h
@@ -195,10 +195,6 @@ virDomainCapsPtr virDomainCapsNew(const char *path,
 
 virDomainCapsCPUModelsPtr virDomainCapsCPUModelsNew(size_t nmodels);
 virDomainCapsCPUModelsPtr virDomainCapsCPUModelsCopy(virDomainCapsCPUModelsPtr 
old);
-int virDomainCapsCPUModelsAddSteal(virDomainCapsCPUModelsPtr cpuModels,
-   char **name,
-   virDomainCapsCPUUsable usable,
-   char ***blockers);
 int virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr cpuModels,
   const char *name,
   virDomainCapsCPUUsable usable,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 641b1f93bd..b35934be1f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -191,7 +191,6 @@ virDomainAuditVcpu;
 
 # conf/domain_capabilities.h
 virDomainCapsCPUModelsAdd;
-virDomainCapsCPUModelsAddSteal;
 virDomainCapsCPUModelsCopy;
 virDomainCapsCPUModelsGet;
 virDomainCapsCPUModelsNew;
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 18/31] qemu: Switch qemuCaps to use qemuMonitorCPUDefs

2019-10-15 Thread Jiri Denemark
We will need to keep some QEMU-specific data for each CPU model
supported by a QEMU binary. Instead of complicating the generic
virDomainCapsCPUModelsPtr, we can just directly store
qemuMonitorCPUDefsPtr returned by the capabilities probing code.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- adapted to changes made by the new patches

 src/qemu/qemu_capabilities.c | 109 +--
 1 file changed, 53 insertions(+), 56 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 436d65f578..a274cef120 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -607,8 +607,8 @@ struct _virQEMUCaps {
 virArch arch;
 
 virHashTablePtr domCapsCache;
-virDomainCapsCPUModelsPtr kvmCPUModels;
-virDomainCapsCPUModelsPtr tcgCPUModels;
+qemuMonitorCPUDefsPtr kvmCPUModels;
+qemuMonitorCPUDefsPtr tcgCPUModels;
 
 size_t nmachineTypes;
 struct virQEMUCapsMachineType *machineTypes;
@@ -1625,17 +1625,9 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr 
qemuCaps)
 
 ret->arch = qemuCaps->arch;
 
-if (qemuCaps->kvmCPUModels) {
-ret->kvmCPUModels = virDomainCapsCPUModelsCopy(qemuCaps->kvmCPUModels);
-if (!ret->kvmCPUModels)
-goto error;
-}
-
-if (qemuCaps->tcgCPUModels) {
-ret->tcgCPUModels = virDomainCapsCPUModelsCopy(qemuCaps->tcgCPUModels);
-if (!ret->tcgCPUModels)
-goto error;
-}
+if (qemuMonitorCPUDefsCopy(>kvmCPUModels, qemuCaps->kvmCPUModels) < 0 
||
+qemuMonitorCPUDefsCopy(>tcgCPUModels, qemuCaps->tcgCPUModels) < 0)
+goto error;
 
 if (virQEMUCapsHostCPUDataCopy(>kvmCPU, >kvmCPU) < 0 ||
 virQEMUCapsHostCPUDataCopy(>tcgCPU, >tcgCPU) < 0)
@@ -1861,25 +1853,36 @@ virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
  virDomainCapsCPUUsable usable)
 {
 size_t i;
-virDomainCapsCPUModelsPtr cpus = NULL;
+size_t start;
+qemuMonitorCPUDefsPtr defs = NULL;
 
 if (type == VIR_DOMAIN_VIRT_KVM && qemuCaps->kvmCPUModels)
-cpus = qemuCaps->kvmCPUModels;
+defs = qemuCaps->kvmCPUModels;
 else if (type == VIR_DOMAIN_VIRT_QEMU && qemuCaps->tcgCPUModels)
-cpus = qemuCaps->tcgCPUModels;
+defs = qemuCaps->tcgCPUModels;
+
+if (defs) {
+start = defs->ncpus;
 
-if (!cpus) {
-if (!(cpus = virDomainCapsCPUModelsNew(count)))
+if (VIR_EXPAND_N(defs->cpus, defs->ncpus, count) < 0)
+return -1;
+} else {
+start = 0;
+
+if (!(defs = qemuMonitorCPUDefsNew(count)))
 return -1;
 
 if (type == VIR_DOMAIN_VIRT_KVM)
-qemuCaps->kvmCPUModels = cpus;
+qemuCaps->kvmCPUModels = defs;
 else
-qemuCaps->tcgCPUModels = cpus;
+qemuCaps->tcgCPUModels = defs;
 }
 
 for (i = 0; i < count; i++) {
-if (virDomainCapsCPUModelsAdd(cpus, name[i], usable, NULL) < 0)
+qemuMonitorCPUDefInfoPtr cpu = defs->cpus + start + i;
+
+cpu->usable = usable;
+if (VIR_STRDUP(cpu->name, name[i]) < 0)
 return -1;
 }
 
@@ -1928,20 +1931,17 @@ virQEMUCapsGetCPUModels(virQEMUCapsPtr qemuCaps,
 const char **modelWhitelist,
 const char **modelBlacklist)
 {
-virDomainCapsCPUModelsPtr cpuModels;
+qemuMonitorCPUDefsPtr defs;
 
 if (type == VIR_DOMAIN_VIRT_KVM)
-cpuModels = qemuCaps->kvmCPUModels;
+defs = qemuCaps->kvmCPUModels;
 else
-cpuModels = qemuCaps->tcgCPUModels;
+defs = qemuCaps->tcgCPUModels;
 
-if (!cpuModels)
+if (!defs)
 return NULL;
 
-if (modelWhitelist || modelBlacklist)
-return virDomainCapsCPUModelsFilter(cpuModels, modelWhitelist, 
modelBlacklist);
-
-return virDomainCapsCPUModelsCopy(cpuModels);
+return virQEMUCapsCPUDefsToModels(defs, modelWhitelist, modelBlacklist);
 }
 
 
@@ -2001,7 +2001,7 @@ virQEMUCapsIsCPUModeSupported(virQEMUCapsPtr qemuCaps,
   virDomainVirtType type,
   virCPUMode mode)
 {
-virDomainCapsCPUModelsPtr cpus;
+qemuMonitorCPUDefsPtr cpus;
 
 switch (mode) {
 case VIR_CPU_MODE_HOST_PASSTHROUGH:
@@ -2017,7 +2017,7 @@ virQEMUCapsIsCPUModeSupported(virQEMUCapsPtr qemuCaps,
 cpus = qemuCaps->kvmCPUModels;
 else
 cpus = qemuCaps->tcgCPUModels;
-return cpus && cpus->nmodels > 0;
+return cpus && cpus->ncpus > 0;
 
 case VIR_CPU_MODE_LAST:
 break;
@@ -2553,18 +2553,18 @@ virQEMUCapsProbeQMPCPUDefinitions(virQEMUCapsPtr 
qemuCaps,
   qemuMonitorPtr mon,
   bool tcg)
 {
-virDomainCapsCPUModelsPtr models = NULL;
+qemuMonitorCPUDefsPtr defs = NULL;
 
 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_QUERY_CPU_DEFINITIONS))
 

[libvirt] [PATCH v2 07/31] qemu: Use virQEMUCapsGetCPUDefinitions more

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_capabilities.c | 21 +++--
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index e6a7ee4f14..b608eb1a43 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -5297,24 +5297,17 @@ virQEMUCapsFillDomainCPUCaps(virCapsPtr caps,
 
 if (virQEMUCapsIsCPUModeSupported(qemuCaps, caps, domCaps->virttype,
   VIR_CPU_MODE_CUSTOM)) {
-virDomainCapsCPUModelsPtr filtered = NULL;
-char **models = NULL;
 const char *blacklist[] = { "host", NULL };
+VIR_AUTOSTRINGLIST models = NULL;
 
 if (virCPUGetModels(domCaps->arch, ) >= 0) {
-virDomainCapsCPUModelsPtr cpus;
-
-if (domCaps->virttype == VIR_DOMAIN_VIRT_KVM)
-cpus = qemuCaps->kvmCPUModels;
-else
-cpus = qemuCaps->tcgCPUModels;
-
-filtered = virDomainCapsCPUModelsFilter(cpus,
-(const char **)models,
-blacklist);
-virStringListFree(models);
+domCaps->cpu.custom = virQEMUCapsGetCPUDefinitions(qemuCaps,
+   
domCaps->virttype,
+   (const char 
**)models,
+   blacklist);
+} else {
+domCaps->cpu.custom = NULL;
 }
-domCaps->cpu.custom = filtered;
 }
 
 return 0;
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 00/31] qemu: Store default CPU in domain XML

2019-10-15 Thread Jiri Denemark
When starting a domain without a CPU model specified in the domain XML,
QEMU will choose a default one. Which is fine unless the domain gets
migrated to another host because libvirt doesn't perform any CPU ABI
checks and the virtual CPU provided by QEMU on the destination host can
differ from the one on the source host.

With QEMU 4.2.0 we can probe for the default CPU model used by QEMU for
a particular machine type and store it in the domain XML. This way the
chosen CPU model is more visible to users and libvirt will make sure
the guest will see the exact same CPU after migration.

https://bugzilla.redhat.com/show_bug.cgi?id=1598151
https://bugzilla.redhat.com/show_bug.cgi?id=1598162

---

Some patches were too large so I decided to shorten them before sending
to the list. You can check the full version of this series with

git fetch https://gitlab.com/jirkade/libvirt cpu-default-type

Jiri Denemark (31):
  tests: Add capabilities for QEMU 4.2.0 on s390x
  domcapstest: Add test case for QEMU 4.2.0 on s390x
  conf: Use VIR_AUTO* in virDomainCapsCPUModelsAdd
  conf: Drop nameLen parameter from virDomainCapsCPUModelsAdd
  qemu: Copy CPU models in virQEMUCapsGetCPUDefinitions
  qemu: Filter models in virQEMUCapsGetCPUDefinitions
  qemu: Use virQEMUCapsGetCPUDefinitions more
  qemu: Use VIR_AUTOPTR in qemuMonitorJSONGetCPUDefinitions
  qemu: Change return type of virQEMUCapsFetchCPUDefinitions
  qemu: Introduce qemuMonitorCPUDefs struct
  qemu: Flatten qemuMonitorCPUDefs.cpus
  build: Export virStringListCopy internal API
  qemu: Add qemuMonitorCPUDefsCopy
  qemu: Use VIR_AUTOFREE in virQEMUCapsLoadCPUModels
  qemu: Introduce virQEMUCapsCPUDefsToModels
  qemu: Rename virQEMUCaps{Get,Fetch}CPUDefinitions
  qemu: Split virQEMUCapsFetchCPUModels
  qemu: Switch qemuCaps to use qemuMonitorCPUDefs
  conf: Drop unused virDomainCapsCPUModelsFilter
  conf: Drop virDomainCapsCPUModelsAddSteal
  qemu: Store typename from query-cpu-definitions in qemuCaps
  qemu: Probe for default CPU types
  qemu: Introduce virQEMUCapsGetMachineDefaultCPU
  qemu: Drop unused virQEMUCapsGetDefaultMachine
  qemu: Use VIR_AUTOUNREF in qemuDomainDefPostParse
  conf: Define VIR_AUTOPTR for virCPUDef
  qemuxml2*test: Add test cases for default CPU models on aarch64
  qemuxml2*test: Add test cases for default CPU models on ppc64
  qemuxml2*test: Add test cases for default CPU models on s390x
  qemuxml2*test: Add test cases for default CPU models on x86_64
  qemu: Store default CPU in domain XML

 src/conf/cpu_conf.h   | 1 +
 src/conf/domain_capabilities.c|86 +-
 src/conf/domain_capabilities.h| 8 -
 src/libvirt_private.syms  | 3 +-
 src/qemu/qemu_capabilities.c  |   358 +-
 src/qemu/qemu_capabilities.h  |15 +-
 src/qemu/qemu_domain.c|84 +-
 src/qemu/qemu_driver.c| 4 +-
 src/qemu/qemu_monitor.c   |74 +-
 src/qemu/qemu_monitor.h   |18 +-
 src/qemu/qemu_monitor_json.c  |82 +-
 src/qemu/qemu_monitor_json.h  | 2 +-
 src/qemu/qemu_process.c   |24 +-
 tests/cputest.c   | 7 +-
 .../domaincapsschemadata/qemu_4.2.0.s390x.xml |   202 +
 tests/domaincapstest.c| 4 +
 .../caps_2.10.0.aarch64.xml   |   126 +-
 .../caps_2.10.0.ppc64.xml |   512 +-
 .../caps_2.10.0.s390x.xml |   264 +-
 .../caps_2.10.0.x86_64.xml|   132 +-
 .../caps_2.11.0.s390x.xml |   264 +-
 .../caps_2.11.0.x86_64.xml|   136 +-
 .../caps_2.12.0.aarch64.xml   |   136 +-
 .../caps_2.12.0.ppc64.xml |   516 +-
 .../caps_2.12.0.s390x.xml |   264 +-
 .../caps_2.12.0.x86_64.xml|   180 +-
 .../qemucapabilitiesdata/caps_2.9.0.ppc64.xml |   512 +-
 .../qemucapabilitiesdata/caps_2.9.0.s390x.xml |   256 +-
 .../caps_2.9.0.x86_64.xml |   128 +-
 .../qemucapabilitiesdata/caps_3.0.0.ppc64.xml |   516 +-
 .../qemucapabilitiesdata/caps_3.0.0.s390x.xml |   272 +-
 .../caps_3.0.0.x86_64.xml |   184 +-
 .../qemucapabilitiesdata/caps_3.1.0.ppc64.xml |   516 +-
 .../caps_3.1.0.x86_64.xml |   196 +-
 .../caps_4.0.0.aarch64.xml|   148 +-
 .../qemucapabilitiesdata/caps_4.0.0.ppc64.xml |   516 +-
 .../qemucapabilitiesdata/caps_4.0.0.s390x.xml |   284 +-
 .../caps_4.0.0.x86_64.xml |   196 +-
 .../caps_4.1.0.x86_64.xml |   400 +-
 .../caps_4.2.0.aarch64.xml|   242 +-
 .../qemucapabilitiesdata/caps_4.2.0.ppc64.xml |   574 +-
 .../caps_4.2.0.s390x.replies  | 22414 
 

[libvirt] [PATCH v2 13/31] qemu: Add qemuMonitorCPUDefsCopy

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_monitor.c | 33 +
 src/qemu/qemu_monitor.h |  2 ++
 2 files changed, 35 insertions(+)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 87a9d06d45..8d86112f30 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3582,6 +3582,39 @@ qemuMonitorCPUDefsNew(size_t count)
 }
 
 
+int
+qemuMonitorCPUDefsCopy(qemuMonitorCPUDefsPtr *dst,
+   qemuMonitorCPUDefsPtr src)
+{
+VIR_AUTOPTR(qemuMonitorCPUDefs) defs = NULL;
+size_t i;
+
+if (!src) {
+*dst = NULL;
+return 0;
+}
+
+if (!(defs = qemuMonitorCPUDefsNew(src->ncpus)))
+return -1;
+
+defs->ncpus = src->ncpus;
+for (i = 0; i < src->ncpus; i++) {
+qemuMonitorCPUDefInfoPtr cpuDst = defs->cpus + i;
+qemuMonitorCPUDefInfoPtr cpuSrc = src->cpus + i;
+
+cpuDst->usable = cpuSrc->usable;
+
+if (VIR_STRDUP(cpuDst->name, cpuSrc->name) < 0 ||
+virStringListCopy(>blockers,
+  (const char **)cpuSrc->blockers) < 0)
+return -1;
+}
+
+VIR_STEAL_PTR(*dst, defs);
+return 0;
+}
+
+
 int
 qemuMonitorGetCPUModelExpansion(qemuMonitorPtr mon,
 qemuMonitorCPUModelExpansionType type,
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index aa2a0e392a..4f355c760a 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -,6 +,8 @@ struct _qemuMonitorCPUDefs {
 int qemuMonitorGetCPUDefinitions(qemuMonitorPtr mon,
  qemuMonitorCPUDefsPtr *cpuDefs);
 qemuMonitorCPUDefsPtr qemuMonitorCPUDefsNew(size_t count);
+int qemuMonitorCPUDefsCopy(qemuMonitorCPUDefsPtr *dst,
+   qemuMonitorCPUDefsPtr src);
 void qemuMonitorCPUDefsFree(qemuMonitorCPUDefsPtr defs);
 VIR_DEFINE_AUTOPTR_FUNC(qemuMonitorCPUDefs, qemuMonitorCPUDefsFree);
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 09/31] qemu: Change return type of virQEMUCapsFetchCPUDefinitions

2019-10-15 Thread Jiri Denemark
The function would return a valid virDomainCapsCPUModelsPtr with empty
CPU models list if query-cpu-definitions exists in QEMU, but returns
GenericError meaning it's not in fact implemented. This behaviour is a
bit strange especially after such virDomainCapsCPUModels structure is
stored in capabilities XML and parsed back, which will result in NULL
virDomainCapsCPUModelsPtr rather than a structure containing nothing.

Let's just keep virDomainCapsCPUModelsPtr NULL if the QMP command is not
implemented and change the return value to int so that callers can
easily check for failure or success.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 src/qemu/qemu_capabilities.c | 34 +-
 src/qemu/qemu_capabilities.h |  5 +++--
 src/qemu/qemu_process.c  | 17 ++---
 3 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index b608eb1a43..6fa5e06edb 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2452,17 +2452,26 @@ virQEMUCapsProbeQMPMachineProps(virQEMUCapsPtr qemuCaps,
 }
 
 
-virDomainCapsCPUModelsPtr
+int
 virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
-   virArch arch)
+   virArch arch,
+   virDomainCapsCPUModelsPtr *cpuModels)
 {
 virDomainCapsCPUModelsPtr models = NULL;
 qemuMonitorCPUDefInfoPtr *cpus = NULL;
 int ncpus = 0;
 size_t i;
+int ret = -1;
+
+*cpuModels = NULL;
 
 if ((ncpus = qemuMonitorGetCPUDefinitions(mon, )) < 0)
-return NULL;
+return -1;
+
+if (ncpus == 0) {
+ret = 0;
+goto cleanup;
+}
 
 /* QEMU 2.11 for Power renamed all CPU models to lower case, we need to
  * translate them back to libvirt's upper case model names. */
@@ -2471,7 +2480,7 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 char **name;
 
 if (virCPUGetModels(arch, ) < 0)
-goto error;
+goto cleanup;
 
 for (name = libvirtModels; name && *name; name++) {
 for (i = 0; i < ncpus; i++) {
@@ -2480,13 +2489,13 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 
 VIR_FREE(cpus[i]->name);
 if (VIR_STRDUP(cpus[i]->name, *name) < 0)
-goto error;
+goto cleanup;
 }
 }
 }
 
 if (!(models = virDomainCapsCPUModelsNew(ncpus)))
-goto error;
+goto cleanup;
 
 for (i = 0; i < ncpus; i++) {
 virDomainCapsCPUUsable usable = VIR_DOMCAPS_CPU_USABLE_UNKNOWN;
@@ -2498,19 +2507,18 @@ virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
 
 if (virDomainCapsCPUModelsAddSteal(models, [i]->name, usable,
[i]->blockers) < 0)
-goto error;
+goto cleanup;
 }
 
+VIR_STEAL_PTR(*cpuModels, models);
+ret = 0;
+
  cleanup:
 for (i = 0; i < ncpus; i++)
 qemuMonitorCPUDefInfoFree(cpus[i]);
 VIR_FREE(cpus);
-return models;
-
- error:
 virObjectUnref(models);
-models = NULL;
-goto cleanup;
+return ret;
 }
 
 
@@ -2524,7 +2532,7 @@ virQEMUCapsProbeQMPCPUDefinitions(virQEMUCapsPtr qemuCaps,
 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_QUERY_CPU_DEFINITIONS))
 return 0;
 
-if (!(models = virQEMUCapsFetchCPUDefinitions(mon, qemuCaps->arch)))
+if (virQEMUCapsFetchCPUDefinitions(mon, qemuCaps->arch, ) < 0)
 return -1;
 
 if (tcg || !virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM))
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 67dccc522e..ba3fe3d2b6 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -573,8 +573,9 @@ virDomainCapsCPUModelsPtr 
virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
virDomainVirtType type,
const char 
**modelWhitelist,
const char 
**modelBlacklist);
-virDomainCapsCPUModelsPtr virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
- virArch arch);
+int virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
+   virArch arch,
+   virDomainCapsCPUModelsPtr *cpuModels);
 
 typedef enum {
 /* Host CPU definition reported in domain capabilities. */
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index f37acab9e4..93fe994f8e 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -4353,27 +4353,30 @@ qemuProcessUpdateAndVerifyCPU(virQEMUDriverPtr driver,
 }
 
 
-static virDomainCapsCPUModelsPtr
+static int
 qemuProcessFetchCPUDefinitions(virQEMUDriverPtr driver,
virDomainObjPtr vm,
-

[libvirt] [PATCH v2 16/31] qemu: Rename virQEMUCaps{Get, Fetch}CPUDefinitions

2019-10-15 Thread Jiri Denemark
The functions return virDomainCapsCPUModelsPtr and thus they should be
called *CPUModels for consistency. Functions called *CPUDefinitions will
work on qemuMonitorCPUDefsPtr.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 src/qemu/qemu_capabilities.c | 28 ++--
 src/qemu/qemu_capabilities.h | 14 +++---
 src/qemu/qemu_driver.c   |  2 +-
 src/qemu/qemu_process.c  |  4 ++--
 tests/cputest.c  |  2 +-
 5 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 87ac9bacdc..cdc3a2d4b0 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1923,10 +1923,10 @@ virQEMUCapsCPUDefsToModels(qemuMonitorCPUDefsPtr defs,
 
 
 virDomainCapsCPUModelsPtr
-virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
- virDomainVirtType type,
- const char **modelWhitelist,
- const char **modelBlacklist)
+virQEMUCapsGetCPUModels(virQEMUCapsPtr qemuCaps,
+virDomainVirtType type,
+const char **modelWhitelist,
+const char **modelBlacklist)
 {
 virDomainCapsCPUModelsPtr cpuModels;
 
@@ -2488,9 +2488,9 @@ virQEMUCapsProbeQMPMachineProps(virQEMUCapsPtr qemuCaps,
 
 
 int
-virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
-   virArch arch,
-   virDomainCapsCPUModelsPtr *cpuModels)
+virQEMUCapsFetchCPUModels(qemuMonitorPtr mon,
+  virArch arch,
+  virDomainCapsCPUModelsPtr *cpuModels)
 {
 VIR_AUTOPTR(qemuMonitorCPUDefs) defs = NULL;
 size_t i;
@@ -2541,7 +2541,7 @@ virQEMUCapsProbeQMPCPUDefinitions(virQEMUCapsPtr qemuCaps,
 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_QUERY_CPU_DEFINITIONS))
 return 0;
 
-if (virQEMUCapsFetchCPUDefinitions(mon, qemuCaps->arch, ) < 0)
+if (virQEMUCapsFetchCPUModels(mon, qemuCaps->arch, ) < 0)
 return -1;
 
 if (tcg || !virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM))
@@ -3164,7 +3164,7 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
 if (!(data = virQEMUCapsGetCPUModelX86Data(qemuCaps, model, migratable)))
 goto cleanup;
 
-cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type, NULL, NULL);
+cpuModels = virQEMUCapsGetCPUModels(qemuCaps, type, NULL, NULL);
 
 if (cpuDecode(cpu, data, cpuModels) < 0)
 goto cleanup;
@@ -3253,7 +3253,7 @@ virQEMUCapsInitHostCPUModel(virQEMUCapsPtr qemuCaps,
 
 VIR_DEBUG("No host CPU model info from QEMU; probing host CPU 
directly");
 
-cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type, NULL, NULL);
+cpuModels = virQEMUCapsGetCPUModels(qemuCaps, type, NULL, NULL);
 hostCPU = virQEMUCapsProbeHostCPU(hostArch, cpuModels);
 
 if (!hostCPU ||
@@ -5308,10 +5308,10 @@ virQEMUCapsFillDomainCPUCaps(virCapsPtr caps,
 VIR_AUTOSTRINGLIST models = NULL;
 
 if (virCPUGetModels(domCaps->arch, ) >= 0) {
-domCaps->cpu.custom = virQEMUCapsGetCPUDefinitions(qemuCaps,
-   
domCaps->virttype,
-   (const char 
**)models,
-   blacklist);
+domCaps->cpu.custom = virQEMUCapsGetCPUModels(qemuCaps,
+  domCaps->virttype,
+  (const char 
**)models,
+  blacklist);
 } else {
 domCaps->cpu.custom = NULL;
 }
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index ba3fe3d2b6..923706c3bf 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -569,13 +569,13 @@ int virQEMUCapsAddCPUDefinitions(virQEMUCapsPtr qemuCaps,
  const char **name,
  size_t count,
  virDomainCapsCPUUsable usable);
-virDomainCapsCPUModelsPtr virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
-   virDomainVirtType type,
-   const char 
**modelWhitelist,
-   const char 
**modelBlacklist);
-int virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
-   virArch arch,
-   virDomainCapsCPUModelsPtr *cpuModels);
+virDomainCapsCPUModelsPtr virQEMUCapsGetCPUModels(virQEMUCapsPtr qemuCaps,
+  virDomainVirtType type,
+  const char **modelWhitelist,

[libvirt] [PATCH v2 17/31] qemu: Split virQEMUCapsFetchCPUModels

2019-10-15 Thread Jiri Denemark
Most of the code moved to a new virQEMUCapsFetchCPUDefinitions function
and the existing virQEMUCapsFetchCPUModels just becomes a small wrapper
around virQEMUCapsFetchCPUDefinitions and virQEMUCapsCPUDefsToModels.

Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 src/qemu/qemu_capabilities.c | 29 +++--
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index cdc3a2d4b0..436d65f578 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2487,15 +2487,15 @@ virQEMUCapsProbeQMPMachineProps(virQEMUCapsPtr qemuCaps,
 }
 
 
-int
-virQEMUCapsFetchCPUModels(qemuMonitorPtr mon,
-  virArch arch,
-  virDomainCapsCPUModelsPtr *cpuModels)
+static int
+virQEMUCapsFetchCPUDefinitions(qemuMonitorPtr mon,
+   virArch arch,
+   qemuMonitorCPUDefsPtr *cpuDefs)
 {
 VIR_AUTOPTR(qemuMonitorCPUDefs) defs = NULL;
 size_t i;
 
-*cpuModels = NULL;
+*cpuDefs = NULL;
 
 if (qemuMonitorGetCPUDefinitions(mon, ) < 0)
 return -1;
@@ -2524,7 +2524,24 @@ virQEMUCapsFetchCPUModels(qemuMonitorPtr mon,
 }
 }
 
-if (!(*cpuModels = virQEMUCapsCPUDefsToModels(defs, NULL, NULL)))
+VIR_STEAL_PTR(*cpuDefs, defs);
+return 0;
+}
+
+
+int
+virQEMUCapsFetchCPUModels(qemuMonitorPtr mon,
+  virArch arch,
+  virDomainCapsCPUModelsPtr *cpuModels)
+{
+VIR_AUTOPTR(qemuMonitorCPUDefs) defs = NULL;
+
+*cpuModels = NULL;
+
+if (virQEMUCapsFetchCPUDefinitions(mon, arch, ) < 0)
+return -1;
+
+if (defs && !(*cpuModels = virQEMUCapsCPUDefsToModels(defs, NULL, NULL)))
 return -1;
 
 return 0;
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 01/31] tests: Add capabilities for QEMU 4.2.0 on s390x

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 .../caps_4.2.0.s390x.replies  | 22414 
 .../qemucapabilitiesdata/caps_4.2.0.s390x.xml |  3345 +++
 2 files changed, 25759 insertions(+)
 create mode 100644 tests/qemucapabilitiesdata/caps_4.2.0.s390x.replies
 create mode 100644 tests/qemucapabilitiesdata/caps_4.2.0.s390x.xml

diff --git a/tests/qemucapabilitiesdata/caps_4.2.0.s390x.replies 
b/tests/qemucapabilitiesdata/caps_4.2.0.s390x.replies
new file mode 100644
index 00..68d70de415
--- /dev/null
+++ b/tests/qemucapabilitiesdata/caps_4.2.0.s390x.replies
@@ -0,0 +1,22414 @@
+{
+  "execute": "qmp_capabilities",
+  "id": "libvirt-1"
+}
+
+{
+  "return": {
+  },
+  "id": "libvirt-1"
+}
+
+{
+  "execute": "query-version",
+  "id": "libvirt-2"
+}
+
+{
+  "return": {
+"qemu": {
+  "micro": 50,
+  "minor": 1,
+  "major": 4
+},
+"package": "v4.1.0-1484-g3779d18011"
+  },
+  "id": "libvirt-2"
+}
...
diff --git a/tests/qemucapabilitiesdata/caps_4.2.0.s390x.xml 
b/tests/qemucapabilitiesdata/caps_4.2.0.s390x.xml
new file mode 100644
index 00..1c9a383816
--- /dev/null
+++ b/tests/qemucapabilitiesdata/caps_4.2.0.s390x.xml
@@ -0,0 +1,3345 @@
+
+  0
+  0
+  0
+  
...
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 12/31] build: Export virStringListCopy internal API

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- no change

 src/libvirt_private.syms | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7d62df8086..1af18b880b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3077,6 +3077,7 @@ virStringIsEmpty;
 virStringIsPrintable;
 virStringListAdd;
 virStringListAutoFree;
+virStringListCopy;
 virStringListFree;
 virStringListFreeCount;
 virStringListGetFirstWithPrefix;
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 05/31] qemu: Copy CPU models in virQEMUCapsGetCPUDefinitions

2019-10-15 Thread Jiri Denemark
Rather than returning a direct pointer the list stored in qemuCaps the
function now creates a new copy of the CPU models list.

The main purpose of this seemingly useless change is to update callers
to free the result returned by virQEMUCapsGetCPUDefinitions because the
internals of this function will change significantly in the following
patches.

Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/qemu/qemu_capabilities.c | 23 ++-
 src/qemu/qemu_driver.c   |  2 +-
 src/qemu/qemu_process.c  |  7 +--
 tests/cputest.c  |  1 -
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 14939fc91d..a40bf99aa3 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1891,10 +1891,17 @@ virDomainCapsCPUModelsPtr
 virQEMUCapsGetCPUDefinitions(virQEMUCapsPtr qemuCaps,
  virDomainVirtType type)
 {
+virDomainCapsCPUModelsPtr cpuModels;
+
 if (type == VIR_DOMAIN_VIRT_KVM)
-return qemuCaps->kvmCPUModels;
+cpuModels = qemuCaps->kvmCPUModels;
 else
-return qemuCaps->tcgCPUModels;
+cpuModels = qemuCaps->tcgCPUModels;
+
+if (!cpuModels)
+return NULL;
+
+return virDomainCapsCPUModelsCopy(cpuModels);
 }
 
 
@@ -3125,6 +3132,7 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
virCPUDefPtr cpu,
bool migratable)
 {
+VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
 virCPUDataPtr data = NULL;
 int ret = -1;
 
@@ -3134,7 +3142,9 @@ virQEMUCapsInitCPUModelX86(virQEMUCapsPtr qemuCaps,
 if (!(data = virQEMUCapsGetCPUModelX86Data(qemuCaps, model, migratable)))
 goto cleanup;
 
-if (cpuDecode(cpu, data, virQEMUCapsGetCPUDefinitions(qemuCaps, type)) < 0)
+cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type);
+
+if (cpuDecode(cpu, data, cpuModels) < 0)
 goto cleanup;
 
 ret = 0;
@@ -3217,10 +3227,13 @@ virQEMUCapsInitHostCPUModel(virQEMUCapsPtr qemuCaps,
 if ((rc = virQEMUCapsInitCPUModel(qemuCaps, type, cpu, false)) < 0) {
 goto error;
 } else if (rc == 1) {
+VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+
 VIR_DEBUG("No host CPU model info from QEMU; probing host CPU 
directly");
 
-hostCPU = virQEMUCapsProbeHostCPU(hostArch,
-  
virQEMUCapsGetCPUDefinitions(qemuCaps, type));
+cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, type);
+hostCPU = virQEMUCapsProbeHostCPU(hostArch, cpuModels);
+
 if (!hostCPU ||
 virCPUDefCopyModelFilter(cpu, hostCPU, true,
  virQEMUCapsCPUFilterFeatures,
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 6ce6348593..15c69667a5 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -13913,7 +13913,7 @@ qemuConnectBaselineHypervisorCPU(virConnectPtr conn,
 virQEMUCapsPtr qemuCaps = NULL;
 virArch arch;
 virDomainVirtType virttype;
-virDomainCapsCPUModelsPtr cpuModels;
+VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
 bool migratable;
 virCPUDefPtr cpu = NULL;
 char *cpustr = NULL;
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 9c50c4a1d8..58192612b0 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -6122,6 +6122,8 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
 
 /* nothing to update for host-passthrough */
 if (def->cpu->mode != VIR_CPU_MODE_HOST_PASSTHROUGH) {
+VIR_AUTOUNREF(virDomainCapsCPUModelsPtr) cpuModels = NULL;
+
 if (def->cpu->check == VIR_CPU_CHECK_PARTIAL &&
 virCPUCompare(caps->host.arch,
   virQEMUCapsGetHostModel(qemuCaps, def->virtType,
@@ -6134,8 +6136,9 @@ qemuProcessUpdateGuestCPU(virDomainDefPtr def,
  
VIR_QEMU_CAPS_HOST_CPU_MIGRATABLE)) < 0)
 return -1;
 
-if (virCPUTranslate(def->os.arch, def->cpu,
-virQEMUCapsGetCPUDefinitions(qemuCaps, 
def->virtType)) < 0)
+cpuModels = virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType);
+
+if (virCPUTranslate(def->os.arch, def->cpu, cpuModels) < 0)
 return -1;
 
 def->cpu->fallback = VIR_CPU_FALLBACK_FORBID;
diff --git a/tests/cputest.c b/tests/cputest.c
index 7f47e3b601..3aa8fd7bf3 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -551,7 +551,6 @@ cpuTestGetCPUModels(const struct data *data,
 return -1;
 
 *models = virQEMUCapsGetCPUDefinitions(qemuCaps, VIR_DOMAIN_VIRT_KVM);
-virObjectRef(*models);
 
 virObjectUnref(qemuCaps);
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 03/31] conf: Use VIR_AUTO* in virDomainCapsCPUModelsAdd

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
Reviewed-by: Ján Tomko 
---

Notes:
Version 2:
- no change

 src/conf/domain_capabilities.c | 15 +--
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/src/conf/domain_capabilities.c b/src/conf/domain_capabilities.c
index b0fdd15d6c..3224ec3f52 100644
--- a/src/conf/domain_capabilities.c
+++ b/src/conf/domain_capabilities.c
@@ -246,25 +246,20 @@ virDomainCapsCPUModelsAdd(virDomainCapsCPUModelsPtr 
cpuModels,
   virDomainCapsCPUUsable usable,
   char **blockers)
 {
-char *nameCopy = NULL;
-char **blockersCopy = NULL;
+VIR_AUTOFREE(char *) nameCopy = NULL;
+VIR_AUTOSTRINGLIST blockersCopy = NULL;
 
 if (VIR_STRNDUP(nameCopy, name, nameLen) < 0)
-goto error;
+return -1;
 
 if (virStringListCopy(, (const char **)blockers) < 0)
-goto error;
+return -1;
 
 if (virDomainCapsCPUModelsAddSteal(cpuModels, ,
usable, ) < 0)
-goto error;
+return -1;
 
 return 0;
-
- error:
-VIR_FREE(nameCopy);
-virStringListFree(blockersCopy);
-return -1;
 }
 
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 02/31] domcapstest: Add test case for QEMU 4.2.0 on s390x

2019-10-15 Thread Jiri Denemark
Signed-off-by: Jiri Denemark 
---

Notes:
Version 2:
- new patch

 .../domaincapsschemadata/qemu_4.2.0.s390x.xml | 202 ++
 tests/domaincapstest.c|   4 +
 2 files changed, 206 insertions(+)
 create mode 100644 tests/domaincapsschemadata/qemu_4.2.0.s390x.xml

diff --git a/tests/domaincapsschemadata/qemu_4.2.0.s390x.xml 
b/tests/domaincapsschemadata/qemu_4.2.0.s390x.xml
new file mode 100644
index 00..1ca3b78251
--- /dev/null
+++ b/tests/domaincapsschemadata/qemu_4.2.0.s390x.xml
@@ -0,0 +1,202 @@
+
+  /usr/bin/qemu-system-s390x
+  kvm
+  s390-ccw-virtio-4.2
+  s390x
+  
+  
+  
+
+
+  /usr/share/AAVMF/AAVMF_CODE.fd
+  /usr/share/AAVMF/AAVMF32_CODE.fd
+  /usr/share/OVMF/OVMF_CODE.fd
+  
+rom
+pflash
+  
+  
+yes
+no
+  
+  
+no
+  
+
+  
+  
+
+
+  z13.2-base
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+  
+
+
+  z800-base
+  z890.2-base
+  z9EC.2
+  z13.2
+  z990.5-base
+  z9BC-base
+  z890.2
+  z890
+  z9BC
+  z13
+  z196
+  z13s
+  z990.3
+  z13s-base
+  z9EC
+  gen15a
+  z14ZR1-base
+  z14.2-base
+  z900.3-base
+  z13.2-base
+  z196.2-base
+  zBC12-base
+  z9BC.2-base
+  z900.2-base
+  z9EC.3
+  zEC12
+  z900
+  z114-base
+  zEC12-base
+  z10EC.2
+  z10EC-base
+  z900.3
+  z14ZR1
+  z10BC
+  z10BC.2-base
+  z9BC.2
+  z990.2
+  z990
+  z14
+  gen15b-base
+  z990.4
+  max
+  z10EC.2-base
+  gen15a-base
+  z800
+  zEC12.2
+  z10EC
+  z990.2-base
+  z900-base
+  z10BC.2
+  z9EC-base
+  z9EC.3-base
+  z114
+  z890.3
+  z196-base
+  z9EC.2-base
+  z196.2
+  z990-base
+  z14.2
+  z900.2
+  z890-base
+  z10EC.3
+  z14-base
+  z990.4-base
+  z10EC.3-base
+  z10BC-base
+  z13-base
+  z990.3-base
+  zEC12.2-base
+  zBC12
+  z890.3-base
+  z990.5
+  gen15b
+  qemu
+
+  
+  
+
+  
+disk
+cdrom
+floppy
+lun
+  
+  
+fdc
+scsi
+virtio
+  
+  
+virtio
+virtio-transitional
+virtio-non-transitional
+  
+
+
+  
+sdl
+vnc
+  
+
+
+  
+virtio
+  
+
+
+  
+subsystem
+  
+  
+default
+mandatory
+requisite
+optional
+  
+  
+usb
+pci
+scsi
+  
+  
+  
+default
+vfio
+  
+
+
+  
+virtio
+virtio-transitional
+virtio-non-transitional
+  
+  
+random
+egd
+  
+
+  
+  
+
+
+
+
+  
+
diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c
index db14114cba..667311c2bd 100644
--- a/tests/domaincapstest.c
+++ b/tests/domaincapstest.c
@@ -468,6 +468,10 @@ mymain(void)
  "/usr/bin/qemu-system-aarch64", NULL,
  "aarch64", VIR_DOMAIN_VIRT_KVM);
 
+DO_TEST_QEMU("4.2.0", "caps_4.2.0",
+ "/usr/bin/qemu-system-s390x", NULL,
+ "s390x", VIR_DOMAIN_VIRT_KVM);
+
 virObjectUnref(cfg);
 
 virFileWrapperClearPrefixes();
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 0/9] More GLib macro usage (glib chronicles)

2019-10-15 Thread Ján Tomko
Ján Tomko (9):
  util: xml: use g_auto directly for VIR_XPATH_NODE_AUTORESTORE
  virbuffer: use g_auto directly for virBuffer
  Use g_autofree instead of VIR_AUTOFREE
  Use G_DEFINE_AUTOPTR_CLEANUP_FUNC instead of VIR_DEFINE_AUTOPTR_FUNC
  Use g_autoptr instead of VIR_AUTOPTR
  Define G_DEFINE_AUTOPTR_CLEANUP_FUNC for virDomainCheckpointDef
  Use g_autoptr instead of VIR_AUTOUNREF
  Delete virObjectAutoUnref
  Remove virautoclean.h

 src/bhyve/bhyve_conf.c |   2 +-
 src/conf/checkpoint_conf.c |  10 +-
 src/conf/checkpoint_conf.h |   1 +
 src/conf/domain_capabilities.h |   3 +-
 src/conf/domain_conf.c | 858 ++---
 src/conf/domain_conf.h |   2 +-
 src/conf/interface_conf.c  |   2 +-
 src/conf/network_conf.c|   6 +-
 src/conf/node_device_conf.c|   2 +-
 src/conf/nwfilter_conf.c   |   2 +-
 src/conf/secret_conf.c |   8 +-
 src/conf/secret_conf.h |   2 +-
 src/conf/snapshot_conf.c   |   4 +-
 src/conf/storage_conf.c|  60 +-
 src/conf/storage_conf.h|   7 +-
 src/conf/virnetworkobj.c   |  10 +-
 src/conf/virnetworkportdef.c   |  18 +-
 src/conf/virnetworkportdef.h   |   2 +-
 src/conf/virstorageobj.c   |   4 +-
 src/cpu/cpu_x86.c  |  12 +-
 src/driver.c   |   4 +-
 src/esx/esx_driver.c   |   2 +-
 src/esx/esx_storage_backend_vmfs.c |   4 +-
 src/interface/interface_backend_netcf.c|   2 +-
 src/interface/interface_backend_udev.c |   2 +-
 src/libvirt-admin.c|   4 +-
 src/libvirt.c  |   2 +-
 src/libvirt_private.syms   |   1 -
 src/libxl/libxl_conf.c |   2 +-
 src/libxl/libxl_driver.c   |   6 +-
 src/libxl/xen_common.c |  24 +-
 src/libxl/xen_xl.c |  14 +-
 src/libxl/xen_xm.c |  10 +-
 src/locking/lock_daemon.c  |   2 +-
 src/locking/lock_daemon_config.c   |   2 +-
 src/locking/lock_driver_lockd.c|   2 +-
 src/locking/lock_driver_sanlock.c  |   2 +-
 src/logging/log_daemon.c   |   2 +-
 src/logging/log_daemon_config.c|   2 +-
 src/lxc/lxc_conf.c |   2 +-
 src/lxc/lxc_native.c   |  18 +-
 src/network/bridge_driver.c|  12 +-
 src/node_device/node_device_hal.c  |   2 +-
 src/node_device/node_device_udev.c |   2 +-
 src/phyp/phyp_driver.c |   4 +-
 src/qemu/qemu_agent.c  |  16 +-
 src/qemu/qemu_block.c  | 126 +--
 src/qemu/qemu_block.h  |   4 +-
 src/qemu/qemu_blockjob.c   |  26 +-
 src/qemu/qemu_capabilities.c   |  12 +-
 src/qemu/qemu_checkpoint.c |  22 +-
 src/qemu/qemu_command.c| 336 
 src/qemu/qemu_conf.c   |  54 +-
 src/qemu/qemu_dbus.c   |   2 +-
 src/qemu/qemu_dbus.h   |   2 +-
 src/qemu/qemu_domain.c | 100 +--
 src/qemu/qemu_domain_address.c |   6 +-
 src/qemu/qemu_driver.c |  58 +-
 src/qemu/qemu_extdevice.c  |   4 +-
 src/qemu/qemu_firmware.c   |  46 +-
 src/qemu/qemu_firmware.h   |   3 +-
 src/qemu/qemu_hotplug.c| 154 ++--
 src/qemu/qemu_interface.c  |   2 +-
 src/qemu/qemu_interop_config.c |  18 +-
 src/qemu/qemu_migration.c  |   8 +-
 src/qemu/qemu_migration_params.h   |   3 +-
 src/qemu/qemu_monitor.c|   4 +-
 src/qemu/qemu_monitor.h|   2 +-
 src/qemu/qemu_monitor_json.c   |  72 +-
 src/qemu/qemu_process.c|  10 +-
 src/qemu/qemu_qapi.c   |   4 +-
 src/qemu/qemu_slirp.c  |  40 +-
 src/qemu/qemu_slirp.h  |   2 +-
 src/qemu/qemu_tpm.c|  10 +-
 src/qemu/qemu_vhost_user.c |  18 +-
 src/qemu/qemu_vhost_user.h |   3 +-
 src/qemu/qemu_vhost_user_gpu.c |  22 +-
 src/remote/remote_daemon.c |   2 +-
 src/remote/remote_daemon_config.c  |   4 +-
 src/remote/remote_daemon_dispatch.c|   8 +-
 src/remote/remote_driver.c |  42 +-
 src/secret/secret_driver.c |   4 +-
 src/security/security_selinux.c|   2 +-
 src/security/security_util.c   |  36 +-
 src/security/virt-aa-helper.c  |   2 +-
 

[libvirt] [PATCH 7/9] Use g_autoptr instead of VIR_AUTOUNREF

2019-10-15 Thread Ján Tomko
Now that all the types using VIR_AUTOUNREF have a cleanup func defined
to virObjectUnref, use g_autoptr instead of VIR_AUTOUNREF.

Signed-off-by: Ján Tomko 
---
 src/conf/checkpoint_conf.c  |  2 +-
 src/conf/domain_conf.c  | 24 
 src/esx/esx_driver.c|  2 +-
 src/libxl/libxl_driver.c|  2 +-
 src/qemu/qemu_blockjob.c| 14 +++---
 src/qemu/qemu_checkpoint.c  | 10 +-
 src/qemu/qemu_command.c |  6 +++---
 src/qemu/qemu_conf.c| 10 +-
 src/qemu/qemu_domain.c  | 14 +++---
 src/qemu/qemu_driver.c  | 24 
 src/qemu/qemu_firmware.c|  2 +-
 src/qemu/qemu_hotplug.c | 24 
 src/qemu/qemu_migration.c   |  4 ++--
 src/qemu/qemu_process.c |  4 ++--
 src/qemu/qemu_slirp.c   |  4 ++--
 src/qemu/qemu_vhost_user_gpu.c  |  6 +++---
 src/storage/storage_backend_gluster.c   |  2 +-
 src/storage/storage_util.c  |  4 ++--
 src/test/test_driver.c  |  4 ++--
 src/util/virhostdev.c   | 18 +-
 src/util/virstoragefile.c   | 20 ++--
 src/vbox/vbox_common.c  |  4 ++--
 src/vz/vz_driver.c  |  2 +-
 tests/qemublocktest.c   |  6 +++---
 tests/qemudomaincheckpointxml2xmltest.c |  2 +-
 tests/qemudomainsnapshotxml2xmltest.c   |  2 +-
 tests/qemusecuritytest.c|  4 ++--
 tests/storagepoolcapstest.c |  6 +++---
 tests/virstoragetest.c  |  8 
 29 files changed, 117 insertions(+), 117 deletions(-)

diff --git a/src/conf/checkpoint_conf.c b/src/conf/checkpoint_conf.c
index 376a627ee4..e7b204a4d2 100644
--- a/src/conf/checkpoint_conf.c
+++ b/src/conf/checkpoint_conf.c
@@ -134,7 +134,7 @@ virDomainCheckpointDefParse(xmlXPathContextPtr ctxt,
 int n;
 char *tmp;
 g_autofree xmlNodePtr *nodes = NULL;
-VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL;
+g_autoptr(virDomainCheckpointDef) def = NULL;
 
 if (!(def = virDomainCheckpointDefNew()))
 return NULL;
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index a0690e23f7..8616f820fc 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1785,7 +1785,7 @@ static virDomainVcpuDefPtr
 virDomainVcpuDefNew(virDomainXMLOptionPtr xmlopt)
 {
 virDomainVcpuDefPtr ret = NULL;
-VIR_AUTOUNREF(virObjectPtr) priv = NULL;
+g_autoptr(virObject) priv = NULL;
 
 if (xmlopt && xmlopt->privateData.vcpuNew &&
 !(priv = xmlopt->privateData.vcpuNew()))
@@ -9276,7 +9276,7 @@ virDomainStorageSourceParseBase(const char *type,
 const char *format,
 const char *index)
 {
-VIR_AUTOUNREF(virStorageSourcePtr) src = NULL;
+g_autoptr(virStorageSource) src = NULL;
 virStorageSourcePtr ret = NULL;
 
 if (!(src = virStorageSourceNew()))
@@ -9402,7 +9402,7 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctxt,
 {
 VIR_XPATH_NODE_AUTORESTORE(ctxt);
 xmlNodePtr source;
-VIR_AUTOUNREF(virStorageSourcePtr) backingStore = NULL;
+g_autoptr(virStorageSource) backingStore = NULL;
 g_autofree char *type = NULL;
 g_autofree char *format = NULL;
 g_autofree char *idx = NULL;
@@ -19526,7 +19526,7 @@ virDomainCachetuneDefParse(virDomainDefPtr def,
 int ret = -1;
 g_autoptr(virBitmap) vcpus = NULL;
 g_autofree xmlNodePtr *nodes = NULL;
-VIR_AUTOUNREF(virResctrlAllocPtr) alloc = NULL;
+g_autoptr(virResctrlAlloc) alloc = NULL;
 
 ctxt->node = node;
 
@@ -19724,7 +19724,7 @@ virDomainMemorytuneDefParse(virDomainDefPtr def,
 virDomainResctrlDefPtr resctrl = NULL;
 g_autoptr(virBitmap) vcpus = NULL;
 g_autofree xmlNodePtr *nodes = NULL;
-VIR_AUTOUNREF(virResctrlAllocPtr) alloc = NULL;
+g_autoptr(virResctrlAlloc) alloc = NULL;
 ssize_t i = 0;
 int n;
 
@@ -30936,9 +30936,9 @@ virDomainNetCreatePort(virConnectPtr conn,
unsigned int flags)
 {
 virErrorPtr save_err;
-VIR_AUTOUNREF(virNetworkPtr) net = NULL;
+g_autoptr(virNetwork) net = NULL;
 g_autoptr(virNetworkPortDef) portdef = NULL;
-VIR_AUTOUNREF(virNetworkPortPtr) port = NULL;
+g_autoptr(virNetworkPort) port = NULL;
 g_autofree char *portxml = NULL;
 
 if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
@@ -31156,8 +31156,8 @@ virDomainNetResolveActualType(virDomainNetDefPtr iface)
 virNetworkDefPtr def = NULL;
 int ret = -1;
 g_autofree char *xml = NULL;
-VIR_AUTOUNREF(virConnectPtr) conn = NULL;
-VIR_AUTOUNREF(virNetworkPtr) net = NULL;
+g_autoptr(virConnect) conn = NULL;
+g_autoptr(virNetwork) net = NULL;
 
 if (iface->type != 

[libvirt] [PATCH 9/9] Remove virautoclean.h

2019-10-15 Thread Ján Tomko
Now that we no longer use any of the macros from this file, remove it.

This also removes a typo.

Signed-off-by: Ján Tomko 
---
 src/conf/domain_capabilities.h   |  1 -
 src/conf/storage_conf.h  |  1 -
 src/qemu/qemu_firmware.h |  1 -
 src/qemu/qemu_migration_params.h |  1 -
 src/qemu/qemu_vhost_user.h   |  1 -
 src/util/Makefile.inc.am |  1 -
 src/util/virauthconfig.h |  1 -
 src/util/virautoclean.h  | 89 
 src/util/virbitmap.h |  1 -
 src/util/virbuffer.h |  1 -
 src/util/vircommand.h|  1 -
 src/util/virconf.h   |  1 -
 src/util/virerror.h  |  1 -
 src/util/virfile.h   |  1 -
 src/util/virfirewall.h   |  1 -
 src/util/virhash.h   |  2 -
 src/util/virjson.h   |  1 -
 src/util/virmacaddr.h|  1 -
 src/util/virmdev.h   |  1 -
 src/util/virnetdev.h |  1 -
 src/util/virnetdevip.h   |  1 -
 src/util/virnetdevvlan.h |  1 -
 src/util/virnetlink.h|  1 -
 src/util/virpci.h|  1 -
 src/util/virperf.h   |  1 -
 src/util/virscsi.h   |  1 -
 src/util/virscsivhost.h  |  1 -
 src/util/virsocketaddr.h |  1 -
 src/util/virstoragefile.h|  1 -
 src/util/virsystemd.h|  1 -
 src/util/virtypedparam.h |  1 -
 src/util/viruri.h|  1 -
 src/util/virusb.h|  1 -
 src/util/virxml.h|  1 -
 tests/qemumonitortestutils.h |  1 -
 35 files changed, 124 deletions(-)
 delete mode 100644 src/util/virautoclean.h

diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h
index b5c322fd90..a31458c653 100644
--- a/src/conf/domain_capabilities.h
+++ b/src/conf/domain_capabilities.h
@@ -22,7 +22,6 @@
 
 #include "internal.h"
 #include "domain_conf.h"
-#include "virautoclean.h"
 #include "virenum.h"
 
 typedef const char * (*virDomainCapsValToStr)(int value);
diff --git a/src/conf/storage_conf.h b/src/conf/storage_conf.h
index b172e82ccb..d2600efff0 100644
--- a/src/conf/storage_conf.h
+++ b/src/conf/storage_conf.h
@@ -29,7 +29,6 @@
 #include "device_conf.h"
 #include "object_event.h"
 #include "storage_adapter_conf.h"
-#include "virautoclean.h"
 #include "virenum.h"
 #include "virxml.h"
 
diff --git a/src/qemu/qemu_firmware.h b/src/qemu/qemu_firmware.h
index 10b8eb822b..4be65bc664 100644
--- a/src/qemu/qemu_firmware.h
+++ b/src/qemu/qemu_firmware.h
@@ -22,7 +22,6 @@
 
 #include "domain_conf.h"
 #include "qemu_conf.h"
-#include "virautoclean.h"
 #include "virarch.h"
 #include "virfirmware.h"
 
diff --git a/src/qemu/qemu_migration_params.h b/src/qemu/qemu_migration_params.h
index e8380e6fb1..9aea24725f 100644
--- a/src/qemu/qemu_migration_params.h
+++ b/src/qemu/qemu_migration_params.h
@@ -27,7 +27,6 @@
 #include "virxml.h"
 #include "qemu_monitor.h"
 #include "qemu_conf.h"
-#include "virautoclean.h"
 #include "virenum.h"
 
 typedef enum {
diff --git a/src/qemu/qemu_vhost_user.h b/src/qemu/qemu_vhost_user.h
index 6bb5f5807c..369ba00caa 100644
--- a/src/qemu/qemu_vhost_user.h
+++ b/src/qemu/qemu_vhost_user.h
@@ -22,7 +22,6 @@
 
 #include "domain_conf.h"
 #include "qemu_conf.h"
-#include "virautoclean.h"
 #include "virarch.h"
 
 typedef struct _qemuVhostUser qemuVhostUser;
diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am
index 9747816fac..705b93c93c 100644
--- a/src/util/Makefile.inc.am
+++ b/src/util/Makefile.inc.am
@@ -17,7 +17,6 @@ UTIL_SOURCES = \
util/virauth.h \
util/virauthconfig.c \
util/virauthconfig.h \
-   util/virautoclean.h \
util/virbitmap.c \
util/virbitmap.h \
util/virbuffer.c \
diff --git a/src/util/virauthconfig.h b/src/util/virauthconfig.h
index 44bef7e2be..de28b1ff28 100644
--- a/src/util/virauthconfig.h
+++ b/src/util/virauthconfig.h
@@ -21,7 +21,6 @@
 #pragma once
 
 #include "internal.h"
-#include "virautoclean.h"
 
 typedef struct _virAuthConfig virAuthConfig;
 typedef virAuthConfig *virAuthConfigPtr;
diff --git a/src/util/virautoclean.h b/src/util/virautoclean.h
deleted file mode 100644
index 71312a2782..00
--- a/src/util/virautoclean.h
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * virautoclean.h: automatic scope-based memory clearing helper macros for
- * use in header files
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser 

[libvirt] [PATCH 6/9] Define G_DEFINE_AUTOPTR_CLEANUP_FUNC for virDomainCheckpointDef

2019-10-15 Thread Ján Tomko
Allow g_autoptr to be used instead of VIR_AUTOUNREF.

Signed-off-by: Ján Tomko 
---
 src/conf/checkpoint_conf.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h
index 2be041ff56..982096abed 100644
--- a/src/conf/checkpoint_conf.h
+++ b/src/conf/checkpoint_conf.h
@@ -57,6 +57,7 @@ struct _virDomainCheckpointDef {
 virDomainCheckpointDiskDef *disks;
 };
 
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainCheckpointDef, virObjectUnref);
 
 typedef enum {
 VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE = 1 << 0,
-- 
2.19.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH 4/9] Use G_DEFINE_AUTOPTR_CLEANUP_FUNC instead of VIR_DEFINE_AUTOPTR_FUNC

2019-10-15 Thread Ján Tomko
Since commit 44e7f029159ed701b4a1739ac711507ee53790ed
util: rewrite auto cleanup macros to use glib's equivalent

VIR_AUTOPTR aliases to g_autoptr. Replace all uses of VIR_DEFINE_AUTOPTR_FUNC
with G_DEFINE_AUTOPTR_CLEANUP_FUNC in preparation for replacing the
rest.

Signed-off-by: Ján Tomko 
---
 src/conf/domain_capabilities.h   | 2 +-
 src/conf/domain_conf.h   | 2 +-
 src/conf/secret_conf.h   | 2 +-
 src/conf/storage_conf.h  | 6 +++---
 src/conf/virnetworkportdef.h | 2 +-
 src/qemu/qemu_block.c| 2 +-
 src/qemu/qemu_block.h| 4 ++--
 src/qemu/qemu_dbus.h | 2 +-
 src/qemu/qemu_firmware.c | 6 +++---
 src/qemu/qemu_firmware.h | 2 +-
 src/qemu/qemu_migration_params.h | 2 +-
 src/qemu/qemu_monitor.h  | 2 +-
 src/qemu/qemu_slirp.h| 2 +-
 src/qemu/qemu_vhost_user.c   | 2 +-
 src/qemu/qemu_vhost_user.h   | 2 +-
 src/util/virauthconfig.h | 2 +-
 src/util/virbitmap.h | 2 +-
 src/util/vircommand.h| 2 +-
 src/util/virconf.h   | 2 +-
 src/util/virerror.h  | 2 +-
 src/util/virfile.h   | 2 +-
 src/util/virfirewall.h   | 2 +-
 src/util/virfirmware.h   | 2 +-
 src/util/virhash.h   | 2 +-
 src/util/virjson.h   | 2 +-
 src/util/virmacaddr.h| 2 +-
 src/util/virmdev.h   | 4 ++--
 src/util/virnetdev.c | 2 +-
 src/util/virnetdev.h | 2 +-
 src/util/virnetdevip.h   | 4 ++--
 src/util/virnetdevvlan.h | 2 +-
 src/util/virnetlink.c| 2 +-
 src/util/virnetlink.h| 2 +-
 src/util/virpci.h| 6 +++---
 src/util/virperf.h   | 2 +-
 src/util/virscsi.c   | 2 +-
 src/util/virscsi.h   | 2 +-
 src/util/virscsivhost.h  | 2 +-
 src/util/virsocketaddr.h | 2 +-
 src/util/virstoragefile.h| 2 +-
 src/util/virsystemd.h| 2 +-
 src/util/virtypedparam.h | 2 +-
 src/util/viruri.h| 2 +-
 src/util/virusb.h| 2 +-
 src/util/virxml.h| 4 ++--
 tests/qemumonitortestutils.h | 2 +-
 tools/virsh-util.h   | 2 +-
 47 files changed, 57 insertions(+), 57 deletions(-)

diff --git a/src/conf/domain_capabilities.h b/src/conf/domain_capabilities.h
index 3a92194602..b5c322fd90 100644
--- a/src/conf/domain_capabilities.h
+++ b/src/conf/domain_capabilities.h
@@ -236,4 +236,4 @@ int virDomainCapsDeviceDefValidate(virDomainCapsPtr const 
caps,
 void
 virSEVCapabilitiesFree(virSEVCapability *capabilities);
 
-VIR_DEFINE_AUTOPTR_FUNC(virSEVCapability, virSEVCapabilitiesFree);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSEVCapability, virSEVCapabilitiesFree);
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index e2edca149a..edac6250e4 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2913,7 +2913,7 @@ bool virDomainDefHasDeviceAddress(virDomainDefPtr def,
 ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
 
 void virDomainDefFree(virDomainDefPtr vm);
-VIR_DEFINE_AUTOPTR_FUNC(virDomainDef, virDomainDefFree);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainDef, virDomainDefFree);
 
 virDomainChrSourceDefPtr
 virDomainChrSourceDefNew(virDomainXMLOptionPtr xmlopt);
diff --git a/src/conf/secret_conf.h b/src/conf/secret_conf.h
index 7cfcfc8a60..ac94751ef3 100644
--- a/src/conf/secret_conf.h
+++ b/src/conf/secret_conf.h
@@ -35,7 +35,7 @@ struct _virSecretDef {
 };
 
 void virSecretDefFree(virSecretDefPtr def);
-VIR_DEFINE_AUTOPTR_FUNC(virSecretDef, virSecretDefFree);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virSecretDef, virSecretDefFree);
 
 virSecretDefPtr virSecretDefParseString(const char *xml);
 virSecretDefPtr virSecretDefParseFile(const char *filename);
diff --git a/src/conf/storage_conf.h b/src/conf/storage_conf.h
index 1f62803a9d..b172e82ccb 100644
--- a/src/conf/storage_conf.h
+++ b/src/conf/storage_conf.h
@@ -478,6 +478,6 @@ VIR_ENUM_DECL(virStoragePartedFs);
  VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART  | \
  VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE)
 
-VIR_DEFINE_AUTOPTR_FUNC(virStoragePoolSource, virStoragePoolSourceFree);
-VIR_DEFINE_AUTOPTR_FUNC(virStoragePoolDef, virStoragePoolDefFree);
-VIR_DEFINE_AUTOPTR_FUNC(virStorageVolDef, virStorageVolDefFree);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePoolSource, virStoragePoolSourceFree);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStoragePoolDef, virStoragePoolDefFree);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageVolDef, virStorageVolDefFree);
diff --git a/src/conf/virnetworkportdef.h b/src/conf/virnetworkportdef.h
index 796e269fe0..f5ba337fc9 100644
--- a/src/conf/virnetworkportdef.h
+++ b/src/conf/virnetworkportdef.h
@@ -82,7 +82,7 @@ struct _virNetworkPortDef {
 
 void
 virNetworkPortDefFree(virNetworkPortDefPtr port);
-VIR_DEFINE_AUTOPTR_FUNC(virNetworkPortDef, virNetworkPortDefFree);

[libvirt] [PATCH 8/9] Delete virObjectAutoUnref

2019-10-15 Thread Ján Tomko
Signed-off-by: Ján Tomko 
---
 src/libvirt_private.syms |  1 -
 src/util/virobject.c | 14 --
 src/util/virobject.h | 15 ---
 3 files changed, 30 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7d62df8086..0da02bb8bd 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2636,7 +2636,6 @@ virClassForObjectRWLockable;
 virClassIsDerivedFrom;
 virClassName;
 virClassNew;
-virObjectAutoUnref;
 virObjectFreeCallback;
 virObjectFreeHashData;
 virObjectIsClass;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 33feeb659a..c5363308d8 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -367,20 +367,6 @@ virObjectUnref(void *anyobj)
 }
 
 
-/**
- * virObjectAutoUnref:
- *
- * Helper used by VIR_AUTOUNREF
- */
-void
-virObjectAutoUnref(void *objptr)
-{
-virObjectPtr *obj = objptr;
-virObjectUnref(*obj);
-*obj = NULL;
-}
-
-
 /**
  * virObjectRef:
  * @anyobj: any instance of virObjectPtr
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 773a009f5e..7cb3457021 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -109,21 +109,6 @@ virObjectNew(virClassPtr klass)
 bool
 virObjectUnref(void *obj);
 
-void
-virObjectAutoUnref(void *objptr);
-
-/**
- * VIR_AUTOUNREF:
- * @type: type of an virObject subclass to be unref'd automatically
- *
- * DEPRECATED: Use g_autoptr(type) instead
- *
- * Declares a variable of @type which will be automatically unref'd when
- * control goes out of the scope.
- */
-#define VIR_AUTOUNREF(type) \
-__attribute__((cleanup(virObjectAutoUnref))) type
-
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virObject, virObjectUnref);
 
 void *
-- 
2.19.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH 2/9] virbuffer: use g_auto directly for virBuffer

2019-10-15 Thread Ján Tomko
Since commit 44e7f029159ed701b4a1739ac711507ee53790ed
util: rewrite auto cleanup macros to use glib's equivalent

VIR_AUTOCLEAN is just an alias for g_auto. Use the GLib macros
directly instead of our custom aliases.

Signed-off-by: Ján Tomko 
---
 src/conf/domain_conf.c   | 100 +-
 src/libxl/xen_common.c   |   2 +-
 src/qemu/qemu_command.c  | 136 +--
 src/qemu/qemu_domain.c   |  28 
 src/qemu/qemu_firmware.c |   2 +-
 src/qemu/qemu_monitor_json.c |   2 +-
 src/util/virbuffer.h |   2 +-
 src/util/virjson.c   |   2 +-
 tests/qemublocktest.c|   8 +--
 tests/qemumonitortestutils.c |   2 +-
 tests/virbuftest.c   |   6 +-
 tests/virstoragetest.c   |   2 +-
 tests/virsystemdtest.c   |   2 +-
 13 files changed, 147 insertions(+), 147 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 10d6bf0eea..16b939d511 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7095,8 +7095,8 @@ virDomainDeviceInfoFormat(virBufferPtr buf,
   virDomainDeviceInfoPtr info,
   unsigned int flags)
 {
-VIR_AUTOCLEAN(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-VIR_AUTOCLEAN(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
 int ret = -1;
 
 if ((flags & VIR_DOMAIN_DEF_FORMAT_ALLOW_BOOT) && info->bootIndex) {
@@ -24139,7 +24139,7 @@ virDomainDiskSourceFormatPrivateData(virBufferPtr buf,
  unsigned int flags,
  virDomainXMLOptionPtr xmlopt)
 {
-VIR_AUTOCLEAN(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
 int ret = -1;
 
 if (!(flags & VIR_DOMAIN_DEF_FORMAT_STATUS) ||
@@ -24183,8 +24183,8 @@ virDomainDiskSourceFormat(virBufferPtr buf,
   unsigned int flags,
   virDomainXMLOptionPtr xmlopt)
 {
-VIR_AUTOCLEAN(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-VIR_AUTOCLEAN(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
 
 virBufferSetChildIndent(, buf);
 
@@ -24271,8 +24271,8 @@ virDomainDiskBackingStoreFormat(virBufferPtr buf,
 virDomainXMLOptionPtr xmlopt,
 unsigned int flags)
 {
-VIR_AUTOCLEAN(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-VIR_AUTOCLEAN(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
 bool inactive = flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE;
 virStorageSourcePtr backingStore = src->backingStore;
 
@@ -24435,8 +24435,8 @@ virDomainDiskDefFormatMirror(virBufferPtr buf,
  unsigned int flags,
  virDomainXMLOptionPtr xmlopt)
 {
-VIR_AUTOCLEAN(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-VIR_AUTOCLEAN(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
 const char *formatStr = NULL;
 
 virBufferSetChildIndent(, buf);
@@ -24488,7 +24488,7 @@ virDomainDiskDefFormatPrivateData(virBufferPtr buf,
   unsigned int flags,
   virDomainXMLOptionPtr xmlopt)
 {
-VIR_AUTOCLEAN(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
 
 if (!(flags & VIR_DOMAIN_DEF_FORMAT_STATUS) ||
 !xmlopt ||
@@ -24637,7 +24637,7 @@ static int
 virDomainControllerDriverFormat(virBufferPtr buf,
 virDomainControllerDefPtr def)
 {
-VIR_AUTOCLEAN(virBuffer) driverBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) driverBuf = VIR_BUFFER_INITIALIZER;
 
 if (def->queues)
 virBufferAsprintf(, " queues='%u'", def->queues);
@@ -24670,8 +24670,8 @@ virDomainControllerDefFormat(virBufferPtr buf,
 const char *type = virDomainControllerTypeToString(def->type);
 const char *model = NULL;
 const char *modelName = NULL;
-VIR_AUTOCLEAN(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-VIR_AUTOCLEAN(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
 
 virBufferSetChildIndent(, buf);
 
@@ -24839,7 +24839,7 @@ virDomainFSDefFormat(virBufferPtr buf,
 const char *fsdriver = virDomainFSDriverTypeToString(def->fsdriver);
 const char *wrpolicy = virDomainFSWrpolicyTypeToString(def->wrpolicy);
 const char *src = def->src->path;
-VIR_AUTOCLEAN(virBuffer) 

[libvirt] [PATCH 1/9] util: xml: use g_auto directly for VIR_XPATH_NODE_AUTORESTORE

2019-10-15 Thread Ján Tomko
Since commit 44e7f029159ed701b4a1739ac711507ee53790ed
util: rewrite auto cleanup macros to use glib's equivalent

VIR_AUTOCLEAN is just an alias for g_auto. Use the GLib macros
directly instead of our custom aliases.

Signed-off-by: Ján Tomko 
---
 src/util/virxml.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/util/virxml.h b/src/util/virxml.h
index c7def9ce61..8813e31f75 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -233,7 +233,7 @@ typedef virXPathContextNodeSave *virXPathContextNodeSavePtr;
 void
 virXPathContextNodeRestore(virXPathContextNodeSavePtr save);
 
-VIR_DEFINE_AUTOCLEAN_FUNC(virXPathContextNodeSave, virXPathContextNodeRestore);
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virXPathContextNodeSave, 
virXPathContextNodeRestore);
 
 /**
  * VIR_XPATH_NODE_AUTORESTORE:
@@ -243,8 +243,8 @@ VIR_DEFINE_AUTOCLEAN_FUNC(virXPathContextNodeSave, 
virXPathContextNodeRestore);
  * node pointer is reset to the original value when this macro was used.
  */
 #define VIR_XPATH_NODE_AUTORESTORE(_ctxt) \
-VIR_AUTOCLEAN(virXPathContextNodeSave) _ctxt ## CtxtSave = { .ctxt = 
_ctxt,\
- .node = 
_ctxt->node}; \
+g_auto(virXPathContextNodeSave) _ctxt ## CtxtSave = { .ctxt = _ctxt,\
+  .node = 
_ctxt->node}; \
 ignore_value(&_ctxt ## CtxtSave)
 
 VIR_DEFINE_AUTOPTR_FUNC(xmlDoc, xmlFreeDoc);
-- 
2.19.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH 7/8] examples: Use G_N_ELEMENTS instead of ARRAY_CARDINALITY

2019-10-15 Thread Ján Tomko

On Tue, Oct 15, 2019 at 04:10:55PM +0200, Andrea Bolognani wrote:

On Tue, 2019-10-15 at 14:21 +0200, Ján Tomko wrote:

We try to keep the example programs independent of libraries
other than libvirt.

Rename the locally defined ARRAY_CARDINALITY macro to G_N_ELEMENTS
which GLib provides, even though we don't actually include GLib.


[...]


-#define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array)))
+#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))


In this case, and in this case only, I think we should leave the name
alone.


This case being "the isolated event-test.c example" or "the
ARRAY_CARDINALITY macro in event-test.c"?

If it's the latter, I can add an exception to syntax-check instead.

Note that I also added a G_GNUC_UNUSED macro earlier, if the G_ prefix
is the issue. (The ATTRIBUTE_UNUSED spelling is used by multiple libs,
including libxml2)

Also, we open-code the 'verify' macro from gnulib.



When we use GLib and simply provide our own implementation when using
a version that's too old, the GLib name is obviously accurate, but in
this specific case I think it's misleading and we should stick with
the current one.



Alternatively, would a comment along the lines of:
/* define these macros to avoid pulling in glib */
make it less misleading?

The reason we included these macros here in the first place is that
we wanted to keep the code in sync with the rest of libvirt.

Jano



--
Andrea Bolognani / Red Hat / Virtualization



signature.asc
Description: PGP signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH 1/1] Add shrink flag to blockresize command

2019-10-15 Thread Cole Robinson
On 10/5/19 2:56 PM, martinsson.pat...@gmail.com wrote:
> From: patchon 
> 

You probably want to fix this to match your signed-off-by name.

> These commits simply adds the '--shrink' flag to the blockresize
> command to prevent accidental shrinking of a block device. This
> behaviour is already present on the vol-resize command and it
> makes sense to mimic that behaviour.
> 

I don't have an opinion on whether the feature should be added at the
API level, I will leave that to others. CCing pkrempa

But as implemented it seems problematic to add a flag that changes the
semantics of the API, essentially requiring a new flag to get the old
behavior. I see the argument that this is a data safety issue, but maybe
apps are depending on this already? I don't know enough about the usage
to guess either way

> Only implemented in the qemu-driver atm.
> 
> Signed-off-by: Patrik Martinsson 
> ---
>  src/qemu/qemu_driver.c | 15 ++-
>  tools/virsh-domain.c   |  7 +++
>  tools/virsh.pod|  9 -
>  3 files changed, 29 insertions(+), 2 deletions(-)
> 
> diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
> index 95fe844c34..4e34eec796 100644
> --- a/src/qemu/qemu_driver.c
> +++ b/src/qemu/qemu_driver.c
> @@ -11265,8 +11265,10 @@ qemuDomainBlockResize(virDomainPtr dom,
>  char *device = NULL;
>  const char *nodename = NULL;
>  virDomainDiskDefPtr disk = NULL;
> +virDomainBlockInfo info;
>  
> -virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES, -1);
> +virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES |
> +  VIR_STORAGE_VOL_RESIZE_SHRINK, -1);
>  

We shouldn't mix and match flag prefix names. Each public API should be
coupled with its own set of flag names. So you would want to call this
VIR_DOMAIN_BLOCK_RESIZE_SHRINK, and add it to
include/libvirt/libvirt-domain.h and add it to documentation in
libvirt-domain.c

- Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 07/12] qemu: handle race on device deletion and usb host device plugging

2019-10-15 Thread Nikolay Shirokovskiy
Imagine host usb device is unplugged from host and as a result we send
command to qemu to delete appropriate device. Then before qemu device is
deleted host usb device is plugged back. Currently code supposes there is
no remnant device in qemu and will try to add new device and the attempt
will fail.

Instead let's check the device is not yet deleted and postpone adding
qemu device to device_deleted event handler.

Signed-off-by: Nikolay Shirokovskiy 
Reviewed-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c | 49 ++
 1 file changed, 49 insertions(+)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d7b44dab2c..030fe8a6c4 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4586,6 +4586,44 @@ processGuestPanicEvent(virQEMUDriverPtr driver,
 }
 
 
+static int
+qemuCheckHostdevPlugged(virQEMUDriverPtr driver,
+virDomainObjPtr vm,
+const char *devAlias)
+{
+virDomainHostdevDefPtr hostdev;
+virDomainHostdevSubsysUSBPtr usbsrc;
+virDomainDeviceDef dev;
+int num;
+
+if (virDomainDefFindDevice(vm->def, devAlias, , false) < 0)
+return 0;
+
+if (dev.type != VIR_DOMAIN_DEVICE_HOSTDEV)
+return 0;
+
+hostdev = dev.data.hostdev;
+if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
+return 0;
+
+usbsrc = >source.subsys.u.usb;
+if (!usbsrc->vendor || !usbsrc->product)
+return 0;
+
+if ((num = virUSBDeviceFindByVendor(usbsrc->vendor, usbsrc->product,
+NULL, false, NULL)) < 0)
+return -1;
+
+if (num == 0)
+return 0;
+
+if (qemuDomainAttachHostDevice(driver, vm, hostdev) < 0)
+return -1;
+
+return 0;
+}
+
+
 static void
 processDeviceDeletedEvent(virQEMUDriverPtr driver,
   virDomainObjPtr vm,
@@ -4613,6 +4651,11 @@ processDeviceDeletedEvent(virQEMUDriverPtr driver,
 
 if (qemuDomainRemoveDevice(driver, vm, ) < 0)
 goto endjob;
+
+/* Fall thru and save status file even on error condition because
+ * device is removed successfully and changed configuration need
+ * to be saved in status file. */
+qemuCheckHostdevPlugged(driver, vm, devAlias);
 }
 
 if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 
0)
@@ -5218,6 +5261,12 @@ processUSBAddedEvent(virQEMUDriverPtr driver,
 if (i == vm->def->nhostdevs)
 goto cleanup;
 
+/* if device is not yet even deleted from qemu then handle plugging later.
+ * Or we failed handling host usb device unplugging, then another attempt 
of
+ * unplug/plug could help. */
+if (usbsrc->bus || usbsrc->device)
+goto cleanup;
+
 if (qemuDomainAttachHostDevice(driver, vm, hostdev) < 0)
 goto cleanup;
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 11/12] qemu: handle detaching of unplugged hostdev

2019-10-15 Thread Nikolay Shirokovskiy
If hostdev is unplugged we don't need to delete it's correspondent device
from qemu etc. Just remove it from config immediately.

Signed-off-by: Nikolay Shirokovskiy 
---
 src/qemu/qemu_hotplug.c | 16 +++-
 src/util/virhostdev.c   |  2 ++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 920e0cb72b..8ba0235dbc 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4462,6 +4462,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 VIR_AUTOFREE(char *) objAlias = NULL;
 bool is_vfio = false;
 bool unplug = hostdev->deleteAction == 
VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
+bool unplugged = false;
 
 VIR_DEBUG("Removing host device %s from domain %p %s",
   hostdev->info->alias, vm, vm->def->name);
@@ -4520,13 +4521,17 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 
 virDomainAuditHostdev(vm, hostdev, "detach", true);
 
+if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
+virDomainHostdevSubsysUSBPtr usbsrc = >source.subsys.u.usb;
+unplugged = !usbsrc->bus && !usbsrc->device;
+}
 /*
  * In case of unplug the attempt to restore label will fail. But we don't
  * need to restore the label! In case of separate mount namespace for the
  * domain we remove device file later in this function. In case of global
  * mount namespace the device file is deleted or being deleted by systemd.
  */
-if (!is_vfio && !unplug &&
+if (!is_vfio && !unplug && !unplugged &&
 qemuSecurityRestoreHostdevLabel(driver, vm, hostdev) < 0)
 VIR_WARN("Failed to restore host device labelling");
 
@@ -5832,6 +5837,15 @@ qemuDomainDetachDeviceLive(virDomainObjPtr vm,
 unplug) < 0) {
 return -1;
 }
+if (!unplug) {
+virDomainHostdevDefPtr hostdev = detach.data.hostdev;
+virDomainHostdevSubsysUSBPtr usbsrc = 
>source.subsys.u.usb;
+
+if (hostdev->startupPolicy != VIR_DOMAIN_STARTUP_POLICY_OPTIONAL &&
+hostdev->startupPolicy != VIR_DOMAIN_STARTUP_POLICY_REQUISITE 
&&
+usbsrc->device == 0 && usbsrc->bus == 0)
+return qemuDomainRemoveDevice(driver, vm, );
+}
 break;
 case VIR_DOMAIN_DEVICE_RNG:
 if (qemuDomainDetachPrepRNG(vm, match->data.rng,
diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index 41fcab7222..ed9bc09e14 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -1793,6 +1793,8 @@ virHostdevReAttachUSBDevices(virHostdevManagerPtr mgr,
 continue;
 if (hostdev->missing)
 continue;
+if (!usbsrc->bus && !usbsrc->device)
+continue;
 
 if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, NULL))) {
 VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s",
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 02/12] qemu: track hostdev delete intention

2019-10-15 Thread Nikolay Shirokovskiy
So we are going to support replug of usb device on host. We need to
delete device from qemu when the device is unplugged on host and to add
device when the device is plugged back. As to deleting let's use
same code as when we detach device from domain. But we need to keep
the usb hostdev etc in libvirt config when handle DEVICE_DELETED event
from qemu. For this purpose let's save delete intention in device config.

Signed-off-by: Nikolay Shirokovskiy 
---
 src/conf/domain_conf.h  | 14 ++
 src/qemu/qemu_driver.c  |  4 ++--
 src/qemu/qemu_hotplug.c | 23 ---
 src/qemu/qemu_hotplug.h |  3 ++-
 tests/qemuhotplugtest.c |  2 +-
 5 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index da3eff242b..0a16413852 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -328,6 +328,19 @@ struct _virDomainHostdevCaps {
 } u;
 };
 
+typedef enum {
+VIR_DOMAIN_HOSTDEV_DELETE_ACTION_NONE = 0,
+/* delete associated device from libvirt config
+ * as intended by client API call */
+VIR_DOMAIN_HOSTDEV_DELETE_ACTION_DELETE,
+/* keep associated device in libvirt config as
+ * qemu device is deleted as a result of unplugging
+ * device from host */
+VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG,
+
+VIR_DOMAIN_HOSTDEV_DELETE_ACTION_LAST
+} virDomainHostdevDeleteActionType;
+
 
 /* basic device for direct passthrough */
 struct _virDomainHostdevDef {
@@ -345,6 +358,7 @@ struct _virDomainHostdevDef {
 bool missing;
 bool readonly;
 bool shareable;
+virDomainHostdevDeleteActionType deleteAction;
 union {
 virDomainHostdevSubsys subsys;
 virDomainHostdevCaps caps;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 681b26814b..ca40879977 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9078,7 +9078,7 @@ qemuDomainDetachDeviceLiveAndConfig(virQEMUDriverPtr 
driver,
 if (flags & VIR_DOMAIN_AFFECT_LIVE) {
 int rc;
 
-if ((rc = qemuDomainDetachDeviceLive(vm, dev_copy, driver, false)) < 0)
+if ((rc = qemuDomainDetachDeviceLive(vm, dev_copy, driver, false, 
false)) < 0)
 goto cleanup;
 
 if (rc == 0 && qemuDomainUpdateDeviceList(driver, vm, 
QEMU_ASYNC_JOB_NONE) < 0)
@@ -9167,7 +9167,7 @@ qemuDomainDetachDeviceAliasLiveAndConfig(virQEMUDriverPtr 
driver,
 if (virDomainDefFindDevice(def, alias, , true) < 0)
 goto cleanup;
 
-if ((rc = qemuDomainDetachDeviceLive(vm, , driver, true)) < 0)
+if ((rc = qemuDomainDetachDeviceLive(vm, , driver, true, false)) < 
0)
 goto cleanup;
 
 if (rc == 0 && qemuDomainUpdateDeviceList(driver, vm, 
QEMU_ASYNC_JOB_NONE) < 0)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 88984dff67..4f7858da71 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -5351,7 +5351,8 @@ qemuDomainDetachPrepController(virDomainObjPtr vm,
 static int
 qemuDomainDetachPrepHostdev(virDomainObjPtr vm,
 virDomainHostdevDefPtr match,
-virDomainHostdevDefPtr *detach)
+virDomainHostdevDefPtr *detach,
+bool unplug)
 {
 virDomainHostdevSubsysPtr subsys = >source.subsys;
 virDomainHostdevSubsysUSBPtr usbsrc = >u.usb;
@@ -5423,6 +5424,20 @@ qemuDomainDetachPrepHostdev(virDomainObjPtr vm,
 return -1;
 }
 
+/*
+ * Why having additional check in second branch? Suppose client
+ * asks for device detaching and we delete device from qemu
+ * but don't get DEVICE_DELETED event yet. Next USB is unplugged
+ * from host and we have this function called again. If we reset
+ * delete action to 'unplug' then device will be left in
+ * libvirt config after handling DEVICE_DELETED event while
+ * it should not as client asked to detach the device before.
+ */
+if (!unplug)
+hostdev->deleteAction = VIR_DOMAIN_HOSTDEV_DELETE_ACTION_DELETE;
+else if (hostdev->deleteAction != VIR_DOMAIN_HOSTDEV_DELETE_ACTION_DELETE)
+hostdev->deleteAction = VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
+
 return 0;
 }
 
@@ -5724,7 +5739,8 @@ int
 qemuDomainDetachDeviceLive(virDomainObjPtr vm,
virDomainDeviceDefPtr match,
virQEMUDriverPtr driver,
-   bool async)
+   bool async,
+   bool unplug)
 {
 virDomainDeviceDef detach = { .type = match->type };
 virDomainDeviceInfoPtr info = NULL;
@@ -5769,7 +5785,8 @@ qemuDomainDetachDeviceLive(virDomainObjPtr vm,
 break;
 case VIR_DOMAIN_DEVICE_HOSTDEV:
 if (qemuDomainDetachPrepHostdev(vm, match->data.hostdev,
-) < 0) {
+,
+unplug) < 0) {
 

[libvirt] [PATCH v3 REBASE 08/12] qemu: hotplug: update device list on device deleted event

2019-10-15 Thread Nikolay Shirokovskiy
I guess this is the missing piece for [1]. It did not hurt before (like
we didn't even see any errors/warns in logs) because in
qemuProcessUpdateDevices function virDomainDefFindDevice does not find
device deleted from libvirt config. But now in case of unpluggind usb
device from host we leave device in config and thus needlessly try to
call qemuDomainRemoveDevice second time.

[1] 0dfb8a1b9: qemu: Unplug devices that disappeared when libvirtd was down

Signed-off-by: Nikolay Shirokovskiy 
---
 src/qemu/qemu_driver.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 030fe8a6c4..e268e2fd1e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4646,6 +4646,9 @@ processDeviceDeletedEvent(virQEMUDriverPtr driver,
 if (STRPREFIX(devAlias, "vcpu")) {
 qemuDomainRemoveVcpuAlias(driver, vm, devAlias);
 } else {
+if (qemuDomainUpdateDeviceList(driver, vm, QEMU_ASYNC_JOB_NONE) < 0)
+goto cleanup;
+
 if (virDomainDefFindDevice(vm->def, devAlias, , true) < 0)
 goto endjob;
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 05/12] qemu: handle host usb device add/del udev events

2019-10-15 Thread Nikolay Shirokovskiy
Now when code handling attaching/detaching usb hostdev is appropriately
changed use it to handle host usb device udev add/del events.

As device adding/deleting needs acquiring job condition it is done in
thread pool rather in event loop thread itself where udev events are
delivered. However implementation in this patch is a bit different from
other places as we instead of finding right domain in event loop thread
and creating a single thread pool job create job for every vm. Such
a strategy allows us to avoid taking domain lock in event loop thread so
we have little chances to block event loop thread because some API
handler grabs domain lock for a significant amount of time.

Signed-off-by: Nikolay Shirokovskiy 
---
 src/qemu/Makefile.inc.am |   2 +
 src/qemu/qemu_conf.h |   3 +
 src/qemu/qemu_domain.c   |   2 +
 src/qemu/qemu_domain.h   |   2 +
 src/qemu/qemu_driver.c   | 324 ++-
 5 files changed, 332 insertions(+), 1 deletion(-)

diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am
index e66da76c0a..49b0202a11 100644
--- a/src/qemu/Makefile.inc.am
+++ b/src/qemu/Makefile.inc.am
@@ -98,6 +98,7 @@ libvirt_driver_qemu_impl_la_CFLAGS = \
-I$(srcdir)/conf \
-I$(srcdir)/secret \
$(AM_CFLAGS) \
+   $(UDEV_CFLAGS) \
$(NULL)
 libvirt_driver_qemu_impl_la_LDFLAGS = $(AM_LDFLAGS)
 libvirt_driver_qemu_impl_la_LIBADD = \
@@ -106,6 +107,7 @@ libvirt_driver_qemu_impl_la_LIBADD = \
$(LIBNL_LIBS) \
$(SELINUX_LIBS) \
$(LIBXML_LIBS) \
+   $(UDEV_LIBS) \
$(NULL)
 libvirt_driver_qemu_impl_la_SOURCES = $(QEMU_DRIVER_SOURCES)
 
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index 7247199d3e..23c9b4d9fa 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -299,6 +299,9 @@ struct _virQEMUDriver {
 
 /* Immutable pointer, self-locking APIs */
 virHashAtomicPtr migrationErrors;
+
+struct udev_monitor *udev_monitor;
+int udev_watch;
 };
 
 virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged);
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index a6c95b7208..61c0c0e143 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -15335,6 +15335,8 @@ qemuProcessEventFree(struct qemuProcessEvent *event)
 case QEMU_PROCESS_EVENT_SERIAL_CHANGED:
 case QEMU_PROCESS_EVENT_BLOCK_JOB:
 case QEMU_PROCESS_EVENT_MONITOR_EOF:
+case QEMU_PROCESS_EVENT_USB_REMOVED:
+case QEMU_PROCESS_EVENT_USB_ADDED:
 VIR_FREE(event->data);
 break;
 case QEMU_PROCESS_EVENT_JOB_STATUS_CHANGE:
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 282ab2c3ec..3ba8b059ec 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -549,6 +549,8 @@ typedef enum {
 QEMU_PROCESS_EVENT_MONITOR_EOF,
 QEMU_PROCESS_EVENT_PR_DISCONNECT,
 QEMU_PROCESS_EVENT_RDMA_GID_STATUS_CHANGED,
+QEMU_PROCESS_EVENT_USB_REMOVED,
+QEMU_PROCESS_EVENT_USB_ADDED,
 
 QEMU_PROCESS_EVENT_LAST
 } qemuProcessEventType;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index ca40879977..1110e11c14 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -34,6 +34,7 @@
 #include 
 #include 
 #include 
+#include 
 
 
 #include "qemu_driver.h"
@@ -657,6 +658,221 @@ qemuDomainFindMaxID(virDomainObjPtr vm,
 }
 
 
+struct qemuUdevUSBRemoveData {
+unsigned int bus;
+unsigned int device;
+};
+
+struct qemuUdevUSBAddData {
+unsigned int vendor;
+unsigned int product;
+};
+
+struct qemuUdevUSBEventData {
+union {
+struct qemuUdevUSBRemoveData remove;
+struct qemuUdevUSBAddData add;
+} data;
+bool remove;
+};
+
+static int
+qemuUdevUSBHandleEvent(virDomainObjPtr vm, void *opaque)
+{
+struct qemuUdevUSBEventData *data = opaque;
+struct qemuProcessEvent *event = NULL;
+
+if (VIR_ALLOC(event) < 0)
+return 0;
+
+if (data->remove) {
+struct qemuUdevUSBRemoveData *rm_data;
+
+
+if (VIR_ALLOC(rm_data) < 0)
+goto cleanup;
+
+*rm_data = data->data.remove;
+event->data = rm_data;
+event->eventType = QEMU_PROCESS_EVENT_USB_REMOVED;
+} else {
+struct qemuUdevUSBAddData *add_data;
+
+if (VIR_ALLOC(add_data) < 0)
+goto cleanup;
+
+*add_data = data->data.add;
+event->data = add_data;
+event->eventType = QEMU_PROCESS_EVENT_USB_ADDED;
+}
+
+event->vm = virObjectRef(vm);
+
+if (virThreadPoolSendJob(qemu_driver->workerPool, 0, event) < 0) {
+virObjectUnref(vm);
+goto cleanup;
+}
+
+event = NULL;
+
+ cleanup:
+qemuProcessEventFree(event);
+
+return 0;
+}
+
+
+static void
+qemuUdevEventHandleCallback(int watch G_GNUC_UNUSED,
+int fd G_GNUC_UNUSED,
+int events G_GNUC_UNUSED,
+void *data G_GNUC_UNUSED)
+{
+struct qemuUdevUSBEventData 

[libvirt] [PATCH v3 REBASE 04/12] qemu: support usb hostdev plugging back

2019-10-15 Thread Nikolay Shirokovskiy
We are going to use qemuDomainAttachHostUSBDevice when
host usb device is plugged back to node.

Signed-off-by: Nikolay Shirokovskiy 
Reviewed-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_hotplug.c | 29 -
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 0e161bfd5a..920e0cb72b 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -2478,8 +2478,18 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
 bool teardownlabel = false;
 bool teardowndevice = false;
 int ret = -1;
+bool replug = false;
+size_t i;
+
+for (i = 0; i < vm->def->nhostdevs; i++) {
+if (vm->def->hostdevs[i] == hostdev) {
+replug = true;
+break;
+}
+}
 
-if (virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)
+if (!replug &&
+virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)
 return -1;
 
 if (qemuHostdevPrepareUSBDevices(driver, vm->def->name, , 1, 0) < 
0)
@@ -2504,7 +2514,7 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
 if (!(devstr = qemuBuildUSBHostdevDevStr(vm->def, hostdev, 
priv->qemuCaps)))
 goto cleanup;
 
-if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
+if (!replug && VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
 goto cleanup;
 
 qemuDomainObjEnterMonitor(driver, vm);
@@ -2517,7 +2527,8 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
 if (ret < 0)
 goto cleanup;
 
-vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
+if (!replug)
+vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
 
 ret = 0;
  cleanup:
@@ -2530,9 +2541,17 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
 if (teardowndevice &&
 qemuDomainNamespaceTeardownHostdev(vm, hostdev) < 0)
 VIR_WARN("Unable to remove host device from /dev");
-if (added)
+if (added) {
 qemuHostdevReAttachUSBDevices(driver, vm->def->name, , 1);
-virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
+
+if (replug) {
+virDomainHostdevSubsysUSBPtr usbsrc = 
>source.subsys.u.usb;
+usbsrc->bus = 0;
+usbsrc->device = 0;
+}
+}
+if (!replug)
+virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
 }
 return ret;
 }
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 01/12] conf: add replug option for usb hostdev

2019-10-15 Thread Nikolay Shirokovskiy
If usb device attached to a domain is unplugged from host and
then plugged back then it will no longer be available in guest.
We are going to support this case so that device will be detached
from qemu on unplug and attached back on replug. As sometimes
this behaviour is not desirable and for backcompat too let's
add 'replug' option for usb hostdev.

Signed-off-by: Nikolay Shirokovskiy 
---
 docs/formatdomain.html.in | 10 -
 docs/schemas/domaincommon.rng |  5 +++
 src/conf/domain_conf.c| 30 ++
 src/conf/domain_conf.h|  2 +
 tests/qemuxml2argvdata/hostdev-usb-replug.xml | 36 +
 .../qemuxml2xmloutdata/hostdev-usb-replug.xml | 40 +++
 tests/qemuxml2xmltest.c   |  1 +
 7 files changed, 122 insertions(+), 2 deletions(-)
 create mode 100644 tests/qemuxml2argvdata/hostdev-usb-replug.xml
 create mode 100644 tests/qemuxml2xmloutdata/hostdev-usb-replug.xml

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 500f114f41..8e1c0bf6fa 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -4688,7 +4688,7 @@
 
 ...
 devices
-  hostdev mode='subsystem' type='usb'
+  hostdev mode='subsystem' type='usb' replug='yes'
 source startupPolicy='optional'
   vendor id='0x1234'/
   product id='0xbeef'/
@@ -4787,7 +4787,13 @@
   usb
   USB devices are detached from the host on guest startup
 and reattached after the guest exits or the device is
-hot-unplugged.
+hot-unplugged. If optional replug
+(since 5.8.0) is "yes" then libvirt
+tracks USB device unplug/plug on host. On unplug the correspondent
+QEMU device will be be deleted but device stays in libvirt config.
+On plug the device will be added back to QEMU. This applies only
+for USB devices with product/vendor pair specified. Default value
+is "no".
   
   pci
   For PCI devices, when managed is "yes" it is
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index ead5a25068..caba7f4e24 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -4658,6 +4658,11 @@
 
   usb
 
+
+  
+
+  
+
 
   
 
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 6bd2d4935d..c99758d9f5 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7702,6 +7702,13 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr node,
 goto out;
 }
 
+if (usbsrc->replug && (!got_vendor || !got_product)) {
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+   _("replug is only possible if vendor/product "
+ "pair is specified"));
+goto out;
+}
+
 ret = 0;
  out:
 return ret;
@@ -8155,12 +8162,14 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
 virDomainHostdevSubsysSCSIPtr scsisrc = >source.subsys.u.scsi;
 virDomainHostdevSubsysSCSIVHostPtr scsihostsrc = 
>source.subsys.u.scsi_host;
 virDomainHostdevSubsysMediatedDevPtr mdevsrc = >source.subsys.u.mdev;
+virDomainHostdevSubsysUSBPtr usbsrc = >source.subsys.u.usb;
 VIR_AUTOFREE(char *) managed = NULL;
 VIR_AUTOFREE(char *) sgio = NULL;
 VIR_AUTOFREE(char *) rawio = NULL;
 VIR_AUTOFREE(char *) backendStr = NULL;
 VIR_AUTOFREE(char *) model = NULL;
 VIR_AUTOFREE(char *) display = NULL;
+VIR_AUTOFREE(char *) replug = NULL;
 
 /* @managed can be read from the xml document - it is always an
  * attribute of the toplevel element, no matter what type of
@@ -8176,6 +8185,7 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
 rawio = virXMLPropString(node, "rawio");
 model = virXMLPropString(node, "model");
 display = virXMLPropString(node, "display");
+replug = virXMLPropString(node, "replug");
 
 /* @type is passed in from the caller rather than read from the
  * xml document, because it is specified in different places for
@@ -8242,6 +8252,20 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
 }
 }
 
+if (replug) {
+if (def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
+virReportError(VIR_ERR_XML_ERROR, "%s",
+   _("replug is only supported for usb host device"));
+return -1;
+}
+
+if ((usbsrc->replug = virTristateBoolTypeFromString(replug)) <= 0) {
+virReportError(VIR_ERR_XML_ERROR,
+   _("unknown hostdev replug setting '%s'"), rawio);
+return -1;
+}
+}
+
 if (def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV &&
 def->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST) {
 if (model) {
@@ -27220,6 +27244,7 @@ 

[libvirt] [PATCH v3 REBASE 12/12] conf: parse hostdev missing flag

2019-10-15 Thread Nikolay Shirokovskiy
We want to keep this flag across libvirtd restarts.

Signed-off-by: Nikolay Shirokovskiy 
Reviewed-by: Daniel Henrique Barboza 
---
 src/conf/domain_conf.c | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index d185730023..35006cac8c 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7596,6 +7596,7 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr node,
 VIR_AUTOFREE(char *) startupPolicy = NULL;
 VIR_AUTOFREE(char *) autoAddress = NULL;
 VIR_AUTOFREE(char *) deleteAction = NULL;
+VIR_AUTOFREE(char *) missing = NULL;
 
 if ((startupPolicy = virXMLPropString(node, "startupPolicy"))) {
 def->startupPolicy =
@@ -7625,6 +7626,11 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr node,
 }
 }
 
+if ((missing = virXMLPropString(node, "missing"))) {
+if (STREQ(missing, "yes"))
+def->missing = true;
+}
+
 /* Product can validly be 0, so we need some extra help to determine
  * if it is uninitialized*/
 got_product = false;
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 03/12] qemu: support host usb device unplug

2019-10-15 Thread Nikolay Shirokovskiy
Handle host usb device unplug in DEVICE_DELETED handle execution
path.

Signed-off-by: Nikolay Shirokovskiy 
Reviewed-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_hotplug.c | 38 +++---
 1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 4f7858da71..0e161bfd5a 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -4400,7 +4400,8 @@ qemuDomainRemoveUSBHostDevice(virQEMUDriverPtr driver,
   virDomainHostdevDefPtr hostdev)
 {
 qemuHostdevReAttachUSBDevices(driver, vm->def->name, , 1);
-qemuDomainReleaseDeviceAddress(vm, hostdev->info);
+if (hostdev->deleteAction != VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG)
+qemuDomainReleaseDeviceAddress(vm, hostdev->info);
 }
 
 static void
@@ -4441,6 +4442,7 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 VIR_AUTOFREE(char *) drivealias = NULL;
 VIR_AUTOFREE(char *) objAlias = NULL;
 bool is_vfio = false;
+bool unplug = hostdev->deleteAction == 
VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
 
 VIR_DEBUG("Removing host device %s from domain %p %s",
   hostdev->info->alias, vm, vm->def->name);
@@ -4488,16 +4490,24 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 }
 }
 
-for (i = 0; i < vm->def->nhostdevs; i++) {
-if (vm->def->hostdevs[i] == hostdev) {
-virDomainHostdevRemove(vm->def, i);
-break;
+if (!unplug) {
+for (i = 0; i < vm->def->nhostdevs; i++) {
+if (vm->def->hostdevs[i] == hostdev) {
+virDomainHostdevRemove(vm->def, i);
+break;
+}
 }
 }
 
 virDomainAuditHostdev(vm, hostdev, "detach", true);
 
-if (!is_vfio &&
+/*
+ * In case of unplug the attempt to restore label will fail. But we don't
+ * need to restore the label! In case of separate mount namespace for the
+ * domain we remove device file later in this function. In case of global
+ * mount namespace the device file is deleted or being deleted by systemd.
+ */
+if (!is_vfio && !unplug &&
 qemuSecurityRestoreHostdevLabel(driver, vm, hostdev) < 0)
 VIR_WARN("Failed to restore host device labelling");
 
@@ -4531,7 +4541,13 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 break;
 }
 
-virDomainHostdevDefFree(hostdev);
+if (unplug) {
+virDomainHostdevSubsysUSBPtr usbsrc = >source.subsys.u.usb;
+usbsrc->bus = 0;
+usbsrc->device = 0;
+} else {
+virDomainHostdevDefFree(hostdev);
+}
 
 if (net) {
 if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
@@ -4546,6 +4562,8 @@ qemuDomainRemoveHostDevice(virQEMUDriverPtr driver,
 virDomainNetDefFree(net);
 }
 
+hostdev->deleteAction = VIR_DOMAIN_HOSTDEV_DELETE_ACTION_NONE;
+
 return 0;
 }
 
@@ -4984,6 +5002,7 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
 virDomainDeviceInfoPtr info;
 virObjectEventPtr event;
 VIR_AUTOFREE(char *) alias = NULL;
+bool unplug;
 
 /*
  * save the alias to use when sending a DEVICE_REMOVED event after
@@ -5024,8 +5043,13 @@ qemuDomainRemoveDevice(virQEMUDriverPtr driver,
 return -1;
 break;
 case VIR_DOMAIN_DEVICE_HOSTDEV:
+unplug = dev->data.hostdev->deleteAction == 
VIR_DOMAIN_HOSTDEV_DELETE_ACTION_UNPLUG;
+
 if (qemuDomainRemoveHostDevice(driver, vm, dev->data.hostdev) < 0)
 return -1;
+
+if (unplug)
+return 0;
 break;
 case VIR_DOMAIN_DEVICE_RNG:
 if (qemuDomainRemoveRNGDevice(driver, vm, dev->data.rng) < 0)
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 10/12] qemu: don't mess with non mandatory hostdevs on reattaching

2019-10-15 Thread Nikolay Shirokovskiy
First I don't want to add code to handle dummy device that is used when
host usb device is not present at the moment of starting/migrating etc.
Second supporting non mandatory policies would require to handle races
when host usb device is plugged to host and libvirtd starts adding
device but if in the meanwhile host usb device it unplugged back then
current code will use dummy device which is not desired in this case.

Signed-off-by: Nikolay Shirokovskiy 
Reviewed-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_driver.c  | 8 
 src/qemu/qemu_process.c | 4 
 2 files changed, 12 insertions(+)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e268e2fd1e..f3aae2ba1a 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -5252,6 +5252,10 @@ processUSBAddedEvent(virQEMUDriverPtr driver,
 if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
 continue;
 
+if (hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_OPTIONAL ||
+hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_REQUISITE)
+continue;
+
 usbsrc = >source.subsys.u.usb;
 
 if (!usbsrc->replug)
@@ -5310,6 +5314,10 @@ processUSBRemovedEvent(virQEMUDriverPtr driver,
 if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
 continue;
 
+if (hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_OPTIONAL ||
+hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_REQUISITE)
+continue;
+
 usbsrc = >source.subsys.u.usb;
 
 if (!usbsrc->replug)
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 82e10f4743..2ab3b6f0b9 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -3755,6 +3755,10 @@ qemuProcessReattachUSBDevices(virQEMUDriverPtr driver,
 if (!usbsrc->vendor || !usbsrc->product)
 continue;
 
+if (hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_OPTIONAL ||
+hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_REQUISITE)
+continue;
+
 if (!usbsrc->bus && !usbsrc->device) {
 int num;
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 06/12] qemu: handle libvirtd restart after host usb device unplug

2019-10-15 Thread Nikolay Shirokovskiy
It is possible for libvirtd to go down before DEVICE_DELETED event is
delivered upon usb hostdev unplug and to receive the event after the
libvirtd is up. In order to handle this case we need to save
usb hostdev deleteAction in status file.

Signed-off-by: Nikolay Shirokovskiy 
Reviewed-by: Daniel Henrique Barboza 
---
 src/conf/domain_conf.c | 26 ++
 src/conf/domain_conf.h |  1 +
 src/qemu/qemu_driver.c | 20 ++--
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index c99758d9f5..d185730023 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1244,6 +1244,13 @@ VIR_ENUM_IMPL(virDomainShmemModel,
   "ivshmem-doorbell",
 );
 
+VIR_ENUM_IMPL(virDomainHostdevDeleteAction,
+  VIR_DOMAIN_HOSTDEV_DELETE_ACTION_LAST,
+  "none",
+  "delete",
+  "unplug"
+);
+
 VIR_ENUM_IMPL(virDomainLaunchSecurity,
   VIR_DOMAIN_LAUNCH_SECURITY_LAST,
   "",
@@ -7588,6 +7595,7 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr node,
 virDomainHostdevSubsysUSBPtr usbsrc = >source.subsys.u.usb;
 VIR_AUTOFREE(char *) startupPolicy = NULL;
 VIR_AUTOFREE(char *) autoAddress = NULL;
+VIR_AUTOFREE(char *) deleteAction = NULL;
 
 if ((startupPolicy = virXMLPropString(node, "startupPolicy"))) {
 def->startupPolicy =
@@ -7605,6 +7613,18 @@ virDomainHostdevSubsysUSBDefParseXML(xmlNodePtr node,
 usbsrc->autoAddress = true;
 }
 
+if ((deleteAction = virXMLPropString(node, "deleteAction"))) {
+def->deleteAction =
+virDomainHostdevDeleteActionTypeFromString(deleteAction);
+
+if (def->deleteAction <= 0) {
+virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+   _("Unknown deleteAction '%s'"),
+   deleteAction);
+goto out;
+}
+}
+
 /* Product can validly be 0, so we need some extra help to determine
  * if it is uninitialized*/
 got_product = false;
@@ -25055,6 +25075,12 @@ virDomainHostdevDefFormatSubsys(virBufferPtr buf,
 
 if (def->missing && !(flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE))
 virBufferAddLit(buf, " missing='yes'");
+
+if (def->deleteAction && (flags & VIR_DOMAIN_DEF_FORMAT_STATUS)) {
+const char *deleteAction;
+deleteAction = 
virDomainHostdevDeleteActionTypeToString(def->deleteAction);
+virBufferAsprintf(buf, " deleteAction='%s'", deleteAction);
+}
 }
 
 if (def->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI &&
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 0a16413852..8055debe56 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -340,6 +340,7 @@ typedef enum {
 
 VIR_DOMAIN_HOSTDEV_DELETE_ACTION_LAST
 } virDomainHostdevDeleteActionType;
+VIR_ENUM_DECL(virDomainHostdevDeleteAction);
 
 
 /* basic device for direct passthrough */
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 1110e11c14..d7b44dab2c 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -5187,10 +5187,13 @@ processUSBAddedEvent(virQEMUDriverPtr driver,
 {
 virDomainHostdevDefPtr hostdev;
 virDomainHostdevSubsysUSBPtr usbsrc;
+virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
 size_t i;
 
-if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0)
+if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) {
+virObjectUnref(cfg);
 return;
+}
 
 if (!virDomainObjIsActive(vm)) {
 VIR_DEBUG("Domain is not running");
@@ -5218,8 +5221,13 @@ processUSBAddedEvent(virQEMUDriverPtr driver,
 if (qemuDomainAttachHostDevice(driver, vm, hostdev) < 0)
 goto cleanup;
 
+if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 
0)
+VIR_WARN("unable to save domain status after plugging device %s",
+ hostdev->info->alias);
+
  cleanup:
 qemuDomainObjEndJob(driver, vm);
+virObjectUnref(cfg);
 }
 
 
@@ -5231,9 +5239,12 @@ processUSBRemovedEvent(virQEMUDriverPtr driver,
 size_t i;
 virDomainHostdevDefPtr hostdev;
 virDomainDeviceDef dev = { .type = VIR_DOMAIN_DEVICE_HOSTDEV };
+virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
 
-if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0)
+if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) {
+virObjectUnref(cfg);
 return;
+}
 
 if (!virDomainObjIsActive(vm)) {
 VIR_DEBUG("Domain is not running");
@@ -5269,8 +5280,13 @@ processUSBRemovedEvent(virQEMUDriverPtr driver,
 if (qemuDomainDetachDeviceLive(vm, , driver, true, true) < 0)
 goto cleanup;
 
+if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 
0)
+VIR_WARN("unable to save domain status after unplugging device 

[libvirt] [PATCH v3 REBASE 00/12] hostdev: handle usb detach/attach on node

2019-10-15 Thread Nikolay Shirokovskiy
Diff to v2[1] version:
- add 'replug' attribute for hostdev element to allow replug semantics
- avoid accuiring domain lock in event loop thread on udev events as
  suggested by Peter
- nit picks after review by Daniel Henrique Barboza

* is used to mark patches that were 'Reviewed-by' by Daniel (sometimes
  with very minor changes to take into account new replug flag).

[1] https://www.redhat.com/archives/libvir-list/2019-September/msg00321.html

Nikolay Shirokovskiy (12):
  conf: add replug option for usb hostdev
  qemu: track hostdev delete intention
  qemu: support host usb device unplug
  qemu: support usb hostdev plugging back
  qemu: handle host usb device add/del udev events
  qemu: handle libvirtd restart after host usb device unplug
  qemu: handle race on device deletion and usb host device plugging
  qemu: hotplug: update device list on device deleted event
  qemu: handle host usb device plug/unplug when libvirtd is down
  qemu: don't mess with non mandatory hostdevs on reattaching
  qemu: handle detaching of unplugged hostdev
  conf: parse hostdev missing flag

 docs/formatdomain.html.in |  10 +-
 docs/schemas/domaincommon.rng |   5 +
 src/conf/domain_conf.c|  62 +++
 src/conf/domain_conf.h|  17 +
 src/qemu/Makefile.inc.am  |   2 +
 src/qemu/qemu_conf.h  |   3 +
 src/qemu/qemu_domain.c|   2 +
 src/qemu/qemu_domain.h|   2 +
 src/qemu/qemu_driver.c| 404 +-
 src/qemu/qemu_hotplug.c   | 104 -
 src/qemu/qemu_hotplug.h   |   3 +-
 src/qemu/qemu_process.c   |  60 +++
 src/util/virhostdev.c |   2 +
 tests/qemuhotplugtest.c   |   2 +-
 tests/qemuxml2argvdata/hostdev-usb-replug.xml |  36 ++
 .../qemuxml2xmloutdata/hostdev-usb-replug.xml |  40 ++
 tests/qemuxml2xmltest.c   |   1 +
 17 files changed, 733 insertions(+), 22 deletions(-)
 create mode 100644 tests/qemuxml2argvdata/hostdev-usb-replug.xml
 create mode 100644 tests/qemuxml2xmloutdata/hostdev-usb-replug.xml

-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 REBASE 09/12] qemu: handle host usb device plug/unplug when libvirtd is down

2019-10-15 Thread Nikolay Shirokovskiy
Somebody can easily unplug usb device from host while libvirtd is being
stopped. Also usb device can be plugged or unplugged/plugged back and so
forth. Let's handle such cases.

Signed-off-by: Nikolay Shirokovskiy 
Reviewed-by: Daniel Henrique Barboza 
---
 src/qemu/qemu_process.c | 56 +
 1 file changed, 56 insertions(+)

diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 0afb7c79e4..82e10f4743 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -3734,6 +3734,59 @@ qemuProcessUpdateDevices(virQEMUDriverPtr driver,
 return ret;
 }
 
+
+static int
+qemuProcessReattachUSBDevices(virQEMUDriverPtr driver,
+  virDomainObjPtr vm)
+{
+size_t i;
+
+for (i = 0; i < vm->def->nhostdevs; i++) {
+virDomainHostdevDefPtr hostdev = vm->def->hostdevs[i];
+virDomainHostdevSubsysUSBPtr usbsrc = >source.subsys.u.usb;
+
+if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB 
||
+!usbsrc->replug)
+continue;
+
+/* don't mess with devices that don't use stable host addressing
+ * with respect to unplug/plug to host
+ */
+if (!usbsrc->vendor || !usbsrc->product)
+continue;
+
+if (!usbsrc->bus && !usbsrc->device) {
+int num;
+
+if ((num = virUSBDeviceFindByVendor(usbsrc->vendor, 
usbsrc->product,
+NULL, false, NULL)) < 0)
+return -1;
+
+if (num > 0 &&
+qemuDomainAttachHostDevice(driver, vm, hostdev) < 0)
+return -1;
+} else {
+virUSBDevicePtr usb;
+
+if (virUSBDeviceFindByBus(usbsrc->bus, usbsrc->device,
+  NULL, false, ) < 0)
+return -1;
+
+if (!usb) {
+virDomainDeviceDef dev = { .type = VIR_DOMAIN_DEVICE_HOSTDEV };
+
+dev.data.hostdev = hostdev;
+if (qemuDomainDetachDeviceLive(vm, , driver, true, true) < 
0)
+return -1;
+}
+virUSBDeviceFree(usb);
+}
+}
+
+return 0;
+}
+
+
 static int
 qemuDomainPerfRestart(virDomainObjPtr vm)
 {
@@ -8220,6 +8273,9 @@ qemuProcessReconnect(void *opaque)
 if (qemuProcessUpdateDevices(driver, obj) < 0)
 goto error;
 
+if (qemuProcessReattachUSBDevices(driver, obj) < 0)
+goto error;
+
 if (qemuRefreshPRManagerState(driver, obj) < 0)
 goto error;
 
-- 
2.23.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 00/30] storagefile, security: qcow2 data_file support

2019-10-15 Thread Cole Robinson
On 10/15/19 12:29 AM, Han Han wrote:
> I find the issue cannot reproduced when `make clean` before build the
> source.
> It is not proper to build with an unclean source dir, right?
> 

I don't use 'make clean' in libvirt.git but in other projects I have hit
issues that required 'make clean', like in qemu if trying to run old
commits, often the build system gets confused. I dip in and out of
libvirt development though so maybe regular devs have hit similar issues

- Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH] qemu: fix EFI nvram removal on domain undefine

2019-10-15 Thread Pavel Mores
On Tue, Oct 15, 2019 at 03:45:55PM +0200, Michal Privoznik wrote:
> On 10/15/19 3:15 PM, Pavel Mores wrote:
> > On Tue, Oct 15, 2019 at 01:51:32PM +0200, Michal Privoznik wrote:
> > > On 10/15/19 10:31 AM, Pavel Mores wrote:
> > > > When undefining a UEFI domain its nvram file has to be properly handled 
> > > > as
> > > > well.  It's mandatory to use one of --nvram and --keep-nvram options 
> > > > when
> > > > 'virsh undefine ' is issued for a UEFI domain.  To fix the bug 
> > > > as
> > > > reported, virsh should return an error message if neither option is used
> > > > and the nvram file should be removed when --nvram is given.
> > > > 
> > > > The cause of the problem is that when qemuDomainUndefineFlags() is 
> > > > invoked
> > > > on an inactive domain the path to its nvram file is empty.  This commit
> > > > aims to fix this by formatting and filling in the path in time for the
> > > > nvram removal code to run properly.
> > > > 
> > > > https://bugzilla.redhat.com/show_bug.cgi?id=1751596
> > > > 
> > > > Signed-off-by: Pavel Mores 
> > > > ---
> > > >src/qemu/qemu_domain.c | 12 +---
> > > >src/qemu/qemu_domain.h |  4 
> > > >src/qemu/qemu_driver.c | 20 +++-
> > > >3 files changed, 28 insertions(+), 8 deletions(-)
> > > > 
> > > > diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
> > > > index 35067c851f..1a0367cc27 100644
> > > > --- a/src/qemu/qemu_domain.c
> > > > +++ b/src/qemu/qemu_domain.c
> > > > @@ -15441,16 +15441,22 @@ 
> > > > qemuDomainDiskIsMissingLocalOptional(virDomainDiskDefPtr disk)
> > > >}
> > > > +int
> > > > +qemuDomainNVRAMPathFormat(virQEMUDriverConfigPtr cfg,
> > > > +virDomainDefPtr def, char **path)
> > > > +{
> > > > +return virAsprintf(path, "%s/%s_VARS.fd", cfg->nvramDir, 
> > > > def->name);
> > > > +}
> > > > +
> > > 
> > > We have two empty lines between functions in this file. And Also, one
> > > argument per line please.
> > > 
> > > >int
> > > >qemuDomainNVRAMPathGenerate(virQEMUDriverConfigPtr cfg,
> > > >virDomainDefPtr def)
> > > >{
> > > >if (def->os.loader &&
> > > >def->os.loader->type == VIR_DOMAIN_LOADER_TYPE_PFLASH &&
> > > > -def->os.loader->readonly == VIR_TRISTATE_SWITCH_ON &&
> > > > +def->os.loader->readonly == VIR_TRISTATE_BOOL_YES &&
> > > >!def->os.loader->nvram) {
> > > > -return virAsprintf(>os.loader->nvram, "%s/%s_VARS.fd",
> > > > -   cfg->nvramDir, def->name);
> > > > +return qemuDomainNVRAMPathFormat(cfg, def, 
> > > > >os.loader->nvram);
> > > >}
> > > >return 0;
> > > > diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
> > > > index 01a54d4265..07725c7cc4 100644
> > > > --- a/src/qemu/qemu_domain.h
> > > > +++ b/src/qemu/qemu_domain.h
> > > > @@ -1207,6 +1207,10 @@ 
> > > > qemuDomainIsUsingNoShutdown(qemuDomainObjPrivatePtr priv);
> > > >bool
> > > >qemuDomainDiskIsMissingLocalOptional(virDomainDiskDefPtr disk);
> > > > +int
> > > > +qemuDomainNVRAMPathFormat(virQEMUDriverConfigPtr cfg,
> > > > +virDomainDefPtr def, char **path);
> > > > +
> > > >int
> > > >qemuDomainNVRAMPathGenerate(virQEMUDriverConfigPtr cfg,
> > > >virDomainDefPtr def);
> > > > diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
> > > > index bc0ede2fb0..dcaadbdb52 100644
> > > > --- a/src/qemu/qemu_driver.c
> > > > +++ b/src/qemu/qemu_driver.c
> > > > @@ -7828,6 +7828,8 @@ qemuDomainUndefineFlags(virDomainPtr dom,
> > > >int nsnapshots;
> > > >int ncheckpoints;
> > > >virQEMUDriverConfigPtr cfg = NULL;
> > > > +char *nvram_path = NULL;
> > > > +bool need_deallocate_nvram_path = false;
> > > 
> > > While this works, I'd rather have @nvram_path autofreed, and ... [1]
> > > 
> > > >virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE |
> > > >  VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA |
> > > > @@ -7905,14 +7907,20 @@ qemuDomainUndefineFlags(virDomainPtr dom,
> > > >}
> > > >}
> > > > -if (vm->def->os.loader &&
> > > > -vm->def->os.loader->nvram &&
> > > > -virFileExists(vm->def->os.loader->nvram)) {
> > > > +if (vm->def->os.firmware == VIR_DOMAIN_OS_DEF_FIRMWARE_EFI) {
> > > > +qemuDomainNVRAMPathFormat(cfg, vm->def, _path);
> > > 
> > > This needs a check for retval.
> > > 
> > > > +need_deallocate_nvram_path = true;
> > > > +} else {
> > > > +if (vm->def->os.loader)
> > > > +nvram_path = vm->def->os.loader->nvram;
> > > 
> > > 1: ... duplicate path here.
> > > 
> > > > +}
> > > > +
> > > > +if (nvram_path && virFileExists(nvram_path)) {
> > > >if ((flags & VIR_DOMAIN_UNDEFINE_NVRAM)) {
> > > > -if (unlink(vm->def->os.loader->nvram) < 0) {
> > > > +if 

Re: [libvirt] [PATCH 7/8] examples: Use G_N_ELEMENTS instead of ARRAY_CARDINALITY

2019-10-15 Thread Andrea Bolognani
On Tue, 2019-10-15 at 14:21 +0200, Ján Tomko wrote:
> We try to keep the example programs independent of libraries
> other than libvirt.
> 
> Rename the locally defined ARRAY_CARDINALITY macro to G_N_ELEMENTS
> which GLib provides, even though we don't actually include GLib.

[...]

> -#define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array)))
> +#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))

In this case, and in this case only, I think we should leave the name
alone.

When we use GLib and simply provide our own implementation when using
a version that's too old, the GLib name is obviously accurate, but in
this specific case I think it's misleading and we should stick with
the current one.

-- 
Andrea Bolognani / Red Hat / Virtualization

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

  1   2   >