debuggers.hg

annotate tools/libxl/libxl.c @ 22726:03718b569d97

xl: Implement flexarray_append() and flexarray_vappend()

Makes a lot of code simpler and nicer and saves a fair amount of screen
real-estate

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