debuggers.hg

view tools/libxl/libxl.c @ 21965:20540b8fe4b6

xl: detect pci-insert-failed dm status on pci-passthrough

NOTE: This functionality depends on a corresponding qemu-dm patch to work as
expected. Should be safe to use with an un-patched qemu-dm as before.

libxl_wait_for_device_model can only wait for one status value, re-work the
API so that a callback function can chose between several different possible
status values for qemu-dm and fix up all callers appropriately.

In the case of PCI device insert we succeed if qemu-dm reports
"pci-device-inserted" and error out instead of hanging forever if it fails
since qemu-dm now reports a status of "pci-insert-failed".

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Wed Aug 04 14:43:46 2010 +0100 (2010-08-04)
parents fd99dc9ab249
children d0469b045277
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, uint8_t new_uuid[16])
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(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 - 1);
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",
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);
1102 return (char **) flexarray_contents(dm_args);
1105 void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
1107 libxl_device_model_starting *starting = for_spawn;
1108 char *kvs[3];
1109 int rc;
1110 struct xs_handle *xsh;
1112 xsh = xs_daemon_open();
1113 /* we mustn't use the parent's handle in the child */
1115 kvs[0] = "image/device-model-pid";
1116 if (asprintf(&kvs[1], "%d", innerchild) < 0)
1117 return;
1118 kvs[2] = NULL;
1120 rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
1121 if (rc)
1122 return;
1123 xs_daemon_close(xsh);
1126 static int libxl_vfb_and_vkb_from_device_model_info(libxl_ctx *ctx,
1127 libxl_device_model_info *info,
1128 libxl_device_vfb *vfb,
1129 libxl_device_vkb *vkb)
1131 memset(vfb, 0x00, sizeof(libxl_device_vfb));
1132 memset(vkb, 0x00, sizeof(libxl_device_vkb));
1134 vfb->backend_domid = 0;
1135 vfb->devid = 0;
1136 vfb->vnc = info->vnc;
1137 vfb->vnclisten = info->vnclisten;
1138 vfb->vncdisplay = info->vncdisplay;
1139 vfb->vncunused = info->vncunused;
1140 vfb->vncpasswd = info->vncpasswd;
1141 vfb->keymap = info->keymap;
1142 vfb->sdl = info->sdl;
1143 vfb->opengl = info->opengl;
1145 vkb->backend_domid = 0;
1146 vkb->devid = 0;
1147 return 0;
1150 static int libxl_write_dmargs(libxl_ctx *ctx, int domid, int guest_domid, char **args)
1152 int i;
1153 char *vm_path;
1154 char *dmargs, *path;
1155 int dmargs_size;
1156 struct xs_permissions roperm[2];
1157 xs_transaction_t t;
1159 roperm[0].id = 0;
1160 roperm[0].perms = XS_PERM_NONE;
1161 roperm[1].id = domid;
1162 roperm[1].perms = XS_PERM_READ;
1164 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
1166 i = 0;
1167 dmargs_size = 0;
1168 while (args[i] != NULL) {
1169 dmargs_size = dmargs_size + strlen(args[i]) + 1;
1170 i++;
1172 dmargs_size++;
1173 dmargs = (char *) malloc(dmargs_size);
1174 i = 1;
1175 dmargs[0] = '\0';
1176 while (args[i] != NULL) {
1177 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
1178 strcat(dmargs, " ");
1179 strcat(dmargs, args[i]);
1181 i++;
1183 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
1185 retry_transaction:
1186 t = xs_transaction_start(ctx->xsh);
1187 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
1188 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
1189 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
1190 if (!xs_transaction_end(ctx->xsh, t, 0))
1191 if (errno == EAGAIN)
1192 goto retry_transaction;
1193 free(dmargs);
1194 return 0;
1197 static int libxl_create_stubdom(libxl_ctx *ctx,
1198 libxl_device_model_info *info,
1199 libxl_device_disk *disks, int num_disks,
1200 libxl_device_nic *vifs, int num_vifs,
1201 libxl_device_vfb *vfb,
1202 libxl_device_vkb *vkb,
1203 libxl_device_model_starting **starting_r)
1205 int i, num_console = 1, ret;
1206 libxl_device_console *console;
1207 libxl_domain_create_info c_info;
1208 libxl_domain_build_info b_info;
1209 libxl_domain_build_state state;
1210 uint32_t domid;
1211 char **args;
1212 struct xs_permissions perm[2];
1213 xs_transaction_t t;
1214 libxl_device_model_starting *dm_starting = 0;
1216 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
1217 if (!args)
1218 return ERROR_FAIL;
1220 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
1221 c_info.hvm = 0;
1222 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
1223 for (i = 0; i < 16; i++)
1224 c_info.uuid[i] = info->uuid[i];
1226 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
1227 b_info.max_vcpus = 1;
1228 b_info.max_memkb = 32 * 1024;
1229 b_info.target_memkb = b_info.max_memkb;
1230 b_info.kernel.path = libxl_abs_path(ctx, "ioemu-stubdom.gz", libxl_xenfirmwaredir_path());
1231 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
1232 b_info.u.pv.ramdisk.path = "";
1233 b_info.u.pv.features = "";
1234 b_info.hvm = 0;
1236 ret = libxl_domain_make(ctx, &c_info, &domid);
1237 if (ret) return ret;
1238 ret = libxl_domain_build(ctx, &b_info, domid, &state);
1239 if (ret) return ret;
1241 libxl_write_dmargs(ctx, domid, info->domid, args);
1242 libxl_xs_write(ctx, XBT_NULL,
1243 libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)),
1244 "%d", domid);
1245 libxl_xs_write(ctx, XBT_NULL,
1246 libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)),
1247 "%d", info->domid);
1248 ret = xc_domain_set_target(ctx->xch, domid, info->domid);
1249 if (ret<0) {
1250 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting target domain %d -> %d", domid, info->domid);
1251 return ERROR_FAIL;
1253 xs_set_target(ctx->xsh, domid, info->domid);
1255 perm[0].id = domid;
1256 perm[0].perms = XS_PERM_NONE;
1257 perm[1].id = info->domid;
1258 perm[1].perms = XS_PERM_READ;
1259 retry_transaction:
1260 t = xs_transaction_start(ctx->xsh);
1261 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
1262 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
1263 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
1264 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
1265 if (!xs_transaction_end(ctx->xsh, t, 0))
1266 if (errno == EAGAIN)
1267 goto retry_transaction;
1269 for (i = 0; i < num_disks; i++) {
1270 disks[i].domid = domid;
1271 ret = libxl_device_disk_add(ctx, domid, &disks[i]);
1272 if (ret) return ret;
1274 for (i = 0; i < num_vifs; i++) {
1275 vifs[i].domid = domid;
1276 ret = libxl_device_nic_add(ctx, domid, &vifs[i]);
1277 if (ret) return ret;
1279 vfb->domid = domid;
1280 ret = libxl_device_vfb_add(ctx, domid, vfb);
1281 if (ret) return ret;
1282 vkb->domid = domid;
1283 ret = libxl_device_vkb_add(ctx, domid, vkb);
1284 if (ret) return ret;
1286 if (info->serial)
1287 num_console++;
1289 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
1290 if (!console)
1291 return ERROR_NOMEM;
1293 for (i = 0; i < num_console; i++) {
1294 console[i].devid = i;
1295 console[i].constype = CONSTYPE_IOEMU;
1296 console[i].domid = domid;
1297 if (!i)
1298 console[i].build_state = &state;
1299 ret = libxl_device_console_add(ctx, domid, &console[i]);
1300 if (ret) return ret;
1302 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
1303 free(args);
1304 return ERROR_FAIL;
1306 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
1307 free(args);
1308 return ERROR_FAIL;
1311 libxl_domain_unpause(ctx, domid);
1313 if (starting_r) {
1314 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
1315 (*starting_r)->domid = info->domid;
1316 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
1317 (*starting_r)->for_spawn = NULL;
1320 free(args);
1321 return 0;
1324 int libxl_create_device_model(libxl_ctx *ctx,
1325 libxl_device_model_info *info,
1326 libxl_device_disk *disks, int num_disks,
1327 libxl_device_nic *vifs, int num_vifs,
1328 libxl_device_model_starting **starting_r)
1330 char *path, *logfile;
1331 int logfile_w, null;
1332 int rc;
1333 char **args;
1334 libxl_device_model_starting buf_starting, *p;
1335 xs_transaction_t t;
1336 char *vm_path;
1337 char **pass_stuff;
1339 if (strstr(info->device_model, "stubdom-dm")) {
1340 libxl_device_vfb vfb;
1341 libxl_device_vkb vkb;
1343 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
1344 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
1347 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
1348 if (!args)
1349 return ERROR_FAIL;
1351 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
1352 xs_mkdir(ctx->xsh, XBT_NULL, path);
1353 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/disable_pf", path), "%d", !info->xen_platform_pci);
1355 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
1356 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
1357 free(logfile);
1358 null = open("/dev/null", O_RDONLY);
1360 if (starting_r) {
1361 rc = ERROR_NOMEM;
1362 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
1363 if (!*starting_r) goto xit;
1364 p = *starting_r;
1365 p->for_spawn = libxl_calloc(ctx, sizeof(libxl_spawn_starting), 1);
1366 } else {
1367 p = &buf_starting;
1368 p->for_spawn = NULL;
1371 p->domid = info->domid;
1372 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
1373 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
1375 if (info->vncpasswd) {
1376 retry_transaction:
1377 /* Find uuid and the write the vnc password to xenstore for qemu. */
1378 t = xs_transaction_start(ctx->xsh);
1379 vm_path = libxl_xs_read(ctx,t,libxl_sprintf(ctx, "%s/vm", p->dom_path));
1380 if (vm_path) {
1381 /* Now write the vncpassword into it. */
1382 pass_stuff = libxl_calloc(ctx, 2, sizeof(char *));
1383 pass_stuff[0] = "vncpasswd";
1384 pass_stuff[1] = info->vncpasswd;
1385 libxl_xs_writev(ctx,t,vm_path,pass_stuff);
1386 if (!xs_transaction_end(ctx->xsh, t, 0))
1387 if (errno == EAGAIN)
1388 goto retry_transaction;
1392 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
1393 if (rc < 0) goto xit;
1394 if (!rc) { /* inner child */
1395 libxl_exec(null, logfile_w, logfile_w,
1396 libxl_abs_path(ctx, info->device_model, libxl_private_bindir_path()),
1397 args);
1400 rc = 0;
1401 xit:
1402 free(args);
1403 close(null);
1404 close(logfile_w);
1406 return rc;
1409 int libxl_detach_device_model(libxl_ctx *ctx,
1410 libxl_device_model_starting *starting)
1412 int rc;
1413 rc = libxl_spawn_detach(ctx, starting->for_spawn);
1414 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
1415 libxl_free(ctx, starting);
1416 return rc;
1420 int libxl_confirm_device_model_startup(libxl_ctx *ctx,
1421 libxl_device_model_starting *starting)
1423 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running", NULL, NULL);
1424 int detach;
1425 if ( !problem )
1426 problem = libxl_spawn_check(ctx, starting->for_spawn);
1427 detach = libxl_detach_device_model(ctx, starting);
1428 return problem ? problem : detach;
1432 /******************************************************************************/
1434 static char *get_blktap2_device(libxl_ctx *ctx,
1435 const char *name, const char *type)
1437 int minor = tap_ctl_find_minor(type, name);
1438 if (minor < 0)
1439 return NULL;
1440 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", minor);
1443 static char *make_blktap2_device(libxl_ctx *ctx,
1444 const char *name, const char *type)
1446 char *params, *devname = NULL;
1447 int err;
1448 params = libxl_sprintf(ctx, "%s:%s", type, name);
1449 err = tap_ctl_create(params, &devname);
1450 if (!err)
1451 libxl_ptr_add(ctx, devname);
1452 return err ? NULL : devname;
1455 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1457 flexarray_t *front;
1458 flexarray_t *back;
1459 char *backend_type;
1460 unsigned int boffset = 0;
1461 unsigned int foffset = 0;
1462 int devid;
1463 libxl_device device;
1464 int major, minor;
1466 front = flexarray_make(16, 1);
1467 if (!front)
1468 return ERROR_NOMEM;
1469 back = flexarray_make(16, 1);
1470 if (!back) /* leaks front if error */
1471 return ERROR_NOMEM;
1473 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
1474 devid = device_disk_dev_number(disk->virtpath);
1475 if (devid==-1) {
1476 XL_LOG(ctx, XL_LOG_ERROR, "Invalid or unsupported"
1477 " virtual disk identifier %s", disk->virtpath);
1478 return ERROR_INVAL;
1481 device.backend_devid = devid;
1482 device.backend_domid = disk->backend_domid;
1483 device.devid = devid;
1484 device.domid = disk->domid;
1485 device.kind = DEVICE_VBD;
1487 switch (disk->phystype) {
1488 case PHYSTYPE_PHY: {
1490 device_physdisk_major_minor(disk->physpath, &major, &minor);
1491 flexarray_set(back, boffset++, "physical-device");
1492 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1494 flexarray_set(back, boffset++, "params");
1495 flexarray_set(back, boffset++, disk->physpath);
1497 device.backend_kind = DEVICE_VBD;
1498 break;
1500 case PHYSTYPE_FILE:
1501 /* let's pretend is tap:aio for the moment */
1502 disk->phystype = PHYSTYPE_AIO;
1503 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD: {
1504 const char *msg;
1505 if (!tap_ctl_check(&msg)) {
1506 const char *type = device_disk_string_of_phystype(disk->phystype);
1507 char *dev;
1508 dev = get_blktap2_device(ctx, disk->physpath, type);
1509 if (!dev)
1510 dev = make_blktap2_device(ctx, disk->physpath, type);
1511 if (!dev)
1512 return ERROR_FAIL;
1513 flexarray_set(back, boffset++, "tapdisk-params");
1514 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
1515 flexarray_set(back, boffset++, "params");
1516 flexarray_set(back, boffset++, libxl_strdup(ctx, dev));
1517 backend_type = "phy";
1518 device_physdisk_major_minor(dev, &major, &minor);
1519 flexarray_set(back, boffset++, "physical-device");
1520 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1521 device.backend_kind = DEVICE_VBD;
1523 break;
1525 flexarray_set(back, boffset++, "params");
1526 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
1527 device_disk_string_of_phystype(disk->phystype), disk->physpath));
1529 device.backend_kind = DEVICE_TAP;
1530 break;
1532 default:
1533 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
1534 return ERROR_INVAL;
1537 flexarray_set(back, boffset++, "frontend-id");
1538 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
1539 flexarray_set(back, boffset++, "online");
1540 flexarray_set(back, boffset++, "1");
1541 flexarray_set(back, boffset++, "removable");
1542 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
1543 flexarray_set(back, boffset++, "bootable");
1544 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1545 flexarray_set(back, boffset++, "state");
1546 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1547 flexarray_set(back, boffset++, "dev");
1548 flexarray_set(back, boffset++, disk->virtpath);
1549 flexarray_set(back, boffset++, "type");
1550 flexarray_set(back, boffset++, backend_type);
1551 flexarray_set(back, boffset++, "mode");
1552 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1554 flexarray_set(front, foffset++, "backend-id");
1555 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
1556 flexarray_set(front, foffset++, "state");
1557 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1558 flexarray_set(front, foffset++, "virtual-device");
1559 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
1560 flexarray_set(front, foffset++, "device-type");
1561 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1563 if (0 /* protocol != native*/) {
1564 flexarray_set(front, foffset++, "protocol");
1565 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1568 libxl_device_generic_add(ctx, &device,
1569 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1570 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1571 flexarray_free(back);
1572 flexarray_free(front);
1573 return 0;
1576 int libxl_device_disk_del(libxl_ctx *ctx,
1577 libxl_device_disk *disk, int wait)
1579 libxl_device device;
1580 int devid;
1582 devid = device_disk_dev_number(disk->virtpath);
1583 device.backend_domid = disk->backend_domid;
1584 device.backend_devid = devid;
1585 device.backend_kind =
1586 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1587 device.domid = disk->domid;
1588 device.devid = devid;
1589 device.kind = DEVICE_VBD;
1590 return libxl_device_del(ctx, &device, wait);
1593 const char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
1595 char *dev = NULL;
1596 int phystype = disk->phystype;
1597 switch (phystype) {
1598 case PHYSTYPE_PHY: {
1599 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1600 dev = disk->physpath;
1601 break;
1603 case PHYSTYPE_FILE:
1604 /* let's pretend is tap:aio for the moment */
1605 phystype = PHYSTYPE_AIO;
1606 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD: {
1607 const char *msg;
1608 if (!tap_ctl_check(&msg)) {
1609 const char *type = device_disk_string_of_phystype(phystype);
1610 dev = get_blktap2_device(ctx, disk->physpath, type);
1611 if (!dev)
1612 dev = make_blktap2_device(ctx, disk->physpath, type);
1614 break;
1616 default:
1617 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1618 break;
1620 return dev;
1623 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1625 /* Nothing to do for PHYSTYPE_PHY. */
1627 /*
1628 * For other device types assume that the blktap2 process is
1629 * needed by the soon to be started domain and do nothing.
1630 */
1632 return 0;
1635 /******************************************************************************/
1636 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1638 flexarray_t *front;
1639 flexarray_t *back;
1640 unsigned int boffset = 0;
1641 unsigned int foffset = 0;
1642 libxl_device device;
1643 char *dompath, **l;
1644 unsigned int nb;
1646 front = flexarray_make(16, 1);
1647 if (!front)
1648 return ERROR_NOMEM;
1649 back = flexarray_make(16, 1);
1650 if (!back)
1651 return ERROR_NOMEM;
1653 if (nic->devid == -1) {
1654 if (!(dompath = libxl_xs_get_dompath(ctx, domid))) {
1655 return ERROR_FAIL;
1657 if (!(l = libxl_xs_directory(ctx, XBT_NULL,
1658 libxl_sprintf(ctx, "%s/device/vif", dompath), &nb))) {
1659 nic->devid = 0;
1660 } else {
1661 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1662 libxl_free(ctx, l);
1666 device.backend_devid = nic->devid;
1667 device.backend_domid = nic->backend_domid;
1668 device.backend_kind = DEVICE_VIF;
1669 device.devid = nic->devid;
1670 device.domid = nic->domid;
1671 device.kind = DEVICE_VIF;
1673 flexarray_set(back, boffset++, "frontend-id");
1674 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
1675 flexarray_set(back, boffset++, "online");
1676 flexarray_set(back, boffset++, "1");
1677 flexarray_set(back, boffset++, "state");
1678 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1679 flexarray_set(back, boffset++, "script");
1680 flexarray_set(back, boffset++, nic->script);
1681 flexarray_set(back, boffset++, "mac");
1682 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1683 nic->mac[0], nic->mac[1], nic->mac[2],
1684 nic->mac[3], nic->mac[4], nic->mac[5]));
1685 flexarray_set(back, boffset++, "bridge");
1686 flexarray_set(back, boffset++, libxl_strdup(ctx, nic->bridge));
1687 flexarray_set(back, boffset++, "handle");
1688 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
1690 flexarray_set(front, foffset++, "backend-id");
1691 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
1692 flexarray_set(front, foffset++, "state");
1693 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1694 flexarray_set(front, foffset++, "handle");
1695 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
1696 flexarray_set(front, foffset++, "mac");
1697 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1698 nic->mac[0], nic->mac[1], nic->mac[2],
1699 nic->mac[3], nic->mac[4], nic->mac[5]));
1700 if (0 /* protocol != native*/) {
1701 flexarray_set(front, foffset++, "protocol");
1702 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1705 libxl_device_generic_add(ctx, &device,
1706 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1707 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1709 /* FIXME: wait for plug */
1710 flexarray_free(back);
1711 flexarray_free(front);
1712 return 0;
1715 int libxl_device_nic_del(libxl_ctx *ctx,
1716 libxl_device_nic *nic, int wait)
1718 libxl_device device;
1720 device.backend_devid = nic->devid;
1721 device.backend_domid = nic->backend_domid;
1722 device.backend_kind = DEVICE_VIF;
1723 device.devid = nic->devid;
1724 device.domid = nic->domid;
1725 device.kind = DEVICE_VIF;
1727 return libxl_device_del(ctx, &device, wait);
1730 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1732 char *dompath, *nic_path_fe;
1733 char **l;
1734 char *val, *tok;
1735 unsigned int nb_nics, i;
1736 libxl_nicinfo *res, *nics;
1738 dompath = libxl_xs_get_dompath(ctx, domid);
1739 if (!dompath) {
1740 return NULL;
1742 l = libxl_xs_directory(ctx, XBT_NULL,
1743 libxl_sprintf(ctx, "%s/device/vif", dompath), &nb_nics);
1744 if (!l) {
1745 return NULL;
1747 res = libxl_calloc(ctx, nb_nics, sizeof (libxl_device_nic));
1748 if (!res) {
1749 libxl_free(ctx, l);
1750 return NULL;
1752 nics = res;
1753 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
1754 nic_path_fe = libxl_sprintf(ctx, "%s/device/vif/%s", dompath, *l);
1756 nics->backend = libxl_xs_read(ctx, XBT_NULL,
1757 libxl_sprintf(ctx, "%s/backend", nic_path_fe));
1758 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", nic_path_fe));
1759 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
1761 nics->devid = strtoul(*l, NULL, 10);
1762 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", nic_path_fe));
1763 nics->state = val ? strtoul(val, NULL, 10) : -1;
1764 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/mac", nic_path_fe));
1765 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1766 ++i, tok = strtok(NULL, ":")) {
1767 nics->mac[i] = strtoul(tok, NULL, 16);
1769 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/event-channel", nic_path_fe));
1770 nics->evtch = val ? strtol(val, NULL, 10) : -1;
1771 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/tx-ring-ref", nic_path_fe));
1772 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
1773 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/rx-ring-ref", nic_path_fe));
1774 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
1775 nics->frontend = libxl_xs_read(ctx, XBT_NULL,
1776 libxl_sprintf(ctx, "%s/frontend", nics->backend));
1777 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", nics->backend));
1778 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1779 nics->script = libxl_xs_read(ctx, XBT_NULL,
1780 libxl_sprintf(ctx, "%s/script", nics->backend));
1782 libxl_free(ctx, nic_path_fe);
1785 libxl_free(ctx, l);
1786 return res;
1789 /******************************************************************************/
1790 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
1792 flexarray_t *front, *back;
1793 unsigned int boffset = 0, foffset = 0;
1794 libxl_device device;
1795 char *dompath, *dom, **l;
1796 unsigned int nb;
1798 front = flexarray_make(16, 1);
1799 if (!front)
1800 return ERROR_NOMEM;
1801 back = flexarray_make(16, 1);
1802 if (!back)
1803 return ERROR_NOMEM;
1805 if (!(dompath = libxl_xs_get_dompath(ctx, domid))) {
1806 return ERROR_FAIL;
1808 dom = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/name", dompath));
1810 if (net2->devid == -1) {
1811 if (!(l = libxl_xs_directory(ctx, XBT_NULL,
1812 libxl_sprintf(ctx, "%s/device/vif2", dompath), &nb))) {
1813 net2->devid = 0;
1814 } else {
1815 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1816 libxl_free(ctx, l);
1820 device.backend_devid = net2->devid;
1821 device.backend_domid = net2->backend_domid;
1822 device.backend_kind = DEVICE_VIF2;
1823 device.devid = net2->devid;
1824 device.domid = net2->domid;
1825 device.kind = DEVICE_VIF2;
1827 flexarray_set(back, boffset++, "domain");
1828 flexarray_set(back, boffset++, dom);
1829 flexarray_set(back, boffset++, "frontend-id");
1830 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->domid));
1832 flexarray_set(back, boffset++, "local-trusted");
1833 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->back_trusted));
1834 flexarray_set(back, boffset++, "mac");
1835 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1836 net2->back_mac[0], net2->back_mac[1],
1837 net2->back_mac[2], net2->back_mac[3],
1838 net2->back_mac[4], net2->back_mac[5]));
1840 flexarray_set(back, boffset++, "remote-trusted");
1841 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->trusted));
1842 flexarray_set(back, boffset++, "remote-mac");
1843 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1844 net2->front_mac[0], net2->front_mac[1],
1845 net2->front_mac[2], net2->front_mac[3],
1846 net2->front_mac[4], net2->front_mac[5]));
1848 flexarray_set(back, boffset++, "max-bypasses");
1849 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->max_bypasses));
1850 flexarray_set(back, boffset++, "filter-mac");
1851 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", !!(net2->filter_mac)));
1852 flexarray_set(back, boffset++, "handle");
1853 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", net2->devid));
1854 flexarray_set(back, boffset++, "online");
1855 flexarray_set(back, boffset++, "1");
1856 flexarray_set(back, boffset++, "state");
1857 flexarray_set(back, boffset++, "1");
1859 flexarray_set(front, foffset++, "backend-id");
1860 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", net2->backend_domid));
1862 flexarray_set(front, foffset++, "local-trusted");
1863 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", net2->trusted));
1864 flexarray_set(front, foffset++, "mac");
1865 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1866 net2->front_mac[0], net2->front_mac[1],
1867 net2->front_mac[2], net2->front_mac[3],
1868 net2->front_mac[4], net2->front_mac[5]));
1870 flexarray_set(front, foffset++, "remote-trusted");
1871 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", net2->back_trusted));
1872 flexarray_set(front, foffset++, "remote-mac");
1873 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1874 net2->back_mac[0], net2->back_mac[1],
1875 net2->back_mac[2], net2->back_mac[3],
1876 net2->back_mac[4], net2->back_mac[5]));
1878 flexarray_set(front, foffset++, "filter-mac");
1879 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", !!(net2->filter_mac)));
1880 flexarray_set(front, foffset++, "state");
1881 flexarray_set(front, foffset++, "1");
1883 libxl_device_generic_add(ctx, &device,
1884 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1885 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1887 /* FIXME: wait for plug */
1888 flexarray_free(back);
1889 flexarray_free(front);
1890 return 0;
1893 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1895 char *dompath, *net2_path_fe;
1896 char **l;
1897 char *val, *tok;
1898 unsigned int nb_net2s, i;
1899 libxl_net2info *res, *net2s;
1901 dompath = libxl_xs_get_dompath(ctx, domid);
1902 if (!dompath) {
1903 return NULL;
1905 l = libxl_xs_directory(ctx, XBT_NULL,
1906 libxl_sprintf(ctx, "%s/device/vif2", dompath), &nb_net2s);
1907 if (!l) {
1908 return NULL;
1910 res = libxl_calloc(ctx, nb_net2s, sizeof (libxl_net2info));
1911 if (!res) {
1912 libxl_free(ctx, l);
1913 return NULL;
1915 net2s = res;
1916 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
1917 net2_path_fe = libxl_sprintf(ctx, "%s/device/vif2/%s", dompath, *l);
1919 net2s->backend = libxl_xs_read(ctx, XBT_NULL,
1920 libxl_sprintf(ctx, "%s/backend", net2_path_fe));
1921 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", net2_path_fe));
1922 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
1924 net2s->devid = strtoul(*l, NULL, 10);
1925 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", net2_path_fe));
1926 net2s->state = val ? strtoul(val, NULL, 10) : -1;
1928 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/mac", net2_path_fe));
1929 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1930 ++i, tok = strtok(NULL, ":")) {
1931 net2s->mac[i] = strtoul(tok, NULL, 16);
1933 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/remote-trusted", net2_path_fe));
1934 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
1936 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/remote-mac", net2_path_fe));
1937 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1938 ++i, tok = strtok(NULL, ":")) {
1939 net2s->back_mac[i] = strtoul(tok, NULL, 16);
1941 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/filter-mac", net2_path_fe));
1942 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
1944 net2s->frontend = libxl_xs_read(ctx, XBT_NULL,
1945 libxl_sprintf(ctx, "%s/frontend", net2s->backend));
1946 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", net2s->backend));
1947 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1948 libxl_free(ctx, net2_path_fe);
1951 libxl_free(ctx, l);
1952 return res;
1955 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
1957 libxl_device device;
1959 device.backend_devid = net2->devid;
1960 device.backend_domid = net2->backend_domid;
1961 device.backend_kind = DEVICE_VIF2;
1962 device.devid = net2->devid;
1963 device.domid = net2->domid;
1964 device.kind = DEVICE_VIF2;
1966 return libxl_device_del(ctx, &device, wait);
1970 /******************************************************************************/
1971 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1973 flexarray_t *front;
1974 flexarray_t *back;
1975 unsigned int boffset = 0;
1976 unsigned int foffset = 0;
1977 libxl_device device;
1979 if (console->build_state) {
1980 xs_transaction_t t;
1981 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
1982 ents[0] = "console/port";
1983 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
1984 ents[2] = "console/ring-ref";
1985 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
1986 ents[4] = "console/limit";
1987 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
1988 ents[6] = "console/type";
1989 if (console->constype == CONSTYPE_XENCONSOLED)
1990 ents[7] = "xenconsoled";
1991 else
1992 ents[7] = "ioemu";
1993 retry_transaction:
1994 t = xs_transaction_start(ctx->xsh);
1995 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
1996 if (!xs_transaction_end(ctx->xsh, t, 0))
1997 if (errno == EAGAIN)
1998 goto retry_transaction;
2001 front = flexarray_make(16, 1);
2002 if (!front)
2003 return ERROR_NOMEM;
2004 back = flexarray_make(16, 1);
2005 if (!back)
2006 return ERROR_NOMEM;
2008 device.backend_devid = console->devid;
2009 device.backend_domid = console->backend_domid;
2010 device.backend_kind = DEVICE_CONSOLE;
2011 device.devid = console->devid;
2012 device.domid = console->domid;
2013 device.kind = DEVICE_CONSOLE;
2015 flexarray_set(back, boffset++, "frontend-id");
2016 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
2017 flexarray_set(back, boffset++, "online");
2018 flexarray_set(back, boffset++, "1");
2019 flexarray_set(back, boffset++, "state");
2020 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
2021 flexarray_set(back, boffset++, "domain");
2022 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
2023 flexarray_set(back, boffset++, "protocol");
2024 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
2026 flexarray_set(front, foffset++, "backend-id");
2027 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
2028 flexarray_set(front, foffset++, "state");
2029 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
2030 flexarray_set(front, foffset++, "limit");
2031 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
2032 flexarray_set(front, foffset++, "protocol");
2033 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
2034 flexarray_set(front, foffset++, "type");
2035 if (console->constype == CONSTYPE_XENCONSOLED)
2036 flexarray_set(front, foffset++, "xenconsoled");
2037 else
2038 flexarray_set(front, foffset++, "ioemu");
2040 libxl_device_generic_add(ctx, &device,
2041 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
2042 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
2043 flexarray_free(back);
2044 flexarray_free(front);
2046 return 0;
2049 /******************************************************************************/
2050 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
2052 flexarray_t *front;
2053 flexarray_t *back;
2054 unsigned int boffset = 0;
2055 unsigned int foffset = 0;
2056 libxl_device device;
2058 front = flexarray_make(16, 1);
2059 if (!front)
2060 return ERROR_NOMEM;
2061 back = flexarray_make(16, 1);
2062 if (!back)
2063 return ERROR_NOMEM;
2065 device.backend_devid = vkb->devid;
2066 device.backend_domid = vkb->backend_domid;
2067 device.backend_kind = DEVICE_VKBD;
2068 device.devid = vkb->devid;
2069 device.domid = vkb->domid;
2070 device.kind = DEVICE_VKBD;
2072 flexarray_set(back, boffset++, "frontend-id");
2073 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
2074 flexarray_set(back, boffset++, "online");
2075 flexarray_set(back, boffset++, "1");
2076 flexarray_set(back, boffset++, "state");
2077 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
2078 flexarray_set(back, boffset++, "domain");
2079 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
2081 flexarray_set(front, foffset++, "backend-id");
2082 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
2083 flexarray_set(front, foffset++, "state");
2084 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
2086 libxl_device_generic_add(ctx, &device,
2087 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
2088 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
2089 flexarray_free(back);
2090 flexarray_free(front);
2092 return 0;
2095 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2097 return ERROR_NI;
2100 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2102 return ERROR_NI;
2105 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
2107 char *be_path_tap, *be_path_vbd;
2108 libxl_device_disk *dend, *disks, *ret = NULL;
2109 char **b, **l = NULL;
2110 unsigned int numl;
2111 char *type;
2113 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
2114 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
2116 b = l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
2117 if (l) {
2118 ret = realloc(ret, sizeof(libxl_device_disk) * numl);
2119 disks = ret;
2120 *num = numl;
2121 dend = ret + *num;
2122 for (; disks < dend; ++disks, ++l) {
2123 disks->backend_domid = 0;
2124 disks->domid = domid;
2125 disks->physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, *l));
2126 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_vbd, *l)), &(disks->phystype));
2127 disks->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, *l));
2128 disks->unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, *l)));
2129 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, *l)), "w"))
2130 disks->readwrite = 1;
2131 else
2132 disks->readwrite = 0;
2133 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))));
2134 disks->is_cdrom = !strcmp(type, "cdrom");
2136 libxl_free(ctx, b);
2138 b = l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
2139 if (l) {
2140 ret = realloc(ret, sizeof(libxl_device_disk) * (*num + numl));
2141 disks = ret + *num;
2142 *num += numl;
2143 for (dend = ret + *num; disks < dend; ++disks, ++l) {
2144 disks->backend_domid = 0;
2145 disks->domid = domid;
2146 disks->physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, *l));
2147 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_tap, *l)), &(disks->phystype));
2148 disks->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, *l));
2149 disks->unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, *l)));
2150 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, *l)), "w"))
2151 disks->readwrite = 1;
2152 else
2153 disks->readwrite = 0;
2154 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))));
2155 disks->is_cdrom = !strcmp(type, "cdrom");
2157 libxl_free(ctx, b);
2159 return ret;
2162 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
2163 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
2165 char *dompath, *diskpath;
2166 char *val;
2168 dompath = libxl_xs_get_dompath(ctx, domid);
2169 diskinfo->devid = device_disk_dev_number(disk->virtpath);
2171 /* tap devices entries in xenstore are written as vbd devices. */
2172 diskpath = libxl_sprintf(ctx, "%s/device/vbd/%d", dompath, diskinfo->devid);
2173 diskinfo->backend = libxl_xs_read(ctx, XBT_NULL,
2174 libxl_sprintf(ctx, "%s/backend", diskpath));
2175 if (!diskinfo->backend) {
2176 return ERROR_FAIL;
2178 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend-id", diskpath));
2179 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
2180 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/state", diskpath));
2181 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
2182 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/event-channel", diskpath));
2183 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
2184 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/ring-ref", diskpath));
2185 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
2186 diskinfo->frontend = libxl_xs_read(ctx, XBT_NULL,
2187 libxl_sprintf(ctx, "%s/frontend", diskinfo->backend));
2188 val = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend-id", diskinfo->backend));
2189 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2191 return 0;
2194 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
2196 int num, i;
2197 uint32_t stubdomid;
2198 libxl_device_disk *disks;
2200 if (!disk->physpath) {
2201 disk->physpath = "";
2202 disk->phystype = PHYSTYPE_PHY;
2204 disks = libxl_device_disk_list(ctx, domid, &num);
2205 for (i = 0; i < num; i++) {
2206 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
2207 /* found */
2208 break;
2210 if (i == num) {
2211 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
2212 return ERROR_FAIL;
2214 libxl_device_disk_del(ctx, disks + i, 1);
2215 libxl_device_disk_add(ctx, domid, disk);
2216 stubdomid = libxl_get_stubdom_id(ctx, domid);
2217 if (stubdomid) {
2218 disks[i].domid = stubdomid;
2219 libxl_device_disk_del(ctx, disks + i, 1);
2220 disk->domid = stubdomid;
2221 libxl_device_disk_add(ctx, stubdomid, disk);
2222 disk->domid = domid;
2224 return 0;
2227 /******************************************************************************/
2228 static int libxl_build_xenpv_qemu_args(libxl_ctx *ctx,
2229 libxl_device_vfb *vfb,
2230 int num_console,
2231 libxl_device_console *console,
2232 libxl_device_model_info *info)
2234 int i = 0, j = 0, num = 0;
2235 memset(info, 0x00, sizeof(libxl_device_model_info));
2237 info->vnc = vfb->vnc;
2238 if (vfb->vnclisten)
2239 info->vnclisten = libxl_strdup(ctx, vfb->vnclisten);
2240 info->vncdisplay = vfb->vncdisplay;
2241 info->vncunused = vfb->vncunused;
2242 if (vfb->vncpasswd)
2243 info->vncpasswd = vfb->vncpasswd;
2244 if (vfb->keymap)
2245 info->keymap = libxl_strdup(ctx, vfb->keymap);
2246 info->sdl = vfb->sdl;
2247 info->opengl = vfb->opengl;
2248 for (i = 0; i < num_console; i++) {
2249 if (console->constype == CONSTYPE_IOEMU)
2250 num++;
2252 if (num > 0) {
2253 uint32_t guest_domid;
2254 if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
2255 char *filename;
2256 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
2257 libxl_create_logfile(ctx, name, &filename);
2258 info->serial = libxl_sprintf(ctx, "file:%s", filename);
2259 free(filename);
2260 } else {
2261 info->serial = "pty";
2263 num--;
2265 if (num > 0) {
2266 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
2267 for (j = 0; j < num * 2; j = j + 2) {
2268 info->extra[j] = "-serial";
2269 info->extra[j + 1] = "pty";
2271 info->extra[j] = NULL;
2273 info->domid = vfb->domid;
2274 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
2275 info->device_model = libxl_abs_path(ctx, "qemu-dm", libxl_libexec_path());
2276 info->type = XENPV;
2277 return 0;
2280 int libxl_create_xenpv_qemu(libxl_ctx *ctx, libxl_device_vfb *vfb,
2281 int num_console, libxl_device_console *console,
2282 libxl_device_model_starting **starting_r)
2284 libxl_device_model_info info;
2286 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
2287 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
2288 return 0;
2291 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
2293 flexarray_t *front;
2294 flexarray_t *back;
2295 unsigned int boffset = 0;
2296 unsigned int foffset = 0;
2297 libxl_device device;
2299 front = flexarray_make(16, 1);
2300 if (!front)
2301 return ERROR_NOMEM;
2302 back = flexarray_make(16, 1);
2303 if (!back)
2304 return ERROR_NOMEM;
2306 device.backend_devid = vfb->devid;
2307 device.backend_domid = vfb->backend_domid;
2308 device.backend_kind = DEVICE_VFB;
2309 device.devid = vfb->devid;
2310 device.domid = vfb->domid;
2311 device.kind = DEVICE_VFB;
2313 flexarray_set(back, boffset++, "frontend-id");
2314 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
2315 flexarray_set(back, boffset++, "online");
2316 flexarray_set(back, boffset++, "1");
2317 flexarray_set(back, boffset++, "state");
2318 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
2319 flexarray_set(back, boffset++, "domain");
2320 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
2321 flexarray_set(back, boffset++, "vnc");
2322 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
2323 flexarray_set(back, boffset++, "vnclisten");
2324 flexarray_set(back, boffset++, vfb->vnclisten);
2325 flexarray_set(back, boffset++, "vncpasswd");
2326 flexarray_set(back, boffset++, vfb->vncpasswd);
2327 flexarray_set(back, boffset++, "vncdisplay");
2328 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
2329 flexarray_set(back, boffset++, "vncunused");
2330 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
2331 flexarray_set(back, boffset++, "sdl");
2332 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
2333 flexarray_set(back, boffset++, "opengl");
2334 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
2335 if (vfb->xauthority) {
2336 flexarray_set(back, boffset++, "xauthority");
2337 flexarray_set(back, boffset++, vfb->xauthority);
2339 if (vfb->display) {
2340 flexarray_set(back, boffset++, "display");
2341 flexarray_set(back, boffset++, vfb->display);
2344 flexarray_set(front, foffset++, "backend-id");
2345 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
2346 flexarray_set(front, foffset++, "state");
2347 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
2349 libxl_device_generic_add(ctx, &device,
2350 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
2351 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
2352 flexarray_free(front);
2353 flexarray_free(back);
2355 return 0;
2358 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2360 return ERROR_NI;
2363 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2365 return ERROR_NI;
2368 /******************************************************************************/
2370 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
2372 char *mem, *endptr;
2373 uint32_t memorykb;
2374 char *dompath = libxl_xs_get_dompath(ctx, domid);
2376 mem = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath));
2377 if (!mem) {
2378 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
2379 return 1;
2381 memorykb = strtoul(mem, &endptr, 10);
2382 if (*endptr != '\0') {
2383 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
2384 return 1;
2387 if (max_memkb < memorykb) {
2388 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
2389 return 1;
2392 if (domid != 0)
2393 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/static-max", dompath), "%"PRIu32, max_memkb);
2395 return 0;
2398 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb, int enforce)
2400 int rc = 0;
2401 uint32_t memorykb = 0, videoram = 0;
2402 char *memmax, *endptr, *videoram_s = NULL;
2403 char *dompath = libxl_xs_get_dompath(ctx, domid);
2404 xc_domaininfo_t info;
2405 libxl_dominfo ptr;
2406 char *uuid;
2408 if (domid) {
2409 memmax = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/static-max", dompath));
2410 if (!memmax) {
2411 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
2412 "cannot get memory info from %s/memory/static-max\n", dompath);
2413 return 1;
2415 memorykb = strtoul(memmax, &endptr, 10);
2416 if (*endptr != '\0') {
2417 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
2418 "invalid max memory %s from %s/memory/static-max\n", memmax, dompath);
2419 return 1;
2422 if (target_memkb > memorykb) {
2423 XL_LOG(ctx, XL_LOG_ERROR,
2424 "memory_dynamic_max must be less than or equal to memory_static_max\n");
2425 return 1;
2429 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
2430 videoram = videoram_s ? atoi(videoram_s) : 0;
2432 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%"PRIu32, target_memkb);
2434 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2435 if (rc != 1 || info.domain != domid)
2436 return rc;
2437 xcinfo2xlinfo(&info, &ptr);
2438 uuid = libxl_uuid2string(ctx, ptr.uuid);
2439 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/vm/%s/memory", uuid), "%"PRIu32, target_memkb / 1024);
2441 if (enforce || !domid)
2442 memorykb = target_memkb;
2443 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb + LIBXL_MAXMEM_CONSTANT);
2444 if (rc != 0)
2445 return rc;
2446 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
2447 return rc;
2450 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
2452 int rc = -1;
2454 switch (button) {
2455 case POWER_BUTTON:
2456 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2457 break;
2458 case SLEEP_BUTTON:
2459 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2460 break;
2461 default:
2462 break;
2465 return rc;
2468 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
2470 xc_physinfo_t xcphysinfo = { 0 };
2471 int rc;
2473 rc = xc_physinfo(ctx->xch, &xcphysinfo);
2474 if (rc != 0) {
2475 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting physinfo");
2476 return ERROR_FAIL;
2478 physinfo->threads_per_core = xcphysinfo.threads_per_core;
2479 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
2480 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
2481 physinfo->nr_cpus = xcphysinfo.nr_cpus;
2482 physinfo->cpu_khz = xcphysinfo.cpu_khz;
2483 physinfo->total_pages = xcphysinfo.total_pages;
2484 physinfo->free_pages = xcphysinfo.free_pages;
2485 physinfo->scrub_pages = xcphysinfo.scrub_pages;
2486 physinfo->nr_nodes = xcphysinfo.nr_nodes;
2487 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
2488 physinfo->phys_cap = xcphysinfo.capabilities;
2490 return 0;
2493 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
2495 union {
2496 xen_extraversion_t xen_extra;
2497 xen_compile_info_t xen_cc;
2498 xen_changeset_info_t xen_chgset;
2499 xen_capabilities_info_t xen_caps;
2500 xen_platform_parameters_t p_parms;
2501 xen_commandline_t xen_commandline;
2502 } u;
2503 long xen_version;
2504 libxl_version_info *info = &ctx->version_info;
2506 if (info->xen_version_extra != NULL)
2507 return info;
2509 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
2510 info->xen_version_major = xen_version >> 16;
2511 info->xen_version_minor = xen_version & 0xFF;
2512 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
2513 info->xen_version_extra = libxl_strdup(ctx, u.xen_extra);
2515 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
2516 info->compiler = libxl_strdup(ctx, u.xen_cc.compiler);
2517 info->compile_by = libxl_strdup(ctx, u.xen_cc.compile_by);
2518 info->compile_domain = libxl_strdup(ctx, u.xen_cc.compile_domain);
2519 info->compile_date = libxl_strdup(ctx, u.xen_cc.compile_date);
2521 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
2522 info->capabilities = libxl_strdup(ctx, u.xen_caps);
2524 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
2525 info->changeset = libxl_strdup(ctx, u.xen_chgset);
2527 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
2528 info->virt_start = u.p_parms.virt_start;
2530 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
2532 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
2533 info->commandline = libxl_strdup(ctx, u.xen_commandline);
2535 return info;
2538 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
2539 int *nb_vcpu, int *cpusize)
2541 libxl_vcpuinfo *ptr, *ret;
2542 xc_domaininfo_t domaininfo;
2543 xc_vcpuinfo_t vcpuinfo;
2544 xc_physinfo_t physinfo = { 0 };
2546 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2547 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting infolist");
2548 return NULL;
2550 if (xc_physinfo(ctx->xch, &physinfo) == -1) {
2551 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting physinfo");
2552 return NULL;
2554 *cpusize = physinfo.max_cpu_id + 1;
2555 ptr = libxl_calloc(ctx, domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
2556 if (!ptr) {
2557 return NULL;
2560 ret = ptr;
2561 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
2562 ptr->cpumap = libxl_calloc(ctx, (*cpusize + 63) / 64, sizeof (uint64_t));
2563 if (!ptr->cpumap) {
2564 return NULL;
2566 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
2567 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting vcpu info");
2568 return NULL;
2570 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap, *cpusize) == -1) {
2571 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting vcpu affinity");
2572 return NULL;
2574 ptr->vcpuid = *nb_vcpu;
2575 ptr->cpu = vcpuinfo.cpu;
2576 ptr->online = !!vcpuinfo.online;
2577 ptr->blocked = !!vcpuinfo.blocked;
2578 ptr->running = !!vcpuinfo.running;
2579 ptr->vcpu_time = vcpuinfo.cpu_time;
2581 return ret;
2584 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
2585 uint64_t *cpumap, int cpusize)
2587 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap, cpusize)) {
2588 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting vcpu affinity");
2589 return ERROR_FAIL;
2591 return 0;
2594 int libxl_set_vcpucount(libxl_ctx *ctx, uint32_t domid, uint32_t count)
2596 xc_domaininfo_t domaininfo;
2597 char *dompath;
2598 int i;
2600 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2601 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting domain info list");
2602 return ERROR_FAIL;
2604 if (!count || ((domaininfo.max_vcpu_id + 1) < count)) {
2605 return ERROR_INVAL;
2607 if (!(dompath = libxl_xs_get_dompath(ctx, domid)))
2608 return ERROR_FAIL;
2610 for (i = 0; i <= domaininfo.max_vcpu_id; ++i) {
2611 libxl_xs_write(ctx, XBT_NULL,
2612 libxl_sprintf(ctx, "%s/cpu/%u/availability", dompath, i),
2613 "%s", ((1 << i) & ((1 << count) - 1)) ? "online" : "offline");
2615 return 0;
2618 /*
2619 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
2620 */
2621 int libxl_get_sched_id(libxl_ctx *ctx)
2623 int sched, ret;
2625 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
2626 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting domain info list");
2627 return ERROR_FAIL;
2629 return sched;
2632 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2634 struct xen_domctl_sched_credit sdom;
2635 int rc;
2637 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
2638 if (rc != 0) {
2639 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting domain sched credit");
2640 return ERROR_FAIL;
2643 scinfo->weight = sdom.weight;
2644 scinfo->cap = sdom.cap;
2646 return 0;
2649 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2651 struct xen_domctl_sched_credit sdom;
2652 xc_domaininfo_t domaininfo;
2653 int rc;
2655 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
2656 if (rc < 0) {
2657 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "getting domain info list");
2658 return ERROR_FAIL;
2660 if (rc != 1 || domaininfo.domain != domid)
2661 return ERROR_INVAL;
2664 if (scinfo->weight < 1 || scinfo->weight > 65535) {
2665 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2666 "Cpu weight out of range, valid values are within range from 1 to 65535");
2667 return ERROR_INVAL;
2670 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
2671 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2672 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
2673 ((domaininfo.max_vcpu_id + 1) * 100));
2674 return ERROR_INVAL;
2677 sdom.weight = scinfo->weight;
2678 sdom.cap = scinfo->cap;
2680 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
2681 if ( rc < 0 ) {
2682 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "setting domain sched credit");
2683 return ERROR_FAIL;
2686 return 0;
2689 static int trigger_type_from_string(char *trigger_name)
2691 if (!strcmp(trigger_name, "nmi"))
2692 return XEN_DOMCTL_SENDTRIGGER_NMI;
2693 else if (!strcmp(trigger_name, "reset"))
2694 return XEN_DOMCTL_SENDTRIGGER_RESET;
2695 else if (!strcmp(trigger_name, "init"))
2696 return XEN_DOMCTL_SENDTRIGGER_INIT;
2697 else if (!strcmp(trigger_name, "power"))
2698 return XEN_DOMCTL_SENDTRIGGER_POWER;
2699 else if (!strcmp(trigger_name, "sleep"))
2700 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
2701 else
2702 return -1;
2705 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
2707 int rc = -1;
2708 int trigger_type = trigger_type_from_string(trigger_name);
2710 if (trigger_type == -1) {
2711 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
2712 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
2713 return ERROR_INVAL;
2716 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
2717 if (rc != 0) {
2718 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2719 "Send trigger '%s' failed", trigger_name);
2720 return ERROR_FAIL;
2723 return 0;
2726 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
2728 char *dompath = libxl_xs_get_dompath(ctx, domid);
2730 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/control/sysrq", dompath), "%c", sysrq);
2732 return 0;
2735 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
2737 int ret;
2738 ret = xc_send_debug_keys(ctx->xch, keys);
2739 if ( ret < 0 ) {
2740 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "sending debug keys");
2741 return ERROR_FAIL;
2743 return 0;
2746 libxl_xen_console_reader *
2747 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
2749 libxl_xen_console_reader *cr;
2750 unsigned int size = 16384;
2751 char *buf = malloc(size);
2753 if (!buf) {
2754 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
2755 " size is %u", size);
2756 return NULL;
2759 cr = malloc(sizeof(libxl_xen_console_reader));
2760 if (!cr) {
2761 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "cannot malloc libxl_xen_console_reader");
2762 return NULL;
2765 memset(cr, 0, sizeof(libxl_xen_console_reader));
2766 cr->buffer = buf;
2767 cr->size = size;
2768 cr->count = size;
2769 cr->clear = clear;
2770 cr->incremental = 1;
2772 return cr;
2775 /* return values: *line_r
2776 * 1 success, whole line obtained from buffer non-0
2777 * 0 no more lines available right now 0
2778 * negative error code ERROR_* 0
2779 * On success *line_r is updated to point to a nul-terminated
2780 * string which is valid until the next call on the same console
2781 * reader. The libxl caller may overwrite parts of the string
2782 * if it wishes. */
2783 int libxl_xen_console_read_line(libxl_ctx *ctx,
2784 libxl_xen_console_reader *cr,
2785 char **line_r)
2787 int ret;
2789 memset(cr->buffer, 0, cr->size);
2790 ret = xc_readconsolering(ctx->xch, &cr->buffer, &cr->count,
2791 cr->clear, cr->incremental, &cr->index);
2792 if (ret < 0) {
2793 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "reading console ring buffer");
2794 return ERROR_FAIL;
2796 if (!ret) {
2797 if (cr->count) {
2798 *line_r = cr->buffer;
2799 ret = 1;
2800 } else {
2801 *line_r = NULL;
2802 ret = 0;
2806 return ret;
2809 void libxl_xen_console_read_finish(libxl_ctx *ctx,
2810 libxl_xen_console_reader *cr)
2812 free(cr->buffer);
2813 free(cr);
2816 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
2818 char *dompath = libxl_xs_get_dompath(ctx, domid);
2819 char *vm_path, *start_time;
2821 vm_path = libxl_xs_read(
2822 ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vm", dompath));
2823 start_time = libxl_xs_read(
2824 ctx, XBT_NULL, libxl_sprintf(ctx, "%s/start_time", vm_path));
2825 if (start_time == NULL) {
2826 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
2827 "Can't get start time of domain '%d'", domid);
2828 return -1;
2831 return strtoul(start_time, NULL, 10);
2834 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
2836 int rc;
2837 char _buf[32768];
2839 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
2840 0, _buf);
2841 if (rc < 0) {
2842 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2843 "Can not get tmem list");
2844 return NULL;
2847 return strdup(_buf);
2850 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
2852 int rc;
2854 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
2855 0, NULL);
2856 if (rc < 0) {
2857 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2858 "Can not freeze tmem pools");
2859 return ERROR_FAIL;
2862 return rc;
2865 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
2867 int rc;
2869 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
2870 0, NULL);
2871 if (rc < 0) {
2872 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2873 "Can not destroy tmem pools");
2874 return ERROR_FAIL;
2877 return rc;
2880 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
2882 int rc;
2884 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
2885 0, NULL);
2886 if (rc < 0) {
2887 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2888 "Can not thaw tmem pools");
2889 return ERROR_FAIL;
2892 return rc;
2895 static int32_t tmem_setop_from_string(char *set_name)
2897 if (!strcmp(set_name, "weight"))
2898 return TMEMC_SET_WEIGHT;
2899 else if (!strcmp(set_name, "cap"))
2900 return TMEMC_SET_CAP;
2901 else if (!strcmp(set_name, "compress"))
2902 return TMEMC_SET_COMPRESS;
2903 else
2904 return -1;
2907 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
2909 int rc;
2910 int32_t subop = tmem_setop_from_string(name);
2912 if (subop == -1) {
2913 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1,
2914 "Invalid set, valid sets are <weight|cap|compress>");
2915 return ERROR_INVAL;
2917 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
2918 if (rc < 0) {
2919 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2920 "Can not set tmem %s", name);
2921 return ERROR_FAIL;
2924 return rc;
2927 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
2928 char* uuid, int auth)
2930 int rc;
2932 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
2933 if (rc < 0) {
2934 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2935 "Can not set tmem shared auth");
2936 return ERROR_FAIL;
2939 return rc;
2942 int libxl_tmem_freeable(libxl_ctx *ctx)
2944 int rc;
2946 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
2947 if (rc < 0) {
2948 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc,
2949 "Can not get tmem freeable memory");
2950 return ERROR_FAIL;
2953 return rc;
2956 int libxl_file_reference_map(libxl_ctx *ctx, libxl_file_reference *f)
2958 struct stat st_buf;
2959 int ret, fd;
2960 void *data;
2962 if (f->mapped)
2963 return 0;
2965 fd = open(f->path, O_RDONLY);
2966 if (f < 0)
2967 return ERROR_FAIL;
2969 ret = fstat(fd, &st_buf);
2970 if (ret < 0)
2971 goto out;
2973 ret = -1;
2974 data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2975 if (data == NULL)
2976 goto out;
2978 f->mapped = 1;
2979 f->data = data;
2980 f->size = st_buf.st_size;
2982 ret = 0;
2983 out:
2984 close(fd);
2986 return ret == 0 ? 0 : ERROR_FAIL;
2989 int libxl_file_reference_unmap(libxl_ctx *ctx, libxl_file_reference *f)
2991 int ret;
2993 if (!f->mapped)
2994 return 0;
2996 ret = munmap(f->data, f->size);
2998 return ret == 0 ? 0 : ERROR_FAIL;