debuggers.hg

annotate tools/libxl/libxl.c @ 21438:f74229f8bfa2

libxl: Add missing start_time entry when create and restore VMs.

Signed-off-by: Yang Hongyang<yanghy@cn.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Sat May 15 08:27:22 2010 +0100 (2010-05-15)
parents b21b9dd9d1e3
children eb4ecc037b7a
rev   line source
keir@20462 1 /*
keir@20462 2 * Copyright (C) 2009 Citrix Ltd.
keir@20462 3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
keir@20462 4 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
keir@20462 5 *
keir@20462 6 * This program is free software; you can redistribute it and/or modify
keir@20462 7 * it under the terms of the GNU Lesser General Public License as published
keir@20462 8 * by the Free Software Foundation; version 2.1 only. with the special
keir@20462 9 * exception on linking described in file LICENSE.
keir@20462 10 *
keir@20462 11 * This program is distributed in the hope that it will be useful,
keir@20462 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
keir@20462 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
keir@20462 14 * GNU Lesser General Public License for more details.
keir@20462 15 */
keir@20462 16
keir@20513 17 #include "libxl_osdeps.h"
keir@20513 18
keir@20462 19 #include <stdio.h>
keir@20462 20 #include <string.h>
keir@20462 21 #include <stdlib.h>
keir@20462 22 #include <sys/types.h>
keir@20462 23 #include <fcntl.h>
keir@20462 24 #include <sys/select.h>
keir@20514 25 #include <sys/wait.h>
keir@20462 26 #include <signal.h>
keir@20468 27 #include <unistd.h> /* for write, unlink and close */
keir@20509 28 #include <stdint.h>
keir@20509 29 #include <inttypes.h>
keir@20514 30 #include <assert.h>
keir@20513 31
keir@20462 32 #include "libxl.h"
keir@20462 33 #include "libxl_utils.h"
keir@20462 34 #include "libxl_internal.h"
keir@20462 35 #include "flexarray.h"
keir@20462 36
keir@20860 37 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
keir@20860 38
keir@20788 39 int libxl_ctx_init(struct libxl_ctx *ctx, int version)
keir@20462 40 {
keir@20788 41 if (version != LIBXL_VERSION)
keir@20788 42 return ERROR_VERSION;
keir@20462 43 memset(ctx, 0, sizeof(struct libxl_ctx));
keir@20462 44 ctx->alloc_maxsize = 256;
keir@20462 45 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
keir@20462 46 if (!ctx->alloc_ptrs)
keir@20462 47 return ERROR_NOMEM;
keir@21266 48 memset(&ctx->version_info, 0, sizeof(libxl_version_info));
keir@20462 49
keir@20462 50 ctx->xch = xc_interface_open();
keir@20819 51 if (ctx->xch == -1) {
keir@20819 52 free(ctx->alloc_ptrs);
keir@20819 53 return ERROR_FAIL;
keir@20819 54 }
keir@20819 55
keir@20462 56 ctx->xsh = xs_daemon_open();
keir@20819 57 if (!ctx->xsh) {
keir@20819 58 xc_interface_close(ctx->xch);
keir@20819 59 free(ctx->alloc_ptrs);
keir@20819 60 return ERROR_FAIL;
keir@20819 61 }
keir@20462 62 return 0;
keir@20462 63 }
keir@20462 64
keir@20462 65 int libxl_ctx_free(struct libxl_ctx *ctx)
keir@20462 66 {
keir@20462 67 libxl_free_all(ctx);
keir@20462 68 free(ctx->alloc_ptrs);
keir@20462 69 xc_interface_close(ctx->xch);
keir@21186 70 if (ctx->xsh) xs_daemon_close(ctx->xsh);
keir@20462 71 return 0;
keir@20462 72 }
keir@20462 73
keir@20462 74 int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, void *log_data)
keir@20462 75 {
keir@20462 76 ctx->log_callback = log_callback;
keir@20462 77 ctx->log_userdata = log_data;
keir@20462 78 return 0;
keir@20462 79 }
keir@20462 80
keir@20462 81 /******************************************************************************/
keir@20462 82
keir@20462 83 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
keir@20462 84 uint32_t *domid)
keir@20462 85 {
keir@21184 86 int flags, ret, i, rc;
keir@20468 87 char *uuid_string;
keir@20646 88 char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
keir@20462 89 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
keir@20646 90 "control", "attr", "messages" };
keir@20462 91 char *dom_path, *vm_path, *vss_path;
keir@20462 92 struct xs_permissions roperm[2];
keir@20462 93 struct xs_permissions rwperm[1];
keir@20462 94 xs_transaction_t t;
keir@20468 95 xen_domain_handle_t handle;
keir@20462 96
keir@21188 97 uuid_string = libxl_uuid2string(ctx, info->uuid);
keir@21188 98 if (!uuid_string) return ERROR_NOMEM;
keir@20462 99
keir@20462 100 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
keir@20462 101 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
keir@21274 102 flags |= info->oos ? 0 : XEN_DOMCTL_CDF_oos_off;
keir@20824 103 *domid = -1;
keir@20462 104
keir@20580 105 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
keir@20580 106 memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
keir@20468 107
keir@20468 108 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
keir@20462 109 if (ret < 0) {
keir@20512 110 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
keir@20462 111 return ERROR_FAIL;
keir@20462 112 }
keir@20462 113
keir@21368 114 ret = xc_cpupool_movedomain(ctx->xch, info->poolid, *domid);
keir@21368 115 if (ret < 0) {
keir@21368 116 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain move fail");
keir@21368 117 return ERROR_FAIL;
keir@21368 118 }
keir@21368 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@21184 154 rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
keir@21184 155 if (rc) return rc;
keir@20462 156
keir@20462 157 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
keir@20462 158 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
keir@20462 159 xs_mkdir(ctx->xsh, t, path);
keir@20462 160 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
keir@20462 161 libxl_free(ctx, path);
keir@20462 162 }
keir@20462 163 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
keir@20462 164 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
keir@20462 165 xs_mkdir(ctx->xsh, t, path);
keir@20462 166 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20462 167 libxl_free(ctx, path);
keir@20462 168 }
keir@20462 169
keir@20462 170 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
keir@20462 171 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
keir@21389 172 if (info->poolname)
keir@21389 173 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/pool_name", vm_path), info->poolname, strlen(info->poolname));
keir@20462 174
keir@20462 175 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
keir@20462 176 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
keir@20462 177
keir@20462 178 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
keir@20462 179
keir@20462 180 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20462 181 if (errno == EAGAIN)
keir@20462 182 goto retry_transaction;
keir@20462 183 return 0;
keir@20462 184 }
keir@20462 185
keir@21184 186 int libxl_domain_rename(struct libxl_ctx *ctx, uint32_t domid,
keir@21184 187 const char *old_name, const char *new_name,
keir@21184 188 xs_transaction_t trans) {
keir@21184 189 char *dom_path = 0;
keir@21184 190 const char *name_path;
keir@21184 191 char *got_old_name;
keir@21184 192 unsigned int got_old_len;
keir@21184 193 xs_transaction_t our_trans = 0;
keir@21184 194 int rc;
keir@21184 195
keir@21184 196 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@21184 197 if (!dom_path) goto x_nomem;
keir@21184 198
keir@21184 199 name_path= libxl_sprintf(ctx, "%s/name", dom_path);
keir@21184 200 if (!name_path) goto x_nomem;
keir@21184 201
keir@21184 202 retry_transaction:
keir@21184 203 if (!trans) {
keir@21184 204 trans = our_trans = xs_transaction_start(ctx->xsh);
keir@21184 205 if (!our_trans) {
keir@21184 206 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno,
keir@21184 207 "create xs transaction for domain (re)name");
keir@21184 208 goto x_fail;
keir@21184 209 }
keir@21184 210 }
keir@21184 211
keir@21184 212 if (old_name) {
keir@21184 213 got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len);
keir@21184 214 if (!got_old_name) {
keir@21184 215 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno, "check old name"
keir@21184 216 " for domain %"PRIu32" allegedly named `%s'",
keir@21184 217 domid, old_name);
keir@21184 218 goto x_fail;
keir@21184 219 }
keir@21184 220 if (strcmp(old_name, got_old_name)) {
keir@21184 221 XL_LOG(ctx, XL_LOG_ERROR, "domain %"PRIu32" allegedly named "
keir@21184 222 "`%s' is actually named `%s' - racing ?",
keir@21184 223 domid, old_name, got_old_name);
keir@21184 224 free(got_old_name);
keir@21184 225 goto x_fail;
keir@21184 226 }
keir@21184 227 free(got_old_name);
keir@21184 228 }
keir@21184 229 if (!xs_write(ctx->xsh, trans, name_path,
keir@21184 230 new_name, strlen(new_name))) {
keir@21184 231 XL_LOG(ctx, XL_LOG_ERROR, "failed to write new name `%s'"
keir@21184 232 " for domain %"PRIu32" previously named `%s'",
keir@21184 233 domid, new_name, old_name);
keir@21184 234 goto x_fail;
keir@21184 235 }
keir@21184 236
keir@21184 237 if (our_trans) {
keir@21184 238 if (!xs_transaction_end(ctx->xsh, our_trans, 0)) {
keir@21184 239 trans = our_trans = 0;
keir@21184 240 if (errno != EAGAIN) {
keir@21184 241 XL_LOG(ctx, XL_LOG_ERROR, "failed to commit new name `%s'"
keir@21184 242 " for domain %"PRIu32" previously named `%s'",
keir@21184 243 domid, new_name, old_name);
keir@21184 244 goto x_fail;
keir@21184 245 }
keir@21184 246 XL_LOG(ctx, XL_LOG_DEBUG, "need to retry rename transaction"
keir@21184 247 " for domain %"PRIu32" (name_path=\"%s\", new_name=\"%s\")",
keir@21184 248 domid, name_path, new_name);
keir@21184 249 goto retry_transaction;
keir@21184 250 }
keir@21184 251 our_trans = 0;
keir@21184 252 }
keir@21184 253
keir@21184 254 rc = 0;
keir@21184 255 x_rc:
keir@21184 256 if (dom_path) libxl_free(ctx, dom_path);
keir@21184 257 if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1);
keir@21184 258 return rc;
keir@21184 259
keir@21184 260 x_fail: rc = ERROR_FAIL; goto x_rc;
keir@21184 261 x_nomem: rc = ERROR_NOMEM; goto x_rc;
keir@21184 262 }
keir@21184 263
keir@20516 264 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
keir@20462 265 {
keir@20462 266 char **vments = NULL, **localents = NULL;
keir@21438 267 struct timeval start_time;
keir@20829 268 int i, ret;
keir@20829 269
keir@20829 270 ret = build_pre(ctx, domid, info, state);
keir@20829 271 if (ret) goto out;
keir@20462 272
keir@21438 273 gettimeofday(&start_time, NULL);
keir@21438 274
keir@20462 275 if (info->hvm) {
keir@20829 276 ret = build_hvm(ctx, domid, info, state);
keir@20829 277 if (ret) goto out;
keir@20829 278
keir@21438 279 vments = libxl_calloc(ctx, 7, sizeof(char *));
keir@20516 280 vments[0] = "rtc/timeoffset";
keir@20516 281 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
keir@20516 282 vments[2] = "image/ostype";
keir@20516 283 vments[3] = "hvm";
keir@21438 284 vments[4] = "start_time";
keir@21438 285 vments[5] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
keir@20462 286 } else {
keir@20829 287 ret = build_pv(ctx, domid, info, state);
keir@20829 288 if (ret) goto out;
keir@20829 289
keir@21438 290 vments = libxl_calloc(ctx, 11, sizeof(char *));
keir@20578 291 i = 0;
keir@20578 292 vments[i++] = "image/ostype";
keir@20578 293 vments[i++] = "linux";
keir@20578 294 vments[i++] = "image/kernel";
keir@20578 295 vments[i++] = (char*) info->kernel;
keir@21438 296 vments[i++] = "start_time";
keir@21438 297 vments[i++] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
keir@20578 298 if (info->u.pv.ramdisk) {
keir@20578 299 vments[i++] = "image/ramdisk";
keir@20578 300 vments[i++] = (char*) info->u.pv.ramdisk;
keir@20578 301 }
keir@20578 302 if (info->u.pv.cmdline) {
keir@20578 303 vments[i++] = "image/cmdline";
keir@20578 304 vments[i++] = (char*) info->u.pv.cmdline;
keir@20578 305 }
keir@20462 306 }
keir@20829 307 ret = build_post(ctx, domid, info, state, vments, localents);
keir@20829 308 out:
keir@20829 309 return ret;
keir@20462 310 }
keir@20462 311
keir@20462 312 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
keir@20565 313 uint32_t domid, int fd, libxl_domain_build_state *state,
keir@20565 314 libxl_device_model_info *dm_info)
keir@20462 315 {
keir@20462 316 char **vments = NULL, **localents = NULL;
keir@21438 317 struct timeval start_time;
keir@21183 318 int i, ret, esave, flags;
keir@20462 319
keir@20829 320 ret = build_pre(ctx, domid, info, state);
keir@20829 321 if (ret) goto out;
keir@20829 322
keir@20829 323 ret = restore_common(ctx, domid, info, state, fd);
keir@20829 324 if (ret) goto out;
keir@20829 325
keir@21438 326 gettimeofday(&start_time, NULL);
keir@21438 327
keir@20462 328 if (info->hvm) {
keir@21438 329 vments = libxl_calloc(ctx, 7, sizeof(char *));
keir@20516 330 vments[0] = "rtc/timeoffset";
keir@20516 331 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
keir@20565 332 vments[2] = "image/ostype";
keir@20565 333 vments[3] = "hvm";
keir@21438 334 vments[4] = "start_time";
keir@21438 335 vments[5] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
keir@20462 336 } else {
keir@21438 337 vments = libxl_calloc(ctx, 11, sizeof(char *));
keir@20627 338 i = 0;
keir@20627 339 vments[i++] = "image/ostype";
keir@20627 340 vments[i++] = "linux";
keir@20627 341 vments[i++] = "image/kernel";
keir@20627 342 vments[i++] = (char*) info->kernel;
keir@21438 343 vments[i++] = "start_time";
keir@21438 344 vments[i++] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
keir@20627 345 if (info->u.pv.ramdisk) {
keir@20627 346 vments[i++] = "image/ramdisk";
keir@20627 347 vments[i++] = (char*) info->u.pv.ramdisk;
keir@20627 348 }
keir@20627 349 if (info->u.pv.cmdline) {
keir@20627 350 vments[i++] = "image/cmdline";
keir@20627 351 vments[i++] = (char*) info->u.pv.cmdline;
keir@20627 352 }
keir@20462 353 }
keir@20829 354 ret = build_post(ctx, domid, info, state, vments, localents);
keir@20829 355 if (ret) goto out;
keir@20829 356
keir@21417 357 dm_info->saved_state = NULL;
keir@21417 358 if (info->hvm) {
keir@21417 359 ret = asprintf(&dm_info->saved_state,
keir@21417 360 "/var/lib/xen/qemu-save.%d", domid);
keir@21417 361 ret = (ret < 0) ? ERROR_FAIL : 0;
keir@21417 362 }
keir@21417 363
keir@20829 364 out:
keir@21183 365 esave = errno;
keir@21183 366
keir@21183 367 flags = fcntl(fd, F_GETFL);
keir@21183 368 if (flags == -1) {
keir@21183 369 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to get flags on restore fd");
keir@21183 370 } else {
keir@21183 371 flags &= ~O_NONBLOCK;
keir@21183 372 if (fcntl(fd, F_SETFL, flags) == -1)
keir@21183 373 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to put restore fd"
keir@21183 374 " back to blocking mode");
keir@21183 375 }
keir@21183 376
keir@21183 377 errno = esave;
keir@20829 378 return ret;
keir@20462 379 }
keir@20462 380
keir@20629 381 int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
keir@20629 382 {
keir@20629 383 if (is_hvm(ctx, domid)) {
keir@20629 384 XL_LOG(ctx, XL_LOG_DEBUG, "Called domain_resume on "
keir@20629 385 "non-cooperative hvm domain %u", domid);
keir@20629 386 return ERROR_NI;
keir@20629 387 }
keir@20629 388 if (xc_domain_resume(ctx->xch, domid, 1)) {
keir@20629 389 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@20629 390 "xc_domain_resume failed for domain %u",
keir@20629 391 domid);
keir@20629 392 return ERROR_FAIL;
keir@20629 393 }
keir@20629 394 if (!xs_resume_domain(ctx->xsh, domid)) {
keir@20629 395 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@20629 396 "xs_resume_domain failed for domain %u",
keir@20629 397 domid);
keir@20629 398 return ERROR_FAIL;
keir@20629 399 }
keir@20629 400 return 0;
keir@20629 401 }
keir@20629 402
keir@21189 403 static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
keir@21189 404 struct libxl_dominfo *xlinfo) {
keir@21189 405 memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
keir@21189 406 xlinfo->domid = xcinfo->domain;
keir@21189 407
keir@21189 408 if (xcinfo->flags & XEN_DOMINF_dying)
keir@21189 409 xlinfo->dying = 1;
keir@21189 410 else if (xcinfo->flags & XEN_DOMINF_paused)
keir@21189 411 xlinfo->paused = 1;
keir@21189 412 else if (xcinfo->flags & XEN_DOMINF_blocked ||
keir@21189 413 xcinfo->flags & XEN_DOMINF_running)
keir@21189 414 xlinfo->running = 1;
keir@21189 415 xlinfo->max_memkb = PAGE_TO_MEMKB(xcinfo->tot_pages);
keir@21189 416 xlinfo->cpu_time = xcinfo->cpu_time;
keir@21189 417 xlinfo->vcpu_max_id = xcinfo->max_vcpu_id;
keir@21189 418 xlinfo->vcpu_online = xcinfo->nr_online_vcpus;
keir@21189 419 }
keir@21189 420
keir@20860 421 struct libxl_dominfo * libxl_list_domain(struct libxl_ctx *ctx, int *nb_domain)
keir@20462 422 {
keir@20462 423 struct libxl_dominfo *ptr;
keir@20867 424 int i, ret;
keir@20822 425 xc_domaininfo_t info[1024];
keir@20822 426 int size = 1024;
keir@20462 427
keir@20516 428 ptr = calloc(size, sizeof(struct libxl_dominfo));
keir@21189 429 if (!ptr) return NULL;
keir@20822 430
keir@20860 431 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
keir@21189 432 if (ret<0) return NULL;
keir@20784 433
keir@21189 434 for (i = 0; i < ret; i++) {
keir@21189 435 xcinfo2xlinfo(&info[i], &ptr[i]);
keir@20462 436 }
keir@20867 437 *nb_domain = ret;
keir@20462 438 return ptr;
keir@20462 439 }
keir@20462 440
keir@21189 441 int libxl_domain_info(struct libxl_ctx *ctx, struct libxl_dominfo *info_r,
keir@21189 442 uint32_t domid) {
keir@21189 443 xc_domaininfo_t xcinfo;
keir@21189 444 int ret;
keir@21189 445
keir@21189 446 ret = xc_domain_getinfolist(ctx->xch, domid, 1, &xcinfo);
keir@21189 447 if (ret<0) return ERROR_FAIL;
keir@21189 448 if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
keir@21189 449
keir@21189 450 xcinfo2xlinfo(&xcinfo, info_r);
keir@21189 451 return 0;
keir@21189 452 }
keir@21189 453
keir@21368 454 struct libxl_poolinfo * libxl_list_pool(struct libxl_ctx *ctx, int *nb_pool)
keir@21368 455 {
keir@21368 456 struct libxl_poolinfo *ptr;
keir@21368 457 int i, ret;
keir@21368 458 xc_cpupoolinfo_t info[256];
keir@21368 459 int size = 256;
keir@21368 460
keir@21368 461 ptr = calloc(size, sizeof(struct libxl_poolinfo));
keir@21368 462 if (!ptr) return NULL;
keir@21368 463
keir@21368 464 ret = xc_cpupool_getinfo(ctx->xch, 0, 256, info);
keir@21368 465 if (ret<0) return NULL;
keir@21368 466
keir@21368 467 for (i = 0; i < ret; i++) {
keir@21368 468 ptr[i].poolid = info[i].cpupool_id;
keir@21368 469 }
keir@21368 470 *nb_pool = ret;
keir@21368 471 return ptr;
keir@21368 472 }
keir@21368 473
keir@20860 474 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
keir@20860 475 struct libxl_vminfo * libxl_list_vm(struct libxl_ctx *ctx, int *nb_vm)
keir@20860 476 {
keir@20860 477 struct libxl_vminfo *ptr;
keir@20860 478 int index, i, ret;
keir@20860 479 xc_domaininfo_t info[1024];
keir@20860 480 int size = 1024;
keir@20860 481
keir@20860 482 ptr = calloc(size, sizeof(struct libxl_dominfo));
keir@20860 483 if (!ptr)
keir@20860 484 return NULL;
keir@20860 485
keir@20860 486 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
keir@20860 487 for (index = i = 0; i < ret; i++) {
keir@20860 488 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
keir@20860 489 continue;
keir@20860 490 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
keir@20860 491 ptr[index].domid = info[i].domain;
keir@20860 492
keir@20860 493 index++;
keir@20860 494 }
keir@20860 495 *nb_vm = index;
keir@20860 496 return ptr;
keir@20860 497 }
keir@20860 498
keir@20462 499 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
keir@20462 500 uint32_t domid, int fd)
keir@20462 501 {
keir@20565 502 int hvm = is_hvm(ctx, domid);
keir@20565 503 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
keir@20565 504 int debug = info != NULL && info->flags & XL_SUSPEND_LIVE;
keir@20462 505
keir@20462 506 core_suspend(ctx, domid, fd, hvm, live, debug);
keir@20565 507 if (hvm)
keir@21417 508 return save_device_model(ctx, domid, fd);
keir@20462 509 return 0;
keir@20462 510 }
keir@20462 511
keir@20462 512 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 513 {
keir@20462 514 xc_domain_pause(ctx->xch, domid);
keir@20462 515 return 0;
keir@20462 516 }
keir@20462 517
keir@20462 518 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 519 {
keir@20779 520 char *path;
keir@20565 521 char *state;
keir@20565 522
keir@20565 523 if (is_hvm(ctx, domid)) {
keir@20779 524 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20565 525 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20565 526 if (state != NULL && !strcmp(state, "paused")) {
keir@20565 527 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue", strlen("continue"));
keir@20565 528 libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL);
keir@20565 529 }
keir@20565 530 }
keir@20462 531 xc_domain_unpause(ctx->xch, domid);
keir@20565 532
keir@20462 533 return 0;
keir@20462 534 }
keir@20462 535
keir@20462 536 static char *req_table[] = {
keir@20462 537 [0] = "poweroff",
keir@20462 538 [1] = "reboot",
keir@20462 539 [2] = "suspend",
keir@20462 540 [3] = "crash",
keir@20462 541 [4] = "halt",
keir@20462 542 };
keir@20462 543
keir@20462 544 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
keir@20462 545 {
keir@20462 546 char *shutdown_path;
keir@20462 547 char *dom_path;
keir@20462 548
keir@20462 549 if (req > ARRAY_SIZE(req_table))
keir@20462 550 return ERROR_INVAL;
keir@20462 551
keir@20462 552 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 553 if (!dom_path)
keir@20512 554 return ERROR_FAIL;
keir@20512 555
keir@20462 556 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
keir@20462 557
keir@20462 558 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
keir@20462 559 if (/* hvm */ 0) {
keir@20462 560 unsigned long acpi_s_state = 0;
keir@20462 561 unsigned long pvdriver = 0;
keir@20462 562 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
keir@20462 563 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
keir@20462 564 if (!pvdriver && acpi_s_state != 0)
keir@20462 565 xc_domain_shutdown(ctx->xch, domid, req);
keir@20462 566 }
keir@20462 567 return 0;
keir@20462 568 }
keir@20462 569
keir@20637 570 int libxl_get_wait_fd(struct libxl_ctx *ctx, int *fd)
keir@20545 571 {
keir@20545 572 *fd = xs_fileno(ctx->xsh);
keir@20545 573 return 0;
keir@20545 574 }
keir@20545 575
keir@20637 576 int libxl_wait_for_domain_death(struct libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
keir@20637 577 {
keir@20637 578 waiter->path = strdup("@releaseDomain");
keir@21417 579 if (asprintf(&(waiter->token), "%d", DOMAIN_DEATH) < 0)
keir@21417 580 return -1;
keir@20637 581 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
keir@20637 582 return -1;
keir@20637 583 return 0;
keir@20637 584 }
keir@20637 585
keir@20637 586 int libxl_wait_for_disk_ejects(struct libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
keir@20637 587 {
keir@20637 588 int i;
keir@20637 589 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
keir@20637 590
keir@20637 591 if (!domid)
keir@20637 592 domid = guest_domid;
keir@20637 593
keir@20637 594 for (i = 0; i < num_disks; i++) {
keir@21417 595 if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
keir@21417 596 libxl_xs_get_dompath(ctx, domid),
keir@21417 597 device_disk_dev_number(disks[i].virtpath)) < 0)
keir@21417 598 return -1;
keir@21417 599 if (asprintf(&(waiter[i].token), "%d", DISK_EJECT) < 0)
keir@21417 600 return -1;
keir@20637 601 xs_watch(ctx->xsh, waiter->path, waiter->token);
keir@20637 602 }
keir@20637 603 return 0;
keir@20637 604 }
keir@20637 605
keir@20637 606 int libxl_get_event(struct libxl_ctx *ctx, libxl_event *event)
keir@20545 607 {
keir@20545 608 unsigned int num;
keir@20637 609 char **events = xs_read_watch(ctx->xsh, &num);
keir@20637 610 if (num != 2) {
keir@20637 611 free(events);
keir@20637 612 return -1;
keir@20637 613 }
keir@20637 614 event->path = strdup(events[XS_WATCH_PATH]);
keir@20637 615 event->token = strdup(events[XS_WATCH_TOKEN]);
keir@20637 616 event->type = atoi(event->token);
keir@20637 617 free(events);
keir@20637 618 return 0;
keir@20637 619 }
keir@20637 620
keir@20637 621 int libxl_stop_waiting(struct libxl_ctx *ctx, libxl_waiter *waiter)
keir@20637 622 {
keir@20637 623 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
keir@20637 624 return -1;
keir@20637 625 else
keir@20637 626 return 0;
keir@20637 627 }
keir@20637 628
keir@20637 629 int libxl_free_event(libxl_event *event)
keir@20637 630 {
keir@20637 631 free(event->path);
keir@20637 632 free(event->token);
keir@20637 633 return 0;
keir@20637 634 }
keir@20637 635
keir@20637 636 int libxl_free_waiter(libxl_waiter *waiter)
keir@20637 637 {
keir@20637 638 free(waiter->path);
keir@20637 639 free(waiter->token);
keir@20637 640 return 0;
keir@20637 641 }
keir@20637 642
keir@20785 643 int libxl_event_get_domain_death_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, xc_domaininfo_t *info)
keir@20637 644 {
keir@20785 645 int rc = 0, ret;
keir@20545 646
keir@20637 647 if (event && event->type == DOMAIN_DEATH) {
keir@20785 648 ret = xc_domain_getinfolist(ctx->xch, domid, 1, info);
keir@20785 649 if (ret == 1 && info->domain == domid) {
keir@20785 650 if (info->flags & XEN_DOMINF_running ||
keir@20785 651 (!(info->flags & XEN_DOMINF_shutdown) && !(info->flags & XEN_DOMINF_dying)))
keir@20545 652 goto out;
keir@20545 653 rc = 1;
keir@20545 654 goto out;
keir@20545 655 }
keir@20785 656 memset(info, 0, sizeof(xc_dominfo_t));
keir@20545 657 rc = 1;
keir@20545 658 goto out;
keir@20545 659 }
keir@20545 660 out:
keir@20545 661 return rc;
keir@20545 662 }
keir@20545 663
keir@20637 664 int libxl_event_get_disk_eject_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
keir@20637 665 {
keir@20637 666 if (event && event->type == DISK_EJECT) {
keir@20637 667 char *path;
keir@20637 668 char *backend;
keir@20637 669 char *value = libxl_xs_read(ctx, XBT_NULL, event->path);
keir@20637 670
keir@20637 671 if (!value || strcmp(value, "eject"))
keir@20637 672 return 0;
keir@20637 673
keir@20637 674 path = strdup(event->path);
keir@20637 675 path[strlen(path) - 6] = '\0';
keir@20637 676 backend = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", path));
keir@20637 677
keir@20637 678 disk->backend_domid = 0;
keir@20637 679 disk->domid = domid;
keir@20637 680 disk->physpath = NULL;
keir@20637 681 disk->phystype = 0;
keir@20637 682 /* this value is returned to the user: do not free right away */
keir@20637 683 disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", backend));
keir@20637 684 disk->unpluggable = 1;
keir@20637 685 disk->readwrite = 0;
keir@20637 686 disk->is_cdrom = 1;
keir@20637 687
keir@20637 688 free(path);
keir@20637 689 return 1;
keir@20637 690 }
keir@20637 691 return 0;
keir@20637 692 }
keir@20637 693
keir@20462 694 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 695 {
keir@20462 696 char *pid;
keir@20462 697 int ret;
keir@20462 698
keir@20462 699 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
keir@20462 700 if (!pid) {
keir@20542 701 int stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20542 702 if (!stubdomid) {
keir@20542 703 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
keir@20542 704 return -1;
keir@20542 705 }
keir@20542 706 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
keir@20542 707 return libxl_domain_destroy(ctx, stubdomid, 0);
keir@20462 708 }
keir@20462 709 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
keir@20462 710
keir@20462 711 ret = kill(atoi(pid), SIGHUP);
keir@20462 712 if (ret < 0 && errno == ESRCH) {
keir@20512 713 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
keir@20462 714 ret = 0;
keir@20462 715 } else if (ret == 0) {
keir@20512 716 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
keir@20462 717 ret = 0;
keir@20462 718 } else {
keir@20512 719 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
keir@20512 720 atoi(pid));
keir@20462 721 }
keir@20462 722 return ret;
keir@20462 723 }
keir@20462 724
keir@20462 725 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
keir@20462 726 {
keir@20801 727 char *dom_path;
keir@20779 728 char *vm_path, *vss_path, *xapi_path;
keir@20628 729 int rc, dm_present;
keir@20628 730
keir@20628 731 if (is_hvm(ctx, domid)) {
keir@20628 732 dm_present = 1;
keir@20628 733 } else {
keir@20628 734 char *pid;
keir@20628 735 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
keir@20628 736 dm_present = (pid != NULL);
keir@20628 737 libxl_free(ctx, pid);
keir@20628 738 }
keir@20462 739
keir@20462 740 dom_path = libxl_xs_get_dompath(ctx, domid);
keir@20512 741 if (!dom_path)
keir@20462 742 return -1;
keir@20512 743
keir@20482 744 if (libxl_device_pci_shutdown(ctx, domid) < 0)
keir@20512 745 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
keir@20628 746 if (dm_present) {
keir@20628 747 xs_write(ctx->xsh, XBT_NULL,
keir@20628 748 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
keir@20628 749 "shutdown", strlen("shutdown"));
keir@20628 750 }
keir@20512 751 rc = xc_domain_pause(ctx->xch, domid);
keir@20512 752 if (rc < 0) {
keir@20512 753 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
keir@20462 754 return -1;
keir@20462 755 }
keir@20628 756 if (dm_present) {
keir@20628 757 if (libxl_destroy_device_model(ctx, domid) < 0)
keir@20628 758 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
keir@20628 759 }
keir@20462 760 if (libxl_devices_destroy(ctx, domid, force) < 0)
keir@20512 761 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
keir@20462 762 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
keir@20512 763 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
keir@20779 764
keir@20801 765 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", domid));
keir@20823 766 if (vm_path)
keir@20823 767 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
keir@20823 768 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
keir@20779 769
keir@20801 770 vss_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vss", domid));
keir@20823 771 if (vss_path)
keir@20823 772 if (!xs_rm(ctx->xsh, XBT_NULL, vss_path))
keir@20823 773 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vss_path);
keir@20779 774
keir@20779 775 xapi_path = libxl_sprintf(ctx, "/xapi/%u", domid);
keir@20823 776 if (xapi_path)
keir@20823 777 if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
keir@20823 778 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
keir@20779 779
keir@21190 780 libxl__userdata_destroyall(ctx, domid);
keir@21190 781
keir@20545 782 rc = xc_domain_destroy(ctx->xch, domid);
keir@20545 783 if (rc < 0) {
keir@20545 784 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
keir@20545 785 return -1;
keir@20545 786 }
keir@20462 787 return 0;
keir@20462 788 }
keir@20462 789
keir@20564 790 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num)
keir@20564 791 {
keir@21283 792 char *cmd = libxl_sprintf(
keir@21283 793 ctx, "%s/xenconsole %d --num %d",
keir@21283 794 libxl_private_bindir_path(), domid, cons_num);
keir@20564 795 return (system(cmd) != 0) ? ERROR_FAIL : 0;
keir@20564 796 }
keir@20564 797
keir@20462 798 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
keir@20462 799 libxl_device_model_info *info,
keir@20462 800 libxl_device_nic *vifs,
keir@20462 801 int num_vifs)
keir@20462 802 {
keir@20462 803 int num = 0, i;
keir@20462 804 flexarray_t *dm_args;
keir@20462 805 dm_args = flexarray_make(16, 1);
keir@20462 806 if (!dm_args)
keir@20462 807 return NULL;
keir@20462 808
keir@20516 809 flexarray_set(dm_args, num++, "qemu-dm");
keir@20516 810 flexarray_set(dm_args, num++, "-d");
keir@20462 811
keir@20462 812 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
keir@20462 813
keir@20462 814 if (info->dom_name) {
keir@20516 815 flexarray_set(dm_args, num++, "-domain-name");
keir@20516 816 flexarray_set(dm_args, num++, info->dom_name);
keir@20462 817 }
keir@20462 818 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
keir@20516 819 flexarray_set(dm_args, num++, "-vnc");
keir@20462 820 if (info->vncdisplay) {
keir@20462 821 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
keir@21362 822 flexarray_set(
keir@21362 823 dm_args, num++,
keir@21362 824 libxl_sprintf(ctx, "%s:%d%s",
keir@21362 825 info->vnclisten,
keir@21362 826 info->vncdisplay,
keir@21362 827 info->vncpasswd ? ",password" : ""));
keir@20462 828 } else {
keir@20462 829 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
keir@20462 830 }
keir@20462 831 } else if (info->vnclisten) {
keir@20462 832 if (strchr(info->vnclisten, ':') != NULL) {
keir@20516 833 flexarray_set(dm_args, num++, info->vnclisten);
keir@20462 834 } else {
keir@20462 835 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
keir@20462 836 }
keir@20462 837 } else {
keir@20516 838 flexarray_set(dm_args, num++, "127.0.0.1:0");
keir@20462 839 }
keir@20462 840 if (info->vncunused) {
keir@20516 841 flexarray_set(dm_args, num++, "-vncunused");
keir@20462 842 }
keir@20462 843 }
keir@21378 844 if (info->sdl) {
keir@20516 845 flexarray_set(dm_args, num++, "-sdl");
keir@20462 846 if (info->opengl) {
keir@20516 847 flexarray_set(dm_args, num++, "-disable-opengl");
keir@20462 848 }
keir@20462 849 }
keir@20462 850 if (info->keymap) {
keir@20516 851 flexarray_set(dm_args, num++, "-k");
keir@20516 852 flexarray_set(dm_args, num++, info->keymap);
keir@20462 853 }
keir@20462 854 if (info->nographic && (!info->sdl && !info->vnc)) {
keir@20516 855 flexarray_set(dm_args, num++, "-nographic");
keir@20462 856 }
keir@20462 857 if (info->serial) {
keir@20516 858 flexarray_set(dm_args, num++, "-serial");
keir@20516 859 flexarray_set(dm_args, num++, info->serial);
keir@20462 860 }
keir@20509 861 if (info->type == XENFV) {
keir@20509 862 if (info->videoram) {
keir@20516 863 flexarray_set(dm_args, num++, "-videoram");
keir@20509 864 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
keir@20509 865 }
keir@20509 866 if (info->stdvga) {
keir@20516 867 flexarray_set(dm_args, num++, "-std-vga");
keir@20509 868 }
keir@20509 869
keir@20509 870 if (info->boot) {
keir@20516 871 flexarray_set(dm_args, num++, "-boot");
keir@20516 872 flexarray_set(dm_args, num++, info->boot);
keir@20509 873 }
keir@20509 874 if (info->usb) {
keir@20516 875 flexarray_set(dm_args, num++, "-usb");
keir@20509 876 if (info->usbdevice) {
keir@20516 877 flexarray_set(dm_args, num++, "-usbdevice");
keir@20516 878 flexarray_set(dm_args, num++, info->usbdevice);
keir@20509 879 }
keir@20509 880 }
keir@20509 881 if (info->apic) {
keir@20516 882 flexarray_set(dm_args, num++, "-acpi");
keir@20509 883 }
keir@20509 884 for (i = 0; i < num_vifs; i++) {
keir@20509 885 if (vifs[i].nictype == NICTYPE_IOEMU) {
keir@20780 886 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20780 887 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
keir@20780 888 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
keir@20781 889 if (!vifs[i].ifname)
keir@20781 890 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid - 1);
keir@20516 891 flexarray_set(dm_args, num++, "-net");
keir@20509 892 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
keir@20780 893 vifs[i].devid, smac, vifs[i].model));
keir@20516 894 flexarray_set(dm_args, num++, "-net");
keir@20509 895 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
keir@20509 896 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
keir@20509 897 }
keir@20462 898 }
keir@20462 899 }
keir@20565 900 if (info->saved_state) {
keir@20565 901 flexarray_set(dm_args, num++, "-loadvm");
keir@20565 902 flexarray_set(dm_args, num++, info->saved_state);
keir@20565 903 }
keir@20509 904 for (i = 0; info->extra && info->extra[i] != NULL; i++)
keir@20509 905 flexarray_set(dm_args, num++, info->extra[i]);
keir@20516 906 flexarray_set(dm_args, num++, "-M");
keir@20509 907 if (info->type == XENPV)
keir@20516 908 flexarray_set(dm_args, num++, "xenpv");
keir@20509 909 else
keir@20516 910 flexarray_set(dm_args, num++, "xenfv");
keir@20462 911 flexarray_set(dm_args, num++, NULL);
keir@20462 912
keir@20462 913 return (char **) flexarray_contents(dm_args);
keir@20462 914 }
keir@20462 915
keir@20834 916 void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
keir@20834 917 {
keir@20514 918 struct libxl_device_model_starting *starting = for_spawn;
keir@20514 919 char *kvs[3];
keir@20820 920 int rc;
keir@20820 921 struct xs_handle *xsh;
keir@20514 922
keir@20820 923 xsh = xs_daemon_open();
keir@20514 924 /* we mustn't use the parent's handle in the child */
keir@20514 925
keir@20514 926 kvs[0] = "image/device-model-pid";
keir@21417 927 if (asprintf(&kvs[1], "%d", innerchild) < 0)
keir@21417 928 return;
keir@20514 929 kvs[2] = NULL;
keir@20820 930
keir@20820 931 rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
keir@20820 932 if (rc)
keir@20820 933 return;
keir@20820 934 xs_daemon_close(xsh);
keir@20514 935 }
keir@20514 936
keir@20542 937 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
keir@20542 938 libxl_device_model_info *info,
keir@20542 939 libxl_device_vfb *vfb,
keir@20542 940 libxl_device_vkb *vkb)
keir@20542 941 {
keir@20542 942 memset(vfb, 0x00, sizeof(libxl_device_vfb));
keir@20542 943 memset(vkb, 0x00, sizeof(libxl_device_vkb));
keir@20542 944
keir@20542 945 vfb->backend_domid = 0;
keir@20542 946 vfb->devid = 0;
keir@20542 947 vfb->vnc = info->vnc;
keir@20542 948 vfb->vnclisten = info->vnclisten;
keir@20542 949 vfb->vncdisplay = info->vncdisplay;
keir@20542 950 vfb->vncunused = info->vncunused;
keir@21362 951 vfb->vncpasswd = info->vncpasswd;
keir@20542 952 vfb->keymap = info->keymap;
keir@20542 953 vfb->sdl = info->sdl;
keir@20542 954 vfb->opengl = info->opengl;
keir@20542 955
keir@20542 956 vkb->backend_domid = 0;
keir@20542 957 vkb->devid = 0;
keir@20542 958 return 0;
keir@20542 959 }
keir@20542 960
keir@20542 961 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
keir@20542 962 {
keir@20542 963 int i;
keir@20542 964 char *vm_path;
keir@20542 965 char *dmargs, *path;
keir@20542 966 int dmargs_size;
keir@20542 967 struct xs_permissions roperm[2];
keir@20542 968 xs_transaction_t t;
keir@20542 969
keir@20542 970 roperm[0].id = 0;
keir@20542 971 roperm[0].perms = XS_PERM_NONE;
keir@20542 972 roperm[1].id = domid;
keir@20542 973 roperm[1].perms = XS_PERM_READ;
keir@20542 974
keir@20542 975 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
keir@20542 976
keir@20542 977 i = 0;
keir@20542 978 dmargs_size = 0;
keir@20542 979 while (args[i] != NULL) {
keir@20542 980 dmargs_size = dmargs_size + strlen(args[i]) + 1;
keir@20542 981 i++;
keir@20542 982 }
keir@20542 983 dmargs_size++;
keir@20542 984 dmargs = (char *) malloc(dmargs_size);
keir@20542 985 i = 1;
keir@20542 986 dmargs[0] = '\0';
keir@20542 987 while (args[i] != NULL) {
keir@20542 988 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
keir@20542 989 strcat(dmargs, " ");
keir@20542 990 strcat(dmargs, args[i]);
keir@20542 991 }
keir@20542 992 i++;
keir@20542 993 }
keir@20542 994 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
keir@20542 995
keir@20542 996 retry_transaction:
keir@20542 997 t = xs_transaction_start(ctx->xsh);
keir@20542 998 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
keir@20542 999 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
keir@20542 1000 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
keir@20542 1001 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 1002 if (errno == EAGAIN)
keir@20542 1003 goto retry_transaction;
keir@20542 1004 free(dmargs);
keir@20542 1005 return 0;
keir@20542 1006 }
keir@20542 1007
keir@20542 1008 static int libxl_create_stubdom(struct libxl_ctx *ctx,
keir@20542 1009 libxl_device_model_info *info,
keir@20542 1010 libxl_device_disk *disks, int num_disks,
keir@20542 1011 libxl_device_nic *vifs, int num_vifs,
keir@20542 1012 libxl_device_vfb *vfb,
keir@20542 1013 libxl_device_vkb *vkb,
keir@20542 1014 libxl_device_model_starting **starting_r)
keir@20542 1015 {
keir@20843 1016 int i, num_console = 1, ret;
keir@20570 1017 libxl_device_console *console;
keir@20542 1018 libxl_domain_create_info c_info;
keir@20542 1019 libxl_domain_build_info b_info;
keir@20542 1020 libxl_domain_build_state state;
keir@20542 1021 uint32_t domid;
keir@20542 1022 char **args;
keir@20542 1023 struct xs_permissions perm[2];
keir@20542 1024 xs_transaction_t t;
keir@20594 1025 libxl_device_model_starting *dm_starting = 0;
keir@20542 1026
keir@20542 1027 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20542 1028 if (!args)
keir@20542 1029 return ERROR_FAIL;
keir@20542 1030
keir@20542 1031 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
keir@20542 1032 c_info.hvm = 0;
keir@20542 1033 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
keir@20801 1034 for (i = 0; i < 16; i++)
keir@20801 1035 c_info.uuid[i] = info->uuid[i];
keir@20542 1036
keir@20542 1037 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
keir@20542 1038 b_info.max_vcpus = 1;
keir@20542 1039 b_info.max_memkb = 32 * 1024;
keir@20647 1040 b_info.target_memkb = b_info.max_memkb;
keir@21353 1041 b_info.kernel = libxl_abs_path(ctx, "ioemu-stubdom.gz", libxl_xenfirmwaredir_path());
keir@20542 1042 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
keir@20542 1043 b_info.u.pv.ramdisk = "";
keir@20542 1044 b_info.u.pv.features = "";
keir@20542 1045 b_info.hvm = 0;
keir@20542 1046
keir@20843 1047 ret = libxl_domain_make(ctx, &c_info, &domid);
keir@20843 1048 if (ret) return ret;
keir@20843 1049 ret = libxl_domain_build(ctx, &b_info, domid, &state);
keir@20843 1050 if (ret) return ret;
keir@20542 1051
keir@20542 1052 libxl_write_dmargs(ctx, domid, info->domid, args);
keir@20843 1053 libxl_xs_write(ctx, XBT_NULL,
keir@20843 1054 libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)),
keir@20843 1055 "%d", domid);
keir@20843 1056 libxl_xs_write(ctx, XBT_NULL,
keir@20843 1057 libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)),
keir@20843 1058 "%d", info->domid);
keir@20542 1059 xc_domain_set_target(ctx->xch, domid, info->domid);
keir@20542 1060 xs_set_target(ctx->xsh, domid, info->domid);
keir@20542 1061
keir@20542 1062 perm[0].id = domid;
keir@20542 1063 perm[0].perms = XS_PERM_NONE;
keir@20542 1064 perm[1].id = info->domid;
keir@20542 1065 perm[1].perms = XS_PERM_READ;
keir@20542 1066 retry_transaction:
keir@20542 1067 t = xs_transaction_start(ctx->xsh);
keir@20542 1068 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
keir@20542 1069 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
keir@20635 1070 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
keir@20635 1071 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
keir@20542 1072 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 1073 if (errno == EAGAIN)
keir@20542 1074 goto retry_transaction;
keir@20542 1075
keir@20542 1076 for (i = 0; i < num_disks; i++) {
keir@20781 1077 disks[i].domid = domid;
keir@20843 1078 ret = libxl_device_disk_add(ctx, domid, &disks[i]);
keir@20843 1079 if (ret) return ret;
keir@20542 1080 }
keir@20542 1081 for (i = 0; i < num_vifs; i++) {
keir@20781 1082 vifs[i].domid = domid;
keir@20843 1083 ret = libxl_device_nic_add(ctx, domid, &vifs[i]);
keir@20843 1084 if (ret) return ret;
keir@20542 1085 }
keir@20781 1086 vfb->domid = domid;
keir@20843 1087 ret = libxl_device_vfb_add(ctx, domid, vfb);
keir@20843 1088 if (ret) return ret;
keir@20781 1089 vkb->domid = domid;
keir@20843 1090 ret = libxl_device_vkb_add(ctx, domid, vkb);
keir@20843 1091 if (ret) return ret;
keir@20542 1092
keir@20570 1093 if (info->serial)
keir@20570 1094 num_console++;
keir@20843 1095
keir@20570 1096 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
keir@20843 1097 if (!console)
keir@20843 1098 return ERROR_NOMEM;
keir@20843 1099
keir@20570 1100 for (i = 0; i < num_console; i++) {
keir@20781 1101 console[i].devid = i;
keir@20781 1102 console[i].constype = CONSTYPE_IOEMU;
keir@20781 1103 console[i].domid = domid;
keir@20570 1104 if (!i)
keir@20781 1105 console[i].build_state = &state;
keir@20843 1106 ret = libxl_device_console_add(ctx, domid, &console[i]);
keir@20843 1107 if (ret) return ret;
keir@20570 1108 }
keir@20594 1109 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
keir@20594 1110 free(args);
keir@20594 1111 return -1;
keir@20594 1112 }
keir@20594 1113 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
keir@20594 1114 free(args);
keir@20594 1115 return -1;
keir@20594 1116 }
keir@20542 1117
keir@20542 1118 libxl_domain_unpause(ctx, domid);
keir@20542 1119
keir@20594 1120 if (starting_r) {
keir@20594 1121 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
keir@20598 1122 (*starting_r)->domid = info->domid;
keir@20594 1123 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20594 1124 (*starting_r)->for_spawn = NULL;
keir@20594 1125 }
keir@20594 1126
keir@20542 1127 free(args);
keir@20542 1128 return 0;
keir@20542 1129 }
keir@20542 1130
keir@20462 1131 int libxl_create_device_model(struct libxl_ctx *ctx,
keir@20462 1132 libxl_device_model_info *info,
keir@20542 1133 libxl_device_disk *disks, int num_disks,
keir@20514 1134 libxl_device_nic *vifs, int num_vifs,
keir@20514 1135 libxl_device_model_starting **starting_r)
keir@20462 1136 {
keir@20546 1137 char *path, *logfile;
keir@20514 1138 int logfile_w, null;
keir@20546 1139 int rc;
keir@20462 1140 char **args;
keir@20594 1141 struct libxl_device_model_starting buf_starting, *p;
keir@21362 1142 xs_transaction_t t;
keir@21362 1143 char *vm_path;
keir@21362 1144 char **pass_stuff;
keir@20514 1145
keir@20542 1146 if (strstr(info->device_model, "stubdom-dm")) {
keir@20542 1147 libxl_device_vfb vfb;
keir@20542 1148 libxl_device_vkb vkb;
keir@20542 1149
keir@20542 1150 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
keir@20542 1151 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
keir@20542 1152 }
keir@20542 1153
keir@20462 1154 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
keir@20462 1155 if (!args)
keir@20462 1156 return ERROR_FAIL;
keir@20462 1157
keir@20462 1158 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
keir@20462 1159 xs_mkdir(ctx->xsh, XBT_NULL, path);
keir@20462 1160
keir@20546 1161 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
keir@20467 1162 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
keir@20546 1163 free(logfile);
keir@20462 1164 null = open("/dev/null", O_RDONLY);
keir@20514 1165
keir@20514 1166 if (starting_r) {
keir@20516 1167 rc = ERROR_NOMEM;
keir@20594 1168 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
keir@20516 1169 if (!*starting_r) goto xit;
keir@20594 1170 p = *starting_r;
keir@20611 1171 p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
keir@20594 1172 } else {
keir@20594 1173 p = &buf_starting;
keir@20594 1174 p->for_spawn = NULL;
keir@20594 1175 }
keir@20514 1176
keir@20594 1177 p->domid = info->domid;
keir@20594 1178 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
keir@20594 1179 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
keir@20594 1180
keir@21362 1181 if (info->vncpasswd) {
keir@21362 1182 retry_transaction:
keir@21362 1183 /* Find uuid and the write the vnc password to xenstore for qemu. */
keir@21362 1184 t = xs_transaction_start(ctx->xsh);
keir@21362 1185 vm_path = libxl_xs_read(ctx,t,libxl_sprintf(ctx, "%s/vm", p->dom_path));
keir@21362 1186 if (vm_path) {
keir@21362 1187 /* Now write the vncpassword into it. */
keir@21362 1188 pass_stuff = libxl_calloc(ctx, 2, sizeof(char *));
keir@21362 1189 pass_stuff[0] = "vncpasswd";
keir@21362 1190 pass_stuff[1] = info->vncpasswd;
keir@21362 1191 libxl_xs_writev(ctx,t,vm_path,pass_stuff);
keir@21362 1192 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@21362 1193 if (errno == EAGAIN)
keir@21362 1194 goto retry_transaction;
keir@21362 1195 }
keir@21362 1196 }
keir@21362 1197
keir@20594 1198 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
keir@20514 1199 if (rc < 0) goto xit;
keir@20514 1200 if (!rc) { /* inner child */
keir@20833 1201 libxl_exec(null, logfile_w, logfile_w,
keir@21353 1202 libxl_abs_path(ctx, info->device_model, libxl_private_bindir_path()),
keir@21353 1203 args);
keir@20514 1204 }
keir@20514 1205
keir@20514 1206 rc = 0;
keir@20514 1207 xit:
keir@20516 1208 free(args);
keir@20462 1209 close(null);
keir@20462 1210 close(logfile_w);
keir@20462 1211
keir@20514 1212 return rc;
keir@20514 1213 }
keir@20514 1214
keir@20514 1215 int libxl_detach_device_model(struct libxl_ctx *ctx,
keir@20779 1216 libxl_device_model_starting *starting)
keir@20779 1217 {
keir@20514 1218 int rc;
keir@20594 1219 rc = libxl_spawn_detach(ctx, starting->for_spawn);
keir@20594 1220 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
keir@20514 1221 libxl_free(ctx, starting);
keir@20514 1222 return rc;
keir@20514 1223 }
keir@20462 1224
keir@20514 1225
keir@20514 1226 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
keir@20779 1227 libxl_device_model_starting *starting)
keir@20779 1228 {
keir@20514 1229 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
keir@20514 1230 libxl_spawn_check,
keir@20594 1231 starting->for_spawn);
keir@20514 1232 int detach = libxl_detach_device_model(ctx, starting);
keir@20514 1233 return problem ? problem : detach;
keir@20542 1234 return 0;
keir@20462 1235 }
keir@20462 1236
keir@20514 1237
keir@20462 1238 /******************************************************************************/
keir@20566 1239
keir@20566 1240 static int is_blktap2_supported(void)
keir@20566 1241 {
keir@20566 1242 char buf[1024];
keir@20566 1243 FILE *f = fopen("/proc/devices", "r");
keir@20566 1244
keir@20566 1245
keir@20566 1246 while (fgets(buf, sizeof(buf), f) != NULL) {
keir@20566 1247 if (strstr(buf, "blktap2")) {
keir@20566 1248 fclose(f);
keir@20566 1249 return 1;
keir@20566 1250 }
keir@20566 1251 }
keir@20566 1252 fclose(f);
keir@20566 1253 return 0;
keir@20566 1254 }
keir@20566 1255
keir@20566 1256 static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type)
keir@20566 1257 {
keir@20566 1258 char buf[1024];
keir@20566 1259 char *p;
keir@20566 1260 int devnum;
keir@20566 1261 FILE *f = fopen("/sys/class/blktap2/devices", "r");
keir@20566 1262
keir@20566 1263
keir@20566 1264 while (!feof(f)) {
keir@20566 1265 fscanf(f, "%d %s", &devnum, buf);
keir@20566 1266 p = strchr(buf, ':');
keir@20571 1267 if (p == NULL)
keir@20571 1268 continue;
keir@20566 1269 p++;
keir@20566 1270 if (!strcmp(p, name) && !strncmp(buf, type, 3)) {
keir@20566 1271 fclose(f);
keir@20566 1272 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", devnum);
keir@20566 1273 }
keir@20566 1274 }
keir@20566 1275 fclose(f);
keir@20566 1276 return NULL;
keir@20566 1277 }
keir@20566 1278
keir@20462 1279 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20462 1280 {
keir@20462 1281 flexarray_t *front;
keir@20462 1282 flexarray_t *back;
keir@20462 1283 char *backend_type;
keir@20462 1284 unsigned int boffset = 0;
keir@20462 1285 unsigned int foffset = 0;
keir@20462 1286 int devid;
keir@20462 1287 libxl_device device;
keir@20566 1288 int major, minor;
keir@20462 1289
keir@20462 1290 front = flexarray_make(16, 1);
keir@20462 1291 if (!front)
keir@20462 1292 return ERROR_NOMEM;
keir@20462 1293 back = flexarray_make(16, 1);
keir@20462 1294 if (!back) /* leaks front if error */
keir@20462 1295 return ERROR_NOMEM;
keir@20462 1296
keir@20462 1297 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
keir@20462 1298 devid = device_disk_dev_number(disk->virtpath);
keir@20947 1299 if (devid==-1) {
keir@20947 1300 XL_LOG(ctx, XL_LOG_ERROR, "Invalid or unuspported"
keir@20947 1301 " virtual disk identifier %s", disk->virtpath);
keir@20947 1302 return ERROR_INVAL;
keir@20947 1303 }
keir@20462 1304
keir@20462 1305 device.backend_devid = devid;
keir@20462 1306 device.backend_domid = disk->backend_domid;
keir@20462 1307 device.devid = devid;
keir@20462 1308 device.domid = disk->domid;
keir@20462 1309 device.kind = DEVICE_VBD;
keir@20462 1310
keir@20462 1311 switch (disk->phystype) {
keir@20462 1312 case PHYSTYPE_PHY: {
keir@20462 1313
keir@20509 1314 device_physdisk_major_minor(disk->physpath, &major, &minor);
keir@20516 1315 flexarray_set(back, boffset++, "physical-device");
keir@20462 1316 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20462 1317
keir@20516 1318 flexarray_set(back, boffset++, "params");
keir@20516 1319 flexarray_set(back, boffset++, disk->physpath);
keir@20462 1320
keir@20462 1321 device.backend_kind = DEVICE_VBD;
keir@20462 1322 break;
keir@20462 1323 }
keir@20566 1324 case PHYSTYPE_FILE:
keir@20566 1325 /* let's pretend is tap:aio for the moment */
keir@20566 1326 disk->phystype = PHYSTYPE_AIO;
keir@20462 1327 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
keir@20566 1328 if (is_blktap2_supported()) {
keir@20566 1329 int rc, c, p[2], tot;
keir@20566 1330 char buf[1024], *dev;
keir@20779 1331 dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
keir@20779 1332 if (dev == NULL) {
keir@21185 1333 rc= libxl_pipe(ctx, p); if (rc==-1) return -1;
keir@21185 1334 rc= libxl_fork(ctx); if (rc==-1) return -1;
keir@21185 1335 if (!rc) { /* child */
keir@20566 1336 int null_r, null_w;
keir@20566 1337 char *args[4];
keir@20566 1338 args[0] = "tapdisk2";
keir@20566 1339 args[1] = "-n";
keir@20566 1340 args[2] = libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath);
keir@20566 1341 args[3] = NULL;
keir@20566 1342
keir@20566 1343 null_r = open("/dev/null", O_RDONLY);
keir@20566 1344 null_w = open("/dev/null", O_WRONLY);
keir@21376 1345 libxl_exec(null_r, p[1], null_w,
keir@21376 1346 libxl_abs_path(ctx, "tapdisk2",
keir@21376 1347 libxl_sbindir_path()),
keir@21376 1348 args);
keir@20566 1349 XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
keir@20566 1350 }
keir@20566 1351 close(p[1]);
keir@20566 1352 tot = 0;
keir@20566 1353 while ((c = read(p[0], buf + tot, sizeof(buf) - tot)) > 0)
keir@20566 1354 tot = tot + c;
keir@20566 1355 close(p[0]);
keir@20566 1356 buf[tot - 1] = '\0';
keir@20566 1357 dev = buf;
keir@20566 1358 }
keir@20566 1359 flexarray_set(back, boffset++, "tapdisk-params");
keir@20566 1360 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20566 1361 flexarray_set(back, boffset++, "params");
keir@20566 1362 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", dev));
keir@20566 1363 backend_type = "phy";
keir@20566 1364 device_physdisk_major_minor(dev, &major, &minor);
keir@20566 1365 flexarray_set(back, boffset++, "physical-device");
keir@20566 1366 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
keir@20566 1367 device.backend_kind = DEVICE_VBD;
keir@20566 1368
keir@20566 1369 break;
keir@20566 1370 }
keir@20516 1371 flexarray_set(back, boffset++, "params");
keir@20462 1372 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
keir@20462 1373 device_disk_string_of_phystype(disk->phystype), disk->physpath));
keir@20462 1374
keir@20462 1375 device.backend_kind = DEVICE_TAP;
keir@20462 1376 break;
keir@20846 1377 default:
keir@20846 1378 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
keir@20846 1379 return ERROR_INVAL;
keir@20462 1380 }
keir@20462 1381
keir@20516 1382 flexarray_set(back, boffset++, "frontend-id");
keir@20462 1383 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
keir@20516 1384 flexarray_set(back, boffset++, "online");
keir@20516 1385 flexarray_set(back, boffset++, "1");
keir@20516 1386 flexarray_set(back, boffset++, "removable");
keir@20462 1387 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
keir@20516 1388 flexarray_set(back, boffset++, "bootable");
keir@20509 1389 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1390 flexarray_set(back, boffset++, "state");
keir@20462 1391 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1392 flexarray_set(back, boffset++, "dev");
keir@20516 1393 flexarray_set(back, boffset++, disk->virtpath);
keir@20516 1394 flexarray_set(back, boffset++, "type");
keir@20516 1395 flexarray_set(back, boffset++, backend_type);
keir@20516 1396 flexarray_set(back, boffset++, "mode");
keir@20516 1397 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
keir@20462 1398
keir@20516 1399 flexarray_set(front, foffset++, "backend-id");
keir@20462 1400 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
keir@20516 1401 flexarray_set(front, foffset++, "state");
keir@20462 1402 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1403 flexarray_set(front, foffset++, "virtual-device");
keir@20462 1404 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
keir@20516 1405 flexarray_set(front, foffset++, "device-type");
keir@20516 1406 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
keir@20462 1407
keir@20462 1408 if (0 /* protocol != native*/) {
keir@20516 1409 flexarray_set(front, foffset++, "protocol");
keir@20516 1410 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 1411 }
keir@20462 1412
keir@20462 1413 libxl_device_generic_add(ctx, &device,
keir@20462 1414 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 1415 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1416 flexarray_free(back);
keir@20516 1417 flexarray_free(front);
keir@20462 1418 return 0;
keir@20462 1419 }
keir@20462 1420
keir@20626 1421 int libxl_device_disk_del(struct libxl_ctx *ctx,
keir@20626 1422 libxl_device_disk *disk, int wait)
keir@20462 1423 {
keir@20626 1424 libxl_device device;
keir@20626 1425 int devid;
keir@20462 1426
keir@20626 1427 devid = device_disk_dev_number(disk->virtpath);
keir@20626 1428 device.backend_domid = disk->backend_domid;
keir@20626 1429 device.backend_devid = devid;
keir@20626 1430 device.backend_kind =
keir@20626 1431 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
keir@20626 1432 device.domid = disk->domid;
keir@20626 1433 device.devid = devid;
keir@20626 1434 device.kind = DEVICE_VBD;
keir@20626 1435 return libxl_device_del(ctx, &device, wait);
keir@20462 1436 }
keir@20462 1437
keir@20462 1438 /******************************************************************************/
keir@20462 1439 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
keir@20462 1440 {
keir@20462 1441 flexarray_t *front;
keir@20462 1442 flexarray_t *back;
keir@20462 1443 unsigned int boffset = 0;
keir@20462 1444 unsigned int foffset = 0;
keir@20462 1445 libxl_device device;
keir@21407 1446 char *dompath, **l;
keir@21407 1447 unsigned int nb;
keir@20462 1448
keir@20462 1449 front = flexarray_make(16, 1);
keir@20462 1450 if (!front)
keir@20462 1451 return ERROR_NOMEM;
keir@20462 1452 back = flexarray_make(16, 1);
keir@20462 1453 if (!back)
keir@20462 1454 return ERROR_NOMEM;
keir@20462 1455
keir@21407 1456 if (nic->devid == -1) {
keir@21407 1457 if (!(dompath = libxl_xs_get_dompath(ctx, domid))) {
keir@21407 1458 return ERROR_FAIL;
keir@21407 1459 }
keir@21407 1460 if (!(l = libxl_xs_directory(ctx, XBT_NULL,
keir@21407 1461 libxl_sprintf(ctx, "%s/device/vif", dompath), &nb))) {
keir@21407 1462 nic->devid = 0;
keir@21407 1463 } else {
keir@21407 1464 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
keir@21407 1465 libxl_free(ctx, l);
keir@21407 1466 }
keir@21407 1467 }
keir@21407 1468
keir@20462 1469 device.backend_devid = nic->devid;
keir@20462 1470 device.backend_domid = nic->backend_domid;
keir@20462 1471 device.backend_kind = DEVICE_VIF;
keir@20462 1472 device.devid = nic->devid;
keir@20462 1473 device.domid = nic->domid;
keir@20462 1474 device.kind = DEVICE_VIF;
keir@20462 1475
keir@20516 1476 flexarray_set(back, boffset++, "frontend-id");
keir@20462 1477 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
keir@20516 1478 flexarray_set(back, boffset++, "online");
keir@20516 1479 flexarray_set(back, boffset++, "1");
keir@20516 1480 flexarray_set(back, boffset++, "state");
keir@20462 1481 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1482 flexarray_set(back, boffset++, "script");
keir@20516 1483 flexarray_set(back, boffset++, nic->script);
keir@20516 1484 flexarray_set(back, boffset++, "mac");
keir@20462 1485 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 1486 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 1487 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20516 1488 flexarray_set(back, boffset++, "handle");
keir@20462 1489 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20462 1490
keir@20516 1491 flexarray_set(front, foffset++, "backend-id");
keir@20462 1492 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
keir@20516 1493 flexarray_set(front, foffset++, "state");
keir@20462 1494 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1495 flexarray_set(front, foffset++, "handle");
keir@20462 1496 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
keir@20516 1497 flexarray_set(front, foffset++, "mac");
keir@20462 1498 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
keir@20462 1499 nic->mac[0], nic->mac[1], nic->mac[2],
keir@20462 1500 nic->mac[3], nic->mac[4], nic->mac[5]));
keir@20462 1501 if (0 /* protocol != native*/) {
keir@20516 1502 flexarray_set(front, foffset++, "protocol");
keir@20516 1503 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
keir@20462 1504 }
keir@20462 1505
keir@20462 1506 libxl_device_generic_add(ctx, &device,
keir@20462 1507 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20462 1508 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20462 1509
keir@20462 1510 /* FIXME: wait for plug */
keir@20516 1511 flexarray_free(back);
keir@20516 1512 flexarray_free(front);
keir@20462 1513 return 0;
keir@20462 1514 }
keir@20462 1515
keir@20626 1516 int libxl_device_nic_del(struct libxl_ctx *ctx,
keir@20626 1517 libxl_device_nic *nic, int wait)
keir@20462 1518 {
keir@20626 1519 libxl_device device;
keir@20462 1520
keir@20626 1521 device.backend_devid = nic->devid;
keir@20626 1522 device.backend_domid = nic->backend_domid;
keir@20626 1523 device.backend_kind = DEVICE_VIF;
keir@20626 1524 device.devid = nic->devid;
keir@20626 1525 device.domid = nic->domid;
keir@20626 1526 device.kind = DEVICE_VIF;
keir@20626 1527
keir@20626 1528 return libxl_device_del(ctx, &device, wait);
keir@20462 1529 }
keir@20462 1530
keir@21408 1531 libxl_nicinfo *libxl_list_nics(struct libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
keir@21408 1532 {
keir@21408 1533 char *dompath, *nic_path_fe;
keir@21408 1534 char **l;
keir@21408 1535 char *val, *tok;
keir@21408 1536 unsigned int nb_nics, i;
keir@21408 1537 libxl_nicinfo *res, *nics;
keir@21408 1538
keir@21408 1539 dompath = libxl_xs_get_dompath(ctx, domid);
keir@21408 1540 if (!dompath) {
keir@21408 1541 return NULL;
keir@21408 1542 }
keir@21408 1543 l = libxl_xs_directory(ctx, XBT_NULL,
keir@21408 1544 libxl_sprintf(ctx, "%s/device/vif", dompath), &nb_nics);
keir@21408 1545 if (!l) {
keir@21408 1546 return NULL;
keir@21408 1547 }
keir@21408 1548 res = libxl_calloc(ctx, nb_nics, sizeof (libxl_device_nic));
keir@21408 1549 if (!res) {
keir@21408 1550 libxl_free(ctx, l);
keir@21408 1551 return NULL;
keir@21408 1552 }
keir@21408 1553 nics = res;
keir@21408 1554 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
keir@21408 1555 nic_path_fe = libxl_sprintf(ctx, "%s/device/vif/%s", dompath, *l);
keir@21408 1556
keir@21408 1557 nics->backend = libxl_xs_read(ctx, XBT_NULL,
keir@21408 1558 libxl_sprintf(ctx, "%s/backend", nic_path_fe));
keir@21408 1559 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", nic_path_fe));
keir@21408 1560 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
keir@21408 1561
keir@21408 1562 nics->devid = strtoul(*l, NULL, 10);
keir@21408 1563 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", nic_path_fe));
keir@21408 1564 nics->state = val ? strtoul(val, NULL, 10) : -1;
keir@21408 1565 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/mac", nic_path_fe));
keir@21408 1566 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
keir@21408 1567 ++i, tok = strtok(NULL, ":")) {
keir@21408 1568 nics->mac[i] = strtoul(tok, NULL, 16);
keir@21408 1569 }
keir@21408 1570 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/event-channel", nic_path_fe));
keir@21408 1571 nics->evtch = val ? strtol(val, NULL, 10) : -1;
keir@21408 1572 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/tx-ring-ref", nic_path_fe));
keir@21408 1573 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
keir@21408 1574 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/rx-ring-ref", nic_path_fe));
keir@21408 1575 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
keir@21408 1576 nics->frontend = libxl_xs_read(ctx, XBT_NULL,
keir@21408 1577 libxl_sprintf(ctx, "%s/frontend", nics->backend));
keir@21408 1578 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", nics->backend));
keir@21408 1579 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
keir@21408 1580 nics->script = libxl_xs_read(ctx, XBT_NULL,
keir@21408 1581 libxl_sprintf(ctx, "%s/script", nics->backend));
keir@21408 1582
keir@21408 1583 libxl_free(ctx, nic_path_fe);
keir@21408 1584 }
keir@21408 1585
keir@21408 1586 libxl_free(ctx, l);
keir@21408 1587 return res;
keir@21408 1588 }
keir@21408 1589
keir@21408 1590
keir@20462 1591 /******************************************************************************/
keir@20509 1592 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
keir@20462 1593 {
keir@20509 1594 flexarray_t *front;
keir@20509 1595 flexarray_t *back;
keir@20509 1596 unsigned int boffset = 0;
keir@20509 1597 unsigned int foffset = 0;
keir@20509 1598 libxl_device device;
keir@20509 1599
keir@20509 1600 if (console->build_state) {
keir@20509 1601 xs_transaction_t t;
keir@20509 1602 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
keir@20516 1603 ents[0] = "console/port";
keir@20509 1604 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
keir@20516 1605 ents[2] = "console/ring-ref";
keir@20509 1606 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
keir@20516 1607 ents[4] = "console/limit";
keir@20509 1608 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
keir@20516 1609 ents[6] = "console/type";
keir@20509 1610 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20509 1611 ents[7] = "xenconsoled";
keir@20509 1612 else
keir@20509 1613 ents[7] = "ioemu";
keir@20509 1614 retry_transaction:
keir@20509 1615 t = xs_transaction_start(ctx->xsh);
keir@20516 1616 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
keir@20509 1617 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20509 1618 if (errno == EAGAIN)
keir@20509 1619 goto retry_transaction;
keir@20509 1620 }
keir@20509 1621
keir@20509 1622 front = flexarray_make(16, 1);
keir@20509 1623 if (!front)
keir@20509 1624 return ERROR_NOMEM;
keir@20509 1625 back = flexarray_make(16, 1);
keir@20509 1626 if (!back)
keir@20509 1627 return ERROR_NOMEM;
keir@20509 1628
keir@20509 1629 device.backend_devid = console->devid;
keir@20509 1630 device.backend_domid = console->backend_domid;
keir@20509 1631 device.backend_kind = DEVICE_CONSOLE;
keir@20509 1632 device.devid = console->devid;
keir@20509 1633 device.domid = console->domid;
keir@20509 1634 device.kind = DEVICE_CONSOLE;
keir@20509 1635
keir@20516 1636 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1637 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
keir@20516 1638 flexarray_set(back, boffset++, "online");
keir@20516 1639 flexarray_set(back, boffset++, "1");
keir@20516 1640 flexarray_set(back, boffset++, "state");
keir@20509 1641 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1642 flexarray_set(back, boffset++, "domain");
keir@20516 1643 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1644 flexarray_set(back, boffset++, "protocol");
keir@20516 1645 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20509 1646
keir@20516 1647 flexarray_set(front, foffset++, "backend-id");
keir@20509 1648 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
keir@20516 1649 flexarray_set(front, foffset++, "state");
keir@20509 1650 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1651 flexarray_set(front, foffset++, "limit");
keir@20509 1652 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
keir@20516 1653 flexarray_set(front, foffset++, "protocol");
keir@20516 1654 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
keir@20516 1655 flexarray_set(front, foffset++, "type");
keir@20509 1656 if (console->constype == CONSTYPE_XENCONSOLED)
keir@20516 1657 flexarray_set(front, foffset++, "xenconsoled");
keir@20509 1658 else
keir@20516 1659 flexarray_set(front, foffset++, "ioemu");
keir@20509 1660
keir@20509 1661 libxl_device_generic_add(ctx, &device,
keir@20509 1662 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1663 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1664 flexarray_free(back);
keir@20516 1665 flexarray_free(front);
keir@20509 1666
keir@20509 1667 return 0;
keir@20509 1668 }
keir@20509 1669
keir@20509 1670 /******************************************************************************/
keir@20509 1671 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
keir@20509 1672 {
keir@20509 1673 flexarray_t *front;
keir@20509 1674 flexarray_t *back;
keir@20509 1675 unsigned int boffset = 0;
keir@20509 1676 unsigned int foffset = 0;
keir@20509 1677 libxl_device device;
keir@20509 1678
keir@20509 1679 front = flexarray_make(16, 1);
keir@20509 1680 if (!front)
keir@20509 1681 return ERROR_NOMEM;
keir@20509 1682 back = flexarray_make(16, 1);
keir@20509 1683 if (!back)
keir@20509 1684 return ERROR_NOMEM;
keir@20509 1685
keir@20509 1686 device.backend_devid = vkb->devid;
keir@20509 1687 device.backend_domid = vkb->backend_domid;
keir@20509 1688 device.backend_kind = DEVICE_VKBD;
keir@20509 1689 device.devid = vkb->devid;
keir@20509 1690 device.domid = vkb->domid;
keir@20509 1691 device.kind = DEVICE_VKBD;
keir@20509 1692
keir@20516 1693 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1694 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
keir@20516 1695 flexarray_set(back, boffset++, "online");
keir@20516 1696 flexarray_set(back, boffset++, "1");
keir@20516 1697 flexarray_set(back, boffset++, "state");
keir@20509 1698 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1699 flexarray_set(back, boffset++, "domain");
keir@20516 1700 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20509 1701
keir@20516 1702 flexarray_set(front, foffset++, "backend-id");
keir@20509 1703 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
keir@20516 1704 flexarray_set(front, foffset++, "state");
keir@20509 1705 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1706
keir@20509 1707 libxl_device_generic_add(ctx, &device,
keir@20509 1708 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1709 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20516 1710 flexarray_free(back);
keir@20516 1711 flexarray_free(front);
keir@20509 1712
keir@20509 1713 return 0;
keir@20462 1714 }
keir@20462 1715
keir@20462 1716 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1717 {
keir@20462 1718 return ERROR_NI;
keir@20462 1719 }
keir@20462 1720
keir@20462 1721 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1722 {
keir@20462 1723 return ERROR_NI;
keir@20462 1724 }
keir@20462 1725
keir@20637 1726 libxl_device_disk *libxl_device_disk_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20637 1727 {
keir@20637 1728 char *be_path_tap, *be_path_vbd;
keir@21411 1729 libxl_device_disk *dend, *disks, *ret = NULL;
keir@21411 1730 char **b, **l = NULL;
keir@20637 1731 unsigned int numl;
keir@20637 1732 char *type;
keir@20637 1733
keir@20637 1734 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
keir@20637 1735 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
keir@20637 1736
keir@21411 1737 b = l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
keir@20637 1738 if (l) {
keir@21411 1739 ret = realloc(ret, sizeof(libxl_device_disk) * numl);
keir@21411 1740 disks = ret;
keir@21411 1741 *num = numl;
keir@21411 1742 dend = ret + *num;
keir@21411 1743 for (; disks < dend; ++disks, ++l) {
keir@21411 1744 disks->backend_domid = 0;
keir@21411 1745 disks->domid = domid;
keir@21411 1746 disks->physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, *l));
keir@21411 1747 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_vbd, *l)), &(disks->phystype));
keir@21411 1748 disks->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, *l));
keir@21411 1749 disks->unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, *l)));
keir@21411 1750 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, *l)), "w"))
keir@21411 1751 disks->readwrite = 1;
keir@20637 1752 else
keir@21411 1753 disks->readwrite = 0;
keir@21411 1754 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, *l))));
keir@21411 1755 disks->is_cdrom = !strcmp(type, "cdrom");
keir@20637 1756 }
keir@21411 1757 libxl_free(ctx, b);
keir@20637 1758 }
keir@21411 1759 b = l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
keir@20637 1760 if (l) {
keir@21411 1761 ret = realloc(ret, sizeof(libxl_device_disk) * (*num + numl));
keir@21411 1762 disks = ret + *num;
keir@21411 1763 *num += numl;
keir@21411 1764 for (dend = ret + *num; disks < dend; ++disks, ++l) {
keir@21411 1765 disks->backend_domid = 0;
keir@21411 1766 disks->domid = domid;
keir@21411 1767 disks->physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, *l));
keir@21411 1768 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_tap, *l)), &(disks->phystype));
keir@21411 1769 disks->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, *l));
keir@21411 1770 disks->unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, *l)));
keir@21411 1771 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, *l)), "w"))
keir@21411 1772 disks->readwrite = 1;
keir@20637 1773 else
keir@21411 1774 disks->readwrite = 0;
keir@21411 1775 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_tap, *l))));
keir@21411 1776 disks->is_cdrom = !strcmp(type, "cdrom");
keir@20637 1777 }
keir@21411 1778 libxl_free(ctx, b);
keir@20637 1779 }
keir@21411 1780 return ret;
keir@21411 1781 }
keir@21411 1782
keir@21411 1783 int libxl_device_disk_getinfo(struct libxl_ctx *ctx, uint32_t domid,
keir@21411 1784 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
keir@21411 1785 {
keir@21411 1786 char *dompath, *diskpath;
keir@21411 1787 char *val;
keir@21411 1788
keir@21411 1789 dompath = libxl_xs_get_dompath(ctx, domid);
keir@21411 1790 diskinfo->devid = device_disk_dev_number(disk->virtpath);
keir@21411 1791
keir@21411 1792 /* tap devices entries in xenstore are written as vbd devices. */
keir@21411 1793 diskpath = libxl_sprintf(ctx, "%s/device/vbd/%d", dompath, diskinfo->devid);
keir@21411 1794 diskinfo->backend = libxl_xs_read(ctx, XBT_NULL,
keir@21411 1795 libxl_sprintf(ctx, "%s/backend", diskpath));
keir@21411 1796 if (!diskinfo->backend) {
keir@21411 1797 return ERROR_FAIL;
keir@21411 1798 }
keir@21411 1799 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", diskpath));
keir@21411 1800 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
keir@21411 1801 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", diskpath));
keir@21411 1802 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
keir@21411 1803 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/event-channel", diskpath));
keir@21411 1804 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
keir@21411 1805 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/ring-ref", diskpath));
keir@21411 1806 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
keir@21411 1807 diskinfo->frontend = libxl_xs_read(ctx, XBT_NULL,
keir@21411 1808 libxl_sprintf(ctx, "%s/frontend", diskinfo->backend));
keir@21411 1809 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", diskinfo->backend));
keir@21411 1810 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
keir@21411 1811
keir@21411 1812 return 0;
keir@20637 1813 }
keir@20637 1814
keir@20637 1815 int libxl_cdrom_insert(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
keir@20637 1816 {
keir@20637 1817 int num, i;
keir@20637 1818 uint32_t stubdomid;
keir@20637 1819 libxl_device_disk *disks;
keir@20637 1820
keir@20637 1821 if (!disk->physpath) {
keir@20637 1822 disk->physpath = "";
keir@20637 1823 disk->phystype = PHYSTYPE_PHY;
keir@20637 1824 }
keir@20637 1825 disks = libxl_device_disk_list(ctx, domid, &num);
keir@20637 1826 for (i = 0; i < num; i++) {
keir@20637 1827 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
keir@20637 1828 /* found */
keir@20637 1829 break;
keir@20637 1830 }
keir@20637 1831 if (i == num) {
keir@20637 1832 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
keir@20637 1833 return -1;
keir@20637 1834 }
keir@20637 1835 libxl_device_disk_del(ctx, disks + i, 1);
keir@20637 1836 libxl_device_disk_add(ctx, domid, disk);
keir@20637 1837 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20637 1838 if (stubdomid) {
keir@20781 1839 disks[i].domid = stubdomid;
keir@20637 1840 libxl_device_disk_del(ctx, disks + i, 1);
keir@20781 1841 disk->domid = stubdomid;
keir@20637 1842 libxl_device_disk_add(ctx, stubdomid, disk);
keir@20781 1843 disk->domid = domid;
keir@20637 1844 }
keir@20637 1845 return 0;
keir@20637 1846 }
keir@20637 1847
keir@20462 1848 /******************************************************************************/
keir@20509 1849 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
keir@20509 1850 libxl_device_vfb *vfb,
keir@20509 1851 int num_console,
keir@20509 1852 libxl_device_console *console,
keir@20860 1853 libxl_device_model_info *info)
keir@20860 1854 {
keir@20509 1855 int i = 0, j = 0, num = 0;
keir@20509 1856 memset(info, 0x00, sizeof(libxl_device_model_info));
keir@20509 1857
keir@20509 1858 info->vnc = vfb->vnc;
keir@20509 1859 if (vfb->vnclisten)
keir@20509 1860 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
keir@20509 1861 info->vncdisplay = vfb->vncdisplay;
keir@20509 1862 info->vncunused = vfb->vncunused;
keir@21362 1863 if (vfb->vncpasswd)
keir@21362 1864 info->vncpasswd = vfb->vncpasswd;
keir@20509 1865 if (vfb->keymap)
keir@20509 1866 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
keir@20509 1867 info->sdl = vfb->sdl;
keir@20509 1868 info->opengl = vfb->opengl;
keir@20509 1869 for (i = 0; i < num_console; i++) {
keir@20509 1870 if (console->constype == CONSTYPE_IOEMU)
keir@20509 1871 num++;
keir@20509 1872 }
keir@20509 1873 if (num > 0) {
keir@20860 1874 uint32_t guest_domid;
keir@20860 1875 if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
keir@20599 1876 char *filename;
keir@20599 1877 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
keir@20599 1878 libxl_create_logfile(ctx, name, &filename);
keir@20599 1879 info->serial = libxl_sprintf(ctx, "file:%s", filename);
keir@20599 1880 free(filename);
keir@20599 1881 } else {
keir@20599 1882 info->serial = "pty";
keir@20599 1883 }
keir@20509 1884 num--;
keir@20509 1885 }
keir@20509 1886 if (num > 0) {
keir@20509 1887 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
keir@20509 1888 for (j = 0; j < num * 2; j = j + 2) {
keir@20509 1889 info->extra[j] = "-serial";
keir@20509 1890 info->extra[j + 1] = "pty";
keir@20509 1891 }
keir@20509 1892 info->extra[j] = NULL;
keir@20509 1893 }
keir@20509 1894 info->domid = vfb->domid;
keir@20509 1895 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
keir@21376 1896 info->device_model = libxl_abs_path(ctx, "qemu-dm", libxl_libexec_path());
keir@20509 1897 info->type = XENPV;
keir@20509 1898 return 0;
keir@20509 1899 }
keir@20509 1900
keir@20509 1901 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
keir@20514 1902 int num_console, libxl_device_console *console,
keir@20514 1903 struct libxl_device_model_starting **starting_r)
keir@20509 1904 {
keir@20509 1905 libxl_device_model_info info;
keir@20509 1906
keir@20509 1907 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
keir@20542 1908 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
keir@20509 1909 return 0;
keir@20509 1910 }
keir@20509 1911
keir@20509 1912 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
keir@20462 1913 {
keir@20509 1914 flexarray_t *front;
keir@20509 1915 flexarray_t *back;
keir@20509 1916 unsigned int boffset = 0;
keir@20509 1917 unsigned int foffset = 0;
keir@20509 1918 libxl_device device;
keir@20509 1919
keir@20509 1920 front = flexarray_make(16, 1);
keir@20509 1921 if (!front)
keir@20509 1922 return ERROR_NOMEM;
keir@20509 1923 back = flexarray_make(16, 1);
keir@20509 1924 if (!back)
keir@20509 1925 return ERROR_NOMEM;
keir@20509 1926
keir@20509 1927 device.backend_devid = vfb->devid;
keir@20509 1928 device.backend_domid = vfb->backend_domid;
keir@20509 1929 device.backend_kind = DEVICE_VFB;
keir@20509 1930 device.devid = vfb->devid;
keir@20509 1931 device.domid = vfb->domid;
keir@20509 1932 device.kind = DEVICE_VFB;
keir@20509 1933
keir@20516 1934 flexarray_set(back, boffset++, "frontend-id");
keir@20509 1935 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
keir@20516 1936 flexarray_set(back, boffset++, "online");
keir@20516 1937 flexarray_set(back, boffset++, "1");
keir@20516 1938 flexarray_set(back, boffset++, "state");
keir@20509 1939 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 1940 flexarray_set(back, boffset++, "domain");
keir@20516 1941 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20516 1942 flexarray_set(back, boffset++, "vnc");
keir@20509 1943 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
keir@20516 1944 flexarray_set(back, boffset++, "vnclisten");
keir@20516 1945 flexarray_set(back, boffset++, vfb->vnclisten);
keir@21362 1946 flexarray_set(back, boffset++, "vncpasswd");
keir@21362 1947 flexarray_set(back, boffset++, vfb->vncpasswd);
keir@20516 1948 flexarray_set(back, boffset++, "vncdisplay");
keir@20509 1949 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
keir@20516 1950 flexarray_set(back, boffset++, "vncunused");
keir@20509 1951 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
keir@20516 1952 flexarray_set(back, boffset++, "sdl");
keir@20509 1953 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
keir@20516 1954 flexarray_set(back, boffset++, "opengl");
keir@20509 1955 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
keir@20509 1956 if (vfb->xauthority) {
keir@20516 1957 flexarray_set(back, boffset++, "xauthority");
keir@20516 1958 flexarray_set(back, boffset++, vfb->xauthority);
keir@20509 1959 }
keir@20509 1960 if (vfb->display) {
keir@20516 1961 flexarray_set(back, boffset++, "display");
keir@20516 1962 flexarray_set(back, boffset++, vfb->display);
keir@20509 1963 }
keir@20509 1964
keir@20516 1965 flexarray_set(front, foffset++, "backend-id");
keir@20509 1966 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
keir@20516 1967 flexarray_set(front, foffset++, "state");
keir@20509 1968 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20509 1969
keir@20509 1970 libxl_device_generic_add(ctx, &device,
keir@20509 1971 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20509 1972 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20509 1973 flexarray_free(front);
keir@20509 1974 flexarray_free(back);
keir@20509 1975
keir@20509 1976 return 0;
keir@20462 1977 }
keir@20462 1978
keir@20462 1979 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1980 {
keir@20462 1981 return ERROR_NI;
keir@20462 1982 }
keir@20462 1983
keir@20462 1984 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20462 1985 {
keir@20462 1986 return ERROR_NI;
keir@20462 1987 }
keir@20462 1988
keir@20462 1989 /******************************************************************************/
keir@20482 1990
keir@20482 1991 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
keir@20482 1992 unsigned int bus, unsigned int dev,
keir@20482 1993 unsigned int func, unsigned int vdevfn)
keir@20482 1994 {
keir@20482 1995 pcidev->domain = domain;
keir@20482 1996 pcidev->bus = bus;
keir@20482 1997 pcidev->dev = dev;
keir@20482 1998 pcidev->func = func;
keir@20482 1999 pcidev->vdevfn = vdevfn;
keir@20482 2000 return 0;
keir@20482 2001 }
keir@20482 2002
keir@20482 2003 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
keir@20482 2004 {
keir@20482 2005 flexarray_t *front;
keir@20482 2006 flexarray_t *back;
keir@20482 2007 unsigned int boffset = 0;
keir@20482 2008 unsigned int foffset = 0;
keir@20482 2009 libxl_device device;
keir@20482 2010 int i;
keir@20482 2011
keir@20482 2012 front = flexarray_make(16, 1);
keir@20482 2013 if (!front)
keir@20482 2014 return ERROR_NOMEM;
keir@20482 2015 back = flexarray_make(16, 1);
keir@20482 2016 if (!back)
keir@20482 2017 return ERROR_NOMEM;
keir@20482 2018
keir@20512 2019 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
keir@20482 2020
keir@20482 2021 /* add pci device */
keir@20482 2022 device.backend_devid = 0;
keir@20482 2023 device.backend_domid = 0;
keir@20482 2024 device.backend_kind = DEVICE_PCI;
keir@20482 2025 device.devid = 0;
keir@20482 2026 device.domid = domid;
keir@20482 2027 device.kind = DEVICE_PCI;
keir@20482 2028
keir@20516 2029 flexarray_set(back, boffset++, "frontend-id");
keir@20482 2030 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
keir@20516 2031 flexarray_set(back, boffset++, "online");
keir@20516 2032 flexarray_set(back, boffset++, "1");
keir@20516 2033 flexarray_set(back, boffset++, "state");
keir@20482 2034 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 2035 flexarray_set(back, boffset++, "domain");
keir@20516 2036 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
keir@20482 2037 for (i = 0; i < num; i++) {
keir@20482 2038 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
keir@20482 2039 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 2040 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
keir@20482 2041 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 2042 if (pcidev->vdevfn) {
keir@20482 2043 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
keir@20482 2044 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 2045 }
keir@20482 2046 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
keir@20482 2047 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 2048 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
keir@20482 2049 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 2050 }
keir@20516 2051 flexarray_set(back, boffset++, "num_devs");
keir@20482 2052 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
keir@20482 2053
keir@20516 2054 flexarray_set(front, foffset++, "backend-id");
keir@20482 2055 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
keir@20516 2056 flexarray_set(front, foffset++, "state");
keir@20482 2057 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
keir@20482 2058
keir@20482 2059 libxl_device_generic_add(ctx, &device,
keir@20482 2060 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
keir@20482 2061 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
keir@20482 2062
keir@20482 2063 flexarray_free(back);
keir@20482 2064 flexarray_free(front);
keir@20482 2065 return 0;
keir@20482 2066 }
keir@20482 2067
keir@20482 2068 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 2069 {
keir@20482 2070 flexarray_t *back;
keir@20482 2071 char *num_devs, *be_path;
keir@20482 2072 int num = 0;
keir@20482 2073 unsigned int boffset = 0;
keir@20482 2074 xs_transaction_t t;
keir@20482 2075
keir@20516 2076 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 2077 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 2078 if (!num_devs)
keir@20482 2079 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
keir@20482 2080
keir@20482 2081 if (!is_hvm(ctx, domid)) {
keir@20482 2082 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
keir@20482 2083 return -1;
keir@20482 2084 }
keir@20482 2085
keir@20482 2086 back = flexarray_make(16, 1);
keir@20482 2087 if (!back)
keir@20482 2088 return ERROR_NOMEM;
keir@20482 2089
keir@20512 2090 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
keir@20482 2091 num = atoi(num_devs);
keir@20482 2092 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
keir@20482 2093 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 2094 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
keir@20482 2095 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
keir@20482 2096 if (pcidev->vdevfn) {
keir@20482 2097 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
keir@20482 2098 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
keir@20482 2099 }
keir@20482 2100 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
keir@20482 2101 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
keir@20482 2102 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
keir@20482 2103 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
keir@20516 2104 flexarray_set(back, boffset++, "num_devs");
keir@20482 2105 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
keir@20516 2106 flexarray_set(back, boffset++, "state");
keir@20482 2107 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
keir@20482 2108
keir@20482 2109 retry_transaction:
keir@20482 2110 t = xs_transaction_start(ctx->xsh);
keir@20482 2111 libxl_xs_writev(ctx, t, be_path,
keir@20482 2112 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
keir@20482 2113 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 2114 if (errno == EAGAIN)
keir@20482 2115 goto retry_transaction;
keir@20482 2116
keir@20482 2117 flexarray_free(back);
keir@20482 2118 return 0;
keir@20482 2119 }
keir@20482 2120
keir@20482 2121 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20482 2122 {
keir@20542 2123 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
keir@20542 2124 int num, i, j;
keir@20482 2125 xs_transaction_t t;
keir@20482 2126 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
keir@20482 2127
keir@20516 2128 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 2129 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
keir@20482 2130 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
keir@20482 2131 if (!num_devs)
keir@20482 2132 return -1;
keir@20482 2133 num = atoi(num_devs);
keir@20482 2134
keir@20482 2135 if (!is_hvm(ctx, domid)) {
keir@20482 2136 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 2137 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20482 2138 return -1;
keir@20482 2139 }
keir@20482 2140 }
keir@20482 2141
keir@20482 2142 for (i = 0; i < num; i++) {
keir@20482 2143 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 2144 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 2145 if (domain == pcidev->domain && bus == pcidev->bus &&
keir@20482 2146 pcidev->dev == dev && pcidev->func == func) {
keir@20482 2147 break;
keir@20482 2148 }
keir@20482 2149 }
keir@20482 2150 if (i == num) {
keir@20512 2151 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
keir@20482 2152 return -1;
keir@20482 2153 }
keir@20482 2154
keir@20482 2155 retry_transaction:
keir@20482 2156 t = xs_transaction_start(ctx->xsh);
keir@20542 2157 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
keir@20482 2158 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
keir@20482 2159 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20482 2160 if (errno == EAGAIN)
keir@20482 2161 goto retry_transaction;
keir@20542 2162
keir@20542 2163 if (!is_hvm(ctx, domid)) {
keir@20542 2164 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
keir@20542 2165 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
keir@20542 2166 return -1;
keir@20542 2167 }
keir@20542 2168 }
keir@20542 2169
keir@20542 2170 retry_transaction2:
keir@20542 2171 t = xs_transaction_start(ctx->xsh);
keir@20542 2172 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
keir@20542 2173 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
keir@20542 2174 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20542 2175 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
keir@20542 2176 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20542 2177 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20542 2178 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
keir@20542 2179 for (j = i + 1; j < num; j++) {
keir@20542 2180 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
keir@20542 2181 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2182 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2183 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2184 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
keir@20542 2185 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2186 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2187 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2188 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
keir@20542 2189 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2190 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2191 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2192 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
keir@20542 2193 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2194 if (tmp) {
keir@20542 2195 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2196 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2197 }
keir@20542 2198 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
keir@20542 2199 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2200 if (tmp) {
keir@20542 2201 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2202 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2203 }
keir@20542 2204 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
keir@20542 2205 tmp = libxl_xs_read(ctx, t, tmppath);
keir@20542 2206 if (tmp) {
keir@20542 2207 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
keir@20542 2208 xs_rm(ctx->xsh, t, tmppath);
keir@20542 2209 }
keir@20542 2210 }
keir@20542 2211 if (!xs_transaction_end(ctx->xsh, t, 0))
keir@20542 2212 if (errno == EAGAIN)
keir@20542 2213 goto retry_transaction2;
keir@20542 2214
keir@20542 2215 if (num == 1) {
keir@20542 2216 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
keir@20542 2217 libxl_device_destroy(ctx, be_path, 1);
keir@20542 2218 xs_rm(ctx->xsh, XBT_NULL, be_path);
keir@20542 2219 xs_rm(ctx->xsh, XBT_NULL, fe_path);
keir@20542 2220 return 0;
keir@20542 2221 }
keir@20542 2222
keir@20482 2223 return 0;
keir@20462 2224 }
keir@20462 2225
keir@20482 2226 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 2227 {
keir@20779 2228 char *path;
keir@20482 2229 char *state, *vdevfn;
keir@20482 2230 int rc, hvm;
keir@20542 2231 int stubdomid = 0;
keir@20482 2232
keir@20482 2233 /* TODO: check if the device can be assigned */
keir@20482 2234
keir@20482 2235 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2236
keir@20779 2237 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20779 2238 if (stubdomid != 0) {
keir@20542 2239 libxl_device_pci pcidev_s = *pcidev;
keir@20542 2240 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
keir@20542 2241 }
keir@20542 2242
keir@20482 2243 hvm = is_hvm(ctx, domid);
keir@20482 2244 if (hvm) {
keir@20831 2245 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
keir@20482 2246 return -1;
keir@20482 2247 }
keir@20779 2248 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2249 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20779 2250 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2251 if (pcidev->vdevfn)
keir@20482 2252 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
keir@20482 2253 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
keir@20482 2254 else
keir@20482 2255 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 2256 pcidev->bus, pcidev->dev, pcidev->func);
keir@20779 2257 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
keir@20482 2258 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
keir@20831 2259 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", NULL, NULL) < 0)
keir@20512 2260 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20779 2261 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2262 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
keir@20482 2263 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
keir@20779 2264 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2265 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 2266 } else {
keir@20542 2267 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 2268 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2269 FILE *f = fopen(sysfs_path, "r");
keir@20482 2270 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 2271 int irq = 0;
keir@20482 2272 int i;
keir@20482 2273
keir@20482 2274 if (f == NULL) {
keir@20512 2275 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2276 return -1;
keir@20482 2277 }
keir@20482 2278 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20512 2279 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
keir@20482 2280 size = end - start + 1;
keir@20482 2281 if (start) {
keir@20482 2282 if (flags & PCI_BAR_IO) {
keir@20482 2283 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
keir@20482 2284 if (rc < 0)
keir@20512 2285 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 2286 } else {
keir@20482 2287 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 2288 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
keir@20482 2289 if (rc < 0)
keir@20512 2290 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 2291 }
keir@20482 2292 }
keir@20482 2293 }
keir@20482 2294 fclose(f);
keir@20542 2295 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 2296 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2297 f = fopen(sysfs_path, "r");
keir@20482 2298 if (f == NULL) {
keir@20512 2299 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2300 goto out;
keir@20482 2301 }
keir@20482 2302 fscanf(f, "%u", &irq);
keir@20482 2303 if (irq) {
keir@20482 2304 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
keir@20482 2305 if (rc < 0) {
keir@20512 2306 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
keir@20482 2307 }
keir@20482 2308 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
keir@20482 2309 if (rc < 0) {
keir@20512 2310 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
keir@20482 2311 }
keir@20482 2312 }
keir@20482 2313 fclose(f);
keir@20482 2314 }
keir@20482 2315 out:
keir@20860 2316 if (!libxl_is_stubdom(ctx, domid, NULL)) {
keir@20779 2317 rc = xc_assign_device(ctx->xch, domid, pcidev->value);
keir@20779 2318 if (rc < 0)
keir@20542 2319 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
keir@20542 2320 }
keir@20482 2321
keir@20482 2322 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
keir@20482 2323 return 0;
keir@20462 2324 }
keir@20462 2325
keir@20482 2326 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
keir@20462 2327 {
keir@20779 2328 char *path;
keir@20482 2329 char *state;
keir@20482 2330 int hvm, rc;
keir@20542 2331 int stubdomid = 0;
keir@20482 2332
keir@20482 2333 /* TODO: check if the device can be detached */
keir@20542 2334 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
keir@20482 2335
keir@20482 2336 hvm = is_hvm(ctx, domid);
keir@20482 2337 if (hvm) {
keir@20831 2338 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
keir@20482 2339 return -1;
keir@20482 2340 }
keir@20779 2341 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2342 state = libxl_xs_read(ctx, XBT_NULL, path);
keir@20779 2343 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
keir@20482 2344 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
keir@20482 2345 pcidev->bus, pcidev->dev, pcidev->func);
keir@20779 2346 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
keir@20482 2347 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
keir@20831 2348 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", NULL, NULL) < 0) {
keir@20512 2349 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
keir@20482 2350 return -1;
keir@20482 2351 }
keir@20779 2352 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
keir@20482 2353 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
keir@20482 2354 } else {
keir@20542 2355 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
keir@20482 2356 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2357 FILE *f = fopen(sysfs_path, "r");
keir@20482 2358 unsigned int start = 0, end = 0, flags = 0, size = 0;
keir@20482 2359 int irq = 0;
keir@20482 2360 int i;
keir@20482 2361
keir@20482 2362 if (f == NULL) {
keir@20512 2363 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2364 goto skip1;
keir@20482 2365 }
keir@20482 2366 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
keir@20482 2367 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
keir@20482 2368 size = end - start + 1;
keir@20482 2369 if (start) {
keir@20482 2370 if (flags & PCI_BAR_IO) {
keir@20482 2371 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
keir@20482 2372 if (rc < 0)
keir@20512 2373 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
keir@20482 2374 } else {
keir@20482 2375 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
keir@20482 2376 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
keir@20482 2377 if (rc < 0)
keir@20512 2378 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
keir@20482 2379 }
keir@20482 2380 }
keir@20482 2381 }
keir@20482 2382 fclose(f);
keir@20482 2383 skip1:
keir@20542 2384 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
keir@20482 2385 pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2386 f = fopen(sysfs_path, "r");
keir@20482 2387 if (f == NULL) {
keir@20512 2388 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
keir@20482 2389 goto out;
keir@20482 2390 }
keir@20482 2391 fscanf(f, "%u", &irq);
keir@20482 2392 if (irq) {
keir@20482 2393 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
keir@20482 2394 if (rc < 0) {
keir@20512 2395 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
keir@20482 2396 }
keir@20482 2397 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
keir@20482 2398 if (rc < 0) {
keir@20512 2399 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
keir@20482 2400 }
keir@20482 2401 }
keir@20482 2402 fclose(f);
keir@20482 2403 }
keir@20482 2404 out:
keir@20482 2405 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
keir@20482 2406
keir@20860 2407 if (!libxl_is_stubdom(ctx, domid, NULL)) {
keir@20779 2408 rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
keir@20779 2409 if (rc < 0)
keir@20542 2410 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
keir@20542 2411 }
keir@20542 2412
keir@20779 2413 stubdomid = libxl_get_stubdom_id(ctx, domid);
keir@20779 2414 if (stubdomid != 0) {
keir@20542 2415 libxl_device_pci pcidev_s = *pcidev;
keir@20542 2416 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
keir@20542 2417 }
keir@20542 2418
keir@20482 2419 return 0;
keir@20462 2420 }
keir@20482 2421
keir@20482 2422 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
keir@20482 2423 {
keir@20482 2424 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
keir@20482 2425 int n, i;
keir@20482 2426 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
keir@20482 2427 libxl_device_pci *pcidevs;
keir@20482 2428
keir@20516 2429 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
keir@20482 2430 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
keir@20482 2431 if (!num_devs) {
keir@20482 2432 *num = 0;
keir@20482 2433 return NULL;
keir@20482 2434 }
keir@20482 2435 n = atoi(num_devs);
keir@20779 2436 pcidevs = calloc(n, sizeof(libxl_device_pci));
keir@20482 2437 *num = n;
keir@20482 2438
keir@20482 2439 for (i = 0; i < n; i++) {
keir@20482 2440 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
keir@20482 2441 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
keir@20482 2442 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
keir@20482 2443 if (xsvdevfn)
keir@20482 2444 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
keir@20482 2445 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
keir@20482 2446 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
keir@20482 2447 if (xsopts) {
keir@20482 2448 char *saveptr;
keir@20482 2449 char *p = strtok_r(xsopts, ",=", &saveptr);
keir@20482 2450 do {
keir@20482 2451 while (*p == ' ')
keir@20482 2452 p++;
keir@20482 2453 if (!strcmp(p, "msitranslate")) {
keir@20482 2454 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 2455 pcidevs[i].msitranslate = atoi(p);
keir@20482 2456 } else if (!strcmp(p, "power_mgmt")) {
keir@20482 2457 p = strtok_r(NULL, ",=", &saveptr);
keir@20482 2458 pcidevs[i].power_mgmt = atoi(p);
keir@20482 2459 }
keir@20482 2460 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
keir@20482 2461 }
keir@20482 2462 }
keir@20482 2463 return pcidevs;
keir@20482 2464 }
keir@20482 2465
keir@20482 2466 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
keir@20482 2467 {
keir@20482 2468 libxl_device_pci *pcidevs;
keir@20482 2469 int num, i;
keir@20482 2470
keir@20482 2471 pcidevs = libxl_device_pci_list(ctx, domid, &num);
keir@20482 2472 for (i = 0; i < num; i++) {
keir@20482 2473 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
keir@20482 2474 return -1;
keir@20482 2475 }
keir@20516 2476 free(pcidevs);
keir@20482 2477 return 0;
keir@20482 2478 }
keir@20482 2479
keir@21404 2480 int libxl_domain_setmaxmem(struct libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
keir@21404 2481 {
keir@21404 2482 char *mem, *endptr;
keir@21404 2483 uint32_t memorykb;
keir@21404 2484 char *dompath = libxl_xs_get_dompath(ctx, domid);
keir@21404 2485
keir@21404 2486 mem = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath));
keir@21404 2487 if (!mem) {
keir@21404 2488 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
keir@21404 2489 return 1;
keir@21404 2490 }
keir@21404 2491 memorykb = strtoul(mem, &endptr, 10);
keir@21404 2492 if (*endptr != '\0') {
keir@21404 2493 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
keir@21404 2494 return 1;
keir@21404 2495 }
keir@21404 2496
keir@21404 2497 if (max_memkb < memorykb) {
keir@21404 2498 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
keir@21404 2499 return 1;
keir@21404 2500 }
keir@21404 2501
keir@21404 2502 if (domid != 0)
keir@21404 2503 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/static-max", dompath), "%lu", max_memkb);
keir@21404 2504
keir@21422 2505 return 0;
keir@21404 2506 }
keir@21404 2507
keir@21422 2508 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb, int enforce)
keir@20647 2509 {
keir@20647 2510 int rc = 0;
keir@21422 2511 uint32_t memorykb = 0, videoram = 0;
keir@21405 2512 char *memmax, *endptr, *videoram_s = NULL;
keir@20647 2513 char *dompath = libxl_xs_get_dompath(ctx, domid);
keir@21374 2514 xc_domaininfo_t info;
keir@21374 2515 struct libxl_dominfo ptr;
keir@21374 2516 char *uuid;
keir@20542 2517
keir@21405 2518 if (domid) {
keir@21405 2519 memmax = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/static-max", dompath));
keir@21405 2520 if (!memmax) {
keir@21405 2521 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@21405 2522 "cannot get memory info from %s/memory/static-max\n", dompath);
keir@21405 2523 return 1;
keir@21405 2524 }
keir@21405 2525 memorykb = strtoul(memmax, &endptr, 10);
keir@21405 2526 if (*endptr != '\0') {
keir@21405 2527 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@21405 2528 "invalid max memory %s from %s/memory/static-max\n", memmax, dompath);
keir@21405 2529 return 1;
keir@21405 2530 }
keir@21405 2531
keir@21405 2532 if (target_memkb > memorykb) {
keir@21405 2533 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
keir@21405 2534 "memory_dynamic_max must be less than or equal to memory_static_max\n");
keir@21405 2535 return 1;
keir@21405 2536 }
keir@21405 2537 }
keir@21405 2538
keir@20647 2539 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
keir@21314 2540 videoram = videoram_s ? atoi(videoram_s) : 0;
keir@20647 2541
keir@20647 2542 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
keir@21374 2543
keir@21374 2544 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
keir@21374 2545 if (rc != 1 || info.domain != domid)
keir@21374 2546 return rc;
keir@21374 2547 xcinfo2xlinfo(&info, &ptr);
keir@21374 2548 uuid = libxl_uuid2string(ctx, ptr.uuid);
keir@21374 2549 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/vm/%s/memory", uuid), "%lu", target_memkb / 1024);
keir@21374 2550
keir@21422 2551 if (enforce || !domid)
keir@21422 2552 memorykb = target_memkb;
keir@21422 2553 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb + LIBXL_MAXMEM_CONSTANT);
keir@21422 2554 if (rc != 0)
keir@21422 2555 return rc;
keir@20647 2556 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
keir@20647 2557 return rc;
keir@20647 2558 }
keir@20875 2559
keir@20875 2560 int libxl_button_press(struct libxl_ctx *ctx, uint32_t domid, libxl_button button)
keir@20875 2561 {
keir@20875 2562 int rc = -1;
keir@20875 2563
keir@20875 2564 switch (button) {
keir@20875 2565 case POWER_BUTTON:
keir@20875 2566 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
keir@20875 2567 break;
keir@20875 2568 case SLEEP_BUTTON:
keir@20875 2569 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
keir@20875 2570 break;
keir@20875 2571 default:
keir@20875 2572 break;
keir@20875 2573 }
keir@20875 2574
keir@20875 2575 return rc;
keir@20875 2576 }
keir@21141 2577
keir@21141 2578 int libxl_get_physinfo(struct libxl_ctx *ctx, struct libxl_physinfo *physinfo)
keir@21141 2579 {
keir@21141 2580 xc_physinfo_t xcphysinfo = { 0 };
keir@21141 2581 int rc;
keir@21141 2582
keir@21141 2583 rc = xc_physinfo(ctx->xch, &xcphysinfo);
keir@21141 2584 if (rc != 0) {
keir@21141 2585 return rc;
keir@21141 2586 }
keir@21141 2587 physinfo->threads_per_core = xcphysinfo.threads_per_core;
keir@21141 2588 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
keir@21142 2589 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
keir@21141 2590 physinfo->nr_cpus = xcphysinfo.nr_cpus;
keir@21141 2591 physinfo->cpu_khz = xcphysinfo.cpu_khz;
keir@21141 2592 physinfo->total_pages = xcphysinfo.total_pages;
keir@21141 2593 physinfo->free_pages = xcphysinfo.free_pages;
keir@21141 2594 physinfo->scrub_pages = xcphysinfo.scrub_pages;
keir@21264 2595 physinfo->nr_nodes = xcphysinfo.nr_nodes;
keir@21264 2596 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
keir@21264 2597 physinfo->phys_cap = xcphysinfo.capabilities;
keir@21264 2598
keir@21141 2599 return 0;
keir@21141 2600 }
keir@21141 2601
keir@21266 2602 const libxl_version_info* libxl_get_version_info(struct libxl_ctx *ctx)
keir@21266 2603 {
keir@21266 2604 union {
keir@21266 2605 xen_extraversion_t xen_extra;
keir@21266 2606 xen_compile_info_t xen_cc;
keir@21266 2607 xen_changeset_info_t xen_chgset;
keir@21266 2608 xen_capabilities_info_t xen_caps;
keir@21266 2609 xen_platform_parameters_t p_parms;
keir@21266 2610 xen_commandline_t xen_commandline;
keir@21266 2611 } u;
keir@21266 2612 long xen_version;
keir@21266 2613 libxl_version_info *info = &ctx->version_info;
keir@21266 2614
keir@21266 2615 if (info->xen_version_extra != NULL)
keir@21266 2616 return info;
keir@21266 2617
keir@21266 2618 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
keir@21266 2619 info->xen_version_major = xen_version >> 16;
keir@21266 2620 info->xen_version_minor = xen_version & 0xFF;
keir@21266 2621 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
keir@21266 2622 info->xen_version_extra = libxl_sprintf(ctx, "%s", u.xen_extra);
keir@21266 2623
keir@21266 2624 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
keir@21266 2625 info->compiler = libxl_sprintf(ctx, "%s", u.xen_cc.compiler);
keir@21266 2626 info->compile_by = libxl_sprintf(ctx, "%s", u.xen_cc.compile_by);
keir@21266 2627 info->compile_domain = libxl_sprintf(ctx, "%s", u.xen_cc.compile_domain);
keir@21266 2628 info->compile_date = libxl_sprintf(ctx, "%s", u.xen_cc.compile_date);
keir@21266 2629
keir@21266 2630 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
keir@21266 2631 info->capabilities = libxl_sprintf(ctx, "%s", u.xen_caps);
keir@21266 2632
keir@21266 2633 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
keir@21266 2634 info->changeset = libxl_sprintf(ctx, "%s", u.xen_chgset);
keir@21266 2635
keir@21266 2636 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
keir@21266 2637 info->virt_start = u.p_parms.virt_start;
keir@21266 2638
keir@21266 2639 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
keir@21266 2640
keir@21266 2641 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
keir@21266 2642 info->commandline = libxl_sprintf(ctx, "%s", u.xen_commandline);
keir@21266 2643
keir@21266 2644 return info;
keir@21266 2645 }
keir@21266 2646
keir@21141 2647 struct libxl_vcpuinfo *libxl_list_vcpu(struct libxl_ctx *ctx, uint32_t domid,
keir@21141 2648 int *nb_vcpu, int *cpusize)
keir@21141 2649 {
keir@21141 2650 struct libxl_vcpuinfo *ptr, *ret;
keir@21141 2651 xc_domaininfo_t domaininfo;
keir@21141 2652 xc_vcpuinfo_t vcpuinfo;
keir@21141 2653 xc_physinfo_t physinfo = { 0 };
keir@21141 2654
keir@21141 2655 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
keir@21141 2656 return NULL;
keir@21141 2657 }
keir@21141 2658 if (xc_physinfo(ctx->xch, &physinfo) == -1) {
keir@21141 2659 return NULL;
keir@21141 2660 }
keir@21141 2661 *cpusize = physinfo.max_cpu_id + 1;
keir@21141 2662 ptr = libxl_calloc(ctx, domaininfo.max_vcpu_id + 1, sizeof (struct libxl_vcpuinfo));
keir@21141 2663 if (!ptr) {
keir@21141 2664 return NULL;
keir@21141 2665 }
keir@21141 2666
keir@21141 2667 ret = ptr;
keir@21141 2668 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
keir@21141 2669 ptr->cpumap = libxl_calloc(ctx, (*cpusize + 63) / 64, sizeof (uint64_t));
keir@21141 2670 if (!ptr->cpumap) {
keir@21141 2671 return NULL;
keir@21141 2672 }
keir@21141 2673 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
keir@21141 2674 return NULL;
keir@21141 2675 }
keir@21141 2676 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap, *cpusize) == -1) {
keir@21141 2677 return NULL;
keir@21141 2678 }
keir@21141 2679 ptr->vcpuid = *nb_vcpu;
keir@21141 2680 ptr->cpu = vcpuinfo.cpu;
keir@21141 2681 ptr->online = !!vcpuinfo.online;
keir@21141 2682 ptr->blocked = !!vcpuinfo.blocked;
keir@21141 2683 ptr->running = !!vcpuinfo.running;
keir@21141 2684 ptr->vcpu_time = vcpuinfo.cpu_time;
keir@21141 2685 }
keir@21141 2686 return ret;
keir@21141 2687 }
keir@21142 2688
keir@21142 2689 int libxl_set_vcpuaffinity(struct libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
keir@21142 2690 uint64_t *cpumap, int cpusize)
keir@21142 2691 {
keir@21142 2692 return (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap, cpusize));
keir@21142 2693 }
keir@21143 2694
keir@21143 2695 int libxl_set_vcpucount(struct libxl_ctx *ctx, uint32_t domid, uint32_t count)
keir@21143 2696 {
keir@21143 2697 xc_domaininfo_t domaininfo;
keir@21143 2698 char *dompath;
keir@21143 2699 int i;
keir@21143 2700
keir@21143 2701 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
keir@21143 2702 return ERROR_FAIL;
keir@21143 2703 }
keir@21143 2704 if (!count || ((domaininfo.max_vcpu_id + 1) < count)) {
keir@21143 2705 return ERROR_INVAL;
keir@21143 2706 }
keir@21143 2707 if (!(dompath = libxl_xs_get_dompath(ctx, domid)))
keir@21143 2708 return ERROR_FAIL;
keir@21143 2709
keir@21143 2710 for (i = 0; i <= domaininfo.max_vcpu_id; ++i) {
keir@21143 2711 libxl_xs_write(ctx, XBT_NULL,
keir@21143 2712 libxl_sprintf(ctx, "%s/cpu/%u/availability", dompath, i),
keir@21143 2713 "%s", ((1 << i) & ((1 << count) - 1)) ? "online" : "offline");
keir@21143 2714 }
keir@21143 2715 return 0;
keir@21143 2716 }
keir@21265 2717
keir@21265 2718 /*
keir@21265 2719 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
keir@21265 2720 * or -1 if an error occured.
keir@21265 2721 */
keir@21265 2722 int libxl_get_sched_id(struct libxl_ctx *ctx)
keir@21265 2723 {
keir@21265 2724 int sched, ret;
keir@21265 2725
keir@21265 2726 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0)
keir@21265 2727 return ret;
keir@21265 2728 return sched;
keir@21265 2729 }
keir@21292 2730
keir@21292 2731 int libxl_sched_credit_domain_get(struct libxl_ctx *ctx, uint32_t domid, struct libxl_sched_credit *scinfo)
keir@21292 2732 {
keir@21292 2733 struct xen_domctl_sched_credit sdom;
keir@21292 2734 int rc;
keir@21292 2735
keir@21292 2736 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
keir@21292 2737 if (rc != 0)
keir@21292 2738 return rc;
keir@21292 2739
keir@21292 2740 scinfo->weight = sdom.weight;
keir@21292 2741 scinfo->cap = sdom.cap;
keir@21292 2742
keir@21292 2743 return 0;
keir@21292 2744 }
keir@21292 2745
keir@21292 2746 int libxl_sched_credit_domain_set(struct libxl_ctx *ctx, uint32_t domid, struct libxl_sched_credit *scinfo)
keir@21292 2747 {
keir@21292 2748 struct xen_domctl_sched_credit sdom;
keir@21292 2749 xc_domaininfo_t domaininfo;
keir@21292 2750 int rc;
keir@21292 2751
keir@21292 2752 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
keir@21292 2753 if (rc != 1 || domaininfo.domain != domid)
keir@21292 2754 return rc;
keir@21292 2755
keir@21292 2756
keir@21292 2757 if (scinfo->weight < 1 || scinfo->weight > 65535) {
keir@21292 2758 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
keir@21292 2759 "Cpu weight out of range, valid values are within range from 1 to 65535");
keir@21292 2760 return -1;
keir@21292 2761 }
keir@21292 2762
keir@21292 2763 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
keir@21292 2764 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
keir@21292 2765 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
keir@21292 2766 ((domaininfo.max_vcpu_id + 1) * 100));
keir@21292 2767 return -1;
keir@21292 2768 }
keir@21292 2769
keir@21292 2770 sdom.weight = scinfo->weight;
keir@21292 2771 sdom.cap = scinfo->cap;
keir@21292 2772
keir@21292 2773 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
keir@21292 2774 if (rc != 0)
keir@21292 2775 return rc;
keir@21292 2776
keir@21292 2777 return 0;
keir@21292 2778 }
keir@21292 2779
keir@21393 2780 static int trigger_type_from_string(char *trigger_name)
keir@21393 2781 {
keir@21393 2782 if (!strcmp(trigger_name, "nmi"))
keir@21393 2783 return XEN_DOMCTL_SENDTRIGGER_NMI;
keir@21393 2784 else if (!strcmp(trigger_name, "reset"))
keir@21393 2785 return XEN_DOMCTL_SENDTRIGGER_RESET;
keir@21393 2786 else if (!strcmp(trigger_name, "init"))
keir@21393 2787 return XEN_DOMCTL_SENDTRIGGER_INIT;
keir@21393 2788 else if (!strcmp(trigger_name, "power"))
keir@21393 2789 return XEN_DOMCTL_SENDTRIGGER_POWER;
keir@21393 2790 else if (!strcmp(trigger_name, "sleep"))
keir@21393 2791 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
keir@21393 2792 else
keir@21393 2793 return -1;
keir@21393 2794 }
keir@21393 2795
keir@21393 2796 int libxl_send_trigger(struct libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
keir@21393 2797 {
keir@21393 2798 int rc = -1;
keir@21393 2799 int trigger_type = trigger_type_from_string(trigger_name);
keir@21393 2800
keir@21393 2801 if (trigger_type == -1) {
keir@21393 2802 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
keir@21393 2803 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
keir@21393 2804 return -1;
keir@21393 2805 }
keir@21393 2806
keir@21393 2807 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
keir@21393 2808 if (rc != 0)
keir@21393 2809 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
keir@21393 2810 "Send trigger '%s' failed", trigger_name);
keir@21393 2811
keir@21393 2812 return rc;
keir@21393 2813 }
keir@21402 2814
keir@21402 2815 int libxl_send_sysrq(struct libxl_ctx *ctx, uint32_t domid, char sysrq)
keir@21402 2816 {
keir@21402 2817 char *dompath = libxl_xs_get_dompath(ctx, domid);
keir@21402 2818
keir@21402 2819 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/control/sysrq", dompath), "%c", sysrq);
keir@21402 2820
keir@21402 2821 return 0;
keir@21402 2822 }
keir@21402 2823
keir@21425 2824 uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid)
keir@21425 2825 {
keir@21425 2826 char *dompath = libxl_xs_get_dompath(ctx, domid);
keir@21425 2827 char *vm_path, *start_time;
keir@21425 2828
keir@21425 2829 vm_path = libxl_xs_read(
keir@21425 2830 ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vm", dompath));
keir@21425 2831 start_time = libxl_xs_read(
keir@21425 2832 ctx, XBT_NULL, libxl_sprintf(ctx, "%s/start_time", vm_path));
keir@21425 2833 if (start_time == NULL) {
keir@21425 2834 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
keir@21425 2835 "Can't get start time of domain '%d'", domid);
keir@21425 2836 return -1;
keir@21425 2837 }
keir@21425 2838
keir@21425 2839 return strtoul(start_time, NULL, 10);
keir@21425 2840 }
keir@21425 2841