debuggers.hg

view tools/libxl/libxl.c @ 22853:787af706dadc

libxl: Check a domain exists before destroying it

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Tue Jan 25 17:02:47 2011 +0000 (2011-01-25)
parents eefb8e971be5
children 6ec61438713a
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 = NULL;
650 disk->phystype = 0;
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_FILE:
886 /* let's pretend is tap:aio for the moment */
887 disk->phystype = PHYSTYPE_AIO;
888 case PHYSTYPE_AIO:
889 case PHYSTYPE_QCOW:
890 case PHYSTYPE_QCOW2:
891 case PHYSTYPE_VHD:
892 if (libxl__blktap_enabled(&gc)) {
893 const char *dev = libxl__blktap_devpath(&gc,
894 disk->physpath, disk->phystype);
895 if (!dev) {
896 rc = ERROR_FAIL;
897 goto out_free;
898 }
899 flexarray_append(back, "tapdisk-params");
900 flexarray_append(back, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
901 flexarray_append(back, "params");
902 flexarray_append(back, libxl__strdup(&gc, dev));
903 backend_type = "phy";
904 libxl__device_physdisk_major_minor(dev, &major, &minor);
905 flexarray_append(back, "physical-device");
906 flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
907 device.backend_kind = DEVICE_VBD;
909 break;
910 }
911 flexarray_append(back, "params");
912 flexarray_append(back, libxl__sprintf(&gc, "%s:%s",
913 libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
915 if (libxl__blktap_enabled(&gc))
916 device.backend_kind = DEVICE_TAP;
917 else
918 device.backend_kind = DEVICE_QDISK;
919 break;
921 default:
922 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
923 rc = ERROR_INVAL;
924 goto out_free;
925 }
927 flexarray_append(back, "frontend-id");
928 flexarray_append(back, libxl__sprintf(&gc, "%d", disk->domid));
929 flexarray_append(back, "online");
930 flexarray_append(back, "1");
931 flexarray_append(back, "removable");
932 flexarray_append(back, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
933 flexarray_append(back, "bootable");
934 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
935 flexarray_append(back, "state");
936 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
937 flexarray_append(back, "dev");
938 flexarray_append(back, disk->virtpath);
939 flexarray_append(back, "type");
940 flexarray_append(back, backend_type);
941 flexarray_append(back, "mode");
942 flexarray_append(back, disk->readwrite ? "w" : "r");
944 flexarray_append(front, "backend-id");
945 flexarray_append(front, libxl__sprintf(&gc, "%d", disk->backend_domid));
946 flexarray_append(front, "state");
947 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
948 flexarray_append(front, "virtual-device");
949 flexarray_append(front, libxl__sprintf(&gc, "%d", devid));
950 flexarray_append(front, "device-type");
951 flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
953 if (0 /* protocol != native*/) {
954 flexarray_append(front, "protocol");
955 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
956 }
958 libxl__device_generic_add(ctx, &device,
959 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
960 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
962 rc = 0;
964 out_free:
965 flexarray_free(back);
966 flexarray_free(front);
967 out:
968 libxl__free_all(&gc);
969 return rc;
970 }
972 int libxl_device_disk_del(libxl_ctx *ctx,
973 libxl_device_disk *disk, int wait)
974 {
975 libxl__device device;
976 int devid;
978 devid = libxl__device_disk_dev_number(disk->virtpath);
979 device.backend_domid = disk->backend_domid;
980 device.backend_devid = devid;
981 device.backend_kind =
982 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
983 device.domid = disk->domid;
984 device.devid = devid;
985 device.kind = DEVICE_VBD;
986 return libxl__device_del(ctx, &device, wait);
987 }
989 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
990 {
991 libxl__gc gc = LIBXL_INIT_GC(ctx);
992 const char *dev = NULL;
993 char *ret;
994 int phystype = disk->phystype;
995 switch (phystype) {
996 case PHYSTYPE_PHY: {
997 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
998 dev = disk->physpath;
999 break;
1001 case PHYSTYPE_FILE:
1002 /* let's pretend is tap:aio for the moment */
1003 phystype = PHYSTYPE_AIO;
1004 case PHYSTYPE_AIO:
1005 case PHYSTYPE_QCOW:
1006 case PHYSTYPE_QCOW2:
1007 case PHYSTYPE_VHD:
1008 if (libxl__blktap_enabled(&gc))
1009 dev = libxl__blktap_devpath(&gc, disk->physpath, phystype);
1010 break;
1012 default:
1013 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1014 break;
1016 ret = strdup(dev);
1017 libxl__free_all(&gc);
1018 return ret;
1021 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1023 /* Nothing to do for PHYSTYPE_PHY. */
1025 /*
1026 * For other device types assume that the blktap2 process is
1027 * needed by the soon to be started domain and do nothing.
1028 */
1030 return 0;
1033 /******************************************************************************/
1034 int libxl_device_nic_init(libxl_device_nic *nic_info, int devnum)
1036 const uint8_t *r;
1037 libxl_uuid uuid;
1039 libxl_uuid_generate(&uuid);
1040 r = libxl_uuid_bytearray(&uuid);
1041 memset(nic_info, '\0', sizeof(*nic_info));
1043 nic_info->backend_domid = 0;
1044 nic_info->domid = 0;
1045 nic_info->devid = devnum;
1046 nic_info->mtu = 1492;
1047 nic_info->model = strdup("e1000");
1048 nic_info->mac[0] = 0x00;
1049 nic_info->mac[1] = 0x16;
1050 nic_info->mac[2] = 0x3e;
1051 nic_info->mac[3] = r[0] & 0x7f;
1052 nic_info->mac[4] = r[1];
1053 nic_info->mac[5] = r[2];
1054 nic_info->ifname = NULL;
1055 nic_info->bridge = strdup("xenbr0");
1056 if ( asprintf(&nic_info->script, "%s/vif-bridge",
1057 libxl_xen_script_dir_path()) < 0 )
1058 return ERROR_FAIL;
1059 nic_info->nictype = NICTYPE_IOEMU;
1060 return 0;
1063 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1065 libxl__gc gc = LIBXL_INIT_GC(ctx);
1066 flexarray_t *front;
1067 flexarray_t *back;
1068 libxl__device device;
1069 char *dompath, **l;
1070 unsigned int nb, rc;
1072 front = flexarray_make(16, 1);
1073 if (!front) {
1074 rc = ERROR_NOMEM;
1075 goto out;
1077 back = flexarray_make(16, 1);
1078 if (!back) {
1079 rc = ERROR_NOMEM;
1080 goto out_free;
1083 if (nic->devid == -1) {
1084 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1085 rc = ERROR_FAIL;
1086 goto out_free;
1088 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1089 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb))) {
1090 nic->devid = 0;
1091 } else {
1092 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1096 device.backend_devid = nic->devid;
1097 device.backend_domid = nic->backend_domid;
1098 device.backend_kind = DEVICE_VIF;
1099 device.devid = nic->devid;
1100 device.domid = nic->domid;
1101 device.kind = DEVICE_VIF;
1103 flexarray_append(back, "frontend-id");
1104 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->domid));
1105 flexarray_append(back, "online");
1106 flexarray_append(back, "1");
1107 flexarray_append(back, "state");
1108 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1109 flexarray_append(back, "script");
1110 flexarray_append(back, nic->script);
1111 flexarray_append(back, "mac");
1112 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1113 nic->mac[0], nic->mac[1], nic->mac[2],
1114 nic->mac[3], nic->mac[4], nic->mac[5]));
1115 flexarray_append(back, "bridge");
1116 flexarray_append(back, libxl__strdup(&gc, nic->bridge));
1117 flexarray_append(back, "handle");
1118 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->devid));
1120 flexarray_append(front, "backend-id");
1121 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->backend_domid));
1122 flexarray_append(front, "state");
1123 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1124 flexarray_append(front, "handle");
1125 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->devid));
1126 flexarray_append(front, "mac");
1127 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1128 nic->mac[0], nic->mac[1], nic->mac[2],
1129 nic->mac[3], nic->mac[4], nic->mac[5]));
1130 if (0 /* protocol != native*/) {
1131 flexarray_append(front, "protocol");
1132 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
1135 libxl__device_generic_add(ctx, &device,
1136 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1137 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1139 /* FIXME: wait for plug */
1140 rc = 0;
1141 out_free:
1142 flexarray_free(back);
1143 flexarray_free(front);
1144 out:
1145 libxl__free_all(&gc);
1146 return rc;
1149 int libxl_device_nic_del(libxl_ctx *ctx,
1150 libxl_device_nic *nic, int wait)
1152 libxl__device device;
1154 device.backend_devid = nic->devid;
1155 device.backend_domid = nic->backend_domid;
1156 device.backend_kind = DEVICE_VIF;
1157 device.devid = nic->devid;
1158 device.domid = nic->domid;
1159 device.kind = DEVICE_VIF;
1161 return libxl__device_del(ctx, &device, wait);
1164 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1166 libxl__gc gc = LIBXL_INIT_GC(ctx);
1167 char *dompath, *nic_path_fe;
1168 char **l, **list;
1169 char *val, *tok;
1170 unsigned int nb_nics, i;
1171 libxl_nicinfo *res, *nics;
1173 dompath = libxl__xs_get_dompath(&gc, domid);
1174 if (!dompath)
1175 goto err;
1176 list = l = libxl__xs_directory(&gc, XBT_NULL,
1177 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb_nics);
1178 if (!l)
1179 goto err;
1180 nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
1181 if (!res)
1182 goto err;
1183 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
1184 nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
1186 nics->backend = xs_read(ctx->xsh, XBT_NULL,
1187 libxl__sprintf(&gc, "%s/backend", nic_path_fe), NULL);
1188 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", nic_path_fe));
1189 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
1191 nics->devid = strtoul(*l, NULL, 10);
1192 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", nic_path_fe));
1193 nics->state = val ? strtoul(val, NULL, 10) : -1;
1194 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", nic_path_fe));
1195 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1196 ++i, tok = strtok(NULL, ":")) {
1197 nics->mac[i] = strtoul(tok, NULL, 16);
1199 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", nic_path_fe));
1200 nics->evtch = val ? strtol(val, NULL, 10) : -1;
1201 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", nic_path_fe));
1202 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
1203 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", nic_path_fe));
1204 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
1205 nics->frontend = xs_read(ctx->xsh, XBT_NULL,
1206 libxl__sprintf(&gc, "%s/frontend", nics->backend), NULL);
1207 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", nics->backend));
1208 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1209 nics->script = xs_read(ctx->xsh, XBT_NULL,
1210 libxl__sprintf(&gc, "%s/script", nics->backend), NULL);
1213 libxl__free_all(&gc);
1214 return res;
1215 err:
1216 libxl__free_all(&gc);
1217 return NULL;
1220 /******************************************************************************/
1221 void libxl_device_net2_init(libxl_device_net2 *net2_info, int devnum)
1223 const uint8_t *r;
1224 libxl_uuid uuid;
1226 libxl_uuid_generate(&uuid);
1227 r = libxl_uuid_bytearray(&uuid);
1228 memset(net2_info, '\0', sizeof(*net2_info));
1230 net2_info->devid = devnum;
1231 net2_info->front_mac[0] = 0x00;
1232 net2_info->front_mac[1] = 0x16;
1233 net2_info->front_mac[2] = 0x3e;;
1234 net2_info->front_mac[3] = 0x7f & r[0];
1235 net2_info->front_mac[4] = r[1];
1236 net2_info->front_mac[5] = r[2];
1237 net2_info->back_mac[0] = 0x00;
1238 net2_info->back_mac[1] = 0x16;
1239 net2_info->back_mac[2] = 0x3e;
1240 net2_info->back_mac[3] = 0x7f & r[3];
1241 net2_info->back_mac[4] = r[4];
1242 net2_info->back_mac[5] = r[5];
1243 net2_info->back_trusted = 1;
1244 net2_info->filter_mac = 1;
1245 net2_info->max_bypasses = 5;
1246 net2_info->bridge = strdup("xenbr0");
1249 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
1251 libxl__gc gc = LIBXL_INIT_GC(ctx);
1252 flexarray_t *front, *back;
1253 libxl__device device;
1254 char *dompath, *dom, **l;
1255 unsigned int nb;
1256 int rc;
1258 front = flexarray_make(16, 1);
1259 if (!front) {
1260 rc = ERROR_NOMEM;
1261 goto err;
1263 back = flexarray_make(16, 1);
1264 if (!back) {
1265 rc = ERROR_NOMEM;
1266 goto err_free;
1269 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1270 rc = ERROR_FAIL;
1271 goto err_free;
1273 dom = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/name", dompath));
1275 if (net2->devid == -1) {
1276 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1277 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb))) {
1278 net2->devid = 0;
1279 } else {
1280 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1284 device.backend_devid = net2->devid;
1285 device.backend_domid = net2->backend_domid;
1286 device.backend_kind = DEVICE_VIF2;
1287 device.devid = net2->devid;
1288 device.domid = net2->domid;
1289 device.kind = DEVICE_VIF2;
1291 flexarray_append(back, "domain");
1292 flexarray_append(back, dom);
1293 flexarray_append(back, "frontend-id");
1294 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->domid));
1296 flexarray_append(back, "local-trusted");
1297 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->back_trusted));
1298 flexarray_append(back, "mac");
1299 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1300 net2->back_mac[0], net2->back_mac[1],
1301 net2->back_mac[2], net2->back_mac[3],
1302 net2->back_mac[4], net2->back_mac[5]));
1304 flexarray_append(back, "remote-trusted");
1305 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->trusted));
1306 flexarray_append(back, "remote-mac");
1307 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1308 net2->front_mac[0], net2->front_mac[1],
1309 net2->front_mac[2], net2->front_mac[3],
1310 net2->front_mac[4], net2->front_mac[5]));
1312 flexarray_append(back, "max-bypasses");
1313 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->max_bypasses));
1314 flexarray_append(back, "filter-mac");
1315 flexarray_append(back, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1316 flexarray_append(back, "handle");
1317 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->devid));
1318 flexarray_append(back, "online");
1319 flexarray_append(back, "1");
1320 flexarray_append(back, "state");
1321 flexarray_append(back, "1");
1323 flexarray_append(front, "backend-id");
1324 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->backend_domid));
1326 flexarray_append(front, "local-trusted");
1327 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->trusted));
1328 flexarray_append(front, "mac");
1329 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1330 net2->front_mac[0], net2->front_mac[1],
1331 net2->front_mac[2], net2->front_mac[3],
1332 net2->front_mac[4], net2->front_mac[5]));
1334 flexarray_append(front, "remote-trusted");
1335 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->back_trusted));
1336 flexarray_append(front, "remote-mac");
1337 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1338 net2->back_mac[0], net2->back_mac[1],
1339 net2->back_mac[2], net2->back_mac[3],
1340 net2->back_mac[4], net2->back_mac[5]));
1342 flexarray_append(front, "filter-mac");
1343 flexarray_append(front, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1344 flexarray_append(front, "state");
1345 flexarray_append(front, "1");
1347 libxl__device_generic_add(ctx, &device,
1348 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1349 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1351 /* FIXME: wait for plug */
1352 rc = 0;
1353 err_free:
1354 flexarray_free(back);
1355 flexarray_free(front);
1356 err:
1357 libxl__free_all(&gc);
1358 return rc;
1361 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1363 libxl__gc gc = LIBXL_INIT_GC(ctx);
1364 char *dompath, *net2_path_fe;
1365 char **l;
1366 char *val, *tok;
1367 unsigned int nb_net2s, i;
1368 libxl_net2info *res, *net2s;
1370 dompath = libxl__xs_get_dompath(&gc, domid);
1371 if (!dompath)
1372 goto err;
1373 l = libxl__xs_directory(&gc, XBT_NULL,
1374 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb_net2s);
1375 if (!l)
1376 goto err;
1377 res = calloc(nb_net2s, sizeof (libxl_net2info));
1378 if (!res)
1379 goto err;
1380 net2s = res;
1381 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
1382 net2_path_fe = libxl__sprintf(&gc, "%s/device/vif2/%s", dompath, *l);
1384 net2s->backend = libxl__xs_read(&gc, XBT_NULL,
1385 libxl__sprintf(&gc, "%s/backend", net2_path_fe));
1386 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", net2_path_fe));
1387 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
1389 net2s->devid = strtoul(*l, NULL, 10);
1390 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", net2_path_fe));
1391 net2s->state = val ? strtoul(val, NULL, 10) : -1;
1393 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", net2_path_fe));
1394 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1395 ++i, tok = strtok(NULL, ":")) {
1396 net2s->mac[i] = strtoul(tok, NULL, 16);
1398 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-trusted", net2_path_fe));
1399 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
1401 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-mac", net2_path_fe));
1402 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1403 ++i, tok = strtok(NULL, ":")) {
1404 net2s->back_mac[i] = strtoul(tok, NULL, 16);
1406 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/filter-mac", net2_path_fe));
1407 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
1409 net2s->frontend = libxl__xs_read(&gc, XBT_NULL,
1410 libxl__sprintf(&gc, "%s/frontend", net2s->backend));
1411 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", net2s->backend));
1412 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1415 libxl__free_all(&gc);
1416 return res;
1417 err:
1418 libxl__free_all(&gc);
1419 return NULL;
1422 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
1424 libxl__device device;
1426 device.backend_devid = net2->devid;
1427 device.backend_domid = net2->backend_domid;
1428 device.backend_kind = DEVICE_VIF2;
1429 device.devid = net2->devid;
1430 device.domid = net2->domid;
1431 device.kind = DEVICE_VIF2;
1433 return libxl__device_del(ctx, &device, wait);
1437 /******************************************************************************/
1438 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1440 libxl__gc gc = LIBXL_INIT_GC(ctx);
1441 flexarray_t *front;
1442 flexarray_t *back;
1443 libxl__device device;
1444 int rc;
1446 front = flexarray_make(16, 1);
1447 if (!front) {
1448 rc = ERROR_NOMEM;
1449 goto out;
1451 back = flexarray_make(16, 1);
1452 if (!back) {
1453 rc = ERROR_NOMEM;
1454 goto out_free;
1457 device.backend_devid = console->devid;
1458 device.backend_domid = console->backend_domid;
1459 device.backend_kind = DEVICE_CONSOLE;
1460 device.devid = console->devid;
1461 device.domid = console->domid;
1462 device.kind = DEVICE_CONSOLE;
1464 flexarray_append(back, "frontend-id");
1465 flexarray_append(back, libxl__sprintf(&gc, "%d", console->domid));
1466 flexarray_append(back, "online");
1467 flexarray_append(back, "1");
1468 flexarray_append(back, "state");
1469 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1470 flexarray_append(back, "domain");
1471 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1472 flexarray_append(back, "protocol");
1473 flexarray_append(back, LIBXL_XENCONSOLE_PROTOCOL);
1475 flexarray_append(front, "backend-id");
1476 flexarray_append(front, libxl__sprintf(&gc, "%d", console->backend_domid));
1477 flexarray_append(front, "limit");
1478 flexarray_append(front, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
1479 flexarray_append(front, "type");
1480 if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
1481 flexarray_append(front, "xenconsoled");
1482 else
1483 flexarray_append(front, "ioemu");
1484 flexarray_append(front, "output");
1485 flexarray_append(front, console->output);
1487 if (device.devid == 0) {
1488 if (console->build_state == NULL) {
1489 rc = ERROR_INVAL;
1490 goto out_free;
1492 flexarray_append(front, "port");
1493 flexarray_append(front, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
1494 flexarray_append(front, "ring-ref");
1495 flexarray_append(front, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
1496 } else {
1497 flexarray_append(front, "state");
1498 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1499 flexarray_append(front, "protocol");
1500 flexarray_append(front, LIBXL_XENCONSOLE_PROTOCOL);
1503 libxl__device_generic_add(ctx, &device,
1504 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1505 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1506 rc = 0;
1507 out_free:
1508 flexarray_free(back);
1509 flexarray_free(front);
1510 out:
1511 libxl__free_all(&gc);
1512 return rc;
1515 /******************************************************************************/
1516 void libxl_device_vkb_init(libxl_device_vkb *vkb, int dev_num)
1518 memset(vkb, 0x00, sizeof(libxl_device_vkb));
1519 vkb->devid = dev_num;
1522 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1524 libxl__gc gc = LIBXL_INIT_GC(ctx);
1525 flexarray_t *front;
1526 flexarray_t *back;
1527 libxl__device device;
1528 int rc;
1530 front = flexarray_make(16, 1);
1531 if (!front) {
1532 rc = ERROR_NOMEM;
1533 goto out;
1535 back = flexarray_make(16, 1);
1536 if (!back) {
1537 rc = ERROR_NOMEM;
1538 goto out_free;
1541 device.backend_devid = vkb->devid;
1542 device.backend_domid = vkb->backend_domid;
1543 device.backend_kind = DEVICE_VKBD;
1544 device.devid = vkb->devid;
1545 device.domid = vkb->domid;
1546 device.kind = DEVICE_VKBD;
1548 flexarray_append(back, "frontend-id");
1549 flexarray_append(back, libxl__sprintf(&gc, "%d", vkb->domid));
1550 flexarray_append(back, "online");
1551 flexarray_append(back, "1");
1552 flexarray_append(back, "state");
1553 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1554 flexarray_append(back, "domain");
1555 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1557 flexarray_append(front, "backend-id");
1558 flexarray_append(front, libxl__sprintf(&gc, "%d", vkb->backend_domid));
1559 flexarray_append(front, "state");
1560 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1562 libxl__device_generic_add(ctx, &device,
1563 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1564 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1565 rc = 0;
1566 out_free:
1567 flexarray_free(back);
1568 flexarray_free(front);
1569 out:
1570 libxl__free_all(&gc);
1571 return rc;
1574 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1576 return ERROR_NI;
1579 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1581 return ERROR_NI;
1584 static unsigned int libxl_append_disk_list_of_type(libxl_ctx *ctx,
1585 uint32_t domid,
1586 const char *type,
1587 libxl_device_disk **disks,
1588 unsigned int *ndisks)
1590 libxl__gc gc = LIBXL_INIT_GC(ctx);
1591 char *be_path = NULL;
1592 char **dir = NULL;
1593 unsigned int n = 0, len = 0;
1594 libxl_device_disk *pdisk = NULL, *pdisk_end = NULL;
1595 char *physpath_tmp = NULL;
1597 be_path = libxl__sprintf(&gc, "%s/backend/%s/%d",
1598 libxl__xs_get_dompath(&gc, 0), type, domid);
1599 dir = libxl__xs_directory(&gc, XBT_NULL, be_path, &n);
1600 if (dir) {
1601 *disks = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
1602 pdisk = *disks + *ndisks;
1603 *ndisks += n;
1604 pdisk_end = *disks + *ndisks;
1605 for (; pdisk < pdisk_end; pdisk++, dir++) {
1606 pdisk->backend_domid = 0;
1607 pdisk->domid = domid;
1608 physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path, *dir), &len);
1609 if (strchr(physpath_tmp, ':')) {
1610 pdisk->physpath = strdup(strchr(physpath_tmp, ':') + 1);
1611 free(physpath_tmp);
1612 } else {
1613 pdisk->physpath = physpath_tmp;
1615 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path, *dir)), &(pdisk->phystype));
1616 pdisk->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path, *dir), &len);
1617 pdisk->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path, *dir)));
1618 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path, *dir)), "w"))
1619 pdisk->readwrite = 1;
1620 else
1621 pdisk->readwrite = 0;
1622 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))));
1623 pdisk->is_cdrom = !strcmp(type, "cdrom");
1627 libxl__free_all(&gc);
1628 return n;
1631 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
1633 libxl_device_disk *disks = NULL;
1634 unsigned int ndisks = 0;
1636 *num = libxl_append_disk_list_of_type(ctx, domid, "vbd", &disks, &ndisks);
1637 *num += libxl_append_disk_list_of_type(ctx, domid, "tap", &disks, &ndisks);
1638 *num += libxl_append_disk_list_of_type(ctx, domid, "qdisk", &disks, &ndisks);
1640 return disks;
1643 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
1644 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
1646 libxl__gc gc = LIBXL_INIT_GC(ctx);
1647 char *dompath, *diskpath;
1648 char *val;
1650 dompath = libxl__xs_get_dompath(&gc, domid);
1651 diskinfo->devid = libxl__device_disk_dev_number(disk->virtpath);
1653 /* tap devices entries in xenstore are written as vbd devices. */
1654 diskpath = libxl__sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
1655 diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
1656 libxl__sprintf(&gc, "%s/backend", diskpath), NULL);
1657 if (!diskinfo->backend) {
1658 libxl__free_all(&gc);
1659 return ERROR_FAIL;
1661 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", diskpath));
1662 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
1663 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", diskpath));
1664 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
1665 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", diskpath));
1666 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
1667 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/ring-ref", diskpath));
1668 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
1669 diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
1670 libxl__sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
1671 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", diskinfo->backend));
1672 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1674 libxl__free_all(&gc);
1675 return 0;
1678 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1680 int num, i;
1681 uint32_t stubdomid;
1682 libxl_device_disk *disks;
1683 int ret = ERROR_FAIL;
1685 if (!disk->physpath) {
1686 disk->physpath = strdup("");
1687 disk->phystype = PHYSTYPE_PHY;
1689 disks = libxl_device_disk_list(ctx, domid, &num);
1690 for (i = 0; i < num; i++) {
1691 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1692 /* found */
1693 break;
1695 if (i == num) {
1696 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual device not found");
1697 goto out;
1700 ret = 0;
1702 libxl_device_disk_del(ctx, disks + i, 1);
1703 libxl_device_disk_add(ctx, domid, disk);
1704 stubdomid = libxl_get_stubdom_id(ctx, domid);
1705 if (stubdomid) {
1706 disks[i].domid = stubdomid;
1707 libxl_device_disk_del(ctx, disks + i, 1);
1708 disk->domid = stubdomid;
1709 libxl_device_disk_add(ctx, stubdomid, disk);
1710 disk->domid = domid;
1712 out:
1713 for (i = 0; i < num; i++)
1714 libxl_device_disk_destroy(&disks[i]);
1715 free(disks);
1716 return ret;
1719 /******************************************************************************/
1720 void libxl_device_vfb_init(libxl_device_vfb *vfb, int dev_num)
1722 memset(vfb, 0x00, sizeof(libxl_device_vfb));
1723 vfb->devid = dev_num;
1724 vfb->display = NULL;
1725 vfb->xauthority = NULL;
1726 vfb->vnc = 1;
1727 vfb->vncpasswd = NULL;
1728 vfb->vnclisten = strdup("127.0.0.1");
1729 vfb->vncdisplay = 0;
1730 vfb->vncunused = 1;
1731 vfb->keymap = NULL;
1732 vfb->sdl = 0;
1733 vfb->opengl = 0;
1736 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1738 libxl__gc gc = LIBXL_INIT_GC(ctx);
1739 flexarray_t *front;
1740 flexarray_t *back;
1741 libxl__device device;
1742 int rc;
1744 front = flexarray_make(16, 1);
1745 if (!front) {
1746 rc = ERROR_NOMEM;
1747 goto out;
1749 back = flexarray_make(16, 1);
1750 if (!back) {
1751 rc = ERROR_NOMEM;
1752 goto out_free;
1755 device.backend_devid = vfb->devid;
1756 device.backend_domid = vfb->backend_domid;
1757 device.backend_kind = DEVICE_VFB;
1758 device.devid = vfb->devid;
1759 device.domid = vfb->domid;
1760 device.kind = DEVICE_VFB;
1762 flexarray_vappend(back, "frontend-id", libxl__sprintf(&gc, "%d", vfb->domid), NULL);
1763 flexarray_vappend(back, "online", "1", NULL);
1764 flexarray_vappend(back, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1765 flexarray_vappend(back, "domain", libxl__domid_to_name(&gc, domid), NULL);
1766 flexarray_vappend(back, "vnc", libxl__sprintf(&gc, "%d", vfb->vnc), NULL);
1767 flexarray_vappend(back, "vnclisten", vfb->vnclisten, NULL);
1768 flexarray_append(back, "vncpasswd");
1769 flexarray_append(back, vfb->vncpasswd);
1770 flexarray_vappend(back, "vncdisplay", libxl__sprintf(&gc, "%d", vfb->vncdisplay), NULL);
1771 flexarray_vappend(back, "vncunused", libxl__sprintf(&gc, "%d", vfb->vncunused), NULL);
1772 flexarray_vappend(back, "sdl", libxl__sprintf(&gc, "%d", vfb->sdl), NULL);
1773 flexarray_vappend(back, "opengl", libxl__sprintf(&gc, "%d", vfb->opengl), NULL);
1774 if (vfb->xauthority) {
1775 flexarray_vappend(back, "xauthority", vfb->xauthority, NULL);
1777 if (vfb->display) {
1778 flexarray_vappend(back, "display", vfb->display, NULL);
1781 flexarray_vappend(front, "backend-id", libxl__sprintf(&gc, "%d", vfb->backend_domid), NULL);
1782 flexarray_vappend(front, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1784 libxl__device_generic_add(ctx, &device,
1785 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1786 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1787 rc = 0;
1788 out_free:
1789 flexarray_free(front);
1790 flexarray_free(back);
1791 out:
1792 libxl__free_all(&gc);
1793 return rc;
1796 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1798 return ERROR_NI;
1801 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1803 return ERROR_NI;
1806 /******************************************************************************/
1808 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
1810 libxl__gc gc = LIBXL_INIT_GC(ctx);
1811 char *mem, *endptr;
1812 uint32_t memorykb;
1813 char *dompath = libxl__xs_get_dompath(&gc, domid);
1814 int rc = 1;
1816 mem = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/memory/target", dompath));
1817 if (!mem) {
1818 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
1819 goto out;
1821 memorykb = strtoul(mem, &endptr, 10);
1822 if (*endptr != '\0') {
1823 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
1824 goto out;
1827 if (max_memkb < memorykb) {
1828 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
1829 goto out;
1832 rc = 0;
1833 out:
1834 libxl__free_all(&gc);
1835 return rc;
1838 static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb)
1840 int rc;
1841 libxl_dominfo info;
1842 libxl_physinfo physinfo;
1843 char *target = NULL, *staticmax = NULL, *freememslack = NULL, *endptr = NULL;
1844 char *target_path = "/local/domain/0/memory/target";
1845 char *max_path = "/local/domain/0/memory/static-max";
1846 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
1847 xs_transaction_t t;
1848 libxl_ctx *ctx = libxl__gc_owner(gc);
1849 uint32_t free_mem_slack_kb = 0;
1851 retry_transaction:
1852 t = xs_transaction_start(ctx->xsh);
1854 target = libxl__xs_read(gc, t, target_path);
1855 staticmax = libxl__xs_read(gc, t, max_path);
1856 freememslack = libxl__xs_read(gc, t, free_mem_slack_path);
1857 if (target && staticmax && freememslack) {
1858 rc = 0;
1859 goto out;
1862 if (target) {
1863 *target_memkb = strtoul(target, &endptr, 10);
1864 if (*endptr != '\0') {
1865 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1866 "invalid memory target %s from %s\n", target, target_path);
1867 rc = ERROR_FAIL;
1868 goto out;
1872 rc = libxl_domain_info(ctx, &info, 0);
1873 if (rc < 0)
1874 goto out;
1876 rc = libxl_get_physinfo(ctx, &physinfo);
1877 if (rc < 0)
1878 goto out;
1880 if (target == NULL) {
1881 libxl__xs_write(gc, t, target_path, "%"PRIu32,
1882 (uint32_t) info.current_memkb);
1883 *target_memkb = (uint32_t) info.current_memkb;
1885 if (staticmax == NULL)
1886 libxl__xs_write(gc, t, max_path, "%"PRIu32,
1887 (uint32_t) info.max_memkb);
1889 if (freememslack == NULL) {
1890 free_mem_slack_kb = (uint32_t) (PAGE_TO_MEMKB(physinfo.total_pages) -
1891 info.current_memkb);
1892 /* From empirical measurements the free_mem_slack shouldn't be more
1893 * than 15% of the total memory present on the system. */
1894 if (free_mem_slack_kb > PAGE_TO_MEMKB(physinfo.total_pages) * 0.15)
1895 free_mem_slack_kb = PAGE_TO_MEMKB(physinfo.total_pages) * 0.15;
1896 libxl__xs_write(gc, t, free_mem_slack_path, "%"PRIu32, free_mem_slack_kb);
1898 rc = 0;
1900 out:
1901 if (!xs_transaction_end(ctx->xsh, t, 0)) {
1902 if (errno == EAGAIN)
1903 goto retry_transaction;
1904 else
1905 rc = ERROR_FAIL;
1909 return rc;
1912 /* returns how much memory should be left free in the system */
1913 static int libxl__get_free_memory_slack(libxl__gc *gc, uint32_t *free_mem_slack)
1915 int rc;
1916 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
1917 char *free_mem_slack_s, *endptr;
1918 uint32_t target_memkb;
1920 retry:
1921 free_mem_slack_s = libxl__xs_read(gc, XBT_NULL, free_mem_slack_path);
1922 if (!free_mem_slack_s) {
1923 rc = libxl__fill_dom0_memory_info(gc, &target_memkb);
1924 if (rc < 0)
1925 return rc;
1926 goto retry;
1927 } else {
1928 *free_mem_slack = strtoul(free_mem_slack_s, &endptr, 10);
1929 if (*endptr != '\0') {
1930 LIBXL__LOG_ERRNO(gc->owner, LIBXL__LOG_ERROR,
1931 "invalid free_mem_slack %s from %s\n",
1932 free_mem_slack_s, free_mem_slack_path);
1933 return ERROR_FAIL;
1936 return 0;
1939 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
1940 int32_t target_memkb, int relative, int enforce)
1942 libxl__gc gc = LIBXL_INIT_GC(ctx);
1943 int rc = 1, abort = 0;
1944 uint32_t memorykb = 0, videoram = 0;
1945 uint32_t current_target_memkb = 0, new_target_memkb = 0;
1946 char *memmax, *endptr, *videoram_s = NULL, *target = NULL;
1947 char *dompath = libxl__xs_get_dompath(&gc, domid);
1948 xc_domaininfo_t info;
1949 libxl_dominfo ptr;
1950 char *uuid;
1951 xs_transaction_t t;
1953 retry_transaction:
1954 t = xs_transaction_start(ctx->xsh);
1956 target = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
1957 "%s/memory/target", dompath));
1958 if (!target && !domid) {
1959 xs_transaction_end(ctx->xsh, t, 1);
1960 rc = libxl__fill_dom0_memory_info(&gc, &current_target_memkb);
1961 if (rc < 0) {
1962 abort = 1;
1963 goto out;
1965 goto retry_transaction;
1966 } else if (!target) {
1967 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1968 "cannot get target memory info from %s/memory/target\n",
1969 dompath);
1970 abort = 1;
1971 goto out;
1972 } else {
1973 current_target_memkb = strtoul(target, &endptr, 10);
1974 if (*endptr != '\0') {
1975 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1976 "invalid memory target %s from %s/memory/target\n",
1977 target, dompath);
1978 abort = 1;
1979 goto out;
1982 memmax = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
1983 "%s/memory/static-max", dompath));
1984 if (!memmax) {
1985 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1986 "cannot get memory info from %s/memory/static-max\n",
1987 dompath);
1988 abort = 1;
1989 goto out;
1991 memorykb = strtoul(memmax, &endptr, 10);
1992 if (*endptr != '\0') {
1993 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
1994 "invalid max memory %s from %s/memory/static-max\n",
1995 memmax, dompath);
1996 abort = 1;
1997 goto out;
2000 if (relative)
2001 new_target_memkb = current_target_memkb + target_memkb;
2002 else
2003 new_target_memkb = target_memkb;
2004 if (new_target_memkb > memorykb) {
2005 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2006 "memory_dynamic_max must be less than or equal to"
2007 " memory_static_max\n");
2008 abort = 1;
2009 goto out;
2012 if (!domid && new_target_memkb < LIBXL_MIN_DOM0_MEM) {
2013 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2014 "new target %d for dom0 is below the minimum threshold\n",
2015 new_target_memkb);
2016 abort = 1;
2017 goto out;
2019 videoram_s = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2020 "%s/memory/videoram", dompath));
2021 videoram = videoram_s ? atoi(videoram_s) : 0;
2023 if (enforce) {
2024 memorykb = new_target_memkb;
2025 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
2026 LIBXL_MAXMEM_CONSTANT);
2027 if (rc != 0) {
2028 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2029 "xc_domain_setmaxmem domid=%d memkb=%d failed "
2030 "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
2031 abort = 1;
2032 goto out;
2036 new_target_memkb -= videoram;
2037 rc = xc_domain_set_pod_target(ctx->xch, domid,
2038 new_target_memkb / 4, NULL, NULL, NULL);
2039 if (rc != 0) {
2040 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2041 "xc_domain_set_pod_target domid=%d, memkb=%d "
2042 "failed rc=%d\n", domid, new_target_memkb / 4,
2043 rc);
2044 abort = 1;
2045 goto out;
2048 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/memory/target",
2049 dompath), "%"PRIu32, new_target_memkb);
2050 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2051 if (rc != 1 || info.domain != domid) {
2052 abort = 1;
2053 goto out;
2055 xcinfo2xlinfo(&info, &ptr);
2056 uuid = libxl__uuid2string(&gc, ptr.uuid);
2057 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "/vm/%s/memory", uuid),
2058 "%"PRIu32, new_target_memkb / 1024);
2060 out:
2061 if (!xs_transaction_end(ctx->xsh, t, abort) && !abort)
2062 if (errno == EAGAIN)
2063 goto retry_transaction;
2065 libxl__free_all(&gc);
2066 return rc;
2069 int libxl_get_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t *out_target)
2071 libxl__gc gc = LIBXL_INIT_GC(ctx);
2072 int rc = 1;
2073 char *target = NULL, *endptr = NULL;
2074 char *dompath = libxl__xs_get_dompath(&gc, domid);
2075 uint32_t target_memkb;
2077 target = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc,
2078 "%s/memory/target", dompath));
2079 if (!target && !domid) {
2080 rc = libxl__fill_dom0_memory_info(&gc, &target_memkb);
2081 if (rc < 0)
2082 goto out;
2083 } else if (!target) {
2084 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2085 "cannot get target memory info from %s/memory/target\n",
2086 dompath);
2087 goto out;
2088 } else {
2089 target_memkb = strtoul(target, &endptr, 10);
2090 if (*endptr != '\0') {
2091 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2092 "invalid memory target %s from %s/memory/target\n",
2093 target, dompath);
2094 goto out;
2097 *out_target = target_memkb;
2098 rc = 0;
2100 out:
2101 libxl__free_all(&gc);
2102 return rc;
2105 int libxl_domain_need_memory(libxl_ctx *ctx, libxl_domain_build_info *b_info,
2106 libxl_device_model_info *dm_info, uint32_t *need_memkb)
2108 *need_memkb = b_info->target_memkb;
2109 if (b_info->hvm) {
2110 *need_memkb += b_info->shadow_memkb + LIBXL_HVM_EXTRA_MEMORY;
2111 if (strstr(dm_info->device_model, "stubdom-dm"))
2112 *need_memkb += 32 * 1024;
2113 } else
2114 *need_memkb += b_info->shadow_memkb + LIBXL_PV_EXTRA_MEMORY;
2115 if (*need_memkb % (2 * 1024))
2116 *need_memkb += (2 * 1024) - (*need_memkb % (2 * 1024));
2117 return 0;
2120 int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
2122 int rc = 0;
2123 libxl_physinfo info;
2124 uint32_t freemem_slack;
2125 libxl__gc gc = LIBXL_INIT_GC(ctx);
2127 rc = libxl_get_physinfo(ctx, &info);
2128 if (rc < 0)
2129 goto out;
2130 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2131 if (rc < 0)
2132 goto out;
2134 if ((info.free_pages + info.scrub_pages) * 4 > freemem_slack)
2135 *memkb = (info.free_pages + info.scrub_pages) * 4 - freemem_slack;
2136 else
2137 *memkb = 0;
2139 out:
2140 libxl__free_all(&gc);
2141 return rc;
2144 int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
2145 memory_kb, int wait_secs)
2147 int rc = 0;
2148 libxl_physinfo info;
2149 uint32_t freemem_slack;
2150 libxl__gc gc = LIBXL_INIT_GC(ctx);
2152 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2153 if (rc < 0)
2154 goto out;
2155 while (wait_secs > 0) {
2156 rc = libxl_get_physinfo(ctx, &info);
2157 if (rc < 0)
2158 goto out;
2159 if (info.free_pages * 4 - freemem_slack >= memory_kb) {
2160 rc = 0;
2161 goto out;
2163 wait_secs--;
2164 sleep(1);
2166 rc = ERROR_NOMEM;
2168 out:
2169 libxl__free_all(&gc);
2170 return rc;
2173 int libxl_wait_for_memory_target(libxl_ctx *ctx, uint32_t domid, int wait_secs)
2175 int rc = 0;
2176 uint32_t target_memkb = 0;
2177 libxl_dominfo info;
2179 do {
2180 wait_secs--;
2181 sleep(1);
2183 rc = libxl_get_memory_target(ctx, domid, &target_memkb);
2184 if (rc < 0)
2185 goto out;
2187 rc = libxl_domain_info(ctx, &info, domid);
2188 if (rc < 0)
2189 return rc;
2190 } while (wait_secs > 0 && info.current_memkb > target_memkb);
2192 if (info.current_memkb <= target_memkb)
2193 rc = 0;
2194 else
2195 rc = ERROR_FAIL;
2197 out:
2198 return 0;
2201 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
2203 int rc = -1;
2205 switch (button) {
2206 case POWER_BUTTON:
2207 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2208 break;
2209 case SLEEP_BUTTON:
2210 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2211 break;
2212 default:
2213 break;
2216 return rc;
2219 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
2221 xc_physinfo_t xcphysinfo = { 0 };
2222 int rc;
2224 rc = xc_physinfo(ctx->xch, &xcphysinfo);
2225 if (rc != 0) {
2226 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting physinfo");
2227 return ERROR_FAIL;
2229 physinfo->threads_per_core = xcphysinfo.threads_per_core;
2230 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
2231 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
2232 physinfo->nr_cpus = xcphysinfo.nr_cpus;
2233 physinfo->cpu_khz = xcphysinfo.cpu_khz;
2234 physinfo->total_pages = xcphysinfo.total_pages;
2235 physinfo->free_pages = xcphysinfo.free_pages;
2236 physinfo->scrub_pages = xcphysinfo.scrub_pages;
2237 physinfo->nr_nodes = xcphysinfo.nr_nodes;
2238 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
2239 physinfo->phys_cap = xcphysinfo.capabilities;
2241 return 0;
2244 int libxl_get_topologyinfo(libxl_ctx *ctx, libxl_topologyinfo *info)
2246 xc_topologyinfo_t tinfo;
2247 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_core_t, coremap);
2248 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_socket_t, socketmap);
2249 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_node_t, nodemap);
2250 int i;
2251 int rc = 0;
2253 rc += libxl_cpuarray_alloc(ctx, &info->coremap);
2254 rc += libxl_cpuarray_alloc(ctx, &info->socketmap);
2255 rc += libxl_cpuarray_alloc(ctx, &info->nodemap);
2256 if (rc)
2257 goto fail;
2259 coremap = xc_hypercall_buffer_alloc(ctx->xch, coremap, sizeof(*coremap) * info->coremap.entries);
2260 socketmap = xc_hypercall_buffer_alloc(ctx->xch, socketmap, sizeof(*socketmap) * info->socketmap.entries);
2261 nodemap = xc_hypercall_buffer_alloc(ctx->xch, nodemap, sizeof(*nodemap) * info->nodemap.entries);
2262 if ((coremap == NULL) || (socketmap == NULL) || (nodemap == NULL))
2263 goto fail;
2265 set_xen_guest_handle(tinfo.cpu_to_core, coremap);
2266 set_xen_guest_handle(tinfo.cpu_to_socket, socketmap);
2267 set_xen_guest_handle(tinfo.cpu_to_node, nodemap);
2268 tinfo.max_cpu_index = info->coremap.entries - 1;
2269 if (xc_topologyinfo(ctx->xch, &tinfo) != 0)
2270 goto fail;
2272 for (i = 0; i <= tinfo.max_cpu_index; i++) {
2273 if (i < info->coremap.entries)
2274 info->coremap.array[i] = (coremap[i] == INVALID_TOPOLOGY_ID) ?
2275 LIBXL_CPUARRAY_INVALID_ENTRY : coremap[i];
2276 if (i < info->socketmap.entries)
2277 info->socketmap.array[i] = (socketmap[i] == INVALID_TOPOLOGY_ID) ?
2278 LIBXL_CPUARRAY_INVALID_ENTRY : socketmap[i];
2279 if (i < info->nodemap.entries)
2280 info->nodemap.array[i] = (nodemap[i] == INVALID_TOPOLOGY_ID) ?
2281 LIBXL_CPUARRAY_INVALID_ENTRY : nodemap[i];
2284 xc_hypercall_buffer_free(ctx->xch, coremap);
2285 xc_hypercall_buffer_free(ctx->xch, socketmap);
2286 xc_hypercall_buffer_free(ctx->xch, nodemap);
2287 return 0;
2289 fail:
2290 xc_hypercall_buffer_free(ctx->xch, coremap);
2291 xc_hypercall_buffer_free(ctx->xch, socketmap);
2292 xc_hypercall_buffer_free(ctx->xch, nodemap);
2293 libxl_topologyinfo_destroy(info);
2294 return ERROR_FAIL;
2297 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
2299 union {
2300 xen_extraversion_t xen_extra;
2301 xen_compile_info_t xen_cc;
2302 xen_changeset_info_t xen_chgset;
2303 xen_capabilities_info_t xen_caps;
2304 xen_platform_parameters_t p_parms;
2305 xen_commandline_t xen_commandline;
2306 } u;
2307 long xen_version;
2308 libxl_version_info *info = &ctx->version_info;
2310 if (info->xen_version_extra != NULL)
2311 return info;
2313 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
2314 info->xen_version_major = xen_version >> 16;
2315 info->xen_version_minor = xen_version & 0xFF;
2317 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
2318 info->xen_version_extra = strdup(u.xen_extra);
2320 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
2321 info->compiler = strdup(u.xen_cc.compiler);
2322 info->compile_by = strdup(u.xen_cc.compile_by);
2323 info->compile_domain = strdup(u.xen_cc.compile_domain);
2324 info->compile_date = strdup(u.xen_cc.compile_date);
2326 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
2327 info->capabilities = strdup(u.xen_caps);
2329 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
2330 info->changeset = strdup(u.xen_chgset);
2332 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
2333 info->virt_start = u.p_parms.virt_start;
2335 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
2337 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
2338 info->commandline = strdup(u.xen_commandline);
2340 return info;
2343 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
2344 int *nb_vcpu, int *nrcpus)
2346 libxl_vcpuinfo *ptr, *ret;
2347 xc_domaininfo_t domaininfo;
2348 xc_vcpuinfo_t vcpuinfo;
2350 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2351 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist");
2352 return NULL;
2354 *nrcpus = libxl_get_max_cpus(ctx);
2355 ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
2356 if (!ptr) {
2357 return NULL;
2360 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
2361 if (libxl_cpumap_alloc(ctx, &ptr->cpumap)) {
2362 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap");
2363 return NULL;
2365 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
2366 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info");
2367 return NULL;
2369 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) {
2370 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity");
2371 return NULL;
2373 ptr->vcpuid = *nb_vcpu;
2374 ptr->cpu = vcpuinfo.cpu;
2375 ptr->online = !!vcpuinfo.online;
2376 ptr->blocked = !!vcpuinfo.blocked;
2377 ptr->running = !!vcpuinfo.running;
2378 ptr->vcpu_time = vcpuinfo.cpu_time;
2380 return ret;
2383 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
2384 libxl_cpumap *cpumap)
2386 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap->map)) {
2387 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting vcpu affinity");
2388 return ERROR_FAIL;
2390 return 0;
2393 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask)
2395 libxl__gc gc = LIBXL_INIT_GC(ctx);
2396 libxl_dominfo info;
2397 char *dompath;
2398 xs_transaction_t t;
2399 int i, rc = ERROR_FAIL;
2401 if (libxl_domain_info(ctx, &info, domid) < 0) {
2402 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2403 goto out;
2405 if (!(dompath = libxl__xs_get_dompath(&gc, domid)))
2406 goto out;
2408 retry_transaction:
2409 t = xs_transaction_start(ctx->xsh);
2410 for (i = 0; i <= info.vcpu_max_id; i++)
2411 libxl__xs_write(&gc, t,
2412 libxl__sprintf(&gc, "%s/cpu/%u/availability", dompath, i),
2413 "%s", ((1 << i) & bitmask) ? "online" : "offline");
2414 if (!xs_transaction_end(ctx->xsh, t, 0)) {
2415 if (errno == EAGAIN)
2416 goto retry_transaction;
2417 } else
2418 rc = 0;
2419 out:
2420 libxl__free_all(&gc);
2421 return rc;
2424 /*
2425 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
2426 */
2427 int libxl_get_sched_id(libxl_ctx *ctx)
2429 int sched, ret;
2431 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
2432 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2433 return ERROR_FAIL;
2435 return sched;
2438 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2440 struct xen_domctl_sched_credit sdom;
2441 int rc;
2443 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
2444 if (rc != 0) {
2445 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2446 return ERROR_FAIL;
2449 scinfo->weight = sdom.weight;
2450 scinfo->cap = sdom.cap;
2452 return 0;
2455 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2457 struct xen_domctl_sched_credit sdom;
2458 xc_domaininfo_t domaininfo;
2459 int rc;
2461 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
2462 if (rc < 0) {
2463 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2464 return ERROR_FAIL;
2466 if (rc != 1 || domaininfo.domain != domid)
2467 return ERROR_INVAL;
2470 if (scinfo->weight < 1 || scinfo->weight > 65535) {
2471 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2472 "Cpu weight out of range, valid values are within range from 1 to 65535");
2473 return ERROR_INVAL;
2476 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
2477 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2478 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
2479 ((domaininfo.max_vcpu_id + 1) * 100));
2480 return ERROR_INVAL;
2483 sdom.weight = scinfo->weight;
2484 sdom.cap = scinfo->cap;
2486 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
2487 if ( rc < 0 ) {
2488 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2489 return ERROR_FAIL;
2492 return 0;
2495 static int trigger_type_from_string(char *trigger_name)
2497 if (!strcmp(trigger_name, "nmi"))
2498 return XEN_DOMCTL_SENDTRIGGER_NMI;
2499 else if (!strcmp(trigger_name, "reset"))
2500 return XEN_DOMCTL_SENDTRIGGER_RESET;
2501 else if (!strcmp(trigger_name, "init"))
2502 return XEN_DOMCTL_SENDTRIGGER_INIT;
2503 else if (!strcmp(trigger_name, "power"))
2504 return XEN_DOMCTL_SENDTRIGGER_POWER;
2505 else if (!strcmp(trigger_name, "sleep"))
2506 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
2507 else
2508 return -1;
2511 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
2513 int rc = -1;
2514 int trigger_type = trigger_type_from_string(trigger_name);
2516 if (trigger_type == -1) {
2517 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2518 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
2519 return ERROR_INVAL;
2522 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
2523 if (rc != 0) {
2524 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2525 "Send trigger '%s' failed", trigger_name);
2526 return ERROR_FAIL;
2529 return 0;
2532 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
2534 libxl__gc gc = LIBXL_INIT_GC(ctx);
2535 char *dompath = libxl__xs_get_dompath(&gc, domid);
2537 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/control/sysrq", dompath), "%c", sysrq);
2539 libxl__free_all(&gc);
2540 return 0;
2543 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
2545 int ret;
2546 ret = xc_send_debug_keys(ctx->xch, keys);
2547 if ( ret < 0 ) {
2548 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "sending debug keys");
2549 return ERROR_FAIL;
2551 return 0;
2554 libxl_xen_console_reader *
2555 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
2557 libxl_xen_console_reader *cr;
2558 unsigned int size = 16384;
2559 char *buf = malloc(size);
2561 if (!buf) {
2562 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
2563 " size is %u", size);
2564 return NULL;
2567 cr = malloc(sizeof(libxl_xen_console_reader));
2568 if (!cr) {
2569 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc libxl_xen_console_reader");
2570 return NULL;
2573 memset(cr, 0, sizeof(libxl_xen_console_reader));
2574 cr->buffer = buf;
2575 cr->size = size;
2576 cr->count = size;
2577 cr->clear = clear;
2578 cr->incremental = 1;
2580 return cr;
2583 /* return values: *line_r
2584 * 1 success, whole line obtained from buffer non-0
2585 * 0 no more lines available right now 0
2586 * negative error code ERROR_* 0
2587 * On success *line_r is updated to point to a nul-terminated
2588 * string which is valid until the next call on the same console
2589 * reader. The libxl caller may overwrite parts of the string
2590 * if it wishes. */
2591 int libxl_xen_console_read_line(libxl_ctx *ctx,
2592 libxl_xen_console_reader *cr,
2593 char **line_r)
2595 int ret;
2597 memset(cr->buffer, 0, cr->size);
2598 ret = xc_readconsolering(ctx->xch, cr->buffer, &cr->count,
2599 cr->clear, cr->incremental, &cr->index);
2600 if (ret < 0) {
2601 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "reading console ring buffer");
2602 return ERROR_FAIL;
2604 if (!ret) {
2605 if (cr->count) {
2606 *line_r = cr->buffer;
2607 ret = 1;
2608 } else {
2609 *line_r = NULL;
2610 ret = 0;
2614 return ret;
2617 void libxl_xen_console_read_finish(libxl_ctx *ctx,
2618 libxl_xen_console_reader *cr)
2620 free(cr->buffer);
2621 free(cr);
2624 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
2626 libxl__gc gc = LIBXL_INIT_GC(ctx);
2627 char *dompath = libxl__xs_get_dompath(&gc, domid);
2628 char *vm_path, *start_time;
2629 uint32_t ret;
2631 vm_path = libxl__xs_read(
2632 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dompath));
2633 start_time = libxl__xs_read(
2634 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/start_time", vm_path));
2635 if (start_time == NULL) {
2636 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2637 "Can't get start time of domain '%d'", domid);
2638 ret = -1;
2639 }else{
2640 ret = strtoul(start_time, NULL, 10);
2642 libxl__free_all(&gc);
2643 return ret;
2646 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
2648 int rc;
2649 char _buf[32768];
2651 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
2652 0, _buf);
2653 if (rc < 0) {
2654 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2655 "Can not get tmem list");
2656 return NULL;
2659 return strdup(_buf);
2662 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
2664 int rc;
2666 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
2667 0, NULL);
2668 if (rc < 0) {
2669 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2670 "Can not freeze tmem pools");
2671 return ERROR_FAIL;
2674 return rc;
2677 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
2679 int rc;
2681 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
2682 0, NULL);
2683 if (rc < 0) {
2684 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2685 "Can not destroy tmem pools");
2686 return ERROR_FAIL;
2689 return rc;
2692 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
2694 int rc;
2696 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
2697 0, NULL);
2698 if (rc < 0) {
2699 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2700 "Can not thaw tmem pools");
2701 return ERROR_FAIL;
2704 return rc;
2707 static int32_t tmem_setop_from_string(char *set_name)
2709 if (!strcmp(set_name, "weight"))
2710 return TMEMC_SET_WEIGHT;
2711 else if (!strcmp(set_name, "cap"))
2712 return TMEMC_SET_CAP;
2713 else if (!strcmp(set_name, "compress"))
2714 return TMEMC_SET_COMPRESS;
2715 else
2716 return -1;
2719 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
2721 int rc;
2722 int32_t subop = tmem_setop_from_string(name);
2724 if (subop == -1) {
2725 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2726 "Invalid set, valid sets are <weight|cap|compress>");
2727 return ERROR_INVAL;
2729 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
2730 if (rc < 0) {
2731 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2732 "Can not set tmem %s", name);
2733 return ERROR_FAIL;
2736 return rc;
2739 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
2740 char* uuid, int auth)
2742 int rc;
2744 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
2745 if (rc < 0) {
2746 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2747 "Can not set tmem shared auth");
2748 return ERROR_FAIL;
2751 return rc;
2754 int libxl_tmem_freeable(libxl_ctx *ctx)
2756 int rc;
2758 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
2759 if (rc < 0) {
2760 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2761 "Can not get tmem freeable memory");
2762 return ERROR_FAIL;
2765 return rc;
2768 void libxl_file_reference_destroy(libxl_file_reference *f)
2770 libxl__file_reference_unmap(f);
2771 free(f->path);
2774 int libxl_get_freecpus(libxl_ctx *ctx, libxl_cpumap *cpumap)
2776 int ncpus;
2778 ncpus = libxl_get_max_cpus(ctx);
2779 if (ncpus == 0)
2780 return ERROR_FAIL;
2782 cpumap->map = xc_cpupool_freeinfo(ctx->xch);
2783 if (cpumap->map == NULL)
2784 return ERROR_FAIL;
2786 cpumap->size = (ncpus + 7) / 8;
2788 return 0;
2791 int libxl_create_cpupool(libxl_ctx *ctx, const char *name, int schedid,
2792 libxl_cpumap cpumap, libxl_uuid *uuid,
2793 uint32_t *poolid)
2795 libxl__gc gc = LIBXL_INIT_GC(ctx);
2796 int rc;
2797 int i;
2798 xs_transaction_t t;
2799 char *uuid_string;
2801 uuid_string = libxl__uuid2string(&gc, *uuid);
2802 if (!uuid_string) {
2803 libxl__free_all(&gc);
2804 return ERROR_NOMEM;
2807 rc = xc_cpupool_create(ctx->xch, poolid, schedid);
2808 if (rc) {
2809 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2810 "Could not create cpupool");
2811 libxl__free_all(&gc);
2812 return ERROR_FAIL;
2815 libxl_for_each_cpu(i, cpumap)
2816 if (libxl_cpumap_test(&cpumap, i)) {
2817 rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
2818 if (rc) {
2819 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2820 "Error moving cpu to cpupool");
2821 libxl_destroy_cpupool(ctx, *poolid);
2822 libxl__free_all(&gc);
2823 return ERROR_FAIL;
2827 for (;;) {
2828 t = xs_transaction_start(ctx->xsh);
2830 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/pool/%d", *poolid));
2831 libxl__xs_write(&gc, t,
2832 libxl__sprintf(&gc, "/local/pool/%d/uuid", *poolid),
2833 "%s", uuid_string);
2834 libxl__xs_write(&gc, t,
2835 libxl__sprintf(&gc, "/local/pool/%d/name", *poolid),
2836 "%s", name);
2838 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN)) {
2839 libxl__free_all(&gc);
2840 return 0;
2845 int libxl_destroy_cpupool(libxl_ctx *ctx, uint32_t poolid)
2847 libxl__gc gc = LIBXL_INIT_GC(ctx);
2848 int rc, i;
2849 xc_cpupoolinfo_t *info;
2850 xs_transaction_t t;
2851 libxl_cpumap cpumap;
2853 info = xc_cpupool_getinfo(ctx->xch, poolid);
2854 if (info == NULL) {
2855 libxl__free_all(&gc);
2856 return ERROR_NOMEM;
2859 rc = ERROR_INVAL;
2860 if ((info->cpupool_id != poolid) || (info->n_dom))
2861 goto out;
2863 rc = ERROR_NOMEM;
2864 if (libxl_cpumap_alloc(ctx, &cpumap))
2865 goto out;
2867 memcpy(cpumap.map, info->cpumap, cpumap.size);
2868 libxl_for_each_cpu(i, cpumap)
2869 if (libxl_cpumap_test(&cpumap, i)) {
2870 rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
2871 if (rc) {
2872 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2873 "Error removing cpu from cpupool");
2874 rc = ERROR_FAIL;
2875 goto out1;
2879 rc = xc_cpupool_destroy(ctx->xch, poolid);
2880 if (rc) {
2881 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "Could not destroy cpupool");
2882 rc = ERROR_FAIL;
2883 goto out1;
2886 for (;;) {
2887 t = xs_transaction_start(ctx->xsh);
2889 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/pool/%d", poolid));
2891 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
2892 break;
2895 rc = 0;
2897 out1:
2898 libxl_cpumap_destroy(&cpumap);
2899 out:
2900 xc_cpupool_infofree(ctx->xch, info);
2901 libxl__free_all(&gc);
2903 return rc;
2906 int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid)
2908 libxl__gc gc = LIBXL_INIT_GC(ctx);
2909 xs_transaction_t t;
2910 xc_cpupoolinfo_t *info;
2911 int rc;
2913 info = xc_cpupool_getinfo(ctx->xch, poolid);
2914 if (info == NULL) {
2915 libxl__free_all(&gc);
2916 return ERROR_NOMEM;
2919 rc = ERROR_INVAL;
2920 if (info->cpupool_id != poolid)
2921 goto out;
2923 rc = 0;
2925 for (;;) {
2926 t = xs_transaction_start(ctx->xsh);
2928 libxl__xs_write(&gc, t,
2929 libxl__sprintf(&gc, "/local/pool/%d/name", poolid),
2930 "%s", name);
2932 if (xs_transaction_end(ctx->xsh, t, 0))
2933 break;
2935 if (errno == EAGAIN)
2936 continue;
2938 rc = ERROR_FAIL;
2939 break;
2942 out:
2943 xc_cpupool_infofree(ctx->xch, info);
2944 libxl__free_all(&gc);
2946 return rc;
2949 int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
2951 int rc;
2953 rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
2954 if (rc) {
2955 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2956 "Error moving cpu to cpupool");
2957 return ERROR_FAIL;
2959 return 0;
2962 int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
2964 int rc = 0;
2965 int cpu;
2966 libxl_cpumap freemap;
2967 libxl_topologyinfo topology;
2969 if (libxl_get_freecpus(ctx, &freemap)) {
2970 return ERROR_FAIL;
2973 if (libxl_get_topologyinfo(ctx, &topology)) {
2974 rc = ERROR_FAIL;
2975 goto out;
2978 *cpus = 0;
2979 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
2980 if (libxl_cpumap_test(&freemap, cpu) &&
2981 (topology.nodemap.array[cpu] == node) &&
2982 !libxl_cpupool_cpuadd(ctx, poolid, cpu)) {
2983 (*cpus)++;
2987 libxl_topologyinfo_destroy(&topology);
2989 out:
2990 libxl_cpumap_destroy(&freemap);
2991 return rc;
2994 int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
2996 int rc;
2998 rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
2999 if (rc) {
3000 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3001 "Error removing cpu from cpupool");
3002 return ERROR_FAIL;
3004 return 0;
3007 int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3009 int ret = 0;
3010 int n_pools;
3011 int p;
3012 int cpu;
3013 libxl_topologyinfo topology;
3014 libxl_cpupoolinfo *poolinfo;
3016 poolinfo = libxl_list_cpupool(ctx, &n_pools);
3017 if (!poolinfo) {
3018 return ERROR_NOMEM;
3021 if (libxl_get_topologyinfo(ctx, &topology)) {
3022 ret = ERROR_FAIL;
3023 goto out;
3026 *cpus = 0;
3027 for (p = 0; p < n_pools; p++) {
3028 if (poolinfo[p].poolid == poolid) {
3029 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3030 if ((topology.nodemap.array[cpu] == node) &&
3031 libxl_cpumap_test(&poolinfo[p].cpumap, cpu) &&
3032 !libxl_cpupool_cpuremove(ctx, poolid, cpu)) {
3033 (*cpus)++;
3039 libxl_topologyinfo_destroy(&topology);
3041 out:
3042 for (p = 0; p < n_pools; p++) {
3043 libxl_cpupoolinfo_destroy(poolinfo + p);
3046 return ret;
3049 int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
3051 libxl__gc gc = LIBXL_INIT_GC(ctx);
3052 int rc;
3053 char *dom_path;
3054 char *vm_path;
3055 char *poolname;
3056 xs_transaction_t t;
3058 dom_path = libxl__xs_get_dompath(&gc, domid);
3059 if (!dom_path) {
3060 libxl__free_all(&gc);
3061 return ERROR_FAIL;
3064 rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
3065 if (rc) {
3066 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3067 "Error moving domain to cpupool");
3068 libxl__free_all(&gc);
3069 return ERROR_FAIL;
3072 for (;;) {
3073 t = xs_transaction_start(ctx->xsh);
3075 poolname = libxl__cpupoolid_to_name(&gc, poolid);
3076 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
3077 if (!vm_path)
3078 break;
3080 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/pool_name", vm_path),
3081 "%s", poolname);
3083 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3084 break;
3087 libxl__free_all(&gc);
3088 return 0;