Gianni Tedesco
2011-Jan-07 16:07 UTC
[Xen-devel] [PATCH 0 of 2] xl: code motion and flexarray append
This is version 3 of ''split dm functions'' to separate file and ''flexarray append''. Changes since v2 is that patches are re-ordered and code is rebased. There is also a fix for a bug in the flexarray append conversion. The libxl domain create code is re-based on top of these and currently in testing. I will re-submit that when I am done with the testing and confident of correctness. Gianni _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gianni Tedesco
2011-Jan-07 16:07 UTC
[Xen-devel] [PATCH 1 of 2] xl: Move device model functions in to a separate file
tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 810 +----------------------------------------- tools/libxl/libxl_dm.c | 833 +++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 2 + 4 files changed, 838 insertions(+), 809 deletions(-) # HG changeset patch # User Gianni Tedesco <gianni.tedesco@citrix.com> # Date 1294416306 0 # Node ID 2bde4e48657ec8087778a5924bf2d07f02666c39 # Parent 82c205df44062aaa6f3ad2d22b5273e18ec3151e xl: Move device model functions in to a separate file No functional changes. Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com> diff -r 82c205df4406 -r 2bde4e48657e tools/libxl/Makefile --- a/tools/libxl/Makefile Fri Jan 07 14:13:15 2011 +0000 +++ b/tools/libxl/Makefile Fri Jan 07 16:05:06 2011 +0000 @@ -29,7 +29,7 @@ endif LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o -LIBXL_OBJS = flexarray.o libxl.o libxl_pci.o libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o libxl_internal.o libxl_utils.o $(LIBXL_OBJS-y) +LIBXL_OBJS = flexarray.o libxl.o libxl_dm.o libxl_pci.o libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o libxl_internal.o libxl_utils.o $(LIBXL_OBJS-y) LIBXL_OBJS += _libxl_types.o AUTOINCS= libxlu_cfg_y.h libxlu_cfg_l.h diff -r 82c205df4406 -r 2bde4e48657e tools/libxl/libxl.c --- a/tools/libxl/libxl.c Fri Jan 07 14:13:15 2011 +0000 +++ b/tools/libxl/libxl.c Fri Jan 07 16:05:06 2011 +0000 @@ -902,46 +902,6 @@ int libxl_event_get_disk_eject_info(libx return 1; } -static int libxl_destroy_device_model(libxl_ctx *ctx, uint32_t domid) -{ - libxl__gc gc = LIBXL_INIT_GC(ctx); - char *pid; - int ret; - - pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/image/device-model-pid", domid)); - if (!pid) { - int stubdomid = libxl_get_stubdom_id(ctx, domid); - 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\n", stubdomid); - ret = libxl_domain_destroy(ctx, stubdomid, 0); - if (ret) - goto out; - } 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; - } - } - xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", domid)); - -out: - libxl__free_all(&gc); - return ret; -} - int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force) { libxl__gc gc = LIBXL_INIT_GC(ctx); @@ -975,8 +935,8 @@ int libxl_domain_destroy(libxl_ctx *ctx, LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_pause failed for %d", domid); } if (dm_present) { - if (libxl_destroy_device_model(ctx, domid) < 0) - LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_device_model failed for %d", domid); + if (libxl__destroy_device_model(ctx, domid) < 0) + LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model failed for %d", domid); } if (libxl__devices_destroy(ctx, domid, force) < 0) LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid); @@ -1107,698 +1067,6 @@ skip_autopass: return 0; } -static char ** libxl_build_device_model_args_old(libxl__gc *gc, - libxl_device_model_info *info, - libxl_device_nic *vifs, - int num_vifs) -{ - int num = 0, i; - flexarray_t *dm_args; - dm_args = flexarray_make(16, 1); - - if (!dm_args) - return NULL; - - flexarray_set(dm_args, num++, "qemu-dm"); - flexarray_set(dm_args, num++, "-d"); - - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid)); - - if (info->dom_name) { - flexarray_set(dm_args, num++, "-domain-name"); - flexarray_set(dm_args, num++, info->dom_name); - } - if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) { - flexarray_set(dm_args, num++, "-vnc"); - if (info->vncdisplay) { - if (info->vnclisten && strchr(info->vnclisten, '':'') == NULL) { - flexarray_set( - dm_args, num++, - libxl__sprintf(gc, "%s:%d%s", - info->vnclisten, - info->vncdisplay, - info->vncpasswd ? ",password" : "")); - } else { - flexarray_set(dm_args, num++, libxl__sprintf(gc, "127.0.0.1:%d", info->vncdisplay)); - } - } else if (info->vnclisten) { - if (strchr(info->vnclisten, '':'') != NULL) { - flexarray_set(dm_args, num++, info->vnclisten); - } else { - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%s:0", info->vnclisten)); - } - } else { - flexarray_set(dm_args, num++, "127.0.0.1:0"); - } - if (info->vncunused) { - flexarray_set(dm_args, num++, "-vncunused"); - } - } - if (info->sdl) { - flexarray_set(dm_args, num++, "-sdl"); - if (!info->opengl) { - flexarray_set(dm_args, num++, "-disable-opengl"); - } - } - if (info->keymap) { - flexarray_set(dm_args, num++, "-k"); - flexarray_set(dm_args, num++, info->keymap); - } - if (info->nographic && (!info->sdl && !info->vnc)) { - flexarray_set(dm_args, num++, "-nographic"); - } - if (info->serial) { - flexarray_set(dm_args, num++, "-serial"); - flexarray_set(dm_args, num++, info->serial); - } - if (info->type == XENFV) { - int ioemu_vifs = 0; - - if (info->videoram) { - flexarray_set(dm_args, num++, "-videoram"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->videoram)); - } - if (info->stdvga) { - flexarray_set(dm_args, num++, "-std-vga"); - } - - if (info->boot) { - flexarray_set(dm_args, num++, "-boot"); - flexarray_set(dm_args, num++, info->boot); - } - if (info->usb || info->usbdevice) { - flexarray_set(dm_args, num++, "-usb"); - if (info->usbdevice) { - flexarray_set(dm_args, num++, "-usbdevice"); - flexarray_set(dm_args, num++, info->usbdevice); - } - } - if (info->soundhw) { - flexarray_set(dm_args, num++, "-soundhw"); - flexarray_set(dm_args, num++, info->soundhw); - } - if (info->apic) { - flexarray_set(dm_args, num++, "-acpi"); - } - if (info->vcpus > 1) { - flexarray_set(dm_args, num++, "-vcpus"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus)); - } - if (info->vcpu_avail) { - flexarray_set(dm_args, num++, "-vcpu_avail"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "0x%x", info->vcpu_avail)); - } - for (i = 0; i < num_vifs; i++) { - if (vifs[i].nictype == NICTYPE_IOEMU) { - char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x", - vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2], - vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]); - char *ifname; - if (!vifs[i].ifname) - ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid); - else - ifname = vifs[i].ifname; - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", - vifs[i].devid, smac, vifs[i].model)); - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=no", - vifs[i].devid, ifname, vifs[i].bridge)); - ioemu_vifs++; - } - } - /* If we have no emulated nics, tell qemu not to create any */ - if ( ioemu_vifs == 0 ) { - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, "none"); - } - } - if (info->saved_state) { - flexarray_set(dm_args, num++, "-loadvm"); - flexarray_set(dm_args, num++, info->saved_state); - } - for (i = 0; info->extra && info->extra[i] != NULL; i++) - flexarray_set(dm_args, num++, info->extra[i]); - flexarray_set(dm_args, num++, "-M"); - if (info->type == XENPV) - flexarray_set(dm_args, num++, "xenpv"); - else - flexarray_set(dm_args, num++, "xenfv"); - flexarray_set(dm_args, num++, NULL); - return (char **) flexarray_contents(dm_args); -} - -static char ** libxl_build_device_model_args_new(libxl__gc *gc, - libxl_device_model_info *info, - libxl_device_nic *vifs, - int num_vifs) -{ - int num = 0, i; - flexarray_t *dm_args; - int nb; - libxl_device_disk *disks; - - dm_args = flexarray_make(16, 1); - if (!dm_args) - return NULL; - - flexarray_set(dm_args, num++, libxl__strdup(gc, info->device_model)); - - flexarray_set(dm_args, num++, "-xen-domid"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid)); - - if (info->type == XENPV) { - flexarray_set(dm_args, num++, "-xen-attach"); - } - - if (info->dom_name) { - flexarray_set(dm_args, num++, "-name"); - flexarray_set(dm_args, num++, info->dom_name); - } - if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) { - int display = 0; - const char *listen = "127.0.0.1"; - - flexarray_set(dm_args, num++, "-vnc"); - - if (info->vncdisplay) { - display = info->vncdisplay; - if (info->vnclisten && strchr(info->vnclisten, '':'') == NULL) { - listen = info->vnclisten; - } - } else if (info->vnclisten) { - listen = info->vnclisten; - } - - if (strchr(listen, '':'') != NULL) - flexarray_set(dm_args, num++, - libxl__sprintf(gc, "%s%s", listen, - info->vncunused ? ",to=99" : "")); - else - flexarray_set(dm_args, num++, - libxl__sprintf(gc, "%s:%d%s", listen, display, - info->vncunused ? ",to=99" : "")); - } - if (info->sdl) { - flexarray_set(dm_args, num++, "-sdl"); - } - - if (info->type == XENPV && !info->nographic) { - flexarray_set(dm_args, num++, "-vga"); - flexarray_set(dm_args, num++, "xenfb"); - } - - if (info->keymap) { - flexarray_set(dm_args, num++, "-k"); - flexarray_set(dm_args, num++, info->keymap); - } - if (info->nographic && (!info->sdl && !info->vnc)) { - flexarray_set(dm_args, num++, "-nographic"); - } - if (info->serial) { - flexarray_set(dm_args, num++, "-serial"); - flexarray_set(dm_args, num++, info->serial); - } - if (info->type == XENFV) { - int ioemu_vifs = 0; - - if (info->stdvga) { - flexarray_set(dm_args, num++, "-vga"); - flexarray_set(dm_args, num++, "std"); - } - - if (info->boot) { - flexarray_set(dm_args, num++, "-boot"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "order=%s", info->boot)); - } - if (info->usb || info->usbdevice) { - flexarray_set(dm_args, num++, "-usb"); - if (info->usbdevice) { - flexarray_set(dm_args, num++, "-usbdevice"); - flexarray_set(dm_args, num++, info->usbdevice); - } - } - if (info->soundhw) { - flexarray_set(dm_args, num++, "-soundhw"); - flexarray_set(dm_args, num++, info->soundhw); - } - if (!info->apic) { - flexarray_set(dm_args, num++, "-no-acpi"); - } - if (info->vcpus > 1) { - flexarray_set(dm_args, num++, "-smp"); - if (info->vcpu_avail) - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail)); - else - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus)); - } - for (i = 0; i < num_vifs; i++) { - if (vifs[i].nictype == NICTYPE_IOEMU) { - char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x", - vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2], - vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]); - char *ifname; - if (!vifs[i].ifname) { - ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid); - } else { - ifname = vifs[i].ifname; - } - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", - vifs[i].devid, smac, vifs[i].model)); - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,script=no", - vifs[i].devid, ifname)); - ioemu_vifs++; - } - } - /* If we have no emulated nics, tell qemu not to create any */ - if ( ioemu_vifs == 0 ) { - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, "none"); - } - } - if (info->saved_state) { - flexarray_set(dm_args, num++, "-loadvm"); - flexarray_set(dm_args, num++, info->saved_state); - } - for (i = 0; info->extra && info->extra[i] != NULL; i++) - flexarray_set(dm_args, num++, info->extra[i]); - flexarray_set(dm_args, num++, "-M"); - if (info->type == XENPV) - flexarray_set(dm_args, num++, "xenpv"); - else - flexarray_set(dm_args, num++, "xenfv"); - - /* RAM Size */ - flexarray_set(dm_args, num++, "-m"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->target_ram)); - - if (info->type == XENFV) { - disks = libxl_device_disk_list(libxl__gc_owner(gc), info->domid, &nb); - for (i; i < nb; i++) { - if (disks[i].is_cdrom) { - flexarray_set(dm_args, num++, "-cdrom"); - flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath)); - } else { - flexarray_set(dm_args, num++, libxl__sprintf(gc, "-%s", disks[i].virtpath)); - flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath)); - } - libxl_device_disk_destroy(&disks[i]); - } - free(disks); - } - flexarray_set(dm_args, num++, NULL); - return (char **) flexarray_contents(dm_args); -} - -static char ** libxl_build_device_model_args(libxl__gc *gc, - libxl_device_model_info *info, - libxl_device_nic *vifs, - int num_vifs) -{ - libxl_ctx *ctx = libxl__gc_owner(gc); - int new_qemu; - - new_qemu = libxl_check_device_model_version(ctx, info->device_model); - - if (new_qemu == 1) { - return libxl_build_device_model_args_new(gc, info, vifs, num_vifs); - } else { - return libxl_build_device_model_args_old(gc, info, vifs, num_vifs); - } -} - -static void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild) -{ - libxl_device_model_starting *starting = for_spawn; - struct xs_handle *xsh; - char *path = NULL, *pid = NULL; - int len; - - if (asprintf(&path, "%s/%s", starting->dom_path, "image/device-model-pid") < 0) - goto out; - - len = asprintf(&pid, "%d", innerchild); - if (len < 0) - goto out; - - /* we mustn''t use the parent''s handle in the child */ - xsh = xs_daemon_open(); - - xs_write(xsh, XBT_NULL, path, pid, len); - - xs_daemon_close(xsh); -out: - free(path); - free(pid); -} - -static int libxl_vfb_and_vkb_from_device_model_info(libxl_ctx *ctx, - libxl_device_model_info *info, - libxl_device_vfb *vfb, - libxl_device_vkb *vkb) -{ - memset(vfb, 0x00, sizeof(libxl_device_vfb)); - memset(vkb, 0x00, sizeof(libxl_device_vkb)); - - vfb->backend_domid = 0; - vfb->devid = 0; - vfb->vnc = info->vnc; - vfb->vnclisten = info->vnclisten; - vfb->vncdisplay = info->vncdisplay; - vfb->vncunused = info->vncunused; - vfb->vncpasswd = info->vncpasswd; - vfb->keymap = info->keymap; - vfb->sdl = info->sdl; - vfb->opengl = info->opengl; - - vkb->backend_domid = 0; - vkb->devid = 0; - return 0; -} - -static int libxl_write_dmargs(libxl_ctx *ctx, int domid, int guest_domid, char **args) -{ - libxl__gc gc = LIBXL_INIT_GC(ctx); - int i; - char *vm_path; - char *dmargs, *path; - int dmargs_size; - struct xs_permissions roperm[2]; - xs_transaction_t t; - - roperm[0].id = 0; - roperm[0].perms = XS_PERM_NONE; - roperm[1].id = domid; - roperm[1].perms = XS_PERM_READ; - - vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/vm", guest_domid)); - - i = 0; - dmargs_size = 0; - while (args[i] != NULL) { - dmargs_size = dmargs_size + strlen(args[i]) + 1; - i++; - } - dmargs_size++; - dmargs = (char *) malloc(dmargs_size); - i = 1; - dmargs[0] = ''\0''; - while (args[i] != NULL) { - if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) { - strcat(dmargs, " "); - strcat(dmargs, args[i]); - } - i++; - } - path = libxl__sprintf(&gc, "%s/image/dmargs", vm_path); - -retry_transaction: - t = xs_transaction_start(ctx->xsh); - xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs)); - xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm)); - xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm)); - if (!xs_transaction_end(ctx->xsh, t, 0)) - if (errno == EAGAIN) - goto retry_transaction; - free(dmargs); - libxl__free_all(&gc); - return 0; -} - -static int libxl_create_stubdom(libxl_ctx *ctx, - libxl_device_model_info *info, - libxl_device_disk *disks, int num_disks, - libxl_device_nic *vifs, int num_vifs, - libxl_device_vfb *vfb, - libxl_device_vkb *vkb, - libxl_device_model_starting **starting_r) -{ - libxl__gc gc = LIBXL_INIT_GC(ctx); - int i, num_console = 1, ret; - libxl_device_console *console; - libxl_domain_create_info c_info; - libxl_domain_build_info b_info; - libxl_domain_build_state state; - uint32_t domid; - char **args; - struct xs_permissions perm[2]; - xs_transaction_t t; - libxl_device_model_starting *dm_starting = 0; - - args = libxl_build_device_model_args(&gc, info, vifs, num_vifs); - if (!args) { - ret = ERROR_FAIL; - goto out; - } - - memset(&c_info, 0x00, sizeof(libxl_domain_create_info)); - c_info.hvm = 0; - c_info.name = libxl__sprintf(&gc, "%s-dm", libxl__domid_to_name(&gc, info->domid)); - - libxl_uuid_copy(&c_info.uuid, &info->uuid); - - memset(&b_info, 0x00, sizeof(libxl_domain_build_info)); - b_info.max_vcpus = 1; - b_info.max_memkb = 32 * 1024; - b_info.target_memkb = b_info.max_memkb; - b_info.kernel.path = libxl__abs_path(&gc, "ioemu-stubdom.gz", libxl_xenfirmwaredir_path()); - b_info.u.pv.cmdline = libxl__sprintf(&gc, " -d %d", info->domid); - b_info.u.pv.ramdisk.path = ""; - b_info.u.pv.features = ""; - b_info.hvm = 0; - - ret = libxl_domain_make(ctx, &c_info, &domid); - if (ret) - goto out_free; - ret = libxl_domain_build(ctx, &b_info, domid, &state); - if (ret) - goto out_free; - - libxl_write_dmargs(ctx, domid, info->domid, args); - libxl__xs_write(&gc, XBT_NULL, - libxl__sprintf(&gc, "%s/image/device-model-domid", libxl__xs_get_dompath(&gc, info->domid)), - "%d", domid); - libxl__xs_write(&gc, XBT_NULL, - libxl__sprintf(&gc, "%s/target", libxl__xs_get_dompath(&gc, domid)), - "%d", info->domid); - ret = xc_domain_set_target(ctx->xch, domid, info->domid); - if (ret<0) { - LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting target domain %d -> %d", domid, info->domid); - ret = ERROR_FAIL; - goto out_free; - } - xs_set_target(ctx->xsh, domid, info->domid); - - perm[0].id = domid; - perm[0].perms = XS_PERM_NONE; - perm[1].id = info->domid; - perm[1].perms = XS_PERM_READ; -retry_transaction: - t = xs_transaction_start(ctx->xsh); - xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid)); - xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm)); - xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/%d/device/vfs", domid)); - xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm)); - if (!xs_transaction_end(ctx->xsh, t, 0)) - if (errno == EAGAIN) - goto retry_transaction; - - for (i = 0; i < num_disks; i++) { - disks[i].domid = domid; - ret = libxl_device_disk_add(ctx, domid, &disks[i]); - if (ret) - goto out_free; - } - for (i = 0; i < num_vifs; i++) { - vifs[i].domid = domid; - ret = libxl_device_nic_add(ctx, domid, &vifs[i]); - if (ret) - goto out_free; - } - vfb->domid = domid; - ret = libxl_device_vfb_add(ctx, domid, vfb); - if (ret) - goto out_free; - vkb->domid = domid; - ret = libxl_device_vkb_add(ctx, domid, vkb); - if (ret) - goto out_free; - - if (info->serial) - num_console++; - - console = libxl__calloc(&gc, num_console, sizeof(libxl_device_console)); - if (!console) { - ret = ERROR_NOMEM; - goto out_free; - } - - for (i = 0; i < num_console; i++) { - console[i].devid = i; - console[i].consback = LIBXL_CONSBACK_IOEMU; - console[i].domid = domid; - if (!i) { - char *filename; - char *name = libxl__sprintf(&gc, "qemu-dm-%s", libxl_domid_to_name(ctx, info->domid)); - libxl_create_logfile(ctx, name, &filename); - console[i].output = libxl__sprintf(&gc, "file:%s", filename); - console[i].build_state = &state; - free(filename); - } else - console[i].output = "pty"; - ret = libxl_device_console_add(ctx, domid, &console[i]); - if (ret) - goto out_free; - } - if (libxl_create_xenpv_qemu(ctx, domid, vfb, &dm_starting) < 0) { - ret = ERROR_FAIL; - goto out_free; - } - if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) { - ret = ERROR_FAIL; - goto out_free; - } - - libxl_domain_unpause(ctx, domid); - - if (starting_r) { - *starting_r = calloc(sizeof(libxl_device_model_starting), 1); - (*starting_r)->domid = info->domid; - (*starting_r)->dom_path = libxl__xs_get_dompath(&gc, info->domid); - (*starting_r)->for_spawn = NULL; - } - - ret = 0; - -out_free: - free(args); -out: - libxl__free_all(&gc); - return ret; -} - -int libxl_create_device_model(libxl_ctx *ctx, - libxl_device_model_info *info, - libxl_device_disk *disks, int num_disks, - libxl_device_nic *vifs, int num_vifs, - libxl_device_model_starting **starting_r) -{ - libxl__gc gc = LIBXL_INIT_GC(ctx); - char *path, *logfile; - int logfile_w, null; - int rc; - char **args; - libxl_device_model_starting buf_starting, *p; - xs_transaction_t t; - char *vm_path; - char **pass_stuff; - - if (strstr(info->device_model, "stubdom-dm")) { - libxl_device_vfb vfb; - libxl_device_vkb vkb; - - libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb); - rc = libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r); - goto out; - } - - args = libxl_build_device_model_args(&gc, info, vifs, num_vifs); - if (!args) { - rc = ERROR_FAIL; - goto out; - } - - path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid); - xs_mkdir(ctx->xsh, XBT_NULL, path); - libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/disable_pf", path), "%d", !info->xen_platform_pci); - - libxl_create_logfile(ctx, libxl__sprintf(&gc, "qemu-dm-%s", info->dom_name), &logfile); - logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644); - free(logfile); - null = open("/dev/null", O_RDONLY); - - if (starting_r) { - rc = ERROR_NOMEM; - *starting_r = calloc(sizeof(libxl_device_model_starting), 1); - if (!*starting_r) - goto out_close; - p = *starting_r; - p->for_spawn = calloc(sizeof(libxl__spawn_starting), 1); - } else { - p = &buf_starting; - p->for_spawn = NULL; - } - - p->domid = info->domid; - p->dom_path = libxl__xs_get_dompath(&gc, info->domid); - if (!p->dom_path) { - rc = ERROR_FAIL; - goto out_close; - } - - if (info->vncpasswd) { -retry_transaction: - /* Find uuid and the write the vnc password to xenstore for qemu. */ - t = xs_transaction_start(ctx->xsh); - vm_path = libxl__xs_read(&gc,t,libxl__sprintf(&gc, "%s/vm", p->dom_path)); - if (vm_path) { - /* Now write the vncpassword into it. */ - pass_stuff = libxl__calloc(&gc, 3, sizeof(char *)); - pass_stuff[0] = "vncpasswd"; - pass_stuff[1] = info->vncpasswd; - libxl__xs_writev(&gc,t,vm_path,pass_stuff); - if (!xs_transaction_end(ctx->xsh, t, 0)) - if (errno == EAGAIN) - goto retry_transaction; - } - } - - rc = libxl__spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid); - if (rc < 0) - goto out_close; - if (!rc) { /* inner child */ - libxl__exec(null, logfile_w, logfile_w, - libxl__abs_path(&gc, info->device_model, libxl_libexec_path()), - args); - } - - rc = 0; - -out_close: - close(null); - close(logfile_w); - free(args); -out: - libxl__free_all(&gc); - return rc; -} - -int libxl_detach_device_model(libxl_ctx *ctx, - libxl_device_model_starting *starting) -{ - int rc; - rc = libxl__spawn_detach(ctx, starting->for_spawn); - if (starting->for_spawn) - free(starting->for_spawn); - free(starting); - return rc; -} - - -int libxl_confirm_device_model_startup(libxl_ctx *ctx, - libxl_device_model_starting *starting) -{ - int problem = libxl__wait_for_device_model(ctx, starting->domid, "running", NULL, NULL); - int detach; - if ( !problem ) - problem = libxl__spawn_check(ctx, starting->for_spawn); - detach = libxl_detach_device_model(ctx, starting); - return problem ? problem : detach; -} - - /******************************************************************************/ int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk) @@ -2631,80 +1899,6 @@ out: } /******************************************************************************/ -static int libxl_build_xenpv_qemu_args(libxl__gc *gc, - uint32_t domid, - libxl_device_vfb *vfb, - libxl_device_model_info *info) -{ - libxl_ctx *ctx = libxl__gc_owner(gc); - memset(info, 0x00, sizeof(libxl_device_model_info)); - - if (vfb != NULL) { - info->vnc = vfb->vnc; - if (vfb->vnclisten) - info->vnclisten = libxl__strdup(gc, vfb->vnclisten); - info->vncdisplay = vfb->vncdisplay; - info->vncunused = vfb->vncunused; - if (vfb->vncpasswd) - info->vncpasswd = vfb->vncpasswd; - if (vfb->keymap) - info->keymap = libxl__strdup(gc, vfb->keymap); - info->sdl = vfb->sdl; - info->opengl = vfb->opengl; - } else - info->nographic = 1; - info->domid = domid; - info->dom_name = libxl_domid_to_name(ctx, domid); - info->device_model = libxl__abs_path(gc, "qemu-dm", libxl_libexec_path()); - info->type = XENPV; - return 0; -} - -int libxl_need_xenpv_qemu(libxl_ctx *ctx, - int nr_consoles, libxl_device_console *consoles, - int nr_vfbs, libxl_device_vfb *vfbs, - int nr_disks, libxl_device_disk *disks) -{ - int i, ret = 0; - libxl__gc gc = LIBXL_INIT_GC(ctx); - - if (nr_consoles > 1) { - ret = 1; - goto out; - } - - for (i = 0; i < nr_consoles; i++) { - if (consoles[i].consback == LIBXL_CONSBACK_IOEMU) { - ret = 1; - goto out; - } - } - - if (nr_vfbs > 0) { - ret = 1; - goto out; - } - - if (nr_disks > 0 && !libxl__blktap_enabled(&gc)) - ret = 1; - -out: - libxl__free_all(&gc); - return ret; -} - -int libxl_create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb, - libxl_device_model_starting **starting_r) -{ - libxl__gc gc = LIBXL_INIT_GC(ctx); - libxl_device_model_info info; - - libxl_build_xenpv_qemu_args(&gc, domid, vfb, &info); - libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r); - libxl__free_all(&gc); - return 0; -} - int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb) { libxl__gc gc = LIBXL_INIT_GC(ctx); diff -r 82c205df4406 -r 2bde4e48657e tools/libxl/libxl_dm.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/libxl/libxl_dm.c Fri Jan 07 16:05:06 2011 +0000 @@ -0,0 +1,833 @@ +/* + * Copyright (C) 2010 Citrix Ltd. + * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com> + * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com> + * Author Gianni Tedesco <gianni.tedesco@citrix.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +#include "libxl_osdeps.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include "libxl_utils.h" +#include "libxl_internal.h" +#include "libxl.h" +#include "flexarray.h" + +static char ** libxl_build_device_model_args_old(libxl__gc *gc, + libxl_device_model_info *info, + libxl_device_nic *vifs, + int num_vifs) +{ + int num = 0, i; + flexarray_t *dm_args; + dm_args = flexarray_make(16, 1); + + if (!dm_args) + return NULL; + + flexarray_set(dm_args, num++, "qemu-dm"); + flexarray_set(dm_args, num++, "-d"); + + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid)); + + if (info->dom_name) { + flexarray_set(dm_args, num++, "-domain-name"); + flexarray_set(dm_args, num++, info->dom_name); + } + if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) { + flexarray_set(dm_args, num++, "-vnc"); + if (info->vncdisplay) { + if (info->vnclisten && strchr(info->vnclisten, '':'') == NULL) { + flexarray_set( + dm_args, num++, + libxl__sprintf(gc, "%s:%d%s", + info->vnclisten, + info->vncdisplay, + info->vncpasswd ? ",password" : "")); + } else { + flexarray_set(dm_args, num++, libxl__sprintf(gc, "127.0.0.1:%d", info->vncdisplay)); + } + } else if (info->vnclisten) { + if (strchr(info->vnclisten, '':'') != NULL) { + flexarray_set(dm_args, num++, info->vnclisten); + } else { + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%s:0", info->vnclisten)); + } + } else { + flexarray_set(dm_args, num++, "127.0.0.1:0"); + } + if (info->vncunused) { + flexarray_set(dm_args, num++, "-vncunused"); + } + } + if (info->sdl) { + flexarray_set(dm_args, num++, "-sdl"); + if (!info->opengl) { + flexarray_set(dm_args, num++, "-disable-opengl"); + } + } + if (info->keymap) { + flexarray_set(dm_args, num++, "-k"); + flexarray_set(dm_args, num++, info->keymap); + } + if (info->nographic && (!info->sdl && !info->vnc)) { + flexarray_set(dm_args, num++, "-nographic"); + } + if (info->serial) { + flexarray_set(dm_args, num++, "-serial"); + flexarray_set(dm_args, num++, info->serial); + } + if (info->type == XENFV) { + int ioemu_vifs = 0; + + if (info->videoram) { + flexarray_set(dm_args, num++, "-videoram"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->videoram)); + } + if (info->stdvga) { + flexarray_set(dm_args, num++, "-std-vga"); + } + + if (info->boot) { + flexarray_set(dm_args, num++, "-boot"); + flexarray_set(dm_args, num++, info->boot); + } + if (info->usb || info->usbdevice) { + flexarray_set(dm_args, num++, "-usb"); + if (info->usbdevice) { + flexarray_set(dm_args, num++, "-usbdevice"); + flexarray_set(dm_args, num++, info->usbdevice); + } + } + if (info->soundhw) { + flexarray_set(dm_args, num++, "-soundhw"); + flexarray_set(dm_args, num++, info->soundhw); + } + if (info->apic) { + flexarray_set(dm_args, num++, "-acpi"); + } + if (info->vcpus > 1) { + flexarray_set(dm_args, num++, "-vcpus"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus)); + } + if (info->vcpu_avail) { + flexarray_set(dm_args, num++, "-vcpu_avail"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "0x%x", info->vcpu_avail)); + } + for (i = 0; i < num_vifs; i++) { + if (vifs[i].nictype == NICTYPE_IOEMU) { + char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x", + vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2], + vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]); + char *ifname; + if (!vifs[i].ifname) + ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid); + else + ifname = vifs[i].ifname; + flexarray_set(dm_args, num++, "-net"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", + vifs[i].devid, smac, vifs[i].model)); + flexarray_set(dm_args, num++, "-net"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=no", + vifs[i].devid, ifname, vifs[i].bridge)); + ioemu_vifs++; + } + } + /* If we have no emulated nics, tell qemu not to create any */ + if ( ioemu_vifs == 0 ) { + flexarray_set(dm_args, num++, "-net"); + flexarray_set(dm_args, num++, "none"); + } + } + if (info->saved_state) { + flexarray_set(dm_args, num++, "-loadvm"); + flexarray_set(dm_args, num++, info->saved_state); + } + for (i = 0; info->extra && info->extra[i] != NULL; i++) + flexarray_set(dm_args, num++, info->extra[i]); + flexarray_set(dm_args, num++, "-M"); + if (info->type == XENPV) + flexarray_set(dm_args, num++, "xenpv"); + else + flexarray_set(dm_args, num++, "xenfv"); + flexarray_set(dm_args, num++, NULL); + return (char **) flexarray_contents(dm_args); +} + +static char ** libxl_build_device_model_args_new(libxl__gc *gc, + libxl_device_model_info *info, + libxl_device_nic *vifs, + int num_vifs) +{ + int num = 0, i; + flexarray_t *dm_args; + int nb; + libxl_device_disk *disks; + + dm_args = flexarray_make(16, 1); + if (!dm_args) + return NULL; + + flexarray_set(dm_args, num++, libxl__strdup(gc, info->device_model)); + + flexarray_set(dm_args, num++, "-xen-domid"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid)); + + if (info->type == XENPV) { + flexarray_set(dm_args, num++, "-xen-attach"); + } + + if (info->dom_name) { + flexarray_set(dm_args, num++, "-name"); + flexarray_set(dm_args, num++, info->dom_name); + } + if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) { + int display = 0; + const char *listen = "127.0.0.1"; + + flexarray_set(dm_args, num++, "-vnc"); + + if (info->vncdisplay) { + display = info->vncdisplay; + if (info->vnclisten && strchr(info->vnclisten, '':'') == NULL) { + listen = info->vnclisten; + } + } else if (info->vnclisten) { + listen = info->vnclisten; + } + + if (strchr(listen, '':'') != NULL) + flexarray_set(dm_args, num++, + libxl__sprintf(gc, "%s%s", listen, + info->vncunused ? ",to=99" : "")); + else + flexarray_set(dm_args, num++, + libxl__sprintf(gc, "%s:%d%s", listen, display, + info->vncunused ? ",to=99" : "")); + } + if (info->sdl) { + flexarray_set(dm_args, num++, "-sdl"); + } + + if (info->type == XENPV && !info->nographic) { + flexarray_set(dm_args, num++, "-vga"); + flexarray_set(dm_args, num++, "xenfb"); + } + + if (info->keymap) { + flexarray_set(dm_args, num++, "-k"); + flexarray_set(dm_args, num++, info->keymap); + } + if (info->nographic && (!info->sdl && !info->vnc)) { + flexarray_set(dm_args, num++, "-nographic"); + } + if (info->serial) { + flexarray_set(dm_args, num++, "-serial"); + flexarray_set(dm_args, num++, info->serial); + } + if (info->type == XENFV) { + int ioemu_vifs = 0; + + if (info->stdvga) { + flexarray_set(dm_args, num++, "-vga"); + flexarray_set(dm_args, num++, "std"); + } + + if (info->boot) { + flexarray_set(dm_args, num++, "-boot"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "order=%s", info->boot)); + } + if (info->usb || info->usbdevice) { + flexarray_set(dm_args, num++, "-usb"); + if (info->usbdevice) { + flexarray_set(dm_args, num++, "-usbdevice"); + flexarray_set(dm_args, num++, info->usbdevice); + } + } + if (info->soundhw) { + flexarray_set(dm_args, num++, "-soundhw"); + flexarray_set(dm_args, num++, info->soundhw); + } + if (!info->apic) { + flexarray_set(dm_args, num++, "-no-acpi"); + } + if (info->vcpus > 1) { + flexarray_set(dm_args, num++, "-smp"); + if (info->vcpu_avail) + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail)); + else + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus)); + } + for (i = 0; i < num_vifs; i++) { + if (vifs[i].nictype == NICTYPE_IOEMU) { + char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x", + vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2], + vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]); + char *ifname; + if (!vifs[i].ifname) { + ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid); + } else { + ifname = vifs[i].ifname; + } + flexarray_set(dm_args, num++, "-net"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", + vifs[i].devid, smac, vifs[i].model)); + flexarray_set(dm_args, num++, "-net"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,script=no", + vifs[i].devid, ifname)); + ioemu_vifs++; + } + } + /* If we have no emulated nics, tell qemu not to create any */ + if ( ioemu_vifs == 0 ) { + flexarray_set(dm_args, num++, "-net"); + flexarray_set(dm_args, num++, "none"); + } + } + if (info->saved_state) { + flexarray_set(dm_args, num++, "-loadvm"); + flexarray_set(dm_args, num++, info->saved_state); + } + for (i = 0; info->extra && info->extra[i] != NULL; i++) + flexarray_set(dm_args, num++, info->extra[i]); + flexarray_set(dm_args, num++, "-M"); + if (info->type == XENPV) + flexarray_set(dm_args, num++, "xenpv"); + else + flexarray_set(dm_args, num++, "xenfv"); + + /* RAM Size */ + flexarray_set(dm_args, num++, "-m"); + flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->target_ram)); + + if (info->type == XENFV) { + disks = libxl_device_disk_list(libxl__gc_owner(gc), info->domid, &nb); + for (i; i < nb; i++) { + if (disks[i].is_cdrom) { + flexarray_set(dm_args, num++, "-cdrom"); + flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath)); + } else { + flexarray_set(dm_args, num++, libxl__sprintf(gc, "-%s", disks[i].virtpath)); + flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath)); + } + libxl_device_disk_destroy(&disks[i]); + } + free(disks); + } + flexarray_set(dm_args, num++, NULL); + return (char **) flexarray_contents(dm_args); +} + +static char ** libxl_build_device_model_args(libxl__gc *gc, + libxl_device_model_info *info, + libxl_device_nic *vifs, + int num_vifs) +{ + libxl_ctx *ctx = libxl__gc_owner(gc); + int new_qemu; + + new_qemu = libxl_check_device_model_version(ctx, info->device_model); + + if (new_qemu == 1) { + return libxl_build_device_model_args_new(gc, info, vifs, num_vifs); + } else { + return libxl_build_device_model_args_old(gc, info, vifs, num_vifs); + } +} + +static void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild) +{ + libxl_device_model_starting *starting = for_spawn; + struct xs_handle *xsh; + char *path = NULL, *pid = NULL; + int len; + + if (asprintf(&path, "%s/%s", starting->dom_path, "image/device-model-pid") < 0) + goto out; + + len = asprintf(&pid, "%d", innerchild); + if (len < 0) + goto out; + + /* we mustn''t use the parent''s handle in the child */ + xsh = xs_daemon_open(); + + xs_write(xsh, XBT_NULL, path, pid, len); + + xs_daemon_close(xsh); +out: + free(path); + free(pid); +} + +static int libxl_vfb_and_vkb_from_device_model_info(libxl_ctx *ctx, + libxl_device_model_info *info, + libxl_device_vfb *vfb, + libxl_device_vkb *vkb) +{ + memset(vfb, 0x00, sizeof(libxl_device_vfb)); + memset(vkb, 0x00, sizeof(libxl_device_vkb)); + + vfb->backend_domid = 0; + vfb->devid = 0; + vfb->vnc = info->vnc; + vfb->vnclisten = info->vnclisten; + vfb->vncdisplay = info->vncdisplay; + vfb->vncunused = info->vncunused; + vfb->vncpasswd = info->vncpasswd; + vfb->keymap = info->keymap; + vfb->sdl = info->sdl; + vfb->opengl = info->opengl; + + vkb->backend_domid = 0; + vkb->devid = 0; + return 0; +} + +static int libxl_write_dmargs(libxl_ctx *ctx, int domid, int guest_domid, char **args) +{ + libxl__gc gc = LIBXL_INIT_GC(ctx); + int i; + char *vm_path; + char *dmargs, *path; + int dmargs_size; + struct xs_permissions roperm[2]; + xs_transaction_t t; + + roperm[0].id = 0; + roperm[0].perms = XS_PERM_NONE; + roperm[1].id = domid; + roperm[1].perms = XS_PERM_READ; + + vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/vm", guest_domid)); + + i = 0; + dmargs_size = 0; + while (args[i] != NULL) { + dmargs_size = dmargs_size + strlen(args[i]) + 1; + i++; + } + dmargs_size++; + dmargs = (char *) malloc(dmargs_size); + i = 1; + dmargs[0] = ''\0''; + while (args[i] != NULL) { + if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) { + strcat(dmargs, " "); + strcat(dmargs, args[i]); + } + i++; + } + path = libxl__sprintf(&gc, "%s/image/dmargs", vm_path); + +retry_transaction: + t = xs_transaction_start(ctx->xsh); + xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs)); + xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm)); + xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm)); + if (!xs_transaction_end(ctx->xsh, t, 0)) + if (errno == EAGAIN) + goto retry_transaction; + free(dmargs); + libxl__free_all(&gc); + return 0; +} + +static int libxl_create_stubdom(libxl_ctx *ctx, + libxl_device_model_info *info, + libxl_device_disk *disks, int num_disks, + libxl_device_nic *vifs, int num_vifs, + libxl_device_vfb *vfb, + libxl_device_vkb *vkb, + libxl_device_model_starting **starting_r) +{ + libxl__gc gc = LIBXL_INIT_GC(ctx); + int i, num_console = 1, ret; + libxl_device_console *console; + libxl_domain_create_info c_info; + libxl_domain_build_info b_info; + libxl_domain_build_state state; + uint32_t domid; + char **args; + struct xs_permissions perm[2]; + xs_transaction_t t; + libxl_device_model_starting *dm_starting = 0; + + args = libxl_build_device_model_args(&gc, info, vifs, num_vifs); + if (!args) { + ret = ERROR_FAIL; + goto out; + } + + memset(&c_info, 0x00, sizeof(libxl_domain_create_info)); + c_info.hvm = 0; + c_info.name = libxl__sprintf(&gc, "%s-dm", libxl__domid_to_name(&gc, info->domid)); + + libxl_uuid_copy(&c_info.uuid, &info->uuid); + + memset(&b_info, 0x00, sizeof(libxl_domain_build_info)); + b_info.max_vcpus = 1; + b_info.max_memkb = 32 * 1024; + b_info.target_memkb = b_info.max_memkb; + b_info.kernel.path = libxl__abs_path(&gc, "ioemu-stubdom.gz", libxl_xenfirmwaredir_path()); + b_info.u.pv.cmdline = libxl__sprintf(&gc, " -d %d", info->domid); + b_info.u.pv.ramdisk.path = ""; + b_info.u.pv.features = ""; + b_info.hvm = 0; + + ret = libxl_domain_make(ctx, &c_info, &domid); + if (ret) + goto out_free; + ret = libxl_domain_build(ctx, &b_info, domid, &state); + if (ret) + goto out_free; + + libxl_write_dmargs(ctx, domid, info->domid, args); + libxl__xs_write(&gc, XBT_NULL, + libxl__sprintf(&gc, "%s/image/device-model-domid", libxl__xs_get_dompath(&gc, info->domid)), + "%d", domid); + libxl__xs_write(&gc, XBT_NULL, + libxl__sprintf(&gc, "%s/target", libxl__xs_get_dompath(&gc, domid)), + "%d", info->domid); + ret = xc_domain_set_target(ctx->xch, domid, info->domid); + if (ret<0) { + LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting target domain %d -> %d", domid, info->domid); + ret = ERROR_FAIL; + goto out_free; + } + xs_set_target(ctx->xsh, domid, info->domid); + + perm[0].id = domid; + perm[0].perms = XS_PERM_NONE; + perm[1].id = info->domid; + perm[1].perms = XS_PERM_READ; +retry_transaction: + t = xs_transaction_start(ctx->xsh); + xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid)); + xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm)); + xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/%d/device/vfs", domid)); + xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm)); + if (!xs_transaction_end(ctx->xsh, t, 0)) + if (errno == EAGAIN) + goto retry_transaction; + + for (i = 0; i < num_disks; i++) { + disks[i].domid = domid; + ret = libxl_device_disk_add(ctx, domid, &disks[i]); + if (ret) + goto out_free; + } + for (i = 0; i < num_vifs; i++) { + vifs[i].domid = domid; + ret = libxl_device_nic_add(ctx, domid, &vifs[i]); + if (ret) + goto out_free; + } + vfb->domid = domid; + ret = libxl_device_vfb_add(ctx, domid, vfb); + if (ret) + goto out_free; + vkb->domid = domid; + ret = libxl_device_vkb_add(ctx, domid, vkb); + if (ret) + goto out_free; + + if (info->serial) + num_console++; + + console = libxl__calloc(&gc, num_console, sizeof(libxl_device_console)); + if (!console) { + ret = ERROR_NOMEM; + goto out_free; + } + + for (i = 0; i < num_console; i++) { + console[i].devid = i; + console[i].consback = LIBXL_CONSBACK_IOEMU; + console[i].domid = domid; + if (!i) { + char *filename; + char *name = libxl__sprintf(&gc, "qemu-dm-%s", libxl_domid_to_name(ctx, info->domid)); + libxl_create_logfile(ctx, name, &filename); + console[i].output = libxl__sprintf(&gc, "file:%s", filename); + console[i].build_state = &state; + free(filename); + } else + console[i].output = "pty"; + ret = libxl_device_console_add(ctx, domid, &console[i]); + if (ret) + goto out_free; + } + if (libxl_create_xenpv_qemu(ctx, domid, vfb, &dm_starting) < 0) { + ret = ERROR_FAIL; + goto out_free; + } + if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) { + ret = ERROR_FAIL; + goto out_free; + } + + libxl_domain_unpause(ctx, domid); + + if (starting_r) { + *starting_r = calloc(sizeof(libxl_device_model_starting), 1); + (*starting_r)->domid = info->domid; + (*starting_r)->dom_path = libxl__xs_get_dompath(&gc, info->domid); + (*starting_r)->for_spawn = NULL; + } + + ret = 0; + +out_free: + free(args); +out: + libxl__free_all(&gc); + return ret; +} + +int libxl_create_device_model(libxl_ctx *ctx, + libxl_device_model_info *info, + libxl_device_disk *disks, int num_disks, + libxl_device_nic *vifs, int num_vifs, + libxl_device_model_starting **starting_r) +{ + libxl__gc gc = LIBXL_INIT_GC(ctx); + char *path, *logfile; + int logfile_w, null; + int rc; + char **args; + libxl_device_model_starting buf_starting, *p; + xs_transaction_t t; + char *vm_path; + char **pass_stuff; + + if (strstr(info->device_model, "stubdom-dm")) { + libxl_device_vfb vfb; + libxl_device_vkb vkb; + + libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb); + rc = libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r); + goto out; + } + + args = libxl_build_device_model_args(&gc, info, vifs, num_vifs); + if (!args) { + rc = ERROR_FAIL; + goto out; + } + + path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid); + xs_mkdir(ctx->xsh, XBT_NULL, path); + libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/disable_pf", path), "%d", !info->xen_platform_pci); + + libxl_create_logfile(ctx, libxl__sprintf(&gc, "qemu-dm-%s", info->dom_name), &logfile); + logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644); + free(logfile); + null = open("/dev/null", O_RDONLY); + + if (starting_r) { + rc = ERROR_NOMEM; + *starting_r = calloc(sizeof(libxl_device_model_starting), 1); + if (!*starting_r) + goto out_close; + p = *starting_r; + p->for_spawn = calloc(sizeof(libxl__spawn_starting), 1); + } else { + p = &buf_starting; + p->for_spawn = NULL; + } + + p->domid = info->domid; + p->dom_path = libxl__xs_get_dompath(&gc, info->domid); + if (!p->dom_path) { + rc = ERROR_FAIL; + goto out_close; + } + + if (info->vncpasswd) { +retry_transaction: + /* Find uuid and the write the vnc password to xenstore for qemu. */ + t = xs_transaction_start(ctx->xsh); + vm_path = libxl__xs_read(&gc,t,libxl__sprintf(&gc, "%s/vm", p->dom_path)); + if (vm_path) { + /* Now write the vncpassword into it. */ + pass_stuff = libxl__calloc(&gc, 3, sizeof(char *)); + pass_stuff[0] = "vncpasswd"; + pass_stuff[1] = info->vncpasswd; + libxl__xs_writev(&gc,t,vm_path,pass_stuff); + if (!xs_transaction_end(ctx->xsh, t, 0)) + if (errno == EAGAIN) + goto retry_transaction; + } + } + + rc = libxl__spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid); + if (rc < 0) + goto out_close; + if (!rc) { /* inner child */ + libxl__exec(null, logfile_w, logfile_w, + libxl__abs_path(&gc, info->device_model, libxl_libexec_path()), + args); + } + + rc = 0; + +out_close: + close(null); + close(logfile_w); + free(args); +out: + libxl__free_all(&gc); + return rc; +} + +int libxl_detach_device_model(libxl_ctx *ctx, + libxl_device_model_starting *starting) +{ + int rc; + rc = libxl__spawn_detach(ctx, starting->for_spawn); + if (starting->for_spawn) + free(starting->for_spawn); + free(starting); + return rc; +} + + +int libxl_confirm_device_model_startup(libxl_ctx *ctx, + libxl_device_model_starting *starting) +{ + int problem = libxl__wait_for_device_model(ctx, starting->domid, "running", NULL, NULL); + int detach; + if ( !problem ) + problem = libxl__spawn_check(ctx, starting->for_spawn); + detach = libxl_detach_device_model(ctx, starting); + return problem ? problem : detach; +} + +int libxl__destroy_device_model(libxl_ctx *ctx, uint32_t domid) +{ + libxl__gc gc = LIBXL_INIT_GC(ctx); + char *pid; + int ret; + + pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/image/device-model-pid", domid)); + if (!pid) { + int stubdomid = libxl_get_stubdom_id(ctx, domid); + 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\n", stubdomid); + ret = libxl_domain_destroy(ctx, stubdomid, 0); + if (ret) + goto out; + } 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; + } + } + xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", domid)); + +out: + libxl__free_all(&gc); + return ret; +} + +static int libxl_build_xenpv_qemu_args(libxl__gc *gc, + uint32_t domid, + libxl_device_vfb *vfb, + libxl_device_model_info *info) +{ + libxl_ctx *ctx = libxl__gc_owner(gc); + memset(info, 0x00, sizeof(libxl_device_model_info)); + + if (vfb != NULL) { + info->vnc = vfb->vnc; + if (vfb->vnclisten) + info->vnclisten = libxl__strdup(gc, vfb->vnclisten); + info->vncdisplay = vfb->vncdisplay; + info->vncunused = vfb->vncunused; + if (vfb->vncpasswd) + info->vncpasswd = vfb->vncpasswd; + if (vfb->keymap) + info->keymap = libxl__strdup(gc, vfb->keymap); + info->sdl = vfb->sdl; + info->opengl = vfb->opengl; + } else + info->nographic = 1; + info->domid = domid; + info->dom_name = libxl_domid_to_name(ctx, domid); + info->device_model = libxl__abs_path(gc, "qemu-dm", libxl_libexec_path()); + info->type = XENPV; + return 0; +} + +int libxl_need_xenpv_qemu(libxl_ctx *ctx, + int nr_consoles, libxl_device_console *consoles, + int nr_vfbs, libxl_device_vfb *vfbs, + int nr_disks, libxl_device_disk *disks) +{ + int i, ret = 0; + libxl__gc gc = LIBXL_INIT_GC(ctx); + + if (nr_consoles > 1) { + ret = 1; + goto out; + } + + for (i = 0; i < nr_consoles; i++) { + if (consoles[i].consback == LIBXL_CONSBACK_IOEMU) { + ret = 1; + goto out; + } + } + + if (nr_vfbs > 0) { + ret = 1; + goto out; + } + + if (nr_disks > 0 && !libxl__blktap_enabled(&gc)) + ret = 1; + +out: + libxl__free_all(&gc); + return ret; +} + +int libxl_create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb, + libxl_device_model_starting **starting_r) +{ + libxl__gc gc = LIBXL_INIT_GC(ctx); + libxl_device_model_info info; + + libxl_build_xenpv_qemu_args(&gc, domid, vfb, &info); + libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r); + libxl__free_all(&gc); + return 0; +} diff -r 82c205df4406 -r 2bde4e48657e tools/libxl/libxl_internal.h --- a/tools/libxl/libxl_internal.h Fri Jan 07 14:13:15 2011 +0000 +++ b/tools/libxl/libxl_internal.h Fri Jan 07 16:05:06 2011 +0000 @@ -204,6 +204,8 @@ _hidden int libxl__spawn_spawn(libxl_ctx libxl_device_model_starting *starting, const char *what, void (*intermediate_hook)(void *for_spawn, pid_t innerchild)); +_hidden int libxl__destroy_device_model(libxl_ctx *ctx, uint32_t domid); + /* Logs errors. A copy of "what" is taken. Return values: * < 0 error, for_spawn need not be detached * +1 caller is the parent, must call detach on *for_spawn eventually _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Gianni Tedesco
2011-Jan-07 16:07 UTC
[Xen-devel] [PATCH 2 of 2] xl: Implement flexarray_append() and flexarray_vappend()
tools/libxl/flexarray.c | 24 +++ tools/libxl/flexarray.h | 3 + tools/libxl/libxl.c | 370 ++++++++++++++++++++++------------------------- tools/libxl/libxl_dm.c | 183 ++++++++++------------- tools/libxl/libxl_pci.c | 73 +++----- 5 files changed, 305 insertions(+), 348 deletions(-) # HG changeset patch # User Gianni Tedesco <gianni.tedesco@citrix.com> # Date 1294416341 0 # Node ID 61dbad8d84446b3969309dda1cf24c7c464a6b24 # Parent 2bde4e48657ec8087778a5924bf2d07f02666c39 xl: Implement flexarray_append() and flexarray_vappend() Makes a lot of code simpler and nicer and saves a fair amount of screen real-estate Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com> diff -r 2bde4e48657e -r 61dbad8d8444 tools/libxl/flexarray.c --- a/tools/libxl/flexarray.c Fri Jan 07 16:05:06 2011 +0000 +++ b/tools/libxl/flexarray.c Fri Jan 07 16:05:41 2011 +0000 @@ -14,6 +14,7 @@ */ #include "libxl_internal.h" +#include <stdarg.h> flexarray_t *flexarray_make(int size, int autogrow) { @@ -21,6 +22,7 @@ flexarray_t *flexarray_make(int size, in if (array) { array->size = size; array->autogrow = autogrow; + array->count = 0; array->data = calloc(size, sizeof(void *)); } return array; @@ -56,10 +58,32 @@ int flexarray_set(flexarray_t *array, un if (flexarray_grow(array, newsize - array->size)) return 2; } + if ( index + 1 > array->count ) + array->count = index + 1; array->data[index] = ptr; return 0; } +int flexarray_append(flexarray_t *array, void *ptr) +{ + return flexarray_set(array, array->count, ptr); +} + +int flexarray_vappend(flexarray_t *array, ...) +{ + va_list va; + void *ptr; + int ret; + + va_start(va, array); + for(ret = 0; (ptr = va_arg(va, void *)); ret++) { + if ( flexarray_append(array, ptr) ) + break; + } + va_end(va); + return ret; +} + int flexarray_get(flexarray_t *array, int index, void **ptr) { if (index >= array->size) diff -r 2bde4e48657e -r 61dbad8d8444 tools/libxl/flexarray.h --- a/tools/libxl/flexarray.h Fri Jan 07 16:05:06 2011 +0000 +++ b/tools/libxl/flexarray.h Fri Jan 07 16:05:41 2011 +0000 @@ -19,6 +19,7 @@ typedef struct flexarray { int size; int autogrow; + unsigned int count; void **data; /* array of pointer */ } flexarray_t; @@ -26,6 +27,8 @@ _hidden flexarray_t *flexarray_make(int _hidden void flexarray_free(flexarray_t *array); _hidden int flexarray_grow(flexarray_t *array, int extents); _hidden int flexarray_set(flexarray_t *array, unsigned int index, void *ptr); +_hidden int flexarray_append(flexarray_t *array, void *ptr); +_hidden int flexarray_vappend(flexarray_t *array, ...); _hidden int flexarray_get(flexarray_t *array, int index, void **ptr); _hidden void **flexarray_contents(flexarray_t *array); diff -r 2bde4e48657e -r 61dbad8d8444 tools/libxl/libxl.c --- a/tools/libxl/libxl.c Fri Jan 07 16:05:06 2011 +0000 +++ b/tools/libxl/libxl.c Fri Jan 07 16:05:41 2011 +0000 @@ -1075,8 +1075,6 @@ int libxl_device_disk_add(libxl_ctx *ctx flexarray_t *front; flexarray_t *back; char *backend_type; - unsigned int boffset = 0; - unsigned int foffset = 0; int devid; libxl__device device; int major, minor, rc; @@ -1111,11 +1109,11 @@ int libxl_device_disk_add(libxl_ctx *ctx case PHYSTYPE_PHY: { libxl__device_physdisk_major_minor(disk->physpath, &major, &minor); - flexarray_set(back, boffset++, "physical-device"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor)); - - flexarray_set(back, boffset++, "params"); - flexarray_set(back, boffset++, disk->physpath); + flexarray_append(back, "physical-device"); + flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor)); + + flexarray_append(back, "params"); + flexarray_append(back, disk->physpath); device.backend_kind = DEVICE_VBD; break; @@ -1134,20 +1132,20 @@ int libxl_device_disk_add(libxl_ctx *ctx rc = ERROR_FAIL; goto out_free; } - flexarray_set(back, boffset++, "tapdisk-params"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath)); - flexarray_set(back, boffset++, "params"); - flexarray_set(back, boffset++, libxl__strdup(&gc, dev)); + flexarray_append(back, "tapdisk-params"); + flexarray_append(back, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath)); + flexarray_append(back, "params"); + flexarray_append(back, libxl__strdup(&gc, dev)); backend_type = "phy"; libxl__device_physdisk_major_minor(dev, &major, &minor); - flexarray_set(back, boffset++, "physical-device"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor)); + flexarray_append(back, "physical-device"); + flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor)); device.backend_kind = DEVICE_VBD; break; } - flexarray_set(back, boffset++, "params"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s", + flexarray_append(back, "params"); + flexarray_append(back, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath)); if (libxl__blktap_enabled(&gc)) @@ -1162,40 +1160,40 @@ int libxl_device_disk_add(libxl_ctx *ctx goto out_free; } - flexarray_set(back, boffset++, "frontend-id"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", disk->domid)); - flexarray_set(back, boffset++, "online"); - flexarray_set(back, boffset++, "1"); - flexarray_set(back, boffset++, "removable"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0)); - flexarray_set(back, boffset++, "bootable"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(back, boffset++, "dev"); - flexarray_set(back, boffset++, disk->virtpath); - flexarray_set(back, boffset++, "type"); - flexarray_set(back, boffset++, backend_type); - flexarray_set(back, boffset++, "mode"); - flexarray_set(back, boffset++, disk->readwrite ? "w" : "r"); - - flexarray_set(front, foffset++, "backend-id"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", disk->backend_domid)); - flexarray_set(front, foffset++, "state"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(front, foffset++, "virtual-device"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", devid)); - flexarray_set(front, foffset++, "device-type"); - flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk"); + flexarray_append(back, "frontend-id"); + flexarray_append(back, libxl__sprintf(&gc, "%d", disk->domid)); + flexarray_append(back, "online"); + flexarray_append(back, "1"); + flexarray_append(back, "removable"); + flexarray_append(back, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0)); + flexarray_append(back, "bootable"); + flexarray_append(back, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(back, "state"); + flexarray_append(back, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(back, "dev"); + flexarray_append(back, disk->virtpath); + flexarray_append(back, "type"); + flexarray_append(back, backend_type); + flexarray_append(back, "mode"); + flexarray_append(back, disk->readwrite ? "w" : "r"); + + flexarray_append(front, "backend-id"); + flexarray_append(front, libxl__sprintf(&gc, "%d", disk->backend_domid)); + flexarray_append(front, "state"); + flexarray_append(front, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(front, "virtual-device"); + flexarray_append(front, libxl__sprintf(&gc, "%d", devid)); + flexarray_append(front, "device-type"); + flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk"); if (0 /* protocol != native*/) { - flexarray_set(front, foffset++, "protocol"); - flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */ + flexarray_append(front, "protocol"); + flexarray_append(front, "x86_32-abi"); /* hardcoded ! */ } libxl__device_generic_add(ctx, &device, - libxl__xs_kvs_of_flexarray(&gc, back, boffset), - libxl__xs_kvs_of_flexarray(&gc, front, foffset)); + libxl__xs_kvs_of_flexarray(&gc, back, back->count), + libxl__xs_kvs_of_flexarray(&gc, front, front->count)); rc = 0; @@ -1274,8 +1272,6 @@ int libxl_device_nic_add(libxl_ctx *ctx, libxl__gc gc = LIBXL_INIT_GC(ctx); flexarray_t *front; flexarray_t *back; - unsigned int boffset = 0; - unsigned int foffset = 0; libxl__device device; char *dompath, **l; unsigned int nb, rc; @@ -1311,41 +1307,41 @@ int libxl_device_nic_add(libxl_ctx *ctx, device.domid = nic->domid; device.kind = DEVICE_VIF; - flexarray_set(back, boffset++, "frontend-id"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->domid)); - flexarray_set(back, boffset++, "online"); - flexarray_set(back, boffset++, "1"); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(back, boffset++, "script"); - flexarray_set(back, boffset++, nic->script); - flexarray_set(back, boffset++, "mac"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", + flexarray_append(back, "frontend-id"); + flexarray_append(back, libxl__sprintf(&gc, "%d", nic->domid)); + flexarray_append(back, "online"); + flexarray_append(back, "1"); + flexarray_append(back, "state"); + flexarray_append(back, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(back, "script"); + flexarray_append(back, nic->script); + flexarray_append(back, "mac"); + flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", nic->mac[0], nic->mac[1], nic->mac[2], nic->mac[3], nic->mac[4], nic->mac[5])); - flexarray_set(back, boffset++, "bridge"); - flexarray_set(back, boffset++, libxl__strdup(&gc, nic->bridge)); - flexarray_set(back, boffset++, "handle"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->devid)); - - flexarray_set(front, foffset++, "backend-id"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->backend_domid)); - flexarray_set(front, foffset++, "state"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(front, foffset++, "handle"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->devid)); - flexarray_set(front, foffset++, "mac"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", + flexarray_append(back, "bridge"); + flexarray_append(back, libxl__strdup(&gc, nic->bridge)); + flexarray_append(back, "handle"); + flexarray_append(back, libxl__sprintf(&gc, "%d", nic->devid)); + + flexarray_append(front, "backend-id"); + flexarray_append(front, libxl__sprintf(&gc, "%d", nic->backend_domid)); + flexarray_append(front, "state"); + flexarray_append(front, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(front, "handle"); + flexarray_append(front, libxl__sprintf(&gc, "%d", nic->devid)); + flexarray_append(front, "mac"); + flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", nic->mac[0], nic->mac[1], nic->mac[2], nic->mac[3], nic->mac[4], nic->mac[5])); if (0 /* protocol != native*/) { - flexarray_set(front, foffset++, "protocol"); - flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */ + flexarray_append(front, "protocol"); + flexarray_append(front, "x86_32-abi"); /* hardcoded ! */ } libxl__device_generic_add(ctx, &device, - libxl__xs_kvs_of_flexarray(&gc, back, boffset), - libxl__xs_kvs_of_flexarray(&gc, front, foffset)); + libxl__xs_kvs_of_flexarray(&gc, back, back->count), + libxl__xs_kvs_of_flexarray(&gc, front, front->count)); /* FIXME: wait for plug */ rc = 0; @@ -1433,7 +1429,6 @@ int libxl_device_net2_add(libxl_ctx *ctx { libxl__gc gc = LIBXL_INIT_GC(ctx); flexarray_t *front, *back; - unsigned int boffset = 0, foffset = 0; libxl__device device; char *dompath, *dom, **l; unsigned int nb; @@ -1472,65 +1467,65 @@ int libxl_device_net2_add(libxl_ctx *ctx device.domid = net2->domid; device.kind = DEVICE_VIF2; - flexarray_set(back, boffset++, "domain"); - flexarray_set(back, boffset++, dom); - flexarray_set(back, boffset++, "frontend-id"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->domid)); - - flexarray_set(back, boffset++, "local-trusted"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->back_trusted)); - flexarray_set(back, boffset++, "mac"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", + flexarray_append(back, "domain"); + flexarray_append(back, dom); + flexarray_append(back, "frontend-id"); + flexarray_append(back, libxl__sprintf(&gc, "%d", net2->domid)); + + flexarray_append(back, "local-trusted"); + flexarray_append(back, libxl__sprintf(&gc, "%d", net2->back_trusted)); + flexarray_append(back, "mac"); + flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", net2->back_mac[0], net2->back_mac[1], net2->back_mac[2], net2->back_mac[3], net2->back_mac[4], net2->back_mac[5])); - flexarray_set(back, boffset++, "remote-trusted"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->trusted)); - flexarray_set(back, boffset++, "remote-mac"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", + flexarray_append(back, "remote-trusted"); + flexarray_append(back, libxl__sprintf(&gc, "%d", net2->trusted)); + flexarray_append(back, "remote-mac"); + flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", net2->front_mac[0], net2->front_mac[1], net2->front_mac[2], net2->front_mac[3], net2->front_mac[4], net2->front_mac[5])); - flexarray_set(back, boffset++, "max-bypasses"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->max_bypasses)); - flexarray_set(back, boffset++, "filter-mac"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac))); - flexarray_set(back, boffset++, "handle"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->devid)); - flexarray_set(back, boffset++, "online"); - flexarray_set(back, boffset++, "1"); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, "1"); - - flexarray_set(front, foffset++, "backend-id"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->backend_domid)); - - flexarray_set(front, foffset++, "local-trusted"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->trusted)); - flexarray_set(front, foffset++, "mac"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", + flexarray_append(back, "max-bypasses"); + flexarray_append(back, libxl__sprintf(&gc, "%d", net2->max_bypasses)); + flexarray_append(back, "filter-mac"); + flexarray_append(back, libxl__sprintf(&gc, "%d", !!(net2->filter_mac))); + flexarray_append(back, "handle"); + flexarray_append(back, libxl__sprintf(&gc, "%d", net2->devid)); + flexarray_append(back, "online"); + flexarray_append(back, "1"); + flexarray_append(back, "state"); + flexarray_append(back, "1"); + + flexarray_append(front, "backend-id"); + flexarray_append(front, libxl__sprintf(&gc, "%d", net2->backend_domid)); + + flexarray_append(front, "local-trusted"); + flexarray_append(front, libxl__sprintf(&gc, "%d", net2->trusted)); + flexarray_append(front, "mac"); + flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", net2->front_mac[0], net2->front_mac[1], net2->front_mac[2], net2->front_mac[3], net2->front_mac[4], net2->front_mac[5])); - flexarray_set(front, foffset++, "remote-trusted"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->back_trusted)); - flexarray_set(front, foffset++, "remote-mac"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", + flexarray_append(front, "remote-trusted"); + flexarray_append(front, libxl__sprintf(&gc, "%d", net2->back_trusted)); + flexarray_append(front, "remote-mac"); + flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x", net2->back_mac[0], net2->back_mac[1], net2->back_mac[2], net2->back_mac[3], net2->back_mac[4], net2->back_mac[5])); - flexarray_set(front, foffset++, "filter-mac"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac))); - flexarray_set(front, foffset++, "state"); - flexarray_set(front, foffset++, "1"); + flexarray_append(front, "filter-mac"); + flexarray_append(front, libxl__sprintf(&gc, "%d", !!(net2->filter_mac))); + flexarray_append(front, "state"); + flexarray_append(front, "1"); libxl__device_generic_add(ctx, &device, - libxl__xs_kvs_of_flexarray(&gc, back, boffset), - libxl__xs_kvs_of_flexarray(&gc, front, foffset)); + libxl__xs_kvs_of_flexarray(&gc, back, back->count), + libxl__xs_kvs_of_flexarray(&gc, front, front->count)); /* FIXME: wait for plug */ rc = 0; @@ -1624,8 +1619,6 @@ int libxl_device_console_add(libxl_ctx * libxl__gc gc = LIBXL_INIT_GC(ctx); flexarray_t *front; flexarray_t *back; - unsigned int boffset = 0; - unsigned int foffset = 0; libxl__device device; int rc; @@ -1647,48 +1640,48 @@ int libxl_device_console_add(libxl_ctx * device.domid = console->domid; device.kind = DEVICE_CONSOLE; - flexarray_set(back, boffset++, "frontend-id"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", console->domid)); - flexarray_set(back, boffset++, "online"); - flexarray_set(back, boffset++, "1"); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(back, boffset++, "domain"); - flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid)); - flexarray_set(back, boffset++, "protocol"); - flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL); - - flexarray_set(front, foffset++, "backend-id"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", console->backend_domid)); - flexarray_set(front, foffset++, "limit"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT)); - flexarray_set(front, foffset++, "type"); + flexarray_append(back, "frontend-id"); + flexarray_append(back, libxl__sprintf(&gc, "%d", console->domid)); + flexarray_append(back, "online"); + flexarray_append(back, "1"); + flexarray_append(back, "state"); + flexarray_append(back, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(back, "domain"); + flexarray_append(back, libxl__domid_to_name(&gc, domid)); + flexarray_append(back, "protocol"); + flexarray_append(back, LIBXL_XENCONSOLE_PROTOCOL); + + flexarray_append(front, "backend-id"); + flexarray_append(front, libxl__sprintf(&gc, "%d", console->backend_domid)); + flexarray_append(front, "limit"); + flexarray_append(front, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT)); + flexarray_append(front, "type"); if (console->consback == LIBXL_CONSBACK_XENCONSOLED) - flexarray_set(front, foffset++, "xenconsoled"); + flexarray_append(front, "xenconsoled"); else - flexarray_set(front, foffset++, "ioemu"); - flexarray_set(front, foffset++, "output"); - flexarray_set(front, foffset++, console->output); + flexarray_append(front, "ioemu"); + flexarray_append(front, "output"); + flexarray_append(front, console->output); if (device.devid == 0) { if (console->build_state == NULL) { rc = ERROR_INVAL; goto out_free; } - flexarray_set(front, foffset++, "port"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port)); - flexarray_set(front, foffset++, "ring-ref"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn)); + flexarray_append(front, "port"); + flexarray_append(front, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port)); + flexarray_append(front, "ring-ref"); + flexarray_append(front, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn)); } else { - flexarray_set(front, foffset++, "state"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(front, foffset++, "protocol"); - flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL); + flexarray_append(front, "state"); + flexarray_append(front, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(front, "protocol"); + flexarray_append(front, LIBXL_XENCONSOLE_PROTOCOL); } libxl__device_generic_add(ctx, &device, - libxl__xs_kvs_of_flexarray(&gc, back, boffset), - libxl__xs_kvs_of_flexarray(&gc, front, foffset)); + libxl__xs_kvs_of_flexarray(&gc, back, back->count), + libxl__xs_kvs_of_flexarray(&gc, front, front->count)); rc = 0; out_free: flexarray_free(back); @@ -1704,8 +1697,6 @@ int libxl_device_vkb_add(libxl_ctx *ctx, libxl__gc gc = LIBXL_INIT_GC(ctx); flexarray_t *front; flexarray_t *back; - unsigned int boffset = 0; - unsigned int foffset = 0; libxl__device device; int rc; @@ -1727,23 +1718,23 @@ int libxl_device_vkb_add(libxl_ctx *ctx, device.domid = vkb->domid; device.kind = DEVICE_VKBD; - flexarray_set(back, boffset++, "frontend-id"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vkb->domid)); - flexarray_set(back, boffset++, "online"); - flexarray_set(back, boffset++, "1"); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(back, boffset++, "domain"); - flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid)); - - flexarray_set(front, foffset++, "backend-id"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vkb->backend_domid)); - flexarray_set(front, foffset++, "state"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(back, "frontend-id"); + flexarray_append(back, libxl__sprintf(&gc, "%d", vkb->domid)); + flexarray_append(back, "online"); + flexarray_append(back, "1"); + flexarray_append(back, "state"); + flexarray_append(back, libxl__sprintf(&gc, "%d", 1)); + flexarray_append(back, "domain"); + flexarray_append(back, libxl__domid_to_name(&gc, domid)); + + flexarray_append(front, "backend-id"); + flexarray_append(front, libxl__sprintf(&gc, "%d", vkb->backend_domid)); + flexarray_append(front, "state"); + flexarray_append(front, libxl__sprintf(&gc, "%d", 1)); libxl__device_generic_add(ctx, &device, - libxl__xs_kvs_of_flexarray(&gc, back, boffset), - libxl__xs_kvs_of_flexarray(&gc, front, foffset)); + libxl__xs_kvs_of_flexarray(&gc, back, back->count), + libxl__xs_kvs_of_flexarray(&gc, front, front->count)); rc = 0; out_free: flexarray_free(back); @@ -1904,8 +1895,6 @@ int libxl_device_vfb_add(libxl_ctx *ctx, libxl__gc gc = LIBXL_INIT_GC(ctx); flexarray_t *front; flexarray_t *back; - unsigned int boffset = 0; - unsigned int foffset = 0; libxl__device device; int rc; @@ -1927,45 +1916,30 @@ int libxl_device_vfb_add(libxl_ctx *ctx, device.domid = vfb->domid; device.kind = DEVICE_VFB; - flexarray_set(back, boffset++, "frontend-id"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->domid)); - flexarray_set(back, boffset++, "online"); - flexarray_set(back, boffset++, "1"); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1)); - flexarray_set(back, boffset++, "domain"); - flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid)); - flexarray_set(back, boffset++, "vnc"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vnc)); - flexarray_set(back, boffset++, "vnclisten"); - flexarray_set(back, boffset++, vfb->vnclisten); - flexarray_set(back, boffset++, "vncpasswd"); - flexarray_set(back, boffset++, vfb->vncpasswd); - flexarray_set(back, boffset++, "vncdisplay"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncdisplay)); - flexarray_set(back, boffset++, "vncunused"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncunused)); - flexarray_set(back, boffset++, "sdl"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->sdl)); - flexarray_set(back, boffset++, "opengl"); - flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->opengl)); + flexarray_vappend(back, "frontend-id", libxl__sprintf(&gc, "%d", vfb->domid), NULL); + flexarray_vappend(back, "online", "1", NULL); + flexarray_vappend(back, "state", libxl__sprintf(&gc, "%d", 1), NULL); + flexarray_vappend(back, "domain", libxl__domid_to_name(&gc, domid), NULL); + flexarray_vappend(back, "vnc", libxl__sprintf(&gc, "%d", vfb->vnc), NULL); + flexarray_vappend(back, "vnclisten", vfb->vnclisten, NULL); + flexarray_vappend(back, "vncpasswd", vfb->vncpasswd, NULL); + flexarray_vappend(back, "vncdisplay", libxl__sprintf(&gc, "%d", vfb->vncdisplay), NULL); + flexarray_vappend(back, "vncunused", libxl__sprintf(&gc, "%d", vfb->vncunused), NULL); + flexarray_vappend(back, "sdl", libxl__sprintf(&gc, "%d", vfb->sdl), NULL); + flexarray_vappend(back, "opengl", libxl__sprintf(&gc, "%d", vfb->opengl), NULL); if (vfb->xauthority) { - flexarray_set(back, boffset++, "xauthority"); - flexarray_set(back, boffset++, vfb->xauthority); + flexarray_vappend(back, "xauthority", vfb->xauthority, NULL); } if (vfb->display) { - flexarray_set(back, boffset++, "display"); - flexarray_set(back, boffset++, vfb->display); + flexarray_vappend(back, "display", vfb->display, NULL); } - flexarray_set(front, foffset++, "backend-id"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vfb->backend_domid)); - flexarray_set(front, foffset++, "state"); - flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1)); + flexarray_vappend(front, "backend-id", libxl__sprintf(&gc, "%d", vfb->backend_domid), NULL); + flexarray_vappend(front, "state", libxl__sprintf(&gc, "%d", 1), NULL); libxl__device_generic_add(ctx, &device, - libxl__xs_kvs_of_flexarray(&gc, back, boffset), - libxl__xs_kvs_of_flexarray(&gc, front, foffset)); + libxl__xs_kvs_of_flexarray(&gc, back, back->count), + libxl__xs_kvs_of_flexarray(&gc, front, front->count)); rc = 0; out_free: flexarray_free(front); diff -r 2bde4e48657e -r 61dbad8d8444 tools/libxl/libxl_dm.c --- a/tools/libxl/libxl_dm.c Fri Jan 07 16:05:06 2011 +0000 +++ b/tools/libxl/libxl_dm.c Fri Jan 07 16:05:41 2011 +0000 @@ -32,101 +32,88 @@ static char ** libxl_build_device_model_ libxl_device_nic *vifs, int num_vifs) { - int num = 0, i; + int i; flexarray_t *dm_args; dm_args = flexarray_make(16, 1); if (!dm_args) return NULL; - flexarray_set(dm_args, num++, "qemu-dm"); - flexarray_set(dm_args, num++, "-d"); + flexarray_vappend(dm_args, "qemu-dm", "-d", libxl__sprintf(gc, "%d", info->domid), NULL); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid)); + if (info->dom_name) + flexarray_vappend(dm_args, "-domain-name", info->dom_name, NULL); - if (info->dom_name) { - flexarray_set(dm_args, num++, "-domain-name"); - flexarray_set(dm_args, num++, info->dom_name); - } if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) { - flexarray_set(dm_args, num++, "-vnc"); + flexarray_append(dm_args, "-vnc"); if (info->vncdisplay) { if (info->vnclisten && strchr(info->vnclisten, '':'') == NULL) { - flexarray_set( - dm_args, num++, + flexarray_append(dm_args, libxl__sprintf(gc, "%s:%d%s", info->vnclisten, info->vncdisplay, info->vncpasswd ? ",password" : "")); } else { - flexarray_set(dm_args, num++, libxl__sprintf(gc, "127.0.0.1:%d", info->vncdisplay)); + flexarray_append(dm_args, libxl__sprintf(gc, "127.0.0.1:%d", info->vncdisplay)); } } else if (info->vnclisten) { if (strchr(info->vnclisten, '':'') != NULL) { - flexarray_set(dm_args, num++, info->vnclisten); + flexarray_append(dm_args, info->vnclisten); } else { - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%s:0", info->vnclisten)); + flexarray_append(dm_args, libxl__sprintf(gc, "%s:0", info->vnclisten)); } } else { - flexarray_set(dm_args, num++, "127.0.0.1:0"); + flexarray_append(dm_args, "127.0.0.1:0"); } if (info->vncunused) { - flexarray_set(dm_args, num++, "-vncunused"); + flexarray_append(dm_args, "-vncunused"); } } if (info->sdl) { - flexarray_set(dm_args, num++, "-sdl"); + flexarray_append(dm_args, "-sdl"); if (!info->opengl) { - flexarray_set(dm_args, num++, "-disable-opengl"); + flexarray_append(dm_args, "-disable-opengl"); } } if (info->keymap) { - flexarray_set(dm_args, num++, "-k"); - flexarray_set(dm_args, num++, info->keymap); + flexarray_vappend(dm_args, "-k", info->keymap, NULL); } if (info->nographic && (!info->sdl && !info->vnc)) { - flexarray_set(dm_args, num++, "-nographic"); + flexarray_append(dm_args, "-nographic"); } if (info->serial) { - flexarray_set(dm_args, num++, "-serial"); - flexarray_set(dm_args, num++, info->serial); + flexarray_vappend(dm_args, "-serial", info->serial, NULL); } if (info->type == XENFV) { int ioemu_vifs = 0; if (info->videoram) { - flexarray_set(dm_args, num++, "-videoram"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->videoram)); + flexarray_vappend(dm_args, "-videoram", libxl__sprintf(gc, "%d", info->videoram), NULL); } if (info->stdvga) { - flexarray_set(dm_args, num++, "-std-vga"); + flexarray_append(dm_args, "-std-vga"); } if (info->boot) { - flexarray_set(dm_args, num++, "-boot"); - flexarray_set(dm_args, num++, info->boot); + flexarray_vappend(dm_args, "-boot", info->boot, NULL); } if (info->usb || info->usbdevice) { - flexarray_set(dm_args, num++, "-usb"); + flexarray_append(dm_args, "-usb"); if (info->usbdevice) { - flexarray_set(dm_args, num++, "-usbdevice"); - flexarray_set(dm_args, num++, info->usbdevice); + flexarray_vappend(dm_args, "-usbdevice", info->usbdevice, NULL); } } if (info->soundhw) { - flexarray_set(dm_args, num++, "-soundhw"); - flexarray_set(dm_args, num++, info->soundhw); + flexarray_vappend(dm_args, "-soundhw", info->soundhw, NULL); } if (info->apic) { - flexarray_set(dm_args, num++, "-acpi"); + flexarray_append(dm_args, "-acpi"); } if (info->vcpus > 1) { - flexarray_set(dm_args, num++, "-vcpus"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus)); + flexarray_vappend(dm_args, "-vcpus", libxl__sprintf(gc, "%d", info->vcpus), NULL); } if (info->vcpu_avail) { - flexarray_set(dm_args, num++, "-vcpu_avail"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "0x%x", info->vcpu_avail)); + flexarray_vappend(dm_args, "-vcpu_avail", libxl__sprintf(gc, "0x%x", info->vcpu_avail), NULL); } for (i = 0; i < num_vifs; i++) { if (vifs[i].nictype == NICTYPE_IOEMU) { @@ -138,33 +125,30 @@ static char ** libxl_build_device_model_ ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid); else ifname = vifs[i].ifname; - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", - vifs[i].devid, smac, vifs[i].model)); - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=no", - vifs[i].devid, ifname, vifs[i].bridge)); + flexarray_vappend(dm_args, + "-net", libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", + vifs[i].devid, smac, vifs[i].model), + "-net", libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=no", + vifs[i].devid, ifname, vifs[i].bridge), NULL); ioemu_vifs++; } } /* If we have no emulated nics, tell qemu not to create any */ if ( ioemu_vifs == 0 ) { - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, "none"); + flexarray_vappend(dm_args, "-net", "none", NULL); } } if (info->saved_state) { - flexarray_set(dm_args, num++, "-loadvm"); - flexarray_set(dm_args, num++, info->saved_state); + flexarray_vappend(dm_args, "-loadvm", info->saved_state, NULL); } for (i = 0; info->extra && info->extra[i] != NULL; i++) - flexarray_set(dm_args, num++, info->extra[i]); - flexarray_set(dm_args, num++, "-M"); + flexarray_append(dm_args, info->extra[i]); + flexarray_append(dm_args, "-M"); if (info->type == XENPV) - flexarray_set(dm_args, num++, "xenpv"); + flexarray_append(dm_args, "xenpv"); else - flexarray_set(dm_args, num++, "xenfv"); - flexarray_set(dm_args, num++, NULL); + flexarray_append(dm_args, "xenfv"); + flexarray_append(dm_args, NULL); return (char **) flexarray_contents(dm_args); } @@ -173,33 +157,29 @@ static char ** libxl_build_device_model_ libxl_device_nic *vifs, int num_vifs) { - int num = 0, i; flexarray_t *dm_args; - int nb; libxl_device_disk *disks; + int nb, i; dm_args = flexarray_make(16, 1); if (!dm_args) return NULL; - flexarray_set(dm_args, num++, libxl__strdup(gc, info->device_model)); - - flexarray_set(dm_args, num++, "-xen-domid"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid)); + flexarray_vappend(dm_args, libxl__strdup(gc, info->device_model), + "-xen-domid", libxl__sprintf(gc, "%d", info->domid), NULL); if (info->type == XENPV) { - flexarray_set(dm_args, num++, "-xen-attach"); + flexarray_append(dm_args, "-xen-attach"); } if (info->dom_name) { - flexarray_set(dm_args, num++, "-name"); - flexarray_set(dm_args, num++, info->dom_name); + flexarray_vappend(dm_args, "-name", info->dom_name, NULL); } if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) { int display = 0; const char *listen = "127.0.0.1"; - flexarray_set(dm_args, num++, "-vnc"); + flexarray_append(dm_args, "-vnc"); if (info->vncdisplay) { display = info->vncdisplay; @@ -211,66 +191,59 @@ static char ** libxl_build_device_model_ } if (strchr(listen, '':'') != NULL) - flexarray_set(dm_args, num++, + flexarray_append(dm_args, libxl__sprintf(gc, "%s%s", listen, info->vncunused ? ",to=99" : "")); else - flexarray_set(dm_args, num++, + flexarray_append(dm_args, libxl__sprintf(gc, "%s:%d%s", listen, display, info->vncunused ? ",to=99" : "")); } if (info->sdl) { - flexarray_set(dm_args, num++, "-sdl"); + flexarray_append(dm_args, "-sdl"); } if (info->type == XENPV && !info->nographic) { - flexarray_set(dm_args, num++, "-vga"); - flexarray_set(dm_args, num++, "xenfb"); + flexarray_vappend(dm_args, "-vga", "xenfb", NULL); } if (info->keymap) { - flexarray_set(dm_args, num++, "-k"); - flexarray_set(dm_args, num++, info->keymap); + flexarray_vappend(dm_args, "-k", info->keymap, NULL); } if (info->nographic && (!info->sdl && !info->vnc)) { - flexarray_set(dm_args, num++, "-nographic"); + flexarray_append(dm_args, "-nographic"); } if (info->serial) { - flexarray_set(dm_args, num++, "-serial"); - flexarray_set(dm_args, num++, info->serial); + flexarray_vappend(dm_args, "-serial", info->serial, NULL); } if (info->type == XENFV) { int ioemu_vifs = 0; if (info->stdvga) { - flexarray_set(dm_args, num++, "-vga"); - flexarray_set(dm_args, num++, "std"); + flexarray_vappend(dm_args, "-vga", "std", NULL); } if (info->boot) { - flexarray_set(dm_args, num++, "-boot"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "order=%s", info->boot)); + flexarray_vappend(dm_args, "-boot", libxl__sprintf(gc, "order=%s", info->boot), NULL); } if (info->usb || info->usbdevice) { - flexarray_set(dm_args, num++, "-usb"); + flexarray_append(dm_args, "-usb"); if (info->usbdevice) { - flexarray_set(dm_args, num++, "-usbdevice"); - flexarray_set(dm_args, num++, info->usbdevice); + flexarray_vappend(dm_args, "-usbdevice", info->usbdevice, NULL); } } if (info->soundhw) { - flexarray_set(dm_args, num++, "-soundhw"); - flexarray_set(dm_args, num++, info->soundhw); + flexarray_vappend(dm_args, "-soundhw", info->soundhw, NULL); } if (!info->apic) { - flexarray_set(dm_args, num++, "-no-acpi"); + flexarray_append(dm_args, "-no-acpi"); } if (info->vcpus > 1) { - flexarray_set(dm_args, num++, "-smp"); + flexarray_append(dm_args, "-smp"); if (info->vcpu_avail) - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail)); + flexarray_append(dm_args, libxl__sprintf(gc, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail)); else - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus)); + flexarray_append(dm_args, libxl__sprintf(gc, "%d", info->vcpus)); } for (i = 0; i < num_vifs; i++) { if (vifs[i].nictype == NICTYPE_IOEMU) { @@ -283,52 +256,52 @@ static char ** libxl_build_device_model_ } else { ifname = vifs[i].ifname; } - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", + flexarray_append(dm_args, "-net"); + flexarray_append(dm_args, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s", vifs[i].devid, smac, vifs[i].model)); - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,script=no", + flexarray_append(dm_args, "-net"); + flexarray_append(dm_args, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,script=no", vifs[i].devid, ifname)); ioemu_vifs++; } } /* If we have no emulated nics, tell qemu not to create any */ if ( ioemu_vifs == 0 ) { - flexarray_set(dm_args, num++, "-net"); - flexarray_set(dm_args, num++, "none"); + flexarray_append(dm_args, "-net"); + flexarray_append(dm_args, "none"); } } if (info->saved_state) { - flexarray_set(dm_args, num++, "-loadvm"); - flexarray_set(dm_args, num++, info->saved_state); + flexarray_append(dm_args, "-loadvm"); + flexarray_append(dm_args, info->saved_state); } for (i = 0; info->extra && info->extra[i] != NULL; i++) - flexarray_set(dm_args, num++, info->extra[i]); - flexarray_set(dm_args, num++, "-M"); + flexarray_append(dm_args, info->extra[i]); + flexarray_append(dm_args, "-M"); if (info->type == XENPV) - flexarray_set(dm_args, num++, "xenpv"); + flexarray_append(dm_args, "xenpv"); else - flexarray_set(dm_args, num++, "xenfv"); + flexarray_append(dm_args, "xenfv"); /* RAM Size */ - flexarray_set(dm_args, num++, "-m"); - flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->target_ram)); + flexarray_append(dm_args, "-m"); + flexarray_append(dm_args, libxl__sprintf(gc, "%d", info->target_ram)); if (info->type == XENFV) { disks = libxl_device_disk_list(libxl__gc_owner(gc), info->domid, &nb); for (i; i < nb; i++) { if (disks[i].is_cdrom) { - flexarray_set(dm_args, num++, "-cdrom"); - flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath)); + flexarray_append(dm_args, "-cdrom"); + flexarray_append(dm_args, libxl__strdup(gc, disks[i].physpath)); } else { - flexarray_set(dm_args, num++, libxl__sprintf(gc, "-%s", disks[i].virtpath)); - flexarray_set(dm_args, num++, libxl__strdup(gc, disks[i].physpath)); + flexarray_append(dm_args, libxl__sprintf(gc, "-%s", disks[i].virtpath)); + flexarray_append(dm_args, libxl__strdup(gc, disks[i].physpath)); } libxl_device_disk_destroy(&disks[i]); } free(disks); } - flexarray_set(dm_args, num++, NULL); + flexarray_append(dm_args, NULL); return (char **) flexarray_contents(dm_args); } diff -r 2bde4e48657e -r 61dbad8d8444 tools/libxl/libxl_pci.c --- a/tools/libxl/libxl_pci.c Fri Jan 07 16:05:06 2011 +0000 +++ b/tools/libxl/libxl_pci.c Fri Jan 07 16:05:41 2011 +0000 @@ -226,8 +226,6 @@ static int libxl_create_pci_backend(libx libxl_ctx *ctx = libxl__gc_owner(gc); flexarray_t *front; flexarray_t *back; - unsigned int boffset = 0; - unsigned int foffset = 0; libxl__device device; int i; @@ -248,39 +246,28 @@ static int libxl_create_pci_backend(libx device.domid = domid; device.kind = DEVICE_PCI; - flexarray_set(back, boffset++, "frontend-id"); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", domid)); - flexarray_set(back, boffset++, "online"); - flexarray_set(back, boffset++, "1"); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 1)); - flexarray_set(back, boffset++, "domain"); - flexarray_set(back, boffset++, libxl__domid_to_name(gc, domid)); + flexarray_vappend(back, "frontend-id", libxl__sprintf(gc, "%d", domid), + "online", "1", "state", libxl__sprintf(gc, "%d", 1), + "domain", libxl__domid_to_name(gc, domid), NULL); for (i = 0; i < num; i++) { - flexarray_set(back, boffset++, libxl__sprintf(gc, "key-%d", i)); - flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "dev-%d", i)); - flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); + flexarray_append(back, libxl__sprintf(gc, "key-%d", i)); + flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); + flexarray_append(back, libxl__sprintf(gc, "dev-%d", i)); + flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); if (pcidev->vdevfn) { - flexarray_set(back, boffset++, libxl__sprintf(gc, "vdevfn-%d", i)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%x", pcidev->vdevfn)); + flexarray_vappend(back, libxl__sprintf(gc, "vdevfn-%d", i), libxl__sprintf(gc, "%x", pcidev->vdevfn), NULL); } - flexarray_set(back, boffset++, libxl__sprintf(gc, "opts-%d", i)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "state-%d", i)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 1)); + flexarray_append(back, libxl__sprintf(gc, "opts-%d", i)); + flexarray_append(back, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt)); + flexarray_vappend(back, libxl__sprintf(gc, "state-%d", i), libxl__sprintf(gc, "%d", 1), NULL); } - flexarray_set(back, boffset++, "num_devs"); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", num)); - - flexarray_set(front, foffset++, "backend-id"); - flexarray_set(front, foffset++, libxl__sprintf(gc, "%d", 0)); - flexarray_set(front, foffset++, "state"); - flexarray_set(front, foffset++, libxl__sprintf(gc, "%d", 1)); + flexarray_vappend(back, "num_devs", libxl__sprintf(gc, "%d", num), + "backend-id", libxl__sprintf(gc, "%d", 0), + "state", libxl__sprintf(gc, "%d", 1), NULL); libxl__device_generic_add(ctx, &device, - libxl__xs_kvs_of_flexarray(gc, back, boffset), - libxl__xs_kvs_of_flexarray(gc, front, foffset)); + libxl__xs_kvs_of_flexarray(gc, back, back->count), + libxl__xs_kvs_of_flexarray(gc, front, front->count)); flexarray_free(back); flexarray_free(front); @@ -293,7 +280,6 @@ static int libxl_device_pci_add_xenstore flexarray_t *back; char *num_devs, *be_path; int num = 0; - unsigned int boffset = 0; xs_transaction_t t; be_path = libxl__sprintf(gc, "%s/backend/pci/%d/0", libxl__xs_get_dompath(gc, 0), domid); @@ -312,27 +298,24 @@ static int libxl_device_pci_add_xenstore LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Adding new pci device to xenstore"); num = atoi(num_devs); - flexarray_set(back, boffset++, libxl__sprintf(gc, "key-%d", num)); - flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "dev-%d", num)); - flexarray_set(back, boffset++, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); + flexarray_append(back, libxl__sprintf(gc, "key-%d", num)); + flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); + flexarray_append(back, libxl__sprintf(gc, "dev-%d", num)); + flexarray_append(back, libxl__sprintf(gc, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func)); if (pcidev->vdevfn) { - flexarray_set(back, boffset++, libxl__sprintf(gc, "vdevfn-%d", num)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%x", pcidev->vdevfn)); + flexarray_append(back, libxl__sprintf(gc, "vdevfn-%d", num)); + flexarray_append(back, libxl__sprintf(gc, "%x", pcidev->vdevfn)); } - flexarray_set(back, boffset++, libxl__sprintf(gc, "opts-%d", num)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "state-%d", num)); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 1)); - flexarray_set(back, boffset++, "num_devs"); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", num + 1)); - flexarray_set(back, boffset++, "state"); - flexarray_set(back, boffset++, libxl__sprintf(gc, "%d", 7)); + flexarray_append(back, libxl__sprintf(gc, "opts-%d", num)); + flexarray_append(back, libxl__sprintf(gc, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt)); + flexarray_vappend(back, libxl__sprintf(gc, "state-%d", num), libxl__sprintf(gc, "%d", 1), NULL); + flexarray_vappend(back, "num_devs", libxl__sprintf(gc, "%d", num + 1), NULL); + flexarray_vappend(back, "state", libxl__sprintf(gc, "%d", 7), NULL); retry_transaction: t = xs_transaction_start(ctx->xsh); libxl__xs_writev(gc, t, be_path, - libxl__xs_kvs_of_flexarray(gc, back, boffset)); + libxl__xs_kvs_of_flexarray(gc, back, back->count)); if (!xs_transaction_end(ctx->xsh, t, 0)) if (errno == EAGAIN) goto retry_transaction; _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Ian Jackson
2011-Jan-07 17:56 UTC
[Xen-devel] Re: [PATCH 1 of 2] xl: Move device model functions in to a separate file
Gianni Tedesco writes ("[PATCH 1 of 2] xl: Move device model functions in to a separate file"):> xl: Move device model functions in to a separate fileDoes not apply to current staging tip, I''m afraid ... Ian. _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Ian Jackson
2011-Jan-11 16:16 UTC
Re: [Xen-devel] [PATCH 0 of 2] xl: code motion and flexarray append
Gianni Tedesco writes ("[Xen-devel] [PATCH 0 of 2] xl: code motion and flexarray append"):> This is version 3 of ''split dm functions'' to separate file and ''flexarray > append''. Changes since v2 is that patches are re-ordered and code is rebased.Thanks. As we discussed, I applied these changes a day or two ago, since while we are in feature freeze, the first versions of your series were posted before the freeze deadline at the end of December. Ian. _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel