debuggers.hg

annotate tools/libxl/libxl.c @ 21913:60f0ba74e18f

libxl: Add LIBXL_EVENT namespace prefix to enum libxl_event_type

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