debuggers.hg

annotate tools/libxl/libxl.c @ 20819:dda8dc485d67

libxenlight: returns errors if xc and/or xs has not been initialized properly.

fixed segfault when xenstore or xc are not available.

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