edecca
From 89bfdeacefd0160080ab98a41109c75db6d5e913 Mon Sep 17 00:00:00 2001
edecca
Message-Id: <89bfdeacefd0160080ab98a41109c75db6d5e913@dist-git>
edecca
From: Michal Privoznik <mprivozn@redhat.com>
edecca
Date: Wed, 11 Jul 2018 17:27:26 +0200
edecca
Subject: [PATCH] qemu: Wire up PR_MANAGER_STATUS_CHANGED event
edecca
MIME-Version: 1.0
edecca
Content-Type: text/plain; charset=UTF-8
edecca
Content-Transfer-Encoding: 8bit
edecca
edecca
https://bugzilla.redhat.com/show_bug.cgi?id=1470007
edecca
edecca
This event is emitted on the monitor if one of pr-managers lost
edecca
connection to its pr-helper process. What libvirt needs to do is
edecca
restart the pr-helper process iff it corresponds to managed
edecca
pr-manager.
edecca
edecca
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
edecca
(cherry picked from commit 6fbda83330293ed8bec1ea9e3ba7273c4ee2b9e2)
edecca
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
edecca
Reviewed-by: Ján Tomko <jtomko@redhat.com>
edecca
---
edecca
 src/qemu/qemu_domain.c       |  1 +
edecca
 src/qemu/qemu_domain.h       |  1 +
edecca
 src/qemu/qemu_driver.c       | 17 +++++++++++
edecca
 src/qemu/qemu_monitor.c      | 15 ++++++++++
edecca
 src/qemu/qemu_monitor.h      | 11 ++++++++
edecca
 src/qemu/qemu_monitor_json.c | 23 +++++++++++++++
edecca
 src/qemu/qemu_process.c      | 55 ++++++++++++++++++++++++++++++++++++
edecca
 7 files changed, 123 insertions(+)
edecca
edecca
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
edecca
index cfecbaca74..dec057e021 100644
edecca
--- a/src/qemu/qemu_domain.c
edecca
+++ b/src/qemu/qemu_domain.c
edecca
@@ -12979,6 +12979,7 @@ qemuProcessEventFree(struct qemuProcessEvent *event)
edecca
     case QEMU_PROCESS_EVENT_MONITOR_EOF:
edecca
         VIR_FREE(event->data);
edecca
         break;
edecca
+    case QEMU_PROCESS_EVENT_PR_DISCONNECT:
edecca
     case QEMU_PROCESS_EVENT_LAST:
edecca
         break;
edecca
     }
edecca
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
edecca
index 30d186a921..e748d78adb 100644
edecca
--- a/src/qemu/qemu_domain.h
edecca
+++ b/src/qemu/qemu_domain.h
edecca
@@ -477,6 +477,7 @@ typedef enum {
edecca
     QEMU_PROCESS_EVENT_SERIAL_CHANGED,
edecca
     QEMU_PROCESS_EVENT_BLOCK_JOB,
edecca
     QEMU_PROCESS_EVENT_MONITOR_EOF,
edecca
+    QEMU_PROCESS_EVENT_PR_DISCONNECT,
edecca
 
edecca
     QEMU_PROCESS_EVENT_LAST
edecca
 } qemuProcessEventType;
edecca
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
edecca
index 825b2b27e6..868ef9d406 100644
edecca
--- a/src/qemu/qemu_driver.c
edecca
+++ b/src/qemu/qemu_driver.c
edecca
@@ -4792,6 +4792,20 @@ processMonitorEOFEvent(virQEMUDriverPtr driver,
edecca
 }
edecca
 
edecca
 
edecca
+static void
edecca
+processPRDisconnectEvent(virDomainObjPtr vm)
edecca
+{
edecca
+    qemuDomainObjPrivatePtr priv = vm->privateData;
edecca
+
edecca
+    if (!virDomainObjIsActive(vm))
edecca
+        return;
edecca
+
edecca
+    if (!priv->prDaemonRunning &&
edecca
+        virDomainDefHasManagedPR(vm->def))
edecca
+        qemuProcessStartManagedPRDaemon(vm);
edecca
+}
edecca
+
edecca
+
edecca
 static void qemuProcessEventHandler(void *data, void *opaque)
edecca
 {
edecca
     struct qemuProcessEvent *processEvent = data;
edecca
@@ -4829,6 +4843,9 @@ static void qemuProcessEventHandler(void *data, void *opaque)
edecca
     case QEMU_PROCESS_EVENT_MONITOR_EOF:
edecca
         processMonitorEOFEvent(driver, vm);
edecca
         break;
edecca
+    case QEMU_PROCESS_EVENT_PR_DISCONNECT:
edecca
+        processPRDisconnectEvent(vm);
edecca
+        break;
edecca
     case QEMU_PROCESS_EVENT_LAST:
edecca
         break;
edecca
     }
edecca
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
edecca
index ae5b23b9fc..86b2b6e985 100644
edecca
--- a/src/qemu/qemu_monitor.c
edecca
+++ b/src/qemu/qemu_monitor.c
edecca
@@ -1669,6 +1669,21 @@ qemuMonitorEmitDumpCompleted(qemuMonitorPtr mon,
edecca
 }
edecca
 
edecca
 
edecca
+int
edecca
+qemuMonitorEmitPRManagerStatusChanged(qemuMonitorPtr mon,
edecca
+                                      const char *prManager,
edecca
+                                      bool connected)
edecca
+{
edecca
+    int ret = -1;
edecca
+    VIR_DEBUG("mon=%p, prManager='%s', connected=%d", mon, prManager, connected);
edecca
+
edecca
+    QEMU_MONITOR_CALLBACK(mon, ret, domainPRManagerStatusChanged,
edecca
+                          mon->vm, prManager, connected);
edecca
+
edecca
+    return ret;
edecca
+}
edecca
+
edecca
+
edecca
 int
edecca
 qemuMonitorSetCapabilities(qemuMonitorPtr mon)
edecca
 {
edecca
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
edecca
index e8adda8aa0..a906bc8410 100644
edecca
--- a/src/qemu/qemu_monitor.h
edecca
+++ b/src/qemu/qemu_monitor.h
edecca
@@ -273,6 +273,12 @@ typedef int (*qemuMonitorDomainDumpCompletedCallback)(qemuMonitorPtr mon,
edecca
                                                       const char *error,
edecca
                                                       void *opaque);
edecca
 
edecca
+typedef int (*qemuMonitorDomainPRManagerStatusChangedCallback)(qemuMonitorPtr mon,
edecca
+                                                               virDomainObjPtr vm,
edecca
+                                                               const char *prManager,
edecca
+                                                               bool connected,
edecca
+                                                               void *opaque);
edecca
+
edecca
 typedef struct _qemuMonitorCallbacks qemuMonitorCallbacks;
edecca
 typedef qemuMonitorCallbacks *qemuMonitorCallbacksPtr;
edecca
 struct _qemuMonitorCallbacks {
edecca
@@ -305,6 +311,7 @@ struct _qemuMonitorCallbacks {
edecca
     qemuMonitorDomainAcpiOstInfoCallback domainAcpiOstInfo;
edecca
     qemuMonitorDomainBlockThresholdCallback domainBlockThreshold;
edecca
     qemuMonitorDomainDumpCompletedCallback domainDumpCompleted;
edecca
+    qemuMonitorDomainPRManagerStatusChangedCallback domainPRManagerStatusChanged;
edecca
 };
edecca
 
edecca
 char *qemuMonitorEscapeArg(const char *in);
edecca
@@ -433,6 +440,10 @@ int qemuMonitorEmitDumpCompleted(qemuMonitorPtr mon,
edecca
                                  qemuMonitorDumpStatsPtr stats,
edecca
                                  const char *error);
edecca
 
edecca
+int qemuMonitorEmitPRManagerStatusChanged(qemuMonitorPtr mon,
edecca
+                                          const char *prManager,
edecca
+                                          bool connected);
edecca
+
edecca
 int qemuMonitorStartCPUs(qemuMonitorPtr mon);
edecca
 int qemuMonitorStopCPUs(qemuMonitorPtr mon);
edecca
 
edecca
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
edecca
index 3e90279b71..03c94cd88b 100644
edecca
--- a/src/qemu/qemu_monitor_json.c
edecca
+++ b/src/qemu/qemu_monitor_json.c
edecca
@@ -91,6 +91,7 @@ static void qemuMonitorJSONHandleMigrationPass(qemuMonitorPtr mon, virJSONValueP
edecca
 static void qemuMonitorJSONHandleAcpiOstInfo(qemuMonitorPtr mon, virJSONValuePtr data);
edecca
 static void qemuMonitorJSONHandleBlockThreshold(qemuMonitorPtr mon, virJSONValuePtr data);
edecca
 static void qemuMonitorJSONHandleDumpCompleted(qemuMonitorPtr mon, virJSONValuePtr data);
edecca
+static void qemuMonitorJSONHandlePRManagerStatusChanged(qemuMonitorPtr mon, virJSONValuePtr data);
edecca
 
edecca
 typedef struct {
edecca
     const char *type;
edecca
@@ -113,6 +114,7 @@ static qemuEventHandler eventHandlers[] = {
edecca
     { "MIGRATION_PASS", qemuMonitorJSONHandleMigrationPass, },
edecca
     { "NIC_RX_FILTER_CHANGED", qemuMonitorJSONHandleNicRxFilterChanged, },
edecca
     { "POWERDOWN", qemuMonitorJSONHandlePowerdown, },
edecca
+    { "PR_MANAGER_STATUS_CHANGED", qemuMonitorJSONHandlePRManagerStatusChanged, },
edecca
     { "RESET", qemuMonitorJSONHandleReset, },
edecca
     { "RESUME", qemuMonitorJSONHandleResume, },
edecca
     { "RTC_CHANGE", qemuMonitorJSONHandleRTCChange, },
edecca
@@ -1297,6 +1299,27 @@ qemuMonitorJSONHandleDumpCompleted(qemuMonitorPtr mon,
edecca
 }
edecca
 
edecca
 
edecca
+static void qemuMonitorJSONHandlePRManagerStatusChanged(qemuMonitorPtr mon,
edecca
+                                                        virJSONValuePtr data)
edecca
+{
edecca
+    const char *name;
edecca
+    bool connected;
edecca
+
edecca
+    if (!(name = virJSONValueObjectGetString(data, "id"))) {
edecca
+        VIR_WARN("missing pr-manager alias in PR_MANAGER_STATUS_CHANGED event");
edecca
+        return;
edecca
+    }
edecca
+
edecca
+    if (virJSONValueObjectGetBoolean(data, "connected", &connected) < 0) {
edecca
+        VIR_WARN("missing connected state for %s "
edecca
+                 "in PR_MANAGER_STATUS_CHANGED event", name);
edecca
+        return;
edecca
+    }
edecca
+
edecca
+    qemuMonitorEmitPRManagerStatusChanged(mon, name, connected);
edecca
+}
edecca
+
edecca
+
edecca
 int
edecca
 qemuMonitorJSONHumanCommandWithFd(qemuMonitorPtr mon,
edecca
                                   const char *cmd_str,
edecca
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
edecca
index f200729cb1..fbc8529f3b 100644
edecca
--- a/src/qemu/qemu_process.c
edecca
+++ b/src/qemu/qemu_process.c
edecca
@@ -1615,6 +1615,60 @@ qemuProcessHandleDumpCompleted(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
edecca
 }
edecca
 
edecca
 
edecca
+static int
edecca
+qemuProcessHandlePRManagerStatusChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
edecca
+                                        virDomainObjPtr vm,
edecca
+                                        const char *prManager,
edecca
+                                        bool connected,
edecca
+                                        void *opaque)
edecca
+{
edecca
+    virQEMUDriverPtr driver = opaque;
edecca
+    qemuDomainObjPrivatePtr priv;
edecca
+    struct qemuProcessEvent *processEvent = NULL;
edecca
+    const char *managedAlias = qemuDomainGetManagedPRAlias();
edecca
+    int ret = -1;
edecca
+
edecca
+    virObjectLock(vm);
edecca
+
edecca
+    VIR_DEBUG("pr-manager %s status changed for domain %p %s connected=%d",
edecca
+              prManager, vm, vm->def->name, connected);
edecca
+
edecca
+    if (connected) {
edecca
+        /* Connect events are boring. */
edecca
+        ret = 0;
edecca
+        goto cleanup;
edecca
+    }
edecca
+    /* Disconnect events are more interesting. */
edecca
+
edecca
+    if (STRNEQ(prManager, managedAlias)) {
edecca
+        VIR_DEBUG("pr-manager %s not managed, ignoring event",
edecca
+                  prManager);
edecca
+        ret = 0;
edecca
+        goto cleanup;
edecca
+    }
edecca
+
edecca
+    priv = vm->privateData;
edecca
+    priv->prDaemonRunning = false;
edecca
+
edecca
+    if (VIR_ALLOC(processEvent) < 0)
edecca
+        goto cleanup;
edecca
+
edecca
+    processEvent->eventType = QEMU_PROCESS_EVENT_PR_DISCONNECT;
edecca
+    processEvent->vm = virObjectRef(vm);
edecca
+
edecca
+    if (virThreadPoolSendJob(driver->workerPool, 0, processEvent) < 0) {
edecca
+        qemuProcessEventFree(processEvent);
edecca
+        virObjectUnref(vm);
edecca
+        goto cleanup;
edecca
+    }
edecca
+
edecca
+    ret = 0;
edecca
+ cleanup:
edecca
+    virObjectUnlock(vm);
edecca
+    return ret;
edecca
+}
edecca
+
edecca
+
edecca
 static qemuMonitorCallbacks monitorCallbacks = {
edecca
     .eofNotify = qemuProcessHandleMonitorEOF,
edecca
     .errorNotify = qemuProcessHandleMonitorError,
edecca
@@ -1643,6 +1697,7 @@ static qemuMonitorCallbacks monitorCallbacks = {
edecca
     .domainAcpiOstInfo = qemuProcessHandleAcpiOstInfo,
edecca
     .domainBlockThreshold = qemuProcessHandleBlockThreshold,
edecca
     .domainDumpCompleted = qemuProcessHandleDumpCompleted,
edecca
+    .domainPRManagerStatusChanged = qemuProcessHandlePRManagerStatusChanged,
edecca
 };
edecca
 
edecca
 static void
edecca
-- 
edecca
2.18.0
edecca