All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v8 00/15] execute hotplug scripts from libxl
@ 2012-07-04 11:59 Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 01/15] libxl: change ao_device_remove to ao_device Roger Pau Monne
                   ` (15 more replies)
  0 siblings, 16 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel

This series have been splitted in several patches, to make them easier
to review. Also the amount of changes introduced is quite important,
since apart from all the hotplug necessary functions and
modifications, libxl_domain_destroy has been converted to an async op.
This was necessary in order to have async operations during device
removal.

Also, as an important change, disk and nics are added at different
points for HVM and device model based guests, since we need the disk
in order to start Qemu, but the nic hotplug scripts should be called
at a later point, when Qemu has created the corresponding tap device.

This new serie (v8) fixes a code refactoring problem that was present 
in v7 (06/15 changed code introduced by 05/15).

 [PATCH v8 01/15] libxl: change ao_device_remove to ao_device
*[PATCH v8 02/15] libxl: move device model creation prototypes
*[PATCH v8 03/15] libxl: convert libxl_domain_destroy to an async op
*[PATCH v8 04/15] libxl: move bootloader data strucutres and
 [PATCH v8 05/15] libxl: refactor disk addition to take a helper
 [PATCH v8 06/15] libxl: convert libxl__device_disk_local_attach to
 [PATCH v8 07/15] libxl: rename vifs to nics
 [PATCH v8 08/15] libxl: convert libxl_device_disk_add to an async op
*[PATCH v8 09/15] libxl: convert libxl_device_nic_add to an async
*[PATCH v8 10/15] libxl: add option to choose who executes hotplug
*[PATCH v8 11/15] libxl: rename _IOEMU nic type to VIF_IOEMU
 [PATCH v8 12/15] libxl: set correct nic type depending on the guest
*[PATCH v8 13/15] libxl: use libxl__xs_path_cleanup on device_destroy
*[PATCH v8 14/15] libxl: call hotplug scripts for disk devices from
*[PATCH v8 15/15] libxl: call hotplug scripts for nic devices from

* = Acked

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v8 01/15] libxl: change ao_device_remove to ao_device
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 02/15] libxl: move device model creation prototypes Roger Pau Monne
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Introduce a new structure to track state of device backends, that will
be used in following patches on this series.

This structure if used for both device creation and device
destruction and removes libxl__ao_device_remove.

Changes since v6:

 * Use libxl__xs_transaction helpers.

Changes since v5:

 * Added a common exit point for device addition/destruction that
   removes backend and frontend entries (on destruction).

 * Posponed the introduction of the base and active fields in the
   ao_device struct.

 * Don't call libxl__ev_devstate_init, since _wait will do it.

 * Removed "action being performed" comment.

Changes sinve v4:

 * Added a more detailed comment in _prepare and _initiate header.

 * Removed an unnecessary state check from
   libxl_initiate_device_remove.

Changes since v2:

 * Remove unnecessary comments in libxl__ao_device.

 * Change libxl__device_cb to device_addrm_aocomplete.

 * Rename aorm parameter in device_addrm_aocomplete to aodev.

 * Use a macro to define {nic,disk,vkb,vfb}_{remove,destroy}
   functions.

 * Rename libxl__init_ao_device to libxl__prepare_ao_device and add a
   comment explaining why we need to set active to 1.

 * Replace al uses of aorm with aodev.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c          |  214 ++++++++++++++----------------------------
 tools/libxl/libxl.h          |   15 ++-
 tools/libxl/libxl_device.c   |  149 +++++++++++++++++++++---------
 tools/libxl/libxl_internal.h |   58 ++++++++++--
 tools/libxl/xl_cmdimpl.c     |    2 +-
 5 files changed, 239 insertions(+), 199 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 9f301a9..fd44fd7 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -1503,6 +1503,26 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
 
 /******************************************************************************/
 
+/* generic callback for devices that only need to set ao_complete */
+static void device_addrm_aocomplete(libxl__egc *egc, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+
+    if (aodev->rc) {
+        LOGE(ERROR, "unable to %s %s with id %u",
+                    aodev->action == DEVICE_CONNECT ? "add" : "remove",
+                    libxl__device_kind_to_string(aodev->dev->kind),
+                    aodev->dev->devid);
+        goto out;
+    }
+
+out:
+    libxl__ao_complete(egc, ao, aodev->rc);
+    return;
+}
+
+/******************************************************************************/
+
 int libxl__device_disk_setdefault(libxl__gc *gc, libxl_device_disk *disk)
 {
     int rc;
@@ -1680,42 +1700,6 @@ int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis
     return rc;
 }
 
-int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
-                             libxl_device_disk *disk,
-                             const libxl_asyncop_how *ao_how)
-{
-    AO_CREATE(ctx, domid, ao_how);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_disk(gc, domid, disk, &device);
-    if (rc != 0) goto out;
-
-    rc = libxl__initiate_device_remove(egc, ao, &device);
-    if (rc) goto out;
-
-    return AO_INPROGRESS;
-
-out:
-    return AO_ABORT(rc);
-}
-
-int libxl_device_disk_destroy(libxl_ctx *ctx, uint32_t domid,
-                              libxl_device_disk *disk)
-{
-    GC_INIT(ctx);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_disk(gc, domid, disk, &device);
-    if (rc != 0) goto out;
-
-    rc = libxl__device_destroy(gc, &device);
-out:
-    GC_FREE;
-    return rc;
-}
-
 static void libxl__device_disk_from_xs_be(libxl__gc *gc,
                                           const char *be_path,
                                           libxl_device_disk *disk)
@@ -2080,8 +2064,9 @@ int libxl__device_disk_local_detach(libxl__gc *gc, libxl_device_disk *disk)
             if (disk->vdev != NULL) {
                 libxl_device_disk_remove(gc->owner, LIBXL_TOOLSTACK_DOMID,
                         disk, 0);
+                /* fixme-ao */
                 rc = libxl_device_disk_destroy(gc->owner,
-                        LIBXL_TOOLSTACK_DOMID, disk);
+                        LIBXL_TOOLSTACK_DOMID, disk, 0);
             }
             break;
         default:
@@ -2249,42 +2234,6 @@ out:
     return rc;
 }
 
-int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid,
-                            libxl_device_nic *nic,
-                            const libxl_asyncop_how *ao_how)
-{
-    AO_CREATE(ctx, domid, ao_how);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_nic(gc, domid, nic, &device);
-    if (rc != 0) goto out;
-
-    rc = libxl__initiate_device_remove(egc, ao, &device);
-    if (rc) goto out;
-
-    return AO_INPROGRESS;
-
-out:
-    return AO_ABORT(rc);
-}
-
-int libxl_device_nic_destroy(libxl_ctx *ctx, uint32_t domid,
-                                  libxl_device_nic *nic)
-{
-    GC_INIT(ctx);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_nic(gc, domid, nic, &device);
-    if (rc != 0) goto out;
-
-    rc = libxl__device_destroy(gc, &device);
-out:
-    GC_FREE;
-    return rc;
-}
-
 static void libxl__device_nic_from_xs_be(libxl__gc *gc,
                                          const char *be_path,
                                          libxl_device_nic *nic)
@@ -2611,42 +2560,6 @@ out:
     return rc;
 }
 
-int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid,
-                            libxl_device_vkb *vkb,
-                            const libxl_asyncop_how *ao_how)
-{
-    AO_CREATE(ctx, domid, ao_how);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_vkb(gc, domid, vkb, &device);
-    if (rc != 0) goto out;
-
-    rc = libxl__initiate_device_remove(egc, ao, &device);
-    if (rc) goto out;
-
-    return AO_INPROGRESS;
-
-out:
-    return AO_ABORT(rc);
-}
-
-int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid,
-                                  libxl_device_vkb *vkb)
-{
-    GC_INIT(ctx);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_vkb(gc, domid, vkb, &device);
-    if (rc != 0) goto out;
-
-    rc = libxl__device_destroy(gc, &device);
-out:
-    GC_FREE;
-    return rc;
-}
-
 /******************************************************************************/
 
 int libxl__device_vfb_setdefault(libxl__gc *gc, libxl_device_vfb *vfb)
@@ -2744,41 +2657,56 @@ out:
     return rc;
 }
 
-int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid,
-                            libxl_device_vfb *vfb,
-                            const libxl_asyncop_how *ao_how)
-{
-    AO_CREATE(ctx, domid, ao_how);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_vfb(gc, domid, vfb, &device);
-    if (rc != 0) goto out;
-
-    rc = libxl__initiate_device_remove(egc, ao, &device);
-    if (rc) goto out;
-
-    return AO_INPROGRESS;
-
-out:
-    return AO_ABORT(rc);
-}
-
-int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid,
-                                  libxl_device_vfb *vfb)
-{
-    GC_INIT(ctx);
-    libxl__device device;
-    int rc;
-
-    rc = libxl__device_from_vfb(gc, domid, vfb, &device);
-    if (rc != 0) goto out;
+/******************************************************************************/
 
-    rc = libxl__device_destroy(gc, &device);
-out:
-    GC_FREE;
-    return rc;
-}
+/* Macro for defining device remove/destroy functions in a compact way */
+#define DEFINE_DEVICE_REMOVE(type, removedestroy, f)                    \
+    int libxl_device_##type##_##removedestroy(libxl_ctx *ctx,           \
+        uint32_t domid, libxl_device_##type *type,                      \
+        const libxl_asyncop_how *ao_how)                                \
+    {                                                                   \
+        AO_CREATE(ctx, domid, ao_how);                                  \
+        libxl__device *device;                                          \
+        libxl__ao_device *aodev;                                        \
+        int rc;                                                         \
+                                                                        \
+        GCNEW(device);                                                  \
+        rc = libxl__device_from_##type(gc, domid, type, device);        \
+        if (rc != 0) goto out;                                          \
+                                                                        \
+        GCNEW(aodev);                                                   \
+        libxl__prepare_ao_device(ao, aodev);                            \
+        aodev->action = DEVICE_DISCONNECT;                              \
+        aodev->dev = device;                                            \
+        aodev->callback = device_addrm_aocomplete;                      \
+        aodev->force = f;                                               \
+        libxl__initiate_device_remove(egc, aodev);                      \
+                                                                        \
+    out:                                                                \
+        if (rc) return AO_ABORT(rc);                                    \
+        return AO_INPROGRESS;                                           \
+    }
+
+/* Define all remove/destroy functions and undef the macro */
+
+/* disk */
+DEFINE_DEVICE_REMOVE(disk, remove, 0)
+DEFINE_DEVICE_REMOVE(disk, destroy, 1)
+
+/* nic */
+DEFINE_DEVICE_REMOVE(nic, remove, 0)
+DEFINE_DEVICE_REMOVE(nic, destroy, 1)
+
+/* vkb */
+DEFINE_DEVICE_REMOVE(vkb, remove, 0)
+DEFINE_DEVICE_REMOVE(vkb, destroy, 1)
+
+/* vfb */
+
+DEFINE_DEVICE_REMOVE(vfb, remove, 0)
+DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
+
+#undef DEFINE_DEVICE_REMOVE
 
 /******************************************************************************/
 
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 371b659..75175c7 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -671,7 +671,8 @@ int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
                              libxl_device_disk *disk,
                              const libxl_asyncop_how *ao_how);
 int libxl_device_disk_destroy(libxl_ctx *ctx, uint32_t domid,
-                              libxl_device_disk *disk);
+                              libxl_device_disk *disk,
+                              const libxl_asyncop_how *ao_how);
 
 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num);
 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
@@ -688,7 +689,9 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
 int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_nic *nic,
                             const libxl_asyncop_how *ao_how);
-int libxl_device_nic_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
+int libxl_device_nic_destroy(libxl_ctx *ctx, uint32_t domid,
+                             libxl_device_nic *nic,
+                             const libxl_asyncop_how *ao_how);
 
 libxl_device_nic *libxl_device_nic_list(libxl_ctx *ctx, uint32_t domid, int *num);
 int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
@@ -699,14 +702,18 @@ int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb);
 int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_vkb *vkb,
                             const libxl_asyncop_how *ao_how);
-int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb);
+int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid,
+                             libxl_device_vkb *vkb,
+                             const libxl_asyncop_how *ao_how);
 
 /* Framebuffer */
 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb);
 int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_vfb *vfb,
                             const libxl_asyncop_how *ao_how);
-int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb);
+int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid,
+                             libxl_device_vfb *vfb,
+                             const libxl_asyncop_how *ao_how);
 
 /* PCI Passthrough */
 int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev);
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 3da60e1..8316707 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -361,11 +361,13 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk,
     return -1;
 }
 
+/* Device AO operations */
 
-typedef struct {
-    libxl__ao *ao;
-    libxl__ev_devstate ds;
-} libxl__ao_device_remove;
+void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev)
+{
+    aodev->ao = ao;
+    aodev->rc = 0;
+}
 
 int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
 {
@@ -441,34 +443,41 @@ out:
 
 /* Callbacks for device related operations */
 
-static void device_remove_callback(libxl__egc *egc, libxl__ev_devstate *ds,
+/* 
+ * device_backend_callback is the main callback entry point, for both device
+ * addition and removal. It gets called if we reach the desired state
+ * (XenbusStateClosed or XenbusStateInitWait). After that, all this
+ * functions get called in the order displayed below.
+ *
+ * If new device types are added, they should only need to modify the
+ * specific hotplug scripts call, which can be found in each OS specific
+ * file. If this new devices don't need a hotplug script, no modification
+ * should be needed.
+ */
+
+static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds,
                                    int rc);
 
-static void device_remove_cleanup(libxl__gc *gc,
-                                  libxl__ao_device_remove *aorm);
+static void device_backend_cleanup(libxl__gc *gc,
+                                   libxl__ao_device *aodev);
 
-int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao,
-                                  libxl__device *dev)
+static void device_xsentries_remove(libxl__egc *egc, libxl__ao_device *aodev);
+
+void libxl__initiate_device_remove(libxl__egc *egc,
+                                   libxl__ao_device *aodev)
 {
-    AO_GC;
+    STATE_AO_GC(aodev->ao);
     libxl_ctx *ctx = libxl__gc_owner(gc);
-    xs_transaction_t t;
-    char *be_path = libxl__device_backend_path(gc, dev);
+    xs_transaction_t t = 0;
+    char *be_path = libxl__device_backend_path(gc, aodev->dev);
     char *state_path = libxl__sprintf(gc, "%s/state", be_path);
-    char *state = libxl__xs_read(gc, XBT_NULL, state_path);
     int rc = 0;
-    libxl__ao_device_remove *aorm = 0;
-
-    if (!state)
-        goto out_ok;
-    if (atoi(state) != 4) {
-        libxl__device_destroy_tapdisk(gc, be_path);
-        xs_rm(ctx->xsh, XBT_NULL, be_path);
-        goto out_ok;
-    }
 
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
+    if (aodev->force)
+        libxl__xs_path_cleanup(gc, t,
+                               libxl__device_frontend_path(gc, aodev->dev));
     xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", strlen("0"));
     xs_write(ctx->xsh, t, state_path, "5", strlen("5"));
     if (!xs_transaction_end(ctx->xsh, t, 0)) {
@@ -479,42 +488,94 @@ retry_transaction:
             goto out_fail;
         }
     }
+    /* mark transaction as ended, to prevent double closing it on out_ok */
+    t = 0;
 
     libxl__device_destroy_tapdisk(gc, be_path);
 
-    aorm = libxl__zalloc(gc, sizeof(*aorm));
-    aorm->ao = ao;
-    libxl__ev_devstate_init(&aorm->ds);
-
-    rc = libxl__ev_devstate_wait(gc, &aorm->ds, device_remove_callback,
+    rc = libxl__ev_devstate_wait(gc, &aodev->backend_ds,
+                                 device_backend_callback,
                                  state_path, XenbusStateClosed,
                                  LIBXL_DESTROY_TIMEOUT * 1000);
-    if (rc) goto out_fail;
+    if (rc) {
+        LOG(ERROR, "unable to remove device %s", be_path);
+        goto out_fail;
+    }
 
-    return 0;
+    return;
 
  out_fail:
     assert(rc);
-    device_remove_cleanup(gc, aorm);
-    return rc;
-
- out_ok:
-    libxl__ao_complete(egc, ao, 0);
-    return 0;
+    aodev->rc = rc;
+    device_xsentries_remove(egc, aodev);
+    return;
 }
 
-static void device_remove_callback(libxl__egc *egc, libxl__ev_devstate *ds,
+static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds,
                                    int rc) {
-    libxl__ao_device_remove *aorm = CONTAINER_OF(ds, *aorm, ds);
-    libxl__gc *gc = &aorm->ao->gc;
-    libxl__ao_complete(egc, aorm->ao, rc);
-    device_remove_cleanup(gc, aorm);
+    libxl__ao_device *aodev = CONTAINER_OF(ds, *aodev, backend_ds);
+    STATE_AO_GC(aodev->ao);
+
+    device_backend_cleanup(gc, aodev);
+
+    if (rc == ERROR_TIMEDOUT && aodev->action == DEVICE_DISCONNECT &&
+        !aodev->force) {
+        aodev->force = 1;
+        libxl__initiate_device_remove(egc, aodev);
+        return;
+    }
+
+    /* Some devices (vkbd) fail to disconnect properly,
+     * but we shouldn't alarm the user if it's during
+     * domain destruction.
+     */
+    if (rc && aodev->action == DEVICE_CONNECT) {
+        LOG(ERROR, "unable to connect device with path %s",
+                   libxl__device_backend_path(gc, aodev->dev));
+        goto out;
+    } else if (rc) {
+        LOG(DEBUG, "unable to disconnect device with path %s",
+                   libxl__device_backend_path(gc, aodev->dev));
+        goto out;
+    }
+
+out:
+    aodev->rc = rc;
+    device_xsentries_remove(egc, aodev);
+    return;
 }
 
-static void device_remove_cleanup(libxl__gc *gc,
-                                  libxl__ao_device_remove *aorm) {
-    if (!aorm) return;
-    libxl__ev_devstate_cancel(gc, &aorm->ds);
+static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev)
+{
+    if (!aodev) return;
+    libxl__ev_devstate_cancel(gc, &aodev->backend_ds);
+}
+
+static void device_xsentries_remove(libxl__egc *egc, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    char *be_path = libxl__device_backend_path(gc, aodev->dev);
+    char *fe_path = libxl__device_frontend_path(gc, aodev->dev);
+    xs_transaction_t t = 0;
+    int rc;
+
+    if (aodev->action == DEVICE_DISCONNECT) {
+        for (;;) {
+            rc = libxl__xs_transaction_start(gc, &t);
+            if (rc) goto out;
+
+            libxl__xs_path_cleanup(gc, t, fe_path);
+            libxl__xs_path_cleanup(gc, t, be_path);
+
+            rc = libxl__xs_transaction_commit(gc, &t);
+            if (!rc) break;
+            if (rc < 0) goto out;
+        }
+    }
+
+out:
+    aodev->callback(egc, aodev);
+    return;
 }
 
 int libxl__wait_for_device_model(libxl__gc *gc,
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 2781398..339f9a6 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -899,13 +899,6 @@ _hidden const char *libxl__device_nic_devname(libxl__gc *gc,
                                               uint32_t devid,
                                               libxl_nic_type type);
 
-/* Arranges that dev will be removed from its guest.  When
- * this is done, the ao will be completed.  An error
- * return from libxl__initiate_device_remove means that the ao
- * will _not_ be completed and the caller must do so. */
-_hidden int libxl__initiate_device_remove(libxl__egc*, libxl__ao*,
-                                          libxl__device *dev);
-
 /*
  * libxl__ev_devstate - waits a given time for a device to
  * reach a given state.  Follows the libxl_ev_* conventions.
@@ -2007,6 +2000,57 @@ _hidden void libxl__bootloader_init(libxl__bootloader_state *bl);
  * If callback is passed rc==0, will have updated st->info appropriately */
 _hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st);
 
+/*----- device addition/removal -----*/
+
+/* Action to perform (either connect or disconnect) */
+typedef enum {
+    DEVICE_CONNECT,
+    DEVICE_DISCONNECT
+} libxl__device_action;
+
+typedef struct libxl__ao_device libxl__ao_device;
+typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);
+
+/* This functions sets the necessary libxl__ao_device struct values to use
+ * safely inside functions. It marks the operation as "active"
+ * since we need to be sure that all device status structs are set
+ * to active before start queueing events, or we might call
+ * ao_complete before all devices had finished
+ *
+ * libxl__initiate_device_{remove/addition} should not be called without
+ * calling libxl__prepare_ao_device first, since it initializes the private
+ * fields of the struct libxl__ao_device to what this functions expect.
+ *
+ * Once _prepare has been called on a libxl__ao_device, it is safe to just
+ * discard this struct, there's no need to call any destroy function.
+ * _prepare can also be called multiple times with the same libxl__ao_device.
+ */
+_hidden void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev);
+
+struct libxl__ao_device {
+    /* filled in by user */
+    libxl__ao *ao;
+    libxl__device_action action;
+    libxl__device *dev;
+    int force;
+    libxl__device_callback *callback;
+    /* private for implementation */
+    int rc;
+    libxl__ev_devstate backend_ds;
+};
+
+/* Arranges that dev will be removed to the guest, and the
+ * hotplug scripts will be executed (if necessary). When
+ * this is done (or an error happens), the callback in
+ * aodev->callback will be called.
+ *
+ * The libxl__ao_device passed to this function should be
+ * prepared using libxl__prepare_ao_device prior to calling
+ * this function.
+ */
+_hidden void libxl__initiate_device_remove(libxl__egc *egc,
+                                           libxl__ao_device *aodev);
+
 /*----- Domain creation -----*/
 
 typedef struct libxl__domain_create_state libxl__domain_create_state;
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 155d183..5040210 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -5381,7 +5381,7 @@ int main_blockdetach(int argc, char **argv)
     if (libxl_device_disk_remove(ctx, domid, &disk, 0)) {
         fprintf(stderr, "libxl_device_disk_remove failed.\n");
     } else
-        libxl_device_disk_destroy(ctx, domid, &disk);
+        libxl_device_disk_destroy(ctx, domid, &disk, 0);
     return 0;
 }
 
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 02/15] libxl: move device model creation prototypes
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 01/15] libxl: change ao_device_remove to ao_device Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 03/15] libxl: convert libxl_domain_destroy to an async op Roger Pau Monne
                   ` (13 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Move prototypes regarding device model creation, since they will
depend on domain destruction in future patches.

This patch is pure code motion.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl_internal.h |   75 ++++++++++++++++++++---------------------
 1 files changed, 37 insertions(+), 38 deletions(-)

diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 339f9a6..61fd100 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1143,44 +1143,6 @@ static inline int libxl__spawn_inuse(libxl__spawn_state *ss)
 _hidden int libxl__spawn_record_pid(libxl__gc*, libxl__spawn_state*,
                                     pid_t innerchild);
 
-/*----- device model creation -----*/
-
-/* First layer; wraps libxl__spawn_spawn. */
-
-typedef struct libxl__dm_spawn_state libxl__dm_spawn_state;
-
-typedef void libxl__dm_spawn_cb(libxl__egc *egc, libxl__dm_spawn_state*,
-                                int rc /* if !0, error was logged */);
-
-struct libxl__dm_spawn_state {
-    /* mixed - spawn.ao must be initialised by user; rest is private: */
-    libxl__spawn_state spawn;
-    /* filled in by user, must remain valid: */
-    uint32_t guest_domid; /* domain being served */
-    libxl_domain_config *guest_config;
-    libxl__domain_build_state *build_state; /* relates to guest_domid */
-    libxl__dm_spawn_cb *callback;
-};
-
-_hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*);
-
-/* Stubdom device models. */
-
-typedef struct {
-    /* Mixed - user must fill in public parts EXCEPT callback,
-     * which may be undefined on entry.  (See above for details) */
-    libxl__dm_spawn_state dm; /* the stub domain device model */
-    /* filled in by user, must remain valid: */
-    libxl__dm_spawn_cb *callback; /* called as callback(,&sdss->dm,) */
-    /* private to libxl__spawn_stub_dm: */
-    libxl_domain_config dm_config;
-    libxl__domain_build_state dm_state;
-    libxl__dm_spawn_state pvqemu;
-} libxl__stub_dm_spawn_state;
-
-_hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*);
-
-
 /*
  * libxl__wait_for_offspring - Wait for child state
  * gc: allocation pool
@@ -2051,6 +2013,43 @@ struct libxl__ao_device {
 _hidden void libxl__initiate_device_remove(libxl__egc *egc,
                                            libxl__ao_device *aodev);
 
+/*----- device model creation -----*/
+
+/* First layer; wraps libxl__spawn_spawn. */
+
+typedef struct libxl__dm_spawn_state libxl__dm_spawn_state;
+
+typedef void libxl__dm_spawn_cb(libxl__egc *egc, libxl__dm_spawn_state*,
+                                int rc /* if !0, error was logged */);
+
+struct libxl__dm_spawn_state {
+    /* mixed - spawn.ao must be initialised by user; rest is private: */
+    libxl__spawn_state spawn;
+    /* filled in by user, must remain valid: */
+    uint32_t guest_domid; /* domain being served */
+    libxl_domain_config *guest_config;
+    libxl__domain_build_state *build_state; /* relates to guest_domid */
+    libxl__dm_spawn_cb *callback;
+};
+
+_hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*);
+
+/* Stubdom device models. */
+
+typedef struct {
+    /* Mixed - user must fill in public parts EXCEPT callback,
+     * which may be undefined on entry.  (See above for details) */
+    libxl__dm_spawn_state dm; /* the stub domain device model */
+    /* filled in by user, must remain valid: */
+    libxl__dm_spawn_cb *callback; /* called as callback(,&sdss->dm,) */
+    /* private to libxl__spawn_stub_dm: */
+    libxl_domain_config dm_config;
+    libxl__domain_build_state dm_state;
+    libxl__dm_spawn_state pvqemu;
+} libxl__stub_dm_spawn_state;
+
+_hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*);
+
 /*----- Domain creation -----*/
 
 typedef struct libxl__domain_create_state libxl__domain_create_state;
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 03/15] libxl: convert libxl_domain_destroy to an async op
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 01/15] libxl: change ao_device_remove to ao_device Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 02/15] libxl: move device model creation prototypes Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 04/15] libxl: move bootloader data strucutres and prototypes Roger Pau Monne
                   ` (12 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

This change introduces some new structures, and breaks the mutual
dependency that libxl_domain_destroy and libxl__destroy_device_model
had. This is done by checking if the domid passed to
libxl_domain_destroy has a stubdom, and then having the bulk of the
destroy machinery in a separate function (libxl__destroy_domid) that
doesn't check for stubdom presence, since we check for it in the upper
level function. The reason behind this change is the need to use
structures for ao operations, and it was impossible to have two
different self-referencing structs.

All uses of libxl_domain_destroy have been changed, and either
replaced by the new libxl_domain_destroy ao function or by the
internal libxl__domain_destroy that can be used inside an already
running ao.

Changes since v6:

 * Fixed typos.

Changes since v5:

 * Introduced a new struct, called libxl__ao_devices that will be used
   to simplify the addition/removal of multiple devices at the same
   time.

 * With this function we can use a generic callback for ao_device,
   libxl__ao_devices_callback, that will check if the device is the
   last one and call ao_devices->callback appropiately.

Changes since v4:

 * Fixed spelling mistakes.

 * Always use "force = 1" in device destruction in
   libxl__destroy_domid function.

 * Changed name of domain destroy callbacks to include "destroy".

 * Changed the use of rc to catch syscall errors.

 * Use libxl__remove_file instead of unlink.

 * Changed variable name of number of devices returned by
   libxl__xs_directory.

 * Simplify libxl__ao_device_check_last return.

 * Correctly propagate error returned from libxl__num_devices.

 * Add a comment about the use of libxl__device_destroy to destroy the
   console.

 * Fixed some uses of LIBXL__LOG.

Changes since v3:

 * Fixed python bindings.

Changes since v2:

 * Remove printfs.

 * Replace aorm with aodev.

 * Define an auxiliary libxl__ao_device *aodev to avoid using the long
   expression: drs->aorm[numdev]...

 * Added a common callback for both domain and stubdomain destruction
   that checks if both domains are finished and handles errors
   correctly.

 * Change libxl__ao_device_check_last logic a bit and add a comment
   describing how does it work.

 * Fixed spelling mistakes.

 * Use a do-while for xs transaction in device_remove_callback.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c               |  176 +++++++++++++++++++++++++++++++++++--
 tools/libxl/libxl.h               |    3 +-
 tools/libxl/libxl_create.c        |   29 +++++-
 tools/libxl/libxl_device.c        |  152 ++++++++++++++++++++++++++++----
 tools/libxl/libxl_dm.c            |   85 +++++++++----------
 tools/libxl/libxl_internal.h      |  116 ++++++++++++++++++++++++-
 tools/libxl/xl_cmdimpl.c          |   12 ++--
 tools/python/xen/lowlevel/xl/xl.c |    2 +-
 8 files changed, 490 insertions(+), 85 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index fd44fd7..7e6d5c9 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -1216,11 +1216,133 @@ void libxl_evdisable_disk_eject(libxl_ctx *ctx, libxl_evgen_disk_eject *evg) {
     GC_FREE;
 }    
 
-int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid)
+/* Callbacks for libxl_domain_destroy */
+
+static void domain_destroy_cb(libxl__egc *egc, libxl__domain_destroy_state *dds,
+                              int rc);
+
+int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid,
+                         const libxl_asyncop_how *ao_how)
 {
-    GC_INIT(ctx);
+    AO_CREATE(ctx, domid, ao_how);
+    libxl__domain_destroy_state *dds;
+
+    GCNEW(dds);
+    dds->ao = ao;
+    dds->domid = domid;
+    dds->callback = domain_destroy_cb;
+    libxl__domain_destroy(egc, dds);
+
+    return AO_INPROGRESS;
+}
+
+static void domain_destroy_cb(libxl__egc *egc, libxl__domain_destroy_state *dds,
+                              int rc)
+{
+    STATE_AO_GC(dds->ao);
+
+    if (rc)
+        LOG(ERROR, "destruction of domain %u failed", dds->domid);
+
+    libxl__ao_complete(egc, ao, rc);
+}
+
+/* Callbacks for libxl__domain_destroy */
+
+static void stubdom_destroy_callback(libxl__egc *egc,
+                                     libxl__destroy_domid_state *dis,
+                                     int rc);
+
+static void domain_destroy_callback(libxl__egc *egc,
+                                    libxl__destroy_domid_state *dis,
+                                    int rc);
+
+static void destroy_finish_check(libxl__egc *egc,
+                                 libxl__domain_destroy_state *dds);
+
+void libxl__domain_destroy(libxl__egc *egc, libxl__domain_destroy_state *dds)
+{
+    STATE_AO_GC(dds->ao);
+    uint32_t stubdomid = libxl_get_stubdom_id(CTX, dds->domid);
+
+    if (stubdomid) {
+        dds->stubdom.ao = ao;
+        dds->stubdom.domid = stubdomid;
+        dds->stubdom.callback = stubdom_destroy_callback;
+        libxl__destroy_domid(egc, &dds->stubdom);
+    } else {
+        dds->stubdom_finished = 1;
+    }
+
+    dds->domain.ao = ao;
+    dds->domain.domid = dds->domid;
+    dds->domain.callback = domain_destroy_callback;
+    libxl__destroy_domid(egc, &dds->domain);
+}
+
+static void stubdom_destroy_callback(libxl__egc *egc,
+                                     libxl__destroy_domid_state *dis,
+                                     int rc)
+{
+    STATE_AO_GC(dis->ao);
+    libxl__domain_destroy_state *dds = CONTAINER_OF(dis, *dds, stubdom);
+    const char *savefile;
+
+    if (rc) {
+        LOG(ERROR, "unable to destroy stubdom with domid %u", dis->domid);
+        dds->rc = rc;
+    }
+
+    dds->stubdom_finished = 1;
+    savefile = libxl__device_model_savefile(gc, dis->domid);
+    rc = libxl__remove_file(gc, savefile);
+    /*
+     * On suspend libxl__domain_save_device_model will have already
+     * unlinked the save file.
+     */
+    if (rc) {
+        LOG(ERROR, "failed to remove device-model savefile %s", savefile);
+    }
+
+    destroy_finish_check(egc, dds);
+}
+
+static void domain_destroy_callback(libxl__egc *egc,
+                                    libxl__destroy_domid_state *dis,
+                                    int rc)
+{
+    STATE_AO_GC(dis->ao);
+    libxl__domain_destroy_state *dds = CONTAINER_OF(dis, *dds, domain);
+
+    if (rc) {
+        LOG(ERROR, "unable to destroy guest with domid %u", dis->domid);
+        dds->rc = rc;
+    }
+
+    dds->domain_finished = 1;
+    destroy_finish_check(egc, dds);
+}
+
+static void destroy_finish_check(libxl__egc *egc,
+                                 libxl__domain_destroy_state *dds)
+{
+    if (!(dds->domain_finished && dds->stubdom_finished))
+        return;
+
+    dds->callback(egc, dds, dds->rc);
+}
+
+/* Callbacks for libxl__destroy_domid */
+static void devices_destroy_cb(libxl__egc *egc,
+                               libxl__devices_remove_state *drs,
+                               int rc);
+
+void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
+{
+    STATE_AO_GC(dis->ao);
+    libxl_ctx *ctx = CTX;
+    uint32_t domid = dis->domid;
     char *dom_path;
-    char *vm_path;
     char *pid;
     int rc, dm_present;
 
@@ -1231,12 +1353,15 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid)
     case ERROR_INVAL:
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "non-existant domain %d", domid);
     default:
-        return rc;
+        goto out;
     }
 
     switch (libxl__domain_type(gc, domid)) {
     case LIBXL_DOMAIN_TYPE_HVM:
-        dm_present = 1;
+        if (!libxl_get_stubdom_id(CTX, domid))
+            dm_present = 1;
+        else
+            dm_present = 0;
         break;
     case LIBXL_DOMAIN_TYPE_PV:
         pid = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "/local/domain/%d/image/device-model-pid", domid));
@@ -1267,7 +1392,37 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid)
 
         libxl__qmp_cleanup(gc, domid);
     }
-    if (libxl__devices_destroy(gc, domid) < 0)
+    dis->drs.ao = ao;
+    dis->drs.domid = domid;
+    dis->drs.callback = devices_destroy_cb;
+    dis->drs.force = 1;
+    libxl__devices_destroy(egc, &dis->drs);
+    return;
+
+out:
+    assert(rc);
+    dis->callback(egc, dis, rc);
+    return;
+}
+
+static void devices_destroy_cb(libxl__egc *egc,
+                               libxl__devices_remove_state *drs,
+                               int rc)
+{
+    STATE_AO_GC(drs->ao);
+    libxl__destroy_domid_state *dis = CONTAINER_OF(drs, *dis, drs);
+    libxl_ctx *ctx = CTX;
+    uint32_t domid = dis->domid;
+    char *dom_path;
+    char *vm_path;
+
+    dom_path = libxl__xs_get_dompath(gc, domid);
+    if (!dom_path) {
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    if (rc < 0)
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, 
                    "libxl__devices_destroy failed for %d", domid);
 
@@ -1280,6 +1435,10 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid)
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", dom_path);
 
     xs_rm(ctx->xsh, XBT_NULL, libxl__xs_libxl_path(gc, domid));
+    xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(gc,
+                                "/local/domain/0/device-model/%d", domid));
+    xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(gc,
+                                "/local/domain/%d/hvmloader", domid));
 
     libxl__userdata_destroyall(gc, domid);
 
@@ -1290,9 +1449,10 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid)
         goto out;
     }
     rc = 0;
+
 out:
-    GC_FREE;
-    return rc;
+    dis->callback(egc, dis, rc);
+    return;
 }
 
 int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num,
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 75175c7..aef1be5 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -526,7 +526,8 @@ int libxl_domain_remus_start(libxl_ctx *ctx, libxl_domain_remus_info *info,
 
 int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid);
 int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid);
-int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid);
+int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid,
+                         const libxl_asyncop_how *ao_how);
 int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid);
 
 /* get max. number of cpus supported by hypervisor */
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index 2ddf5fb..b54717a 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -571,6 +571,12 @@ static void domcreate_complete(libxl__egc *egc,
                                libxl__domain_create_state *dcs,
                                int rc);
 
+/* If creation is not successful, this callback will be executed
+ * when domain destruction is finished */
+static void domcreate_destruction_cb(libxl__egc *egc,
+                                     libxl__domain_destroy_state *dds,
+                                     int rc);
+
 static void initiate_domain_create(libxl__egc *egc,
                                    libxl__domain_create_state *dcs)
 {
@@ -996,16 +1002,31 @@ static void domcreate_complete(libxl__egc *egc,
 
     if (rc) {
         if (dcs->guest_domid) {
-            int rc2 = libxl_domain_destroy(CTX, dcs->guest_domid);
-            if (rc2)
-                LOG(ERROR, "unable to destroy domain %d following"
-                    " failed creation", dcs->guest_domid);
+            dcs->dds.ao = ao;
+            dcs->dds.domid = dcs->guest_domid;
+            dcs->dds.callback = domcreate_destruction_cb;
+            libxl__domain_destroy(egc, &dcs->dds);
+            return;
         }
         dcs->guest_domid = -1;
     }
     dcs->callback(egc, dcs, rc, dcs->guest_domid);
 }
 
+static void domcreate_destruction_cb(libxl__egc *egc,
+                                     libxl__domain_destroy_state *dds,
+                                     int rc)
+{
+    STATE_AO_GC(dds->ao);
+    libxl__domain_create_state *dcs = CONTAINER_OF(dds, *dcs, dds);
+
+    if (rc)
+        LOG(ERROR, "unable to destroy domain %u following failed creation",
+                   dds->domid);
+
+    dcs->callback(egc, dcs, ERROR_FAIL, dcs->guest_domid);
+}
+
 /*----- application-facing domain creation interface -----*/
 
 typedef struct {
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 8316707..8daf69d 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -58,6 +58,48 @@ int libxl__parse_backend_path(libxl__gc *gc,
     return libxl__device_kind_from_string(strkind, &dev->backend_kind);
 }
 
+static int libxl__num_devices(libxl__gc *gc, uint32_t domid)
+{
+    char *path;
+    unsigned int num_kinds, num_devs;
+    char **kinds = NULL, **devs = NULL;
+    int i, j, rc = 0;
+    libxl__device dev;
+    libxl__device_kind kind;
+    int numdevs = 0;
+
+    path = GCSPRINTF("/local/domain/%d/device", domid);
+    kinds = libxl__xs_directory(gc, XBT_NULL, path, &num_kinds);
+    if (!kinds) {
+        if (errno != ENOENT) {
+            LOGE(ERROR, "unable to get xenstore device listing %s", path);
+            rc = ERROR_FAIL;
+            goto out;
+        }
+        num_kinds = 0;
+    }
+    for (i = 0; i < num_kinds; i++) {
+        if (libxl__device_kind_from_string(kinds[i], &kind))
+            continue;
+
+        path = GCSPRINTF("/local/domain/%d/device/%s", domid, kinds[i]);
+        devs = libxl__xs_directory(gc, XBT_NULL, path, &num_devs);
+        if (!devs)
+            continue;
+        for (j = 0; j < num_devs; j++) {
+            path = GCSPRINTF("/local/domain/%d/device/%s/%s/backend",
+                             domid, kinds[i], devs[j]);
+            path = libxl__xs_read(gc, XBT_NULL, path);
+            if (path && libxl__parse_backend_path(gc, path, &dev) == 0) {
+                numdevs++;
+            }
+        }
+    }
+out:
+    if (rc) return rc;
+    return numdevs;
+}
+
 int libxl__device_generic_add(libxl__gc *gc, xs_transaction_t t,
         libxl__device *device, char **bents, char **fents)
 {
@@ -367,6 +409,37 @@ void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev)
 {
     aodev->ao = ao;
     aodev->rc = 0;
+    aodev->active = 1;
+}
+
+void libxl__prepare_ao_devices(libxl__ao *ao, libxl__ao_devices *aodevs)
+{
+    AO_GC;
+
+    GCNEW_ARRAY(aodevs->array, aodevs->size);
+    for (int i = 0; i < aodevs->size; i++) {
+        aodevs->array[i].aodevs = aodevs;
+        libxl__prepare_ao_device(ao, &aodevs->array[i]);
+    }
+}
+
+void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    libxl__ao_devices *aodevs = aodev->aodevs;
+    int i, error = 0;
+
+    aodev->active = 0;
+    for (i = 0; i < aodevs->size; i++) {
+        if (aodevs->array[i].active)
+            return;
+
+        if (aodevs->array[i].rc)
+            error = aodevs->array[i].rc;
+    }
+
+    aodevs->callback(egc, aodevs, error);
+    return;
 }
 
 int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
@@ -383,16 +456,35 @@ int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
     return 0;
 }
 
-int libxl__devices_destroy(libxl__gc *gc, uint32_t domid)
+/* Callback for device destruction */
+
+static void devices_remove_callback(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                    int rc);
+
+void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs)
 {
+    STATE_AO_GC(drs->ao);
     libxl_ctx *ctx = libxl__gc_owner(gc);
+    uint32_t domid = drs->domid;
     char *path;
-    unsigned int num_kinds, num_devs;
+    unsigned int num_kinds, num_dev_xsentries;
     char **kinds = NULL, **devs = NULL;
-    int i, j;
-    libxl__device dev;
+    int i, j, numdev = 0, rc = 0;
+    libxl__device *dev;
+    libxl__ao_devices *aodevs = &drs->aodevs;
+    libxl__ao_device *aodev;
     libxl__device_kind kind;
 
+    aodevs->size = libxl__num_devices(gc, drs->domid);
+    if (aodevs->size < 0) {
+        LOG(ERROR, "unable to get number of devices for domain %u", drs->domid);
+        rc = aodevs->size;
+        goto out;
+    }
+
+    libxl__prepare_ao_devices(drs->ao, aodevs);
+    aodevs->callback = devices_remove_callback;
+
     path = libxl__sprintf(gc, "/local/domain/%d/device", domid);
     kinds = libxl__xs_directory(gc, XBT_NULL, path, &num_kinds);
     if (!kinds) {
@@ -408,19 +500,25 @@ int libxl__devices_destroy(libxl__gc *gc, uint32_t domid)
             continue;
 
         path = libxl__sprintf(gc, "/local/domain/%d/device/%s", domid, kinds[i]);
-        devs = libxl__xs_directory(gc, XBT_NULL, path, &num_devs);
+        devs = libxl__xs_directory(gc, XBT_NULL, path, &num_dev_xsentries);
         if (!devs)
             continue;
-        for (j = 0; j < num_devs; j++) {
+        for (j = 0; j < num_dev_xsentries; j++) {
             path = libxl__sprintf(gc, "/local/domain/%d/device/%s/%s/backend",
                                   domid, kinds[i], devs[j]);
             path = libxl__xs_read(gc, XBT_NULL, path);
-            if (path && libxl__parse_backend_path(gc, path, &dev) == 0) {
-                dev.domid = domid;
-                dev.kind = kind;
-                dev.devid = atoi(devs[j]);
-
-                libxl__device_destroy(gc, &dev);
+            GCNEW(dev);
+            if (path && libxl__parse_backend_path(gc, path, dev) == 0) {
+                aodev = &aodevs->array[numdev];
+                dev->domid = domid;
+                dev->kind = kind;
+                dev->devid = atoi(devs[j]);
+                aodev->action = DEVICE_DISCONNECT;
+                aodev->dev = dev;
+                aodev->callback = libxl__ao_devices_callback;
+                aodev->force = drs->force;
+                libxl__initiate_device_remove(egc, aodev);
+                numdev++;
             }
         }
     }
@@ -428,17 +526,22 @@ int libxl__devices_destroy(libxl__gc *gc, uint32_t domid)
     /* console 0 frontend directory is not under /local/domain/<domid>/device */
     path = libxl__sprintf(gc, "/local/domain/%d/console/backend", domid);
     path = libxl__xs_read(gc, XBT_NULL, path);
+    GCNEW(dev);
     if (path && strcmp(path, "") &&
-        libxl__parse_backend_path(gc, path, &dev) == 0) {
-        dev.domid = domid;
-        dev.kind = LIBXL__DEVICE_KIND_CONSOLE;
-        dev.devid = 0;
-
-        libxl__device_destroy(gc, &dev);
+        libxl__parse_backend_path(gc, path, dev) == 0) {
+        dev->domid = domid;
+        dev->kind = LIBXL__DEVICE_KIND_CONSOLE;
+        dev->devid = 0;
+
+        /* Currently console devices can be destroyed synchronously by just
+         * removing xenstore entries, this is what libxl__device_destroy does.
+         */
+        libxl__device_destroy(gc, dev);
     }
 
 out:
-    return 0;
+    if (!numdev) drs->callback(egc, drs, rc);
+    return;
 }
 
 /* Callbacks for device related operations */
@@ -536,6 +639,7 @@ static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds,
     } else if (rc) {
         LOG(DEBUG, "unable to disconnect device with path %s",
                    libxl__device_backend_path(gc, aodev->dev));
+        rc = 0;
         goto out;
     }
 
@@ -578,6 +682,16 @@ out:
     return;
 }
 
+static void devices_remove_callback(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                    int rc)
+{
+    libxl__devices_remove_state *drs = CONTAINER_OF(aodevs, *drs, aodevs);
+    STATE_AO_GC(drs->ao);
+
+    drs->callback(egc, drs, rc);
+    return;
+}
+
 int libxl__wait_for_device_model(libxl__gc *gc,
                                  uint32_t domid, char *state,
                                  libxl__spawn_starting *spawning,
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index acba613..361b77d 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -694,6 +694,10 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
                                 libxl__dm_spawn_state *stubdom_dmss,
                                 int rc);
 
+static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc,
+                                           libxl__destroy_domid_state *dis,
+                                           int rc);
+
 void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
 {
     STATE_AO_GC(sdss->dm.spawn.ao);
@@ -914,12 +918,31 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
 
  out:
     if (rc) {
-        if (dm_domid)
-            libxl_domain_destroy(CTX, dm_domid);
+        if (dm_domid) {
+            sdss->dis.ao = ao;
+            sdss->dis.domid = dm_domid;
+            sdss->dis.callback = spaw_stubdom_pvqemu_destroy_cb;
+            libxl__destroy_domid(egc, &sdss->dis);
+            return;
+        }
     }
     sdss->callback(egc, &sdss->dm, rc);
 }
 
+static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc,
+                                           libxl__destroy_domid_state *dis,
+                                           int rc)
+{
+    libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(dis, *sdss, dis);
+    STATE_AO_GC(sdss->dis.ao);
+
+    if (rc)
+        LOG(ERROR, "destruction of domain %u after failed creation failed",
+                   sdss->pvqemu.guest_domid);
+
+    sdss->callback(egc, &sdss->dm, rc);
+}
+
 /* callbacks passed to libxl__spawn_spawn */
 static void device_model_confirm(libxl__egc *egc, libxl__spawn_state *spawn,
                                  const char *xsdata);
@@ -1115,55 +1138,27 @@ static void device_model_spawn_outcome(libxl__egc *egc,
 
 int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid)
 {
-    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *pid;
     int ret;
 
     pid = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "/local/domain/%d/image/device-model-pid", domid));
-    if (!pid) {
-        int stubdomid = libxl_get_stubdom_id(ctx, domid);
-        const char *savefile;
-
-        if (!stubdomid) {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't find device model's pid");
-            ret = ERROR_INVAL;
-            goto out;
-        }
-        LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device model is a stubdom, domid=%d", stubdomid);
-        ret = libxl_domain_destroy(ctx, stubdomid);
-        if (ret)
-            goto out;
-
-        savefile = libxl__device_model_savefile(gc, domid);
-        ret = unlink(savefile);
-        /*
-         * On suspend libxl__domain_save_device_model will have already
-         * unlinked the save file.
-         */
-        if (ret && errno == ENOENT) ret = 0;
-        if (ret) {
-            LIBXL__LOG_ERRNO(ctx, XTL_ERROR,
-                             "failed to remove device-model savefile %s\n",
-                             savefile);
-            goto out;
-        }
+    if (!pid || !atoi(pid)) {
+        LOG(ERROR, "could not find device-model's pid for dom %u", domid);
+        ret = ERROR_FAIL;
+        goto out;
+    }
+    ret = kill(atoi(pid), SIGHUP);
+    if (ret < 0 && errno == ESRCH) {
+        LOG(ERROR, "Device Model already exited");
+        ret = 0;
+    } else if (ret == 0) {
+        LOG(DEBUG, "Device Model signaled");
+        ret = 0;
     } else {
-        ret = kill(atoi(pid), SIGHUP);
-        if (ret < 0 && errno == ESRCH) {
-            LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device Model already exited");
-            ret = 0;
-        } else if (ret == 0) {
-            LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device Model signaled");
-            ret = 0;
-        } else {
-            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "failed to kill Device Model [%d]",
-                    atoi(pid));
-            ret = ERROR_FAIL;
-            goto out;
-        }
+        LOGE(ERROR, "failed to kill Device Model [%d]", atoi(pid));
+        ret = ERROR_FAIL;
+        goto out;
     }
-    xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(gc, "/local/domain/0/device-model/%d", domid));
-    xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(gc, "/local/domain/%d/hvmloader", domid));
 
 out:
     return ret;
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 61fd100..7aa46b8 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -868,7 +868,6 @@ _hidden char *libxl__device_frontend_path(libxl__gc *gc, libxl__device *device);
 _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path,
                                       libxl__device *dev);
 _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev);
-_hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid);
 _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);
 
 /*
@@ -1971,6 +1970,7 @@ typedef enum {
 } libxl__device_action;
 
 typedef struct libxl__ao_device libxl__ao_device;
+typedef struct libxl__ao_devices libxl__ao_devices;
 typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);
 
 /* This functions sets the necessary libxl__ao_device struct values to use
@@ -1989,6 +1989,20 @@ typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);
  */
 _hidden void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev);
 
+/* Prepare a bunch of devices for addition/removal. Every ao_device in
+ * ao_devices is set to 'active', and the ao_device 'base' field is set to
+ * the one pointed by aodevs.
+ */
+_hidden void libxl__prepare_ao_devices(libxl__ao *ao,
+                                       libxl__ao_devices *aodevs);
+
+/* Generic callback to use when adding/removing several devices, this will
+ * check if the given aodev is the last one, and call the callback in the
+ * parent libxl__ao_devices struct, passing the appropriate error if found.
+ */
+_hidden void libxl__ao_devices_callback(libxl__egc *egc,
+                                        libxl__ao_device *aodev);
+
 struct libxl__ao_device {
     /* filled in by user */
     libxl__ao *ao;
@@ -1997,8 +2011,25 @@ struct libxl__ao_device {
     int force;
     libxl__device_callback *callback;
     /* private for implementation */
+    int active;
     int rc;
     libxl__ev_devstate backend_ds;
+    /* Used internally to have a reference to the upper libxl__ao_devices
+     * struct when present */
+    libxl__ao_devices *aodevs;
+};
+
+/* Helper struct to simply the plug/unplug of multiple devices at the same
+ * time.
+ *
+ * This structure holds several devices, and the callback is only called
+ * when all the devices inside of the array have finished.
+ */
+typedef void libxl__devices_callback(libxl__egc*, libxl__ao_devices*, int rc);
+struct libxl__ao_devices {
+    libxl__ao_device *array;
+    int size;
+    libxl__devices_callback *callback;
 };
 
 /* Arranges that dev will be removed to the guest, and the
@@ -2013,6 +2044,86 @@ struct libxl__ao_device {
 _hidden void libxl__initiate_device_remove(libxl__egc *egc,
                                            libxl__ao_device *aodev);
 
+/*----- Domain destruction -----*/
+
+/* Domain destruction has been split into two functions:
+ *
+ * libxl__domain_destroy is the main destroy function, which detects
+ * stubdoms and calls libxl__destroy_domid on the domain and its
+ * stubdom if present, creating a different libxl__destroy_domid_state
+ * for each one of them.
+ *
+ * libxl__destroy_domid actually destroys the domain, but it
+ * doesn't check for stubdomains, since that would involve
+ * recursion, which we want to avoid.
+ */
+
+typedef struct libxl__domain_destroy_state libxl__domain_destroy_state;
+typedef struct libxl__destroy_domid_state libxl__destroy_domid_state;
+typedef struct libxl__devices_remove_state libxl__devices_remove_state;
+
+typedef void libxl__domain_destroy_cb(libxl__egc *egc,
+                                      libxl__domain_destroy_state *dds,
+                                      int rc);
+
+typedef void libxl__domid_destroy_cb(libxl__egc *egc,
+                                     libxl__destroy_domid_state *dis,
+                                     int rc);
+
+typedef void libxl__devices_remove_callback(libxl__egc *egc,
+                                            libxl__devices_remove_state *drs,
+                                            int rc);
+
+struct libxl__devices_remove_state {
+    /* filled in by user */
+    libxl__ao *ao;
+    uint32_t domid;
+    libxl__devices_remove_callback *callback;
+    int force; /* libxl_device_TYPE_destroy rather than _remove */
+    /* private */
+    libxl__ao_devices aodevs;
+    int num_devices;
+};
+
+struct libxl__destroy_domid_state {
+    /* filled in by user */
+    libxl__ao *ao;
+    uint32_t domid;
+    libxl__domid_destroy_cb *callback;
+    /* private to implementation */
+    libxl__devices_remove_state drs;
+};
+
+struct libxl__domain_destroy_state {
+    /* filled by the user */
+    libxl__ao *ao;
+    uint32_t domid;
+    libxl__domain_destroy_cb *callback;
+    /* Private */
+    int rc;
+    uint32_t stubdomid;
+    libxl__destroy_domid_state stubdom;
+    int stubdom_finished;
+    libxl__destroy_domid_state domain;
+    int domain_finished;
+};
+
+/*
+ * Entry point for domain destruction
+ * This function checks for stubdom presence and then calls
+ * libxl__destroy_domid on the passed domain and its stubdom if found.
+ */
+_hidden void libxl__domain_destroy(libxl__egc *egc,
+                                   libxl__domain_destroy_state *dds);
+
+/* Used to destroy a domain with the passed id (it doesn't check for stubs) */
+_hidden void libxl__destroy_domid(libxl__egc *egc,
+                                  libxl__destroy_domid_state *dis);
+
+/* Entry point for devices destruction */
+_hidden void libxl__devices_destroy(libxl__egc *egc,
+                                    libxl__devices_remove_state *drs);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
@@ -2046,6 +2157,7 @@ typedef struct {
     libxl_domain_config dm_config;
     libxl__domain_build_state dm_state;
     libxl__dm_spawn_state pvqemu;
+    libxl__destroy_domid_state dis;
 } libxl__stub_dm_spawn_state;
 
 _hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*);
@@ -2073,6 +2185,8 @@ struct libxl__domain_create_state {
         /* If we're not doing stubdom, we use only dmss.dm,
          * for the non-stubdom device model. */
     libxl__save_helper_state shs;
+    /* necessary if the domain creation failed and we have to destroy it */
+    libxl__domain_destroy_state dds;
 };
 
 /*----- Domain suspend (save) functions -----*/
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 5040210..b3d8b7a 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -1401,7 +1401,7 @@ static int handle_domain_death(uint32_t *r_domid,
     case LIBXL_ACTION_ON_SHUTDOWN_DESTROY:
         LOG("Domain %d needs to be cleaned up: destroying the domain",
             *r_domid);
-        libxl_domain_destroy(ctx, *r_domid);
+        libxl_domain_destroy(ctx, *r_domid, 0);
         *r_domid = INVALID_DOMID;
         break;
 
@@ -2011,7 +2011,7 @@ start:
 error_out:
     release_lock();
     if (libxl_domid_valid_guest(domid)) {
-        libxl_domain_destroy(ctx, domid);
+        libxl_domain_destroy(ctx, domid, 0);
         domid = INVALID_DOMID;
     }
 
@@ -2576,7 +2576,7 @@ static void destroy_domain(const char *p)
         fprintf(stderr, "Cannot destroy privileged domain 0.\n\n");
         exit(-1);
     }
-    rc = libxl_domain_destroy(ctx, domid);
+    rc = libxl_domain_destroy(ctx, domid, 0);
     if (rc) { fprintf(stderr,"destroy failed (rc=%d)\n",rc); exit(-1); }
 }
 
@@ -2850,7 +2850,7 @@ static int save_domain(const char *p, const char *filename, int checkpoint,
     if (checkpoint)
         libxl_domain_resume(ctx, domid, 1);
     else
-        libxl_domain_destroy(ctx, domid);
+        libxl_domain_destroy(ctx, domid, 0);
 
     exit(0);
 }
@@ -3107,7 +3107,7 @@ static void migrate_domain(const char *domain_spec, const char *rune,
     }
 
     fprintf(stderr, "migration sender: Target reports successful startup.\n");
-    libxl_domain_destroy(ctx, domid); /* bang! */
+    libxl_domain_destroy(ctx, domid, 0); /* bang! */
     fprintf(stderr, "Migration successful.\n");
     exit(0);
 
@@ -3260,7 +3260,7 @@ static void migrate_receive(int debug, int daemonize, int monitor,
     if (rc) {
         fprintf(stderr, "migration target: Failure, destroying our copy.\n");
 
-        rc2 = libxl_domain_destroy(ctx, domid);
+        rc2 = libxl_domain_destroy(ctx, domid, 0);
         if (rc2) {
             fprintf(stderr, "migration target: Failed to destroy our copy"
                     " (code %d).\n", rc2);
diff --git a/tools/python/xen/lowlevel/xl/xl.c b/tools/python/xen/lowlevel/xl/xl.c
index 1db777d..4ff3120 100644
--- a/tools/python/xen/lowlevel/xl/xl.c
+++ b/tools/python/xen/lowlevel/xl/xl.c
@@ -437,7 +437,7 @@ static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args)
     int domid;
     if ( !PyArg_ParseTuple(args, "i", &domid) )
         return NULL;
-    if ( libxl_domain_destroy(self->ctx, domid) ) {
+    if ( libxl_domain_destroy(self->ctx, domid, 0) ) {
         PyErr_SetString(xl_error_obj, "cannot destroy domain");
         return NULL;
     }
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 04/15] libxl: move bootloader data strucutres and prototypes
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (2 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 03/15] libxl: convert libxl_domain_destroy to an async op Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 05/15] libxl: refactor disk addition to take a helper Roger Pau Monne
                   ` (11 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Move bootloader and related data after all the device stuff, since
libxl__bootloader_state will depend on libxl__ao_device (to perform
the local attach of a device).

This is pure code motion.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl_internal.h |  166 +++++++++++++++++++++---------------------
 1 files changed, 83 insertions(+), 83 deletions(-)

diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 7aa46b8..7a75809 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1740,6 +1740,89 @@ _hidden const char *libxl__xen_script_dir_path(void);
 _hidden const char *libxl__lock_dir_path(void);
 _hidden const char *libxl__run_dir_path(void);
 
+/*----- device addition/removal -----*/
+
+/* Action to perform (either connect or disconnect) */
+typedef enum {
+    DEVICE_CONNECT,
+    DEVICE_DISCONNECT
+} libxl__device_action;
+
+typedef struct libxl__ao_device libxl__ao_device;
+typedef struct libxl__ao_devices libxl__ao_devices;
+typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);
+
+/* This functions sets the necessary libxl__ao_device struct values to use
+ * safely inside functions. It marks the operation as "active"
+ * since we need to be sure that all device status structs are set
+ * to active before start queueing events, or we might call
+ * ao_complete before all devices had finished
+ *
+ * libxl__initiate_device_{remove/addition} should not be called without
+ * calling libxl__prepare_ao_device first, since it initializes the private
+ * fields of the struct libxl__ao_device to what this functions expect.
+ *
+ * Once _prepare has been called on a libxl__ao_device, it is safe to just
+ * discard this struct, there's no need to call any destroy function.
+ * _prepare can also be called multiple times with the same libxl__ao_device.
+ */
+_hidden void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev);
+
+/* Prepare a bunch of devices for addition/removal. Every ao_device in
+ * ao_devices is set to 'active', and the ao_device 'base' field is set to
+ * the one pointed by aodevs.
+ */
+_hidden void libxl__prepare_ao_devices(libxl__ao *ao,
+                                       libxl__ao_devices *aodevs);
+
+/* Generic callback to use when adding/removing several devices, this will
+ * check if the given aodev is the last one, and call the callback in the
+ * parent libxl__ao_devices struct, passing the appropriate error if found.
+ */
+_hidden void libxl__ao_devices_callback(libxl__egc *egc,
+                                        libxl__ao_device *aodev);
+
+struct libxl__ao_device {
+    /* filled in by user */
+    libxl__ao *ao;
+    libxl__device_action action;
+    libxl__device *dev;
+    int force;
+    libxl__device_callback *callback;
+    /* private for implementation */
+    int active;
+    int rc;
+    libxl__ev_devstate backend_ds;
+    /* Used internally to have a reference to the upper libxl__ao_devices
+     * struct when present */
+    libxl__ao_devices *aodevs;
+};
+
+/* Helper struct to simply the plug/unplug of multiple devices at the same
+ * time.
+ *
+ * This structure holds several devices, and the callback is only called
+ * when all the devices inside of the array have finished.
+ */
+typedef void libxl__devices_callback(libxl__egc*, libxl__ao_devices*, int rc);
+struct libxl__ao_devices {
+    libxl__ao_device *array;
+    int size;
+    libxl__devices_callback *callback;
+};
+
+/* Arranges that dev will be removed to the guest, and the
+ * hotplug scripts will be executed (if necessary). When
+ * this is done (or an error happens), the callback in
+ * aodev->callback will be called.
+ *
+ * The libxl__ao_device passed to this function should be
+ * prepared using libxl__prepare_ao_device prior to calling
+ * this function.
+ */
+_hidden void libxl__initiate_device_remove(libxl__egc *egc,
+                                           libxl__ao_device *aodev);
+
 /*----- datacopier: copies data from one fd to another -----*/
 
 typedef struct libxl__datacopier_state libxl__datacopier_state;
@@ -1961,89 +2044,6 @@ _hidden void libxl__bootloader_init(libxl__bootloader_state *bl);
  * If callback is passed rc==0, will have updated st->info appropriately */
 _hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st);
 
-/*----- device addition/removal -----*/
-
-/* Action to perform (either connect or disconnect) */
-typedef enum {
-    DEVICE_CONNECT,
-    DEVICE_DISCONNECT
-} libxl__device_action;
-
-typedef struct libxl__ao_device libxl__ao_device;
-typedef struct libxl__ao_devices libxl__ao_devices;
-typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*);
-
-/* This functions sets the necessary libxl__ao_device struct values to use
- * safely inside functions. It marks the operation as "active"
- * since we need to be sure that all device status structs are set
- * to active before start queueing events, or we might call
- * ao_complete before all devices had finished
- *
- * libxl__initiate_device_{remove/addition} should not be called without
- * calling libxl__prepare_ao_device first, since it initializes the private
- * fields of the struct libxl__ao_device to what this functions expect.
- *
- * Once _prepare has been called on a libxl__ao_device, it is safe to just
- * discard this struct, there's no need to call any destroy function.
- * _prepare can also be called multiple times with the same libxl__ao_device.
- */
-_hidden void libxl__prepare_ao_device(libxl__ao *ao, libxl__ao_device *aodev);
-
-/* Prepare a bunch of devices for addition/removal. Every ao_device in
- * ao_devices is set to 'active', and the ao_device 'base' field is set to
- * the one pointed by aodevs.
- */
-_hidden void libxl__prepare_ao_devices(libxl__ao *ao,
-                                       libxl__ao_devices *aodevs);
-
-/* Generic callback to use when adding/removing several devices, this will
- * check if the given aodev is the last one, and call the callback in the
- * parent libxl__ao_devices struct, passing the appropriate error if found.
- */
-_hidden void libxl__ao_devices_callback(libxl__egc *egc,
-                                        libxl__ao_device *aodev);
-
-struct libxl__ao_device {
-    /* filled in by user */
-    libxl__ao *ao;
-    libxl__device_action action;
-    libxl__device *dev;
-    int force;
-    libxl__device_callback *callback;
-    /* private for implementation */
-    int active;
-    int rc;
-    libxl__ev_devstate backend_ds;
-    /* Used internally to have a reference to the upper libxl__ao_devices
-     * struct when present */
-    libxl__ao_devices *aodevs;
-};
-
-/* Helper struct to simply the plug/unplug of multiple devices at the same
- * time.
- *
- * This structure holds several devices, and the callback is only called
- * when all the devices inside of the array have finished.
- */
-typedef void libxl__devices_callback(libxl__egc*, libxl__ao_devices*, int rc);
-struct libxl__ao_devices {
-    libxl__ao_device *array;
-    int size;
-    libxl__devices_callback *callback;
-};
-
-/* Arranges that dev will be removed to the guest, and the
- * hotplug scripts will be executed (if necessary). When
- * this is done (or an error happens), the callback in
- * aodev->callback will be called.
- *
- * The libxl__ao_device passed to this function should be
- * prepared using libxl__prepare_ao_device prior to calling
- * this function.
- */
-_hidden void libxl__initiate_device_remove(libxl__egc *egc,
-                                           libxl__ao_device *aodev);
-
 /*----- Domain destruction -----*/
 
 /* Domain destruction has been split into two functions:
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 05/15] libxl: refactor disk addition to take a helper
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (3 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 04/15] libxl: move bootloader data strucutres and prototypes Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 06/15] libxl: convert libxl__device_disk_local_attach to an async op Roger Pau Monne
                   ` (10 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Change libxl__device_disk_add to no longer take a xs transaction and
instead pass a helper for the local attach case that's used to get the
free vdev.

This function contains some non-functional changes due to an
indentation change.

Changes since v7:

 * Fixed the use of the transaction inside device_disk_add (which in
   v7 was fixed in the next patch).

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c          |  254 +++++++++++++++++++++++-------------------
 tools/libxl/libxl_internal.h |    2 +-
 2 files changed, 138 insertions(+), 118 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 7e6d5c9..4d832bb 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -1733,115 +1733,146 @@ int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
     return 0;
 }
 
-int libxl__device_disk_add(libxl__gc *gc, uint32_t domid,
-        xs_transaction_t t, libxl_device_disk *disk)
-{
-    flexarray_t *front;
-    flexarray_t *back;
+/* Specific function called directly only by local disk attach,
+ * all other users should instead use the regular
+ * libxl__device_disk_add wrapper
+ */
+static int device_disk_add(libxl__gc *gc, uint32_t domid,
+                           libxl_device_disk *disk,
+                           char *(*fn)(libxl__gc *, const char *,
+                                              xs_transaction_t),
+                           const char *blkdev_start)
+{
+    flexarray_t *front = NULL;
+    flexarray_t *back = NULL;
     char *dev;
     libxl__device device;
     int major, minor, rc;
     libxl_ctx *ctx = gc->owner;
+    xs_transaction_t t = XBT_NULL;
 
-    rc = libxl__device_disk_setdefault(gc, disk);
-    if (rc) goto out;
+    for (;;) {
+        rc = libxl__xs_transaction_start(gc, &t);
+        if (rc) goto out;
+
+        if (fn) {
+            assert(blkdev_start);
+            disk->vdev = fn(gc, blkdev_start, t);
+            if (disk->vdev == NULL) {
+                LOG(ERROR, "libxl__alloc_vdev failed");
+                rc = ERROR_FAIL;
+                goto out;
+            }
+        }
 
-    front = flexarray_make(16, 1);
-    if (!front) {
-        rc = ERROR_NOMEM;
-        goto out;
-    }
-    back = flexarray_make(16, 1);
-    if (!back) {
-        rc = ERROR_NOMEM;
-        goto out_free;
-    }
+        rc = libxl__device_disk_setdefault(gc, disk);
+        if (rc) goto out;
 
-    if (disk->script) {
-        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "External block scripts"
-                   " not yet supported, sorry");
-        rc = ERROR_INVAL;
-        goto out_free;
-    }
+        if (front)
+            flexarray_free(front);
+        front = flexarray_make(16, 1);
+        if (!front) {
+            rc = ERROR_NOMEM;
+            goto out;
+        }
+        if (back)
+            flexarray_free(back);
+        back = flexarray_make(16, 1);
+        if (!back) {
+            rc = ERROR_NOMEM;
+            goto out_free;
+        }
 
-    rc = libxl__device_from_disk(gc, domid, disk, &device);
-    if (rc != 0) {
-        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
-               " virtual disk identifier %s", disk->vdev);
-        goto out_free;
-    }
+        if (disk->script) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "External block scripts"
+                       " not yet supported, sorry");
+            rc = ERROR_INVAL;
+            goto out_free;
+        }
 
-    switch (disk->backend) {
-        case LIBXL_DISK_BACKEND_PHY:
-            dev = disk->pdev_path;
-    do_backend_phy:
-            libxl__device_physdisk_major_minor(dev, &major, &minor);
-            flexarray_append(back, "physical-device");
-            flexarray_append(back, libxl__sprintf(gc, "%x:%x", major, minor));
+        rc = libxl__device_from_disk(gc, domid, disk, &device);
+        if (rc != 0) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
+                   " virtual disk identifier %s", disk->vdev);
+            goto out_free;
+        }
 
-            flexarray_append(back, "params");
-            flexarray_append(back, dev);
+        switch (disk->backend) {
+            case LIBXL_DISK_BACKEND_PHY:
+                dev = disk->pdev_path;
+        do_backend_phy:
+                libxl__device_physdisk_major_minor(dev, &major, &minor);
+                flexarray_append(back, "physical-device");
+                flexarray_append(back, libxl__sprintf(gc, "%x:%x", major, minor));
 
-            assert(device.backend_kind == LIBXL__DEVICE_KIND_VBD);
-            break;
-        case LIBXL_DISK_BACKEND_TAP:
-            dev = libxl__blktap_devpath(gc, disk->pdev_path, disk->format);
-            if (!dev) {
-                LOG(ERROR, "failed to get blktap devpath for %p\n",
-                    disk->pdev_path);
-                rc = ERROR_FAIL;
-                goto out_free;
-            }
-            flexarray_append(back, "tapdisk-params");
-            flexarray_append(back, libxl__sprintf(gc, "%s:%s",
-                libxl__device_disk_string_of_format(disk->format),
-                disk->pdev_path));
+                flexarray_append(back, "params");
+                flexarray_append(back, dev);
 
-            /* now create a phy device to export the device to the guest */
-            goto do_backend_phy;
-        case LIBXL_DISK_BACKEND_QDISK:
-            flexarray_append(back, "params");
-            flexarray_append(back, libxl__sprintf(gc, "%s:%s",
-                          libxl__device_disk_string_of_format(disk->format), disk->pdev_path));
-            assert(device.backend_kind == LIBXL__DEVICE_KIND_QDISK);
-            break;
-        default:
-            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk backend type: %d\n", disk->backend);
-            rc = ERROR_INVAL;
-            goto out_free;
-    }
+                assert(device.backend_kind == LIBXL__DEVICE_KIND_VBD);
+                break;
+            case LIBXL_DISK_BACKEND_TAP:
+                dev = libxl__blktap_devpath(gc, disk->pdev_path, disk->format);
+                if (!dev) {
+                    LOG(ERROR, "failed to get blktap devpath for %p\n",
+                        disk->pdev_path);
+                    rc = ERROR_FAIL;
+                    goto out_free;
+                }
+                flexarray_append(back, "tapdisk-params");
+                flexarray_append(back, libxl__sprintf(gc, "%s:%s",
+                    libxl__device_disk_string_of_format(disk->format),
+                    disk->pdev_path));
+
+                /* now create a phy device to export the device to the guest */
+                goto do_backend_phy;
+            case LIBXL_DISK_BACKEND_QDISK:
+                flexarray_append(back, "params");
+                flexarray_append(back, libxl__sprintf(gc, "%s:%s",
+                              libxl__device_disk_string_of_format(disk->format), disk->pdev_path));
+                assert(device.backend_kind == LIBXL__DEVICE_KIND_QDISK);
+                break;
+            default:
+                LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk backend type: %d\n", disk->backend);
+                rc = ERROR_INVAL;
+                goto out_free;
+        }
 
-    flexarray_append(back, "frontend-id");
-    flexarray_append(back, libxl__sprintf(gc, "%d", domid));
-    flexarray_append(back, "online");
-    flexarray_append(back, "1");
-    flexarray_append(back, "removable");
-    flexarray_append(back, libxl__sprintf(gc, "%d", (disk->removable) ? 1 : 0));
-    flexarray_append(back, "bootable");
-    flexarray_append(back, libxl__sprintf(gc, "%d", 1));
-    flexarray_append(back, "state");
-    flexarray_append(back, libxl__sprintf(gc, "%d", 1));
-    flexarray_append(back, "dev");
-    flexarray_append(back, disk->vdev);
-    flexarray_append(back, "type");
-    flexarray_append(back, libxl__device_disk_string_of_backend(disk->backend));
-    flexarray_append(back, "mode");
-    flexarray_append(back, disk->readwrite ? "w" : "r");
-    flexarray_append(back, "device-type");
-    flexarray_append(back, disk->is_cdrom ? "cdrom" : "disk");
+        flexarray_append(back, "frontend-id");
+        flexarray_append(back, libxl__sprintf(gc, "%d", domid));
+        flexarray_append(back, "online");
+        flexarray_append(back, "1");
+        flexarray_append(back, "removable");
+        flexarray_append(back, libxl__sprintf(gc, "%d", (disk->removable) ? 1 : 0));
+        flexarray_append(back, "bootable");
+        flexarray_append(back, libxl__sprintf(gc, "%d", 1));
+        flexarray_append(back, "state");
+        flexarray_append(back, libxl__sprintf(gc, "%d", 1));
+        flexarray_append(back, "dev");
+        flexarray_append(back, disk->vdev);
+        flexarray_append(back, "type");
+        flexarray_append(back, libxl__device_disk_string_of_backend(disk->backend));
+        flexarray_append(back, "mode");
+        flexarray_append(back, disk->readwrite ? "w" : "r");
+        flexarray_append(back, "device-type");
+        flexarray_append(back, disk->is_cdrom ? "cdrom" : "disk");
+
+        flexarray_append(front, "backend-id");
+        flexarray_append(front, libxl__sprintf(gc, "%d", disk->backend_domid));
+        flexarray_append(front, "state");
+        flexarray_append(front, libxl__sprintf(gc, "%d", 1));
+        flexarray_append(front, "virtual-device");
+        flexarray_append(front, libxl__sprintf(gc, "%d", device.devid));
+        flexarray_append(front, "device-type");
+        flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
 
-    flexarray_append(front, "backend-id");
-    flexarray_append(front, libxl__sprintf(gc, "%d", disk->backend_domid));
-    flexarray_append(front, "state");
-    flexarray_append(front, libxl__sprintf(gc, "%d", 1));
-    flexarray_append(front, "virtual-device");
-    flexarray_append(front, libxl__sprintf(gc, "%d", device.devid));
-    flexarray_append(front, "device-type");
-    flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
+        libxl__device_generic_add(gc, t, &device,
+                            libxl__xs_kvs_of_flexarray(gc, back, back->count),
+                            libxl__xs_kvs_of_flexarray(gc, front, front->count));
 
-    libxl__device_generic_add(gc, t, &device,
-                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
-                             libxl__xs_kvs_of_flexarray(gc, front, front->count));
+        rc = libxl__xs_transaction_commit(gc, &t);
+        if (!rc) break;
+        if (rc < 0) goto out_free;
+    }
 
     rc = 0;
 
@@ -1849,13 +1880,20 @@ out_free:
     flexarray_free(back);
     flexarray_free(front);
 out:
+    libxl__xs_transaction_abort(gc, &t);
     return rc;
 }
 
+int libxl__device_disk_add(libxl__gc *gc, uint32_t domid,
+                           libxl_device_disk *disk)
+{
+    return device_disk_add(gc, domid, disk, NULL, NULL);
+}
+
 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
 {
     GC_INIT(ctx);
-    int rc = libxl__device_disk_add(gc, domid, XBT_NULL, disk);
+    int rc = libxl__device_disk_add(gc, domid, disk);
     GC_FREE;
     return rc;
 }
@@ -2113,7 +2151,7 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
     libxl_ctx *ctx = gc->owner;
     char *dev = NULL, *be_path = NULL;
     char *ret = NULL;
-    int rc, xs_ret;
+    int rc;
     libxl__device device;
     xs_transaction_t t = XBT_NULL;
 
@@ -2159,27 +2197,9 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
             break;
         case LIBXL_DISK_BACKEND_QDISK:
             if (disk->format != LIBXL_DISK_FORMAT_RAW) {
-                do {
-                    t = xs_transaction_start(ctx->xsh);
-                    if (t == XBT_NULL) {
-                        LOG(ERROR, "failed to start a xenstore transaction");
-                        goto out;
-                    }
-                    disk->vdev = libxl__alloc_vdev(gc, blkdev_start, t);
-                    if (disk->vdev == NULL) {
-                        LOG(ERROR, "libxl__alloc_vdev failed");
-                        goto out;
-                    }
-                    if (libxl__device_disk_add(gc, LIBXL_TOOLSTACK_DOMID,
-                                t, disk)) {
-                        LOG(ERROR, "libxl_device_disk_add failed");
-                        goto out;
-                    }
-                    xs_ret = xs_transaction_end(ctx->xsh, t, 0);
-                } while (xs_ret == 0 && errno == EAGAIN);
-                t = XBT_NULL;
-                if (xs_ret == 0) {
-                    LOGE(ERROR, "xenstore transaction failed");
+                if (device_disk_add(gc, LIBXL_TOOLSTACK_DOMID, disk,
+                                    libxl__alloc_vdev, blkdev_start)) {
+                    LOG(ERROR, "libxl_device_disk_add failed");
                     goto out;
                 }
                 dev = GCSPRINTF("/dev/%s", disk->vdev);
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 7a75809..1befdc5 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1291,7 +1291,7 @@ _hidden int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
                                    libxl_device_disk *disk,
                                    libxl__device *device);
 _hidden int libxl__device_disk_add(libxl__gc *gc, uint32_t domid,
-        xs_transaction_t t, libxl_device_disk *disk);
+                                   libxl_device_disk *disk);
 
 /*
  * Make a disk available in this (the control) domain. Returns path to
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 06/15] libxl: convert libxl__device_disk_local_attach to an async op
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (4 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 05/15] libxl: refactor disk addition to take a helper Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 07/15] libxl: rename vifs to nics Roger Pau Monne
                   ` (9 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Stefano Stabellini, Ian Jackson, Roger Pau Monne

This will be needed in future patches, when libxl__device_disk_add
becomes async also. Create a new status structure that defines the
local attach of a disk device and use it in
libxl__device_disk_local_attach.

This is done in this patch to split the changes introduced when
libxl__device_disk_add becomes async.

Changes since v6:

 * Added better comments.

 * Instead of passing the xs_transaction arround several functions,
   put all the xs related operations inside device_disk_add.

 * Make sure libxl__device_disk_local_initiate_detach either calls the
   async device destroy function, or the callback.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c            |  100 +++++++++++++++++++++++++++++++---------
 tools/libxl/libxl_bootloader.c |   93 +++++++++++++++++++++++++++++++------
 tools/libxl/libxl_internal.h   |   66 +++++++++++++++++++++------
 3 files changed, 208 insertions(+), 51 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 4d832bb..b2a163e 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -2143,20 +2143,31 @@ static char * libxl__alloc_vdev(libxl__gc *gc, const char *blkdev_start,
     return NULL;
 }
 
-char * libxl__device_disk_local_attach(libxl__gc *gc,
-        const libxl_device_disk *in_disk,
-        libxl_device_disk *disk,
-        const char *blkdev_start)
+void libxl__device_disk_local_init(libxl__disk_local_state *dls, libxl__ao *ao,
+                                   libxl_device_disk *disk,
+                                   const char *blkdev_start,
+                                   libxl__disk_local_state_callback *callback)
 {
-    libxl_ctx *ctx = gc->owner;
+    dls->ao = ao;
+    dls->in_disk = disk;
+    dls->blkdev_start = blkdev_start;
+    dls->callback = callback;
+}
+
+void libxl__device_disk_local_initiate_attach(libxl__egc *egc,
+                                     libxl__disk_local_state *dls)
+{
+    STATE_AO_GC(dls->ao);
+    libxl_ctx *ctx = CTX;
     char *dev = NULL, *be_path = NULL;
-    char *ret = NULL;
     int rc;
     libxl__device device;
     xs_transaction_t t = XBT_NULL;
+    const libxl_device_disk *in_disk = dls->in_disk;
+    libxl_device_disk *disk = &dls->disk;
+    const char *blkdev_start = dls->blkdev_start;
 
-    if (in_disk->pdev_path == NULL)
-        return NULL;
+    assert(in_disk->pdev_path);
 
     memcpy(disk, in_disk, sizeof(libxl_device_disk));
     disk->pdev_path = libxl__strdup(gc, in_disk->pdev_path);
@@ -2192,6 +2203,7 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
             default:
                 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
                            "unrecognized disk format: %d", disk->format);
+                rc = ERROR_FAIL;
                 break;
             }
             break;
@@ -2200,6 +2212,7 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
                 if (device_disk_add(gc, LIBXL_TOOLSTACK_DOMID, disk,
                                     libxl__alloc_vdev, blkdev_start)) {
                     LOG(ERROR, "libxl_device_disk_add failed");
+                    rc = ERROR_FAIL;
                     goto out;
                 }
                 dev = GCSPRINTF("/dev/%s", disk->vdev);
@@ -2211,6 +2224,7 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
         default:
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk backend "
                 "type: %d", disk->backend);
+            rc = ERROR_FAIL;
             break;
     }
 
@@ -2224,42 +2238,84 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
             goto out;
     }
     if (dev != NULL)
-        ret = strdup(dev);
-    return ret;
+        dls->diskpath = strdup(dev);
+
+    dls->callback(egc, dls, 0);
+    return;
 
  out:
+    assert(rc);
     if (t != XBT_NULL)
         xs_transaction_end(ctx->xsh, t, 1);
-    else
-        libxl__device_disk_local_detach(gc, disk);
-    return NULL;
+    dls->callback(egc, dls, rc);
 }
 
-int libxl__device_disk_local_detach(libxl__gc *gc, libxl_device_disk *disk)
+/* Callbacks for local detach */
+
+static void local_device_detach_cb(libxl__egc *egc, libxl__ao_device *aodev);
+
+void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
+                                     libxl__disk_local_state *dls)
 {
+    STATE_AO_GC(dls->ao);
     int rc = 0;
+    libxl_device_disk *disk = &dls->disk;
+    libxl__device *device;
+    libxl__ao_device *aodev = &dls->aodev;
+
+    if (!dls->diskpath) {
+        dls->callback(egc, dls, rc);
+        return;
+    }
 
     switch (disk->backend) {
         case LIBXL_DISK_BACKEND_QDISK:
             if (disk->vdev != NULL) {
-                libxl_device_disk_remove(gc->owner, LIBXL_TOOLSTACK_DOMID,
-                        disk, 0);
-                /* fixme-ao */
-                rc = libxl_device_disk_destroy(gc->owner,
-                        LIBXL_TOOLSTACK_DOMID, disk, 0);
+                GCNEW(device);
+                rc = libxl__device_from_disk(gc, LIBXL_TOOLSTACK_DOMID,
+                                             disk, device);
+                if (rc != 0) goto out;
+
+                libxl__prepare_ao_device(ao, aodev);
+                aodev->action = DEVICE_DISCONNECT;
+                aodev->dev = device;
+                aodev->callback = local_device_detach_cb;
+                aodev->force = 0;
+                libxl__initiate_device_remove(egc, aodev);
+                return;
             }
-            break;
         default:
             /*
              * Nothing to do for PHYSTYPE_PHY.
              * For other device types assume that the blktap2 process is
              * needed by the soon to be started domain and do nothing.
              */
-            break;
+            dls->callback(egc, dls, rc);
+            return;
     }
 
+out:
+    assert(rc);
+    dls->callback(egc, dls, rc);
+    return;
+}
+
+static void local_device_detach_cb(libxl__egc *egc, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    libxl__disk_local_state *dls = CONTAINER_OF(aodev, *dls, aodev);
 
-    return rc;
+    if (aodev->rc) {
+        LOGE(ERROR, "unable to %s %s with id %u",
+                    aodev->action == DEVICE_CONNECT ? "add" : "remove",
+                    libxl__device_kind_to_string(aodev->dev->kind),
+                    aodev->dev->devid);
+        goto out;
+    }
+
+out:
+    dls->callback(egc, dls, aodev->rc);
+    return;
 }
 
 /******************************************************************************/
diff --git a/tools/libxl/libxl_bootloader.c b/tools/libxl/libxl_bootloader.c
index 7ebc0df..7ba3502 100644
--- a/tools/libxl/libxl_bootloader.c
+++ b/tools/libxl/libxl_bootloader.c
@@ -75,7 +75,7 @@ static void make_bootloader_args(libxl__gc *gc, libxl__bootloader_state *bl,
         }
     }
 
-    ARG(bl->diskpath);
+    ARG(bl->dls.diskpath);
 
     /* Sentinel for execv */
     ARG(NULL);
@@ -206,8 +206,9 @@ static int parse_bootloader_result(libxl__egc *egc,
 void libxl__bootloader_init(libxl__bootloader_state *bl)
 {
     assert(bl->ao);
-    bl->diskpath = NULL;
+    bl->dls.diskpath = NULL;
     bl->openpty.ao = bl->ao;
+    bl->dls.ao = bl->ao;
     bl->ptys[0].master = bl->ptys[0].slave = 0;
     bl->ptys[1].master = bl->ptys[1].slave = 0;
     libxl__ev_child_init(&bl->child);
@@ -224,11 +225,6 @@ static void bootloader_cleanup(libxl__egc *egc, libxl__bootloader_state *bl)
     if (bl->outputpath) libxl__remove_file(gc, bl->outputpath);
     if (bl->outputdir) libxl__remove_directory(gc, bl->outputdir);
 
-    if (bl->diskpath) {
-        libxl__device_disk_local_detach(gc, &bl->localdisk);
-        free(bl->diskpath);
-        bl->diskpath = 0;
-    }
     libxl__domaindeathcheck_stop(gc,&bl->deathcheck);
     libxl__datacopier_kill(&bl->keystrokes);
     libxl__datacopier_kill(&bl->display);
@@ -249,10 +245,35 @@ static void bootloader_setpaths(libxl__gc *gc, libxl__bootloader_state *bl)
     bl->outputpath = GCSPRINTF(XEN_RUN_DIR "/bootloader.%"PRIu32".out", domid);
 }
 
+/* Callbacks */
+
+static void bootloader_finished_cb(libxl__egc *egc,
+                                   libxl__disk_local_state *dls,
+                                   int rc);
+
 static void bootloader_callback(libxl__egc *egc, libxl__bootloader_state *bl,
                                 int rc)
 {
     bootloader_cleanup(egc, bl);
+
+    bl->dls.callback = bootloader_finished_cb;
+    libxl__device_disk_local_initiate_detach(egc, &bl->dls);
+}
+
+static void bootloader_finished_cb(libxl__egc *egc,
+                                   libxl__disk_local_state *dls,
+                                   int rc)
+{
+    STATE_AO_GC(dls->ao);
+    libxl__bootloader_state *bl = CONTAINER_OF(dls, *bl, dls);
+
+    free(bl->dls.diskpath);
+    bl->dls.diskpath = 0;
+
+    if (rc) {
+        LOG(ERROR, "unable to detach locally attached disk");
+    }
+
     bl->callback(egc, bl, rc);
 }
 
@@ -275,6 +296,16 @@ static void bootloader_abort(libxl__egc *egc,
 
 /*----- main flow of control -----*/
 
+/* Callbacks */
+
+static void bootloader_disk_attached_cb(libxl__egc *egc,
+                                        libxl__disk_local_state *dls,
+                                        int rc);
+
+static void bootloader_disk_failed_cb(libxl__egc *egc,
+                                      libxl__disk_local_state *dls,
+                                      int rc);
+
 void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl)
 {
     STATE_AO_GC(bl->ao);
@@ -282,7 +313,6 @@ void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl)
     uint32_t domid = bl->domid;
     char *logfile_tmp = NULL;
     int rc, r;
-    const char *bootloader;
 
     libxl__bootloader_init(bl);
 
@@ -344,11 +374,32 @@ void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl)
         goto out;
     }
 
-    bl->diskpath = libxl__device_disk_local_attach(gc, bl->disk, &bl->localdisk,
-            info->blkdev_start);
-    if (!bl->diskpath) {
-        rc = ERROR_FAIL;
-        goto out;
+    libxl__device_disk_local_init(&bl->dls, ao, bl->disk, info->blkdev_start,
+                                  bootloader_disk_attached_cb);
+    libxl__device_disk_local_initiate_attach(egc, &bl->dls);
+    return;
+
+ out:
+    assert(rc);
+ out_ok:
+    free(logfile_tmp);
+    bootloader_callback(egc, bl, rc);
+}
+
+static void bootloader_disk_attached_cb(libxl__egc *egc,
+                                        libxl__disk_local_state *dls,
+                                        int rc)
+{
+    STATE_AO_GC(dls->ao);
+    libxl__bootloader_state *bl = CONTAINER_OF(dls, *bl, dls);
+    const libxl_domain_build_info *info = bl->info;
+    const char *bootloader;
+
+    if (rc) {
+        LOG(ERROR, "failed to attach local disk for bootloader execution");
+        dls->callback = bootloader_disk_failed_cb;
+        libxl__device_disk_local_initiate_detach(egc, dls);
+        return;
     }
 
     LOG(DEBUG, "Config bootloader value: %s", info->u.pv.bootloader);
@@ -389,11 +440,23 @@ void libxl__bootloader_run(libxl__egc *egc, libxl__bootloader_state *bl)
 
  out:
     assert(rc);
- out_ok:
-    free(logfile_tmp);
     bootloader_callback(egc, bl, rc);
 }
 
+static void bootloader_disk_failed_cb(libxl__egc *egc,
+                                      libxl__disk_local_state *dls,
+                                      int rc)
+{
+    STATE_AO_GC(dls->ao);
+    libxl__bootloader_state *bl = CONTAINER_OF(dls, *bl, dls);
+
+    if (rc) {
+        LOG(ERROR, "failed to detach locally attached disk");
+    }
+
+    bootloader_callback(egc, bl, ERROR_FAIL);
+}
+
 static void bootloader_gotptys(libxl__egc *egc, libxl__openpty_state *op)
 {
     libxl__bootloader_state *bl = CONTAINER_OF(op, *bl, openpty);
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 1befdc5..8e256d9 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1293,17 +1293,6 @@ _hidden int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
 _hidden int libxl__device_disk_add(libxl__gc *gc, uint32_t domid,
                                    libxl_device_disk *disk);
 
-/*
- * Make a disk available in this (the control) domain. Returns path to
- * a device.
- */
-_hidden char * libxl__device_disk_local_attach(libxl__gc *gc,
-        const libxl_device_disk *in_disk,
-        libxl_device_disk *new_disk,
-        const char *blkdev_start);
-_hidden int libxl__device_disk_local_detach(libxl__gc *gc,
-        libxl_device_disk *disk);
-
 _hidden char *libxl__uuid2string(libxl__gc *gc, const libxl_uuid uuid);
 
 struct libxl__xen_console_reader {
@@ -1823,6 +1812,56 @@ struct libxl__ao_devices {
 _hidden void libxl__initiate_device_remove(libxl__egc *egc,
                                            libxl__ao_device *aodev);
 
+/*----- local disk attach: attach a disk locally to run the bootloader -----*/
+
+typedef struct libxl__disk_local_state libxl__disk_local_state;
+typedef void libxl__disk_local_state_callback(libxl__egc*,
+                                              libxl__disk_local_state*,
+                                              int rc);
+
+/* A libxl__disk_local_state may be in the following states:
+ * Undefined, Idle, Attaching, Attached, Detaching, Detached.
+ */
+struct libxl__disk_local_state {
+    /* filled by the user */
+    libxl__ao *ao;
+    const libxl_device_disk *in_disk;
+    libxl_device_disk disk;
+    const char *blkdev_start;
+    libxl__disk_local_state_callback *callback;
+    /* filled by libxl__device_disk_local_initiate_attach */
+    char *diskpath;
+    /* private for implementation of local detach */
+    libxl__ao_device aodev;
+};
+
+/* Initializes the state of a libxl__disk_local_state to init,
+ * can be called multiple times.
+ * State Undefined -> Idle
+ */
+_hidden void libxl__device_disk_local_init(libxl__disk_local_state *dls,
+                                           libxl__ao *ao,
+                                           libxl_device_disk *disk,
+                                           const char *blkdev_start,
+                                           libxl__disk_local_state_callback
+                                           *callback);
+
+/* Make a disk available in this (the control) domain. Always calls
+ * dls->callback when finished.
+ * State Idle -> Attached
+ */
+_hidden void libxl__device_disk_local_initiate_attach(libxl__egc *egc,
+                                                libxl__disk_local_state *dls);
+
+/* Disconnects a disk device form the control domain. If the passed
+ * dls is not attached (or has already been detached),
+ * libxl__device_disk_local_initiate_detach will just call the callback
+ * directly.
+ * State Idle/Attached -> Idle
+ */
+_hidden void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
+                                                libxl__disk_local_state *dls);
+
 /*----- datacopier: copies data from one fd to another -----*/
 
 typedef struct libxl__datacopier_state libxl__datacopier_state;
@@ -2012,10 +2051,10 @@ struct libxl__bootloader_state {
     /* Should be zeroed by caller on entry.  Will be filled in by
      * bootloader machinery; represents the local attachment of the
      * disk for the benefit of the bootloader.  Must be detached by
-     * the caller using libxl__device_disk_local_detach, but only
+     * the caller using libxl__device_disk_local_initiate_detach, but only
      * after the domain's kernel and initramfs have been loaded into
      * memory and the file references disposed of. */
-    libxl_device_disk localdisk;
+    libxl__disk_local_state dls;
     uint32_t domid;
     /* outputs:
      *  - caller must initialise kernel and ramdisk to point to file
@@ -2027,7 +2066,6 @@ struct libxl__bootloader_state {
     const char *cmdline;
     /* private to libxl__run_bootloader */
     char *outputpath, *outputdir, *logfile;
-    char *diskpath; /* not from gc, represents actually attached disk */
     libxl__openpty_state openpty;
     libxl__openpty_result ptys[2];  /* [0] is for bootloader */
     libxl__ev_child child;
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 07/15] libxl: rename vifs to nics
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (5 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 06/15] libxl: convert libxl__device_disk_local_attach to an async op Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 08/15] libxl: convert libxl_device_disk_add to an async op Roger Pau Monne
                   ` (8 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

This change renames functions and struct values that used to contain
vifs in their names to nics, that provides a more clear name to
define network interfaces without referring to the backend that is
behind them.

This is not a functional change.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.h        |    4 +-
 tools/libxl/libxl_create.c |   10 ++++----
 tools/libxl/libxl_dm.c     |   54 ++++++++++++++++++++++----------------------
 tools/libxl/libxl_json.c   |    8 +++---
 tools/libxl/xl_cmdimpl.c   |   14 +++++-----
 tools/libxl/xl_sxp.c       |   20 ++++++++--------
 6 files changed, 55 insertions(+), 55 deletions(-)

diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index aef1be5..4b5e1ad 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -469,10 +469,10 @@ typedef struct {
     libxl_domain_create_info c_info;
     libxl_domain_build_info b_info;
 
-    int num_disks, num_vifs, num_pcidevs, num_vfbs, num_vkbs;
+    int num_disks, num_nics, num_pcidevs, num_vfbs, num_vkbs;
 
     libxl_device_disk *disks;
-    libxl_device_nic *vifs;
+    libxl_device_nic *nics;
     libxl_device_pci *pcidevs;
     libxl_device_vfb *vfbs;
     libxl_device_vkb *vkbs;
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index b54717a..afeee41 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -39,9 +39,9 @@ void libxl_domain_config_dispose(libxl_domain_config *d_config)
         libxl_device_disk_dispose(&d_config->disks[i]);
     free(d_config->disks);
 
-    for (i=0; i<d_config->num_vifs; i++)
-        libxl_device_nic_dispose(&d_config->vifs[i]);
-    free(d_config->vifs);
+    for (i=0; i<d_config->num_nics; i++)
+        libxl_device_nic_dispose(&d_config->nics[i]);
+    free(d_config->nics);
 
     for (i=0; i<d_config->num_pcidevs; i++)
         libxl_device_pci_dispose(&d_config->pcidevs[i]);
@@ -870,8 +870,8 @@ static void domcreate_rebuild_done(libxl__egc *egc,
             goto error_out;
         }
     }
-    for (i = 0; i < d_config->num_vifs; i++) {
-        ret = libxl_device_nic_add(ctx, domid, &d_config->vifs[i]);
+    for (i = 0; i < d_config->num_nics; i++) {
+        ret = libxl_device_nic_add(ctx, domid, &d_config->nics[i]);
         if (ret) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
                        "cannot add nic %d to domain: %d", i, ret);
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index 361b77d..06b68b7 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -102,10 +102,10 @@ static char ** libxl__build_device_model_args_old(libxl__gc *gc,
 {
     const libxl_domain_create_info *c_info = &guest_config->c_info;
     const libxl_domain_build_info *b_info = &guest_config->b_info;
-    const libxl_device_nic *vifs = guest_config->vifs;
+    const libxl_device_nic *nics = guest_config->nics;
     const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config);
     const libxl_sdl_info *sdl = dm_sdl(guest_config);
-    const int num_vifs = guest_config->num_vifs;
+    const int num_nics = guest_config->num_nics;
     const char *keymap = dm_keymap(guest_config);
     int i;
     flexarray_t *dm_args;
@@ -159,7 +159,7 @@ static char ** libxl__build_device_model_args_old(libxl__gc *gc,
         flexarray_vappend(dm_args, "-k", keymap, NULL);
     }
     if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
-        int ioemu_vifs = 0;
+        int ioemu_nics = 0;
         int nr_set_cpus = 0;
         char *s;
 
@@ -214,31 +214,31 @@ static char ** libxl__build_device_model_args_old(libxl__gc *gc,
                               libxl__sprintf(gc, "%s", s), NULL);
         free(s);
 
-        for (i = 0; i < num_vifs; i++) {
-            if (vifs[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
+        for (i = 0; i < num_nics; i++) {
+            if (nics[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
                 char *smac = libxl__sprintf(gc,
-                                   LIBXL_MAC_FMT, LIBXL_MAC_BYTES(vifs[i].mac));
+                                   LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
                 const char *ifname = libxl__device_nic_devname(gc,
-                                                domid, vifs[i].devid,
+                                                domid, nics[i].devid,
                                                 LIBXL_NIC_TYPE_IOEMU);
                 flexarray_vappend(dm_args,
                                   "-net",
                                   GCSPRINTF(
                                       "nic,vlan=%d,macaddr=%s,model=%s",
-                                      vifs[i].devid, smac, vifs[i].model),
+                                      nics[i].devid, smac, nics[i].model),
                                   "-net",
                                   GCSPRINTF(
                                       "tap,vlan=%d,ifname=%s,bridge=%s,"
                                       "script=%s,downscript=%s",
-                                      vifs[i].devid, ifname, vifs[i].bridge,
+                                      nics[i].devid, ifname, nics[i].bridge,
                                       libxl_tapif_script(gc),
                                       libxl_tapif_script(gc)),
                                   NULL);
-                ioemu_vifs++;
+                ioemu_nics++;
             }
         }
         /* If we have no emulated nics, tell qemu not to create any */
-        if ( ioemu_vifs == 0 ) {
+        if ( ioemu_nics == 0 ) {
             flexarray_vappend(dm_args, "-net", "none", NULL);
         }
         if (libxl_defbool_val(b_info->u.hvm.gfx_passthru)) {
@@ -330,9 +330,9 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     const libxl_domain_create_info *c_info = &guest_config->c_info;
     const libxl_domain_build_info *b_info = &guest_config->b_info;
     const libxl_device_disk *disks = guest_config->disks;
-    const libxl_device_nic *vifs = guest_config->vifs;
+    const libxl_device_nic *nics = guest_config->nics;
     const int num_disks = guest_config->num_disks;
-    const int num_vifs = guest_config->num_vifs;
+    const int num_nics = guest_config->num_nics;
     const libxl_vnc_info *vnc = libxl__dm_vnc(guest_config);
     const libxl_sdl_info *sdl = dm_sdl(guest_config);
     const char *keymap = dm_keymap(guest_config);
@@ -409,7 +409,7 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     }
 
     if (b_info->type == LIBXL_DOMAIN_TYPE_HVM) {
-        int ioemu_vifs = 0;
+        int ioemu_nics = 0;
 
         if (b_info->u.hvm.serial) {
             flexarray_vappend(dm_args, "-serial", b_info->u.hvm.serial, NULL);
@@ -468,30 +468,30 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                 flexarray_append(dm_args, libxl__sprintf(gc, "%d",
                                                          b_info->max_vcpus));
         }
-        for (i = 0; i < num_vifs; i++) {
-            if (vifs[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
+        for (i = 0; i < num_nics; i++) {
+            if (nics[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
                 char *smac = libxl__sprintf(gc,
-                                LIBXL_MAC_FMT, LIBXL_MAC_BYTES(vifs[i].mac));
+                                LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
                 const char *ifname = libxl__device_nic_devname(gc,
-                                                guest_domid, vifs[i].devid,
+                                                guest_domid, nics[i].devid,
                                                 LIBXL_NIC_TYPE_IOEMU);
                 flexarray_append(dm_args, "-device");
                 flexarray_append(dm_args,
                    libxl__sprintf(gc, "%s,id=nic%d,netdev=net%d,mac=%s",
-                                                vifs[i].model, vifs[i].devid,
-                                                vifs[i].devid, smac));
+                                                nics[i].model, nics[i].devid,
+                                                nics[i].devid, smac));
                 flexarray_append(dm_args, "-netdev");
                 flexarray_append(dm_args, GCSPRINTF(
                                           "type=tap,id=net%d,ifname=%s,"
                                           "script=%s,downscript=%s",
-                                          vifs[i].devid, ifname,
+                                          nics[i].devid, ifname,
                                           libxl_tapif_script(gc),
                                           libxl_tapif_script(gc)));
-                ioemu_vifs++;
+                ioemu_nics++;
             }
         }
         /* If we have no emulated nics, tell qemu not to create any */
-        if ( ioemu_vifs == 0 ) {
+        if ( ioemu_nics == 0 ) {
             flexarray_append(dm_args, "-net");
             flexarray_append(dm_args, "none");
         }
@@ -753,8 +753,8 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     dm_config->disks = guest_config->disks;
     dm_config->num_disks = guest_config->num_disks;
 
-    dm_config->vifs = guest_config->vifs;
-    dm_config->num_vifs = guest_config->num_vifs;
+    dm_config->nics = guest_config->nics;
+    dm_config->num_nics = guest_config->num_nics;
 
     ret = libxl__domain_create_info_setdefault(gc, &dm_config->c_info);
     if (ret) goto out;
@@ -827,8 +827,8 @@ retry_transaction:
         if (ret)
             goto out_free;
     }
-    for (i = 0; i < dm_config->num_vifs; i++) {
-        ret = libxl_device_nic_add(ctx, dm_domid, &dm_config->vifs[i]);
+    for (i = 0; i < dm_config->num_nics; i++) {
+        ret = libxl_device_nic_add(ctx, dm_domid, &dm_config->nics[i]);
         if (ret)
             goto out_free;
     }
diff --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c
index cd2719a..18f0dfd 100644
--- a/tools/libxl/libxl_json.c
+++ b/tools/libxl/libxl_json.c
@@ -822,15 +822,15 @@ yajl_gen_status libxl_domain_config_gen_json(yajl_gen hand,
     if (s != yajl_gen_status_ok)
         goto out;
 
-    s = yajl_gen_string(hand, (const unsigned char *)"vifs",
-                        sizeof("vifs")-1);
+    s = yajl_gen_string(hand, (const unsigned char *)"nics",
+                        sizeof("nics")-1);
     if (s != yajl_gen_status_ok)
         goto out;
     s = yajl_gen_array_open(hand);
     if (s != yajl_gen_status_ok)
         goto out;
-    for (i = 0; i < p->num_vifs; i++) {
-        s = libxl_device_nic_gen_json(hand, &p->vifs[i]);
+    for (i = 0; i < p->num_nics; i++) {
+        s = libxl_device_nic_gen_json(hand, &p->nics[i]);
         if (s != yajl_gen_status_ok)
             goto out;
     }
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index b3d8b7a..cf56839 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -920,17 +920,17 @@ static void parse_config_data(const char *config_source,
     }
 
     if (!xlu_cfg_get_list (config, "vif", &nics, 0, 0)) {
-        d_config->num_vifs = 0;
-        d_config->vifs = NULL;
-        while ((buf = xlu_cfg_get_listitem (nics, d_config->num_vifs)) != NULL) {
+        d_config->num_nics = 0;
+        d_config->nics = NULL;
+        while ((buf = xlu_cfg_get_listitem (nics, d_config->num_nics)) != NULL) {
             libxl_device_nic *nic;
             char *buf2 = strdup(buf);
             char *p, *p2;
 
-            d_config->vifs = (libxl_device_nic *) realloc(d_config->vifs, sizeof (libxl_device_nic) * (d_config->num_vifs+1));
-            nic = d_config->vifs + d_config->num_vifs;
+            d_config->nics = (libxl_device_nic *) realloc(d_config->nics, sizeof (libxl_device_nic) * (d_config->num_nics+1));
+            nic = d_config->nics + d_config->num_nics;
             libxl_device_nic_init(nic);
-            nic->devid = d_config->num_vifs;
+            nic->devid = d_config->num_nics;
 
             if (default_vifscript) {
                 free(nic->script);
@@ -1003,7 +1003,7 @@ static void parse_config_data(const char *config_source,
             } while ((p = strtok(NULL, ",")) != NULL);
 skip:
             free(buf2);
-            d_config->num_vifs++;
+            d_config->num_nics++;
         }
     }
 
diff --git a/tools/libxl/xl_sxp.c b/tools/libxl/xl_sxp.c
index 6e0a389..91f8f75 100644
--- a/tools/libxl/xl_sxp.c
+++ b/tools/libxl/xl_sxp.c
@@ -175,20 +175,20 @@ void printf_info_sexp(int domid, libxl_domain_config *d_config)
         printf("\t)\n");
     }
 
-    for (i = 0; i < d_config->num_vifs; i++) {
+    for (i = 0; i < d_config->num_nics; i++) {
         printf("\t(device\n");
         printf("\t\t(vif\n");
-        if (d_config->vifs[i].ifname)
-            printf("\t\t\t(vifname %s)\n", d_config->vifs[i].ifname);
-        printf("\t\t\t(backend_domid %d)\n", d_config->vifs[i].backend_domid);
+        if (d_config->nics[i].ifname)
+            printf("\t\t\t(vifname %s)\n", d_config->nics[i].ifname);
+        printf("\t\t\t(backend_domid %d)\n", d_config->nics[i].backend_domid);
         printf("\t\t\t(frontend_domid %d)\n", domid);
-        printf("\t\t\t(devid %d)\n", d_config->vifs[i].devid);
-        printf("\t\t\t(mtu %d)\n", d_config->vifs[i].mtu);
-        printf("\t\t\t(model %s)\n", d_config->vifs[i].model);
+        printf("\t\t\t(devid %d)\n", d_config->nics[i].devid);
+        printf("\t\t\t(mtu %d)\n", d_config->nics[i].mtu);
+        printf("\t\t\t(model %s)\n", d_config->nics[i].model);
         printf("\t\t\t(mac %02x%02x%02x%02x%02x%02x)\n",
-               d_config->vifs[i].mac[0], d_config->vifs[i].mac[1],
-               d_config->vifs[i].mac[2], d_config->vifs[i].mac[3],
-               d_config->vifs[i].mac[4], d_config->vifs[i].mac[5]);
+               d_config->nics[i].mac[0], d_config->nics[i].mac[1],
+               d_config->nics[i].mac[2], d_config->nics[i].mac[3],
+               d_config->nics[i].mac[4], d_config->nics[i].mac[5]);
         printf("\t\t)\n");
         printf("\t)\n");
     }
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 08/15] libxl: convert libxl_device_disk_add to an async op
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (6 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 07/15] libxl: rename vifs to nics Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 09/15] libxl: convert libxl_device_nic_add to an async operation Roger Pau Monne
                   ` (7 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

This patch converts libxl_device_disk_add to an ao operation that
waits for device backend to reach state XenbusStateInitWait and then
marks the operation as completed. This is not really useful now, but
will be used by later patches that will launch hotplug scripts after
we reached the desired xenbus state.

As usual, libxl_device_disk_add callers have been modified, and the
internal function libxl__device_disk_add has been used if the call was
inside an already running ao.

Changes since v6:

 * Fixed comments.

 * Rename libxl__initiate_device_connection to
   libxl__wait_device_connection.

 * Cancel backend devstate watch if local attach of disk fails.

Changes since v5:

 * Change the disk addition to use the new ao_devices structure.

 * Change name and comment of _initiate_device_add to
   _initiate_device_connect.

Changes since v4:

 * Added more comments about libxl__device_disk_add and
   libxl__add_disks.

 * Removed stray hunk in Makefile.

 * Fixed _prepare call libxl__add_disks (separate into a different
   loop).

 * Moved some code to check if disks have finished to a macro that
   generates a custom function, which will also be used for vif
   interfaces.

Changes since v2:

 * Remove state read from libxl__initiate_device_add

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c          |  130 ++++++++++++++++++++++++++++-------------
 tools/libxl/libxl.h          |    4 +-
 tools/libxl/libxl_create.c   |   41 ++++++++++---
 tools/libxl/libxl_device.c   |   64 +++++++++++++++++++++
 tools/libxl/libxl_dm.c       |   72 +++++++++++++++++-------
 tools/libxl/libxl_internal.h |   42 +++++++++++++-
 tools/libxl/xl_cmdimpl.c     |    2 +-
 7 files changed, 280 insertions(+), 75 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index b2a163e..94d15b2 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -1737,16 +1737,18 @@ int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
  * all other users should instead use the regular
  * libxl__device_disk_add wrapper
  */
-static int device_disk_add(libxl__gc *gc, uint32_t domid,
-                           libxl_device_disk *disk,
-                           char *(*fn)(libxl__gc *, const char *,
+static void device_disk_add(libxl__egc *egc, uint32_t domid,
+                            libxl_device_disk *disk,
+                            libxl__ao_device *aodev,
+                            char *(*fn)(libxl__gc *, const char *,
                                               xs_transaction_t),
-                           const char *blkdev_start)
+                            const char *blkdev_start)
 {
+    STATE_AO_GC(aodev->ao);
     flexarray_t *front = NULL;
     flexarray_t *back = NULL;
     char *dev;
-    libxl__device device;
+    libxl__device *device;
     int major, minor, rc;
     libxl_ctx *ctx = gc->owner;
     xs_transaction_t t = XBT_NULL;
@@ -1790,7 +1792,8 @@ static int device_disk_add(libxl__gc *gc, uint32_t domid,
             goto out_free;
         }
 
-        rc = libxl__device_from_disk(gc, domid, disk, &device);
+        GCNEW(device);
+        rc = libxl__device_from_disk(gc, domid, disk, device);
         if (rc != 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
                    " virtual disk identifier %s", disk->vdev);
@@ -1808,7 +1811,7 @@ static int device_disk_add(libxl__gc *gc, uint32_t domid,
                 flexarray_append(back, "params");
                 flexarray_append(back, dev);
 
-                assert(device.backend_kind == LIBXL__DEVICE_KIND_VBD);
+                assert(device->backend_kind == LIBXL__DEVICE_KIND_VBD);
                 break;
             case LIBXL_DISK_BACKEND_TAP:
                 dev = libxl__blktap_devpath(gc, disk->pdev_path, disk->format);
@@ -1829,7 +1832,7 @@ static int device_disk_add(libxl__gc *gc, uint32_t domid,
                 flexarray_append(back, "params");
                 flexarray_append(back, libxl__sprintf(gc, "%s:%s",
                               libxl__device_disk_string_of_format(disk->format), disk->pdev_path));
-                assert(device.backend_kind == LIBXL__DEVICE_KIND_QDISK);
+                assert(device->backend_kind == LIBXL__DEVICE_KIND_QDISK);
                 break;
             default:
                 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk backend type: %d\n", disk->backend);
@@ -1861,11 +1864,11 @@ static int device_disk_add(libxl__gc *gc, uint32_t domid,
         flexarray_append(front, "state");
         flexarray_append(front, libxl__sprintf(gc, "%d", 1));
         flexarray_append(front, "virtual-device");
-        flexarray_append(front, libxl__sprintf(gc, "%d", device.devid));
+        flexarray_append(front, libxl__sprintf(gc, "%d", device->devid));
         flexarray_append(front, "device-type");
         flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
 
-        libxl__device_generic_add(gc, t, &device,
+        libxl__device_generic_add(gc, t, device,
                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
                             libxl__xs_kvs_of_flexarray(gc, front, front->count));
 
@@ -1874,6 +1877,10 @@ static int device_disk_add(libxl__gc *gc, uint32_t domid,
         if (rc < 0) goto out_free;
     }
 
+    aodev->dev = device;
+    aodev->action = DEVICE_CONNECT;
+    libxl__wait_device_connection(egc, aodev);
+
     rc = 0;
 
 out_free:
@@ -1881,21 +1888,30 @@ out_free:
     flexarray_free(front);
 out:
     libxl__xs_transaction_abort(gc, &t);
-    return rc;
+    aodev->rc = rc;
+    if (rc) aodev->callback(egc, aodev);
+    return;
 }
 
-int libxl__device_disk_add(libxl__gc *gc, uint32_t domid,
-                           libxl_device_disk *disk)
+void libxl__device_disk_add(libxl__egc *egc, uint32_t domid,
+                           libxl_device_disk *disk, libxl__ao_device *aodev)
 {
-    return device_disk_add(gc, domid, disk, NULL, NULL);
+    device_disk_add(egc, domid, disk, aodev, NULL, NULL);
 }
 
-int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
+int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid,
+                          libxl_device_disk *disk,
+                          const libxl_asyncop_how *ao_how)
 {
-    GC_INIT(ctx);
-    int rc = libxl__device_disk_add(gc, domid, disk);
-    GC_FREE;
-    return rc;
+    AO_CREATE(ctx, domid, ao_how);
+    libxl__ao_device *aodev;
+
+    GCNEW(aodev);
+    libxl__prepare_ao_device(ao, aodev);
+    aodev->callback = device_addrm_aocomplete;
+    libxl__device_disk_add(egc, domid, disk, aodev);
+
+    return AO_INPROGRESS;
 }
 
 static void libxl__device_disk_from_xs_be(libxl__gc *gc,
@@ -2098,11 +2114,13 @@ int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
     ret = 0;
 
     libxl_device_disk_remove(ctx, domid, disks + i, 0);
-    libxl_device_disk_add(ctx, domid, disk);
+    /* fixme-ao */
+    libxl_device_disk_add(ctx, domid, disk, 0);
     stubdomid = libxl_get_stubdom_id(ctx, domid);
     if (stubdomid) {
         libxl_device_disk_remove(ctx, stubdomid, disks + i, 0);
-        libxl_device_disk_add(ctx, stubdomid, disk);
+        /* fixme-ao */
+        libxl_device_disk_add(ctx, stubdomid, disk, 0);
     }
 out:
     for (i = 0; i < num; i++)
@@ -2154,15 +2172,17 @@ void libxl__device_disk_local_init(libxl__disk_local_state *dls, libxl__ao *ao,
     dls->callback = callback;
 }
 
+/* Callbacks */
+
+static void local_device_attach_cb(libxl__egc *egc, libxl__ao_device *aodev);
+
 void libxl__device_disk_local_initiate_attach(libxl__egc *egc,
                                      libxl__disk_local_state *dls)
 {
     STATE_AO_GC(dls->ao);
     libxl_ctx *ctx = CTX;
-    char *dev = NULL, *be_path = NULL;
+    char *dev = NULL;
     int rc;
-    libxl__device device;
-    xs_transaction_t t = XBT_NULL;
     const libxl_device_disk *in_disk = dls->in_disk;
     libxl_device_disk *disk = &dls->disk;
     const char *blkdev_start = dls->blkdev_start;
@@ -2209,13 +2229,12 @@ void libxl__device_disk_local_initiate_attach(libxl__egc *egc,
             break;
         case LIBXL_DISK_BACKEND_QDISK:
             if (disk->format != LIBXL_DISK_FORMAT_RAW) {
-                if (device_disk_add(gc, LIBXL_TOOLSTACK_DOMID, disk,
-                                    libxl__alloc_vdev, blkdev_start)) {
-                    LOG(ERROR, "libxl_device_disk_add failed");
-                    rc = ERROR_FAIL;
-                    goto out;
-                }
-                dev = GCSPRINTF("/dev/%s", disk->vdev);
+                libxl__prepare_ao_device(ao, &dls->aodev);
+                dls->aodev.callback = local_device_attach_cb;
+                device_disk_add(egc, LIBXL_TOOLSTACK_DOMID, disk,
+                                &dls->aodev, libxl__alloc_vdev,
+                                blkdev_start);
+                return;
             } else {
                 dev = disk->pdev_path;
             }
@@ -2228,15 +2247,46 @@ void libxl__device_disk_local_initiate_attach(libxl__egc *egc,
             break;
     }
 
-    if (disk->vdev != NULL) {
-        rc = libxl__device_from_disk(gc, LIBXL_TOOLSTACK_DOMID, disk, &device);
-        if (rc < 0)
-            goto out;
-        be_path = libxl__device_backend_path(gc, &device);
-        rc = libxl__wait_for_backend(gc, be_path, "4");
-        if (rc < 0)
-            goto out;
+    if (dev != NULL)
+        dls->diskpath = strdup(dev);
+
+    dls->callback(egc, dls, 0);
+    return;
+
+ out:
+    assert(rc);
+    dls->callback(egc, dls, rc);
+}
+
+static void local_device_attach_cb(libxl__egc *egc, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    libxl__disk_local_state *dls = CONTAINER_OF(aodev, *dls, aodev);
+    char *dev = NULL, *be_path = NULL;
+    int rc;
+    libxl__device device;
+    libxl_device_disk *disk = &dls->disk;
+
+    rc = aodev->rc;
+    if (rc) {
+        LOGE(ERROR, "unable to %s %s with id %u",
+                    aodev->action == DEVICE_CONNECT ? "add" : "remove",
+                    libxl__device_kind_to_string(aodev->dev->kind),
+                    aodev->dev->devid);
+        goto out;
     }
+
+    dev = GCSPRINTF("/dev/%s", disk->vdev);
+    LOG(DEBUG, "locally attaching qdisk %s", dev);
+
+    rc = libxl__device_from_disk(gc, LIBXL_TOOLSTACK_DOMID, disk, &device);
+    if (rc < 0)
+        goto out;
+    be_path = libxl__device_backend_path(gc, &device);
+    rc = libxl__wait_for_backend(gc, be_path, "4");
+    if (rc < 0)
+        goto out;
+
     if (dev != NULL)
         dls->diskpath = strdup(dev);
 
@@ -2245,8 +2295,6 @@ void libxl__device_disk_local_initiate_attach(libxl__egc *egc,
 
  out:
     assert(rc);
-    if (t != XBT_NULL)
-        xs_transaction_end(ctx->xsh, t, 1);
     dls->callback(egc, dls, rc);
 }
 
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 4b5e1ad..7516e99 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -667,7 +667,9 @@ void libxl_vcpuinfo_list_free(libxl_vcpuinfo *, int nr_vcpus);
  */
 
 /* Disks */
-int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk);
+int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid,
+                          libxl_device_disk *disk,
+                          const libxl_asyncop_how *ao_how);
 int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid,
                              libxl_device_disk *disk,
                              const libxl_asyncop_how *ao_how);
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index afeee41..c6d1a10 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -558,6 +558,10 @@ static void domcreate_bootloader_console_available(libxl__egc *egc,
 static void domcreate_bootloader_done(libxl__egc *egc,
                                       libxl__bootloader_state *bl,
                                       int rc);
+
+static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                int rc);
+
 static void domcreate_console_available(libxl__egc *egc,
                                         libxl__domain_create_state *dcs);
 
@@ -845,12 +849,10 @@ static void domcreate_rebuild_done(libxl__egc *egc,
                                    int ret)
 {
     STATE_AO_GC(dcs->ao);
-    int i;
 
     /* convenience aliases */
     const uint32_t domid = dcs->guest_domid;
     libxl_domain_config *const d_config = dcs->guest_config;
-    libxl__domain_build_state *const state = &dcs->build_state;
     libxl_ctx *const ctx = CTX;
 
     if (ret) {
@@ -861,14 +863,33 @@ static void domcreate_rebuild_done(libxl__egc *egc,
 
     store_libxl_entry(gc, domid, &d_config->b_info);
 
-    for (i = 0; i < d_config->num_disks; i++) {
-        ret = libxl_device_disk_add(ctx, domid, &d_config->disks[i]);
-        if (ret) {
-            LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
-                       "cannot add disk %d to domain: %d", i, ret);
-            ret = ERROR_FAIL;
-            goto error_out;
-        }
+    dcs->aodevs.size = d_config->num_disks;
+    libxl__add_disks(egc, ao, domid, d_config, &dcs->aodevs,
+                     domcreate_launch_dm);
+
+    return;
+
+ error_out:
+    assert(ret);
+    domcreate_complete(egc, dcs, ret);
+}
+
+static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                int rc)
+{
+    libxl__domain_create_state *dcs = CONTAINER_OF(aodevs, *dcs, aodevs);
+    STATE_AO_GC(dcs->ao);
+    int i, ret = 0;
+
+    /* convenience aliases */
+    const uint32_t domid = dcs->guest_domid;
+    libxl_domain_config *const d_config = dcs->guest_config;
+    libxl__domain_build_state *const state = &dcs->build_state;
+    libxl_ctx *const ctx = CTX;
+
+    if (rc) {
+        LOG(ERROR, "unable to add disk devices");
+        goto error_out;
     }
     for (i = 0; i < d_config->num_nics; i++) {
         ret = libxl_device_nic_add(ctx, domid, &d_config->nics[i]);
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 8daf69d..9438a82 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -442,6 +442,40 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
     return;
 }
 
+/******************************************************************************/
+
+/* Macro for defining the functions that will add a bunch of disks when
+ * inside an async op.
+ *
+ * This macro is added to prevent repetition of code.
+ */
+
+#define DEFINE_DEVICES_ADD(type)                                              \
+    void libxl__add_##type##s(libxl__egc *egc, libxl__ao *ao, uint32_t domid, \
+                              libxl_domain_config *d_config,                  \
+                              libxl__ao_devices *aodevs,                      \
+                              libxl__devices_callback *callback)              \
+    {                                                                         \
+        AO_GC;                                                                \
+        int i;                                                                \
+        aodevs->size = d_config->num_##type##s;                               \
+        aodevs->callback = callback;                                          \
+        libxl__prepare_ao_devices(ao, aodevs);                                \
+        aodevs->callback = callback;                                          \
+                                                                              \
+        for (i = 0; i < aodevs->size; i++) {                                  \
+            aodevs->array[i].callback = libxl__ao_devices_callback;           \
+            libxl__device_##type##_add(egc, domid, &d_config->type##s[i],     \
+                                       &aodevs->array[i]);                    \
+        }                                                                     \
+    }
+
+DEFINE_DEVICES_ADD(disk)
+
+#undef DEFINE_DEVICES_ADD
+
+/******************************************************************************/
+
 int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
@@ -566,6 +600,36 @@ static void device_backend_cleanup(libxl__gc *gc,
 
 static void device_xsentries_remove(libxl__egc *egc, libxl__ao_device *aodev);
 
+void libxl__wait_device_connection(libxl__egc *egc, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    char *be_path = libxl__device_backend_path(gc, aodev->dev);
+    char *state_path = libxl__sprintf(gc, "%s/state", be_path);
+    int rc = 0;
+
+    if (aodev->dev->backend_kind == LIBXL__DEVICE_KIND_QDISK) {
+        aodev->callback(egc, aodev);
+        return;
+    }
+
+    rc = libxl__ev_devstate_wait(gc, &aodev->backend_ds,
+                                 device_backend_callback,
+                                 state_path, XenbusStateInitWait,
+                                 LIBXL_INIT_TIMEOUT * 1000);
+    if (rc) {
+        LOGE(ERROR, "unable to initialize device %s", be_path);
+        goto out_fail;
+    }
+
+    return;
+
+out_fail:
+    assert(rc);
+    aodev->rc = rc;
+    device_xsentries_remove(egc, aodev);
+    return;
+}
+
 void libxl__initiate_device_remove(libxl__egc *egc,
                                    libxl__ao_device *aodev)
 {
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index 06b68b7..30b7556 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -694,6 +694,9 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
                                 libxl__dm_spawn_state *stubdom_dmss,
                                 int rc);
 
+static void spawn_stub_launch_dm(libxl__egc *egc,
+                                 libxl__ao_devices *aodevs, int rc);
+
 static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc,
                                            libxl__destroy_domid_state *dis,
                                            int rc);
@@ -702,10 +705,9 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
 {
     STATE_AO_GC(sdss->dm.spawn.ao);
     libxl_ctx *ctx = libxl__gc_owner(gc);
-    int i, num_console = STUBDOM_SPECIAL_CONSOLES, ret;
-    libxl__device_console *console;
-    libxl_device_vfb vfb;
-    libxl_device_vkb vkb;
+    int ret;
+    libxl_device_vfb *vfb;
+    libxl_device_vkb *vkb;
     char **args;
     struct xs_permissions perm[2];
     xs_transaction_t t;
@@ -761,10 +763,12 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     ret = libxl__domain_build_info_setdefault(gc, &dm_config->b_info);
     if (ret) goto out;
 
-    libxl__vfb_and_vkb_from_hvm_guest_config(gc, guest_config, &vfb, &vkb);
-    dm_config->vfbs = &vfb;
+    GCNEW(vfb);
+    GCNEW(vkb);
+    libxl__vfb_and_vkb_from_hvm_guest_config(gc, guest_config, vfb, vkb);
+    dm_config->vfbs = vfb;
     dm_config->num_vfbs = 1;
-    dm_config->vkbs = &vkb;
+    dm_config->vkbs = vkb;
     dm_config->num_vkbs = 1;
 
     stubdom_state->pv_kernel.path
@@ -822,22 +826,53 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
 
-    for (i = 0; i < dm_config->num_disks; i++) {
-        ret = libxl_device_disk_add(ctx, dm_domid, &dm_config->disks[i]);
-        if (ret)
-            goto out_free;
-    }
+    sdss->aodevs.size = dm_config->num_disks;
+    libxl__add_disks(egc, ao, dm_domid, dm_config, &sdss->aodevs,
+                     spawn_stub_launch_dm);
+
+    free(args);
+    return;
+
+out_free:
+    free(args);
+out:
+    assert(ret);
+    spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, ret);
+}
+
+static void spawn_stub_launch_dm(libxl__egc *egc,
+                                 libxl__ao_devices *aodevs, int rc)
+{
+    libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(aodevs, *sdss, aodevs);
+    STATE_AO_GC(sdss->dm.spawn.ao);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+    int i, num_console = STUBDOM_SPECIAL_CONSOLES, ret = 0;
+    libxl__device_console *console;
+
+    /* convenience aliases */
+    libxl_domain_config *const dm_config = &sdss->dm_config;
+    libxl_domain_config *const guest_config = sdss->dm.guest_config;
+    const int guest_domid = sdss->dm.guest_domid;
+    libxl__domain_build_state *const d_state = sdss->dm.build_state;
+    libxl__domain_build_state *const stubdom_state = &sdss->dm_state;
+    uint32_t dm_domid = sdss->pvqemu.guest_domid;
+
+    if (rc) {
+        LOG(ERROR, "error connecting disk devices");
+        goto out;
+     }
+
     for (i = 0; i < dm_config->num_nics; i++) {
         ret = libxl_device_nic_add(ctx, dm_domid, &dm_config->nics[i]);
         if (ret)
-            goto out_free;
+            goto out;
     }
     ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]);
     if (ret)
-        goto out_free;
+        goto out;
     ret = libxl_device_vkb_add(ctx, dm_domid, &dm_config->vkbs[0]);
     if (ret)
-        goto out_free;
+        goto out;
 
     if (guest_config->b_info.u.hvm.serial)
         num_console++;
@@ -845,7 +880,7 @@ retry_transaction:
     console = libxl__calloc(gc, num_console, sizeof(libxl__device_console));
     if (!console) {
         ret = ERROR_NOMEM;
-        goto out_free;
+        goto out;
     }
 
     for (i = 0; i < num_console; i++) {
@@ -881,7 +916,7 @@ retry_transaction:
         ret = libxl__device_console_add(gc, dm_domid, &console[i],
                         i == STUBDOM_CONSOLE_LOGGING ? stubdom_state : NULL);
         if (ret)
-            goto out_free;
+            goto out;
     }
 
     sdss->pvqemu.spawn.ao = ao;
@@ -892,11 +927,8 @@ retry_transaction:
 
     libxl__spawn_local_dm(egc, &sdss->pvqemu);
 
-    free(args);
     return;
 
-out_free:
-    free(args);
 out:
     assert(ret);
     spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, ret);
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 8e256d9..90fa19a 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -72,6 +72,7 @@
 #include "_libxl_types_internal.h"
 #include "_libxl_types_internal_json.h"
 
+#define LIBXL_INIT_TIMEOUT 10
 #define LIBXL_DESTROY_TIMEOUT 10
 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
 #define LIBXL_XENCONSOLE_LIMIT 1048576
@@ -1290,8 +1291,6 @@ _hidden void libxl__device_destroy_tapdisk(libxl__gc *gc, char *be_path);
 _hidden int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
                                    libxl_device_disk *disk,
                                    libxl__device *device);
-_hidden int libxl__device_disk_add(libxl__gc *gc, uint32_t domid,
-                                   libxl_device_disk *disk);
 
 _hidden char *libxl__uuid2string(libxl__gc *gc, const libxl_uuid uuid);
 
@@ -1800,6 +1799,28 @@ struct libxl__ao_devices {
     libxl__devices_callback *callback;
 };
 
+/* AO operation to connect a disk device, called by
+ * libxl_device_disk_add and libxl__add_disks. This function calls
+ * libxl__wait_device_connection to wait for the device to
+ * finish the connection (might involve executing hotplug scripts).
+ *
+ * Once finished, aodev->callback will be executed.
+ */
+_hidden void libxl__device_disk_add(libxl__egc *egc, uint32_t domid,
+                                    libxl_device_disk *disk,
+                                    libxl__ao_device *aodev);
+
+/* Waits for the passed device to reach state XenbusStateInitWait.
+ * This is not really useful by itself, but is important when executing
+ * hotplug scripts, since we need to be sure the device is in the correct
+ * state before executing them.
+ *
+ * Once finished, aodev->callback will be executed.
+ */
+_hidden void libxl__wait_device_connection(libxl__egc*,
+                                           libxl__ao_device *aodev);
+
+
 /* Arranges that dev will be removed to the guest, and the
  * hotplug scripts will be executed (if necessary). When
  * this is done (or an error happens), the callback in
@@ -1808,6 +1829,8 @@ struct libxl__ao_devices {
  * The libxl__ao_device passed to this function should be
  * prepared using libxl__prepare_ao_device prior to calling
  * this function.
+ *
+ * Once finished, aodev->callback will be executed.
  */
 _hidden void libxl__initiate_device_remove(libxl__egc *egc,
                                            libxl__ao_device *aodev);
@@ -2162,6 +2185,19 @@ _hidden void libxl__destroy_domid(libxl__egc *egc,
 _hidden void libxl__devices_destroy(libxl__egc *egc,
                                     libxl__devices_remove_state *drs);
 
+/* Helper function to add a bunch of disks. This should be used when
+ * the caller is inside an async op. "devices" will be prepared by this
+ * function, so there's no need to call _prepare before calling this
+ * function.
+ *
+ * The "callback" will be called for each device, and the user is responsible
+ * for calling libxl__ao_device_check_last on the callback.
+ */
+void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
+                      libxl_domain_config *d_config,
+                      libxl__ao_devices *aodevs,
+                      libxl__devices_callback *callback);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
@@ -2196,6 +2232,7 @@ typedef struct {
     libxl__domain_build_state dm_state;
     libxl__dm_spawn_state pvqemu;
     libxl__destroy_domid_state dis;
+    libxl__ao_devices aodevs;
 } libxl__stub_dm_spawn_state;
 
 _hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*);
@@ -2225,6 +2262,7 @@ struct libxl__domain_create_state {
     libxl__save_helper_state shs;
     /* necessary if the domain creation failed and we have to destroy it */
     libxl__domain_destroy_state dds;
+    libxl__ao_devices aodevs;
 };
 
 /*----- Domain suspend (save) functions -----*/
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index cf56839..11ad0e8 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -5320,7 +5320,7 @@ int main_blockattach(int argc, char **argv)
         return 0;
     }
 
-    if (libxl_device_disk_add(ctx, fe_domid, &disk)) {
+    if (libxl_device_disk_add(ctx, fe_domid, &disk, 0)) {
         fprintf(stderr, "libxl_device_disk_add failed.\n");
     }
     return 0;
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 09/15] libxl: convert libxl_device_nic_add to an async operation
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (7 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 08/15] libxl: convert libxl_device_disk_add to an async op Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 10/15] libxl: add option to choose who executes hotplug scripts Roger Pau Monne
                   ` (6 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

This patch converts libxl_device_nic_add to an ao operation that
waits for device backend to reach state XenbusStateInitWait and then
marks the operation as completed. This is not really useful now, but
will be used by latter patches that will launch hotplug scripts after
we reached the desired xenbus state.

Calls to libxl_device_nic_add have also been moved to occur after the
device model has been launched, so when hotplug scripts are called
from this functions the interfaces already exists.

As usual, libxl_device_nic_add callers have been modified, and the
internal function libxl__device_disk_add has been used if the call was
inside an already running ao.

Changes since v5:

 * Updated to use the new ao_devices struct.

 * Renamed ao_device in nic_add to aodev.

Changes since v4:

 * Used the macro defined in previous patch to define the generic
   callback to wait for nics to be connected.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c          |   39 +++++++++++++++++++++++++------
 tools/libxl/libxl.h          |    3 +-
 tools/libxl/libxl_create.c   |   51 +++++++++++++++++++++++++++++++++++------
 tools/libxl/libxl_device.c   |    1 +
 tools/libxl/libxl_dm.c       |   37 ++++++++++++++++++++++++++++--
 tools/libxl/libxl_internal.h |   18 ++++++++++++++
 tools/libxl/xl_cmdimpl.c     |    2 +-
 7 files changed, 130 insertions(+), 21 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 94d15b2..038e340 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -2417,12 +2417,27 @@ static int libxl__device_from_nic(libxl__gc *gc, uint32_t domid,
     return 0;
 }
 
-int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
+int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic,
+                         const libxl_asyncop_how *ao_how)
 {
-    GC_INIT(ctx);
+    AO_CREATE(ctx, domid, ao_how);
+    libxl__ao_device *aodev;
+
+    GCNEW(aodev);
+    libxl__prepare_ao_device(ao, aodev);
+    aodev->callback = device_addrm_aocomplete;
+    libxl__device_nic_add(egc, domid, nic, aodev);
+
+    return AO_INPROGRESS;
+}
+
+void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
+                           libxl_device_nic *nic, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
     flexarray_t *front;
     flexarray_t *back;
-    libxl__device device;
+    libxl__device *device;
     char *dompath, **l;
     unsigned int nb, rc;
 
@@ -2453,7 +2468,8 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
         }
     }
 
-    rc = libxl__device_from_nic(gc, domid, nic, &device);
+    GCNEW(device);
+    rc = libxl__device_from_nic(gc, domid, nic, device);
     if ( rc != 0 ) goto out_free;
 
     flexarray_append(back, "frontend-id");
@@ -2494,6 +2510,9 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
     flexarray_append(back, libxl__strdup(gc, nic->bridge));
     flexarray_append(back, "handle");
     flexarray_append(back, libxl__sprintf(gc, "%d", nic->devid));
+    flexarray_append(back, "type");
+    flexarray_append(back, libxl__strdup(gc,
+                                     libxl_nic_type_to_string(nic->nictype)));
 
     flexarray_append(front, "backend-id");
     flexarray_append(front, libxl__sprintf(gc, "%d", nic->backend_domid));
@@ -2504,18 +2523,22 @@ int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
     flexarray_append(front, "mac");
     flexarray_append(front, libxl__sprintf(gc,
                                     LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nic->mac)));
-    libxl__device_generic_add(gc, XBT_NULL, &device,
+    libxl__device_generic_add(gc, XBT_NULL, device,
                              libxl__xs_kvs_of_flexarray(gc, back, back->count),
                              libxl__xs_kvs_of_flexarray(gc, front, front->count));
 
-    /* FIXME: wait for plug */
+    aodev->dev = device;
+    aodev->action = DEVICE_CONNECT;
+    libxl__wait_device_connection(egc, aodev);
+
     rc = 0;
 out_free:
     flexarray_free(back);
     flexarray_free(front);
 out:
-    GC_FREE;
-    return rc;
+    aodev->rc = rc;
+    if (rc) aodev->callback(egc, aodev);
+    return;
 }
 
 static void libxl__device_nic_from_xs_be(libxl__gc *gc,
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 7516e99..d7ae6e4 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -688,7 +688,8 @@ int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk);
 
 /* Network Interfaces */
-int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic);
+int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic,
+                         const libxl_asyncop_how *ao_how);
 int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_nic *nic,
                             const libxl_asyncop_how *ao_how);
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index c6d1a10..6f4c865 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -562,6 +562,9 @@ static void domcreate_bootloader_done(libxl__egc *egc,
 static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
                                 int rc);
 
+static void domcreate_attach_pci(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                 int rc);
+
 static void domcreate_console_available(libxl__egc *egc,
                                         libxl__domain_create_state *dcs);
 
@@ -891,14 +894,13 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
         LOG(ERROR, "unable to add disk devices");
         goto error_out;
     }
+
     for (i = 0; i < d_config->num_nics; i++) {
-        ret = libxl_device_nic_add(ctx, domid, &d_config->nics[i]);
-        if (ret) {
-            LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
-                       "cannot add nic %d to domain: %d", i, ret);
-            ret = ERROR_FAIL;
-            goto error_out;
-        }
+        /* We have to init the nic here, because we still haven't
+         * called libxl_device_nic_add at this point, but qemu needs
+         * the nic information to be complete.
+         */
+        libxl__device_nic_setdefault(gc, &d_config->nics[i]);
     }
     switch (d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
@@ -971,7 +973,6 @@ static void domcreate_devmodel_started(libxl__egc *egc,
 {
     libxl__domain_create_state *dcs = CONTAINER_OF(dmss, *dcs, dmss.dm);
     STATE_AO_GC(dmss->spawn.ao);
-    int i;
     libxl_ctx *ctx = CTX;
     int domid = dcs->guest_domid;
 
@@ -991,6 +992,40 @@ static void domcreate_devmodel_started(libxl__egc *egc,
         }
     }
 
+    /* Plug nic interfaces */
+    if (d_config->num_nics > 0) {
+        /* Attach nics */
+        dcs->aodevs.size = d_config->num_nics;
+        libxl__add_nics(egc, ao, domid, d_config, &dcs->aodevs,
+                        domcreate_attach_pci);
+        return;
+    }
+
+    domcreate_attach_pci(egc, &dcs->aodevs, 0);
+    return;
+
+error_out:
+    assert(ret);
+    domcreate_complete(egc, dcs, ret);
+}
+
+static void domcreate_attach_pci(libxl__egc *egc, libxl__ao_devices *aodevs,
+                                 int rc)
+{
+    libxl__domain_create_state *dcs = CONTAINER_OF(aodevs, *dcs, aodevs);
+    STATE_AO_GC(dcs->ao);
+    int i, ret = 0;
+    libxl_ctx *ctx = CTX;
+    int domid = dcs->guest_domid;
+
+    /* convenience aliases */
+    libxl_domain_config *const d_config = dcs->guest_config;
+
+    if (rc) {
+        LOG(ERROR, "unable to add nic devices");
+        goto error_out;
+    }
+
     for (i = 0; i < d_config->num_pcidevs; i++)
         libxl__device_pci_add(gc, domid, &d_config->pcidevs[i], 1);
 
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 9438a82..13f9ecb 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -471,6 +471,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
     }
 
 DEFINE_DEVICES_ADD(disk)
+DEFINE_DEVICES_ADD(nic)
 
 #undef DEFINE_DEVICES_ADD
 
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index 30b7556..b4a5f1f 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -697,6 +697,10 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
 static void spawn_stub_launch_dm(libxl__egc *egc,
                                  libxl__ao_devices *aodevs, int rc);
 
+static void stubdom_pvqemu_cb(libxl__egc *egc,
+                              libxl__ao_devices *aodevs,
+                              int rc);
+
 static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc,
                                            libxl__destroy_domid_state *dis,
                                            int rc);
@@ -863,9 +867,11 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
      }
 
     for (i = 0; i < dm_config->num_nics; i++) {
-        ret = libxl_device_nic_add(ctx, dm_domid, &dm_config->nics[i]);
-        if (ret)
-            goto out;
+         /* We have to init the nic here, because we still haven't
+         * called libxl_device_nic_add at this point, but qemu needs
+         * the nic information to be complete.
+         */
+        libxl__device_nic_setdefault(gc, &dm_config->nics[i]);
     }
     ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]);
     if (ret)
@@ -942,9 +948,34 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
         CONTAINER_OF(stubdom_dmss, *sdss, pvqemu);
     STATE_AO_GC(sdss->dm.spawn.ao);
     uint32_t dm_domid = sdss->pvqemu.guest_domid;
+    libxl_domain_config *d_config = stubdom_dmss->guest_config;
 
     if (rc) goto out;
 
+    if (d_config->num_nics > 0) {
+        sdss->aodevs.size = d_config->num_nics;
+        libxl__add_nics(egc, ao, dm_domid, d_config, &sdss->aodevs,
+                        stubdom_pvqemu_cb);
+        return;
+    }
+
+out:
+    stubdom_pvqemu_cb(egc, &sdss->aodevs, rc);
+}
+
+static void stubdom_pvqemu_cb(libxl__egc *egc,
+                              libxl__ao_devices *aodevs,
+                              int rc)
+{
+    libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(aodevs, *sdss, aodevs);
+    STATE_AO_GC(sdss->dm.spawn.ao);
+    uint32_t dm_domid = sdss->pvqemu.guest_domid;
+
+    if (rc) {
+        LOGE(ERROR, "error connecting nics devices");
+        goto out;
+    }
+
     rc = libxl_domain_unpause(CTX, dm_domid);
     if (rc) goto out;
 
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 90fa19a..df2db10 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1810,6 +1810,11 @@ _hidden void libxl__device_disk_add(libxl__egc *egc, uint32_t domid,
                                     libxl_device_disk *disk,
                                     libxl__ao_device *aodev);
 
+/* AO operation to connect a nic device */
+_hidden void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
+                                   libxl_device_nic *nic,
+                                   libxl__ao_device *aodev);
+
 /* Waits for the passed device to reach state XenbusStateInitWait.
  * This is not really useful by itself, but is important when executing
  * hotplug scripts, since we need to be sure the device is in the correct
@@ -2198,6 +2203,19 @@ void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
                       libxl__ao_devices *aodevs,
                       libxl__devices_callback *callback);
 
+/* Helper function to add a bunch of nics. This should be used when
+ * the caller is inside an async op. "devices" will be prepared by this
+ * function, so there's no need to call _prepare before calling this
+ * function.
+ *
+ * The "callback" will be called for each device, and the user is responsible
+ * for calling libxl__ao_device_check_last on the callback.
+ */
+void libxl__add_nics(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
+                     libxl_domain_config *d_config,
+                     libxl__ao_devices *aodevs,
+                     libxl__devices_callback *callback);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 11ad0e8..4c3cabd 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -5209,7 +5209,7 @@ int main_networkattach(int argc, char **argv)
         return 0;
     }
 
-    if (libxl_device_nic_add(ctx, domid, &nic)) {
+    if (libxl_device_nic_add(ctx, domid, &nic, 0)) {
         fprintf(stderr, "libxl_device_nic_add failed.\n");
         return 1;
     }
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 10/15] libxl: add option to choose who executes hotplug scripts
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (8 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 09/15] libxl: convert libxl_device_nic_add to an async operation Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-06 10:28   ` Ian Campbell
  2012-07-04 11:59 ` [PATCH v8 11/15] libxl: rename _IOEMU nic type to VIF_IOEMU Roger Pau Monne
                   ` (5 subsequent siblings)
  15 siblings, 1 reply; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Add and option to xl.conf file to decide if hotplug scripts are
executed from the toolstack (xl) or from udev as it used to be in the
past.

This option is only introduced in this patch, but it has no effect
since the code to call hotplug scripts from libxl is introduced in a
latter patch.

This choice will be saved in "libxl/disable_udev", as specified in the
DISABLE_UDEV_PATH constant.

Changes since v2:

 * Change atoi(...) to !!atoi(...) to prevent returning negative
   values from xenstore (which will be handled as errors).

 * Check for errors on the return value of libxl__hotplug_settings.

Changes since v1:

 * Used an auxiliary function to check for the current setting.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 docs/man/xl.conf.pod.5       |    8 ++++++++
 tools/examples/xl.conf       |    5 +++++
 tools/libxl/libxl_create.c   |   38 +++++++++++++++++++++++++++++++++++++-
 tools/libxl/libxl_dm.c       |    4 ++++
 tools/libxl/libxl_internal.c |   19 +++++++++++++++++++
 tools/libxl/libxl_internal.h |    3 +++
 tools/libxl/libxl_types.idl  |    1 +
 tools/libxl/xl.c             |    4 ++++
 tools/libxl/xl.h             |    1 +
 tools/libxl/xl_cmdimpl.c     |    1 +
 10 files changed, 83 insertions(+), 1 deletions(-)

diff --git a/docs/man/xl.conf.pod.5 b/docs/man/xl.conf.pod.5
index 149430c..23932be 100644
--- a/docs/man/xl.conf.pod.5
+++ b/docs/man/xl.conf.pod.5
@@ -55,6 +55,14 @@ default.
 
 Default: C<1>
 
+=item B<run_hotplug_scripts=BOOLEAN>
+
+If disabled hotplug scripts will be called from udev, as it used to
+be in the previous releases. With the default option, hotplug scripts
+will be launched by xl directly.
+
+Default: C<1>
+
 =item B<lockfile="PATH">
 
 Sets the path to the lock file used by xl to serialise certain
diff --git a/tools/examples/xl.conf b/tools/examples/xl.conf
index ebf057c..28ab796 100644
--- a/tools/examples/xl.conf
+++ b/tools/examples/xl.conf
@@ -15,3 +15,8 @@
 
 # first block device to be used for temporary VM disk mounts
 #blkdev_start="xvda"
+
+# default option to run hotplug scripts from xl
+# if disabled the old behaviour will be used, and hotplug scripts will be
+# launched by udev.
+#run_hotplug_scripts=1
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index 6f4c865..c584c4e 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -70,6 +70,8 @@ int libxl__domain_create_info_setdefault(libxl__gc *gc,
         libxl_defbool_setdefault(&c_info->oos, true);
     }
 
+    libxl_defbool_setdefault(&c_info->run_hotplug_scripts, true);
+
     return 0;
 }
 
@@ -382,7 +384,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info,
                        uint32_t *domid)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
-    int flags, ret, rc;
+    int flags, ret, rc, nb_vm;
     char *uuid_string;
     char *dom_path, *vm_path, *libxl_path;
     struct xs_permissions roperm[2];
@@ -503,6 +505,40 @@ retry_transaction:
             libxl__sprintf(gc, "%s/hvmloader/generation-id-address", dom_path),
                         rwperm, ARRAY_SIZE(rwperm));
 
+    if (libxl_list_vm(ctx, &nb_vm) < 0) {
+        LOG(ERROR, "cannot get number of running guests");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    int hotplug_setting = libxl__hotplug_settings(gc, t);
+    if (hotplug_setting < 0) {
+        LOG(ERROR, "unable to get current hotplug scripts execution setting");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+    if (libxl_defbool_val(info->run_hotplug_scripts) != hotplug_setting &&
+        (nb_vm - 1)) {
+        LOG(ERROR, "cannot change hotplug execution option once set, "
+                    "please shutdown all guests before changing it");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    if (libxl_defbool_val(info->run_hotplug_scripts)) {
+        rc = libxl__xs_write(gc, t, DISABLE_UDEV_PATH, "1");
+        if (rc) {
+            LOGE(ERROR, "unable to write %s = 1", DISABLE_UDEV_PATH);
+            goto out;
+        }
+    } else {
+        rc = xs_rm(ctx->xsh, t, DISABLE_UDEV_PATH);
+        if (rc) {
+            LOGE(ERROR, "unable to delete %s", DISABLE_UDEV_PATH);
+            goto out;
+        }
+    }
+
     xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
     xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/name", vm_path), info->name, strlen(info->name));
 
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index b4a5f1f..612e9db 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -762,6 +762,10 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     dm_config->nics = guest_config->nics;
     dm_config->num_nics = guest_config->num_nics;
 
+    libxl_defbool_set(
+        &dm_config->c_info.run_hotplug_scripts,
+        libxl_defbool_val(guest_config->c_info.run_hotplug_scripts));
+
     ret = libxl__domain_create_info_setdefault(gc, &dm_config->c_info);
     if (ret) goto out;
     ret = libxl__domain_build_info_setdefault(gc, &dm_config->b_info);
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index fbff7d0..4775bb6 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -353,6 +353,25 @@ libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc,
     return value;
 }
 
+int libxl__hotplug_settings(libxl__gc *gc, xs_transaction_t t)
+{
+    int rc = 0;
+    char *val;
+
+    val = libxl__xs_read(gc, t, DISABLE_UDEV_PATH);
+    if (!val && errno != ENOENT) {
+        LOGE(ERROR, "cannot read %s from xenstore", DISABLE_UDEV_PATH);
+        rc = ERROR_FAIL;
+        goto out;
+    }
+    if (!val) val = "0";
+
+    rc = !!atoi(val);
+
+out:
+    return rc;
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index df2db10..96be57a 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -90,6 +90,7 @@
 #define STUBDOM_CONSOLE_SERIAL 3
 #define STUBDOM_SPECIAL_CONSOLES 3
 #define TAP_DEVICE_SUFFIX "-emu"
+#define DISABLE_UDEV_PATH "libxl/disable_udev"
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 
@@ -1463,6 +1464,8 @@ _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc, const char *s);
 _hidden libxl_device_model_version
 libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
 
+/* Check how executes hotplug script currently */
+int libxl__hotplug_settings(libxl__gc *gc, xs_transaction_t t);
 
 /*
  * Calling context and GC for event-generating functions:
diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index 00947e1..cca295d 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -231,6 +231,7 @@ libxl_domain_create_info = Struct("domain_create_info",[
     ("xsdata",       libxl_key_value_list),
     ("platformdata", libxl_key_value_list),
     ("poolid",       uint32),
+    ("run_hotplug_scripts",libxl_defbool),
     ], dir=DIR_IN)
 
 MemKB = UInt(64, init_val = "LIBXL_MEMKB_DEFAULT")
diff --git a/tools/libxl/xl.c b/tools/libxl/xl.c
index d6f2b3e..f31e836 100644
--- a/tools/libxl/xl.c
+++ b/tools/libxl/xl.c
@@ -39,6 +39,7 @@ int dryrun_only;
 int force_execution;
 int autoballoon = 1;
 char *blkdev_start;
+int run_hotplug_scripts = 1;
 char *lockfile;
 char *default_vifscript = NULL;
 char *default_bridge = NULL;
@@ -70,6 +71,9 @@ static void parse_global_config(const char *configfile,
     if (!xlu_cfg_get_long (config, "autoballoon", &l, 0))
         autoballoon = l;
 
+    if (!xlu_cfg_get_long (config, "run_hotplug_scripts", &l, 0))
+        run_hotplug_scripts = l;
+
     if (!xlu_cfg_get_string (config, "lockfile", &buf, 0))
         lockfile = strdup(buf);
     else {
diff --git a/tools/libxl/xl.h b/tools/libxl/xl.h
index b0ba357..0b2f848 100644
--- a/tools/libxl/xl.h
+++ b/tools/libxl/xl.h
@@ -140,6 +140,7 @@ int xl_child_pid(xlchildnum); /* returns 0 if child struct is not in use */
 
 /* global options */
 extern int autoballoon;
+extern int run_hotplug_scripts;
 extern int dryrun_only;
 extern char *lockfile;
 extern char *default_vifscript;
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 4c3cabd..ea933e8 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -595,6 +595,7 @@ static void parse_config_data(const char *config_source,
         }
     }
 
+    libxl_defbool_set(&c_info->run_hotplug_scripts, run_hotplug_scripts);
     c_info->type = LIBXL_DOMAIN_TYPE_PV;
     if (!xlu_cfg_get_string (config, "builder", &buf, 0) &&
         !strncmp(buf, "hvm", strlen(buf)))
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 11/15] libxl: rename _IOEMU nic type to VIF_IOEMU
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (9 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 10/15] libxl: add option to choose who executes hotplug scripts Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 12/15] libxl: set correct nic type depending on the guest Roger Pau Monne
                   ` (4 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

This change will avoid the confusion caused by the fact that IOEMU
means both PV and TAP network interfaces.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c         |    4 ++--
 tools/libxl/libxl_dm.c      |    8 ++++----
 tools/libxl/libxl_types.idl |    2 +-
 tools/libxl/xl_cmdimpl.c    |    4 ++--
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 038e340..9733054 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -2399,7 +2399,7 @@ int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic)
                                   libxl__xen_script_dir_path()) < 0 )
         return ERROR_FAIL;
     if (!nic->nictype)
-        nic->nictype = LIBXL_NIC_TYPE_IOEMU;
+        nic->nictype = LIBXL_NIC_TYPE_VIF_IOEMU;
     return 0;
 }
 
@@ -2709,7 +2709,7 @@ const char *libxl__device_nic_devname(libxl__gc *gc,
     switch (type) {
     case LIBXL_NIC_TYPE_VIF:
         return GCSPRINTF("vif%u.%d", domid, devid);
-    case LIBXL_NIC_TYPE_IOEMU:
+    case LIBXL_NIC_TYPE_VIF_IOEMU:
         return GCSPRINTF("vif%u.%d" TAP_DEVICE_SUFFIX, domid, devid);
     default:
         abort();
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index 612e9db..22eac4c 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -215,12 +215,12 @@ static char ** libxl__build_device_model_args_old(libxl__gc *gc,
         free(s);
 
         for (i = 0; i < num_nics; i++) {
-            if (nics[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
+            if (nics[i].nictype == LIBXL_NIC_TYPE_VIF_IOEMU) {
                 char *smac = libxl__sprintf(gc,
                                    LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
                 const char *ifname = libxl__device_nic_devname(gc,
                                                 domid, nics[i].devid,
-                                                LIBXL_NIC_TYPE_IOEMU);
+                                                LIBXL_NIC_TYPE_VIF_IOEMU);
                 flexarray_vappend(dm_args,
                                   "-net",
                                   GCSPRINTF(
@@ -469,12 +469,12 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                                                          b_info->max_vcpus));
         }
         for (i = 0; i < num_nics; i++) {
-            if (nics[i].nictype == LIBXL_NIC_TYPE_IOEMU) {
+            if (nics[i].nictype == LIBXL_NIC_TYPE_VIF_IOEMU) {
                 char *smac = libxl__sprintf(gc,
                                 LIBXL_MAC_FMT, LIBXL_MAC_BYTES(nics[i].mac));
                 const char *ifname = libxl__device_nic_devname(gc,
                                                 guest_domid, nics[i].devid,
-                                                LIBXL_NIC_TYPE_IOEMU);
+                                                LIBXL_NIC_TYPE_VIF_IOEMU);
                 flexarray_append(dm_args, "-device");
                 flexarray_append(dm_args,
                    libxl__sprintf(gc, "%s,id=nic%d,netdev=net%d,mac=%s",
diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index cca295d..b654a3c 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -60,7 +60,7 @@ libxl_disk_backend = Enumeration("disk_backend", [
     ])
 
 libxl_nic_type = Enumeration("nic_type", [
-    (1, "IOEMU"),
+    (1, "VIF_IOEMU"),
     (2, "VIF"),
     ])
 
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index ea933e8..437a2c4 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -979,7 +979,7 @@ static void parse_config_data(const char *config_source,
                     nic->bridge = strdup(p2 + 1);
                 } else if (!strcmp(p, "type")) {
                     if (!strcmp(p2 + 1, "ioemu"))
-                        nic->nictype = LIBXL_NIC_TYPE_IOEMU;
+                        nic->nictype = LIBXL_NIC_TYPE_VIF_IOEMU;
                     else
                         nic->nictype = LIBXL_NIC_TYPE_VIF;
                 } else if (!strcmp(p, "ip")) {
@@ -5161,7 +5161,7 @@ int main_networkattach(int argc, char **argv)
             if (!strcmp("vif", oparg)) {
                 nic.nictype = LIBXL_NIC_TYPE_VIF;
             } else if (!strcmp("ioemu", oparg)) {
-                nic.nictype = LIBXL_NIC_TYPE_IOEMU;
+                nic.nictype = LIBXL_NIC_TYPE_VIF_IOEMU;
             } else {
                 fprintf(stderr, "Invalid parameter `type'.\n");
                 return 1;
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 12/15] libxl: set correct nic type depending on the guest
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (10 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 11/15] libxl: rename _IOEMU nic type to VIF_IOEMU Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 13/15] libxl: use libxl__xs_path_cleanup on device_destroy Roger Pau Monne
                   ` (3 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Fix the use of nic type, which results in the following for each type
of domain:

 * HVM: let the user choose, if none specified use VIF_IOEMU.
 * HVM with stubdom: use VIF always.
 * PV: use VIF always.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl.c          |   22 ++++++++++++++++++----
 tools/libxl/libxl_create.c   |    2 +-
 tools/libxl/libxl_dm.c       |    2 +-
 tools/libxl/libxl_internal.h |    3 ++-
 4 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 9733054..c8d872b 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -2368,7 +2368,8 @@ out:
 
 /******************************************************************************/
 
-int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic)
+int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic,
+                                 uint32_t domid)
 {
     if (!nic->mtu)
         nic->mtu = 1492;
@@ -2398,8 +2399,21 @@ int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic)
     if ( !nic->script && asprintf(&nic->script, "%s/vif-bridge",
                                   libxl__xen_script_dir_path()) < 0 )
         return ERROR_FAIL;
-    if (!nic->nictype)
-        nic->nictype = LIBXL_NIC_TYPE_VIF_IOEMU;
+
+    switch (libxl__domain_type(gc, domid)) {
+    case LIBXL_DOMAIN_TYPE_HVM:
+        if (libxl_get_stubdom_id(CTX, domid))
+            nic->nictype = LIBXL_NIC_TYPE_VIF;
+        else if (!nic->nictype)
+            nic->nictype = LIBXL_NIC_TYPE_VIF_IOEMU;
+        break;
+    case LIBXL_DOMAIN_TYPE_PV:
+        nic->nictype = LIBXL_NIC_TYPE_VIF;
+        break;
+    default:
+        abort();
+    }
+
     return 0;
 }
 
@@ -2441,7 +2455,7 @@ void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
     char *dompath, **l;
     unsigned int nb, rc;
 
-    rc = libxl__device_nic_setdefault(gc, nic);
+    rc = libxl__device_nic_setdefault(gc, nic, domid);
     if (rc) goto out;
 
     front = flexarray_make(16, 1);
diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
index c584c4e..022e626 100644
--- a/tools/libxl/libxl_create.c
+++ b/tools/libxl/libxl_create.c
@@ -936,7 +936,7 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
          * called libxl_device_nic_add at this point, but qemu needs
          * the nic information to be complete.
          */
-        libxl__device_nic_setdefault(gc, &d_config->nics[i]);
+        libxl__device_nic_setdefault(gc, &d_config->nics[i], domid);
     }
     switch (d_config->c_info.type) {
     case LIBXL_DOMAIN_TYPE_HVM:
diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
index 22eac4c..8a5e1a7 100644
--- a/tools/libxl/libxl_dm.c
+++ b/tools/libxl/libxl_dm.c
@@ -875,7 +875,7 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
          * called libxl_device_nic_add at this point, but qemu needs
          * the nic information to be complete.
          */
-        libxl__device_nic_setdefault(gc, &dm_config->nics[i]);
+        libxl__device_nic_setdefault(gc, &dm_config->nics[i], dm_domid);
     }
     ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]);
     if (ret)
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 96be57a..4dad6e2 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -890,7 +890,8 @@ _hidden int libxl__domain_build_info_setdefault(libxl__gc *gc,
                                         libxl_domain_build_info *b_info);
 _hidden int libxl__device_disk_setdefault(libxl__gc *gc,
                                           libxl_device_disk *disk);
-_hidden int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic);
+_hidden int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic,
+                                         uint32_t domid);
 _hidden int libxl__device_vfb_setdefault(libxl__gc *gc, libxl_device_vfb *vfb);
 _hidden int libxl__device_vkb_setdefault(libxl__gc *gc, libxl_device_vkb *vkb);
 _hidden int libxl__device_pci_setdefault(libxl__gc *gc, libxl_device_pci *pci);
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 13/15] libxl: use libxl__xs_path_cleanup on device_destroy
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (11 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 12/15] libxl: set correct nic type depending on the guest Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 14/15] libxl: call hotplug scripts for disk devices from libxl Roger Pau Monne
                   ` (2 subsequent siblings)
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Since the hotplug script that was in charge of cleaning the backend is
no longer launched, we need to clean the backend by ourselves, so use
libxl__xs_path_cleanup instead of xs_rm.

Changes sinve v2:

 * Changed the goto construction to a do-while loop.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/libxl/libxl_device.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 13f9ecb..2f6e427 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -479,16 +479,26 @@ DEFINE_DEVICES_ADD(nic)
 
 int libxl__device_destroy(libxl__gc *gc, libxl__device *dev)
 {
-    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *be_path = libxl__device_backend_path(gc, dev);
     char *fe_path = libxl__device_frontend_path(gc, dev);
+    xs_transaction_t t = 0;
+    int rc = 0;
 
-    xs_rm(ctx->xsh, XBT_NULL, be_path);
-    xs_rm(ctx->xsh, XBT_NULL, fe_path);
+    do {
+        t = xs_transaction_start(CTX->xsh);
+        libxl__xs_path_cleanup(gc, t, fe_path);
+        libxl__xs_path_cleanup(gc, t, be_path);
+        rc = !xs_transaction_end(CTX->xsh, t, 0);
+    } while (rc && errno == EAGAIN);
+    if (rc) {
+        LOGE(ERROR, "unable to finish transaction");
+        goto out;
+    }
 
     libxl__device_destroy_tapdisk(gc, be_path);
 
-    return 0;
+out:
+    return rc;
 }
 
 /* Callback for device destruction */
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 14/15] libxl: call hotplug scripts for disk devices from libxl
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (12 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 13/15] libxl: use libxl__xs_path_cleanup on device_destroy Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-04 11:59 ` [PATCH v8 15/15] libxl: call hotplug scripts for nic " Roger Pau Monne
  2012-07-08 18:32 ` [PATCH v8 00/15] execute hotplug scripts " Ian Campbell
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Since most of the needed work is already done in previous patches,
this patch only contains the necessary code to call hotplug scripts
for disk devices, that should be called when the device is added or
removed from a guest.

We will chain the launch of the disk hotplug scripts after the
device_backend_callback callback, or directly from
libxl__initiate_device_{add,remove} if the device is already in the
desired state.

Changes since v5:

 * Added common exit point using the device_hotplug_done function.

 * Changed the "what" field in ao_device to "const char *".

Changes since v4:

 * Init args and env to NULL.

 * Correctly propagate errors from libxl__get_hotplug_script_info.

 * Replaced if in libxl__ev_child_inuse with asserts.

 * Renamed fork_cb to child_death_cb.

 * Move deregistration of device_hotplug_timeout_cb to the top of the
   function.

 * Removed "pid" field from libxl__ao_device struct.

 * Moved arraysize declaration.

 * Fixed diff complain about no newline at the end of libxl_netbsd.c.

Changes since v2:

 * Added array size check with assert.

 * Added NetBSD code (so compilation is not broken).

 * Removed a check for null in device_hotplug_timeout_cb.

Changes since v1:

 * Moved all the event related code that was inside libxl_linux.c into
   libxl_device.c, so the flow of the device addition/removal event is
   all in the same file.

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/hotplug/Linux/xen-backend.rules     |    6 +-
 tools/hotplug/Linux/xen-hotplug-common.sh |    6 ++
 tools/libxl/libxl.c                       |   10 ++
 tools/libxl/libxl_device.c                |  129 +++++++++++++++++++++++++++-
 tools/libxl/libxl_internal.h              |   19 ++++
 tools/libxl/libxl_linux.c                 |  100 ++++++++++++++++++++++
 tools/libxl/libxl_netbsd.c                |    8 ++
 7 files changed, 270 insertions(+), 8 deletions(-)

diff --git a/tools/hotplug/Linux/xen-backend.rules b/tools/hotplug/Linux/xen-backend.rules
index 405387f..d55ff11 100644
--- a/tools/hotplug/Linux/xen-backend.rules
+++ b/tools/hotplug/Linux/xen-backend.rules
@@ -1,11 +1,11 @@
-SUBSYSTEM=="xen-backend", KERNEL=="tap*", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
-SUBSYSTEM=="xen-backend", KERNEL=="vbd*", RUN+="/etc/xen/scripts/block $env{ACTION}"
+SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/blktap $env{ACTION}"
+SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/block $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="/etc/xen/scripts/vif2 $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif"
 SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif"
 SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}"
-SUBSYSTEM=="xen-backend", ACTION=="remove", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
+SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
 KERNEL=="evtchn", NAME="xen/%k"
 SUBSYSTEM=="xen", KERNEL=="blktap[0-9]*", NAME="xen/%k", MODE="0600"
 SUBSYSTEM=="blktap2", KERNEL=="blktap[0-9]*", NAME="xen/blktap-2/%k", MODE="0600"
diff --git a/tools/hotplug/Linux/xen-hotplug-common.sh b/tools/hotplug/Linux/xen-hotplug-common.sh
index 8f6557d..4a7bc73 100644
--- a/tools/hotplug/Linux/xen-hotplug-common.sh
+++ b/tools/hotplug/Linux/xen-hotplug-common.sh
@@ -15,6 +15,12 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 
+# Hack to prevent the execution of hotplug scripts from udev if the domain
+# has been launched from libxl
+if [ -n "${UDEV_CALL}" ] && \
+   xenstore-read "libxl/disable_udev" >/dev/null 2>&1; then
+    exit 0
+fi
 
 dir=$(dirname "$0")
 . "$dir/hotplugpath.sh"
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index c8d872b..47a4bde 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -1811,6 +1811,11 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
                 flexarray_append(back, "params");
                 flexarray_append(back, dev);
 
+                flexarray_append(back, "script");
+                flexarray_append(back, GCSPRINTF("%s/%s",
+                                                 libxl__xen_script_dir_path(),
+                                                 "block"));
+
                 assert(device->backend_kind == LIBXL__DEVICE_KIND_VBD);
                 break;
             case LIBXL_DISK_BACKEND_TAP:
@@ -1826,6 +1831,11 @@ static void device_disk_add(libxl__egc *egc, uint32_t domid,
                     libxl__device_disk_string_of_format(disk->format),
                     disk->pdev_path));
 
+                flexarray_append(back, "script");
+                flexarray_append(back, GCSPRINTF("%s/%s",
+                                                 libxl__xen_script_dir_path(),
+                                                 "blktap"));
+
                 /* now create a phy device to export the device to the guest */
                 goto do_backend_phy;
             case LIBXL_DISK_BACKEND_QDISK:
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 2f6e427..4dd375e 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -609,7 +609,16 @@ static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds,
 static void device_backend_cleanup(libxl__gc *gc,
                                    libxl__ao_device *aodev);
 
-static void device_xsentries_remove(libxl__egc *egc, libxl__ao_device *aodev);
+static void device_hotplug(libxl__egc *egc, libxl__ao_device *aodev);
+
+static void device_hotplug_timeout_cb(libxl__egc *egc, libxl__ev_time *ev,
+                                      const struct timeval *requested_abs);
+
+static void device_hotplug_child_death_cb(libxl__egc *egc,
+                                          libxl__ev_child *child,
+                                          pid_t pid, int status);
+
+static void device_hotplug_done(libxl__egc *egc, libxl__ao_device *aodev);
 
 void libxl__wait_device_connection(libxl__egc *egc, libxl__ao_device *aodev)
 {
@@ -637,7 +646,7 @@ void libxl__wait_device_connection(libxl__egc *egc, libxl__ao_device *aodev)
 out_fail:
     assert(rc);
     aodev->rc = rc;
-    device_xsentries_remove(egc, aodev);
+    device_hotplug_child_death_cb(egc, &aodev->child, 0, 0);
     return;
 }
 
@@ -685,7 +694,7 @@ retry_transaction:
  out_fail:
     assert(rc);
     aodev->rc = rc;
-    device_xsentries_remove(egc, aodev);
+    device_hotplug_child_death_cb(egc, &aodev->child, 0, 0);
     return;
 }
 
@@ -703,6 +712,11 @@ static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds,
         return;
     }
 
+    /* We init this here because we might call device_hotplug_done
+     * without actually calling any hotplug script */
+    libxl__ev_child_init(&aodev->child);
+    libxl__ev_time_init(&aodev->ev);
+
     /* Some devices (vkbd) fail to disconnect properly,
      * but we shouldn't alarm the user if it's during
      * domain destruction.
@@ -718,9 +732,12 @@ static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds,
         goto out;
     }
 
+    device_hotplug(egc, aodev);
+    return;
+
 out:
     aodev->rc = rc;
-    device_xsentries_remove(egc, aodev);
+    device_hotplug_done(egc, aodev);
     return;
 }
 
@@ -730,7 +747,104 @@ static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev)
     libxl__ev_devstate_cancel(gc, &aodev->backend_ds);
 }
 
-static void device_xsentries_remove(libxl__egc *egc, libxl__ao_device *aodev)
+static void device_hotplug(libxl__egc *egc, libxl__ao_device *aodev)
+{
+    STATE_AO_GC(aodev->ao);
+    char *be_path = libxl__device_backend_path(gc, aodev->dev);
+    char **args = NULL, **env = NULL;
+    int rc = 0;
+    int hotplug;
+    pid_t pid;
+
+    /* Check if we have to execute hotplug scripts for this device
+     * and return the necessary args/env vars for execution */
+    hotplug = libxl__get_hotplug_script_info(gc, aodev->dev, &args, &env,
+                                             aodev->action);
+    switch (hotplug) {
+    case 0:
+        /* no hotplug script to execute */
+        goto out;
+    case 1:
+        /* execute hotplug script */
+        break;
+    default:
+        /* everything else is an error */
+        LOG(ERROR, "unable to get args/env to execute hotplug script for "
+                   "device %s", libxl__device_backend_path(gc, aodev->dev));
+        rc = hotplug;
+        goto out;
+    }
+
+    /* Set hotplug timeout */
+    rc = libxl__ev_time_register_rel(gc, &aodev->ev, device_hotplug_timeout_cb,
+                                     LIBXL_HOTPLUG_TIMEOUT * 1000);
+    if (rc) {
+        LOG(ERROR, "unable to register timeout for hotplug device %s", be_path);
+        goto out;
+    }
+
+    aodev->what = GCSPRINTF("%s %s", args[0], args[1]);
+    LOG(DEBUG, "calling hotplug script: %s %s", args[0], args[1]);
+
+    /* fork and execute hotplug script */
+    pid = libxl__ev_child_fork(gc, &aodev->child, device_hotplug_child_death_cb);
+    if (pid == -1) {
+        LOG(ERROR, "unable to fork");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    if (!pid) {
+        /* child */
+        libxl__exec(gc, -1, -1, -1, args[0], args, env);
+        /* notreached */
+        abort();
+    }
+
+    assert(libxl__ev_child_inuse(&aodev->child));
+
+    return;
+
+out:
+    aodev->rc = rc;
+    device_hotplug_done(egc, aodev);
+    return;
+}
+
+static void device_hotplug_timeout_cb(libxl__egc *egc, libxl__ev_time *ev,
+                                      const struct timeval *requested_abs)
+{
+    libxl__ao_device *aodev = CONTAINER_OF(ev, *aodev, ev);
+    STATE_AO_GC(aodev->ao);
+
+    assert(libxl__ev_child_inuse(&aodev->child));
+    LOG(DEBUG, "killing hotplug script %s because of timeout", aodev->what);
+    if (kill(aodev->child.pid, SIGKILL)) {
+        LOGEV(ERROR, errno, "unable to kill hotplug script %s [%ld]",
+                            aodev->what, (unsigned long)aodev->child.pid);
+    }
+
+    return;
+}
+
+static void device_hotplug_child_death_cb(libxl__egc *egc,
+                                          libxl__ev_child *child,
+                                          pid_t pid, int status)
+{
+    libxl__ao_device *aodev = CONTAINER_OF(child, *aodev, child);
+    STATE_AO_GC(aodev->ao);
+
+    if (status) {
+        libxl_report_child_exitstatus(CTX, aodev->rc ? LIBXL__LOG_ERROR
+                                                     : LIBXL__LOG_WARNING,
+                                      aodev->what, pid, status);
+        aodev->rc = ERROR_FAIL;
+    }
+
+    device_hotplug_done(egc, aodev);
+}
+
+static void device_hotplug_done(libxl__egc *egc, libxl__ao_device *aodev)
 {
     STATE_AO_GC(aodev->ao);
     char *be_path = libxl__device_backend_path(gc, aodev->dev);
@@ -738,6 +852,11 @@ static void device_xsentries_remove(libxl__egc *egc, libxl__ao_device *aodev)
     xs_transaction_t t = 0;
     int rc;
 
+    /* Clean events and check reentrancy */
+    libxl__ev_time_deregister(gc, &aodev->ev);
+    assert(!libxl__ev_child_inuse(&aodev->child));
+
+    /* Clean xenstore if it's a disconnection */
     if (aodev->action == DEVICE_DISCONNECT) {
         for (;;) {
             rc = libxl__xs_transaction_start(gc, &t);
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 4dad6e2..d4d7cb0 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -74,6 +74,7 @@
 
 #define LIBXL_INIT_TIMEOUT 10
 #define LIBXL_DESTROY_TIMEOUT 10
+#define LIBXL_HOTPLUG_TIMEOUT 10
 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
 #define LIBXL_XENCONSOLE_LIMIT 1048576
 #define LIBXL_XENCONSOLE_PROTOCOL "vt100"
@@ -1788,6 +1789,10 @@ struct libxl__ao_device {
     /* Used internally to have a reference to the upper libxl__ao_devices
      * struct when present */
     libxl__ao_devices *aodevs;
+    /* device hotplug execution */
+    const char *what;
+    libxl__ev_time ev;
+    libxl__ev_child child;
 };
 
 /* Helper struct to simply the plug/unplug of multiple devices at the same
@@ -1844,6 +1849,20 @@ _hidden void libxl__wait_device_connection(libxl__egc*,
 _hidden void libxl__initiate_device_remove(libxl__egc *egc,
                                            libxl__ao_device *aodev);
 
+/*
+ * libxl__get_hotplug_script_info returns the args and env that should
+ * be passed to the hotplug script for the requested device.
+ *
+ * Since a device might not need to execute any hotplug script, this function
+ * can return the following values:
+ * < 0: Error
+ * 0: No need to execute hotplug script
+ * 1: Execute hotplug script
+ */
+_hidden int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
+                                           char ***args, char ***env,
+                                           libxl__device_action action);
+
 /*----- local disk attach: attach a disk locally to run the bootloader -----*/
 
 typedef struct libxl__disk_local_state libxl__disk_local_state;
diff --git a/tools/libxl/libxl_linux.c b/tools/libxl/libxl_linux.c
index 0169b2f..8883e45 100644
--- a/tools/libxl/libxl_linux.c
+++ b/tools/libxl/libxl_linux.c
@@ -77,3 +77,103 @@ char *libxl__devid_to_localdev(libxl__gc *gc, int devid)
                 "%d", minor & (nr_parts - 1));
     return ret;
 }
+
+/* Hotplug scripts helpers */
+
+static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev)
+{
+    char *be_path = libxl__device_backend_path(gc, dev);
+    char *script;
+    const char *type = libxl__device_kind_to_string(dev->backend_kind);
+    char **env;
+    int nr = 0;
+
+    script = libxl__xs_read(gc, XBT_NULL,
+                            GCSPRINTF("%s/%s", be_path, "script"));
+    if (!script) {
+        LOGEV(ERROR, errno, "unable to read script from %s", be_path);
+        return NULL;
+    }
+
+    const int arraysize = 9;
+    GCNEW_ARRAY(env, arraysize);
+    env[nr++] = "script";
+    env[nr++] = script;
+    env[nr++] = "XENBUS_TYPE";
+    env[nr++] = libxl__strdup(gc, type);
+    env[nr++] = "XENBUS_PATH";
+    env[nr++] = GCSPRINTF("backend/%s/%u/%d", type, dev->domid, dev->devid);
+    env[nr++] = "XENBUS_BASE_PATH";
+    env[nr++] = "backend";
+    env[nr++] = NULL;
+    assert(nr == arraysize);
+
+    return env;
+}
+
+/* Hotplug scripts caller functions */
+
+static int libxl__hotplug_disk(libxl__gc *gc, libxl__device *dev,
+                               char ***args, char ***env,
+                               libxl__device_action action)
+{
+    char *be_path = libxl__device_backend_path(gc, dev);
+    char *script;
+    int nr = 0, rc = 0;
+
+    script = libxl__xs_read(gc, XBT_NULL,
+                            GCSPRINTF("%s/%s", be_path, "script"));
+    if (!script) {
+        LOGEV(ERROR, errno, "unable to read script from %s", be_path);
+        rc = ERROR_FAIL;
+        goto error;
+    }
+
+    *env = get_hotplug_env(gc, dev);
+    if (!*env) {
+        rc = ERROR_FAIL;
+        goto error;
+    }
+
+    const int arraysize = 3;
+    GCNEW_ARRAY(*args, arraysize);
+    (*args)[nr++] = script;
+    (*args)[nr++] = action == DEVICE_CONNECT ? "add" : "remove";
+    (*args)[nr++] = NULL;
+    assert(nr == arraysize);
+
+    rc = 0;
+
+error:
+    return rc;
+}
+
+int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
+                                   char ***args, char ***env,
+                                   libxl__device_action action)
+{
+    char *disable_udev = libxl__xs_read(gc, XBT_NULL, DISABLE_UDEV_PATH);
+    int rc;
+
+    /* Check if we have to run hotplug scripts */
+    if (!disable_udev) {
+        rc = 0;
+        goto out;
+    }
+
+    switch (dev->backend_kind) {
+    case LIBXL__DEVICE_KIND_VBD:
+        rc = libxl__hotplug_disk(gc, dev, args, env, action);
+        if (!rc) rc = 1;
+        break;
+    default:
+        /* If no need to execute any hotplug scripts,
+         * call the callback manually
+         */
+        rc = 0;
+        break;
+    }
+
+out:
+    return rc;
+}
diff --git a/tools/libxl/libxl_netbsd.c b/tools/libxl/libxl_netbsd.c
index dbf5f71..a2f8d3f 100644
--- a/tools/libxl/libxl_netbsd.c
+++ b/tools/libxl/libxl_netbsd.c
@@ -30,3 +30,11 @@ char *libxl__devid_to_localdev(libxl__gc *gc, int devid)
     /* TODO */
     return NULL;
 }
+
+/* Hotplug scripts caller functions */
+int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
+                                   char ***args, char ***env,
+                                   libxl__device_action action)
+{
+    return 0;
+}
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v8 15/15] libxl: call hotplug scripts for nic devices from libxl
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (13 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 14/15] libxl: call hotplug scripts for disk devices from libxl Roger Pau Monne
@ 2012-07-04 11:59 ` Roger Pau Monne
  2012-07-08 18:32 ` [PATCH v8 00/15] execute hotplug scripts " Ian Campbell
  15 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-04 11:59 UTC (permalink / raw)
  To: xen-devel; +Cc: Ian Jackson, Roger Pau Monne

Since most of the needed work is already done in previous patches,
this patch only contains the necessary code to call hotplug scripts
for nic devices, that should be called when the device is added or
removed from a guest.

Added another parameter to libxl__get_hotplug_script_info, that is
used to know the number of times hotplug scripts have been called for
that device. This is currently used by IOEMU nics on Linux.

Changes since v6:

 * Added an rc = 0 before exitting libxl__nic_type if successful.

Changes since v4:

 * Add num_exec to NetBSD dummy function.

 * Better comment in the prototype of libxl__get_hotplug_script_info.

 * Remove nasty use of goto.

 * Keep calling device_hotplug until libxl__get_hotplug_script_info
   returns <= 0. This is used by TAP nics which also have a VIF
   interface (PV_IOEMU).

Changes since v2:

 * Change libxl__nic_type to return the value in a parameter passed by
   the caller.

 * Rename vif_execute to num_exec, to represent the number of times
   hotplug scripts have been called for that device.

Changes since v1:

 * Move event code to libxl_device.c (as in previous patch).

Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Jackson<ian.jackson@eu.citrix.com>
Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
---
 tools/hotplug/Linux/xen-backend.rules |    6 +-
 tools/libxl/libxl_device.c            |   52 ++++++++++++++++--
 tools/libxl/libxl_internal.h          |   14 ++++-
 tools/libxl/libxl_linux.c             |   98 +++++++++++++++++++++++++++++++--
 tools/libxl/libxl_netbsd.c            |    3 +-
 5 files changed, 158 insertions(+), 15 deletions(-)

diff --git a/tools/hotplug/Linux/xen-backend.rules b/tools/hotplug/Linux/xen-backend.rules
index d55ff11..c591a3f 100644
--- a/tools/hotplug/Linux/xen-backend.rules
+++ b/tools/hotplug/Linux/xen-backend.rules
@@ -2,8 +2,8 @@ SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scr
 SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/block $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}"
 SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="/etc/xen/scripts/vif2 $env{ACTION}"
-SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif"
-SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif"
+SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif"
+SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif"
 SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}"
 SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/xen-hotplug-cleanup"
 KERNEL=="evtchn", NAME="xen/%k"
@@ -13,4 +13,4 @@ KERNEL=="blktap-control", NAME="xen/blktap-2/control", MODE="0600"
 KERNEL=="gntdev", NAME="xen/%k", MODE="0600"
 KERNEL=="pci_iomul", NAME="xen/%k", MODE="0600"
 KERNEL=="tapdev[a-z]*", NAME="xen/blktap-2/tapdev%m", MODE="0600"
-SUBSYSTEM=="net", KERNEL=="vif*-emu", ACTION=="add", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap"
+SUBSYSTEM=="net", KERNEL=="vif*-emu", ACTION=="add", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap"
diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index 4dd375e..91a6ef4 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -100,6 +100,31 @@ out:
     return numdevs;
 }
 
+int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype)
+{
+    char *snictype, *be_path;
+    int rc = 0;
+
+    be_path = libxl__device_backend_path(gc, dev);
+    snictype = libxl__xs_read(gc, XBT_NULL,
+                              GCSPRINTF("%s/%s", be_path, "type"));
+    if (!snictype) {
+        LOGE(ERROR, "unable to read nictype from %s", be_path);
+        rc = ERROR_FAIL;
+        goto out;
+    }
+    rc = libxl_nic_type_from_string(snictype, nictype);
+    if (rc) {
+        LOGE(ERROR, "unable to parse nictype from %s", be_path);
+        goto out;
+    }
+
+    rc = 0;
+
+out:
+    return rc;
+}
+
 int libxl__device_generic_add(libxl__gc *gc, xs_transaction_t t,
         libxl__device *device, char **bents, char **fents)
 {
@@ -620,6 +645,8 @@ static void device_hotplug_child_death_cb(libxl__egc *egc,
 
 static void device_hotplug_done(libxl__egc *egc, libxl__ao_device *aodev);
 
+static void device_hotplug_clean(libxl__gc *gc, libxl__ao_device *aodev);
+
 void libxl__wait_device_connection(libxl__egc *egc, libxl__ao_device *aodev)
 {
     STATE_AO_GC(aodev->ao);
@@ -759,7 +786,8 @@ static void device_hotplug(libxl__egc *egc, libxl__ao_device *aodev)
     /* Check if we have to execute hotplug scripts for this device
      * and return the necessary args/env vars for execution */
     hotplug = libxl__get_hotplug_script_info(gc, aodev->dev, &args, &env,
-                                             aodev->action);
+                                             aodev->action,
+                                             aodev->num_exec);
     switch (hotplug) {
     case 0:
         /* no hotplug script to execute */
@@ -841,7 +869,16 @@ static void device_hotplug_child_death_cb(libxl__egc *egc,
         aodev->rc = ERROR_FAIL;
     }
 
-    device_hotplug_done(egc, aodev);
+    device_hotplug_clean(gc, aodev);
+
+    /* Increase num_exec and call hotplug scripts again if necessary
+     * If no more executions are needed, device_hotplug will call
+     * device_hotplug_done breaking the loop.
+     */
+    aodev->num_exec++;
+    device_hotplug(egc, aodev);
+
+    return;
 }
 
 static void device_hotplug_done(libxl__egc *egc, libxl__ao_device *aodev)
@@ -852,9 +889,7 @@ static void device_hotplug_done(libxl__egc *egc, libxl__ao_device *aodev)
     xs_transaction_t t = 0;
     int rc;
 
-    /* Clean events and check reentrancy */
-    libxl__ev_time_deregister(gc, &aodev->ev);
-    assert(!libxl__ev_child_inuse(&aodev->child));
+    device_hotplug_clean(gc, aodev);
 
     /* Clean xenstore if it's a disconnection */
     if (aodev->action == DEVICE_DISCONNECT) {
@@ -876,6 +911,13 @@ out:
     return;
 }
 
+static void device_hotplug_clean(libxl__gc *gc, libxl__ao_device *aodev)
+{
+    /* Clean events and check reentrancy */
+    libxl__ev_time_deregister(gc, &aodev->ev);
+    assert(!libxl__ev_child_inuse(&aodev->child));
+}
+
 static void devices_remove_callback(libxl__egc *egc, libxl__ao_devices *aodevs,
                                     int rc)
 {
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index d4d7cb0..b7a25cb 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -872,6 +872,8 @@ _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path,
                                       libxl__device *dev);
 _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev);
 _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);
+_hidden int libxl__nic_type(libxl__gc *gc, libxl__device *dev,
+                            libxl_nic_type *nictype);
 
 /*
  * For each aggregate type which can be used as an input we provide:
@@ -1791,6 +1793,7 @@ struct libxl__ao_device {
     libxl__ao_devices *aodevs;
     /* device hotplug execution */
     const char *what;
+    int num_exec;
     libxl__ev_time ev;
     libxl__ev_child child;
 };
@@ -1858,10 +1861,19 @@ _hidden void libxl__initiate_device_remove(libxl__egc *egc,
  * < 0: Error
  * 0: No need to execute hotplug script
  * 1: Execute hotplug script
+ *
+ * The last parameter, "num_exec" refeers to the number of times hotplug
+ * scripts have been called for this device.
+ *
+ * The main body of libxl will, for each device, keep calling
+ * libxl__get_hotplug_script_info, with incrementing values of
+ * num_exec, and executing the resulting script accordingly,
+ * until libxl__get_hotplug_script_info returns<=0.
  */
 _hidden int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
                                            char ***args, char ***env,
-                                           libxl__device_action action);
+                                           libxl__device_action action,
+                                           int num_exec);
 
 /*----- local disk attach: attach a disk locally to run the bootloader -----*/
 
diff --git a/tools/libxl/libxl_linux.c b/tools/libxl/libxl_linux.c
index 8883e45..7b1cb29 100644
--- a/tools/libxl/libxl_linux.c
+++ b/tools/libxl/libxl_linux.c
@@ -87,6 +87,7 @@ static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev)
     const char *type = libxl__device_kind_to_string(dev->backend_kind);
     char **env;
     int nr = 0;
+    libxl_nic_type nictype;
 
     script = libxl__xs_read(gc, XBT_NULL,
                             GCSPRINTF("%s/%s", be_path, "script"));
@@ -95,7 +96,7 @@ static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev)
         return NULL;
     }
 
-    const int arraysize = 9;
+    const int arraysize = 13;
     GCNEW_ARRAY(env, arraysize);
     env[nr++] = "script";
     env[nr++] = script;
@@ -105,14 +106,90 @@ static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev)
     env[nr++] = GCSPRINTF("backend/%s/%u/%d", type, dev->domid, dev->devid);
     env[nr++] = "XENBUS_BASE_PATH";
     env[nr++] = "backend";
+    if (dev->backend_kind == LIBXL__DEVICE_KIND_VIF) {
+        if (libxl__nic_type(gc, dev, &nictype)) {
+            LOG(ERROR, "unable to get nictype");
+            return NULL;
+        }
+        switch (nictype) {
+        case LIBXL_NIC_TYPE_VIF_IOEMU:
+            env[nr++] = "INTERFACE";
+            env[nr++] = libxl__strdup(gc, libxl__device_nic_devname(gc,
+                                                      dev->domid, dev->devid,
+                                                      LIBXL_NIC_TYPE_VIF_IOEMU));
+        case LIBXL_NIC_TYPE_VIF:
+            env[nr++] = "vif";
+            env[nr++] = libxl__strdup(gc, libxl__device_nic_devname(gc,
+                                                      dev->domid, dev->devid,
+                                                      LIBXL_NIC_TYPE_VIF));
+            break;
+        default:
+            return NULL;
+        }
+    }
+
     env[nr++] = NULL;
-    assert(nr == arraysize);
+    assert(nr <= arraysize);
 
     return env;
 }
 
 /* Hotplug scripts caller functions */
 
+static int libxl__hotplug_nic(libxl__gc *gc, libxl__device *dev,
+                               char ***args, char ***env,
+                               libxl__device_action action, int num_exec)
+{
+    char *be_path = libxl__device_backend_path(gc, dev);
+    char *script;
+    int nr = 0, rc = 0;
+    libxl_nic_type nictype;
+
+    script = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/%s", be_path,
+                                                             "script"));
+    if (!script) {
+        LOGE(ERROR, "unable to read script from %s", be_path);
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    rc = libxl__nic_type(gc, dev, &nictype);
+    if (rc) {
+        LOG(ERROR, "error when fetching nic type");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+    if (nictype == LIBXL_NIC_TYPE_VIF && num_exec != 0) {
+        rc = 0;
+        goto out;
+    }
+
+    *env = get_hotplug_env(gc, dev);
+    if (!env) {
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    const int arraysize = 4;
+    GCNEW_ARRAY(*args, arraysize);
+    (*args)[nr++] = script;
+
+    if (nictype == LIBXL_NIC_TYPE_VIF_IOEMU && num_exec) {
+        (*args)[nr++] = action == DEVICE_CONNECT ? "add" : "remove";
+        (*args)[nr++] = libxl__strdup(gc, "type_if=tap");
+        (*args)[nr++] = NULL;
+    } else {
+        (*args)[nr++] = action == DEVICE_CONNECT ? "online" : "offline";
+        (*args)[nr++] = libxl__strdup(gc, "type_if=vif");
+        (*args)[nr++] = NULL;
+    }
+    assert(nr == arraysize);
+    rc = 1;
+
+out:
+    return rc;
+}
+
 static int libxl__hotplug_disk(libxl__gc *gc, libxl__device *dev,
                                char ***args, char ***env,
                                libxl__device_action action)
@@ -142,7 +219,7 @@ static int libxl__hotplug_disk(libxl__gc *gc, libxl__device *dev,
     (*args)[nr++] = NULL;
     assert(nr == arraysize);
 
-    rc = 0;
+    rc = 1;
 
 error:
     return rc;
@@ -150,7 +227,8 @@ error:
 
 int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
                                    char ***args, char ***env,
-                                   libxl__device_action action)
+                                   libxl__device_action action,
+                                   int num_exec)
 {
     char *disable_udev = libxl__xs_read(gc, XBT_NULL, DISABLE_UDEV_PATH);
     int rc;
@@ -163,8 +241,18 @@ int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
 
     switch (dev->backend_kind) {
     case LIBXL__DEVICE_KIND_VBD:
+        if (num_exec != 0) {
+            rc = 0;
+            goto out;
+        }
         rc = libxl__hotplug_disk(gc, dev, args, env, action);
-        if (!rc) rc = 1;
+        break;
+    case LIBXL__DEVICE_KIND_VIF:
+        if (num_exec > 1) {
+            rc = 0;
+            goto out;
+        }
+        rc = libxl__hotplug_nic(gc, dev, args, env, action, num_exec);
         break;
     default:
         /* If no need to execute any hotplug scripts,
diff --git a/tools/libxl/libxl_netbsd.c b/tools/libxl/libxl_netbsd.c
index a2f8d3f..28cdf21 100644
--- a/tools/libxl/libxl_netbsd.c
+++ b/tools/libxl/libxl_netbsd.c
@@ -34,7 +34,8 @@ char *libxl__devid_to_localdev(libxl__gc *gc, int devid)
 /* Hotplug scripts caller functions */
 int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev,
                                    char ***args, char ***env,
-                                   libxl__device_action action)
+                                   libxl__device_action action,
+                                   int num_exec)
 {
     return 0;
 }
-- 
1.7.7.5 (Apple Git-26)

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 10/15] libxl: add option to choose who executes hotplug scripts
  2012-07-04 11:59 ` [PATCH v8 10/15] libxl: add option to choose who executes hotplug scripts Roger Pau Monne
@ 2012-07-06 10:28   ` Ian Campbell
  2012-07-10  9:20     ` Roger Pau Monne
  0 siblings, 1 reply; 29+ messages in thread
From: Ian Campbell @ 2012-07-06 10:28 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: Ian Jackson, xen-devel

On Wed, 2012-07-04 at 12:59 +0100, Roger Pau Monne wrote:
> Add and option to xl.conf file to decide if hotplug scripts are
> executed from the toolstack (xl) or from udev as it used to be in the
> past.
> 
> This option is only introduced in this patch, but it has no effect
> since the code to call hotplug scripts from libxl is introduced in a
> latter patch.
> 
> This choice will be saved in "libxl/disable_udev", as specified in the
> DISABLE_UDEV_PATH constant.
> 
> Changes since v2:
> 
>  * Change atoi(...) to !!atoi(...) to prevent returning negative
>    values from xenstore (which will be handled as errors).
> 
>  * Check for errors on the return value of libxl__hotplug_settings.
> 
> Changes since v1:
> 
>  * Used an auxiliary function to check for the current setting.
> 
> Cc: Ian Jackson <ian.jackson@eu.citrix.com>
> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com>
> ---
>  docs/man/xl.conf.pod.5       |    8 ++++++++
>  tools/examples/xl.conf       |    5 +++++
>  tools/libxl/libxl_create.c   |   38 +++++++++++++++++++++++++++++++++++++-
>  tools/libxl/libxl_dm.c       |    4 ++++
>  tools/libxl/libxl_internal.c |   19 +++++++++++++++++++
>  tools/libxl/libxl_internal.h |    3 +++
>  tools/libxl/libxl_types.idl  |    1 +
>  tools/libxl/xl.c             |    4 ++++
>  tools/libxl/xl.h             |    1 +
>  tools/libxl/xl_cmdimpl.c     |    1 +
>  10 files changed, 83 insertions(+), 1 deletions(-)
> 
> diff --git a/docs/man/xl.conf.pod.5 b/docs/man/xl.conf.pod.5
> index 149430c..23932be 100644
> --- a/docs/man/xl.conf.pod.5
> +++ b/docs/man/xl.conf.pod.5
> @@ -55,6 +55,14 @@ default.
>  
>  Default: C<1>
>  
> +=item B<run_hotplug_scripts=BOOLEAN>
> +
> +If disabled hotplug scripts will be called from udev, as it used to
> +be in the previous releases. With the default option, hotplug scripts
> +will be launched by xl directly.
> +
> +Default: C<1>
> +
>  =item B<lockfile="PATH">
>  
>  Sets the path to the lock file used by xl to serialise certain
> diff --git a/tools/examples/xl.conf b/tools/examples/xl.conf
> index ebf057c..28ab796 100644
> --- a/tools/examples/xl.conf
> +++ b/tools/examples/xl.conf
> @@ -15,3 +15,8 @@
>  
>  # first block device to be used for temporary VM disk mounts
>  #blkdev_start="xvda"
> +
> +# default option to run hotplug scripts from xl
> +# if disabled the old behaviour will be used, and hotplug scripts will be
> +# launched by udev.
> +#run_hotplug_scripts=1
> diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
> index 6f4c865..c584c4e 100644
> --- a/tools/libxl/libxl_create.c
> +++ b/tools/libxl/libxl_create.c
> @@ -70,6 +70,8 @@ int libxl__domain_create_info_setdefault(libxl__gc *gc,
>          libxl_defbool_setdefault(&c_info->oos, true);
>      }
>  
> +    libxl_defbool_setdefault(&c_info->run_hotplug_scripts, true);
> +
>      return 0;
>  }
>  
> @@ -382,7 +384,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info,
>                         uint32_t *domid)
>  {
>      libxl_ctx *ctx = libxl__gc_owner(gc);
> -    int flags, ret, rc;
> +    int flags, ret, rc, nb_vm;
>      char *uuid_string;
>      char *dom_path, *vm_path, *libxl_path;
>      struct xs_permissions roperm[2];
> @@ -503,6 +505,40 @@ retry_transaction:
>              libxl__sprintf(gc, "%s/hvmloader/generation-id-address", dom_path),
>                          rwperm, ARRAY_SIZE(rwperm));
>  
> +    if (libxl_list_vm(ctx, &nb_vm) < 0) {

This will leak the returned list of vms...

> +        LOG(ERROR, "cannot get number of running guests");
> +        rc = ERROR_FAIL;
> +        goto out;
> +    }
> +
> +    int hotplug_setting = libxl__hotplug_settings(gc, t);
> +    if (hotplug_setting < 0) {
> +        LOG(ERROR, "unable to get current hotplug scripts execution setting");
> +        rc = ERROR_FAIL;
> +        goto out;
> +    }
> +    if (libxl_defbool_val(info->run_hotplug_scripts) != hotplug_setting &&
> +        (nb_vm - 1)) {
> +        LOG(ERROR, "cannot change hotplug execution option once set, "
> +                    "please shutdown all guests before changing it");
> +        rc = ERROR_FAIL;
> +        goto out;
> +    }

> +
> +    if (libxl_defbool_val(info->run_hotplug_scripts)) {
> +        rc = libxl__xs_write(gc, t, DISABLE_UDEV_PATH, "1");
> +        if (rc) {
> +            LOGE(ERROR, "unable to write %s = 1", DISABLE_UDEV_PATH);
> +            goto out;
> +        }
> +    } else {
> +        rc = xs_rm(ctx->xsh, t, DISABLE_UDEV_PATH);
> +        if (rc) {
> +            LOGE(ERROR, "unable to delete %s", DISABLE_UDEV_PATH);
> +            goto out;
> +        }
> +    }

You could use libxl__xs_{write,rm}_checked here I think.

> +
>      xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
>      xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/name", vm_path), info->name, strlen(info->name));
>  
> diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
> index b4a5f1f..612e9db 100644
> --- a/tools/libxl/libxl_dm.c
> +++ b/tools/libxl/libxl_dm.c
> @@ -762,6 +762,10 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
>      dm_config->nics = guest_config->nics;
>      dm_config->num_nics = guest_config->num_nics;
>  
> +    libxl_defbool_set(
> +        &dm_config->c_info.run_hotplug_scripts,
> +        libxl_defbool_val(guest_config->c_info.run_hotplug_scripts));

This is the same as:

dm_config->c_info.run_hotplug_scripts = guest_config->c_info.run_hotplug_scripts;

Ian.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
                   ` (14 preceding siblings ...)
  2012-07-04 11:59 ` [PATCH v8 15/15] libxl: call hotplug scripts for nic " Roger Pau Monne
@ 2012-07-08 18:32 ` Ian Campbell
  2012-07-08 18:41   ` Ian Campbell
  2012-07-10 11:31   ` Roger Pau Monne
  15 siblings, 2 replies; 29+ messages in thread
From: Ian Campbell @ 2012-07-08 18:32 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: xen-devel

(resending as I don't think I had SMTP setup properly on my laptop -- sorry if you get this twice!)

On Wed, 2012-07-04 at 07:59 -0400, Roger Pau Monne wrote:
> This new serie (v8) fixes a code refactoring problem that was present 
> in v7 (06/15 changed code introduced by 05/15).

>From somewhere in here I'm seeing timeouts waiting for the b/e to go to
state 5 when doing cd-insert on an HVM guest. I suspect because this is
(or should be) turned into a virtual media change rather than an actual
device remove and insert?

BTW libxl_cdrom_insert hasn't been async'd up yet -- I was actually just
looking into that when I noticed this.

Ian.

        ~# xl -vvv cd-insert dHVM-1 hdc /scratch/mini.iso,raw
        libxl: debug: libxl.c:3074:libxl_device_disk_remove: ao 0x8069c30: create: how=(nil) callback=(nil) poller=0x8069a50
        libxl: debug: libxl_event.c:512:libxl__ev_xswatch_register: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/0: register slotnum=3
        libxl: debug: libxl.c:3074:libxl_device_disk_remove: ao 0x8069c30: inprogress: poller=0x8069a50, flags=i
        libxl: debug: libxl_event.c:457:watchfd_callback: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/0: event epath=/local/domain/0/backend/qdisk/43/5632/state
        libxl: debug: libxl_event.c:600:devstate_watch_callback: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6 still waiting state 5
        
        [... long wait ...]
        
        libxl: debug: libxl_event.c:614:devstate_timeout: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6  timed out
        libxl: debug: libxl_event.c:549:libxl__ev_xswatch_deregister: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/0: deregister slotnum=3
        libxl: debug: libxl_event.c:561:libxl__ev_xswatch_deregister: watch w=0x8069dcc: deregister unregistered
        libxl: debug: libxl_event.c:512:libxl__ev_xswatch_register: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/1: register slotnum=3
        libxl: debug: libxl_event.c:457:watchfd_callback: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/1: event epath=/local/domain/0/backend/qdisk/43/5632/state
        libxl: debug: libxl_event.c:600:devstate_watch_callback: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6 still waiting state 5
        
        [... long wait ...]
        
        libxl: debug: libxl_event.c:614:devstate_timeout: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6  timed out
        libxl: debug: libxl_event.c:549:libxl__ev_xswatch_deregister: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/1: deregister slotnum=3
        libxl: debug: libxl_event.c:561:libxl__ev_xswatch_deregister: watch w=0x8069dcc: deregister unregistered
        libxl: debug: libxl_device.c:759:device_backend_callback: unable to disconnect device with path /local/domain/0/backend/qdisk/43/5632
        libxl: debug: libxl_event.c:1434:libxl__ao_complete: ao 0x8069c30: complete, rc=0
        libxl: debug: libxl_event.c:1406:libxl__ao__destroy: ao 0x8069c30: destroy
        libxl: debug: libxl.c:1958:libxl_device_disk_add: ao 0x8069c30: create: how=(nil) callback=(nil) poller=0x8069a50
        libxl: debug: libxl_device.c:255:libxl__device_disk_set_backend: Disk vdev=hdc spec.backend=unknown
        libxl: debug: libxl_device.c:258:libxl__device_disk_set_backend:      format=raw cdrom=yes
        libxl: debug: libxl_device.c:209:disk_try_backend: Disk vdev=hdc, backend phy unsuitable as phys path not a block device
        libxl: debug: libxl_device.c:216:disk_try_backend: Disk vdev=hdc, backend tap unsuitable because blktap not available
        libxl: debug: libxl_device.c:293:libxl__device_disk_set_backend: Disk vdev=hdc, using backend qdisk
        libxl: debug: libxl_event.c:1434:libxl__ao_complete: ao 0x8069c30: complete, rc=0
        libxl: debug: libxl.c:1971:libxl_device_disk_add: ao 0x8069c30: inprogress: poller=0x8069a50, flags=ic
        libxl: debug: libxl_event.c:1406:libxl__ao__destroy: ao 0x8069c30: destroy
        xc: debug: hypercall buffer: total allocations:2 total releases:2
        xc: debug: hypercall buffer: current allocations:0 maximum allocations:2
        xc: debug: hypercall buffer: cache current size:1
        xc: debug: hypercall buffer: cache hits:0 misses:1 toobig:1

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-08 18:32 ` [PATCH v8 00/15] execute hotplug scripts " Ian Campbell
@ 2012-07-08 18:41   ` Ian Campbell
  2012-07-10 11:31   ` Roger Pau Monne
  1 sibling, 0 replies; 29+ messages in thread
From: Ian Campbell @ 2012-07-08 18:41 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: xen-devel

On Sun, 2012-07-08 at 14:32 -0400, Ian Campbell wrote:
> (resending as I don't think I had SMTP setup properly on my laptop -- sorry if you get this twice!)
> 
> On Wed, 2012-07-04 at 07:59 -0400, Roger Pau Monne wrote:
> > This new serie (v8) fixes a code refactoring problem that was present 
> > in v7 (06/15 changed code introduced by 05/15).
> 
> From somewhere in here I'm seeing timeouts waiting for the b/e to go to
> state 5 when doing cd-insert on an HVM guest.

I should have said that it does actually seem to eventually succeed.

>  I suspect because this is
> (or should be) turned into a virtual media change rather than an actual
> device remove and insert?

I've not really got a clue how this is suppose to work, but looking at
tools/qemu-xen-traditional-dir/xenstore.c I see things like:
        fprintf(stderr,"medium change watch on ...
        
Experimentally it seems that ejecting the ISO can be done with:
         xenstore-rm /local/domain/0/backend/qdisk/45/5632/params 
and inserting with
        xenstore-write /local/domain/0/backend/qdisk/45/5632/params aio:/scratch/mini.iso
(whether you can skip eject to do a change I didn't check)

So I think the libxl_cdrom_insert alg needs to be something like:

	if (hvm)
		rewrite params key
		if stubdomid
			libxl_device_disk_remove(domid)
			libxl_device_disk_add(domid)

	else /* pv */
		libxl_device_disk_remove(domid)
		libxl_device_disk_add(domid)

where the libxl_device_disk_* are actually async initiations and a chain
of callbacks

Grepping around in xend I don't see where this used to be done, maybe it
wasn't. I can't see an xm equiv to xl cd-insert...

Ian.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 10/15] libxl: add option to choose who executes hotplug scripts
  2012-07-06 10:28   ` Ian Campbell
@ 2012-07-10  9:20     ` Roger Pau Monne
  0 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-10  9:20 UTC (permalink / raw)
  To: Ian Campbell; +Cc: Ian Jackson, xen-devel

Ian Campbell wrote:
> On Wed, 2012-07-04 at 12:59 +0100, Roger Pau Monne wrote:
>> Add and option to xl.conf file to decide if hotplug scripts are
>> executed from the toolstack (xl) or from udev as it used to be in the
>> past.
>>
>> This option is only introduced in this patch, but it has no effect
>> since the code to call hotplug scripts from libxl is introduced in a
>> latter patch.
>>
>> This choice will be saved in "libxl/disable_udev", as specified in the
>> DISABLE_UDEV_PATH constant.
>>
>> Changes since v2:
>>
>>   * Change atoi(...) to !!atoi(...) to prevent returning negative
>>     values from xenstore (which will be handled as errors).
>>
>>   * Check for errors on the return value of libxl__hotplug_settings.
>>
>> Changes since v1:
>>
>>   * Used an auxiliary function to check for the current setting.
>>
>> Cc: Ian Jackson<ian.jackson@eu.citrix.com>
>> Acked-by: Ian Jackson<ian.jackson@eu.citrix.com>
>> Signed-off-by: Roger Pau Monne<roger.pau@citrix.com>
>> ---
>>   docs/man/xl.conf.pod.5       |    8 ++++++++
>>   tools/examples/xl.conf       |    5 +++++
>>   tools/libxl/libxl_create.c   |   38 +++++++++++++++++++++++++++++++++++++-
>>   tools/libxl/libxl_dm.c       |    4 ++++
>>   tools/libxl/libxl_internal.c |   19 +++++++++++++++++++
>>   tools/libxl/libxl_internal.h |    3 +++
>>   tools/libxl/libxl_types.idl  |    1 +
>>   tools/libxl/xl.c             |    4 ++++
>>   tools/libxl/xl.h             |    1 +
>>   tools/libxl/xl_cmdimpl.c     |    1 +
>>   10 files changed, 83 insertions(+), 1 deletions(-)
>>
>> diff --git a/docs/man/xl.conf.pod.5 b/docs/man/xl.conf.pod.5
>> index 149430c..23932be 100644
>> --- a/docs/man/xl.conf.pod.5
>> +++ b/docs/man/xl.conf.pod.5
>> @@ -55,6 +55,14 @@ default.
>>
>>   Default: C<1>
>>
>> +=item B<run_hotplug_scripts=BOOLEAN>
>> +
>> +If disabled hotplug scripts will be called from udev, as it used to
>> +be in the previous releases. With the default option, hotplug scripts
>> +will be launched by xl directly.
>> +
>> +Default: C<1>
>> +
>>   =item B<lockfile="PATH">
>>
>>   Sets the path to the lock file used by xl to serialise certain
>> diff --git a/tools/examples/xl.conf b/tools/examples/xl.conf
>> index ebf057c..28ab796 100644
>> --- a/tools/examples/xl.conf
>> +++ b/tools/examples/xl.conf
>> @@ -15,3 +15,8 @@
>>
>>   # first block device to be used for temporary VM disk mounts
>>   #blkdev_start="xvda"
>> +
>> +# default option to run hotplug scripts from xl
>> +# if disabled the old behaviour will be used, and hotplug scripts will be
>> +# launched by udev.
>> +#run_hotplug_scripts=1
>> diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
>> index 6f4c865..c584c4e 100644
>> --- a/tools/libxl/libxl_create.c
>> +++ b/tools/libxl/libxl_create.c
>> @@ -70,6 +70,8 @@ int libxl__domain_create_info_setdefault(libxl__gc *gc,
>>           libxl_defbool_setdefault(&c_info->oos, true);
>>       }
>>
>> +    libxl_defbool_setdefault(&c_info->run_hotplug_scripts, true);
>> +
>>       return 0;
>>   }
>>
>> @@ -382,7 +384,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info,
>>                          uint32_t *domid)
>>   {
>>       libxl_ctx *ctx = libxl__gc_owner(gc);
>> -    int flags, ret, rc;
>> +    int flags, ret, rc, nb_vm;
>>       char *uuid_string;
>>       char *dom_path, *vm_path, *libxl_path;
>>       struct xs_permissions roperm[2];
>> @@ -503,6 +505,40 @@ retry_transaction:
>>               libxl__sprintf(gc, "%s/hvmloader/generation-id-address", dom_path),
>>                           rwperm, ARRAY_SIZE(rwperm));
>>
>> +    if (libxl_list_vm(ctx,&nb_vm)<  0) {
>
> This will leak the returned list of vms...

Fixed.

>
>> +        LOG(ERROR, "cannot get number of running guests");
>> +        rc = ERROR_FAIL;
>> +        goto out;
>> +    }
>> +
>> +    int hotplug_setting = libxl__hotplug_settings(gc, t);
>> +    if (hotplug_setting<  0) {
>> +        LOG(ERROR, "unable to get current hotplug scripts execution setting");
>> +        rc = ERROR_FAIL;
>> +        goto out;
>> +    }
>> +    if (libxl_defbool_val(info->run_hotplug_scripts) != hotplug_setting&&
>> +        (nb_vm - 1)) {
>> +        LOG(ERROR, "cannot change hotplug execution option once set, "
>> +                    "please shutdown all guests before changing it");
>> +        rc = ERROR_FAIL;
>> +        goto out;
>> +    }
>
>> +
>> +    if (libxl_defbool_val(info->run_hotplug_scripts)) {
>> +        rc = libxl__xs_write(gc, t, DISABLE_UDEV_PATH, "1");
>> +        if (rc) {
>> +            LOGE(ERROR, "unable to write %s = 1", DISABLE_UDEV_PATH);
>> +            goto out;
>> +        }
>> +    } else {
>> +        rc = xs_rm(ctx->xsh, t, DISABLE_UDEV_PATH);
>> +        if (rc) {
>> +            LOGE(ERROR, "unable to delete %s", DISABLE_UDEV_PATH);
>> +            goto out;
>> +        }
>> +    }
>
> You could use libxl__xs_{write,rm}_checked here I think.

Yes, when this was written libxl__xs_{write,rm}_checked wasn't there. 
Now I've changed it, although the rest of the code in this function 
still use xs_rm or libxl_xs_write.

>
>> +
>>       xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
>>       xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/name", vm_path), info->name, strlen(info->name));
>>
>> diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
>> index b4a5f1f..612e9db 100644
>> --- a/tools/libxl/libxl_dm.c
>> +++ b/tools/libxl/libxl_dm.c
>> @@ -762,6 +762,10 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
>>       dm_config->nics = guest_config->nics;
>>       dm_config->num_nics = guest_config->num_nics;
>>
>> +    libxl_defbool_set(
>> +&dm_config->c_info.run_hotplug_scripts,
>> +        libxl_defbool_val(guest_config->c_info.run_hotplug_scripts));
>
> This is the same as:
>
> dm_config->c_info.run_hotplug_scripts = guest_config->c_info.run_hotplug_scripts;

Changed, thanks for the comments.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-08 18:32 ` [PATCH v8 00/15] execute hotplug scripts " Ian Campbell
  2012-07-08 18:41   ` Ian Campbell
@ 2012-07-10 11:31   ` Roger Pau Monne
  2012-07-10 15:55     ` Ian Campbell
  1 sibling, 1 reply; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-10 11:31 UTC (permalink / raw)
  To: Ian Campbell; +Cc: xen-devel

Ian Campbell wrote:
> (resending as I don't think I had SMTP setup properly on my laptop -- sorry if you get this twice!)
>
> On Wed, 2012-07-04 at 07:59 -0400, Roger Pau Monne wrote:
>> This new serie (v8) fixes a code refactoring problem that was present
>> in v7 (06/15 changed code introduced by 05/15).
>
>  From somewhere in here I'm seeing timeouts waiting for the b/e to go to
> state 5 when doing cd-insert on an HVM guest. I suspect because this is
> (or should be) turned into a virtual media change rather than an actual
> device remove and insert?
>
> BTW libxl_cdrom_insert hasn't been async'd up yet -- I was actually just
> looking into that when I noticed this.
>
> Ian.
>
>          ~# xl -vvv cd-insert dHVM-1 hdc /scratch/mini.iso,raw
>          libxl: debug: libxl.c:3074:libxl_device_disk_remove: ao 0x8069c30: create: how=(nil) callback=(nil) poller=0x8069a50
>          libxl: debug: libxl_event.c:512:libxl__ev_xswatch_register: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/0: register slotnum=3
>          libxl: debug: libxl.c:3074:libxl_device_disk_remove: ao 0x8069c30: inprogress: poller=0x8069a50, flags=i
>          libxl: debug: libxl_event.c:457:watchfd_callback: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/0: event epath=/local/domain/0/backend/qdisk/43/5632/state
>          libxl: debug: libxl_event.c:600:devstate_watch_callback: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6 still waiting state 5
>
>          [... long wait ...]
>
>          libxl: debug: libxl_event.c:614:devstate_timeout: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6  timed out
>          libxl: debug: libxl_event.c:549:libxl__ev_xswatch_deregister: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/0: deregister slotnum=3
>          libxl: debug: libxl_event.c:561:libxl__ev_xswatch_deregister: watch w=0x8069dcc: deregister unregistered
>          libxl: debug: libxl_event.c:512:libxl__ev_xswatch_register: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/1: register slotnum=3
>          libxl: debug: libxl_event.c:457:watchfd_callback: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/1: event epath=/local/domain/0/backend/qdisk/43/5632/state
>          libxl: debug: libxl_event.c:600:devstate_watch_callback: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6 still waiting state 5
>
>          [... long wait ...]
>
>          libxl: debug: libxl_event.c:614:devstate_timeout: backend /local/domain/0/backend/qdisk/43/5632/state wanted state 6  timed out
>          libxl: debug: libxl_event.c:549:libxl__ev_xswatch_deregister: watch w=0x8069dcc wpath=/local/domain/0/backend/qdisk/43/5632/state token=3/1: deregister slotnum=3
>          libxl: debug: libxl_event.c:561:libxl__ev_xswatch_deregister: watch w=0x8069dcc: deregister unregistered
>          libxl: debug: libxl_device.c:759:device_backend_callback: unable to disconnect device with path /local/domain/0/backend/qdisk/43/5632
>          libxl: debug: libxl_event.c:1434:libxl__ao_complete: ao 0x8069c30: complete, rc=0
>          libxl: debug: libxl_event.c:1406:libxl__ao__destroy: ao 0x8069c30: destroy
>          libxl: debug: libxl.c:1958:libxl_device_disk_add: ao 0x8069c30: create: how=(nil) callback=(nil) poller=0x8069a50
>          libxl: debug: libxl_device.c:255:libxl__device_disk_set_backend: Disk vdev=hdc spec.backend=unknown
>          libxl: debug: libxl_device.c:258:libxl__device_disk_set_backend:      format=raw cdrom=yes
>          libxl: debug: libxl_device.c:209:disk_try_backend: Disk vdev=hdc, backend phy unsuitable as phys path not a block device
>          libxl: debug: libxl_device.c:216:disk_try_backend: Disk vdev=hdc, backend tap unsuitable because blktap not available
>          libxl: debug: libxl_device.c:293:libxl__device_disk_set_backend: Disk vdev=hdc, using backend qdisk
>          libxl: debug: libxl_event.c:1434:libxl__ao_complete: ao 0x8069c30: complete, rc=0
>          libxl: debug: libxl.c:1971:libxl_device_disk_add: ao 0x8069c30: inprogress: poller=0x8069a50, flags=ic
>          libxl: debug: libxl_event.c:1406:libxl__ao__destroy: ao 0x8069c30: destroy
>          xc: debug: hypercall buffer: total allocations:2 total releases:2
>          xc: debug: hypercall buffer: current allocations:0 maximum allocations:2
>          xc: debug: hypercall buffer: cache current size:1
>          xc: debug: hypercall buffer: cache hits:0 misses:1 toobig:1

Yes, this is due to the fact that Qemu (traditional at least) doesn't 
honour the connection/disconnection protocol, so neither removing the 
frontend or setting the backend to "closing" (5), will make Qemu 
disconnect the device. I used to have a special "dev->backend_type == 
QDISK" to skip the waiting, I've added it to my series again, and it 
should solve the waiting problem.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 11:31   ` Roger Pau Monne
@ 2012-07-10 15:55     ` Ian Campbell
  2012-07-10 16:07       ` Roger Pau Monne
  0 siblings, 1 reply; 29+ messages in thread
From: Ian Campbell @ 2012-07-10 15:55 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: xen-devel

On Tue, 2012-07-10 at 07:31 -0400, Roger Pau Monne wrote:
> Ian Campbell wrote:
> > (resending as I don't think I had SMTP setup properly on my laptop -- sorry if you get this twice!)
> >
> > On Wed, 2012-07-04 at 07:59 -0400, Roger Pau Monne wrote:
> >> This new serie (v8) fixes a code refactoring problem that was present
> >> in v7 (06/15 changed code introduced by 05/15).
> >
> >  From somewhere in here I'm seeing timeouts waiting for the b/e to go to
> > state 5 when doing cd-insert on an HVM guest. I suspect because this is
> > (or should be) turned into a virtual media change rather than an actual
> > device remove and insert?
> >
> > BTW libxl_cdrom_insert hasn't been async'd up yet -- I was actually just
> > looking into that when I noticed this.
> >
[...]
> Yes, this is due to the fact that Qemu (traditional at least) doesn't 
> honour the connection/disconnection protocol, so neither removing the 
> frontend or setting the backend to "closing" (5), will make Qemu 
> disconnect the device. I used to have a special "dev->backend_type == 
> QDISK" to skip the waiting, I've added it to my series again, and it 
> should solve the waiting problem.

Actually I think libxl_cdrom_insert is just broken. For an HVM guest
with an emulated CDROM (i..e the normal case, even if you have PV
drivers) then the media change protocol is not to remove the device and
reinsert it. Instead you are supposed to just change the params key. I
have half a patch to do this (as part of the asyncification of the
interface) and I think it will make this special case unnecessary, at
least or the cdrom case.

Perhaps you also need it for the disk case though, I don't know. Or
maybe qemu should be taught to honour the protocol?

Ian

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 15:55     ` Ian Campbell
@ 2012-07-10 16:07       ` Roger Pau Monne
  2012-07-10 16:57         ` Ian Campbell
  0 siblings, 1 reply; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-10 16:07 UTC (permalink / raw)
  To: Ian Campbell; +Cc: xen-devel

Ian Campbell wrote:
> On Tue, 2012-07-10 at 07:31 -0400, Roger Pau Monne wrote:
>> Ian Campbell wrote:
>>> (resending as I don't think I had SMTP setup properly on my laptop -- sorry if you get this twice!)
>>>
>>> On Wed, 2012-07-04 at 07:59 -0400, Roger Pau Monne wrote:
>>>> This new serie (v8) fixes a code refactoring problem that was present
>>>> in v7 (06/15 changed code introduced by 05/15).
>>>  From somewhere in here I'm seeing timeouts waiting for the b/e to go to
>>> state 5 when doing cd-insert on an HVM guest. I suspect because this is
>>> (or should be) turned into a virtual media change rather than an actual
>>> device remove and insert?
>>>
>>> BTW libxl_cdrom_insert hasn't been async'd up yet -- I was actually just
>>> looking into that when I noticed this.
>>>
> [...]
>> Yes, this is due to the fact that Qemu (traditional at least) doesn't 
>> honour the connection/disconnection protocol, so neither removing the 
>> frontend or setting the backend to "closing" (5), will make Qemu 
>> disconnect the device. I used to have a special "dev->backend_type == 
>> QDISK" to skip the waiting, I've added it to my series again, and it 
>> should solve the waiting problem.
> 
> Actually I think libxl_cdrom_insert is just broken. For an HVM guest
> with an emulated CDROM (i..e the normal case, even if you have PV
> drivers) then the media change protocol is not to remove the device and
> reinsert it. Instead you are supposed to just change the params key. I
> have half a patch to do this (as part of the asyncification of the
> interface) and I think it will make this special case unnecessary, at
> least or the cdrom case.
> 
> Perhaps you also need it for the disk case though, I don't know. Or
> maybe qemu should be taught to honour the protocol?

I'm working on making vfb/vkb async, once that is done I can take a look
on this, but since the device removal is done after Qemu has been killed
I don't have much expectations that this is going to work (at least for
the shutdown/destroy case).

The block-attach/detach case might work with this approach.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 16:07       ` Roger Pau Monne
@ 2012-07-10 16:57         ` Ian Campbell
  2012-07-10 17:23           ` Roger Pau Monne
  0 siblings, 1 reply; 29+ messages in thread
From: Ian Campbell @ 2012-07-10 16:57 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: xen-devel

On Tue, 2012-07-10 at 12:07 -0400, Roger Pau Monne wrote:

> I'm working on making vfb/vkb async, once that is done I can take a look
> on this,

Cool. BTW is the other stuff ready? I guess it is waiting on IanJ to
review again?

>  but since the device removal is done after Qemu has been killed
> I don't have much expectations that this is going to work (at least for
> the shutdown/destroy case).

Right, that's an interesting problem. I wonder if qemu should set state
= 5 (or 6?) for all its backends as it does a graceful shutdown?

> The block-attach/detach case might work with this approach.

Might be good for that case.

Ian.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 16:57         ` Ian Campbell
@ 2012-07-10 17:23           ` Roger Pau Monne
  2012-07-10 17:30             ` Ian Campbell
  2012-07-10 17:40             ` Ian Campbell
  0 siblings, 2 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-10 17:23 UTC (permalink / raw)
  To: Ian Campbell; +Cc: xen-devel

Ian Campbell wrote:
> On Tue, 2012-07-10 at 12:07 -0400, Roger Pau Monne wrote:
> 
>> I'm working on making vfb/vkb async, once that is done I can take a look
>> on this,
> 
> Cool. BTW is the other stuff ready? I guess it is waiting on IanJ to
> review again?

I've changed the series according to your comments, and I'm waiting for
IanJ review. I also have NetBSD hotplug & vfb/vkb series ready, that
will come after this is committed. If you want I can start posting them,
I didn't do so yet because they depend a lot on this.

>>  but since the device removal is done after Qemu has been killed
>> I don't have much expectations that this is going to work (at least for
>> the shutdown/destroy case).
> 
> Right, that's an interesting problem. I wonder if qemu should set state
> = 5 (or 6?) for all its backends as it does a graceful shutdown?

Sorry, what I've said is not accurate. Qemu sets state to "closed" on
shutdown/destroy, what doesn't seem to work is the disconnection by
setting online to 0 or status to 5 while Qemu is running.

Maybe we should not call libxl_device_disk_add/remove when the backend
is Qemu for cdrom_insert and instead perform the params remove/addition
that you exposed before directly on cdrom_insert.

>> The block-attach/detach case might work with this approach.
> 
> Might be good for that case.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 17:23           ` Roger Pau Monne
@ 2012-07-10 17:30             ` Ian Campbell
  2012-07-10 17:39               ` Roger Pau Monne
  2012-07-10 17:40             ` Ian Campbell
  1 sibling, 1 reply; 29+ messages in thread
From: Ian Campbell @ 2012-07-10 17:30 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: xen-devel

On Tue, 2012-07-10 at 13:23 -0400, Roger Pau Monne wrote:
> Ian Campbell wrote:
> > On Tue, 2012-07-10 at 12:07 -0400, Roger Pau Monne wrote:
> > 
> >> I'm working on making vfb/vkb async, once that is done I can take a look
> >> on this,
> > 
> > Cool. BTW is the other stuff ready? I guess it is waiting on IanJ to
> > review again?
> 
> I've changed the series according to your comments, and I'm waiting for
> IanJ review. I also have NetBSD hotplug & vfb/vkb series ready, that
> will come after this is committed. If you want I can start posting them,
> I didn't do so yet because they depend a lot on this.

I think there's no harm in posting stuff earlier if it is ready.
Especially since ian J and myself are at debconf and unlikely to be
doing a whole lot of review etc.

> >>  but since the device removal is done after Qemu has been killed
> >> I don't have much expectations that this is going to work (at least for
> >> the shutdown/destroy case).
> > 
> > Right, that's an interesting problem. I wonder if qemu should set state
> > = 5 (or 6?) for all its backends as it does a graceful shutdown?
> 
> Sorry, what I've said is not accurate. Qemu sets state to "closed" on
> shutdown/destroy, what doesn't seem to work is the disconnection by
> setting online to 0 or status to 5 while Qemu is running.
> 
> Maybe we should not call libxl_device_disk_add/remove when the backend
> is Qemu for cdrom_insert and instead perform the params remove/addition
> that you exposed before directly on cdrom_insert.

yes, precisely. I've got half a skanky patch to do this ...

Ian.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 17:30             ` Ian Campbell
@ 2012-07-10 17:39               ` Roger Pau Monne
  0 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-10 17:39 UTC (permalink / raw)
  To: Ian Campbell; +Cc: xen-devel

Ian Campbell wrote:
> On Tue, 2012-07-10 at 13:23 -0400, Roger Pau Monne wrote:
>> Ian Campbell wrote:
>>> On Tue, 2012-07-10 at 12:07 -0400, Roger Pau Monne wrote:
>>>
>>>> I'm working on making vfb/vkb async, once that is done I can take a look
>>>> on this,
>>> Cool. BTW is the other stuff ready? I guess it is waiting on IanJ to
>>> review again?
>> I've changed the series according to your comments, and I'm waiting for
>> IanJ review. I also have NetBSD hotplug & vfb/vkb series ready, that
>> will come after this is committed. If you want I can start posting them,
>> I didn't do so yet because they depend a lot on this.
> 
> I think there's no harm in posting stuff earlier if it is ready.
> Especially since ian J and myself are at debconf and unlikely to be
> doing a whole lot of review etc.

OK, I'm finishing testing the NetBSD series, and I will post them
tomorrow. I still have to figure out how to test the vkb/vfb stuff,
which I will try to post tomorrow also, once I've tested it. I just
didn't want to gather Acks that I would had to drop if the underlying
code changed.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 17:23           ` Roger Pau Monne
  2012-07-10 17:30             ` Ian Campbell
@ 2012-07-10 17:40             ` Ian Campbell
  2012-07-11  9:08               ` Roger Pau Monne
  1 sibling, 1 reply; 29+ messages in thread
From: Ian Campbell @ 2012-07-10 17:40 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: xen-devel

On Tue, 2012-07-10 at 13:23 -0400, Roger Pau Monne wrote:
> >> I'm working on making vfb/vkb async, once that is done I can take a look
> >> on this,
> > 
> > Cool. BTW is the other stuff ready? I guess it is waiting on IanJ to
> > review again?
> 
> I've changed the series according to your comments, and I'm waiting for
> IanJ review.

BTW, an interesting enhancement might be to read
the /local/domain/0/backend/vif/23/0/hotplug-error node and print it
out. Currently with the "hotplug: vif: fail if a duplicate vifname is
used" patch that I've just posted xl continues on despite the failure.
With this series this is improved to:
        # xl cr /etc/xen//debian-x86_32p-1
        Parsing config from /etc/xen//debian-x86_32p-1
        libxl: warning: libxl_exec.c:118:libxl_report_child_exitstatus: /etc/xen/scripts/vif-bridge online [11939] exited with error status 1
        
but with xm I get:
        # xm cr /etc//xen/debian-HVM-1 
        Using config file "/etc//xen/debian-HVM-1".
        Error: Device 0 (vif) could not be connected. Cannot rename interface vif20.0. An interface with name foo already exists.
        
which is much friendlier.

Also it seems that despite xl reporting the error with you patches the
domain is still running, although without networking... If a hotplug
script fails can we abort and destroy the partially constructed domain
instead?

Ian.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v8 00/15] execute hotplug scripts from libxl
  2012-07-10 17:40             ` Ian Campbell
@ 2012-07-11  9:08               ` Roger Pau Monne
  0 siblings, 0 replies; 29+ messages in thread
From: Roger Pau Monne @ 2012-07-11  9:08 UTC (permalink / raw)
  To: Ian Campbell; +Cc: xen-devel

Ian Campbell wrote:
> On Tue, 2012-07-10 at 13:23 -0400, Roger Pau Monne wrote:
>>>> I'm working on making vfb/vkb async, once that is done I can take a look
>>>> on this,
>>> Cool. BTW is the other stuff ready? I guess it is waiting on IanJ to
>>> review again?
>> I've changed the series according to your comments, and I'm waiting for
>> IanJ review.
> 
> BTW, an interesting enhancement might be to read
> the /local/domain/0/backend/vif/23/0/hotplug-error node and print it
> out. Currently with the "hotplug: vif: fail if a duplicate vifname is
> used" patch that I've just posted xl continues on despite the failure.
> With this series this is improved to:
>         # xl cr /etc/xen//debian-x86_32p-1
>         Parsing config from /etc/xen//debian-x86_32p-1
>         libxl: warning: libxl_exec.c:118:libxl_report_child_exitstatus: /etc/xen/scripts/vif-bridge online [11939] exited with error status 1
>         
> but with xm I get:
>         # xm cr /etc//xen/debian-HVM-1 
>         Using config file "/etc//xen/debian-HVM-1".
>         Error: Device 0 (vif) could not be connected. Cannot rename interface vif20.0. An interface with name foo already exists.
>         
> which is much friendlier.

Ok, so on error I will read "hotplug-error" and add it to the error
message. I think this is a Linux only thing, but it won't hurt to add it.

> 
> Also it seems that despite xl reporting the error with you patches the
> domain is still running, although without networking... If a hotplug
> script fails can we abort and destroy the partially constructed domain
> instead?

Yes, I will change that and print and error and exit instead of just
printing a warning.

Thanks for the report.

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2012-07-11  9:08 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-04 11:59 [PATCH v8 00/15] execute hotplug scripts from libxl Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 01/15] libxl: change ao_device_remove to ao_device Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 02/15] libxl: move device model creation prototypes Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 03/15] libxl: convert libxl_domain_destroy to an async op Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 04/15] libxl: move bootloader data strucutres and prototypes Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 05/15] libxl: refactor disk addition to take a helper Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 06/15] libxl: convert libxl__device_disk_local_attach to an async op Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 07/15] libxl: rename vifs to nics Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 08/15] libxl: convert libxl_device_disk_add to an async op Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 09/15] libxl: convert libxl_device_nic_add to an async operation Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 10/15] libxl: add option to choose who executes hotplug scripts Roger Pau Monne
2012-07-06 10:28   ` Ian Campbell
2012-07-10  9:20     ` Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 11/15] libxl: rename _IOEMU nic type to VIF_IOEMU Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 12/15] libxl: set correct nic type depending on the guest Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 13/15] libxl: use libxl__xs_path_cleanup on device_destroy Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 14/15] libxl: call hotplug scripts for disk devices from libxl Roger Pau Monne
2012-07-04 11:59 ` [PATCH v8 15/15] libxl: call hotplug scripts for nic " Roger Pau Monne
2012-07-08 18:32 ` [PATCH v8 00/15] execute hotplug scripts " Ian Campbell
2012-07-08 18:41   ` Ian Campbell
2012-07-10 11:31   ` Roger Pau Monne
2012-07-10 15:55     ` Ian Campbell
2012-07-10 16:07       ` Roger Pau Monne
2012-07-10 16:57         ` Ian Campbell
2012-07-10 17:23           ` Roger Pau Monne
2012-07-10 17:30             ` Ian Campbell
2012-07-10 17:39               ` Roger Pau Monne
2012-07-10 17:40             ` Ian Campbell
2012-07-11  9:08               ` Roger Pau Monne

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.