debuggers.hg

annotate tools/libxl/libxl.c @ 21274:49e3aa844902

xl: support oos domain parameter

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