debuggers.hg

annotate tools/libxl/libxl.c @ 22078:fa82b0540e8c

libxl: generate destructors for each libxl defined type

I chose the name "_destroy" rather than "_free" because the destructor
functions will free only the members of a type recursively but will
not free the actual type structure itself. The allocation of the type
is typically done by the caller and may not be a single allocation,
e.g. lists/arrays of types or embedded in other structures etc.

The exceptions to this rule are libxl_string_list_destroy and
libxl_key_value_list_destroy but I'm not 100% convinced they are
exceptions (since they are kind-of opaque) and I couldn't see a
cleanerway to express this concept. I have made a best effort attempt
to implement these functions sanely but since as far as I can tell
nothing in the current code base ever sets
libxl_domain_create_info.{xsdata,platformdata} I'm flying somewhat
blind.

[PATCH 05 of 16 of
libxl: autogenerate type definitions and destructor functions]

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