debuggers.hg

annotate tools/libxl/libxl.c @ 22243:a0044f16d3df

libxl: use transactions in libxl_set_memory_target

[fixed up for conflicts with libxl__ naming policy changes]

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