debuggers.hg

annotate tools/libxl/libxl.c @ 21292:3e5383b537c5

xl: Add subcommand "xl sched-credit"

Signed-off-by: Yu Zhiguo <yuzg@cn.fujitsu.com>
Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 04 11:09:32 2010 +0100 (2010-05-04)
parents a167ea374f26
children 67303cccfb79
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@21283 742 char *cmd = libxl_sprintf(
keir@21283 743 ctx, "%s/xenconsole %d --num %d",
keir@21283 744 libxl_private_bindir_path(), domid, cons_num);
keir@20564 745 return (system(cmd) != 0) ? ERROR_FAIL : 0;
keir@20564 746 }
keir@20564 747
keir@20462 748 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
keir@20462 749 libxl_device_model_info *info,
keir@20462 750 libxl_device_nic *vifs,
keir@20462 751 int num_vifs)
keir@20462 752 {
keir@20462 753 int num = 0, i;
keir@20462 754 flexarray_t *dm_args;
keir@20462 755 dm_args = flexarray_make(16, 1);
keir@20462 756 if (!dm_args)
keir@20462 757 return NULL;
keir@20462 758
keir@20516 759 flexarray_set(dm_args, num++, "qemu-dm");
keir@20516 760 flexarray_set(dm_args, num++, "-d");
keir@20462 761
keir@20462 762 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
keir@20462 763
keir@20462 764 if (info->dom_name) {
keir@20516 765 flexarray_set(dm_args, num++, "-domain-name");
keir@20516 766 flexarray_set(dm_args, num++, info->dom_name);
keir@20462 767 }
keir@20462 768 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
keir@20516 769 flexarray_set(dm_args, num++, "-vnc");
keir@20462 770 if (info->vncdisplay) {
keir@20462 771 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
keir@20462 772 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:%d", info->vnclisten, info->vncdisplay));
keir@20462 773 } else {
keir@20462 774 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
keir@20462 775 }
keir@20462 776 } else if (info->vnclisten) {
keir@20462 777 if (strchr(info->vnclisten, ':') != NULL) {
keir@20516 778 flexarray_set(dm_args, num++, info->vnclisten);
keir@20462 779 } else {
keir@20462 780 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
keir@20462 781 }
keir@20462 782 } else {
keir@20516 783 flexarray_set(dm_args, num++, "127.0.0.1:0");
keir@20462 784 }
keir@20462 785 if (info->vncunused) {
keir@20516 786 flexarray_set(dm_args, num++, "-vncunused");
keir@20462 787 }
keir@20462 788 }
keir@20462 789 if (info->sdl || info->opengl) {
keir@20516 790 flexarray_set(dm_args, num++, "-sdl");
keir@20462 791 if (info->opengl) {
keir@20516 792 flexarray_set(dm_args, num++, "-disable-opengl");
keir@20462 793 }
keir@20462 794 }
keir@20462 795 if (info->keymap) {
keir@20516 796 flexarray_set(dm_args, num++, "-k");
keir@20516 797 flexarray_set(dm_args, num++, info->keymap);
keir@20462 798 }
keir@20462 799 if (info->nographic && (!info->sdl && !info->vnc)) {
keir@20516 800 flexarray_set(dm_args, num++, "-nographic");
keir@20462 801 }
keir@20462 802 if (info->serial) {
keir@20516 803 flexarray_set(dm_args, num++, "-serial");
keir@20516 804 flexarray_set(dm_args, num++, info->serial);
keir@20462 805 }
keir@20509 806 if (info->type == XENFV) {
keir@20509 807 if (info->videoram) {
keir@20516 808 flexarray_set(dm_args, num++, "-videoram");
keir@20509 809 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
keir@20509 810 }
keir@20509 811 if (info->stdvga) {
keir@20516 812 flexarray_set(dm_args, num++, "-std-vga");
keir@20509 813 }
keir@20509 814
keir@20509 815 if (info->boot) {
keir@20516 816 flexarray_set(dm_args, num++, "-boot");
keir@20516 817 flexarray_set(dm_args, num++, info->boot);
keir@20509 818 }
keir@20509 819 if (info->usb) {
keir@20516 820 flexarray_set(dm_args, num++, "-usb");
keir@20509 821 if (info->usbdevice) {
keir@20516 822 flexarray_set(dm_args, num++, "-usbdevice");
keir@20516 823 flexarray_set(dm_args, num++, info->usbdevice);
keir@20509 824 }
keir@20509 825 }
keir@20509 826 if (info->apic) {
keir@20516 827 flexarray_set(dm_args, num++, "-acpi");
keir@20509 828 }
keir@20509 829 for (i = 0; i < num_vifs; i++) {
keir@20509 830 if (vifs[i].nictype == NICTYPE_IOEMU) {
keir@20780 831 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20780 832 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
keir@20780 833 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
keir@20781 834 if (!vifs[i].ifname)
keir@20781 835 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid - 1);
keir@20516 836 flexarray_set(dm_args, num++, "-net");
keir@20509 837 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
keir@20780 838 vifs[i].devid, smac, vifs[i].model));
keir@20516 839 flexarray_set(dm_args, num++, "-net");
keir@20509 840 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
keir@20509 841 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
keir@20509 842 }
keir@20462 843 }
keir@20462 844 }
keir@20565 845 if (info->saved_state) {
keir@20565 846 flexarray_set(dm_args, num++, "-loadvm");
keir@20565 847 flexarray_set(dm_args, num++, info->saved_state);
keir@20565 848 }
keir@20509 849 for (i = 0; info->extra && info->extra[i] != NULL; i++)
keir@20509 850 flexarray_set(dm_args, num++, info->extra[i]);
keir@20516 851 flexarray_set(dm_args, num++, "-M");
keir@20509 852 if (info->type == XENPV)
keir@20516 853 flexarray_set(dm_args, num++, "xenpv");
keir@20509 854 else
keir@20516 855 flexarray_set(dm_args, num++, "xenfv");
keir@20462 856 flexarray_set(dm_args, num++, NULL);
keir@20462 857
keir@20462 858 return (char **) flexarray_contents(dm_args);
keir@20462 859 }
keir@20462 860
keir@20834 861 void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
keir@20834 862 {
keir@20514 863 struct libxl_device_model_starting *starting = for_spawn;
keir@20514 864 char *kvs[3];
keir@20820 865 int rc;
keir@20820 866 struct xs_handle *xsh;
keir@20514 867
keir@20820 868 xsh = xs_daemon_open();
keir@20514 869 /* we mustn't use the parent's handle in the child */
keir@20514 870
keir@20514 871 kvs[0] = "image/device-model-pid";
keir@20820 872 asprintf(&kvs[1], "%d", innerchild);
keir@20514 873 kvs[2] = NULL;
keir@20820 874
keir@20820 875 rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
keir@20820 876 if (rc)
keir@20820 877 return;
keir@20820 878 xs_daemon_close(xsh);
keir@20514 879 }
keir@20514 880
keir@20542 881 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
keir@20542 882 libxl_device_model_info *info,
keir@20542 883 libxl_device_vfb *vfb,
keir@20542 884 libxl_device_vkb *vkb)
keir@20542 885 {
keir@20542 886 memset(vfb, 0x00, sizeof(libxl_device_vfb));
keir@20542 887 memset(vkb, 0x00, sizeof(libxl_device_vkb));
keir@20542 888
keir@20542 889 vfb->backend_domid = 0;
keir@20542 890 vfb->devid = 0;
keir@20542 891 vfb->vnc = info->vnc;
keir@20542 892 vfb->vnclisten = info->vnclisten;
keir@20542 893 vfb->vncdisplay = info->vncdisplay;
keir@20542 894 vfb->vncunused = info->vncunused;
keir@20542 895 vfb->keymap = info->keymap;
keir@20542 896 vfb->sdl = info->sdl;
keir@20542 897 vfb->opengl = info->opengl;
keir@20542 898
keir@20542 899 vkb->backend_domid = 0;
keir@20542 900 vkb->devid = 0;
keir@20542 901 return 0;
keir@20542 902 }
keir@20542 903
keir@20542 904 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
keir@20542 905 {
keir@20542 906 int i;
keir@20542 907 char *vm_path;
keir@20542 908 char *dmargs, *path;
keir@20542 909 int dmargs_size;
keir@20542 910 struct xs_permissions roperm[2];
keir@20542 911 xs_transaction_t t;
keir@20542 912
keir@20542 913 roperm[0].id = 0;
keir@20542 914 roperm[0].perms = XS_PERM_NONE;
keir@20542 915 roperm[1].id = domid;
keir@20542 916 roperm[1].perms = XS_PERM_READ;
keir@20542 917
keir@20542 918 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
keir@20542 919
keir@20542 920 i = 0;
keir@20542 921 dmargs_size = 0;
keir@20542 922 while (args[i] != NULL) {
keir@20542 923 dmargs_size = dmargs_size + strlen(args[i]) + 1;
keir@20542 924 i++;
keir@20542 925 }
keir@20542 926 dmargs_size++;
keir@20542 927 dmargs = (char *) malloc(dmargs_size);
keir@20542 928 i = 1;
keir@20542 929 dmargs[0] = '\0';
keir@20542 930 while (args[i] != NULL) {
keir@20542 931 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
keir@20542 932 strcat(dmargs, " ");
keir@20542 933 strcat(dmargs, args[i]);
keir@20542 934 }
keir@20542 935 i++;
keir@20542 936 }
keir@20542 937 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
keir@20542 938
keir@20542 939 retry_transaction:
keir@20542 940 t = xs_transaction_start(ctx->xsh);
keir@20542 941 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
keir@20542 942 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20542 943 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
keir@20542 944 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 945 if (errno == EAGAIN)
keir@20542 946 goto retry_transaction;
keir@20542 947 free(dmargs);
keir@20542 948 return 0;
keir@20542 949 }
keir@20542 950
keir@20542 951 static int libxl_create_stubdom(struct libxl_ctx *ctx,
keir@20542 952 libxl_device_model_info *info,
keir@20542 953 libxl_device_disk *disks, int num_disks,
keir@20542 954 libxl_device_nic *vifs, int num_vifs,
keir@20542 955 libxl_device_vfb *vfb,
keir@20542 956 libxl_device_vkb *vkb,
keir@20542 957 libxl_device_model_starting **starting_r)
keir@20542 958 {
keir@20843 959 int i, num_console = 1, ret;
keir@20570 960 libxl_device_console *console;
keir@20542 961 libxl_domain_create_info c_info;
keir@20542 962 libxl_domain_build_info b_info;
keir@20542 963 libxl_domain_build_state state;
keir@20542 964 uint32_t domid;
keir@20542 965 char **args;
keir@20542 966 struct xs_permissions perm[2];
keir@20542 967 xs_transaction_t t;
keir@20594 968 libxl_device_model_starting *dm_starting = 0;
keir@20542 969
keir@20542 970 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20542 971 if (!args)
keir@20542 972 return ERROR_FAIL;
keir@20542 973
keir@20542 974 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
keir@20542 975 c_info.hvm = 0;
keir@20542 976 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
keir@20801 977 for (i = 0; i < 16; i++)
keir@20801 978 c_info.uuid[i] = info->uuid[i];
keir@20542 979
keir@20542 980 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
keir@20542 981 b_info.max_vcpus = 1;
keir@20542 982 b_info.max_memkb = 32 * 1024;
keir@20647 983 b_info.target_memkb = b_info.max_memkb;
keir@20542 984 b_info.kernel = "/usr/lib/xen/boot/ioemu-stubdom.gz";
keir@20542 985 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
keir@20542 986 b_info.u.pv.ramdisk = "";
keir@20542 987 b_info.u.pv.features = "";
keir@20542 988 b_info.hvm = 0;
keir@20542 989
keir@20843 990 ret = libxl_domain_make(ctx, &c_info, &domid);
keir@20843 991 if (ret) return ret;
keir@20843 992 ret = libxl_domain_build(ctx, &b_info, domid, &state);
keir@20843 993 if (ret) return ret;
keir@20542 994
keir@20542 995 libxl_write_dmargs(ctx, domid, info->domid, args);
keir@20843 996 libxl_xs_write(ctx, XBT_NULL,
keir@20843 997 libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)),
keir@20843 998 "%d", domid);
keir@20843 999 libxl_xs_write(ctx, XBT_NULL,
keir@20843 1000 libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)),
keir@20843 1001 "%d", info->domid);
keir@20542 1002 xc_domain_set_target(ctx->xch, domid, info->domid);
keir@20542 1003 xs_set_target(ctx->xsh, domid, info->domid);
keir@20542 1004
keir@20542 1005 perm[0].id = domid;
keir@20542 1006 perm[0].perms = XS_PERM_NONE;
keir@20542 1007 perm[1].id = info->domid;
keir@20542 1008 perm[1].perms = XS_PERM_READ;
keir@20542 1009 retry_transaction:
keir@20542 1010 t = xs_transaction_start(ctx->xsh);
keir@20542 1011 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
keir@20542 1012 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
keir@20635 1013 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
keir@20635 1014 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
keir@20542 1015 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 1016 if (errno == EAGAIN)
keir@20542 1017 goto retry_transaction;
keir@20542 1018
keir@20542 1019 for (i = 0; i < num_disks; i++) {
keir@20781 1020 disks[i].domid = domid;
keir@20843 1021 ret = libxl_device_disk_add(ctx, domid, &disks[i]);
keir@20843 1022 if (ret) return ret;
keir@20542 1023 }
keir@20542 1024 for (i = 0; i < num_vifs; i++) {
keir@20781 1025 vifs[i].domid = domid;
keir@20843 1026 ret = libxl_device_nic_add(ctx, domid, &vifs[i]);
keir@20843 1027 if (ret) return ret;
keir@20542 1028 }
keir@20781 1029 vfb->domid = domid;
keir@20843 1030 ret = libxl_device_vfb_add(ctx, domid, vfb);
keir@20843 1031 if (ret) return ret;
keir@20781 1032 vkb->domid = domid;
keir@20843 1033 ret = libxl_device_vkb_add(ctx, domid, vkb);
keir@20843 1034 if (ret) return ret;
keir@20542 1035
keir@20570 1036 if (info->serial)
keir@20570 1037 num_console++;
keir@20843 1038
keir@20570 1039 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
keir@20843 1040 if (!console)
keir@20843 1041 return ERROR_NOMEM;
keir@20843 1042
keir@20570 1043 for (i = 0; i < num_console; i++) {
keir@20781 1044 console[i].devid = i;
keir@20781 1045 console[i].constype = CONSTYPE_IOEMU;
keir@20781 1046 console[i].domid = domid;
keir@20570 1047 if (!i)
keir@20781 1048 console[i].build_state = &state;
keir@20843 1049 ret = libxl_device_console_add(ctx, domid, &console[i]);
keir@20843 1050 if (ret) return ret;
keir@20570 1051 }
keir@20594 1052 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
keir@20594 1053 free(args);
keir@20594 1054 return -1;
keir@20594 1055 }
keir@20594 1056 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
keir@20594 1057 free(args);
keir@20594 1058 return -1;
keir@20594 1059 }
keir@20542 1060
keir@20542 1061 libxl_domain_unpause(ctx, domid);
keir@20542 1062
keir@20594 1063 if (starting_r) {
keir@20594 1064 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
keir@20598 1065 (*starting_r)->domid = info->domid;
keir@20594 1066 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20594 1067 (*starting_r)->for_spawn = NULL;
keir@20594 1068 }
keir@20594 1069
keir@20542 1070 free(args);
keir@20542 1071 return 0;
keir@20542 1072 }
keir@20542 1073
keir@20462 1074 int libxl_create_device_model(struct libxl_ctx *ctx,
keir@20462 1075 libxl_device_model_info *info,
keir@20542 1076 libxl_device_disk *disks, int num_disks,
keir@20514 1077 libxl_device_nic *vifs, int num_vifs,
keir@20514 1078 libxl_device_model_starting **starting_r)
keir@20462 1079 {
keir@20546 1080 char *path, *logfile;
keir@20514 1081 int logfile_w, null;
keir@20546 1082 int rc;
keir@20462 1083 char **args;
keir@20594 1084 struct libxl_device_model_starting buf_starting, *p;
keir@20514 1085
keir@20542 1086 if (strstr(info->device_model, "stubdom-dm")) {
keir@20542 1087 libxl_device_vfb vfb;
keir@20542 1088 libxl_device_vkb vkb;
keir@20542 1089
keir@20542 1090 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
keir@20542 1091 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
keir@20542 1092 }
keir@20542 1093
keir@20462 1094 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20462 1095 if (!args)
keir@20462 1096 return ERROR_FAIL;
keir@20462 1097
keir@20462 1098 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
keir@20462 1099 xs_mkdir(ctx->xsh, XBT_NULL, path);
keir@20462 1100
keir@20546 1101 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
keir@20467 1102 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
keir@20546 1103 free(logfile);
keir@20462 1104 null = open("/dev/null", O_RDONLY);
keir@20514 1105
keir@20514 1106 if (starting_r) {
keir@20516 1107 rc = ERROR_NOMEM;
keir@20594 1108 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
keir@20516 1109 if (!*starting_r) goto xit;
keir@20594 1110 p = *starting_r;
keir@20611 1111 p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
keir@20594 1112 } else {
keir@20594 1113 p = &buf_starting;
keir@20594 1114 p->for_spawn = NULL;
keir@20594 1115 }
keir@20514 1116
keir@20594 1117 p->domid = info->domid;
keir@20594 1118 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20594 1119 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
keir@20594 1120
keir@20594 1121 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
keir@20514 1122 if (rc < 0) goto xit;
keir@20514 1123 if (!rc) { /* inner child */
keir@20833 1124 libxl_exec(null, logfile_w, logfile_w,
keir@20514 1125 info->device_model, args);
keir@20514 1126 }
keir@20514 1127
keir@20514 1128 rc = 0;
keir@20514 1129 xit:
keir@20516 1130 free(args);
keir@20462 1131 close(null);
keir@20462 1132 close(logfile_w);
keir@20462 1133
keir@20514 1134 return rc;
keir@20514 1135 }
keir@20514 1136
keir@20514 1137 int libxl_detach_device_model(struct libxl_ctx *ctx,
keir@20779 1138 libxl_device_model_starting *starting)
keir@20779 1139 {
keir@20514 1140 int rc;
keir@20594 1141 rc = libxl_spawn_detach(ctx, starting->for_spawn);
keir@20594 1142 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
keir@20514 1143 libxl_free(ctx, starting);
keir@20514 1144 return rc;
keir@20514 1145 }
keir@20462 1146
keir@20514 1147
keir@20514 1148 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
keir@20779 1149 libxl_device_model_starting *starting)
keir@20779 1150 {
keir@20514 1151 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
keir@20514 1152 libxl_spawn_check,
keir@20594 1153 starting->for_spawn);
keir@20514 1154 int detach = libxl_detach_device_model(ctx, starting);
keir@20514 1155 return problem ? problem : detach;
keir@20542 1156 return 0;
keir@20462 1157 }
keir@20462 1158
keir@20514 1159
keir@20462 1160 /******************************************************************************/
keir@20566 1161
keir@20566 1162 static int is_blktap2_supported(void)
keir@20566 1163 {
keir@20566 1164 char buf[1024];
keir@20566 1165 FILE *f = fopen("/proc/devices", "r");
keir@20566 1166
keir@20566 1167
keir@20566 1168 while (fgets(buf, sizeof(buf), f) != NULL) {
keir@20566 1169 if (strstr(buf, "blktap2")) {
keir@20566 1170 fclose(f);
keir@20566 1171 return 1;
keir@20566 1172 }
keir@20566 1173 }
keir@20566 1174 fclose(f);
keir@20566 1175 return 0;
keir@20566 1176 }
keir@20566 1177
keir@20566 1178 static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type)
keir@20566 1179 {
keir@20566 1180 char buf[1024];
keir@20566 1181 char *p;
keir@20566 1182 int devnum;
keir@20566 1183 FILE *f = fopen("/sys/class/blktap2/devices", "r");
keir@20566 1184
keir@20566 1185
keir@20566 1186 while (!feof(f)) {
keir@20566 1187 fscanf(f, "%d %s", &devnum, buf);
keir@20566 1188 p = strchr(buf, ':');
keir@20571 1189 if (p == NULL)
keir@20571 1190 continue;
keir@20566 1191 p++;
keir@20566 1192 if (!strcmp(p, name) && !strncmp(buf, type, 3)) {
keir@20566 1193 fclose(f);
keir@20566 1194 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", devnum);
keir@20566 1195 }
keir@20566 1196 }
keir@20566 1197 fclose(f);
keir@20566 1198 return NULL;
keir@20566 1199 }
keir@20566 1200
keir@20462 1201 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20462 1202 {
keir@20462 1203 flexarray_t *front;
keir@20462 1204 flexarray_t *back;
keir@20462 1205 char *backend_type;
keir@20462 1206 unsigned int boffset = 0;
keir@20462 1207 unsigned int foffset = 0;
keir@20462 1208 int devid;
keir@20462 1209 libxl_device device;
keir@20566 1210 int major, minor;
keir@20462 1211
keir@20462 1212 front = flexarray_make(16, 1);
keir@20462 1213 if (!front)
keir@20462 1214 return ERROR_NOMEM;
keir@20462 1215 back = flexarray_make(16, 1);
keir@20462 1216 if (!back) /* leaks front if error */
keir@20462 1217 return ERROR_NOMEM;
keir@20462 1218
keir@20462 1219 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
keir@20462 1220 devid = device_disk_dev_number(disk->virtpath);
keir@20947 1221 if (devid==-1) {
keir@20947 1222 XL_LOG(ctx, XL_LOG_ERROR, "Invalid or unuspported"
keir@20947 1223 " virtual disk identifier %s", disk->virtpath);
keir@20947 1224 return ERROR_INVAL;
keir@20947 1225 }
keir@20462 1226
keir@20462 1227 device.backend_devid = devid;
keir@20462 1228 device.backend_domid = disk->backend_domid;
keir@20462 1229 device.devid = devid;
keir@20462 1230 device.domid = disk->domid;
keir@20462 1231 device.kind = DEVICE_VBD;
keir@20462 1232
keir@20462 1233 switch (disk->phystype) {
keir@20462 1234 case PHYSTYPE_PHY: {
keir@20462 1235
keir@20509 1236 device_physdisk_major_minor(disk->physpath, &major, &minor);
keir@20516 1237 flexarray_set(back, boffset++, "physical-device");
keir@20462 1238 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20462 1239
keir@20516 1240 flexarray_set(back, boffset++, "params");
keir@20516 1241 flexarray_set(back, boffset++, disk->physpath);
keir@20462 1242
keir@20462 1243 device.backend_kind = DEVICE_VBD;
keir@20462 1244 break;
keir@20462 1245 }
keir@20566 1246 case PHYSTYPE_FILE:
keir@20566 1247 /* let's pretend is tap:aio for the moment */
keir@20566 1248 disk->phystype = PHYSTYPE_AIO;
keir@20462 1249 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
keir@20566 1250 if (is_blktap2_supported()) {
keir@20566 1251 int rc, c, p[2], tot;
keir@20566 1252 char buf[1024], *dev;
keir@20779 1253 dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
keir@20779 1254 if (dev == NULL) {
keir@21185 1255 rc= libxl_pipe(ctx, p); if (rc==-1) return -1;
keir@21185 1256 rc= libxl_fork(ctx); if (rc==-1) return -1;
keir@21185 1257 if (!rc) { /* child */
keir@20566 1258 int null_r, null_w;
keir@20566 1259 char *args[4];
keir@20566 1260 args[0] = "tapdisk2";
keir@20566 1261 args[1] = "-n";
keir@20566 1262 args[2] = libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath);
keir@20566 1263 args[3] = NULL;
keir@20566 1264
keir@20566 1265 null_r = open("/dev/null", O_RDONLY);
keir@20566 1266 null_w = open("/dev/null", O_WRONLY);
keir@20833 1267 libxl_exec(null_r, p[1], null_w, "/usr/sbin/tapdisk2", args);
keir@20566 1268 XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
keir@20566 1269 }
keir@20566 1270 close(p[1]);
keir@20566 1271 tot = 0;
keir@20566 1272 while ((c = read(p[0], buf + tot, sizeof(buf) - tot)) > 0)
keir@20566 1273 tot = tot + c;
keir@20566 1274 close(p[0]);
keir@20566 1275 buf[tot - 1] = '\0';
keir@20566 1276 dev = buf;
keir@20566 1277 }
keir@20566 1278 flexarray_set(back, boffset++, "tapdisk-params");
keir@20566 1279 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20566 1280 flexarray_set(back, boffset++, "params");
keir@20566 1281 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", dev));
keir@20566 1282 backend_type = "phy";
keir@20566 1283 device_physdisk_major_minor(dev, &major, &minor);
keir@20566 1284 flexarray_set(back, boffset++, "physical-device");
keir@20566 1285 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20566 1286 device.backend_kind = DEVICE_VBD;
keir@20566 1287
keir@20566 1288 break;
keir@20566 1289 }
keir@20516 1290 flexarray_set(back, boffset++, "params");
keir@20462 1291 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
keir@20462 1292 device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20462 1293
keir@20462 1294 device.backend_kind = DEVICE_TAP;
keir@20462 1295 break;
keir@20846 1296 default:
keir@20846 1297 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
keir@20846 1298 return ERROR_INVAL;
keir@20462 1299 }
keir@20462 1300
keir@20516 1301 flexarray_set(back, boffset++, "frontend-id");
keir@20462 1302 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
keir@20516 1303 flexarray_set(back, boffset++, "online");
keir@20516 1304 flexarray_set(back, boffset++, "1");
keir@20516 1305 flexarray_set(back, boffset++, "removable");
keir@20462 1306 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
keir@20516 1307 flexarray_set(back, boffset++, "bootable");
keir@20509 1308 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1309 flexarray_set(back, boffset++, "state");
keir@20462 1310 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1311 flexarray_set(back, boffset++, "dev");
keir@20516 1312 flexarray_set(back, boffset++, disk->virtpath);
keir@20516 1313 flexarray_set(back, boffset++, "type");
keir@20516 1314 flexarray_set(back, boffset++, backend_type);
keir@20516 1315 flexarray_set(back, boffset++, "mode");
keir@20516 1316 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
keir@20462 1317
keir@20516 1318 flexarray_set(front, foffset++, "backend-id");
keir@20462 1319 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
keir@20516 1320 flexarray_set(front, foffset++, "state");
keir@20462 1321 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1322 flexarray_set(front, foffset++, "virtual-device");
keir@20462 1323 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
keir@20516 1324 flexarray_set(front, foffset++, "device-type");
keir@20516 1325 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
keir@20462 1326
keir@20462 1327 if (0 /* protocol != native*/) {
keir@20516 1328 flexarray_set(front, foffset++, "protocol");
keir@20516 1329 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 1330 }
keir@20462 1331
keir@20462 1332 libxl_device_generic_add(ctx, &device,
keir@20462 1333 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 1334 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1335 flexarray_free(back);
keir@20516 1336 flexarray_free(front);
keir@20462 1337 return 0;
keir@20462 1338 }
keir@20462 1339
keir@20626 1340 int libxl_device_disk_del(struct libxl_ctx *ctx,
keir@20626 1341 libxl_device_disk *disk, int wait)
keir@20462 1342 {
keir@20626 1343 libxl_device device;
keir@20626 1344 int devid;
keir@20462 1345
keir@20626 1346 devid = device_disk_dev_number(disk->virtpath);
keir@20626 1347 device.backend_domid = disk->backend_domid;
keir@20626 1348 device.backend_devid = devid;
keir@20626 1349 device.backend_kind =
keir@20626 1350 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
keir@20626 1351 device.domid = disk->domid;
keir@20626 1352 device.devid = devid;
keir@20626 1353 device.kind = DEVICE_VBD;
keir@20626 1354 return libxl_device_del(ctx, &device, wait);
keir@20462 1355 }
keir@20462 1356
keir@20462 1357 /******************************************************************************/
keir@20462 1358 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
keir@20462 1359 {
keir@20462 1360 flexarray_t *front;
keir@20462 1361 flexarray_t *back;
keir@20462 1362 unsigned int boffset = 0;
keir@20462 1363 unsigned int foffset = 0;
keir@20462 1364 libxl_device device;
keir@20462 1365
keir@20462 1366 front = flexarray_make(16, 1);
keir@20462 1367 if (!front)
keir@20462 1368 return ERROR_NOMEM;
keir@20462 1369 back = flexarray_make(16, 1);
keir@20462 1370 if (!back)
keir@20462 1371 return ERROR_NOMEM;
keir@20462 1372
keir@20462 1373 device.backend_devid = nic->devid;
keir@20462 1374 device.backend_domid = nic->backend_domid;
keir@20462 1375 device.backend_kind = DEVICE_VIF;
keir@20462 1376 device.devid = nic->devid;
keir@20462 1377 device.domid = nic->domid;
keir@20462 1378 device.kind = DEVICE_VIF;
keir@20462 1379
keir@20516 1380 flexarray_set(back, boffset++, "frontend-id");
keir@20462 1381 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
keir@20516 1382 flexarray_set(back, boffset++, "online");
keir@20516 1383 flexarray_set(back, boffset++, "1");
keir@20516 1384 flexarray_set(back, boffset++, "state");
keir@20462 1385 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1386 flexarray_set(back, boffset++, "script");
keir@20516 1387 flexarray_set(back, boffset++, nic->script);
keir@20516 1388 flexarray_set(back, boffset++, "mac");
keir@20462 1389 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 1390 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 1391 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20516 1392 flexarray_set(back, boffset++, "handle");
keir@20462 1393 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20462 1394
keir@20516 1395 flexarray_set(front, foffset++, "backend-id");
keir@20462 1396 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
keir@20516 1397 flexarray_set(front, foffset++, "state");
keir@20462 1398 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1399 flexarray_set(front, foffset++, "handle");
keir@20462 1400 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20516 1401 flexarray_set(front, foffset++, "mac");
keir@20462 1402 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 1403 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 1404 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20462 1405 if (0 /* protocol != native*/) {
keir@20516 1406 flexarray_set(front, foffset++, "protocol");
keir@20516 1407 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 1408 }
keir@20462 1409
keir@20462 1410 libxl_device_generic_add(ctx, &device,
keir@20462 1411 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 1412 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20462 1413
keir@20462 1414 /* FIXME: wait for plug */
keir@20516 1415 flexarray_free(back);
keir@20516 1416 flexarray_free(front);
keir@20462 1417 return 0;
keir@20462 1418 }
keir@20462 1419
keir@20626 1420 int libxl_device_nic_del(struct libxl_ctx *ctx,
keir@20626 1421 libxl_device_nic *nic, int wait)
keir@20462 1422 {
keir@20626 1423 libxl_device device;
keir@20462 1424
keir@20626 1425 device.backend_devid = nic->devid;
keir@20626 1426 device.backend_domid = nic->backend_domid;
keir@20626 1427 device.backend_kind = DEVICE_VIF;
keir@20626 1428 device.devid = nic->devid;
keir@20626 1429 device.domid = nic->domid;
keir@20626 1430 device.kind = DEVICE_VIF;
keir@20626 1431
keir@20626 1432 return libxl_device_del(ctx, &device, wait);
keir@20462 1433 }
keir@20462 1434
keir@20462 1435 /******************************************************************************/
keir@20509 1436 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
keir@20462 1437 {
keir@20509 1438 flexarray_t *front;
keir@20509 1439 flexarray_t *back;
keir@20509 1440 unsigned int boffset = 0;
keir@20509 1441 unsigned int foffset = 0;
keir@20509 1442 libxl_device device;
keir@20509 1443
keir@20509 1444 if (console->build_state) {
keir@20509 1445 xs_transaction_t t;
keir@20509 1446 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
keir@20516 1447 ents[0] = "console/port";
keir@20509 1448 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
keir@20516 1449 ents[2] = "console/ring-ref";
keir@20509 1450 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
keir@20516 1451 ents[4] = "console/limit";
keir@20509 1452 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
keir@20516 1453 ents[6] = "console/type";
keir@20509 1454 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20509 1455 ents[7] = "xenconsoled";
keir@20509 1456 else
keir@20509 1457 ents[7] = "ioemu";
keir@20509 1458 retry_transaction:
keir@20509 1459 t = xs_transaction_start(ctx->xsh);
keir@20516 1460 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
keir@20509 1461 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20509 1462 if (errno == EAGAIN)
keir@20509 1463 goto retry_transaction;
keir@20509 1464 }
keir@20509 1465
keir@20509 1466 front = flexarray_make(16, 1);
keir@20509 1467 if (!front)
keir@20509 1468 return ERROR_NOMEM;
keir@20509 1469 back = flexarray_make(16, 1);
keir@20509 1470 if (!back)
keir@20509 1471 return ERROR_NOMEM;
keir@20509 1472
keir@20509 1473 device.backend_devid = console->devid;
keir@20509 1474 device.backend_domid = console->backend_domid;
keir@20509 1475 device.backend_kind = DEVICE_CONSOLE;
keir@20509 1476 device.devid = console->devid;
keir@20509 1477 device.domid = console->domid;
keir@20509 1478 device.kind = DEVICE_CONSOLE;
keir@20509 1479
keir@20516 1480 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1481 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
keir@20516 1482 flexarray_set(back, boffset++, "online");
keir@20516 1483 flexarray_set(back, boffset++, "1");
keir@20516 1484 flexarray_set(back, boffset++, "state");
keir@20509 1485 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1486 flexarray_set(back, boffset++, "domain");
keir@20516 1487 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1488 flexarray_set(back, boffset++, "protocol");
keir@20516 1489 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20509 1490
keir@20516 1491 flexarray_set(front, foffset++, "backend-id");
keir@20509 1492 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
keir@20516 1493 flexarray_set(front, foffset++, "state");
keir@20509 1494 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1495 flexarray_set(front, foffset++, "limit");
keir@20509 1496 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
keir@20516 1497 flexarray_set(front, foffset++, "protocol");
keir@20516 1498 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20516 1499 flexarray_set(front, foffset++, "type");
keir@20509 1500 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20516 1501 flexarray_set(front, foffset++, "xenconsoled");
keir@20509 1502 else
keir@20516 1503 flexarray_set(front, foffset++, "ioemu");
keir@20509 1504
keir@20509 1505 libxl_device_generic_add(ctx, &device,
keir@20509 1506 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1507 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1508 flexarray_free(back);
keir@20516 1509 flexarray_free(front);
keir@20509 1510
keir@20509 1511 return 0;
keir@20509 1512 }
keir@20509 1513
keir@20509 1514 /******************************************************************************/
keir@20509 1515 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
keir@20509 1516 {
keir@20509 1517 flexarray_t *front;
keir@20509 1518 flexarray_t *back;
keir@20509 1519 unsigned int boffset = 0;
keir@20509 1520 unsigned int foffset = 0;
keir@20509 1521 libxl_device device;
keir@20509 1522
keir@20509 1523 front = flexarray_make(16, 1);
keir@20509 1524 if (!front)
keir@20509 1525 return ERROR_NOMEM;
keir@20509 1526 back = flexarray_make(16, 1);
keir@20509 1527 if (!back)
keir@20509 1528 return ERROR_NOMEM;
keir@20509 1529
keir@20509 1530 device.backend_devid = vkb->devid;
keir@20509 1531 device.backend_domid = vkb->backend_domid;
keir@20509 1532 device.backend_kind = DEVICE_VKBD;
keir@20509 1533 device.devid = vkb->devid;
keir@20509 1534 device.domid = vkb->domid;
keir@20509 1535 device.kind = DEVICE_VKBD;
keir@20509 1536
keir@20516 1537 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1538 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
keir@20516 1539 flexarray_set(back, boffset++, "online");
keir@20516 1540 flexarray_set(back, boffset++, "1");
keir@20516 1541 flexarray_set(back, boffset++, "state");
keir@20509 1542 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1543 flexarray_set(back, boffset++, "domain");
keir@20516 1544 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20509 1545
keir@20516 1546 flexarray_set(front, foffset++, "backend-id");
keir@20509 1547 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
keir@20516 1548 flexarray_set(front, foffset++, "state");
keir@20509 1549 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1550
keir@20509 1551 libxl_device_generic_add(ctx, &device,
keir@20509 1552 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1553 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1554 flexarray_free(back);
keir@20516 1555 flexarray_free(front);
keir@20509 1556
keir@20509 1557 return 0;
keir@20462 1558 }
keir@20462 1559
keir@20462 1560 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1561 {
keir@20462 1562 return ERROR_NI;
keir@20462 1563 }
keir@20462 1564
keir@20462 1565 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1566 {
keir@20462 1567 return ERROR_NI;
keir@20462 1568 }
keir@20462 1569
keir@20637 1570 libxl_device_disk *libxl_device_disk_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20637 1571 {
keir@20637 1572 char *be_path_tap, *be_path_vbd;
keir@20637 1573 libxl_device_disk *disks = NULL;
keir@20637 1574 char **l = NULL;
keir@20637 1575 unsigned int numl;
keir@20637 1576 int num_disks = 0, i;
keir@20637 1577 char *type;
keir@20637 1578
keir@20637 1579 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
keir@20637 1580 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
keir@20637 1581
keir@20637 1582 l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
keir@20637 1583 if (l) {
keir@20637 1584 num_disks += numl;
keir@20637 1585 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
keir@20637 1586 for (i = 0; i < numl; i++) {
keir@20637 1587 disks[i].backend_domid = 0;
keir@20637 1588 disks[i].domid = domid;
keir@20637 1589 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, l[i]));
keir@20637 1590 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 1591 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, l[i]));
keir@20637 1592 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, l[i])));
keir@20637 1593 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, l[i])), "w"))
keir@20637 1594 disks[i].readwrite = 1;
keir@20637 1595 else
keir@20637 1596 disks[i].readwrite = 0;
keir@20637 1597 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 1598 disks[i].is_cdrom = !strcmp(type, "cdrom");
keir@20637 1599 }
keir@20637 1600 free(l);
keir@20637 1601 }
keir@20637 1602 l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
keir@20637 1603 if (l) {
keir@20637 1604 num_disks += numl;
keir@20637 1605 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
keir@20637 1606 for (i = 0; i < numl; i++) {
keir@20637 1607 disks[i].backend_domid = 0;
keir@20637 1608 disks[i].domid = domid;
keir@20637 1609 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, l[i]));
keir@20637 1610 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 1611 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, l[i]));
keir@20637 1612 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, l[i])));
keir@20637 1613 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, l[i])), "w"))
keir@20637 1614 disks[i].readwrite = 1;
keir@20637 1615 else
keir@20637 1616 disks[i].readwrite = 0;
keir@20637 1617 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 1618 disks[i].is_cdrom = !strcmp(type, "cdrom");
keir@20637 1619 }
keir@20637 1620 free(l);
keir@20637 1621 }
keir@20637 1622 *num = num_disks;
keir@20637 1623 return disks;
keir@20637 1624 }
keir@20637 1625
keir@20637 1626 int libxl_cdrom_insert(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20637 1627 {
keir@20637 1628 int num, i;
keir@20637 1629 uint32_t stubdomid;
keir@20637 1630 libxl_device_disk *disks;
keir@20637 1631
keir@20637 1632 if (!disk->physpath) {
keir@20637 1633 disk->physpath = "";
keir@20637 1634 disk->phystype = PHYSTYPE_PHY;
keir@20637 1635 }
keir@20637 1636 disks = libxl_device_disk_list(ctx, domid, &num);
keir@20637 1637 for (i = 0; i < num; i++) {
keir@20637 1638 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
keir@20637 1639 /* found */
keir@20637 1640 break;
keir@20637 1641 }
keir@20637 1642 if (i == num) {
keir@20637 1643 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
keir@20637 1644 return -1;
keir@20637 1645 }
keir@20637 1646 libxl_device_disk_del(ctx, disks + i, 1);
keir@20637 1647 libxl_device_disk_add(ctx, domid, disk);
keir@20637 1648 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20637 1649 if (stubdomid) {
keir@20781 1650 disks[i].domid = stubdomid;
keir@20637 1651 libxl_device_disk_del(ctx, disks + i, 1);
keir@20781 1652 disk->domid = stubdomid;
keir@20637 1653 libxl_device_disk_add(ctx, stubdomid, disk);
keir@20781 1654 disk->domid = domid;
keir@20637 1655 }
keir@20637 1656 return 0;
keir@20637 1657 }
keir@20637 1658
keir@20462 1659 /******************************************************************************/
keir@20509 1660 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
keir@20509 1661 libxl_device_vfb *vfb,
keir@20509 1662 int num_console,
keir@20509 1663 libxl_device_console *console,
keir@20860 1664 libxl_device_model_info *info)
keir@20860 1665 {
keir@20509 1666 int i = 0, j = 0, num = 0;
keir@20509 1667 memset(info, 0x00, sizeof(libxl_device_model_info));
keir@20509 1668
keir@20509 1669 info->vnc = vfb->vnc;
keir@20509 1670 if (vfb->vnclisten)
keir@20509 1671 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
keir@20509 1672 info->vncdisplay = vfb->vncdisplay;
keir@20509 1673 info->vncunused = vfb->vncunused;
keir@20509 1674 if (vfb->keymap)
keir@20509 1675 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
keir@20509 1676 info->sdl = vfb->sdl;
keir@20509 1677 info->opengl = vfb->opengl;
keir@20509 1678 for (i = 0; i < num_console; i++) {
keir@20509 1679 if (console->constype == CONSTYPE_IOEMU)
keir@20509 1680 num++;
keir@20509 1681 }
keir@20509 1682 if (num > 0) {
keir@20860 1683 uint32_t guest_domid;
keir@20860 1684 if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
keir@20599 1685 char *filename;
keir@20599 1686 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
keir@20599 1687 libxl_create_logfile(ctx, name, &filename);
keir@20599 1688 info->serial = libxl_sprintf(ctx, "file:%s", filename);
keir@20599 1689 free(filename);
keir@20599 1690 } else {
keir@20599 1691 info->serial = "pty";
keir@20599 1692 }
keir@20509 1693 num--;
keir@20509 1694 }
keir@20509 1695 if (num > 0) {
keir@20509 1696 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
keir@20509 1697 for (j = 0; j < num * 2; j = j + 2) {
keir@20509 1698 info->extra[j] = "-serial";
keir@20509 1699 info->extra[j + 1] = "pty";
keir@20509 1700 }
keir@20509 1701 info->extra[j] = NULL;
keir@20509 1702 }
keir@20509 1703 info->domid = vfb->domid;
keir@20509 1704 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
keir@20509 1705 info->device_model = "/usr/lib/xen/bin/qemu-dm";
keir@20509 1706 info->type = XENPV;
keir@20509 1707 return 0;
keir@20509 1708 }
keir@20509 1709
keir@20509 1710 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
keir@20514 1711 int num_console, libxl_device_console *console,
keir@20514 1712 struct libxl_device_model_starting **starting_r)
keir@20509 1713 {
keir@20509 1714 libxl_device_model_info info;
keir@20509 1715
keir@20509 1716 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
keir@20542 1717 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
keir@20509 1718 return 0;
keir@20509 1719 }
keir@20509 1720
keir@20509 1721 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
keir@20462 1722 {
keir@20509 1723 flexarray_t *front;
keir@20509 1724 flexarray_t *back;
keir@20509 1725 unsigned int boffset = 0;
keir@20509 1726 unsigned int foffset = 0;
keir@20509 1727 libxl_device device;
keir@20509 1728
keir@20509 1729 front = flexarray_make(16, 1);
keir@20509 1730 if (!front)
keir@20509 1731 return ERROR_NOMEM;
keir@20509 1732 back = flexarray_make(16, 1);
keir@20509 1733 if (!back)
keir@20509 1734 return ERROR_NOMEM;
keir@20509 1735
keir@20509 1736 device.backend_devid = vfb->devid;
keir@20509 1737 device.backend_domid = vfb->backend_domid;
keir@20509 1738 device.backend_kind = DEVICE_VFB;
keir@20509 1739 device.devid = vfb->devid;
keir@20509 1740 device.domid = vfb->domid;
keir@20509 1741 device.kind = DEVICE_VFB;
keir@20509 1742
keir@20516 1743 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1744 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
keir@20516 1745 flexarray_set(back, boffset++, "online");
keir@20516 1746 flexarray_set(back, boffset++, "1");
keir@20516 1747 flexarray_set(back, boffset++, "state");
keir@20509 1748 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1749 flexarray_set(back, boffset++, "domain");
keir@20516 1750 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1751 flexarray_set(back, boffset++, "vnc");
keir@20509 1752 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
keir@20516 1753 flexarray_set(back, boffset++, "vnclisten");
keir@20516 1754 flexarray_set(back, boffset++, vfb->vnclisten);
keir@20516 1755 flexarray_set(back, boffset++, "vncdisplay");
keir@20509 1756 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
keir@20516 1757 flexarray_set(back, boffset++, "vncunused");
keir@20509 1758 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
keir@20516 1759 flexarray_set(back, boffset++, "sdl");
keir@20509 1760 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
keir@20516 1761 flexarray_set(back, boffset++, "opengl");
keir@20509 1762 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
keir@20509 1763 if (vfb->xauthority) {
keir@20516 1764 flexarray_set(back, boffset++, "xauthority");
keir@20516 1765 flexarray_set(back, boffset++, vfb->xauthority);
keir@20509 1766 }
keir@20509 1767 if (vfb->display) {
keir@20516 1768 flexarray_set(back, boffset++, "display");
keir@20516 1769 flexarray_set(back, boffset++, vfb->display);
keir@20509 1770 }
keir@20509 1771
keir@20516 1772 flexarray_set(front, foffset++, "backend-id");
keir@20509 1773 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
keir@20516 1774 flexarray_set(front, foffset++, "state");
keir@20509 1775 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1776
keir@20509 1777 libxl_device_generic_add(ctx, &device,
keir@20509 1778 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1779 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20509 1780 flexarray_free(front);
keir@20509 1781 flexarray_free(back);
keir@20509 1782
keir@20509 1783 return 0;
keir@20462 1784 }
keir@20462 1785
keir@20462 1786 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1787 {
keir@20462 1788 return ERROR_NI;
keir@20462 1789 }
keir@20462 1790
keir@20462 1791 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1792 {
keir@20462 1793 return ERROR_NI;
keir@20462 1794 }
keir@20462 1795
keir@20462 1796 /******************************************************************************/
keir@20482 1797
keir@20482 1798 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
keir@20482 1799 unsigned int bus, unsigned int dev,
keir@20482 1800 unsigned int func, unsigned int vdevfn)
keir@20482 1801 {
keir@20482 1802 pcidev->domain = domain;
keir@20482 1803 pcidev->bus = bus;
keir@20482 1804 pcidev->dev = dev;
keir@20482 1805 pcidev->func = func;
keir@20482 1806 pcidev->vdevfn = vdevfn;
keir@20482 1807 return 0;
keir@20482 1808 }
keir@20482 1809
keir@20482 1810 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
keir@20482 1811 {
keir@20482 1812 flexarray_t *front;
keir@20482 1813 flexarray_t *back;
keir@20482 1814 unsigned int boffset = 0;
keir@20482 1815 unsigned int foffset = 0;
keir@20482 1816 libxl_device device;
keir@20482 1817 int i;
keir@20482 1818
keir@20482 1819 front = flexarray_make(16, 1);
keir@20482 1820 if (!front)
keir@20482 1821 return ERROR_NOMEM;
keir@20482 1822 back = flexarray_make(16, 1);
keir@20482 1823 if (!back)
keir@20482 1824 return ERROR_NOMEM;
keir@20482 1825
keir@20512 1826 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
keir@20482 1827
keir@20482 1828 /* add pci device */
keir@20482 1829 device.backend_devid = 0;
keir@20482 1830 device.backend_domid = 0;
keir@20482 1831 device.backend_kind = DEVICE_PCI;
keir@20482 1832 device.devid = 0;
keir@20482 1833 device.domid = domid;
keir@20482 1834 device.kind = DEVICE_PCI;
keir@20482 1835
keir@20516 1836 flexarray_set(back, boffset++, "frontend-id");
keir@20482 1837 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
keir@20516 1838 flexarray_set(back, boffset++, "online");
keir@20516 1839 flexarray_set(back, boffset++, "1");
keir@20516 1840 flexarray_set(back, boffset++, "state");
keir@20482 1841 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1842 flexarray_set(back, boffset++, "domain");
keir@20516 1843 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20482 1844 for (i = 0; i < num; i++) {
keir@20482 1845 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
keir@20482 1846 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1847 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
keir@20482 1848 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1849 if (pcidev->vdevfn) {
keir@20482 1850 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
keir@20482 1851 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1852 }
keir@20482 1853 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
keir@20482 1854 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1855 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
keir@20482 1856 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1857 }
keir@20516 1858 flexarray_set(back, boffset++, "num_devs");
keir@20482 1859 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
keir@20482 1860
keir@20516 1861 flexarray_set(front, foffset++, "backend-id");
keir@20482 1862 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
keir@20516 1863 flexarray_set(front, foffset++, "state");
keir@20482 1864 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1865
keir@20482 1866 libxl_device_generic_add(ctx, &device,
keir@20482 1867 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20482 1868 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20482 1869
keir@20482 1870 flexarray_free(back);
keir@20482 1871 flexarray_free(front);
keir@20482 1872 return 0;
keir@20482 1873 }
keir@20482 1874
keir@20482 1875 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1876 {
keir@20482 1877 flexarray_t *back;
keir@20482 1878 char *num_devs, *be_path;
keir@20482 1879 int num = 0;
keir@20482 1880 unsigned int boffset = 0;
keir@20482 1881 xs_transaction_t t;
keir@20482 1882
keir@20516 1883 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 1884 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 1885 if (!num_devs)
keir@20482 1886 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
keir@20482 1887
keir@20482 1888 if (!is_hvm(ctx, domid)) {
keir@20482 1889 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
keir@20482 1890 return -1;
keir@20482 1891 }
keir@20482 1892
keir@20482 1893 back = flexarray_make(16, 1);
keir@20482 1894 if (!back)
keir@20482 1895 return ERROR_NOMEM;
keir@20482 1896
keir@20512 1897 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
keir@20482 1898 num = atoi(num_devs);
keir@20482 1899 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
keir@20482 1900 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1901 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
keir@20482 1902 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1903 if (pcidev->vdevfn) {
keir@20482 1904 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
keir@20482 1905 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1906 }
keir@20482 1907 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
keir@20482 1908 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1909 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
keir@20482 1910 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1911 flexarray_set(back, boffset++, "num_devs");
keir@20482 1912 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
keir@20516 1913 flexarray_set(back, boffset++, "state");
keir@20482 1914 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
keir@20482 1915
keir@20482 1916 retry_transaction:
keir@20482 1917 t = xs_transaction_start(ctx->xsh);
keir@20482 1918 libxl_xs_writev(ctx, t, be_path,
keir@20482 1919 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
keir@20482 1920 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 1921 if (errno == EAGAIN)
keir@20482 1922 goto retry_transaction;
keir@20482 1923
keir@20482 1924 flexarray_free(back);
keir@20482 1925 return 0;
keir@20482 1926 }
keir@20482 1927
keir@20482 1928 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20482 1929 {
keir@20542 1930 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
keir@20542 1931 int num, i, j;
keir@20482 1932 xs_transaction_t t;
keir@20482 1933 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
keir@20482 1934
keir@20516 1935 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 1936 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
keir@20482 1937 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
keir@20482 1938 if (!num_devs)
keir@20482 1939 return -1;
keir@20482 1940 num = atoi(num_devs);
keir@20482 1941
keir@20482 1942 if (!is_hvm(ctx, domid)) {
keir@20482 1943 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 1944 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20482 1945 return -1;
keir@20482 1946 }
keir@20482 1947 }
keir@20482 1948
keir@20482 1949 for (i = 0; i < num; i++) {
keir@20482 1950 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 1951 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 1952 if (domain == pcidev->domain && bus == pcidev->bus &&
keir@20482 1953 pcidev->dev == dev && pcidev->func == func) {
keir@20482 1954 break;
keir@20482 1955 }
keir@20482 1956 }
keir@20482 1957 if (i == num) {
keir@20512 1958 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
keir@20482 1959 return -1;
keir@20482 1960 }
keir@20482 1961
keir@20482 1962 retry_transaction:
keir@20482 1963 t = xs_transaction_start(ctx->xsh);
keir@20542 1964 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
keir@20482 1965 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
keir@20482 1966 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 1967 if (errno == EAGAIN)
keir@20482 1968 goto retry_transaction;
keir@20542 1969
keir@20542 1970 if (!is_hvm(ctx, domid)) {
keir@20542 1971 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 1972 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20542 1973 return -1;
keir@20542 1974 }
keir@20542 1975 }
keir@20542 1976
keir@20542 1977 retry_transaction2:
keir@20542 1978 t = xs_transaction_start(ctx->xsh);
keir@20542 1979 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
keir@20542 1980 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
keir@20542 1981 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20542 1982 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
keir@20542 1983 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20542 1984 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20542 1985 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
keir@20542 1986 for (j = i + 1; j < num; j++) {
keir@20542 1987 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
keir@20542 1988 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1989 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1990 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1991 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
keir@20542 1992 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1993 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1994 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1995 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
keir@20542 1996 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1997 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1998 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1999 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
keir@20542 2000 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2001 if (tmp) {
keir@20542 2002 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2003 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2004 }
keir@20542 2005 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
keir@20542 2006 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2007 if (tmp) {
keir@20542 2008 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2009 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2010 }
keir@20542 2011 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
keir@20542 2012 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2013 if (tmp) {
keir@20542 2014 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2015 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2016 }
keir@20542 2017 }
keir@20542 2018 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 2019 if (errno == EAGAIN)
keir@20542 2020 goto retry_transaction2;
keir@20542 2021
keir@20542 2022 if (num == 1) {
keir@20542 2023 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
keir@20542 2024 libxl_device_destroy(ctx, be_path, 1);
keir@20542 2025 xs_rm(ctx->xsh, XBT_NULL, be_path);
keir@20542 2026 xs_rm(ctx->xsh, XBT_NULL, fe_path);
keir@20542 2027 return 0;
keir@20542 2028 }
keir@20542 2029
keir@20482 2030 return 0;
keir@20462 2031 }
keir@20462 2032
keir@20482 2033 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 2034 {
keir@20779 2035 char *path;
keir@20482 2036 char *state, *vdevfn;
keir@20482 2037 int rc, hvm;
keir@20542 2038 int stubdomid = 0;
keir@20482 2039
keir@20482 2040 /* TODO: check if the device can be assigned */
keir@20482 2041
keir@20482 2042 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2043
keir@20779 2044 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20779 2045 if (stubdomid != 0) {
keir@20542 2046 libxl_device_pci pcidev_s = *pcidev;
keir@20542 2047 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
keir@20542 2048 }
keir@20542 2049
keir@20482 2050 hvm = is_hvm(ctx, domid);
keir@20482 2051 if (hvm) {
keir@20831 2052 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
keir@20482 2053 return -1;
keir@20482 2054 }
keir@20779 2055 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2056 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20779 2057 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2058 if (pcidev->vdevfn)
keir@20482 2059 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
keir@20482 2060 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
keir@20482 2061 else
keir@20482 2062 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 2063 pcidev->bus, pcidev->dev, pcidev->func);
keir@20779 2064 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
keir@20482 2065 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
keir@20831 2066 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", NULL, NULL) < 0)
keir@20512 2067 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20779 2068 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2069 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 2070 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
keir@20779 2071 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2072 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 2073 } else {
keir@20542 2074 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 2075 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2076 FILE *f = fopen(sysfs_path, "r");
keir@20482 2077 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 2078 int irq = 0;
keir@20482 2079 int i;
keir@20482 2080
keir@20482 2081 if (f == NULL) {
keir@20512 2082 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2083 return -1;
keir@20482 2084 }
keir@20482 2085 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20512 2086 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
keir@20482 2087 size = end - start + 1;
keir@20482 2088 if (start) {
keir@20482 2089 if (flags & PCI_BAR_IO) {
keir@20482 2090 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
keir@20482 2091 if (rc < 0)
keir@20512 2092 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 2093 } else {
keir@20482 2094 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 2095 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
keir@20482 2096 if (rc < 0)
keir@20512 2097 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 2098 }
keir@20482 2099 }
keir@20482 2100 }
keir@20482 2101 fclose(f);
keir@20542 2102 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 2103 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2104 f = fopen(sysfs_path, "r");
keir@20482 2105 if (f == NULL) {
keir@20512 2106 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2107 goto out;
keir@20482 2108 }
keir@20482 2109 fscanf(f, "%u", &irq);
keir@20482 2110 if (irq) {
keir@20482 2111 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
keir@20482 2112 if (rc < 0) {
keir@20512 2113 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
keir@20482 2114 }
keir@20482 2115 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
keir@20482 2116 if (rc < 0) {
keir@20512 2117 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
keir@20482 2118 }
keir@20482 2119 }
keir@20482 2120 fclose(f);
keir@20482 2121 }
keir@20482 2122 out:
keir@20860 2123 if (!libxl_is_stubdom(ctx, domid, NULL)) {
keir@20779 2124 rc = xc_assign_device(ctx->xch, domid, pcidev->value);
keir@20779 2125 if (rc < 0)
keir@20542 2126 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
keir@20542 2127 }
keir@20482 2128
keir@20482 2129 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
keir@20482 2130 return 0;
keir@20462 2131 }
keir@20462 2132
keir@20482 2133 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 2134 {
keir@20779 2135 char *path;
keir@20482 2136 char *state;
keir@20482 2137 int hvm, rc;
keir@20542 2138 int stubdomid = 0;
keir@20482 2139
keir@20482 2140 /* TODO: check if the device can be detached */
keir@20542 2141 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
keir@20482 2142
keir@20482 2143 hvm = is_hvm(ctx, domid);
keir@20482 2144 if (hvm) {
keir@20831 2145 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
keir@20482 2146 return -1;
keir@20482 2147 }
keir@20779 2148 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2149 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20779 2150 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2151 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 2152 pcidev->bus, pcidev->dev, pcidev->func);
keir@20779 2153 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
keir@20482 2154 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
keir@20831 2155 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", NULL, NULL) < 0) {
keir@20512 2156 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20482 2157 return -1;
keir@20482 2158 }
keir@20779 2159 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2160 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 2161 } else {
keir@20542 2162 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 2163 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2164 FILE *f = fopen(sysfs_path, "r");
keir@20482 2165 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 2166 int irq = 0;
keir@20482 2167 int i;
keir@20482 2168
keir@20482 2169 if (f == NULL) {
keir@20512 2170 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2171 goto skip1;
keir@20482 2172 }
keir@20482 2173 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20482 2174 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
keir@20482 2175 size = end - start + 1;
keir@20482 2176 if (start) {
keir@20482 2177 if (flags & PCI_BAR_IO) {
keir@20482 2178 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
keir@20482 2179 if (rc < 0)
keir@20512 2180 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 2181 } else {
keir@20482 2182 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 2183 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
keir@20482 2184 if (rc < 0)
keir@20512 2185 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 2186 }
keir@20482 2187 }
keir@20482 2188 }
keir@20482 2189 fclose(f);
keir@20482 2190 skip1:
keir@20542 2191 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 2192 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2193 f = fopen(sysfs_path, "r");
keir@20482 2194 if (f == NULL) {
keir@20512 2195 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2196 goto out;
keir@20482 2197 }
keir@20482 2198 fscanf(f, "%u", &irq);
keir@20482 2199 if (irq) {
keir@20482 2200 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
keir@20482 2201 if (rc < 0) {
keir@20512 2202 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
keir@20482 2203 }
keir@20482 2204 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
keir@20482 2205 if (rc < 0) {
keir@20512 2206 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
keir@20482 2207 }
keir@20482 2208 }
keir@20482 2209 fclose(f);
keir@20482 2210 }
keir@20482 2211 out:
keir@20482 2212 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2213
keir@20860 2214 if (!libxl_is_stubdom(ctx, domid, NULL)) {
keir@20779 2215 rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
keir@20779 2216 if (rc < 0)
keir@20542 2217 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
keir@20542 2218 }
keir@20542 2219
keir@20779 2220 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20779 2221 if (stubdomid != 0) {
keir@20542 2222 libxl_device_pci pcidev_s = *pcidev;
keir@20542 2223 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
keir@20542 2224 }
keir@20542 2225
keir@20482 2226 return 0;
keir@20462 2227 }
keir@20482 2228
keir@20482 2229 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20482 2230 {
keir@20482 2231 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
keir@20482 2232 int n, i;
keir@20482 2233 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
keir@20482 2234 libxl_device_pci *pcidevs;
keir@20482 2235
keir@20516 2236 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 2237 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 2238 if (!num_devs) {
keir@20482 2239 *num = 0;
keir@20482 2240 return NULL;
keir@20482 2241 }
keir@20482 2242 n = atoi(num_devs);
keir@20779 2243 pcidevs = calloc(n, sizeof(libxl_device_pci));
keir@20482 2244 *num = n;
keir@20482 2245
keir@20482 2246 for (i = 0; i < n; i++) {
keir@20482 2247 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 2248 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 2249 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20482 2250 if (xsvdevfn)
keir@20482 2251 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
keir@20482 2252 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
keir@20482 2253 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20482 2254 if (xsopts) {
keir@20482 2255 char *saveptr;
keir@20482 2256 char *p = strtok_r(xsopts, ",=", &saveptr);
keir@20482 2257 do {
keir@20482 2258 while (*p == ' ')
keir@20482 2259 p++;
keir@20482 2260 if (!strcmp(p, "msitranslate")) {
keir@20482 2261 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 2262 pcidevs[i].msitranslate = atoi(p);
keir@20482 2263 } else if (!strcmp(p, "power_mgmt")) {
keir@20482 2264 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 2265 pcidevs[i].power_mgmt = atoi(p);
keir@20482 2266 }
keir@20482 2267 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
keir@20482 2268 }
keir@20482 2269 }
keir@20482 2270 return pcidevs;
keir@20482 2271 }
keir@20482 2272
keir@20482 2273 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20482 2274 {
keir@20482 2275 libxl_device_pci *pcidevs;
keir@20482 2276 int num, i;
keir@20482 2277
keir@20482 2278 pcidevs = libxl_device_pci_list(ctx, domid, &num);
keir@20482 2279 for (i = 0; i < num; i++) {
keir@20482 2280 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
keir@20482 2281 return -1;
keir@20482 2282 }
keir@20516 2283 free(pcidevs);
keir@20482 2284 return 0;
keir@20482 2285 }
keir@20482 2286
keir@20647 2287 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
keir@20647 2288 {
keir@20647 2289 int rc = 0;
keir@20647 2290 uint32_t videoram;
keir@20647 2291 char *videoram_s = NULL;
keir@20647 2292 char *dompath = libxl_xs_get_dompath(ctx, domid);
keir@20542 2293
keir@20647 2294 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
keir@20647 2295 if (!videoram_s)
keir@20647 2296 return -1;
keir@20647 2297 videoram = atoi(videoram_s);
keir@20647 2298
keir@20647 2299 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
keir@20779 2300 rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT);
keir@20779 2301 if (rc != 0)
keir@20779 2302 return rc;
keir@20647 2303 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
keir@20647 2304 return rc;
keir@20647 2305 }
keir@20875 2306
keir@20875 2307 int libxl_button_press(struct libxl_ctx *ctx, uint32_t domid, libxl_button button)
keir@20875 2308 {
keir@20875 2309 int rc = -1;
keir@20875 2310
keir@20875 2311 switch (button) {
keir@20875 2312 case POWER_BUTTON:
keir@20875 2313 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
keir@20875 2314 break;
keir@20875 2315 case SLEEP_BUTTON:
keir@20875 2316 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
keir@20875 2317 break;
keir@20875 2318 default:
keir@20875 2319 break;
keir@20875 2320 }
keir@20875 2321
keir@20875 2322 return rc;
keir@20875 2323 }
keir@21141 2324
keir@21141 2325 int libxl_get_physinfo(struct libxl_ctx *ctx, struct libxl_physinfo *physinfo)
keir@21141 2326 {
keir@21141 2327 xc_physinfo_t xcphysinfo = { 0 };
keir@21141 2328 int rc;
keir@21141 2329
keir@21141 2330 rc = xc_physinfo(ctx->xch, &xcphysinfo);
keir@21141 2331 if (rc != 0) {
keir@21141 2332 return rc;
keir@21141 2333 }
keir@21141 2334 physinfo->threads_per_core = xcphysinfo.threads_per_core;
keir@21141 2335 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
keir@21142 2336 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
keir@21141 2337 physinfo->nr_cpus = xcphysinfo.nr_cpus;
keir@21141 2338 physinfo->cpu_khz = xcphysinfo.cpu_khz;
keir@21141 2339 physinfo->total_pages = xcphysinfo.total_pages;
keir@21141 2340 physinfo->free_pages = xcphysinfo.free_pages;
keir@21141 2341 physinfo->scrub_pages = xcphysinfo.scrub_pages;
keir@21264 2342 physinfo->nr_nodes = xcphysinfo.nr_nodes;
keir@21264 2343 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
keir@21264 2344 physinfo->phys_cap = xcphysinfo.capabilities;
keir@21264 2345
keir@21141 2346 return 0;
keir@21141 2347 }
keir@21141 2348
keir@21266 2349 const libxl_version_info* libxl_get_version_info(struct libxl_ctx *ctx)
keir@21266 2350 {
keir@21266 2351 union {
keir@21266 2352 xen_extraversion_t xen_extra;
keir@21266 2353 xen_compile_info_t xen_cc;
keir@21266 2354 xen_changeset_info_t xen_chgset;
keir@21266 2355 xen_capabilities_info_t xen_caps;
keir@21266 2356 xen_platform_parameters_t p_parms;
keir@21266 2357 xen_commandline_t xen_commandline;
keir@21266 2358 } u;
keir@21266 2359 long xen_version;
keir@21266 2360 libxl_version_info *info = &ctx->version_info;
keir@21266 2361
keir@21266 2362 if (info->xen_version_extra != NULL)
keir@21266 2363 return info;
keir@21266 2364
keir@21266 2365 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
keir@21266 2366 info->xen_version_major = xen_version >> 16;
keir@21266 2367 info->xen_version_minor = xen_version & 0xFF;
keir@21266 2368 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
keir@21266 2369 info->xen_version_extra = libxl_sprintf(ctx, "%s", u.xen_extra);
keir@21266 2370
keir@21266 2371 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
keir@21266 2372 info->compiler = libxl_sprintf(ctx, "%s", u.xen_cc.compiler);
keir@21266 2373 info->compile_by = libxl_sprintf(ctx, "%s", u.xen_cc.compile_by);
keir@21266 2374 info->compile_domain = libxl_sprintf(ctx, "%s", u.xen_cc.compile_domain);
keir@21266 2375 info->compile_date = libxl_sprintf(ctx, "%s", u.xen_cc.compile_date);
keir@21266 2376
keir@21266 2377 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
keir@21266 2378 info->capabilities = libxl_sprintf(ctx, "%s", u.xen_caps);
keir@21266 2379
keir@21266 2380 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
keir@21266 2381 info->changeset = libxl_sprintf(ctx, "%s", u.xen_chgset);
keir@21266 2382
keir@21266 2383 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
keir@21266 2384 info->virt_start = u.p_parms.virt_start;
keir@21266 2385
keir@21266 2386 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
keir@21266 2387
keir@21266 2388 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
keir@21266 2389 info->commandline = libxl_sprintf(ctx, "%s", u.xen_commandline);
keir@21266 2390
keir@21266 2391 return info;
keir@21266 2392 }
keir@21266 2393
keir@21141 2394 struct libxl_vcpuinfo *libxl_list_vcpu(struct libxl_ctx *ctx, uint32_t domid,
keir@21141 2395 int *nb_vcpu, int *cpusize)
keir@21141 2396 {
keir@21141 2397 struct libxl_vcpuinfo *ptr, *ret;
keir@21141 2398 xc_domaininfo_t domaininfo;
keir@21141 2399 xc_vcpuinfo_t vcpuinfo;
keir@21141 2400 xc_physinfo_t physinfo = { 0 };
keir@21141 2401
keir@21141 2402 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
keir@21141 2403 return NULL;
keir@21141 2404 }
keir@21141 2405 if (xc_physinfo(ctx->xch, &physinfo) == -1) {
keir@21141 2406 return NULL;
keir@21141 2407 }
keir@21141 2408 *cpusize = physinfo.max_cpu_id + 1;
keir@21141 2409 ptr = libxl_calloc(ctx, domaininfo.max_vcpu_id + 1, sizeof (struct libxl_vcpuinfo));
keir@21141 2410 if (!ptr) {
keir@21141 2411 return NULL;
keir@21141 2412 }
keir@21141 2413
keir@21141 2414 ret = ptr;
keir@21141 2415 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
keir@21141 2416 ptr->cpumap = libxl_calloc(ctx, (*cpusize + 63) / 64, sizeof (uint64_t));
keir@21141 2417 if (!ptr->cpumap) {
keir@21141 2418 return NULL;
keir@21141 2419 }
keir@21141 2420 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
keir@21141 2421 return NULL;
keir@21141 2422 }
keir@21141 2423 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap, *cpusize) == -1) {
keir@21141 2424 return NULL;
keir@21141 2425 }
keir@21141 2426 ptr->vcpuid = *nb_vcpu;
keir@21141 2427 ptr->cpu = vcpuinfo.cpu;
keir@21141 2428 ptr->online = !!vcpuinfo.online;
keir@21141 2429 ptr->blocked = !!vcpuinfo.blocked;
keir@21141 2430 ptr->running = !!vcpuinfo.running;
keir@21141 2431 ptr->vcpu_time = vcpuinfo.cpu_time;
keir@21141 2432 }
keir@21141 2433 return ret;
keir@21141 2434 }
keir@21142 2435
keir@21142 2436 int libxl_set_vcpuaffinity(struct libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
keir@21142 2437 uint64_t *cpumap, int cpusize)
keir@21142 2438 {
keir@21142 2439 return (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap, cpusize));
keir@21142 2440 }
keir@21143 2441
keir@21143 2442 int libxl_set_vcpucount(struct libxl_ctx *ctx, uint32_t domid, uint32_t count)
keir@21143 2443 {
keir@21143 2444 xc_domaininfo_t domaininfo;
keir@21143 2445 char *dompath;
keir@21143 2446 int i;
keir@21143 2447
keir@21143 2448 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
keir@21143 2449 return ERROR_FAIL;
keir@21143 2450 }
keir@21143 2451 if (!count || ((domaininfo.max_vcpu_id + 1) < count)) {
keir@21143 2452 return ERROR_INVAL;
keir@21143 2453 }
keir@21143 2454 if (!(dompath = libxl_xs_get_dompath(ctx, domid)))
keir@21143 2455 return ERROR_FAIL;
keir@21143 2456
keir@21143 2457 for (i = 0; i <= domaininfo.max_vcpu_id; ++i) {
keir@21143 2458 libxl_xs_write(ctx, XBT_NULL,
keir@21143 2459 libxl_sprintf(ctx, "%s/cpu/%u/availability", dompath, i),
keir@21143 2460 "%s", ((1 << i) & ((1 << count) - 1)) ? "online" : "offline");
keir@21143 2461 }
keir@21143 2462 return 0;
keir@21143 2463 }
keir@21265 2464
keir@21265 2465 /*
keir@21265 2466 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
keir@21265 2467 * or -1 if an error occured.
keir@21265 2468 */
keir@21265 2469 int libxl_get_sched_id(struct libxl_ctx *ctx)
keir@21265 2470 {
keir@21265 2471 int sched, ret;
keir@21265 2472
keir@21265 2473 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0)
keir@21265 2474 return ret;
keir@21265 2475 return sched;
keir@21265 2476 }
keir@21292 2477
keir@21292 2478 int libxl_sched_credit_domain_get(struct libxl_ctx *ctx, uint32_t domid, struct libxl_sched_credit *scinfo)
keir@21292 2479 {
keir@21292 2480 struct xen_domctl_sched_credit sdom;
keir@21292 2481 int rc;
keir@21292 2482
keir@21292 2483 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
keir@21292 2484 if (rc != 0)
keir@21292 2485 return rc;
keir@21292 2486
keir@21292 2487 scinfo->weight = sdom.weight;
keir@21292 2488 scinfo->cap = sdom.cap;
keir@21292 2489
keir@21292 2490 return 0;
keir@21292 2491 }
keir@21292 2492
keir@21292 2493 int libxl_sched_credit_domain_set(struct libxl_ctx *ctx, uint32_t domid, struct libxl_sched_credit *scinfo)
keir@21292 2494 {
keir@21292 2495 struct xen_domctl_sched_credit sdom;
keir@21292 2496 xc_domaininfo_t domaininfo;
keir@21292 2497 int rc;
keir@21292 2498
keir@21292 2499 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
keir@21292 2500 if (rc != 1 || domaininfo.domain != domid)
keir@21292 2501 return rc;
keir@21292 2502
keir@21292 2503
keir@21292 2504 if (scinfo->weight < 1 || scinfo->weight > 65535) {
keir@21292 2505 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
keir@21292 2506 "Cpu weight out of range, valid values are within range from 1 to 65535");
keir@21292 2507 return -1;
keir@21292 2508 }
keir@21292 2509
keir@21292 2510 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
keir@21292 2511 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
keir@21292 2512 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
keir@21292 2513 ((domaininfo.max_vcpu_id + 1) * 100));
keir@21292 2514 return -1;
keir@21292 2515 }
keir@21292 2516
keir@21292 2517 sdom.weight = scinfo->weight;
keir@21292 2518 sdom.cap = scinfo->cap;
keir@21292 2519
keir@21292 2520 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
keir@21292 2521 if (rc != 0)
keir@21292 2522 return rc;
keir@21292 2523
keir@21292 2524 return 0;
keir@21292 2525 }
keir@21292 2526