https://bugzilla.redhat.com/show_bug.cgi?id=1293351
I've came across a bit of a silly scenario, but long story short:
after domain was resumed, the virDomainSetTime() API hung for 5
seconds after which it failed with an error. Problem was, that
there is no guest agent installed in the domain. But this got me
thinking and digging into the code. So even though we do listen
to VSERPORT_CHANGE events and connect and disconnect ourselves to
guest agent, we still do connect to the guest agent at both
domain startup and resume. This is a bit silly as it produces the
delay - basically, because we connect to the guest agent,
priv->agent is not NULL. Therefore qemuDomainAgentAvailable()
will return true. And the place where we hang? Well,
historically, when there was no VSERPORT_CHANGE event, we used a
dummy ping command to the guest which has 5 seconds timeout. And
it's still there and effective. So there's where the delay comes
from.
What's the resolution? Well, I'm introducing new capability
QEMU_CAPS_VSERPORT_CHANGE that is enabled iff qemu is capable of
the event. And, during domain startup, reconnect after resume and
attach we do connect to the agent socket iff the capability is
NOT set.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/qemu/qemu_capabilities.c | 2 ++
src/qemu/qemu_capabilities.h | 1 +
src/qemu/qemu_domain.c | 11 ++++-------
src/qemu/qemu_domain.h | 2 +-
src/qemu/qemu_driver.c | 2 +-
src/qemu/qemu_process.c | 25 +++++++++++++++++++------
src/qemu/qemu_process.h | 4 +++-
tests/qemucapabilitiesdata/caps_2.1.1-1.caps | 1 +
tests/qemucapabilitiesdata/caps_2.4.0-1.caps | 1 +
tests/qemucapabilitiesdata/caps_2.5.0-1.caps | 1 +
10 files changed, 34 insertions(+), 16 deletions(-)
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 6e5d203..9e11af9 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -308,6 +308,7 @@ VIR_ENUM_IMPL(virQEMUCaps, QEMU_CAPS_LAST,
"virtio-tablet", /* 205 */
"virtio-input-host",
+ "vserport-change-event",
);
@@ -1479,6 +1480,7 @@ struct virQEMUCapsStringFlags virQEMUCapsEvents[] = {
{ "SPICE_MIGRATE_COMPLETED", QEMU_CAPS_SEAMLESS_MIGRATION },
{ "DEVICE_DELETED", QEMU_CAPS_DEVICE_DEL_EVENT },
{ "MIGRATION", QEMU_CAPS_MIGRATION_EVENT },
+ { "VSERPORT_CHANGE", QEMU_CAPS_VSERPORT_CHANGE },
};
struct virQEMUCapsStringFlags virQEMUCapsObjectTypes[] = {
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index 61d6379..983faf6 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -335,6 +335,7 @@ typedef enum {
/* 205 */
QEMU_CAPS_VIRTIO_TABLET, /* -device virtio-tablet-{device,pci} */
QEMU_CAPS_VIRTIO_INPUT_HOST, /* -device virtio-input-host-{device,pci} */
+ QEMU_CAPS_VSERPORT_CHANGE, /* VSERPORT_CHANGE event */
QEMU_CAPS_LAST /* this must always be the last item */
} virQEMUCapsFlags;
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index bb8d47f..1c546f8 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -3650,10 +3650,9 @@ qemuDomainSupportsBlockJobs(virDomainObjPtr vm,
* Returns the pointer to the channel definition that is used to access the
* guest agent if the agent is configured or NULL otherwise.
*/
-virDomainChrSourceDefPtr
+virDomainChrDefPtr
qemuFindAgentConfig(virDomainDefPtr def)
{
- virDomainChrSourceDefPtr config = NULL;
size_t i;
for (i = 0; i < def->nchannels; i++) {
@@ -3662,13 +3661,11 @@ qemuFindAgentConfig(virDomainDefPtr def)
if (channel->targetType != VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO)
continue;
- if (STREQ_NULLABLE(channel->target.name, "org.qemu.guest_agent.0"))
{
- config = &channel->source;
- break;
- }
+ if (STREQ_NULLABLE(channel->target.name, "org.qemu.guest_agent.0"))
+ return channel;
}
- return config;
+ return NULL;
}
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index cff48d7..ca88f8a 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -487,7 +487,7 @@ int qemuDomainAlignMemorySizes(virDomainDefPtr def);
void qemuDomainMemoryDeviceAlignSize(virDomainDefPtr def,
virDomainMemoryDefPtr mem);
-virDomainChrSourceDefPtr qemuFindAgentConfig(virDomainDefPtr def);
+virDomainChrDefPtr qemuFindAgentConfig(virDomainDefPtr def);
bool qemuDomainMachineIsQ35(const virDomainDef *def);
bool qemuDomainMachineIsI440FX(const virDomainDef *def);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index e8ba3a6..fc00073 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4486,7 +4486,7 @@ processSerialChangedEvent(virQEMUDriverPtr driver,
if (STREQ_NULLABLE(dev.data.chr->target.name, "org.qemu.guest_agent.0"))
{
if (newstate == VIR_DOMAIN_CHR_DEVICE_STATE_CONNECTED) {
if (!priv->agent) {
- if ((rc = qemuConnectAgent(driver, vm)) == -2)
+ if ((rc = qemuConnectAgent(driver, vm, true)) == -2)
goto endjob;
if (rc < 0)
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index f274068..3aca227 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -198,16 +198,29 @@ static qemuAgentCallbacks agentCallbacks = {
int
-qemuConnectAgent(virQEMUDriverPtr driver, virDomainObjPtr vm)
+qemuConnectAgent(virQEMUDriverPtr driver,
+ virDomainObjPtr vm,
+ bool force)
{
qemuDomainObjPrivatePtr priv = vm->privateData;
int ret = -1;
qemuAgentPtr agent = NULL;
- virDomainChrSourceDefPtr config = qemuFindAgentConfig(vm->def);
+ virDomainChrDefPtr config = qemuFindAgentConfig(vm->def);
if (!config)
return 0;
+ if (!force &&
+ virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_VSERPORT_CHANGE) &&
+ config->state == VIR_DOMAIN_CHR_DEVICE_STATE_DISCONNECTED) {
+ /* With newer qemus capable of VSERPORT_CHANGE event, we are connecting and
+ * disconnecting to the guest agent as it connects or disconnects to the
+ * channel within the guest. So, it's important to connect here iff we are
+ * running qemu not capable of the event or we are called from the event
+ * callback (@force == true). */
+ return 0;
+ }
+
if (virSecurityManagerSetDaemonSocketLabel(driver->securityManager,
vm->def) < 0) {
VIR_ERROR(_("Failed to set security context for agent for %s"),
@@ -223,7 +236,7 @@ qemuConnectAgent(virQEMUDriverPtr driver, virDomainObjPtr vm)
virObjectUnlock(vm);
agent = qemuAgentOpen(vm,
- config,
+ &config->source,
&agentCallbacks);
virObjectLock(vm);
@@ -3525,7 +3538,7 @@ qemuProcessReconnect(void *opaque)
goto error;
/* Failure to connect to agent shouldn't be fatal */
- if ((ret = qemuConnectAgent(driver, obj)) < 0) {
+ if ((ret = qemuConnectAgent(driver, obj, false)) < 0) {
if (ret == -2)
goto error;
@@ -4950,7 +4963,7 @@ qemuProcessLaunch(virConnectPtr conn,
goto cleanup;
/* Failure to connect to agent shouldn't be fatal */
- if ((rv = qemuConnectAgent(driver, vm)) < 0) {
+ if ((rv = qemuConnectAgent(driver, vm, false)) < 0) {
if (rv == -2)
goto cleanup;
@@ -5665,7 +5678,7 @@ int qemuProcessAttach(virConnectPtr conn ATTRIBUTE_UNUSED,
goto error;
/* Failure to connect to agent shouldn't be fatal */
- if ((ret = qemuConnectAgent(driver, vm)) < 0) {
+ if ((ret = qemuConnectAgent(driver, vm, false)) < 0) {
if (ret == -2)
goto error;
diff --git a/src/qemu/qemu_process.h b/src/qemu/qemu_process.h
index 85e3a06..555181f 100644
--- a/src/qemu/qemu_process.h
+++ b/src/qemu/qemu_process.h
@@ -151,6 +151,8 @@ int qemuProcessSPICEAllocatePorts(virQEMUDriverPtr driver,
virDomainDiskDefPtr qemuProcessFindDomainDiskByAlias(virDomainObjPtr vm,
const char *alias);
-int qemuConnectAgent(virQEMUDriverPtr driver, virDomainObjPtr vm);
+int qemuConnectAgent(virQEMUDriverPtr driver,
+ virDomainObjPtr vm,
+ bool force);
#endif /* __QEMU_PROCESS_H__ */
diff --git a/tests/qemucapabilitiesdata/caps_2.1.1-1.caps
b/tests/qemucapabilitiesdata/caps_2.1.1-1.caps
index 1098dcf..332b85a 100644
--- a/tests/qemucapabilitiesdata/caps_2.1.1-1.caps
+++ b/tests/qemucapabilitiesdata/caps_2.1.1-1.caps
@@ -159,4 +159,5 @@
<flag name='rtl8139'/>
<flag name='e1000'/>
<flag name='virtio-net'/>
+ <flag name='vserport-change-event'/>
</qemuCaps>
diff --git a/tests/qemucapabilitiesdata/caps_2.4.0-1.caps
b/tests/qemucapabilitiesdata/caps_2.4.0-1.caps
index d67a48d..1a5fe81 100644
--- a/tests/qemucapabilitiesdata/caps_2.4.0-1.caps
+++ b/tests/qemucapabilitiesdata/caps_2.4.0-1.caps
@@ -167,4 +167,5 @@
<flag name='virtio-mouse'/>
<flag name='virtio-tablet'/>
<flag name='virtio-input-host'/>
+ <flag name='vserport-change-event'/>
</qemuCaps>
diff --git a/tests/qemucapabilitiesdata/caps_2.5.0-1.caps
b/tests/qemucapabilitiesdata/caps_2.5.0-1.caps
index f4f3673..8b3586a 100644
--- a/tests/qemucapabilitiesdata/caps_2.5.0-1.caps
+++ b/tests/qemucapabilitiesdata/caps_2.5.0-1.caps
@@ -168,4 +168,5 @@
<flag name='virtio-mouse'/>
<flag name='virtio-tablet'/>
<flag name='virtio-input-host'/>
+ <flag name='vserport-change-event'/>
</qemuCaps>
--
2.4.10