debuggers.hg

annotate tools/libxl/libxl.c @ 20627:cabc1f9341e4

libxenlight: avoid writing empty values to xenstore

Prevent segmentation fault caused by empty values
in key-value pairs for the /vm/ subdirectory
when restoring a pv domain.

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