debuggers.hg

annotate tools/libxl/libxl.c @ 21930:bf4d0eb3643d

libxl: consistently use typedef struct {} libxl_$type

typedef struct seems to be the dominant idiom in the mixture currently
in use.

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