debuggers.hg

annotate tools/libxl/libxl.c @ 21368:360032605d07

libxl: Support cpupools

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