debuggers.hg

annotate tools/libxl/libxl.c @ 22000:6e621e039cde

Fix glibc crash dump in xl vcpu-{list,set}

xc_vcpu_[sg]etaffinity require the number of _bytes_ needed for
holding a CPU map, not the number of bits. Fix this when
calling the function from libxl and rename the misleading variable
name to avoid future confusion.

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