debuggers.hg

view tools/libxl/libxl.c @ 22906:700ac6445812

Now add KDB to the non-kdb tree
author Mukesh Rathor
date Thu Feb 03 15:42:41 2011 -0800 (2011-02-03)
parents 74cd0f668546
children
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 (new_name[0]) {
145 /* nonempty names must be unique */
146 uint32_t domid_e;
147 rc = libxl_name_to_domid(ctx, new_name, &domid_e);
148 if (rc == ERROR_INVAL) {
149 /* no such domain, good */
150 } else if (rc != 0) {
151 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unexpected error"
152 "checking for existing domain");
153 goto x_rc;
154 } else if (domid_e == domid) {
155 /* domain already has this name, ok (but we do still
156 * need the rest of the code as we may need to check
157 * old_name, for example). */
158 } else {
159 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "domain with name \"%s\""
160 " already exists.", new_name);
161 rc = ERROR_INVAL;
162 goto x_rc;
163 }
164 }
166 if (old_name) {
167 got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len);
168 if (!got_old_name) {
169 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno, "check old name"
170 " for domain %"PRIu32" allegedly named `%s'",
171 domid, old_name);
172 goto x_fail;
173 }
174 if (strcmp(old_name, got_old_name)) {
175 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "domain %"PRIu32" allegedly named "
176 "`%s' is actually named `%s' - racing ?",
177 domid, old_name, got_old_name);
178 free(got_old_name);
179 goto x_fail;
180 }
181 free(got_old_name);
182 }
183 if (!xs_write(ctx->xsh, trans, name_path,
184 new_name, strlen(new_name))) {
185 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "failed to write new name `%s'"
186 " for domain %"PRIu32" previously named `%s'",
187 new_name, domid, old_name);
188 goto x_fail;
189 }
191 if (our_trans) {
192 if (!xs_transaction_end(ctx->xsh, our_trans, 0)) {
193 trans = our_trans = 0;
194 if (errno != EAGAIN) {
195 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "failed to commit new name `%s'"
196 " for domain %"PRIu32" previously named `%s'",
197 new_name, domid, old_name);
198 goto x_fail;
199 }
200 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "need to retry rename transaction"
201 " for domain %"PRIu32" (name_path=\"%s\", new_name=\"%s\")",
202 domid, name_path, new_name);
203 goto retry_transaction;
204 }
205 our_trans = 0;
206 }
208 rc = 0;
209 x_rc:
210 if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1);
211 libxl__free_all(&gc);
212 return rc;
214 x_fail: rc = ERROR_FAIL; goto x_rc;
215 x_nomem: rc = ERROR_NOMEM; goto x_rc;
216 }
218 int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid)
219 {
220 libxl__gc gc = LIBXL_INIT_GC(ctx);
221 int rc = 0;
223 if (libxl__domain_is_hvm(ctx, domid)) {
224 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Called domain_resume on "
225 "non-cooperative hvm domain %u", domid);
226 rc = ERROR_NI;
227 goto out;
228 }
229 if (xc_domain_resume(ctx->xch, domid, 1)) {
230 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
231 "xc_domain_resume failed for domain %u",
232 domid);
233 rc = ERROR_FAIL;
234 goto out;
235 }
236 if (!xs_resume_domain(ctx->xsh, domid)) {
237 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
238 "xs_resume_domain failed for domain %u",
239 domid);
240 rc = ERROR_FAIL;
241 }
242 out:
243 libxl__free_all(&gc);
244 return 0;
245 }
247 /*
248 * Preserves a domain but rewrites xenstore etc to make it unique so
249 * that the domain can be restarted.
250 *
251 * Does not modify info so that it may be reused.
252 */
253 int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid,
254 libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid)
255 {
256 libxl__gc gc = LIBXL_INIT_GC(ctx);
257 struct xs_permissions roperm[2];
258 xs_transaction_t t;
259 char *preserved_name;
260 char *uuid_string;
261 char *vm_path;
262 char *dom_path;
264 int rc;
266 preserved_name = libxl__sprintf(&gc, "%s%s", info->name, name_suffix);
267 if (!preserved_name) {
268 libxl__free_all(&gc);
269 return ERROR_NOMEM;
270 }
272 uuid_string = libxl__uuid2string(&gc, new_uuid);
273 if (!uuid_string) {
274 libxl__free_all(&gc);
275 return ERROR_NOMEM;
276 }
278 dom_path = libxl__xs_get_dompath(&gc, domid);
279 if (!dom_path) {
280 libxl__free_all(&gc);
281 return ERROR_FAIL;
282 }
284 vm_path = libxl__sprintf(&gc, "/vm/%s", uuid_string);
285 if (!vm_path) {
286 libxl__free_all(&gc);
287 return ERROR_FAIL;
288 }
290 roperm[0].id = 0;
291 roperm[0].perms = XS_PERM_NONE;
292 roperm[1].id = domid;
293 roperm[1].perms = XS_PERM_READ;
295 retry_transaction:
296 t = xs_transaction_start(ctx->xsh);
298 xs_rm(ctx->xsh, t, vm_path);
299 xs_mkdir(ctx->xsh, t, vm_path);
300 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
302 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/vm", dom_path), vm_path, strlen(vm_path));
303 rc = libxl_domain_rename(ctx, domid, info->name, preserved_name, t);
304 if (rc) {
305 libxl__free_all(&gc);
306 return rc;
307 }
309 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
311 if (!xs_transaction_end(ctx->xsh, t, 0))
312 if (errno == EAGAIN)
313 goto retry_transaction;
315 libxl__free_all(&gc);
316 return 0;
317 }
319 static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
320 libxl_dominfo *xlinfo)
321 {
322 memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
323 xlinfo->domid = xcinfo->domain;
325 xlinfo->dying = !!(xcinfo->flags&XEN_DOMINF_dying);
326 xlinfo->shutdown = !!(xcinfo->flags&XEN_DOMINF_shutdown);
327 xlinfo->paused = !!(xcinfo->flags&XEN_DOMINF_paused);
328 xlinfo->blocked = !!(xcinfo->flags&XEN_DOMINF_blocked);
329 xlinfo->running = !!(xcinfo->flags&XEN_DOMINF_running);
331 if (xlinfo->shutdown || xlinfo->dying)
332 xlinfo->shutdown_reason = (xcinfo->flags>>XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask;
333 else
334 xlinfo->shutdown_reason = ~0;
336 xlinfo->current_memkb = PAGE_TO_MEMKB(xcinfo->tot_pages);
337 xlinfo->max_memkb = PAGE_TO_MEMKB(xcinfo->max_pages);
338 xlinfo->cpu_time = xcinfo->cpu_time;
339 xlinfo->vcpu_max_id = xcinfo->max_vcpu_id;
340 xlinfo->vcpu_online = xcinfo->nr_online_vcpus;
341 }
343 libxl_dominfo * libxl_list_domain(libxl_ctx *ctx, int *nb_domain)
344 {
345 libxl_dominfo *ptr;
346 int i, ret;
347 xc_domaininfo_t info[1024];
348 int size = 1024;
350 ptr = calloc(size, sizeof(libxl_dominfo));
351 if (!ptr) {
352 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating domain info");
353 return NULL;
354 }
356 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
357 if (ret<0) {
358 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
359 return NULL;
360 }
362 for (i = 0; i < ret; i++) {
363 xcinfo2xlinfo(&info[i], &ptr[i]);
364 }
365 *nb_domain = ret;
366 return ptr;
367 }
369 int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
370 uint32_t domid) {
371 xc_domaininfo_t xcinfo;
372 int ret;
374 ret = xc_domain_getinfolist(ctx->xch, domid, 1, &xcinfo);
375 if (ret<0) {
376 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
377 return ERROR_FAIL;
378 }
379 if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
381 xcinfo2xlinfo(&xcinfo, info_r);
382 return 0;
383 }
385 libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool)
386 {
387 libxl_cpupoolinfo *ptr, *tmp;
388 int i;
389 xc_cpupoolinfo_t *info;
390 uint32_t poolid;
392 ptr = NULL;
394 poolid = 0;
395 for (i = 0;; i++) {
396 info = xc_cpupool_getinfo(ctx->xch, poolid);
397 if (info == NULL)
398 break;
399 tmp = realloc(ptr, (i + 1) * sizeof(libxl_cpupoolinfo));
400 if (!tmp) {
401 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpupool info");
402 free(ptr);
403 xc_cpupool_infofree(ctx->xch, info);
404 return NULL;
405 }
406 ptr = tmp;
407 ptr[i].poolid = info->cpupool_id;
408 ptr[i].sched_id = info->sched_id;
409 ptr[i].n_dom = info->n_dom;
410 if (libxl_cpumap_alloc(ctx, &ptr[i].cpumap)) {
411 xc_cpupool_infofree(ctx->xch, info);
412 break;
413 }
414 memcpy(ptr[i].cpumap.map, info->cpumap, ptr[i].cpumap.size);
415 poolid = info->cpupool_id + 1;
416 xc_cpupool_infofree(ctx->xch, info);
417 }
419 *nb_pool = i;
420 return ptr;
421 }
423 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
424 libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm)
425 {
426 libxl_vminfo *ptr;
427 int index, i, ret;
428 xc_domaininfo_t info[1024];
429 int size = 1024;
431 ptr = calloc(size, sizeof(libxl_dominfo));
432 if (!ptr)
433 return NULL;
435 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
436 if (ret<0) {
437 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
438 return NULL;
439 }
440 for (index = i = 0; i < ret; i++) {
441 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
442 continue;
443 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
444 ptr[index].domid = info[i].domain;
446 index++;
447 }
448 *nb_vm = index;
449 return ptr;
450 }
452 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
453 uint32_t domid, int fd)
454 {
455 int hvm = libxl__domain_is_hvm(ctx, domid);
456 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
457 int debug = info != NULL && info->flags & XL_SUSPEND_DEBUG;
458 int rc = 0;
460 rc = libxl__domain_suspend_common(ctx, domid, fd, hvm, live, debug);
461 if (!rc && hvm)
462 rc = libxl__domain_save_device_model(ctx, domid, fd);
463 return rc;
464 }
466 int libxl_domain_pause(libxl_ctx *ctx, uint32_t domid)
467 {
468 int ret;
469 ret = xc_domain_pause(ctx->xch, domid);
470 if (ret<0) {
471 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "pausing domain %d", domid);
472 return ERROR_FAIL;
473 }
474 return 0;
475 }
477 int libxl_domain_core_dump(libxl_ctx *ctx, uint32_t domid,
478 const char *filename)
479 {
480 int ret;
481 ret = xc_domain_dumpcore(ctx->xch, domid, filename);
482 if (ret<0) {
483 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "core dumping domain %d to %s",
484 domid, filename);
485 return ERROR_FAIL;
486 }
487 return 0;
488 }
490 int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
491 {
492 libxl__gc gc = LIBXL_INIT_GC(ctx);
493 char *path;
494 char *state;
495 int ret, rc = 0;
497 if (libxl__domain_is_hvm(ctx, domid)) {
498 path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d/state", domid);
499 state = libxl__xs_read(&gc, XBT_NULL, path);
500 if (state != NULL && !strcmp(state, "paused")) {
501 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d/command", domid), "continue");
502 libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL);
503 }
504 }
505 ret = xc_domain_unpause(ctx->xch, domid);
506 if (ret<0) {
507 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain %d", domid);
508 rc = ERROR_FAIL;
509 }
510 libxl__free_all(&gc);
511 return rc;
512 }
514 static char *req_table[] = {
515 [0] = "poweroff",
516 [1] = "reboot",
517 [2] = "suspend",
518 [3] = "crash",
519 [4] = "halt",
520 };
522 int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
523 {
524 libxl__gc gc = LIBXL_INIT_GC(ctx);
525 char *shutdown_path;
526 char *dom_path;
528 if (req > ARRAY_SIZE(req_table)) {
529 libxl__free_all(&gc);
530 return ERROR_INVAL;
531 }
533 dom_path = libxl__xs_get_dompath(&gc, domid);
534 if (!dom_path) {
535 libxl__free_all(&gc);
536 return ERROR_FAIL;
537 }
539 if (libxl__domain_is_hvm(ctx,domid)) {
540 unsigned long pvdriver = 0;
541 int ret;
542 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
543 if (ret<0) {
544 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
545 libxl__free_all(&gc);
546 return ERROR_FAIL;
547 }
548 if (!pvdriver) {
549 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "HVM domain without PV drivers:"
550 " graceful shutdown not possible, use destroy");
551 libxl__free_all(&gc);
552 return ERROR_FAIL;
553 }
554 }
556 shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
557 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
559 libxl__free_all(&gc);
560 return 0;
561 }
563 int libxl_get_wait_fd(libxl_ctx *ctx, int *fd)
564 {
565 *fd = xs_fileno(ctx->xsh);
566 return 0;
567 }
569 int libxl_wait_for_domain_death(libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
570 {
571 waiter->path = strdup("@releaseDomain");
572 if (asprintf(&(waiter->token), "%d", LIBXL_EVENT_DOMAIN_DEATH) < 0)
573 return -1;
574 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
575 return -1;
576 return 0;
577 }
579 int libxl_wait_for_disk_ejects(libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
580 {
581 libxl__gc gc = LIBXL_INIT_GC(ctx);
582 int i, rc = -1;
583 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
585 if (!domid)
586 domid = guest_domid;
588 for (i = 0; i < num_disks; i++) {
589 if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
590 libxl__xs_get_dompath(&gc, domid),
591 libxl__device_disk_dev_number(disks[i].virtpath)) < 0)
592 goto out;
593 if (asprintf(&(waiter[i].token), "%d", LIBXL_EVENT_DISK_EJECT) < 0)
594 goto out;
595 xs_watch(ctx->xsh, waiter->path, waiter->token);
596 }
597 rc = 0;
598 out:
599 libxl__free_all(&gc);
600 return rc;
601 }
603 int libxl_get_event(libxl_ctx *ctx, libxl_event *event)
604 {
605 unsigned int num;
606 char **events = xs_read_watch(ctx->xsh, &num);
607 if (num != 2) {
608 free(events);
609 return ERROR_FAIL;
610 }
611 event->path = strdup(events[XS_WATCH_PATH]);
612 event->token = strdup(events[XS_WATCH_TOKEN]);
613 event->type = atoi(event->token);
614 free(events);
615 return 0;
616 }
618 int libxl_stop_waiting(libxl_ctx *ctx, libxl_waiter *waiter)
619 {
620 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
621 return ERROR_FAIL;
622 else
623 return 0;
624 }
626 int libxl_free_event(libxl_event *event)
627 {
628 free(event->path);
629 free(event->token);
630 return 0;
631 }
633 int libxl_free_waiter(libxl_waiter *waiter)
634 {
635 free(waiter->path);
636 free(waiter->token);
637 return 0;
638 }
640 int libxl_event_get_domain_death_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_dominfo *info)
641 {
642 if (libxl_domain_info(ctx, info, domid) < 0)
643 return 0;
645 if (info->running || (!info->shutdown && !info->dying))
646 return ERROR_INVAL;
648 return 1;
649 }
651 int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
652 {
653 libxl__gc gc = LIBXL_INIT_GC(ctx);
654 char *path;
655 char *backend;
656 char *value;
658 value = libxl__xs_read(&gc, XBT_NULL, event->path);
660 if (!value || strcmp(value, "eject")) {
661 libxl__free_all(&gc);
662 return 0;
663 }
665 path = strdup(event->path);
666 path[strlen(path) - 6] = '\0';
667 backend = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend", path));
669 disk->backend_domid = 0;
670 disk->domid = domid;
671 disk->physpath = strdup("");
672 disk->phystype = PHYSTYPE_EMPTY;
673 /* this value is returned to the user: do not free right away */
674 disk->virtpath = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/dev", backend));
675 disk->unpluggable = 1;
676 disk->readwrite = 0;
677 disk->is_cdrom = 1;
679 free(path);
680 libxl__free_all(&gc);
681 return 1;
682 }
684 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
685 {
686 libxl__gc gc = LIBXL_INIT_GC(ctx);
687 libxl_dominfo dominfo;
688 char *dom_path;
689 char *vm_path;
690 int rc, dm_present;
692 rc = libxl_domain_info(ctx, &dominfo, domid);
693 switch(rc) {
694 case 0:
695 break;
696 case ERROR_INVAL:
697 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "non-existant domain %d", domid);
698 default:
699 return rc;
700 }
702 if (libxl__domain_is_hvm(ctx, domid)) {
703 dm_present = 1;
704 } else {
705 char *pid;
706 pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/image/device-model-pid", domid));
707 dm_present = (pid != NULL);
708 }
710 dom_path = libxl__xs_get_dompath(&gc, domid);
711 if (!dom_path) {
712 rc = ERROR_FAIL;
713 goto out;
714 }
716 if (libxl_device_pci_shutdown(ctx, domid) < 0)
717 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "pci shutdown failed for domid %d", domid);
718 rc = xc_domain_pause(ctx->xch, domid);
719 if (rc < 0) {
720 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
721 }
722 if (dm_present) {
723 if (libxl__destroy_device_model(ctx, domid) < 0)
724 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model failed for %d", domid);
725 }
726 if (libxl__devices_destroy(ctx, domid, force) < 0)
727 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
729 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
730 if (vm_path)
731 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
732 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", vm_path);
734 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
735 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", dom_path);
737 libxl__userdata_destroyall(ctx, domid);
739 rc = xc_domain_destroy(ctx->xch, domid);
740 if (rc < 0) {
741 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
742 rc = ERROR_FAIL;
743 goto out;
744 }
745 rc = 0;
746 out:
747 libxl__free_all(&gc);
748 return 0;
749 }
751 int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num, libxl_console_constype type)
752 {
753 libxl__gc gc = LIBXL_INIT_GC(ctx);
754 char *p = libxl__sprintf(&gc, "%s/xenconsole", libxl_private_bindir_path());
755 char *domid_s = libxl__sprintf(&gc, "%d", domid);
756 char *cons_num_s = libxl__sprintf(&gc, "%d", cons_num);
757 char *cons_type_s;
759 switch (type) {
760 case LIBXL_CONSTYPE_PV:
761 cons_type_s = "pv";
762 break;
763 case LIBXL_CONSTYPE_SERIAL:
764 cons_type_s = "serial";
765 break;
766 default:
767 goto out;
768 }
770 execl(p, p, domid_s, "--num", cons_num_s, "--type", cons_type_s, (void *)NULL);
772 out:
773 libxl__free_all(&gc);
774 return ERROR_FAIL;
775 }
777 int libxl_primary_console_exec(libxl_ctx *ctx, uint32_t domid_vm)
778 {
779 uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm);
780 if (stubdomid)
781 return libxl_console_exec(ctx, stubdomid,
782 STUBDOM_CONSOLE_SERIAL, LIBXL_CONSTYPE_PV);
783 else {
784 if (libxl__domain_is_hvm(ctx, domid_vm))
785 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_SERIAL);
786 else
787 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_PV);
788 }
789 }
791 int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
792 {
793 libxl__gc gc = LIBXL_INIT_GC(ctx);
794 const char *vnc_port, *vfb_back;
795 const char *vnc_listen = NULL, *vnc_pass = NULL;
796 int port = 0, autopass_fd = -1;
797 char *vnc_bin, *args[] = {
798 "vncviewer",
799 NULL, /* hostname:display */
800 NULL, /* -autopass */
801 NULL,
802 };
804 vnc_port = libxl__xs_read(&gc, XBT_NULL,
805 libxl__sprintf(&gc,
806 "/local/domain/%d/console/vnc-port", domid));
807 if ( vnc_port )
808 port = atoi(vnc_port) - 5900;
810 vfb_back = libxl__xs_read(&gc, XBT_NULL,
811 libxl__sprintf(&gc,
812 "/local/domain/%d/device/vfb/0/backend", domid));
813 if ( vfb_back ) {
814 vnc_listen = libxl__xs_read(&gc, XBT_NULL,
815 libxl__sprintf(&gc,
816 "/local/domain/%d/console/vnc-listen", domid));
817 if ( autopass )
818 vnc_pass = libxl__xs_read(&gc, XBT_NULL,
819 libxl__sprintf(&gc,
820 "/local/domain/%d/console/vnc-pass", domid));
821 }
823 if ( NULL == vnc_listen )
824 vnc_listen = "localhost";
826 if ( (vnc_bin = getenv("VNCVIEWER")) )
827 args[0] = vnc_bin;
829 args[1] = libxl__sprintf(&gc, "%s:%d", vnc_listen, port);
831 if ( vnc_pass ) {
832 char tmpname[] = "/tmp/vncautopass.XXXXXX";
833 autopass_fd = mkstemp(tmpname);
834 if ( autopass_fd < 0 )
835 goto skip_autopass;
837 if ( unlink(tmpname) )
838 /* should never happen */
839 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unlink %s failed", tmpname);
841 if ( libxl_write_exactly(ctx, autopass_fd, vnc_pass, strlen(vnc_pass),
842 tmpname, "vnc password") ) {
843 do { close(autopass_fd); } while(errno == EINTR);
844 goto skip_autopass;
845 }
847 args[2] = "-autopass";
848 }
850 skip_autopass:
851 libxl__free_all(&gc);
852 libxl__exec(autopass_fd, -1, -1, args[0], args);
853 return 0;
854 }
856 /******************************************************************************/
858 static int validate_virtual_disk(libxl_ctx *ctx, char *file_name, libxl_disk_phystype disk_type)
859 {
860 struct stat stat_buf;
862 if ( (file_name[0] == '\0') && (disk_type == PHYSTYPE_EMPTY) )
863 return 0;
865 if ( stat(file_name, &stat_buf) != 0 ) {
866 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "failed to stat %s", file_name);
867 return ERROR_INVAL;
868 }
869 if ( disk_type == PHYSTYPE_PHY ) {
870 if ( !(S_ISBLK(stat_buf.st_mode)) ) {
871 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual disk %s is not a block device!\n",
872 file_name);
873 return ERROR_INVAL;
874 }
875 } else if ( S_ISREG(stat_buf.st_mode) && stat_buf.st_size == 0 ) {
876 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual disk %s size is 0!\n", file_name);
877 return ERROR_INVAL;
878 }
880 return 0;
881 }
883 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
884 {
885 libxl__gc gc = LIBXL_INIT_GC(ctx);
886 flexarray_t *front;
887 flexarray_t *back;
888 char *backend_type;
889 int devid;
890 libxl__device device;
891 int major, minor, rc;
893 rc = validate_virtual_disk(ctx, disk->physpath, disk->phystype);
894 if (rc)
895 return rc;
897 front = flexarray_make(16, 1);
898 if (!front) {
899 rc = ERROR_NOMEM;
900 goto out;
901 }
902 back = flexarray_make(16, 1);
903 if (!back) {
904 rc = ERROR_NOMEM;
905 goto out_free;
906 }
908 backend_type = libxl__device_disk_backend_type_of_phystype(disk->phystype);
909 devid = libxl__device_disk_dev_number(disk->virtpath);
910 if (devid==-1) {
911 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
912 " virtual disk identifier %s", disk->virtpath);
913 rc = ERROR_INVAL;
914 goto out_free;
915 }
917 device.backend_devid = devid;
918 device.backend_domid = disk->backend_domid;
919 device.devid = devid;
920 device.domid = disk->domid;
921 device.kind = DEVICE_VBD;
923 switch (disk->phystype) {
924 case PHYSTYPE_PHY: {
926 libxl__device_physdisk_major_minor(disk->physpath, &major, &minor);
927 flexarray_append(back, "physical-device");
928 flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
930 flexarray_append(back, "params");
931 flexarray_append(back, disk->physpath);
933 device.backend_kind = DEVICE_VBD;
934 break;
935 }
936 case PHYSTYPE_EMPTY:
937 break;
938 case PHYSTYPE_FILE:
939 /* let's pretend is tap:aio for the moment */
940 disk->phystype = PHYSTYPE_AIO;
941 case PHYSTYPE_AIO:
942 case PHYSTYPE_QCOW:
943 case PHYSTYPE_QCOW2:
944 case PHYSTYPE_VHD:
945 if (libxl__blktap_enabled(&gc)) {
946 const char *dev = libxl__blktap_devpath(&gc,
947 disk->physpath, disk->phystype);
948 if (!dev) {
949 rc = ERROR_FAIL;
950 goto out_free;
951 }
952 flexarray_append(back, "tapdisk-params");
953 flexarray_append(back, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
954 flexarray_append(back, "params");
955 flexarray_append(back, libxl__strdup(&gc, dev));
956 backend_type = "phy";
957 libxl__device_physdisk_major_minor(dev, &major, &minor);
958 flexarray_append(back, "physical-device");
959 flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
960 device.backend_kind = DEVICE_VBD;
962 break;
963 }
964 flexarray_append(back, "params");
965 flexarray_append(back, libxl__sprintf(&gc, "%s:%s",
966 libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
968 if (libxl__blktap_enabled(&gc))
969 device.backend_kind = DEVICE_TAP;
970 else
971 device.backend_kind = DEVICE_QDISK;
972 break;
974 default:
975 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
976 rc = ERROR_INVAL;
977 goto out_free;
978 }
980 flexarray_append(back, "frontend-id");
981 flexarray_append(back, libxl__sprintf(&gc, "%d", disk->domid));
982 flexarray_append(back, "online");
983 flexarray_append(back, "1");
984 flexarray_append(back, "removable");
985 flexarray_append(back, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
986 flexarray_append(back, "bootable");
987 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
988 flexarray_append(back, "state");
989 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
990 flexarray_append(back, "dev");
991 flexarray_append(back, disk->virtpath);
992 flexarray_append(back, "type");
993 flexarray_append(back, backend_type);
994 flexarray_append(back, "mode");
995 flexarray_append(back, disk->readwrite ? "w" : "r");
997 flexarray_append(front, "backend-id");
998 flexarray_append(front, libxl__sprintf(&gc, "%d", disk->backend_domid));
999 flexarray_append(front, "state");
1000 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1001 flexarray_append(front, "virtual-device");
1002 flexarray_append(front, libxl__sprintf(&gc, "%d", devid));
1003 flexarray_append(front, "device-type");
1004 flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
1006 if (0 /* protocol != native*/) {
1007 flexarray_append(front, "protocol");
1008 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
1011 libxl__device_generic_add(ctx, &device,
1012 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1013 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1015 rc = 0;
1017 out_free:
1018 flexarray_free(back);
1019 flexarray_free(front);
1020 out:
1021 libxl__free_all(&gc);
1022 return rc;
1025 int libxl_device_disk_del(libxl_ctx *ctx,
1026 libxl_device_disk *disk, int wait)
1028 libxl__device device;
1029 int devid;
1031 devid = libxl__device_disk_dev_number(disk->virtpath);
1032 device.backend_domid = disk->backend_domid;
1033 device.backend_devid = devid;
1034 device.backend_kind =
1035 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1036 device.domid = disk->domid;
1037 device.devid = devid;
1038 device.kind = DEVICE_VBD;
1039 return libxl__device_del(ctx, &device, wait);
1042 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
1044 libxl__gc gc = LIBXL_INIT_GC(ctx);
1045 const char *dev = NULL;
1046 char *ret = NULL;
1047 int phystype = disk->phystype;
1048 switch (phystype) {
1049 case PHYSTYPE_PHY: {
1050 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1051 dev = disk->physpath;
1052 break;
1054 case PHYSTYPE_FILE:
1055 /* let's pretend is tap:aio for the moment */
1056 phystype = PHYSTYPE_AIO;
1057 case PHYSTYPE_AIO:
1058 if (!libxl__blktap_enabled(&gc)) {
1059 dev = disk->physpath;
1060 break;
1062 case PHYSTYPE_VHD:
1063 if (libxl__blktap_enabled(&gc))
1064 dev = libxl__blktap_devpath(&gc, disk->physpath, phystype);
1065 else
1066 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "tapdisk2 is required to open a vhd disk\n");
1067 break;
1068 case PHYSTYPE_QCOW:
1069 case PHYSTYPE_QCOW2:
1070 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot locally attach a qcow or qcow2 disk image\n");
1071 break;
1073 default:
1074 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1075 break;
1077 if (dev != NULL)
1078 ret = strdup(dev);
1079 libxl__free_all(&gc);
1080 return ret;
1083 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1085 /* Nothing to do for PHYSTYPE_PHY. */
1087 /*
1088 * For other device types assume that the blktap2 process is
1089 * needed by the soon to be started domain and do nothing.
1090 */
1092 return 0;
1095 /******************************************************************************/
1096 int libxl_device_nic_init(libxl_device_nic *nic_info, int devnum)
1098 const uint8_t *r;
1099 libxl_uuid uuid;
1101 libxl_uuid_generate(&uuid);
1102 r = libxl_uuid_bytearray(&uuid);
1103 memset(nic_info, '\0', sizeof(*nic_info));
1105 nic_info->backend_domid = 0;
1106 nic_info->domid = 0;
1107 nic_info->devid = devnum;
1108 nic_info->mtu = 1492;
1109 nic_info->model = strdup("e1000");
1110 nic_info->mac[0] = 0x00;
1111 nic_info->mac[1] = 0x16;
1112 nic_info->mac[2] = 0x3e;
1113 nic_info->mac[3] = r[0] & 0x7f;
1114 nic_info->mac[4] = r[1];
1115 nic_info->mac[5] = r[2];
1116 nic_info->ifname = NULL;
1117 nic_info->bridge = strdup("xenbr0");
1118 if ( asprintf(&nic_info->script, "%s/vif-bridge",
1119 libxl_xen_script_dir_path()) < 0 )
1120 return ERROR_FAIL;
1121 nic_info->nictype = NICTYPE_IOEMU;
1122 return 0;
1125 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1127 libxl__gc gc = LIBXL_INIT_GC(ctx);
1128 flexarray_t *front;
1129 flexarray_t *back;
1130 libxl__device device;
1131 char *dompath, **l;
1132 unsigned int nb, rc;
1134 front = flexarray_make(16, 1);
1135 if (!front) {
1136 rc = ERROR_NOMEM;
1137 goto out;
1139 back = flexarray_make(16, 1);
1140 if (!back) {
1141 rc = ERROR_NOMEM;
1142 goto out_free;
1145 if (nic->devid == -1) {
1146 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1147 rc = ERROR_FAIL;
1148 goto out_free;
1150 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1151 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb))) {
1152 nic->devid = 0;
1153 } else {
1154 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1158 device.backend_devid = nic->devid;
1159 device.backend_domid = nic->backend_domid;
1160 device.backend_kind = DEVICE_VIF;
1161 device.devid = nic->devid;
1162 device.domid = nic->domid;
1163 device.kind = DEVICE_VIF;
1165 flexarray_append(back, "frontend-id");
1166 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->domid));
1167 flexarray_append(back, "online");
1168 flexarray_append(back, "1");
1169 flexarray_append(back, "state");
1170 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1171 flexarray_append(back, "script");
1172 flexarray_append(back, nic->script);
1173 flexarray_append(back, "mac");
1174 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1175 nic->mac[0], nic->mac[1], nic->mac[2],
1176 nic->mac[3], nic->mac[4], nic->mac[5]));
1177 flexarray_append(back, "bridge");
1178 flexarray_append(back, libxl__strdup(&gc, nic->bridge));
1179 flexarray_append(back, "handle");
1180 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->devid));
1182 flexarray_append(front, "backend-id");
1183 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->backend_domid));
1184 flexarray_append(front, "state");
1185 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1186 flexarray_append(front, "handle");
1187 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->devid));
1188 flexarray_append(front, "mac");
1189 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1190 nic->mac[0], nic->mac[1], nic->mac[2],
1191 nic->mac[3], nic->mac[4], nic->mac[5]));
1192 if (0 /* protocol != native*/) {
1193 flexarray_append(front, "protocol");
1194 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
1197 libxl__device_generic_add(ctx, &device,
1198 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1199 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1201 /* FIXME: wait for plug */
1202 rc = 0;
1203 out_free:
1204 flexarray_free(back);
1205 flexarray_free(front);
1206 out:
1207 libxl__free_all(&gc);
1208 return rc;
1211 int libxl_device_nic_del(libxl_ctx *ctx,
1212 libxl_device_nic *nic, int wait)
1214 libxl__device device;
1216 device.backend_devid = nic->devid;
1217 device.backend_domid = nic->backend_domid;
1218 device.backend_kind = DEVICE_VIF;
1219 device.devid = nic->devid;
1220 device.domid = nic->domid;
1221 device.kind = DEVICE_VIF;
1223 return libxl__device_del(ctx, &device, wait);
1226 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1228 libxl__gc gc = LIBXL_INIT_GC(ctx);
1229 char *dompath, *nic_path_fe;
1230 char **l, **list;
1231 char *val, *tok;
1232 unsigned int nb_nics, i;
1233 libxl_nicinfo *res, *nics;
1235 dompath = libxl__xs_get_dompath(&gc, domid);
1236 if (!dompath)
1237 goto err;
1238 list = l = libxl__xs_directory(&gc, XBT_NULL,
1239 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb_nics);
1240 if (!l)
1241 goto err;
1242 nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
1243 if (!res)
1244 goto err;
1245 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
1246 nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
1248 nics->backend = xs_read(ctx->xsh, XBT_NULL,
1249 libxl__sprintf(&gc, "%s/backend", nic_path_fe), NULL);
1250 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", nic_path_fe));
1251 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
1253 nics->devid = strtoul(*l, NULL, 10);
1254 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", nic_path_fe));
1255 nics->state = val ? strtoul(val, NULL, 10) : -1;
1256 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", nic_path_fe));
1257 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1258 ++i, tok = strtok(NULL, ":")) {
1259 nics->mac[i] = strtoul(tok, NULL, 16);
1261 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", nic_path_fe));
1262 nics->evtch = val ? strtol(val, NULL, 10) : -1;
1263 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", nic_path_fe));
1264 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
1265 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", nic_path_fe));
1266 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
1267 nics->frontend = xs_read(ctx->xsh, XBT_NULL,
1268 libxl__sprintf(&gc, "%s/frontend", nics->backend), NULL);
1269 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", nics->backend));
1270 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1271 nics->script = xs_read(ctx->xsh, XBT_NULL,
1272 libxl__sprintf(&gc, "%s/script", nics->backend), NULL);
1275 libxl__free_all(&gc);
1276 return res;
1277 err:
1278 libxl__free_all(&gc);
1279 return NULL;
1282 /******************************************************************************/
1283 void libxl_device_net2_init(libxl_device_net2 *net2_info, int devnum)
1285 const uint8_t *r;
1286 libxl_uuid uuid;
1288 libxl_uuid_generate(&uuid);
1289 r = libxl_uuid_bytearray(&uuid);
1290 memset(net2_info, '\0', sizeof(*net2_info));
1292 net2_info->devid = devnum;
1293 net2_info->front_mac[0] = 0x00;
1294 net2_info->front_mac[1] = 0x16;
1295 net2_info->front_mac[2] = 0x3e;;
1296 net2_info->front_mac[3] = 0x7f & r[0];
1297 net2_info->front_mac[4] = r[1];
1298 net2_info->front_mac[5] = r[2];
1299 net2_info->back_mac[0] = 0x00;
1300 net2_info->back_mac[1] = 0x16;
1301 net2_info->back_mac[2] = 0x3e;
1302 net2_info->back_mac[3] = 0x7f & r[3];
1303 net2_info->back_mac[4] = r[4];
1304 net2_info->back_mac[5] = r[5];
1305 net2_info->back_trusted = 1;
1306 net2_info->filter_mac = 1;
1307 net2_info->max_bypasses = 5;
1308 net2_info->bridge = strdup("xenbr0");
1311 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
1313 libxl__gc gc = LIBXL_INIT_GC(ctx);
1314 flexarray_t *front, *back;
1315 libxl__device device;
1316 char *dompath, *dom, **l;
1317 unsigned int nb;
1318 int rc;
1320 front = flexarray_make(16, 1);
1321 if (!front) {
1322 rc = ERROR_NOMEM;
1323 goto err;
1325 back = flexarray_make(16, 1);
1326 if (!back) {
1327 rc = ERROR_NOMEM;
1328 goto err_free;
1331 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1332 rc = ERROR_FAIL;
1333 goto err_free;
1335 dom = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/name", dompath));
1337 if (net2->devid == -1) {
1338 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1339 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb))) {
1340 net2->devid = 0;
1341 } else {
1342 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1346 device.backend_devid = net2->devid;
1347 device.backend_domid = net2->backend_domid;
1348 device.backend_kind = DEVICE_VIF2;
1349 device.devid = net2->devid;
1350 device.domid = net2->domid;
1351 device.kind = DEVICE_VIF2;
1353 flexarray_append(back, "domain");
1354 flexarray_append(back, dom);
1355 flexarray_append(back, "frontend-id");
1356 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->domid));
1358 flexarray_append(back, "local-trusted");
1359 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->back_trusted));
1360 flexarray_append(back, "mac");
1361 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1362 net2->back_mac[0], net2->back_mac[1],
1363 net2->back_mac[2], net2->back_mac[3],
1364 net2->back_mac[4], net2->back_mac[5]));
1366 flexarray_append(back, "remote-trusted");
1367 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->trusted));
1368 flexarray_append(back, "remote-mac");
1369 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1370 net2->front_mac[0], net2->front_mac[1],
1371 net2->front_mac[2], net2->front_mac[3],
1372 net2->front_mac[4], net2->front_mac[5]));
1374 flexarray_append(back, "max-bypasses");
1375 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->max_bypasses));
1376 flexarray_append(back, "filter-mac");
1377 flexarray_append(back, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1378 flexarray_append(back, "handle");
1379 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->devid));
1380 flexarray_append(back, "online");
1381 flexarray_append(back, "1");
1382 flexarray_append(back, "state");
1383 flexarray_append(back, "1");
1385 flexarray_append(front, "backend-id");
1386 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->backend_domid));
1388 flexarray_append(front, "local-trusted");
1389 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->trusted));
1390 flexarray_append(front, "mac");
1391 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1392 net2->front_mac[0], net2->front_mac[1],
1393 net2->front_mac[2], net2->front_mac[3],
1394 net2->front_mac[4], net2->front_mac[5]));
1396 flexarray_append(front, "remote-trusted");
1397 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->back_trusted));
1398 flexarray_append(front, "remote-mac");
1399 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1400 net2->back_mac[0], net2->back_mac[1],
1401 net2->back_mac[2], net2->back_mac[3],
1402 net2->back_mac[4], net2->back_mac[5]));
1404 flexarray_append(front, "filter-mac");
1405 flexarray_append(front, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1406 flexarray_append(front, "state");
1407 flexarray_append(front, "1");
1409 libxl__device_generic_add(ctx, &device,
1410 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1411 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1413 /* FIXME: wait for plug */
1414 rc = 0;
1415 err_free:
1416 flexarray_free(back);
1417 flexarray_free(front);
1418 err:
1419 libxl__free_all(&gc);
1420 return rc;
1423 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1425 libxl__gc gc = LIBXL_INIT_GC(ctx);
1426 char *dompath, *net2_path_fe;
1427 char **l;
1428 char *val, *tok;
1429 unsigned int nb_net2s, i;
1430 libxl_net2info *res, *net2s;
1432 dompath = libxl__xs_get_dompath(&gc, domid);
1433 if (!dompath)
1434 goto err;
1435 l = libxl__xs_directory(&gc, XBT_NULL,
1436 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb_net2s);
1437 if (!l)
1438 goto err;
1439 res = calloc(nb_net2s, sizeof (libxl_net2info));
1440 if (!res)
1441 goto err;
1442 net2s = res;
1443 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
1444 net2_path_fe = libxl__sprintf(&gc, "%s/device/vif2/%s", dompath, *l);
1446 net2s->backend = libxl__xs_read(&gc, XBT_NULL,
1447 libxl__sprintf(&gc, "%s/backend", net2_path_fe));
1448 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", net2_path_fe));
1449 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
1451 net2s->devid = strtoul(*l, NULL, 10);
1452 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", net2_path_fe));
1453 net2s->state = val ? strtoul(val, NULL, 10) : -1;
1455 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", net2_path_fe));
1456 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1457 ++i, tok = strtok(NULL, ":")) {
1458 net2s->mac[i] = strtoul(tok, NULL, 16);
1460 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-trusted", net2_path_fe));
1461 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
1463 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-mac", net2_path_fe));
1464 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1465 ++i, tok = strtok(NULL, ":")) {
1466 net2s->back_mac[i] = strtoul(tok, NULL, 16);
1468 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/filter-mac", net2_path_fe));
1469 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
1471 net2s->frontend = libxl__xs_read(&gc, XBT_NULL,
1472 libxl__sprintf(&gc, "%s/frontend", net2s->backend));
1473 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", net2s->backend));
1474 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1477 libxl__free_all(&gc);
1478 return res;
1479 err:
1480 libxl__free_all(&gc);
1481 return NULL;
1484 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
1486 libxl__device device;
1488 device.backend_devid = net2->devid;
1489 device.backend_domid = net2->backend_domid;
1490 device.backend_kind = DEVICE_VIF2;
1491 device.devid = net2->devid;
1492 device.domid = net2->domid;
1493 device.kind = DEVICE_VIF2;
1495 return libxl__device_del(ctx, &device, wait);
1499 /******************************************************************************/
1500 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1502 libxl__gc gc = LIBXL_INIT_GC(ctx);
1503 flexarray_t *front;
1504 flexarray_t *back;
1505 libxl__device device;
1506 int rc;
1508 front = flexarray_make(16, 1);
1509 if (!front) {
1510 rc = ERROR_NOMEM;
1511 goto out;
1513 back = flexarray_make(16, 1);
1514 if (!back) {
1515 rc = ERROR_NOMEM;
1516 goto out_free;
1519 device.backend_devid = console->devid;
1520 device.backend_domid = console->backend_domid;
1521 device.backend_kind = DEVICE_CONSOLE;
1522 device.devid = console->devid;
1523 device.domid = console->domid;
1524 device.kind = DEVICE_CONSOLE;
1526 flexarray_append(back, "frontend-id");
1527 flexarray_append(back, libxl__sprintf(&gc, "%d", console->domid));
1528 flexarray_append(back, "online");
1529 flexarray_append(back, "1");
1530 flexarray_append(back, "state");
1531 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1532 flexarray_append(back, "domain");
1533 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1534 flexarray_append(back, "protocol");
1535 flexarray_append(back, LIBXL_XENCONSOLE_PROTOCOL);
1537 flexarray_append(front, "backend-id");
1538 flexarray_append(front, libxl__sprintf(&gc, "%d", console->backend_domid));
1539 flexarray_append(front, "limit");
1540 flexarray_append(front, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
1541 flexarray_append(front, "type");
1542 if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
1543 flexarray_append(front, "xenconsoled");
1544 else
1545 flexarray_append(front, "ioemu");
1546 flexarray_append(front, "output");
1547 flexarray_append(front, console->output);
1549 if (device.devid == 0) {
1550 if (console->build_state == NULL) {
1551 rc = ERROR_INVAL;
1552 goto out_free;
1554 flexarray_append(front, "port");
1555 flexarray_append(front, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
1556 flexarray_append(front, "ring-ref");
1557 flexarray_append(front, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
1558 } else {
1559 flexarray_append(front, "state");
1560 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1561 flexarray_append(front, "protocol");
1562 flexarray_append(front, LIBXL_XENCONSOLE_PROTOCOL);
1565 libxl__device_generic_add(ctx, &device,
1566 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1567 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1568 rc = 0;
1569 out_free:
1570 flexarray_free(back);
1571 flexarray_free(front);
1572 out:
1573 libxl__free_all(&gc);
1574 return rc;
1577 /******************************************************************************/
1578 void libxl_device_vkb_init(libxl_device_vkb *vkb, int dev_num)
1580 memset(vkb, 0x00, sizeof(libxl_device_vkb));
1581 vkb->devid = dev_num;
1584 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1586 libxl__gc gc = LIBXL_INIT_GC(ctx);
1587 flexarray_t *front;
1588 flexarray_t *back;
1589 libxl__device device;
1590 int rc;
1592 front = flexarray_make(16, 1);
1593 if (!front) {
1594 rc = ERROR_NOMEM;
1595 goto out;
1597 back = flexarray_make(16, 1);
1598 if (!back) {
1599 rc = ERROR_NOMEM;
1600 goto out_free;
1603 device.backend_devid = vkb->devid;
1604 device.backend_domid = vkb->backend_domid;
1605 device.backend_kind = DEVICE_VKBD;
1606 device.devid = vkb->devid;
1607 device.domid = vkb->domid;
1608 device.kind = DEVICE_VKBD;
1610 flexarray_append(back, "frontend-id");
1611 flexarray_append(back, libxl__sprintf(&gc, "%d", vkb->domid));
1612 flexarray_append(back, "online");
1613 flexarray_append(back, "1");
1614 flexarray_append(back, "state");
1615 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1616 flexarray_append(back, "domain");
1617 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1619 flexarray_append(front, "backend-id");
1620 flexarray_append(front, libxl__sprintf(&gc, "%d", vkb->backend_domid));
1621 flexarray_append(front, "state");
1622 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1624 libxl__device_generic_add(ctx, &device,
1625 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1626 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1627 rc = 0;
1628 out_free:
1629 flexarray_free(back);
1630 flexarray_free(front);
1631 out:
1632 libxl__free_all(&gc);
1633 return rc;
1636 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1638 return ERROR_NI;
1641 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1643 return ERROR_NI;
1646 static unsigned int libxl_append_disk_list_of_type(libxl_ctx *ctx,
1647 uint32_t domid,
1648 const char *type,
1649 libxl_device_disk **disks,
1650 unsigned int *ndisks)
1652 libxl__gc gc = LIBXL_INIT_GC(ctx);
1653 char *be_path = NULL;
1654 char **dir = NULL;
1655 unsigned int n = 0, len = 0;
1656 libxl_device_disk *pdisk = NULL, *pdisk_end = NULL;
1657 char *physpath_tmp = NULL;
1659 be_path = libxl__sprintf(&gc, "%s/backend/%s/%d",
1660 libxl__xs_get_dompath(&gc, 0), type, domid);
1661 dir = libxl__xs_directory(&gc, XBT_NULL, be_path, &n);
1662 if (dir) {
1663 *disks = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
1664 pdisk = *disks + *ndisks;
1665 *ndisks += n;
1666 pdisk_end = *disks + *ndisks;
1667 for (; pdisk < pdisk_end; pdisk++, dir++) {
1668 pdisk->backend_domid = 0;
1669 pdisk->domid = domid;
1670 physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path, *dir), &len);
1671 if (physpath_tmp && strchr(physpath_tmp, ':')) {
1672 pdisk->physpath = strdup(strchr(physpath_tmp, ':') + 1);
1673 free(physpath_tmp);
1674 } else {
1675 pdisk->physpath = physpath_tmp;
1677 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path, *dir)), &(pdisk->phystype));
1678 pdisk->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path, *dir), &len);
1679 pdisk->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path, *dir)));
1680 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path, *dir)), "w"))
1681 pdisk->readwrite = 1;
1682 else
1683 pdisk->readwrite = 0;
1684 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))));
1685 pdisk->is_cdrom = !strcmp(type, "cdrom");
1689 libxl__free_all(&gc);
1690 return n;
1693 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
1695 libxl_device_disk *disks = NULL;
1696 unsigned int ndisks = 0;
1698 *num = libxl_append_disk_list_of_type(ctx, domid, "vbd", &disks, &ndisks);
1699 *num += libxl_append_disk_list_of_type(ctx, domid, "tap", &disks, &ndisks);
1700 *num += libxl_append_disk_list_of_type(ctx, domid, "qdisk", &disks, &ndisks);
1702 return disks;
1705 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
1706 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
1708 libxl__gc gc = LIBXL_INIT_GC(ctx);
1709 char *dompath, *diskpath;
1710 char *val;
1712 dompath = libxl__xs_get_dompath(&gc, domid);
1713 diskinfo->devid = libxl__device_disk_dev_number(disk->virtpath);
1715 /* tap devices entries in xenstore are written as vbd devices. */
1716 diskpath = libxl__sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
1717 diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
1718 libxl__sprintf(&gc, "%s/backend", diskpath), NULL);
1719 if (!diskinfo->backend) {
1720 libxl__free_all(&gc);
1721 return ERROR_FAIL;
1723 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", diskpath));
1724 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
1725 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", diskpath));
1726 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
1727 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", diskpath));
1728 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
1729 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/ring-ref", diskpath));
1730 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
1731 diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
1732 libxl__sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
1733 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", diskinfo->backend));
1734 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1736 libxl__free_all(&gc);
1737 return 0;
1740 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1742 int num, i;
1743 uint32_t stubdomid;
1744 libxl_device_disk *disks;
1745 int ret = ERROR_FAIL;
1747 if (!disk->physpath) {
1748 disk->physpath = strdup("");
1749 disk->phystype = PHYSTYPE_EMPTY;
1751 disks = libxl_device_disk_list(ctx, domid, &num);
1752 for (i = 0; i < num; i++) {
1753 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1754 /* found */
1755 break;
1757 if (i == num) {
1758 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual device not found");
1759 goto out;
1762 ret = 0;
1764 libxl_device_disk_del(ctx, disks + i, 1);
1765 libxl_device_disk_add(ctx, domid, disk);
1766 stubdomid = libxl_get_stubdom_id(ctx, domid);
1767 if (stubdomid) {
1768 disks[i].domid = stubdomid;
1769 libxl_device_disk_del(ctx, disks + i, 1);
1770 disk->domid = stubdomid;
1771 libxl_device_disk_add(ctx, stubdomid, disk);
1772 disk->domid = domid;
1774 out:
1775 for (i = 0; i < num; i++)
1776 libxl_device_disk_destroy(&disks[i]);
1777 free(disks);
1778 return ret;
1781 /******************************************************************************/
1782 void libxl_device_vfb_init(libxl_device_vfb *vfb, int dev_num)
1784 memset(vfb, 0x00, sizeof(libxl_device_vfb));
1785 vfb->devid = dev_num;
1786 vfb->display = NULL;
1787 vfb->xauthority = NULL;
1788 vfb->vnc = 1;
1789 vfb->vncpasswd = NULL;
1790 vfb->vnclisten = strdup("127.0.0.1");
1791 vfb->vncdisplay = 0;
1792 vfb->vncunused = 1;
1793 vfb->keymap = NULL;
1794 vfb->sdl = 0;
1795 vfb->opengl = 0;
1798 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1800 libxl__gc gc = LIBXL_INIT_GC(ctx);
1801 flexarray_t *front;
1802 flexarray_t *back;
1803 libxl__device device;
1804 int rc;
1806 front = flexarray_make(16, 1);
1807 if (!front) {
1808 rc = ERROR_NOMEM;
1809 goto out;
1811 back = flexarray_make(16, 1);
1812 if (!back) {
1813 rc = ERROR_NOMEM;
1814 goto out_free;
1817 device.backend_devid = vfb->devid;
1818 device.backend_domid = vfb->backend_domid;
1819 device.backend_kind = DEVICE_VFB;
1820 device.devid = vfb->devid;
1821 device.domid = vfb->domid;
1822 device.kind = DEVICE_VFB;
1824 flexarray_vappend(back, "frontend-id", libxl__sprintf(&gc, "%d", vfb->domid), NULL);
1825 flexarray_vappend(back, "online", "1", NULL);
1826 flexarray_vappend(back, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1827 flexarray_vappend(back, "domain", libxl__domid_to_name(&gc, domid), NULL);
1828 flexarray_vappend(back, "vnc", libxl__sprintf(&gc, "%d", vfb->vnc), NULL);
1829 flexarray_vappend(back, "vnclisten", vfb->vnclisten, NULL);
1830 flexarray_append(back, "vncpasswd");
1831 flexarray_append(back, vfb->vncpasswd);
1832 flexarray_vappend(back, "vncdisplay", libxl__sprintf(&gc, "%d", vfb->vncdisplay), NULL);
1833 flexarray_vappend(back, "vncunused", libxl__sprintf(&gc, "%d", vfb->vncunused), NULL);
1834 flexarray_vappend(back, "sdl", libxl__sprintf(&gc, "%d", vfb->sdl), NULL);
1835 flexarray_vappend(back, "opengl", libxl__sprintf(&gc, "%d", vfb->opengl), NULL);
1836 if (vfb->xauthority) {
1837 flexarray_vappend(back, "xauthority", vfb->xauthority, NULL);
1839 if (vfb->display) {
1840 flexarray_vappend(back, "display", vfb->display, NULL);
1843 flexarray_vappend(front, "backend-id", libxl__sprintf(&gc, "%d", vfb->backend_domid), NULL);
1844 flexarray_vappend(front, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1846 libxl__device_generic_add(ctx, &device,
1847 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1848 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1849 rc = 0;
1850 out_free:
1851 flexarray_free(front);
1852 flexarray_free(back);
1853 out:
1854 libxl__free_all(&gc);
1855 return rc;
1858 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1860 return ERROR_NI;
1863 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1865 return ERROR_NI;
1868 /******************************************************************************/
1870 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
1872 libxl__gc gc = LIBXL_INIT_GC(ctx);
1873 char *mem, *endptr;
1874 uint32_t memorykb;
1875 char *dompath = libxl__xs_get_dompath(&gc, domid);
1876 int rc = 1;
1878 mem = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/memory/target", dompath));
1879 if (!mem) {
1880 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
1881 goto out;
1883 memorykb = strtoul(mem, &endptr, 10);
1884 if (*endptr != '\0') {
1885 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
1886 goto out;
1889 if (max_memkb < memorykb) {
1890 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
1891 goto out;
1894 rc = 0;
1895 out:
1896 libxl__free_all(&gc);
1897 return rc;
1900 static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb)
1902 int rc;
1903 libxl_dominfo info;
1904 libxl_physinfo physinfo;
1905 char *target = NULL, *staticmax = NULL, *freememslack = NULL, *endptr = NULL;
1906 char *target_path = "/local/domain/0/memory/target";
1907 char *max_path = "/local/domain/0/memory/static-max";
1908 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
1909 xs_transaction_t t;
1910 libxl_ctx *ctx = libxl__gc_owner(gc);
1911 uint32_t free_mem_slack_kb = 0;
1913 retry_transaction:
1914 t = xs_transaction_start(ctx->xsh);
1916 target = libxl__xs_read(gc, t, target_path);
1917 staticmax = libxl__xs_read(gc, t, max_path);
1918 freememslack = libxl__xs_read(gc, t, free_mem_slack_path);
1919 if (target && staticmax && freememslack) {
1920 rc = 0;
1921 goto out;
1924 if (target) {
1925 *target_memkb = strtoul(target, &endptr, 10);
1926 if (*endptr != '\0') {
1927 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1928 "invalid memory target %s from %s\n", target, target_path);
1929 rc = ERROR_FAIL;
1930 goto out;
1934 rc = libxl_domain_info(ctx, &info, 0);
1935 if (rc < 0)
1936 goto out;
1938 rc = libxl_get_physinfo(ctx, &physinfo);
1939 if (rc < 0)
1940 goto out;
1942 if (target == NULL) {
1943 libxl__xs_write(gc, t, target_path, "%"PRIu32,
1944 (uint32_t) info.current_memkb);
1945 *target_memkb = (uint32_t) info.current_memkb;
1947 if (staticmax == NULL)
1948 libxl__xs_write(gc, t, max_path, "%"PRIu32,
1949 (uint32_t) info.max_memkb);
1951 if (freememslack == NULL) {
1952 free_mem_slack_kb = (uint32_t) (PAGE_TO_MEMKB(physinfo.total_pages) -
1953 info.current_memkb);
1954 /* From empirical measurements the free_mem_slack shouldn't be more
1955 * than 15% of the total memory present on the system. */
1956 if (free_mem_slack_kb > PAGE_TO_MEMKB(physinfo.total_pages) * 0.15)
1957 free_mem_slack_kb = PAGE_TO_MEMKB(physinfo.total_pages) * 0.15;
1958 libxl__xs_write(gc, t, free_mem_slack_path, "%"PRIu32, free_mem_slack_kb);
1960 rc = 0;
1962 out:
1963 if (!xs_transaction_end(ctx->xsh, t, 0)) {
1964 if (errno == EAGAIN)
1965 goto retry_transaction;
1966 else
1967 rc = ERROR_FAIL;
1971 return rc;
1974 /* returns how much memory should be left free in the system */
1975 static int libxl__get_free_memory_slack(libxl__gc *gc, uint32_t *free_mem_slack)
1977 int rc;
1978 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
1979 char *free_mem_slack_s, *endptr;
1980 uint32_t target_memkb;
1982 retry:
1983 free_mem_slack_s = libxl__xs_read(gc, XBT_NULL, free_mem_slack_path);
1984 if (!free_mem_slack_s) {
1985 rc = libxl__fill_dom0_memory_info(gc, &target_memkb);
1986 if (rc < 0)
1987 return rc;
1988 goto retry;
1989 } else {
1990 *free_mem_slack = strtoul(free_mem_slack_s, &endptr, 10);
1991 if (*endptr != '\0') {
1992 LIBXL__LOG_ERRNO(gc->owner, LIBXL__LOG_ERROR,
1993 "invalid free_mem_slack %s from %s\n",
1994 free_mem_slack_s, free_mem_slack_path);
1995 return ERROR_FAIL;
1998 return 0;
2001 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
2002 int32_t target_memkb, int relative, int enforce)
2004 libxl__gc gc = LIBXL_INIT_GC(ctx);
2005 int rc = 1, abort = 0;
2006 uint32_t memorykb = 0, videoram = 0;
2007 uint32_t current_target_memkb = 0, new_target_memkb = 0;
2008 char *memmax, *endptr, *videoram_s = NULL, *target = NULL;
2009 char *dompath = libxl__xs_get_dompath(&gc, domid);
2010 xc_domaininfo_t info;
2011 libxl_dominfo ptr;
2012 char *uuid;
2013 xs_transaction_t t;
2015 retry_transaction:
2016 t = xs_transaction_start(ctx->xsh);
2018 target = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2019 "%s/memory/target", dompath));
2020 if (!target && !domid) {
2021 xs_transaction_end(ctx->xsh, t, 1);
2022 rc = libxl__fill_dom0_memory_info(&gc, &current_target_memkb);
2023 if (rc < 0) {
2024 abort = 1;
2025 goto out;
2027 goto retry_transaction;
2028 } else if (!target) {
2029 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2030 "cannot get target memory info from %s/memory/target\n",
2031 dompath);
2032 abort = 1;
2033 goto out;
2034 } else {
2035 current_target_memkb = strtoul(target, &endptr, 10);
2036 if (*endptr != '\0') {
2037 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2038 "invalid memory target %s from %s/memory/target\n",
2039 target, dompath);
2040 abort = 1;
2041 goto out;
2044 memmax = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2045 "%s/memory/static-max", dompath));
2046 if (!memmax) {
2047 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2048 "cannot get memory info from %s/memory/static-max\n",
2049 dompath);
2050 abort = 1;
2051 goto out;
2053 memorykb = strtoul(memmax, &endptr, 10);
2054 if (*endptr != '\0') {
2055 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2056 "invalid max memory %s from %s/memory/static-max\n",
2057 memmax, dompath);
2058 abort = 1;
2059 goto out;
2062 if (relative) {
2063 if (target_memkb < 0 && abs(target_memkb) > current_target_memkb)
2064 new_target_memkb = 0;
2065 else
2066 new_target_memkb = current_target_memkb + target_memkb;
2067 } else
2068 new_target_memkb = target_memkb;
2069 if (new_target_memkb > memorykb) {
2070 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2071 "memory_dynamic_max must be less than or equal to"
2072 " memory_static_max\n");
2073 abort = 1;
2074 goto out;
2077 if (!domid && new_target_memkb < LIBXL_MIN_DOM0_MEM) {
2078 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2079 "new target %d for dom0 is below the minimum threshold\n",
2080 new_target_memkb);
2081 abort = 1;
2082 goto out;
2084 videoram_s = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2085 "%s/memory/videoram", dompath));
2086 videoram = videoram_s ? atoi(videoram_s) : 0;
2088 if (enforce) {
2089 memorykb = new_target_memkb;
2090 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
2091 LIBXL_MAXMEM_CONSTANT);
2092 if (rc != 0) {
2093 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2094 "xc_domain_setmaxmem domid=%d memkb=%d failed "
2095 "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
2096 abort = 1;
2097 goto out;
2101 new_target_memkb -= videoram;
2102 rc = xc_domain_set_pod_target(ctx->xch, domid,
2103 new_target_memkb / 4, NULL, NULL, NULL);
2104 if (rc != 0) {
2105 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2106 "xc_domain_set_pod_target domid=%d, memkb=%d "
2107 "failed rc=%d\n", domid, new_target_memkb / 4,
2108 rc);
2109 abort = 1;
2110 goto out;
2113 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/memory/target",
2114 dompath), "%"PRIu32, new_target_memkb);
2115 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2116 if (rc != 1 || info.domain != domid) {
2117 abort = 1;
2118 goto out;
2120 xcinfo2xlinfo(&info, &ptr);
2121 uuid = libxl__uuid2string(&gc, ptr.uuid);
2122 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "/vm/%s/memory", uuid),
2123 "%"PRIu32, new_target_memkb / 1024);
2125 out:
2126 if (!xs_transaction_end(ctx->xsh, t, abort) && !abort)
2127 if (errno == EAGAIN)
2128 goto retry_transaction;
2130 libxl__free_all(&gc);
2131 return rc;
2134 int libxl_get_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t *out_target)
2136 libxl__gc gc = LIBXL_INIT_GC(ctx);
2137 int rc = 1;
2138 char *target = NULL, *endptr = NULL;
2139 char *dompath = libxl__xs_get_dompath(&gc, domid);
2140 uint32_t target_memkb;
2142 target = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc,
2143 "%s/memory/target", dompath));
2144 if (!target && !domid) {
2145 rc = libxl__fill_dom0_memory_info(&gc, &target_memkb);
2146 if (rc < 0)
2147 goto out;
2148 } else if (!target) {
2149 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2150 "cannot get target memory info from %s/memory/target\n",
2151 dompath);
2152 goto out;
2153 } else {
2154 target_memkb = strtoul(target, &endptr, 10);
2155 if (*endptr != '\0') {
2156 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2157 "invalid memory target %s from %s/memory/target\n",
2158 target, dompath);
2159 goto out;
2162 *out_target = target_memkb;
2163 rc = 0;
2165 out:
2166 libxl__free_all(&gc);
2167 return rc;
2170 int libxl_domain_need_memory(libxl_ctx *ctx, libxl_domain_build_info *b_info,
2171 libxl_device_model_info *dm_info, uint32_t *need_memkb)
2173 *need_memkb = b_info->target_memkb;
2174 if (b_info->hvm) {
2175 *need_memkb += b_info->shadow_memkb + LIBXL_HVM_EXTRA_MEMORY;
2176 if (strstr(dm_info->device_model, "stubdom-dm"))
2177 *need_memkb += 32 * 1024;
2178 } else
2179 *need_memkb += b_info->shadow_memkb + LIBXL_PV_EXTRA_MEMORY;
2180 if (*need_memkb % (2 * 1024))
2181 *need_memkb += (2 * 1024) - (*need_memkb % (2 * 1024));
2182 return 0;
2185 int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
2187 int rc = 0;
2188 libxl_physinfo info;
2189 uint32_t freemem_slack;
2190 libxl__gc gc = LIBXL_INIT_GC(ctx);
2192 rc = libxl_get_physinfo(ctx, &info);
2193 if (rc < 0)
2194 goto out;
2195 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2196 if (rc < 0)
2197 goto out;
2199 if ((info.free_pages + info.scrub_pages) * 4 > freemem_slack)
2200 *memkb = (info.free_pages + info.scrub_pages) * 4 - freemem_slack;
2201 else
2202 *memkb = 0;
2204 out:
2205 libxl__free_all(&gc);
2206 return rc;
2209 int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
2210 memory_kb, int wait_secs)
2212 int rc = 0;
2213 libxl_physinfo info;
2214 uint32_t freemem_slack;
2215 libxl__gc gc = LIBXL_INIT_GC(ctx);
2217 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2218 if (rc < 0)
2219 goto out;
2220 while (wait_secs > 0) {
2221 rc = libxl_get_physinfo(ctx, &info);
2222 if (rc < 0)
2223 goto out;
2224 if (info.free_pages * 4 - freemem_slack >= memory_kb) {
2225 rc = 0;
2226 goto out;
2228 wait_secs--;
2229 sleep(1);
2231 rc = ERROR_NOMEM;
2233 out:
2234 libxl__free_all(&gc);
2235 return rc;
2238 int libxl_wait_for_memory_target(libxl_ctx *ctx, uint32_t domid, int wait_secs)
2240 int rc = 0;
2241 uint32_t target_memkb = 0;
2242 libxl_dominfo info;
2244 do {
2245 wait_secs--;
2246 sleep(1);
2248 rc = libxl_get_memory_target(ctx, domid, &target_memkb);
2249 if (rc < 0)
2250 goto out;
2252 rc = libxl_domain_info(ctx, &info, domid);
2253 if (rc < 0)
2254 return rc;
2255 } while (wait_secs > 0 && info.current_memkb > target_memkb);
2257 if (info.current_memkb <= target_memkb)
2258 rc = 0;
2259 else
2260 rc = ERROR_FAIL;
2262 out:
2263 return 0;
2266 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
2268 int rc = -1;
2270 switch (button) {
2271 case POWER_BUTTON:
2272 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2273 break;
2274 case SLEEP_BUTTON:
2275 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2276 break;
2277 default:
2278 break;
2281 return rc;
2284 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
2286 xc_physinfo_t xcphysinfo = { 0 };
2287 int rc;
2289 rc = xc_physinfo(ctx->xch, &xcphysinfo);
2290 if (rc != 0) {
2291 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting physinfo");
2292 return ERROR_FAIL;
2294 physinfo->threads_per_core = xcphysinfo.threads_per_core;
2295 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
2296 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
2297 physinfo->nr_cpus = xcphysinfo.nr_cpus;
2298 physinfo->cpu_khz = xcphysinfo.cpu_khz;
2299 physinfo->total_pages = xcphysinfo.total_pages;
2300 physinfo->free_pages = xcphysinfo.free_pages;
2301 physinfo->scrub_pages = xcphysinfo.scrub_pages;
2302 physinfo->nr_nodes = xcphysinfo.nr_nodes;
2303 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
2304 physinfo->phys_cap = xcphysinfo.capabilities;
2306 return 0;
2309 int libxl_get_topologyinfo(libxl_ctx *ctx, libxl_topologyinfo *info)
2311 xc_topologyinfo_t tinfo;
2312 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_core_t, coremap);
2313 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_socket_t, socketmap);
2314 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_node_t, nodemap);
2315 int i;
2316 int rc = 0;
2318 rc += libxl_cpuarray_alloc(ctx, &info->coremap);
2319 rc += libxl_cpuarray_alloc(ctx, &info->socketmap);
2320 rc += libxl_cpuarray_alloc(ctx, &info->nodemap);
2321 if (rc)
2322 goto fail;
2324 coremap = xc_hypercall_buffer_alloc(ctx->xch, coremap, sizeof(*coremap) * info->coremap.entries);
2325 socketmap = xc_hypercall_buffer_alloc(ctx->xch, socketmap, sizeof(*socketmap) * info->socketmap.entries);
2326 nodemap = xc_hypercall_buffer_alloc(ctx->xch, nodemap, sizeof(*nodemap) * info->nodemap.entries);
2327 if ((coremap == NULL) || (socketmap == NULL) || (nodemap == NULL))
2328 goto fail;
2330 set_xen_guest_handle(tinfo.cpu_to_core, coremap);
2331 set_xen_guest_handle(tinfo.cpu_to_socket, socketmap);
2332 set_xen_guest_handle(tinfo.cpu_to_node, nodemap);
2333 tinfo.max_cpu_index = info->coremap.entries - 1;
2334 if (xc_topologyinfo(ctx->xch, &tinfo) != 0)
2335 goto fail;
2337 for (i = 0; i <= tinfo.max_cpu_index; i++) {
2338 if (i < info->coremap.entries)
2339 info->coremap.array[i] = (coremap[i] == INVALID_TOPOLOGY_ID) ?
2340 LIBXL_CPUARRAY_INVALID_ENTRY : coremap[i];
2341 if (i < info->socketmap.entries)
2342 info->socketmap.array[i] = (socketmap[i] == INVALID_TOPOLOGY_ID) ?
2343 LIBXL_CPUARRAY_INVALID_ENTRY : socketmap[i];
2344 if (i < info->nodemap.entries)
2345 info->nodemap.array[i] = (nodemap[i] == INVALID_TOPOLOGY_ID) ?
2346 LIBXL_CPUARRAY_INVALID_ENTRY : nodemap[i];
2349 xc_hypercall_buffer_free(ctx->xch, coremap);
2350 xc_hypercall_buffer_free(ctx->xch, socketmap);
2351 xc_hypercall_buffer_free(ctx->xch, nodemap);
2352 return 0;
2354 fail:
2355 xc_hypercall_buffer_free(ctx->xch, coremap);
2356 xc_hypercall_buffer_free(ctx->xch, socketmap);
2357 xc_hypercall_buffer_free(ctx->xch, nodemap);
2358 libxl_topologyinfo_destroy(info);
2359 return ERROR_FAIL;
2362 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
2364 union {
2365 xen_extraversion_t xen_extra;
2366 xen_compile_info_t xen_cc;
2367 xen_changeset_info_t xen_chgset;
2368 xen_capabilities_info_t xen_caps;
2369 xen_platform_parameters_t p_parms;
2370 xen_commandline_t xen_commandline;
2371 } u;
2372 long xen_version;
2373 libxl_version_info *info = &ctx->version_info;
2375 if (info->xen_version_extra != NULL)
2376 return info;
2378 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
2379 info->xen_version_major = xen_version >> 16;
2380 info->xen_version_minor = xen_version & 0xFF;
2382 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
2383 info->xen_version_extra = strdup(u.xen_extra);
2385 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
2386 info->compiler = strdup(u.xen_cc.compiler);
2387 info->compile_by = strdup(u.xen_cc.compile_by);
2388 info->compile_domain = strdup(u.xen_cc.compile_domain);
2389 info->compile_date = strdup(u.xen_cc.compile_date);
2391 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
2392 info->capabilities = strdup(u.xen_caps);
2394 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
2395 info->changeset = strdup(u.xen_chgset);
2397 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
2398 info->virt_start = u.p_parms.virt_start;
2400 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
2402 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
2403 info->commandline = strdup(u.xen_commandline);
2405 return info;
2408 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
2409 int *nb_vcpu, int *nrcpus)
2411 libxl_vcpuinfo *ptr, *ret;
2412 xc_domaininfo_t domaininfo;
2413 xc_vcpuinfo_t vcpuinfo;
2415 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2416 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist");
2417 return NULL;
2419 *nrcpus = libxl_get_max_cpus(ctx);
2420 ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
2421 if (!ptr) {
2422 return NULL;
2425 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
2426 if (libxl_cpumap_alloc(ctx, &ptr->cpumap)) {
2427 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap");
2428 return NULL;
2430 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
2431 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info");
2432 return NULL;
2434 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) {
2435 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity");
2436 return NULL;
2438 ptr->vcpuid = *nb_vcpu;
2439 ptr->cpu = vcpuinfo.cpu;
2440 ptr->online = !!vcpuinfo.online;
2441 ptr->blocked = !!vcpuinfo.blocked;
2442 ptr->running = !!vcpuinfo.running;
2443 ptr->vcpu_time = vcpuinfo.cpu_time;
2445 return ret;
2448 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
2449 libxl_cpumap *cpumap)
2451 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap->map)) {
2452 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting vcpu affinity");
2453 return ERROR_FAIL;
2455 return 0;
2458 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask)
2460 libxl__gc gc = LIBXL_INIT_GC(ctx);
2461 libxl_dominfo info;
2462 char *dompath;
2463 xs_transaction_t t;
2464 int i, rc = ERROR_FAIL;
2466 if (libxl_domain_info(ctx, &info, domid) < 0) {
2467 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2468 goto out;
2470 if (!(dompath = libxl__xs_get_dompath(&gc, domid)))
2471 goto out;
2473 retry_transaction:
2474 t = xs_transaction_start(ctx->xsh);
2475 for (i = 0; i <= info.vcpu_max_id; i++)
2476 libxl__xs_write(&gc, t,
2477 libxl__sprintf(&gc, "%s/cpu/%u/availability", dompath, i),
2478 "%s", ((1 << i) & bitmask) ? "online" : "offline");
2479 if (!xs_transaction_end(ctx->xsh, t, 0)) {
2480 if (errno == EAGAIN)
2481 goto retry_transaction;
2482 } else
2483 rc = 0;
2484 out:
2485 libxl__free_all(&gc);
2486 return rc;
2489 /*
2490 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
2491 */
2492 int libxl_get_sched_id(libxl_ctx *ctx)
2494 int sched, ret;
2496 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
2497 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2498 return ERROR_FAIL;
2500 return sched;
2503 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2505 struct xen_domctl_sched_credit sdom;
2506 int rc;
2508 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
2509 if (rc != 0) {
2510 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2511 return ERROR_FAIL;
2514 scinfo->weight = sdom.weight;
2515 scinfo->cap = sdom.cap;
2517 return 0;
2520 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2522 struct xen_domctl_sched_credit sdom;
2523 xc_domaininfo_t domaininfo;
2524 int rc;
2526 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
2527 if (rc < 0) {
2528 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2529 return ERROR_FAIL;
2531 if (rc != 1 || domaininfo.domain != domid)
2532 return ERROR_INVAL;
2535 if (scinfo->weight < 1 || scinfo->weight > 65535) {
2536 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2537 "Cpu weight out of range, valid values are within range from 1 to 65535");
2538 return ERROR_INVAL;
2541 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
2542 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2543 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
2544 ((domaininfo.max_vcpu_id + 1) * 100));
2545 return ERROR_INVAL;
2548 sdom.weight = scinfo->weight;
2549 sdom.cap = scinfo->cap;
2551 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
2552 if ( rc < 0 ) {
2553 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2554 return ERROR_FAIL;
2557 return 0;
2560 static int trigger_type_from_string(char *trigger_name)
2562 if (!strcmp(trigger_name, "nmi"))
2563 return XEN_DOMCTL_SENDTRIGGER_NMI;
2564 else if (!strcmp(trigger_name, "reset"))
2565 return XEN_DOMCTL_SENDTRIGGER_RESET;
2566 else if (!strcmp(trigger_name, "init"))
2567 return XEN_DOMCTL_SENDTRIGGER_INIT;
2568 else if (!strcmp(trigger_name, "power"))
2569 return XEN_DOMCTL_SENDTRIGGER_POWER;
2570 else if (!strcmp(trigger_name, "sleep"))
2571 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
2572 else
2573 return -1;
2576 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
2578 int rc = -1;
2579 int trigger_type = trigger_type_from_string(trigger_name);
2581 if (trigger_type == -1) {
2582 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2583 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
2584 return ERROR_INVAL;
2587 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
2588 if (rc != 0) {
2589 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2590 "Send trigger '%s' failed", trigger_name);
2591 return ERROR_FAIL;
2594 return 0;
2597 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
2599 libxl__gc gc = LIBXL_INIT_GC(ctx);
2600 char *dompath = libxl__xs_get_dompath(&gc, domid);
2602 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/control/sysrq", dompath), "%c", sysrq);
2604 libxl__free_all(&gc);
2605 return 0;
2608 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
2610 int ret;
2611 ret = xc_send_debug_keys(ctx->xch, keys);
2612 if ( ret < 0 ) {
2613 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "sending debug keys");
2614 return ERROR_FAIL;
2616 return 0;
2619 libxl_xen_console_reader *
2620 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
2622 libxl_xen_console_reader *cr;
2623 unsigned int size = 16384;
2624 char *buf = malloc(size);
2626 if (!buf) {
2627 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
2628 " size is %u", size);
2629 return NULL;
2632 cr = malloc(sizeof(libxl_xen_console_reader));
2633 if (!cr) {
2634 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc libxl_xen_console_reader");
2635 return NULL;
2638 memset(cr, 0, sizeof(libxl_xen_console_reader));
2639 cr->buffer = buf;
2640 cr->size = size;
2641 cr->count = size;
2642 cr->clear = clear;
2643 cr->incremental = 1;
2645 return cr;
2648 /* return values: *line_r
2649 * 1 success, whole line obtained from buffer non-0
2650 * 0 no more lines available right now 0
2651 * negative error code ERROR_* 0
2652 * On success *line_r is updated to point to a nul-terminated
2653 * string which is valid until the next call on the same console
2654 * reader. The libxl caller may overwrite parts of the string
2655 * if it wishes. */
2656 int libxl_xen_console_read_line(libxl_ctx *ctx,
2657 libxl_xen_console_reader *cr,
2658 char **line_r)
2660 int ret;
2662 memset(cr->buffer, 0, cr->size);
2663 ret = xc_readconsolering(ctx->xch, cr->buffer, &cr->count,
2664 cr->clear, cr->incremental, &cr->index);
2665 if (ret < 0) {
2666 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "reading console ring buffer");
2667 return ERROR_FAIL;
2669 if (!ret) {
2670 if (cr->count) {
2671 *line_r = cr->buffer;
2672 ret = 1;
2673 } else {
2674 *line_r = NULL;
2675 ret = 0;
2679 return ret;
2682 void libxl_xen_console_read_finish(libxl_ctx *ctx,
2683 libxl_xen_console_reader *cr)
2685 free(cr->buffer);
2686 free(cr);
2689 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
2691 libxl__gc gc = LIBXL_INIT_GC(ctx);
2692 char *dompath = libxl__xs_get_dompath(&gc, domid);
2693 char *vm_path, *start_time;
2694 uint32_t ret;
2696 vm_path = libxl__xs_read(
2697 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dompath));
2698 start_time = libxl__xs_read(
2699 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/start_time", vm_path));
2700 if (start_time == NULL) {
2701 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2702 "Can't get start time of domain '%d'", domid);
2703 ret = -1;
2704 }else{
2705 ret = strtoul(start_time, NULL, 10);
2707 libxl__free_all(&gc);
2708 return ret;
2711 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
2713 int rc;
2714 char _buf[32768];
2716 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
2717 0, _buf);
2718 if (rc < 0) {
2719 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2720 "Can not get tmem list");
2721 return NULL;
2724 return strdup(_buf);
2727 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
2729 int rc;
2731 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
2732 0, NULL);
2733 if (rc < 0) {
2734 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2735 "Can not freeze tmem pools");
2736 return ERROR_FAIL;
2739 return rc;
2742 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
2744 int rc;
2746 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
2747 0, NULL);
2748 if (rc < 0) {
2749 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2750 "Can not destroy tmem pools");
2751 return ERROR_FAIL;
2754 return rc;
2757 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
2759 int rc;
2761 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
2762 0, NULL);
2763 if (rc < 0) {
2764 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2765 "Can not thaw tmem pools");
2766 return ERROR_FAIL;
2769 return rc;
2772 static int32_t tmem_setop_from_string(char *set_name)
2774 if (!strcmp(set_name, "weight"))
2775 return TMEMC_SET_WEIGHT;
2776 else if (!strcmp(set_name, "cap"))
2777 return TMEMC_SET_CAP;
2778 else if (!strcmp(set_name, "compress"))
2779 return TMEMC_SET_COMPRESS;
2780 else
2781 return -1;
2784 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
2786 int rc;
2787 int32_t subop = tmem_setop_from_string(name);
2789 if (subop == -1) {
2790 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2791 "Invalid set, valid sets are <weight|cap|compress>");
2792 return ERROR_INVAL;
2794 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
2795 if (rc < 0) {
2796 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2797 "Can not set tmem %s", name);
2798 return ERROR_FAIL;
2801 return rc;
2804 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
2805 char* uuid, int auth)
2807 int rc;
2809 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
2810 if (rc < 0) {
2811 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2812 "Can not set tmem shared auth");
2813 return ERROR_FAIL;
2816 return rc;
2819 int libxl_tmem_freeable(libxl_ctx *ctx)
2821 int rc;
2823 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
2824 if (rc < 0) {
2825 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2826 "Can not get tmem freeable memory");
2827 return ERROR_FAIL;
2830 return rc;
2833 void libxl_file_reference_destroy(libxl_file_reference *f)
2835 libxl__file_reference_unmap(f);
2836 free(f->path);
2839 int libxl_get_freecpus(libxl_ctx *ctx, libxl_cpumap *cpumap)
2841 int ncpus;
2843 ncpus = libxl_get_max_cpus(ctx);
2844 if (ncpus == 0)
2845 return ERROR_FAIL;
2847 cpumap->map = xc_cpupool_freeinfo(ctx->xch);
2848 if (cpumap->map == NULL)
2849 return ERROR_FAIL;
2851 cpumap->size = (ncpus + 7) / 8;
2853 return 0;
2856 int libxl_create_cpupool(libxl_ctx *ctx, const char *name, int schedid,
2857 libxl_cpumap cpumap, libxl_uuid *uuid,
2858 uint32_t *poolid)
2860 libxl__gc gc = LIBXL_INIT_GC(ctx);
2861 int rc;
2862 int i;
2863 xs_transaction_t t;
2864 char *uuid_string;
2866 uuid_string = libxl__uuid2string(&gc, *uuid);
2867 if (!uuid_string) {
2868 libxl__free_all(&gc);
2869 return ERROR_NOMEM;
2872 rc = xc_cpupool_create(ctx->xch, poolid, schedid);
2873 if (rc) {
2874 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2875 "Could not create cpupool");
2876 libxl__free_all(&gc);
2877 return ERROR_FAIL;
2880 libxl_for_each_cpu(i, cpumap)
2881 if (libxl_cpumap_test(&cpumap, i)) {
2882 rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
2883 if (rc) {
2884 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2885 "Error moving cpu to cpupool");
2886 libxl_destroy_cpupool(ctx, *poolid);
2887 libxl__free_all(&gc);
2888 return ERROR_FAIL;
2892 for (;;) {
2893 t = xs_transaction_start(ctx->xsh);
2895 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/pool/%d", *poolid));
2896 libxl__xs_write(&gc, t,
2897 libxl__sprintf(&gc, "/local/pool/%d/uuid", *poolid),
2898 "%s", uuid_string);
2899 libxl__xs_write(&gc, t,
2900 libxl__sprintf(&gc, "/local/pool/%d/name", *poolid),
2901 "%s", name);
2903 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN)) {
2904 libxl__free_all(&gc);
2905 return 0;
2910 int libxl_destroy_cpupool(libxl_ctx *ctx, uint32_t poolid)
2912 libxl__gc gc = LIBXL_INIT_GC(ctx);
2913 int rc, i;
2914 xc_cpupoolinfo_t *info;
2915 xs_transaction_t t;
2916 libxl_cpumap cpumap;
2918 info = xc_cpupool_getinfo(ctx->xch, poolid);
2919 if (info == NULL) {
2920 libxl__free_all(&gc);
2921 return ERROR_NOMEM;
2924 rc = ERROR_INVAL;
2925 if ((info->cpupool_id != poolid) || (info->n_dom))
2926 goto out;
2928 rc = ERROR_NOMEM;
2929 if (libxl_cpumap_alloc(ctx, &cpumap))
2930 goto out;
2932 memcpy(cpumap.map, info->cpumap, cpumap.size);
2933 libxl_for_each_cpu(i, cpumap)
2934 if (libxl_cpumap_test(&cpumap, i)) {
2935 rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
2936 if (rc) {
2937 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2938 "Error removing cpu from cpupool");
2939 rc = ERROR_FAIL;
2940 goto out1;
2944 rc = xc_cpupool_destroy(ctx->xch, poolid);
2945 if (rc) {
2946 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "Could not destroy cpupool");
2947 rc = ERROR_FAIL;
2948 goto out1;
2951 for (;;) {
2952 t = xs_transaction_start(ctx->xsh);
2954 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/pool/%d", poolid));
2956 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
2957 break;
2960 rc = 0;
2962 out1:
2963 libxl_cpumap_destroy(&cpumap);
2964 out:
2965 xc_cpupool_infofree(ctx->xch, info);
2966 libxl__free_all(&gc);
2968 return rc;
2971 int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid)
2973 libxl__gc gc = LIBXL_INIT_GC(ctx);
2974 xs_transaction_t t;
2975 xc_cpupoolinfo_t *info;
2976 int rc;
2978 info = xc_cpupool_getinfo(ctx->xch, poolid);
2979 if (info == NULL) {
2980 libxl__free_all(&gc);
2981 return ERROR_NOMEM;
2984 rc = ERROR_INVAL;
2985 if (info->cpupool_id != poolid)
2986 goto out;
2988 rc = 0;
2990 for (;;) {
2991 t = xs_transaction_start(ctx->xsh);
2993 libxl__xs_write(&gc, t,
2994 libxl__sprintf(&gc, "/local/pool/%d/name", poolid),
2995 "%s", name);
2997 if (xs_transaction_end(ctx->xsh, t, 0))
2998 break;
3000 if (errno == EAGAIN)
3001 continue;
3003 rc = ERROR_FAIL;
3004 break;
3007 out:
3008 xc_cpupool_infofree(ctx->xch, info);
3009 libxl__free_all(&gc);
3011 return rc;
3014 int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
3016 int rc;
3018 rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
3019 if (rc) {
3020 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3021 "Error moving cpu to cpupool");
3022 return ERROR_FAIL;
3024 return 0;
3027 int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3029 int rc = 0;
3030 int cpu;
3031 libxl_cpumap freemap;
3032 libxl_topologyinfo topology;
3034 if (libxl_get_freecpus(ctx, &freemap)) {
3035 return ERROR_FAIL;
3038 if (libxl_get_topologyinfo(ctx, &topology)) {
3039 rc = ERROR_FAIL;
3040 goto out;
3043 *cpus = 0;
3044 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3045 if (libxl_cpumap_test(&freemap, cpu) &&
3046 (topology.nodemap.array[cpu] == node) &&
3047 !libxl_cpupool_cpuadd(ctx, poolid, cpu)) {
3048 (*cpus)++;
3052 libxl_topologyinfo_destroy(&topology);
3054 out:
3055 libxl_cpumap_destroy(&freemap);
3056 return rc;
3059 int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
3061 int rc;
3063 rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
3064 if (rc) {
3065 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3066 "Error removing cpu from cpupool");
3067 return ERROR_FAIL;
3069 return 0;
3072 int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3074 int ret = 0;
3075 int n_pools;
3076 int p;
3077 int cpu;
3078 libxl_topologyinfo topology;
3079 libxl_cpupoolinfo *poolinfo;
3081 poolinfo = libxl_list_cpupool(ctx, &n_pools);
3082 if (!poolinfo) {
3083 return ERROR_NOMEM;
3086 if (libxl_get_topologyinfo(ctx, &topology)) {
3087 ret = ERROR_FAIL;
3088 goto out;
3091 *cpus = 0;
3092 for (p = 0; p < n_pools; p++) {
3093 if (poolinfo[p].poolid == poolid) {
3094 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3095 if ((topology.nodemap.array[cpu] == node) &&
3096 libxl_cpumap_test(&poolinfo[p].cpumap, cpu) &&
3097 !libxl_cpupool_cpuremove(ctx, poolid, cpu)) {
3098 (*cpus)++;
3104 libxl_topologyinfo_destroy(&topology);
3106 out:
3107 for (p = 0; p < n_pools; p++) {
3108 libxl_cpupoolinfo_destroy(poolinfo + p);
3111 return ret;
3114 int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
3116 libxl__gc gc = LIBXL_INIT_GC(ctx);
3117 int rc;
3118 char *dom_path;
3119 char *vm_path;
3120 char *poolname;
3121 xs_transaction_t t;
3123 dom_path = libxl__xs_get_dompath(&gc, domid);
3124 if (!dom_path) {
3125 libxl__free_all(&gc);
3126 return ERROR_FAIL;
3129 rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
3130 if (rc) {
3131 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3132 "Error moving domain to cpupool");
3133 libxl__free_all(&gc);
3134 return ERROR_FAIL;
3137 for (;;) {
3138 t = xs_transaction_start(ctx->xsh);
3140 poolname = libxl__cpupoolid_to_name(&gc, poolid);
3141 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
3142 if (!vm_path)
3143 break;
3145 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/pool_name", vm_path),
3146 "%s", poolname);
3148 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3149 break;
3152 libxl__free_all(&gc);
3153 return 0;