debuggers.hg

annotate tools/libxl/libxl.c @ 20516:a986c3c5b163

libxenlight: fix memory leaks

In particular:

- all the temporary flexarrays allocated in the create
device functions must be freed;

- all the strings that don't need to be modified can be added as they
are
to these temporary flexarrays instead of duplicating them;

- any data returned to the user shouldn't be added to the global
memory tracker so that the user can free it whenever he wishes.

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