With this patch, block write threshold events delivered by qemu
are converted into libvirt events.
This patch takes the easy road, and only reports events if
the node name is still cached by libvirtd (true in the common
case when libvirtd is not restarted, since you can't get an
event if you didn't register a threshold). A followup patch
will be needed to properly handle grabbing the job lock and
getting the node name when the cache lookup fails, using code
similar to how we update domain XML after a block job
completes. But for getting the initial API in place, I
figured this was a good enough start.
* src/qemu/qemu_monitor_json.c
(qemuMonitorJSONHandleBlockWriteThreshold): New function.
* src/qemu/qemu_monitor.c (qemuMonitorEmitBlockThreshold):
Likewise.
* src/qemu/qemu_monitor.h (qemuMonitorEmitBlockThreshold):
Likewise.
* src/qemu/qemu_process.c (qemuProcessHandleBlockThreshold):
Likewise.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/qemu/qemu_monitor.c | 14 ++++++++++++++
src/qemu/qemu_monitor.h | 11 +++++++++++
src/qemu/qemu_monitor_json.c | 31 +++++++++++++++++++++++++++++++
src/qemu/qemu_process.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 100 insertions(+)
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index a3ad740..c4b6073 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -1433,6 +1433,20 @@ qemuMonitorEmitBlockJob(qemuMonitorPtr mon,
}
+int qemuMonitorEmitBlockThreshold(qemuMonitorPtr mon,
+ const char *node,
+ unsigned long long threshold,
+ unsigned long long length)
+{
+ int ret = -1;
+ VIR_DEBUG("mon=%p", mon);
+
+ QEMU_MONITOR_CALLBACK(mon, ret, domainBlockThreshold, mon->vm,
+ node, threshold, length);
+ return ret;
+}
+
+
int
qemuMonitorEmitBalloonChange(qemuMonitorPtr mon,
unsigned long long actual)
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 541f774..8c84cdb 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -146,6 +146,12 @@ typedef int (*qemuMonitorDomainBlockJobCallback)(qemuMonitorPtr mon,
int type,
int status,
void *opaque);
+typedef int (*qemuMonitorDomainBlockThresholdCallback)(qemuMonitorPtr mon,
+ virDomainObjPtr vm,
+ const char *node,
+ unsigned long long threshold,
+ unsigned long long length,
+ void *opaque);
typedef int (*qemuMonitorDomainTrayChangeCallback)(qemuMonitorPtr mon,
virDomainObjPtr vm,
const char *devAlias,
@@ -204,6 +210,7 @@ struct _qemuMonitorCallbacks {
qemuMonitorDomainIOErrorCallback domainIOError;
qemuMonitorDomainGraphicsCallback domainGraphics;
qemuMonitorDomainBlockJobCallback domainBlockJob;
+ qemuMonitorDomainBlockThresholdCallback domainBlockThreshold;
qemuMonitorDomainTrayChangeCallback domainTrayChange;
qemuMonitorDomainPMWakeupCallback domainPMWakeup;
qemuMonitorDomainPMSuspendCallback domainPMSuspend;
@@ -301,6 +308,10 @@ int qemuMonitorEmitBlockJob(qemuMonitorPtr mon,
const char *diskAlias,
int type,
int status);
+int qemuMonitorEmitBlockThreshold(qemuMonitorPtr mon,
+ const char *node,
+ unsigned long long threshold,
+ unsigned long long length);
int qemuMonitorEmitBalloonChange(qemuMonitorPtr mon,
unsigned long long actual);
int qemuMonitorEmitPMSuspendDisk(qemuMonitorPtr mon);
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 885b6f4..46383d7 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -77,6 +77,8 @@ static void qemuMonitorJSONHandlePMSuspend(qemuMonitorPtr mon,
virJSONValuePtr d
static void qemuMonitorJSONHandleBlockJobCompleted(qemuMonitorPtr mon, virJSONValuePtr
data);
static void qemuMonitorJSONHandleBlockJobCanceled(qemuMonitorPtr mon, virJSONValuePtr
data);
static void qemuMonitorJSONHandleBlockJobReady(qemuMonitorPtr mon, virJSONValuePtr
data);
+static void qemuMonitorJSONHandleBlockWriteThreshold(qemuMonitorPtr mon,
+ virJSONValuePtr data);
static void qemuMonitorJSONHandleBalloonChange(qemuMonitorPtr mon, virJSONValuePtr
data);
static void qemuMonitorJSONHandlePMSuspendDisk(qemuMonitorPtr mon, virJSONValuePtr
data);
static void qemuMonitorJSONHandleGuestPanic(qemuMonitorPtr mon, virJSONValuePtr data);
@@ -96,6 +98,7 @@ static qemuEventHandler eventHandlers[] = {
{ "BLOCK_JOB_CANCELLED", qemuMonitorJSONHandleBlockJobCanceled, },
{ "BLOCK_JOB_COMPLETED", qemuMonitorJSONHandleBlockJobCompleted, },
{ "BLOCK_JOB_READY", qemuMonitorJSONHandleBlockJobReady, },
+ { "BLOCK_WRITE_THRESHOLD", qemuMonitorJSONHandleBlockWriteThreshold, },
{ "DEVICE_DELETED", qemuMonitorJSONHandleDeviceDeleted, },
{ "DEVICE_TRAY_MOVED", qemuMonitorJSONHandleTrayChange, },
{ "GUEST_PANICKED", qemuMonitorJSONHandleGuestPanic, },
@@ -843,6 +846,34 @@ qemuMonitorJSONHandleBlockJobReady(qemuMonitorPtr mon,
VIR_DOMAIN_BLOCK_JOB_READY);
}
+
+static void
+qemuMonitorJSONHandleBlockWriteThreshold(qemuMonitorPtr mon,
+ virJSONValuePtr data)
+{
+ const char *node;
+ unsigned long long offset = 0, len = 0;
+
+ if ((node = virJSONValueObjectGetString(data, "node-name")) == NULL) {
+ VIR_WARN("missing node in block threshold event");
+ goto out;
+ }
+
+ if (virJSONValueObjectGetNumberUlong(data, "write-threshold",
+ &offset) < 0) {
+ VIR_WARN("missing threshold in block threshold event");
+ goto out;
+ }
+
+ if (virJSONValueObjectGetNumberUlong(data, "amount-exceeded", &len)
< 0) {
+ VIR_WARN("missing len in block threshold event");
+ goto out;
+ }
+
+ out:
+ qemuMonitorEmitBlockThreshold(mon, node, offset, len);
+}
+
static void
qemuMonitorJSONHandleBalloonChange(qemuMonitorPtr mon,
virJSONValuePtr data)
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index ba84182..5f582e5 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -1037,6 +1037,49 @@ qemuProcessHandleBlockJob(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
static int
+qemuProcessHandleBlockThreshold(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
+ virDomainObjPtr vm,
+ const char *node,
+ unsigned long long threshold,
+ unsigned long long length,
+ void *opaque)
+{
+ virQEMUDriverPtr driver = opaque;
+ virDomainDiskDefPtr disk;
+ virObjectEventPtr event = NULL;
+ const char *path = NULL;
+
+ virObjectLock(vm);
+
+ VIR_DEBUG("Block threshold for node %s (domain: %p,%s) threshold %llu "
+ "length %llu", node, vm, vm->def->name, threshold,
length);
+
+ /* TODO: If the node name is not found in the current domain XML,
+ * we need to grab a job lock and query the monitor to repopulate
+ * the node name cache. For now, if that happens, we just discard
+ * the event. */
+ if (!(disk = qemuDomainDiskResolveAllocationNode(driver, vm, node,
+ false))) {
+ VIR_WARN("failed to locate disk matching node '%s'", node);
+ goto cleanup;
+ }
+ if (virStorageSourceIsLocalStorage(disk->src))
+ path = disk->src->path;
+
+ /* TODO: Once we support node names for more than just the active
+ * layer, we will need to map this into 'vda[1]' notation. */
+ event = virDomainEventWriteThresholdNewFromObj(vm, disk->dst, path,
+ threshold, length);
+
+ cleanup:
+ virObjectUnlock(vm);
+ if (event)
+ qemuDomainEventQueue(driver, event);
+ return 0;
+}
+
+
+static int
qemuProcessHandleGraphics(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
int phase,
@@ -1522,6 +1565,7 @@ static qemuMonitorCallbacks monitorCallbacks = {
.domainIOError = qemuProcessHandleIOError,
.domainGraphics = qemuProcessHandleGraphics,
.domainBlockJob = qemuProcessHandleBlockJob,
+ .domainBlockThreshold = qemuProcessHandleBlockThreshold,
.domainTrayChange = qemuProcessHandleTrayChange,
.domainPMWakeup = qemuProcessHandlePMWakeup,
.domainPMSuspend = qemuProcessHandlePMSuspend,
--
2.4.3