debuggers.hg

annotate tools/libxl/libxl.c @ 21681:8e3658934612

libxl: give reasons for ctx init failures

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