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