debuggers.hg

annotate tools/libxl/libxl.c @ 22015:ca51dba3a7b1

xl: don't use libxl allocator for vcpu_list
This also fixes a bug with an erroneous call to libxl_free().
A destructor for the vcpu list is also implemented which is called from
xl.

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