debuggers.hg

annotate tools/libxl/libxl.c @ 20514:2f7cb671ef38

libxenlight: check for early failures of qemu-dm

This patch makes xl create check whether qemu-dm has started
correctly, and causes it to fail immediately with appropriate errors
if not. There are other bugfixes too.

More specifically:

* libxl_create_device_model forks twice rather than once so that the
process which calls libxl does not end up being the actual parent
of qemu. That avoids the need for the qemu-dm process to be reaped
at some indefinite time in the future.

* The first fork generates an intermediate process which is
responsible for writing the qemu-dm pid to xenstore and then merely
waits to collect and report on qemu-dm's exit status during
startup. New arguments to libxl_create_device_model allow the
preservation of its pid so that a later call can check whether the
startup is successful.

* The core of this functionality (the double fork, waitpid, signal
handling and so forth) is abstracted away into a new facility
libxl_spawn_... in libxl_exec.c.

Consequential changes:

* libxl_wait_for_device_model now takes a callback function parameter
which is called repeatedly in the loop iteration and allows the
caller to abort the wait.

* libxl_exec no longer calls fork; there is a new libxl_fork.

* There is a hook to override waitpid, which will be necessary for
some callers.

Remaining problems and other issues I noticed or we found:

* The error handling is rather inconsistent still and lacking in
places.

* destroy_device_model can kill random dom0 processes (!)

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Nov 23 07:01:51 2009 +0000 (2009-11-23)
parents 20063cea1606
children a986c3c5b163
rev   line source
keir@20462 1 /*
keir@20462 2 * Copyright (C) 2009 Citrix Ltd.
keir@20462 3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
keir@20462 4 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
keir@20462 5 *
keir@20462 6 * This program is free software; you can redistribute it and/or modify
keir@20462 7 * it under the terms of the GNU Lesser General Public License as published
keir@20462 8 * by the Free Software Foundation; version 2.1 only. with the special
keir@20462 9 * exception on linking described in file LICENSE.
keir@20462 10 *
keir@20462 11 * This program is distributed in the hope that it will be useful,
keir@20462 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
keir@20462 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
keir@20462 14 * GNU Lesser General Public License for more details.
keir@20462 15 */
keir@20462 16
keir@20513 17 #include "libxl_osdeps.h"
keir@20513 18
keir@20462 19 #include <stdio.h>
keir@20462 20 #include <string.h>
keir@20462 21 #include <stdlib.h>
keir@20462 22 #include <sys/stat.h>
keir@20462 23 #include <sys/types.h>
keir@20462 24 #include <fcntl.h>
keir@20462 25 #include <sys/select.h>
keir@20514 26 #include <sys/wait.h>
keir@20462 27 #include <signal.h>
keir@20468 28 #include <unistd.h> /* for write, unlink and close */
keir@20509 29 #include <stdint.h>
keir@20509 30 #include <inttypes.h>
keir@20514 31 #include <assert.h>
keir@20513 32
keir@20462 33 #include "libxl.h"
keir@20462 34 #include "libxl_utils.h"
keir@20462 35 #include "libxl_internal.h"
keir@20462 36 #include "flexarray.h"
keir@20462 37
keir@20462 38 int libxl_ctx_init(struct libxl_ctx *ctx)
keir@20462 39 {
keir@20462 40 memset(ctx, 0, sizeof(struct libxl_ctx));
keir@20462 41 ctx->alloc_maxsize = 256;
keir@20462 42 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
keir@20462 43 if (!ctx->alloc_ptrs)
keir@20462 44 return ERROR_NOMEM;
keir@20462 45
keir@20462 46 ctx->xch = xc_interface_open();
keir@20462 47 ctx->xsh = xs_daemon_open();
keir@20514 48
keir@20514 49 ctx->waitpid_instead= libxl_waitpid_instead_default;
keir@20462 50 return 0;
keir@20462 51 }
keir@20462 52
keir@20462 53 int libxl_ctx_free(struct libxl_ctx *ctx)
keir@20462 54 {
keir@20462 55 libxl_free_all(ctx);
keir@20462 56 free(ctx->alloc_ptrs);
keir@20462 57 ctx->alloc_ptrs = NULL;
keir@20462 58 xc_interface_close(ctx->xch);
keir@20462 59 xs_daemon_close(ctx->xsh);
keir@20462 60 return 0;
keir@20462 61 }
keir@20462 62
keir@20462 63 int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, void *log_data)
keir@20462 64 {
keir@20462 65 ctx->log_callback = log_callback;
keir@20462 66 ctx->log_userdata = log_data;
keir@20462 67 return 0;
keir@20462 68 }
keir@20462 69
keir@20462 70 /******************************************************************************/
keir@20462 71
keir@20462 72 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
keir@20462 73 uint32_t *domid)
keir@20462 74 {
keir@20462 75 int flags, ret, i;
keir@20468 76 char *uuid_string;
keir@20462 77 char *rw_paths[] = { "device" };
keir@20462 78 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
keir@20462 79 "control", "attr", "data", "messages" };
keir@20462 80 char *dom_path, *vm_path, *vss_path;
keir@20462 81 struct xs_permissions roperm[2];
keir@20462 82 struct xs_permissions rwperm[1];
keir@20462 83 xs_transaction_t t;
keir@20468 84 xen_domain_handle_t handle;
keir@20462 85
keir@20468 86 uuid_string = libxl_uuid_to_string(ctx, info->uuid);
keir@20462 87 if (!uuid_string) {
keir@20462 88 XL_LOG(ctx, XL_LOG_ERROR, "missing uuid");
keir@20462 89 return ERROR_FAIL;
keir@20462 90 }
keir@20462 91
keir@20462 92 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
keir@20462 93 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
keir@20462 94 *domid = 0;
keir@20462 95
keir@20468 96 /* XXX handle has to be initialised here.
keir@20468 97 * info->uuid != xen_domain_handle_t
keir@20468 98 * See:
keir@20468 99 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
keir@20468 100 * http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
keir@20468 101 *
keir@20468 102 * A DCE 1.1 compatible source representation of UUIDs.
keir@20468 103 *
keir@20468 104 * struct uuid {
keir@20468 105 * uint32_t time_low;
keir@20468 106 * uint16_t time_mid;
keir@20468 107 * uint16_t time_hi_and_version;
keir@20468 108 * uint8_t clock_seq_hi_and_reserved;
keir@20468 109 * uint8_t clock_seq_low;
keir@20468 110 * uint8_t node[_UUID_NODE_LEN];
keir@20468 111 * };
keir@20468 112 */
keir@20468 113
keir@20468 114 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
keir@20462 115 if (ret < 0) {
keir@20512 116 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
keir@20462 117 return ERROR_FAIL;
keir@20462 118 }
keir@20462 119
keir@20462 120 dom_path = libxl_xs_get_dompath(ctx, *domid);
keir@20512 121 if (!dom_path)
keir@20512 122 return ERROR_FAIL;
keir@20512 123
keir@20462 124 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
keir@20462 125 vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
keir@20512 126 if (!vm_path || !vss_path) {
keir@20462 127 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
keir@20462 128 return ERROR_FAIL;
keir@20462 129 }
keir@20462 130
keir@20462 131 roperm[0].id = 0;
keir@20462 132 roperm[0].perms = XS_PERM_NONE;
keir@20462 133 roperm[1].id = *domid;
keir@20462 134 roperm[1].perms = XS_PERM_READ;
keir@20462 135 rwperm[0].id = *domid;
keir@20462 136 rwperm[0].perms = XS_PERM_NONE;
keir@20462 137
keir@20462 138 retry_transaction:
keir@20462 139 t = xs_transaction_start(ctx->xsh);
keir@20462 140 xs_rm(ctx->xsh, t, dom_path);
keir@20462 141 xs_mkdir(ctx->xsh, t, dom_path);
keir@20462 142 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
keir@20462 143
keir@20462 144 xs_rm(ctx->xsh, t, vm_path);
keir@20462 145 xs_mkdir(ctx->xsh, t, vm_path);
keir@20462 146 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
keir@20462 147
keir@20462 148 xs_rm(ctx->xsh, t, vss_path);
keir@20462 149 xs_mkdir(ctx->xsh, t, vss_path);
keir@20462 150 xs_set_permissions(ctx->xsh, t, vss_path, rwperm, ARRAY_SIZE(rwperm));
keir@20462 151
keir@20462 152 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
keir@20462 153 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
keir@20462 154 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", dom_path), info->name, strlen(info->name));
keir@20462 155
keir@20462 156 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
keir@20462 157 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
keir@20462 158 xs_mkdir(ctx->xsh, t, path);
keir@20462 159 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
keir@20462 160 libxl_free(ctx, path);
keir@20462 161 }
keir@20462 162 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
keir@20462 163 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
keir@20462 164 xs_mkdir(ctx->xsh, t, path);
keir@20462 165 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20462 166 libxl_free(ctx, path);
keir@20462 167 }
keir@20462 168
keir@20462 169 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
keir@20462 170 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
keir@20462 171
keir@20462 172 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
keir@20462 173 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
keir@20462 174
keir@20462 175 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
keir@20462 176
keir@20462 177 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20462 178 if (errno == EAGAIN)
keir@20462 179 goto retry_transaction;
keir@20462 180 return 0;
keir@20462 181 }
keir@20462 182
keir@20509 183 libxl_domain_build_state *libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid)
keir@20462 184 {
keir@20509 185 libxl_domain_build_state *state = (libxl_domain_build_state *) libxl_calloc(ctx, 1, sizeof(libxl_domain_build_state));
keir@20462 186 char **vments = NULL, **localents = NULL;
keir@20462 187
keir@20509 188 build_pre(ctx, domid, info, state);
keir@20462 189 if (info->hvm) {
keir@20509 190 build_hvm(ctx, domid, info, state);
keir@20509 191 vments = libxl_calloc(ctx, 5, sizeof(char *));
keir@20462 192 vments[0] = libxl_sprintf(ctx, "rtc/timeoffset");
keir@20462 193 vments[1] = libxl_sprintf(ctx, "%s", (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "");
keir@20509 194 vments[2] = libxl_sprintf(ctx, "image/ostype");
keir@20509 195 vments[3] = libxl_sprintf(ctx, "hvm");
keir@20462 196 } else {
keir@20509 197 build_pv(ctx, domid, info, state);
keir@20509 198 vments = libxl_calloc(ctx, 9, sizeof(char *));
keir@20509 199 vments[0] = libxl_sprintf(ctx, "image/ostype");
keir@20509 200 vments[1] = libxl_sprintf(ctx, "linux");
keir@20509 201 vments[2] = libxl_sprintf(ctx, "image/kernel");
keir@20509 202 vments[3] = libxl_sprintf(ctx, info->kernel);
keir@20509 203 vments[4] = libxl_sprintf(ctx, "image/ramdisk");
keir@20509 204 vments[5] = libxl_sprintf(ctx, info->u.pv.ramdisk);
keir@20509 205 vments[6] = libxl_sprintf(ctx, "image/cmdline");
keir@20509 206 vments[7] = libxl_sprintf(ctx, info->u.pv.cmdline);
keir@20462 207 }
keir@20509 208 build_post(ctx, domid, info, state, vments, localents);
keir@20509 209 return state;
keir@20462 210 }
keir@20462 211
keir@20462 212 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
keir@20462 213 uint32_t domid, int fd)
keir@20462 214 {
keir@20462 215 libxl_domain_build_state state;
keir@20462 216 char **vments = NULL, **localents = NULL;
keir@20462 217
keir@20462 218 memset(&state, '\0', sizeof(state));
keir@20462 219
keir@20462 220 build_pre(ctx, domid, info, &state);
keir@20462 221 restore_common(ctx, domid, info, &state, fd);
keir@20462 222 if (info->hvm) {
keir@20462 223 vments = libxl_calloc(ctx, 4, sizeof(char *));
keir@20462 224 vments[0] = libxl_sprintf(ctx, "rtc/timeoffset");
keir@20462 225 vments[1] = libxl_sprintf(ctx, "%s", (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "");
keir@20462 226 } else {
keir@20462 227 localents = libxl_calloc(ctx, 4 * 2, sizeof(char *));
keir@20462 228 localents[0] = libxl_sprintf(ctx, "serial/0/limit");
keir@20462 229 localents[1] = libxl_sprintf(ctx, "%d", 65536);
keir@20462 230 localents[2] = libxl_sprintf(ctx, "console/port");
keir@20462 231 localents[3] = libxl_sprintf(ctx, "%d", state.console_port);
keir@20462 232 localents[4] = libxl_sprintf(ctx, "console/ring-ref");
keir@20462 233 localents[5] = libxl_sprintf(ctx, "%ld", state.console_mfn);
keir@20462 234 }
keir@20462 235 build_post(ctx, domid, info, &state, vments, localents);
keir@20462 236 return 0;
keir@20462 237 }
keir@20462 238
keir@20462 239 struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
keir@20462 240 {
keir@20462 241 struct libxl_dominfo *ptr;
keir@20462 242 int index, i, ret, first_domain;
keir@20462 243 xc_domaininfo_t info[16];
keir@20462 244 int size = 16;
keir@20462 245
keir@20462 246 first_domain = 1;
keir@20462 247 index = 0;
keir@20462 248 ptr = libxl_calloc(ctx, size, sizeof(struct libxl_dominfo));
keir@20462 249 if (!ptr)
keir@20462 250 return NULL;
keir@20462 251 redo:
keir@20462 252 ret = xc_domain_getinfolist(ctx->xch, first_domain, 16, info);
keir@20462 253 for (i = 0; i < ret; i++) {
keir@20462 254 if (index == size) {
keir@20462 255 struct libxl_dominfo *ptr2;
keir@20462 256
keir@20462 257 ptr2 = libxl_calloc(ctx, size * 2, sizeof(struct libxl_dominfo));
keir@20462 258 if (!ptr2) {
keir@20462 259 libxl_free(ctx, ptr);
keir@20462 260 return NULL;
keir@20462 261 }
keir@20462 262 memcpy(ptr2, ptr, sizeof(struct libxl_dominfo) * size);
keir@20462 263 libxl_free(ctx, ptr);
keir@20462 264 ptr = ptr2;
keir@20462 265 size *= 2;
keir@20462 266 }
keir@20462 267 memcpy(ptr[index].uuid, info[i].handle, 16 * sizeof(uint8_t));
keir@20462 268 ptr[index].domid = info[i].domain;
keir@20462 269 first_domain = info[i].domain + 1;
keir@20462 270 index++;
keir@20462 271 }
keir@20462 272 if (ret == 16)
keir@20462 273 goto redo;
keir@20462 274 *nb_domain = index;
keir@20462 275 return ptr;
keir@20462 276 }
keir@20462 277
keir@20462 278 xc_dominfo_t * libxl_domain_infolist(struct libxl_ctx *ctx, int *nb_domain)
keir@20462 279 {
keir@20462 280 int index, first_domain;
keir@20462 281 xc_dominfo_t *info;
keir@20462 282 int size = 1024;
keir@20462 283
keir@20462 284 first_domain = 0;
keir@20462 285 index = 0;
keir@20462 286 info = (xc_dominfo_t *) libxl_calloc(ctx, size, sizeof(xc_dominfo_t));
keir@20462 287 if (!info) {
keir@20462 288 *nb_domain = 0;
keir@20462 289 return NULL;
keir@20462 290 }
keir@20462 291 *nb_domain = xc_domain_getinfo(ctx->xch, first_domain, 1024, info);
keir@20462 292 return info;
keir@20462 293 }
keir@20462 294
keir@20462 295 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
keir@20462 296 uint32_t domid, int fd)
keir@20462 297 {
keir@20462 298 int hvm = 1;
keir@20462 299 int live = 0;
keir@20462 300 int debug = 0;
keir@20462 301 char savesig[] = "XenSavedDomain\n";
keir@20462 302
keir@20462 303 write(fd, savesig, strlen(savesig));
keir@20462 304
keir@20462 305 core_suspend(ctx, domid, fd, hvm, live, debug);
keir@20462 306
keir@20462 307 return 0;
keir@20462 308 }
keir@20462 309
keir@20462 310 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 311 {
keir@20462 312 xc_domain_pause(ctx->xch, domid);
keir@20462 313 return 0;
keir@20462 314 }
keir@20462 315
keir@20462 316 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 317 {
keir@20462 318 xc_domain_unpause(ctx->xch, domid);
keir@20462 319 return 0;
keir@20462 320 }
keir@20462 321
keir@20462 322 static char *req_table[] = {
keir@20462 323 [0] = "poweroff",
keir@20462 324 [1] = "reboot",
keir@20462 325 [2] = "suspend",
keir@20462 326 [3] = "crash",
keir@20462 327 [4] = "halt",
keir@20462 328 };
keir@20462 329
keir@20462 330 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
keir@20462 331 {
keir@20462 332 char *shutdown_path;
keir@20462 333 char *dom_path;
keir@20462 334
keir@20462 335 if (req > ARRAY_SIZE(req_table))
keir@20462 336 return ERROR_INVAL;
keir@20462 337
keir@20462 338 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 339 if (!dom_path)
keir@20512 340 return ERROR_FAIL;
keir@20512 341
keir@20462 342 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
keir@20462 343
keir@20462 344 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
keir@20462 345 if (/* hvm */ 0) {
keir@20462 346 unsigned long acpi_s_state = 0;
keir@20462 347 unsigned long pvdriver = 0;
keir@20462 348 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
keir@20462 349 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
keir@20462 350 if (!pvdriver && acpi_s_state != 0)
keir@20462 351 xc_domain_shutdown(ctx->xch, domid, req);
keir@20462 352 }
keir@20462 353 return 0;
keir@20462 354 }
keir@20462 355
keir@20462 356 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 357 {
keir@20462 358 char *pid;
keir@20462 359 int ret;
keir@20462 360
keir@20462 361 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
keir@20462 362 if (!pid) {
keir@20512 363 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
keir@20462 364 return -1;
keir@20462 365 }
keir@20462 366 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
keir@20462 367
keir@20462 368 ret = kill(atoi(pid), SIGHUP);
keir@20462 369 if (ret < 0 && errno == ESRCH) {
keir@20512 370 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
keir@20462 371 ret = 0;
keir@20462 372 } else if (ret == 0) {
keir@20512 373 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
keir@20462 374 ret = 0;
keir@20462 375 } else {
keir@20512 376 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
keir@20512 377 atoi(pid));
keir@20462 378 }
keir@20462 379 return ret;
keir@20462 380 }
keir@20462 381
keir@20462 382 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
keir@20462 383 {
keir@20462 384 char *dom_path, vm_path[41];
keir@20468 385 xen_uuid_t *uuid;
keir@20512 386 int rc;
keir@20462 387
keir@20462 388 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 389 if (!dom_path)
keir@20462 390 return -1;
keir@20512 391
keir@20462 392 if (libxl_domid_to_uuid(ctx, &uuid, domid) < 0) {
keir@20512 393 XL_LOG(ctx, XL_LOG_ERROR, "failed ot get uuid for %d", domid);
keir@20462 394 return -1;
keir@20462 395 }
keir@20482 396 if (libxl_device_pci_shutdown(ctx, domid) < 0)
keir@20512 397 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
keir@20462 398 xs_write(ctx->xsh, XBT_NULL,
keir@20462 399 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
keir@20462 400 "shutdown", strlen("shutdown"));
keir@20512 401 rc = xc_domain_pause(ctx->xch, domid);
keir@20512 402 if (rc < 0) {
keir@20512 403 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
keir@20462 404 return -1;
keir@20462 405 }
keir@20512 406 rc = xc_domain_destroy(ctx->xch, domid);
keir@20512 407 if (rc < 0) {
keir@20512 408 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
keir@20462 409 return -1;
keir@20462 410 }
keir@20462 411 if (libxl_devices_destroy(ctx, domid, force) < 0)
keir@20512 412 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
keir@20462 413 if (libxl_destroy_device_model(ctx, domid) < 0)
keir@20512 414 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
keir@20462 415 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
keir@20512 416 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
keir@20468 417 snprintf(vm_path, sizeof(vm_path), "/vm/%s", libxl_uuid_to_string(ctx, uuid));
keir@20462 418 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
keir@20512 419 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
keir@20462 420 return 0;
keir@20462 421 }
keir@20462 422
keir@20462 423 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
keir@20462 424 libxl_device_model_info *info,
keir@20462 425 libxl_device_nic *vifs,
keir@20462 426 int num_vifs)
keir@20462 427 {
keir@20462 428 int num = 0, i;
keir@20462 429 flexarray_t *dm_args;
keir@20462 430 dm_args = flexarray_make(16, 1);
keir@20462 431 if (!dm_args)
keir@20462 432 return NULL;
keir@20462 433
keir@20462 434 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "qemu-dm"));
keir@20462 435 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-d"));
keir@20462 436
keir@20462 437 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
keir@20462 438
keir@20462 439 if (info->dom_name) {
keir@20462 440 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-domain-name"));
keir@20462 441 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s", info->dom_name));
keir@20462 442 }
keir@20462 443 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
keir@20462 444 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-vnc"));
keir@20462 445 if (info->vncdisplay) {
keir@20462 446 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
keir@20462 447 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:%d", info->vnclisten, info->vncdisplay));
keir@20462 448 } else {
keir@20462 449 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
keir@20462 450 }
keir@20462 451 } else if (info->vnclisten) {
keir@20462 452 if (strchr(info->vnclisten, ':') != NULL) {
keir@20462 453 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s", info->vnclisten));
keir@20462 454 } else {
keir@20462 455 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
keir@20462 456 }
keir@20462 457 } else {
keir@20462 458 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:0"));
keir@20462 459 }
keir@20462 460 if (info->vncunused) {
keir@20462 461 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-vncunused"));
keir@20462 462 }
keir@20462 463 }
keir@20462 464 if (info->sdl || info->opengl) {
keir@20462 465 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-sdl"));
keir@20462 466 if (info->opengl) {
keir@20462 467 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-disable-opengl"));
keir@20462 468 }
keir@20462 469 }
keir@20462 470 if (info->keymap) {
keir@20462 471 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-k"));
keir@20462 472 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s", info->keymap));
keir@20462 473 }
keir@20462 474 if (info->nographic && (!info->sdl && !info->vnc)) {
keir@20462 475 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-nographic"));
keir@20462 476 }
keir@20462 477 if (info->serial) {
keir@20462 478 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-serial"));
keir@20462 479 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s", info->serial));
keir@20462 480 }
keir@20509 481 if (info->type == XENFV) {
keir@20509 482 if (info->videoram) {
keir@20509 483 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-videoram"));
keir@20509 484 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
keir@20509 485 }
keir@20509 486 if (info->stdvga) {
keir@20509 487 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-std-vga"));
keir@20509 488 }
keir@20509 489
keir@20509 490 if (info->boot) {
keir@20509 491 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-boot"));
keir@20509 492 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s", info->boot));
keir@20509 493 }
keir@20509 494 if (info->usb) {
keir@20509 495 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-usb"));
keir@20509 496 if (info->usbdevice) {
keir@20509 497 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-usbdevice"));
keir@20509 498 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s", info->usbdevice));
keir@20509 499 }
keir@20509 500 }
keir@20509 501 if (info->apic) {
keir@20509 502 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-acpi"));
keir@20509 503 }
keir@20509 504 for (i = 0; i < num_vifs; i++) {
keir@20509 505 if (vifs[i].nictype == NICTYPE_IOEMU) {
keir@20509 506 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-net"));
keir@20509 507 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
keir@20509 508 vifs[i].devid, vifs[i].smac, vifs[i].model));
keir@20509 509 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-net"));
keir@20509 510 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
keir@20509 511 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
keir@20509 512 }
keir@20462 513 }
keir@20462 514 }
keir@20509 515 for (i = 0; info->extra && info->extra[i] != NULL; i++)
keir@20509 516 flexarray_set(dm_args, num++, info->extra[i]);
keir@20462 517 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-M"));
keir@20509 518 if (info->type == XENPV)
keir@20509 519 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "xenpv"));
keir@20509 520 else
keir@20509 521 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "xenfv"));
keir@20462 522 flexarray_set(dm_args, num++, NULL);
keir@20462 523
keir@20462 524 return (char **) flexarray_contents(dm_args);
keir@20462 525 }
keir@20462 526
keir@20514 527 struct libxl_device_model_starting {
keir@20514 528 struct libxl_spawn_starting for_spawn; /* first! */
keir@20514 529 char *dom_path; /* from libxl_malloc, only for dm_xenstore_record_pid */
keir@20514 530 int domid;
keir@20514 531 };
keir@20514 532
keir@20514 533 void dm_xenstore_record_pid(struct libxl_ctx *ctx, void *for_spawn,
keir@20514 534 pid_t innerchild) {
keir@20514 535 struct libxl_device_model_starting *starting = for_spawn;
keir@20514 536 struct libxl_ctx clone;
keir@20514 537 char *kvs[3];
keir@20514 538 int rc;
keir@20514 539
keir@20514 540 clone = *ctx;
keir@20514 541 clone.xsh = xs_daemon_open();
keir@20514 542 /* we mustn't use the parent's handle in the child */
keir@20514 543
keir@20514 544 kvs[0] = "image/device-model-pid";
keir@20514 545 kvs[1] = libxl_sprintf(ctx, "%d", innerchild);
keir@20514 546 kvs[2] = NULL;
keir@20514 547 rc = libxl_xs_writev(ctx, XBT_NULL, starting->dom_path, kvs);
keir@20514 548 if (rc) XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@20514 549 "Couldn't record device model pid %ld at %s/%s",
keir@20514 550 (unsigned long)innerchild, starting->dom_path, kvs);
keir@20514 551 }
keir@20514 552
keir@20462 553 int libxl_create_device_model(struct libxl_ctx *ctx,
keir@20462 554 libxl_device_model_info *info,
keir@20514 555 libxl_device_nic *vifs, int num_vifs,
keir@20514 556 libxl_device_model_starting **starting_r)
keir@20462 557 {
keir@20514 558 char *path, *logfile, *logfile_new;
keir@20462 559 struct stat stat_buf;
keir@20514 560 int logfile_w, null;
keir@20514 561 int i, rc;
keir@20462 562 char **args;
keir@20514 563 struct libxl_spawn_starting buf_spawn, *for_spawn;
keir@20514 564
keir@20514 565 *starting_r= 0;
keir@20462 566
keir@20462 567 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20462 568 if (!args)
keir@20462 569 return ERROR_FAIL;
keir@20462 570
keir@20462 571 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
keir@20462 572 xs_mkdir(ctx->xsh, XBT_NULL, path);
keir@20462 573
keir@20462 574 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
keir@20462 575 if (stat(logfile, &stat_buf) == 0) {
keir@20462 576 /* file exists, rotate */
keir@20462 577 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.10", info->dom_name);
keir@20462 578 unlink(logfile);
keir@20462 579 for (i = 9; i > 0; i--) {
keir@20462 580 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i);
keir@20462 581 logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i + 1);
keir@20462 582 rename(logfile, logfile_new);
keir@20462 583 }
keir@20462 584 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
keir@20462 585 logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.1", info->dom_name);
keir@20462 586 rename(logfile, logfile_new);
keir@20462 587 }
keir@20462 588 logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name);
keir@20467 589 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
keir@20462 590 null = open("/dev/null", O_RDONLY);
keir@20514 591
keir@20514 592 if (starting_r) {
keir@20514 593 *starting_r= libxl_calloc(ctx, sizeof(**starting_r), 1);
keir@20514 594 if (!*starting_r) return ERROR_NOMEM;
keir@20514 595 (*starting_r)->domid= info->domid;
keir@20514 596
keir@20514 597 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20514 598 if (!(*starting_r)->dom_path) { free(*starting_r); return ERROR_FAIL; }
keir@20514 599
keir@20514 600 for_spawn= &(*starting_r)->for_spawn;
keir@20514 601 } else {
keir@20514 602 for_spawn= &buf_spawn;
keir@20514 603 }
keir@20514 604 rc = libxl_spawn_spawn(ctx, for_spawn, "device model",
keir@20514 605 dm_xenstore_record_pid);
keir@20514 606 if (rc < 0) goto xit;
keir@20514 607 if (!rc) { /* inner child */
keir@20514 608 libxl_exec(ctx, null, logfile_w, logfile_w,
keir@20514 609 info->device_model, args);
keir@20514 610 }
keir@20514 611
keir@20514 612 rc = 0;
keir@20514 613 xit:
keir@20462 614 close(null);
keir@20462 615 close(logfile_w);
keir@20462 616
keir@20514 617 return rc;
keir@20514 618 }
keir@20514 619
keir@20514 620 int libxl_detach_device_model(struct libxl_ctx *ctx,
keir@20514 621 libxl_device_model_starting *starting) {
keir@20514 622 int rc;
keir@20514 623 rc = libxl_spawn_detach(ctx, &starting->for_spawn);
keir@20514 624 libxl_free(ctx, starting);
keir@20514 625 return rc;
keir@20514 626 }
keir@20462 627
keir@20514 628
keir@20514 629 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
keir@20514 630 libxl_device_model_starting *starting) {
keir@20514 631 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
keir@20514 632 libxl_spawn_check,
keir@20514 633 &starting->for_spawn);
keir@20514 634 int detach = libxl_detach_device_model(ctx, starting);
keir@20514 635 return problem ? problem : detach;
keir@20462 636 }
keir@20462 637
keir@20514 638
keir@20462 639 /******************************************************************************/
keir@20462 640 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20462 641 {
keir@20462 642 flexarray_t *front;
keir@20462 643 flexarray_t *back;
keir@20462 644 char *backend_type;
keir@20462 645 unsigned int boffset = 0;
keir@20462 646 unsigned int foffset = 0;
keir@20462 647 int devid;
keir@20462 648 libxl_device device;
keir@20462 649
keir@20462 650 front = flexarray_make(16, 1);
keir@20462 651 if (!front)
keir@20462 652 return ERROR_NOMEM;
keir@20462 653 back = flexarray_make(16, 1);
keir@20462 654 if (!back) /* leaks front if error */
keir@20462 655 return ERROR_NOMEM;
keir@20462 656
keir@20462 657 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
keir@20462 658 devid = device_disk_dev_number(disk->virtpath);
keir@20462 659
keir@20462 660 device.backend_devid = devid;
keir@20462 661 device.backend_domid = disk->backend_domid;
keir@20462 662 device.devid = devid;
keir@20462 663 device.domid = disk->domid;
keir@20462 664 device.kind = DEVICE_VBD;
keir@20462 665
keir@20462 666 switch (disk->phystype) {
keir@20462 667 case PHYSTYPE_FILE:
keir@20462 668 return ERROR_NI; /* FIXME */
keir@20462 669 break;
keir@20462 670 case PHYSTYPE_PHY: {
keir@20462 671 int major, minor;
keir@20462 672
keir@20509 673 device_physdisk_major_minor(disk->physpath, &major, &minor);
keir@20462 674 flexarray_set(back, boffset++, libxl_sprintf(ctx, "physical-device"));
keir@20462 675 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20462 676
keir@20462 677 flexarray_set(back, boffset++, libxl_sprintf(ctx, "params"));
keir@20462 678 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", disk->physpath));
keir@20462 679
keir@20462 680 device.backend_kind = DEVICE_VBD;
keir@20462 681 break;
keir@20462 682 }
keir@20462 683 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
keir@20462 684 flexarray_set(back, boffset++, libxl_sprintf(ctx, "params"));
keir@20462 685 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
keir@20462 686 device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20462 687
keir@20462 688 device.backend_kind = DEVICE_TAP;
keir@20462 689 break;
keir@20462 690 }
keir@20462 691
keir@20462 692 flexarray_set(back, boffset++, libxl_sprintf(ctx, "frontend-id"));
keir@20462 693 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
keir@20462 694 flexarray_set(back, boffset++, libxl_sprintf(ctx, "online"));
keir@20462 695 flexarray_set(back, boffset++, libxl_sprintf(ctx, "1"));
keir@20462 696 flexarray_set(back, boffset++, libxl_sprintf(ctx, "removable"));
keir@20462 697 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
keir@20509 698 flexarray_set(back, boffset++, libxl_sprintf(ctx, "bootable"));
keir@20509 699 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20462 700 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state"));
keir@20462 701 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20462 702 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev"));
keir@20462 703 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", disk->virtpath));
keir@20462 704 flexarray_set(back, boffset++, libxl_sprintf(ctx, "type"));
keir@20462 705 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", backend_type));
keir@20462 706 flexarray_set(back, boffset++, libxl_sprintf(ctx, "mode"));
keir@20462 707 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", (disk->readwrite) ? "w" : "r"));
keir@20462 708
keir@20462 709 flexarray_set(front, foffset++, libxl_sprintf(ctx, "backend-id"));
keir@20462 710 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
keir@20462 711 flexarray_set(front, foffset++, libxl_sprintf(ctx, "state"));
keir@20462 712 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20462 713 flexarray_set(front, foffset++, libxl_sprintf(ctx, "virtual-device"));
keir@20462 714 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
keir@20462 715 flexarray_set(front, foffset++, libxl_sprintf(ctx, "device-type"));
keir@20462 716 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%s", (disk->is_cdrom) ? "cdrom" : "disk"));
keir@20462 717
keir@20462 718 if (0 /* protocol != native*/) {
keir@20462 719 flexarray_set(front, foffset++, libxl_sprintf(ctx, "protocol"));
keir@20462 720 flexarray_set(front, foffset++, libxl_sprintf(ctx, "x86_32-abi")); /* hardcoded ! */
keir@20462 721 }
keir@20462 722
keir@20462 723 libxl_device_generic_add(ctx, &device,
keir@20462 724 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 725 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20462 726 /* leaks both flexarray here */
keir@20462 727 return 0;
keir@20462 728 }
keir@20462 729
keir@20462 730 int libxl_device_disk_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 731 {
keir@20462 732 return ERROR_NI;
keir@20462 733 }
keir@20462 734
keir@20462 735 int libxl_device_disk_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 736 {
keir@20462 737 return ERROR_NI;
keir@20462 738 }
keir@20462 739
keir@20462 740 /******************************************************************************/
keir@20462 741 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
keir@20462 742 {
keir@20462 743 flexarray_t *front;
keir@20462 744 flexarray_t *back;
keir@20462 745 unsigned int boffset = 0;
keir@20462 746 unsigned int foffset = 0;
keir@20462 747 libxl_device device;
keir@20462 748
keir@20462 749 front = flexarray_make(16, 1);
keir@20462 750 if (!front)
keir@20462 751 return ERROR_NOMEM;
keir@20462 752 back = flexarray_make(16, 1);
keir@20462 753 if (!back)
keir@20462 754 return ERROR_NOMEM;
keir@20462 755
keir@20462 756 device.backend_devid = nic->devid;
keir@20462 757 device.backend_domid = nic->backend_domid;
keir@20462 758 device.backend_kind = DEVICE_VIF;
keir@20462 759 device.devid = nic->devid;
keir@20462 760 device.domid = nic->domid;
keir@20462 761 device.kind = DEVICE_VIF;
keir@20462 762
keir@20462 763 flexarray_set(back, boffset++, libxl_sprintf(ctx, "frontend-id"));
keir@20462 764 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
keir@20462 765 flexarray_set(back, boffset++, libxl_sprintf(ctx, "online"));
keir@20462 766 flexarray_set(back, boffset++, libxl_sprintf(ctx, "1"));
keir@20462 767 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state"));
keir@20462 768 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20462 769 flexarray_set(back, boffset++, libxl_sprintf(ctx, "script"));
keir@20462 770 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", nic->script));
keir@20462 771 flexarray_set(back, boffset++, libxl_sprintf(ctx, "mac"));
keir@20462 772 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 773 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 774 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20462 775 flexarray_set(back, boffset++, libxl_sprintf(ctx, "handle"));
keir@20462 776 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20462 777
keir@20462 778 flexarray_set(front, foffset++, libxl_sprintf(ctx, "backend-id"));
keir@20462 779 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
keir@20462 780 flexarray_set(front, foffset++, libxl_sprintf(ctx, "state"));
keir@20462 781 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20462 782 flexarray_set(front, foffset++, libxl_sprintf(ctx, "handle"));
keir@20462 783 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20462 784 flexarray_set(front, foffset++, libxl_sprintf(ctx, "mac"));
keir@20462 785 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 786 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 787 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20462 788 if (0 /* protocol != native*/) {
keir@20462 789 flexarray_set(front, foffset++, libxl_sprintf(ctx, "protocol"));
keir@20462 790 flexarray_set(front, foffset++, libxl_sprintf(ctx, "x86_32-abi")); /* hardcoded ! */
keir@20462 791 }
keir@20462 792
keir@20462 793 libxl_device_generic_add(ctx, &device,
keir@20462 794 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 795 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20462 796
keir@20462 797 /* FIXME: wait for plug */
keir@20462 798 return 0;
keir@20462 799 }
keir@20462 800
keir@20462 801 int libxl_device_nic_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 802 {
keir@20462 803 return ERROR_NI;
keir@20462 804 }
keir@20462 805
keir@20462 806 int libxl_device_nic_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 807 {
keir@20462 808 return ERROR_NI;
keir@20462 809 }
keir@20462 810
keir@20462 811 /******************************************************************************/
keir@20509 812 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
keir@20462 813 {
keir@20509 814 flexarray_t *front;
keir@20509 815 flexarray_t *back;
keir@20509 816 unsigned int boffset = 0;
keir@20509 817 unsigned int foffset = 0;
keir@20509 818 libxl_device device;
keir@20509 819
keir@20509 820 if (console->build_state) {
keir@20509 821 xs_transaction_t t;
keir@20509 822 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
keir@20509 823 ents[0] = libxl_sprintf(ctx, "console/port");
keir@20509 824 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
keir@20509 825 ents[2] = libxl_sprintf(ctx, "console/ring-ref");
keir@20509 826 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
keir@20509 827 ents[4] = libxl_sprintf(ctx, "console/limit");
keir@20509 828 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
keir@20509 829 ents[6] = libxl_sprintf(ctx, "console/type");
keir@20509 830 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20509 831 ents[7] = "xenconsoled";
keir@20509 832 else
keir@20509 833 ents[7] = "ioemu";
keir@20509 834 retry_transaction:
keir@20509 835 t = xs_transaction_start(ctx->xsh);
keir@20509 836 libxl_xs_writev(ctx, t, xs_get_domain_path(ctx->xsh, console->domid), ents);
keir@20509 837 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20509 838 if (errno == EAGAIN)
keir@20509 839 goto retry_transaction;
keir@20509 840 }
keir@20509 841
keir@20509 842 front = flexarray_make(16, 1);
keir@20509 843 if (!front)
keir@20509 844 return ERROR_NOMEM;
keir@20509 845 back = flexarray_make(16, 1);
keir@20509 846 if (!back)
keir@20509 847 return ERROR_NOMEM;
keir@20509 848
keir@20509 849 device.backend_devid = console->devid;
keir@20509 850 device.backend_domid = console->backend_domid;
keir@20509 851 device.backend_kind = DEVICE_CONSOLE;
keir@20509 852 device.devid = console->devid;
keir@20509 853 device.domid = console->domid;
keir@20509 854 device.kind = DEVICE_CONSOLE;
keir@20509 855
keir@20509 856 flexarray_set(back, boffset++, libxl_sprintf(ctx, "frontend-id"));
keir@20509 857 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
keir@20509 858 flexarray_set(back, boffset++, libxl_sprintf(ctx, "online"));
keir@20509 859 flexarray_set(back, boffset++, libxl_sprintf(ctx, "1"));
keir@20509 860 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state"));
keir@20509 861 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 862 flexarray_set(back, boffset++, libxl_sprintf(ctx, "domain"));
keir@20509 863 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", libxl_domid_to_name(ctx, domid)));
keir@20509 864 flexarray_set(back, boffset++, libxl_sprintf(ctx, "protocol"));
keir@20509 865 flexarray_set(back, boffset++, libxl_sprintf(ctx, LIBXL_XENCONSOLE_PROTOCOL));
keir@20509 866
keir@20509 867 flexarray_set(front, foffset++, libxl_sprintf(ctx, "backend-id"));
keir@20509 868 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
keir@20509 869 flexarray_set(front, foffset++, libxl_sprintf(ctx, "state"));
keir@20509 870 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 871 flexarray_set(front, foffset++, libxl_sprintf(ctx, "limit"));
keir@20509 872 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
keir@20509 873 flexarray_set(front, foffset++, libxl_sprintf(ctx, "protocol"));
keir@20509 874 flexarray_set(front, foffset++, libxl_sprintf(ctx, LIBXL_XENCONSOLE_PROTOCOL));
keir@20509 875 flexarray_set(front, foffset++, libxl_sprintf(ctx, "type"));
keir@20509 876 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20509 877 flexarray_set(front, foffset++, libxl_sprintf(ctx, "xenconsoled"));
keir@20509 878 else
keir@20509 879 flexarray_set(front, foffset++, libxl_sprintf(ctx, "ioemu"));
keir@20509 880
keir@20509 881 libxl_device_generic_add(ctx, &device,
keir@20509 882 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 883 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20509 884
keir@20509 885
keir@20509 886 return 0;
keir@20509 887 }
keir@20509 888
keir@20509 889 /******************************************************************************/
keir@20509 890 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
keir@20509 891 {
keir@20509 892 flexarray_t *front;
keir@20509 893 flexarray_t *back;
keir@20509 894 unsigned int boffset = 0;
keir@20509 895 unsigned int foffset = 0;
keir@20509 896 libxl_device device;
keir@20509 897
keir@20509 898 front = flexarray_make(16, 1);
keir@20509 899 if (!front)
keir@20509 900 return ERROR_NOMEM;
keir@20509 901 back = flexarray_make(16, 1);
keir@20509 902 if (!back)
keir@20509 903 return ERROR_NOMEM;
keir@20509 904
keir@20509 905 device.backend_devid = vkb->devid;
keir@20509 906 device.backend_domid = vkb->backend_domid;
keir@20509 907 device.backend_kind = DEVICE_VKBD;
keir@20509 908 device.devid = vkb->devid;
keir@20509 909 device.domid = vkb->domid;
keir@20509 910 device.kind = DEVICE_VKBD;
keir@20509 911
keir@20509 912 flexarray_set(back, boffset++, libxl_sprintf(ctx, "frontend-id"));
keir@20509 913 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
keir@20509 914 flexarray_set(back, boffset++, libxl_sprintf(ctx, "online"));
keir@20509 915 flexarray_set(back, boffset++, libxl_sprintf(ctx, "1"));
keir@20509 916 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state"));
keir@20509 917 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 918 flexarray_set(back, boffset++, libxl_sprintf(ctx, "domain"));
keir@20509 919 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", libxl_domid_to_name(ctx, domid)));
keir@20509 920
keir@20509 921 flexarray_set(front, foffset++, libxl_sprintf(ctx, "backend-id"));
keir@20509 922 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
keir@20509 923 flexarray_set(front, foffset++, libxl_sprintf(ctx, "state"));
keir@20509 924 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 925
keir@20509 926 libxl_device_generic_add(ctx, &device,
keir@20509 927 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 928 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20509 929
keir@20509 930 return 0;
keir@20462 931 }
keir@20462 932
keir@20462 933 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 934 {
keir@20462 935 return ERROR_NI;
keir@20462 936 }
keir@20462 937
keir@20462 938 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 939 {
keir@20462 940 return ERROR_NI;
keir@20462 941 }
keir@20462 942
keir@20462 943 /******************************************************************************/
keir@20509 944 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
keir@20509 945 libxl_device_vfb *vfb,
keir@20509 946 int num_console,
keir@20509 947 libxl_device_console *console,
keir@20509 948 libxl_device_model_info *info) {
keir@20509 949 int i = 0, j = 0, num = 0;
keir@20509 950 memset(info, 0x00, sizeof(libxl_device_model_info));
keir@20509 951
keir@20509 952 info->vnc = vfb->vnc;
keir@20509 953 if (vfb->vnclisten)
keir@20509 954 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
keir@20509 955 info->vncdisplay = vfb->vncdisplay;
keir@20509 956 info->vncunused = vfb->vncunused;
keir@20509 957 if (vfb->keymap)
keir@20509 958 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
keir@20509 959 info->sdl = vfb->sdl;
keir@20509 960 info->opengl = vfb->opengl;
keir@20509 961 for (i = 0; i < num_console; i++) {
keir@20509 962 if (console->constype == CONSTYPE_IOEMU)
keir@20509 963 num++;
keir@20509 964 }
keir@20509 965 if (num > 0) {
keir@20509 966 info->serial = "pty";
keir@20509 967 num--;
keir@20509 968 }
keir@20509 969 if (num > 0) {
keir@20509 970 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
keir@20509 971 for (j = 0; j < num * 2; j = j + 2) {
keir@20509 972 info->extra[j] = "-serial";
keir@20509 973 info->extra[j + 1] = "pty";
keir@20509 974 }
keir@20509 975 info->extra[j] = NULL;
keir@20509 976 }
keir@20509 977 info->domid = vfb->domid;
keir@20509 978 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
keir@20509 979 info->device_model = "/usr/lib/xen/bin/qemu-dm";
keir@20509 980 info->type = XENPV;
keir@20509 981 return 0;
keir@20509 982 }
keir@20509 983
keir@20509 984 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
keir@20514 985 int num_console, libxl_device_console *console,
keir@20514 986 struct libxl_device_model_starting **starting_r)
keir@20509 987 {
keir@20509 988 libxl_device_model_info info;
keir@20509 989
keir@20509 990 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
keir@20514 991 libxl_create_device_model(ctx, &info, NULL, 0, starting_r);
keir@20509 992 return 0;
keir@20509 993 }
keir@20509 994
keir@20509 995 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
keir@20462 996 {
keir@20509 997 flexarray_t *front;
keir@20509 998 flexarray_t *back;
keir@20509 999 unsigned int boffset = 0;
keir@20509 1000 unsigned int foffset = 0;
keir@20509 1001 libxl_device device;
keir@20509 1002
keir@20509 1003 front = flexarray_make(16, 1);
keir@20509 1004 if (!front)
keir@20509 1005 return ERROR_NOMEM;
keir@20509 1006 back = flexarray_make(16, 1);
keir@20509 1007 if (!back)
keir@20509 1008 return ERROR_NOMEM;
keir@20509 1009
keir@20509 1010 device.backend_devid = vfb->devid;
keir@20509 1011 device.backend_domid = vfb->backend_domid;
keir@20509 1012 device.backend_kind = DEVICE_VFB;
keir@20509 1013 device.devid = vfb->devid;
keir@20509 1014 device.domid = vfb->domid;
keir@20509 1015 device.kind = DEVICE_VFB;
keir@20509 1016
keir@20509 1017 flexarray_set(back, boffset++, libxl_sprintf(ctx, "frontend-id"));
keir@20509 1018 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
keir@20509 1019 flexarray_set(back, boffset++, libxl_sprintf(ctx, "online"));
keir@20509 1020 flexarray_set(back, boffset++, libxl_sprintf(ctx, "1"));
keir@20509 1021 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state"));
keir@20509 1022 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1023 flexarray_set(back, boffset++, libxl_sprintf(ctx, "domain"));
keir@20509 1024 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", libxl_domid_to_name(ctx, domid)));
keir@20509 1025 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vnc"));
keir@20509 1026 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
keir@20509 1027 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vnclisten"));
keir@20509 1028 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", vfb->vnclisten));
keir@20509 1029 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vncdisplay"));
keir@20509 1030 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
keir@20509 1031 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vncunused"));
keir@20509 1032 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
keir@20509 1033 flexarray_set(back, boffset++, libxl_sprintf(ctx, "sdl"));
keir@20509 1034 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
keir@20509 1035 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opengl"));
keir@20509 1036 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
keir@20509 1037 if (vfb->xauthority) {
keir@20509 1038 flexarray_set(back, boffset++, libxl_sprintf(ctx, "xauthority"));
keir@20509 1039 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", vfb->xauthority));
keir@20509 1040 }
keir@20509 1041 if (vfb->display) {
keir@20509 1042 flexarray_set(back, boffset++, libxl_sprintf(ctx, "display"));
keir@20509 1043 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", vfb->display));
keir@20509 1044 }
keir@20509 1045
keir@20509 1046 flexarray_set(front, foffset++, libxl_sprintf(ctx, "backend-id"));
keir@20509 1047 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
keir@20509 1048 flexarray_set(front, foffset++, libxl_sprintf(ctx, "state"));
keir@20509 1049 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1050
keir@20509 1051 libxl_device_generic_add(ctx, &device,
keir@20509 1052 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1053 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20509 1054 flexarray_free(front);
keir@20509 1055 flexarray_free(back);
keir@20509 1056
keir@20509 1057 return 0;
keir@20462 1058 }
keir@20462 1059
keir@20462 1060 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1061 {
keir@20462 1062 return ERROR_NI;
keir@20462 1063 }
keir@20462 1064
keir@20462 1065 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1066 {
keir@20462 1067 return ERROR_NI;
keir@20462 1068 }
keir@20462 1069
keir@20462 1070 /******************************************************************************/
keir@20482 1071
keir@20482 1072 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
keir@20482 1073 unsigned int bus, unsigned int dev,
keir@20482 1074 unsigned int func, unsigned int vdevfn)
keir@20482 1075 {
keir@20482 1076 pcidev->domain = domain;
keir@20482 1077 pcidev->bus = bus;
keir@20482 1078 pcidev->dev = dev;
keir@20482 1079 pcidev->func = func;
keir@20482 1080 pcidev->vdevfn = vdevfn;
keir@20482 1081 return 0;
keir@20482 1082 }
keir@20482 1083
keir@20482 1084 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
keir@20482 1085 {
keir@20482 1086 flexarray_t *front;
keir@20482 1087 flexarray_t *back;
keir@20482 1088 unsigned int boffset = 0;
keir@20482 1089 unsigned int foffset = 0;
keir@20482 1090 libxl_device device;
keir@20482 1091 int i;
keir@20482 1092
keir@20482 1093 front = flexarray_make(16, 1);
keir@20482 1094 if (!front)
keir@20482 1095 return ERROR_NOMEM;
keir@20482 1096 back = flexarray_make(16, 1);
keir@20482 1097 if (!back)
keir@20482 1098 return ERROR_NOMEM;
keir@20482 1099
keir@20512 1100 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
keir@20482 1101
keir@20482 1102 /* add pci device */
keir@20482 1103 device.backend_devid = 0;
keir@20482 1104 device.backend_domid = 0;
keir@20482 1105 device.backend_kind = DEVICE_PCI;
keir@20482 1106 device.devid = 0;
keir@20482 1107 device.domid = domid;
keir@20482 1108 device.kind = DEVICE_PCI;
keir@20482 1109
keir@20482 1110 flexarray_set(back, boffset++, libxl_sprintf(ctx, "frontend-id"));
keir@20482 1111 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
keir@20482 1112 flexarray_set(back, boffset++, libxl_sprintf(ctx, "online"));
keir@20482 1113 flexarray_set(back, boffset++, libxl_sprintf(ctx, "1"));
keir@20482 1114 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state"));
keir@20482 1115 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1116 flexarray_set(back, boffset++, libxl_sprintf(ctx, "domain"));
keir@20482 1117 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", libxl_domid_to_name(ctx, domid)));
keir@20482 1118 for (i = 0; i < num; i++) {
keir@20482 1119 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
keir@20482 1120 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1121 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
keir@20482 1122 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1123 if (pcidev->vdevfn) {
keir@20482 1124 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
keir@20482 1125 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1126 }
keir@20482 1127 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
keir@20482 1128 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1129 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
keir@20482 1130 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1131 }
keir@20482 1132 flexarray_set(back, boffset++, libxl_sprintf(ctx, "num_devs"));
keir@20482 1133 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
keir@20482 1134
keir@20482 1135 flexarray_set(front, foffset++, libxl_sprintf(ctx, "backend-id"));
keir@20482 1136 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
keir@20482 1137 flexarray_set(front, foffset++, libxl_sprintf(ctx, "state"));
keir@20482 1138 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1139
keir@20482 1140 libxl_device_generic_add(ctx, &device,
keir@20482 1141 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20482 1142 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20482 1143
keir@20482 1144 flexarray_free(back);
keir@20482 1145 flexarray_free(front);
keir@20482 1146 return 0;
keir@20482 1147 }
keir@20482 1148
keir@20482 1149 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1150 {
keir@20482 1151 flexarray_t *back;
keir@20482 1152 char *num_devs, *be_path;
keir@20482 1153 int num = 0;
keir@20482 1154 unsigned int boffset = 0;
keir@20482 1155 xs_transaction_t t;
keir@20482 1156
keir@20482 1157 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", xs_get_domain_path(ctx->xsh, 0), domid);
keir@20482 1158 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 1159 if (!num_devs)
keir@20482 1160 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
keir@20482 1161
keir@20482 1162 if (!is_hvm(ctx, domid)) {
keir@20482 1163 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
keir@20482 1164 return -1;
keir@20482 1165 }
keir@20482 1166
keir@20482 1167 back = flexarray_make(16, 1);
keir@20482 1168 if (!back)
keir@20482 1169 return ERROR_NOMEM;
keir@20482 1170
keir@20512 1171 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
keir@20482 1172 num = atoi(num_devs);
keir@20482 1173 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
keir@20482 1174 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1175 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
keir@20482 1176 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 1177 if (pcidev->vdevfn) {
keir@20482 1178 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
keir@20482 1179 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 1180 }
keir@20482 1181 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
keir@20482 1182 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 1183 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
keir@20482 1184 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 1185 flexarray_set(back, boffset++, libxl_sprintf(ctx, "num_devs"));
keir@20482 1186 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
keir@20482 1187 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state"));
keir@20482 1188 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
keir@20482 1189
keir@20482 1190 retry_transaction:
keir@20482 1191 t = xs_transaction_start(ctx->xsh);
keir@20482 1192 libxl_xs_writev(ctx, t, be_path,
keir@20482 1193 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
keir@20482 1194 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 1195 if (errno == EAGAIN)
keir@20482 1196 goto retry_transaction;
keir@20482 1197
keir@20482 1198 flexarray_free(back);
keir@20482 1199 return 0;
keir@20482 1200 }
keir@20482 1201
keir@20482 1202 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20482 1203 {
keir@20482 1204 char *be_path, *num_devs_path, *num_devs, *xsdev;
keir@20482 1205 int num, i;
keir@20482 1206 xs_transaction_t t;
keir@20482 1207 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
keir@20482 1208
keir@20482 1209 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", xs_get_domain_path(ctx->xsh, 0), domid);
keir@20482 1210 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
keir@20482 1211 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
keir@20482 1212 if (!num_devs)
keir@20482 1213 return -1;
keir@20482 1214 num = atoi(num_devs);
keir@20482 1215 if (num == 1) {
keir@20482 1216 libxl_device_destroy(ctx, be_path, 1);
keir@20482 1217 xs_rm(ctx->xsh, XBT_NULL, be_path);
keir@20482 1218 return 0;
keir@20482 1219 }
keir@20482 1220
keir@20482 1221 if (!is_hvm(ctx, domid)) {
keir@20482 1222 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20512 1223 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready");
keir@20482 1224 return -1;
keir@20482 1225 }
keir@20482 1226 }
keir@20482 1227
keir@20482 1228 for (i = 0; i < num; i++) {
keir@20482 1229 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 1230 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 1231 if (domain == pcidev->domain && bus == pcidev->bus &&
keir@20482 1232 pcidev->dev == dev && pcidev->func == func) {
keir@20482 1233 break;
keir@20482 1234 }
keir@20482 1235 }
keir@20482 1236 if (i == num) {
keir@20512 1237 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
keir@20482 1238 return -1;
keir@20482 1239 }
keir@20482 1240
keir@20482 1241 retry_transaction:
keir@20482 1242 t = xs_transaction_start(ctx->xsh);
keir@20482 1243 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
keir@20482 1244 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "6", strlen("6"));
keir@20482 1245 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
keir@20482 1246 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 1247 if (errno == EAGAIN)
keir@20482 1248 goto retry_transaction;
keir@20482 1249 return 0;
keir@20462 1250 }
keir@20462 1251
keir@20482 1252 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1253 {
keir@20482 1254 char path[50];
keir@20482 1255 char *state, *vdevfn;
keir@20482 1256 int rc, hvm;
keir@20482 1257
keir@20482 1258 /* TODO: check if the device can be assigned */
keir@20482 1259
keir@20482 1260 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1261
keir@20482 1262 hvm = is_hvm(ctx, domid);
keir@20482 1263 if (hvm) {
keir@20514 1264 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
keir@20482 1265 return -1;
keir@20482 1266 }
keir@20482 1267 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1268 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 1269 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 1270 if (pcidev->vdevfn)
keir@20482 1271 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
keir@20482 1272 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
keir@20482 1273 else
keir@20482 1274 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 1275 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1276 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
keir@20482 1277 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
keir@20514 1278 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", 0,0) < 0)
keir@20512 1279 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20482 1280 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 1281 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 1282 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
keir@20482 1283 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1284 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 1285 } else {
keir@20482 1286 char *sysfs_path = libxl_sprintf(ctx, "SYSFS_PCI_DEV/"PCI_BDF"/resource", pcidev->domain,
keir@20482 1287 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1288 FILE *f = fopen(sysfs_path, "r");
keir@20482 1289 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 1290 int irq = 0;
keir@20482 1291 int i;
keir@20482 1292
keir@20482 1293 if (f == NULL) {
keir@20512 1294 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1295 return -1;
keir@20482 1296 }
keir@20482 1297 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20512 1298 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
keir@20482 1299 size = end - start + 1;
keir@20482 1300 if (start) {
keir@20482 1301 if (flags & PCI_BAR_IO) {
keir@20482 1302 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
keir@20482 1303 if (rc < 0)
keir@20512 1304 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 1305 } else {
keir@20482 1306 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 1307 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
keir@20482 1308 if (rc < 0)
keir@20512 1309 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 1310 }
keir@20482 1311 }
keir@20482 1312 }
keir@20482 1313 fclose(f);
keir@20482 1314 sysfs_path = libxl_sprintf(ctx, "SYSFS_PCI_DEV/"PCI_BDF"/irq", pcidev->domain,
keir@20482 1315 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1316 f = fopen(sysfs_path, "r");
keir@20482 1317 if (f == NULL) {
keir@20512 1318 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1319 goto out;
keir@20482 1320 }
keir@20482 1321 fscanf(f, "%u", &irq);
keir@20482 1322 if (irq) {
keir@20482 1323 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
keir@20482 1324 if (rc < 0) {
keir@20512 1325 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
keir@20482 1326 }
keir@20482 1327 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
keir@20482 1328 if (rc < 0) {
keir@20512 1329 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
keir@20482 1330 }
keir@20482 1331 }
keir@20482 1332 fclose(f);
keir@20482 1333 }
keir@20482 1334 out:
keir@20482 1335 if ((rc = xc_assign_device(ctx->xch, domid, pcidev->value)) < 0)
keir@20512 1336 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
keir@20482 1337
keir@20482 1338 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
keir@20482 1339 return 0;
keir@20462 1340 }
keir@20462 1341
keir@20482 1342 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 1343 {
keir@20482 1344 char path[50];
keir@20482 1345 char *state;
keir@20482 1346 int hvm, rc;
keir@20482 1347
keir@20482 1348 /* TODO: check if the device can be detached */
keir@20482 1349
keir@20482 1350 hvm = is_hvm(ctx, domid);
keir@20482 1351 if (hvm) {
keir@20514 1352 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
keir@20482 1353 return -1;
keir@20482 1354 }
keir@20482 1355 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1356 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 1357 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 1358 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 1359 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1360 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
keir@20482 1361 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
keir@20514 1362 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", 0,0) < 0) {
keir@20512 1363 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20482 1364 return -1;
keir@20482 1365 }
keir@20482 1366 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
keir@20482 1367 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 1368 } else {
keir@20482 1369 char *sysfs_path = libxl_sprintf(ctx, "SYSFS_PCI_DEV/"PCI_BDF"/resource", pcidev->domain,
keir@20482 1370 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1371 FILE *f = fopen(sysfs_path, "r");
keir@20482 1372 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 1373 int irq = 0;
keir@20482 1374 int i;
keir@20482 1375
keir@20482 1376 if (f == NULL) {
keir@20512 1377 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1378 goto skip1;
keir@20482 1379 }
keir@20482 1380 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20482 1381 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
keir@20482 1382 size = end - start + 1;
keir@20482 1383 if (start) {
keir@20482 1384 if (flags & PCI_BAR_IO) {
keir@20482 1385 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
keir@20482 1386 if (rc < 0)
keir@20512 1387 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 1388 } else {
keir@20482 1389 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 1390 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
keir@20482 1391 if (rc < 0)
keir@20512 1392 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 1393 }
keir@20482 1394 }
keir@20482 1395 }
keir@20482 1396 fclose(f);
keir@20482 1397 skip1:
keir@20482 1398 sysfs_path = libxl_sprintf(ctx, "SYSFS_PCI_DEV/"PCI_BDF"/irq", pcidev->domain,
keir@20482 1399 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1400 f = fopen(sysfs_path, "r");
keir@20482 1401 if (f == NULL) {
keir@20512 1402 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 1403 goto out;
keir@20482 1404 }
keir@20482 1405 fscanf(f, "%u", &irq);
keir@20482 1406 if (irq) {
keir@20482 1407 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
keir@20482 1408 if (rc < 0) {
keir@20512 1409 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
keir@20482 1410 }
keir@20482 1411 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
keir@20482 1412 if (rc < 0) {
keir@20512 1413 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
keir@20482 1414 }
keir@20482 1415 }
keir@20482 1416 fclose(f);
keir@20482 1417 }
keir@20482 1418 out:
keir@20482 1419 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
keir@20482 1420
keir@20482 1421 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 1422
keir@20482 1423 if ((rc = xc_deassign_device(ctx->xch, domid, pcidev->value)) < 0)
keir@20512 1424 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
keir@20482 1425 return 0;
keir@20462 1426 }
keir@20482 1427
keir@20482 1428 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20482 1429 {
keir@20482 1430 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
keir@20482 1431 int n, i;
keir@20482 1432 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
keir@20482 1433 libxl_device_pci *pcidevs;
keir@20482 1434
keir@20482 1435 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", xs_get_domain_path(ctx->xsh, 0), domid);
keir@20482 1436 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 1437 if (!num_devs) {
keir@20482 1438 *num = 0;
keir@20482 1439 return NULL;
keir@20482 1440 }
keir@20482 1441 n = atoi(num_devs);
keir@20482 1442 pcidevs = (libxl_device_pci *) libxl_calloc(ctx, n, sizeof(libxl_device_pci));
keir@20482 1443 *num = n;
keir@20482 1444
keir@20482 1445 for (i = 0; i < n; i++) {
keir@20482 1446 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 1447 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 1448 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20482 1449 if (xsvdevfn)
keir@20482 1450 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
keir@20482 1451 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
keir@20482 1452 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20482 1453 if (xsopts) {
keir@20482 1454 char *saveptr;
keir@20482 1455 char *p = strtok_r(xsopts, ",=", &saveptr);
keir@20482 1456 do {
keir@20482 1457 while (*p == ' ')
keir@20482 1458 p++;
keir@20482 1459 if (!strcmp(p, "msitranslate")) {
keir@20482 1460 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 1461 pcidevs[i].msitranslate = atoi(p);
keir@20482 1462 } else if (!strcmp(p, "power_mgmt")) {
keir@20482 1463 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 1464 pcidevs[i].power_mgmt = atoi(p);
keir@20482 1465 }
keir@20482 1466 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
keir@20482 1467 }
keir@20482 1468 }
keir@20482 1469 return pcidevs;
keir@20482 1470 }
keir@20482 1471
keir@20482 1472 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20482 1473 {
keir@20482 1474 libxl_device_pci *pcidevs;
keir@20482 1475 int num, i;
keir@20482 1476
keir@20482 1477 pcidevs = libxl_device_pci_list(ctx, domid, &num);
keir@20482 1478 for (i = 0; i < num; i++) {
keir@20482 1479 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
keir@20482 1480 return -1;
keir@20482 1481 }
keir@20482 1482 return 0;
keir@20482 1483 }
keir@20482 1484