debuggers.hg

annotate tools/libxl/libxl.c @ 21378:3b6e33ebb57b

xl: ignore opengl unless sdl is enabled

The "opengl" setting is documented as having no effect unless "sdl" is
set. Make sure this is honoured.

Signed-off-by: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon May 10 09:21:58 2010 +0100 (2010-05-10)
parents a97ef0eb0b9d
children 4ede18e45935
rev   line source
keir@20462 1 /*
keir@20462 2 * Copyright (C) 2009 Citrix Ltd.
keir@20462 3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
keir@20462 4 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
keir@20462 5 *
keir@20462 6 * This program is free software; you can redistribute it and/or modify
keir@20462 7 * it under the terms of the GNU Lesser General Public License as published
keir@20462 8 * by the Free Software Foundation; version 2.1 only. with the special
keir@20462 9 * exception on linking described in file LICENSE.
keir@20462 10 *
keir@20462 11 * This program is distributed in the hope that it will be useful,
keir@20462 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
keir@20462 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
keir@20462 14 * GNU Lesser General Public License for more details.
keir@20462 15 */
keir@20462 16
keir@20513 17 #include "libxl_osdeps.h"
keir@20513 18
keir@20462 19 #include <stdio.h>
keir@20462 20 #include <string.h>
keir@20462 21 #include <stdlib.h>
keir@20462 22 #include <sys/types.h>
keir@20462 23 #include <fcntl.h>
keir@20462 24 #include <sys/select.h>
keir@20514 25 #include <sys/wait.h>
keir@20462 26 #include <signal.h>
keir@20468 27 #include <unistd.h> /* for write, unlink and close */
keir@20509 28 #include <stdint.h>
keir@20509 29 #include <inttypes.h>
keir@20514 30 #include <assert.h>
keir@20513 31
keir@20462 32 #include "libxl.h"
keir@20462 33 #include "libxl_utils.h"
keir@20462 34 #include "libxl_internal.h"
keir@20462 35 #include "flexarray.h"
keir@20462 36
keir@20860 37 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
keir@20860 38
keir@20788 39 int libxl_ctx_init(struct libxl_ctx *ctx, int version)
keir@20462 40 {
keir@20788 41 if (version != LIBXL_VERSION)
keir@20788 42 return ERROR_VERSION;
keir@20462 43 memset(ctx, 0, sizeof(struct libxl_ctx));
keir@20462 44 ctx->alloc_maxsize = 256;
keir@20462 45 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
keir@20462 46 if (!ctx->alloc_ptrs)
keir@20462 47 return ERROR_NOMEM;
keir@21266 48 memset(&ctx->version_info, 0, sizeof(libxl_version_info));
keir@20462 49
keir@20462 50 ctx->xch = xc_interface_open();
keir@20819 51 if (ctx->xch == -1) {
keir@20819 52 free(ctx->alloc_ptrs);
keir@20819 53 return ERROR_FAIL;
keir@20819 54 }
keir@20819 55
keir@20462 56 ctx->xsh = xs_daemon_open();
keir@20819 57 if (!ctx->xsh) {
keir@20819 58 xc_interface_close(ctx->xch);
keir@20819 59 free(ctx->alloc_ptrs);
keir@20819 60 return ERROR_FAIL;
keir@20819 61 }
keir@20462 62 return 0;
keir@20462 63 }
keir@20462 64
keir@20462 65 int libxl_ctx_free(struct libxl_ctx *ctx)
keir@20462 66 {
keir@20462 67 libxl_free_all(ctx);
keir@20462 68 free(ctx->alloc_ptrs);
keir@20462 69 xc_interface_close(ctx->xch);
keir@21186 70 if (ctx->xsh) xs_daemon_close(ctx->xsh);
keir@20462 71 return 0;
keir@20462 72 }
keir@20462 73
keir@20462 74 int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, void *log_data)
keir@20462 75 {
keir@20462 76 ctx->log_callback = log_callback;
keir@20462 77 ctx->log_userdata = log_data;
keir@20462 78 return 0;
keir@20462 79 }
keir@20462 80
keir@20462 81 /******************************************************************************/
keir@20462 82
keir@20462 83 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
keir@20462 84 uint32_t *domid)
keir@20462 85 {
keir@21184 86 int flags, ret, i, rc;
keir@20468 87 char *uuid_string;
keir@20646 88 char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
keir@20462 89 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
keir@20646 90 "control", "attr", "messages" };
keir@20462 91 char *dom_path, *vm_path, *vss_path;
keir@20462 92 struct xs_permissions roperm[2];
keir@20462 93 struct xs_permissions rwperm[1];
keir@20462 94 xs_transaction_t t;
keir@20468 95 xen_domain_handle_t handle;
keir@20462 96
keir@21188 97 uuid_string = libxl_uuid2string(ctx, info->uuid);
keir@21188 98 if (!uuid_string) return ERROR_NOMEM;
keir@20462 99
keir@20462 100 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
keir@20462 101 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
keir@21274 102 flags |= info->oos ? 0 : XEN_DOMCTL_CDF_oos_off;
keir@20824 103 *domid = -1;
keir@20462 104
keir@20580 105 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
keir@20580 106 memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
keir@20468 107
keir@20468 108 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
keir@20462 109 if (ret < 0) {
keir@20512 110 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
keir@20462 111 return ERROR_FAIL;
keir@20462 112 }
keir@20462 113
keir@21368 114 ret = xc_cpupool_movedomain(ctx->xch, info->poolid, *domid);
keir@21368 115 if (ret < 0) {
keir@21368 116 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain move fail");
keir@21368 117 return ERROR_FAIL;
keir@21368 118 }
keir@21368 119
keir@20462 120 dom_path = libxl_xs_get_dompath(ctx, *domid);
keir@20512 121 if (!dom_path)
keir@20512 122 return ERROR_FAIL;
keir@20512 123
keir@20462 124 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
keir@20462 125 vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
keir@20512 126 if (!vm_path || !vss_path) {
keir@20462 127 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
keir@20462 128 return ERROR_FAIL;
keir@20462 129 }
keir@20462 130
keir@20462 131 roperm[0].id = 0;
keir@20462 132 roperm[0].perms = XS_PERM_NONE;
keir@20462 133 roperm[1].id = *domid;
keir@20462 134 roperm[1].perms = XS_PERM_READ;
keir@20462 135 rwperm[0].id = *domid;
keir@20462 136 rwperm[0].perms = XS_PERM_NONE;
keir@20462 137
keir@20462 138 retry_transaction:
keir@20462 139 t = xs_transaction_start(ctx->xsh);
keir@20462 140 xs_rm(ctx->xsh, t, dom_path);
keir@20462 141 xs_mkdir(ctx->xsh, t, dom_path);
keir@20462 142 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
keir@20462 143
keir@20462 144 xs_rm(ctx->xsh, t, vm_path);
keir@20462 145 xs_mkdir(ctx->xsh, t, vm_path);
keir@20462 146 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
keir@20462 147
keir@20462 148 xs_rm(ctx->xsh, t, vss_path);
keir@20462 149 xs_mkdir(ctx->xsh, t, vss_path);
keir@20462 150 xs_set_permissions(ctx->xsh, t, vss_path, rwperm, ARRAY_SIZE(rwperm));
keir@20462 151
keir@20462 152 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
keir@20462 153 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
keir@21184 154 rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
keir@21184 155 if (rc) return rc;
keir@20462 156
keir@20462 157 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
keir@20462 158 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
keir@20462 159 xs_mkdir(ctx->xsh, t, path);
keir@20462 160 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
keir@20462 161 libxl_free(ctx, path);
keir@20462 162 }
keir@20462 163 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
keir@20462 164 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
keir@20462 165 xs_mkdir(ctx->xsh, t, path);
keir@20462 166 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20462 167 libxl_free(ctx, path);
keir@20462 168 }
keir@20462 169
keir@20462 170 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
keir@20462 171 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
keir@21368 172 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/pool_name", vm_path), info->poolname, strlen(info->poolname));
keir@20462 173
keir@20462 174 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
keir@20462 175 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
keir@20462 176
keir@20462 177 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
keir@20462 178
keir@20462 179 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20462 180 if (errno == EAGAIN)
keir@20462 181 goto retry_transaction;
keir@20462 182 return 0;
keir@20462 183 }
keir@20462 184
keir@21184 185 int libxl_domain_rename(struct libxl_ctx *ctx, uint32_t domid,
keir@21184 186 const char *old_name, const char *new_name,
keir@21184 187 xs_transaction_t trans) {
keir@21184 188 char *dom_path = 0;
keir@21184 189 const char *name_path;
keir@21184 190 char *got_old_name;
keir@21184 191 unsigned int got_old_len;
keir@21184 192 xs_transaction_t our_trans = 0;
keir@21184 193 int rc;
keir@21184 194
keir@21184 195 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@21184 196 if (!dom_path) goto x_nomem;
keir@21184 197
keir@21184 198 name_path= libxl_sprintf(ctx, "%s/name", dom_path);
keir@21184 199 if (!name_path) goto x_nomem;
keir@21184 200
keir@21184 201 retry_transaction:
keir@21184 202 if (!trans) {
keir@21184 203 trans = our_trans = xs_transaction_start(ctx->xsh);
keir@21184 204 if (!our_trans) {
keir@21184 205 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno,
keir@21184 206 "create xs transaction for domain (re)name");
keir@21184 207 goto x_fail;
keir@21184 208 }
keir@21184 209 }
keir@21184 210
keir@21184 211 if (old_name) {
keir@21184 212 got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len);
keir@21184 213 if (!got_old_name) {
keir@21184 214 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno, "check old name"
keir@21184 215 " for domain %"PRIu32" allegedly named `%s'",
keir@21184 216 domid, old_name);
keir@21184 217 goto x_fail;
keir@21184 218 }
keir@21184 219 if (strcmp(old_name, got_old_name)) {
keir@21184 220 XL_LOG(ctx, XL_LOG_ERROR, "domain %"PRIu32" allegedly named "
keir@21184 221 "`%s' is actually named `%s' - racing ?",
keir@21184 222 domid, old_name, got_old_name);
keir@21184 223 free(got_old_name);
keir@21184 224 goto x_fail;
keir@21184 225 }
keir@21184 226 free(got_old_name);
keir@21184 227 }
keir@21184 228 if (!xs_write(ctx->xsh, trans, name_path,
keir@21184 229 new_name, strlen(new_name))) {
keir@21184 230 XL_LOG(ctx, XL_LOG_ERROR, "failed to write new name `%s'"
keir@21184 231 " for domain %"PRIu32" previously named `%s'",
keir@21184 232 domid, new_name, old_name);
keir@21184 233 goto x_fail;
keir@21184 234 }
keir@21184 235
keir@21184 236 if (our_trans) {
keir@21184 237 if (!xs_transaction_end(ctx->xsh, our_trans, 0)) {
keir@21184 238 trans = our_trans = 0;
keir@21184 239 if (errno != EAGAIN) {
keir@21184 240 XL_LOG(ctx, XL_LOG_ERROR, "failed to commit new name `%s'"
keir@21184 241 " for domain %"PRIu32" previously named `%s'",
keir@21184 242 domid, new_name, old_name);
keir@21184 243 goto x_fail;
keir@21184 244 }
keir@21184 245 XL_LOG(ctx, XL_LOG_DEBUG, "need to retry rename transaction"
keir@21184 246 " for domain %"PRIu32" (name_path=\"%s\", new_name=\"%s\")",
keir@21184 247 domid, name_path, new_name);
keir@21184 248 goto retry_transaction;
keir@21184 249 }
keir@21184 250 our_trans = 0;
keir@21184 251 }
keir@21184 252
keir@21184 253 rc = 0;
keir@21184 254 x_rc:
keir@21184 255 if (dom_path) libxl_free(ctx, dom_path);
keir@21184 256 if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1);
keir@21184 257 return rc;
keir@21184 258
keir@21184 259 x_fail: rc = ERROR_FAIL; goto x_rc;
keir@21184 260 x_nomem: rc = ERROR_NOMEM; goto x_rc;
keir@21184 261 }
keir@21184 262
keir@20516 263 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
keir@20462 264 {
keir@20462 265 char **vments = NULL, **localents = NULL;
keir@20829 266 int i, ret;
keir@20829 267
keir@20829 268 ret = build_pre(ctx, domid, info, state);
keir@20829 269 if (ret) goto out;
keir@20462 270
keir@20462 271 if (info->hvm) {
keir@20829 272 ret = build_hvm(ctx, domid, info, state);
keir@20829 273 if (ret) goto out;
keir@20829 274
keir@20509 275 vments = libxl_calloc(ctx, 5, sizeof(char *));
keir@20516 276 vments[0] = "rtc/timeoffset";
keir@20516 277 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
keir@20516 278 vments[2] = "image/ostype";
keir@20516 279 vments[3] = "hvm";
keir@20462 280 } else {
keir@20829 281 ret = build_pv(ctx, domid, info, state);
keir@20829 282 if (ret) goto out;
keir@20829 283
keir@20509 284 vments = libxl_calloc(ctx, 9, sizeof(char *));
keir@20578 285 i = 0;
keir@20578 286 vments[i++] = "image/ostype";
keir@20578 287 vments[i++] = "linux";
keir@20578 288 vments[i++] = "image/kernel";
keir@20578 289 vments[i++] = (char*) info->kernel;
keir@20578 290 if (info->u.pv.ramdisk) {
keir@20578 291 vments[i++] = "image/ramdisk";
keir@20578 292 vments[i++] = (char*) info->u.pv.ramdisk;
keir@20578 293 }
keir@20578 294 if (info->u.pv.cmdline) {
keir@20578 295 vments[i++] = "image/cmdline";
keir@20578 296 vments[i++] = (char*) info->u.pv.cmdline;
keir@20578 297 }
keir@20462 298 }
keir@20829 299 ret = build_post(ctx, domid, info, state, vments, localents);
keir@20829 300 out:
keir@20829 301 return ret;
keir@20462 302 }
keir@20462 303
keir@20462 304 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
keir@20565 305 uint32_t domid, int fd, libxl_domain_build_state *state,
keir@20565 306 libxl_device_model_info *dm_info)
keir@20462 307 {
keir@20462 308 char **vments = NULL, **localents = NULL;
keir@21183 309 int i, ret, esave, flags;
keir@20462 310
keir@20829 311 ret = build_pre(ctx, domid, info, state);
keir@20829 312 if (ret) goto out;
keir@20829 313
keir@20829 314 ret = restore_common(ctx, domid, info, state, fd);
keir@20829 315 if (ret) goto out;
keir@20829 316
keir@20462 317 if (info->hvm) {
keir@20565 318 vments = libxl_calloc(ctx, 5, sizeof(char *));
keir@20516 319 vments[0] = "rtc/timeoffset";
keir@20516 320 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
keir@20565 321 vments[2] = "image/ostype";
keir@20565 322 vments[3] = "hvm";
keir@20462 323 } else {
keir@20565 324 vments = libxl_calloc(ctx, 9, sizeof(char *));
keir@20627 325 i = 0;
keir@20627 326 vments[i++] = "image/ostype";
keir@20627 327 vments[i++] = "linux";
keir@20627 328 vments[i++] = "image/kernel";
keir@20627 329 vments[i++] = (char*) info->kernel;
keir@20627 330 if (info->u.pv.ramdisk) {
keir@20627 331 vments[i++] = "image/ramdisk";
keir@20627 332 vments[i++] = (char*) info->u.pv.ramdisk;
keir@20627 333 }
keir@20627 334 if (info->u.pv.cmdline) {
keir@20627 335 vments[i++] = "image/cmdline";
keir@20627 336 vments[i++] = (char*) info->u.pv.cmdline;
keir@20627 337 }
keir@20462 338 }
keir@20829 339 ret = build_post(ctx, domid, info, state, vments, localents);
keir@20829 340 if (ret) goto out;
keir@20829 341
keir@20565 342 if (info->hvm)
keir@20565 343 asprintf(&(dm_info->saved_state), "/var/lib/xen/qemu-save.%d", domid);
keir@20565 344 else
keir@20565 345 dm_info->saved_state = NULL;
keir@20829 346 out:
keir@21183 347 esave = errno;
keir@21183 348
keir@21183 349 flags = fcntl(fd, F_GETFL);
keir@21183 350 if (flags == -1) {
keir@21183 351 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to get flags on restore fd");
keir@21183 352 } else {
keir@21183 353 flags &= ~O_NONBLOCK;
keir@21183 354 if (fcntl(fd, F_SETFL, flags) == -1)
keir@21183 355 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to put restore fd"
keir@21183 356 " back to blocking mode");
keir@21183 357 }
keir@21183 358
keir@21183 359 errno = esave;
keir@20829 360 return ret;
keir@20462 361 }
keir@20462 362
keir@20629 363 int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
keir@20629 364 {
keir@20629 365 if (is_hvm(ctx, domid)) {
keir@20629 366 XL_LOG(ctx, XL_LOG_DEBUG, "Called domain_resume on "
keir@20629 367 "non-cooperative hvm domain %u", domid);
keir@20629 368 return ERROR_NI;
keir@20629 369 }
keir@20629 370 if (xc_domain_resume(ctx->xch, domid, 1)) {
keir@20629 371 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@20629 372 "xc_domain_resume failed for domain %u",
keir@20629 373 domid);
keir@20629 374 return ERROR_FAIL;
keir@20629 375 }
keir@20629 376 if (!xs_resume_domain(ctx->xsh, domid)) {
keir@20629 377 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@20629 378 "xs_resume_domain failed for domain %u",
keir@20629 379 domid);
keir@20629 380 return ERROR_FAIL;
keir@20629 381 }
keir@20629 382 return 0;
keir@20629 383 }
keir@20629 384
keir@21189 385 static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
keir@21189 386 struct libxl_dominfo *xlinfo) {
keir@21189 387 memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
keir@21189 388 xlinfo->domid = xcinfo->domain;
keir@21189 389
keir@21189 390 if (xcinfo->flags & XEN_DOMINF_dying)
keir@21189 391 xlinfo->dying = 1;
keir@21189 392 else if (xcinfo->flags & XEN_DOMINF_paused)
keir@21189 393 xlinfo->paused = 1;
keir@21189 394 else if (xcinfo->flags & XEN_DOMINF_blocked ||
keir@21189 395 xcinfo->flags & XEN_DOMINF_running)
keir@21189 396 xlinfo->running = 1;
keir@21189 397 xlinfo->max_memkb = PAGE_TO_MEMKB(xcinfo->tot_pages);
keir@21189 398 xlinfo->cpu_time = xcinfo->cpu_time;
keir@21189 399 xlinfo->vcpu_max_id = xcinfo->max_vcpu_id;
keir@21189 400 xlinfo->vcpu_online = xcinfo->nr_online_vcpus;
keir@21189 401 }
keir@21189 402
keir@20860 403 struct libxl_dominfo * libxl_list_domain(struct libxl_ctx *ctx, int *nb_domain)
keir@20462 404 {
keir@20462 405 struct libxl_dominfo *ptr;
keir@20867 406 int i, ret;
keir@20822 407 xc_domaininfo_t info[1024];
keir@20822 408 int size = 1024;
keir@20462 409
keir@20516 410 ptr = calloc(size, sizeof(struct libxl_dominfo));
keir@21189 411 if (!ptr) return NULL;
keir@20822 412
keir@20860 413 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
keir@21189 414 if (ret<0) return NULL;
keir@20784 415
keir@21189 416 for (i = 0; i < ret; i++) {
keir@21189 417 xcinfo2xlinfo(&info[i], &ptr[i]);
keir@20462 418 }
keir@20867 419 *nb_domain = ret;
keir@20462 420 return ptr;
keir@20462 421 }
keir@20462 422
keir@21189 423 int libxl_domain_info(struct libxl_ctx *ctx, struct libxl_dominfo *info_r,
keir@21189 424 uint32_t domid) {
keir@21189 425 xc_domaininfo_t xcinfo;
keir@21189 426 int ret;
keir@21189 427
keir@21189 428 ret = xc_domain_getinfolist(ctx->xch, domid, 1, &xcinfo);
keir@21189 429 if (ret<0) return ERROR_FAIL;
keir@21189 430 if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
keir@21189 431
keir@21189 432 xcinfo2xlinfo(&xcinfo, info_r);
keir@21189 433 return 0;
keir@21189 434 }
keir@21189 435
keir@21368 436 struct libxl_poolinfo * libxl_list_pool(struct libxl_ctx *ctx, int *nb_pool)
keir@21368 437 {
keir@21368 438 struct libxl_poolinfo *ptr;
keir@21368 439 int i, ret;
keir@21368 440 xc_cpupoolinfo_t info[256];
keir@21368 441 int size = 256;
keir@21368 442
keir@21368 443 ptr = calloc(size, sizeof(struct libxl_poolinfo));
keir@21368 444 if (!ptr) return NULL;
keir@21368 445
keir@21368 446 ret = xc_cpupool_getinfo(ctx->xch, 0, 256, info);
keir@21368 447 if (ret<0) return NULL;
keir@21368 448
keir@21368 449 for (i = 0; i < ret; i++) {
keir@21368 450 ptr[i].poolid = info[i].cpupool_id;
keir@21368 451 }
keir@21368 452 *nb_pool = ret;
keir@21368 453 return ptr;
keir@21368 454 }
keir@21368 455
keir@20860 456 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
keir@20860 457 struct libxl_vminfo * libxl_list_vm(struct libxl_ctx *ctx, int *nb_vm)
keir@20860 458 {
keir@20860 459 struct libxl_vminfo *ptr;
keir@20860 460 int index, i, ret;
keir@20860 461 xc_domaininfo_t info[1024];
keir@20860 462 int size = 1024;
keir@20860 463
keir@20860 464 ptr = calloc(size, sizeof(struct libxl_dominfo));
keir@20860 465 if (!ptr)
keir@20860 466 return NULL;
keir@20860 467
keir@20860 468 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
keir@20860 469 for (index = i = 0; i < ret; i++) {
keir@20860 470 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
keir@20860 471 continue;
keir@20860 472 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
keir@20860 473 ptr[index].domid = info[i].domain;
keir@20860 474
keir@20860 475 index++;
keir@20860 476 }
keir@20860 477 *nb_vm = index;
keir@20860 478 return ptr;
keir@20860 479 }
keir@20860 480
keir@20462 481 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
keir@20462 482 uint32_t domid, int fd)
keir@20462 483 {
keir@20565 484 int hvm = is_hvm(ctx, domid);
keir@20565 485 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
keir@20565 486 int debug = info != NULL && info->flags & XL_SUSPEND_LIVE;
keir@20462 487
keir@20462 488 core_suspend(ctx, domid, fd, hvm, live, debug);
keir@20565 489 if (hvm)
keir@20779 490 save_device_model(ctx, domid, fd);
keir@20462 491 return 0;
keir@20462 492 }
keir@20462 493
keir@20462 494 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 495 {
keir@20462 496 xc_domain_pause(ctx->xch, domid);
keir@20462 497 return 0;
keir@20462 498 }
keir@20462 499
keir@20462 500 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 501 {
keir@20779 502 char *path;
keir@20565 503 char *state;
keir@20565 504
keir@20565 505 if (is_hvm(ctx, domid)) {
keir@20779 506 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20565 507 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20565 508 if (state != NULL && !strcmp(state, "paused")) {
keir@20565 509 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue", strlen("continue"));
keir@20565 510 libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL);
keir@20565 511 }
keir@20565 512 }
keir@20462 513 xc_domain_unpause(ctx->xch, domid);
keir@20565 514
keir@20462 515 return 0;
keir@20462 516 }
keir@20462 517
keir@20462 518 static char *req_table[] = {
keir@20462 519 [0] = "poweroff",
keir@20462 520 [1] = "reboot",
keir@20462 521 [2] = "suspend",
keir@20462 522 [3] = "crash",
keir@20462 523 [4] = "halt",
keir@20462 524 };
keir@20462 525
keir@20462 526 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
keir@20462 527 {
keir@20462 528 char *shutdown_path;
keir@20462 529 char *dom_path;
keir@20462 530
keir@20462 531 if (req > ARRAY_SIZE(req_table))
keir@20462 532 return ERROR_INVAL;
keir@20462 533
keir@20462 534 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 535 if (!dom_path)
keir@20512 536 return ERROR_FAIL;
keir@20512 537
keir@20462 538 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
keir@20462 539
keir@20462 540 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
keir@20462 541 if (/* hvm */ 0) {
keir@20462 542 unsigned long acpi_s_state = 0;
keir@20462 543 unsigned long pvdriver = 0;
keir@20462 544 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
keir@20462 545 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
keir@20462 546 if (!pvdriver && acpi_s_state != 0)
keir@20462 547 xc_domain_shutdown(ctx->xch, domid, req);
keir@20462 548 }
keir@20462 549 return 0;
keir@20462 550 }
keir@20462 551
keir@20637 552 int libxl_get_wait_fd(struct libxl_ctx *ctx, int *fd)
keir@20545 553 {
keir@20545 554 *fd = xs_fileno(ctx->xsh);
keir@20545 555 return 0;
keir@20545 556 }
keir@20545 557
keir@20637 558 int libxl_wait_for_domain_death(struct libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
keir@20637 559 {
keir@20637 560 waiter->path = strdup("@releaseDomain");
keir@20637 561 asprintf(&(waiter->token), "%d", DOMAIN_DEATH);
keir@20637 562 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
keir@20637 563 return -1;
keir@20637 564 return 0;
keir@20637 565 }
keir@20637 566
keir@20637 567 int libxl_wait_for_disk_ejects(struct libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
keir@20637 568 {
keir@20637 569 int i;
keir@20637 570 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
keir@20637 571
keir@20637 572 if (!domid)
keir@20637 573 domid = guest_domid;
keir@20637 574
keir@20637 575 for (i = 0; i < num_disks; i++) {
keir@20637 576 asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject", libxl_xs_get_dompath(ctx, domid), device_disk_dev_number(disks[i].virtpath));
keir@20637 577 asprintf(&(waiter[i].token), "%d", DISK_EJECT);
keir@20637 578 xs_watch(ctx->xsh, waiter->path, waiter->token);
keir@20637 579 }
keir@20637 580 return 0;
keir@20637 581 }
keir@20637 582
keir@20637 583 int libxl_get_event(struct libxl_ctx *ctx, libxl_event *event)
keir@20545 584 {
keir@20545 585 unsigned int num;
keir@20637 586 char **events = xs_read_watch(ctx->xsh, &num);
keir@20637 587 if (num != 2) {
keir@20637 588 free(events);
keir@20637 589 return -1;
keir@20637 590 }
keir@20637 591 event->path = strdup(events[XS_WATCH_PATH]);
keir@20637 592 event->token = strdup(events[XS_WATCH_TOKEN]);
keir@20637 593 event->type = atoi(event->token);
keir@20637 594 free(events);
keir@20637 595 return 0;
keir@20637 596 }
keir@20637 597
keir@20637 598 int libxl_stop_waiting(struct libxl_ctx *ctx, libxl_waiter *waiter)
keir@20637 599 {
keir@20637 600 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
keir@20637 601 return -1;
keir@20637 602 else
keir@20637 603 return 0;
keir@20637 604 }
keir@20637 605
keir@20637 606 int libxl_free_event(libxl_event *event)
keir@20637 607 {
keir@20637 608 free(event->path);
keir@20637 609 free(event->token);
keir@20637 610 return 0;
keir@20637 611 }
keir@20637 612
keir@20637 613 int libxl_free_waiter(libxl_waiter *waiter)
keir@20637 614 {
keir@20637 615 free(waiter->path);
keir@20637 616 free(waiter->token);
keir@20637 617 return 0;
keir@20637 618 }
keir@20637 619
keir@20785 620 int libxl_event_get_domain_death_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, xc_domaininfo_t *info)
keir@20637 621 {
keir@20785 622 int rc = 0, ret;
keir@20545 623
keir@20637 624 if (event && event->type == DOMAIN_DEATH) {
keir@20785 625 ret = xc_domain_getinfolist(ctx->xch, domid, 1, info);
keir@20785 626 if (ret == 1 && info->domain == domid) {
keir@20785 627 if (info->flags & XEN_DOMINF_running ||
keir@20785 628 (!(info->flags & XEN_DOMINF_shutdown) && !(info->flags & XEN_DOMINF_dying)))
keir@20545 629 goto out;
keir@20545 630 rc = 1;
keir@20545 631 goto out;
keir@20545 632 }
keir@20785 633 memset(info, 0, sizeof(xc_dominfo_t));
keir@20545 634 rc = 1;
keir@20545 635 goto out;
keir@20545 636 }
keir@20545 637 out:
keir@20545 638 return rc;
keir@20545 639 }
keir@20545 640
keir@20637 641 int libxl_event_get_disk_eject_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
keir@20637 642 {
keir@20637 643 if (event && event->type == DISK_EJECT) {
keir@20637 644 char *path;
keir@20637 645 char *backend;
keir@20637 646 char *value = libxl_xs_read(ctx, XBT_NULL, event->path);
keir@20637 647
keir@20637 648 if (!value || strcmp(value, "eject"))
keir@20637 649 return 0;
keir@20637 650
keir@20637 651 path = strdup(event->path);
keir@20637 652 path[strlen(path) - 6] = '\0';
keir@20637 653 backend = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", path));
keir@20637 654
keir@20637 655 disk->backend_domid = 0;
keir@20637 656 disk->domid = domid;
keir@20637 657 disk->physpath = NULL;
keir@20637 658 disk->phystype = 0;
keir@20637 659 /* this value is returned to the user: do not free right away */
keir@20637 660 disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", backend));
keir@20637 661 disk->unpluggable = 1;
keir@20637 662 disk->readwrite = 0;
keir@20637 663 disk->is_cdrom = 1;
keir@20637 664
keir@20637 665 free(path);
keir@20637 666 return 1;
keir@20637 667 }
keir@20637 668 return 0;
keir@20637 669 }
keir@20637 670
keir@20462 671 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 672 {
keir@20462 673 char *pid;
keir@20462 674 int ret;
keir@20462 675
keir@20462 676 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
keir@20462 677 if (!pid) {
keir@20542 678 int stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20542 679 if (!stubdomid) {
keir@20542 680 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
keir@20542 681 return -1;
keir@20542 682 }
keir@20542 683 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
keir@20542 684 return libxl_domain_destroy(ctx, stubdomid, 0);
keir@20462 685 }
keir@20462 686 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
keir@20462 687
keir@20462 688 ret = kill(atoi(pid), SIGHUP);
keir@20462 689 if (ret < 0 && errno == ESRCH) {
keir@20512 690 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
keir@20462 691 ret = 0;
keir@20462 692 } else if (ret == 0) {
keir@20512 693 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
keir@20462 694 ret = 0;
keir@20462 695 } else {
keir@20512 696 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
keir@20512 697 atoi(pid));
keir@20462 698 }
keir@20462 699 return ret;
keir@20462 700 }
keir@20462 701
keir@20462 702 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
keir@20462 703 {
keir@20801 704 char *dom_path;
keir@20779 705 char *vm_path, *vss_path, *xapi_path;
keir@20628 706 int rc, dm_present;
keir@20628 707
keir@20628 708 if (is_hvm(ctx, domid)) {
keir@20628 709 dm_present = 1;
keir@20628 710 } else {
keir@20628 711 char *pid;
keir@20628 712 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
keir@20628 713 dm_present = (pid != NULL);
keir@20628 714 libxl_free(ctx, pid);
keir@20628 715 }
keir@20462 716
keir@20462 717 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 718 if (!dom_path)
keir@20462 719 return -1;
keir@20512 720
keir@20482 721 if (libxl_device_pci_shutdown(ctx, domid) < 0)
keir@20512 722 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
keir@20628 723 if (dm_present) {
keir@20628 724 xs_write(ctx->xsh, XBT_NULL,
keir@20628 725 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
keir@20628 726 "shutdown", strlen("shutdown"));
keir@20628 727 }
keir@20512 728 rc = xc_domain_pause(ctx->xch, domid);
keir@20512 729 if (rc < 0) {
keir@20512 730 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
keir@20462 731 return -1;
keir@20462 732 }
keir@20628 733 if (dm_present) {
keir@20628 734 if (libxl_destroy_device_model(ctx, domid) < 0)
keir@20628 735 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
keir@20628 736 }
keir@20462 737 if (libxl_devices_destroy(ctx, domid, force) < 0)
keir@20512 738 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
keir@20462 739 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
keir@20512 740 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
keir@20779 741
keir@20801 742 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", domid));
keir@20823 743 if (vm_path)
keir@20823 744 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
keir@20823 745 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
keir@20779 746
keir@20801 747 vss_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vss", domid));
keir@20823 748 if (vss_path)
keir@20823 749 if (!xs_rm(ctx->xsh, XBT_NULL, vss_path))
keir@20823 750 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vss_path);
keir@20779 751
keir@20779 752 xapi_path = libxl_sprintf(ctx, "/xapi/%u", domid);
keir@20823 753 if (xapi_path)
keir@20823 754 if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
keir@20823 755 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
keir@20779 756
keir@21190 757 libxl__userdata_destroyall(ctx, domid);
keir@21190 758
keir@20545 759 rc = xc_domain_destroy(ctx->xch, domid);
keir@20545 760 if (rc < 0) {
keir@20545 761 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
keir@20545 762 return -1;
keir@20545 763 }
keir@20462 764 return 0;
keir@20462 765 }
keir@20462 766
keir@20564 767 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num)
keir@20564 768 {
keir@21283 769 char *cmd = libxl_sprintf(
keir@21283 770 ctx, "%s/xenconsole %d --num %d",
keir@21283 771 libxl_private_bindir_path(), domid, cons_num);
keir@20564 772 return (system(cmd) != 0) ? ERROR_FAIL : 0;
keir@20564 773 }
keir@20564 774
keir@20462 775 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
keir@20462 776 libxl_device_model_info *info,
keir@20462 777 libxl_device_nic *vifs,
keir@20462 778 int num_vifs)
keir@20462 779 {
keir@20462 780 int num = 0, i;
keir@20462 781 flexarray_t *dm_args;
keir@20462 782 dm_args = flexarray_make(16, 1);
keir@20462 783 if (!dm_args)
keir@20462 784 return NULL;
keir@20462 785
keir@20516 786 flexarray_set(dm_args, num++, "qemu-dm");
keir@20516 787 flexarray_set(dm_args, num++, "-d");
keir@20462 788
keir@20462 789 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
keir@20462 790
keir@20462 791 if (info->dom_name) {
keir@20516 792 flexarray_set(dm_args, num++, "-domain-name");
keir@20516 793 flexarray_set(dm_args, num++, info->dom_name);
keir@20462 794 }
keir@20462 795 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
keir@20516 796 flexarray_set(dm_args, num++, "-vnc");
keir@20462 797 if (info->vncdisplay) {
keir@20462 798 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
keir@21362 799 flexarray_set(
keir@21362 800 dm_args, num++,
keir@21362 801 libxl_sprintf(ctx, "%s:%d%s",
keir@21362 802 info->vnclisten,
keir@21362 803 info->vncdisplay,
keir@21362 804 info->vncpasswd ? ",password" : ""));
keir@20462 805 } else {
keir@20462 806 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
keir@20462 807 }
keir@20462 808 } else if (info->vnclisten) {
keir@20462 809 if (strchr(info->vnclisten, ':') != NULL) {
keir@20516 810 flexarray_set(dm_args, num++, info->vnclisten);
keir@20462 811 } else {
keir@20462 812 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
keir@20462 813 }
keir@20462 814 } else {
keir@20516 815 flexarray_set(dm_args, num++, "127.0.0.1:0");
keir@20462 816 }
keir@20462 817 if (info->vncunused) {
keir@20516 818 flexarray_set(dm_args, num++, "-vncunused");
keir@20462 819 }
keir@20462 820 }
keir@21378 821 if (info->sdl) {
keir@20516 822 flexarray_set(dm_args, num++, "-sdl");
keir@20462 823 if (info->opengl) {
keir@20516 824 flexarray_set(dm_args, num++, "-disable-opengl");
keir@20462 825 }
keir@20462 826 }
keir@20462 827 if (info->keymap) {
keir@20516 828 flexarray_set(dm_args, num++, "-k");
keir@20516 829 flexarray_set(dm_args, num++, info->keymap);
keir@20462 830 }
keir@20462 831 if (info->nographic && (!info->sdl && !info->vnc)) {
keir@20516 832 flexarray_set(dm_args, num++, "-nographic");
keir@20462 833 }
keir@20462 834 if (info->serial) {
keir@20516 835 flexarray_set(dm_args, num++, "-serial");
keir@20516 836 flexarray_set(dm_args, num++, info->serial);
keir@20462 837 }
keir@20509 838 if (info->type == XENFV) {
keir@20509 839 if (info->videoram) {
keir@20516 840 flexarray_set(dm_args, num++, "-videoram");
keir@20509 841 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
keir@20509 842 }
keir@20509 843 if (info->stdvga) {
keir@20516 844 flexarray_set(dm_args, num++, "-std-vga");
keir@20509 845 }
keir@20509 846
keir@20509 847 if (info->boot) {
keir@20516 848 flexarray_set(dm_args, num++, "-boot");
keir@20516 849 flexarray_set(dm_args, num++, info->boot);
keir@20509 850 }
keir@20509 851 if (info->usb) {
keir@20516 852 flexarray_set(dm_args, num++, "-usb");
keir@20509 853 if (info->usbdevice) {
keir@20516 854 flexarray_set(dm_args, num++, "-usbdevice");
keir@20516 855 flexarray_set(dm_args, num++, info->usbdevice);
keir@20509 856 }
keir@20509 857 }
keir@20509 858 if (info->apic) {
keir@20516 859 flexarray_set(dm_args, num++, "-acpi");
keir@20509 860 }
keir@20509 861 for (i = 0; i < num_vifs; i++) {
keir@20509 862 if (vifs[i].nictype == NICTYPE_IOEMU) {
keir@20780 863 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20780 864 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
keir@20780 865 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
keir@20781 866 if (!vifs[i].ifname)
keir@20781 867 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid - 1);
keir@20516 868 flexarray_set(dm_args, num++, "-net");
keir@20509 869 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
keir@20780 870 vifs[i].devid, smac, vifs[i].model));
keir@20516 871 flexarray_set(dm_args, num++, "-net");
keir@20509 872 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
keir@20509 873 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
keir@20509 874 }
keir@20462 875 }
keir@20462 876 }
keir@20565 877 if (info->saved_state) {
keir@20565 878 flexarray_set(dm_args, num++, "-loadvm");
keir@20565 879 flexarray_set(dm_args, num++, info->saved_state);
keir@20565 880 }
keir@20509 881 for (i = 0; info->extra && info->extra[i] != NULL; i++)
keir@20509 882 flexarray_set(dm_args, num++, info->extra[i]);
keir@20516 883 flexarray_set(dm_args, num++, "-M");
keir@20509 884 if (info->type == XENPV)
keir@20516 885 flexarray_set(dm_args, num++, "xenpv");
keir@20509 886 else
keir@20516 887 flexarray_set(dm_args, num++, "xenfv");
keir@20462 888 flexarray_set(dm_args, num++, NULL);
keir@20462 889
keir@20462 890 return (char **) flexarray_contents(dm_args);
keir@20462 891 }
keir@20462 892
keir@20834 893 void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
keir@20834 894 {
keir@20514 895 struct libxl_device_model_starting *starting = for_spawn;
keir@20514 896 char *kvs[3];
keir@20820 897 int rc;
keir@20820 898 struct xs_handle *xsh;
keir@20514 899
keir@20820 900 xsh = xs_daemon_open();
keir@20514 901 /* we mustn't use the parent's handle in the child */
keir@20514 902
keir@20514 903 kvs[0] = "image/device-model-pid";
keir@20820 904 asprintf(&kvs[1], "%d", innerchild);
keir@20514 905 kvs[2] = NULL;
keir@20820 906
keir@20820 907 rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
keir@20820 908 if (rc)
keir@20820 909 return;
keir@20820 910 xs_daemon_close(xsh);
keir@20514 911 }
keir@20514 912
keir@20542 913 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
keir@20542 914 libxl_device_model_info *info,
keir@20542 915 libxl_device_vfb *vfb,
keir@20542 916 libxl_device_vkb *vkb)
keir@20542 917 {
keir@20542 918 memset(vfb, 0x00, sizeof(libxl_device_vfb));
keir@20542 919 memset(vkb, 0x00, sizeof(libxl_device_vkb));
keir@20542 920
keir@20542 921 vfb->backend_domid = 0;
keir@20542 922 vfb->devid = 0;
keir@20542 923 vfb->vnc = info->vnc;
keir@20542 924 vfb->vnclisten = info->vnclisten;
keir@20542 925 vfb->vncdisplay = info->vncdisplay;
keir@20542 926 vfb->vncunused = info->vncunused;
keir@21362 927 vfb->vncpasswd = info->vncpasswd;
keir@20542 928 vfb->keymap = info->keymap;
keir@20542 929 vfb->sdl = info->sdl;
keir@20542 930 vfb->opengl = info->opengl;
keir@20542 931
keir@20542 932 vkb->backend_domid = 0;
keir@20542 933 vkb->devid = 0;
keir@20542 934 return 0;
keir@20542 935 }
keir@20542 936
keir@20542 937 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
keir@20542 938 {
keir@20542 939 int i;
keir@20542 940 char *vm_path;
keir@20542 941 char *dmargs, *path;
keir@20542 942 int dmargs_size;
keir@20542 943 struct xs_permissions roperm[2];
keir@20542 944 xs_transaction_t t;
keir@20542 945
keir@20542 946 roperm[0].id = 0;
keir@20542 947 roperm[0].perms = XS_PERM_NONE;
keir@20542 948 roperm[1].id = domid;
keir@20542 949 roperm[1].perms = XS_PERM_READ;
keir@20542 950
keir@20542 951 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
keir@20542 952
keir@20542 953 i = 0;
keir@20542 954 dmargs_size = 0;
keir@20542 955 while (args[i] != NULL) {
keir@20542 956 dmargs_size = dmargs_size + strlen(args[i]) + 1;
keir@20542 957 i++;
keir@20542 958 }
keir@20542 959 dmargs_size++;
keir@20542 960 dmargs = (char *) malloc(dmargs_size);
keir@20542 961 i = 1;
keir@20542 962 dmargs[0] = '\0';
keir@20542 963 while (args[i] != NULL) {
keir@20542 964 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
keir@20542 965 strcat(dmargs, " ");
keir@20542 966 strcat(dmargs, args[i]);
keir@20542 967 }
keir@20542 968 i++;
keir@20542 969 }
keir@20542 970 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
keir@20542 971
keir@20542 972 retry_transaction:
keir@20542 973 t = xs_transaction_start(ctx->xsh);
keir@20542 974 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
keir@20542 975 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20542 976 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
keir@20542 977 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 978 if (errno == EAGAIN)
keir@20542 979 goto retry_transaction;
keir@20542 980 free(dmargs);
keir@20542 981 return 0;
keir@20542 982 }
keir@20542 983
keir@20542 984 static int libxl_create_stubdom(struct libxl_ctx *ctx,
keir@20542 985 libxl_device_model_info *info,
keir@20542 986 libxl_device_disk *disks, int num_disks,
keir@20542 987 libxl_device_nic *vifs, int num_vifs,
keir@20542 988 libxl_device_vfb *vfb,
keir@20542 989 libxl_device_vkb *vkb,
keir@20542 990 libxl_device_model_starting **starting_r)
keir@20542 991 {
keir@20843 992 int i, num_console = 1, ret;
keir@20570 993 libxl_device_console *console;
keir@20542 994 libxl_domain_create_info c_info;
keir@20542 995 libxl_domain_build_info b_info;
keir@20542 996 libxl_domain_build_state state;
keir@20542 997 uint32_t domid;
keir@20542 998 char **args;
keir@20542 999 struct xs_permissions perm[2];
keir@20542 1000 xs_transaction_t t;
keir@20594 1001 libxl_device_model_starting *dm_starting = 0;
keir@20542 1002
keir@20542 1003 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20542 1004 if (!args)
keir@20542 1005 return ERROR_FAIL;
keir@20542 1006
keir@20542 1007 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
keir@20542 1008 c_info.hvm = 0;
keir@20542 1009 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
keir@20801 1010 for (i = 0; i < 16; i++)
keir@20801 1011 c_info.uuid[i] = info->uuid[i];
keir@20542 1012
keir@20542 1013 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
keir@20542 1014 b_info.max_vcpus = 1;
keir@20542 1015 b_info.max_memkb = 32 * 1024;
keir@20647 1016 b_info.target_memkb = b_info.max_memkb;
keir@21353 1017 b_info.kernel = libxl_abs_path(ctx, "ioemu-stubdom.gz", libxl_xenfirmwaredir_path());
keir@20542 1018 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
keir@20542 1019 b_info.u.pv.ramdisk = "";
keir@20542 1020 b_info.u.pv.features = "";
keir@20542 1021 b_info.hvm = 0;
keir@20542 1022
keir@20843 1023 ret = libxl_domain_make(ctx, &c_info, &domid);
keir@20843 1024 if (ret) return ret;
keir@20843 1025 ret = libxl_domain_build(ctx, &b_info, domid, &state);
keir@20843 1026 if (ret) return ret;
keir@20542 1027
keir@20542 1028 libxl_write_dmargs(ctx, domid, info->domid, args);
keir@20843 1029 libxl_xs_write(ctx, XBT_NULL,
keir@20843 1030 libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)),
keir@20843 1031 "%d", domid);
keir@20843 1032 libxl_xs_write(ctx, XBT_NULL,
keir@20843 1033 libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)),
keir@20843 1034 "%d", info->domid);
keir@20542 1035 xc_domain_set_target(ctx->xch, domid, info->domid);
keir@20542 1036 xs_set_target(ctx->xsh, domid, info->domid);
keir@20542 1037
keir@20542 1038 perm[0].id = domid;
keir@20542 1039 perm[0].perms = XS_PERM_NONE;
keir@20542 1040 perm[1].id = info->domid;
keir@20542 1041 perm[1].perms = XS_PERM_READ;
keir@20542 1042 retry_transaction:
keir@20542 1043 t = xs_transaction_start(ctx->xsh);
keir@20542 1044 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
keir@20542 1045 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
keir@20635 1046 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
keir@20635 1047 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
keir@20542 1048 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 1049 if (errno == EAGAIN)
keir@20542 1050 goto retry_transaction;
keir@20542 1051
keir@20542 1052 for (i = 0; i < num_disks; i++) {
keir@20781 1053 disks[i].domid = domid;
keir@20843 1054 ret = libxl_device_disk_add(ctx, domid, &disks[i]);
keir@20843 1055 if (ret) return ret;
keir@20542 1056 }
keir@20542 1057 for (i = 0; i < num_vifs; i++) {
keir@20781 1058 vifs[i].domid = domid;
keir@20843 1059 ret = libxl_device_nic_add(ctx, domid, &vifs[i]);
keir@20843 1060 if (ret) return ret;
keir@20542 1061 }
keir@20781 1062 vfb->domid = domid;
keir@20843 1063 ret = libxl_device_vfb_add(ctx, domid, vfb);
keir@20843 1064 if (ret) return ret;
keir@20781 1065 vkb->domid = domid;
keir@20843 1066 ret = libxl_device_vkb_add(ctx, domid, vkb);
keir@20843 1067 if (ret) return ret;
keir@20542 1068
keir@20570 1069 if (info->serial)
keir@20570 1070 num_console++;
keir@20843 1071
keir@20570 1072 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
keir@20843 1073 if (!console)
keir@20843 1074 return ERROR_NOMEM;
keir@20843 1075
keir@20570 1076 for (i = 0; i < num_console; i++) {
keir@20781 1077 console[i].devid = i;
keir@20781 1078 console[i].constype = CONSTYPE_IOEMU;
keir@20781 1079 console[i].domid = domid;
keir@20570 1080 if (!i)
keir@20781 1081 console[i].build_state = &state;
keir@20843 1082 ret = libxl_device_console_add(ctx, domid, &console[i]);
keir@20843 1083 if (ret) return ret;
keir@20570 1084 }
keir@20594 1085 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
keir@20594 1086 free(args);
keir@20594 1087 return -1;
keir@20594 1088 }
keir@20594 1089 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
keir@20594 1090 free(args);
keir@20594 1091 return -1;
keir@20594 1092 }
keir@20542 1093
keir@20542 1094 libxl_domain_unpause(ctx, domid);
keir@20542 1095
keir@20594 1096 if (starting_r) {
keir@20594 1097 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
keir@20598 1098 (*starting_r)->domid = info->domid;
keir@20594 1099 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20594 1100 (*starting_r)->for_spawn = NULL;
keir@20594 1101 }
keir@20594 1102
keir@20542 1103 free(args);
keir@20542 1104 return 0;
keir@20542 1105 }
keir@20542 1106
keir@20462 1107 int libxl_create_device_model(struct libxl_ctx *ctx,
keir@20462 1108 libxl_device_model_info *info,
keir@20542 1109 libxl_device_disk *disks, int num_disks,
keir@20514 1110 libxl_device_nic *vifs, int num_vifs,
keir@20514 1111 libxl_device_model_starting **starting_r)
keir@20462 1112 {
keir@20546 1113 char *path, *logfile;
keir@20514 1114 int logfile_w, null;
keir@20546 1115 int rc;
keir@20462 1116 char **args;
keir@20594 1117 struct libxl_device_model_starting buf_starting, *p;
keir@21362 1118 xs_transaction_t t;
keir@21362 1119 char *vm_path;
keir@21362 1120 char **pass_stuff;
keir@20514 1121
keir@20542 1122 if (strstr(info->device_model, "stubdom-dm")) {
keir@20542 1123 libxl_device_vfb vfb;
keir@20542 1124 libxl_device_vkb vkb;
keir@20542 1125
keir@20542 1126 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
keir@20542 1127 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
keir@20542 1128 }
keir@20542 1129
keir@20462 1130 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20462 1131 if (!args)
keir@20462 1132 return ERROR_FAIL;
keir@20462 1133
keir@20462 1134 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
keir@20462 1135 xs_mkdir(ctx->xsh, XBT_NULL, path);
keir@20462 1136
keir@20546 1137 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
keir@20467 1138 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
keir@20546 1139 free(logfile);
keir@20462 1140 null = open("/dev/null", O_RDONLY);
keir@20514 1141
keir@20514 1142 if (starting_r) {
keir@20516 1143 rc = ERROR_NOMEM;
keir@20594 1144 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
keir@20516 1145 if (!*starting_r) goto xit;
keir@20594 1146 p = *starting_r;
keir@20611 1147 p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
keir@20594 1148 } else {
keir@20594 1149 p = &buf_starting;
keir@20594 1150 p->for_spawn = NULL;
keir@20594 1151 }
keir@20514 1152
keir@20594 1153 p->domid = info->domid;
keir@20594 1154 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20594 1155 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
keir@20594 1156
keir@21362 1157 if (info->vncpasswd) {
keir@21362 1158 retry_transaction:
keir@21362 1159 /* Find uuid and the write the vnc password to xenstore for qemu. */
keir@21362 1160 t = xs_transaction_start(ctx->xsh);
keir@21362 1161 vm_path = libxl_xs_read(ctx,t,libxl_sprintf(ctx, "%s/vm", p->dom_path));
keir@21362 1162 if (vm_path) {
keir@21362 1163 /* Now write the vncpassword into it. */
keir@21362 1164 pass_stuff = libxl_calloc(ctx, 2, sizeof(char *));
keir@21362 1165 pass_stuff[0] = "vncpasswd";
keir@21362 1166 pass_stuff[1] = info->vncpasswd;
keir@21362 1167 libxl_xs_writev(ctx,t,vm_path,pass_stuff);
keir@21362 1168 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@21362 1169 if (errno == EAGAIN)
keir@21362 1170 goto retry_transaction;
keir@21362 1171 }
keir@21362 1172 }
keir@21362 1173
keir@20594 1174 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
keir@20514 1175 if (rc < 0) goto xit;
keir@20514 1176 if (!rc) { /* inner child */
keir@20833 1177 libxl_exec(null, logfile_w, logfile_w,
keir@21353 1178 libxl_abs_path(ctx, info->device_model, libxl_private_bindir_path()),
keir@21353 1179 args);
keir@20514 1180 }
keir@20514 1181
keir@20514 1182 rc = 0;
keir@20514 1183 xit:
keir@20516 1184 free(args);
keir@20462 1185 close(null);
keir@20462 1186 close(logfile_w);
keir@20462 1187
keir@20514 1188 return rc;
keir@20514 1189 }
keir@20514 1190
keir@20514 1191 int libxl_detach_device_model(struct libxl_ctx *ctx,
keir@20779 1192 libxl_device_model_starting *starting)
keir@20779 1193 {
keir@20514 1194 int rc;
keir@20594 1195 rc = libxl_spawn_detach(ctx, starting->for_spawn);
keir@20594 1196 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
keir@20514 1197 libxl_free(ctx, starting);
keir@20514 1198 return rc;
keir@20514 1199 }
keir@20462 1200
keir@20514 1201
keir@20514 1202 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
keir@20779 1203 libxl_device_model_starting *starting)
keir@20779 1204 {
keir@20514 1205 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
keir@20514 1206 libxl_spawn_check,
keir@20594 1207 starting->for_spawn);
keir@20514 1208 int detach = libxl_detach_device_model(ctx, starting);
keir@20514 1209 return problem ? problem : detach;
keir@20542 1210 return 0;
keir@20462 1211 }
keir@20462 1212
keir@20514 1213
keir@20462 1214 /******************************************************************************/
keir@20566 1215
keir@20566 1216 static int is_blktap2_supported(void)
keir@20566 1217 {
keir@20566 1218 char buf[1024];
keir@20566 1219 FILE *f = fopen("/proc/devices", "r");
keir@20566 1220
keir@20566 1221
keir@20566 1222 while (fgets(buf, sizeof(buf), f) != NULL) {
keir@20566 1223 if (strstr(buf, "blktap2")) {
keir@20566 1224 fclose(f);
keir@20566 1225 return 1;
keir@20566 1226 }
keir@20566 1227 }
keir@20566 1228 fclose(f);
keir@20566 1229 return 0;
keir@20566 1230 }
keir@20566 1231
keir@20566 1232 static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type)
keir@20566 1233 {
keir@20566 1234 char buf[1024];
keir@20566 1235 char *p;
keir@20566 1236 int devnum;
keir@20566 1237 FILE *f = fopen("/sys/class/blktap2/devices", "r");
keir@20566 1238
keir@20566 1239
keir@20566 1240 while (!feof(f)) {
keir@20566 1241 fscanf(f, "%d %s", &devnum, buf);
keir@20566 1242 p = strchr(buf, ':');
keir@20571 1243 if (p == NULL)
keir@20571 1244 continue;
keir@20566 1245 p++;
keir@20566 1246 if (!strcmp(p, name) && !strncmp(buf, type, 3)) {
keir@20566 1247 fclose(f);
keir@20566 1248 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", devnum);
keir@20566 1249 }
keir@20566 1250 }
keir@20566 1251 fclose(f);
keir@20566 1252 return NULL;
keir@20566 1253 }
keir@20566 1254
keir@20462 1255 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20462 1256 {
keir@20462 1257 flexarray_t *front;
keir@20462 1258 flexarray_t *back;
keir@20462 1259 char *backend_type;
keir@20462 1260 unsigned int boffset = 0;
keir@20462 1261 unsigned int foffset = 0;
keir@20462 1262 int devid;
keir@20462 1263 libxl_device device;
keir@20566 1264 int major, minor;
keir@20462 1265
keir@20462 1266 front = flexarray_make(16, 1);
keir@20462 1267 if (!front)
keir@20462 1268 return ERROR_NOMEM;
keir@20462 1269 back = flexarray_make(16, 1);
keir@20462 1270 if (!back) /* leaks front if error */
keir@20462 1271 return ERROR_NOMEM;
keir@20462 1272
keir@20462 1273 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
keir@20462 1274 devid = device_disk_dev_number(disk->virtpath);
keir@20947 1275 if (devid==-1) {
keir@20947 1276 XL_LOG(ctx, XL_LOG_ERROR, "Invalid or unuspported"
keir@20947 1277 " virtual disk identifier %s", disk->virtpath);
keir@20947 1278 return ERROR_INVAL;
keir@20947 1279 }
keir@20462 1280
keir@20462 1281 device.backend_devid = devid;
keir@20462 1282 device.backend_domid = disk->backend_domid;
keir@20462 1283 device.devid = devid;
keir@20462 1284 device.domid = disk->domid;
keir@20462 1285 device.kind = DEVICE_VBD;
keir@20462 1286
keir@20462 1287 switch (disk->phystype) {
keir@20462 1288 case PHYSTYPE_PHY: {
keir@20462 1289
keir@20509 1290 device_physdisk_major_minor(disk->physpath, &major, &minor);
keir@20516 1291 flexarray_set(back, boffset++, "physical-device");
keir@20462 1292 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20462 1293
keir@20516 1294 flexarray_set(back, boffset++, "params");
keir@20516 1295 flexarray_set(back, boffset++, disk->physpath);
keir@20462 1296
keir@20462 1297 device.backend_kind = DEVICE_VBD;
keir@20462 1298 break;
keir@20462 1299 }
keir@20566 1300 case PHYSTYPE_FILE:
keir@20566 1301 /* let's pretend is tap:aio for the moment */
keir@20566 1302 disk->phystype = PHYSTYPE_AIO;
keir@20462 1303 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
keir@20566 1304 if (is_blktap2_supported()) {
keir@20566 1305 int rc, c, p[2], tot;
keir@20566 1306 char buf[1024], *dev;
keir@20779 1307 dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
keir@20779 1308 if (dev == NULL) {
keir@21185 1309 rc= libxl_pipe(ctx, p); if (rc==-1) return -1;
keir@21185 1310 rc= libxl_fork(ctx); if (rc==-1) return -1;
keir@21185 1311 if (!rc) { /* child */
keir@20566 1312 int null_r, null_w;
keir@20566 1313 char *args[4];
keir@20566 1314 args[0] = "tapdisk2";
keir@20566 1315 args[1] = "-n";
keir@20566 1316 args[2] = libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath);
keir@20566 1317 args[3] = NULL;
keir@20566 1318
keir@20566 1319 null_r = open("/dev/null", O_RDONLY);
keir@20566 1320 null_w = open("/dev/null", O_WRONLY);
keir@21376 1321 libxl_exec(null_r, p[1], null_w,
keir@21376 1322 libxl_abs_path(ctx, "tapdisk2",
keir@21376 1323 libxl_sbindir_path()),
keir@21376 1324 args);
keir@20566 1325 XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
keir@20566 1326 }
keir@20566 1327 close(p[1]);
keir@20566 1328 tot = 0;
keir@20566 1329 while ((c = read(p[0], buf + tot, sizeof(buf) - tot)) > 0)
keir@20566 1330 tot = tot + c;
keir@20566 1331 close(p[0]);
keir@20566 1332 buf[tot - 1] = '\0';
keir@20566 1333 dev = buf;
keir@20566 1334 }
keir@20566 1335 flexarray_set(back, boffset++, "tapdisk-params");
keir@20566 1336 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20566 1337 flexarray_set(back, boffset++, "params");
keir@20566 1338 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", dev));
keir@20566 1339 backend_type = "phy";
keir@20566 1340 device_physdisk_major_minor(dev, &major, &minor);
keir@20566 1341 flexarray_set(back, boffset++, "physical-device");
keir@20566 1342 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20566 1343 device.backend_kind = DEVICE_VBD;
keir@20566 1344
keir@20566 1345 break;
keir@20566 1346 }
keir@20516 1347 flexarray_set(back, boffset++, "params");
keir@20462 1348 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
keir@20462 1349 device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20462 1350
keir@20462 1351 device.backend_kind = DEVICE_TAP;
keir@20462 1352 break;
keir@20846 1353 default:
keir@20846 1354 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
keir@20846 1355 return ERROR_INVAL;
keir@20462 1356 }
keir@20462 1357
keir@20516 1358 flexarray_set(back, boffset++, "frontend-id");
keir@20462 1359 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
keir@20516 1360 flexarray_set(back, boffset++, "online");
keir@20516 1361 flexarray_set(back, boffset++, "1");
keir@20516 1362 flexarray_set(back, boffset++, "removable");
keir@20462 1363 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
keir@20516 1364 flexarray_set(back, boffset++, "bootable");
keir@20509 1365 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1366 flexarray_set(back, boffset++, "state");
keir@20462 1367 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1368 flexarray_set(back, boffset++, "dev");
keir@20516 1369 flexarray_set(back, boffset++, disk->virtpath);
keir@20516 1370 flexarray_set(back, boffset++, "type");
keir@20516 1371 flexarray_set(back, boffset++, backend_type);
keir@20516 1372 flexarray_set(back, boffset++, "mode");
keir@20516 1373 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
keir@20462 1374
keir@20516 1375 flexarray_set(front, foffset++, "backend-id");
keir@20462 1376 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
keir@20516 1377 flexarray_set(front, foffset++, "state");
keir@20462 1378 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1379 flexarray_set(front, foffset++, "virtual-device");
keir@20462 1380 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
keir@20516 1381 flexarray_set(front, foffset++, "device-type");
keir@20516 1382 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
keir@20462 1383
keir@20462 1384 if (0 /* protocol != native*/) {
keir@20516 1385 flexarray_set(front, foffset++, "protocol");
keir@20516 1386 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 1387 }
keir@20462 1388
keir@20462 1389 libxl_device_generic_add(ctx, &device,
keir@20462 1390 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 1391 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1392 flexarray_free(back);
keir@20516 1393 flexarray_free(front);
keir@20462 1394 return 0;
keir@20462 1395 }
keir@20462 1396
keir@20626 1397 int libxl_device_disk_del(struct libxl_ctx *ctx,
keir@20626 1398 libxl_device_disk *disk, int wait)
keir@20462 1399 {
keir@20626 1400 libxl_device device;
keir@20626 1401 int devid;
keir@20462 1402
keir@20626 1403 devid = device_disk_dev_number(disk->virtpath);
keir@20626 1404 device.backend_domid = disk->backend_domid;
keir@20626 1405 device.backend_devid = devid;
keir@20626 1406 device.backend_kind =
keir@20626 1407 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
keir@20626 1408 device.domid = disk->domid;
keir@20626 1409 device.devid = devid;
keir@20626 1410 device.kind = DEVICE_VBD;
keir@20626 1411 return libxl_device_del(ctx, &device, wait);
keir@20462 1412 }
keir@20462 1413
keir@20462 1414 /******************************************************************************/
keir@20462 1415 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
keir@20462 1416 {
keir@20462 1417 flexarray_t *front;
keir@20462 1418 flexarray_t *back;
keir@20462 1419 unsigned int boffset = 0;
keir@20462 1420 unsigned int foffset = 0;
keir@20462 1421 libxl_device device;
keir@20462 1422
keir@20462 1423 front = flexarray_make(16, 1);
keir@20462 1424 if (!front)
keir@20462 1425 return ERROR_NOMEM;
keir@20462 1426 back = flexarray_make(16, 1);
keir@20462 1427 if (!back)
keir@20462 1428 return ERROR_NOMEM;
keir@20462 1429
keir@20462 1430 device.backend_devid = nic->devid;
keir@20462 1431 device.backend_domid = nic->backend_domid;
keir@20462 1432 device.backend_kind = DEVICE_VIF;
keir@20462 1433 device.devid = nic->devid;
keir@20462 1434 device.domid = nic->domid;
keir@20462 1435 device.kind = DEVICE_VIF;
keir@20462 1436
keir@20516 1437 flexarray_set(back, boffset++, "frontend-id");
keir@20462 1438 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
keir@20516 1439 flexarray_set(back, boffset++, "online");
keir@20516 1440 flexarray_set(back, boffset++, "1");
keir@20516 1441 flexarray_set(back, boffset++, "state");
keir@20462 1442 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1443 flexarray_set(back, boffset++, "script");
keir@20516 1444 flexarray_set(back, boffset++, nic->script);
keir@20516 1445 flexarray_set(back, boffset++, "mac");
keir@20462 1446 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 1447 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 1448 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20516 1449 flexarray_set(back, boffset++, "handle");
keir@20462 1450 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20462 1451
keir@20516 1452 flexarray_set(front, foffset++, "backend-id");
keir@20462 1453 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
keir@20516 1454 flexarray_set(front, foffset++, "state");
keir@20462 1455 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1456 flexarray_set(front, foffset++, "handle");
keir@20462 1457 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20516 1458 flexarray_set(front, foffset++, "mac");
keir@20462 1459 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 1460 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 1461 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20462 1462 if (0 /* protocol != native*/) {
keir@20516 1463 flexarray_set(front, foffset++, "protocol");
keir@20516 1464 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 1465 }
keir@20462 1466
keir@20462 1467 libxl_device_generic_add(ctx, &device,
keir@20462 1468 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 1469 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20462 1470
keir@20462 1471 /* FIXME: wait for plug */
keir@20516 1472 flexarray_free(back);
keir@20516 1473 flexarray_free(front);
keir@20462 1474 return 0;
keir@20462 1475 }
keir@20462 1476
keir@20626 1477 int libxl_device_nic_del(struct libxl_ctx *ctx,
keir@20626 1478 libxl_device_nic *nic, int wait)
keir@20462 1479 {
keir@20626 1480 libxl_device device;
keir@20462 1481
keir@20626 1482 device.backend_devid = nic->devid;
keir@20626 1483 device.backend_domid = nic->backend_domid;
keir@20626 1484 device.backend_kind = DEVICE_VIF;
keir@20626 1485 device.devid = nic->devid;
keir@20626 1486 device.domid = nic->domid;
keir@20626 1487 device.kind = DEVICE_VIF;
keir@20626 1488
keir@20626 1489 return libxl_device_del(ctx, &device, wait);
keir@20462 1490 }
keir@20462 1491
keir@20462 1492 /******************************************************************************/
keir@20509 1493 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
keir@20462 1494 {
keir@20509 1495 flexarray_t *front;
keir@20509 1496 flexarray_t *back;
keir@20509 1497 unsigned int boffset = 0;
keir@20509 1498 unsigned int foffset = 0;
keir@20509 1499 libxl_device device;
keir@20509 1500
keir@20509 1501 if (console->build_state) {
keir@20509 1502 xs_transaction_t t;
keir@20509 1503 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
keir@20516 1504 ents[0] = "console/port";
keir@20509 1505 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
keir@20516 1506 ents[2] = "console/ring-ref";
keir@20509 1507 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
keir@20516 1508 ents[4] = "console/limit";
keir@20509 1509 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
keir@20516 1510 ents[6] = "console/type";
keir@20509 1511 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20509 1512 ents[7] = "xenconsoled";
keir@20509 1513 else
keir@20509 1514 ents[7] = "ioemu";
keir@20509 1515 retry_transaction:
keir@20509 1516 t = xs_transaction_start(ctx->xsh);
keir@20516 1517 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
keir@20509 1518 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20509 1519 if (errno == EAGAIN)
keir@20509 1520 goto retry_transaction;
keir@20509 1521 }
keir@20509 1522
keir@20509 1523 front = flexarray_make(16, 1);
keir@20509 1524 if (!front)
keir@20509 1525 return ERROR_NOMEM;
keir@20509 1526 back = flexarray_make(16, 1);
keir@20509 1527 if (!back)
keir@20509 1528 return ERROR_NOMEM;
keir@20509 1529
keir@20509 1530 device.backend_devid = console->devid;
keir@20509 1531 device.backend_domid = console->backend_domid;
keir@20509 1532 device.backend_kind = DEVICE_CONSOLE;
keir@20509 1533 device.devid = console->devid;
keir@20509 1534 device.domid = console->domid;
keir@20509 1535 device.kind = DEVICE_CONSOLE;
keir@20509 1536
keir@20516 1537 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1538 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
keir@20516 1539 flexarray_set(back, boffset++, "online");
keir@20516 1540 flexarray_set(back, boffset++, "1");
keir@20516 1541 flexarray_set(back, boffset++, "state");
keir@20509 1542 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1543 flexarray_set(back, boffset++, "domain");
keir@20516 1544 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1545 flexarray_set(back, boffset++, "protocol");
keir@20516 1546 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20509 1547
keir@20516 1548 flexarray_set(front, foffset++, "backend-id");
keir@20509 1549 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
keir@20516 1550 flexarray_set(front, foffset++, "state");
keir@20509 1551 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1552 flexarray_set(front, foffset++, "limit");
keir@20509 1553 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
keir@20516 1554 flexarray_set(front, foffset++, "protocol");
keir@20516 1555 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20516 1556 flexarray_set(front, foffset++, "type");
keir@20509 1557 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20516 1558 flexarray_set(front, foffset++, "xenconsoled");
keir@20509 1559 else
keir@20516 1560 flexarray_set(front, foffset++, "ioemu");
keir@20509 1561
keir@20509 1562 libxl_device_generic_add(ctx, &device,
keir@20509 1563 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1564 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1565 flexarray_free(back);
keir@20516 1566 flexarray_free(front);
keir@20509 1567
keir@20509 1568 return 0;
keir@20509 1569 }
keir@20509 1570
keir@20509 1571 /******************************************************************************/
keir@20509 1572 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
keir@20509 1573 {
keir@20509 1574 flexarray_t *front;
keir@20509 1575 flexarray_t *back;
keir@20509 1576 unsigned int boffset = 0;
keir@20509 1577 unsigned int foffset = 0;
keir@20509 1578 libxl_device device;
keir@20509 1579
keir@20509 1580 front = flexarray_make(16, 1);
keir@20509 1581 if (!front)
keir@20509 1582 return ERROR_NOMEM;
keir@20509 1583 back = flexarray_make(16, 1);
keir@20509 1584 if (!back)
keir@20509 1585 return ERROR_NOMEM;
keir@20509 1586
keir@20509 1587 device.backend_devid = vkb->devid;
keir@20509 1588 device.backend_domid = vkb->backend_domid;
keir@20509 1589 device.backend_kind = DEVICE_VKBD;
keir@20509 1590 device.devid = vkb->devid;
keir@20509 1591 device.domid = vkb->domid;
keir@20509 1592 device.kind = DEVICE_VKBD;
keir@20509 1593
keir@20516 1594 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1595 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
keir@20516 1596 flexarray_set(back, boffset++, "online");
keir@20516 1597 flexarray_set(back, boffset++, "1");
keir@20516 1598 flexarray_set(back, boffset++, "state");
keir@20509 1599 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1600 flexarray_set(back, boffset++, "domain");
keir@20516 1601 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20509 1602
keir@20516 1603 flexarray_set(front, foffset++, "backend-id");
keir@20509 1604 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
keir@20516 1605 flexarray_set(front, foffset++, "state");
keir@20509 1606 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1607
keir@20509 1608 libxl_device_generic_add(ctx, &device,
keir@20509 1609 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1610 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1611 flexarray_free(back);
keir@20516 1612 flexarray_free(front);
keir@20509 1613
keir@20509 1614 return 0;
keir@20462 1615 }
keir@20462 1616
keir@20462 1617 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1618 {
keir@20462 1619 return ERROR_NI;
keir@20462 1620 }
keir@20462 1621
keir@20462 1622 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1623 {
keir@20462 1624 return ERROR_NI;
keir@20462 1625 }
keir@20462 1626
keir@20637 1627 libxl_device_disk *libxl_device_disk_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20637 1628 {
keir@20637 1629 char *be_path_tap, *be_path_vbd;
keir@20637 1630 libxl_device_disk *disks = NULL;
keir@20637 1631 char **l = NULL;
keir@20637 1632 unsigned int numl;
keir@20637 1633 int num_disks = 0, i;
keir@20637 1634 char *type;
keir@20637 1635
keir@20637 1636 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
keir@20637 1637 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
keir@20637 1638
keir@20637 1639 l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
keir@20637 1640 if (l) {
keir@20637 1641 num_disks += numl;
keir@20637 1642 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
keir@20637 1643 for (i = 0; i < numl; i++) {
keir@20637 1644 disks[i].backend_domid = 0;
keir@20637 1645 disks[i].domid = domid;
keir@20637 1646 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, l[i]));
keir@20637 1647 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_vbd, l[i])), &(disks[i].phystype));
keir@20637 1648 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, l[i]));
keir@20637 1649 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, l[i])));
keir@20637 1650 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, l[i])), "w"))
keir@20637 1651 disks[i].readwrite = 1;
keir@20637 1652 else
keir@20637 1653 disks[i].readwrite = 0;
keir@20637 1654 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, l[i]))));
keir@20637 1655 disks[i].is_cdrom = !strcmp(type, "cdrom");
keir@20637 1656 }
keir@20637 1657 free(l);
keir@20637 1658 }
keir@20637 1659 l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
keir@20637 1660 if (l) {
keir@20637 1661 num_disks += numl;
keir@20637 1662 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
keir@20637 1663 for (i = 0; i < numl; i++) {
keir@20637 1664 disks[i].backend_domid = 0;
keir@20637 1665 disks[i].domid = domid;
keir@20637 1666 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, l[i]));
keir@20637 1667 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_tap, l[i])), &(disks[i].phystype));
keir@20637 1668 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, l[i]));
keir@20637 1669 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, l[i])));
keir@20637 1670 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, l[i])), "w"))
keir@20637 1671 disks[i].readwrite = 1;
keir@20637 1672 else
keir@20637 1673 disks[i].readwrite = 0;
keir@20637 1674 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, l[i]))));
keir@20637 1675 disks[i].is_cdrom = !strcmp(type, "cdrom");
keir@20637 1676 }
keir@20637 1677 free(l);
keir@20637 1678 }
keir@20637 1679 *num = num_disks;
keir@20637 1680 return disks;
keir@20637 1681 }
keir@20637 1682
keir@20637 1683 int libxl_cdrom_insert(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20637 1684 {
keir@20637 1685 int num, i;
keir@20637 1686 uint32_t stubdomid;
keir@20637 1687 libxl_device_disk *disks;
keir@20637 1688
keir@20637 1689 if (!disk->physpath) {
keir@20637 1690 disk->physpath = "";
keir@20637 1691 disk->phystype = PHYSTYPE_PHY;
keir@20637 1692 }
keir@20637 1693 disks = libxl_device_disk_list(ctx, domid, &num);
keir@20637 1694 for (i = 0; i < num; i++) {
keir@20637 1695 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
keir@20637 1696 /* found */
keir@20637 1697 break;
keir@20637 1698 }
keir@20637 1699 if (i == num) {
keir@20637 1700 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
keir@20637 1701 return -1;
keir@20637 1702 }
keir@20637 1703 libxl_device_disk_del(ctx, disks + i, 1);
keir@20637 1704 libxl_device_disk_add(ctx, domid, disk);
keir@20637 1705 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20637 1706 if (stubdomid) {
keir@20781 1707 disks[i].domid = stubdomid;
keir@20637 1708 libxl_device_disk_del(ctx, disks + i, 1);
keir@20781 1709 disk->domid = stubdomid;
keir@20637 1710 libxl_device_disk_add(ctx, stubdomid, disk);
keir@20781 1711 disk->domid = domid;
keir@20637 1712 }
keir@20637 1713 return 0;
keir@20637 1714 }
keir@20637 1715
keir@20462 1716 /******************************************************************************/
keir@20509 1717 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
keir@20509 1718 libxl_device_vfb *vfb,
keir@20509 1719 int num_console,
keir@20509 1720 libxl_device_console *console,
keir@20860 1721 libxl_device_model_info *info)
keir@20860 1722 {
keir@20509 1723 int i = 0, j = 0, num = 0;
keir@20509 1724 memset(info, 0x00, sizeof(libxl_device_model_info));
keir@20509 1725
keir@20509 1726 info->vnc = vfb->vnc;
keir@20509 1727 if (vfb->vnclisten)
keir@20509 1728 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
keir@20509 1729 info->vncdisplay = vfb->vncdisplay;
keir@20509 1730 info->vncunused = vfb->vncunused;
keir@21362 1731 if (vfb->vncpasswd)
keir@21362 1732 info->vncpasswd = vfb->vncpasswd;
keir@20509 1733 if (vfb->keymap)
keir@20509 1734 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
keir@20509 1735 info->sdl = vfb->sdl;
keir@20509 1736 info->opengl = vfb->opengl;
keir@20509 1737 for (i = 0; i < num_console; i++) {
keir@20509 1738 if (console->constype == CONSTYPE_IOEMU)
keir@20509 1739 num++;
keir@20509 1740 }
keir@20509 1741 if (num > 0) {
keir@20860 1742 uint32_t guest_domid;
keir@20860 1743 if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
keir@20599 1744 char *filename;
keir@20599 1745 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
keir@20599 1746 libxl_create_logfile(ctx, name, &filename);
keir@20599 1747 info->serial = libxl_sprintf(ctx, "file:%s", filename);
keir@20599 1748 free(filename);
keir@20599 1749 } else {
keir@20599 1750 info->serial = "pty";
keir@20599 1751 }
keir@20509 1752 num--;
keir@20509 1753 }
keir@20509 1754 if (num > 0) {
keir@20509 1755 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
keir@20509 1756 for (j = 0; j < num * 2; j = j + 2) {
keir@20509 1757 info->extra[j] = "-serial";
keir@20509 1758 info->extra[j + 1] = "pty";
keir@20509 1759 }
keir@20509 1760 info->extra[j] = NULL;
keir@20509 1761 }
keir@20509 1762 info->domid = vfb->domid;
keir@20509 1763 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
keir@21376 1764 info->device_model = libxl_abs_path(ctx, "qemu-dm", libxl_libexec_path());
keir@20509 1765 info->type = XENPV;
keir@20509 1766 return 0;
keir@20509 1767 }
keir@20509 1768
keir@20509 1769 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
keir@20514 1770 int num_console, libxl_device_console *console,
keir@20514 1771 struct libxl_device_model_starting **starting_r)
keir@20509 1772 {
keir@20509 1773 libxl_device_model_info info;
keir@20509 1774
keir@20509 1775 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
keir@20542 1776 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
keir@20509 1777 return 0;
keir@20509 1778 }
keir@20509 1779
keir@20509 1780 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
keir@20462 1781 {
keir@20509 1782 flexarray_t *front;
keir@20509 1783 flexarray_t *back;
keir@20509 1784 unsigned int boffset = 0;
keir@20509 1785 unsigned int foffset = 0;
keir@20509 1786 libxl_device device;
keir@20509 1787
keir@20509 1788 front = flexarray_make(16, 1);
keir@20509 1789 if (!front)
keir@20509 1790 return ERROR_NOMEM;
keir@20509 1791 back = flexarray_make(16, 1);
keir@20509 1792 if (!back)
keir@20509 1793 return ERROR_NOMEM;
keir@20509 1794
keir@20509 1795 device.backend_devid = vfb->devid;
keir@20509 1796 device.backend_domid = vfb->backend_domid;
keir@20509 1797 device.backend_kind = DEVICE_VFB;
keir@20509 1798 device.devid = vfb->devid;
keir@20509 1799 device.domid = vfb->domid;
keir@20509 1800 device.kind = DEVICE_VFB;
keir@20509 1801
keir@20516 1802 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1803 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
keir@20516 1804 flexarray_set(back, boffset++, "online");
keir@20516 1805 flexarray_set(back, boffset++, "1");
keir@20516 1806 flexarray_set(back, boffset++, "state");
keir@20509 1807 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1808 flexarray_set(back, boffset++, "domain");
keir@20516 1809 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1810 flexarray_set(back, boffset++, "vnc");
keir@20509 1811 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
keir@20516 1812 flexarray_set(back, boffset++, "vnclisten");
keir@20516 1813 flexarray_set(back, boffset++, vfb->vnclisten);
keir@21362 1814 flexarray_set(back, boffset++, "vncpasswd");
keir@21362 1815 flexarray_set(back, boffset++, vfb->vncpasswd);
keir@20516 1816 flexarray_set(back, boffset++, "vncdisplay");
keir@20509 1817 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
keir@20516 1818 flexarray_set(back, boffset++, "vncunused");
keir@20509 1819 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
keir@20516 1820 flexarray_set(back, boffset++, "sdl");
keir@20509 1821 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
keir@20516 1822 flexarray_set(back, boffset++, "opengl");
keir@20509 1823 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
keir@20509 1824 if (vfb->xauthority) {
keir@20516 1825 flexarray_set(back, boffset++, "xauthority");
keir@20516 1826 flexarray_set(back, boffset++, vfb->xauthority);
keir@20509 1827 }
keir@20509 1828 if (vfb->display) {
keir@20516 1829 flexarray_set(back, boffset++, "display");
keir@20516 1830 flexarray_set(back, boffset++, vfb->display);
keir@20509 1831 }
keir@20509 1832
keir@20516 1833 flexarray_set(front, foffset++, "backend-id");
keir@20509 1834 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
keir@20516 1835 flexarray_set(front, foffset++, "state");
keir@20509 1836 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1837
keir@20509 1838 libxl_device_generic_add(ctx, &device,
keir@20509 1839 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1840 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20509 1841 flexarray_free(front);
keir@20509 1842 flexarray_free(back);
keir@20509 1843
keir@20509 1844 return 0;
keir@20462 1845 }
keir@20462 1846
keir@20462 1847 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1848 {
keir@20462 1849 return ERROR_NI;
keir@20462 1850 }
keir@20462 1851
keir@20462 1852 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1853 {
keir@20462 1854 return ERROR_NI;
keir@20462 1855 }
keir@20462 1856
keir@20462 1857 /******************************************************************************/
keir@20482 1858
keir@20482 1859 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
keir@20482 1860 unsigned int bus, unsigned int dev,
keir@20482 1861 unsigned int func, unsigned int vdevfn)
keir@20482 1862 {
keir@20482 1863 pcidev->domain = domain;
keir@20482 1864 pcidev->bus = bus;
keir@20482 1865 pcidev->dev = dev;
keir@20482 1866 pcidev->func = func;
keir@20482 1867 pcidev->vdevfn = vdevfn;
keir@20482 1868 return 0;
keir@20482 1869 }
keir@20482 1870
keir@20482 1871 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
keir@20482 1872 {
keir@20482 1873 flexarray_t *front;
keir@20482 1874 flexarray_t *back;
keir@20482 1875 unsigned int boffset = 0;
keir@20482 1876 unsigned int foffset = 0;
keir@20482 1877 libxl_device device;
keir@20482 1878 int i;
keir@20482 1879
keir@20482 1880 front = flexarray_make(16, 1);
keir@20482 1881 if (!front)
keir@20482 1882 return ERROR_NOMEM;
keir@20482 1883 back = flexarray_make(16, 1);
keir@20482 1884 if (!back)
keir@20482 1885 return ERROR_NOMEM;
keir@20482 1886
keir@20512 1887 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
keir@20482 1888
keir@20482 1889 /* add pci device */
keir@20482 1890 device.backend_devid = 0;
keir@20482 1891 device.backend_domid = 0;
keir@20482 1892 device.backend_kind = DEVICE_PCI;
keir@20482 1893 device.devid = 0;
keir@20482 1894 device.domid = domid;
keir@20482 1895 device.kind = DEVICE_PCI;
keir@20482 1896
keir@20516 1897 flexarray_set(back, boffset++, "frontend-id");
keir@20482 1898 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
keir@20516 1899 flexarray_set(back, boffset++, "online");
keir@20516 1900 flexarray_set(back, boffset++, "1");
keir@20516 1901 flexarray_set(back, boffset++, "state");
keir@20482 1902 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1903 flexarray_set(back, boffset++, "domain");
keir@20516 1904 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20482 1905 for (i = 0; i < num; i++) {
keir@20482 1906 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
keir@20482 1907 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1908 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
keir@20482 1909 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1910 if (pcidev->vdevfn) {
keir@20482 1911 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
keir@20482 1912 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1913 }
keir@20482 1914 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
keir@20482 1915 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1916 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
keir@20482 1917 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1918 }
keir@20516 1919 flexarray_set(back, boffset++, "num_devs");
keir@20482 1920 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
keir@20482 1921
keir@20516 1922 flexarray_set(front, foffset++, "backend-id");
keir@20482 1923 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
keir@20516 1924 flexarray_set(front, foffset++, "state");
keir@20482 1925 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1926
keir@20482 1927 libxl_device_generic_add(ctx, &device,
keir@20482 1928 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20482 1929 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20482 1930
keir@20482 1931 flexarray_free(back);
keir@20482 1932 flexarray_free(front);
keir@20482 1933 return 0;
keir@20482 1934 }
keir@20482 1935
keir@20482 1936 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1937 {
keir@20482 1938 flexarray_t *back;
keir@20482 1939 char *num_devs, *be_path;
keir@20482 1940 int num = 0;
keir@20482 1941 unsigned int boffset = 0;
keir@20482 1942 xs_transaction_t t;
keir@20482 1943
keir@20516 1944 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 1945 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 1946 if (!num_devs)
keir@20482 1947 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
keir@20482 1948
keir@20482 1949 if (!is_hvm(ctx, domid)) {
keir@20482 1950 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
keir@20482 1951 return -1;
keir@20482 1952 }
keir@20482 1953
keir@20482 1954 back = flexarray_make(16, 1);
keir@20482 1955 if (!back)
keir@20482 1956 return ERROR_NOMEM;
keir@20482 1957
keir@20512 1958 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
keir@20482 1959 num = atoi(num_devs);
keir@20482 1960 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
keir@20482 1961 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1962 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
keir@20482 1963 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1964 if (pcidev->vdevfn) {
keir@20482 1965 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
keir@20482 1966 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1967 }
keir@20482 1968 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
keir@20482 1969 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1970 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
keir@20482 1971 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1972 flexarray_set(back, boffset++, "num_devs");
keir@20482 1973 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
keir@20516 1974 flexarray_set(back, boffset++, "state");
keir@20482 1975 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
keir@20482 1976
keir@20482 1977 retry_transaction:
keir@20482 1978 t = xs_transaction_start(ctx->xsh);
keir@20482 1979 libxl_xs_writev(ctx, t, be_path,
keir@20482 1980 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
keir@20482 1981 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 1982 if (errno == EAGAIN)
keir@20482 1983 goto retry_transaction;
keir@20482 1984
keir@20482 1985 flexarray_free(back);
keir@20482 1986 return 0;
keir@20482 1987 }
keir@20482 1988
keir@20482 1989 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20482 1990 {
keir@20542 1991 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
keir@20542 1992 int num, i, j;
keir@20482 1993 xs_transaction_t t;
keir@20482 1994 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
keir@20482 1995
keir@20516 1996 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 1997 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
keir@20482 1998 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
keir@20482 1999 if (!num_devs)
keir@20482 2000 return -1;
keir@20482 2001 num = atoi(num_devs);
keir@20482 2002
keir@20482 2003 if (!is_hvm(ctx, domid)) {
keir@20482 2004 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 2005 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20482 2006 return -1;
keir@20482 2007 }
keir@20482 2008 }
keir@20482 2009
keir@20482 2010 for (i = 0; i < num; i++) {
keir@20482 2011 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 2012 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 2013 if (domain == pcidev->domain && bus == pcidev->bus &&
keir@20482 2014 pcidev->dev == dev && pcidev->func == func) {
keir@20482 2015 break;
keir@20482 2016 }
keir@20482 2017 }
keir@20482 2018 if (i == num) {
keir@20512 2019 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
keir@20482 2020 return -1;
keir@20482 2021 }
keir@20482 2022
keir@20482 2023 retry_transaction:
keir@20482 2024 t = xs_transaction_start(ctx->xsh);
keir@20542 2025 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
keir@20482 2026 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
keir@20482 2027 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 2028 if (errno == EAGAIN)
keir@20482 2029 goto retry_transaction;
keir@20542 2030
keir@20542 2031 if (!is_hvm(ctx, domid)) {
keir@20542 2032 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 2033 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20542 2034 return -1;
keir@20542 2035 }
keir@20542 2036 }
keir@20542 2037
keir@20542 2038 retry_transaction2:
keir@20542 2039 t = xs_transaction_start(ctx->xsh);
keir@20542 2040 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
keir@20542 2041 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
keir@20542 2042 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20542 2043 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
keir@20542 2044 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20542 2045 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20542 2046 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
keir@20542 2047 for (j = i + 1; j < num; j++) {
keir@20542 2048 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
keir@20542 2049 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2050 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2051 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2052 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
keir@20542 2053 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2054 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2055 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2056 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
keir@20542 2057 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2058 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2059 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2060 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
keir@20542 2061 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2062 if (tmp) {
keir@20542 2063 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2064 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2065 }
keir@20542 2066 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
keir@20542 2067 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2068 if (tmp) {
keir@20542 2069 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2070 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2071 }
keir@20542 2072 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
keir@20542 2073 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2074 if (tmp) {
keir@20542 2075 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2076 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2077 }
keir@20542 2078 }
keir@20542 2079 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 2080 if (errno == EAGAIN)
keir@20542 2081 goto retry_transaction2;
keir@20542 2082
keir@20542 2083 if (num == 1) {
keir@20542 2084 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
keir@20542 2085 libxl_device_destroy(ctx, be_path, 1);
keir@20542 2086 xs_rm(ctx->xsh, XBT_NULL, be_path);
keir@20542 2087 xs_rm(ctx->xsh, XBT_NULL, fe_path);
keir@20542 2088 return 0;
keir@20542 2089 }
keir@20542 2090
keir@20482 2091 return 0;
keir@20462 2092 }
keir@20462 2093
keir@20482 2094 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 2095 {
keir@20779 2096 char *path;
keir@20482 2097 char *state, *vdevfn;
keir@20482 2098 int rc, hvm;
keir@20542 2099 int stubdomid = 0;
keir@20482 2100
keir@20482 2101 /* TODO: check if the device can be assigned */
keir@20482 2102
keir@20482 2103 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2104
keir@20779 2105 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20779 2106 if (stubdomid != 0) {
keir@20542 2107 libxl_device_pci pcidev_s = *pcidev;
keir@20542 2108 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
keir@20542 2109 }
keir@20542 2110
keir@20482 2111 hvm = is_hvm(ctx, domid);
keir@20482 2112 if (hvm) {
keir@20831 2113 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
keir@20482 2114 return -1;
keir@20482 2115 }
keir@20779 2116 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2117 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20779 2118 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2119 if (pcidev->vdevfn)
keir@20482 2120 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
keir@20482 2121 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
keir@20482 2122 else
keir@20482 2123 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 2124 pcidev->bus, pcidev->dev, pcidev->func);
keir@20779 2125 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
keir@20482 2126 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
keir@20831 2127 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", NULL, NULL) < 0)
keir@20512 2128 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20779 2129 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2130 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 2131 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
keir@20779 2132 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2133 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 2134 } else {
keir@20542 2135 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 2136 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2137 FILE *f = fopen(sysfs_path, "r");
keir@20482 2138 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 2139 int irq = 0;
keir@20482 2140 int i;
keir@20482 2141
keir@20482 2142 if (f == NULL) {
keir@20512 2143 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2144 return -1;
keir@20482 2145 }
keir@20482 2146 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20512 2147 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
keir@20482 2148 size = end - start + 1;
keir@20482 2149 if (start) {
keir@20482 2150 if (flags & PCI_BAR_IO) {
keir@20482 2151 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
keir@20482 2152 if (rc < 0)
keir@20512 2153 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 2154 } else {
keir@20482 2155 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 2156 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
keir@20482 2157 if (rc < 0)
keir@20512 2158 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 2159 }
keir@20482 2160 }
keir@20482 2161 }
keir@20482 2162 fclose(f);
keir@20542 2163 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 2164 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2165 f = fopen(sysfs_path, "r");
keir@20482 2166 if (f == NULL) {
keir@20512 2167 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2168 goto out;
keir@20482 2169 }
keir@20482 2170 fscanf(f, "%u", &irq);
keir@20482 2171 if (irq) {
keir@20482 2172 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
keir@20482 2173 if (rc < 0) {
keir@20512 2174 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
keir@20482 2175 }
keir@20482 2176 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
keir@20482 2177 if (rc < 0) {
keir@20512 2178 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
keir@20482 2179 }
keir@20482 2180 }
keir@20482 2181 fclose(f);
keir@20482 2182 }
keir@20482 2183 out:
keir@20860 2184 if (!libxl_is_stubdom(ctx, domid, NULL)) {
keir@20779 2185 rc = xc_assign_device(ctx->xch, domid, pcidev->value);
keir@20779 2186 if (rc < 0)
keir@20542 2187 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
keir@20542 2188 }
keir@20482 2189
keir@20482 2190 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
keir@20482 2191 return 0;
keir@20462 2192 }
keir@20462 2193
keir@20482 2194 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 2195 {
keir@20779 2196 char *path;
keir@20482 2197 char *state;
keir@20482 2198 int hvm, rc;
keir@20542 2199 int stubdomid = 0;
keir@20482 2200
keir@20482 2201 /* TODO: check if the device can be detached */
keir@20542 2202 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
keir@20482 2203
keir@20482 2204 hvm = is_hvm(ctx, domid);
keir@20482 2205 if (hvm) {
keir@20831 2206 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
keir@20482 2207 return -1;
keir@20482 2208 }
keir@20779 2209 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2210 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20779 2211 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2212 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 2213 pcidev->bus, pcidev->dev, pcidev->func);
keir@20779 2214 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
keir@20482 2215 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
keir@20831 2216 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", NULL, NULL) < 0) {
keir@20512 2217 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20482 2218 return -1;
keir@20482 2219 }
keir@20779 2220 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2221 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 2222 } else {
keir@20542 2223 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 2224 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2225 FILE *f = fopen(sysfs_path, "r");
keir@20482 2226 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 2227 int irq = 0;
keir@20482 2228 int i;
keir@20482 2229
keir@20482 2230 if (f == NULL) {
keir@20512 2231 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2232 goto skip1;
keir@20482 2233 }
keir@20482 2234 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20482 2235 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
keir@20482 2236 size = end - start + 1;
keir@20482 2237 if (start) {
keir@20482 2238 if (flags & PCI_BAR_IO) {
keir@20482 2239 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
keir@20482 2240 if (rc < 0)
keir@20512 2241 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 2242 } else {
keir@20482 2243 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 2244 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
keir@20482 2245 if (rc < 0)
keir@20512 2246 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 2247 }
keir@20482 2248 }
keir@20482 2249 }
keir@20482 2250 fclose(f);
keir@20482 2251 skip1:
keir@20542 2252 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 2253 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2254 f = fopen(sysfs_path, "r");
keir@20482 2255 if (f == NULL) {
keir@20512 2256 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2257 goto out;
keir@20482 2258 }
keir@20482 2259 fscanf(f, "%u", &irq);
keir@20482 2260 if (irq) {
keir@20482 2261 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
keir@20482 2262 if (rc < 0) {
keir@20512 2263 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
keir@20482 2264 }
keir@20482 2265 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
keir@20482 2266 if (rc < 0) {
keir@20512 2267 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
keir@20482 2268 }
keir@20482 2269 }
keir@20482 2270 fclose(f);
keir@20482 2271 }
keir@20482 2272 out:
keir@20482 2273 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2274
keir@20860 2275 if (!libxl_is_stubdom(ctx, domid, NULL)) {
keir@20779 2276 rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
keir@20779 2277 if (rc < 0)
keir@20542 2278 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
keir@20542 2279 }
keir@20542 2280
keir@20779 2281 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20779 2282 if (stubdomid != 0) {
keir@20542 2283 libxl_device_pci pcidev_s = *pcidev;
keir@20542 2284 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
keir@20542 2285 }
keir@20542 2286
keir@20482 2287 return 0;
keir@20462 2288 }
keir@20482 2289
keir@20482 2290 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20482 2291 {
keir@20482 2292 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
keir@20482 2293 int n, i;
keir@20482 2294 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
keir@20482 2295 libxl_device_pci *pcidevs;
keir@20482 2296
keir@20516 2297 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 2298 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 2299 if (!num_devs) {
keir@20482 2300 *num = 0;
keir@20482 2301 return NULL;
keir@20482 2302 }
keir@20482 2303 n = atoi(num_devs);
keir@20779 2304 pcidevs = calloc(n, sizeof(libxl_device_pci));
keir@20482 2305 *num = n;
keir@20482 2306
keir@20482 2307 for (i = 0; i < n; i++) {
keir@20482 2308 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 2309 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 2310 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20482 2311 if (xsvdevfn)
keir@20482 2312 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
keir@20482 2313 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
keir@20482 2314 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20482 2315 if (xsopts) {
keir@20482 2316 char *saveptr;
keir@20482 2317 char *p = strtok_r(xsopts, ",=", &saveptr);
keir@20482 2318 do {
keir@20482 2319 while (*p == ' ')
keir@20482 2320 p++;
keir@20482 2321 if (!strcmp(p, "msitranslate")) {
keir@20482 2322 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 2323 pcidevs[i].msitranslate = atoi(p);
keir@20482 2324 } else if (!strcmp(p, "power_mgmt")) {
keir@20482 2325 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 2326 pcidevs[i].power_mgmt = atoi(p);
keir@20482 2327 }
keir@20482 2328 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
keir@20482 2329 }
keir@20482 2330 }
keir@20482 2331 return pcidevs;
keir@20482 2332 }
keir@20482 2333
keir@20482 2334 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20482 2335 {
keir@20482 2336 libxl_device_pci *pcidevs;
keir@20482 2337 int num, i;
keir@20482 2338
keir@20482 2339 pcidevs = libxl_device_pci_list(ctx, domid, &num);
keir@20482 2340 for (i = 0; i < num; i++) {
keir@20482 2341 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
keir@20482 2342 return -1;
keir@20482 2343 }
keir@20516 2344 free(pcidevs);
keir@20482 2345 return 0;
keir@20482 2346 }
keir@20482 2347
keir@20647 2348 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
keir@20647 2349 {
keir@20647 2350 int rc = 0;
keir@20647 2351 uint32_t videoram;
keir@20647 2352 char *videoram_s = NULL;
keir@20647 2353 char *dompath = libxl_xs_get_dompath(ctx, domid);
keir@21374 2354 xc_domaininfo_t info;
keir@21374 2355 struct libxl_dominfo ptr;
keir@21374 2356 char *uuid;
keir@20542 2357
keir@20647 2358 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
keir@21314 2359 videoram = videoram_s ? atoi(videoram_s) : 0;
keir@20647 2360
keir@20647 2361 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
keir@21374 2362
keir@21374 2363 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
keir@21374 2364 if (rc != 1 || info.domain != domid)
keir@21374 2365 return rc;
keir@21374 2366 xcinfo2xlinfo(&info, &ptr);
keir@21374 2367 uuid = libxl_uuid2string(ctx, ptr.uuid);
keir@21374 2368 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/vm/%s/memory", uuid), "%lu", target_memkb / 1024);
keir@21374 2369
keir@20779 2370 rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT);
keir@20779 2371 if (rc != 0)
keir@20779 2372 return rc;
keir@20647 2373 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
keir@20647 2374 return rc;
keir@20647 2375 }
keir@20875 2376
keir@20875 2377 int libxl_button_press(struct libxl_ctx *ctx, uint32_t domid, libxl_button button)
keir@20875 2378 {
keir@20875 2379 int rc = -1;
keir@20875 2380
keir@20875 2381 switch (button) {
keir@20875 2382 case POWER_BUTTON:
keir@20875 2383 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
keir@20875 2384 break;
keir@20875 2385 case SLEEP_BUTTON:
keir@20875 2386 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
keir@20875 2387 break;
keir@20875 2388 default:
keir@20875 2389 break;
keir@20875 2390 }
keir@20875 2391
keir@20875 2392 return rc;
keir@20875 2393 }
keir@21141 2394
keir@21141 2395 int libxl_get_physinfo(struct libxl_ctx *ctx, struct libxl_physinfo *physinfo)
keir@21141 2396 {
keir@21141 2397 xc_physinfo_t xcphysinfo = { 0 };
keir@21141 2398 int rc;
keir@21141 2399
keir@21141 2400 rc = xc_physinfo(ctx->xch, &xcphysinfo);
keir@21141 2401 if (rc != 0) {
keir@21141 2402 return rc;
keir@21141 2403 }
keir@21141 2404 physinfo->threads_per_core = xcphysinfo.threads_per_core;
keir@21141 2405 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
keir@21142 2406 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
keir@21141 2407 physinfo->nr_cpus = xcphysinfo.nr_cpus;
keir@21141 2408 physinfo->cpu_khz = xcphysinfo.cpu_khz;
keir@21141 2409 physinfo->total_pages = xcphysinfo.total_pages;
keir@21141 2410 physinfo->free_pages = xcphysinfo.free_pages;
keir@21141 2411 physinfo->scrub_pages = xcphysinfo.scrub_pages;
keir@21264 2412 physinfo->nr_nodes = xcphysinfo.nr_nodes;
keir@21264 2413 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
keir@21264 2414 physinfo->phys_cap = xcphysinfo.capabilities;
keir@21264 2415
keir@21141 2416 return 0;
keir@21141 2417 }
keir@21141 2418
keir@21266 2419 const libxl_version_info* libxl_get_version_info(struct libxl_ctx *ctx)
keir@21266 2420 {
keir@21266 2421 union {
keir@21266 2422 xen_extraversion_t xen_extra;
keir@21266 2423 xen_compile_info_t xen_cc;
keir@21266 2424 xen_changeset_info_t xen_chgset;
keir@21266 2425 xen_capabilities_info_t xen_caps;
keir@21266 2426 xen_platform_parameters_t p_parms;
keir@21266 2427 xen_commandline_t xen_commandline;
keir@21266 2428 } u;
keir@21266 2429 long xen_version;
keir@21266 2430 libxl_version_info *info = &ctx->version_info;
keir@21266 2431
keir@21266 2432 if (info->xen_version_extra != NULL)
keir@21266 2433 return info;
keir@21266 2434
keir@21266 2435 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
keir@21266 2436 info->xen_version_major = xen_version >> 16;
keir@21266 2437 info->xen_version_minor = xen_version & 0xFF;
keir@21266 2438 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
keir@21266 2439 info->xen_version_extra = libxl_sprintf(ctx, "%s", u.xen_extra);
keir@21266 2440
keir@21266 2441 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
keir@21266 2442 info->compiler = libxl_sprintf(ctx, "%s", u.xen_cc.compiler);
keir@21266 2443 info->compile_by = libxl_sprintf(ctx, "%s", u.xen_cc.compile_by);
keir@21266 2444 info->compile_domain = libxl_sprintf(ctx, "%s", u.xen_cc.compile_domain);
keir@21266 2445 info->compile_date = libxl_sprintf(ctx, "%s", u.xen_cc.compile_date);
keir@21266 2446
keir@21266 2447 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
keir@21266 2448 info->capabilities = libxl_sprintf(ctx, "%s", u.xen_caps);
keir@21266 2449
keir@21266 2450 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
keir@21266 2451 info->changeset = libxl_sprintf(ctx, "%s", u.xen_chgset);
keir@21266 2452
keir@21266 2453 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
keir@21266 2454 info->virt_start = u.p_parms.virt_start;
keir@21266 2455
keir@21266 2456 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
keir@21266 2457
keir@21266 2458 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
keir@21266 2459 info->commandline = libxl_sprintf(ctx, "%s", u.xen_commandline);
keir@21266 2460
keir@21266 2461 return info;
keir@21266 2462 }
keir@21266 2463
keir@21141 2464 struct libxl_vcpuinfo *libxl_list_vcpu(struct libxl_ctx *ctx, uint32_t domid,
keir@21141 2465 int *nb_vcpu, int *cpusize)
keir@21141 2466 {
keir@21141 2467 struct libxl_vcpuinfo *ptr, *ret;
keir@21141 2468 xc_domaininfo_t domaininfo;
keir@21141 2469 xc_vcpuinfo_t vcpuinfo;
keir@21141 2470 xc_physinfo_t physinfo = { 0 };
keir@21141 2471
keir@21141 2472 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
keir@21141 2473 return NULL;
keir@21141 2474 }
keir@21141 2475 if (xc_physinfo(ctx->xch, &physinfo) == -1) {
keir@21141 2476 return NULL;
keir@21141 2477 }
keir@21141 2478 *cpusize = physinfo.max_cpu_id + 1;
keir@21141 2479 ptr = libxl_calloc(ctx, domaininfo.max_vcpu_id + 1, sizeof (struct libxl_vcpuinfo));
keir@21141 2480 if (!ptr) {
keir@21141 2481 return NULL;
keir@21141 2482 }
keir@21141 2483
keir@21141 2484 ret = ptr;
keir@21141 2485 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
keir@21141 2486 ptr->cpumap = libxl_calloc(ctx, (*cpusize + 63) / 64, sizeof (uint64_t));
keir@21141 2487 if (!ptr->cpumap) {
keir@21141 2488 return NULL;
keir@21141 2489 }
keir@21141 2490 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
keir@21141 2491 return NULL;
keir@21141 2492 }
keir@21141 2493 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap, *cpusize) == -1) {
keir@21141 2494 return NULL;
keir@21141 2495 }
keir@21141 2496 ptr->vcpuid = *nb_vcpu;
keir@21141 2497 ptr->cpu = vcpuinfo.cpu;
keir@21141 2498 ptr->online = !!vcpuinfo.online;
keir@21141 2499 ptr->blocked = !!vcpuinfo.blocked;
keir@21141 2500 ptr->running = !!vcpuinfo.running;
keir@21141 2501 ptr->vcpu_time = vcpuinfo.cpu_time;
keir@21141 2502 }
keir@21141 2503 return ret;
keir@21141 2504 }
keir@21142 2505
keir@21142 2506 int libxl_set_vcpuaffinity(struct libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
keir@21142 2507 uint64_t *cpumap, int cpusize)
keir@21142 2508 {
keir@21142 2509 return (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap, cpusize));
keir@21142 2510 }
keir@21143 2511
keir@21143 2512 int libxl_set_vcpucount(struct libxl_ctx *ctx, uint32_t domid, uint32_t count)
keir@21143 2513 {
keir@21143 2514 xc_domaininfo_t domaininfo;
keir@21143 2515 char *dompath;
keir@21143 2516 int i;
keir@21143 2517
keir@21143 2518 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
keir@21143 2519 return ERROR_FAIL;
keir@21143 2520 }
keir@21143 2521 if (!count || ((domaininfo.max_vcpu_id + 1) < count)) {
keir@21143 2522 return ERROR_INVAL;
keir@21143 2523 }
keir@21143 2524 if (!(dompath = libxl_xs_get_dompath(ctx, domid)))
keir@21143 2525 return ERROR_FAIL;
keir@21143 2526
keir@21143 2527 for (i = 0; i <= domaininfo.max_vcpu_id; ++i) {
keir@21143 2528 libxl_xs_write(ctx, XBT_NULL,
keir@21143 2529 libxl_sprintf(ctx, "%s/cpu/%u/availability", dompath, i),
keir@21143 2530 "%s", ((1 << i) & ((1 << count) - 1)) ? "online" : "offline");
keir@21143 2531 }
keir@21143 2532 return 0;
keir@21143 2533 }
keir@21265 2534
keir@21265 2535 /*
keir@21265 2536 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
keir@21265 2537 * or -1 if an error occured.
keir@21265 2538 */
keir@21265 2539 int libxl_get_sched_id(struct libxl_ctx *ctx)
keir@21265 2540 {
keir@21265 2541 int sched, ret;
keir@21265 2542
keir@21265 2543 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0)
keir@21265 2544 return ret;
keir@21265 2545 return sched;
keir@21265 2546 }
keir@21292 2547
keir@21292 2548 int libxl_sched_credit_domain_get(struct libxl_ctx *ctx, uint32_t domid, struct libxl_sched_credit *scinfo)
keir@21292 2549 {
keir@21292 2550 struct xen_domctl_sched_credit sdom;
keir@21292 2551 int rc;
keir@21292 2552
keir@21292 2553 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
keir@21292 2554 if (rc != 0)
keir@21292 2555 return rc;
keir@21292 2556
keir@21292 2557 scinfo->weight = sdom.weight;
keir@21292 2558 scinfo->cap = sdom.cap;
keir@21292 2559
keir@21292 2560 return 0;
keir@21292 2561 }
keir@21292 2562
keir@21292 2563 int libxl_sched_credit_domain_set(struct libxl_ctx *ctx, uint32_t domid, struct libxl_sched_credit *scinfo)
keir@21292 2564 {
keir@21292 2565 struct xen_domctl_sched_credit sdom;
keir@21292 2566 xc_domaininfo_t domaininfo;
keir@21292 2567 int rc;
keir@21292 2568
keir@21292 2569 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
keir@21292 2570 if (rc != 1 || domaininfo.domain != domid)
keir@21292 2571 return rc;
keir@21292 2572
keir@21292 2573
keir@21292 2574 if (scinfo->weight < 1 || scinfo->weight > 65535) {
keir@21292 2575 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
keir@21292 2576 "Cpu weight out of range, valid values are within range from 1 to 65535");
keir@21292 2577 return -1;
keir@21292 2578 }
keir@21292 2579
keir@21292 2580 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
keir@21292 2581 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
keir@21292 2582 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
keir@21292 2583 ((domaininfo.max_vcpu_id + 1) * 100));
keir@21292 2584 return -1;
keir@21292 2585 }
keir@21292 2586
keir@21292 2587 sdom.weight = scinfo->weight;
keir@21292 2588 sdom.cap = scinfo->cap;
keir@21292 2589
keir@21292 2590 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
keir@21292 2591 if (rc != 0)
keir@21292 2592 return rc;
keir@21292 2593
keir@21292 2594 return 0;
keir@21292 2595 }
keir@21292 2596