debuggers.hg

annotate tools/libxl/libxl.c @ 20781:3e4051dbfd34

libxl: remove structure init from the library and structure domid
fixup completly

structure init are more accurately done in the client of the library.

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