debuggers.hg

view tools/libxl/libxl.c @ 21990:0232bc7c9544

tools/hotplug, Use udev rules instead of qemu script to setup the bridge.
From: Anthony PERARD <anthony.perard@citrix.com>

This patch adds a second argument to vif-bridge script. It can be "vif"
or "tap". "vif" give the default behavior and "tap" just add the
interface to the found bridge when the action is "add".

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
tools/hotplug/Linux/vif-bridge | 20 ++++++---
tools/hotplug/Linux/vif-common.sh | 70 ++++++++++++++++++++++++---------
tools/hotplug/Linux/xen-backend.rules | 5 +-
tools/libxl/libxl.c | 6 +-
4 files changed, 70 insertions(+), 31 deletions(-)
author Anthony Perard <anthony.perard@citrix.com>
date Mon Aug 09 16:46:02 2010 +0100 (2010-08-09)
parents 88adea5b4546
children 6e621e039cde
line source
1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
4 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; version 2.1 only. with the special
9 * exception on linking described in file LICENSE.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 */
17 #include "libxl_osdeps.h"
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <sys/select.h>
26 #include <sys/mman.h>
27 #include <sys/wait.h>
28 #include <signal.h>
29 #include <unistd.h> /* for write, unlink and close */
30 #include <stdint.h>
31 #include <inttypes.h>
32 #include <assert.h>
34 #include "libxl.h"
35 #include "libxl_utils.h"
36 #include "libxl_internal.h"
37 #include "flexarray.h"
38 #include "tap-ctl.h"
40 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
42 int libxl_ctx_init(libxl_ctx *ctx, int version, xentoollog_logger *lg)
43 {
44 if (version != LIBXL_VERSION)
45 return ERROR_VERSION;
46 memset(ctx, 0, sizeof(libxl_ctx));
47 ctx->lg = lg;
48 ctx->alloc_maxsize = 256;
49 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
50 if (!ctx->alloc_ptrs)
51 return ERROR_NOMEM;
52 memset(&ctx->version_info, 0, sizeof(libxl_version_info));
54 ctx->xch = xc_interface_open(lg,lg,0);
55 if (!ctx->xch) {
56 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno,
57 "cannot open libxc handle");
58 free(ctx->alloc_ptrs);
59 return ERROR_FAIL;
60 }
62 ctx->xsh = xs_daemon_open();
63 if (!ctx->xsh) {
64 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno,
65 "cannot connect to xenstore");
66 xc_interface_close(ctx->xch);
67 free(ctx->alloc_ptrs);
68 return ERROR_FAIL;
69 }
70 return 0;
71 }
73 int libxl_ctx_free(libxl_ctx *ctx)
74 {
75 libxl_free_all(ctx);
76 free(ctx->alloc_ptrs);
77 xc_interface_close(ctx->xch);
78 if (ctx->xsh) xs_daemon_close(ctx->xsh);
79 return 0;
80 }
82 /******************************************************************************/
84 int libxl_domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
85 uint32_t *domid)
86 {
87 int flags, ret, i, rc;
88 char *uuid_string;
89 char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
90 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
91 "control", "attr", "messages" };
92 char *dom_path, *vm_path;
93 struct xs_permissions roperm[2];
94 struct xs_permissions rwperm[1];
95 xs_transaction_t t;
96 xen_domain_handle_t handle;
98 uuid_string = libxl_uuid2string(ctx, info->uuid);
99 if (!uuid_string) return ERROR_NOMEM;
101 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
102 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
103 flags |= info->oos ? 0 : XEN_DOMCTL_CDF_oos_off;
104 *domid = -1;
106 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
107 memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
109 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
110 if (ret < 0) {
111 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
112 return ERROR_FAIL;
113 }
115 ret = xc_cpupool_movedomain(ctx->xch, info->poolid, *domid);
116 if (ret < 0) {
117 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain move fail");
118 return ERROR_FAIL;
119 }
121 dom_path = libxl_xs_get_dompath(ctx, *domid);
122 if (!dom_path)
123 return ERROR_FAIL;
125 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
126 if (!vm_path) {
127 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
128 return ERROR_FAIL;
129 }
131 roperm[0].id = 0;
132 roperm[0].perms = XS_PERM_NONE;
133 roperm[1].id = *domid;
134 roperm[1].perms = XS_PERM_READ;
135 rwperm[0].id = *domid;
136 rwperm[0].perms = XS_PERM_NONE;
138 retry_transaction:
139 t = xs_transaction_start(ctx->xsh);
140 xs_rm(ctx->xsh, t, dom_path);
141 xs_mkdir(ctx->xsh, t, dom_path);
142 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
144 xs_rm(ctx->xsh, t, vm_path);
145 xs_mkdir(ctx->xsh, t, vm_path);
146 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
148 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
149 rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
150 if (rc) return rc;
152 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
153 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
154 xs_mkdir(ctx->xsh, t, path);
155 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
156 libxl_free(ctx, path);
157 }
158 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
159 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
160 xs_mkdir(ctx->xsh, t, path);
161 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
162 libxl_free(ctx, path);
163 }
165 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
166 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
167 if (info->poolname)
168 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/pool_name", vm_path), info->poolname, strlen(info->poolname));
170 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
171 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
173 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
175 if (!xs_transaction_end(ctx->xsh, t, 0))
176 if (errno == EAGAIN)
177 goto retry_transaction;
178 return 0;
179 }
181 int libxl_domain_rename(libxl_ctx *ctx, uint32_t domid,
182 const char *old_name, const char *new_name,
183 xs_transaction_t trans) {
184 char *dom_path = 0;
185 const char *name_path;
186 char *got_old_name;
187 unsigned int got_old_len;
188 xs_transaction_t our_trans = 0;
189 int rc;
191 dom_path = libxl_xs_get_dompath(ctx, domid);
192 if (!dom_path) goto x_nomem;
194 name_path= libxl_sprintf(ctx, "%s/name", dom_path);
195 if (!name_path) goto x_nomem;
197 retry_transaction:
198 if (!trans) {
199 trans = our_trans = xs_transaction_start(ctx->xsh);
200 if (!our_trans) {
201 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno,
202 "create xs transaction for domain (re)name");
203 goto x_fail;
204 }
205 }
207 if (old_name) {
208 got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len);
209 if (!got_old_name) {
210 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, errno, "check old name"
211 " for domain %"PRIu32" allegedly named `%s'",
212 domid, old_name);
213 goto x_fail;
214 }
215 if (strcmp(old_name, got_old_name)) {
216 XL_LOG(ctx, XL_LOG_ERROR, "domain %"PRIu32" allegedly named "
217 "`%s' is actually named `%s' - racing ?",
218 domid, old_name, got_old_name);
219 free(got_old_name);
220 goto x_fail;
221 }
222 free(got_old_name);
223 }
224 if (!xs_write(ctx->xsh, trans, name_path,
225 new_name, strlen(new_name))) {
226 XL_LOG(ctx, XL_LOG_ERROR, "failed to write new name `%s'"
227 " for domain %"PRIu32" previously named `%s'",
228 new_name, domid, old_name);
229 goto x_fail;
230 }
232 if (our_trans) {
233 if (!xs_transaction_end(ctx->xsh, our_trans, 0)) {
234 trans = our_trans = 0;
235 if (errno != EAGAIN) {
236 XL_LOG(ctx, XL_LOG_ERROR, "failed to commit new name `%s'"
237 " for domain %"PRIu32" previously named `%s'",
238 new_name, domid, old_name);
239 goto x_fail;
240 }
241 XL_LOG(ctx, XL_LOG_DEBUG, "need to retry rename transaction"
242 " for domain %"PRIu32" (name_path=\"%s\", new_name=\"%s\")",
243 domid, name_path, new_name);
244 goto retry_transaction;
245 }
246 our_trans = 0;
247 }
249 rc = 0;
250 x_rc:
251 if (dom_path) libxl_free(ctx, dom_path);
252 if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1);
253 return rc;
255 x_fail: rc = ERROR_FAIL; goto x_rc;
256 x_nomem: rc = ERROR_NOMEM; goto x_rc;
257 }
259 int libxl_domain_build(libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
260 {
261 char **vments = NULL, **localents = NULL;
262 struct timeval start_time;
263 int i, ret;
265 ret = build_pre(ctx, domid, info, state);
266 if (ret) goto out;
268 gettimeofday(&start_time, NULL);
270 if (info->hvm) {
271 ret = build_hvm(ctx, domid, info, state);
272 if (ret) goto out;
274 vments = libxl_calloc(ctx, 7, sizeof(char *));
275 vments[0] = "rtc/timeoffset";
276 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
277 vments[2] = "image/ostype";
278 vments[3] = "hvm";
279 vments[4] = "start_time";
280 vments[5] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
281 } else {
282 ret = build_pv(ctx, domid, info, state);
283 if (ret) goto out;
285 vments = libxl_calloc(ctx, 11, sizeof(char *));
286 i = 0;
287 vments[i++] = "image/ostype";
288 vments[i++] = "linux";
289 vments[i++] = "image/kernel";
290 vments[i++] = (char*) info->kernel.path;
291 vments[i++] = "start_time";
292 vments[i++] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
293 if (info->u.pv.ramdisk.path) {
294 vments[i++] = "image/ramdisk";
295 vments[i++] = (char*) info->u.pv.ramdisk.path;
296 }
297 if (info->u.pv.cmdline) {
298 vments[i++] = "image/cmdline";
299 vments[i++] = (char*) info->u.pv.cmdline;
300 }
301 }
302 ret = build_post(ctx, domid, info, state, vments, localents);
303 out:
304 libxl_file_reference_unmap(ctx, &info->kernel);
305 if (!info->hvm)
306 libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
308 return ret;
309 }
311 int libxl_domain_restore(libxl_ctx *ctx, libxl_domain_build_info *info,
312 uint32_t domid, int fd, libxl_domain_build_state *state,
313 libxl_device_model_info *dm_info)
314 {
315 char **vments = NULL, **localents = NULL;
316 struct timeval start_time;
317 int i, ret, esave, flags;
319 ret = build_pre(ctx, domid, info, state);
320 if (ret) goto out;
322 ret = restore_common(ctx, domid, info, state, fd);
323 if (ret) goto out;
325 gettimeofday(&start_time, NULL);
327 if (info->hvm) {
328 vments = libxl_calloc(ctx, 7, sizeof(char *));
329 vments[0] = "rtc/timeoffset";
330 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
331 vments[2] = "image/ostype";
332 vments[3] = "hvm";
333 vments[4] = "start_time";
334 vments[5] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
335 } else {
336 vments = libxl_calloc(ctx, 11, sizeof(char *));
337 i = 0;
338 vments[i++] = "image/ostype";
339 vments[i++] = "linux";
340 vments[i++] = "image/kernel";
341 vments[i++] = (char*) info->kernel.path;
342 vments[i++] = "start_time";
343 vments[i++] = libxl_sprintf(ctx, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
344 if (info->u.pv.ramdisk.path) {
345 vments[i++] = "image/ramdisk";
346 vments[i++] = (char*) info->u.pv.ramdisk.path;
347 }
348 if (info->u.pv.cmdline) {
349 vments[i++] = "image/cmdline";
350 vments[i++] = (char*) info->u.pv.cmdline;
351 }
352 }
353 ret = build_post(ctx, domid, info, state, vments, localents);
354 if (ret) goto out;
356 dm_info->saved_state = NULL;
357 if (info->hvm) {
358 ret = asprintf(&dm_info->saved_state,
359 "/var/lib/xen/qemu-save.%d", domid);
360 ret = (ret < 0) ? ERROR_FAIL : 0;
361 }
363 out:
364 libxl_file_reference_unmap(ctx, &info->kernel);
365 if (!info->hvm)
366 libxl_file_reference_unmap(ctx, &info->u.pv.ramdisk);
368 esave = errno;
370 flags = fcntl(fd, F_GETFL);
371 if (flags == -1) {
372 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to get flags on restore fd");
373 } else {
374 flags &= ~O_NONBLOCK;
375 if (fcntl(fd, F_SETFL, flags) == -1)
376 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unable to put restore fd"
377 " back to blocking mode");
378 }
380 errno = esave;
381 return ret;
382 }
384 int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid)
385 {
386 if (is_hvm(ctx, domid)) {
387 XL_LOG(ctx, XL_LOG_DEBUG, "Called domain_resume on "
388 "non-cooperative hvm domain %u", domid);
389 return ERROR_NI;
390 }
391 if (xc_domain_resume(ctx->xch, domid, 1)) {
392 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
393 "xc_domain_resume failed for domain %u",
394 domid);
395 return ERROR_FAIL;
396 }
397 if (!xs_resume_domain(ctx->xsh, domid)) {
398 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
399 "xs_resume_domain failed for domain %u",
400 domid);
401 return ERROR_FAIL;
402 }
403 return 0;
404 }
406 /*
407 * Preserves a domain but rewrites xenstore etc to make it unique so
408 * that the domain can be restarted.
409 *
410 * Does not modify info so that it may be reused.
411 */
412 int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid,
413 libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid)
414 {
415 struct xs_permissions roperm[2];
416 xs_transaction_t t;
417 char *preserved_name;
418 char *uuid_string;
419 char *vm_path;
420 char *dom_path;
422 int rc;
424 preserved_name = libxl_sprintf(ctx, "%s%s", info->name, name_suffix);
425 if (!preserved_name) return ERROR_NOMEM;
427 uuid_string = libxl_uuid2string(ctx, new_uuid);
428 if (!uuid_string) return ERROR_NOMEM;
430 dom_path = libxl_xs_get_dompath(ctx, domid);
431 if (!dom_path) return ERROR_FAIL;
433 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
434 if (!vm_path) return ERROR_FAIL;
436 roperm[0].id = 0;
437 roperm[0].perms = XS_PERM_NONE;
438 roperm[1].id = domid;
439 roperm[1].perms = XS_PERM_READ;
441 retry_transaction:
442 t = xs_transaction_start(ctx->xsh);
444 xs_rm(ctx->xsh, t, vm_path);
445 xs_mkdir(ctx->xsh, t, vm_path);
446 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
448 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
449 rc = libxl_domain_rename(ctx, domid, info->name, preserved_name, t);
450 if (rc) return rc;
452 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
454 if (!xs_transaction_end(ctx->xsh, t, 0))
455 if (errno == EAGAIN)
456 goto retry_transaction;
458 return 0;
459 }
461 static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
462 libxl_dominfo *xlinfo)
463 {
464 memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
465 xlinfo->domid = xcinfo->domain;
467 xlinfo->dying = !!(xcinfo->flags&XEN_DOMINF_dying);
468 xlinfo->shutdown = !!(xcinfo->flags&XEN_DOMINF_shutdown);
469 xlinfo->paused = !!(xcinfo->flags&XEN_DOMINF_paused);
470 xlinfo->blocked = !!(xcinfo->flags&XEN_DOMINF_blocked);
471 xlinfo->running = !!(xcinfo->flags&XEN_DOMINF_running);
473 if (xlinfo->shutdown || xlinfo->dying)
474 xlinfo->shutdown_reason = (xcinfo->flags>>XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask;
475 else
476 xlinfo->shutdown_reason = ~0;
478 xlinfo->max_memkb = PAGE_TO_MEMKB(xcinfo->tot_pages);
479 xlinfo->cpu_time = xcinfo->cpu_time;
480 xlinfo->vcpu_max_id = xcinfo->max_vcpu_id;
481 xlinfo->vcpu_online = xcinfo->nr_online_vcpus;
482 }
484 libxl_dominfo * libxl_list_domain(libxl_ctx *ctx, int *nb_domain)
485 {
486 libxl_dominfo *ptr;
487 int i, ret;
488 xc_domaininfo_t info[1024];
489 int size = 1024;
491 ptr = calloc(size, sizeof(libxl_dominfo));
492 if (!ptr) {
493 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "allocating domain info");
494 return NULL;
495 }
497 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
498 if (ret<0) {
499 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "geting domain info list");
500 return NULL;
501 }
503 for (i = 0; i < ret; i++) {
504 xcinfo2xlinfo(&info[i], &ptr[i]);
505 }
506 *nb_domain = ret;
507 return ptr;
508 }
510 int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
511 uint32_t domid) {
512 xc_domaininfo_t xcinfo;
513 int ret;
515 ret = xc_domain_getinfolist(ctx->xch, domid, 1, &xcinfo);
516 if (ret<0) {
517 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "geting domain info list");
518 return ERROR_FAIL;
519 }
520 if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
522 xcinfo2xlinfo(&xcinfo, info_r);
523 return 0;
524 }
526 libxl_poolinfo * libxl_list_pool(libxl_ctx *ctx, int *nb_pool)
527 {
528 libxl_poolinfo *ptr;
529 int i, ret;
530 xc_cpupoolinfo_t info[256];
531 int size = 256;
533 ptr = calloc(size, sizeof(libxl_poolinfo));
534 if (!ptr) {
535 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "allocating cpupool info");
536 return NULL;
537 }
539 ret = xc_cpupool_getinfo(ctx->xch, 0, 256, info);
540 if (ret<0) {
541 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting cpupool info");
542 return NULL;
543 }
545 for (i = 0; i < ret; i++) {
546 ptr[i].poolid = info[i].cpupool_id;
547 }
548 *nb_pool = ret;
549 return ptr;
550 }
552 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
553 libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm)
554 {
555 libxl_vminfo *ptr;
556 int index, i, ret;
557 xc_domaininfo_t info[1024];
558 int size = 1024;
560 ptr = calloc(size, sizeof(libxl_dominfo));
561 if (!ptr)
562 return NULL;
564 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
565 if (ret<0) {
566 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "geting domain info list");
567 return NULL;
568 }
569 for (index = i = 0; i < ret; i++) {
570 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
571 continue;
572 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
573 ptr[index].domid = info[i].domain;
575 index++;
576 }
577 *nb_vm = index;
578 return ptr;
579 }
581 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
582 uint32_t domid, int fd)
583 {
584 int hvm = is_hvm(ctx, domid);
585 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
586 int debug = info != NULL && info->flags & XL_SUSPEND_DEBUG;
588 core_suspend(ctx, domid, fd, hvm, live, debug);
589 if (hvm)
590 return save_device_model(ctx, domid, fd);
591 return 0;
592 }
594 int libxl_domain_pause(libxl_ctx *ctx, uint32_t domid)
595 {
596 int ret;
597 ret = xc_domain_pause(ctx->xch, domid);
598 if (ret<0) {
599 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "pausing domain %d", domid);
600 return ERROR_FAIL;
601 }
602 return 0;
603 }
605 int libxl_domain_core_dump(libxl_ctx *ctx, uint32_t domid,
606 const char *filename)
607 {
608 int ret;
609 ret = xc_domain_dumpcore(ctx->xch, domid, filename);
610 if (ret<0) {
611 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "core dumping domain %d to %s",
612 domid, filename);
613 return ERROR_FAIL;
614 }
615 return 0;
616 }
618 int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
619 {
620 char *path;
621 char *state;
622 int ret;
624 if (is_hvm(ctx, domid)) {
625 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
626 state = libxl_xs_read(ctx, XBT_NULL, path);
627 if (state != NULL && !strcmp(state, "paused")) {
628 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue");
629 libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL);
630 }
631 }
632 ret = xc_domain_unpause(ctx->xch, domid);
633 if (ret<0) {
634 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unpausing domain %d", domid);
635 return ERROR_FAIL;
636 }
637 return 0;
638 }
640 static char *req_table[] = {
641 [0] = "poweroff",
642 [1] = "reboot",
643 [2] = "suspend",
644 [3] = "crash",
645 [4] = "halt",
646 };
648 int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
649 {
650 char *shutdown_path;
651 char *dom_path;
653 if (req > ARRAY_SIZE(req_table))
654 return ERROR_INVAL;
656 dom_path = libxl_xs_get_dompath(ctx, domid);
657 if (!dom_path)
658 return ERROR_FAIL;
660 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
662 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
663 if (is_hvm(ctx,domid)) {
664 unsigned long acpi_s_state = 0;
665 unsigned long pvdriver = 0;
666 int ret;
667 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
668 if (ret<0) {
669 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting ACPI S-state");
670 return ERROR_FAIL;
671 }
672 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
673 if (ret<0) {
674 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting HVM callback IRQ");
675 return ERROR_FAIL;
676 }
677 if (!pvdriver || acpi_s_state != 0) {
678 ret = xc_domain_shutdown(ctx->xch, domid, req);
679 if (ret<0) {
680 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unpausing domain");
681 return ERROR_FAIL;
682 }
683 }
684 }
685 return 0;
686 }
688 int libxl_get_wait_fd(libxl_ctx *ctx, int *fd)
689 {
690 *fd = xs_fileno(ctx->xsh);
691 return 0;
692 }
694 int libxl_wait_for_domain_death(libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
695 {
696 waiter->path = strdup("@releaseDomain");
697 if (asprintf(&(waiter->token), "%d", LIBXL_EVENT_DOMAIN_DEATH) < 0)
698 return -1;
699 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
700 return -1;
701 return 0;
702 }
704 int libxl_wait_for_disk_ejects(libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
705 {
706 int i;
707 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
709 if (!domid)
710 domid = guest_domid;
712 for (i = 0; i < num_disks; i++) {
713 if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
714 libxl_xs_get_dompath(ctx, domid),
715 device_disk_dev_number(disks[i].virtpath)) < 0)
716 return -1;
717 if (asprintf(&(waiter[i].token), "%d", LIBXL_EVENT_DISK_EJECT) < 0)
718 return -1;
719 xs_watch(ctx->xsh, waiter->path, waiter->token);
720 }
721 return 0;
722 }
724 int libxl_get_event(libxl_ctx *ctx, libxl_event *event)
725 {
726 unsigned int num;
727 char **events = xs_read_watch(ctx->xsh, &num);
728 if (num != 2) {
729 free(events);
730 return ERROR_FAIL;
731 }
732 event->path = strdup(events[XS_WATCH_PATH]);
733 event->token = strdup(events[XS_WATCH_TOKEN]);
734 event->type = atoi(event->token);
735 free(events);
736 return 0;
737 }
739 int libxl_stop_waiting(libxl_ctx *ctx, libxl_waiter *waiter)
740 {
741 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
742 return ERROR_FAIL;
743 else
744 return 0;
745 }
747 int libxl_free_event(libxl_event *event)
748 {
749 free(event->path);
750 free(event->token);
751 return 0;
752 }
754 int libxl_free_waiter(libxl_waiter *waiter)
755 {
756 free(waiter->path);
757 free(waiter->token);
758 return 0;
759 }
761 int libxl_event_get_domain_death_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_dominfo *info)
762 {
763 if (libxl_domain_info(ctx, info, domid) < 0)
764 return 0;
766 if (info->running || (!info->shutdown && !info->dying))
767 return ERROR_INVAL;
769 return 1;
770 }
772 int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
773 {
774 char *path;
775 char *backend;
776 char *value;
778 value = libxl_xs_read(ctx, XBT_NULL, event->path);
780 if (!value || strcmp(value, "eject"))
781 return 0;
783 path = strdup(event->path);
784 path[strlen(path) - 6] = '\0';
785 backend = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", path));
787 disk->backend_domid = 0;
788 disk->domid = domid;
789 disk->physpath = NULL;
790 disk->phystype = 0;
791 /* this value is returned to the user: do not free right away */
792 disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", backend));
793 disk->unpluggable = 1;
794 disk->readwrite = 0;
795 disk->is_cdrom = 1;
797 free(path);
798 return 1;
799 }
801 static int libxl_destroy_device_model(libxl_ctx *ctx, uint32_t domid)
802 {
803 char *pid;
804 int ret;
806 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
807 if (!pid) {
808 int stubdomid = libxl_get_stubdom_id(ctx, domid);
809 if (!stubdomid) {
810 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
811 return ERROR_INVAL;
812 }
813 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
814 return libxl_domain_destroy(ctx, stubdomid, 0);
815 }
816 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
818 ret = kill(atoi(pid), SIGHUP);
819 if (ret < 0 && errno == ESRCH) {
820 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
821 ret = 0;
822 } else if (ret == 0) {
823 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
824 ret = 0;
825 } else {
826 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
827 atoi(pid));
828 }
829 return ret;
830 }
832 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
833 {
834 char *dom_path;
835 char *vm_path;
836 int rc, dm_present;
838 if (is_hvm(ctx, domid)) {
839 dm_present = 1;
840 } else {
841 char *pid;
842 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
843 dm_present = (pid != NULL);
844 libxl_free(ctx, pid);
845 }
847 dom_path = libxl_xs_get_dompath(ctx, domid);
848 if (!dom_path)
849 return ERROR_FAIL;
851 if (libxl_device_pci_shutdown(ctx, domid) < 0)
852 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
853 if (dm_present) {
854 xs_write(ctx->xsh, XBT_NULL,
855 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
856 "shutdown", strlen("shutdown"));
857 }
858 rc = xc_domain_pause(ctx->xch, domid);
859 if (rc < 0) {
860 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
861 }
862 if (dm_present) {
863 if (libxl_destroy_device_model(ctx, domid) < 0)
864 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
865 }
866 if (libxl_devices_destroy(ctx, domid, force) < 0)
867 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
869 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vm", dom_path));
870 if (vm_path)
871 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
872 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
874 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
875 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
877 libxl__userdata_destroyall(ctx, domid);
879 rc = xc_domain_destroy(ctx->xch, domid);
880 if (rc < 0) {
881 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
882 return ERROR_FAIL;
883 }
884 return 0;
885 }
887 int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num)
888 {
889 char *p = libxl_sprintf(ctx, "%s/xenconsole", libxl_private_bindir_path());
890 char *domid_s = libxl_sprintf(ctx, "%d", domid);
891 char *cons_num_s = libxl_sprintf(ctx, "%d", cons_num);
892 return execl(p, p, domid_s, "--num", cons_num_s, (void *)NULL) == 0 ? 0 : ERROR_FAIL;
893 }
895 int libxl_primary_console_exec(libxl_ctx *ctx, uint32_t domid_vm)
896 {
897 uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm);
898 if (stubdomid)
899 return libxl_console_exec(ctx, stubdomid, 1);
900 else
901 return libxl_console_exec(ctx, domid_vm, 0);
902 }
904 int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
905 {
906 const char *vnc_port, *vfb_back;
907 const char *vnc_listen = NULL, *vnc_pass = NULL;
908 int port = 0, autopass_fd = -1;
909 char *vnc_bin, *args[] = {
910 "vncviewer",
911 NULL, /* hostname:display */
912 NULL, /* -autopass */
913 NULL,
914 };
916 vnc_port = libxl_xs_read(ctx, XBT_NULL,
917 libxl_sprintf(ctx,
918 "/local/domain/%d/console/vnc-port", domid));
919 if ( vnc_port )
920 port = atoi(vnc_port) - 5900;
922 vfb_back = libxl_xs_read(ctx, XBT_NULL,
923 libxl_sprintf(ctx,
924 "/local/domain/%d/device/vfb/0/backend", domid));
925 if ( vfb_back ) {
926 vnc_listen = libxl_xs_read(ctx, XBT_NULL,
927 libxl_sprintf(ctx,
928 "/local/domain/%d/console/vnc-listen", domid));
929 if ( autopass )
930 vnc_pass = libxl_xs_read(ctx, XBT_NULL,
931 libxl_sprintf(ctx,
932 "/local/domain/%d/console/vnc-pass", domid));
933 }
935 if ( NULL == vnc_listen )
936 vnc_listen = "localhost";
938 if ( (vnc_bin = getenv("VNCVIEWER")) )
939 args[0] = vnc_bin;
941 args[1] = libxl_sprintf(ctx, "%s:%d", vnc_listen, port);
943 if ( vnc_pass ) {
944 char tmpname[] = "/tmp/vncautopass.XXXXXX";
945 autopass_fd = mkstemp(tmpname);
946 if ( autopass_fd < 0 )
947 goto skip_autopass;
949 if ( unlink(tmpname) )
950 /* should never happen */
951 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "unlink %s failed", tmpname);
953 if ( libxl_write_exactly(ctx, autopass_fd, vnc_pass, strlen(vnc_pass),
954 tmpname, "vnc password") ) {
955 do { close(autopass_fd); } while(errno == EINTR);
956 goto skip_autopass;
957 }
959 args[2] = "-autopass";
960 }
962 skip_autopass:
963 libxl_exec(autopass_fd, -1, -1, args[0], args);
964 return 0;
965 }
967 static char ** libxl_build_device_model_args_old(libxl_ctx *ctx,
968 libxl_device_model_info *info,
969 libxl_device_nic *vifs,
970 int num_vifs)
971 {
972 int num = 0, i;
973 flexarray_t *dm_args;
974 dm_args = flexarray_make(16, 1);
975 if (!dm_args)
976 return NULL;
978 flexarray_set(dm_args, num++, "qemu-dm");
979 flexarray_set(dm_args, num++, "-d");
981 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
983 if (info->dom_name) {
984 flexarray_set(dm_args, num++, "-domain-name");
985 flexarray_set(dm_args, num++, info->dom_name);
986 }
987 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
988 flexarray_set(dm_args, num++, "-vnc");
989 if (info->vncdisplay) {
990 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
991 flexarray_set(
992 dm_args, num++,
993 libxl_sprintf(ctx, "%s:%d%s",
994 info->vnclisten,
995 info->vncdisplay,
996 info->vncpasswd ? ",password" : ""));
997 } else {
998 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
999 }
1000 } else if (info->vnclisten) {
1001 if (strchr(info->vnclisten, ':') != NULL) {
1002 flexarray_set(dm_args, num++, info->vnclisten);
1003 } else {
1004 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
1006 } else {
1007 flexarray_set(dm_args, num++, "127.0.0.1:0");
1009 if (info->vncunused) {
1010 flexarray_set(dm_args, num++, "-vncunused");
1013 if (info->sdl) {
1014 flexarray_set(dm_args, num++, "-sdl");
1015 if (info->opengl) {
1016 flexarray_set(dm_args, num++, "-disable-opengl");
1019 if (info->keymap) {
1020 flexarray_set(dm_args, num++, "-k");
1021 flexarray_set(dm_args, num++, info->keymap);
1023 if (info->nographic && (!info->sdl && !info->vnc)) {
1024 flexarray_set(dm_args, num++, "-nographic");
1026 if (info->serial) {
1027 flexarray_set(dm_args, num++, "-serial");
1028 flexarray_set(dm_args, num++, info->serial);
1030 if (info->type == XENFV) {
1031 int ioemu_vifs = 0;
1033 if (info->videoram) {
1034 flexarray_set(dm_args, num++, "-videoram");
1035 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
1037 if (info->stdvga) {
1038 flexarray_set(dm_args, num++, "-std-vga");
1041 if (info->boot) {
1042 flexarray_set(dm_args, num++, "-boot");
1043 flexarray_set(dm_args, num++, info->boot);
1045 if (info->usb || info->usbdevice) {
1046 flexarray_set(dm_args, num++, "-usb");
1047 if (info->usbdevice) {
1048 flexarray_set(dm_args, num++, "-usbdevice");
1049 flexarray_set(dm_args, num++, info->usbdevice);
1052 if (info->soundhw) {
1053 flexarray_set(dm_args, num++, "-soundhw");
1054 flexarray_set(dm_args, num++, info->soundhw);
1056 if (info->apic) {
1057 flexarray_set(dm_args, num++, "-acpi");
1059 if (info->vcpus > 1) {
1060 flexarray_set(dm_args, num++, "-vcpus");
1061 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->vcpus));
1063 if (info->vcpu_avail) {
1064 flexarray_set(dm_args, num++, "-vcpu_avail");
1065 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "0x%x", info->vcpu_avail));
1067 for (i = 0; i < num_vifs; i++) {
1068 if (vifs[i].nictype == NICTYPE_IOEMU) {
1069 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1070 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
1071 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
1072 if (!vifs[i].ifname)
1073 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid);
1074 flexarray_set(dm_args, num++, "-net");
1075 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
1076 vifs[i].devid, smac, vifs[i].model));
1077 flexarray_set(dm_args, num++, "-net");
1078 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s,script=no",
1079 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
1080 ioemu_vifs++;
1083 /* If we have no emulated nics, tell qemu not to create any */
1084 if ( ioemu_vifs == 0 ) {
1085 flexarray_set(dm_args, num++, "-net");
1086 flexarray_set(dm_args, num++, "none");
1089 if (info->saved_state) {
1090 flexarray_set(dm_args, num++, "-loadvm");
1091 flexarray_set(dm_args, num++, info->saved_state);
1093 for (i = 0; info->extra && info->extra[i] != NULL; i++)
1094 flexarray_set(dm_args, num++, info->extra[i]);
1095 flexarray_set(dm_args, num++, "-M");
1096 if (info->type == XENPV)
1097 flexarray_set(dm_args, num++, "xenpv");
1098 else
1099 flexarray_set(dm_args, num++, "xenfv");
1100 flexarray_set(dm_args, num++, NULL);
1101 return (char **) flexarray_contents(dm_args);
1104 static char ** libxl_build_device_model_args_new(libxl_ctx *ctx,
1105 libxl_device_model_info *info,
1106 libxl_device_nic *vifs,
1107 int num_vifs)
1109 int num = 0, i;
1110 flexarray_t *dm_args;
1111 int nb;
1112 libxl_device_disk *disks;
1114 dm_args = flexarray_make(16, 1);
1115 if (!dm_args)
1116 return NULL;
1118 flexarray_set(dm_args, num++, "qemu-system-xen");
1119 flexarray_set(dm_args, num++, "-xen-domid");
1121 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
1123 if (info->dom_name) {
1124 flexarray_set(dm_args, num++, "-name");
1125 flexarray_set(dm_args, num++, info->dom_name);
1127 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
1128 int display = 0;
1129 const char *listen = "127.0.0.1";
1131 flexarray_set(dm_args, num++, "-vnc");
1133 if (info->vncdisplay) {
1134 display = info->vncdisplay;
1135 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
1136 listen = info->vnclisten;
1138 } else if (info->vnclisten) {
1139 listen = info->vnclisten;
1142 if (strchr(listen, ':') != NULL)
1143 flexarray_set(dm_args, num++,
1144 libxl_sprintf(ctx, "%s%s", listen,
1145 info->vncunused ? ",to=99" : ""));
1146 else
1147 flexarray_set(dm_args, num++,
1148 libxl_sprintf(ctx, "%s:%d%s", listen, display,
1149 info->vncunused ? ",to=99" : ""));
1151 if (info->sdl) {
1152 flexarray_set(dm_args, num++, "-sdl");
1154 if (info->keymap) {
1155 flexarray_set(dm_args, num++, "-k");
1156 flexarray_set(dm_args, num++, info->keymap);
1158 if (info->nographic && (!info->sdl && !info->vnc)) {
1159 flexarray_set(dm_args, num++, "-nographic");
1161 if (info->serial) {
1162 flexarray_set(dm_args, num++, "-serial");
1163 flexarray_set(dm_args, num++, info->serial);
1165 if (info->type == XENFV) {
1166 int ioemu_vifs = 0;
1168 if (info->stdvga) {
1169 flexarray_set(dm_args, num++, "-vga");
1170 flexarray_set(dm_args, num++, "std");
1173 if (info->boot) {
1174 flexarray_set(dm_args, num++, "-boot");
1175 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "order=%s", info->boot));
1177 if (info->usb || info->usbdevice) {
1178 flexarray_set(dm_args, num++, "-usb");
1179 if (info->usbdevice) {
1180 flexarray_set(dm_args, num++, "-usbdevice");
1181 flexarray_set(dm_args, num++, info->usbdevice);
1184 if (info->soundhw) {
1185 flexarray_set(dm_args, num++, "-soundhw");
1186 flexarray_set(dm_args, num++, info->soundhw);
1188 if (!info->apic) {
1189 flexarray_set(dm_args, num++, "-no-acpi");
1191 if (info->vcpus > 1) {
1192 flexarray_set(dm_args, num++, "-smp");
1193 if (info->vcpu_avail)
1194 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail));
1195 else
1196 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->vcpus));
1198 for (i = 0; i < num_vifs; i++) {
1199 if (vifs[i].nictype == NICTYPE_IOEMU) {
1200 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1201 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
1202 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
1203 if (!vifs[i].ifname)
1204 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid);
1205 flexarray_set(dm_args, num++, "-net");
1206 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
1207 vifs[i].devid, smac, vifs[i].model));
1208 flexarray_set(dm_args, num++, "-net");
1209 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,script=no",
1210 vifs[i].devid, vifs[i].ifname));
1211 ioemu_vifs++;
1214 /* If we have no emulated nics, tell qemu not to create any */
1215 if ( ioemu_vifs == 0 ) {
1216 flexarray_set(dm_args, num++, "-net");
1217 flexarray_set(dm_args, num++, "none");
1220 if (info->saved_state) {
1221 flexarray_set(dm_args, num++, "-loadvm");
1222 flexarray_set(dm_args, num++, info->saved_state);
1224 for (i = 0; info->extra && info->extra[i] != NULL; i++)
1225 flexarray_set(dm_args, num++, info->extra[i]);
1226 flexarray_set(dm_args, num++, "-M");
1227 if (info->type == XENPV)
1228 flexarray_set(dm_args, num++, "xenpv");
1229 else
1230 flexarray_set(dm_args, num++, "xenfv");
1232 disks = libxl_device_disk_list(ctx, info->domid, &nb);
1233 for (; nb > 0; --nb, ++disks) {
1234 if ( disks->is_cdrom ) {
1235 flexarray_set(dm_args, num++, "-cdrom");
1236 flexarray_set(dm_args, num++, disks->physpath);
1237 }else{
1238 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "-%s", disks->virtpath));
1239 flexarray_set(dm_args, num++, disks->physpath);
1243 flexarray_set(dm_args, num++, NULL);
1244 return (char **) flexarray_contents(dm_args);
1247 static char ** libxl_build_device_model_args(libxl_ctx *ctx,
1248 libxl_device_model_info *info,
1249 libxl_device_nic *vifs,
1250 int num_vifs)
1252 int new_qemu;
1254 new_qemu = libxl_check_device_model_version(ctx, info->device_model);
1256 if (new_qemu == 1) {
1257 return libxl_build_device_model_args_new(ctx, info, vifs, num_vifs);
1258 } else {
1259 return libxl_build_device_model_args_old(ctx, info, vifs, num_vifs);
1263 void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
1265 libxl_device_model_starting *starting = for_spawn;
1266 char *kvs[3];
1267 int rc;
1268 struct xs_handle *xsh;
1270 xsh = xs_daemon_open();
1271 /* we mustn't use the parent's handle in the child */
1273 kvs[0] = "image/device-model-pid";
1274 if (asprintf(&kvs[1], "%d", innerchild) < 0)
1275 return;
1276 kvs[2] = NULL;
1278 rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
1279 if (rc)
1280 return;
1281 xs_daemon_close(xsh);
1284 static int libxl_vfb_and_vkb_from_device_model_info(libxl_ctx *ctx,
1285 libxl_device_model_info *info,
1286 libxl_device_vfb *vfb,
1287 libxl_device_vkb *vkb)
1289 memset(vfb, 0x00, sizeof(libxl_device_vfb));
1290 memset(vkb, 0x00, sizeof(libxl_device_vkb));
1292 vfb->backend_domid = 0;
1293 vfb->devid = 0;
1294 vfb->vnc = info->vnc;
1295 vfb->vnclisten = info->vnclisten;
1296 vfb->vncdisplay = info->vncdisplay;
1297 vfb->vncunused = info->vncunused;
1298 vfb->vncpasswd = info->vncpasswd;
1299 vfb->keymap = info->keymap;
1300 vfb->sdl = info->sdl;
1301 vfb->opengl = info->opengl;
1303 vkb->backend_domid = 0;
1304 vkb->devid = 0;
1305 return 0;
1308 static int libxl_write_dmargs(libxl_ctx *ctx, int domid, int guest_domid, char **args)
1310 int i;
1311 char *vm_path;
1312 char *dmargs, *path;
1313 int dmargs_size;
1314 struct xs_permissions roperm[2];
1315 xs_transaction_t t;
1317 roperm[0].id = 0;
1318 roperm[0].perms = XS_PERM_NONE;
1319 roperm[1].id = domid;
1320 roperm[1].perms = XS_PERM_READ;
1322 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
1324 i = 0;
1325 dmargs_size = 0;
1326 while (args[i] != NULL) {
1327 dmargs_size = dmargs_size + strlen(args[i]) + 1;
1328 i++;
1330 dmargs_size++;
1331 dmargs = (char *) malloc(dmargs_size);
1332 i = 1;
1333 dmargs[0] = '\0';
1334 while (args[i] != NULL) {
1335 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
1336 strcat(dmargs, " ");
1337 strcat(dmargs, args[i]);
1339 i++;
1341 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
1343 retry_transaction:
1344 t = xs_transaction_start(ctx->xsh);
1345 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
1346 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
1347 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
1348 if (!xs_transaction_end(ctx->xsh, t, 0))
1349 if (errno == EAGAIN)
1350 goto retry_transaction;
1351 free(dmargs);
1352 return 0;
1355 static int libxl_create_stubdom(libxl_ctx *ctx,
1356 libxl_device_model_info *info,
1357 libxl_device_disk *disks, int num_disks,
1358 libxl_device_nic *vifs, int num_vifs,
1359 libxl_device_vfb *vfb,
1360 libxl_device_vkb *vkb,
1361 libxl_device_model_starting **starting_r)
1363 int i, num_console = 1, ret;
1364 libxl_device_console *console;
1365 libxl_domain_create_info c_info;
1366 libxl_domain_build_info b_info;
1367 libxl_domain_build_state state;
1368 uint32_t domid;
1369 char **args;
1370 struct xs_permissions perm[2];
1371 xs_transaction_t t;
1372 libxl_device_model_starting *dm_starting = 0;
1374 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
1375 if (!args)
1376 return ERROR_FAIL;
1378 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
1379 c_info.hvm = 0;
1380 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
1381 for (i = 0; i < 16; i++)
1382 c_info.uuid[i] = info->uuid[i];
1384 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
1385 b_info.max_vcpus = 1;
1386 b_info.max_memkb = 32 * 1024;
1387 b_info.target_memkb = b_info.max_memkb;
1388 b_info.kernel.path = libxl_abs_path(ctx, "ioemu-stubdom.gz", libxl_xenfirmwaredir_path());
1389 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
1390 b_info.u.pv.ramdisk.path = "";
1391 b_info.u.pv.features = "";
1392 b_info.hvm = 0;
1394 ret = libxl_domain_make(ctx, &c_info, &domid);
1395 if (ret) return ret;
1396 ret = libxl_domain_build(ctx, &b_info, domid, &state);
1397 if (ret) return ret;
1399 libxl_write_dmargs(ctx, domid, info->domid, args);
1400 libxl_xs_write(ctx, XBT_NULL,
1401 libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)),
1402 "%d", domid);
1403 libxl_xs_write(ctx, XBT_NULL,
1404 libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)),
1405 "%d", info->domid);
1406 ret = xc_domain_set_target(ctx->xch, domid, info->domid);
1407 if (ret<0) {
1408 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting target domain %d -> %d", domid, info->domid);
1409 return ERROR_FAIL;
1411 xs_set_target(ctx->xsh, domid, info->domid);
1413 perm[0].id = domid;
1414 perm[0].perms = XS_PERM_NONE;
1415 perm[1].id = info->domid;
1416 perm[1].perms = XS_PERM_READ;
1417 retry_transaction:
1418 t = xs_transaction_start(ctx->xsh);
1419 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
1420 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
1421 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
1422 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
1423 if (!xs_transaction_end(ctx->xsh, t, 0))
1424 if (errno == EAGAIN)
1425 goto retry_transaction;
1427 for (i = 0; i < num_disks; i++) {
1428 disks[i].domid = domid;
1429 ret = libxl_device_disk_add(ctx, domid, &disks[i]);
1430 if (ret) return ret;
1432 for (i = 0; i < num_vifs; i++) {
1433 vifs[i].domid = domid;
1434 ret = libxl_device_nic_add(ctx, domid, &vifs[i]);
1435 if (ret) return ret;
1437 vfb->domid = domid;
1438 ret = libxl_device_vfb_add(ctx, domid, vfb);
1439 if (ret) return ret;
1440 vkb->domid = domid;
1441 ret = libxl_device_vkb_add(ctx, domid, vkb);
1442 if (ret) return ret;
1444 if (info->serial)
1445 num_console++;
1447 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
1448 if (!console)
1449 return ERROR_NOMEM;
1451 for (i = 0; i < num_console; i++) {
1452 console[i].devid = i;
1453 console[i].constype = CONSTYPE_IOEMU;
1454 console[i].domid = domid;
1455 if (!i)
1456 console[i].build_state = &state;
1457 ret = libxl_device_console_add(ctx, domid, &console[i]);
1458 if (ret) return ret;
1460 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
1461 free(args);
1462 return ERROR_FAIL;
1464 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
1465 free(args);
1466 return ERROR_FAIL;
1469 libxl_domain_unpause(ctx, domid);
1471 if (starting_r) {
1472 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
1473 (*starting_r)->domid = info->domid;
1474 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
1475 (*starting_r)->for_spawn = NULL;
1478 free(args);
1479 return 0;
1482 int libxl_create_device_model(libxl_ctx *ctx,
1483 libxl_device_model_info *info,
1484 libxl_device_disk *disks, int num_disks,
1485 libxl_device_nic *vifs, int num_vifs,
1486 libxl_device_model_starting **starting_r)
1488 char *path, *logfile;
1489 int logfile_w, null;
1490 int rc;
1491 char **args;
1492 libxl_device_model_starting buf_starting, *p;
1493 xs_transaction_t t;
1494 char *vm_path;
1495 char **pass_stuff;
1497 if (strstr(info->device_model, "stubdom-dm")) {
1498 libxl_device_vfb vfb;
1499 libxl_device_vkb vkb;
1501 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
1502 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
1505 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
1506 if (!args)
1507 return ERROR_FAIL;
1509 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
1510 xs_mkdir(ctx->xsh, XBT_NULL, path);
1511 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/disable_pf", path), "%d", !info->xen_platform_pci);
1513 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
1514 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
1515 free(logfile);
1516 null = open("/dev/null", O_RDONLY);
1518 if (starting_r) {
1519 rc = ERROR_NOMEM;
1520 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
1521 if (!*starting_r) goto xit;
1522 p = *starting_r;
1523 p->for_spawn = libxl_calloc(ctx, sizeof(libxl_spawn_starting), 1);
1524 } else {
1525 p = &buf_starting;
1526 p->for_spawn = NULL;
1529 p->domid = info->domid;
1530 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
1531 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
1533 if (info->vncpasswd) {
1534 retry_transaction:
1535 /* Find uuid and the write the vnc password to xenstore for qemu. */
1536 t = xs_transaction_start(ctx->xsh);
1537 vm_path = libxl_xs_read(ctx,t,libxl_sprintf(ctx, "%s/vm", p->dom_path));
1538 if (vm_path) {
1539 /* Now write the vncpassword into it. */
1540 pass_stuff = libxl_calloc(ctx, 2, sizeof(char *));
1541 pass_stuff[0] = "vncpasswd";
1542 pass_stuff[1] = info->vncpasswd;
1543 libxl_xs_writev(ctx,t,vm_path,pass_stuff);
1544 if (!xs_transaction_end(ctx->xsh, t, 0))
1545 if (errno == EAGAIN)
1546 goto retry_transaction;
1550 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
1551 if (rc < 0) goto xit;
1552 if (!rc) { /* inner child */
1553 libxl_exec(null, logfile_w, logfile_w,
1554 libxl_abs_path(ctx, info->device_model, libxl_private_bindir_path()),
1555 args);
1558 rc = 0;
1559 xit:
1560 free(args);
1561 close(null);
1562 close(logfile_w);
1564 return rc;
1567 int libxl_detach_device_model(libxl_ctx *ctx,
1568 libxl_device_model_starting *starting)
1570 int rc;
1571 rc = libxl_spawn_detach(ctx, starting->for_spawn);
1572 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
1573 libxl_free(ctx, starting);
1574 return rc;
1578 int libxl_confirm_device_model_startup(libxl_ctx *ctx,
1579 libxl_device_model_starting *starting)
1581 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running", NULL, NULL);
1582 int detach;
1583 if ( !problem )
1584 problem = libxl_spawn_check(ctx, starting->for_spawn);
1585 detach = libxl_detach_device_model(ctx, starting);
1586 return problem ? problem : detach;
1590 /******************************************************************************/
1592 static char *get_blktap2_device(libxl_ctx *ctx,
1593 const char *name, const char *type)
1595 int minor = tap_ctl_find_minor(type, name);
1596 if (minor < 0)
1597 return NULL;
1598 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", minor);
1601 static char *make_blktap2_device(libxl_ctx *ctx,
1602 const char *name, const char *type)
1604 char *params, *devname = NULL;
1605 int err;
1606 params = libxl_sprintf(ctx, "%s:%s", type, name);
1607 err = tap_ctl_create(params, &devname);
1608 if (!err)
1609 libxl_ptr_add(ctx, devname);
1610 return err ? NULL : devname;
1613 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1615 flexarray_t *front;
1616 flexarray_t *back;
1617 char *backend_type;
1618 unsigned int boffset = 0;
1619 unsigned int foffset = 0;
1620 int devid;
1621 libxl_device device;
1622 int major, minor;
1624 front = flexarray_make(16, 1);
1625 if (!front)
1626 return ERROR_NOMEM;
1627 back = flexarray_make(16, 1);
1628 if (!back) /* leaks front if error */
1629 return ERROR_NOMEM;
1631 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
1632 devid = device_disk_dev_number(disk->virtpath);
1633 if (devid==-1) {
1634 XL_LOG(ctx, XL_LOG_ERROR, "Invalid or unsupported"
1635 " virtual disk identifier %s", disk->virtpath);
1636 return ERROR_INVAL;
1639 device.backend_devid = devid;
1640 device.backend_domid = disk->backend_domid;
1641 device.devid = devid;
1642 device.domid = disk->domid;
1643 device.kind = DEVICE_VBD;
1645 switch (disk->phystype) {
1646 case PHYSTYPE_PHY: {
1648 device_physdisk_major_minor(disk->physpath, &major, &minor);
1649 flexarray_set(back, boffset++, "physical-device");
1650 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1652 flexarray_set(back, boffset++, "params");
1653 flexarray_set(back, boffset++, disk->physpath);
1655 device.backend_kind = DEVICE_VBD;
1656 break;
1658 case PHYSTYPE_FILE:
1659 /* let's pretend is tap:aio for the moment */
1660 disk->phystype = PHYSTYPE_AIO;
1661 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD: {
1662 const char *msg;
1663 if (!tap_ctl_check(&msg)) {
1664 const char *type = device_disk_string_of_phystype(disk->phystype);
1665 char *dev;
1666 dev = get_blktap2_device(ctx, disk->physpath, type);
1667 if (!dev)
1668 dev = make_blktap2_device(ctx, disk->physpath, type);
1669 if (!dev)
1670 return ERROR_FAIL;
1671 flexarray_set(back, boffset++, "tapdisk-params");
1672 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
1673 flexarray_set(back, boffset++, "params");
1674 flexarray_set(back, boffset++, libxl_strdup(ctx, dev));
1675 backend_type = "phy";
1676 device_physdisk_major_minor(dev, &major, &minor);
1677 flexarray_set(back, boffset++, "physical-device");
1678 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1679 device.backend_kind = DEVICE_VBD;
1681 break;
1683 flexarray_set(back, boffset++, "params");
1684 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
1685 device_disk_string_of_phystype(disk->phystype), disk->physpath));
1687 device.backend_kind = DEVICE_TAP;
1688 break;
1690 default:
1691 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
1692 return ERROR_INVAL;
1695 flexarray_set(back, boffset++, "frontend-id");
1696 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
1697 flexarray_set(back, boffset++, "online");
1698 flexarray_set(back, boffset++, "1");
1699 flexarray_set(back, boffset++, "removable");
1700 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
1701 flexarray_set(back, boffset++, "bootable");
1702 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1703 flexarray_set(back, boffset++, "state");
1704 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1705 flexarray_set(back, boffset++, "dev");
1706 flexarray_set(back, boffset++, disk->virtpath);
1707 flexarray_set(back, boffset++, "type");
1708 flexarray_set(back, boffset++, backend_type);
1709 flexarray_set(back, boffset++, "mode");
1710 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1712 flexarray_set(front, foffset++, "backend-id");
1713 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
1714 flexarray_set(front, foffset++, "state");
1715 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1716 flexarray_set(front, foffset++, "virtual-device");
1717 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
1718 flexarray_set(front, foffset++, "device-type");
1719 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1721 if (0 /* protocol != native*/) {
1722 flexarray_set(front, foffset++, "protocol");
1723 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1726 libxl_device_generic_add(ctx, &device,
1727 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1728 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1729 flexarray_free(back);
1730 flexarray_free(front);
1731 return 0;
1734 int libxl_device_disk_del(libxl_ctx *ctx,
1735 libxl_device_disk *disk, int wait)
1737 libxl_device device;
1738 int devid;
1740 devid = device_disk_dev_number(disk->virtpath);
1741 device.backend_domid = disk->backend_domid;
1742 device.backend_devid = devid;
1743 device.backend_kind =
1744 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1745 device.domid = disk->domid;
1746 device.devid = devid;
1747 device.kind = DEVICE_VBD;
1748 return libxl_device_del(ctx, &device, wait);
1751 const char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
1753 char *dev = NULL;
1754 int phystype = disk->phystype;
1755 switch (phystype) {
1756 case PHYSTYPE_PHY: {
1757 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1758 dev = disk->physpath;
1759 break;
1761 case PHYSTYPE_FILE:
1762 /* let's pretend is tap:aio for the moment */
1763 phystype = PHYSTYPE_AIO;
1764 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD: {
1765 const char *msg;
1766 if (!tap_ctl_check(&msg)) {
1767 const char *type = device_disk_string_of_phystype(phystype);
1768 dev = get_blktap2_device(ctx, disk->physpath, type);
1769 if (!dev)
1770 dev = make_blktap2_device(ctx, disk->physpath, type);
1772 break;
1774 default:
1775 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1776 break;
1778 return dev;
1781 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1783 /* Nothing to do for PHYSTYPE_PHY. */
1785 /*
1786 * For other device types assume that the blktap2 process is
1787 * needed by the soon to be started domain and do nothing.
1788 */
1790 return 0;
1793 /******************************************************************************/
1794 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1796 flexarray_t *front;
1797 flexarray_t *back;
1798 unsigned int boffset = 0;
1799 unsigned int foffset = 0;
1800 libxl_device device;
1801 char *dompath, **l;
1802 unsigned int nb;
1804 front = flexarray_make(16, 1);
1805 if (!front)
1806 return ERROR_NOMEM;
1807 back = flexarray_make(16, 1);
1808 if (!back)
1809 return ERROR_NOMEM;
1811 if (nic->devid == -1) {
1812 if (!(dompath = libxl_xs_get_dompath(ctx, domid))) {
1813 return ERROR_FAIL;
1815 if (!(l = libxl_xs_directory(ctx, XBT_NULL,
1816 libxl_sprintf(ctx, "%s/device/vif", dompath), &nb))) {
1817 nic->devid = 0;
1818 } else {
1819 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1820 libxl_free(ctx, l);
1824 device.backend_devid = nic->devid;
1825 device.backend_domid = nic->backend_domid;
1826 device.backend_kind = DEVICE_VIF;
1827 device.devid = nic->devid;
1828 device.domid = nic->domid;
1829 device.kind = DEVICE_VIF;
1831 flexarray_set(back, boffset++, "frontend-id");
1832 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
1833 flexarray_set(back, boffset++, "online");
1834 flexarray_set(back, boffset++, "1");
1835 flexarray_set(back, boffset++, "state");
1836 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1837 flexarray_set(back, boffset++, "script");
1838 flexarray_set(back, boffset++, nic->script);
1839 flexarray_set(back, boffset++, "mac");
1840 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1841 nic->mac[0], nic->mac[1], nic->mac[2],
1842 nic->mac[3], nic->mac[4], nic->mac[5]));
1843 flexarray_set(back, boffset++, "bridge");
1844 flexarray_set(back, boffset++, libxl_strdup(ctx, nic->bridge));
1845 flexarray_set(back, boffset++, "handle");
1846 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
1848 flexarray_set(front, foffset++, "backend-id");
1849 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
1850 flexarray_set(front, foffset++, "state");
1851 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1852 flexarray_set(front, foffset++, "handle");
1853 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
1854 flexarray_set(front, foffset++, "mac");
1855 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1856 nic->mac[0], nic->mac[1], nic->mac[2],
1857 nic->mac[3], nic->mac[4], nic->mac[5]));
1858 if (0 /* protocol != native*/) {
1859 flexarray_set(front, foffset++, "protocol");
1860 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1863 libxl_device_generic_add(ctx, &device,
1864 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1865 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1867 /* FIXME: wait for plug */
1868 flexarray_free(back);
1869 flexarray_free(front);
1870 return 0;
1873 int libxl_device_nic_del(libxl_ctx *ctx,
1874 libxl_device_nic *nic, int wait)
1876 libxl_device device;
1878 device.backend_devid = nic->devid;
1879 device.backend_domid = nic->backend_domid;
1880 device.backend_kind = DEVICE_VIF;
1881 device.devid = nic->devid;
1882 device.domid = nic->domid;
1883 device.kind = DEVICE_VIF;
1885 return libxl_device_del(ctx, &device, wait);
1888 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1890 char *dompath, *nic_path_fe;
1891 char **l;
1892 char *val, *tok;
1893 unsigned int nb_nics, i;
1894 libxl_nicinfo *res, *nics;
1896 dompath = libxl_xs_get_dompath(ctx, domid);
1897 if (!dompath) {
1898 return NULL;
1900 l = libxl_xs_directory(ctx, XBT_NULL,
1901 libxl_sprintf(ctx, "%s/device/vif", dompath), &nb_nics);
1902 if (!l) {
1903 return NULL;
1905 res = libxl_calloc(ctx, nb_nics, sizeof (libxl_device_nic));
1906 if (!res) {
1907 libxl_free(ctx, l);
1908 return NULL;
1910 nics = res;
1911 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
1912 nic_path_fe = libxl_sprintf(ctx, "%s/device/vif/%s", dompath, *l);
1914 nics->backend = libxl_xs_read(ctx, XBT_NULL,
1915 libxl_sprintf(ctx, "%s/backend", nic_path_fe));
1916 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", nic_path_fe));
1917 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
1919 nics->devid = strtoul(*l, NULL, 10);
1920 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", nic_path_fe));
1921 nics->state = val ? strtoul(val, NULL, 10) : -1;
1922 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/mac", nic_path_fe));
1923 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1924 ++i, tok = strtok(NULL, ":")) {
1925 nics->mac[i] = strtoul(tok, NULL, 16);
1927 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/event-channel", nic_path_fe));
1928 nics->evtch = val ? strtol(val, NULL, 10) : -1;
1929 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/tx-ring-ref", nic_path_fe));
1930 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
1931 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/rx-ring-ref", nic_path_fe));
1932 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
1933 nics->frontend = libxl_xs_read(ctx, XBT_NULL,
1934 libxl_sprintf(ctx, "%s/frontend", nics->backend));
1935 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", nics->backend));
1936 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1937 nics->script = libxl_xs_read(ctx, XBT_NULL,
1938 libxl_sprintf(ctx, "%s/script", nics->backend));
1940 libxl_free(ctx, nic_path_fe);
1943 libxl_free(ctx, l);
1944 return res;
1947 /******************************************************************************/
1948 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
1950 flexarray_t *front, *back;
1951 unsigned int boffset = 0, foffset = 0;
1952 libxl_device device;
1953 char *dompath, *dom, **l;
1954 unsigned int nb;
1956 front = flexarray_make(16, 1);
1957 if (!front)
1958 return ERROR_NOMEM;
1959 back = flexarray_make(16, 1);
1960 if (!back)
1961 return ERROR_NOMEM;
1963 if (!(dompath = libxl_xs_get_dompath(ctx, domid))) {
1964 return ERROR_FAIL;
1966 dom = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/name", dompath));
1968 if (net2->devid == -1) {
1969 if (!(l = libxl_xs_directory(ctx, XBT_NULL,
1970 libxl_sprintf(ctx, "%s/device/vif2", dompath), &nb))) {
1971 net2->devid = 0;
1972 } else {
1973 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1974 libxl_free(ctx, l);
1978 device.backend_devid = net2->devid;
1979 device.backend_domid = net2->backend_domid;
1980 device.backend_kind = DEVICE_VIF2;
1981 device.devid = net2->devid;
1982 device.domid = net2->domid;
1983 device.kind = DEVICE_VIF2;
1985 flexarray_set(back, boffset++, "domain");
1986 flexarray_set(back, boffset++, dom);
1987 flexarray_set(back, boffset++, "frontend-id");
1988 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->domid));
1990 flexarray_set(back, boffset++, "local-trusted");
1991 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->back_trusted));
1992 flexarray_set(back, boffset++, "mac");
1993 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1994 net2->back_mac[0], net2->back_mac[1],
1995 net2->back_mac[2], net2->back_mac[3],
1996 net2->back_mac[4], net2->back_mac[5]));
1998 flexarray_set(back, boffset++, "remote-trusted");
1999 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->trusted));
2000 flexarray_set(back, boffset++, "remote-mac");
2001 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
2002 net2->front_mac[0], net2->front_mac[1],
2003 net2->front_mac[2], net2->front_mac[3],
2004 net2->front_mac[4], net2->front_mac[5]));
2006 flexarray_set(back, boffset++, "max-bypasses");
2007 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->max_bypasses));
2008 flexarray_set(back, boffset++, "filter-mac");
2009 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", !!(net2->filter_mac)));
2010 flexarray_set(back, boffset++, "handle");
2011 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->devid));
2012 flexarray_set(back, boffset++, "online");
2013 flexarray_set(back, boffset++, "1");
2014 flexarray_set(back, boffset++, "state");
2015 flexarray_set(back, boffset++, "1");
2017 flexarray_set(front, foffset++, "backend-id");
2018 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", net2->backend_domid));
2020 flexarray_set(front, foffset++, "local-trusted");
2021 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", net2->trusted));
2022 flexarray_set(front, foffset++, "mac");
2023 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
2024 net2->front_mac[0], net2->front_mac[1],
2025 net2->front_mac[2], net2->front_mac[3],
2026 net2->front_mac[4], net2->front_mac[5]));
2028 flexarray_set(front, foffset++, "remote-trusted");
2029 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", net2->back_trusted));
2030 flexarray_set(front, foffset++, "remote-mac");
2031 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
2032 net2->back_mac[0], net2->back_mac[1],
2033 net2->back_mac[2], net2->back_mac[3],
2034 net2->back_mac[4], net2->back_mac[5]));
2036 flexarray_set(front, foffset++, "filter-mac");
2037 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", !!(net2->filter_mac)));
2038 flexarray_set(front, foffset++, "state");
2039 flexarray_set(front, foffset++, "1");
2041 libxl_device_generic_add(ctx, &device,
2042 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
2043 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
2045 /* FIXME: wait for plug */
2046 flexarray_free(back);
2047 flexarray_free(front);
2048 return 0;
2051 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
2053 char *dompath, *net2_path_fe;
2054 char **l;
2055 char *val, *tok;
2056 unsigned int nb_net2s, i;
2057 libxl_net2info *res, *net2s;
2059 dompath = libxl_xs_get_dompath(ctx, domid);
2060 if (!dompath) {
2061 return NULL;
2063 l = libxl_xs_directory(ctx, XBT_NULL,
2064 libxl_sprintf(ctx, "%s/device/vif2", dompath), &nb_net2s);
2065 if (!l) {
2066 return NULL;
2068 res = libxl_calloc(ctx, nb_net2s, sizeof (libxl_net2info));
2069 if (!res) {
2070 libxl_free(ctx, l);
2071 return NULL;
2073 net2s = res;
2074 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
2075 net2_path_fe = libxl_sprintf(ctx, "%s/device/vif2/%s", dompath, *l);
2077 net2s->backend = libxl_xs_read(ctx, XBT_NULL,
2078 libxl_sprintf(ctx, "%s/backend", net2_path_fe));
2079 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", net2_path_fe));
2080 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
2082 net2s->devid = strtoul(*l, NULL, 10);
2083 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", net2_path_fe));
2084 net2s->state = val ? strtoul(val, NULL, 10) : -1;
2086 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/mac", net2_path_fe));
2087 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2088 ++i, tok = strtok(NULL, ":")) {
2089 net2s->mac[i] = strtoul(tok, NULL, 16);
2091 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/remote-trusted", net2_path_fe));
2092 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
2094 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/remote-mac", net2_path_fe));
2095 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2096 ++i, tok = strtok(NULL, ":")) {
2097 net2s->back_mac[i] = strtoul(tok, NULL, 16);
2099 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/filter-mac", net2_path_fe));
2100 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
2102 net2s->frontend = libxl_xs_read(ctx, XBT_NULL,
2103 libxl_sprintf(ctx, "%s/frontend", net2s->backend));
2104 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", net2s->backend));
2105 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2106 libxl_free(ctx, net2_path_fe);
2109 libxl_free(ctx, l);
2110 return res;
2113 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
2115 libxl_device device;
2117 device.backend_devid = net2->devid;
2118 device.backend_domid = net2->backend_domid;
2119 device.backend_kind = DEVICE_VIF2;
2120 device.devid = net2->devid;
2121 device.domid = net2->domid;
2122 device.kind = DEVICE_VIF2;
2124 return libxl_device_del(ctx, &device, wait);
2128 /******************************************************************************/
2129 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
2131 flexarray_t *front;
2132 flexarray_t *back;
2133 unsigned int boffset = 0;
2134 unsigned int foffset = 0;
2135 libxl_device device;
2137 if (console->build_state) {
2138 xs_transaction_t t;
2139 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
2140 ents[0] = "console/port";
2141 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
2142 ents[2] = "console/ring-ref";
2143 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
2144 ents[4] = "console/limit";
2145 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
2146 ents[6] = "console/type";
2147 if (console->constype == CONSTYPE_XENCONSOLED)
2148 ents[7] = "xenconsoled";
2149 else
2150 ents[7] = "ioemu";
2151 retry_transaction:
2152 t = xs_transaction_start(ctx->xsh);
2153 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
2154 if (!xs_transaction_end(ctx->xsh, t, 0))
2155 if (errno == EAGAIN)
2156 goto retry_transaction;
2159 front = flexarray_make(16, 1);
2160 if (!front)
2161 return ERROR_NOMEM;
2162 back = flexarray_make(16, 1);
2163 if (!back)
2164 return ERROR_NOMEM;
2166 device.backend_devid = console->devid;
2167 device.backend_domid = console->backend_domid;
2168 device.backend_kind = DEVICE_CONSOLE;
2169 device.devid = console->devid;
2170 device.domid = console->domid;
2171 device.kind = DEVICE_CONSOLE;
2173 flexarray_set(back, boffset++, "frontend-id");
2174 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
2175 flexarray_set(back, boffset++, "online");
2176 flexarray_set(back, boffset++, "1");
2177 flexarray_set(back, boffset++, "state");
2178 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
2179 flexarray_set(back, boffset++, "domain");
2180 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
2181 flexarray_set(back, boffset++, "protocol");
2182 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
2184 flexarray_set(front, foffset++, "backend-id");
2185 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
2186 flexarray_set(front, foffset++, "state");
2187 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
2188 flexarray_set(front, foffset++, "limit");
2189 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
2190 flexarray_set(front, foffset++, "protocol");
2191 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
2192 flexarray_set(front, foffset++, "type");
2193 if (console->constype == CONSTYPE_XENCONSOLED)
2194 flexarray_set(front, foffset++, "xenconsoled");
2195 else
2196 flexarray_set(front, foffset++, "ioemu");
2198 libxl_device_generic_add(ctx, &device,
2199 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
2200 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
2201 flexarray_free(back);
2202 flexarray_free(front);
2204 return 0;
2207 /******************************************************************************/
2208 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
2210 flexarray_t *front;
2211 flexarray_t *back;
2212 unsigned int boffset = 0;
2213 unsigned int foffset = 0;
2214 libxl_device device;
2216 front = flexarray_make(16, 1);
2217 if (!front)
2218 return ERROR_NOMEM;
2219 back = flexarray_make(16, 1);
2220 if (!back)
2221 return ERROR_NOMEM;
2223 device.backend_devid = vkb->devid;
2224 device.backend_domid = vkb->backend_domid;
2225 device.backend_kind = DEVICE_VKBD;
2226 device.devid = vkb->devid;
2227 device.domid = vkb->domid;
2228 device.kind = DEVICE_VKBD;
2230 flexarray_set(back, boffset++, "frontend-id");
2231 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
2232 flexarray_set(back, boffset++, "online");
2233 flexarray_set(back, boffset++, "1");
2234 flexarray_set(back, boffset++, "state");
2235 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
2236 flexarray_set(back, boffset++, "domain");
2237 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
2239 flexarray_set(front, foffset++, "backend-id");
2240 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
2241 flexarray_set(front, foffset++, "state");
2242 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
2244 libxl_device_generic_add(ctx, &device,
2245 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
2246 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
2247 flexarray_free(back);
2248 flexarray_free(front);
2250 return 0;
2253 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2255 return ERROR_NI;
2258 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2260 return ERROR_NI;
2263 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
2265 char *be_path_tap, *be_path_vbd;
2266 libxl_device_disk *dend, *disks, *ret = NULL;
2267 char **b, **l = NULL;
2268 unsigned int numl;
2269 char *type;
2271 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
2272 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
2274 b = l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
2275 if (l) {
2276 ret = realloc(ret, sizeof(libxl_device_disk) * numl);
2277 disks = ret;
2278 *num = numl;
2279 dend = ret + *num;
2280 for (; disks < dend; ++disks, ++l) {
2281 disks->backend_domid = 0;
2282 disks->domid = domid;
2283 disks->physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, *l));
2284 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_vbd, *l)), &(disks->phystype));
2285 disks->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, *l));
2286 disks->unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, *l)));
2287 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, *l)), "w"))
2288 disks->readwrite = 1;
2289 else
2290 disks->readwrite = 0;
2291 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, *l))));
2292 disks->is_cdrom = !strcmp(type, "cdrom");
2294 libxl_free(ctx, b);
2296 b = l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
2297 if (l) {
2298 ret = realloc(ret, sizeof(libxl_device_disk) * (*num + numl));
2299 disks = ret + *num;
2300 *num += numl;
2301 for (dend = ret + *num; disks < dend; ++disks, ++l) {
2302 disks->backend_domid = 0;
2303 disks->domid = domid;
2304 disks->physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, *l));
2305 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_tap, *l)), &(disks->phystype));
2306 disks->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, *l));
2307 disks->unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, *l)));
2308 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, *l)), "w"))
2309 disks->readwrite = 1;
2310 else
2311 disks->readwrite = 0;
2312 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_tap, *l))));
2313 disks->is_cdrom = !strcmp(type, "cdrom");
2315 libxl_free(ctx, b);
2317 return ret;
2320 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
2321 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
2323 char *dompath, *diskpath;
2324 char *val;
2326 dompath = libxl_xs_get_dompath(ctx, domid);
2327 diskinfo->devid = device_disk_dev_number(disk->virtpath);
2329 /* tap devices entries in xenstore are written as vbd devices. */
2330 diskpath = libxl_sprintf(ctx, "%s/device/vbd/%d", dompath, diskinfo->devid);
2331 diskinfo->backend = libxl_xs_read(ctx, XBT_NULL,
2332 libxl_sprintf(ctx, "%s/backend", diskpath));
2333 if (!diskinfo->backend) {
2334 return ERROR_FAIL;
2336 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", diskpath));
2337 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
2338 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", diskpath));
2339 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
2340 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/event-channel", diskpath));
2341 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
2342 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/ring-ref", diskpath));
2343 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
2344 diskinfo->frontend = libxl_xs_read(ctx, XBT_NULL,
2345 libxl_sprintf(ctx, "%s/frontend", diskinfo->backend));
2346 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", diskinfo->backend));
2347 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2349 return 0;
2352 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
2354 int num, i;
2355 uint32_t stubdomid;
2356 libxl_device_disk *disks;
2358 if (!disk->physpath) {
2359 disk->physpath = "";
2360 disk->phystype = PHYSTYPE_PHY;
2362 disks = libxl_device_disk_list(ctx, domid, &num);
2363 for (i = 0; i < num; i++) {
2364 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
2365 /* found */
2366 break;
2368 if (i == num) {
2369 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
2370 return ERROR_FAIL;
2372 libxl_device_disk_del(ctx, disks + i, 1);
2373 libxl_device_disk_add(ctx, domid, disk);
2374 stubdomid = libxl_get_stubdom_id(ctx, domid);
2375 if (stubdomid) {
2376 disks[i].domid = stubdomid;
2377 libxl_device_disk_del(ctx, disks + i, 1);
2378 disk->domid = stubdomid;
2379 libxl_device_disk_add(ctx, stubdomid, disk);
2380 disk->domid = domid;
2382 return 0;
2385 /******************************************************************************/
2386 static int libxl_build_xenpv_qemu_args(libxl_ctx *ctx,
2387 libxl_device_vfb *vfb,
2388 int num_console,
2389 libxl_device_console *console,
2390 libxl_device_model_info *info)
2392 int i = 0, j = 0, num = 0;
2393 memset(info, 0x00, sizeof(libxl_device_model_info));
2395 info->vnc = vfb->vnc;
2396 if (vfb->vnclisten)
2397 info->vnclisten = libxl_strdup(ctx, vfb->vnclisten);
2398 info->vncdisplay = vfb->vncdisplay;
2399 info->vncunused = vfb->vncunused;
2400 if (vfb->vncpasswd)
2401 info->vncpasswd = vfb->vncpasswd;
2402 if (vfb->keymap)
2403 info->keymap = libxl_strdup(ctx, vfb->keymap);
2404 info->sdl = vfb->sdl;
2405 info->opengl = vfb->opengl;
2406 for (i = 0; i < num_console; i++) {
2407 if (console->constype == CONSTYPE_IOEMU)
2408 num++;
2410 if (num > 0) {
2411 uint32_t guest_domid;
2412 if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
2413 char *filename;
2414 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
2415 libxl_create_logfile(ctx, name, &filename);
2416 info->serial = libxl_sprintf(ctx, "file:%s", filename);
2417 free(filename);
2418 } else {
2419 info->serial = "pty";
2421 num--;
2423 if (num > 0) {
2424 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
2425 for (j = 0; j < num * 2; j = j + 2) {
2426 info->extra[j] = "-serial";
2427 info->extra[j + 1] = "pty";
2429 info->extra[j] = NULL;
2431 info->domid = vfb->domid;
2432 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
2433 info->device_model = libxl_abs_path(ctx, "qemu-dm", libxl_libexec_path());
2434 info->type = XENPV;
2435 return 0;
2438 int libxl_create_xenpv_qemu(libxl_ctx *ctx, libxl_device_vfb *vfb,
2439 int num_console, libxl_device_console *console,
2440 libxl_device_model_starting **starting_r)
2442 libxl_device_model_info info;
2444 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
2445 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
2446 return 0;
2449 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
2451 flexarray_t *front;
2452 flexarray_t *back;
2453 unsigned int boffset = 0;
2454 unsigned int foffset = 0;
2455 libxl_device device;
2457 front = flexarray_make(16, 1);
2458 if (!front)
2459 return ERROR_NOMEM;
2460 back = flexarray_make(16, 1);
2461 if (!back)
2462 return ERROR_NOMEM;
2464 device.backend_devid = vfb->devid;
2465 device.backend_domid = vfb->backend_domid;
2466 device.backend_kind = DEVICE_VFB;
2467 device.devid = vfb->devid;
2468 device.domid = vfb->domid;
2469 device.kind = DEVICE_VFB;
2471 flexarray_set(back, boffset++, "frontend-id");
2472 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
2473 flexarray_set(back, boffset++, "online");
2474 flexarray_set(back, boffset++, "1");
2475 flexarray_set(back, boffset++, "state");
2476 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
2477 flexarray_set(back, boffset++, "domain");
2478 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
2479 flexarray_set(back, boffset++, "vnc");
2480 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
2481 flexarray_set(back, boffset++, "vnclisten");
2482 flexarray_set(back, boffset++, vfb->vnclisten);
2483 flexarray_set(back, boffset++, "vncpasswd");
2484 flexarray_set(back, boffset++, vfb->vncpasswd);
2485 flexarray_set(back, boffset++, "vncdisplay");
2486 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
2487 flexarray_set(back, boffset++, "vncunused");
2488 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
2489 flexarray_set(back, boffset++, "sdl");
2490 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
2491 flexarray_set(back, boffset++, "opengl");
2492 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
2493 if (vfb->xauthority) {
2494 flexarray_set(back, boffset++, "xauthority");
2495 flexarray_set(back, boffset++, vfb->xauthority);
2497 if (vfb->display) {
2498 flexarray_set(back, boffset++, "display");
2499 flexarray_set(back, boffset++, vfb->display);
2502 flexarray_set(front, foffset++, "backend-id");
2503 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
2504 flexarray_set(front, foffset++, "state");
2505 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
2507 libxl_device_generic_add(ctx, &device,
2508 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
2509 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
2510 flexarray_free(front);
2511 flexarray_free(back);
2513 return 0;
2516 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2518 return ERROR_NI;
2521 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2523 return ERROR_NI;
2526 /******************************************************************************/
2528 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
2530 char *mem, *endptr;
2531 uint32_t memorykb;
2532 char *dompath = libxl_xs_get_dompath(ctx, domid);
2534 mem = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath));
2535 if (!mem) {
2536 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
2537 return 1;
2539 memorykb = strtoul(mem, &endptr, 10);
2540 if (*endptr != '\0') {
2541 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
2542 return 1;
2545 if (max_memkb < memorykb) {
2546 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
2547 return 1;
2550 if (domid != 0)
2551 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/static-max", dompath), "%"PRIu32, max_memkb);
2553 return 0;
2556 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb, int enforce)
2558 int rc = 0;
2559 uint32_t memorykb = 0, videoram = 0;
2560 char *memmax, *endptr, *videoram_s = NULL;
2561 char *dompath = libxl_xs_get_dompath(ctx, domid);
2562 xc_domaininfo_t info;
2563 libxl_dominfo ptr;
2564 char *uuid;
2566 if (domid) {
2567 memmax = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/static-max", dompath));
2568 if (!memmax) {
2569 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
2570 "cannot get memory info from %s/memory/static-max\n", dompath);
2571 return 1;
2573 memorykb = strtoul(memmax, &endptr, 10);
2574 if (*endptr != '\0') {
2575 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
2576 "invalid max memory %s from %s/memory/static-max\n", memmax, dompath);
2577 return 1;
2580 if (target_memkb > memorykb) {
2581 XL_LOG(ctx, XL_LOG_ERROR,
2582 "memory_dynamic_max must be less than or equal to memory_static_max\n");
2583 return 1;
2587 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
2588 videoram = videoram_s ? atoi(videoram_s) : 0;
2590 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%"PRIu32, target_memkb);
2592 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2593 if (rc != 1 || info.domain != domid)
2594 return rc;
2595 xcinfo2xlinfo(&info, &ptr);
2596 uuid = libxl_uuid2string(ctx, ptr.uuid);
2597 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/vm/%s/memory", uuid), "%"PRIu32, target_memkb / 1024);
2599 if (enforce || !domid)
2600 memorykb = target_memkb;
2601 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb + LIBXL_MAXMEM_CONSTANT);
2602 if (rc != 0)
2603 return rc;
2604 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
2605 return rc;
2608 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
2610 int rc = -1;
2612 switch (button) {
2613 case POWER_BUTTON:
2614 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2615 break;
2616 case SLEEP_BUTTON:
2617 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2618 break;
2619 default:
2620 break;
2623 return rc;
2626 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
2628 xc_physinfo_t xcphysinfo = { 0 };
2629 int rc;
2631 rc = xc_physinfo(ctx->xch, &xcphysinfo);
2632 if (rc != 0) {
2633 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting physinfo");
2634 return ERROR_FAIL;
2636 physinfo->threads_per_core = xcphysinfo.threads_per_core;
2637 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
2638 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
2639 physinfo->nr_cpus = xcphysinfo.nr_cpus;
2640 physinfo->cpu_khz = xcphysinfo.cpu_khz;
2641 physinfo->total_pages = xcphysinfo.total_pages;
2642 physinfo->free_pages = xcphysinfo.free_pages;
2643 physinfo->scrub_pages = xcphysinfo.scrub_pages;
2644 physinfo->nr_nodes = xcphysinfo.nr_nodes;
2645 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
2646 physinfo->phys_cap = xcphysinfo.capabilities;
2648 return 0;
2651 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
2653 union {
2654 xen_extraversion_t xen_extra;
2655 xen_compile_info_t xen_cc;
2656 xen_changeset_info_t xen_chgset;
2657 xen_capabilities_info_t xen_caps;
2658 xen_platform_parameters_t p_parms;
2659 xen_commandline_t xen_commandline;
2660 } u;
2661 long xen_version;
2662 libxl_version_info *info = &ctx->version_info;
2664 if (info->xen_version_extra != NULL)
2665 return info;
2667 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
2668 info->xen_version_major = xen_version >> 16;
2669 info->xen_version_minor = xen_version & 0xFF;
2670 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
2671 info->xen_version_extra = libxl_strdup(ctx, u.xen_extra);
2673 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
2674 info->compiler = libxl_strdup(ctx, u.xen_cc.compiler);
2675 info->compile_by = libxl_strdup(ctx, u.xen_cc.compile_by);
2676 info->compile_domain = libxl_strdup(ctx, u.xen_cc.compile_domain);
2677 info->compile_date = libxl_strdup(ctx, u.xen_cc.compile_date);
2679 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
2680 info->capabilities = libxl_strdup(ctx, u.xen_caps);
2682 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
2683 info->changeset = libxl_strdup(ctx, u.xen_chgset);
2685 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
2686 info->virt_start = u.p_parms.virt_start;
2688 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
2690 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
2691 info->commandline = libxl_strdup(ctx, u.xen_commandline);
2693 return info;
2696 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
2697 int *nb_vcpu, int *cpusize)
2699 libxl_vcpuinfo *ptr, *ret;
2700 xc_domaininfo_t domaininfo;
2701 xc_vcpuinfo_t vcpuinfo;
2702 xc_physinfo_t physinfo = { 0 };
2704 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2705 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting infolist");
2706 return NULL;
2708 if (xc_physinfo(ctx->xch, &physinfo) == -1) {
2709 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting physinfo");
2710 return NULL;
2712 *cpusize = physinfo.max_cpu_id + 1;
2713 ptr = libxl_calloc(ctx, domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
2714 if (!ptr) {
2715 return NULL;
2718 ret = ptr;
2719 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
2720 ptr->cpumap = libxl_calloc(ctx, (*cpusize + 63) / 64, sizeof (uint64_t));
2721 if (!ptr->cpumap) {
2722 return NULL;
2724 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
2725 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting vcpu info");
2726 return NULL;
2728 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap, *cpusize) == -1) {
2729 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting vcpu affinity");
2730 return NULL;
2732 ptr->vcpuid = *nb_vcpu;
2733 ptr->cpu = vcpuinfo.cpu;
2734 ptr->online = !!vcpuinfo.online;
2735 ptr->blocked = !!vcpuinfo.blocked;
2736 ptr->running = !!vcpuinfo.running;
2737 ptr->vcpu_time = vcpuinfo.cpu_time;
2739 return ret;
2742 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
2743 uint64_t *cpumap, int cpusize)
2745 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap, cpusize)) {
2746 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting vcpu affinity");
2747 return ERROR_FAIL;
2749 return 0;
2752 int libxl_set_vcpucount(libxl_ctx *ctx, uint32_t domid, uint32_t count)
2754 xc_domaininfo_t domaininfo;
2755 char *dompath;
2756 int i;
2758 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2759 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting domain info list");
2760 return ERROR_FAIL;
2762 if (!count || ((domaininfo.max_vcpu_id + 1) < count)) {
2763 return ERROR_INVAL;
2765 if (!(dompath = libxl_xs_get_dompath(ctx, domid)))
2766 return ERROR_FAIL;
2768 for (i = 0; i <= domaininfo.max_vcpu_id; ++i) {
2769 libxl_xs_write(ctx, XBT_NULL,
2770 libxl_sprintf(ctx, "%s/cpu/%u/availability", dompath, i),
2771 "%s", ((1 << i) & ((1 << count) - 1)) ? "online" : "offline");
2773 return 0;
2776 /*
2777 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
2778 */
2779 int libxl_get_sched_id(libxl_ctx *ctx)
2781 int sched, ret;
2783 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
2784 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting domain info list");
2785 return ERROR_FAIL;
2787 return sched;
2790 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2792 struct xen_domctl_sched_credit sdom;
2793 int rc;
2795 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
2796 if (rc != 0) {
2797 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting domain sched credit");
2798 return ERROR_FAIL;
2801 scinfo->weight = sdom.weight;
2802 scinfo->cap = sdom.cap;
2804 return 0;
2807 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2809 struct xen_domctl_sched_credit sdom;
2810 xc_domaininfo_t domaininfo;
2811 int rc;
2813 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
2814 if (rc < 0) {
2815 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting domain info list");
2816 return ERROR_FAIL;
2818 if (rc != 1 || domaininfo.domain != domid)
2819 return ERROR_INVAL;
2822 if (scinfo->weight < 1 || scinfo->weight > 65535) {
2823 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2824 "Cpu weight out of range, valid values are within range from 1 to 65535");
2825 return ERROR_INVAL;
2828 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
2829 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2830 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
2831 ((domaininfo.max_vcpu_id + 1) * 100));
2832 return ERROR_INVAL;
2835 sdom.weight = scinfo->weight;
2836 sdom.cap = scinfo->cap;
2838 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
2839 if ( rc < 0 ) {
2840 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting domain sched credit");
2841 return ERROR_FAIL;
2844 return 0;
2847 static int trigger_type_from_string(char *trigger_name)
2849 if (!strcmp(trigger_name, "nmi"))
2850 return XEN_DOMCTL_SENDTRIGGER_NMI;
2851 else if (!strcmp(trigger_name, "reset"))
2852 return XEN_DOMCTL_SENDTRIGGER_RESET;
2853 else if (!strcmp(trigger_name, "init"))
2854 return XEN_DOMCTL_SENDTRIGGER_INIT;
2855 else if (!strcmp(trigger_name, "power"))
2856 return XEN_DOMCTL_SENDTRIGGER_POWER;
2857 else if (!strcmp(trigger_name, "sleep"))
2858 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
2859 else
2860 return -1;
2863 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
2865 int rc = -1;
2866 int trigger_type = trigger_type_from_string(trigger_name);
2868 if (trigger_type == -1) {
2869 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
2870 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
2871 return ERROR_INVAL;
2874 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
2875 if (rc != 0) {
2876 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2877 "Send trigger '%s' failed", trigger_name);
2878 return ERROR_FAIL;
2881 return 0;
2884 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
2886 char *dompath = libxl_xs_get_dompath(ctx, domid);
2888 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/control/sysrq", dompath), "%c", sysrq);
2890 return 0;
2893 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
2895 int ret;
2896 ret = xc_send_debug_keys(ctx->xch, keys);
2897 if ( ret < 0 ) {
2898 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "sending debug keys");
2899 return ERROR_FAIL;
2901 return 0;
2904 libxl_xen_console_reader *
2905 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
2907 libxl_xen_console_reader *cr;
2908 unsigned int size = 16384;
2909 char *buf = malloc(size);
2911 if (!buf) {
2912 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
2913 " size is %u", size);
2914 return NULL;
2917 cr = malloc(sizeof(libxl_xen_console_reader));
2918 if (!cr) {
2919 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot malloc libxl_xen_console_reader");
2920 return NULL;
2923 memset(cr, 0, sizeof(libxl_xen_console_reader));
2924 cr->buffer = buf;
2925 cr->size = size;
2926 cr->count = size;
2927 cr->clear = clear;
2928 cr->incremental = 1;
2930 return cr;
2933 /* return values: *line_r
2934 * 1 success, whole line obtained from buffer non-0
2935 * 0 no more lines available right now 0
2936 * negative error code ERROR_* 0
2937 * On success *line_r is updated to point to a nul-terminated
2938 * string which is valid until the next call on the same console
2939 * reader. The libxl caller may overwrite parts of the string
2940 * if it wishes. */
2941 int libxl_xen_console_read_line(libxl_ctx *ctx,
2942 libxl_xen_console_reader *cr,
2943 char **line_r)
2945 int ret;
2947 memset(cr->buffer, 0, cr->size);
2948 ret = xc_readconsolering(ctx->xch, &cr->buffer, &cr->count,
2949 cr->clear, cr->incremental, &cr->index);
2950 if (ret < 0) {
2951 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "reading console ring buffer");
2952 return ERROR_FAIL;
2954 if (!ret) {
2955 if (cr->count) {
2956 *line_r = cr->buffer;
2957 ret = 1;
2958 } else {
2959 *line_r = NULL;
2960 ret = 0;
2964 return ret;
2967 void libxl_xen_console_read_finish(libxl_ctx *ctx,
2968 libxl_xen_console_reader *cr)
2970 free(cr->buffer);
2971 free(cr);
2974 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
2976 char *dompath = libxl_xs_get_dompath(ctx, domid);
2977 char *vm_path, *start_time;
2979 vm_path = libxl_xs_read(
2980 ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vm", dompath));
2981 start_time = libxl_xs_read(
2982 ctx, XBT_NULL, libxl_sprintf(ctx, "%s/start_time", vm_path));
2983 if (start_time == NULL) {
2984 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
2985 "Can't get start time of domain '%d'", domid);
2986 return -1;
2989 return strtoul(start_time, NULL, 10);
2992 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
2994 int rc;
2995 char _buf[32768];
2997 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
2998 0, _buf);
2999 if (rc < 0) {
3000 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
3001 "Can not get tmem list");
3002 return NULL;
3005 return strdup(_buf);
3008 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
3010 int rc;
3012 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
3013 0, NULL);
3014 if (rc < 0) {
3015 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
3016 "Can not freeze tmem pools");
3017 return ERROR_FAIL;
3020 return rc;
3023 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
3025 int rc;
3027 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
3028 0, NULL);
3029 if (rc < 0) {
3030 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
3031 "Can not destroy tmem pools");
3032 return ERROR_FAIL;
3035 return rc;
3038 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
3040 int rc;
3042 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
3043 0, NULL);
3044 if (rc < 0) {
3045 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
3046 "Can not thaw tmem pools");
3047 return ERROR_FAIL;
3050 return rc;
3053 static int32_t tmem_setop_from_string(char *set_name)
3055 if (!strcmp(set_name, "weight"))
3056 return TMEMC_SET_WEIGHT;
3057 else if (!strcmp(set_name, "cap"))
3058 return TMEMC_SET_CAP;
3059 else if (!strcmp(set_name, "compress"))
3060 return TMEMC_SET_COMPRESS;
3061 else
3062 return -1;
3065 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
3067 int rc;
3068 int32_t subop = tmem_setop_from_string(name);
3070 if (subop == -1) {
3071 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
3072 "Invalid set, valid sets are <weight|cap|compress>");
3073 return ERROR_INVAL;
3075 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
3076 if (rc < 0) {
3077 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
3078 "Can not set tmem %s", name);
3079 return ERROR_FAIL;
3082 return rc;
3085 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
3086 char* uuid, int auth)
3088 int rc;
3090 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
3091 if (rc < 0) {
3092 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
3093 "Can not set tmem shared auth");
3094 return ERROR_FAIL;
3097 return rc;
3100 int libxl_tmem_freeable(libxl_ctx *ctx)
3102 int rc;
3104 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
3105 if (rc < 0) {
3106 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
3107 "Can not get tmem freeable memory");
3108 return ERROR_FAIL;
3111 return rc;
3114 int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f)
3116 struct stat st_buf;
3117 int ret, fd;
3118 void *data;
3120 if (f->mapped)
3121 return 0;
3123 fd = open(f->path, O_RDONLY);
3124 if (f < 0)
3125 return ERROR_FAIL;
3127 ret = fstat(fd, &st_buf);
3128 if (ret < 0)
3129 goto out;
3131 ret = -1;
3132 data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3133 if (data == NULL)
3134 goto out;
3136 f->mapped = 1;
3137 f->data = data;
3138 f->size = st_buf.st_size;
3140 ret = 0;
3141 out:
3142 close(fd);
3144 return ret == 0 ? 0 : ERROR_FAIL;
3147 int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f)
3149 int ret;
3151 if (!f->mapped)
3152 return 0;
3154 ret = munmap(f->data, f->size);
3156 return ret == 0 ? 0 : ERROR_FAIL;