debuggers.hg

annotate tools/libxl/libxl.c @ 21425:b21b9dd9d1e3

xl: Add "xl uptime" command, a clone of "xm uptime".

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