Skip to contentSkip to conversation

execute hotplug scripts from libxl v10

PATCH Superseded Roger Pau Monne 3 participants 44 messages Jul 20, 2012 – Jul 23, 2012 Arm/actions semi architecture

v10 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

PATCH v10 1/19 execute hotplug scripts from libxl v10

Changes are available in the git repository at:
git://xenbits.xen.org/people/royger/xen.git hotplug.v10

Roger Pau Monne (19):
*libxl: check backend state before setting it to "closing"
libxl: change ao_device_remove to ao_device
*libxl: move device model creation prototypes *libxl: convert libxl_domain_destroy to an async op *libxl: move bootloader data strucutres and prototypes
libxl: refactor disk addition to take a helper
libxl: convert libxl__device_disk_local_attach to an async op
*libxl: rename vifs to nics
*libxl: convert libxl_device_disk_add to an async op *libxl: convert libxl_device_nic_add to an async operation *libxl: add option to choose who executes hotplug scripts *libxl: rename _IOEMU nic type to VIF_IOEMU
libxl: set correct nic type depending on the guest
*libxl: use libxl__xs_path_cleanup on device_destroy *libxl: call hotplug scripts for disk devices from libxl *libxl: call hotplug scripts for nic devices from libxl
libxl: convert libxl_device_vkb_add to an async operation
libxl: convert libxl_device_vfb_add to an async operation
xl: main_blockdetach don't call destroy if remove succeeds

* Acked

#1 lore raw

PATCH v10 1/19 libxl: check backend state before setting it to "closing"

Check backend state before unconditionally setting it to "closing" (5), since it might already be in "closed" (6).

---
 tools/libxl/libxl_device.c |   59 ++++++++++++++++++++++++++-----------------
 1 files changed, 36 insertions(+), 23 deletions(-)
tools/libxl/libxl_device.c +36 -23
index 3da60e1..d33ac68 100644
@@ -451,33 +451,48 @@ int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao,
                                   libxl__device *dev)
 {
     AO_GC;
-    libxl_ctx *ctx = libxl__gc_owner(gc);
-    xs_transaction_t t;
+    xs_transaction_t t = 0;
     char *be_path = libxl__device_backend_path(gc, dev);
     char *state_path = libxl__sprintf(gc, "%s/state", be_path);
-    char *state = libxl__xs_read(gc, XBT_NULL, state_path);
+    char *online_path = GCSPRINTF("%s/online", be_path);
+    const char *state;
+
     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;
-    }
+    for (;;) {
+        rc = libxl__xs_transaction_start(gc, &t);
+        if (rc) {
+            LOG(ERROR, "unable to start transaction");
+            goto out_fail;
+        }
 
-retry_transaction:
-    t = xs_transaction_start(ctx->xsh);
-    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)) {
-        if (errno == EAGAIN)
-            goto retry_transaction;
-        else {
-            rc = ERROR_FAIL;
+        rc = libxl__xs_read_checked(gc, t, state_path, &state);
+        if (rc) {
+            LOG(ERROR, "unable to read device state from path %s", state_path);
             goto out_fail;
         }
+
+        /*
+         * Check if device is already in "closed" state, in which case
+         * it should not be changed.
+         */
+         if (state && atoi(state) != XenbusStateClosed) {
+            rc = libxl__xs_write_checked(gc, t, online_path, "0");
+            if (rc) {
+                LOG(ERROR, "unable to write to xenstore path %s", online_path);
+                goto out_fail;
+            }
+            rc = libxl__xs_write_checked(gc, t, state_path, "5");
+            if (rc) {
+                LOG(ERROR, "unable to write to xenstore path %s", state_path);
+                goto out_fail;
+            }
+        }
+
+        rc = libxl__xs_transaction_commit(gc, &t);
+        if (!rc) break;
+        if (rc < 0) goto out_fail;
     }
 
     libxl__device_destroy_tapdisk(gc, be_path);
@@ -491,16 +506,14 @@ retry_transaction:
                                  LIBXL_DESTROY_TIMEOUT * 1000);
     if (rc) goto out_fail;
 
+    libxl__ao_complete(egc, ao, 0);
     return 0;
 
  out_fail:
     assert(rc);
+    libxl__xs_transaction_abort(gc, &t);
     device_remove_cleanup(gc, aorm);
     return rc;
-
- out_ok:
-    libxl__ao_complete(egc, ao, 0);
-    return 0;
 }
 
 static void device_remove_callback(libxl__egc *egc, libxl__ev_devstate *ds,
Signature · 1 line
1.7.7.5 (Apple Git-26)
#2 lore raw

PATCH v10 2/19 libxl: change ao_device_remove to ao_device

In reply to 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 v9:

* Fixed and documented behaviour with Qemu backed devices.

* Fix Ocaml bindings.

Changes since v8:

* Don't wait for QDISK, VKBD or VFB to disconnect, since Qemu doesn't honour the disconnection protocol.

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.

---
 tools/libxl/libxl.c                  |  229 +++++++++++++---------------------
 tools/libxl/libxl.h                  |   15 ++-
 tools/libxl/libxl_device.c           |  193 +++++++++++++++++++++++------
 tools/libxl/libxl_internal.h         |  143 ++++++++++++++++++++-
 tools/libxl/xl_cmdimpl.c             |    2 +-
 tools/ocaml/libs/xl/xenlight_stubs.c |    4 +-
 6 files changed, 390 insertions(+), 196 deletions(-)
tools/libxl/libxl.c +86 -143
index 4277745..ebc314b 100644
@@ -1503,6 +1503,31 @@ 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) {
+        if (aodev->dev) {
+            LOG(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);
+        } else {
+            LOG(ERROR, "unable to %s device",
+                       aodev->action == DEVICE_CONNECT ? "add" : "remove");
+        }
+        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 +1705,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 +2069,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 +2239,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 +2565,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 +2662,66 @@ 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 */
+/* The following functions are defined:
+ * libxl_device_disk_remove
+ * libxl_device_disk_destroy
+ * libxl_device_nic_remove
+ * libxl_device_nic_destroy
+ * libxl_device_vkb_remove
+ * libxl_device_vkb_destroy
+ * libxl_device_vfb_remove
+ * libxl_device_vfb_destroy
+ */
+#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
 
 /******************************************************************************/
 
tools/libxl/libxl.h +11 -4
index f5a8f87..c2d5c06 100644
@@ -679,7 +679,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,
@@ -696,7 +697,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,
@@ -707,14 +710,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);
tools/libxl/libxl_device.c +154 -39

254 lines not on this page · raw message

tools/libxl/libxl_internal.h +136 -7

167 lines not on this page · raw message

tools/libxl/xl_cmdimpl.c +1 -1
index e2aa8592..29523e1 100644
@@ -5416,7 +5416,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;
 }
 
tools/ocaml/libs/xl/xenlight_stubs.c +2 -2
index c712b2b..53751b1 100644
@@ -351,7 +351,7 @@ value stub_xl_device_vkb_destroy(value info, value domid)
 	device_vkb_val(&gc, &lg, &c_info, info);
 
 	INIT_CTX();
-	ret = libxl_device_vkb_destroy(ctx, Int_val(domid), &c_info);
+	ret = libxl_device_vkb_destroy(ctx, Int_val(domid), &c_info, 0);
 	if (ret != 0)
 		failwith_xl("vkb_hard_shutdown", &lg);
 	FREE_CTX();
@@ -405,7 +405,7 @@ value stub_xl_device_vfb_destroy(value info, value domid)
 	device_vfb_val(&gc, &lg, &c_info, info);
 
 	INIT_CTX();
-	ret = libxl_device_vfb_destroy(ctx, Int_val(domid), &c_info);
+	ret = libxl_device_vfb_destroy(ctx, Int_val(domid), &c_info, 0);
 	if (ret != 0)
 		failwith_xl("vfb_hard_shutdown", &lg);
 	FREE_CTX();
Signature · 1 line
1.7.7.5 (Apple Git-26)
Acked-by on v10 2/19
#3 lore raw

Re: [PATCH v10 02/19] libxl: change ao_device_remove to ao_device

Roger Pau Monne writes ("[PATCH v10 02/19] libxl: change ao_device_remove to ao_device"):

Quoted 5 lines

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.

Thanks.

tools/libxl/libxl_device.c int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao, · line 579

Quoted 10 lines
tools/libxl/libxl_device.c +10 -0
+static void device_qemu_timeout(libxl__egc *egc, libxl__ev_time *ev,
+                                const struct timeval *requested_abs)
+{
+    libxl__ao_device *aodev = CONTAINER_OF(ev, *aodev, timeout);
+    STATE_AO_GC(aodev->ao);
+    char *be_path = libxl__device_backend_path(gc, aodev->dev);
+    char *state_path = GCSPRINTF("%s/state", be_path);
+    int rc = 0;
+
+    libxl__ev_time_deregister(gc, &aodev->timeout);

This last call is unnecessary, but harmless. A timeout is already deregistered when the event system calls the handler.

Ian.

#4 lore raw

PATCH v10 3/19 libxl: move device model creation prototypes

In reply to 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.

---
 tools/libxl/libxl_internal.h |   75 ++++++++++++++++++++---------------------
 1 files changed, 37 insertions(+), 38 deletions(-)
tools/libxl/libxl_internal.h +37 -38
index 5d985ff..101e7fb 100644
@@ -1150,44 +1150,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
@@ -2136,6 +2098,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;
Signature · 1 line
1.7.7.5 (Apple Git-26)
#5 lore raw

PATCH v10 4/19 libxl: convert libxl_domain_destroy to an async op

In reply to 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.

---
 tools/libxl/libxl.c               |  176 +++++++++++++++++++++++++++++++++++--
 tools/libxl/libxl.h               |    3 +-
 tools/libxl/libxl_create.c        |   29 +++++-
 tools/libxl/libxl_device.c        |  149 +++++++++++++++++++++++++++----
 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, 488 insertions(+), 84 deletions(-)
tools/libxl/libxl.c +168 -8
index ebc314b..0600ce4 100644
@@ -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,
tools/libxl/libxl.h +2 -1
index c2d5c06..5c819f1 100644
@@ -527,7 +527,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 */
tools/libxl/libxl_create.c +25 -4
index 294a73f..16d32c2 100644
@@ -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 {
tools/libxl/libxl_device.c +131 -18

201 lines not on this page · raw message

tools/libxl/libxl_dm.c +40 -45

113 lines not on this page · raw message

tools/libxl/libxl_internal.h +115 -1

162 lines not on this page · raw message

tools/libxl/xl_cmdimpl.c +6 -6
index 29523e1..61791e1 100644
@@ -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;
 
@@ -2016,7 +2016,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;
     }
 
@@ -2581,7 +2581,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); }
 }
 
@@ -2855,7 +2855,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);
 }
@@ -3112,7 +3112,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);
 
@@ -3265,7 +3265,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);
tools/python/xen/lowlevel/xl/xl.c +1 -1
index b68b55a..553fc58 100644
@@ -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;
     }
Signature · 1 line
1.7.7.5 (Apple Git-26)
#6 lore raw

PATCH v10 5/19 libxl: move bootloader data strucutres and prototypes

In reply to 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.

---
 tools/libxl/libxl_internal.h |  322 +++++++++++++++++++++---------------------
 1 files changed, 161 insertions(+), 161 deletions(-)
tools/libxl/libxl_internal.h +161 -161
index 562ca61..52d79a9 100644
@@ -1747,6 +1747,167 @@ _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;
+    /* Bodge for Qemu devices */
+    libxl__ev_time timeout;
+    /* 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;
+};
+
+/*
+ * Algorithm for handling device removal (including domain
+ * destruction).  This is somewhat subtle because we may already have
+ * killed the domain and caused the death of qemu.
+ *
+ * In current versions of qemu there is no mechanism for ensuring that
+ * the resources used by its devices (both emulated and any PV devices
+ * provided by qemu) are freed (eg, fds closed) before it shuts down,
+ * and no confirmation from a terminating qemu back to the toolstack.
+ *
+ * This will need to be fixed in Xen 4.3.  In the meantime (Xen 4.2)
+ * we implement a bodge.
+ *
+ *      WE WANT TO UNPLUG         WE WANT TO SHUT DOWN OR DESTROY
+ *                    |                           |
+ *                    |             LIBXL SENDS SIGHUP TO QEMU
+ *                    |      .....................|........................
+ *                    |      : XEN 4.3+ PLANNED   |                       :
+ *                    |      :      QEMU TEARS DOWN ALL DEVICES           :
+ *                    |      :      FREES RESOURCES (closing fds)         :
+ *                    |      :      SETS PV BACKENDS TO STATE 5,          :
+ *                    |      :       waits for PV frontends to shut down  :
+ *                    |      :       SETS PV BACKENDS TO STATE 6          :
+ *                    |      :                    |                       :
+ *                    |      :      QEMU NOTIFIES TOOLSTACK (via          :
+ *                    |      :       xenstore) that it is exiting         :
+ *                    |      :      QEMU EXITS (parent may be init)       :
+ *                    |      :                    |                       :
+ *                    |      :        TOOLSTACK WAITS FOR QEMU            :
+ *                    |      :        notices qemu has finished           :
+ *                    |      :....................|.......................:
+ *                    |      .--------------------'
+ *                    V      V
+ *                  for each device
+ *                 we want to unplug/remove
+ *       ..................|...........................................
+ *       :                 V                       XEN 4.2 RACY BODGE :
+ *       :      device is provided by    qemu                         :
+ *       :            |            `-----------.                      :
+ *       :   something|                        V                      :
+ *       :    else, eg|             domain (that is domain for which  :
+ *       :     blkback|              this PV device is the backend,   :
+ *       :            |              which might be the stub dm)      :
+ *       :            |                is still alive?                :
+ *       :            |                  |        |                   :
+ *       :            |                  |alive   |dead               :
+ *       :            |<-----------------'        |                   :
+ *       :            |    hopefully qemu is      |                   :
+ *       :            |       still running       |                   :
+ *       :............|.................          |                   :
+ *             ,----->|                :     we may be racing         :
+ *             |    backend state?     :      with qemu's death       :
+ *             ^      |         |      :          |                   :
+ *     xenstore|      |other    |6     :      WAIT 2.0s               :
+ *     conflict|      |         |      :       TIMEOUT                :
+ *             |   WRITE B.E.   |      :          |                   :
+ *             |    STATE:=5    |      :     hopefully qemu has       :
+ *             `---'  |         |      :      gone by now and         :
+ *                    |ok       |      :      freed its resources     :
+ *                    |         |      :          |                   :
+ *              WAIT FOR        |      :     SET B.E.                 :
+ *              STATE==6        |      :      STATE:=6                :
+ *              /     |         |      :..........|...................:
+ *      timeout/    ok|         |                 |
+ *            /       |         |                 |
+ *           |    RUN HOTPLUG <-'<----------------'
+ *           |      SCRIPT
+ *           |        |
+ *           `---> NUKE
+ *                  BACKEND
+ *                    |
+ *                   DONE.
+ */
+
+/* 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.
+ *
+ * Once finished, aodev->callback will be executed.
+ */
+_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;
@@ -1968,167 +2129,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;
-    /* Bodge for Qemu devices */
-    libxl__ev_time timeout;
-    /* 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;
-};
-
-/*
- * Algorithm for handling device removal (including domain
- * destruction).  This is somewhat subtle because we may already have
- * killed the domain and caused the death of qemu.
- *
- * In current versions of qemu there is no mechanism for ensuring that
- * the resources used by its devices (both emulated and any PV devices
- * provided by qemu) are freed (eg, fds closed) before it shuts down,
- * and no confirmation from a terminating qemu back to the toolstack.
- *
- * This will need to be fixed in Xen 4.3.  In the meantime (Xen 4.2)
- * we implement a bodge.
- *
- *      WE WANT TO UNPLUG         WE WANT TO SHUT DOWN OR DESTROY
- *                    |                           |
- *                    |             LIBXL SENDS SIGHUP TO QEMU
- *                    |      .....................|........................
- *                    |      : XEN 4.3+ PLANNED   |                       :
- *                    |      :      QEMU TEARS DOWN ALL DEVICES           :
- *                    |      :      FREES RESOURCES (closing fds)         :
- *                    |      :      SETS PV BACKENDS TO STATE 5,          :
- *                    |      :       waits for PV frontends to shut down  :
- *                    |      :       SETS PV BACKENDS TO STATE 6          :
- *                    |      :                    |                       :
- *                    |      :      QEMU NOTIFIES TOOLSTACK (via          :
- *                    |      :       xenstore) that it is exiting         :
- *                    |      :      QEMU EXITS (parent may be init)       :
- *                    |      :                    |                       :
- *                    |      :        TOOLSTACK WAITS FOR QEMU            :
- *                    |      :        notices qemu has finished           :
- *                    |      :....................|.......................:
- *                    |      .--------------------'
- *                    V      V
- *                  for each device
- *                 we want to unplug/remove
- *       ..................|...........................................
- *       :                 V                       XEN 4.2 RACY BODGE :
- *       :      device is provided by    qemu                         :
- *       :            |            `-----------.                      :
- *       :   something|                        V                      :
- *       :    else, eg|             domain (that is domain for which  :
- *       :     blkback|              this PV device is the backend,   :
- *       :            |              which might be the stub dm)      :
- *       :            |                is still alive?                :
- *       :            |                  |        |                   :
- *       :            |                  |alive   |dead               :
- *       :            |<-----------------'        |                   :
- *       :            |    hopefully qemu is      |                   :
- *       :            |       still running       |                   :
- *       :............|.................          |                   :
- *             ,----->|                :     we may be racing         :
- *             |    backend state?     :      with qemu's death       :
- *             ^      |         |      :          |                   :
- *     xenstore|      |other    |6     :      WAIT 2.0s               :
- *     conflict|      |         |      :       TIMEOUT                :
- *             |   WRITE B.E.   |      :          |                   :
- *             |    STATE:=5    |      :     hopefully qemu has       :
- *             `---'  |         |      :      gone by now and         :
- *                    |ok       |      :      freed its resources     :
- *                    |         |      :          |                   :
- *              WAIT FOR        |      :     SET B.E.                 :
- *              STATE==6        |      :      STATE:=6                :
- *              /     |         |      :..........|...................:
- *      timeout/    ok|         |                 |
- *            /       |         |                 |
- *           |    RUN HOTPLUG <-'<----------------'
- *           |      SCRIPT
- *           |        |
- *           `---> NUKE
- *                  BACKEND
- *                    |
- *                   DONE.
- */
-
-/* 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.
- *
- * Once finished, aodev->callback will be executed.
- */
-_hidden void libxl__initiate_device_remove(libxl__egc *egc,
-                                           libxl__ao_device *aodev);
-
 /*----- Domain destruction -----*/
 
 /* Domain destruction has been split into two functions:
Signature · 1 line
1.7.7.5 (Apple Git-26)
Acked-by on v10 5/19
#7 lore raw

Re: [PATCH v10 05/19] libxl: move bootloader data strucutres and prototypes

Roger Pau Monne writes ("[PATCH v10 05/19] libxl: move bootloader data strucutres and prototypes"):

Quoted 5 lines

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.

#8 lore raw

PATCH v10 6/19 libxl: refactor disk addition to take a helper

In reply to 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 v9:

* Added better comment for device_disk_add parameters.

* Changed char *fn to void *get_vdev_user

* Removed error log message.

Changes since v7:

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

---
 tools/libxl/libxl.c          |  266 +++++++++++++++++++++++-------------------
 tools/libxl/libxl_internal.h |    2 +-
 2 files changed, 145 insertions(+), 123 deletions(-)
tools/libxl/libxl.c +144 -122
index 0600ce4..8df457e 100644
@@ -1738,115 +1738,152 @@ 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
+ *
+ * The (optionally) passed function get_vdev_user will be used to
+ * set the vdev the disk should be attached to. When it is set the caller
+ * must also pass blkdev_start, which will be passed to get_vdev_user.
+ *
+ * The passed get_vdev_user function is also in charge of printing
+ * the corresponding error message when appropiate.
+ */
+static int device_disk_add(libxl__gc *gc, uint32_t domid,
+                           libxl_device_disk *disk,
+                           void *get_vdev_user(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;
 
-    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;
-    }
+        if (get_vdev_user) {
+            assert(blkdev_start);
+            disk->vdev = (char *) get_vdev_user(gc, blkdev_start, t);
+            if (disk->vdev == NULL) {
+                rc = ERROR_FAIL;
+                goto out;
+            }
+        }
 
-    if (disk->script) {
-        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "External block scripts"
-                   " not yet supported, sorry");
-        rc = ERROR_INVAL;
-        goto out_free;
-    }
+        rc = libxl__device_disk_setdefault(gc, disk);
+        if (rc) goto out;
 
-    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 (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;
+        }
 
-    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));
+        if (disk->script) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "External block scripts"
+                       " not yet supported, sorry");
+            rc = ERROR_INVAL;
+            goto out_free;
+        }
 
-            flexarray_append(back, "params");
-            flexarray_append(back, dev);
+        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;
+        }
 
-            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));
+        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));
 
-            /* 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, "params");
+                flexarray_append(back, dev);
 
-    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");
+                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(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(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");
 
-    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;
 
@@ -1854,13 +1891,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;
 }
@@ -2118,9 +2162,8 @@ 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;
 
     if (in_disk->pdev_path == NULL)
         return NULL;
@@ -2164,27 +2207,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,
+                                    (void *) libxl__alloc_vdev, blkdev_start)) {
+                    LOG(ERROR, "libxl_device_disk_add failed");
                     goto out;
                 }
                 dev = GCSPRINTF("/dev/%s", disk->vdev);
@@ -2213,10 +2238,7 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
     return ret;
 
  out:
-    if (t != XBT_NULL)
-        xs_transaction_end(ctx->xsh, t, 1);
-    else
-        libxl__device_disk_local_detach(gc, disk);
+    libxl__device_disk_local_detach(gc, disk);
     return NULL;
 }
 
tools/libxl/libxl_internal.h +1 -1
index 52d79a9..4f3a232 100644
@@ -1298,7 +1298,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
Signature · 1 line
1.7.7.5 (Apple Git-26)
#9 lore raw

Re: [PATCH v10 06/19] libxl: refactor disk addition to take a helper

Roger Pau Monne writes ("[PATCH v10 06/19] libxl: refactor disk addition to take a helper"):

Quoted 6 lines

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.

...

Quoted 6 lines
tools/libxl/libxl.c +6 -0
+/* Specific function called directly only by local disk attach,
+ * all other users should instead use the regular
+ * libxl__device_disk_add wrapper
+ *
+ * The (optionally) passed function get_vdev_user will be used to
+ * set the vdev the disk should be attached to. When it is set the caller

tools/libxl/libxl.c int libxl__device_from_disk(libxl__gc *gc, uint32_t domid, · line 1756

Quoted 10 lines
tools/libxl/libxl.c +10 -0
+ * must also pass blkdev_start, which will be passed to get_vdev_user.
+ *
+ * The passed get_vdev_user function is also in charge of printing
+ * the corresponding error message when appropiate.
+ */
+static int device_disk_add(libxl__gc *gc, uint32_t domid,
+                           libxl_device_disk *disk,
+                           void *get_vdev_user(libxl__gc *, const char *,
+                                               xs_transaction_t),
+                           const char *blkdev_start)

Uh, I meant:
+ char *get_vdev(libxl__gc *, void *user,
+ xs_transaction_t),
+ void *get_vdev_user)

I was trying to suggest that device_disk_add shouldn't need to know the meaning of the parameter which you've called blkdev_start.

I think what you have now is even odder than before.

Ian.

#10 lore raw

PATCH v10 7/19 libxl: convert libxl__device_disk_local_attach to an async op

In reply to 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 v9:

* Fixed state changes comments on header of functions.

* Rework libxl__device_disk_local_init to just set rc to 0, and perform the initialization in the callers context.

* If attach fails, perform a detach, so the state of the local_device on the callback is Attached if rc == 0, or Idle if rc != 0.

* Free diskpath inside the detach callback.

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 <[email protected]>
Cc Stefano Stabellini <[email protected]>
---
 tools/libxl/libxl.c            |  108 ++++++++++++++++++++++++++++++++--------
 tools/libxl/libxl_bootloader.c |   73 ++++++++++++++++++++++-----
 tools/libxl/libxl_internal.h   |   72 +++++++++++++++++++++-----
 3 files changed, 204 insertions(+), 49 deletions(-)
tools/libxl/libxl.c +87 -21
index 8df457e..bd64e0c 100644
@@ -2154,19 +2154,19 @@ 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_initiate_attach(libxl__egc *egc,
+                                     libxl__disk_local_state *dls)
 {
-    libxl_ctx *ctx = gc->owner;
+    STATE_AO_GC(dls->ao);
+    libxl_ctx *ctx = CTX;
     char *dev = NULL, *be_path = NULL;
-    char *ret = NULL;
     int rc;
     libxl__device device;
+    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);
@@ -2202,6 +2202,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;
@@ -2210,6 +2211,7 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
                 if (device_disk_add(gc, LIBXL_TOOLSTACK_DOMID, disk,
                                     (void *) libxl__alloc_vdev, blkdev_start)) {
                     LOG(ERROR, "libxl_device_disk_add failed");
+                    rc = ERROR_FAIL;
                     goto out;
                 }
                 dev = GCSPRINTF("/dev/%s", disk->vdev);
@@ -2221,6 +2223,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;
     }
 
@@ -2234,39 +2237,102 @@ 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:
-    libxl__device_disk_local_detach(gc, disk);
-    return NULL;
+    assert(rc);
+    dls->rc = rc;
+    libxl__device_disk_local_initiate_detach(egc, dls);
+    return;
 }
 
-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) goto out;
 
     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;
+            /* disk->vdev == NULL; fall through */
         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;
+            goto out;
     }
 
+out:
+    if (dls->diskpath) {
+        free(dls->diskpath);
+        dls->diskpath = 0;
+    }
+    /*
+     * If there was an error in dls->rc, it means we have been called from
+     * a failed execution of libxl__device_disk_local_initiate_attach,
+     * so return the original error.
+     */
+    rc = dls->rc ? dls->rc : rc;
+    dls->callback(egc, dls, rc);
+    return;
+}
 
-    return rc;
+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);
+    int rc;
+
+    if (dls->diskpath) {
+        free(dls->diskpath);
+        dls->diskpath = 0;
+    }
+
+    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:
+    /*
+     * If there was an error in dls->rc, it means we have been called from
+     * a failed execution of libxl__device_disk_local_initiate_attach,
+     * so return the original error.
+     */
+    rc = dls->rc ? dls->rc : aodev->rc;
+    dls->callback(egc, dls, rc);
+    return;
 }
 
 /******************************************************************************/
tools/libxl/libxl_bootloader.c +59 -14

126 lines not on this page · raw message

tools/libxl/libxl_internal.h +58 -14

98 lines not on this page · raw message

Signature · 1 line
1.7.7.5 (Apple Git-26)
#11 lore raw

Re: [PATCH v10 07/19] libxl: convert libxl__device_disk_local_attach to an async op

Roger Pau Monne writes ("[PATCH v10 07/19] libxl: convert libxl__device_disk_local_attach to an async op"):

Quoted 7 lines

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.

Thanks for this. See my other comment earlier today about the error handling. The rest of my comments are below. Very nearly there I think.

Thanks,
Ian.

tools/libxl/libxl.c char * libxl__device_disk_local_attach(libxl__gc *gc, · line 2240

Quoted 7 lines
tools/libxl/libxl.c +1 -2
@@ -2234,39 +2237,102 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
             goto out;
     }
     if (dev != NULL)
-        ret = strdup(dev);
-    return ret;
+        dls->diskpath = strdup(dev);

libxl__strdup, surely. And I'm not sure I understand why this string can't be from the gc. In any case the memory allocation strategy should be documented in the struct. Eg:

Quoted 1 line
tools/libxl/libxl_internal.h +1 -0
+struct libxl__disk_local_state {

...

tools/libxl/libxl_internal.h struct libxl__ao_devices { · line 1918

Quoted 2 lines
tools/libxl/libxl_internal.h +2 -0
+    /* filled by libxl__device_disk_local_initiate_attach */
+    char *diskpath;

+ char *diskpath; /* from the gc */
or
+ char *diskpath; /* non-0 whenever Attached, disposed by detach */
or something.

Quoted 3 lines
tools/libxl/libxl.c +2 -0
+void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
+                                     libxl__disk_local_state *dls)
 {

...

tools/libxl/libxl.c char * libxl__device_disk_local_attach(libxl__gc *gc, · line 2305

Quoted 13 lines
tools/libxl/libxl.c +13 -0
+out:
+    if (dls->diskpath) {
+        free(dls->diskpath);
+        dls->diskpath = 0;
+    }
+    /*
+     * If there was an error in dls->rc, it means we have been called from
+     * a failed execution of libxl__device_disk_local_initiate_attach,
+     * so return the original error.
+     */
+    rc = dls->rc ? dls->rc : rc;
+    dls->callback(egc, dls, rc);
+    return;

This seems to occur twice. Once here and once in local_device_detach_cb. Clearly they should be unified, probably by dismembering local_device_detach_cb:

...

Quoted 12 lines
tools/libxl/libxl.c +12 -0
+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);
+    int rc;
+
+    if (dls->diskpath) {
+        free(dls->diskpath);
+        dls->diskpath = 0;
+    }
+
+    if (aodev->rc) {

...

Quoted 18 lines
tools/libxl/libxl.c +11 -0
+    }
+
+out:
+    /*
+     * If there was an error in dls->rc, it means we have been called from
+     * a failed execution of libxl__device_disk_local_initiate_attach,
+     * so return the original error.
+     */
+    rc = dls->rc ? dls->rc : aodev->rc;
+    dls->callback(egc, dls, rc);
+    return;
tools/libxl/libxl_bootloader.c +0 -0
index 7ebc0df..4bcca3d 100644
@@ -249,10 +245,32 @@ static void bootloader_setpaths(libxl__gc *gc, libxl__bootloader_state *bl)

...

tools/libxl/libxl_bootloader.c static void bootloader_setpaths(libxl__gc *gc, libxl__bootloader_state *bl) · line 252

Quoted 5 lines
tools/libxl/libxl_bootloader.c +5 -0
+/* Callbacks */
+
+static void bootloader_finished_cb(libxl__egc *egc,
+                                   libxl__disk_local_state *dls,
+                                   int rc);

Wouldn't this be better named
bootloader_local_detached_cb
or something ? Because the function called when the bootloader finishes is bootloader_callback.

Quoted 11 lines

static void bootloader_callback(libxl__egc *egc, libxl__bootloader_state *bl,
int rc)

 {
     bootloader_cleanup(egc, bl);
tools/libxl/libxl_bootloader.c +4 -0
+
+    bl->dls.callback = bootloader_finished_cb;
+    libxl__device_disk_local_initiate_detach(egc, &bl->dls);
+}
tools/libxl/libxl_internal.h +0 -0
index 4f3a232..ab590be 100644

...

tools/libxl/libxl_internal.h struct libxl__ao_devices { · line 1928

Quoted 5 lines
tools/libxl/libxl_internal.h +5 -0
+/*
+ * Clears the internal error code, can be called multiple times, and
+ * must be called before libxl__device_disk_local_initiate_attach.
+ */
+static inline void libxl__device_disk_local_init(libxl__disk_local_state *dls)

"Clears the internal error code" should not be in interface documentation, since it refers to an implementation detail.

Perhaps you mean "Prepares a dls for use". You should explicitly state which of the documented states it transitions between. I guess "Undefined -> Idle" ?

Quoted 8 lines

+/* Make a disk available in this (the control) domain. Always calls
+ * dls->callback when finished.
+ * State Idle -> Attaching
+ *
+ * The state on which the device is when in the callback depends
+ * on the passed value of rc:
+ * Attached if rc == 0
+ * Idle if rc != 0

A nit: this would be slightly easier to read if there the two subordinate options were indented.

Also correct English would be "the state in which the device is" but really you mean the state of the dls, not of the device. I would write:
+ * The state of dls on entry to the callback depends on the value
+ * of rc passed to the callback:
+ * rc == 0: Attached if rc == 0
+ * rc != 0: Idle

tools/libxl/libxl_internal.h struct libxl__ao_devices { · line 1951

Quoted 10 lines
tools/libxl/libxl_internal.h +10 -0
+_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 -> Detaching
+ *
+ * The state on which the device is when in the callback is Idle.

Again, "in which", "dls" or reword as above.

tools/libxl/libxl_internal.h struct libxl__bootloader_state { · line 2147

Quoted 8 lines
tools/libxl/libxl_internal.h +1 -1
@@ -2097,10 +2142,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. */

Is this last comment, which I wrote, wrong ? That is, as previously discussed, is it legal to detach the disk before the kernel and initramfs have been loaded into the domain's memory ?

I think it probably is. If so perhaps I should write a separate patch to fix it.

Thanks,
Ian.

#12 lore raw

Re: [PATCH v10 07/19] libxl: convert libxl__device_disk_local_attach to an async op

Quoted 29 lines · Ian Jackson

Roger Pau Monne writes ("[PATCH v10 07/19] libxl: convert libxl__device_disk_local_attach to an async op"):

Quoted 7 lines

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.

Thanks for this. See my other comment earlier today about the error handling. The rest of my comments are below. Very nearly there I think.

Thanks,
Ian.

tools/libxl/libxl.c char * libxl__device_disk_local_attach(libxl__gc *gc, · line 2240

Quoted 7 lines
tools/libxl/libxl.c +1 -2
@@ -2234,39 +2237,102 @@ char * libxl__device_disk_local_attach(libxl__gc *gc,
             goto out;
     }
     if (dev != NULL)
-        ret = strdup(dev);
-    return ret;
+        dls->diskpath = strdup(dev);

libxl__strdup, surely. And I'm not sure I understand why this string can't be from the gc. In any case the memory allocation strategy should be documented in the struct. Eg:

tools/libxl/libxl_internal.h struct libxl__ao_devices { · line 1910

Quoted 1 line
tools/libxl/libxl_internal.h +1 -0
+struct libxl__disk_local_state {
Quoted 8 lines

...

tools/libxl/libxl_internal.h struct libxl__ao_devices { · line 1918

Quoted 2 lines
tools/libxl/libxl_internal.h +2 -0
+    /* filled by libxl__device_disk_local_initiate_attach */
+    char *diskpath;

+ char *diskpath; /* from the gc */
or
+ char *diskpath; /* non-0 whenever Attached, disposed by detach */
or something.

I've changed diskpath to be allocated from the gc.

Quoted 18 lines
Quoted 3 lines
tools/libxl/libxl.c +2 -0
+void libxl__device_disk_local_initiate_detach(libxl__egc *egc,
+                                     libxl__disk_local_state *dls)
 {

...

tools/libxl/libxl.c char * libxl__device_disk_local_attach(libxl__gc *gc, · line 2305

Quoted 13 lines
tools/libxl/libxl.c +13 -0
+out:
+    if (dls->diskpath) {
+        free(dls->diskpath);
+        dls->diskpath = 0;
+    }
+    /*
+     * If there was an error in dls->rc, it means we have been called from
+     * a failed execution of libxl__device_disk_local_initiate_attach,
+     * so return the original error.
+     */
+    rc = dls->rc ? dls->rc : rc;
+    dls->callback(egc, dls, rc);
+    return;
Quoted 3 lines

This seems to occur twice. Once here and once in local_device_detach_cb. Clearly they should be unified, probably by dismembering local_device_detach_cb:

I'm calling local_device_detach_cb instead of the callback directly.

Quoted 33 lines

...

Quoted 12 lines
tools/libxl/libxl.c +12 -0
+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);
+    int rc;
+
+    if (dls->diskpath) {
+        free(dls->diskpath);
+        dls->diskpath = 0;
+    }
+
+    if (aodev->rc) {

...

Quoted 18 lines
tools/libxl/libxl.c +11 -0
+    }
+
+out:
+    /*
+     * If there was an error in dls->rc, it means we have been called from
+     * a failed execution of libxl__device_disk_local_initiate_attach,
+     * so return the original error.
+     */
+    rc = dls->rc ? dls->rc : aodev->rc;
+    dls->callback(egc, dls, rc);
+    return;
tools/libxl/libxl_bootloader.c +0 -0
index 7ebc0df..4bcca3d 100644
@@ -249,10 +245,32 @@ static void bootloader_setpaths(libxl__gc *gc, libxl__bootloader_state *bl)

...

Quoted 10 lines

tools/libxl/libxl_bootloader.c static void bootloader_setpaths(libxl__gc *gc, libxl__bootloader_state *bl) · line 252

Quoted 5 lines
tools/libxl/libxl_bootloader.c +5 -0
+/* Callbacks */
+
+static void bootloader_finished_cb(libxl__egc *egc,
+                                   libxl__disk_local_state *dls,
+                                   int rc);

Wouldn't this be better named
bootloader_local_detached_cb
or something ? Because the function called when the bootloader finishes is bootloader_callback.

Yes, changed to bootloader_local_detached_cb.

Quoted 21 lines
Quoted 11 lines

static void bootloader_callback(libxl__egc *egc, libxl__bootloader_state *bl,
int rc)

 {
     bootloader_cleanup(egc, bl);
tools/libxl/libxl_bootloader.c +4 -0
+
+    bl->dls.callback = bootloader_finished_cb;
+    libxl__device_disk_local_initiate_detach(egc, &bl->dls);
+}
tools/libxl/libxl_internal.h +0 -0
index 4f3a232..ab590be 100644

...

tools/libxl/libxl_internal.h struct libxl__ao_devices { · line 1928

Quoted 5 lines
tools/libxl/libxl_internal.h +5 -0
+/*
+ * Clears the internal error code, can be called multiple times, and
+ * must be called before libxl__device_disk_local_initiate_attach.
+ */
+static inline void libxl__device_disk_local_init(libxl__disk_local_state *dls)

"Clears the internal error code" should not be in interface documentation, since it refers to an implementation detail.

Quoted 3 lines

Perhaps you mean "Prepares a dls for use". You should explicitly state which of the documented states it transitions between. I guess "Undefined -> Idle" ?

Changed the comment, and added the state transition.

Quoted 20 lines
Quoted 8 lines

+/* Make a disk available in this (the control) domain. Always calls
+ * dls->callback when finished.
+ * State Idle -> Attaching
+ *
+ * The state on which the device is when in the callback depends
+ * on the passed value of rc:
+ * Attached if rc == 0
+ * Idle if rc != 0

A nit: this would be slightly easier to read if there the two subordinate options were indented.

Also correct English would be "the state in which the device is" but really you mean the state of the dls, not of the device. I would write:
+ * The state of dls on entry to the callback depends on the value
+ * of rc passed to the callback:
+ * rc == 0: Attached if rc == 0
+ * rc != 0: Idle

Quoted 12 lines

tools/libxl/libxl_internal.h struct libxl__ao_devices { · line 1951

Quoted 10 lines
tools/libxl/libxl_internal.h +10 -0
+_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 -> Detaching
+ *
+ * The state on which the device is when in the callback is Idle.

Again, "in which", "dls" or reword as above.

I've addressed both comments.

Quoted 13 lines

tools/libxl/libxl_internal.h struct libxl__bootloader_state { · line 2147

Quoted 8 lines
tools/libxl/libxl_internal.h +1 -1
@@ -2097,10 +2142,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. */

Is this last comment, which I wrote, wrong ? That is, as previously discussed, is it legal to detach the disk before the kernel and initramfs have been loaded into the domain's memory ?

Quoted 2 lines

I think it probably is. If so perhaps I should write a separate patch to fix it.

I didn't change this comment, but if you want I can do that in this patch. The bootloader makes a copy of the kernel/initramfs to somewhere, and that is what we load to the memory.

#13 lore raw

PATCH v10 8/19 libxl: rename vifs to nics

In reply to 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.

---
 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(-)
tools/libxl/libxl.h +2 -2
index 5c819f1..c730ac5 100644
@@ -470,10 +470,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;
tools/libxl/libxl_create.c +5 -5
index 16d32c2..a4b8248 100644
@@ -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);
tools/libxl/libxl_dm.c +27 -27

137 lines not on this page · raw message

tools/libxl/libxl_json.c +4 -4
index e870606..caa8312 100644
@@ -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;
     }
tools/libxl/xl_cmdimpl.c +7 -7
index 61791e1..8c04fe6 100644
@@ -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++;
         }
     }
 
tools/libxl/xl_sxp.c +10 -10
index 6e0a389..91f8f75 100644
@@ -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");
     }
Signature · 1 line
1.7.7.5 (Apple Git-26)
#14 lore raw

PATCH v10 9/19 libxl: convert libxl_device_disk_add to an async op

In reply to 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 v9:

* Removed a wrong comment hunk.

* Fixed Ocaml bindings.

Changes since v8:

* Added a macro to define libxl_device_{type}_add functions, since they are all equal (just like for device removal/destroy).

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

---
 tools/libxl/libxl.c                  |  144 +++++++++++++++++++++++++---------
 tools/libxl/libxl.h                  |    4 +-
 tools/libxl/libxl_create.c           |   42 ++++++++---
 tools/libxl/libxl_device.c           |   77 ++++++++++++++++++
 tools/libxl/libxl_dm.c               |   73 +++++++++++++-----
 tools/libxl/libxl_internal.h         |   39 +++++++++-
 tools/libxl/xl_cmdimpl.c             |    2 +-
 tools/ocaml/libs/xl/xenlight_stubs.c |    2 +-
 8 files changed, 310 insertions(+), 73 deletions(-)
tools/libxl/libxl.c +106 -38
index bd64e0c..d97add5 100644
@@ -1749,16 +1749,18 @@ int libxl__device_from_disk(libxl__gc *gc, uint32_t domid,
  * The passed get_vdev_user function is also in charge of printing
  * the corresponding error message when appropiate.
  */
-static int device_disk_add(libxl__gc *gc, uint32_t domid,
+static void device_disk_add(libxl__egc *egc, uint32_t domid,
                            libxl_device_disk *disk,
+                           libxl__ao_device *aodev,
                            void *get_vdev_user(libxl__gc *, const char *,
                                                xs_transaction_t),
                            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;
@@ -1801,7 +1803,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);
@@ -1819,7 +1822,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);
@@ -1840,7 +1843,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);
@@ -1872,11 +1875,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));
 
@@ -1885,6 +1888,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:
@@ -1892,21 +1899,15 @@ out_free:
     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);
+    aodev->rc = rc;
+    if (rc) aodev->callback(egc, aodev);
+    return;
 }
 
-int libxl_device_disk_add(libxl_ctx *ctx, 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)
 {
-    GC_INIT(ctx);
-    int rc = libxl__device_disk_add(gc, domid, disk);
-    GC_FREE;
-    return rc;
+    device_disk_add(egc, domid, disk, aodev, NULL, NULL);
 }
 
 static void libxl__device_disk_from_xs_be(libxl__gc *gc,
@@ -2109,11 +2110,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,14 +2157,17 @@ static char * libxl__alloc_vdev(libxl__gc *gc, const char *blkdev_start,
     return NULL;
 }
 
+/* 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;
     const libxl_device_disk *in_disk = dls->in_disk;
     libxl_device_disk *disk = &dls->disk;
     const char *blkdev_start = dls->blkdev_start;
@@ -2208,13 +2214,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,
-                                    (void *) 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, (void *) libxl__alloc_vdev,
+                                blkdev_start);
+                return;
             } else {
                 dev = disk->pdev_path;
             }
@@ -2227,15 +2232,48 @@ 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->rc = rc;
+    libxl__device_disk_local_initiate_detach(egc, dls);
+    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);
 
@@ -2973,6 +3011,36 @@ DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
 
 /******************************************************************************/
 
+/* Macro for defining device addition functions in a compact way */
+/* The following functions are defined:
+ * libxl_device_disk_add
+ */
+
+#define DEFINE_DEVICE_ADD(type)                                         \
+    int libxl_device_##type##_add(libxl_ctx *ctx,                       \
+        uint32_t domid, libxl_device_##type *type,                      \
+        const libxl_asyncop_how *ao_how)                                \
+    {                                                                   \
+        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_##type##_add(egc, domid, type, aodev);            \
+                                                                        \
+        return AO_INPROGRESS;                                           \
+    }
+
+/* Define alladd functions and undef the macro */
+
+/* disk */
+DEFINE_DEVICE_ADD(disk)
+
+#undef DEFINE_DEVICE_ADD
+
+/******************************************************************************/
+
 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
 {
     GC_INIT(ctx);
tools/libxl/libxl.h +3 -1
index c730ac5..f3161d5 100644
@@ -675,7 +675,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);
tools/libxl/libxl_create.c +32 -10

64 lines not on this page · raw message

tools/libxl/libxl_device.c +77 -0

89 lines not on this page · raw message

tools/libxl/libxl_dm.c +53 -20

126 lines not on this page · raw message

tools/libxl/libxl_internal.h +37 -2
index ab590be..eb69721 100644
@@ -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_QEMU_BODGE_TIMEOUT 2
@@ -1297,8 +1298,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);
 
@@ -1883,6 +1882,27 @@ struct libxl__ao_devices {
  *                   DONE.
  */
 
+/* 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
@@ -2253,6 +2273,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 NOT be prepared by this
+ * function, so the caller must make sure to call _prepare before calling this
+ * function. The start parameter contains the position inside the aodevs array
+ * that should be used to store the state of this devices.
+ *
+ * The "callback" will be called for each device, and the user is responsible
+ * for calling libxl__ao_device_check_last on the callback.
+ */
+_hidden void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
+                              int start, libxl_domain_config *d_config,
+                              libxl__ao_devices *aodevs);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
@@ -2287,6 +2320,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*);
@@ -2316,6 +2350,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 -----*/
tools/libxl/xl_cmdimpl.c +1 -1
index 8c04fe6..f27e811 100644
@@ -5355,7 +5355,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;
tools/ocaml/libs/xl/xenlight_stubs.c +1 -1
index 53751b1..a158351 100644
@@ -247,7 +247,7 @@ value stub_xl_device_disk_add(value info, value domid)
 	device_disk_val(&gc, &lg, &c_info, info);
 
 	INIT_CTX();
-	ret = libxl_device_disk_add(ctx, Int_val(domid), &c_info);
+	ret = libxl_device_disk_add(ctx, Int_val(domid), &c_info, 0);
 	if (ret != 0)
 		failwith_xl("disk_add", &lg);
 	FREE_CTX();
Signature · 1 line
1.7.7.5 (Apple Git-26)
Acked-by on v10 9/19
#15 lore raw

Re: [PATCH v10 09/19] libxl: convert libxl_device_disk_add to an async op

Roger Pau Monne writes ("[PATCH v10 09/19] libxl: convert libxl_device_disk_add to an async op"):

Quoted 2 lines

Changes since v9:
* Removed a wrong comment hunk.

Did that make it into the other patch it should have been in ?

Anyway, this patch is

#16 lore raw

PATCH v10 10/19 libxl: convert libxl_device_nic_add to an async operation

In reply to 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 v9:

* Fixed Ocaml bindings.

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.

---
 tools/libxl/libxl.c                  |   29 ++++++++++++++-----
 tools/libxl/libxl.h                  |    3 +-
 tools/libxl/libxl_create.c           |   51 ++++++++++++++++++++++++++++-----
 tools/libxl/libxl_device.c           |    2 +
 tools/libxl/libxl_dm.c               |   38 +++++++++++++++++++++++--
 tools/libxl/libxl_internal.h         |    9 ++++++
 tools/libxl/xl_cmdimpl.c             |    2 +-
 tools/ocaml/libs/xl/xenlight_stubs.c |    2 +-
 8 files changed, 114 insertions(+), 22 deletions(-)
tools/libxl/libxl.c +21 -8
index d97add5..d27ac20 100644
@@ -2424,12 +2424,13 @@ 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)
+void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
+                           libxl_device_nic *nic, libxl__ao_device *aodev)
 {
-    GC_INIT(ctx);
+    STATE_AO_GC(aodev->ao);
     flexarray_t *front;
     flexarray_t *back;
-    libxl__device device;
+    libxl__device *device;
     char *dompath, **l;
     unsigned int nb, rc;
 
@@ -2460,7 +2461,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");
@@ -2501,6 +2503,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));
@@ -2511,18 +2516,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,
@@ -3014,6 +3023,7 @@ DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
 /* Macro for defining device addition functions in a compact way */
 /* The following functions are defined:
  * libxl_device_disk_add
+ * libxl_device_nic_add
  */
 
 #define DEFINE_DEVICE_ADD(type)                                         \
@@ -3037,6 +3047,9 @@ DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
 /* disk */
 DEFINE_DEVICE_ADD(disk)
 
+/* nic */
+DEFINE_DEVICE_ADD(nic)
+
 #undef DEFINE_DEVICE_ADD
 
 /******************************************************************************/
tools/libxl/libxl.h +2 -1
index f3161d5..d548090 100644
@@ -696,7 +696,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);
tools/libxl/libxl_create.c +43 -8

75 lines not on this page · raw message

tools/libxl/libxl_device.c +2 -0
index a935ced..ccbd84a 100644
@@ -453,6 +453,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
  *
  * The following functions are defined:
  * libxl__add_disks
+ * libxl__add_nics
  */
 
 #define DEFINE_DEVICES_ADD(type)                                               \
@@ -471,6 +472,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
 
tools/libxl/libxl_dm.c +35 -3
index 9a5cb0a..8f5618b 100644
@@ -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 ret);
 
+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);
@@ -864,9 +868,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)
@@ -943,9 +949,35 @@ 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;
+        sdss->aodevs.callback = stubdom_pvqemu_cb;
+        libxl__prepare_ao_devices(ao, &sdss->aodevs);
+        libxl__add_nics(egc, ao, dm_domid, 0, d_config, &sdss->aodevs);
+        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;
 
tools/libxl/libxl_internal.h +9 -0
index eb69721..67dfebe 100644
@@ -1893,6 +1893,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
@@ -2286,6 +2291,10 @@ _hidden void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
                               int start, libxl_domain_config *d_config,
                               libxl__ao_devices *aodevs);
 
+_hidden void libxl__add_nics(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
+                             int start, libxl_domain_config *d_config,
+                             libxl__ao_devices *aodevs);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
tools/libxl/xl_cmdimpl.c +1 -1
index f27e811..183dd99 100644
@@ -5244,7 +5244,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;
     }
tools/ocaml/libs/xl/xenlight_stubs.c +1 -1
index a158351..0e9c65e 100644
@@ -281,7 +281,7 @@ value stub_xl_device_nic_add(value info, value domid)
 	device_nic_val(&gc, &lg, &c_info, info);
 
 	INIT_CTX();
-	ret = libxl_device_nic_add(ctx, Int_val(domid), &c_info);
+	ret = libxl_device_nic_add(ctx, Int_val(domid), &c_info, 0);
 	if (ret != 0)
 		failwith_xl("nic_add", &lg);
 	FREE_CTX();
Signature · 1 line
1.7.7.5 (Apple Git-26)
#17 lore raw

PATCH v10 11/19 libxl: add option to choose who executes hotplug scripts

In reply to 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 v8:

* Free list of returned VMs

* Use libxl__xs_{write,rm}_checked.

* Change initialization of dm_config->c_info.run_hotplug_scripts.

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.

---
 docs/man/xl.conf.pod.5       |    8 ++++++++
 tools/examples/xl.conf       |    5 +++++
 tools/libxl/libxl_create.c   |   40 +++++++++++++++++++++++++++++++++++++++-
 tools/libxl/libxl_dm.c       |    3 +++
 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, 84 insertions(+), 1 deletions(-)
docs/man/xl.conf.pod.5 +8 -0
index 149430c..23932be 100644
@@ -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
tools/examples/xl.conf +5 -0
index ebf057c..28ab796 100644
@@ -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
tools/libxl/libxl_create.c +39 -1
index 36f7366..d3dc038 100644
@@ -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];
@@ -390,6 +392,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info,
     struct xs_permissions noperm[1];
     xs_transaction_t t = 0;
     xen_domain_handle_t handle;
+    libxl_vminfo *vm_list;
 
 
     assert(!libxl_domid_valid_guest(*domid));
@@ -503,6 +506,41 @@ retry_transaction:
             libxl__sprintf(gc, "%s/hvmloader/generation-id-address", dom_path),
                         rwperm, ARRAY_SIZE(rwperm));
 
+                    vm_list = libxl_list_vm(ctx, &nb_vm);
+    if (!vm_list) {
+        LOG(ERROR, "cannot get number of running guests");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+    libxl_vminfo_list_free(vm_list, nb_vm);
+    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_checked(gc, t, DISABLE_UDEV_PATH, "1");
+        if (rc) {
+            LOGE(ERROR, "unable to write %s = 1", DISABLE_UDEV_PATH);
+            goto out;
+        }
+    } else {
+        rc = libxl__xs_rm_checked(gc, 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));
 
tools/libxl/libxl_dm.c +3 -0
index 8f5618b..dbbd0db 100644
@@ -762,6 +762,9 @@ 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;
 
+    dm_config->c_info.run_hotplug_scripts =
+        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);
tools/libxl/libxl_internal.c +19 -0
index fbff7d0..4775bb6 100644
@@ -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
tools/libxl/libxl_internal.h +3 -0
index 67dfebe..cd55da6 100644
@@ -91,6 +91,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]))
 
@@ -1470,6 +1471,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:
tools/libxl/libxl_types.idl +1 -0
index c3fbe77..9c83436 100644
@@ -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")
tools/libxl/xl.c +4 -0
index d6f2b3e..f31e836 100644
@@ -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 {
tools/libxl/xl.h +1 -0
index b0ba357..0b2f848 100644
@@ -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;
tools/libxl/xl_cmdimpl.c +1 -0
index 183dd99..5162b6f 100644
@@ -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)))
Signature · 1 line
1.7.7.5 (Apple Git-26)
#18 lore raw

PATCH v10 12/19 libxl: rename _IOEMU nic type to VIF_IOEMU

In reply to Roger Pau Monne

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

---
 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(-)
tools/libxl/libxl.c +2 -2
index d27ac20..05ba211 100644
@@ -2406,7 +2406,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;
 }
 
@@ -2702,7 +2702,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();
tools/libxl/libxl_dm.c +4 -4
index dbbd0db..f21fb4f 100644
@@ -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",
tools/libxl/libxl_types.idl +1 -1
index 9c83436..1a6c4a2 100644
@@ -60,7 +60,7 @@ libxl_disk_backend = Enumeration("disk_backend", [
     ])
 
 libxl_nic_type = Enumeration("nic_type", [
-    (1, "IOEMU"),
+    (1, "VIF_IOEMU"),
     (2, "VIF"),
     ])
 
tools/libxl/xl_cmdimpl.c +2 -2
index 5162b6f..1371458 100644
@@ -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")) {
@@ -5196,7 +5196,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;
Signature · 1 line
1.7.7.5 (Apple Git-26)
#19 lore raw

Re: [PATCH v10 12/19] libxl: rename _IOEMU nic type to VIF_IOEMU

Quoted 2 lines · Roger Pau Monne

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

You missed some in comments. e.g. in

static void libxl__device_nic_from_xs_be(libxl__gc *gc,
const char *be_path, libxl_device_nic *nic)
...

    /* XXX ioemu nics are not in xenstore at all? */
    nic->nictype = LIBXL_NIC_TYPE_VIF;
    nic->model = NULL; /* XXX Only for TYPE_IOEMU */
    nic->ifname = NULL; /* XXX Only for TYPE_IOEMU */

(I also suspect this comment is just wrong...)

#20 lore raw

Re: [PATCH v10 12/19] libxl: rename _IOEMU nic type to VIF_IOEMU

Quoted 5 lines · Ian Campbell
Quoted 2 lines · Roger Pau Monne

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

You missed some in comments.

Also I think it makes the type argument to libxl__device_nic_devname a bit misleading. Should perhaps become "bool emulated" ?

Ian.

#21 lore raw

PATCH v10 13/19 libxl: set correct nic type depending on the guest

In reply to 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.
* PV: use VIF always.

Changes since v9:

* Don't force nic type for HVM-stubdom guests.

---
 tools/libxl/libxl.c          |   20 ++++++++++++++++----
 tools/libxl/libxl_create.c   |    2 +-
 tools/libxl/libxl_dm.c       |    2 +-
 tools/libxl/libxl_internal.h |    3 ++-
 4 files changed, 20 insertions(+), 7 deletions(-)
tools/libxl/libxl.c +16 -4
index 05ba211..5c5c876 100644
@@ -2375,7 +2375,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;
@@ -2405,8 +2406,19 @@ 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 (!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;
 }
 
@@ -2434,7 +2446,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);
tools/libxl/libxl_create.c +1 -1
index d3dc038..7db9457 100644
@@ -938,7 +938,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:
tools/libxl/libxl_dm.c +1 -1
index f21fb4f..cbe518f 100644
@@ -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)
tools/libxl/libxl_internal.h +2 -1
index cd55da6..3d87030 100644
@@ -897,7 +897,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);
Signature · 1 line
1.7.7.5 (Apple Git-26)
#22 lore raw

Re: [PATCH v10 13/19] libxl: set correct nic type depending on the guest

Roger Pau Monne writes ("[PATCH v10 13/19] libxl: set correct nic type depending on the guest"):

Quoted 2 lines

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

...>

Quoted 2 lines

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

I think attempts by the user to use VIF_IOEMU with a PV domain should be rejected with ERROR_INVAL, not silently ignored.

Quoted 3 lines
tools/libxl/libxl.c +2 -1
-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)

...

tools/libxl/libxl.c int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic) · line 2417

Quoted 3 lines
tools/libxl/libxl.c +3 -0
+    case LIBXL_DOMAIN_TYPE_PV:
+        nic->nictype = LIBXL_NIC_TYPE_VIF;
+        break;

Somewhere here.

Thanks,
Ian.

#23 lore raw

PATCH v10 14/19 libxl: use libxl__xs_path_cleanup on device_destroy

In reply to 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.

---
 tools/libxl/libxl_device.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)
tools/libxl/libxl_device.c +14 -4
index ccbd84a..d730b39 100644
@@ -480,16 +480,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 */
Signature · 1 line
1.7.7.5 (Apple Git-26)
#24 lore raw

Re: [PATCH v10 14/19] libxl: use libxl__xs_path_cleanup on device_destroy

Quoted 3 lines · 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.

Something in this series, I suspect this patch, is causing lots of:
libxl: debug: libxl_xshelp.c:222:libxl__xs_path_cleanup: unable to remove path /local/domain/81/device/console/3: No such file or directory
on destroy.

I suspect that libxl__xs_path_cleanup should be checking for ENOENT and "failing" silently in that case, since failing to cleanup something which doesn't exist is harmless.

Ian.

#25 lore raw

PATCH v10 15/19 libxl: call hotplug scripts for disk devices from libxl

In reply to 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 v8:

* Print "hotplug_error" xenstore entry if hotplug execution fails and hotplug_error contains information.

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.

---
 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                |  133 ++++++++++++++++++++++++++++-
 tools/libxl/libxl_internal.h              |   20 ++++-
 tools/libxl/libxl_linux.c                 |   97 +++++++++++++++++++++
 tools/libxl/libxl_netbsd.c                |    8 ++
 7 files changed, 274 insertions(+), 6 deletions(-)
tools/hotplug/Linux/xen-backend.rules +3 -3
index 405387f..d55ff11 100644
@@ -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"
tools/hotplug/Linux/xen-hotplug-common.sh +6 -0
index 8f6557d..4a7bc73 100644
@@ -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"
tools/libxl/libxl.c +10 -0
index 5c5c876..25d9786 100644
@@ -1822,6 +1822,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:
@@ -1837,6 +1842,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:
tools/libxl/libxl_device.c +131 -2

177 lines not on this page · raw message

tools/libxl/libxl_internal.h +19 -1
index 3d87030..786b534 100644
@@ -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_QEMU_BODGE_TIMEOUT 2
 #define LIBXL_XENCONSOLE_LIMIT 1048576
@@ -1792,11 +1793,14 @@ struct libxl__ao_device {
     int active;
     int rc;
     libxl__ev_devstate backend_ds;
-    /* Bodge for Qemu devices */
+    /* Bodge for Qemu devices, also used for timeout of hotplug execution */
     libxl__ev_time timeout;
     /* 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_child child;
 };
 
 /* Helper struct to simply the plug/unplug of multiple devices at the same
@@ -1926,6 +1930,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;
tools/libxl/libxl_linux.c +97 -0

100 lines not on this page · raw message

tools/libxl/libxl_netbsd.c +8 -0
index dbf5f71..a2f8d3f 100644
@@ -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;
+}
Signature · 1 line
1.7.7.5 (Apple Git-26)
#26 lore raw

Re: [PATCH v10 15/19] libxl: call hotplug scripts for disk devices from libxl

tools/libxl/libxl_device.c static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev) · line 930

Quoted 8 lines · Roger Pau Monne
tools/libxl/libxl_device.c +8 -0
+    if (status) {
+        libxl_report_child_exitstatus(CTX, LIBXL__LOG_ERROR,
+                                      aodev->what, pid, status);
+        hotplug_error = libxl__xs_read(gc, XBT_NULL,
+                                       GCSPRINTF("%s/hotplug-error", be_path));
+        if (hotplug_error)
+            LOG(ERROR, "reason of failure reported by hotplug script: %s",
+                       hotplug_error); 

This should be "reason *for* failure" but actually it's rather verbose, the log line is ~100 chars before you even append hotplug_error. I think just a "script: " prefix, or even nothing, would be ok.

Ian.

#27 lore raw

PATCH v10 16/19 libxl: call hotplug scripts for nic devices from libxl

In reply to 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 v8:

* Correctly detect errors from hotplug execution and cancel domain creation if an error is found.

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).

---
 tools/hotplug/Linux/xen-backend.rules |    6 +-
 tools/libxl/libxl_device.c            |   60 +++++++++++++++++++--
 tools/libxl/libxl_internal.h          |   14 +++++-
 tools/libxl/libxl_linux.c             |   95 +++++++++++++++++++++++++++++++-
 tools/libxl/libxl_netbsd.c            |    3 +-
 5 files changed, 166 insertions(+), 12 deletions(-)
tools/hotplug/Linux/xen-backend.rules +3 -3
index d55ff11..c591a3f 100644
@@ -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"
tools/libxl/libxl_device.c +56 -4

104 lines not on this page · raw message

tools/libxl/libxl_internal.h +13 -1
index 786b534..cfe9dc0 100644
@@ -879,6 +879,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:
@@ -1800,6 +1802,7 @@ struct libxl__ao_device {
     libxl__ao_devices *aodevs;
     /* device hotplug execution */
     const char *what;
+    int num_exec;
     libxl__ev_child child;
 };
 
@@ -1939,10 +1942,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 -----*/
 
tools/libxl/libxl_linux.c +92 -3

134 lines not on this page · raw message

tools/libxl/libxl_netbsd.c +2 -1
index a2f8d3f..28cdf21 100644
@@ -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;
 }
Signature · 1 line
1.7.7.5 (Apple Git-26)
#28 lore raw

Re: [PATCH v10 16/19] libxl: call hotplug scripts for nic devices from libxl

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 119

Quoted 7 lines
tools/libxl/libxl_linux.c +6 -0
+        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));

Is this fall-through of the case deliberate? If so then a comment would be good.

However I'm not sure about this. It seems like the hotplug scripts are only called once in the VIF_IOEMU case? I would expect one call for the PV VIF device and one for the TAP device? Perhaps I'm just missing the location of the other one? Is this "num_exec" variable involved in some way? But you don't propagate that to get_hotplug_env() and therefore include *both* sets of env vars?

In any case something related seems to be broken for guests which use a stub domain and use vifname=foo. What I see in that case is the script called three times...
1. For the PV interface of the stub domain (LIBXL_NIC_TYPE_VIF), renames vifX+1.0 -> foo-emu (*)
2. For the PV interface of the real domain (LIBXL_NIC_TYPE_VIF_IOEMU part 1), renames vifX.0 -> foo
3. For the emu interface of the real domain (LIBXL_NIC_TYPE_VIF_IOEMU part 2) renames vifX.0 -> foo-emu

The rename in #3 fails, because foo-emu already exists. But in fact this call to the script should never happen for a domain with a stub domain since the emulated device in this case is within the stub domain, not the driver domain.

(*) This is with the patch I posted earlier to rename the stub domain's vif with a -emu suffix. Without that then the clash is at #2 instead.

Ian.

#29 lore raw

Re: [PATCH v10 16/19] libxl: call hotplug scripts for nic devices from libxl

Quoted 9 lines · Ian Campbell

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 119

Quoted 6 lines
tools/libxl/libxl_linux.c +6 -0
+        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));

Is this fall-through of the case deliberate? If so then a comment would be good.

Also -- isn't the strdup redendant here too?

#30 lore raw

Re: [PATCH v10 16/19] libxl: call hotplug scripts for nic devices from libxl

In reply to Ian Campbell
Quoted 27 lines · Ian Campbell

Review

Quoted 6 lines
(quoted diff) +6 -0
+        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));

Is this fall-through of the case deliberate? If so then a comment would be good.

However I'm not sure about this. It seems like the hotplug scripts are only called once in the VIF_IOEMU case? I would expect one call for the PV VIF device and one for the TAP device? Perhaps I'm just missing the location of the other one? Is this "num_exec" variable involved in some way? But you don't propagate that to get_hotplug_env() and therefore include *both* sets of env vars?

In any case something related seems to be broken for guests which use a stub domain and use vifname=foo. What I see in that case is the script called three times...
1. For the PV interface of the stub domain (LIBXL_NIC_TYPE_VIF), renames vifX+1.0 -> foo-emu (*)
2. For the PV interface of the real domain (LIBXL_NIC_TYPE_VIF_IOEMU part 1), renames vifX.0 -> foo
3. For the emu interface of the real domain (LIBXL_NIC_TYPE_VIF_IOEMU part 2) renames vifX.0 -> foo-emu

Quoted 4 lines

The rename in #3 fails, because foo-emu already exists. But in fact this call to the script should never happen for a domain with a stub domain since the emulated device in this case is within the stub domain, not the driver domain.

My slightly skanky fix, still with debugging still, is below. I'm posting it now because it's late and I'm going home.

I think a better fix would be to determine up front which scripts needed running and track that instead of num_execs.

tools/libxl/libxl_dm.c +20 -2
diff -r 574bece33c88 tools/libxl/libxl_dm.c
@@ -616,6 +616,25 @@ static char ** libxl__build_device_model
     }
 }
 
+static void libxl__dm_nics_from_hvm_guest_config(libxl__gc *gc,
+                                    libxl_domain_config * const guest_config,
+                                    libxl_domain_config *dm_config)
+{
+    int i, nr = guest_config->num_nics;
+
+    GCNEW_ARRAY(dm_config->nics, nr);
+
+    for (i=0; i<nr; i++) {
+        dm_config->nics[i] = guest_config->nics[i];
+        if (dm_config->nics[i].ifname)
+            dm_config->nics[i].ifname = GCSPRINTF("%s" TAP_DEVICE_SUFFIX,
+                                                  dm_config->nics[i].ifname);
+        dm_config->nics[i].nictype = LIBXL_NIC_TYPE_VIF;
+    }
+
+    dm_config->num_nics = nr;
+}
+
 static int libxl__vfb_and_vkb_from_hvm_guest_config(libxl__gc *gc,
                                         const libxl_domain_config *guest_config,
                                         libxl_device_vfb *vfb,
@@ -764,8 +783,7 @@ void libxl__spawn_stub_dm(libxl__egc *eg
     dm_config->disks = guest_config->disks;
     dm_config->num_disks = guest_config->num_disks;
 
-    dm_config->nics = guest_config->nics;
-    dm_config->num_nics = guest_config->num_nics;
+    libxl__dm_nics_from_hvm_guest_config(gc, guest_config, dm_config);
 
     dm_config->c_info.run_hotplug_scripts =
         guest_config->c_info.run_hotplug_scripts;
tools/libxl/libxl_linux.c +14 -1
diff -r 574bece33c88 tools/libxl/libxl_linux.c
@@ -117,11 +117,13 @@ static char **get_hotplug_env(libxl__gc 
             env[nr++] = libxl__strdup(gc, libxl__device_nic_devname(gc,
                                                       dev->domid, dev->devid,
                                                       LIBXL_NIC_TYPE_VIF_IOEMU));
+            fprintf(stderr, "dom%d: %d: VIF_IOEMU %s = %s\n", dev->domid, dev->devid, env[nr-2], env[nr-1]);
         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));
+            fprintf(stderr, "dom%d: %d: VIF %s = %s\n", dev->domid, dev->devid, env[nr-2], env[nr-1]);
             break;
         default:
             return NULL;
@@ -159,11 +161,22 @@ static int libxl__hotplug_nic(libxl__gc 
         rc = ERROR_FAIL;
         goto out;
     }
+
+    fprintf(stderr, "%s: type=%s num_exec=%d action=%d hasstubdom=%s isstubdom=%s\n",
+            __func__, libxl_nic_type_to_string(nictype),
+            num_exec, action,
+            libxl_get_stubdom_id(CTX, dev->domid) ? "yes" : "no",
+            libxl_is_stubdom(CTX, dev->domid, NULL) ? "yes" : "no");
+
     if (nictype == LIBXL_NIC_TYPE_VIF && num_exec != 0) {
         rc = 0;
         goto out;
     }
-
+    if (nictype == LIBXL_NIC_TYPE_VIF_IOEMU && num_exec != 0 && libxl_get_stubdom_id(CTX, dev->domid)) {
+        fprintf(stderr, "skipping EMU for domain w/ stubdom\n");
+        rc = 0;
+        goto out;
+    }
     *env = get_hotplug_env(gc, dev);
     if (!env) {
         rc = ERROR_FAIL;
#31 lore raw

Re: [PATCH v10 16/19] libxl: call hotplug scripts for nic devices from libxl

In reply to Ian Campbell
Quoted 10 lines · Ian Campbell

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 119

Quoted 6 lines
tools/libxl/libxl_linux.c +6 -0
+        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));

Is this fall-through of the case deliberate? If so then a comment would be good.

Quoted 6 lines

However I'm not sure about this. It seems like the hotplug scripts are only called once in the VIF_IOEMU case? I would expect one call for the PV VIF device and one for the TAP device? Perhaps I'm just missing the location of the other one? Is this "num_exec" variable involved in some way? But you don't propagate that to get_hotplug_env() and therefore include *both* sets of env vars?

Yes, we perform two calls, one for the vif device and one for the tap device. I agree that this approach is not really clear, what might be more suitable is to return arrays of hotplug execution items from get_hotplug_script_info:

args =

	{
		{"/etc/xen/script/foo", "bar"},
		{"/etc/xen/script/foo2", "bar2"},
		NULL
	};

And the same with env, so we keep executing items until we reach args[i] == NULL.

Quoted 10 lines

In any case something related seems to be broken for guests which use a stub domain and use vifname=foo. What I see in that case is the script called three times...
1. For the PV interface of the stub domain (LIBXL_NIC_TYPE_VIF), renames vifX+1.0 -> foo-emu (*)
2. For the PV interface of the real domain (LIBXL_NIC_TYPE_VIF_IOEMU part 1), renames vifX.0 -> foo
3. For the emu interface of the real domain (LIBXL_NIC_TYPE_VIF_IOEMU part 2) renames vifX.0 -> foo-emu

Quoted 4 lines

The rename in #3 fails, because foo-emu already exists. But in fact this call to the script should never happen for a domain with a stub domain since the emulated device in this case is within the stub domain, not the driver domain.

As talked, the correct fix for this seems to detect if the guest has a stubdom and return 0 on the second call to get_hotplug_script_info, to prevent the execution of #3.

Quoted 2 lines

(*) This is with the patch I posted earlier to rename the stub domain's vif with a -emu suffix. Without that then the clash is at #2 instead.

#32 lore raw

Re: [PATCH v10 16/19] libxl: call hotplug scripts for nic devices from libxl

In reply to Roger Pau Monne

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 179

Quoted 3 lines · Roger Pau Monne
tools/libxl/libxl_linux.c +3 -0
+    if (nictype == LIBXL_NIC_TYPE_VIF_IOEMU && num_exec) {
+        (*args)[nr++] = action == DEVICE_CONNECT ? "add" : "remove";
+        (*args)[nr++] = libxl__strdup(gc, "type_if=tap");

This strdup (and the one below) are redundant, aren't they?

tools/libxl/libxl_linux.c static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) · line 186

Quoted 7 lines
tools/libxl/libxl_linux.c +7 -0
+        (*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); 

num_exec is a really clumsy way to track "in need to execute script for type FOO next". Couldn't we have an explicit enum?

#33 lore raw

PATCH v10 17/19 libxl: convert libxl_device_vkb_add to an async operation

In reply to Roger Pau Monne

Split libxl_device_vkb_add into libxl__device_vkb_add (to be used inside already running ao's), and make libxl_device_vkb_add a stub to call libxl__device_vkb_add.

Changes since v9:

* Fixed Ocaml bindings.

* Don't abort if num_vkbs > 0.

---
 tools/libxl/libxl.c                  |   26 +++++++++++++++++++-------
 tools/libxl/libxl.h                  |    3 ++-
 tools/libxl/libxl_create.c           |   18 ++++++++++--------
 tools/libxl/libxl_device.c           |    2 ++
 tools/libxl/libxl_dm.c               |    9 ++++-----
 tools/libxl/libxl_internal.h         |    9 +++++++++
 tools/ocaml/libs/xl/xenlight_stubs.c |    2 +-
 7 files changed, 47 insertions(+), 22 deletions(-)
tools/libxl/libxl.c +19 -7
index 25d9786..7de82a9 100644
@@ -2831,12 +2831,13 @@ static int libxl__device_from_vkb(libxl__gc *gc, uint32_t domid,
     return 0;
 }
 
-int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
+void libxl__device_vkb_add(libxl__egc *egc, uint32_t domid,
+                           libxl_device_vkb *vkb, libxl__ao_device *aodev)
 {
-    GC_INIT(ctx);
+    STATE_AO_GC(aodev->ao);
     flexarray_t *front;
     flexarray_t *back;
-    libxl__device device;
+    libxl__device *device;
     int rc;
 
     rc = libxl__device_vkb_setdefault(gc, vkb);
@@ -2853,7 +2854,8 @@ int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
         goto out_free;
     }
 
-    rc = libxl__device_from_vkb(gc, domid, vkb, &device);
+    GCNEW(device);
+    rc = libxl__device_from_vkb(gc, domid, vkb, device);
     if (rc != 0) goto out_free;
 
     flexarray_append(back, "frontend-id");
@@ -2870,16 +2872,22 @@ int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
     flexarray_append(front, "state");
     flexarray_append(front, libxl__sprintf(gc, "%d", 1));
 
-    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));
+
+    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;
 }
 
 /******************************************************************************/
@@ -3046,6 +3054,7 @@ DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
 /* The following functions are defined:
  * libxl_device_disk_add
  * libxl_device_nic_add
+ * libxl_device_vkb_add
  */
 
 #define DEFINE_DEVICE_ADD(type)                                         \
@@ -3072,6 +3081,9 @@ DEFINE_DEVICE_ADD(disk)
 /* nic */
 DEFINE_DEVICE_ADD(nic)
 
+/* vkb */
+DEFINE_DEVICE_ADD(vkb)
+
 #undef DEFINE_DEVICE_ADD
 
 /******************************************************************************/
tools/libxl/libxl.h +2 -1
index d548090..84d4efd 100644
@@ -710,7 +710,8 @@ int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,
                               libxl_device_nic *nic, libxl_nicinfo *nicinfo);
 
 /* Keyboard */
-int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb);
+int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb,
+                         const libxl_asyncop_how *ao_how);
 int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_vkb *vkb,
                             const libxl_asyncop_how *ao_how);
tools/libxl/libxl_create.c +10 -8
index 7db9457..39c858a 100644
@@ -904,10 +904,18 @@ static void domcreate_rebuild_done(libxl__egc *egc,
 
     store_libxl_entry(gc, domid, &d_config->b_info);
 
-    dcs->aodevs.size = d_config->num_disks;
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM && !d_config->num_vkbs) {
+        d_config->vkbs = libxl__zalloc(NOGC, sizeof(*d_config->vkbs));
+        libxl_device_vkb_init(&d_config->vkbs[0]);
+        d_config->num_vkbs = 1;
+    }
+
+    dcs->aodevs.size = d_config->num_disks + d_config->num_vkbs;
     dcs->aodevs.callback = domcreate_launch_dm;
     libxl__prepare_ao_devices(ao, &dcs->aodevs);
     libxl__add_disks(egc, ao, domid, 0, d_config, &dcs->aodevs);
+    libxl__add_vkbs(egc, ao, domid, d_config->num_disks, d_config,
+                    &dcs->aodevs);
 
     return;
 
@@ -930,7 +938,7 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
     libxl_ctx *const ctx = CTX;
 
     if (ret) {
-        LOG(ERROR, "unable to add disk devices");
+        LOG(ERROR, "unable to add misc devices");
         goto error_out;
     }
     for (i = 0; i < d_config->num_nics; i++) {
@@ -944,7 +952,6 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
     case LIBXL_DOMAIN_TYPE_HVM:
     {
         libxl__device_console console;
-        libxl_device_vkb vkb;
 
         ret = init_console_info(&console, 0);
         if ( ret )
@@ -952,10 +959,6 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
         libxl__device_console_add(gc, domid, &console, state);
         libxl__device_console_dispose(&console);
 
-        libxl_device_vkb_init(&vkb);
-        libxl_device_vkb_add(ctx, domid, &vkb);
-        libxl_device_vkb_dispose(&vkb);
-
         dcs->dmss.dm.guest_domid = domid;
         if (libxl_defbool_val(d_config->b_info.device_model_stubdomain))
             libxl__spawn_stub_dm(egc, &dcs->dmss);
@@ -970,7 +973,6 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
 
         for (i = 0; i < d_config->num_vfbs; i++) {
             libxl_device_vfb_add(ctx, domid, &d_config->vfbs[i]);
-            libxl_device_vkb_add(ctx, domid, &d_config->vkbs[i]);
         }
 
         ret = init_console_info(&console, 0);
tools/libxl/libxl_device.c +2 -0
index 4c5a7e2..65cd264 100644
@@ -482,6 +482,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
  * The following functions are defined:
  * libxl__add_disks
  * libxl__add_nics
+ * libxl__add_vkbs
  */
 
 #define DEFINE_DEVICES_ADD(type)                                               \
@@ -501,6 +502,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
 
 DEFINE_DEVICES_ADD(disk)
 DEFINE_DEVICES_ADD(nic)
+DEFINE_DEVICES_ADD(vkb)
 
 #undef DEFINE_DEVICES_ADD
 
tools/libxl/libxl_dm.c +4 -5
index cbe518f..a6ee080 100644
@@ -833,10 +833,12 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
 
-    sdss->aodevs.size = dm_config->num_disks;
+    sdss->aodevs.size = dm_config->num_disks + dm_config->num_vkbs;
     sdss->aodevs.callback = spawn_stub_launch_dm;
     libxl__prepare_ao_devices(ao, &sdss->aodevs);
     libxl__add_disks(egc, ao, dm_domid, 0, dm_config, &sdss->aodevs);
+    libxl__add_vkbs(egc, ao, dm_domid, dm_config->num_disks, dm_config,
+                    &sdss->aodevs);
 
     free(args);
     return;
@@ -866,7 +868,7 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
     uint32_t dm_domid = sdss->pvqemu.guest_domid;
 
     if (ret) {
-        LOG(ERROR, "error connecting disk devices");
+        LOG(ERROR, "error connecting misc devices");
         goto out;
      }
 
@@ -880,9 +882,6 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
     ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]);
     if (ret)
         goto out;
-    ret = libxl_device_vkb_add(ctx, dm_domid, &dm_config->vkbs[0]);
-    if (ret)
-        goto out;
 
     if (guest_config->b_info.u.hvm.serial)
         num_console++;
tools/libxl/libxl_internal.h +9 -0
index cfe9dc0..41c5e5b 100644
@@ -1909,6 +1909,11 @@ _hidden void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,
                                    libxl_device_nic *nic,
                                    libxl__ao_device *aodev);
 
+/* AO operation to connect a vkb device */
+_hidden void libxl__device_vkb_add(libxl__egc *egc, uint32_t domid,
+                                   libxl_device_vkb *vkb,
+                                   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
@@ -2329,6 +2334,10 @@ _hidden void libxl__add_nics(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
                              int start, libxl_domain_config *d_config,
                              libxl__ao_devices *aodevs);
 
+_hidden void libxl__add_vkbs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
+                             int start, libxl_domain_config *d_config,
+                             libxl__ao_devices *aodevs);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
tools/ocaml/libs/xl/xenlight_stubs.c +1 -1
index 0e9c65e..3d2493b 100644
@@ -315,7 +315,7 @@ value stub_xl_device_vkb_add(value info, value domid)
 	device_vkb_val(&gc, &lg, &c_info, info);
 
 	INIT_CTX();
-	ret = libxl_device_vkb_add(ctx, Int_val(domid), &c_info);
+	ret = libxl_device_vkb_add(ctx, Int_val(domid), &c_info, 0);
 	if (ret != 0)
 		failwith_xl("vkb_add", &lg);
 	FREE_CTX();
Signature · 1 line
1.7.7.5 (Apple Git-26)
Acked-by on v10 17/19
#34 lore raw

Re: [PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation

Roger Pau Monne writes ("[PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation"):

Quoted 3 lines

Split libxl_device_vkb_add into libxl__device_vkb_add (to be used inside already running ao's), and make libxl_device_vkb_add a stub to call libxl__device_vkb_add.

But if you are reposting for any reason you might like

tools/libxl/libxl_create.c static void domcreate_rebuild_done(libxl__egc *egc, · line 908

Quoted 3 lines
tools/libxl/libxl_create.c +2 -1
-    dcs->aodevs.size = d_config->num_disks;
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM && !d_config->num_vkbs) {
+        d_config->vkbs = libxl__zalloc(NOGC, sizeof(*d_config->vkbs));

to turn that into a use of GCNEW. Or move

tools/libxl/libxl_create.c static void domcreate_rebuild_done(libxl__egc *egc, · line 910

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+        libxl_device_vkb_init(&d_config->vkbs[0]);
+        d_config->num_vkbs = 1;

that assignment up and use GCNEW_ARRAY. Up to you.

Thanks,
Ian.

#35 lore raw

Re: [PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation

Quoted 14 lines · Ian Jackson

Roger Pau Monne writes ("[PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation"):

Quoted 3 lines

Split libxl_device_vkb_add into libxl__device_vkb_add (to be used inside already running ao's), and make libxl_device_vkb_add a stub to call libxl__device_vkb_add.

But if you are reposting for any reason you might like

tools/libxl/libxl_create.c static void domcreate_rebuild_done(libxl__egc *egc, · line 908

Quoted 3 lines
tools/libxl/libxl_create.c +2 -1
-    dcs->aodevs.size = d_config->num_disks;
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM && !d_config->num_vkbs) {
+        d_config->vkbs = libxl__zalloc(NOGC, sizeof(*d_config->vkbs));
Quoted 6 lines

to turn that into a use of GCNEW. Or move

tools/libxl/libxl_create.c static void domcreate_rebuild_done(libxl__egc *egc, · line 910

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+        libxl_device_vkb_init(&d_config->vkbs[0]);
+        d_config->num_vkbs = 1;

that assignment up and use GCNEW_ARRAY. Up to you.

I can't use GCNEW because this is not allocated from the GC (I'm passing NOGC to zalloc).

Thanks.

#36 lore raw

Re: [PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation

Quoted 17 lines · Roger Pau Monne
Quoted 16 lines · Ian Jackson

Roger Pau Monne writes ("[PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation"):

Quoted 3 lines

Split libxl_device_vkb_add into libxl__device_vkb_add (to be used inside already running ao's), and make libxl_device_vkb_add a stub to call libxl__device_vkb_add.

But if you are reposting for any reason you might like

tools/libxl/libxl_create.c static void domcreate_rebuild_done(libxl__egc *egc, · line 908

Quoted 3 lines
tools/libxl/libxl_create.c +2 -1
-    dcs->aodevs.size = d_config->num_disks;
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM && !d_config->num_vkbs) {
+        d_config->vkbs = libxl__zalloc(NOGC, sizeof(*d_config->vkbs));

to turn that into a use of GCNEW. Or move

Quoted 7 lines
Quoted 4 lines

tools/libxl/libxl_create.c static void domcreate_rebuild_done(libxl__egc *egc, · line 910

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+        libxl_device_vkb_init(&d_config->vkbs[0]);
+        d_config->num_vkbs = 1;

that assignment up and use GCNEW_ARRAY. Up to you.

I can't use GCNEW because this is not allocated from the GC (I'm passing NOGC to zalloc).

GCNEW uses libxl__zalloc which can take a NOGC, as can GCNEW_ARRAY via libxl__calloc. In both cases perhaps the doc comment needs an update.

Ian.

#37 lore raw

Re: [PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation

In reply to Roger Pau Monne

Roger Pau Monne writes ("Re: [PATCH v10 17/19] libxl: convert libxl_device_vkb_add to an async operation"):

Quoted 2 lines

I can't use GCNEW because this is not allocated from the GC (I'm passing NOGC to zalloc).

Oh of course, sorry.

Ian.

#38 lore raw

PATCH v10 18/19 libxl: convert libxl_device_vfb_add to an async operation

In reply to Roger Pau Monne

Split libxl_device_vfb_add into libxl__device_vfb_add (to be used inside already running ao's), and make libxl_device_vfb_add a stub to call libxl__device_vfb_add.

Changes since v9:

* Fixed Ocaml bindings.

---
 tools/libxl/libxl.c                  |   26 +++++++++++++++++++-------
 tools/libxl/libxl.h                  |    3 ++-
 tools/libxl/libxl_create.c           |   10 ++++------
 tools/libxl/libxl_device.c           |    2 ++
 tools/libxl/libxl_dm.c               |    9 +++++----
 tools/libxl/libxl_internal.h         |    9 +++++++++
 tools/ocaml/libs/xl/xenlight_stubs.c |    2 +-
 7 files changed, 42 insertions(+), 19 deletions(-)
tools/libxl/libxl.c +19 -7
index 7de82a9..412794f 100644
@@ -2923,12 +2923,13 @@ static int libxl__device_from_vfb(libxl__gc *gc, uint32_t domid,
     return 0;
 }
 
-int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
+void libxl__device_vfb_add(libxl__egc *egc, uint32_t domid,
+                           libxl_device_vfb *vfb, libxl__ao_device *aodev)
 {
-    GC_INIT(ctx);
+    STATE_AO_GC(aodev->ao);
     flexarray_t *front;
     flexarray_t *back;
-    libxl__device device;
+    libxl__device *device;
     int rc;
 
     rc = libxl__device_vfb_setdefault(gc, vfb);
@@ -2945,7 +2946,8 @@ int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
         goto out_free;
     }
 
-    rc = libxl__device_from_vfb(gc, domid, vfb, &device);
+    GCNEW(device);
+    rc = libxl__device_from_vfb(gc, domid, vfb, device);
     if (rc != 0) goto out_free;
 
     flexarray_append_pair(back, "frontend-id", libxl__sprintf(gc, "%d", domid));
@@ -2975,16 +2977,22 @@ int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
                           libxl__sprintf(gc, "%d", vfb->backend_domid));
     flexarray_append_pair(front, "state", libxl__sprintf(gc, "%d", 1));
 
-    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));
+
+    aodev->dev = device;
+    aodev->action = DEVICE_CONNECT;
+    libxl__wait_device_connection(egc, aodev);
+
     rc = 0;
 out_free:
     flexarray_free(front);
     flexarray_free(back);
 out:
-    GC_FREE;
-    return rc;
+    aodev->rc = rc;
+    if (rc) aodev->callback(egc, aodev);
+    return;
 }
 
 /******************************************************************************/
@@ -3055,6 +3063,7 @@ DEFINE_DEVICE_REMOVE(vfb, destroy, 1)
  * libxl_device_disk_add
  * libxl_device_nic_add
  * libxl_device_vkb_add
+ * libxl_device_vfb_add
  */
 
 #define DEFINE_DEVICE_ADD(type)                                         \
@@ -3084,6 +3093,9 @@ DEFINE_DEVICE_ADD(nic)
 /* vkb */
 DEFINE_DEVICE_ADD(vkb)
 
+/* vfb */
+DEFINE_DEVICE_ADD(vfb)
+
 #undef DEFINE_DEVICE_ADD
 
 /******************************************************************************/
tools/libxl/libxl.h +2 -1
index 84d4efd..414475e 100644
@@ -720,7 +720,8 @@ int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid,
                              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_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb,
+                         const libxl_asyncop_how *ao_how);
 int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid,
                             libxl_device_vfb *vfb,
                             const libxl_asyncop_how *ao_how);
tools/libxl/libxl_create.c +4 -6
index 39c858a..03edb3d 100644
@@ -910,12 +910,15 @@ static void domcreate_rebuild_done(libxl__egc *egc,
         d_config->num_vkbs = 1;
     }
 
-    dcs->aodevs.size = d_config->num_disks + d_config->num_vkbs;
+    dcs->aodevs.size = d_config->num_disks + d_config->num_vkbs +
+                       d_config->num_vfbs;
     dcs->aodevs.callback = domcreate_launch_dm;
     libxl__prepare_ao_devices(ao, &dcs->aodevs);
     libxl__add_disks(egc, ao, domid, 0, d_config, &dcs->aodevs);
     libxl__add_vkbs(egc, ao, domid, d_config->num_disks, d_config,
                     &dcs->aodevs);
+    libxl__add_vfbs(egc, ao, domid, d_config->num_disks + d_config->num_vkbs,
+                    d_config, &dcs->aodevs);
 
     return;
 
@@ -935,7 +938,6 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
     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) {
         LOG(ERROR, "unable to add misc devices");
@@ -971,10 +973,6 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__ao_devices *aodevs,
         int need_qemu = 0;
         libxl__device_console console;
 
-        for (i = 0; i < d_config->num_vfbs; i++) {
-            libxl_device_vfb_add(ctx, domid, &d_config->vfbs[i]);
-        }
-
         ret = init_console_info(&console, 0);
         if ( ret )
             goto error_out;
tools/libxl/libxl_device.c +2 -0
index 65cd264..f9c8784 100644
@@ -483,6 +483,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
  * libxl__add_disks
  * libxl__add_nics
  * libxl__add_vkbs
+ * libxl__add_vfbs
  */
 
 #define DEFINE_DEVICES_ADD(type)                                               \
@@ -503,6 +504,7 @@ void libxl__ao_devices_callback(libxl__egc *egc, libxl__ao_device *aodev)
 DEFINE_DEVICES_ADD(disk)
 DEFINE_DEVICES_ADD(nic)
 DEFINE_DEVICES_ADD(vkb)
+DEFINE_DEVICES_ADD(vfb)
 
 #undef DEFINE_DEVICES_ADD
 
tools/libxl/libxl_dm.c +5 -4
index a6ee080..cc35380 100644
@@ -833,12 +833,16 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
 
-    sdss->aodevs.size = dm_config->num_disks + dm_config->num_vkbs;
+    sdss->aodevs.size = dm_config->num_disks + dm_config->num_vkbs +
+                        dm_config->num_vfbs;
     sdss->aodevs.callback = spawn_stub_launch_dm;
     libxl__prepare_ao_devices(ao, &sdss->aodevs);
     libxl__add_disks(egc, ao, dm_domid, 0, dm_config, &sdss->aodevs);
     libxl__add_vkbs(egc, ao, dm_domid, dm_config->num_disks, dm_config,
                     &sdss->aodevs);
+    libxl__add_vfbs(egc, ao, dm_domid,
+                    dm_config->num_disks + dm_config->num_vfbs, dm_config,
+                    &sdss->aodevs);
 
     free(args);
     return;
@@ -879,9 +883,6 @@ static void spawn_stub_launch_dm(libxl__egc *egc,
          */
         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)
-        goto out;
 
     if (guest_config->b_info.u.hvm.serial)
         num_console++;
tools/libxl/libxl_internal.h +9 -0
index 41c5e5b..d02c5ef 100644
@@ -1914,6 +1914,11 @@ _hidden void libxl__device_vkb_add(libxl__egc *egc, uint32_t domid,
                                    libxl_device_vkb *vkb,
                                    libxl__ao_device *aodev);
 
+/* AO operation to connect a vfb device */
+_hidden void libxl__device_vfb_add(libxl__egc *egc, uint32_t domid,
+                                   libxl_device_vfb *vfb,
+                                   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
@@ -2338,6 +2343,10 @@ _hidden void libxl__add_vkbs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
                              int start, libxl_domain_config *d_config,
                              libxl__ao_devices *aodevs);
 
+_hidden void libxl__add_vfbs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
+                             int start, libxl_domain_config *d_config,
+                             libxl__ao_devices *aodevs);
+
 /*----- device model creation -----*/
 
 /* First layer; wraps libxl__spawn_spawn. */
tools/ocaml/libs/xl/xenlight_stubs.c +1 -1
index 3d2493b..adda67f 100644
@@ -369,7 +369,7 @@ value stub_xl_device_vfb_add(value info, value domid)
 	device_vfb_val(&gc, &lg, &c_info, info);
 
 	INIT_CTX();
-	ret = libxl_device_vfb_add(ctx, Int_val(domid), &c_info);
+	ret = libxl_device_vfb_add(ctx, Int_val(domid), &c_info, 0);
 	if (ret != 0)
 		failwith_xl("vfb_add", &lg);
 	FREE_CTX();
Signature · 1 line
1.7.7.5 (Apple Git-26)
#39 lore raw

Re: [PATCH v10 18/19] libxl: convert libxl_device_vfb_add to an async operation

Roger Pau Monne writes ("[PATCH v10 18/19] libxl: convert libxl_device_vfb_add to an async operation"):

Quoted 3 lines

Split libxl_device_vfb_add into libxl__device_vfb_add (to be used inside already running ao's), and make libxl_device_vfb_add a stub to call libxl__device_vfb_add.

If I'm not mistaken this has the effect of newly waiting for vfbs to enter the correct state on xenstore. Is that intentional ? If so it needs to be explained in the commit message.

Aside from that it looks plausible.

Thanks,
Ian.

#40 lore raw

Re: [PATCH v10 18/19] libxl: convert libxl_device_vfb_add to an async operation

Quoted 7 lines · Ian Jackson

Roger Pau Monne writes ("[PATCH v10 18/19] libxl: convert libxl_device_vfb_add to an async operation"):

Quoted 3 lines

Split libxl_device_vfb_add into libxl__device_vfb_add (to be used inside already running ao's), and make libxl_device_vfb_add a stub to call libxl__device_vfb_add.

If I'm not mistaken this has the effect of newly waiting for vfbs to enter the correct state on xenstore. Is that intentional ?

Yes, libxl waits for vfb to reach state 2 (InitWait), is that ok?

Quoted 2 lines

If so it
needs to be explained in the commit message.

Ok, I will rework on the commit message.

Quoted 4 lines

Aside from that it looks plausible.

Thanks,
Ian.

#41 lore raw

PATCH v10 19/19 xl: main_blockdetach don't call destroy if remove succeeds

In reply to Roger Pau Monne

xl was calling libxl_device_disk_destroy after a successful call to libxl_device_disk_remove, which leads to an error.

---
 tools/libxl/xl_cmdimpl.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +1 -2
index 1371458..d68bac3 100644
@@ -5416,8 +5416,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, 0);
+    }
     return 0;
 }
 
Signature · 1 line
1.7.7.5 (Apple Git-26)
#42 lore raw

Re: [PATCH v10 19/19] xl: main_blockdetach don't call destroy if remove succeeds

Quoted 14 lines · Roger Pau Monne

xl was calling libxl_device_disk_destroy after a successful call to libxl_device_disk_remove, which leads to an error.

---
 tools/libxl/xl_cmdimpl.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +0 -0
index 1371458..d68bac3 100644

tools/libxl/xl_cmdimpl.c int main_blockdetach(int argc, char **argv) · line 5419

Quoted 6 lines
tools/libxl/xl_cmdimpl.c +1 -2
@@ -5416,8 +5416,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, 0);
+    }

Somebody should probably be calling dispose on the disk here, unconditionally. dispose used to be called destroy so that might be where the confusion arose?

Ian.

#43 lore raw
In reply to Roger Pau Monne
Quoted 9 lines · Roger Pau Monne

Changes are available in the git repository at:
git://xenbits.xen.org/people/royger/xen.git hotplug.v10

Roger Pau Monne (19):
*libxl: check backend state before setting it to "closing"
libxl: change ao_device_remove to ao_device
*libxl: move device model creation prototypes *libxl: convert libxl_domain_destroy to an async op *libxl: move bootloader data strucutres and prototypes

Ian J has now acked all of these, so I have applied them.

Quoted 21 lines

libxl: refactor disk addition to take a helper
libxl: convert libxl__device_disk_local_attach to an async op
*libxl: rename vifs to nics
*libxl: convert libxl_device_disk_add to an async op *libxl: convert libxl_device_nic_add to an async operation *libxl: add option to choose who executes hotplug scripts *libxl: rename _IOEMU nic type to VIF_IOEMU
libxl: set correct nic type depending on the guest
*libxl: use libxl__xs_path_cleanup on device_destroy *libxl: call hotplug scripts for disk devices from libxl *libxl: call hotplug scripts for nic devices from libxl
libxl: convert libxl_device_vkb_add to an async operation
libxl: convert libxl_device_vfb_add to an async operation
xl: main_blockdetach don't call destroy if remove succeeds

* Acked

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