Skip to contentSkip to conversation

libxl: Make domain_shutdown fail if graceful not possible

PATCH In review Ian Jackson 3 participants 7 messages Jan 19, 2011 – Jan 20, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: Make domain_shutdown fail if graceful not possible

Currently "xl shutdown" (like "xm shutdown") is not capable of doing the proper ACPI negotiation with an HVM no-pv-drivers guest which would be necessary for a graceful shutdown.

Instead (following the ill-advised lead of "xm shutdown") it simply shoots the guest in the head.

This patch changes the behaviour so that "xl shutdown" fails if the domain cannot be shut down gracefully for this reason and suggests in the error message using destroy instead.

tools/libxl/libxl.c +5 -15
index 7ffb985..2cc2d21 100644
@@ -510,29 +510,19 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
 
     xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
     if (libxl__domain_is_hvm(ctx,domid)) {
-        unsigned long acpi_s_state = 0;
         unsigned long pvdriver = 0;
         int ret;
-        ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
-        if (ret<0) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting ACPI S-state");
-            libxl__free_all(&gc);
-            return ERROR_FAIL;
-        }
         ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
         if (ret<0) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
             libxl__free_all(&gc);
             return ERROR_FAIL;
         }
-        if (!pvdriver || acpi_s_state != 0) {
-            ret = xc_domain_shutdown(ctx->xch, domid, req);
-            if (ret<0) {
-                LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain");
-                libxl__free_all(&gc);
-                return ERROR_FAIL;
-            }
-       }
+        if (!pvdriver) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "HVM domain without PV drivers:"
+                       " graceful shutdown not possible, use destroy");
+            return ERROR_FAIL;
+        }
     }
     libxl__free_all(&gc);
     return 0;
#1 lore raw
Quoted 3 lines · I wrote:

Currently "xl shutdown" (like "xm shutdown") is not capable of doing the proper ACPI negotiation with an HVM no-pv-drivers guest which would be necessary for a graceful shutdown.

That patch leaks the gc. Here's a revised version.

Ian.

libxl: Make domain_shutdown fail if graceful not possible

Currently "xl shutdown" (like "xm shutdown") is not capable of doing the proper ACPI negotiation with an HVM no-pv-drivers guest which would be necessary for a graceful shutdown.

Instead (following the ill-advised lead of "xm shutdown") it simply shoots the guest in the head.

This patch changes the behaviour so that "xl shutdown" fails if the domain cannot be shut down gracefully for this reason and suggests in the error message using destroy instead.

tools/libxl/libxl.c +5 -14
index 7ffb985..00a68bb 100644
@@ -510,29 +510,20 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
 
     xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
     if (libxl__domain_is_hvm(ctx,domid)) {
-        unsigned long acpi_s_state = 0;
         unsigned long pvdriver = 0;
         int ret;
-        ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
+        ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
         if (ret<0) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting ACPI S-state");
+            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
             libxl__free_all(&gc);
             return ERROR_FAIL;
         }
-        ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
-        if (ret<0) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
+        if (!pvdriver) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "HVM domain without PV drivers:"
+                       " graceful shutdown not possible, use destroy");
             libxl__free_all(&gc);
             return ERROR_FAIL;
         }
-        if (!pvdriver || acpi_s_state != 0) {
-            ret = xc_domain_shutdown(ctx->xch, domid, req);
-            if (ret<0) {
-                LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain");
-                libxl__free_all(&gc);
-                return ERROR_FAIL;
-            }
-       }
     }
     libxl__free_all(&gc);
     return 0;
#2 lore raw
In reply to Ian Jackson
Quoted 4 lines · Ian Jackson

Currently "xl shutdown" (like "xm shutdown") is not capable of doing the proper ACPI negotiation with an HVM no-pv-drivers guest which would be necessary for a graceful shutdown.

Quoted 8 lines

Instead (following the ill-advised lead of "xm shutdown") it simply shoots the guest in the head.

This patch changes the behaviour so that "xl shutdown" fails if the domain cannot be shut down gracefully for this reason and suggests in the error message using destroy instead.

I strongly agree with the principal that graceful commands shouldn't fallback automatically to a non-graceful variant!

Acked/Reviewed-by: Ian Campbell <[email protected]>

Perhaps the check should be before the xs_write though -- i.e. check for pvdriver support before attempting to use it?

Ian.

Quoted 45 lines
tools/libxl/libxl.c +5 -15
index 7ffb985..2cc2d21 100644
@@ -510,29 +510,19 @@ int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
 
     xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
     if (libxl__domain_is_hvm(ctx,domid)) {
-        unsigned long acpi_s_state = 0;
         unsigned long pvdriver = 0;
         int ret;
-        ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
-        if (ret<0) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting ACPI S-state");
-            libxl__free_all(&gc);
-            return ERROR_FAIL;
-        }
         ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
         if (ret<0) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
             libxl__free_all(&gc);
             return ERROR_FAIL;
         }
-        if (!pvdriver || acpi_s_state != 0) {
-            ret = xc_domain_shutdown(ctx->xch, domid, req);
-            if (ret<0) {
-                LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain");
-                libxl__free_all(&gc);
-                return ERROR_FAIL;
-            }
-       }
+        if (!pvdriver) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "HVM domain without PV drivers:"
+                       " graceful shutdown not possible, use destroy");
+            return ERROR_FAIL;
+        }
     }
     libxl__free_all(&gc);
     return 0;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxl: Make domain_shutdown fail if graceful not possible"):

Quoted 2 lines

Perhaps the check should be before the xs_write though -- i.e. check for pvdriver support before attempting to use it?

A sensible suggestion.

Ian.

libxl: Make domain_shutdown fail if graceful not possible

Currently "xl shutdown" (like "xm shutdown") is not capable of doing the proper ACPI negotiation with an HVM no-pv-drivers guest which would be necessary for a graceful shutdown.

Instead (following the ill-advised lead of "xm shutdown") it simply shoots the guest in the head.

This patch changes the behaviour so that "xl shutdown" fails if the domain cannot be shut down gracefully for this reason and suggests in the error message using destroy instead.

Also, check whether the PV shutdown protocol is available before we try to use it.

tools/libxl/libxl.c +10 -18
diff -r 051a1b1b8f8a tools/libxl/libxl.c
@@ -506,34 +506,26 @@ int libxl_domain_shutdown(libxl_ctx *ctx
         return ERROR_FAIL;
     }
 
-    shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
-
-    xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
     if (libxl__domain_is_hvm(ctx,domid)) {
-        unsigned long acpi_s_state = 0;
         unsigned long pvdriver = 0;
         int ret;
-        ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
-        if (ret<0) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting ACPI S-state");
-            libxl__free_all(&gc);
-            return ERROR_FAIL;
-        }
         ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
         if (ret<0) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
             libxl__free_all(&gc);
             return ERROR_FAIL;
         }
-        if (!pvdriver || acpi_s_state != 0) {
-            ret = xc_domain_shutdown(ctx->xch, domid, req);
-            if (ret<0) {
-                LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain");
-                libxl__free_all(&gc);
-                return ERROR_FAIL;
-            }
-       }
+        if (!pvdriver) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "HVM domain without PV drivers:"
+                       " graceful shutdown not possible, use destroy");
+            libxl__free_all(&gc);
+            return ERROR_FAIL;
+        }
     }
+
+    shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
+    xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
+
     libxl__free_all(&gc);
     return 0;
 }
#4 lore raw
Quoted 17 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxl: Make domain_shutdown fail if graceful not possible"):

Quoted 2 lines

Perhaps the check should be before the xs_write though -- i.e. check for pvdriver support before attempting to use it?

A sensible suggestion.

Ian.

libxl: Make domain_shutdown fail if graceful not possible

Currently "xl shutdown" (like "xm shutdown") is not capable of doing the proper ACPI negotiation with an HVM no-pv-drivers guest which would be necessary for a graceful shutdown.

Instead (following the ill-advised lead of "xm shutdown") it simply shoots the guest in the head.

Quoted 8 lines

This patch changes the behaviour so that "xl shutdown" fails if the domain cannot be shut down gracefully for this reason and suggests in the error message using destroy instead.

Also, check whether the PV shutdown protocol is available before we try to use it.

Acked/Reviewd-by: Ian Campbell <[email protected]>

Not related to this patch, other than I happened to notice it while reviewingm but the "req" paramter to libxl_domain_shutdown is pretty nasty. It can apparently taken a number from 0-4 inclusive, with no symbolic names, but which are translated per:

static char *req_table[] = {
    [0] = "poweroff",
    [1] = "reboot",
    [2] = "suspend",
    [3] = "crash",
    [4] = "halt",
};

However only 0 and 1 are ever actually passed by xl and I'd wager that only 0, 1 and 4 are even vaguely valid in this context. suspend has it's own libxl function and iirc crash is something only a guest does...

I'm not sure what the distinction between poweroff and halt is (pvops Linux apparently treats them the same) but I'd suggest that having libxl_domain_shutdown => "poweroff" and libxl_domain_reboot => "reboot" with the existing function becoming a common internal backend makes sense.

Ian.

Quoted 49 lines

diff -r 051a1b1b8f8a tools/libxl/libxl.c

tools/libxl/libxl.c +10 -18
@@ -506,34 +506,26 @@ int libxl_domain_shutdown(libxl_ctx *ctx
         return ERROR_FAIL;
     }
 
-    shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
-
-    xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
     if (libxl__domain_is_hvm(ctx,domid)) {
-        unsigned long acpi_s_state = 0;
         unsigned long pvdriver = 0;
         int ret;
-        ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
-        if (ret<0) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting ACPI S-state");
-            libxl__free_all(&gc);
-            return ERROR_FAIL;
-        }
         ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
         if (ret<0) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
             libxl__free_all(&gc);
             return ERROR_FAIL;
         }
-        if (!pvdriver || acpi_s_state != 0) {
-            ret = xc_domain_shutdown(ctx->xch, domid, req);
-            if (ret<0) {
-                LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain");
-                libxl__free_all(&gc);
-                return ERROR_FAIL;
-            }
-       }
+        if (!pvdriver) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "HVM domain without PV drivers:"
+                       " graceful shutdown not possible, use destroy");
+            libxl__free_all(&gc);
+            return ERROR_FAIL;
+        }
     }
+
+    shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
+    xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
+
     libxl__free_all(&gc);
     return 0;
 }
#5 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxl: Make domain_shutdown fail if graceful not possible"):

Quoted 1 line

Acked/Reviewd-by: Ian Campbell <[email protected]>

Thanks, I've applied it.

Quoted 5 lines

Not related to this patch, other than I happened to notice it while reviewingm but the "req" paramter to libxl_domain_shutdown is pretty nasty. It can apparently taken a number from 0-4 inclusive, with no symbolic names, but which are translated per:

Quoted 7 lines
static char *req_table[] = {
    [0] = "poweroff",
    [1] = "reboot",
    [2] = "suspend",
    [3] = "crash",
    [4] = "halt",
};

Yuk.

Quoted 3 lines

However only 0 and 1 are ever actually passed by xl and I'd wager that only 0, 1 and 4 are even vaguely valid in this context. suspend has it's own libxl function and iirc crash is something only a guest does...

Yes.

Quoted 5 lines

I'm not sure what the distinction between poweroff and halt is (pvops Linux apparently treats them the same) but I'd suggest that having libxl_domain_shutdown => "poweroff" and libxl_domain_reboot => "reboot" with the existing function becoming a common internal backend makes sense.

Yes, but for 4.2 I think.

Ian.

#6 lore raw
Quoted 7 lines · Ian Jackson
Quoted 5 lines

I'm not sure what the distinction between poweroff and halt is (pvops Linux apparently treats them the same) but I'd suggest that having libxl_domain_shutdown => "poweroff" and libxl_domain_reboot => "reboot" with the existing function becoming a common internal backend makes sense.

Yes, but for 4.2 I think.

ACK.

Ian.