debuggers.hg

annotate tools/libxl/libxl.c @ 21183:c031244c190e

libxl: libxl_domain_restore: Put fd back to blocking mode

libxl_domain_restore calls, indirectly, xc_domain_restore. The
latter, when doing a live migration, sets the fd from blocking mode
(which it must be on entry, or things go wrong) to nonblocking mode
and leaves it this way. Arguably this is a bug in libxc, but to avoid
disrupting any callers we fix it in libxl.

So libxl_domain_restore now puts the fd back into blocking mode
before returning.

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