debuggers.hg

annotate tools/libxl/libxl.c @ 20542:9ed5d7f23db5

libxenlight: implement stubdom support

this patch implements stubdom support for libxenlight:

- it adds two functions to find the stubdom domid of a domain and to
figure out if a certain domain is actually a stubdom;

- it moves all the device init functions from xl.c to libxl.c because
they are needed to setup the devices of stubdoms;

- it fixes some bugs in the pci setup that prevented pci passthrough
from working correctly with stubdoms.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Nov 25 14:11:37 2009 +0000 (2009-11-25)
parents a986c3c5b163
children 0e64f0b7d049
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/stat.h>
keir@20462 23 #include <sys/types.h>
keir@20462 24 #include <fcntl.h>
keir@20462 25 #include <sys/select.h>
keir@20514 26 #include <sys/wait.h>
keir@20462 27 #include <signal.h>
keir@20468 28 #include <unistd.h> /* for write, unlink and close */
keir@20509 29 #include <stdint.h>
keir@20509 30 #include <inttypes.h>
keir@20514 31 #include <assert.h>
keir@20513 32
keir@20462 33 #include "libxl.h"
keir@20462 34 #include "libxl_utils.h"
keir@20462 35 #include "libxl_internal.h"
keir@20462 36 #include "flexarray.h"
keir@20462 37
keir@20462 38 int libxl_ctx_init(struct libxl_ctx *ctx)
keir@20462 39 {
keir@20462 40 memset(ctx, 0, sizeof(struct libxl_ctx));
keir@20462 41 ctx->alloc_maxsize = 256;
keir@20462 42 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
keir@20462 43 if (!ctx->alloc_ptrs)
keir@20462 44 return ERROR_NOMEM;
keir@20462 45
keir@20462 46 ctx->xch = xc_interface_open();
keir@20462 47 ctx->xsh = xs_daemon_open();
keir@20514 48
keir@20514 49 ctx->waitpid_instead= libxl_waitpid_instead_default;
keir@20462 50 return 0;
keir@20462 51 }
keir@20462 52
keir@20462 53 int libxl_ctx_free(struct libxl_ctx *ctx)
keir@20462 54 {
keir@20462 55 libxl_free_all(ctx);
keir@20462 56 free(ctx->alloc_ptrs);
keir@20462 57 ctx->alloc_ptrs = NULL;
keir@20462 58 xc_interface_close(ctx->xch);
keir@20462 59 xs_daemon_close(ctx->xsh);
keir@20462 60 return 0;
keir@20462 61 }
keir@20462 62
keir@20462 63 int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, void *log_data)
keir@20462 64 {
keir@20462 65 ctx->log_callback = log_callback;
keir@20462 66 ctx->log_userdata = log_data;
keir@20462 67 return 0;
keir@20462 68 }
keir@20462 69
keir@20462 70 /******************************************************************************/
keir@20462 71
keir@20462 72 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
keir@20462 73 uint32_t *domid)
keir@20462 74 {
keir@20462 75 int flags, ret, i;
keir@20468 76 char *uuid_string;
keir@20462 77 char *rw_paths[] = { "device" };
keir@20462 78 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
keir@20462 79 "control", "attr", "data", "messages" };
keir@20462 80 char *dom_path, *vm_path, *vss_path;
keir@20462 81 struct xs_permissions roperm[2];
keir@20462 82 struct xs_permissions rwperm[1];
keir@20462 83 xs_transaction_t t;
keir@20468 84 xen_domain_handle_t handle;
keir@20462 85
keir@20468 86 uuid_string = libxl_uuid_to_string(ctx, info->uuid);
keir@20462 87 if (!uuid_string) {
keir@20462 88 XL_LOG(ctx, XL_LOG_ERROR, "missing uuid");
keir@20462 89 return ERROR_FAIL;
keir@20462 90 }
keir@20462 91
keir@20462 92 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
keir@20462 93 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
keir@20462 94 *domid = 0;
keir@20462 95
keir@20468 96 /* XXX handle has to be initialised here.
keir@20468 97 * info->uuid != xen_domain_handle_t
keir@20468 98 * See:
keir@20468 99 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
keir@20468 100 * http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
keir@20468 101 *
keir@20468 102 * A DCE 1.1 compatible source representation of UUIDs.
keir@20468 103 *
keir@20468 104 * struct uuid {
keir@20468 105 * uint32_t time_low;
keir@20468 106 * uint16_t time_mid;
keir@20468 107 * uint16_t time_hi_and_version;
keir@20468 108 * uint8_t clock_seq_hi_and_reserved;
keir@20468 109 * uint8_t clock_seq_low;
keir@20468 110 * uint8_t node[_UUID_NODE_LEN];
keir@20468 111 * };
keir@20468 112 */
keir@20468 113
keir@20468 114 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
keir@20462 115 if (ret < 0) {
keir@20512 116 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
keir@20462 117 return ERROR_FAIL;
keir@20462 118 }
keir@20462 119
keir@20462 120 dom_path = libxl_xs_get_dompath(ctx, *domid);
keir@20512 121 if (!dom_path)
keir@20512 122 return ERROR_FAIL;
keir@20512 123
keir@20462 124 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
keir@20462 125 vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
keir@20512 126 if (!vm_path || !vss_path) {
keir@20462 127 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
keir@20462 128 return ERROR_FAIL;
keir@20462 129 }
keir@20462 130
keir@20462 131 roperm[0].id = 0;
keir@20462 132 roperm[0].perms = XS_PERM_NONE;
keir@20462 133 roperm[1].id = *domid;
keir@20462 134 roperm[1].perms = XS_PERM_READ;
keir@20462 135 rwperm[0].id = *domid;
keir@20462 136 rwperm[0].perms = XS_PERM_NONE;
keir@20462 137
keir@20462 138 retry_transaction:
keir@20462 139 t = xs_transaction_start(ctx->xsh);
keir@20462 140 xs_rm(ctx->xsh, t, dom_path);
keir@20462 141 xs_mkdir(ctx->xsh, t, dom_path);
keir@20462 142 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
keir@20462 143
keir@20462 144 xs_rm(ctx->xsh, t, vm_path);
keir@20462 145 xs_mkdir(ctx->xsh, t, vm_path);
keir@20462 146 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
keir@20462 147
keir@20462 148 xs_rm(ctx->xsh, t, vss_path);
keir@20462 149 xs_mkdir(ctx->xsh, t, vss_path);
keir@20462 150 xs_set_permissions(ctx->xsh, t, vss_path, rwperm, ARRAY_SIZE(rwperm));
keir@20462 151
keir@20462 152 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
keir@20462 153 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
keir@20462 154 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", dom_path), info->name, strlen(info->name));
keir@20462 155
keir@20462 156 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
keir@20462 157 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
keir@20462 158 xs_mkdir(ctx->xsh, t, path);
keir@20462 159 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
keir@20462 160 libxl_free(ctx, path);
keir@20462 161 }
keir@20462 162 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
keir@20462 163 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
keir@20462 164 xs_mkdir(ctx->xsh, t, path);
keir@20462 165 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20462 166 libxl_free(ctx, path);
keir@20462 167 }
keir@20462 168
keir@20462 169 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
keir@20462 170 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
keir@20462 171
keir@20462 172 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
keir@20462 173 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
keir@20462 174
keir@20462 175 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
keir@20462 176
keir@20462 177 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20462 178 if (errno == EAGAIN)
keir@20462 179 goto retry_transaction;
keir@20462 180 return 0;
keir@20462 181 }
keir@20462 182
keir@20516 183 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
keir@20462 184 {
keir@20462 185 char **vments = NULL, **localents = NULL;
keir@20462 186
keir@20509 187 build_pre(ctx, domid, info, state);
keir@20462 188 if (info->hvm) {
keir@20509 189 build_hvm(ctx, domid, info, state);
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@20509 196 build_pv(ctx, domid, info, state);
keir@20509 197 vments = libxl_calloc(ctx, 9, sizeof(char *));
keir@20516 198 vments[0] = "image/ostype";
keir@20516 199 vments[1] = "linux";
keir@20516 200 vments[2] = "image/kernel";
keir@20516 201 vments[3] = (char*) info->kernel;
keir@20516 202 vments[4] = "image/ramdisk";
keir@20516 203 vments[5] = (char*) info->u.pv.ramdisk;
keir@20516 204 vments[6] = "image/cmdline";
keir@20516 205 vments[7] = (char*) info->u.pv.cmdline;
keir@20462 206 }
keir@20509 207 build_post(ctx, domid, info, state, vments, localents);
keir@20516 208 return 0;
keir@20462 209 }
keir@20462 210
keir@20462 211 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
keir@20462 212 uint32_t domid, int fd)
keir@20462 213 {
keir@20462 214 libxl_domain_build_state state;
keir@20462 215 char **vments = NULL, **localents = NULL;
keir@20462 216
keir@20462 217 memset(&state, '\0', sizeof(state));
keir@20462 218
keir@20462 219 build_pre(ctx, domid, info, &state);
keir@20462 220 restore_common(ctx, domid, info, &state, fd);
keir@20462 221 if (info->hvm) {
keir@20462 222 vments = libxl_calloc(ctx, 4, sizeof(char *));
keir@20516 223 vments[0] = "rtc/timeoffset";
keir@20516 224 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
keir@20462 225 } else {
keir@20462 226 localents = libxl_calloc(ctx, 4 * 2, sizeof(char *));
keir@20516 227 localents[0] = "serial/0/limit";
keir@20462 228 localents[1] = libxl_sprintf(ctx, "%d", 65536);
keir@20516 229 localents[2] = "console/port";
keir@20462 230 localents[3] = libxl_sprintf(ctx, "%d", state.console_port);
keir@20516 231 localents[4] = "console/ring-ref";
keir@20462 232 localents[5] = libxl_sprintf(ctx, "%ld", state.console_mfn);
keir@20462 233 }
keir@20462 234 build_post(ctx, domid, info, &state, vments, localents);
keir@20462 235 return 0;
keir@20462 236 }
keir@20462 237
keir@20462 238 struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
keir@20462 239 {
keir@20462 240 struct libxl_dominfo *ptr;
keir@20462 241 int index, i, ret, first_domain;
keir@20462 242 xc_domaininfo_t info[16];
keir@20462 243 int size = 16;
keir@20462 244
keir@20462 245 first_domain = 1;
keir@20462 246 index = 0;
keir@20516 247 ptr = calloc(size, sizeof(struct libxl_dominfo));
keir@20462 248 if (!ptr)
keir@20462 249 return NULL;
keir@20462 250 redo:
keir@20462 251 ret = xc_domain_getinfolist(ctx->xch, first_domain, 16, info);
keir@20462 252 for (i = 0; i < ret; i++) {
keir@20462 253 if (index == size) {
keir@20462 254 struct libxl_dominfo *ptr2;
keir@20462 255
keir@20516 256 ptr2 = calloc(size * 2, sizeof(struct libxl_dominfo));
keir@20462 257 if (!ptr2) {
keir@20516 258 free(ptr);
keir@20462 259 return NULL;
keir@20462 260 }
keir@20462 261 memcpy(ptr2, ptr, sizeof(struct libxl_dominfo) * size);
keir@20516 262 free(ptr);
keir@20462 263 ptr = ptr2;
keir@20462 264 size *= 2;
keir@20462 265 }
keir@20462 266 memcpy(ptr[index].uuid, info[i].handle, 16 * sizeof(uint8_t));
keir@20462 267 ptr[index].domid = info[i].domain;
keir@20462 268 first_domain = info[i].domain + 1;
keir@20462 269 index++;
keir@20462 270 }
keir@20462 271 if (ret == 16)
keir@20462 272 goto redo;
keir@20462 273 *nb_domain = index;
keir@20462 274 return ptr;
keir@20462 275 }
keir@20462 276
keir@20462 277 xc_dominfo_t * libxl_domain_infolist(struct libxl_ctx *ctx, int *nb_domain)
keir@20462 278 {
keir@20462 279 int index, first_domain;
keir@20462 280 xc_dominfo_t *info;
keir@20462 281 int size = 1024;
keir@20462 282
keir@20462 283 first_domain = 0;
keir@20462 284 index = 0;
keir@20516 285 info = (xc_dominfo_t *) calloc(size, sizeof(xc_dominfo_t));
keir@20462 286 if (!info) {
keir@20462 287 *nb_domain = 0;
keir@20462 288 return NULL;
keir@20462 289 }
keir@20462 290 *nb_domain = xc_domain_getinfo(ctx->xch, first_domain, 1024, info);
keir@20462 291 return info;
keir@20462 292 }
keir@20462 293
keir@20462 294 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
keir@20462 295 uint32_t domid, int fd)
keir@20462 296 {
keir@20462 297 int hvm = 1;
keir@20462 298 int live = 0;
keir@20462 299 int debug = 0;
keir@20462 300 char savesig[] = "XenSavedDomain\n";
keir@20462 301
keir@20462 302 write(fd, savesig, strlen(savesig));
keir@20462 303
keir@20462 304 core_suspend(ctx, domid, fd, hvm, live, debug);
keir@20462 305
keir@20462 306 return 0;
keir@20462 307 }
keir@20462 308
keir@20462 309 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 310 {
keir@20462 311 xc_domain_pause(ctx->xch, domid);
keir@20462 312 return 0;
keir@20462 313 }
keir@20462 314
keir@20462 315 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 316 {
keir@20462 317 xc_domain_unpause(ctx->xch, domid);
keir@20462 318 return 0;
keir@20462 319 }
keir@20462 320
keir@20462 321 static char *req_table[] = {
keir@20462 322 [0] = "poweroff",
keir@20462 323 [1] = "reboot",
keir@20462 324 [2] = "suspend",
keir@20462 325 [3] = "crash",
keir@20462 326 [4] = "halt",
keir@20462 327 };
keir@20462 328
keir@20462 329 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
keir@20462 330 {
keir@20462 331 char *shutdown_path;
keir@20462 332 char *dom_path;
keir@20462 333
keir@20462 334 if (req > ARRAY_SIZE(req_table))
keir@20462 335 return ERROR_INVAL;
keir@20462 336
keir@20462 337 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 338 if (!dom_path)
keir@20512 339 return ERROR_FAIL;
keir@20512 340
keir@20462 341 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
keir@20462 342
keir@20462 343 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
keir@20462 344 if (/* hvm */ 0) {
keir@20462 345 unsigned long acpi_s_state = 0;
keir@20462 346 unsigned long pvdriver = 0;
keir@20462 347 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
keir@20462 348 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
keir@20462 349 if (!pvdriver && acpi_s_state != 0)
keir@20462 350 xc_domain_shutdown(ctx->xch, domid, req);
keir@20462 351 }
keir@20462 352 return 0;
keir@20462 353 }
keir@20462 354
keir@20462 355 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 356 {
keir@20462 357 char *pid;
keir@20462 358 int ret;
keir@20462 359
keir@20462 360 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
keir@20462 361 if (!pid) {
keir@20542 362 int stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20542 363 if (!stubdomid) {
keir@20542 364 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
keir@20542 365 return -1;
keir@20542 366 }
keir@20542 367 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
keir@20542 368 return libxl_domain_destroy(ctx, stubdomid, 0);
keir@20462 369 }
keir@20462 370 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
keir@20462 371
keir@20462 372 ret = kill(atoi(pid), SIGHUP);
keir@20462 373 if (ret < 0 && errno == ESRCH) {
keir@20512 374 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
keir@20462 375 ret = 0;
keir@20462 376 } else if (ret == 0) {
keir@20512 377 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
keir@20462 378 ret = 0;
keir@20462 379 } else {
keir@20512 380 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
keir@20512 381 atoi(pid));
keir@20462 382 }
keir@20462 383 return ret;
keir@20462 384 }
keir@20462 385
keir@20462 386 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
keir@20462 387 {
keir@20462 388 char *dom_path, vm_path[41];
keir@20468 389 xen_uuid_t *uuid;
keir@20512 390 int rc;
keir@20462 391
keir@20462 392 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 393 if (!dom_path)
keir@20462 394 return -1;
keir@20512 395
keir@20462 396 if (libxl_domid_to_uuid(ctx, &uuid, domid) < 0) {
keir@20512 397 XL_LOG(ctx, XL_LOG_ERROR, "failed ot get uuid for %d", domid);
keir@20462 398 return -1;
keir@20462 399 }
keir@20482 400 if (libxl_device_pci_shutdown(ctx, domid) < 0)
keir@20512 401 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
keir@20462 402 xs_write(ctx->xsh, XBT_NULL,
keir@20462 403 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
keir@20462 404 "shutdown", strlen("shutdown"));
keir@20512 405 rc = xc_domain_pause(ctx->xch, domid);
keir@20512 406 if (rc < 0) {
keir@20512 407 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
keir@20462 408 return -1;
keir@20462 409 }
keir@20542 410 if (libxl_destroy_device_model(ctx, domid) < 0)
keir@20542 411 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
keir@20512 412 rc = xc_domain_destroy(ctx->xch, domid);
keir@20512 413 if (rc < 0) {
keir@20512 414 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
keir@20462 415 return -1;
keir@20462 416 }
keir@20462 417 if (libxl_devices_destroy(ctx, domid, force) < 0)
keir@20512 418 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
keir@20462 419 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
keir@20512 420 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
keir@20468 421 snprintf(vm_path, sizeof(vm_path), "/vm/%s", libxl_uuid_to_string(ctx, uuid));
keir@20462 422 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
keir@20512 423 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
keir@20462 424 return 0;
keir@20462 425 }
keir@20462 426
keir@20462 427 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
keir@20462 428 libxl_device_model_info *info,
keir@20462 429 libxl_device_nic *vifs,
keir@20462 430 int num_vifs)
keir@20462 431 {
keir@20462 432 int num = 0, i;
keir@20462 433 flexarray_t *dm_args;
keir@20462 434 dm_args = flexarray_make(16, 1);
keir@20462 435 if (!dm_args)
keir@20462 436 return NULL;
keir@20462 437
keir@20516 438 flexarray_set(dm_args, num++, "qemu-dm");
keir@20516 439 flexarray_set(dm_args, num++, "-d");
keir@20462 440
keir@20462 441 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
keir@20462 442
keir@20462 443 if (info->dom_name) {
keir@20516 444 flexarray_set(dm_args, num++, "-domain-name");
keir@20516 445 flexarray_set(dm_args, num++, info->dom_name);
keir@20462 446 }
keir@20462 447 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
keir@20516 448 flexarray_set(dm_args, num++, "-vnc");
keir@20462 449 if (info->vncdisplay) {
keir@20462 450 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
keir@20462 451 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:%d", info->vnclisten, info->vncdisplay));
keir@20462 452 } else {
keir@20462 453 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
keir@20462 454 }
keir@20462 455 } else if (info->vnclisten) {
keir@20462 456 if (strchr(info->vnclisten, ':') != NULL) {
keir@20516 457 flexarray_set(dm_args, num++, info->vnclisten);
keir@20462 458 } else {
keir@20462 459 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
keir@20462 460 }
keir@20462 461 } else {
keir@20516 462 flexarray_set(dm_args, num++, "127.0.0.1:0");
keir@20462 463 }
keir@20462 464 if (info->vncunused) {
keir@20516 465 flexarray_set(dm_args, num++, "-vncunused");
keir@20462 466 }
keir@20462 467 }
keir@20462 468 if (info->sdl || info->opengl) {
keir@20516 469 flexarray_set(dm_args, num++, "-sdl");
keir@20462 470 if (info->opengl) {
keir@20516 471 flexarray_set(dm_args, num++, "-disable-opengl");
keir@20462 472 }
keir@20462 473 }
keir@20462 474 if (info->keymap) {
keir@20516 475 flexarray_set(dm_args, num++, "-k");
keir@20516 476 flexarray_set(dm_args, num++, info->keymap);
keir@20462 477 }
keir@20462 478 if (info->nographic && (!info->sdl && !info->vnc)) {
keir@20516 479 flexarray_set(dm_args, num++, "-nographic");
keir@20462 480 }
keir@20462 481 if (info->serial) {
keir@20516 482 flexarray_set(dm_args, num++, "-serial");
keir@20516 483 flexarray_set(dm_args, num++, info->serial);
keir@20462 484 }
keir@20509 485 if (info->type == XENFV) {
keir@20509 486 if (info->videoram) {
keir@20516 487 flexarray_set(dm_args, num++, "-videoram");
keir@20509 488 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
keir@20509 489 }
keir@20509 490 if (info->stdvga) {
keir@20516 491 flexarray_set(dm_args, num++, "-std-vga");
keir@20509 492 }
keir@20509 493
keir@20509 494 if (info->boot) {
keir@20516 495 flexarray_set(dm_args, num++, "-boot");
keir@20516 496 flexarray_set(dm_args, num++, info->boot);
keir@20509 497 }
keir@20509 498 if (info->usb) {
keir@20516 499 flexarray_set(dm_args, num++, "-usb");
keir@20509 500 if (info->usbdevice) {
keir@20516 501 flexarray_set(dm_args, num++, "-usbdevice");
keir@20516 502 flexarray_set(dm_args, num++, info->usbdevice);
keir@20509 503 }
keir@20509 504 }
keir@20509 505 if (info->apic) {
keir@20516 506 flexarray_set(dm_args, num++, "-acpi");
keir@20509 507 }
keir@20509 508 for (i = 0; i < num_vifs; i++) {
keir@20509 509 if (vifs[i].nictype == NICTYPE_IOEMU) {
keir@20516 510 flexarray_set(dm_args, num++, "-net");
keir@20509 511 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
keir@20509 512 vifs[i].devid, vifs[i].smac, vifs[i].model));
keir@20516 513 flexarray_set(dm_args, num++, "-net");
keir@20509 514 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
keir@20509 515 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
keir@20509 516 }
keir@20462 517 }
keir@20462 518 }
keir@20509 519 for (i = 0; info->extra && info->extra[i] != NULL; i++)
keir@20509 520 flexarray_set(dm_args, num++, info->extra[i]);
keir@20516 521 flexarray_set(dm_args, num++, "-M");
keir@20509 522 if (info->type == XENPV)
keir@20516 523 flexarray_set(dm_args, num++, "xenpv");
keir@20509 524 else
keir@20516 525 flexarray_set(dm_args, num++, "xenfv");
keir@20462 526 flexarray_set(dm_args, num++, NULL);
keir@20462 527
keir@20462 528 return (char **) flexarray_contents(dm_args);
keir@20462 529 }
keir@20462 530
keir@20514 531 struct libxl_device_model_starting {
keir@20514 532 struct libxl_spawn_starting for_spawn; /* first! */
keir@20514 533 char *dom_path; /* from libxl_malloc, only for dm_xenstore_record_pid */
keir@20514 534 int domid;
keir@20514 535 };
keir@20514 536
keir@20514 537 void dm_xenstore_record_pid(struct libxl_ctx *ctx, void *for_spawn,
keir@20514 538 pid_t innerchild) {
keir@20514 539 struct libxl_device_model_starting *starting = for_spawn;
keir@20514 540 struct libxl_ctx clone;
keir@20514 541 char *kvs[3];
keir@20514 542 int rc;
keir@20514 543
keir@20514 544 clone = *ctx;
keir@20514 545 clone.xsh = xs_daemon_open();
keir@20514 546 /* we mustn't use the parent's handle in the child */
keir@20514 547
keir@20514 548 kvs[0] = "image/device-model-pid";
keir@20514 549 kvs[1] = libxl_sprintf(ctx, "%d", innerchild);
keir@20514 550 kvs[2] = NULL;
keir@20514 551 rc = libxl_xs_writev(ctx, XBT_NULL, starting->dom_path, kvs);
keir@20514 552 if (rc) XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@20514 553 "Couldn't record device model pid %ld at %s/%s",
keir@20514 554 (unsigned long)innerchild, starting->dom_path, kvs);
keir@20514 555 }
keir@20514 556
keir@20542 557 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
keir@20542 558 libxl_device_model_info *info,
keir@20542 559 libxl_device_vfb *vfb,
keir@20542 560 libxl_device_vkb *vkb)
keir@20542 561 {
keir@20542 562 memset(vfb, 0x00, sizeof(libxl_device_vfb));
keir@20542 563 memset(vkb, 0x00, sizeof(libxl_device_vkb));
keir@20542 564
keir@20542 565 vfb->backend_domid = 0;
keir@20542 566 vfb->devid = 0;
keir@20542 567 vfb->vnc = info->vnc;
keir@20542 568 vfb->vnclisten = info->vnclisten;
keir@20542 569 vfb->vncdisplay = info->vncdisplay;
keir@20542 570 vfb->vncunused = info->vncunused;
keir@20542 571 vfb->keymap = info->keymap;
keir@20542 572 vfb->sdl = info->sdl;
keir@20542 573 vfb->opengl = info->opengl;
keir@20542 574
keir@20542 575 vkb->backend_domid = 0;
keir@20542 576 vkb->devid = 0;
keir@20542 577 return 0;
keir@20542 578 }
keir@20542 579
keir@20542 580 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
keir@20542 581 {
keir@20542 582 int i;
keir@20542 583 char *vm_path;
keir@20542 584 char *dmargs, *path;
keir@20542 585 int dmargs_size;
keir@20542 586 struct xs_permissions roperm[2];
keir@20542 587 xs_transaction_t t;
keir@20542 588
keir@20542 589 roperm[0].id = 0;
keir@20542 590 roperm[0].perms = XS_PERM_NONE;
keir@20542 591 roperm[1].id = domid;
keir@20542 592 roperm[1].perms = XS_PERM_READ;
keir@20542 593
keir@20542 594 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
keir@20542 595
keir@20542 596 i = 0;
keir@20542 597 dmargs_size = 0;
keir@20542 598 while (args[i] != NULL) {
keir@20542 599 dmargs_size = dmargs_size + strlen(args[i]) + 1;
keir@20542 600 i++;
keir@20542 601 }
keir@20542 602 dmargs_size++;
keir@20542 603 dmargs = (char *) malloc(dmargs_size);
keir@20542 604 i = 1;
keir@20542 605 dmargs[0] = '\0';
keir@20542 606 while (args[i] != NULL) {
keir@20542 607 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
keir@20542 608 strcat(dmargs, " ");
keir@20542 609 strcat(dmargs, args[i]);
keir@20542 610 }
keir@20542 611 i++;
keir@20542 612 }
keir@20542 613 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
keir@20542 614
keir@20542 615 retry_transaction:
keir@20542 616 t = xs_transaction_start(ctx->xsh);
keir@20542 617 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
keir@20542 618 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20542 619 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
keir@20542 620 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 621 if (errno == EAGAIN)
keir@20542 622 goto retry_transaction;
keir@20542 623 free(dmargs);
keir@20542 624 return 0;
keir@20542 625 }
keir@20542 626
keir@20542 627 static int libxl_create_stubdom(struct libxl_ctx *ctx,
keir@20542 628 libxl_device_model_info *info,
keir@20542 629 libxl_device_disk *disks, int num_disks,
keir@20542 630 libxl_device_nic *vifs, int num_vifs,
keir@20542 631 libxl_device_vfb *vfb,
keir@20542 632 libxl_device_vkb *vkb,
keir@20542 633 libxl_device_model_starting **starting_r)
keir@20542 634 {
keir@20542 635 int i;
keir@20542 636 libxl_device_console console;
keir@20542 637 libxl_domain_create_info c_info;
keir@20542 638 libxl_domain_build_info b_info;
keir@20542 639 libxl_domain_build_state state;
keir@20542 640 uint32_t domid;
keir@20542 641 char **args;
keir@20542 642 xen_uuid_t uuid[16];
keir@20542 643 struct xs_permissions perm[2];
keir@20542 644 xs_transaction_t t;
keir@20542 645
keir@20542 646 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20542 647 if (!args)
keir@20542 648 return ERROR_FAIL;
keir@20542 649
keir@20542 650 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
keir@20542 651 c_info.hvm = 0;
keir@20542 652 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
keir@20542 653 xen_uuid_generate(uuid);
keir@20542 654 c_info.uuid = uuid;
keir@20542 655
keir@20542 656 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
keir@20542 657 b_info.max_vcpus = 1;
keir@20542 658 b_info.max_memkb = 32 * 1024;
keir@20542 659 b_info.kernel = "/usr/lib/xen/boot/ioemu-stubdom.gz";
keir@20542 660 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
keir@20542 661 b_info.u.pv.ramdisk = "";
keir@20542 662 b_info.u.pv.features = "";
keir@20542 663 b_info.hvm = 0;
keir@20542 664
keir@20542 665 libxl_domain_make(ctx, &c_info, &domid);
keir@20542 666 libxl_domain_build(ctx, &b_info, domid, &state);
keir@20542 667
keir@20542 668 libxl_write_dmargs(ctx, domid, info->domid, args);
keir@20542 669 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)), "%d", domid);
keir@20542 670 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)), "%d", info->domid);
keir@20542 671 xc_domain_set_target(ctx->xch, domid, info->domid);
keir@20542 672 xs_set_target(ctx->xsh, domid, info->domid);
keir@20542 673
keir@20542 674 perm[0].id = domid;
keir@20542 675 perm[0].perms = XS_PERM_NONE;
keir@20542 676 perm[1].id = info->domid;
keir@20542 677 perm[1].perms = XS_PERM_READ;
keir@20542 678 retry_transaction:
keir@20542 679 t = xs_transaction_start(ctx->xsh);
keir@20542 680 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
keir@20542 681 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
keir@20542 682 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 683 if (errno == EAGAIN)
keir@20542 684 goto retry_transaction;
keir@20542 685
keir@20542 686 for (i = 0; i < num_disks; i++) {
keir@20542 687 libxl_device_disk disk = disks[i];
keir@20542 688 disk_info_domid_fixup(&disk, domid);
keir@20542 689 libxl_device_disk_add(ctx, domid, &disk);
keir@20542 690 }
keir@20542 691 for (i = 0; i < num_vifs; i++) {
keir@20542 692 libxl_device_nic nic = vifs[i];
keir@20542 693 nic_info_domid_fixup(&nic, domid);
keir@20542 694 libxl_device_nic_add(ctx, domid, &nic);
keir@20542 695 }
keir@20542 696 vfb_info_domid_fixup(vfb, domid);
keir@20542 697 libxl_device_vfb_add(ctx, domid, vfb);
keir@20542 698 vkb_info_domid_fixup(vkb, domid);
keir@20542 699 libxl_device_vkb_add(ctx, domid, vkb);
keir@20542 700
keir@20542 701 init_console_info(&console, 0, &state);
keir@20542 702 console_info_domid_fixup(&console, domid);
keir@20542 703 console.constype = CONSTYPE_IOEMU;
keir@20542 704 libxl_device_console_add(ctx, domid, &console);
keir@20542 705 libxl_create_xenpv_qemu(ctx, vfb, 1, &console, starting_r);
keir@20542 706
keir@20542 707 libxl_domain_unpause(ctx, domid);
keir@20542 708
keir@20542 709 free(args);
keir@20542 710 return 0;
keir@20542 711 }
keir@20542 712
keir@20462 713 int libxl_create_device_model(struct libxl_ctx *ctx,
keir@20462 714 libxl_device_model_info *info,
keir@20542 715 libxl_device_disk *disks, int num_disks,
keir@20514 716 libxl_device_nic *vifs, int num_vifs,
keir@20514 717 libxl_device_model_starting **starting_r)
keir@20462 718 {
keir@20514 719 char *path, *logfile, *logfile_new;
keir@20462 720 struct stat stat_buf;
keir@20514 721 int logfile_w, null;
keir@20514 722 int i, rc;
keir@20462 723 char **args;
keir@20514 724 struct libxl_spawn_starting buf_spawn, *for_spawn;
keir@20514 725
keir@20542 726 if (strstr(info->device_model, "stubdom-dm")) {
keir@20542 727 libxl_device_vfb vfb;
keir@20542 728 libxl_device_vkb vkb;
keir@20542 729
keir@20542 730 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
keir@20542 731 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
keir@20542 732 }
keir@20542 733
keir@20514 734 *starting_r= 0;
keir@20462 735
keir@20462 736 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20462 737 if (!args)
keir@20462 738 return ERROR_FAIL;
keir@20462 739
keir@20462 740 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
keir@20462 741 xs_mkdir(ctx->xsh, XBT_NULL, path);
keir@20462 742
keir@20462 743 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
keir@20462 744 if (stat(logfile, &stat_buf) == 0) {
keir@20462 745 /* file exists, rotate */
keir@20462 746 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.10", info->dom_name);
keir@20462 747 unlink(logfile);
keir@20462 748 for (i = 9; i > 0; i--) {
keir@20462 749 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i);
keir@20462 750 logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i + 1);
keir@20462 751 rename(logfile, logfile_new);
keir@20462 752 }
keir@20462 753 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
keir@20462 754 logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.1", info->dom_name);
keir@20462 755 rename(logfile, logfile_new);
keir@20462 756 }
keir@20462 757 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
keir@20467 758 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
keir@20462 759 null = open("/dev/null", O_RDONLY);
keir@20514 760
keir@20514 761 if (starting_r) {
keir@20516 762 rc = ERROR_NOMEM;
keir@20514 763 *starting_r= libxl_calloc(ctx, sizeof(**starting_r), 1);
keir@20516 764 if (!*starting_r) goto xit;
keir@20514 765 (*starting_r)->domid= info->domid;
keir@20514 766
keir@20514 767 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20514 768 if (!(*starting_r)->dom_path) { free(*starting_r); return ERROR_FAIL; }
keir@20514 769
keir@20514 770 for_spawn= &(*starting_r)->for_spawn;
keir@20514 771 } else {
keir@20514 772 for_spawn= &buf_spawn;
keir@20514 773 }
keir@20514 774 rc = libxl_spawn_spawn(ctx, for_spawn, "device model",
keir@20514 775 dm_xenstore_record_pid);
keir@20514 776 if (rc < 0) goto xit;
keir@20514 777 if (!rc) { /* inner child */
keir@20514 778 libxl_exec(ctx, null, logfile_w, logfile_w,
keir@20514 779 info->device_model, args);
keir@20514 780 }
keir@20514 781
keir@20514 782 rc = 0;
keir@20514 783 xit:
keir@20516 784 free(args);
keir@20462 785 close(null);
keir@20462 786 close(logfile_w);
keir@20462 787
keir@20514 788 return rc;
keir@20514 789 }
keir@20514 790
keir@20514 791 int libxl_detach_device_model(struct libxl_ctx *ctx,
keir@20514 792 libxl_device_model_starting *starting) {
keir@20514 793 int rc;
keir@20514 794 rc = libxl_spawn_detach(ctx, &starting->for_spawn);
keir@20514 795 libxl_free(ctx, starting);
keir@20514 796 return rc;
keir@20514 797 }
keir@20462 798
keir@20514 799
keir@20514 800 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
keir@20514 801 libxl_device_model_starting *starting) {
keir@20514 802 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
keir@20514 803 libxl_spawn_check,
keir@20514 804 &starting->for_spawn);
keir@20514 805 int detach = libxl_detach_device_model(ctx, starting);
keir@20514 806 return problem ? problem : detach;
keir@20542 807 return 0;
keir@20462 808 }
keir@20462 809
keir@20514 810
keir@20462 811 /******************************************************************************/
keir@20462 812 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20462 813 {
keir@20462 814 flexarray_t *front;
keir@20462 815 flexarray_t *back;
keir@20462 816 char *backend_type;
keir@20462 817 unsigned int boffset = 0;
keir@20462 818 unsigned int foffset = 0;
keir@20462 819 int devid;
keir@20462 820 libxl_device device;
keir@20462 821
keir@20462 822 front = flexarray_make(16, 1);
keir@20462 823 if (!front)
keir@20462 824 return ERROR_NOMEM;
keir@20462 825 back = flexarray_make(16, 1);
keir@20462 826 if (!back) /* leaks front if error */
keir@20462 827 return ERROR_NOMEM;
keir@20462 828
keir@20462 829 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
keir@20462 830 devid = device_disk_dev_number(disk->virtpath);
keir@20462 831
keir@20462 832 device.backend_devid = devid;
keir@20462 833 device.backend_domid = disk->backend_domid;
keir@20462 834 device.devid = devid;
keir@20462 835 device.domid = disk->domid;
keir@20462 836 device.kind = DEVICE_VBD;
keir@20462 837
keir@20462 838 switch (disk->phystype) {
keir@20462 839 case PHYSTYPE_FILE:
keir@20462 840 return ERROR_NI; /* FIXME */
keir@20462 841 break;
keir@20462 842 case PHYSTYPE_PHY: {
keir@20462 843 int major, minor;
keir@20462 844
keir@20509 845 device_physdisk_major_minor(disk->physpath, &major, &minor);
keir@20516 846 flexarray_set(back, boffset++, "physical-device");
keir@20462 847 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20462 848
keir@20516 849 flexarray_set(back, boffset++, "params");
keir@20516 850 flexarray_set(back, boffset++, disk->physpath);
keir@20462 851
keir@20462 852 device.backend_kind = DEVICE_VBD;
keir@20462 853 break;
keir@20462 854 }
keir@20462 855 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
keir@20516 856 flexarray_set(back, boffset++, "params");
keir@20462 857 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
keir@20462 858 device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20462 859
keir@20462 860 device.backend_kind = DEVICE_TAP;
keir@20462 861 break;
keir@20462 862 }
keir@20462 863
keir@20516 864 flexarray_set(back, boffset++, "frontend-id");
keir@20462 865 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
keir@20516 866 flexarray_set(back, boffset++, "online");
keir@20516 867 flexarray_set(back, boffset++, "1");
keir@20516 868 flexarray_set(back, boffset++, "removable");
keir@20462 869 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
keir@20516 870 flexarray_set(back, boffset++, "bootable");
keir@20509 871 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 872 flexarray_set(back, boffset++, "state");
keir@20462 873 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 874 flexarray_set(back, boffset++, "dev");
keir@20516 875 flexarray_set(back, boffset++, disk->virtpath);
keir@20516 876 flexarray_set(back, boffset++, "type");
keir@20516 877 flexarray_set(back, boffset++, backend_type);
keir@20516 878 flexarray_set(back, boffset++, "mode");
keir@20516 879 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
keir@20462 880
keir@20516 881 flexarray_set(front, foffset++, "backend-id");
keir@20462 882 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
keir@20516 883 flexarray_set(front, foffset++, "state");
keir@20462 884 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 885 flexarray_set(front, foffset++, "virtual-device");
keir@20462 886 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
keir@20516 887 flexarray_set(front, foffset++, "device-type");
keir@20516 888 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
keir@20462 889
keir@20462 890 if (0 /* protocol != native*/) {
keir@20516 891 flexarray_set(front, foffset++, "protocol");
keir@20516 892 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 893 }
keir@20462 894
keir@20462 895 libxl_device_generic_add(ctx, &device,
keir@20462 896 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 897 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 898 flexarray_free(back);
keir@20516 899 flexarray_free(front);
keir@20462 900 return 0;
keir@20462 901 }
keir@20462 902
keir@20462 903 int libxl_device_disk_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 904 {
keir@20462 905 return ERROR_NI;
keir@20462 906 }
keir@20462 907
keir@20462 908 int libxl_device_disk_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 909 {
keir@20462 910 return ERROR_NI;
keir@20462 911 }
keir@20462 912
keir@20462 913 /******************************************************************************/
keir@20462 914 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
keir@20462 915 {
keir@20462 916 flexarray_t *front;
keir@20462 917 flexarray_t *back;
keir@20462 918 unsigned int boffset = 0;
keir@20462 919 unsigned int foffset = 0;
keir@20462 920 libxl_device device;
keir@20462 921
keir@20462 922 front = flexarray_make(16, 1);
keir@20462 923 if (!front)
keir@20462 924 return ERROR_NOMEM;
keir@20462 925 back = flexarray_make(16, 1);
keir@20462 926 if (!back)
keir@20462 927 return ERROR_NOMEM;
keir@20462 928
keir@20462 929 device.backend_devid = nic->devid;
keir@20462 930 device.backend_domid = nic->backend_domid;
keir@20462 931 device.backend_kind = DEVICE_VIF;
keir@20462 932 device.devid = nic->devid;
keir@20462 933 device.domid = nic->domid;
keir@20462 934 device.kind = DEVICE_VIF;
keir@20462 935
keir@20516 936 flexarray_set(back, boffset++, "frontend-id");
keir@20462 937 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
keir@20516 938 flexarray_set(back, boffset++, "online");
keir@20516 939 flexarray_set(back, boffset++, "1");
keir@20516 940 flexarray_set(back, boffset++, "state");
keir@20462 941 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 942 flexarray_set(back, boffset++, "script");
keir@20516 943 flexarray_set(back, boffset++, nic->script);
keir@20516 944 flexarray_set(back, boffset++, "mac");
keir@20462 945 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 946 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 947 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20516 948 flexarray_set(back, boffset++, "handle");
keir@20462 949 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20462 950
keir@20516 951 flexarray_set(front, foffset++, "backend-id");
keir@20462 952 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
keir@20516 953 flexarray_set(front, foffset++, "state");
keir@20462 954 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 955 flexarray_set(front, foffset++, "handle");
keir@20462 956 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20516 957 flexarray_set(front, foffset++, "mac");
keir@20462 958 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 959 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 960 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20462 961 if (0 /* protocol != native*/) {
keir@20516 962 flexarray_set(front, foffset++, "protocol");
keir@20516 963 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 964 }
keir@20462 965
keir@20462 966 libxl_device_generic_add(ctx, &device,
keir@20462 967 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 968 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20462 969
keir@20462 970 /* FIXME: wait for plug */
keir@20516 971 flexarray_free(back);
keir@20516 972 flexarray_free(front);
keir@20462 973 return 0;
keir@20462 974 }
keir@20462 975
keir@20462 976 int libxl_device_nic_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 977 {
keir@20462 978 return ERROR_NI;
keir@20462 979 }
keir@20462 980
keir@20462 981 int libxl_device_nic_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 982 {
keir@20462 983 return ERROR_NI;
keir@20462 984 }
keir@20462 985
keir@20462 986 /******************************************************************************/
keir@20509 987 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
keir@20462 988 {
keir@20509 989 flexarray_t *front;
keir@20509 990 flexarray_t *back;
keir@20509 991 unsigned int boffset = 0;
keir@20509 992 unsigned int foffset = 0;
keir@20509 993 libxl_device device;
keir@20509 994
keir@20509 995 if (console->build_state) {
keir@20509 996 xs_transaction_t t;
keir@20509 997 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
keir@20516 998 ents[0] = "console/port";
keir@20509 999 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
keir@20516 1000 ents[2] = "console/ring-ref";
keir@20509 1001 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
keir@20516 1002 ents[4] = "console/limit";
keir@20509 1003 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
keir@20516 1004 ents[6] = "console/type";
keir@20509 1005 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20509 1006 ents[7] = "xenconsoled";
keir@20509 1007 else
keir@20509 1008 ents[7] = "ioemu";
keir@20509 1009 retry_transaction:
keir@20509 1010 t = xs_transaction_start(ctx->xsh);
keir@20516 1011 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
keir@20509 1012 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20509 1013 if (errno == EAGAIN)
keir@20509 1014 goto retry_transaction;
keir@20509 1015 }
keir@20509 1016
keir@20509 1017 front = flexarray_make(16, 1);
keir@20509 1018 if (!front)
keir@20509 1019 return ERROR_NOMEM;
keir@20509 1020 back = flexarray_make(16, 1);
keir@20509 1021 if (!back)
keir@20509 1022 return ERROR_NOMEM;
keir@20509 1023
keir@20509 1024 device.backend_devid = console->devid;
keir@20509 1025 device.backend_domid = console->backend_domid;
keir@20509 1026 device.backend_kind = DEVICE_CONSOLE;
keir@20509 1027 device.devid = console->devid;
keir@20509 1028 device.domid = console->domid;
keir@20509 1029 device.kind = DEVICE_CONSOLE;
keir@20509 1030
keir@20516 1031 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1032 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
keir@20516 1033 flexarray_set(back, boffset++, "online");
keir@20516 1034 flexarray_set(back, boffset++, "1");
keir@20516 1035 flexarray_set(back, boffset++, "state");
keir@20509 1036 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1037 flexarray_set(back, boffset++, "domain");
keir@20516 1038 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1039 flexarray_set(back, boffset++, "protocol");
keir@20516 1040 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20509 1041
keir@20516 1042 flexarray_set(front, foffset++, "backend-id");
keir@20509 1043 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
keir@20516 1044 flexarray_set(front, foffset++, "state");
keir@20509 1045 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1046 flexarray_set(front, foffset++, "limit");
keir@20509 1047 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
keir@20516 1048 flexarray_set(front, foffset++, "protocol");
keir@20516 1049 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20516 1050 flexarray_set(front, foffset++, "type");
keir@20509 1051 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20516 1052 flexarray_set(front, foffset++, "xenconsoled");
keir@20509 1053 else
keir@20516 1054 flexarray_set(front, foffset++, "ioemu");
keir@20509 1055
keir@20509 1056 libxl_device_generic_add(ctx, &device,
keir@20509 1057 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1058 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1059 flexarray_free(back);
keir@20516 1060 flexarray_free(front);
keir@20509 1061
keir@20509 1062 return 0;
keir@20509 1063 }
keir@20509 1064
keir@20509 1065 /******************************************************************************/
keir@20509 1066 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
keir@20509 1067 {
keir@20509 1068 flexarray_t *front;
keir@20509 1069 flexarray_t *back;
keir@20509 1070 unsigned int boffset = 0;
keir@20509 1071 unsigned int foffset = 0;
keir@20509 1072 libxl_device device;
keir@20509 1073
keir@20509 1074 front = flexarray_make(16, 1);
keir@20509 1075 if (!front)
keir@20509 1076 return ERROR_NOMEM;
keir@20509 1077 back = flexarray_make(16, 1);
keir@20509 1078 if (!back)
keir@20509 1079 return ERROR_NOMEM;
keir@20509 1080
keir@20509 1081 device.backend_devid = vkb->devid;
keir@20509 1082 device.backend_domid = vkb->backend_domid;
keir@20509 1083 device.backend_kind = DEVICE_VKBD;
keir@20509 1084 device.devid = vkb->devid;
keir@20509 1085 device.domid = vkb->domid;
keir@20509 1086 device.kind = DEVICE_VKBD;
keir@20509 1087
keir@20516 1088 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1089 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
keir@20516 1090 flexarray_set(back, boffset++, "online");
keir@20516 1091 flexarray_set(back, boffset++, "1");
keir@20516 1092 flexarray_set(back, boffset++, "state");
keir@20509 1093 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1094 flexarray_set(back, boffset++, "domain");
keir@20516 1095 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20509 1096
keir@20516 1097 flexarray_set(front, foffset++, "backend-id");
keir@20509 1098 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
keir@20516 1099 flexarray_set(front, foffset++, "state");
keir@20509 1100 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1101
keir@20509 1102 libxl_device_generic_add(ctx, &device,
keir@20509 1103 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1104 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1105 flexarray_free(back);
keir@20516 1106 flexarray_free(front);
keir@20509 1107
keir@20509 1108 return 0;
keir@20462 1109 }
keir@20462 1110
keir@20462 1111 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1112 {
keir@20462 1113 return ERROR_NI;
keir@20462 1114 }
keir@20462 1115
keir@20462 1116 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1117 {
keir@20462 1118 return ERROR_NI;
keir@20462 1119 }
keir@20462 1120
keir@20462 1121 /******************************************************************************/
keir@20509 1122 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
keir@20509 1123 libxl_device_vfb *vfb,
keir@20509 1124 int num_console,
keir@20509 1125 libxl_device_console *console,
keir@20509 1126 libxl_device_model_info *info) {
keir@20509 1127 int i = 0, j = 0, num = 0;
keir@20509 1128 memset(info, 0x00, sizeof(libxl_device_model_info));
keir@20509 1129
keir@20509 1130 info->vnc = vfb->vnc;
keir@20509 1131 if (vfb->vnclisten)
keir@20509 1132 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
keir@20509 1133 info->vncdisplay = vfb->vncdisplay;
keir@20509 1134 info->vncunused = vfb->vncunused;
keir@20509 1135 if (vfb->keymap)
keir@20509 1136 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
keir@20509 1137 info->sdl = vfb->sdl;
keir@20509 1138 info->opengl = vfb->opengl;
keir@20509 1139 for (i = 0; i < num_console; i++) {
keir@20509 1140 if (console->constype == CONSTYPE_IOEMU)
keir@20509 1141 num++;
keir@20509 1142 }
keir@20509 1143 if (num > 0) {
keir@20509 1144 info->serial = "pty";
keir@20509 1145 num--;
keir@20509 1146 }
keir@20509 1147 if (num > 0) {
keir@20509 1148 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
keir@20509 1149 for (j = 0; j < num * 2; j = j + 2) {
keir@20509 1150 info->extra[j] = "-serial";
keir@20509 1151 info->extra[j + 1] = "pty";
keir@20509 1152 }
keir@20509 1153 info->extra[j] = NULL;
keir@20509 1154 }
keir@20509 1155 info->domid = vfb->domid;
keir@20509 1156 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
keir@20509 1157 info->device_model = "/usr/lib/xen/bin/qemu-dm";
keir@20509 1158 info->type = XENPV;
keir@20509 1159 return 0;
keir@20509 1160 }
keir@20509 1161
keir@20509 1162 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
keir@20514 1163 int num_console, libxl_device_console *console,
keir@20514 1164 struct libxl_device_model_starting **starting_r)
keir@20509 1165 {
keir@20509 1166 libxl_device_model_info info;
keir@20509 1167
keir@20509 1168 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
keir@20542 1169 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
keir@20509 1170 return 0;
keir@20509 1171 }
keir@20509 1172
keir@20509 1173 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
keir@20462 1174 {
keir@20509 1175 flexarray_t *front;
keir@20509 1176 flexarray_t *back;
keir@20509 1177 unsigned int boffset = 0;
keir@20509 1178 unsigned int foffset = 0;
keir@20509 1179 libxl_device device;
keir@20509 1180
keir@20509 1181 front = flexarray_make(16, 1);
keir@20509 1182 if (!front)
keir@20509 1183 return ERROR_NOMEM;
keir@20509 1184 back = flexarray_make(16, 1);
keir@20509 1185 if (!back)
keir@20509 1186 return ERROR_NOMEM;
keir@20509 1187
keir@20509 1188 device.backend_devid = vfb->devid;
keir@20509 1189 device.backend_domid = vfb->backend_domid;
keir@20509 1190 device.backend_kind = DEVICE_VFB;
keir@20509 1191 device.devid = vfb->devid;
keir@20509 1192 device.domid = vfb->domid;
keir@20509 1193 device.kind = DEVICE_VFB;
keir@20509 1194
keir@20516 1195 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1196 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
keir@20516 1197 flexarray_set(back, boffset++, "online");
keir@20516 1198 flexarray_set(back, boffset++, "1");
keir@20516 1199 flexarray_set(back, boffset++, "state");
keir@20509 1200 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1201 flexarray_set(back, boffset++, "domain");
keir@20516 1202 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1203 flexarray_set(back, boffset++, "vnc");
keir@20509 1204 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
keir@20516 1205 flexarray_set(back, boffset++, "vnclisten");
keir@20516 1206 flexarray_set(back, boffset++, vfb->vnclisten);
keir@20516 1207 flexarray_set(back, boffset++, "vncdisplay");
keir@20509 1208 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
keir@20516 1209 flexarray_set(back, boffset++, "vncunused");
keir@20509 1210 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
keir@20516 1211 flexarray_set(back, boffset++, "sdl");
keir@20509 1212 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
keir@20516 1213 flexarray_set(back, boffset++, "opengl");
keir@20509 1214 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
keir@20509 1215 if (vfb->xauthority) {
keir@20516 1216 flexarray_set(back, boffset++, "xauthority");
keir@20516 1217 flexarray_set(back, boffset++, vfb->xauthority);
keir@20509 1218 }
keir@20509 1219 if (vfb->display) {
keir@20516 1220 flexarray_set(back, boffset++, "display");
keir@20516 1221 flexarray_set(back, boffset++, vfb->display);
keir@20509 1222 }
keir@20509 1223
keir@20516 1224 flexarray_set(front, foffset++, "backend-id");
keir@20509 1225 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
keir@20516 1226 flexarray_set(front, foffset++, "state");
keir@20509 1227 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1228
keir@20509 1229 libxl_device_generic_add(ctx, &device,
keir@20509 1230 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1231 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20509 1232 flexarray_free(front);
keir@20509 1233 flexarray_free(back);
keir@20509 1234
keir@20509 1235 return 0;
keir@20462 1236 }
keir@20462 1237
keir@20462 1238 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1239 {
keir@20462 1240 return ERROR_NI;
keir@20462 1241 }
keir@20462 1242
keir@20462 1243 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1244 {
keir@20462 1245 return ERROR_NI;
keir@20462 1246 }
keir@20462 1247
keir@20462 1248 /******************************************************************************/
keir@20482 1249
keir@20482 1250 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
keir@20482 1251 unsigned int bus, unsigned int dev,
keir@20482 1252 unsigned int func, unsigned int vdevfn)
keir@20482 1253 {
keir@20482 1254 pcidev->domain = domain;
keir@20482 1255 pcidev->bus = bus;
keir@20482 1256 pcidev->dev = dev;
keir@20482 1257 pcidev->func = func;
keir@20482 1258 pcidev->vdevfn = vdevfn;
keir@20482 1259 return 0;
keir@20482 1260 }
keir@20482 1261
keir@20482 1262 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
keir@20482 1263 {
keir@20482 1264 flexarray_t *front;
keir@20482 1265 flexarray_t *back;
keir@20482 1266 unsigned int boffset = 0;
keir@20482 1267 unsigned int foffset = 0;
keir@20482 1268 libxl_device device;
keir@20482 1269 int i;
keir@20482 1270
keir@20482 1271 front = flexarray_make(16, 1);
keir@20482 1272 if (!front)
keir@20482 1273 return ERROR_NOMEM;
keir@20482 1274 back = flexarray_make(16, 1);
keir@20482 1275 if (!back)
keir@20482 1276 return ERROR_NOMEM;
keir@20482 1277
keir@20512 1278 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
keir@20482 1279
keir@20482 1280 /* add pci device */
keir@20482 1281 device.backend_devid = 0;
keir@20482 1282 device.backend_domid = 0;
keir@20482 1283 device.backend_kind = DEVICE_PCI;
keir@20482 1284 device.devid = 0;
keir@20482 1285 device.domid = domid;
keir@20482 1286 device.kind = DEVICE_PCI;
keir@20482 1287
keir@20516 1288 flexarray_set(back, boffset++, "frontend-id");
keir@20482 1289 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
keir@20516 1290 flexarray_set(back, boffset++, "online");
keir@20516 1291 flexarray_set(back, boffset++, "1");
keir@20516 1292 flexarray_set(back, boffset++, "state");
keir@20482 1293 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1294 flexarray_set(back, boffset++, "domain");
keir@20516 1295 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20482 1296 for (i = 0; i < num; i++) {
keir@20482 1297 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
keir@20482 1298 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1299 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
keir@20482 1300 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1301 if (pcidev->vdevfn) {
keir@20482 1302 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
keir@20482 1303 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1304 }
keir@20482 1305 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
keir@20482 1306 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1307 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
keir@20482 1308 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1309 }
keir@20516 1310 flexarray_set(back, boffset++, "num_devs");
keir@20482 1311 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
keir@20482 1312
keir@20516 1313 flexarray_set(front, foffset++, "backend-id");
keir@20482 1314 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
keir@20516 1315 flexarray_set(front, foffset++, "state");
keir@20482 1316 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1317
keir@20482 1318 libxl_device_generic_add(ctx, &device,
keir@20482 1319 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20482 1320 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20482 1321
keir@20482 1322 flexarray_free(back);
keir@20482 1323 flexarray_free(front);
keir@20482 1324 return 0;
keir@20482 1325 }
keir@20482 1326
keir@20482 1327 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1328 {
keir@20482 1329 flexarray_t *back;
keir@20482 1330 char *num_devs, *be_path;
keir@20482 1331 int num = 0;
keir@20482 1332 unsigned int boffset = 0;
keir@20482 1333 xs_transaction_t t;
keir@20482 1334
keir@20516 1335 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 1336 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 1337 if (!num_devs)
keir@20482 1338 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
keir@20482 1339
keir@20482 1340 if (!is_hvm(ctx, domid)) {
keir@20482 1341 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
keir@20482 1342 return -1;
keir@20482 1343 }
keir@20482 1344
keir@20482 1345 back = flexarray_make(16, 1);
keir@20482 1346 if (!back)
keir@20482 1347 return ERROR_NOMEM;
keir@20482 1348
keir@20512 1349 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
keir@20482 1350 num = atoi(num_devs);
keir@20482 1351 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
keir@20482 1352 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1353 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
keir@20482 1354 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1355 if (pcidev->vdevfn) {
keir@20482 1356 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
keir@20482 1357 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1358 }
keir@20482 1359 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
keir@20482 1360 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1361 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
keir@20482 1362 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1363 flexarray_set(back, boffset++, "num_devs");
keir@20482 1364 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
keir@20516 1365 flexarray_set(back, boffset++, "state");
keir@20482 1366 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
keir@20482 1367
keir@20482 1368 retry_transaction:
keir@20482 1369 t = xs_transaction_start(ctx->xsh);
keir@20482 1370 libxl_xs_writev(ctx, t, be_path,
keir@20482 1371 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
keir@20482 1372 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 1373 if (errno == EAGAIN)
keir@20482 1374 goto retry_transaction;
keir@20482 1375
keir@20482 1376 flexarray_free(back);
keir@20482 1377 return 0;
keir@20482 1378 }
keir@20482 1379
keir@20482 1380 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20482 1381 {
keir@20542 1382 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
keir@20542 1383 int num, i, j;
keir@20482 1384 xs_transaction_t t;
keir@20482 1385 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
keir@20482 1386
keir@20516 1387 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 1388 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
keir@20482 1389 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
keir@20482 1390 if (!num_devs)
keir@20482 1391 return -1;
keir@20482 1392 num = atoi(num_devs);
keir@20482 1393
keir@20482 1394 if (!is_hvm(ctx, domid)) {
keir@20482 1395 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 1396 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20482 1397 return -1;
keir@20482 1398 }
keir@20482 1399 }
keir@20482 1400
keir@20482 1401 for (i = 0; i < num; i++) {
keir@20482 1402 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 1403 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 1404 if (domain == pcidev->domain && bus == pcidev->bus &&
keir@20482 1405 pcidev->dev == dev && pcidev->func == func) {
keir@20482 1406 break;
keir@20482 1407 }
keir@20482 1408 }
keir@20482 1409 if (i == num) {
keir@20512 1410 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
keir@20482 1411 return -1;
keir@20482 1412 }
keir@20482 1413
keir@20482 1414 retry_transaction:
keir@20482 1415 t = xs_transaction_start(ctx->xsh);
keir@20542 1416 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
keir@20482 1417 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
keir@20482 1418 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 1419 if (errno == EAGAIN)
keir@20482 1420 goto retry_transaction;
keir@20542 1421
keir@20542 1422 if (!is_hvm(ctx, domid)) {
keir@20542 1423 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 1424 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20542 1425 return -1;
keir@20542 1426 }
keir@20542 1427 }
keir@20542 1428
keir@20542 1429 retry_transaction2:
keir@20542 1430 t = xs_transaction_start(ctx->xsh);
keir@20542 1431 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
keir@20542 1432 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
keir@20542 1433 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20542 1434 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
keir@20542 1435 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20542 1436 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20542 1437 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
keir@20542 1438 for (j = i + 1; j < num; j++) {
keir@20542 1439 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
keir@20542 1440 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1441 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1442 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1443 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
keir@20542 1444 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1445 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1446 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1447 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
keir@20542 1448 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1449 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1450 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1451 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
keir@20542 1452 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1453 if (tmp) {
keir@20542 1454 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1455 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1456 }
keir@20542 1457 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
keir@20542 1458 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1459 if (tmp) {
keir@20542 1460 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1461 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1462 }
keir@20542 1463 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
keir@20542 1464 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 1465 if (tmp) {
keir@20542 1466 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 1467 xs_rm(ctx->xsh, t, tmppath);
keir@20542 1468 }
keir@20542 1469 }
keir@20542 1470 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 1471 if (errno == EAGAIN)
keir@20542 1472 goto retry_transaction2;
keir@20542 1473
keir@20542 1474 if (num == 1) {
keir@20542 1475 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
keir@20542 1476 libxl_device_destroy(ctx, be_path, 1);
keir@20542 1477 xs_rm(ctx->xsh, XBT_NULL, be_path);
keir@20542 1478 xs_rm(ctx->xsh, XBT_NULL, fe_path);
keir@20542 1479 return 0;
keir@20542 1480 }
keir@20542 1481
keir@20482 1482 return 0;
keir@20462 1483 }
keir@20462 1484
keir@20482 1485 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1486 {
keir@20482 1487 char path[50];
keir@20482 1488 char *state, *vdevfn;
keir@20482 1489 int rc, hvm;
keir@20542 1490 int stubdomid = 0;
keir@20482 1491
keir@20482 1492 /* TODO: check if the device can be assigned */
keir@20482 1493
keir@20482 1494 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1495
keir@20542 1496 if ((stubdomid = libxl_get_stubdom_id(ctx, domid)) != 0) {
keir@20542 1497 libxl_device_pci pcidev_s = *pcidev;
keir@20542 1498 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
keir@20542 1499 }
keir@20542 1500
keir@20482 1501 hvm = is_hvm(ctx, domid);
keir@20482 1502 if (hvm) {
keir@20514 1503 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
keir@20482 1504 return -1;
keir@20482 1505 }
keir@20482 1506 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1507 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 1508 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 1509 if (pcidev->vdevfn)
keir@20482 1510 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
keir@20482 1511 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
keir@20482 1512 else
keir@20482 1513 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 1514 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1515 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
keir@20482 1516 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
keir@20514 1517 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", 0,0) < 0)
keir@20512 1518 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20482 1519 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 1520 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 1521 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
keir@20482 1522 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1523 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 1524 } else {
keir@20542 1525 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 1526 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1527 FILE *f = fopen(sysfs_path, "r");
keir@20482 1528 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 1529 int irq = 0;
keir@20482 1530 int i;
keir@20482 1531
keir@20482 1532 if (f == NULL) {
keir@20512 1533 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1534 return -1;
keir@20482 1535 }
keir@20482 1536 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20512 1537 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
keir@20482 1538 size = end - start + 1;
keir@20482 1539 if (start) {
keir@20482 1540 if (flags & PCI_BAR_IO) {
keir@20482 1541 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
keir@20482 1542 if (rc < 0)
keir@20512 1543 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 1544 } else {
keir@20482 1545 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 1546 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
keir@20482 1547 if (rc < 0)
keir@20512 1548 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 1549 }
keir@20482 1550 }
keir@20482 1551 }
keir@20482 1552 fclose(f);
keir@20542 1553 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 1554 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1555 f = fopen(sysfs_path, "r");
keir@20482 1556 if (f == NULL) {
keir@20512 1557 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1558 goto out;
keir@20482 1559 }
keir@20482 1560 fscanf(f, "%u", &irq);
keir@20482 1561 if (irq) {
keir@20482 1562 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
keir@20482 1563 if (rc < 0) {
keir@20512 1564 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
keir@20482 1565 }
keir@20482 1566 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
keir@20482 1567 if (rc < 0) {
keir@20512 1568 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
keir@20482 1569 }
keir@20482 1570 }
keir@20482 1571 fclose(f);
keir@20482 1572 }
keir@20482 1573 out:
keir@20542 1574 if (!libxl_is_stubdom(ctx, domid)) {
keir@20542 1575 if ((rc = xc_assign_device(ctx->xch, domid, pcidev->value)) < 0)
keir@20542 1576 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
keir@20542 1577 }
keir@20482 1578
keir@20482 1579 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
keir@20482 1580 return 0;
keir@20462 1581 }
keir@20462 1582
keir@20482 1583 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1584 {
keir@20482 1585 char path[50];
keir@20482 1586 char *state;
keir@20482 1587 int hvm, rc;
keir@20542 1588 int stubdomid = 0;
keir@20482 1589
keir@20482 1590 /* TODO: check if the device can be detached */
keir@20542 1591 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
keir@20482 1592
keir@20482 1593 hvm = is_hvm(ctx, domid);
keir@20482 1594 if (hvm) {
keir@20514 1595 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
keir@20482 1596 return -1;
keir@20482 1597 }
keir@20482 1598 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1599 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 1600 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 1601 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 1602 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1603 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
keir@20482 1604 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
keir@20514 1605 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", 0,0) < 0) {
keir@20512 1606 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20482 1607 return -1;
keir@20482 1608 }
keir@20482 1609 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1610 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 1611 } else {
keir@20542 1612 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 1613 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1614 FILE *f = fopen(sysfs_path, "r");
keir@20482 1615 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 1616 int irq = 0;
keir@20482 1617 int i;
keir@20482 1618
keir@20482 1619 if (f == NULL) {
keir@20512 1620 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1621 goto skip1;
keir@20482 1622 }
keir@20482 1623 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20482 1624 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
keir@20482 1625 size = end - start + 1;
keir@20482 1626 if (start) {
keir@20482 1627 if (flags & PCI_BAR_IO) {
keir@20482 1628 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
keir@20482 1629 if (rc < 0)
keir@20512 1630 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 1631 } else {
keir@20482 1632 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 1633 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
keir@20482 1634 if (rc < 0)
keir@20512 1635 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 1636 }
keir@20482 1637 }
keir@20482 1638 }
keir@20482 1639 fclose(f);
keir@20482 1640 skip1:
keir@20542 1641 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 1642 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1643 f = fopen(sysfs_path, "r");
keir@20482 1644 if (f == NULL) {
keir@20512 1645 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1646 goto out;
keir@20482 1647 }
keir@20482 1648 fscanf(f, "%u", &irq);
keir@20482 1649 if (irq) {
keir@20482 1650 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
keir@20482 1651 if (rc < 0) {
keir@20512 1652 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
keir@20482 1653 }
keir@20482 1654 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
keir@20482 1655 if (rc < 0) {
keir@20512 1656 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
keir@20482 1657 }
keir@20482 1658 }
keir@20482 1659 fclose(f);
keir@20482 1660 }
keir@20482 1661 out:
keir@20482 1662 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1663
keir@20542 1664 if (!libxl_is_stubdom(ctx, domid)) {
keir@20542 1665 if ((rc = xc_deassign_device(ctx->xch, domid, pcidev->value)) < 0)
keir@20542 1666 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
keir@20542 1667 }
keir@20542 1668
keir@20542 1669 if ((stubdomid = libxl_get_stubdom_id(ctx, domid)) != 0) {
keir@20542 1670 libxl_device_pci pcidev_s = *pcidev;
keir@20542 1671 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
keir@20542 1672 }
keir@20542 1673
keir@20482 1674 return 0;
keir@20462 1675 }
keir@20482 1676
keir@20482 1677 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20482 1678 {
keir@20482 1679 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
keir@20482 1680 int n, i;
keir@20482 1681 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
keir@20482 1682 libxl_device_pci *pcidevs;
keir@20482 1683
keir@20516 1684 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 1685 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 1686 if (!num_devs) {
keir@20482 1687 *num = 0;
keir@20482 1688 return NULL;
keir@20482 1689 }
keir@20482 1690 n = atoi(num_devs);
keir@20516 1691 pcidevs = (libxl_device_pci *) calloc(n, sizeof(libxl_device_pci));
keir@20482 1692 *num = n;
keir@20482 1693
keir@20482 1694 for (i = 0; i < n; i++) {
keir@20482 1695 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 1696 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 1697 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20482 1698 if (xsvdevfn)
keir@20482 1699 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
keir@20482 1700 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
keir@20482 1701 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20482 1702 if (xsopts) {
keir@20482 1703 char *saveptr;
keir@20482 1704 char *p = strtok_r(xsopts, ",=", &saveptr);
keir@20482 1705 do {
keir@20482 1706 while (*p == ' ')
keir@20482 1707 p++;
keir@20482 1708 if (!strcmp(p, "msitranslate")) {
keir@20482 1709 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 1710 pcidevs[i].msitranslate = atoi(p);
keir@20482 1711 } else if (!strcmp(p, "power_mgmt")) {
keir@20482 1712 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 1713 pcidevs[i].power_mgmt = atoi(p);
keir@20482 1714 }
keir@20482 1715 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
keir@20482 1716 }
keir@20482 1717 }
keir@20482 1718 return pcidevs;
keir@20482 1719 }
keir@20482 1720
keir@20482 1721 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20482 1722 {
keir@20482 1723 libxl_device_pci *pcidevs;
keir@20482 1724 int num, i;
keir@20482 1725
keir@20482 1726 pcidevs = libxl_device_pci_list(ctx, domid, &num);
keir@20482 1727 for (i = 0; i < num; i++) {
keir@20482 1728 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
keir@20482 1729 return -1;
keir@20482 1730 }
keir@20516 1731 free(pcidevs);
keir@20482 1732 return 0;
keir@20482 1733 }
keir@20482 1734
keir@20542 1735 void nic_info_domid_fixup(libxl_device_nic *nic_info, int domid)
keir@20542 1736 {
keir@20542 1737 nic_info->domid = domid;
keir@20542 1738 asprintf(&(nic_info->ifname), "tap%d.%d", domid, nic_info->devid - 1);
keir@20542 1739 }
keir@20542 1740
keir@20542 1741 void disk_info_domid_fixup(libxl_device_disk *disk_info, int domid)
keir@20542 1742 {
keir@20542 1743 disk_info->domid = domid;
keir@20542 1744 }
keir@20542 1745
keir@20542 1746 void vfb_info_domid_fixup(libxl_device_vfb *vfb, int domid)
keir@20542 1747 {
keir@20542 1748 vfb->domid = domid;
keir@20542 1749 }
keir@20542 1750
keir@20542 1751 void vkb_info_domid_fixup(libxl_device_vkb *vkb, int domid)
keir@20542 1752 {
keir@20542 1753 vkb->domid = domid;
keir@20542 1754 }
keir@20542 1755
keir@20542 1756 void console_info_domid_fixup(libxl_device_console *console, int domid)
keir@20542 1757 {
keir@20542 1758 console->domid = domid;
keir@20542 1759 }
keir@20542 1760
keir@20542 1761 void device_model_info_domid_fixup(libxl_device_model_info *dm_info, int domid)
keir@20542 1762 {
keir@20542 1763 dm_info->domid = domid;
keir@20542 1764 }
keir@20542 1765
keir@20542 1766 void init_create_info(libxl_domain_create_info *c_info)
keir@20542 1767 {
keir@20542 1768 memset(c_info, '\0', sizeof(*c_info));
keir@20542 1769 c_info->xsdata = NULL;
keir@20542 1770 c_info->platformdata = NULL;
keir@20542 1771 c_info->hvm = 1;
keir@20542 1772 c_info->ssidref = 0;
keir@20542 1773 }
keir@20542 1774
keir@20542 1775 void init_build_info(libxl_domain_build_info *b_info, libxl_domain_create_info *c_info)
keir@20542 1776 {
keir@20542 1777 memset(b_info, '\0', sizeof(*b_info));
keir@20542 1778 b_info->timer_mode = -1;
keir@20542 1779 b_info->hpet = 1;
keir@20542 1780 b_info->vpt_align = -1;
keir@20542 1781 b_info->max_vcpus = 1;
keir@20542 1782 b_info->max_memkb = 32 * 1024;
keir@20542 1783 if (c_info->hvm) {
keir@20542 1784 b_info->shadow_memkb = libxl_get_required_shadow_memory(b_info->max_memkb, b_info->max_vcpus);
keir@20542 1785 b_info->video_memkb = 8 * 1024;
keir@20542 1786 b_info->kernel = "/usr/lib/xen/boot/hvmloader";
keir@20542 1787 b_info->hvm = 1;
keir@20542 1788 b_info->u.hvm.pae = 1;
keir@20542 1789 b_info->u.hvm.apic = 1;
keir@20542 1790 b_info->u.hvm.acpi = 1;
keir@20542 1791 b_info->u.hvm.nx = 1;
keir@20542 1792 b_info->u.hvm.viridian = 0;
keir@20542 1793 }
keir@20542 1794 }
keir@20542 1795
keir@20542 1796 void init_dm_info(libxl_device_model_info *dm_info,
keir@20542 1797 libxl_domain_create_info *c_info, libxl_domain_build_info *b_info)
keir@20542 1798 {
keir@20542 1799 memset(dm_info, '\0', sizeof(*dm_info));
keir@20542 1800
keir@20542 1801 dm_info->dom_name = c_info->name;
keir@20542 1802 dm_info->device_model = "/usr/lib/xen/bin/qemu-dm";
keir@20542 1803 dm_info->videoram = b_info->video_memkb / 1024;
keir@20542 1804 dm_info->apic = b_info->u.hvm.apic;
keir@20542 1805
keir@20542 1806 dm_info->stdvga = 0;
keir@20542 1807 dm_info->vnc = 1;
keir@20542 1808 dm_info->vnclisten = "127.0.0.1";
keir@20542 1809 dm_info->vncdisplay = 0;
keir@20542 1810 dm_info->vncunused = 0;
keir@20542 1811 dm_info->keymap = NULL;
keir@20542 1812 dm_info->sdl = 0;
keir@20542 1813 dm_info->opengl = 0;
keir@20542 1814 dm_info->nographic = 0;
keir@20542 1815 dm_info->serial = NULL;
keir@20542 1816 dm_info->boot = "cda";
keir@20542 1817 dm_info->usb = 0;
keir@20542 1818 dm_info->usbdevice = NULL;
keir@20542 1819 }
keir@20542 1820
keir@20542 1821 void init_nic_info(libxl_device_nic *nic_info, int devnum)
keir@20542 1822 {
keir@20542 1823 memset(nic_info, '\0', sizeof(*nic_info));
keir@20542 1824
keir@20542 1825
keir@20542 1826 nic_info->backend_domid = 0;
keir@20542 1827 nic_info->domid = 0;
keir@20542 1828 nic_info->devid = devnum;
keir@20542 1829 nic_info->mtu = 1492;
keir@20542 1830 nic_info->model = "e1000";
keir@20542 1831 srand(time(0));
keir@20542 1832 nic_info->mac[0] = 0x00;
keir@20542 1833 nic_info->mac[1] = 0x16;
keir@20542 1834 nic_info->mac[2] = 0x3e;
keir@20542 1835 nic_info->mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
keir@20542 1836 nic_info->mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
keir@20542 1837 nic_info->mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
keir@20542 1838 asprintf(&(nic_info->smac), "%02x:%02x:%02x:%02x:%02x:%02x", nic_info->mac[0], nic_info->mac[1], nic_info->mac[2], nic_info->mac[3], nic_info->mac[4], nic_info->mac[5]);
keir@20542 1839 nic_info->ifname = NULL;
keir@20542 1840 nic_info->bridge = "xenbr0";
keir@20542 1841 nic_info->script = "/etc/xen/scripts/vif-bridge";
keir@20542 1842 nic_info->nictype = NICTYPE_IOEMU;
keir@20542 1843 }
keir@20542 1844
keir@20542 1845 void init_vfb_info(libxl_device_vfb *vfb, int dev_num)
keir@20542 1846 {
keir@20542 1847 memset(vfb, 0x00, sizeof(libxl_device_vfb));
keir@20542 1848 vfb->devid = dev_num;
keir@20542 1849 vfb->vnc = 1;
keir@20542 1850 vfb->vnclisten = "127.0.0.1";
keir@20542 1851 vfb->vncdisplay = 0;
keir@20542 1852 vfb->vncunused = 1;
keir@20542 1853 vfb->keymap = NULL;
keir@20542 1854 vfb->sdl = 0;
keir@20542 1855 vfb->opengl = 0;
keir@20542 1856 }
keir@20542 1857
keir@20542 1858 void init_vkb_info(libxl_device_vkb *vkb, int dev_num)
keir@20542 1859 {
keir@20542 1860 memset(vkb, 0x00, sizeof(libxl_device_vkb));
keir@20542 1861 vkb->devid = dev_num;
keir@20542 1862 }
keir@20542 1863
keir@20542 1864 void init_console_info(libxl_device_console *console, int dev_num, libxl_domain_build_state *state)
keir@20542 1865 {
keir@20542 1866 memset(console, 0x00, sizeof(libxl_device_console));
keir@20542 1867 console->devid = dev_num;
keir@20542 1868 console->constype = CONSTYPE_XENCONSOLED;
keir@20542 1869 if (state)
keir@20542 1870 console->build_state = state;
keir@20542 1871 }
keir@20542 1872
keir@20542 1873