From mboxrd@z Thu Jan 1 00:00:00 1970 From: Wei Liu Subject: [PATCH V4 24/24] xl: update configuration when we unplug a device Date: Thu, 1 May 2014 13:58:21 +0100 Message-ID: <1398949101-23320-25-git-send-email-wei.liu2@citrix.com> References: <1398949101-23320-1-git-send-email-wei.liu2@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1398949101-23320-1-git-send-email-wei.liu2@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: Wei Liu , ian.jackson@eu.citrix.com, ian.campbell@citrix.com List-Id: xen-devel@lists.xenproject.org Some macros are written to accomplish following tasks: 1. load domain configuration 2. allocate a new array of devices 3. copy the devices that are to be remain in configuration 4. replace pointer in libxl_domain_config 5. store domain configuration Signed-off-by: Wei Liu --- tools/libxl/xl_cmdimpl.c | 83 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 14 deletions(-) diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 91172c5..ae3df6e 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -411,6 +411,59 @@ static void *xrealloc(void *ptr, size_t sz) { libxl_domain_config_dispose((d_config)); \ } while (0) +#define COMPARE_DEVID(a, b) ((a)->devid == (b)->devid) +#define COMPARE_DISK(a, b) (!strcmp((a)->vdev, (b)->vdev)) +#define COMPARE_PCI(a, b) ((a)->bus == (b)->bus && \ + (a)->dev == (b)->dev && \ + (a)->func == (b)->func) + +/* Remove / destroy a device and update the stored configuration */ +#define REMOVEDESTROY_DEVICE(devtype,domid,dev,ptr,cnt, \ + compare,removedestroy) \ + do { \ + libxl_domain_config d_config; \ + libxl_device_ ## devtype *p = NULL, *x; \ + int num; \ + int j, k; \ + \ + libxl_domain_config_init(&d_config); \ + load_domain_config((domid), &d_config); \ + \ + k = 0; \ + for (j = 0; j < d_config.cnt; j++) { \ + x = d_config.ptr + j; \ + if (compare(x, &(dev))) \ + k++; \ + } \ + \ + num = d_config.cnt - k; \ + \ + p = xrealloc(p, sizeof(*p) * num); \ + \ + k = 0; \ + for (j = 0; j < d_config.cnt; j++) { \ + x = d_config.ptr + j; \ + if (compare(x, &(dev))) \ + continue; \ + libxl_device_ ## devtype ## _copy(ctx, &p[k], \ + d_config.ptr + j); \ + k++; \ + } \ + \ + if (libxl_device_ ## devtype ## _ ## removedestroy(ctx,(domid), \ + &(dev), 0)) { \ + fprintf(stderr, "libxl_device_%s_remove failed.\n", #devtype); \ + exit(1); \ + } \ + \ + free(d_config.ptr); \ + d_config.ptr = p; \ + d_config.cnt = num; \ + \ + store_domain_config((domid), &d_config); \ + libxl_domain_config_dispose(&d_config); \ + } while (0) + #define LOG(_f, _a...) dolog(__FILE__, __LINE__, __func__, _f "\n", ##_a) static void dolog(const char *file, int line, const char *func, char *fmt, ...) @@ -3048,9 +3101,11 @@ static void pcidetach(uint32_t domid, const char *bdf, int force) exit(2); } if (force) - libxl_device_pci_destroy(ctx, domid, &pcidev, 0); + REMOVEDESTROY_DEVICE(pci, domid, pcidev, pcidevs, + num_pcidevs, COMPARE_PCI, destroy); else - libxl_device_pci_remove(ctx, domid, &pcidev, 0); + REMOVEDESTROY_DEVICE(pci, domid, pcidev, pcidevs, + num_pcidevs, COMPARE_PCI, remove); libxl_device_pci_dispose(&pcidev); xlu_cfg_destroy(config); @@ -6154,10 +6209,10 @@ int main_networkdetach(int argc, char **argv) return 1; } } - if (libxl_device_nic_remove(ctx, domid, &nic, 0)) { - fprintf(stderr, "libxl_device_nic_del failed.\n"); - return 1; - } + + REMOVEDESTROY_DEVICE(nic, domid, nic, nics, num_nics, + COMPARE_DEVID, remove); + libxl_device_nic_dispose(&nic); return 0; } @@ -6254,10 +6309,10 @@ int main_blockdetach(int argc, char **argv) fprintf(stderr, "Error: Device %s not connected.\n", argv[optind+1]); return 1; } - rc = libxl_device_disk_remove(ctx, domid, &disk, 0); - if (rc) { - fprintf(stderr, "libxl_device_disk_remove failed.\n"); - } + + REMOVEDESTROY_DEVICE(disk, domid, disk, disks, num_disks, + COMPARE_DISK, remove); + libxl_device_disk_dispose(&disk); return rc; } @@ -6382,10 +6437,10 @@ int main_vtpmdetach(int argc, char **argv) return 1; } } - rc = libxl_device_vtpm_remove(ctx, domid, &vtpm, 0); - if (rc) { - fprintf(stderr, "libxl_device_vtpm_remove failed.\n"); - } + + REMOVEDESTROY_DEVICE(vtpm, domid, vtpm, vtpms, num_vtpms, + COMPARE_DEVID, remove); + libxl_device_vtpm_dispose(&vtpm); return rc; } -- 1.7.10.4