debuggers.hg

annotate tools/libxl/libxl.c @ 21408:5e997c43d6c1

xl: Implement network-list command

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