debuggers.hg

view tools/libxl/libxl.c @ 22854:6ec61438713a

libxl/xl: support empty disk paths (ejected cdroms)

Support empty disk paths, including in domain config files, for
compatibility with xend. This allows a domain to be created with a
cdrom which is initially in the "ejected" (no cd present) state.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
date Tue Jan 25 18:06:53 2011 +0000 (2011-01-25)
parents 787af706dadc
children 1d1eec7e1fb4
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/wait.h>
27 #include <sys/time.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"
39 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
41 int libxl_ctx_init(libxl_ctx *ctx, int version, xentoollog_logger *lg)
42 {
43 struct stat stat_buf;
45 if (version != LIBXL_VERSION)
46 return ERROR_VERSION;
47 memset(ctx, 0, sizeof(libxl_ctx));
48 ctx->lg = lg;
49 memset(&ctx->version_info, 0, sizeof(libxl_version_info));
51 if ( stat(XENSTORE_PID_FILE, &stat_buf) != 0 ) {
52 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Is xenstore daemon running?\n"
53 "failed to stat %s", XENSTORE_PID_FILE);
54 return ERROR_FAIL;
55 }
57 ctx->xch = xc_interface_open(lg,lg,0);
58 if (!ctx->xch) {
59 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
60 "cannot open libxc handle");
61 return ERROR_FAIL;
62 }
64 ctx->xsh = xs_daemon_open();
65 if (!ctx->xsh)
66 ctx->xsh = xs_domain_open();
67 if (!ctx->xsh) {
68 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
69 "cannot connect to xenstore");
70 xc_interface_close(ctx->xch);
71 return ERROR_FAIL;
72 }
73 return 0;
74 }
76 int libxl_ctx_free(libxl_ctx *ctx)
77 {
78 if (ctx->xch) xc_interface_close(ctx->xch);
79 libxl_version_info_destroy(&ctx->version_info);
80 if (ctx->xsh) xs_daemon_close(ctx->xsh);
81 return 0;
82 }
84 void libxl_string_list_destroy(libxl_string_list *psl)
85 {
86 int i;
87 libxl_string_list sl = *psl;
89 if (!sl)
90 return;
92 for (i = 0; sl[i] != NULL; i++)
93 free(sl[i]);
94 free(sl);
95 }
97 void libxl_key_value_list_destroy(libxl_key_value_list *pkvl)
98 {
99 int i;
100 libxl_key_value_list kvl = *pkvl;
102 if (!kvl)
103 return;
105 for (i = 0; kvl[i] != NULL; i += 2) {
106 free(kvl[i]);
107 if (kvl[i + 1])
108 free(kvl[i + 1]);
109 }
110 free(kvl);
111 }
113 /******************************************************************************/
116 int libxl_domain_rename(libxl_ctx *ctx, uint32_t domid,
117 const char *old_name, const char *new_name,
118 xs_transaction_t trans)
119 {
120 libxl__gc gc = LIBXL_INIT_GC(ctx);
121 char *dom_path = 0;
122 const char *name_path;
123 char *got_old_name;
124 unsigned int got_old_len;
125 xs_transaction_t our_trans = 0;
126 int rc;
128 dom_path = libxl__xs_get_dompath(&gc, domid);
129 if (!dom_path) goto x_nomem;
131 name_path= libxl__sprintf(&gc, "%s/name", dom_path);
132 if (!name_path) goto x_nomem;
134 retry_transaction:
135 if (!trans) {
136 trans = our_trans = xs_transaction_start(ctx->xsh);
137 if (!our_trans) {
138 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
139 "create xs transaction for domain (re)name");
140 goto x_fail;
141 }
142 }
144 if (old_name) {
145 got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len);
146 if (!got_old_name) {
147 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno, "check old name"
148 " for domain %"PRIu32" allegedly named `%s'",
149 domid, old_name);
150 goto x_fail;
151 }
152 if (strcmp(old_name, got_old_name)) {
153 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "domain %"PRIu32" allegedly named "
154 "`%s' is actually named `%s' - racing ?",
155 domid, old_name, got_old_name);
156 free(got_old_name);
157 goto x_fail;
158 }
159 free(got_old_name);
160 }
161 if (!xs_write(ctx->xsh, trans, name_path,
162 new_name, strlen(new_name))) {
163 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "failed to write new name `%s'"
164 " for domain %"PRIu32" previously named `%s'",
165 new_name, domid, old_name);
166 goto x_fail;
167 }
169 if (our_trans) {
170 if (!xs_transaction_end(ctx->xsh, our_trans, 0)) {
171 trans = our_trans = 0;
172 if (errno != EAGAIN) {
173 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "failed to commit new name `%s'"
174 " for domain %"PRIu32" previously named `%s'",
175 new_name, domid, old_name);
176 goto x_fail;
177 }
178 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "need to retry rename transaction"
179 " for domain %"PRIu32" (name_path=\"%s\", new_name=\"%s\")",
180 domid, name_path, new_name);
181 goto retry_transaction;
182 }
183 our_trans = 0;
184 }
186 rc = 0;
187 x_rc:
188 if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1);
189 libxl__free_all(&gc);
190 return rc;
192 x_fail: rc = ERROR_FAIL; goto x_rc;
193 x_nomem: rc = ERROR_NOMEM; goto x_rc;
194 }
196 int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid)
197 {
198 libxl__gc gc = LIBXL_INIT_GC(ctx);
199 int rc = 0;
201 if (libxl__domain_is_hvm(ctx, domid)) {
202 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Called domain_resume on "
203 "non-cooperative hvm domain %u", domid);
204 rc = ERROR_NI;
205 goto out;
206 }
207 if (xc_domain_resume(ctx->xch, domid, 1)) {
208 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
209 "xc_domain_resume failed for domain %u",
210 domid);
211 rc = ERROR_FAIL;
212 goto out;
213 }
214 if (!xs_resume_domain(ctx->xsh, domid)) {
215 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
216 "xs_resume_domain failed for domain %u",
217 domid);
218 rc = ERROR_FAIL;
219 }
220 out:
221 libxl__free_all(&gc);
222 return 0;
223 }
225 /*
226 * Preserves a domain but rewrites xenstore etc to make it unique so
227 * that the domain can be restarted.
228 *
229 * Does not modify info so that it may be reused.
230 */
231 int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid,
232 libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid)
233 {
234 libxl__gc gc = LIBXL_INIT_GC(ctx);
235 struct xs_permissions roperm[2];
236 xs_transaction_t t;
237 char *preserved_name;
238 char *uuid_string;
239 char *vm_path;
240 char *dom_path;
242 int rc;
244 preserved_name = libxl__sprintf(&gc, "%s%s", info->name, name_suffix);
245 if (!preserved_name) {
246 libxl__free_all(&gc);
247 return ERROR_NOMEM;
248 }
250 uuid_string = libxl__uuid2string(&gc, new_uuid);
251 if (!uuid_string) {
252 libxl__free_all(&gc);
253 return ERROR_NOMEM;
254 }
256 dom_path = libxl__xs_get_dompath(&gc, domid);
257 if (!dom_path) {
258 libxl__free_all(&gc);
259 return ERROR_FAIL;
260 }
262 vm_path = libxl__sprintf(&gc, "/vm/%s", uuid_string);
263 if (!vm_path) {
264 libxl__free_all(&gc);
265 return ERROR_FAIL;
266 }
268 roperm[0].id = 0;
269 roperm[0].perms = XS_PERM_NONE;
270 roperm[1].id = domid;
271 roperm[1].perms = XS_PERM_READ;
273 retry_transaction:
274 t = xs_transaction_start(ctx->xsh);
276 xs_rm(ctx->xsh, t, vm_path);
277 xs_mkdir(ctx->xsh, t, vm_path);
278 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
280 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/vm", dom_path), vm_path, strlen(vm_path));
281 rc = libxl_domain_rename(ctx, domid, info->name, preserved_name, t);
282 if (rc) {
283 libxl__free_all(&gc);
284 return rc;
285 }
287 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
289 if (!xs_transaction_end(ctx->xsh, t, 0))
290 if (errno == EAGAIN)
291 goto retry_transaction;
293 libxl__free_all(&gc);
294 return 0;
295 }
297 static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
298 libxl_dominfo *xlinfo)
299 {
300 memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
301 xlinfo->domid = xcinfo->domain;
303 xlinfo->dying = !!(xcinfo->flags&XEN_DOMINF_dying);
304 xlinfo->shutdown = !!(xcinfo->flags&XEN_DOMINF_shutdown);
305 xlinfo->paused = !!(xcinfo->flags&XEN_DOMINF_paused);
306 xlinfo->blocked = !!(xcinfo->flags&XEN_DOMINF_blocked);
307 xlinfo->running = !!(xcinfo->flags&XEN_DOMINF_running);
309 if (xlinfo->shutdown || xlinfo->dying)
310 xlinfo->shutdown_reason = (xcinfo->flags>>XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask;
311 else
312 xlinfo->shutdown_reason = ~0;
314 xlinfo->current_memkb = PAGE_TO_MEMKB(xcinfo->tot_pages);
315 xlinfo->max_memkb = PAGE_TO_MEMKB(xcinfo->max_pages);
316 xlinfo->cpu_time = xcinfo->cpu_time;
317 xlinfo->vcpu_max_id = xcinfo->max_vcpu_id;
318 xlinfo->vcpu_online = xcinfo->nr_online_vcpus;
319 }
321 libxl_dominfo * libxl_list_domain(libxl_ctx *ctx, int *nb_domain)
322 {
323 libxl_dominfo *ptr;
324 int i, ret;
325 xc_domaininfo_t info[1024];
326 int size = 1024;
328 ptr = calloc(size, sizeof(libxl_dominfo));
329 if (!ptr) {
330 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating domain info");
331 return NULL;
332 }
334 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
335 if (ret<0) {
336 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
337 return NULL;
338 }
340 for (i = 0; i < ret; i++) {
341 xcinfo2xlinfo(&info[i], &ptr[i]);
342 }
343 *nb_domain = ret;
344 return ptr;
345 }
347 int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
348 uint32_t domid) {
349 xc_domaininfo_t xcinfo;
350 int ret;
352 ret = xc_domain_getinfolist(ctx->xch, domid, 1, &xcinfo);
353 if (ret<0) {
354 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
355 return ERROR_FAIL;
356 }
357 if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
359 xcinfo2xlinfo(&xcinfo, info_r);
360 return 0;
361 }
363 libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool)
364 {
365 libxl_cpupoolinfo *ptr, *tmp;
366 int i;
367 xc_cpupoolinfo_t *info;
368 uint32_t poolid;
370 ptr = NULL;
372 poolid = 0;
373 for (i = 0;; i++) {
374 info = xc_cpupool_getinfo(ctx->xch, poolid);
375 if (info == NULL)
376 break;
377 tmp = realloc(ptr, (i + 1) * sizeof(libxl_cpupoolinfo));
378 if (!tmp) {
379 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpupool info");
380 free(ptr);
381 xc_cpupool_infofree(ctx->xch, info);
382 return NULL;
383 }
384 ptr = tmp;
385 ptr[i].poolid = info->cpupool_id;
386 ptr[i].sched_id = info->sched_id;
387 ptr[i].n_dom = info->n_dom;
388 if (libxl_cpumap_alloc(ctx, &ptr[i].cpumap)) {
389 xc_cpupool_infofree(ctx->xch, info);
390 break;
391 }
392 memcpy(ptr[i].cpumap.map, info->cpumap, ptr[i].cpumap.size);
393 poolid = info->cpupool_id + 1;
394 xc_cpupool_infofree(ctx->xch, info);
395 }
397 *nb_pool = i;
398 return ptr;
399 }
401 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
402 libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm)
403 {
404 libxl_vminfo *ptr;
405 int index, i, ret;
406 xc_domaininfo_t info[1024];
407 int size = 1024;
409 ptr = calloc(size, sizeof(libxl_dominfo));
410 if (!ptr)
411 return NULL;
413 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
414 if (ret<0) {
415 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
416 return NULL;
417 }
418 for (index = i = 0; i < ret; i++) {
419 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
420 continue;
421 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
422 ptr[index].domid = info[i].domain;
424 index++;
425 }
426 *nb_vm = index;
427 return ptr;
428 }
430 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
431 uint32_t domid, int fd)
432 {
433 int hvm = libxl__domain_is_hvm(ctx, domid);
434 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
435 int debug = info != NULL && info->flags & XL_SUSPEND_DEBUG;
436 int rc = 0;
438 rc = libxl__domain_suspend_common(ctx, domid, fd, hvm, live, debug);
439 if (!rc && hvm)
440 rc = libxl__domain_save_device_model(ctx, domid, fd);
441 return rc;
442 }
444 int libxl_domain_pause(libxl_ctx *ctx, uint32_t domid)
445 {
446 int ret;
447 ret = xc_domain_pause(ctx->xch, domid);
448 if (ret<0) {
449 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "pausing domain %d", domid);
450 return ERROR_FAIL;
451 }
452 return 0;
453 }
455 int libxl_domain_core_dump(libxl_ctx *ctx, uint32_t domid,
456 const char *filename)
457 {
458 int ret;
459 ret = xc_domain_dumpcore(ctx->xch, domid, filename);
460 if (ret<0) {
461 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "core dumping domain %d to %s",
462 domid, filename);
463 return ERROR_FAIL;
464 }
465 return 0;
466 }
468 int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
469 {
470 libxl__gc gc = LIBXL_INIT_GC(ctx);
471 char *path;
472 char *state;
473 int ret, rc = 0;
475 if (libxl__domain_is_hvm(ctx, domid)) {
476 path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d/state", domid);
477 state = libxl__xs_read(&gc, XBT_NULL, path);
478 if (state != NULL && !strcmp(state, "paused")) {
479 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d/command", domid), "continue");
480 libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL);
481 }
482 }
483 ret = xc_domain_unpause(ctx->xch, domid);
484 if (ret<0) {
485 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain %d", domid);
486 rc = ERROR_FAIL;
487 }
488 libxl__free_all(&gc);
489 return rc;
490 }
492 static char *req_table[] = {
493 [0] = "poweroff",
494 [1] = "reboot",
495 [2] = "suspend",
496 [3] = "crash",
497 [4] = "halt",
498 };
500 int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
501 {
502 libxl__gc gc = LIBXL_INIT_GC(ctx);
503 char *shutdown_path;
504 char *dom_path;
506 if (req > ARRAY_SIZE(req_table)) {
507 libxl__free_all(&gc);
508 return ERROR_INVAL;
509 }
511 dom_path = libxl__xs_get_dompath(&gc, domid);
512 if (!dom_path) {
513 libxl__free_all(&gc);
514 return ERROR_FAIL;
515 }
517 if (libxl__domain_is_hvm(ctx,domid)) {
518 unsigned long pvdriver = 0;
519 int ret;
520 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
521 if (ret<0) {
522 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
523 libxl__free_all(&gc);
524 return ERROR_FAIL;
525 }
526 if (!pvdriver) {
527 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "HVM domain without PV drivers:"
528 " graceful shutdown not possible, use destroy");
529 libxl__free_all(&gc);
530 return ERROR_FAIL;
531 }
532 }
534 shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
535 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
537 libxl__free_all(&gc);
538 return 0;
539 }
541 int libxl_get_wait_fd(libxl_ctx *ctx, int *fd)
542 {
543 *fd = xs_fileno(ctx->xsh);
544 return 0;
545 }
547 int libxl_wait_for_domain_death(libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
548 {
549 waiter->path = strdup("@releaseDomain");
550 if (asprintf(&(waiter->token), "%d", LIBXL_EVENT_DOMAIN_DEATH) < 0)
551 return -1;
552 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
553 return -1;
554 return 0;
555 }
557 int libxl_wait_for_disk_ejects(libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
558 {
559 libxl__gc gc = LIBXL_INIT_GC(ctx);
560 int i, rc = -1;
561 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
563 if (!domid)
564 domid = guest_domid;
566 for (i = 0; i < num_disks; i++) {
567 if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
568 libxl__xs_get_dompath(&gc, domid),
569 libxl__device_disk_dev_number(disks[i].virtpath)) < 0)
570 goto out;
571 if (asprintf(&(waiter[i].token), "%d", LIBXL_EVENT_DISK_EJECT) < 0)
572 goto out;
573 xs_watch(ctx->xsh, waiter->path, waiter->token);
574 }
575 rc = 0;
576 out:
577 libxl__free_all(&gc);
578 return rc;
579 }
581 int libxl_get_event(libxl_ctx *ctx, libxl_event *event)
582 {
583 unsigned int num;
584 char **events = xs_read_watch(ctx->xsh, &num);
585 if (num != 2) {
586 free(events);
587 return ERROR_FAIL;
588 }
589 event->path = strdup(events[XS_WATCH_PATH]);
590 event->token = strdup(events[XS_WATCH_TOKEN]);
591 event->type = atoi(event->token);
592 free(events);
593 return 0;
594 }
596 int libxl_stop_waiting(libxl_ctx *ctx, libxl_waiter *waiter)
597 {
598 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
599 return ERROR_FAIL;
600 else
601 return 0;
602 }
604 int libxl_free_event(libxl_event *event)
605 {
606 free(event->path);
607 free(event->token);
608 return 0;
609 }
611 int libxl_free_waiter(libxl_waiter *waiter)
612 {
613 free(waiter->path);
614 free(waiter->token);
615 return 0;
616 }
618 int libxl_event_get_domain_death_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_dominfo *info)
619 {
620 if (libxl_domain_info(ctx, info, domid) < 0)
621 return 0;
623 if (info->running || (!info->shutdown && !info->dying))
624 return ERROR_INVAL;
626 return 1;
627 }
629 int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
630 {
631 libxl__gc gc = LIBXL_INIT_GC(ctx);
632 char *path;
633 char *backend;
634 char *value;
636 value = libxl__xs_read(&gc, XBT_NULL, event->path);
638 if (!value || strcmp(value, "eject")) {
639 libxl__free_all(&gc);
640 return 0;
641 }
643 path = strdup(event->path);
644 path[strlen(path) - 6] = '\0';
645 backend = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend", path));
647 disk->backend_domid = 0;
648 disk->domid = domid;
649 disk->physpath = strdup("");
650 disk->phystype = PHYSTYPE_EMPTY;
651 /* this value is returned to the user: do not free right away */
652 disk->virtpath = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/dev", backend));
653 disk->unpluggable = 1;
654 disk->readwrite = 0;
655 disk->is_cdrom = 1;
657 free(path);
658 libxl__free_all(&gc);
659 return 1;
660 }
662 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
663 {
664 libxl__gc gc = LIBXL_INIT_GC(ctx);
665 libxl_dominfo dominfo;
666 char *dom_path;
667 char *vm_path;
668 int rc, dm_present;
670 rc = libxl_domain_info(ctx, &dominfo, domid);
671 switch(rc) {
672 case 0:
673 break;
674 case ERROR_INVAL:
675 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "non-existant domain %d", domid);
676 default:
677 return rc;
678 }
680 if (libxl__domain_is_hvm(ctx, domid)) {
681 dm_present = 1;
682 } else {
683 char *pid;
684 pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/image/device-model-pid", domid));
685 dm_present = (pid != NULL);
686 }
688 dom_path = libxl__xs_get_dompath(&gc, domid);
689 if (!dom_path) {
690 rc = ERROR_FAIL;
691 goto out;
692 }
694 if (libxl_device_pci_shutdown(ctx, domid) < 0)
695 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "pci shutdown failed for domid %d", domid);
696 rc = xc_domain_pause(ctx->xch, domid);
697 if (rc < 0) {
698 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
699 }
700 if (dm_present) {
701 if (libxl__destroy_device_model(ctx, domid) < 0)
702 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model failed for %d", domid);
703 }
704 if (libxl__devices_destroy(ctx, domid, force) < 0)
705 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
707 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
708 if (vm_path)
709 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
710 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", vm_path);
712 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
713 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", dom_path);
715 libxl__userdata_destroyall(ctx, domid);
717 rc = xc_domain_destroy(ctx->xch, domid);
718 if (rc < 0) {
719 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
720 rc = ERROR_FAIL;
721 goto out;
722 }
723 rc = 0;
724 out:
725 libxl__free_all(&gc);
726 return 0;
727 }
729 int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num, libxl_console_constype type)
730 {
731 libxl__gc gc = LIBXL_INIT_GC(ctx);
732 char *p = libxl__sprintf(&gc, "%s/xenconsole", libxl_private_bindir_path());
733 char *domid_s = libxl__sprintf(&gc, "%d", domid);
734 char *cons_num_s = libxl__sprintf(&gc, "%d", cons_num);
735 char *cons_type_s;
737 switch (type) {
738 case LIBXL_CONSTYPE_PV:
739 cons_type_s = "pv";
740 break;
741 case LIBXL_CONSTYPE_SERIAL:
742 cons_type_s = "serial";
743 break;
744 default:
745 goto out;
746 }
748 execl(p, p, domid_s, "--num", cons_num_s, "--type", cons_type_s, (void *)NULL);
750 out:
751 libxl__free_all(&gc);
752 return ERROR_FAIL;
753 }
755 int libxl_primary_console_exec(libxl_ctx *ctx, uint32_t domid_vm)
756 {
757 uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm);
758 if (stubdomid)
759 return libxl_console_exec(ctx, stubdomid,
760 STUBDOM_CONSOLE_SERIAL, LIBXL_CONSTYPE_PV);
761 else {
762 if (libxl__domain_is_hvm(ctx, domid_vm))
763 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_SERIAL);
764 else
765 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_PV);
766 }
767 }
769 int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
770 {
771 libxl__gc gc = LIBXL_INIT_GC(ctx);
772 const char *vnc_port, *vfb_back;
773 const char *vnc_listen = NULL, *vnc_pass = NULL;
774 int port = 0, autopass_fd = -1;
775 char *vnc_bin, *args[] = {
776 "vncviewer",
777 NULL, /* hostname:display */
778 NULL, /* -autopass */
779 NULL,
780 };
782 vnc_port = libxl__xs_read(&gc, XBT_NULL,
783 libxl__sprintf(&gc,
784 "/local/domain/%d/console/vnc-port", domid));
785 if ( vnc_port )
786 port = atoi(vnc_port) - 5900;
788 vfb_back = libxl__xs_read(&gc, XBT_NULL,
789 libxl__sprintf(&gc,
790 "/local/domain/%d/device/vfb/0/backend", domid));
791 if ( vfb_back ) {
792 vnc_listen = libxl__xs_read(&gc, XBT_NULL,
793 libxl__sprintf(&gc,
794 "/local/domain/%d/console/vnc-listen", domid));
795 if ( autopass )
796 vnc_pass = libxl__xs_read(&gc, XBT_NULL,
797 libxl__sprintf(&gc,
798 "/local/domain/%d/console/vnc-pass", domid));
799 }
801 if ( NULL == vnc_listen )
802 vnc_listen = "localhost";
804 if ( (vnc_bin = getenv("VNCVIEWER")) )
805 args[0] = vnc_bin;
807 args[1] = libxl__sprintf(&gc, "%s:%d", vnc_listen, port);
809 if ( vnc_pass ) {
810 char tmpname[] = "/tmp/vncautopass.XXXXXX";
811 autopass_fd = mkstemp(tmpname);
812 if ( autopass_fd < 0 )
813 goto skip_autopass;
815 if ( unlink(tmpname) )
816 /* should never happen */
817 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unlink %s failed", tmpname);
819 if ( libxl_write_exactly(ctx, autopass_fd, vnc_pass, strlen(vnc_pass),
820 tmpname, "vnc password") ) {
821 do { close(autopass_fd); } while(errno == EINTR);
822 goto skip_autopass;
823 }
825 args[2] = "-autopass";
826 }
828 skip_autopass:
829 libxl__free_all(&gc);
830 libxl__exec(autopass_fd, -1, -1, args[0], args);
831 return 0;
832 }
834 /******************************************************************************/
836 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
837 {
838 libxl__gc gc = LIBXL_INIT_GC(ctx);
839 flexarray_t *front;
840 flexarray_t *back;
841 char *backend_type;
842 int devid;
843 libxl__device device;
844 int major, minor, rc;
846 front = flexarray_make(16, 1);
847 if (!front) {
848 rc = ERROR_NOMEM;
849 goto out;
850 }
851 back = flexarray_make(16, 1);
852 if (!back) {
853 rc = ERROR_NOMEM;
854 goto out_free;
855 }
857 backend_type = libxl__device_disk_backend_type_of_phystype(disk->phystype);
858 devid = libxl__device_disk_dev_number(disk->virtpath);
859 if (devid==-1) {
860 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
861 " virtual disk identifier %s", disk->virtpath);
862 rc = ERROR_INVAL;
863 goto out_free;
864 }
866 device.backend_devid = devid;
867 device.backend_domid = disk->backend_domid;
868 device.devid = devid;
869 device.domid = disk->domid;
870 device.kind = DEVICE_VBD;
872 switch (disk->phystype) {
873 case PHYSTYPE_PHY: {
875 libxl__device_physdisk_major_minor(disk->physpath, &major, &minor);
876 flexarray_append(back, "physical-device");
877 flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
879 flexarray_append(back, "params");
880 flexarray_append(back, disk->physpath);
882 device.backend_kind = DEVICE_VBD;
883 break;
884 }
885 case PHYSTYPE_EMPTY:
886 break;
887 case PHYSTYPE_FILE:
888 /* let's pretend is tap:aio for the moment */
889 disk->phystype = PHYSTYPE_AIO;
890 case PHYSTYPE_AIO:
891 case PHYSTYPE_QCOW:
892 case PHYSTYPE_QCOW2:
893 case PHYSTYPE_VHD:
894 if (libxl__blktap_enabled(&gc)) {
895 const char *dev = libxl__blktap_devpath(&gc,
896 disk->physpath, disk->phystype);
897 if (!dev) {
898 rc = ERROR_FAIL;
899 goto out_free;
900 }
901 flexarray_append(back, "tapdisk-params");
902 flexarray_append(back, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
903 flexarray_append(back, "params");
904 flexarray_append(back, libxl__strdup(&gc, dev));
905 backend_type = "phy";
906 libxl__device_physdisk_major_minor(dev, &major, &minor);
907 flexarray_append(back, "physical-device");
908 flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
909 device.backend_kind = DEVICE_VBD;
911 break;
912 }
913 flexarray_append(back, "params");
914 flexarray_append(back, libxl__sprintf(&gc, "%s:%s",
915 libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
917 if (libxl__blktap_enabled(&gc))
918 device.backend_kind = DEVICE_TAP;
919 else
920 device.backend_kind = DEVICE_QDISK;
921 break;
923 default:
924 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
925 rc = ERROR_INVAL;
926 goto out_free;
927 }
929 flexarray_append(back, "frontend-id");
930 flexarray_append(back, libxl__sprintf(&gc, "%d", disk->domid));
931 flexarray_append(back, "online");
932 flexarray_append(back, "1");
933 flexarray_append(back, "removable");
934 flexarray_append(back, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
935 flexarray_append(back, "bootable");
936 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
937 flexarray_append(back, "state");
938 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
939 flexarray_append(back, "dev");
940 flexarray_append(back, disk->virtpath);
941 flexarray_append(back, "type");
942 flexarray_append(back, backend_type);
943 flexarray_append(back, "mode");
944 flexarray_append(back, disk->readwrite ? "w" : "r");
946 flexarray_append(front, "backend-id");
947 flexarray_append(front, libxl__sprintf(&gc, "%d", disk->backend_domid));
948 flexarray_append(front, "state");
949 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
950 flexarray_append(front, "virtual-device");
951 flexarray_append(front, libxl__sprintf(&gc, "%d", devid));
952 flexarray_append(front, "device-type");
953 flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
955 if (0 /* protocol != native*/) {
956 flexarray_append(front, "protocol");
957 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
958 }
960 libxl__device_generic_add(ctx, &device,
961 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
962 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
964 rc = 0;
966 out_free:
967 flexarray_free(back);
968 flexarray_free(front);
969 out:
970 libxl__free_all(&gc);
971 return rc;
972 }
974 int libxl_device_disk_del(libxl_ctx *ctx,
975 libxl_device_disk *disk, int wait)
976 {
977 libxl__device device;
978 int devid;
980 devid = libxl__device_disk_dev_number(disk->virtpath);
981 device.backend_domid = disk->backend_domid;
982 device.backend_devid = devid;
983 device.backend_kind =
984 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
985 device.domid = disk->domid;
986 device.devid = devid;
987 device.kind = DEVICE_VBD;
988 return libxl__device_del(ctx, &device, wait);
989 }
991 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
992 {
993 libxl__gc gc = LIBXL_INIT_GC(ctx);
994 const char *dev = NULL;
995 char *ret;
996 int phystype = disk->phystype;
997 switch (phystype) {
998 case PHYSTYPE_PHY: {
999 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1000 dev = disk->physpath;
1001 break;
1003 case PHYSTYPE_FILE:
1004 /* let's pretend is tap:aio for the moment */
1005 phystype = PHYSTYPE_AIO;
1006 case PHYSTYPE_AIO:
1007 case PHYSTYPE_QCOW:
1008 case PHYSTYPE_QCOW2:
1009 case PHYSTYPE_VHD:
1010 if (libxl__blktap_enabled(&gc))
1011 dev = libxl__blktap_devpath(&gc, disk->physpath, phystype);
1012 break;
1014 default:
1015 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1016 break;
1018 ret = strdup(dev);
1019 libxl__free_all(&gc);
1020 return ret;
1023 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1025 /* Nothing to do for PHYSTYPE_PHY. */
1027 /*
1028 * For other device types assume that the blktap2 process is
1029 * needed by the soon to be started domain and do nothing.
1030 */
1032 return 0;
1035 /******************************************************************************/
1036 int libxl_device_nic_init(libxl_device_nic *nic_info, int devnum)
1038 const uint8_t *r;
1039 libxl_uuid uuid;
1041 libxl_uuid_generate(&uuid);
1042 r = libxl_uuid_bytearray(&uuid);
1043 memset(nic_info, '\0', sizeof(*nic_info));
1045 nic_info->backend_domid = 0;
1046 nic_info->domid = 0;
1047 nic_info->devid = devnum;
1048 nic_info->mtu = 1492;
1049 nic_info->model = strdup("e1000");
1050 nic_info->mac[0] = 0x00;
1051 nic_info->mac[1] = 0x16;
1052 nic_info->mac[2] = 0x3e;
1053 nic_info->mac[3] = r[0] & 0x7f;
1054 nic_info->mac[4] = r[1];
1055 nic_info->mac[5] = r[2];
1056 nic_info->ifname = NULL;
1057 nic_info->bridge = strdup("xenbr0");
1058 if ( asprintf(&nic_info->script, "%s/vif-bridge",
1059 libxl_xen_script_dir_path()) < 0 )
1060 return ERROR_FAIL;
1061 nic_info->nictype = NICTYPE_IOEMU;
1062 return 0;
1065 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1067 libxl__gc gc = LIBXL_INIT_GC(ctx);
1068 flexarray_t *front;
1069 flexarray_t *back;
1070 libxl__device device;
1071 char *dompath, **l;
1072 unsigned int nb, rc;
1074 front = flexarray_make(16, 1);
1075 if (!front) {
1076 rc = ERROR_NOMEM;
1077 goto out;
1079 back = flexarray_make(16, 1);
1080 if (!back) {
1081 rc = ERROR_NOMEM;
1082 goto out_free;
1085 if (nic->devid == -1) {
1086 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1087 rc = ERROR_FAIL;
1088 goto out_free;
1090 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1091 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb))) {
1092 nic->devid = 0;
1093 } else {
1094 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1098 device.backend_devid = nic->devid;
1099 device.backend_domid = nic->backend_domid;
1100 device.backend_kind = DEVICE_VIF;
1101 device.devid = nic->devid;
1102 device.domid = nic->domid;
1103 device.kind = DEVICE_VIF;
1105 flexarray_append(back, "frontend-id");
1106 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->domid));
1107 flexarray_append(back, "online");
1108 flexarray_append(back, "1");
1109 flexarray_append(back, "state");
1110 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1111 flexarray_append(back, "script");
1112 flexarray_append(back, nic->script);
1113 flexarray_append(back, "mac");
1114 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1115 nic->mac[0], nic->mac[1], nic->mac[2],
1116 nic->mac[3], nic->mac[4], nic->mac[5]));
1117 flexarray_append(back, "bridge");
1118 flexarray_append(back, libxl__strdup(&gc, nic->bridge));
1119 flexarray_append(back, "handle");
1120 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->devid));
1122 flexarray_append(front, "backend-id");
1123 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->backend_domid));
1124 flexarray_append(front, "state");
1125 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1126 flexarray_append(front, "handle");
1127 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->devid));
1128 flexarray_append(front, "mac");
1129 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1130 nic->mac[0], nic->mac[1], nic->mac[2],
1131 nic->mac[3], nic->mac[4], nic->mac[5]));
1132 if (0 /* protocol != native*/) {
1133 flexarray_append(front, "protocol");
1134 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
1137 libxl__device_generic_add(ctx, &device,
1138 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1139 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1141 /* FIXME: wait for plug */
1142 rc = 0;
1143 out_free:
1144 flexarray_free(back);
1145 flexarray_free(front);
1146 out:
1147 libxl__free_all(&gc);
1148 return rc;
1151 int libxl_device_nic_del(libxl_ctx *ctx,
1152 libxl_device_nic *nic, int wait)
1154 libxl__device device;
1156 device.backend_devid = nic->devid;
1157 device.backend_domid = nic->backend_domid;
1158 device.backend_kind = DEVICE_VIF;
1159 device.devid = nic->devid;
1160 device.domid = nic->domid;
1161 device.kind = DEVICE_VIF;
1163 return libxl__device_del(ctx, &device, wait);
1166 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1168 libxl__gc gc = LIBXL_INIT_GC(ctx);
1169 char *dompath, *nic_path_fe;
1170 char **l, **list;
1171 char *val, *tok;
1172 unsigned int nb_nics, i;
1173 libxl_nicinfo *res, *nics;
1175 dompath = libxl__xs_get_dompath(&gc, domid);
1176 if (!dompath)
1177 goto err;
1178 list = l = libxl__xs_directory(&gc, XBT_NULL,
1179 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb_nics);
1180 if (!l)
1181 goto err;
1182 nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
1183 if (!res)
1184 goto err;
1185 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
1186 nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
1188 nics->backend = xs_read(ctx->xsh, XBT_NULL,
1189 libxl__sprintf(&gc, "%s/backend", nic_path_fe), NULL);
1190 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", nic_path_fe));
1191 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
1193 nics->devid = strtoul(*l, NULL, 10);
1194 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", nic_path_fe));
1195 nics->state = val ? strtoul(val, NULL, 10) : -1;
1196 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", nic_path_fe));
1197 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1198 ++i, tok = strtok(NULL, ":")) {
1199 nics->mac[i] = strtoul(tok, NULL, 16);
1201 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", nic_path_fe));
1202 nics->evtch = val ? strtol(val, NULL, 10) : -1;
1203 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", nic_path_fe));
1204 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
1205 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", nic_path_fe));
1206 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
1207 nics->frontend = xs_read(ctx->xsh, XBT_NULL,
1208 libxl__sprintf(&gc, "%s/frontend", nics->backend), NULL);
1209 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", nics->backend));
1210 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1211 nics->script = xs_read(ctx->xsh, XBT_NULL,
1212 libxl__sprintf(&gc, "%s/script", nics->backend), NULL);
1215 libxl__free_all(&gc);
1216 return res;
1217 err:
1218 libxl__free_all(&gc);
1219 return NULL;
1222 /******************************************************************************/
1223 void libxl_device_net2_init(libxl_device_net2 *net2_info, int devnum)
1225 const uint8_t *r;
1226 libxl_uuid uuid;
1228 libxl_uuid_generate(&uuid);
1229 r = libxl_uuid_bytearray(&uuid);
1230 memset(net2_info, '\0', sizeof(*net2_info));
1232 net2_info->devid = devnum;
1233 net2_info->front_mac[0] = 0x00;
1234 net2_info->front_mac[1] = 0x16;
1235 net2_info->front_mac[2] = 0x3e;;
1236 net2_info->front_mac[3] = 0x7f & r[0];
1237 net2_info->front_mac[4] = r[1];
1238 net2_info->front_mac[5] = r[2];
1239 net2_info->back_mac[0] = 0x00;
1240 net2_info->back_mac[1] = 0x16;
1241 net2_info->back_mac[2] = 0x3e;
1242 net2_info->back_mac[3] = 0x7f & r[3];
1243 net2_info->back_mac[4] = r[4];
1244 net2_info->back_mac[5] = r[5];
1245 net2_info->back_trusted = 1;
1246 net2_info->filter_mac = 1;
1247 net2_info->max_bypasses = 5;
1248 net2_info->bridge = strdup("xenbr0");
1251 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
1253 libxl__gc gc = LIBXL_INIT_GC(ctx);
1254 flexarray_t *front, *back;
1255 libxl__device device;
1256 char *dompath, *dom, **l;
1257 unsigned int nb;
1258 int rc;
1260 front = flexarray_make(16, 1);
1261 if (!front) {
1262 rc = ERROR_NOMEM;
1263 goto err;
1265 back = flexarray_make(16, 1);
1266 if (!back) {
1267 rc = ERROR_NOMEM;
1268 goto err_free;
1271 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1272 rc = ERROR_FAIL;
1273 goto err_free;
1275 dom = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/name", dompath));
1277 if (net2->devid == -1) {
1278 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1279 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb))) {
1280 net2->devid = 0;
1281 } else {
1282 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1286 device.backend_devid = net2->devid;
1287 device.backend_domid = net2->backend_domid;
1288 device.backend_kind = DEVICE_VIF2;
1289 device.devid = net2->devid;
1290 device.domid = net2->domid;
1291 device.kind = DEVICE_VIF2;
1293 flexarray_append(back, "domain");
1294 flexarray_append(back, dom);
1295 flexarray_append(back, "frontend-id");
1296 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->domid));
1298 flexarray_append(back, "local-trusted");
1299 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->back_trusted));
1300 flexarray_append(back, "mac");
1301 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1302 net2->back_mac[0], net2->back_mac[1],
1303 net2->back_mac[2], net2->back_mac[3],
1304 net2->back_mac[4], net2->back_mac[5]));
1306 flexarray_append(back, "remote-trusted");
1307 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->trusted));
1308 flexarray_append(back, "remote-mac");
1309 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1310 net2->front_mac[0], net2->front_mac[1],
1311 net2->front_mac[2], net2->front_mac[3],
1312 net2->front_mac[4], net2->front_mac[5]));
1314 flexarray_append(back, "max-bypasses");
1315 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->max_bypasses));
1316 flexarray_append(back, "filter-mac");
1317 flexarray_append(back, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1318 flexarray_append(back, "handle");
1319 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->devid));
1320 flexarray_append(back, "online");
1321 flexarray_append(back, "1");
1322 flexarray_append(back, "state");
1323 flexarray_append(back, "1");
1325 flexarray_append(front, "backend-id");
1326 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->backend_domid));
1328 flexarray_append(front, "local-trusted");
1329 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->trusted));
1330 flexarray_append(front, "mac");
1331 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1332 net2->front_mac[0], net2->front_mac[1],
1333 net2->front_mac[2], net2->front_mac[3],
1334 net2->front_mac[4], net2->front_mac[5]));
1336 flexarray_append(front, "remote-trusted");
1337 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->back_trusted));
1338 flexarray_append(front, "remote-mac");
1339 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1340 net2->back_mac[0], net2->back_mac[1],
1341 net2->back_mac[2], net2->back_mac[3],
1342 net2->back_mac[4], net2->back_mac[5]));
1344 flexarray_append(front, "filter-mac");
1345 flexarray_append(front, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1346 flexarray_append(front, "state");
1347 flexarray_append(front, "1");
1349 libxl__device_generic_add(ctx, &device,
1350 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1351 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1353 /* FIXME: wait for plug */
1354 rc = 0;
1355 err_free:
1356 flexarray_free(back);
1357 flexarray_free(front);
1358 err:
1359 libxl__free_all(&gc);
1360 return rc;
1363 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1365 libxl__gc gc = LIBXL_INIT_GC(ctx);
1366 char *dompath, *net2_path_fe;
1367 char **l;
1368 char *val, *tok;
1369 unsigned int nb_net2s, i;
1370 libxl_net2info *res, *net2s;
1372 dompath = libxl__xs_get_dompath(&gc, domid);
1373 if (!dompath)
1374 goto err;
1375 l = libxl__xs_directory(&gc, XBT_NULL,
1376 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb_net2s);
1377 if (!l)
1378 goto err;
1379 res = calloc(nb_net2s, sizeof (libxl_net2info));
1380 if (!res)
1381 goto err;
1382 net2s = res;
1383 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
1384 net2_path_fe = libxl__sprintf(&gc, "%s/device/vif2/%s", dompath, *l);
1386 net2s->backend = libxl__xs_read(&gc, XBT_NULL,
1387 libxl__sprintf(&gc, "%s/backend", net2_path_fe));
1388 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", net2_path_fe));
1389 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
1391 net2s->devid = strtoul(*l, NULL, 10);
1392 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", net2_path_fe));
1393 net2s->state = val ? strtoul(val, NULL, 10) : -1;
1395 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", net2_path_fe));
1396 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1397 ++i, tok = strtok(NULL, ":")) {
1398 net2s->mac[i] = strtoul(tok, NULL, 16);
1400 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-trusted", net2_path_fe));
1401 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
1403 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-mac", net2_path_fe));
1404 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1405 ++i, tok = strtok(NULL, ":")) {
1406 net2s->back_mac[i] = strtoul(tok, NULL, 16);
1408 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/filter-mac", net2_path_fe));
1409 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
1411 net2s->frontend = libxl__xs_read(&gc, XBT_NULL,
1412 libxl__sprintf(&gc, "%s/frontend", net2s->backend));
1413 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", net2s->backend));
1414 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1417 libxl__free_all(&gc);
1418 return res;
1419 err:
1420 libxl__free_all(&gc);
1421 return NULL;
1424 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
1426 libxl__device device;
1428 device.backend_devid = net2->devid;
1429 device.backend_domid = net2->backend_domid;
1430 device.backend_kind = DEVICE_VIF2;
1431 device.devid = net2->devid;
1432 device.domid = net2->domid;
1433 device.kind = DEVICE_VIF2;
1435 return libxl__device_del(ctx, &device, wait);
1439 /******************************************************************************/
1440 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1442 libxl__gc gc = LIBXL_INIT_GC(ctx);
1443 flexarray_t *front;
1444 flexarray_t *back;
1445 libxl__device device;
1446 int rc;
1448 front = flexarray_make(16, 1);
1449 if (!front) {
1450 rc = ERROR_NOMEM;
1451 goto out;
1453 back = flexarray_make(16, 1);
1454 if (!back) {
1455 rc = ERROR_NOMEM;
1456 goto out_free;
1459 device.backend_devid = console->devid;
1460 device.backend_domid = console->backend_domid;
1461 device.backend_kind = DEVICE_CONSOLE;
1462 device.devid = console->devid;
1463 device.domid = console->domid;
1464 device.kind = DEVICE_CONSOLE;
1466 flexarray_append(back, "frontend-id");
1467 flexarray_append(back, libxl__sprintf(&gc, "%d", console->domid));
1468 flexarray_append(back, "online");
1469 flexarray_append(back, "1");
1470 flexarray_append(back, "state");
1471 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1472 flexarray_append(back, "domain");
1473 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1474 flexarray_append(back, "protocol");
1475 flexarray_append(back, LIBXL_XENCONSOLE_PROTOCOL);
1477 flexarray_append(front, "backend-id");
1478 flexarray_append(front, libxl__sprintf(&gc, "%d", console->backend_domid));
1479 flexarray_append(front, "limit");
1480 flexarray_append(front, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
1481 flexarray_append(front, "type");
1482 if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
1483 flexarray_append(front, "xenconsoled");
1484 else
1485 flexarray_append(front, "ioemu");
1486 flexarray_append(front, "output");
1487 flexarray_append(front, console->output);
1489 if (device.devid == 0) {
1490 if (console->build_state == NULL) {
1491 rc = ERROR_INVAL;
1492 goto out_free;
1494 flexarray_append(front, "port");
1495 flexarray_append(front, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
1496 flexarray_append(front, "ring-ref");
1497 flexarray_append(front, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
1498 } else {
1499 flexarray_append(front, "state");
1500 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1501 flexarray_append(front, "protocol");
1502 flexarray_append(front, LIBXL_XENCONSOLE_PROTOCOL);
1505 libxl__device_generic_add(ctx, &device,
1506 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1507 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1508 rc = 0;
1509 out_free:
1510 flexarray_free(back);
1511 flexarray_free(front);
1512 out:
1513 libxl__free_all(&gc);
1514 return rc;
1517 /******************************************************************************/
1518 void libxl_device_vkb_init(libxl_device_vkb *vkb, int dev_num)
1520 memset(vkb, 0x00, sizeof(libxl_device_vkb));
1521 vkb->devid = dev_num;
1524 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1526 libxl__gc gc = LIBXL_INIT_GC(ctx);
1527 flexarray_t *front;
1528 flexarray_t *back;
1529 libxl__device device;
1530 int rc;
1532 front = flexarray_make(16, 1);
1533 if (!front) {
1534 rc = ERROR_NOMEM;
1535 goto out;
1537 back = flexarray_make(16, 1);
1538 if (!back) {
1539 rc = ERROR_NOMEM;
1540 goto out_free;
1543 device.backend_devid = vkb->devid;
1544 device.backend_domid = vkb->backend_domid;
1545 device.backend_kind = DEVICE_VKBD;
1546 device.devid = vkb->devid;
1547 device.domid = vkb->domid;
1548 device.kind = DEVICE_VKBD;
1550 flexarray_append(back, "frontend-id");
1551 flexarray_append(back, libxl__sprintf(&gc, "%d", vkb->domid));
1552 flexarray_append(back, "online");
1553 flexarray_append(back, "1");
1554 flexarray_append(back, "state");
1555 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1556 flexarray_append(back, "domain");
1557 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1559 flexarray_append(front, "backend-id");
1560 flexarray_append(front, libxl__sprintf(&gc, "%d", vkb->backend_domid));
1561 flexarray_append(front, "state");
1562 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1564 libxl__device_generic_add(ctx, &device,
1565 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1566 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1567 rc = 0;
1568 out_free:
1569 flexarray_free(back);
1570 flexarray_free(front);
1571 out:
1572 libxl__free_all(&gc);
1573 return rc;
1576 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1578 return ERROR_NI;
1581 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1583 return ERROR_NI;
1586 static unsigned int libxl_append_disk_list_of_type(libxl_ctx *ctx,
1587 uint32_t domid,
1588 const char *type,
1589 libxl_device_disk **disks,
1590 unsigned int *ndisks)
1592 libxl__gc gc = LIBXL_INIT_GC(ctx);
1593 char *be_path = NULL;
1594 char **dir = NULL;
1595 unsigned int n = 0, len = 0;
1596 libxl_device_disk *pdisk = NULL, *pdisk_end = NULL;
1597 char *physpath_tmp = NULL;
1599 be_path = libxl__sprintf(&gc, "%s/backend/%s/%d",
1600 libxl__xs_get_dompath(&gc, 0), type, domid);
1601 dir = libxl__xs_directory(&gc, XBT_NULL, be_path, &n);
1602 if (dir) {
1603 *disks = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
1604 pdisk = *disks + *ndisks;
1605 *ndisks += n;
1606 pdisk_end = *disks + *ndisks;
1607 for (; pdisk < pdisk_end; pdisk++, dir++) {
1608 pdisk->backend_domid = 0;
1609 pdisk->domid = domid;
1610 physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path, *dir), &len);
1611 if (physpath_tmp && strchr(physpath_tmp, ':')) {
1612 pdisk->physpath = strdup(strchr(physpath_tmp, ':') + 1);
1613 free(physpath_tmp);
1614 } else {
1615 pdisk->physpath = physpath_tmp;
1617 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path, *dir)), &(pdisk->phystype));
1618 pdisk->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path, *dir), &len);
1619 pdisk->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path, *dir)));
1620 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path, *dir)), "w"))
1621 pdisk->readwrite = 1;
1622 else
1623 pdisk->readwrite = 0;
1624 type = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/device-type", libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/frontend", be_path, *dir))));
1625 pdisk->is_cdrom = !strcmp(type, "cdrom");
1629 libxl__free_all(&gc);
1630 return n;
1633 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
1635 libxl_device_disk *disks = NULL;
1636 unsigned int ndisks = 0;
1638 *num = libxl_append_disk_list_of_type(ctx, domid, "vbd", &disks, &ndisks);
1639 *num += libxl_append_disk_list_of_type(ctx, domid, "tap", &disks, &ndisks);
1640 *num += libxl_append_disk_list_of_type(ctx, domid, "qdisk", &disks, &ndisks);
1642 return disks;
1645 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
1646 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
1648 libxl__gc gc = LIBXL_INIT_GC(ctx);
1649 char *dompath, *diskpath;
1650 char *val;
1652 dompath = libxl__xs_get_dompath(&gc, domid);
1653 diskinfo->devid = libxl__device_disk_dev_number(disk->virtpath);
1655 /* tap devices entries in xenstore are written as vbd devices. */
1656 diskpath = libxl__sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
1657 diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
1658 libxl__sprintf(&gc, "%s/backend", diskpath), NULL);
1659 if (!diskinfo->backend) {
1660 libxl__free_all(&gc);
1661 return ERROR_FAIL;
1663 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", diskpath));
1664 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
1665 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", diskpath));
1666 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
1667 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", diskpath));
1668 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
1669 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/ring-ref", diskpath));
1670 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
1671 diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
1672 libxl__sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
1673 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", diskinfo->backend));
1674 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1676 libxl__free_all(&gc);
1677 return 0;
1680 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1682 int num, i;
1683 uint32_t stubdomid;
1684 libxl_device_disk *disks;
1685 int ret = ERROR_FAIL;
1687 if (!disk->physpath) {
1688 disk->physpath = strdup("");
1689 disk->phystype = PHYSTYPE_EMPTY;
1691 disks = libxl_device_disk_list(ctx, domid, &num);
1692 for (i = 0; i < num; i++) {
1693 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1694 /* found */
1695 break;
1697 if (i == num) {
1698 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual device not found");
1699 goto out;
1702 ret = 0;
1704 libxl_device_disk_del(ctx, disks + i, 1);
1705 libxl_device_disk_add(ctx, domid, disk);
1706 stubdomid = libxl_get_stubdom_id(ctx, domid);
1707 if (stubdomid) {
1708 disks[i].domid = stubdomid;
1709 libxl_device_disk_del(ctx, disks + i, 1);
1710 disk->domid = stubdomid;
1711 libxl_device_disk_add(ctx, stubdomid, disk);
1712 disk->domid = domid;
1714 out:
1715 for (i = 0; i < num; i++)
1716 libxl_device_disk_destroy(&disks[i]);
1717 free(disks);
1718 return ret;
1721 /******************************************************************************/
1722 void libxl_device_vfb_init(libxl_device_vfb *vfb, int dev_num)
1724 memset(vfb, 0x00, sizeof(libxl_device_vfb));
1725 vfb->devid = dev_num;
1726 vfb->display = NULL;
1727 vfb->xauthority = NULL;
1728 vfb->vnc = 1;
1729 vfb->vncpasswd = NULL;
1730 vfb->vnclisten = strdup("127.0.0.1");
1731 vfb->vncdisplay = 0;
1732 vfb->vncunused = 1;
1733 vfb->keymap = NULL;
1734 vfb->sdl = 0;
1735 vfb->opengl = 0;
1738 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1740 libxl__gc gc = LIBXL_INIT_GC(ctx);
1741 flexarray_t *front;
1742 flexarray_t *back;
1743 libxl__device device;
1744 int rc;
1746 front = flexarray_make(16, 1);
1747 if (!front) {
1748 rc = ERROR_NOMEM;
1749 goto out;
1751 back = flexarray_make(16, 1);
1752 if (!back) {
1753 rc = ERROR_NOMEM;
1754 goto out_free;
1757 device.backend_devid = vfb->devid;
1758 device.backend_domid = vfb->backend_domid;
1759 device.backend_kind = DEVICE_VFB;
1760 device.devid = vfb->devid;
1761 device.domid = vfb->domid;
1762 device.kind = DEVICE_VFB;
1764 flexarray_vappend(back, "frontend-id", libxl__sprintf(&gc, "%d", vfb->domid), NULL);
1765 flexarray_vappend(back, "online", "1", NULL);
1766 flexarray_vappend(back, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1767 flexarray_vappend(back, "domain", libxl__domid_to_name(&gc, domid), NULL);
1768 flexarray_vappend(back, "vnc", libxl__sprintf(&gc, "%d", vfb->vnc), NULL);
1769 flexarray_vappend(back, "vnclisten", vfb->vnclisten, NULL);
1770 flexarray_append(back, "vncpasswd");
1771 flexarray_append(back, vfb->vncpasswd);
1772 flexarray_vappend(back, "vncdisplay", libxl__sprintf(&gc, "%d", vfb->vncdisplay), NULL);
1773 flexarray_vappend(back, "vncunused", libxl__sprintf(&gc, "%d", vfb->vncunused), NULL);
1774 flexarray_vappend(back, "sdl", libxl__sprintf(&gc, "%d", vfb->sdl), NULL);
1775 flexarray_vappend(back, "opengl", libxl__sprintf(&gc, "%d", vfb->opengl), NULL);
1776 if (vfb->xauthority) {
1777 flexarray_vappend(back, "xauthority", vfb->xauthority, NULL);
1779 if (vfb->display) {
1780 flexarray_vappend(back, "display", vfb->display, NULL);
1783 flexarray_vappend(front, "backend-id", libxl__sprintf(&gc, "%d", vfb->backend_domid), NULL);
1784 flexarray_vappend(front, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1786 libxl__device_generic_add(ctx, &device,
1787 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1788 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1789 rc = 0;
1790 out_free:
1791 flexarray_free(front);
1792 flexarray_free(back);
1793 out:
1794 libxl__free_all(&gc);
1795 return rc;
1798 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1800 return ERROR_NI;
1803 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1805 return ERROR_NI;
1808 /******************************************************************************/
1810 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
1812 libxl__gc gc = LIBXL_INIT_GC(ctx);
1813 char *mem, *endptr;
1814 uint32_t memorykb;
1815 char *dompath = libxl__xs_get_dompath(&gc, domid);
1816 int rc = 1;
1818 mem = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/memory/target", dompath));
1819 if (!mem) {
1820 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
1821 goto out;
1823 memorykb = strtoul(mem, &endptr, 10);
1824 if (*endptr != '\0') {
1825 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
1826 goto out;
1829 if (max_memkb < memorykb) {
1830 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
1831 goto out;
1834 rc = 0;
1835 out:
1836 libxl__free_all(&gc);
1837 return rc;
1840 static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb)
1842 int rc;
1843 libxl_dominfo info;
1844 libxl_physinfo physinfo;
1845 char *target = NULL, *staticmax = NULL, *freememslack = NULL, *endptr = NULL;
1846 char *target_path = "/local/domain/0/memory/target";
1847 char *max_path = "/local/domain/0/memory/static-max";
1848 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
1849 xs_transaction_t t;
1850 libxl_ctx *ctx = libxl__gc_owner(gc);
1851 uint32_t free_mem_slack_kb = 0;
1853 retry_transaction:
1854 t = xs_transaction_start(ctx->xsh);
1856 target = libxl__xs_read(gc, t, target_path);
1857 staticmax = libxl__xs_read(gc, t, max_path);
1858 freememslack = libxl__xs_read(gc, t, free_mem_slack_path);
1859 if (target && staticmax && freememslack) {
1860 rc = 0;
1861 goto out;
1864 if (target) {
1865 *target_memkb = strtoul(target, &endptr, 10);
1866 if (*endptr != '\0') {
1867 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1868 "invalid memory target %s from %s\n", target, target_path);
1869 rc = ERROR_FAIL;
1870 goto out;
1874 rc = libxl_domain_info(ctx, &info, 0);
1875 if (rc < 0)
1876 goto out;
1878 rc = libxl_get_physinfo(ctx, &physinfo);
1879 if (rc < 0)
1880 goto out;
1882 if (target == NULL) {
1883 libxl__xs_write(gc, t, target_path, "%"PRIu32,
1884 (uint32_t) info.current_memkb);
1885 *target_memkb = (uint32_t) info.current_memkb;
1887 if (staticmax == NULL)
1888 libxl__xs_write(gc, t, max_path, "%"PRIu32,
1889 (uint32_t) info.max_memkb);
1891 if (freememslack == NULL) {
1892 free_mem_slack_kb = (uint32_t) (PAGE_TO_MEMKB(physinfo.total_pages) -
1893 info.current_memkb);
1894 /* From empirical measurements the free_mem_slack shouldn't be more
1895 * than 15% of the total memory present on the system. */
1896 if (free_mem_slack_kb > PAGE_TO_MEMKB(physinfo.total_pages) * 0.15)
1897 free_mem_slack_kb = PAGE_TO_MEMKB(physinfo.total_pages) * 0.15;
1898 libxl__xs_write(gc, t, free_mem_slack_path, "%"PRIu32, free_mem_slack_kb);
1900 rc = 0;
1902 out:
1903 if (!xs_transaction_end(ctx->xsh, t, 0)) {
1904 if (errno == EAGAIN)
1905 goto retry_transaction;
1906 else
1907 rc = ERROR_FAIL;
1911 return rc;
1914 /* returns how much memory should be left free in the system */
1915 static int libxl__get_free_memory_slack(libxl__gc *gc, uint32_t *free_mem_slack)
1917 int rc;
1918 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
1919 char *free_mem_slack_s, *endptr;
1920 uint32_t target_memkb;
1922 retry:
1923 free_mem_slack_s = libxl__xs_read(gc, XBT_NULL, free_mem_slack_path);
1924 if (!free_mem_slack_s) {
1925 rc = libxl__fill_dom0_memory_info(gc, &target_memkb);
1926 if (rc < 0)
1927 return rc;
1928 goto retry;
1929 } else {
1930 *free_mem_slack = strtoul(free_mem_slack_s, &endptr, 10);
1931 if (*endptr != '\0') {
1932 LIBXL__LOG_ERRNO(gc->owner, LIBXL__LOG_ERROR,
1933 "invalid free_mem_slack %s from %s\n",
1934 free_mem_slack_s, free_mem_slack_path);
1935 return ERROR_FAIL;
1938 return 0;
1941 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
1942 int32_t target_memkb, int relative, int enforce)
1944 libxl__gc gc = LIBXL_INIT_GC(ctx);
1945 int rc = 1, abort = 0;
1946 uint32_t memorykb = 0, videoram = 0;
1947 uint32_t current_target_memkb = 0, new_target_memkb = 0;
1948 char *memmax, *endptr, *videoram_s = NULL, *target = NULL;
1949 char *dompath = libxl__xs_get_dompath(&gc, domid);
1950 xc_domaininfo_t info;
1951 libxl_dominfo ptr;
1952 char *uuid;
1953 xs_transaction_t t;
1955 retry_transaction:
1956 t = xs_transaction_start(ctx->xsh);
1958 target = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
1959 "%s/memory/target", dompath));
1960 if (!target && !domid) {
1961 xs_transaction_end(ctx->xsh, t, 1);
1962 rc = libxl__fill_dom0_memory_info(&gc, &current_target_memkb);
1963 if (rc < 0) {
1964 abort = 1;
1965 goto out;
1967 goto retry_transaction;
1968 } else if (!target) {
1969 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1970 "cannot get target memory info from %s/memory/target\n",
1971 dompath);
1972 abort = 1;
1973 goto out;
1974 } else {
1975 current_target_memkb = strtoul(target, &endptr, 10);
1976 if (*endptr != '\0') {
1977 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1978 "invalid memory target %s from %s/memory/target\n",
1979 target, dompath);
1980 abort = 1;
1981 goto out;
1984 memmax = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
1985 "%s/memory/static-max", dompath));
1986 if (!memmax) {
1987 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1988 "cannot get memory info from %s/memory/static-max\n",
1989 dompath);
1990 abort = 1;
1991 goto out;
1993 memorykb = strtoul(memmax, &endptr, 10);
1994 if (*endptr != '\0') {
1995 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1996 "invalid max memory %s from %s/memory/static-max\n",
1997 memmax, dompath);
1998 abort = 1;
1999 goto out;
2002 if (relative)
2003 new_target_memkb = current_target_memkb + target_memkb;
2004 else
2005 new_target_memkb = target_memkb;
2006 if (new_target_memkb > memorykb) {
2007 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2008 "memory_dynamic_max must be less than or equal to"
2009 " memory_static_max\n");
2010 abort = 1;
2011 goto out;
2014 if (!domid && new_target_memkb < LIBXL_MIN_DOM0_MEM) {
2015 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2016 "new target %d for dom0 is below the minimum threshold\n",
2017 new_target_memkb);
2018 abort = 1;
2019 goto out;
2021 videoram_s = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2022 "%s/memory/videoram", dompath));
2023 videoram = videoram_s ? atoi(videoram_s) : 0;
2025 if (enforce) {
2026 memorykb = new_target_memkb;
2027 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
2028 LIBXL_MAXMEM_CONSTANT);
2029 if (rc != 0) {
2030 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2031 "xc_domain_setmaxmem domid=%d memkb=%d failed "
2032 "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
2033 abort = 1;
2034 goto out;
2038 new_target_memkb -= videoram;
2039 rc = xc_domain_set_pod_target(ctx->xch, domid,
2040 new_target_memkb / 4, NULL, NULL, NULL);
2041 if (rc != 0) {
2042 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2043 "xc_domain_set_pod_target domid=%d, memkb=%d "
2044 "failed rc=%d\n", domid, new_target_memkb / 4,
2045 rc);
2046 abort = 1;
2047 goto out;
2050 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/memory/target",
2051 dompath), "%"PRIu32, new_target_memkb);
2052 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2053 if (rc != 1 || info.domain != domid) {
2054 abort = 1;
2055 goto out;
2057 xcinfo2xlinfo(&info, &ptr);
2058 uuid = libxl__uuid2string(&gc, ptr.uuid);
2059 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "/vm/%s/memory", uuid),
2060 "%"PRIu32, new_target_memkb / 1024);
2062 out:
2063 if (!xs_transaction_end(ctx->xsh, t, abort) && !abort)
2064 if (errno == EAGAIN)
2065 goto retry_transaction;
2067 libxl__free_all(&gc);
2068 return rc;
2071 int libxl_get_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t *out_target)
2073 libxl__gc gc = LIBXL_INIT_GC(ctx);
2074 int rc = 1;
2075 char *target = NULL, *endptr = NULL;
2076 char *dompath = libxl__xs_get_dompath(&gc, domid);
2077 uint32_t target_memkb;
2079 target = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc,
2080 "%s/memory/target", dompath));
2081 if (!target && !domid) {
2082 rc = libxl__fill_dom0_memory_info(&gc, &target_memkb);
2083 if (rc < 0)
2084 goto out;
2085 } else if (!target) {
2086 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2087 "cannot get target memory info from %s/memory/target\n",
2088 dompath);
2089 goto out;
2090 } else {
2091 target_memkb = strtoul(target, &endptr, 10);
2092 if (*endptr != '\0') {
2093 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2094 "invalid memory target %s from %s/memory/target\n",
2095 target, dompath);
2096 goto out;
2099 *out_target = target_memkb;
2100 rc = 0;
2102 out:
2103 libxl__free_all(&gc);
2104 return rc;
2107 int libxl_domain_need_memory(libxl_ctx *ctx, libxl_domain_build_info *b_info,
2108 libxl_device_model_info *dm_info, uint32_t *need_memkb)
2110 *need_memkb = b_info->target_memkb;
2111 if (b_info->hvm) {
2112 *need_memkb += b_info->shadow_memkb + LIBXL_HVM_EXTRA_MEMORY;
2113 if (strstr(dm_info->device_model, "stubdom-dm"))
2114 *need_memkb += 32 * 1024;
2115 } else
2116 *need_memkb += b_info->shadow_memkb + LIBXL_PV_EXTRA_MEMORY;
2117 if (*need_memkb % (2 * 1024))
2118 *need_memkb += (2 * 1024) - (*need_memkb % (2 * 1024));
2119 return 0;
2122 int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
2124 int rc = 0;
2125 libxl_physinfo info;
2126 uint32_t freemem_slack;
2127 libxl__gc gc = LIBXL_INIT_GC(ctx);
2129 rc = libxl_get_physinfo(ctx, &info);
2130 if (rc < 0)
2131 goto out;
2132 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2133 if (rc < 0)
2134 goto out;
2136 if ((info.free_pages + info.scrub_pages) * 4 > freemem_slack)
2137 *memkb = (info.free_pages + info.scrub_pages) * 4 - freemem_slack;
2138 else
2139 *memkb = 0;
2141 out:
2142 libxl__free_all(&gc);
2143 return rc;
2146 int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
2147 memory_kb, int wait_secs)
2149 int rc = 0;
2150 libxl_physinfo info;
2151 uint32_t freemem_slack;
2152 libxl__gc gc = LIBXL_INIT_GC(ctx);
2154 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2155 if (rc < 0)
2156 goto out;
2157 while (wait_secs > 0) {
2158 rc = libxl_get_physinfo(ctx, &info);
2159 if (rc < 0)
2160 goto out;
2161 if (info.free_pages * 4 - freemem_slack >= memory_kb) {
2162 rc = 0;
2163 goto out;
2165 wait_secs--;
2166 sleep(1);
2168 rc = ERROR_NOMEM;
2170 out:
2171 libxl__free_all(&gc);
2172 return rc;
2175 int libxl_wait_for_memory_target(libxl_ctx *ctx, uint32_t domid, int wait_secs)
2177 int rc = 0;
2178 uint32_t target_memkb = 0;
2179 libxl_dominfo info;
2181 do {
2182 wait_secs--;
2183 sleep(1);
2185 rc = libxl_get_memory_target(ctx, domid, &target_memkb);
2186 if (rc < 0)
2187 goto out;
2189 rc = libxl_domain_info(ctx, &info, domid);
2190 if (rc < 0)
2191 return rc;
2192 } while (wait_secs > 0 && info.current_memkb > target_memkb);
2194 if (info.current_memkb <= target_memkb)
2195 rc = 0;
2196 else
2197 rc = ERROR_FAIL;
2199 out:
2200 return 0;
2203 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
2205 int rc = -1;
2207 switch (button) {
2208 case POWER_BUTTON:
2209 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2210 break;
2211 case SLEEP_BUTTON:
2212 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2213 break;
2214 default:
2215 break;
2218 return rc;
2221 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
2223 xc_physinfo_t xcphysinfo = { 0 };
2224 int rc;
2226 rc = xc_physinfo(ctx->xch, &xcphysinfo);
2227 if (rc != 0) {
2228 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting physinfo");
2229 return ERROR_FAIL;
2231 physinfo->threads_per_core = xcphysinfo.threads_per_core;
2232 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
2233 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
2234 physinfo->nr_cpus = xcphysinfo.nr_cpus;
2235 physinfo->cpu_khz = xcphysinfo.cpu_khz;
2236 physinfo->total_pages = xcphysinfo.total_pages;
2237 physinfo->free_pages = xcphysinfo.free_pages;
2238 physinfo->scrub_pages = xcphysinfo.scrub_pages;
2239 physinfo->nr_nodes = xcphysinfo.nr_nodes;
2240 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
2241 physinfo->phys_cap = xcphysinfo.capabilities;
2243 return 0;
2246 int libxl_get_topologyinfo(libxl_ctx *ctx, libxl_topologyinfo *info)
2248 xc_topologyinfo_t tinfo;
2249 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_core_t, coremap);
2250 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_socket_t, socketmap);
2251 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_node_t, nodemap);
2252 int i;
2253 int rc = 0;
2255 rc += libxl_cpuarray_alloc(ctx, &info->coremap);
2256 rc += libxl_cpuarray_alloc(ctx, &info->socketmap);
2257 rc += libxl_cpuarray_alloc(ctx, &info->nodemap);
2258 if (rc)
2259 goto fail;
2261 coremap = xc_hypercall_buffer_alloc(ctx->xch, coremap, sizeof(*coremap) * info->coremap.entries);
2262 socketmap = xc_hypercall_buffer_alloc(ctx->xch, socketmap, sizeof(*socketmap) * info->socketmap.entries);
2263 nodemap = xc_hypercall_buffer_alloc(ctx->xch, nodemap, sizeof(*nodemap) * info->nodemap.entries);
2264 if ((coremap == NULL) || (socketmap == NULL) || (nodemap == NULL))
2265 goto fail;
2267 set_xen_guest_handle(tinfo.cpu_to_core, coremap);
2268 set_xen_guest_handle(tinfo.cpu_to_socket, socketmap);
2269 set_xen_guest_handle(tinfo.cpu_to_node, nodemap);
2270 tinfo.max_cpu_index = info->coremap.entries - 1;
2271 if (xc_topologyinfo(ctx->xch, &tinfo) != 0)
2272 goto fail;
2274 for (i = 0; i <= tinfo.max_cpu_index; i++) {
2275 if (i < info->coremap.entries)
2276 info->coremap.array[i] = (coremap[i] == INVALID_TOPOLOGY_ID) ?
2277 LIBXL_CPUARRAY_INVALID_ENTRY : coremap[i];
2278 if (i < info->socketmap.entries)
2279 info->socketmap.array[i] = (socketmap[i] == INVALID_TOPOLOGY_ID) ?
2280 LIBXL_CPUARRAY_INVALID_ENTRY : socketmap[i];
2281 if (i < info->nodemap.entries)
2282 info->nodemap.array[i] = (nodemap[i] == INVALID_TOPOLOGY_ID) ?
2283 LIBXL_CPUARRAY_INVALID_ENTRY : nodemap[i];
2286 xc_hypercall_buffer_free(ctx->xch, coremap);
2287 xc_hypercall_buffer_free(ctx->xch, socketmap);
2288 xc_hypercall_buffer_free(ctx->xch, nodemap);
2289 return 0;
2291 fail:
2292 xc_hypercall_buffer_free(ctx->xch, coremap);
2293 xc_hypercall_buffer_free(ctx->xch, socketmap);
2294 xc_hypercall_buffer_free(ctx->xch, nodemap);
2295 libxl_topologyinfo_destroy(info);
2296 return ERROR_FAIL;
2299 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
2301 union {
2302 xen_extraversion_t xen_extra;
2303 xen_compile_info_t xen_cc;
2304 xen_changeset_info_t xen_chgset;
2305 xen_capabilities_info_t xen_caps;
2306 xen_platform_parameters_t p_parms;
2307 xen_commandline_t xen_commandline;
2308 } u;
2309 long xen_version;
2310 libxl_version_info *info = &ctx->version_info;
2312 if (info->xen_version_extra != NULL)
2313 return info;
2315 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
2316 info->xen_version_major = xen_version >> 16;
2317 info->xen_version_minor = xen_version & 0xFF;
2319 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
2320 info->xen_version_extra = strdup(u.xen_extra);
2322 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
2323 info->compiler = strdup(u.xen_cc.compiler);
2324 info->compile_by = strdup(u.xen_cc.compile_by);
2325 info->compile_domain = strdup(u.xen_cc.compile_domain);
2326 info->compile_date = strdup(u.xen_cc.compile_date);
2328 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
2329 info->capabilities = strdup(u.xen_caps);
2331 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
2332 info->changeset = strdup(u.xen_chgset);
2334 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
2335 info->virt_start = u.p_parms.virt_start;
2337 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
2339 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
2340 info->commandline = strdup(u.xen_commandline);
2342 return info;
2345 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
2346 int *nb_vcpu, int *nrcpus)
2348 libxl_vcpuinfo *ptr, *ret;
2349 xc_domaininfo_t domaininfo;
2350 xc_vcpuinfo_t vcpuinfo;
2352 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2353 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist");
2354 return NULL;
2356 *nrcpus = libxl_get_max_cpus(ctx);
2357 ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
2358 if (!ptr) {
2359 return NULL;
2362 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
2363 if (libxl_cpumap_alloc(ctx, &ptr->cpumap)) {
2364 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap");
2365 return NULL;
2367 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
2368 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info");
2369 return NULL;
2371 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) {
2372 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity");
2373 return NULL;
2375 ptr->vcpuid = *nb_vcpu;
2376 ptr->cpu = vcpuinfo.cpu;
2377 ptr->online = !!vcpuinfo.online;
2378 ptr->blocked = !!vcpuinfo.blocked;
2379 ptr->running = !!vcpuinfo.running;
2380 ptr->vcpu_time = vcpuinfo.cpu_time;
2382 return ret;
2385 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
2386 libxl_cpumap *cpumap)
2388 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap->map)) {
2389 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting vcpu affinity");
2390 return ERROR_FAIL;
2392 return 0;
2395 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask)
2397 libxl__gc gc = LIBXL_INIT_GC(ctx);
2398 libxl_dominfo info;
2399 char *dompath;
2400 xs_transaction_t t;
2401 int i, rc = ERROR_FAIL;
2403 if (libxl_domain_info(ctx, &info, domid) < 0) {
2404 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2405 goto out;
2407 if (!(dompath = libxl__xs_get_dompath(&gc, domid)))
2408 goto out;
2410 retry_transaction:
2411 t = xs_transaction_start(ctx->xsh);
2412 for (i = 0; i <= info.vcpu_max_id; i++)
2413 libxl__xs_write(&gc, t,
2414 libxl__sprintf(&gc, "%s/cpu/%u/availability", dompath, i),
2415 "%s", ((1 << i) & bitmask) ? "online" : "offline");
2416 if (!xs_transaction_end(ctx->xsh, t, 0)) {
2417 if (errno == EAGAIN)
2418 goto retry_transaction;
2419 } else
2420 rc = 0;
2421 out:
2422 libxl__free_all(&gc);
2423 return rc;
2426 /*
2427 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
2428 */
2429 int libxl_get_sched_id(libxl_ctx *ctx)
2431 int sched, ret;
2433 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
2434 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2435 return ERROR_FAIL;
2437 return sched;
2440 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2442 struct xen_domctl_sched_credit sdom;
2443 int rc;
2445 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
2446 if (rc != 0) {
2447 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2448 return ERROR_FAIL;
2451 scinfo->weight = sdom.weight;
2452 scinfo->cap = sdom.cap;
2454 return 0;
2457 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2459 struct xen_domctl_sched_credit sdom;
2460 xc_domaininfo_t domaininfo;
2461 int rc;
2463 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
2464 if (rc < 0) {
2465 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2466 return ERROR_FAIL;
2468 if (rc != 1 || domaininfo.domain != domid)
2469 return ERROR_INVAL;
2472 if (scinfo->weight < 1 || scinfo->weight > 65535) {
2473 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2474 "Cpu weight out of range, valid values are within range from 1 to 65535");
2475 return ERROR_INVAL;
2478 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
2479 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2480 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
2481 ((domaininfo.max_vcpu_id + 1) * 100));
2482 return ERROR_INVAL;
2485 sdom.weight = scinfo->weight;
2486 sdom.cap = scinfo->cap;
2488 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
2489 if ( rc < 0 ) {
2490 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2491 return ERROR_FAIL;
2494 return 0;
2497 static int trigger_type_from_string(char *trigger_name)
2499 if (!strcmp(trigger_name, "nmi"))
2500 return XEN_DOMCTL_SENDTRIGGER_NMI;
2501 else if (!strcmp(trigger_name, "reset"))
2502 return XEN_DOMCTL_SENDTRIGGER_RESET;
2503 else if (!strcmp(trigger_name, "init"))
2504 return XEN_DOMCTL_SENDTRIGGER_INIT;
2505 else if (!strcmp(trigger_name, "power"))
2506 return XEN_DOMCTL_SENDTRIGGER_POWER;
2507 else if (!strcmp(trigger_name, "sleep"))
2508 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
2509 else
2510 return -1;
2513 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
2515 int rc = -1;
2516 int trigger_type = trigger_type_from_string(trigger_name);
2518 if (trigger_type == -1) {
2519 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2520 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
2521 return ERROR_INVAL;
2524 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
2525 if (rc != 0) {
2526 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2527 "Send trigger '%s' failed", trigger_name);
2528 return ERROR_FAIL;
2531 return 0;
2534 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
2536 libxl__gc gc = LIBXL_INIT_GC(ctx);
2537 char *dompath = libxl__xs_get_dompath(&gc, domid);
2539 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/control/sysrq", dompath), "%c", sysrq);
2541 libxl__free_all(&gc);
2542 return 0;
2545 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
2547 int ret;
2548 ret = xc_send_debug_keys(ctx->xch, keys);
2549 if ( ret < 0 ) {
2550 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "sending debug keys");
2551 return ERROR_FAIL;
2553 return 0;
2556 libxl_xen_console_reader *
2557 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
2559 libxl_xen_console_reader *cr;
2560 unsigned int size = 16384;
2561 char *buf = malloc(size);
2563 if (!buf) {
2564 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
2565 " size is %u", size);
2566 return NULL;
2569 cr = malloc(sizeof(libxl_xen_console_reader));
2570 if (!cr) {
2571 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc libxl_xen_console_reader");
2572 return NULL;
2575 memset(cr, 0, sizeof(libxl_xen_console_reader));
2576 cr->buffer = buf;
2577 cr->size = size;
2578 cr->count = size;
2579 cr->clear = clear;
2580 cr->incremental = 1;
2582 return cr;
2585 /* return values: *line_r
2586 * 1 success, whole line obtained from buffer non-0
2587 * 0 no more lines available right now 0
2588 * negative error code ERROR_* 0
2589 * On success *line_r is updated to point to a nul-terminated
2590 * string which is valid until the next call on the same console
2591 * reader. The libxl caller may overwrite parts of the string
2592 * if it wishes. */
2593 int libxl_xen_console_read_line(libxl_ctx *ctx,
2594 libxl_xen_console_reader *cr,
2595 char **line_r)
2597 int ret;
2599 memset(cr->buffer, 0, cr->size);
2600 ret = xc_readconsolering(ctx->xch, cr->buffer, &cr->count,
2601 cr->clear, cr->incremental, &cr->index);
2602 if (ret < 0) {
2603 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "reading console ring buffer");
2604 return ERROR_FAIL;
2606 if (!ret) {
2607 if (cr->count) {
2608 *line_r = cr->buffer;
2609 ret = 1;
2610 } else {
2611 *line_r = NULL;
2612 ret = 0;
2616 return ret;
2619 void libxl_xen_console_read_finish(libxl_ctx *ctx,
2620 libxl_xen_console_reader *cr)
2622 free(cr->buffer);
2623 free(cr);
2626 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
2628 libxl__gc gc = LIBXL_INIT_GC(ctx);
2629 char *dompath = libxl__xs_get_dompath(&gc, domid);
2630 char *vm_path, *start_time;
2631 uint32_t ret;
2633 vm_path = libxl__xs_read(
2634 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dompath));
2635 start_time = libxl__xs_read(
2636 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/start_time", vm_path));
2637 if (start_time == NULL) {
2638 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2639 "Can't get start time of domain '%d'", domid);
2640 ret = -1;
2641 }else{
2642 ret = strtoul(start_time, NULL, 10);
2644 libxl__free_all(&gc);
2645 return ret;
2648 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
2650 int rc;
2651 char _buf[32768];
2653 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
2654 0, _buf);
2655 if (rc < 0) {
2656 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2657 "Can not get tmem list");
2658 return NULL;
2661 return strdup(_buf);
2664 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
2666 int rc;
2668 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
2669 0, NULL);
2670 if (rc < 0) {
2671 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2672 "Can not freeze tmem pools");
2673 return ERROR_FAIL;
2676 return rc;
2679 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
2681 int rc;
2683 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
2684 0, NULL);
2685 if (rc < 0) {
2686 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2687 "Can not destroy tmem pools");
2688 return ERROR_FAIL;
2691 return rc;
2694 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
2696 int rc;
2698 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
2699 0, NULL);
2700 if (rc < 0) {
2701 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2702 "Can not thaw tmem pools");
2703 return ERROR_FAIL;
2706 return rc;
2709 static int32_t tmem_setop_from_string(char *set_name)
2711 if (!strcmp(set_name, "weight"))
2712 return TMEMC_SET_WEIGHT;
2713 else if (!strcmp(set_name, "cap"))
2714 return TMEMC_SET_CAP;
2715 else if (!strcmp(set_name, "compress"))
2716 return TMEMC_SET_COMPRESS;
2717 else
2718 return -1;
2721 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
2723 int rc;
2724 int32_t subop = tmem_setop_from_string(name);
2726 if (subop == -1) {
2727 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2728 "Invalid set, valid sets are <weight|cap|compress>");
2729 return ERROR_INVAL;
2731 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
2732 if (rc < 0) {
2733 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2734 "Can not set tmem %s", name);
2735 return ERROR_FAIL;
2738 return rc;
2741 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
2742 char* uuid, int auth)
2744 int rc;
2746 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
2747 if (rc < 0) {
2748 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2749 "Can not set tmem shared auth");
2750 return ERROR_FAIL;
2753 return rc;
2756 int libxl_tmem_freeable(libxl_ctx *ctx)
2758 int rc;
2760 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
2761 if (rc < 0) {
2762 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2763 "Can not get tmem freeable memory");
2764 return ERROR_FAIL;
2767 return rc;
2770 void libxl_file_reference_destroy(libxl_file_reference *f)
2772 libxl__file_reference_unmap(f);
2773 free(f->path);
2776 int libxl_get_freecpus(libxl_ctx *ctx, libxl_cpumap *cpumap)
2778 int ncpus;
2780 ncpus = libxl_get_max_cpus(ctx);
2781 if (ncpus == 0)
2782 return ERROR_FAIL;
2784 cpumap->map = xc_cpupool_freeinfo(ctx->xch);
2785 if (cpumap->map == NULL)
2786 return ERROR_FAIL;
2788 cpumap->size = (ncpus + 7) / 8;
2790 return 0;
2793 int libxl_create_cpupool(libxl_ctx *ctx, const char *name, int schedid,
2794 libxl_cpumap cpumap, libxl_uuid *uuid,
2795 uint32_t *poolid)
2797 libxl__gc gc = LIBXL_INIT_GC(ctx);
2798 int rc;
2799 int i;
2800 xs_transaction_t t;
2801 char *uuid_string;
2803 uuid_string = libxl__uuid2string(&gc, *uuid);
2804 if (!uuid_string) {
2805 libxl__free_all(&gc);
2806 return ERROR_NOMEM;
2809 rc = xc_cpupool_create(ctx->xch, poolid, schedid);
2810 if (rc) {
2811 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2812 "Could not create cpupool");
2813 libxl__free_all(&gc);
2814 return ERROR_FAIL;
2817 libxl_for_each_cpu(i, cpumap)
2818 if (libxl_cpumap_test(&cpumap, i)) {
2819 rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
2820 if (rc) {
2821 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2822 "Error moving cpu to cpupool");
2823 libxl_destroy_cpupool(ctx, *poolid);
2824 libxl__free_all(&gc);
2825 return ERROR_FAIL;
2829 for (;;) {
2830 t = xs_transaction_start(ctx->xsh);
2832 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/pool/%d", *poolid));
2833 libxl__xs_write(&gc, t,
2834 libxl__sprintf(&gc, "/local/pool/%d/uuid", *poolid),
2835 "%s", uuid_string);
2836 libxl__xs_write(&gc, t,
2837 libxl__sprintf(&gc, "/local/pool/%d/name", *poolid),
2838 "%s", name);
2840 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN)) {
2841 libxl__free_all(&gc);
2842 return 0;
2847 int libxl_destroy_cpupool(libxl_ctx *ctx, uint32_t poolid)
2849 libxl__gc gc = LIBXL_INIT_GC(ctx);
2850 int rc, i;
2851 xc_cpupoolinfo_t *info;
2852 xs_transaction_t t;
2853 libxl_cpumap cpumap;
2855 info = xc_cpupool_getinfo(ctx->xch, poolid);
2856 if (info == NULL) {
2857 libxl__free_all(&gc);
2858 return ERROR_NOMEM;
2861 rc = ERROR_INVAL;
2862 if ((info->cpupool_id != poolid) || (info->n_dom))
2863 goto out;
2865 rc = ERROR_NOMEM;
2866 if (libxl_cpumap_alloc(ctx, &cpumap))
2867 goto out;
2869 memcpy(cpumap.map, info->cpumap, cpumap.size);
2870 libxl_for_each_cpu(i, cpumap)
2871 if (libxl_cpumap_test(&cpumap, i)) {
2872 rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
2873 if (rc) {
2874 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2875 "Error removing cpu from cpupool");
2876 rc = ERROR_FAIL;
2877 goto out1;
2881 rc = xc_cpupool_destroy(ctx->xch, poolid);
2882 if (rc) {
2883 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "Could not destroy cpupool");
2884 rc = ERROR_FAIL;
2885 goto out1;
2888 for (;;) {
2889 t = xs_transaction_start(ctx->xsh);
2891 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/pool/%d", poolid));
2893 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
2894 break;
2897 rc = 0;
2899 out1:
2900 libxl_cpumap_destroy(&cpumap);
2901 out:
2902 xc_cpupool_infofree(ctx->xch, info);
2903 libxl__free_all(&gc);
2905 return rc;
2908 int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid)
2910 libxl__gc gc = LIBXL_INIT_GC(ctx);
2911 xs_transaction_t t;
2912 xc_cpupoolinfo_t *info;
2913 int rc;
2915 info = xc_cpupool_getinfo(ctx->xch, poolid);
2916 if (info == NULL) {
2917 libxl__free_all(&gc);
2918 return ERROR_NOMEM;
2921 rc = ERROR_INVAL;
2922 if (info->cpupool_id != poolid)
2923 goto out;
2925 rc = 0;
2927 for (;;) {
2928 t = xs_transaction_start(ctx->xsh);
2930 libxl__xs_write(&gc, t,
2931 libxl__sprintf(&gc, "/local/pool/%d/name", poolid),
2932 "%s", name);
2934 if (xs_transaction_end(ctx->xsh, t, 0))
2935 break;
2937 if (errno == EAGAIN)
2938 continue;
2940 rc = ERROR_FAIL;
2941 break;
2944 out:
2945 xc_cpupool_infofree(ctx->xch, info);
2946 libxl__free_all(&gc);
2948 return rc;
2951 int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
2953 int rc;
2955 rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
2956 if (rc) {
2957 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2958 "Error moving cpu to cpupool");
2959 return ERROR_FAIL;
2961 return 0;
2964 int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
2966 int rc = 0;
2967 int cpu;
2968 libxl_cpumap freemap;
2969 libxl_topologyinfo topology;
2971 if (libxl_get_freecpus(ctx, &freemap)) {
2972 return ERROR_FAIL;
2975 if (libxl_get_topologyinfo(ctx, &topology)) {
2976 rc = ERROR_FAIL;
2977 goto out;
2980 *cpus = 0;
2981 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
2982 if (libxl_cpumap_test(&freemap, cpu) &&
2983 (topology.nodemap.array[cpu] == node) &&
2984 !libxl_cpupool_cpuadd(ctx, poolid, cpu)) {
2985 (*cpus)++;
2989 libxl_topologyinfo_destroy(&topology);
2991 out:
2992 libxl_cpumap_destroy(&freemap);
2993 return rc;
2996 int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
2998 int rc;
3000 rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
3001 if (rc) {
3002 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3003 "Error removing cpu from cpupool");
3004 return ERROR_FAIL;
3006 return 0;
3009 int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3011 int ret = 0;
3012 int n_pools;
3013 int p;
3014 int cpu;
3015 libxl_topologyinfo topology;
3016 libxl_cpupoolinfo *poolinfo;
3018 poolinfo = libxl_list_cpupool(ctx, &n_pools);
3019 if (!poolinfo) {
3020 return ERROR_NOMEM;
3023 if (libxl_get_topologyinfo(ctx, &topology)) {
3024 ret = ERROR_FAIL;
3025 goto out;
3028 *cpus = 0;
3029 for (p = 0; p < n_pools; p++) {
3030 if (poolinfo[p].poolid == poolid) {
3031 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3032 if ((topology.nodemap.array[cpu] == node) &&
3033 libxl_cpumap_test(&poolinfo[p].cpumap, cpu) &&
3034 !libxl_cpupool_cpuremove(ctx, poolid, cpu)) {
3035 (*cpus)++;
3041 libxl_topologyinfo_destroy(&topology);
3043 out:
3044 for (p = 0; p < n_pools; p++) {
3045 libxl_cpupoolinfo_destroy(poolinfo + p);
3048 return ret;
3051 int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
3053 libxl__gc gc = LIBXL_INIT_GC(ctx);
3054 int rc;
3055 char *dom_path;
3056 char *vm_path;
3057 char *poolname;
3058 xs_transaction_t t;
3060 dom_path = libxl__xs_get_dompath(&gc, domid);
3061 if (!dom_path) {
3062 libxl__free_all(&gc);
3063 return ERROR_FAIL;
3066 rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
3067 if (rc) {
3068 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3069 "Error moving domain to cpupool");
3070 libxl__free_all(&gc);
3071 return ERROR_FAIL;
3074 for (;;) {
3075 t = xs_transaction_start(ctx->xsh);
3077 poolname = libxl__cpupoolid_to_name(&gc, poolid);
3078 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
3079 if (!vm_path)
3080 break;
3082 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/pool_name", vm_path),
3083 "%s", poolname);
3085 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3086 break;
3089 libxl__free_all(&gc);
3090 return 0;