debuggers.hg

annotate tools/libxl/libxl.c @ 21581:a08a8256e997

xl: network2-list command

Usage: xl network2-list <Domains>

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