debuggers.hg

annotate tools/libxl/libxl.c @ 21141:fe30bd463e84

xl: vcpu-list command

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