This series have been splitted in several patches, to make them easier to review. Also the amount of changes introduced is quite important, since apart from all the hotplug necessary functions and modifications, libxl_domain_destroy has been converted to an async op. This was necessary in order to have async operations during device removal. Also, as an important change, disk and nics are added at different points for HVM and device model based guests, since we need the disk in order to start Qemu, but the nic hotplug scripts should be called at a later point, when Qemu has created the corresponding tap device. Acked: [PATCH v5 02/10] libxl: move device model creation prototypes [PATCH v5 03/10] libxl: convert libxl_domain_destroy to an async op [PATCH v5 06/10] libxl: add option to choose who executes hotplug [PATCH v5 10/10] libxl: use libxl__xs_path_cleanup on device_destroy Pending: [PATCH v5 01/10] libxl: change libxl__ao_device_remove to [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op [PATCH v5 05/10] libxl: convert libxl_device_nic_add to an async [PATCH v5 07/10] libxl: set nic type to VIF by default [PATCH v5 08/10] libxl: call hotplug scripts for disk devices from [PATCH v5 09/10] libxl: call hotplug scripts for nic devices from
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 01/10] libxl: change libxl__ao_device_remove to libxl__ao_device
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 sinve v4: * Added a more detailed comment in _prepare and _initiate header. * Removed an unnecessary state check from libxl_initiate_device_remove. Changes since v2: * Remove unnecessary comments in libxl__ao_device. * Change libxl__device_cb to device_addrm_aocomplete. * Rename aorm parameter in device_addrm_aocomplete to aodev. * Use a macro to define {nic,disk,vkb,vfb}_{remove,destroy} functions. * Rename libxl__init_ao_device to libxl__prepare_ao_device and add a comment explaining why we need to set active to 1. * Replace al uses of aorm with aodev. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/libxl/libxl.c | 211 ++++++++++++++---------------------------- tools/libxl/libxl.h | 15 ++- tools/libxl/libxl_device.c | 110 +++++++++++++--------- tools/libxl/libxl_internal.h | 62 +++++++++++-- 4 files changed, 202 insertions(+), 196 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 0e281a6..c4e31e1 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -1317,6 +1317,26 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass) /******************************************************************************/ +/* generic callback for devices that only need to set ao_complete */ +static void device_addrm_aocomplete(libxl__egc *egc, libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); + + if (aodev->rc) { + LOGE(ERROR, "unable to %s %s with id %u", + aodev->action == DEVICE_CONNECT ? "add" : "remove", + libxl__device_kind_to_string(aodev->dev->kind), + aodev->dev->devid); + goto out; + } + +out: + libxl__ao_complete(egc, ao, aodev->rc); + return; +} + +/******************************************************************************/ + int libxl__device_disk_setdefault(libxl__gc *gc, libxl_device_disk *disk) { int rc; @@ -1486,42 +1506,6 @@ out: 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) @@ -1964,42 +1948,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) @@ -2326,42 +2274,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) @@ -2459,41 +2371,56 @@ out: return rc; } -int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid, - libxl_device_vfb *vfb, - const libxl_asyncop_how *ao_how) -{ - AO_CREATE(ctx, domid, ao_how); - libxl__device device; - int rc; - - rc = libxl__device_from_vfb(gc, domid, vfb, &device); - if (rc != 0) goto out; - - rc = libxl__initiate_device_remove(egc, ao, &device); - if (rc) goto out; - - return AO_INPROGRESS; - -out: - return AO_ABORT(rc); -} - -int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid, - libxl_device_vfb *vfb) -{ - GC_INIT(ctx); - libxl__device device; - int rc; - - rc = libxl__device_from_vfb(gc, domid, vfb, &device); - if (rc != 0) goto out; +/******************************************************************************/ - rc = libxl__device_destroy(gc, &device); -out: - GC_FREE; - return rc; -} +/* Macro for defining device remove/destroy functions in a compact way */ +#define DEFINE_DEVICE_REMOVE(type, removedestroy, f) \ + int libxl_device_##type##_##removedestroy(libxl_ctx *ctx, \ + uint32_t domid, libxl_device_##type *type, \ + const libxl_asyncop_how *ao_how) \ + { \ + AO_CREATE(ctx, domid, ao_how); \ + libxl__device *device; \ + libxl__ao_device *aodev; \ + int rc; \ + \ + GCNEW(device); \ + rc = libxl__device_from_##type(gc, domid, type, device); \ + if (rc != 0) goto out; \ + \ + GCNEW(aodev); \ + libxl__prepare_ao_device(aodev, ao, NULL); \ + aodev->action = DEVICE_DISCONNECT; \ + aodev->dev = device; \ + aodev->callback = device_addrm_aocomplete; \ + aodev->force = f; \ + libxl__initiate_device_remove(egc, aodev); \ + \ + out: \ + if (rc) return AO_ABORT(rc); \ + return AO_INPROGRESS; \ + } + +/* Define all remove/destroy functions and undef the macro */ + +/* disk */ +DEFINE_DEVICE_REMOVE(disk, remove, 0) +DEFINE_DEVICE_REMOVE(disk, destroy, 1) + +/* nic */ +DEFINE_DEVICE_REMOVE(nic, remove, 0) +DEFINE_DEVICE_REMOVE(nic, destroy, 1) + +/* vkb */ +DEFINE_DEVICE_REMOVE(vkb, remove, 0) +DEFINE_DEVICE_REMOVE(vkb, destroy, 1) + +/* vfb */ + +DEFINE_DEVICE_REMOVE(vfb, remove, 0) +DEFINE_DEVICE_REMOVE(vfb, destroy, 1) + +#undef DEFINE_DEVICE_REMOVE /******************************************************************************/ diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index 9c03f15..6bd028f 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -642,7 +642,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, @@ -666,7 +667,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, @@ -677,14 +680,18 @@ int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb); int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb, const libxl_asyncop_how *ao_how); -int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb); +int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid, + libxl_device_vkb *vkb, + const libxl_asyncop_how *ao_how); /* Framebuffer */ int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb); int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb, const libxl_asyncop_how *ao_how); -int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb); +int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid, + libxl_device_vfb *vfb, + const libxl_asyncop_how *ao_how); /* PCI Passthrough */ int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev); diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 2006406..d898a89 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -356,11 +356,16 @@ int libxl__device_disk_dev_number(const char *virtpath, int *pdisk, return -1; } +/* Device AO operations */ -typedef struct { - libxl__ao *ao; - libxl__ev_devstate ds; -} libxl__ao_device_remove; +void libxl__prepare_ao_device(libxl__ao_device *aodev, libxl__ao *ao, + libxl__ao_device **base) +{ + aodev->ao = ao; + aodev->active = 1; + aodev->rc = 0; + aodev->base = base; +} int libxl__device_destroy(libxl__gc *gc, libxl__device *dev) { @@ -436,34 +441,27 @@ out: /* Callbacks for device related operations */ -static void device_remove_callback(libxl__egc *egc, libxl__ev_devstate *ds, +static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds, int rc); -static void device_remove_cleanup(libxl__gc *gc, - libxl__ao_device_remove *aorm); +static void device_backend_cleanup(libxl__gc *gc, + libxl__ao_device *aodev); -int libxl__initiate_device_remove(libxl__egc *egc, libxl__ao *ao, - libxl__device *dev) +void libxl__initiate_device_remove(libxl__egc *egc, + libxl__ao_device *aodev) { - AO_GC; + STATE_AO_GC(aodev->ao); libxl_ctx *ctx = libxl__gc_owner(gc); - xs_transaction_t t; - char *be_path = libxl__device_backend_path(gc, dev); + xs_transaction_t t = 0; + char *be_path = libxl__device_backend_path(gc, aodev->dev); char *state_path = libxl__sprintf(gc, "%s/state", be_path); - char *state = libxl__xs_read(gc, XBT_NULL, state_path); int rc = 0; - libxl__ao_device_remove *aorm = 0; - - if (!state) - goto out_ok; - if (atoi(state) != 4) { - libxl__device_destroy_tapdisk(gc, be_path); - xs_rm(ctx->xsh, XBT_NULL, be_path); - goto out_ok; - } retry_transaction: t = xs_transaction_start(ctx->xsh); + if (aodev->force) + libxl__xs_path_cleanup(gc, t, + libxl__device_frontend_path(gc, aodev->dev)); xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", strlen("0")); xs_write(ctx->xsh, t, state_path, "5", strlen("5")); if (!xs_transaction_end(ctx->xsh, t, 0)) { @@ -474,42 +472,68 @@ retry_transaction: goto out_fail; } } + /* mark transaction as ended, to prevent double closing it on out_ok */ + t = 0; libxl__device_destroy_tapdisk(gc, be_path); - aorm = libxl__zalloc(gc, sizeof(*aorm)); - aorm->ao = ao; - libxl__ev_devstate_init(&aorm->ds); + libxl__ev_devstate_init(&aodev->ds); - rc = libxl__ev_devstate_wait(gc, &aorm->ds, device_remove_callback, + rc = libxl__ev_devstate_wait(gc, &aodev->ds, device_backend_callback, state_path, XenbusStateClosed, LIBXL_DESTROY_TIMEOUT * 1000); - if (rc) goto out_fail; + if (rc) { + LOG(ERROR, "unable to remove device %s", be_path); + goto out_fail; + } - return 0; + return; out_fail: assert(rc); - device_remove_cleanup(gc, aorm); - return rc; - - out_ok: - libxl__ao_complete(egc, ao, 0); - return 0; + aodev->rc = rc; + aodev->callback(egc, aodev); + return; } -static void device_remove_callback(libxl__egc *egc, libxl__ev_devstate *ds, +static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds, int rc) { - libxl__ao_device_remove *aorm = CONTAINER_OF(ds, *aorm, ds); - libxl__gc *gc = &aorm->ao->gc; - libxl__ao_complete(egc, aorm->ao, rc); - device_remove_cleanup(gc, aorm); + libxl__ao_device *aodev = CONTAINER_OF(ds, *aodev, ds); + STATE_AO_GC(aodev->ao); + + device_backend_cleanup(gc, aodev); + + if (rc == ERROR_TIMEDOUT && aodev->action == DEVICE_DISCONNECT && + !aodev->force) { + aodev->force = 1; + libxl__initiate_device_remove(egc, aodev); + return; + } + + /* Some devices (vkbd) fail to disconnect properly, + * but we shouldn''t alarm the user if it''s during + * domain destruction. + */ + if (rc && aodev->action == DEVICE_CONNECT) { + LOG(ERROR, "unable to connect device with path %s", + libxl__device_backend_path(gc, aodev->dev)); + goto out; + } else if (rc) { + LOG(DEBUG, "unable to disconnect device with path %s", + libxl__device_backend_path(gc, aodev->dev)); + goto out; + } + +out: + aodev->rc = rc; + aodev->callback(egc, aodev); + return; } -static void device_remove_cleanup(libxl__gc *gc, - libxl__ao_device_remove *aorm) { - if (!aorm) return; - libxl__ev_devstate_cancel(gc, &aorm->ds); +static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev) +{ + if (!aodev) return; + libxl__ev_devstate_cancel(gc, &aodev->ds); } int libxl__wait_for_device_model(libxl__gc *gc, diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index bbc2149..5baf1f0 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -848,13 +848,6 @@ _hidden const char *libxl__device_nic_devname(libxl__gc *gc, uint32_t devid, libxl_nic_type type); -/* Arranges that dev will be removed from its guest. When - * this is done, the ao will be completed. An error - * return from libxl__initiate_device_remove means that the ao - * will _not_ be completed and the caller must do so. */ -_hidden int libxl__initiate_device_remove(libxl__egc*, libxl__ao*, - libxl__device *dev); - /* * libxl__ev_devstate - waits a given time for a device to * reach a given state. Follows the libxl_ev_* conventions. @@ -1837,6 +1830,61 @@ _hidden void libxl__bootloader_init(libxl__bootloader_state *bl); * If callback is passed rc==0, will have updated st->info appropriately */ _hidden void libxl__bootloader_run(libxl__egc*, libxl__bootloader_state *st); +/*----- device addition/removal -----*/ + +/* Action to perform (either connect or disconnect) */ +typedef enum { + DEVICE_CONNECT, + DEVICE_DISCONNECT +} libxl__device_action; + +typedef struct libxl__ao_device libxl__ao_device; +typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*); + +/* This functions sets the necessary libxl__ao_device struct values to use + * safely inside functions. It marks the operation as "active" + * since we need to be sure that all device status structs are set + * to active before start queueing events, or we might call + * ao_complete before all devices had finished + * + * libxl__initiate_device_{remove/addition} should not be called without + * calling libxl__prepare_ao_device first, since it initializes the private + * fields of the struct libxl__ao_device to what this functions expect. + * + * Once _prepare has been called on a libxl__ao_device, it is safe to just + * discard this struct, there''s no need to call any destroy function. + * _prepare can also be called multiple times with the same libxl__ao_device. + */ +_hidden void libxl__prepare_ao_device(libxl__ao_device *aodev, libxl__ao *ao, + libxl__ao_device **base); + +struct libxl__ao_device { + /* filled in by user */ + libxl__ao *ao; + /* action being performed */ + libxl__device_action action; + libxl__device *dev; + int force; + libxl__device_callback *callback; + /* private for implementation */ + int active; + int rc; + libxl__ev_devstate ds; + void *base; +}; + +/* Arranges that dev will be removed to the guest, and the + * hotplug scripts will be executed (if necessary). When + * this is done (or an error happens), the callback in + * aodev->callback will be called. + * + * The libxl__ao_device passed to this function should be + * prepared using libxl__prepare_ao_device prior to calling + * this function. + */ +_hidden void libxl__initiate_device_remove(libxl__egc *egc, + libxl__ao_device *aodev); + /*----- Domain creation -----*/ typedef struct libxl__domain_create_state libxl__domain_create_state; -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 02/10] libxl: move device model creation prototypes
Move prototypes regarding device model creation, since they will depend on domain destruction in future patches. This patch is pure code motion. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/libxl/libxl_internal.h | 75 ++++++++++++++++++++--------------------- 1 files changed, 37 insertions(+), 38 deletions(-) diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 5baf1f0..5605440 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -1088,44 +1088,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 @@ -1885,6 +1847,43 @@ struct libxl__ao_device { _hidden void libxl__initiate_device_remove(libxl__egc *egc, libxl__ao_device *aodev); +/*----- device model creation -----*/ + +/* First layer; wraps libxl__spawn_spawn. */ + +typedef struct libxl__dm_spawn_state libxl__dm_spawn_state; + +typedef void libxl__dm_spawn_cb(libxl__egc *egc, libxl__dm_spawn_state*, + int rc /* if !0, error was logged */); + +struct libxl__dm_spawn_state { + /* mixed - spawn.ao must be initialised by user; rest is private: */ + libxl__spawn_state spawn; + /* filled in by user, must remain valid: */ + uint32_t guest_domid; /* domain being served */ + libxl_domain_config *guest_config; + libxl__domain_build_state *build_state; /* relates to guest_domid */ + libxl__dm_spawn_cb *callback; +}; + +_hidden void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state*); + +/* Stubdom device models. */ + +typedef struct { + /* Mixed - user must fill in public parts EXCEPT callback, + * which may be undefined on entry. (See above for details) */ + libxl__dm_spawn_state dm; /* the stub domain device model */ + /* filled in by user, must remain valid: */ + libxl__dm_spawn_cb *callback; /* called as callback(,&sdss->dm,) */ + /* private to libxl__spawn_stub_dm: */ + libxl_domain_config dm_config; + libxl__domain_build_state dm_state; + libxl__dm_spawn_state pvqemu; +} libxl__stub_dm_spawn_state; + +_hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*); + /*----- Domain creation -----*/ typedef struct libxl__domain_create_state libxl__domain_create_state; -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 03/10] libxl: convert libxl_domain_destroy to an async op
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 v4: * Fixed spelling mistakes. * Always use "force = 1" in device destruction in libxl__destroy_domid function. * Changed name of domain destroy callbacks to include "destroy". * Changed the use of rc to catch syscall errors. * Use libxl__remove_file instead of unlink. * Changed variable name of number of devices returned by libxl__xs_directory. * Simplify libxl__ao_device_check_last return. * Correctly propagate error returned from libxl__num_devices. * Add a comment about the use of libxl__device_destroy to destroy the console. * Fixed some uses of LIBXL__LOG. Changes since v3: * Fixed python bindings. Changes since v2: * Remove printfs. * Replace aorm with aodev. * Define an auxiliary libxl__ao_device *aodev to avoid using the long expression: drs->aorm[numdev]... * Added a common callback for both domain and stubdomain destruction that checks if both domains are finished and handles errors correctly. * Change libxl__ao_device_check_last logic a bit and add a comment describing how does it work. * Fixed spelling mistakes. * Use a do-while for xs transaction in device_remove_callback. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/libxl/libxl.c | 167 +++++++++++++++++++++++++++++++++++-- tools/libxl/libxl.h | 3 +- tools/libxl/libxl_create.c | 29 ++++++- tools/libxl/libxl_device.c | 160 +++++++++++++++++++++++++++++++---- tools/libxl/libxl_dm.c | 83 +++++++++--------- tools/libxl/libxl_internal.h | 94 ++++++++++++++++++++- tools/libxl/xl_cmdimpl.c | 12 ++-- tools/python/xen/lowlevel/xl/xl.c | 2 +- 8 files changed, 469 insertions(+), 81 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index c4e31e1..4044b9e 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -1112,11 +1112,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; @@ -1127,7 +1249,7 @@ 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)) { @@ -1160,7 +1282,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); @@ -1183,9 +1335,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, libxl_console_type type) diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index 6bd028f..d5dc2a0 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -526,7 +526,8 @@ int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info, int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid, int suspend_cancel); int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid); int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid); -int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid); +int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, + const libxl_asyncop_how *ao_how); int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid, libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid); /* get max. number of cpus supported by hypervisor */ diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index e5999c0..1df116e 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -586,6 +586,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) { @@ -860,16 +866,31 @@ static void domcreate_complete(libxl__egc *egc, if (rc) { if (dcs->guest_domid) { - int rc2 = libxl_domain_destroy(CTX, dcs->guest_domid); - if (rc2) - LOG(ERROR, "unable to destroy domain %d following" - " failed creation", dcs->guest_domid); + dcs->dds.ao = ao; + dcs->dds.domid = dcs->guest_domid; + dcs->dds.callback = domcreate_destruction_cb; + libxl__domain_destroy(egc, &dcs->dds); + return; } dcs->guest_domid = -1; } dcs->callback(egc, dcs, rc, dcs->guest_domid); } +static void domcreate_destruction_cb(libxl__egc *egc, + libxl__domain_destroy_state *dds, + int rc) +{ + STATE_AO_GC(dds->ao); + libxl__domain_create_state *dcs = CONTAINER_OF(dds, *dcs, dds); + + if (rc) + LOG(ERROR, "unable to destroy domain %u following failed creation", + dds->domid); + + dcs->callback(egc, dcs, ERROR_FAIL, dcs->guest_domid); +} + /*----- application-facing domain creation interface -----*/ typedef struct { diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index d898a89..882e647 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -58,6 +58,48 @@ int libxl__parse_backend_path(libxl__gc *gc, return libxl__device_kind_from_string(strkind, &dev->backend_kind); } +static int libxl__num_devices(libxl__gc *gc, uint32_t domid) +{ + char *path; + unsigned int num_kinds, num_devs; + char **kinds = NULL, **devs = NULL; + int i, j, rc = 0; + libxl__device dev; + libxl__device_kind kind; + int numdevs = 0; + + path = GCSPRINTF("/local/domain/%d/device", domid); + kinds = libxl__xs_directory(gc, XBT_NULL, path, &num_kinds); + if (!kinds) { + if (errno != ENOENT) { + LOGE(ERROR, "unable to get xenstore device listing %s", path); + rc = ERROR_FAIL; + goto out; + } + num_kinds = 0; + } + for (i = 0; i < num_kinds; i++) { + if (libxl__device_kind_from_string(kinds[i], &kind)) + continue; + + path = GCSPRINTF("/local/domain/%d/device/%s", domid, kinds[i]); + devs = libxl__xs_directory(gc, XBT_NULL, path, &num_devs); + if (!devs) + continue; + for (j = 0; j < num_devs; j++) { + path = GCSPRINTF("/local/domain/%d/device/%s/%s/backend", + domid, kinds[i], devs[j]); + path = libxl__xs_read(gc, XBT_NULL, path); + if (path && libxl__parse_backend_path(gc, path, &dev) == 0) { + numdevs++; + } + } + } +out: + if (rc) return rc; + return numdevs; +} + int libxl__device_generic_add(libxl__gc *gc, libxl__device *device, char **bents, char **fents) { @@ -367,6 +409,23 @@ void libxl__prepare_ao_device(libxl__ao_device *aodev, libxl__ao *ao, aodev->base = base; } +int libxl__ao_device_check_last(libxl__gc *gc, libxl__ao_device *device, + libxl__ao_device *list, int num) +{ + int i, error = 0; + + device->active = 0; + for (i = 0; i < num; i++) { + if (list[i].active) + return +1; + + if (list[i].rc) + error = list[i].rc; + } + + return error; +} + int libxl__device_destroy(libxl__gc *gc, libxl__device *dev) { libxl_ctx *ctx = libxl__gc_owner(gc); @@ -381,16 +440,35 @@ int libxl__device_destroy(libxl__gc *gc, libxl__device *dev) return 0; } -int libxl__devices_destroy(libxl__gc *gc, uint32_t domid) +/* Callback for device destruction */ + +static void device_remove_callback(libxl__egc *egc, libxl__ao_device *aodev); + +void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs) { + STATE_AO_GC(drs->ao); libxl_ctx *ctx = libxl__gc_owner(gc); + uint32_t domid = drs->domid; char *path; - unsigned int num_kinds, num_devs; + unsigned int num_kinds, num_dev_xsentries; char **kinds = NULL, **devs = NULL; - int i, j; - libxl__device dev; + int i, j, numdev = 0, rc = 0; + libxl__device *dev; + libxl__ao_device *aodev; libxl__device_kind kind; + drs->num_devices = libxl__num_devices(gc, drs->domid); + if (drs->num_devices < 0) { + LOG(ERROR, "unable to get number of devices for domain %u", drs->domid); + rc = drs->num_devices; + goto out; + } + + GCNEW_ARRAY(drs->aodev, drs->num_devices); + for (i = 0; i < drs->num_devices; i++) { + libxl__prepare_ao_device(&drs->aodev[i], drs->ao, &drs->aodev); + } + path = libxl__sprintf(gc, "/local/domain/%d/device", domid); kinds = libxl__xs_directory(gc, XBT_NULL, path, &num_kinds); if (!kinds) { @@ -406,19 +484,25 @@ int libxl__devices_destroy(libxl__gc *gc, uint32_t domid) continue; path = libxl__sprintf(gc, "/local/domain/%d/device/%s", domid, kinds[i]); - devs = libxl__xs_directory(gc, XBT_NULL, path, &num_devs); + devs = libxl__xs_directory(gc, XBT_NULL, path, &num_dev_xsentries); if (!devs) continue; - for (j = 0; j < num_devs; j++) { + for (j = 0; j < num_dev_xsentries; j++) { path = libxl__sprintf(gc, "/local/domain/%d/device/%s/%s/backend", domid, kinds[i], devs[j]); path = libxl__xs_read(gc, XBT_NULL, path); - if (path && libxl__parse_backend_path(gc, path, &dev) == 0) { - dev.domid = domid; - dev.kind = kind; - dev.devid = atoi(devs[j]); - - libxl__device_destroy(gc, &dev); + GCNEW(dev); + if (path && libxl__parse_backend_path(gc, path, dev) == 0) { + aodev = &drs->aodev[numdev]; + dev->domid = domid; + dev->kind = kind; + dev->devid = atoi(devs[j]); + aodev->action = DEVICE_DISCONNECT; + aodev->dev = dev; + aodev->callback = device_remove_callback; + aodev->force = drs->force; + libxl__initiate_device_remove(egc, aodev); + numdev++; } } } @@ -426,17 +510,22 @@ int libxl__devices_destroy(libxl__gc *gc, uint32_t domid) /* console 0 frontend directory is not under /local/domain/<domid>/device */ path = libxl__sprintf(gc, "/local/domain/%d/console/backend", domid); path = libxl__xs_read(gc, XBT_NULL, path); + GCNEW(dev); if (path && strcmp(path, "") && - libxl__parse_backend_path(gc, path, &dev) == 0) { - dev.domid = domid; - dev.kind = LIBXL__DEVICE_KIND_CONSOLE; - dev.devid = 0; - - libxl__device_destroy(gc, &dev); + libxl__parse_backend_path(gc, path, dev) == 0) { + dev->domid = domid; + dev->kind = LIBXL__DEVICE_KIND_CONSOLE; + dev->devid = 0; + + /* Currently console devices can be destroyed synchronously by just + * removing xenstore entries, this is what libxl__device_destroy does. + */ + libxl__device_destroy(gc, dev); } out: - return 0; + if (!numdev) drs->callback(egc, drs, rc); + return; } /* Callbacks for device related operations */ @@ -536,6 +625,39 @@ static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev) libxl__ev_devstate_cancel(gc, &aodev->ds); } +static void device_remove_callback(libxl__egc *egc, libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); + libxl__devices_remove_state *drs = CONTAINER_OF(aodev->base, *drs, aodev); + char *be_path = libxl__device_backend_path(gc, aodev->dev); + char *fe_path = libxl__device_frontend_path(gc, aodev->dev); + xs_transaction_t t = 0; + int rc = 0, ret = 0; + + if (aodev->action == DEVICE_DISCONNECT) { + do { + t = xs_transaction_start(CTX->xsh); + libxl__xs_path_cleanup(gc, t, fe_path); + libxl__xs_path_cleanup(gc, t, be_path); + ret = !xs_transaction_end(CTX->xsh, t, 0); + } while (ret && errno == EAGAIN); + + if (ret) { + LOGE(ERROR, "unable to finish transaction"); + rc = ERROR_FAIL; + goto out; + } + } + +out: + rc = libxl__ao_device_check_last(gc, aodev, drs->aodev, + drs->num_devices); + if (rc > 0) return; + + drs->callback(egc, drs, rc); + return; +} + int libxl__wait_for_device_model(libxl__gc *gc, uint32_t domid, char *state, libxl__spawn_starting *spawning, diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c index d987347..99059dc 100644 --- a/tools/libxl/libxl_dm.c +++ b/tools/libxl/libxl_dm.c @@ -673,6 +673,10 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc, libxl__dm_spawn_state *stubdom_dmss, int rc); +static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc, + libxl__destroy_domid_state *dis, + int rc); + void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss) { STATE_AO_GC(sdss->dm.spawn.ao); @@ -893,12 +897,31 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc, out: if (rc) { - if (dm_domid) - libxl_domain_destroy(CTX, dm_domid); + if (dm_domid) { + sdss->dis.ao = ao; + sdss->dis.domid = dm_domid; + sdss->dis.callback = spaw_stubdom_pvqemu_destroy_cb; + libxl__destroy_domid(egc, &sdss->dis); + return; + } } sdss->callback(egc, &sdss->dm, rc); } +static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc, + libxl__destroy_domid_state *dis, + int rc) +{ + libxl__stub_dm_spawn_state *sdss = CONTAINER_OF(dis, *sdss, dis); + STATE_AO_GC(sdss->dis.ao); + + if (rc) + LOG(ERROR, "destruction of domain %u after failed creation failed", + sdss->pvqemu.guest_domid); + + sdss->callback(egc, &sdss->dm, rc); +} + /* callbacks passed to libxl__spawn_spawn */ static void device_model_confirm(libxl__egc *egc, libxl__spawn_state *spawn, const char *xsdata); @@ -1091,48 +1114,24 @@ int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid) int ret; pid = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "/local/domain/%d/image/device-model-pid", domid)); - if (!pid) { - int stubdomid = libxl_get_stubdom_id(ctx, domid); - const char *savefile; - - if (!stubdomid) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn''t find device model''s pid"); - ret = ERROR_INVAL; - goto out; - } - LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device model is a stubdom, domid=%d", stubdomid); - ret = libxl_domain_destroy(ctx, stubdomid); - if (ret) - goto out; - - savefile = libxl__device_model_savefile(gc, domid); - ret = unlink(savefile); - /* - * On suspend libxl__domain_save_device_model will have already - * unlinked the save file. - */ - if (ret && errno == ENOENT) ret = 0; - if (ret) { - LIBXL__LOG_ERRNO(ctx, XTL_ERROR, - "failed to remove device-model savefile %s\n", - savefile); - goto out; - } + if (!pid || !atoi(pid)) { + LOG(ERROR, "could not find device-model''s pid for dom %u", domid); + ret = ERROR_FAIL; + goto out; + } + ret = kill(atoi(pid), SIGHUP); + if (ret < 0 && errno == ESRCH) { + LOG(ERROR, "Device Model already exited"); + ret = 0; + } else if (ret == 0) { + LOG(DEBUG, "Device Model signaled"); + ret = 0; } else { - ret = kill(atoi(pid), SIGHUP); - if (ret < 0 && errno == ESRCH) { - LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device Model already exited"); - ret = 0; - } else if (ret == 0) { - LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device Model signaled"); - ret = 0; - } else { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "failed to kill Device Model [%d]", - atoi(pid)); - ret = ERROR_FAIL; - goto out; - } + LOGE(ERROR, "failed to kill Device Model [%d]", atoi(pid)); + ret = ERROR_FAIL; + goto out; } + xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(gc, "/local/domain/0/device-model/%d", domid)); xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(gc, "/local/domain/%d/hvmloader", domid)); diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 5605440..5c15ca5 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -817,7 +817,6 @@ _hidden char *libxl__device_frontend_path(libxl__gc *gc, libxl__device *device); _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path, libxl__device *dev); _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev); -_hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid); _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state); /* @@ -1820,6 +1819,16 @@ typedef void libxl__device_callback(libxl__egc*, libxl__ao_device*); _hidden void libxl__prepare_ao_device(libxl__ao_device *aodev, libxl__ao *ao, libxl__ao_device **base); +/* Check if there are devices that have pending events in the array + * pointed to by the "list" parameter. Return values can be: + * < 0: All done, but error(s) found. + * 0: All done + * > 0: Not all done + * The passed libxl__ao_device struct in "device" is marked as finished. + */ +_hidden int libxl__ao_device_check_last(libxl__gc *gc, libxl__ao_device *device, + libxl__ao_device *list, int num); + struct libxl__ao_device { /* filled in by user */ libxl__ao *ao; @@ -1847,6 +1856,86 @@ struct libxl__ao_device { _hidden void libxl__initiate_device_remove(libxl__egc *egc, libxl__ao_device *aodev); +/*----- Domain destruction -----*/ + +/* Domain destruction has been split into two functions: + * + * libxl__domain_destroy is the main destroy function, which detects + * stubdoms and calls libxl__destroy_domid on the domain and its + * stubdom if present, creating a different libxl__destroy_domid_state + * for each one of them. + * + * libxl__destroy_domid actually destroys the domain, but it + * doesn''t check for stubdomains, since that would involve + * recursion, which we want to avoid. + */ + +typedef struct libxl__domain_destroy_state libxl__domain_destroy_state; +typedef struct libxl__destroy_domid_state libxl__destroy_domid_state; +typedef struct libxl__devices_remove_state libxl__devices_remove_state; + +typedef void libxl__domain_destroy_cb(libxl__egc *egc, + libxl__domain_destroy_state *dds, + int rc); + +typedef void libxl__domid_destroy_cb(libxl__egc *egc, + libxl__destroy_domid_state *dis, + int rc); + +typedef void libxl__devices_remove_callback(libxl__egc *egc, + libxl__devices_remove_state *drs, + int rc); + +struct libxl__devices_remove_state { + /* filled in by user */ + libxl__ao *ao; + uint32_t domid; + libxl__devices_remove_callback *callback; + int force; /* libxl_device_TYPE_destroy rather than _remove */ + /* private */ + libxl__ao_device *aodev; + int num_devices; +}; + +struct libxl__destroy_domid_state { + /* filled in by user */ + libxl__ao *ao; + uint32_t domid; + libxl__domid_destroy_cb *callback; + /* private to implementation */ + libxl__devices_remove_state drs; +}; + +struct libxl__domain_destroy_state { + /* filled by the user */ + libxl__ao *ao; + uint32_t domid; + libxl__domain_destroy_cb *callback; + /* Private */ + int rc; + uint32_t stubdomid; + libxl__destroy_domid_state stubdom; + int stubdom_finished; + libxl__destroy_domid_state domain; + int domain_finished; +}; + +/* + * Entry point for domain destruction + * This function checks for stubdom presence and then calls + * libxl__destroy_domid on the passed domain and its stubdom if found. + */ +_hidden void libxl__domain_destroy(libxl__egc *egc, + libxl__domain_destroy_state *dds); + +/* Used to destroy a domain with the passed id (it doesn''t check for stubs) */ +_hidden void libxl__destroy_domid(libxl__egc *egc, + libxl__destroy_domid_state *dis); + +/* Entry point for devices destruction */ +_hidden void libxl__devices_destroy(libxl__egc *egc, + libxl__devices_remove_state *drs); + /*----- device model creation -----*/ /* First layer; wraps libxl__spawn_spawn. */ @@ -1880,6 +1969,7 @@ typedef struct { libxl_domain_config dm_config; libxl__domain_build_state dm_state; libxl__dm_spawn_state pvqemu; + libxl__destroy_domid_state dis; } libxl__stub_dm_spawn_state; _hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*); @@ -1906,6 +1996,8 @@ struct libxl__domain_create_state { libxl__stub_dm_spawn_state dmss; /* If we''re not doing stubdom, we use only dmss.dm, * for the non-stubdom device model. */ + /* necessary if the domain creation failed and we have to destroy it */ + libxl__domain_destroy_state dds; }; /* diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index ca988d6..3220e89 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -1389,7 +1389,7 @@ static int handle_domain_death(uint32_t domid, /* fall-through */ case LIBXL_ACTION_ON_SHUTDOWN_DESTROY: LOG("Domain %d needs to be cleaned up: destroying the domain", domid); - libxl_domain_destroy(ctx, domid); + libxl_domain_destroy(ctx, domid, 0); break; case LIBXL_ACTION_ON_SHUTDOWN_COREDUMP_DESTROY: @@ -1987,7 +1987,7 @@ start: error_out: release_lock(); if (libxl_domid_valid_guest(domid)) - libxl_domain_destroy(ctx, domid); + libxl_domain_destroy(ctx, domid, 0); out: if (logfile != 2) @@ -2550,7 +2550,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); } } @@ -2824,7 +2824,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); } @@ -3084,7 +3084,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); @@ -3237,7 +3237,7 @@ static void migrate_receive(int debug, int daemonize, int monitor, if (rc) { fprintf(stderr, "migration target: Failure, destroying our copy.\n"); - rc2 = libxl_domain_destroy(ctx, domid); + rc2 = libxl_domain_destroy(ctx, domid, 0); if (rc2) { fprintf(stderr, "migration target: Failed to destroy our copy" " (code %d).\n", rc2); diff --git a/tools/python/xen/lowlevel/xl/xl.c b/tools/python/xen/lowlevel/xl/xl.c index 1db777d..4ff3120 100644 --- a/tools/python/xen/lowlevel/xl/xl.c +++ b/tools/python/xen/lowlevel/xl/xl.c @@ -437,7 +437,7 @@ static PyObject *pyxl_domain_destroy(XlObject *self, PyObject *args) int domid; if ( !PyArg_ParseTuple(args, "i", &domid) ) return NULL; - if ( libxl_domain_destroy(self->ctx, domid) ) { + if ( libxl_domain_destroy(self->ctx, domid, 0) ) { PyErr_SetString(xl_error_obj, "cannot destroy domain"); return NULL; } -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
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 latter 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 v4: * Added more comments about libxl__device_disk_add and libxl__add_disks. * Removed stray hunk in Makefile. * Fixed _prepare call libxl__add_disks (separate into a different loop). * Moved some code to check if disks have finished to a macro that generates a custom function, which will also be used for vif interfaces. Changes since v2: * Remove state read from libxl__initiate_device_add Cc: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/libxl/libxl.c | 50 ++++++++++++++++++++++++-------- tools/libxl/libxl.h | 4 ++- tools/libxl/libxl_create.c | 44 ++++++++++++++++++++++------ tools/libxl/libxl_device.c | 65 ++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_dm.c | 63 +++++++++++++++++++++++++++++++--------- tools/libxl/libxl_internal.h | 52 +++++++++++++++++++++++++++++++++ tools/libxl/xl_cmdimpl.c | 2 +- 7 files changed, 242 insertions(+), 38 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 4044b9e..b674557 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -1540,13 +1540,31 @@ static int libxl__device_from_disk(libxl__gc *gc, uint32_t domid, return 0; } -int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk) +int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_disk *disk, + const libxl_asyncop_how *ao_how) { - GC_INIT(ctx); + AO_CREATE(ctx, domid, ao_how); + libxl__ao_device *device; + + GCNEW(device); + libxl__prepare_ao_device(device, ao, NULL); + device->callback = device_addrm_aocomplete; + libxl__device_disk_add(egc, domid, disk, device); + + return AO_INPROGRESS; +} + +void libxl__device_disk_add(libxl__egc *egc, uint32_t domid, + libxl_device_disk *disk, + libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); + libxl_ctx *ctx = CTX; flexarray_t *front; flexarray_t *back; char *dev; - libxl__device device; + libxl__device *device; int major, minor, rc; rc = libxl__device_disk_setdefault(gc, disk); @@ -1570,7 +1588,8 @@ int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis 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); @@ -1588,7 +1607,7 @@ int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis 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); @@ -1609,7 +1628,7 @@ int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis 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); @@ -1641,22 +1660,27 @@ int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *dis 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, &device, + libxl__device_generic_add(gc, 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__initiate_device_add(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_disk_from_xs_be(libxl__gc *gc, @@ -1859,11 +1883,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++) diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index d5dc2a0..ff0078d 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -638,7 +638,9 @@ void libxl_vminfo_list_free(libxl_vminfo *list, int nr); */ /* Disks */ -int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk); +int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, + libxl_device_disk *disk, + const libxl_asyncop_how *ao_how); int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk, const libxl_asyncop_how *ao_how); diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index 1df116e..2dc1cee 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -577,6 +577,14 @@ static void domcreate_bootloader_done(libxl__egc *egc, libxl__bootloader_state *bl, int rc); +static void domcreate_launch_dm(libxl__egc *egc, + libxl__domain_create_state *dcs, + int rc); + +DEFINE_DEVICES_CALLBACK(domcreate_disk_connected, + libxl__domain_create_state, + devices, num_devices, domcreate_launch_dm) + static void domcreate_console_available(libxl__egc *egc, libxl__domain_create_state *dcs); @@ -677,7 +685,6 @@ static void domcreate_bootloader_done(libxl__egc *egc, { libxl__domain_create_state *dcs = CONTAINER_OF(bl, *dcs, bl); STATE_AO_GC(bl->ao); - int i; /* convenience aliases */ const uint32_t domid = dcs->guest_domid; @@ -716,15 +723,34 @@ static void domcreate_bootloader_done(libxl__egc *egc, store_libxl_entry(gc, domid, &d_config->b_info); - for (i = 0; i < d_config->num_disks; i++) { - ret = libxl_device_disk_add(ctx, domid, &d_config->disks[i]); - if (ret) { - LIBXL__LOG(ctx, LIBXL__LOG_ERROR, - "cannot add disk %d to domain: %d", i, ret); - ret = ERROR_FAIL; - goto error_out; - } + dcs->num_devices = d_config->num_disks; + libxl__add_disks(egc, ao, domid, d_config, &dcs->devices, + domcreate_disk_connected); + + return; + + error_out: + assert(ret); + domcreate_complete(egc, dcs, ret); +} + +static void domcreate_launch_dm(libxl__egc *egc, + libxl__domain_create_state *dcs, int rc) +{ + STATE_AO_GC(dcs->ao); + int i, ret = 0; + + /* convenience aliases */ + const uint32_t domid = dcs->guest_domid; + libxl_domain_config *const d_config = dcs->guest_config; + libxl__domain_build_state *const state = &dcs->build_state; + libxl_ctx *const ctx = CTX; + + if (rc) { + LOG(ERROR, "error connecting disk devices"); + goto error_out; } + for (i = 0; i < d_config->num_vifs; i++) { ret = libxl_device_nic_add(ctx, domid, &d_config->vifs[i]); if (ret) { diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 882e647..0beb4bb 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -426,6 +426,41 @@ int libxl__ao_device_check_last(libxl__gc *gc, libxl__ao_device *device, return error; } +/******************************************************************************/ + +/* Macro for defining the functions that will add a bunch of disks when + * inside an async op. + * + * This macro is added to prevent repetition of code. + */ +#define DEFINE_DEVICES_ADD(type) \ + void libxl__add_##type##s(libxl__egc *egc, libxl__ao *ao, uint32_t domid, \ + libxl_domain_config *d_config, \ + libxl__ao_device **devices, \ + libxl__device_callback *callback) \ + { \ + AO_GC; \ + GCNEW_ARRAY(*devices, d_config->num_##type##s); \ + libxl__ao_device *aodev = *devices; \ + int i; \ + \ + for (i = 0; i < d_config->num_##type##s; i++) { \ + libxl__prepare_ao_device(&aodev[i], ao, devices); \ + aodev[i].callback = callback; \ + } \ + \ + for (i = 0; i < d_config->num_##type##s; i++) { \ + libxl__device_##type##_add(egc, domid, &d_config->type##s[i], \ + &aodev[i]); \ + } \ + } + +DEFINE_DEVICES_ADD(disk) + +#undef DEFINE_DEVICES_ADD + +/******************************************************************************/ + int libxl__device_destroy(libxl__gc *gc, libxl__device *dev) { libxl_ctx *ctx = libxl__gc_owner(gc); @@ -536,6 +571,36 @@ static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds, static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev); +void libxl__initiate_device_add(libxl__egc *egc, libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); + char *be_path = libxl__device_backend_path(gc, aodev->dev); + char *state_path = libxl__sprintf(gc, "%s/state", be_path); + int rc = 0; + + if (aodev->dev->backend_kind == LIBXL__DEVICE_KIND_QDISK) { + aodev->callback(egc, aodev); + return; + } + + libxl__ev_devstate_init(&aodev->ds); + rc = libxl__ev_devstate_wait(gc, &aodev->ds, device_backend_callback, + state_path, XenbusStateInitWait, + LIBXL_INIT_TIMEOUT * 1000); + if (rc) { + LOGE(ERROR, "unable to initialize device %s", be_path); + goto out_fail; + } + + return; + +out_fail: + assert(rc); + aodev->rc = rc; + aodev->callback(egc, aodev); + return; +} + void libxl__initiate_device_remove(libxl__egc *egc, libxl__ao_device *aodev) { diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c index 99059dc..dbd8431 100644 --- a/tools/libxl/libxl_dm.c +++ b/tools/libxl/libxl_dm.c @@ -673,6 +673,13 @@ static void spawn_stubdom_pvqemu_cb(libxl__egc *egc, libxl__dm_spawn_state *stubdom_dmss, int rc); +static void spawn_stub_launch_dm(libxl__egc *egc, + libxl__stub_dm_spawn_state *sdss, int rc); + +DEFINE_DEVICES_CALLBACK(spawn_stub_disk_connected, + libxl__stub_dm_spawn_state, + devices, num_devices, spawn_stub_launch_dm) + static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc, libxl__destroy_domid_state *dis, int rc); @@ -681,8 +688,7 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss) { STATE_AO_GC(sdss->dm.spawn.ao); libxl_ctx *ctx = libxl__gc_owner(gc); - int i, num_console = STUBDOM_SPECIAL_CONSOLES, ret; - libxl__device_console *console; + int ret; libxl_device_vfb vfb; libxl_device_vkb vkb; char **args; @@ -801,22 +807,52 @@ retry_transaction: if (errno == EAGAIN) goto retry_transaction; - for (i = 0; i < dm_config->num_disks; i++) { - ret = libxl_device_disk_add(ctx, dm_domid, &dm_config->disks[i]); - if (ret) - goto out_free; - } + sdss->num_devices = dm_config->num_disks; + libxl__add_disks(egc, ao, dm_domid, dm_config, &sdss->devices, + spawn_stub_disk_connected); + + free(args); + return; + +out_free: + free(args); +out: + assert(ret); + spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, ret); +} + +static void spawn_stub_launch_dm(libxl__egc *egc, + libxl__stub_dm_spawn_state *sdss, int rc) +{ + STATE_AO_GC(sdss->dm.spawn.ao); + libxl_ctx *ctx = libxl__gc_owner(gc); + int i, num_console = STUBDOM_SPECIAL_CONSOLES, ret = 0; + libxl__device_console *console; + + /* convenience aliases */ + libxl_domain_config *const dm_config = &sdss->dm_config; + libxl_domain_config *const guest_config = sdss->dm.guest_config; + const int guest_domid = sdss->dm.guest_domid; + libxl__domain_build_state *const d_state = sdss->dm.build_state; + libxl__domain_build_state *const stubdom_state = &sdss->dm_state; + uint32_t dm_domid = sdss->pvqemu.guest_domid; + + if (rc) { + LOG(ERROR, "error connecting disk devices"); + goto out; + } + for (i = 0; i < dm_config->num_vifs; i++) { ret = libxl_device_nic_add(ctx, dm_domid, &dm_config->vifs[i]); if (ret) - goto out_free; + goto out; } ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]); if (ret) - goto out_free; + goto out; ret = libxl_device_vkb_add(ctx, dm_domid, &dm_config->vkbs[0]); if (ret) - goto out_free; + goto out; if (guest_config->b_info.u.hvm.serial) num_console++; @@ -824,7 +860,7 @@ retry_transaction: console = libxl__calloc(gc, num_console, sizeof(libxl__device_console)); if (!console) { ret = ERROR_NOMEM; - goto out_free; + goto out; } for (i = 0; i < num_console; i++) { @@ -860,7 +896,7 @@ retry_transaction: ret = libxl__device_console_add(gc, dm_domid, &console[i], i == STUBDOM_CONSOLE_LOGGING ? stubdom_state : NULL); if (ret) - goto out_free; + goto out; } sdss->pvqemu.spawn.ao = ao; @@ -871,11 +907,8 @@ retry_transaction: libxl__spawn_local_dm(egc, &sdss->pvqemu); - free(args); return; -out_free: - free(args); out: assert(ret); spawn_stubdom_pvqemu_cb(egc, &sdss->pvqemu, ret); diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 5c15ca5..6df68a6 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -70,6 +70,7 @@ #include "_libxl_types_internal.h" #include "_libxl_types_internal_json.h" +#define LIBXL_INIT_TIMEOUT 10 #define LIBXL_DESTROY_TIMEOUT 10 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10 #define LIBXL_XENCONSOLE_LIMIT 1048576 @@ -1844,6 +1845,21 @@ struct libxl__ao_device { void *base; }; +/* Internal AO operation to connect a disk device, called by + * libxl_device_disk_add and libxl__add_disks. This function calls + * libxl__initiate_device_add */ +_hidden void libxl__device_disk_add(libxl__egc *egc, uint32_t domid, + libxl_device_disk *disk, + libxl__ao_device *aodev); + +/* Arranges that dev will be added 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. + */ +_hidden void libxl__initiate_device_add(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 @@ -1936,6 +1952,36 @@ _hidden void libxl__destroy_domid(libxl__egc *egc, _hidden void libxl__devices_destroy(libxl__egc *egc, libxl__devices_remove_state *drs); +/* Helper function to add a bunch of disks. This should be used when + * the caller is inside an async op. "devices" will be prepared by this + * function, so there''s no need to call _prepare before calling this + * function. + * + * The "callback" will be called for each device, and the user is responsible + * for calling libxl__ao_device_check_last on the callback. + */ +void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid, + libxl_domain_config *d_config, + libxl__ao_device **devices, + libxl__device_callback *callback); + +/* Macro to define callbacks of libxl__add_*, checks if device is last + * and calls the function passed as a parameter to this macro with the + * following syntax; func(libxl__egc *, parent_type *, int rc) */ +#define DEFINE_DEVICES_CALLBACK(callback_name, parent_type, array_name, \ + array_size_name, func_to_call) \ + static void callback_name(libxl__egc *egc, libxl__ao_device *aodev) \ + { \ + STATE_AO_GC(aodev->ao); \ + parent_type *p = CONTAINER_OF(aodev->base, *p, array_name); \ + int rc; \ + \ + rc = libxl__ao_device_check_last(gc, aodev, p->array_name, \ + p->array_size_name); \ + if (rc > 0) return; \ + func_to_call(egc, p, rc); \ + } + /*----- device model creation -----*/ /* First layer; wraps libxl__spawn_spawn. */ @@ -1970,6 +2016,9 @@ typedef struct { libxl__domain_build_state dm_state; libxl__dm_spawn_state pvqemu; libxl__destroy_domid_state dis; + /* used to store the state of devices being connected */ + libxl__ao_device *devices; + int num_devices; } libxl__stub_dm_spawn_state; _hidden void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state*); @@ -1998,6 +2047,9 @@ struct libxl__domain_create_state { * for the non-stubdom device model. */ /* necessary if the domain creation failed and we have to destroy it */ libxl__domain_destroy_state dds; + /* used to store the state of devices being connected */ + libxl__ao_device *devices; + int num_devices; }; /* diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 3220e89..a7fa0b9 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -5343,7 +5343,7 @@ int main_blockattach(int argc, char **argv) return 0; } - if (libxl_device_disk_add(ctx, fe_domid, &disk)) { + if (libxl_device_disk_add(ctx, fe_domid, &disk, 0)) { fprintf(stderr, "libxl_device_disk_add failed.\n"); } return 0; -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 05/10] libxl: convert libxl_device_nic_add to an async operation
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 v4: * Used the macro defined in previous patch to define the generic callback to wait for nics to be connected. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/libxl/libxl.c | 39 ++++++++++++++++++++++++------ tools/libxl/libxl.h | 3 +- tools/libxl/libxl_create.c | 53 +++++++++++++++++++++++++++++++++++------ tools/libxl/libxl_device.c | 9 ++++--- tools/libxl/libxl_dm.c | 40 +++++++++++++++++++++++++++++-- tools/libxl/libxl_internal.h | 18 ++++++++++++++ tools/libxl/xl_cmdimpl.c | 2 +- 7 files changed, 139 insertions(+), 25 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index b674557..88869f6 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -2026,12 +2026,27 @@ static int libxl__device_from_nic(libxl__gc *gc, uint32_t domid, return 0; } -int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic) +int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic, + const libxl_asyncop_how *ao_how) { - GC_INIT(ctx); + AO_CREATE(ctx, domid, ao_how); + libxl__ao_device *device; + + GCNEW(device); + libxl__prepare_ao_device(device, ao, NULL); + device->callback = device_addrm_aocomplete; + libxl__device_nic_add(egc, domid, nic, device); + + return AO_INPROGRESS; +} + +void libxl__device_nic_add(libxl__egc *egc, uint32_t domid, + libxl_device_nic *nic, libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); flexarray_t *front; flexarray_t *back; - libxl__device device; + libxl__device *device; char *dompath, **l; unsigned int nb, rc; @@ -2062,7 +2077,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"); @@ -2103,6 +2119,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)); @@ -2113,18 +2132,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, &device, + libxl__device_generic_add(gc, 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__initiate_device_add(egc, aodev); + rc = 0; out_free: flexarray_free(back); flexarray_free(front); out: - GC_FREE; - return rc; + aodev->rc = rc; + if (rc) aodev->callback(egc, aodev); + return; } static void libxl__device_nic_from_xs_be(libxl__gc *gc, diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index ff0078d..51f2e60 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -666,7 +666,8 @@ char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk); int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk); /* Network Interfaces */ -int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic); +int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic, + const libxl_asyncop_how *ao_how); int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic, const libxl_asyncop_how *ao_how); diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index 2dc1cee..1dc8247 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -585,6 +585,13 @@ DEFINE_DEVICES_CALLBACK(domcreate_disk_connected, libxl__domain_create_state, devices, num_devices, domcreate_launch_dm) +static void domcreate_attach_pci(libxl__egc *egc, + libxl__domain_create_state *dcs, int rc); + +DEFINE_DEVICES_CALLBACK(domcreate_nic_connected, + libxl__domain_create_state, + devices, num_devices, domcreate_attach_pci) + static void domcreate_console_available(libxl__egc *egc, libxl__domain_create_state *dcs); @@ -752,13 +759,11 @@ static void domcreate_launch_dm(libxl__egc *egc, } for (i = 0; i < d_config->num_vifs; i++) { - ret = libxl_device_nic_add(ctx, domid, &d_config->vifs[i]); - if (ret) { - LIBXL__LOG(ctx, LIBXL__LOG_ERROR, - "cannot add nic %d to domain: %d", i, ret); - ret = ERROR_FAIL; - goto error_out; - } + /* We have to init the nic here, because we still haven''t + * called libxl_device_nic_add at this point, but qemu needs + * the nic information to be complete. + */ + libxl__device_nic_setdefault(gc, &d_config->vifs[i]); } switch (d_config->c_info.type) { case LIBXL_DOMAIN_TYPE_HVM: @@ -831,7 +836,6 @@ static void domcreate_devmodel_started(libxl__egc *egc, { libxl__domain_create_state *dcs = CONTAINER_OF(dmss, *dcs, dmss.dm); STATE_AO_GC(dmss->spawn.ao); - int i; libxl_ctx *ctx = CTX; int domid = dcs->guest_domid; @@ -851,6 +855,39 @@ static void domcreate_devmodel_started(libxl__egc *egc, } } + /* Plug nic interfaces */ + if (!ret && d_config->num_vifs > 0) { + /* Attach nics */ + dcs->num_devices = d_config->num_vifs; + libxl__add_nics(egc, ao, domid, d_config, &dcs->devices, + domcreate_nic_connected); + return; + } + + domcreate_attach_pci(egc, dcs, 0); + return; + +error_out: + assert(ret); + domcreate_complete(egc, dcs, ret); +} + +static void domcreate_attach_pci(libxl__egc *egc, + libxl__domain_create_state *dcs, int rc) +{ + STATE_AO_GC(dcs->ao); + int i, ret = 0; + libxl_ctx *ctx = CTX; + int domid = dcs->guest_domid; + + /* convenience aliases */ + libxl_domain_config *const d_config = dcs->guest_config; + + if (rc) { + LOG(ERROR, "error connecting nic devices"); + goto error_out; + } + for (i = 0; i < d_config->num_pcidevs; i++) libxl__device_pci_add(gc, domid, &d_config->pcidevs[i], 1); diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 0beb4bb..de7904a 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -433,8 +433,8 @@ int libxl__ao_device_check_last(libxl__gc *gc, libxl__ao_device *device, * * This macro is added to prevent repetition of code. */ -#define DEFINE_DEVICES_ADD(type) \ - void libxl__add_##type##s(libxl__egc *egc, libxl__ao *ao, uint32_t domid, \ +#define DEFINE_DEVICES_ADD(type, name) \ + void libxl__add_##name##s(libxl__egc *egc, libxl__ao *ao, uint32_t domid, \ libxl_domain_config *d_config, \ libxl__ao_device **devices, \ libxl__device_callback *callback) \ @@ -450,12 +450,13 @@ int libxl__ao_device_check_last(libxl__gc *gc, libxl__ao_device *device, } \ \ for (i = 0; i < d_config->num_##type##s; i++) { \ - libxl__device_##type##_add(egc, domid, &d_config->type##s[i], \ + libxl__device_##name##_add(egc, domid, &d_config->type##s[i], \ &aodev[i]); \ } \ } -DEFINE_DEVICES_ADD(disk) +DEFINE_DEVICES_ADD(disk, disk) +DEFINE_DEVICES_ADD(vif, nic) #undef DEFINE_DEVICES_ADD diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c index dbd8431..9522a9b 100644 --- a/tools/libxl/libxl_dm.c +++ b/tools/libxl/libxl_dm.c @@ -680,6 +680,14 @@ DEFINE_DEVICES_CALLBACK(spawn_stub_disk_connected, libxl__stub_dm_spawn_state, devices, num_devices, spawn_stub_launch_dm) +static void stubdom_pvqemu_cb(libxl__egc *egc, + libxl__stub_dm_spawn_state *sdss, + int rc); + +DEFINE_DEVICES_CALLBACK(stubdom_nics_connected, + libxl__stub_dm_spawn_state, + devices, num_devices, stubdom_pvqemu_cb) + static void spaw_stubdom_pvqemu_destroy_cb(libxl__egc *egc, libxl__destroy_domid_state *dis, int rc); @@ -843,9 +851,11 @@ static void spawn_stub_launch_dm(libxl__egc *egc, } for (i = 0; i < dm_config->num_vifs; i++) { - ret = libxl_device_nic_add(ctx, dm_domid, &dm_config->vifs[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->vifs[i]); } ret = libxl_device_vfb_add(ctx, dm_domid, &dm_config->vfbs[0]); if (ret) @@ -922,9 +932,33 @@ 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 (!rc && d_config->num_vifs > 0) { + sdss->num_devices = d_config->num_vifs; + libxl__add_nics(egc, ao, dm_domid, d_config, &sdss->devices, + stubdom_nics_connected); + return; + } + +out: + stubdom_pvqemu_cb(egc, sdss, rc); +} + +static void stubdom_pvqemu_cb(libxl__egc *egc, + libxl__stub_dm_spawn_state *sdss, + int rc) +{ + STATE_AO_GC(sdss->dm.spawn.ao); + uint32_t dm_domid = sdss->pvqemu.guest_domid; + + if (rc) { + LOGE(ERROR, "error connecting nics devices"); + goto out; + } + rc = libxl_domain_unpause(CTX, dm_domid); if (rc) goto out; diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 6df68a6..49085b4 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -1852,6 +1852,11 @@ _hidden void libxl__device_disk_add(libxl__egc *egc, uint32_t domid, libxl_device_disk *disk, libxl__ao_device *aodev); +/* Internal 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); + /* Arranges that dev will be added to the guest, and the * hotplug scripts will be executed (if necessary). When * this is done (or an error happens), the callback in @@ -1965,6 +1970,19 @@ void libxl__add_disks(libxl__egc *egc, libxl__ao *ao, uint32_t domid, libxl__ao_device **devices, libxl__device_callback *callback); +/* Helper function to add a bunch of nics. This should be used when + * the caller is inside an async op. "devices" will be prepared by this + * function, so there''s no need to call _prepare before calling this + * function. + * + * The "callback" will be called for each device, and the user is responsible + * for calling libxl__ao_device_check_last on the callback. + */ +void libxl__add_nics(libxl__egc *egc, libxl__ao *ao, uint32_t domid, + libxl_domain_config *d_config, + libxl__ao_device **devices, + libxl__device_callback callback); + /* Macro to define callbacks of libxl__add_*, checks if device is last * and calls the function passed as a parameter to this macro with the * following syntax; func(libxl__egc *, parent_type *, int rc) */ diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index a7fa0b9..1f9b857 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -5232,7 +5232,7 @@ int main_networkattach(int argc, char **argv) return 0; } - if (libxl_device_nic_add(ctx, domid, &nic)) { + if (libxl_device_nic_add(ctx, domid, &nic, 0)) { fprintf(stderr, "libxl_device_nic_add failed.\n"); return 1; } -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 06/10] libxl: add option to choose who executes hotplug scripts
Add and option to xl.conf file to decide if hotplug scripts are executed from the toolstack (xl) or from udev as it used to be in the past. This option is only introduced in this patch, but it has no effect since the code to call hotplug scripts from libxl is introduced in a latter patch. This choice will be saved in "libxl/disable_udev", as specified in the DISABLE_UDEV_PATH constant. Changes since v2: * Change atoi(...) to !!atoi(...) to prevent returning negative values from xenstore (which will be handled as errors). * Check for errors on the return value of libxl__hotplug_settings. Changes since v1: * Used an auxiliary function to check for the current setting. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- docs/man/xl.conf.pod.5 | 8 ++++++++ tools/examples/xl.conf | 5 +++++ tools/libxl/libxl_create.c | 36 +++++++++++++++++++++++++++++++++++- 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 + 9 files changed, 77 insertions(+), 1 deletions(-) diff --git a/docs/man/xl.conf.pod.5 b/docs/man/xl.conf.pod.5 index 8bd45ea..72825a0 100644 --- a/docs/man/xl.conf.pod.5 +++ b/docs/man/xl.conf.pod.5 @@ -55,6 +55,14 @@ default. Default: C<1> +=item B<run_hotplug_scripts=BOOLEAN> + +If disabled hotplug scripts will be called from udev, as it used to +be in the previous releases. With the default option, hotplug scripts +will be launched by xl directly. + +Default: C<1> + =item B<lockfile="PATH"> Sets the path to the lock file used by xl to serialise certain diff --git a/tools/examples/xl.conf b/tools/examples/xl.conf index 56d3b3b..75b00e0 100644 --- a/tools/examples/xl.conf +++ b/tools/examples/xl.conf @@ -12,3 +12,8 @@ # default output format used by "xl list -l" #output_format="json" + +# default option to run hotplug scripts from xl +# if disabled the old behaviour will be used, and hotplug scripts will be +# launched by udev. +#run_hotplug_scripts=1 diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index 1dc8247..317654f 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -400,7 +400,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]; @@ -521,6 +521,40 @@ retry_transaction: libxl__sprintf(gc, "%s/hvmloader/generation-id-address", dom_path), rwperm, ARRAY_SIZE(rwperm)); + if (libxl_list_vm(ctx, &nb_vm) < 0) { + LOG(ERROR, "cannot get number of running guests"); + rc = ERROR_FAIL; + goto out; + } + + int hotplug_setting = libxl__hotplug_settings(gc, t); + if (hotplug_setting < 0) { + LOG(ERROR, "unable to get current hotplug scripts execution setting"); + rc = ERROR_FAIL; + goto out; + } + if (libxl_defbool_val(info->run_hotplug_scripts) != hotplug_setting && + (nb_vm - 1)) { + LOG(ERROR, "cannot change hotplug execution option once set, " + "please shutdown all guests before changing it"); + rc = ERROR_FAIL; + goto out; + } + + if (libxl_defbool_val(info->run_hotplug_scripts)) { + rc = libxl__xs_write(gc, t, DISABLE_UDEV_PATH, "1"); + if (rc) { + LOGE(ERROR, "unable to write %s = 1", DISABLE_UDEV_PATH); + goto out; + } + } else { + rc = xs_rm(ctx->xsh, t, DISABLE_UDEV_PATH); + if (rc) { + LOGE(ERROR, "unable to delete %s", DISABLE_UDEV_PATH); + goto out; + } + } + xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string)); xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/name", vm_path), info->name, strlen(info->name)); diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 8139520..bc56f21 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -346,6 +346,25 @@ libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc, return value; } +int libxl__hotplug_settings(libxl__gc *gc, xs_transaction_t t) +{ + int rc = 0; + char *val; + + val = libxl__xs_read(gc, t, DISABLE_UDEV_PATH); + if (!val && errno != ENOENT) { + LOGE(ERROR, "cannot read %s from xenstore", DISABLE_UDEV_PATH); + rc = ERROR_FAIL; + goto out; + } + if (!val) val = "0"; + + rc = !!atoi(val); + +out: + return rc; +} + /* * Local variables: * mode: C diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 49085b4..5a2a87a 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -86,6 +86,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])) @@ -1408,6 +1409,8 @@ _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc, const char *s); _hidden libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); +/* Check how executes hotplug script currently */ +int libxl__hotplug_settings(libxl__gc *gc, xs_transaction_t t); /* * Calling context and GC for event-generating functions: diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl index 5b81ee9..3c8636c 100644 --- a/tools/libxl/libxl_types.idl +++ b/tools/libxl/libxl_types.idl @@ -218,6 +218,7 @@ libxl_domain_create_info = Struct("domain_create_info",[ ("xsdata", libxl_key_value_list), ("platformdata", libxl_key_value_list), ("poolid", uint32), + ("run_hotplug_scripts",libxl_defbool), ], dir=DIR_IN) MemKB = UInt(64, init_val = "LIBXL_MEMKB_DEFAULT") diff --git a/tools/libxl/xl.c b/tools/libxl/xl.c index 69f8737..02a84eb 100644 --- a/tools/libxl/xl.c +++ b/tools/libxl/xl.c @@ -38,6 +38,7 @@ xentoollog_logger_stdiostream *logger; int dryrun_only; int force_execution; int autoballoon = 1; +libxl_defbool run_hotplug_scripts; char *lockfile; char *default_vifscript = NULL; char *default_bridge = NULL; @@ -69,6 +70,9 @@ static void parse_global_config(const char *configfile, if (!xlu_cfg_get_long (config, "autoballoon", &l, 0)) autoballoon = l; + libxl_defbool_setdefault(&run_hotplug_scripts, true); + xlu_cfg_get_defbool(config, "run_hotplug_scripts", &run_hotplug_scripts, 0); + if (!xlu_cfg_get_string (config, "lockfile", &buf, 0)) lockfile = strdup(buf); else { diff --git a/tools/libxl/xl.h b/tools/libxl/xl.h index 2af9428..59f931f 100644 --- a/tools/libxl/xl.h +++ b/tools/libxl/xl.h @@ -139,6 +139,7 @@ int xl_child_pid(xlchildnum); /* returns 0 if child struct is not in use */ /* global options */ extern int autoballoon; +extern libxl_defbool run_hotplug_scripts; extern int dryrun_only; extern char *lockfile; extern char *default_vifscript; diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 1f9b857..b8e5773 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -594,6 +594,7 @@ static void parse_config_data(const char *config_source, } } + c_info->run_hotplug_scripts = run_hotplug_scripts; c_info->type = LIBXL_DOMAIN_TYPE_PV; if (!xlu_cfg_get_string (config, "builder", &buf, 0) && !strncmp(buf, "hvm", strlen(buf))) -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 07/10] libxl: set nic type to VIF by default
Set the default value for nic interfaces to VIF, since it used to be IOEMU, even for PV guests. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/libxl/libxl.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 88869f6..ef99fa6 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -2008,7 +2008,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; return 0; } -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 08/10] libxl: call hotplug scripts for disk devices from libxl
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 v4: * Init args and env to NULL. * Correctly propagate errors from libxl__get_hotplug_script_info. * Replaced if in libxl__ev_child_inuse with asserts. * Renamed fork_cb to child_death_cb. * Move deregistration of device_hotplug_timeout_cb to the top of the function. * Removed "pid" field from libxl__ao_device struct. * Moved arraysize declaration. * Fixed diff complain about no newline at the end of libxl_netbsd.c. Changes since v2: * Added array size check with assert. * Added NetBSD code (so compilation is not broken). * Removed a check for null in device_hotplug_timeout_cb. Changes since v1: * Moved all the event related code that was inside libxl_linux.c into libxl_device.c, so the flow of the device addition/removal event is all in the same file. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/hotplug/Linux/xen-backend.rules | 6 +- tools/hotplug/Linux/xen-hotplug-common.sh | 6 ++ tools/libxl/libxl.c | 10 +++ tools/libxl/libxl_device.c | 115 +++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 19 +++++ tools/libxl/libxl_linux.c | 100 +++++++++++++++++++++++++ tools/libxl/libxl_netbsd.c | 9 ++ 7 files changed, 262 insertions(+), 3 deletions(-) diff --git a/tools/hotplug/Linux/xen-backend.rules b/tools/hotplug/Linux/xen-backend.rules index 405387f..d55ff11 100644 --- a/tools/hotplug/Linux/xen-backend.rules +++ b/tools/hotplug/Linux/xen-backend.rules @@ -1,11 +1,11 @@ -SUBSYSTEM=="xen-backend", KERNEL=="tap*", RUN+="/etc/xen/scripts/blktap $env{ACTION}" -SUBSYSTEM=="xen-backend", KERNEL=="vbd*", RUN+="/etc/xen/scripts/block $env{ACTION}" +SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/blktap $env{ACTION}" +SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/block $env{ACTION}" SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}" SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="/etc/xen/scripts/vif2 $env{ACTION}" SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif" SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif" SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}" -SUBSYSTEM=="xen-backend", ACTION=="remove", RUN+="/etc/xen/scripts/xen-hotplug-cleanup" +SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/xen-hotplug-cleanup" KERNEL=="evtchn", NAME="xen/%k" SUBSYSTEM=="xen", KERNEL=="blktap[0-9]*", NAME="xen/%k", MODE="0600" SUBSYSTEM=="blktap2", KERNEL=="blktap[0-9]*", NAME="xen/blktap-2/%k", MODE="0600" diff --git a/tools/hotplug/Linux/xen-hotplug-common.sh b/tools/hotplug/Linux/xen-hotplug-common.sh index 8f6557d..4a7bc73 100644 --- a/tools/hotplug/Linux/xen-hotplug-common.sh +++ b/tools/hotplug/Linux/xen-hotplug-common.sh @@ -15,6 +15,12 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # +# Hack to prevent the execution of hotplug scripts from udev if the domain +# has been launched from libxl +if [ -n "${UDEV_CALL}" ] && \ + xenstore-read "libxl/disable_udev" >/dev/null 2>&1; then + exit 0 +fi dir=$(dirname "$0") . "$dir/hotplugpath.sh" diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index ef99fa6..f7a137f 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -1607,6 +1607,11 @@ void libxl__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: @@ -1622,6 +1627,11 @@ void libxl__device_disk_add(libxl__egc *egc, uint32_t domid, libxl__device_disk_string_of_format(disk->format), disk->pdev_path)); + flexarray_append(back, "script"); + flexarray_append(back, GCSPRINTF("%s/%s", + libxl__xen_script_dir_path(), + "blktap")); + /* now create a phy device to export the device to the guest */ goto do_backend_phy; case LIBXL_DISK_BACKEND_QDISK: diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index de7904a..7db379a 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -572,6 +572,15 @@ static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds, static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev); +static void device_hotplug(libxl__egc *egc, libxl__ao_device *aodev); + +static void device_hotplug_child_death_cb(libxl__egc *egc, + libxl__ev_child *child, + pid_t pid, int status); + +static void device_hotplug_timeout_cb(libxl__egc *egc, libxl__ev_time *ev, + const struct timeval *requested_abs); + void libxl__initiate_device_add(libxl__egc *egc, libxl__ao_device *aodev) { STATE_AO_GC(aodev->ao); @@ -679,6 +688,9 @@ static void device_backend_callback(libxl__egc *egc, libxl__ev_devstate *ds, goto out; } + device_hotplug(egc, aodev); + return; + out: aodev->rc = rc; aodev->callback(egc, aodev); @@ -691,6 +703,109 @@ static void device_backend_cleanup(libxl__gc *gc, libxl__ao_device *aodev) libxl__ev_devstate_cancel(gc, &aodev->ds); } +static void device_hotplug(libxl__egc *egc, libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); + char *be_path = libxl__device_backend_path(gc, aodev->dev); + char **args = NULL, **env = NULL; + int rc = 0; + int hotplug; + pid_t pid; + + /* Check if we have to execute hotplug scripts for this device + * and return the necessary args/env vars for execution */ + hotplug = libxl__get_hotplug_script_info(gc, aodev->dev, &args, &env, + aodev->action); + switch (hotplug) { + case 0: + /* no hotplug script to execute */ + goto out; + case 1: + /* execute hotplug script */ + break; + default: + /* everything else is an error */ + LOG(ERROR, "unable to get args/env to execute hotplug script for " + "device %s", libxl__device_backend_path(gc, aodev->dev)); + rc = hotplug; + goto out; + } + + /* Set hotplug timeout */ + libxl__ev_time_init(&aodev->ev); + rc = libxl__ev_time_register_rel(gc, &aodev->ev, device_hotplug_timeout_cb, + LIBXL_HOTPLUG_TIMEOUT * 1000); + if (rc) { + LOG(ERROR, "unable to register timeout for hotplug device %s", be_path); + goto out; + } + + aodev->what = GCSPRINTF("%s %s", args[0], args[1]); + LOG(DEBUG, "calling hotplug script: %s %s", args[0], args[1]); + libxl__ev_child_init(&aodev->child); + + /* fork and execute hotplug script */ + pid = libxl__ev_child_fork(gc, &aodev->child, device_hotplug_child_death_cb); + if (pid == -1) { + LOG(ERROR, "unable to fork"); + rc = ERROR_FAIL; + goto out; + } + + if (!pid) { + /* child */ + libxl__exec(gc, -1, -1, -1, args[0], args, env); + /* notreached */ + abort(); + } + + assert(libxl__ev_child_inuse(&aodev->child)); + + return; + +out: + libxl__ev_time_deregister(gc, &aodev->ev); + aodev->rc = rc; + aodev->callback(egc, aodev); + return; +} + +static void device_hotplug_child_death_cb(libxl__egc *egc, + libxl__ev_child *child, + pid_t pid, int status) +{ + libxl__ao_device *aodev = CONTAINER_OF(child, *aodev, child); + STATE_AO_GC(aodev->ao); + + libxl__ev_time_deregister(gc, &aodev->ev); + + if (status) { + libxl_report_child_exitstatus(CTX, aodev->rc ? LIBXL__LOG_ERROR + : LIBXL__LOG_WARNING, + aodev->what, pid, status); + aodev->rc = ERROR_FAIL; + } + aodev->callback(egc, aodev); +} + +static void device_hotplug_timeout_cb(libxl__egc *egc, libxl__ev_time *ev, + const struct timeval *requested_abs) +{ + libxl__ao_device *aodev = CONTAINER_OF(ev, *aodev, ev); + STATE_AO_GC(aodev->ao); + + libxl__ev_time_deregister(gc, &aodev->ev); + + assert(libxl__ev_child_inuse(&aodev->child)); + LOG(DEBUG, "killing hotplug script %s because of timeout", aodev->what); + if (kill(aodev->child.pid, SIGKILL)) { + LOGEV(ERROR, errno, "unable to kill hotplug script %s [%ld]", + aodev->what, (unsigned long)aodev->child.pid); + } + + return; +} + static void device_remove_callback(libxl__egc *egc, libxl__ao_device *aodev) { STATE_AO_GC(aodev->ao); diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 5a2a87a..9dd2404 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -72,6 +72,7 @@ #define LIBXL_INIT_TIMEOUT 10 #define LIBXL_DESTROY_TIMEOUT 10 +#define LIBXL_HOTPLUG_TIMEOUT 10 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10 #define LIBXL_XENCONSOLE_LIMIT 1048576 #define LIBXL_XENCONSOLE_PROTOCOL "vt100" @@ -1846,6 +1847,10 @@ struct libxl__ao_device { int rc; libxl__ev_devstate ds; void *base; + /* device hotplug execution */ + char *what; + libxl__ev_time ev; + libxl__ev_child child; }; /* Internal AO operation to connect a disk device, called by @@ -1880,6 +1885,20 @@ _hidden void libxl__initiate_device_add(libxl__egc*, libxl__ao_device *aodev); _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); + /*----- Domain destruction -----*/ /* Domain destruction has been split into two functions: diff --git a/tools/libxl/libxl_linux.c b/tools/libxl/libxl_linux.c index 925248b..60a966f 100644 --- a/tools/libxl/libxl_linux.c +++ b/tools/libxl/libxl_linux.c @@ -25,3 +25,103 @@ int libxl__try_phy_backend(mode_t st_mode) return 1; } + +/* Hotplug scripts helpers */ + +static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) +{ + char *be_path = libxl__device_backend_path(gc, dev); + char *script; + const char *type = libxl__device_kind_to_string(dev->backend_kind); + char **env; + int nr = 0; + + script = libxl__xs_read(gc, XBT_NULL, + GCSPRINTF("%s/%s", be_path, "script")); + if (!script) { + LOGEV(ERROR, errno, "unable to read script from %s", be_path); + return NULL; + } + + const int arraysize = 9; + GCNEW_ARRAY(env, arraysize); + env[nr++] = "script"; + env[nr++] = script; + env[nr++] = "XENBUS_TYPE"; + env[nr++] = libxl__strdup(gc, type); + env[nr++] = "XENBUS_PATH"; + env[nr++] = GCSPRINTF("backend/%s/%u/%d", type, dev->domid, dev->devid); + env[nr++] = "XENBUS_BASE_PATH"; + env[nr++] = "backend"; + env[nr++] = NULL; + assert(nr == arraysize); + + return env; +} + +/* Hotplug scripts caller functions */ + +static int libxl__hotplug_disk(libxl__gc *gc, libxl__device *dev, + char ***args, char ***env, + libxl__device_action action) +{ + char *be_path = libxl__device_backend_path(gc, dev); + char *script; + int nr = 0, rc = 0; + + script = libxl__xs_read(gc, XBT_NULL, + GCSPRINTF("%s/%s", be_path, "script")); + if (!script) { + LOGEV(ERROR, errno, "unable to read script from %s", be_path); + rc = ERROR_FAIL; + goto error; + } + + *env = get_hotplug_env(gc, dev); + if (!*env) { + rc = ERROR_FAIL; + goto error; + } + + const int arraysize = 3; + GCNEW_ARRAY(*args, arraysize); + (*args)[nr++] = script; + (*args)[nr++] = action == DEVICE_CONNECT ? "add" : "remove"; + (*args)[nr++] = NULL; + assert(nr == arraysize); + + rc = 0; + +error: + return rc; +} + +int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev, + char ***args, char ***env, + libxl__device_action action) +{ + char *disable_udev = libxl__xs_read(gc, XBT_NULL, DISABLE_UDEV_PATH); + int rc; + + /* Check if we have to run hotplug scripts */ + if (!disable_udev) { + rc = 0; + goto out; + } + + switch (dev->backend_kind) { + case LIBXL__DEVICE_KIND_VBD: + rc = libxl__hotplug_disk(gc, dev, args, env, action); + if (!rc) rc = 1; + break; + default: + /* If no need to execute any hotplug scripts, + * call the callback manually + */ + rc = 0; + break; + } + +out: + return rc; +} diff --git a/tools/libxl/libxl_netbsd.c b/tools/libxl/libxl_netbsd.c index 9e0ed6d..d41a046 100644 --- a/tools/libxl/libxl_netbsd.c +++ b/tools/libxl/libxl_netbsd.c @@ -24,3 +24,12 @@ int libxl__try_phy_backend(mode_t st_mode) return 0; } + +/* Hotplug scripts caller functions */ + +int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev, + char ***args, char ***env, + libxl__device_action action) +{ + return 0; +} -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl
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 v4: * Add num_exec to NetBSD dummy function. * Better comment in the prototype of libxl__get_hotplug_script_info. * Remove nasty use of goto. Changes since v2: * Change libxl__nic_type to return the value in a parameter passed by the caller. * Rename vif_execute to num_exec, to represent the number of times hotplug scripts have been called for that device. Changes since v1: * Move event code to libxl_device.c (as in previous patch). Cc: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/hotplug/Linux/xen-backend.rules | 6 +- tools/libxl/libxl_device.c | 46 +++++++++++++++++- tools/libxl/libxl_internal.h | 14 +++++- tools/libxl/libxl_linux.c | 84 +++++++++++++++++++++++++++++++- tools/libxl/libxl_netbsd.c | 3 +- 5 files changed, 144 insertions(+), 9 deletions(-) diff --git a/tools/hotplug/Linux/xen-backend.rules b/tools/hotplug/Linux/xen-backend.rules index d55ff11..c591a3f 100644 --- a/tools/hotplug/Linux/xen-backend.rules +++ b/tools/hotplug/Linux/xen-backend.rules @@ -2,8 +2,8 @@ SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scr SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/block $env{ACTION}" SUBSYSTEM=="xen-backend", KERNEL=="vtpm*", RUN+="/etc/xen/scripts/vtpm $env{ACTION}" SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="/etc/xen/scripts/vif2 $env{ACTION}" -SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif" -SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif" +SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif" +SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif" SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}" SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/xen-hotplug-cleanup" KERNEL=="evtchn", NAME="xen/%k" @@ -13,4 +13,4 @@ KERNEL=="blktap-control", NAME="xen/blktap-2/control", MODE="0600" KERNEL=="gntdev", NAME="xen/%k", MODE="0600" KERNEL=="pci_iomul", NAME="xen/%k", MODE="0600" KERNEL=="tapdev[a-z]*", NAME="xen/blktap-2/tapdev%m", MODE="0600" -SUBSYSTEM=="net", KERNEL=="vif*-emu", ACTION=="add", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap" +SUBSYSTEM=="net", KERNEL=="vif*-emu", ACTION=="add", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap" diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 7db379a..841f9ac 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -100,6 +100,29 @@ out: return numdevs; } +int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype) +{ + char *snictype, *be_path; + int rc = 0; + + be_path = libxl__device_backend_path(gc, dev); + snictype = libxl__xs_read(gc, XBT_NULL, + GCSPRINTF("%s/%s", be_path, "type")); + if (!snictype) { + LOGE(ERROR, "unable to read nictype from %s", be_path); + rc = ERROR_FAIL; + goto out; + } + rc = libxl_nic_type_from_string(snictype, nictype); + if (rc) { + LOGE(ERROR, "unable to parse nictype from %s", be_path); + goto out; + } + +out: + return rc; +} + int libxl__device_generic_add(libxl__gc *gc, libxl__device *device, char **bents, char **fents) { @@ -715,7 +738,8 @@ static void device_hotplug(libxl__egc *egc, libxl__ao_device *aodev) /* Check if we have to execute hotplug scripts for this device * and return the necessary args/env vars for execution */ hotplug = libxl__get_hotplug_script_info(gc, aodev->dev, &args, &env, - aodev->action); + aodev->action, + aodev->num_exec); switch (hotplug) { case 0: /* no hotplug script to execute */ @@ -776,6 +800,8 @@ static void device_hotplug_child_death_cb(libxl__egc *egc, { libxl__ao_device *aodev = CONTAINER_OF(child, *aodev, child); STATE_AO_GC(aodev->ao); + libxl_nic_type nictype; + int rc; libxl__ev_time_deregister(gc, &aodev->ev); @@ -784,7 +810,25 @@ static void device_hotplug_child_death_cb(libxl__egc *egc, : LIBXL__LOG_WARNING, aodev->what, pid, status); aodev->rc = ERROR_FAIL; + goto out; } + + if (aodev->dev->backend_kind == LIBXL__DEVICE_KIND_VIF && + aodev->num_exec == 0) { + rc = libxl__nic_type(gc, aodev->dev, &nictype); + if (rc) { + LOG(ERROR, "unable to get type of nic device"); + aodev->rc = rc; + goto out; + } + if (nictype == LIBXL_NIC_TYPE_IOEMU) { + aodev->num_exec++; + device_hotplug(egc, aodev); + return; + } + } + +out: aodev->callback(egc, aodev); } diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 9dd2404..b938756 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -821,6 +821,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: @@ -1849,6 +1851,7 @@ struct libxl__ao_device { void *base; /* device hotplug execution */ char *what; + int num_exec; libxl__ev_time ev; libxl__ev_child child; }; @@ -1894,10 +1897,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. This is currently used for + * IOEMU nic interfaces on Linux, since we need to call hotplug scripts twice + * for the same device, the first one to add the vif interface, and the second + * time to add the tap interface, so: + * num_exec == 0: execute hotplug for vif interface. + * num_exec == 1: execute hotplug for the associated tap interface. */ _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); /*----- Domain destruction -----*/ diff --git a/tools/libxl/libxl_linux.c b/tools/libxl/libxl_linux.c index 60a966f..3cd8f40 100644 --- a/tools/libxl/libxl_linux.c +++ b/tools/libxl/libxl_linux.c @@ -35,6 +35,7 @@ static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) const char *type = libxl__device_kind_to_string(dev->backend_kind); char **env; int nr = 0; + libxl_nic_type nictype; script = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/%s", be_path, "script")); @@ -43,7 +44,7 @@ static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) return NULL; } - const int arraysize = 9; + const int arraysize = 13; GCNEW_ARRAY(env, arraysize); env[nr++] = "script"; env[nr++] = script; @@ -53,14 +54,86 @@ static char **get_hotplug_env(libxl__gc *gc, libxl__device *dev) env[nr++] = GCSPRINTF("backend/%s/%u/%d", type, dev->domid, dev->devid); env[nr++] = "XENBUS_BASE_PATH"; env[nr++] = "backend"; + if (dev->backend_kind == LIBXL__DEVICE_KIND_VIF) { + if (libxl__nic_type(gc, dev, &nictype)) { + LOG(ERROR, "unable to get nictype"); + return NULL; + } + switch (nictype) { + case LIBXL_NIC_TYPE_IOEMU: + env[nr++] = "INTERFACE"; + env[nr++] = libxl__strdup(gc, libxl__device_nic_devname(gc, + dev->domid, dev->devid, + LIBXL_NIC_TYPE_IOEMU)); + case LIBXL_NIC_TYPE_VIF: + env[nr++] = "vif"; + env[nr++] = libxl__strdup(gc, libxl__device_nic_devname(gc, + dev->domid, dev->devid, + LIBXL_NIC_TYPE_VIF)); + break; + default: + return NULL; + } + } + env[nr++] = NULL; - assert(nr == arraysize); + assert(nr <= arraysize); return env; } /* Hotplug scripts caller functions */ +static int libxl__hotplug_nic(libxl__gc *gc, libxl__device *dev, + char ***args, char ***env, + libxl__device_action action, int num_exec) +{ + char *be_path = libxl__device_backend_path(gc, dev); + char *script; + int nr = 0, rc = 0; + libxl_nic_type nictype; + + script = libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/%s", be_path, + "script")); + if (!script) { + LOGE(ERROR, "unable to read script from %s", be_path); + rc = ERROR_FAIL; + goto out; + } + + rc = libxl__nic_type(gc, dev, &nictype); + if (rc) { + LOG(ERROR, "error when fetching nic type"); + rc = ERROR_FAIL; + goto out; + } + + *env = get_hotplug_env(gc, dev); + if (!env) { + rc = ERROR_FAIL; + goto out; + } + + const int arraysize = 4; + GCNEW_ARRAY(*args, arraysize); + (*args)[nr++] = script; + + if (nictype == LIBXL_NIC_TYPE_IOEMU && num_exec) { + (*args)[nr++] = action == DEVICE_CONNECT ? "add" : "remove"; + (*args)[nr++] = libxl__strdup(gc, "type_if=tap"); + (*args)[nr++] = NULL; + } else { + (*args)[nr++] = action == DEVICE_CONNECT ? "online" : "offline"; + (*args)[nr++] = libxl__strdup(gc, "type_if=vif"); + (*args)[nr++] = NULL; + } + assert(nr == arraysize); + rc = 0; + +out: + return rc; +} + static int libxl__hotplug_disk(libxl__gc *gc, libxl__device *dev, char ***args, char ***env, libxl__device_action action) @@ -98,7 +171,8 @@ error: int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev, char ***args, char ***env, - libxl__device_action action) + libxl__device_action action, + int num_exec) { char *disable_udev = libxl__xs_read(gc, XBT_NULL, DISABLE_UDEV_PATH); int rc; @@ -114,6 +188,10 @@ int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev, rc = libxl__hotplug_disk(gc, dev, args, env, action); if (!rc) rc = 1; break; + case LIBXL__DEVICE_KIND_VIF: + rc = libxl__hotplug_nic(gc, dev, args, env, action, num_exec); + if (!rc) rc = 1; + break; default: /* If no need to execute any hotplug scripts, * call the callback manually diff --git a/tools/libxl/libxl_netbsd.c b/tools/libxl/libxl_netbsd.c index d41a046..702eb1e 100644 --- a/tools/libxl/libxl_netbsd.c +++ b/tools/libxl/libxl_netbsd.c @@ -29,7 +29,8 @@ int libxl__try_phy_backend(mode_t st_mode) int libxl__get_hotplug_script_info(libxl__gc *gc, libxl__device *dev, char ***args, char ***env, - libxl__device_action action) + libxl__device_action action, + int num_exec) { return 0; } -- 1.7.7.5 (Apple Git-26)
Roger Pau Monne
2012-May-30 13:07 UTC
[PATCH v5 10/10] libxl: use libxl__xs_path_cleanup on device_destroy
Since the hotplug script that was in charge of cleaning the backend is no longer launched, we need to clean the backend by ourselves, so use libxl__xs_path_cleanup instead of xs_rm. Changes sinve v2: * Changed the goto construction to a do-while loop. Cc: Ian Jackson <ian.jackson@eu.citrix.com> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com> Signed-off-by: Roger Pau Monne <roger.pau@citrix.com> --- tools/libxl/libxl_device.c | 18 ++++++++++++++---- 1 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 841f9ac..77c85b9 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -487,16 +487,26 @@ DEFINE_DEVICES_ADD(vif, nic) int libxl__device_destroy(libxl__gc *gc, libxl__device *dev) { - libxl_ctx *ctx = libxl__gc_owner(gc); char *be_path = libxl__device_backend_path(gc, dev); char *fe_path = libxl__device_frontend_path(gc, dev); + xs_transaction_t t = 0; + int rc = 0; - xs_rm(ctx->xsh, XBT_NULL, be_path); - xs_rm(ctx->xsh, XBT_NULL, fe_path); + do { + t = xs_transaction_start(CTX->xsh); + libxl__xs_path_cleanup(gc, t, fe_path); + libxl__xs_path_cleanup(gc, t, be_path); + rc = !xs_transaction_end(CTX->xsh, t, 0); + } while (rc && errno == EAGAIN); + if (rc) { + LOGE(ERROR, "unable to finish transaction"); + goto out; + } libxl__device_destroy_tapdisk(gc, be_path); - return 0; +out: + return rc; } /* Callback for device destruction */ -- 1.7.7.5 (Apple Git-26)
Ian Jackson
2012-Jun-07 10:53 UTC
Re: [PATCH v5 01/10] libxl: change libxl__ao_device_remove to libxl__ao_device
Roger Pau Monne writes ("[PATCH v5 01/10] libxl: change libxl__ao_device_remove to libxl__ao_device"):> Introduce a new structure to track state of device backends, that will > be used in following patches on this series....> +_hidden void libxl__prepare_ao_device(libxl__ao_device *aodev, libxl__ao *ao, > + libxl__ao_device **base);The doc comment doesn''t seem to explain base. What is it used for ? I see only this:> +void libxl__prepare_ao_device(libxl__ao_device *aodev, libxl__ao *ao, > + libxl__ao_device **base)...> + aodev->base = base;but:> +struct libxl__ao_device {...> + /* private for implementation */...> + void *base; > +};Why is it void* here when it''s only ever set to a libxl__ao_device** ?> +void libxl__initiate_device_remove(libxl__egc *egc, > + libxl__ao_device *aodev)...> - libxl__ev_devstate_init(&aorm->ds); > + libxl__ev_devstate_init(&aodev->ds); > > - rc = libxl__ev_devstate_wait(gc, &aorm->ds, device_remove_callback, > + rc = libxl__ev_devstate_wait(gc, &aodev->ds, device_backend_callback, > state_path, XenbusStateClosed, > LIBXL_DESTROY_TIMEOUT * 1000);libxl__ev_devstate_init is not needed before _wait. (Admittedly that''s there in the code before you touched it.) In general foo_init is not needed before foo_do_the_thing_and_call_me_back. Given the use of `backend'' in function names to refer to what is manipulated with aodev->ds, perhaps aodev->ds should be called aodev->backend_ds ? I was briefly confused about whether `device_backend_cleanup'' was a general cleanup function for a libxl__ao_device (which it isn''t). And there is of course a frontend state too, which we don''t explicitly deal with here. How do the frontend and backend directories get removed from xenstore ? (I have a feeling I have asked this before but I don''t remember the answer. Perhaps it could be a comment in the code in some appropriate place?)> +/* This functions sets the necessary libxl__ao_device struct values to use > + * safely inside functions. It marks the operation as "active"This `active'' business is not used in this patch. The variable and its initialisation and commentary should be introduced in the same patch as its use. So either the ancillary functions which use `active'' need to be brought forward and documented here, or the introduction of `active'' should be postponed.> +struct libxl__ao_device { > + /* filled in by user */ > + libxl__ao *ao; > + /* action being performed */ > + libxl__device_action action; > + libxl__device *dev; > + int force; > + libxl__device_callback *callback; > + /* private for implementation */ > + int active;... This is a bit confusing. I would remove the comment `/* action being performed */'' because it seems to refer to all the following variables (and to disapply `/* filled in by user */'' which does in fact apply). And the only thing it seems to be actually trying to document is that the variable `action'' is the action, which is a bit pointless. Thanks, Ian.
Ian Jackson
2012-Jun-07 11:38 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Roger Pau Monne writes ("[PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"):> This patch converts libxl_device_disk_add to an ao operation thatIn the subject line `asyn'' should be `async''.> @@ -1859,11 +1883,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);Is there going to be a later patch in a future version of the series that fixes these ?> +/* Macro to define callbacks of libxl__add_*, checks if device is last > + * and calls the function passed as a parameter to this macro with the > + * following syntax; func(libxl__egc *, parent_type *, int rc) */ > +#define DEFINE_DEVICES_CALLBACK(callback_name, parent_type, array_name, \ > + array_size_name, func_to_call) \ > + static void callback_name(libxl__egc *egc, libxl__ao_device *aodev) \ > + { \ > + STATE_AO_GC(aodev->ao); \ > + parent_type *p = CONTAINER_OF(aodev->base, *p, array_name); \ > + int rc; \ > + \ > + rc = libxl__ao_device_check_last(gc, aodev, p->array_name, \ > + p->array_size_name); \ > + if (rc > 0) return; \ > + func_to_call(egc, p, rc); \ > + }This is acceptable IMO and better than the repetition. However the use of a macro for this, so far from the invocation sites, is indeed less than perfect. Looking at the content of this macro, the only thing it needs from the parent is array_name and array_size_name. If you invented typedef struct libxl__ao_devices libxl__ao_devices; struct libxl__ao_devices { libxl__ao_device *array; int size; void (*callback)(libxl__egc*, libxl__ao_devices*, int rc); }; Then aodev->base could be of type libxl__ao_devices* and the macro above could be a function. And indeed it would have type suitable for simply filling into aodev->callback. Would that work ? Doing this would also perhaps mean DEFINE_DEVICES_ADD''s generated libxl__add_FOOs functions could become one single function. AFAICT the only macroish things it does are: (1) accessing d_config->num_FOOs, which could be passed in as a parameter; (2) passing d_config->FOOs[i] to libxl__device_FOO_add, which could be dealt with by making libxl__device_FOO_add take a void* for the config instead, and passing the start of the d_config->FOOs array, plus the element size, and the add function, as arguments. Sorry to mess you about. Whether you retain this as a macro or make it a function,> diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c > index 2dc1cee..1dc8247 100644 > --- a/tools/libxl/libxl_create.c > +++ b/tools/libxl/libxl_create.c > @@ -585,6 +585,13 @@ DEFINE_DEVICES_CALLBACK(domcreate_disk_connected, > libxl__domain_create_state, > devices, num_devices, domcreate_launch_dm) > > +static void domcreate_attach_pci(libxl__egc *egc, > + libxl__domain_create_state *dcs, int rc); > + > +DEFINE_DEVICES_CALLBACK(domcreate_nic_connected, > + libxl__domain_create_state, > + devices, num_devices, domcreate_attach_pci) > + > static void domcreate_console_available(libxl__egc *egc, > libxl__domain_create_state *dcs); >These seem not to be in linear execution order ? These kind of arrangements with a chain of callbacks should really have both the declarations and the definitions in linear order. Sadly that means writing out the declarations of domcreate_nic_connected etc. here in the declarations section but I think that''s a price worth paying. (This is something you''d have to do anyway if you replace the macro with a function as I suggest.) Ian.
Ian Jackson
2012-Jun-07 14:20 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Roger Pau Monne writes ("[PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"):> 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 latter patches that will launch hotplug scripts after > we reached the desired xenbus state.Just spotted:> +void libxl__initiate_device_add(libxl__egc *egc, libxl__ao_device *aodev) > +{...> + libxl__ev_devstate_init(&aodev->ds); > + rc = libxl__ev_devstate_wait(gc, &aodev->ds, device_backend_callback, > + state_path, XenbusStateInitWait, > + LIBXL_INIT_TIMEOUT * 1000);Another unneeded _init. I won''t comment on any more of these I find. Ian.
Ian Jackson
2012-Jun-07 14:25 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Roger Pau Monne writes ("[PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"):> 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 latter patches that will launch hotplug scripts after > we reached the desired xenbus state.And:> +/* Internal AO operation to connect a disk device, called by > + * libxl_device_disk_add and libxl__add_disks. This function calls > + * libxl__initiate_device_add */ > +_hidden void libxl__device_disk_add(libxl__egc *egc, uint32_t domid, > + libxl_device_disk *disk, > + libxl__ao_device *aodev); > + > +/* Arranges that dev will be added 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. > + */ > +_hidden void libxl__initiate_device_add(libxl__egc*, libxl__ao_device *aodev);I don''t think these comments are coherent. I think a function which "Arranges that dev will be added to the guest" is one which does everything necessary - ie, the outermost entrypoint. And you''re using `internal'' here to mean `internal to libxl''. I think that''s clear from the name (which has `__''). Whereas on first reading it sounds like you mean `internal to the device adding machinery'' - but `libxl__device_disk_add'' is the main entrypoint to that machinery. And `libxl__initiate_device_add'' is the internal helper function. I think these comments need to be clarified and perhaps libxl__initiate_device_add needs to be renamed to be clear about what exactly it is for. Eg /* Initiates the device-kind-independent operations blah blah hidden void libxl__initiate_device_add_core(libxl__egc*, libxl__ao_device *aodev); or something. Please do reply suggesting alternative wording Ian.
Ian Jackson
2012-Jun-07 14:26 UTC
Re: [PATCH v5 05/10] libxl: convert libxl_device_nic_add to an async operation
Roger Pau Monne writes ("[PATCH v5 05/10] libxl: convert libxl_device_nic_add to an async operation"):> 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.This is now a lot smaller, excellent.> -int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic) > +int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic, > + const libxl_asyncop_how *ao_how) > { > - GC_INIT(ctx); > + AO_CREATE(ctx, domid, ao_how); > + libxl__ao_device *device;Can we call that variable `aodev'' ? Elsewhere `device'' is a libxl_device_FOO. Ian.
Ian Jackson
2012-Jun-07 14:40 UTC
Re: [PATCH v5 08/10] libxl: call hotplug scripts for disk devices from libxl
Roger Pau Monne writes ("[PATCH v5 08/10] libxl: call hotplug scripts for disk devices from libxl"):> 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....> +static void device_hotplug(libxl__egc *egc, libxl__ao_device *aodev)...> +out: > + libxl__ev_time_deregister(gc, &aodev->ev); > + aodev->rc = rc; > + aodev->callback(egc, aodev);This little set of things - deregistering the timeout, and calling the callback, occur a few times. It would also be a good idea to check that the child has been reaped, or we may reenter this confusingly later. Perhaps it would be a good idea to have: static void device_hotplug_done(blah blah) { libxl__ev_time_deregister(gc, &aodev->ev); assert(!libxl__ev_child_inuse(&aodev->child)); aodev->callback(egc, aodev); } And this could be used on all of the completion paths. (You''ll have to initialise the ev_child at the top of device_hotplug of course.) That way the whole thing has one entry and one exit.> diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h > index 5a2a87a..9dd2404 100644 > --- a/tools/libxl/libxl_internal.h > +++ b/tools/libxl/libxl_internal.h > @@ -72,6 +72,7 @@ > > #define LIBXL_INIT_TIMEOUT 10 > #define LIBXL_DESTROY_TIMEOUT 10 > +#define LIBXL_HOTPLUG_TIMEOUT 10 > #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10 > #define LIBXL_XENCONSOLE_LIMIT 1048576 > #define LIBXL_XENCONSOLE_PROTOCOL "vt100" > @@ -1846,6 +1847,10 @@ struct libxl__ao_device { > int rc; > libxl__ev_devstate ds; > void *base; > + /* device hotplug execution */ > + char *what;`const char *what;'' ? Ian.
Ian Jackson
2012-Jun-07 14:48 UTC
Re: [PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl
Roger Pau Monne writes ("[PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl"):> 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.> @@ -1894,10 +1897,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. This is currently used for > + * IOEMU nic interfaces on Linux, since we need to call hotplug scripts twice > + * for the same device, the first one to add the vif interface, and the second > + * time to add the tap interface, so: > + * num_exec == 0: execute hotplug for vif interface. > + * num_exec == 1: execute hotplug for the associated tap interface. > */I think you should add: * 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. Or * The main body of libxl will call libxl__get_hotplug_script_info * with exactly the stated values of num_exec, above. For device * types not mentioned the main body calls it once with * num_exec==0. Personally I''m inclined think the knowledge that nics need two invocations while disks need only one should be confined to the non-portable Linux code. Or do you think different platforms will always do this the same way ? It seems a bit odd to have the information about num_exec spread about like this. So I would prefer the former comment (and the corresponding change to the code). That also avoids a nic-specific section in the main body of libxl''s hotplug script machinery.> +int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype) > +{...> + } > + > +out: > + return rc; > +} > +As a matter of good practice I think you should say rc = 0; just before out, on the success path, and not rely on it having happened to be set to 0 by the previous successful call. Thanks, Ian.
Ian Jackson
2012-Jun-07 14:50 UTC
Re: [PATCH v5 10/10] libxl: use libxl__xs_path_cleanup on device_destroy
Roger Pau Monne writes ("[PATCH v5 10/10] libxl: use libxl__xs_path_cleanup on device_destroy"):> 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.Surely this change should be in (or before) one of the earlier patches ? Specifically, the one where that hotplug script is disabled. Ian.
Roger Pau Monne
2012-Jun-07 16:42 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Ian Jackson wrote:> Roger Pau Monne writes ("[PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"): >> 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 latter patches that will launch hotplug scripts after >> we reached the desired xenbus state. > > Just spotted: > >> +void libxl__initiate_device_add(libxl__egc *egc, libxl__ao_device *aodev) >> +{ > ... >> + libxl__ev_devstate_init(&aodev->ds); >> + rc = libxl__ev_devstate_wait(gc,&aodev->ds, device_backend_callback, >> + state_path, XenbusStateInitWait, >> + LIBXL_INIT_TIMEOUT * 1000); > > Another unneeded _init.Ok, I thought that you need to call libxl__ev_devstate_init before libxl__ev_devstate_wait? I just saw that _wait performs the _init itself (but without calling _init). Can''t we remove _init, if it''s not needed? Having and _init function makes people thing they have to use it, and the comments doesn''t say anything about _wait calling _init itself.> I won''t comment on any more of these I find.
Ian Jackson
2012-Jun-07 16:47 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Roger Pau Monne writes ("Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"):> Can''t we remove _init, if it''s not needed? Having and _init function > makes people thing they have to use it, and the comments doesn''t say > anything about _wait calling _init itself._init is needed to provide a way to initialise the structure so that it is safe to pass to _cancel. This means that caller can use idempotent initialisation and cleanup, which avoids leaks and logic errors. This is the same way it''s done for many other of the event functions, eg: libxl__ev_FOO_{init,register,deregister} libxl__datacopier_{state,init,start,kill} libxl__bootloader_{state,init,run} Feel free to provide a comment clarifying the API. Ian.
Roger Pau Monne
2012-Jun-07 16:55 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Ian Jackson wrote:> Roger Pau Monne writes ("[PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"): >> 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 latter patches that will launch hotplug scripts after >> we reached the desired xenbus state. > > And: > >> +/* Internal AO operation to connect a disk device, called by >> + * libxl_device_disk_add and libxl__add_disks. This function calls >> + * libxl__initiate_device_add */ >> +_hidden void libxl__device_disk_add(libxl__egc *egc, uint32_t domid, >> + libxl_device_disk *disk, >> + libxl__ao_device *aodev); >> + >> +/* Arranges that dev will be added 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. >> + */ >> +_hidden void libxl__initiate_device_add(libxl__egc*, libxl__ao_device *aodev); > > I don''t think these comments are coherent. > > I think a function which "Arranges that dev will be added to the > guest" is one which does everything necessary - ie, the outermost > entrypoint. > > And you''re using `internal'' here to mean `internal to libxl''. I think > that''s clear from the name (which has `__''). Whereas on first reading > it sounds like you mean `internal to the device adding machinery'' - > but `libxl__device_disk_add'' is the main entrypoint to that > machinery.I''ve used ''internal'' here to reflex the difference between libxl_device_disk_add and libxl__device_disk_add, but probably leads to confusion.> And `libxl__initiate_device_add'' is the internal helper function. > > I think these comments need to be clarified and perhaps > libxl__initiate_device_add needs to be renamed to be clear about what > exactly it is for. Eg > /* Initiates the device-kind-independent operations blah blah > hidden void libxl__initiate_device_add_core(libxl__egc*, > libxl__ao_device *aodev); > or something. > > Please do reply suggesting alternative wording > > Ian.Well the order is the following: libxl__device_{disk/nic}_add (device type dependant function) -> libxl__initiate_device_add (device type independent). I''m really bad about this naming thing. But the fact is that libxl__initiate_device_add is not a good name, since when we call this function the device is already added (to xenstore), this merely waits for it to reach the desired state and performs the necessary actions to "add" it to the guest (call hotplug scripts). Maybe "connect" is a more appropriate name than "add". I liked the nomenclature because it follows the same style as libxl__initiate_device_remove, and I think (and I might be wrong) it makes things easier to understand.
Ian Jackson
2012-Jun-07 17:05 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Roger Pau Monne writes ("Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"):> Ian Jackson wrote: > > And you''re using `internal'' here to mean `internal to libxl''. I think > > that''s clear from the name (which has `__''). Whereas on first reading > > it sounds like you mean `internal to the device adding machinery'' - > > but `libxl__device_disk_add'' is the main entrypoint to that > > machinery. > > I''ve used ''internal'' here to reflex the difference between > libxl_device_disk_add and libxl__device_disk_add, but probably leads to > confusion.Yes.> Well the order is the following: > > libxl__device_{disk/nic}_add (device type dependant function) -> > libxl__initiate_device_add (device type independent).Yes.> I''m really bad about this naming thing. But the fact is that > libxl__initiate_device_add is not a good name, since when we call this > function the device is already added (to xenstore), this merely waits > for it to reach the desired state and performs the necessary actions to > "add" it to the guest (call hotplug scripts). Maybe "connect" is a more > appropriate name than "add".Perhaps so.> I liked the nomenclature because it follows the same style as > libxl__initiate_device_remove, and I think (and I might be wrong) it > makes things easier to understand.But libxl__initiate_device_remove _is_ the function which actually initiates a device removal. It isn''t an internal helper for a family of libxl__device_FOO_remove functions. If it were symmetrical, libxl__initate_device_add would be the single entrypoint and it would call something kind-specific as a helper. That''s what the comments and names seem to suggest to me, apart from the explicit statement that it''s the other way around. Hence my feeling that it needs to be clarified and perhaps this function renamed. Ian.
Roger Pau Monne
2012-Jun-07 17:07 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Ian Jackson wrote:> Roger Pau Monne writes ("Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"): >> Ian Jackson wrote: >>> And you''re using `internal'' here to mean `internal to libxl''. I think >>> that''s clear from the name (which has `__''). Whereas on first reading >>> it sounds like you mean `internal to the device adding machinery'' - >>> but `libxl__device_disk_add'' is the main entrypoint to that >>> machinery. >> I''ve used ''internal'' here to reflex the difference between >> libxl_device_disk_add and libxl__device_disk_add, but probably leads to >> confusion. > > Yes. > >> Well the order is the following: >> >> libxl__device_{disk/nic}_add (device type dependant function) -> >> libxl__initiate_device_add (device type independent). > > Yes. > >> I''m really bad about this naming thing. But the fact is that >> libxl__initiate_device_add is not a good name, since when we call this >> function the device is already added (to xenstore), this merely waits >> for it to reach the desired state and performs the necessary actions to >> "add" it to the guest (call hotplug scripts). Maybe "connect" is a more >> appropriate name than "add". > > Perhaps so. > >> I liked the nomenclature because it follows the same style as >> libxl__initiate_device_remove, and I think (and I might be wrong) it >> makes things easier to understand. > > But libxl__initiate_device_remove _is_ the function which actually > initiates a device removal. It isn''t an internal helper for a > family of libxl__device_FOO_remove functions. > > If it were symmetrical, libxl__initate_device_add would be the > single entrypoint and it would call something kind-specific as a helper. > That''s what the comments and names seem to suggest to me, apart from > the explicit statement that it''s the other way around. > > Hence my feeling that it needs to be clarified and perhaps this > function renamed.What about libxl__initiate_device_connection? and thanks for reviewing the code :).
Ian Jackson
2012-Jun-07 17:11 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Roger Pau Monne writes ("Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"):> Ian Jackson wrote:...> > Hence my feeling that it needs to be clarified and perhaps this > > function renamed. > > What about libxl__initiate_device_connection?That would be fine by me. You''ll want to improve the comments too.> and thanks for reviewing the code :).You''re welcome. Sorry I found things this time round that I could have perhaps seen last time... Ian.
Roger Pau Monne
2012-Jun-11 10:09 UTC
Re: [PATCH v5 01/10] libxl: change libxl__ao_device_remove to libxl__ao_device
Ian Jackson wrote: I''ve fixed al other comments, so I''m not going to reply to every one of them :)>> +void libxl__initiate_device_remove(libxl__egc *egc, >> + libxl__ao_device *aodev) > ... >> - libxl__ev_devstate_init(&aorm->ds); >> + libxl__ev_devstate_init(&aodev->ds); >> >> - rc = libxl__ev_devstate_wait(gc,&aorm->ds, device_remove_callback, >> + rc = libxl__ev_devstate_wait(gc,&aodev->ds, device_backend_callback, >> state_path, XenbusStateClosed, >> LIBXL_DESTROY_TIMEOUT * 1000); > > libxl__ev_devstate_init is not needed before _wait. (Admittedly > that''s there in the code before you touched it.) In general foo_init > is not needed before foo_do_the_thing_and_call_me_back. > > Given the use of `backend'' in function names to refer to what is > manipulated with aodev->ds, perhaps aodev->ds should be called > aodev->backend_ds ?Done, I''ve changed it to backend_ds> I was briefly confused about whether `device_backend_cleanup'' was a > general cleanup function for a libxl__ao_device (which it isn''t). And > there is of course a frontend state too, which we don''t explicitly > deal with here. > > How do the frontend and backend directories get removed from > xenstore ? (I have a feeling I have asked this before but I don''t > remember the answer. Perhaps it could be a comment in the code in > some appropriate place?)We talked about this, and now I''ve introduced a function that is always called before the user callback, that deletes both frontend and backend xs entries. So the flow of execution is a little bit more "linear", and we have common entry and exit points, we only call the callback from the exit function.
Roger Pau Monne
2012-Jun-11 12:33 UTC
Re: [PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op
Ian Jackson wrote:> Roger Pau Monne writes ("[PATCH v5 04/10] libxl: convert libxl_device_disk_add to an asyn op"): >> This patch converts libxl_device_disk_add to an ao operation that > > In the subject line `asyn'' should be `async''. > > >> @@ -1859,11 +1883,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); > > Is there going to be a later patch in a future version of the series > that fixes these ? >Not really, but I might add one.> >> +/* Macro to define callbacks of libxl__add_*, checks if device is last >> + * and calls the function passed as a parameter to this macro with the >> + * following syntax; func(libxl__egc *, parent_type *, int rc) */ >> +#define DEFINE_DEVICES_CALLBACK(callback_name, parent_type, array_name, \ >> + array_size_name, func_to_call) \ >> + static void callback_name(libxl__egc *egc, libxl__ao_device *aodev) \ >> + { \ >> + STATE_AO_GC(aodev->ao); \ >> + parent_type *p = CONTAINER_OF(aodev->base, *p, array_name); \ >> + int rc; \ >> + \ >> + rc = libxl__ao_device_check_last(gc, aodev, p->array_name, \ >> + p->array_size_name); \ >> + if (rc> 0) return; \ >> + func_to_call(egc, p, rc); \ >> + } > > This is acceptable IMO and better than the repetition. However the > use of a macro for this, so far from the invocation sites, is indeed > less than perfect. > > Looking at the content of this macro, the only thing it needs from the > parent is array_name and array_size_name. If you invented > > typedef struct libxl__ao_devices libxl__ao_devices; > struct libxl__ao_devices { > libxl__ao_device *array; > int size; > void (*callback)(libxl__egc*, libxl__ao_devices*, int rc); > }; > > Then aodev->base could be of type libxl__ao_devices* and the macro > above could be a function. And indeed it would have type > suitable for simply filling into aodev->callback. Would that work ?Yes, I''ve applied this change.> Doing this would also perhaps mean DEFINE_DEVICES_ADD''s generated > libxl__add_FOOs functions could become one single function. AFAICT > the only macroish things it does are: (1) accessing > d_config->num_FOOs, which could be passed in as a parameter; > (2) passing d_config->FOOs[i] to libxl__device_FOO_add, which could be > dealt with by making libxl__device_FOO_add take a void* for the config > instead, and passing the start of the d_config->FOOs array, plus the > element size, and the add function, as arguments.I think this is really too complex for the benefits it introduces, libxl__add_{disks/nics} already takes a lot of arguments. Also take into account that libxl__device_disk_add is different from the rest now, because it takes the extra xs_transaction parameter.> Sorry to mess you about. > > > Whether you retain this as a macro or make it a function, > >> diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c >> index 2dc1cee..1dc8247 100644 >> --- a/tools/libxl/libxl_create.c >> +++ b/tools/libxl/libxl_create.c >> @@ -585,6 +585,13 @@ DEFINE_DEVICES_CALLBACK(domcreate_disk_connected, >> libxl__domain_create_state, >> devices, num_devices, domcreate_launch_dm) >> >> +static void domcreate_attach_pci(libxl__egc *egc, >> + libxl__domain_create_state *dcs, int rc); >> + >> +DEFINE_DEVICES_CALLBACK(domcreate_nic_connected, >> + libxl__domain_create_state, >> + devices, num_devices, domcreate_attach_pci) >> + >> static void domcreate_console_available(libxl__egc *egc, >> libxl__domain_create_state *dcs); >> > > These seem not to be in linear execution order ? These kind of > arrangements with a chain of callbacks should really have both the > declarations and the definitions in linear order. Sadly that means > writing out the declarations of domcreate_nic_connected etc. here in > the declarations section but I think that''s a price worth paying. > (This is something you''d have to do anyway if you replace the macro > with a function as I suggest.)Done.
Roger Pau Monne
2012-Jun-11 14:34 UTC
Re: [PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl
Ian Jackson wrote:> Roger Pau Monne writes ("[PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl"): >> 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. > >> @@ -1894,10 +1897,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. This is currently used for >> + * IOEMU nic interfaces on Linux, since we need to call hotplug scripts twice >> + * for the same device, the first one to add the vif interface, and the second >> + * time to add the tap interface, so: >> + * num_exec == 0: execute hotplug for vif interface. >> + * num_exec == 1: execute hotplug for the associated tap interface. >> */ > > I think you should add: > > * 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. > > Or > > * The main body of libxl will call libxl__get_hotplug_script_info > * with exactly the stated values of num_exec, above. For device > * types not mentioned the main body calls it once with > * num_exec==0. > > Personally I''m inclined think the knowledge that nics need two > invocations while disks need only one should be confined to the > non-portable Linux code. Or do you think different platforms will > always do this the same way ? It seems a bit odd to have the > information about num_exec spread about like this. So I would prefer > the former comment (and the corresponding change to the code). > > That also avoids a nic-specific section in the main body of libxl''s > hotplug script machinery.What do you think about having a function in the non-portable code that tells you the number of times you have to call libxl__get_hotplug_script_info? So we can do something like: aodev->total_exec = libxl__get_hotplug_num_exec(...) It''s not pretty, but at least will allow us to abstract this code from Linux/NetBSD, since what I basically do now with NetBSD is return 0 on the second call, thus avoiding executing anything. But we still have the ugly Linux part of the code in libxl_device.>> +int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype) >> +{ > ... >> + } >> + >> +out: >> + return rc; >> +} >> + > > As a matter of good practice I think you should say > rc = 0; > just before out, on the success path, and not rely on it having > happened to be set to 0 by the previous successful call. > > Thanks, > Ian.
Ian Jackson
2012-Jun-22 11:47 UTC
Re: [PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl [and 1 more messages]
Roger Pau Monne writes ("[PATCH v6 13/13] libxl: call hotplug scripts for nic devices from libxl"):> 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.Thanks. Acked-by: Ian Jackson <ian.jackson@eu.citrix.com> However, AFAICT you missed this comment of mine:> Roger Pau Monne writes ("[PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl"): > > +int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype) > > +{ > ... > > + } > > + > > +out: > > + return rc; > > +} > > + > > As a matter of good practice I think you should say > rc = 0; > just before out, on the success path, and not rely on it having > happened to be set to 0 by the previous successful call.So IWBNI in the next version you did that too. Thanks, Ian.
Roger Pau Monne
2012-Jun-26 08:57 UTC
Re: [PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl [and 1 more messages]
Ian Jackson wrote:> Roger Pau Monne writes ("[PATCH v6 13/13] libxl: call hotplug scripts for nic devices from libxl"): >> 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. > > Thanks. > > Acked-by: Ian Jackson<ian.jackson@eu.citrix.com> > > However, AFAICT you missed this comment of mine: >> Roger Pau Monne writes ("[PATCH v5 09/10] libxl: call hotplug scripts for nic devices from libxl"): >>> +int libxl__nic_type(libxl__gc *gc, libxl__device *dev, libxl_nic_type *nictype) >>> +{ >> ... >>> + } >>> + >>> +out: >>> + return rc; >>> +} >>> + >> As a matter of good practice I think you should say >> rc = 0; >> just before out, on the success path, and not rely on it having >> happened to be set to 0 by the previous successful call. > > So IWBNI in the next version you did that too.Sorry, I will add that and the acked-by on the next repost.> Thanks, > Ian.