debuggers.hg

annotate tools/libxl/libxl.c @ 21920:cb3b15c1a8e6

libxl: add libxl_domain_preserve

This method is intended to preserve an existing (but shut down) domain
(for debugging purposes) in such a way that the domain can also be
restarted.

There is likely to be more required to achieve this aim than the
function currently does.

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