76daa3
From 2b77727b356747ab6b91af0400f4a6f1a7d36a40 Mon Sep 17 00:00:00 2001
76daa3
From: Eric Blake <eblake@redhat.com>
76daa3
Date: Thu, 1 Jun 2017 16:58:18 +0200
76daa3
Subject: [PATCH 02/17] shutdown: Prepare for use of an enum in
76daa3
 reset/shutdown_request
76daa3
76daa3
RH-Author: Eric Blake <eblake@redhat.com>
76daa3
Message-id: <20170601165821.26810-3-eblake@redhat.com>
76daa3
Patchwork-id: 75465
76daa3
O-Subject: [RHEL-7.4 qemu-kvm-rhev PATCH v2 2/5] shutdown: Prepare for use of an enum in reset/shutdown_request
76daa3
Bugzilla: 1418927
76daa3
RH-Acked-by: John Snow <jsnow@redhat.com>
76daa3
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
76daa3
RH-Acked-by: Thomas Huth <thuth@redhat.com>
76daa3
76daa3
We want to track why a guest was shutdown; in particular, being able
76daa3
to tell the difference between a guest request (such as ACPI request)
76daa3
and host request (such as SIGINT) will prove useful to libvirt.
76daa3
Since all requests eventually end up changing shutdown_requested in
76daa3
vl.c, the logical change is to make that value track the reason,
76daa3
rather than its current 0/1 contents.
76daa3
76daa3
Since command-line options control whether a reset request is turned
76daa3
into a shutdown request instead, the same treatment is given to
76daa3
reset_requested.
76daa3
76daa3
This patch adds an internal enum ShutdownCause that describes reasons
76daa3
that a shutdown can be requested, and changes qemu_system_reset() to
76daa3
pass the reason through, although for now nothing is actually changed
76daa3
with regards to what gets reported.  The enum could be exported via
76daa3
QAPI at a later date, if deemed necessary, but for now, there has not
76daa3
been a request to expose that much detail to end clients.
76daa3
76daa3
For the most part, we turn 0 into SHUTDOWN_CAUSE_NONE, and 1 into
76daa3
SHUTDOWN_CAUSE_HOST_ERROR; the only specific case where we have enough
76daa3
information right now to use a different value is when we are reacting
76daa3
to a host signal.  It will take a further patch to edit all call-sites
76daa3
that can trigger a reset or shutdown request to properly pass in any
76daa3
other reasons; this patch includes TODOs to point such places out.
76daa3
76daa3
qemu_system_reset() trades its 'bool report' parameter for a
76daa3
'ShutdownCause reason', with all non-zero values having the same
76daa3
effect; this lets us get rid of the weird #defines for VMRESET_*
76daa3
as synonyms for bools.
76daa3
76daa3
Signed-off-by: Eric Blake <eblake@redhat.com>
76daa3
Message-Id: <20170515214114.15442-3-eblake@redhat.com>
76daa3
Reviewed-by: Markus Armbruster <armbru@redhat.com>
76daa3
Signed-off-by: Markus Armbruster <armbru@redhat.com>
76daa3
(cherry picked from commit aedbe19297907143f17b733a7ff0e0534377bed1)
76daa3
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
76daa3
---
76daa3
 include/sysemu/sysemu.h | 23 ++++++++++++++++-----
76daa3
 migration/colo.c        |  2 +-
76daa3
 migration/savevm.c      |  2 +-
76daa3
 vl.c                    | 53 ++++++++++++++++++++++++++++++-------------------
76daa3
 xen-hvm.c               |  7 +++++--
76daa3
 5 files changed, 58 insertions(+), 29 deletions(-)
76daa3
76daa3
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
76daa3
index 1fd51a8..f942562 100644
76daa3
--- a/include/sysemu/sysemu.h
76daa3
+++ b/include/sysemu/sysemu.h
76daa3
@@ -33,8 +33,21 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
76daa3
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e);
76daa3
 void vm_state_notify(int running, RunState state);
76daa3
 
76daa3
-#define VMRESET_SILENT   false
76daa3
-#define VMRESET_REPORT   true
76daa3
+/* Enumeration of various causes for shutdown. */
76daa3
+typedef enum ShutdownCause {
76daa3
+    SHUTDOWN_CAUSE_NONE,          /* No shutdown request pending */
76daa3
+    SHUTDOWN_CAUSE_HOST_ERROR,    /* An error prevents further use of guest */
76daa3
+    SHUTDOWN_CAUSE_HOST_QMP,      /* Reaction to a QMP command, like 'quit' */
76daa3
+    SHUTDOWN_CAUSE_HOST_SIGNAL,   /* Reaction to a signal, such as SIGINT */
76daa3
+    SHUTDOWN_CAUSE_HOST_UI,       /* Reaction to UI event, like window close */
76daa3
+    SHUTDOWN_CAUSE_GUEST_SHUTDOWN,/* Guest shutdown/suspend request, via
76daa3
+                                     ACPI or other hardware-specific means */
76daa3
+    SHUTDOWN_CAUSE_GUEST_RESET,   /* Guest reset request, and command line
76daa3
+                                     turns that into a shutdown */
76daa3
+    SHUTDOWN_CAUSE_GUEST_PANIC,   /* Guest panicked, and command line turns
76daa3
+                                     that into a shutdown */
76daa3
+    SHUTDOWN_CAUSE__MAX,
76daa3
+} ShutdownCause;
76daa3
 
76daa3
 void vm_start(void);
76daa3
 int vm_prepare_start(void);
76daa3
@@ -62,10 +75,10 @@ void qemu_system_debug_request(void);
76daa3
 void qemu_system_vmstop_request(RunState reason);
76daa3
 void qemu_system_vmstop_request_prepare(void);
76daa3
 bool qemu_vmstop_requested(RunState *r);
76daa3
-int qemu_shutdown_requested_get(void);
76daa3
-int qemu_reset_requested_get(void);
76daa3
+ShutdownCause qemu_shutdown_requested_get(void);
76daa3
+ShutdownCause qemu_reset_requested_get(void);
76daa3
 void qemu_system_killed(int signal, pid_t pid);
76daa3
-void qemu_system_reset(bool report);
76daa3
+void qemu_system_reset(ShutdownCause reason);
76daa3
 void qemu_system_guest_panicked(GuestPanicInformation *info);
76daa3
 size_t qemu_target_page_bits(void);
76daa3
 
76daa3
diff --git a/migration/colo.c b/migration/colo.c
76daa3
index 963c802..12d355a 100644
76daa3
--- a/migration/colo.c
76daa3
+++ b/migration/colo.c
76daa3
@@ -623,7 +623,7 @@ void *colo_process_incoming_thread(void *opaque)
76daa3
         }
76daa3
 
76daa3
         qemu_mutex_lock_iothread();
76daa3
-        qemu_system_reset(VMRESET_SILENT);
76daa3
+        qemu_system_reset(SHUTDOWN_CAUSE_NONE);
76daa3
         vmstate_loading = true;
76daa3
         if (qemu_loadvm_state(fb) < 0) {
76daa3
             error_report("COLO: loadvm failed");
76daa3
diff --git a/migration/savevm.c b/migration/savevm.c
76daa3
index 6e2f9e7..483c05f 100644
76daa3
--- a/migration/savevm.c
76daa3
+++ b/migration/savevm.c
76daa3
@@ -2368,7 +2368,7 @@ int load_vmstate(const char *name)
76daa3
         return -EINVAL;
76daa3
     }
76daa3
 
76daa3
-    qemu_system_reset(VMRESET_SILENT);
76daa3
+    qemu_system_reset(SHUTDOWN_CAUSE_NONE);
76daa3
     mis->from_src_file = f;
76daa3
 
76daa3
     aio_context_acquire(aio_context);
76daa3
diff --git a/vl.c b/vl.c
76daa3
index f5dea2c..ea181c6 100644
76daa3
--- a/vl.c
76daa3
+++ b/vl.c
76daa3
@@ -1596,8 +1596,9 @@ void vm_state_notify(int running, RunState state)
76daa3
     }
76daa3
 }
76daa3
 
76daa3
-static int reset_requested;
76daa3
-static int shutdown_requested, shutdown_signal;
76daa3
+static ShutdownCause reset_requested;
76daa3
+static ShutdownCause shutdown_requested;
76daa3
+static int shutdown_signal;
76daa3
 static pid_t shutdown_pid;
76daa3
 static int powerdown_requested;
76daa3
 static int debug_requested;
76daa3
@@ -1611,19 +1612,19 @@ static NotifierList wakeup_notifiers =
76daa3
     NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
76daa3
 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
76daa3
 
76daa3
-int qemu_shutdown_requested_get(void)
76daa3
+ShutdownCause qemu_shutdown_requested_get(void)
76daa3
 {
76daa3
     return shutdown_requested;
76daa3
 }
76daa3
 
76daa3
-int qemu_reset_requested_get(void)
76daa3
+ShutdownCause qemu_reset_requested_get(void)
76daa3
 {
76daa3
     return reset_requested;
76daa3
 }
76daa3
 
76daa3
 static int qemu_shutdown_requested(void)
76daa3
 {
76daa3
-    return atomic_xchg(&shutdown_requested, 0);
76daa3
+    return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
76daa3
 }
76daa3
 
76daa3
 static void qemu_kill_report(void)
76daa3
@@ -1646,14 +1647,15 @@ static void qemu_kill_report(void)
76daa3
     }
76daa3
 }
76daa3
 
76daa3
-static int qemu_reset_requested(void)
76daa3
+static ShutdownCause qemu_reset_requested(void)
76daa3
 {
76daa3
-    int r = reset_requested;
76daa3
+    ShutdownCause r = reset_requested;
76daa3
+
76daa3
     if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
76daa3
-        reset_requested = 0;
76daa3
+        reset_requested = SHUTDOWN_CAUSE_NONE;
76daa3
         return r;
76daa3
     }
76daa3
-    return false;
76daa3
+    return SHUTDOWN_CAUSE_NONE;
76daa3
 }
76daa3
 
76daa3
 static int qemu_suspend_requested(void)
76daa3
@@ -1685,7 +1687,10 @@ static int qemu_debug_requested(void)
76daa3
     return r;
76daa3
 }
76daa3
 
76daa3
-void qemu_system_reset(bool report)
76daa3
+/*
76daa3
+ * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
76daa3
+ */
76daa3
+void qemu_system_reset(ShutdownCause reason)
76daa3
 {
76daa3
     MachineClass *mc;
76daa3
 
76daa3
@@ -1698,7 +1703,8 @@ void qemu_system_reset(bool report)
76daa3
     } else {
76daa3
         qemu_devices_reset();
76daa3
     }
76daa3
-    if (report) {
76daa3
+    if (reason) {
76daa3
+        /* TODO update event based on reason */
76daa3
         qapi_event_send_reset(&error_abort);
76daa3
     }
76daa3
     cpu_synchronize_all_post_reset();
76daa3
@@ -1737,9 +1743,10 @@ void qemu_system_guest_panicked(GuestPanicInformation *info)
76daa3
 void qemu_system_reset_request(void)
76daa3
 {
76daa3
     if (no_reboot) {
76daa3
-        shutdown_requested = 1;
76daa3
+        /* TODO - add a parameter to allow callers to specify reason */
76daa3
+        shutdown_requested = SHUTDOWN_CAUSE_HOST_ERROR;
76daa3
     } else {
76daa3
-        reset_requested = 1;
76daa3
+        reset_requested = SHUTDOWN_CAUSE_HOST_ERROR;
76daa3
     }
76daa3
     cpu_stop_current();
76daa3
     qemu_notify_event();
76daa3
@@ -1806,7 +1813,7 @@ void qemu_system_killed(int signal, pid_t pid)
76daa3
     /* Cannot call qemu_system_shutdown_request directly because
76daa3
      * we are in a signal handler.
76daa3
      */
76daa3
-    shutdown_requested = 1;
76daa3
+    shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
76daa3
     qemu_notify_event();
76daa3
 }
76daa3
 
76daa3
@@ -1814,7 +1821,8 @@ void qemu_system_shutdown_request(void)
76daa3
 {
76daa3
     trace_qemu_system_shutdown_request();
76daa3
     replay_shutdown_request();
76daa3
-    shutdown_requested = 1;
76daa3
+    /* TODO - add a parameter to allow callers to specify reason */
76daa3
+    shutdown_requested = SHUTDOWN_CAUSE_HOST_ERROR;
76daa3
     qemu_notify_event();
76daa3
 }
76daa3
 
76daa3
@@ -1845,14 +1853,18 @@ void qemu_system_debug_request(void)
76daa3
 static bool main_loop_should_exit(void)
76daa3
 {
76daa3
     RunState r;
76daa3
+    ShutdownCause request;
76daa3
+
76daa3
     if (qemu_debug_requested()) {
76daa3
         vm_stop(RUN_STATE_DEBUG);
76daa3
     }
76daa3
     if (qemu_suspend_requested()) {
76daa3
         qemu_system_suspend();
76daa3
     }
76daa3
-    if (qemu_shutdown_requested()) {
76daa3
+    request = qemu_shutdown_requested();
76daa3
+    if (request) {
76daa3
         qemu_kill_report();
76daa3
+        /* TODO update event based on request */
76daa3
         qapi_event_send_shutdown(&error_abort);
76daa3
         if (no_shutdown) {
76daa3
             vm_stop(RUN_STATE_SHUTDOWN);
76daa3
@@ -1860,9 +1872,10 @@ static bool main_loop_should_exit(void)
76daa3
             return true;
76daa3
         }
76daa3
     }
76daa3
-    if (qemu_reset_requested()) {
76daa3
+    request = qemu_reset_requested();
76daa3
+    if (request) {
76daa3
         pause_all_vcpus();
76daa3
-        qemu_system_reset(VMRESET_REPORT);
76daa3
+        qemu_system_reset(request);
76daa3
         resume_all_vcpus();
76daa3
         if (!runstate_check(RUN_STATE_RUNNING) &&
76daa3
                 !runstate_check(RUN_STATE_INMIGRATE)) {
76daa3
@@ -1871,7 +1884,7 @@ static bool main_loop_should_exit(void)
76daa3
     }
76daa3
     if (qemu_wakeup_requested()) {
76daa3
         pause_all_vcpus();
76daa3
-        qemu_system_reset(VMRESET_SILENT);
76daa3
+        qemu_system_reset(SHUTDOWN_CAUSE_NONE);
76daa3
         notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
76daa3
         wakeup_reason = QEMU_WAKEUP_REASON_NONE;
76daa3
         resume_all_vcpus();
76daa3
@@ -4689,7 +4702,7 @@ int main(int argc, char **argv, char **envp)
76daa3
        reading from the other reads, because timer polling functions query
76daa3
        clock values from the log. */
76daa3
     replay_checkpoint(CHECKPOINT_RESET);
76daa3
-    qemu_system_reset(VMRESET_SILENT);
76daa3
+    qemu_system_reset(SHUTDOWN_CAUSE_NONE);
76daa3
     register_global_state();
76daa3
     if (replay_mode != REPLAY_MODE_NONE) {
76daa3
         replay_vmstate_init();
76daa3
diff --git a/xen-hvm.c b/xen-hvm.c
76daa3
index 5043beb..858e934 100644
76daa3
--- a/xen-hvm.c
76daa3
+++ b/xen-hvm.c
76daa3
@@ -1090,11 +1090,14 @@ static void cpu_handle_ioreq(void *opaque)
76daa3
          * causes Xen to powerdown the domain.
76daa3
          */
76daa3
         if (runstate_is_running()) {
76daa3
+            ShutdownCause request;
76daa3
+
76daa3
             if (qemu_shutdown_requested_get()) {
76daa3
                 destroy_hvm_domain(false);
76daa3
             }
76daa3
-            if (qemu_reset_requested_get()) {
76daa3
-                qemu_system_reset(VMRESET_REPORT);
76daa3
+            request = qemu_reset_requested_get();
76daa3
+            if (request) {
76daa3
+                qemu_system_reset(request);
76daa3
                 destroy_hvm_domain(true);
76daa3
             }
76daa3
         }
76daa3
-- 
76daa3
1.8.3.1
76daa3