debuggers.hg

annotate tools/libxl/libxl.c @ 21265:8567087654f0

libxl: add sched_get_id function

To get the name of the currently used scheduler, Xen provides a
sched_id sysctl. Add a libxl wrapper around the libxc function to
query this.

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