debuggers.hg

annotate tools/libxl/libxl.c @ 21879:9ddd7905d093

implement xl vncviewer

Implement xl vncviewer. The only difference from xm vncviewer is that we
obey the VNCVIEWER environment variable.

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