debuggers.hg

annotate tools/libxl/libxl.c @ 21894:267ddd7fa11f

libxl: do not create /vss and /xapi paths in xenstore.

libxl doesn't know anything about these paths and there is no reason
it needs to manage them.

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