debuggers.hg

annotate tools/libxl/libxl.c @ 20564:6864bc03783f

libxenlight: add console command

This patch adds "xl console" command similar to "xm console".

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