debuggers.hg

annotate tools/libxl/libxl.c @ 21389:4ede18e45935

libxl: More cpupool fixes.

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