debuggers.hg

annotate tools/libxl/libxl.c @ 20647:2c6a04fdf8fb

libxenlight: implement libxl_set_memory_target

This patch adds a target_memkb parameter to libxl_domain_build_info to
set the target memory for the VM at build time and a new function
called libxl_set_memory_target to dynamically modify the memory target
of a VM at run time. Finally a new command "mem-set" is added to xl
that calls directly libxl_set_memory_target.

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