debuggers.hg

view tools/libxl/libxl.c @ 22725:e1392f5327ce

xl: Move device model functions in to a separate file

No functional changes.

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Fri Jan 07 18:24:54 2011 +0000 (2011-01-07)
parents e18d9af29d2a
children 03718b569d97
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 if (version != LIBXL_VERSION)
44 return ERROR_VERSION;
45 memset(ctx, 0, sizeof(libxl_ctx));
46 ctx->lg = lg;
47 memset(&ctx->version_info, 0, sizeof(libxl_version_info));
49 ctx->xch = xc_interface_open(lg,lg,0);
50 if (!ctx->xch) {
51 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
52 "cannot open libxc handle");
53 return ERROR_FAIL;
54 }
56 ctx->xsh = xs_daemon_open();
57 if (!ctx->xsh)
58 ctx->xsh = xs_domain_open();
59 if (!ctx->xsh) {
60 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
61 "cannot connect to xenstore");
62 xc_interface_close(ctx->xch);
63 return ERROR_FAIL;
64 }
65 return 0;
66 }
68 int libxl_ctx_free(libxl_ctx *ctx)
69 {
70 if (ctx->xch) xc_interface_close(ctx->xch);
71 libxl_version_info_destroy(&ctx->version_info);
72 if (ctx->xsh) xs_daemon_close(ctx->xsh);
73 return 0;
74 }
76 void libxl_string_list_destroy(libxl_string_list *psl)
77 {
78 int i;
79 libxl_string_list sl = *psl;
81 if (!sl)
82 return;
84 for (i = 0; sl[i] != NULL; i++)
85 free(sl[i]);
86 free(sl);
87 }
89 void libxl_key_value_list_destroy(libxl_key_value_list *pkvl)
90 {
91 int i;
92 libxl_key_value_list kvl = *pkvl;
94 if (!kvl)
95 return;
97 for (i = 0; kvl[i] != NULL; i += 2) {
98 free(kvl[i]);
99 if (kvl[i + 1])
100 free(kvl[i + 1]);
101 }
102 free(kvl);
103 }
105 /******************************************************************************/
107 int libxl_domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
108 uint32_t *domid)
109 {
110 libxl__gc gc = LIBXL_INIT_GC(ctx);
111 int flags, ret, i, rc;
112 char *uuid_string;
113 char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
114 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
115 "control", "attr", "messages" };
116 char *dom_path, *vm_path;
117 struct xs_permissions roperm[2];
118 struct xs_permissions rwperm[1];
119 xs_transaction_t t;
120 xen_domain_handle_t handle;
122 uuid_string = libxl__uuid2string(&gc, info->uuid);
123 if (!uuid_string) {
124 libxl__free_all(&gc);
125 return ERROR_NOMEM;
126 }
128 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
129 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
130 flags |= info->oos ? 0 : XEN_DOMCTL_CDF_oos_off;
131 *domid = -1;
133 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
134 libxl_uuid_copy((libxl_uuid *)handle, &info->uuid);
136 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
137 if (ret < 0) {
138 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, ret, "domain creation fail");
139 libxl__free_all(&gc);
140 return ERROR_FAIL;
141 }
143 ret = xc_cpupool_movedomain(ctx->xch, info->poolid, *domid);
144 if (ret < 0) {
145 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, ret, "domain move fail");
146 libxl__free_all(&gc);
147 return ERROR_FAIL;
148 }
150 dom_path = libxl__xs_get_dompath(&gc, *domid);
151 if (!dom_path) {
152 libxl__free_all(&gc);
153 return ERROR_FAIL;
154 }
156 vm_path = libxl__sprintf(&gc, "/vm/%s", uuid_string);
157 if (!vm_path) {
158 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot allocate create paths");
159 libxl__free_all(&gc);
160 return ERROR_FAIL;
161 }
163 roperm[0].id = 0;
164 roperm[0].perms = XS_PERM_NONE;
165 roperm[1].id = *domid;
166 roperm[1].perms = XS_PERM_READ;
167 rwperm[0].id = *domid;
168 rwperm[0].perms = XS_PERM_NONE;
170 retry_transaction:
171 t = xs_transaction_start(ctx->xsh);
172 xs_rm(ctx->xsh, t, dom_path);
173 xs_mkdir(ctx->xsh, t, dom_path);
174 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
176 xs_rm(ctx->xsh, t, vm_path);
177 xs_mkdir(ctx->xsh, t, vm_path);
178 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
180 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/vm", dom_path), vm_path, strlen(vm_path));
181 rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
182 if (rc) {
183 libxl__free_all(&gc);
184 return rc;
185 }
187 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
188 char *path = libxl__sprintf(&gc, "%s/%s", dom_path, rw_paths[i]);
189 xs_mkdir(ctx->xsh, t, path);
190 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
191 }
192 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
193 char *path = libxl__sprintf(&gc, "%s/%s", dom_path, ro_paths[i]);
194 xs_mkdir(ctx->xsh, t, path);
195 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
196 }
198 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
199 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/name", vm_path), info->name, strlen(info->name));
200 if (info->poolname)
201 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/pool_name", vm_path), info->poolname, strlen(info->poolname));
203 libxl__xs_writev(&gc, t, dom_path, info->xsdata);
204 libxl__xs_writev(&gc, t, libxl__sprintf(&gc, "%s/platform", dom_path), info->platformdata);
206 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
208 if (!xs_transaction_end(ctx->xsh, t, 0))
209 if (errno == EAGAIN)
210 goto retry_transaction;
212 libxl__free_all(&gc);
213 return 0;
214 }
216 int libxl_domain_rename(libxl_ctx *ctx, uint32_t domid,
217 const char *old_name, const char *new_name,
218 xs_transaction_t trans)
219 {
220 libxl__gc gc = LIBXL_INIT_GC(ctx);
221 char *dom_path = 0;
222 const char *name_path;
223 char *got_old_name;
224 unsigned int got_old_len;
225 xs_transaction_t our_trans = 0;
226 int rc;
228 dom_path = libxl__xs_get_dompath(&gc, domid);
229 if (!dom_path) goto x_nomem;
231 name_path= libxl__sprintf(&gc, "%s/name", dom_path);
232 if (!name_path) goto x_nomem;
234 retry_transaction:
235 if (!trans) {
236 trans = our_trans = xs_transaction_start(ctx->xsh);
237 if (!our_trans) {
238 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno,
239 "create xs transaction for domain (re)name");
240 goto x_fail;
241 }
242 }
244 if (old_name) {
245 got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len);
246 if (!got_old_name) {
247 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, errno, "check old name"
248 " for domain %"PRIu32" allegedly named `%s'",
249 domid, old_name);
250 goto x_fail;
251 }
252 if (strcmp(old_name, got_old_name)) {
253 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "domain %"PRIu32" allegedly named "
254 "`%s' is actually named `%s' - racing ?",
255 domid, old_name, got_old_name);
256 free(got_old_name);
257 goto x_fail;
258 }
259 free(got_old_name);
260 }
261 if (!xs_write(ctx->xsh, trans, name_path,
262 new_name, strlen(new_name))) {
263 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "failed to write new name `%s'"
264 " for domain %"PRIu32" previously named `%s'",
265 new_name, domid, old_name);
266 goto x_fail;
267 }
269 if (our_trans) {
270 if (!xs_transaction_end(ctx->xsh, our_trans, 0)) {
271 trans = our_trans = 0;
272 if (errno != EAGAIN) {
273 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "failed to commit new name `%s'"
274 " for domain %"PRIu32" previously named `%s'",
275 new_name, domid, old_name);
276 goto x_fail;
277 }
278 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "need to retry rename transaction"
279 " for domain %"PRIu32" (name_path=\"%s\", new_name=\"%s\")",
280 domid, name_path, new_name);
281 goto retry_transaction;
282 }
283 our_trans = 0;
284 }
286 rc = 0;
287 x_rc:
288 if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1);
289 libxl__free_all(&gc);
290 return rc;
292 x_fail: rc = ERROR_FAIL; goto x_rc;
293 x_nomem: rc = ERROR_NOMEM; goto x_rc;
294 }
296 int libxl_domain_build(libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
297 {
298 libxl__gc gc = LIBXL_INIT_GC(ctx);
299 char **vments = NULL, **localents = NULL;
300 struct timeval start_time;
301 int i, ret;
303 ret = libxl__build_pre(ctx, domid, info, state);
304 if (ret)
305 goto out;
307 gettimeofday(&start_time, NULL);
309 if (info->hvm) {
310 ret = libxl__build_hvm(ctx, domid, info, state);
311 if (ret)
312 goto out;
314 vments = libxl__calloc(&gc, 7, sizeof(char *));
315 vments[0] = "rtc/timeoffset";
316 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
317 vments[2] = "image/ostype";
318 vments[3] = "hvm";
319 vments[4] = "start_time";
320 vments[5] = libxl__sprintf(&gc, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
321 } else {
322 ret = libxl__build_pv(ctx, domid, info, state);
323 if (ret)
324 goto out;
326 vments = libxl__calloc(&gc, 11, sizeof(char *));
327 i = 0;
328 vments[i++] = "image/ostype";
329 vments[i++] = "linux";
330 vments[i++] = "image/kernel";
331 vments[i++] = (char*) info->kernel.path;
332 vments[i++] = "start_time";
333 vments[i++] = libxl__sprintf(&gc, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
334 if (info->u.pv.ramdisk.path) {
335 vments[i++] = "image/ramdisk";
336 vments[i++] = (char*) info->u.pv.ramdisk.path;
337 }
338 if (info->u.pv.cmdline) {
339 vments[i++] = "image/cmdline";
340 vments[i++] = (char*) info->u.pv.cmdline;
341 }
342 }
343 ret = libxl__build_post(ctx, domid, info, state, vments, localents);
344 out:
345 libxl__file_reference_unmap(&info->kernel);
346 if (!info->hvm)
347 libxl__file_reference_unmap(&info->u.pv.ramdisk);
349 libxl__free_all(&gc);
350 return ret;
351 }
353 int libxl_domain_restore(libxl_ctx *ctx, libxl_domain_build_info *info,
354 uint32_t domid, int fd, libxl_domain_build_state *state,
355 libxl_device_model_info *dm_info)
356 {
357 libxl__gc gc = LIBXL_INIT_GC(ctx);
358 char **vments = NULL, **localents = NULL;
359 struct timeval start_time;
360 int i, ret, esave, flags;
362 ret = libxl__build_pre(ctx, domid, info, state);
363 if (ret)
364 goto out;
366 ret = libxl__domain_restore_common(ctx, domid, info, state, fd);
367 if (ret)
368 goto out;
370 gettimeofday(&start_time, NULL);
372 if (info->hvm) {
373 vments = libxl__calloc(&gc, 7, sizeof(char *));
374 vments[0] = "rtc/timeoffset";
375 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
376 vments[2] = "image/ostype";
377 vments[3] = "hvm";
378 vments[4] = "start_time";
379 vments[5] = libxl__sprintf(&gc, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
380 } else {
381 vments = libxl__calloc(&gc, 11, sizeof(char *));
382 i = 0;
383 vments[i++] = "image/ostype";
384 vments[i++] = "linux";
385 vments[i++] = "image/kernel";
386 vments[i++] = (char*) info->kernel.path;
387 vments[i++] = "start_time";
388 vments[i++] = libxl__sprintf(&gc, "%lu.%02d", start_time.tv_sec,(int)start_time.tv_usec/10000);
389 if (info->u.pv.ramdisk.path) {
390 vments[i++] = "image/ramdisk";
391 vments[i++] = (char*) info->u.pv.ramdisk.path;
392 }
393 if (info->u.pv.cmdline) {
394 vments[i++] = "image/cmdline";
395 vments[i++] = (char*) info->u.pv.cmdline;
396 }
397 }
398 ret = libxl__build_post(ctx, domid, info, state, vments, localents);
399 if (ret)
400 goto out;
402 dm_info->saved_state = NULL;
403 if (info->hvm) {
404 ret = asprintf(&dm_info->saved_state,
405 "/var/lib/xen/qemu-save.%d", domid);
406 ret = (ret < 0) ? ERROR_FAIL : 0;
407 }
409 out:
410 libxl__file_reference_unmap(&info->kernel);
411 if (!info->hvm)
412 libxl__file_reference_unmap(&info->u.pv.ramdisk);
414 esave = errno;
416 flags = fcntl(fd, F_GETFL);
417 if (flags == -1) {
418 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unable to get flags on restore fd");
419 } else {
420 flags &= ~O_NONBLOCK;
421 if (fcntl(fd, F_SETFL, flags) == -1)
422 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unable to put restore fd"
423 " back to blocking mode");
424 }
426 errno = esave;
427 libxl__free_all(&gc);
428 return ret;
429 }
431 int libxl_domain_resume(libxl_ctx *ctx, uint32_t domid)
432 {
433 libxl__gc gc = LIBXL_INIT_GC(ctx);
434 int rc = 0;
436 if (libxl__domain_is_hvm(ctx, domid)) {
437 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Called domain_resume on "
438 "non-cooperative hvm domain %u", domid);
439 rc = ERROR_NI;
440 goto out;
441 }
442 if (xc_domain_resume(ctx->xch, domid, 1)) {
443 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
444 "xc_domain_resume failed for domain %u",
445 domid);
446 rc = ERROR_FAIL;
447 goto out;
448 }
449 if (!xs_resume_domain(ctx->xsh, domid)) {
450 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
451 "xs_resume_domain failed for domain %u",
452 domid);
453 rc = ERROR_FAIL;
454 }
455 out:
456 libxl__free_all(&gc);
457 return 0;
458 }
460 /*
461 * Preserves a domain but rewrites xenstore etc to make it unique so
462 * that the domain can be restarted.
463 *
464 * Does not modify info so that it may be reused.
465 */
466 int libxl_domain_preserve(libxl_ctx *ctx, uint32_t domid,
467 libxl_domain_create_info *info, const char *name_suffix, libxl_uuid new_uuid)
468 {
469 libxl__gc gc = LIBXL_INIT_GC(ctx);
470 struct xs_permissions roperm[2];
471 xs_transaction_t t;
472 char *preserved_name;
473 char *uuid_string;
474 char *vm_path;
475 char *dom_path;
477 int rc;
479 preserved_name = libxl__sprintf(&gc, "%s%s", info->name, name_suffix);
480 if (!preserved_name) {
481 libxl__free_all(&gc);
482 return ERROR_NOMEM;
483 }
485 uuid_string = libxl__uuid2string(&gc, new_uuid);
486 if (!uuid_string) {
487 libxl__free_all(&gc);
488 return ERROR_NOMEM;
489 }
491 dom_path = libxl__xs_get_dompath(&gc, domid);
492 if (!dom_path) {
493 libxl__free_all(&gc);
494 return ERROR_FAIL;
495 }
497 vm_path = libxl__sprintf(&gc, "/vm/%s", uuid_string);
498 if (!vm_path) {
499 libxl__free_all(&gc);
500 return ERROR_FAIL;
501 }
503 roperm[0].id = 0;
504 roperm[0].perms = XS_PERM_NONE;
505 roperm[1].id = domid;
506 roperm[1].perms = XS_PERM_READ;
508 retry_transaction:
509 t = xs_transaction_start(ctx->xsh);
511 xs_rm(ctx->xsh, t, vm_path);
512 xs_mkdir(ctx->xsh, t, vm_path);
513 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
515 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/vm", dom_path), vm_path, strlen(vm_path));
516 rc = libxl_domain_rename(ctx, domid, info->name, preserved_name, t);
517 if (rc) {
518 libxl__free_all(&gc);
519 return rc;
520 }
522 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
524 if (!xs_transaction_end(ctx->xsh, t, 0))
525 if (errno == EAGAIN)
526 goto retry_transaction;
528 libxl__free_all(&gc);
529 return 0;
530 }
532 static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
533 libxl_dominfo *xlinfo)
534 {
535 memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
536 xlinfo->domid = xcinfo->domain;
538 xlinfo->dying = !!(xcinfo->flags&XEN_DOMINF_dying);
539 xlinfo->shutdown = !!(xcinfo->flags&XEN_DOMINF_shutdown);
540 xlinfo->paused = !!(xcinfo->flags&XEN_DOMINF_paused);
541 xlinfo->blocked = !!(xcinfo->flags&XEN_DOMINF_blocked);
542 xlinfo->running = !!(xcinfo->flags&XEN_DOMINF_running);
544 if (xlinfo->shutdown || xlinfo->dying)
545 xlinfo->shutdown_reason = (xcinfo->flags>>XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask;
546 else
547 xlinfo->shutdown_reason = ~0;
549 xlinfo->current_memkb = PAGE_TO_MEMKB(xcinfo->tot_pages);
550 xlinfo->max_memkb = PAGE_TO_MEMKB(xcinfo->max_pages);
551 xlinfo->cpu_time = xcinfo->cpu_time;
552 xlinfo->vcpu_max_id = xcinfo->max_vcpu_id;
553 xlinfo->vcpu_online = xcinfo->nr_online_vcpus;
554 }
556 libxl_dominfo * libxl_list_domain(libxl_ctx *ctx, int *nb_domain)
557 {
558 libxl_dominfo *ptr;
559 int i, ret;
560 xc_domaininfo_t info[1024];
561 int size = 1024;
563 ptr = calloc(size, sizeof(libxl_dominfo));
564 if (!ptr) {
565 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating domain info");
566 return NULL;
567 }
569 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
570 if (ret<0) {
571 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
572 return NULL;
573 }
575 for (i = 0; i < ret; i++) {
576 xcinfo2xlinfo(&info[i], &ptr[i]);
577 }
578 *nb_domain = ret;
579 return ptr;
580 }
582 int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
583 uint32_t domid) {
584 xc_domaininfo_t xcinfo;
585 int ret;
587 ret = xc_domain_getinfolist(ctx->xch, domid, 1, &xcinfo);
588 if (ret<0) {
589 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
590 return ERROR_FAIL;
591 }
592 if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
594 xcinfo2xlinfo(&xcinfo, info_r);
595 return 0;
596 }
598 libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool)
599 {
600 libxl_cpupoolinfo *ptr, *tmp;
601 int i;
602 xc_cpupoolinfo_t *info;
603 uint32_t poolid;
605 ptr = NULL;
607 poolid = 0;
608 for (i = 0;; i++) {
609 info = xc_cpupool_getinfo(ctx->xch, poolid);
610 if (info == NULL)
611 break;
612 tmp = realloc(ptr, (i + 1) * sizeof(libxl_cpupoolinfo));
613 if (!tmp) {
614 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpupool info");
615 free(ptr);
616 xc_cpupool_infofree(ctx->xch, info);
617 return NULL;
618 }
619 ptr = tmp;
620 ptr[i].poolid = info->cpupool_id;
621 ptr[i].sched_id = info->sched_id;
622 ptr[i].n_dom = info->n_dom;
623 if (libxl_cpumap_alloc(ctx, &ptr[i].cpumap)) {
624 xc_cpupool_infofree(ctx->xch, info);
625 break;
626 }
627 memcpy(ptr[i].cpumap.map, info->cpumap, ptr[i].cpumap.size);
628 poolid = info->cpupool_id + 1;
629 xc_cpupool_infofree(ctx->xch, info);
630 }
632 *nb_pool = i;
633 return ptr;
634 }
636 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
637 libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm)
638 {
639 libxl_vminfo *ptr;
640 int index, i, ret;
641 xc_domaininfo_t info[1024];
642 int size = 1024;
644 ptr = calloc(size, sizeof(libxl_dominfo));
645 if (!ptr)
646 return NULL;
648 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
649 if (ret<0) {
650 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
651 return NULL;
652 }
653 for (index = i = 0; i < ret; i++) {
654 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
655 continue;
656 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
657 ptr[index].domid = info[i].domain;
659 index++;
660 }
661 *nb_vm = index;
662 return ptr;
663 }
665 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
666 uint32_t domid, int fd)
667 {
668 int hvm = libxl__domain_is_hvm(ctx, domid);
669 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
670 int debug = info != NULL && info->flags & XL_SUSPEND_DEBUG;
671 int rc = 0;
673 rc = libxl__domain_suspend_common(ctx, domid, fd, hvm, live, debug);
674 if (!rc && hvm)
675 rc = libxl__domain_save_device_model(ctx, domid, fd);
676 return rc;
677 }
679 int libxl_domain_pause(libxl_ctx *ctx, uint32_t domid)
680 {
681 int ret;
682 ret = xc_domain_pause(ctx->xch, domid);
683 if (ret<0) {
684 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "pausing domain %d", domid);
685 return ERROR_FAIL;
686 }
687 return 0;
688 }
690 int libxl_domain_core_dump(libxl_ctx *ctx, uint32_t domid,
691 const char *filename)
692 {
693 int ret;
694 ret = xc_domain_dumpcore(ctx->xch, domid, filename);
695 if (ret<0) {
696 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "core dumping domain %d to %s",
697 domid, filename);
698 return ERROR_FAIL;
699 }
700 return 0;
701 }
703 int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
704 {
705 libxl__gc gc = LIBXL_INIT_GC(ctx);
706 char *path;
707 char *state;
708 int ret, rc = 0;
710 if (libxl__domain_is_hvm(ctx, domid)) {
711 path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d/state", domid);
712 state = libxl__xs_read(&gc, XBT_NULL, path);
713 if (state != NULL && !strcmp(state, "paused")) {
714 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d/command", domid), "continue");
715 libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL);
716 }
717 }
718 ret = xc_domain_unpause(ctx->xch, domid);
719 if (ret<0) {
720 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain %d", domid);
721 rc = ERROR_FAIL;
722 }
723 libxl__free_all(&gc);
724 return rc;
725 }
727 static char *req_table[] = {
728 [0] = "poweroff",
729 [1] = "reboot",
730 [2] = "suspend",
731 [3] = "crash",
732 [4] = "halt",
733 };
735 int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
736 {
737 libxl__gc gc = LIBXL_INIT_GC(ctx);
738 char *shutdown_path;
739 char *dom_path;
741 if (req > ARRAY_SIZE(req_table)) {
742 libxl__free_all(&gc);
743 return ERROR_INVAL;
744 }
746 dom_path = libxl__xs_get_dompath(&gc, domid);
747 if (!dom_path) {
748 libxl__free_all(&gc);
749 return ERROR_FAIL;
750 }
752 shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
754 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
755 if (libxl__domain_is_hvm(ctx,domid)) {
756 unsigned long acpi_s_state = 0;
757 unsigned long pvdriver = 0;
758 int ret;
759 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
760 if (ret<0) {
761 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting ACPI S-state");
762 libxl__free_all(&gc);
763 return ERROR_FAIL;
764 }
765 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
766 if (ret<0) {
767 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
768 libxl__free_all(&gc);
769 return ERROR_FAIL;
770 }
771 if (!pvdriver || acpi_s_state != 0) {
772 ret = xc_domain_shutdown(ctx->xch, domid, req);
773 if (ret<0) {
774 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain");
775 libxl__free_all(&gc);
776 return ERROR_FAIL;
777 }
778 }
779 }
780 libxl__free_all(&gc);
781 return 0;
782 }
784 int libxl_get_wait_fd(libxl_ctx *ctx, int *fd)
785 {
786 *fd = xs_fileno(ctx->xsh);
787 return 0;
788 }
790 int libxl_wait_for_domain_death(libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
791 {
792 waiter->path = strdup("@releaseDomain");
793 if (asprintf(&(waiter->token), "%d", LIBXL_EVENT_DOMAIN_DEATH) < 0)
794 return -1;
795 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
796 return -1;
797 return 0;
798 }
800 int libxl_wait_for_disk_ejects(libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
801 {
802 libxl__gc gc = LIBXL_INIT_GC(ctx);
803 int i, rc = -1;
804 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
806 if (!domid)
807 domid = guest_domid;
809 for (i = 0; i < num_disks; i++) {
810 if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
811 libxl__xs_get_dompath(&gc, domid),
812 libxl__device_disk_dev_number(disks[i].virtpath)) < 0)
813 goto out;
814 if (asprintf(&(waiter[i].token), "%d", LIBXL_EVENT_DISK_EJECT) < 0)
815 goto out;
816 xs_watch(ctx->xsh, waiter->path, waiter->token);
817 }
818 rc = 0;
819 out:
820 libxl__free_all(&gc);
821 return rc;
822 }
824 int libxl_get_event(libxl_ctx *ctx, libxl_event *event)
825 {
826 unsigned int num;
827 char **events = xs_read_watch(ctx->xsh, &num);
828 if (num != 2) {
829 free(events);
830 return ERROR_FAIL;
831 }
832 event->path = strdup(events[XS_WATCH_PATH]);
833 event->token = strdup(events[XS_WATCH_TOKEN]);
834 event->type = atoi(event->token);
835 free(events);
836 return 0;
837 }
839 int libxl_stop_waiting(libxl_ctx *ctx, libxl_waiter *waiter)
840 {
841 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
842 return ERROR_FAIL;
843 else
844 return 0;
845 }
847 int libxl_free_event(libxl_event *event)
848 {
849 free(event->path);
850 free(event->token);
851 return 0;
852 }
854 int libxl_free_waiter(libxl_waiter *waiter)
855 {
856 free(waiter->path);
857 free(waiter->token);
858 return 0;
859 }
861 int libxl_event_get_domain_death_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_dominfo *info)
862 {
863 if (libxl_domain_info(ctx, info, domid) < 0)
864 return 0;
866 if (info->running || (!info->shutdown && !info->dying))
867 return ERROR_INVAL;
869 return 1;
870 }
872 int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
873 {
874 libxl__gc gc = LIBXL_INIT_GC(ctx);
875 char *path;
876 char *backend;
877 char *value;
879 value = libxl__xs_read(&gc, XBT_NULL, event->path);
881 if (!value || strcmp(value, "eject")) {
882 libxl__free_all(&gc);
883 return 0;
884 }
886 path = strdup(event->path);
887 path[strlen(path) - 6] = '\0';
888 backend = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend", path));
890 disk->backend_domid = 0;
891 disk->domid = domid;
892 disk->physpath = NULL;
893 disk->phystype = 0;
894 /* this value is returned to the user: do not free right away */
895 disk->virtpath = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/dev", backend));
896 disk->unpluggable = 1;
897 disk->readwrite = 0;
898 disk->is_cdrom = 1;
900 free(path);
901 libxl__free_all(&gc);
902 return 1;
903 }
905 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
906 {
907 libxl__gc gc = LIBXL_INIT_GC(ctx);
908 char *dom_path;
909 char *vm_path;
910 int rc, dm_present;
912 if (libxl__domain_is_hvm(ctx, domid)) {
913 dm_present = 1;
914 } else {
915 char *pid;
916 pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/image/device-model-pid", domid));
917 dm_present = (pid != NULL);
918 }
920 dom_path = libxl__xs_get_dompath(&gc, domid);
921 if (!dom_path) {
922 rc = ERROR_FAIL;
923 goto out;
924 }
926 if (libxl_device_pci_shutdown(ctx, domid) < 0)
927 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "pci shutdown failed for domid %d", domid);
928 if (dm_present) {
929 xs_write(ctx->xsh, XBT_NULL,
930 libxl__sprintf(&gc, "/local/domain/0/device-model/%d/command", domid),
931 "shutdown", strlen("shutdown"));
932 }
933 rc = xc_domain_pause(ctx->xch, domid);
934 if (rc < 0) {
935 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
936 }
937 if (dm_present) {
938 if (libxl__destroy_device_model(ctx, domid) < 0)
939 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model failed for %d", domid);
940 }
941 if (libxl__devices_destroy(ctx, domid, force) < 0)
942 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
944 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
945 if (vm_path)
946 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
947 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", vm_path);
949 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
950 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", dom_path);
952 libxl__userdata_destroyall(ctx, domid);
954 rc = xc_domain_destroy(ctx->xch, domid);
955 if (rc < 0) {
956 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
957 rc = ERROR_FAIL;
958 goto out;
959 }
960 rc = 0;
961 out:
962 libxl__free_all(&gc);
963 return 0;
964 }
966 int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num, libxl_console_constype type)
967 {
968 libxl__gc gc = LIBXL_INIT_GC(ctx);
969 char *p = libxl__sprintf(&gc, "%s/xenconsole", libxl_private_bindir_path());
970 char *domid_s = libxl__sprintf(&gc, "%d", domid);
971 char *cons_num_s = libxl__sprintf(&gc, "%d", cons_num);
972 char *cons_type_s;
974 switch (type) {
975 case LIBXL_CONSTYPE_PV:
976 cons_type_s = "pv";
977 break;
978 case LIBXL_CONSTYPE_SERIAL:
979 cons_type_s = "serial";
980 break;
981 default:
982 goto out;
983 }
985 execl(p, p, domid_s, "--num", cons_num_s, "--type", cons_type_s, (void *)NULL);
987 out:
988 libxl__free_all(&gc);
989 return ERROR_FAIL;
990 }
992 int libxl_primary_console_exec(libxl_ctx *ctx, uint32_t domid_vm)
993 {
994 uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm);
995 if (stubdomid)
996 return libxl_console_exec(ctx, stubdomid, 1, LIBXL_CONSTYPE_PV);
997 else {
998 if (libxl__domain_is_hvm(ctx, domid_vm))
999 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_SERIAL);
1000 else
1001 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_PV);
1005 int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
1007 libxl__gc gc = LIBXL_INIT_GC(ctx);
1008 const char *vnc_port, *vfb_back;
1009 const char *vnc_listen = NULL, *vnc_pass = NULL;
1010 int port = 0, autopass_fd = -1;
1011 char *vnc_bin, *args[] = {
1012 "vncviewer",
1013 NULL, /* hostname:display */
1014 NULL, /* -autopass */
1015 NULL,
1016 };
1018 vnc_port = libxl__xs_read(&gc, XBT_NULL,
1019 libxl__sprintf(&gc,
1020 "/local/domain/%d/console/vnc-port", domid));
1021 if ( vnc_port )
1022 port = atoi(vnc_port) - 5900;
1024 vfb_back = libxl__xs_read(&gc, XBT_NULL,
1025 libxl__sprintf(&gc,
1026 "/local/domain/%d/device/vfb/0/backend", domid));
1027 if ( vfb_back ) {
1028 vnc_listen = libxl__xs_read(&gc, XBT_NULL,
1029 libxl__sprintf(&gc,
1030 "/local/domain/%d/console/vnc-listen", domid));
1031 if ( autopass )
1032 vnc_pass = libxl__xs_read(&gc, XBT_NULL,
1033 libxl__sprintf(&gc,
1034 "/local/domain/%d/console/vnc-pass", domid));
1037 if ( NULL == vnc_listen )
1038 vnc_listen = "localhost";
1040 if ( (vnc_bin = getenv("VNCVIEWER")) )
1041 args[0] = vnc_bin;
1043 args[1] = libxl__sprintf(&gc, "%s:%d", vnc_listen, port);
1045 if ( vnc_pass ) {
1046 char tmpname[] = "/tmp/vncautopass.XXXXXX";
1047 autopass_fd = mkstemp(tmpname);
1048 if ( autopass_fd < 0 )
1049 goto skip_autopass;
1051 if ( unlink(tmpname) )
1052 /* should never happen */
1053 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unlink %s failed", tmpname);
1055 if ( libxl_write_exactly(ctx, autopass_fd, vnc_pass, strlen(vnc_pass),
1056 tmpname, "vnc password") ) {
1057 do { close(autopass_fd); } while(errno == EINTR);
1058 goto skip_autopass;
1061 args[2] = "-autopass";
1064 skip_autopass:
1065 libxl__free_all(&gc);
1066 libxl__exec(autopass_fd, -1, -1, args[0], args);
1067 return 0;
1070 /******************************************************************************/
1072 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1074 libxl__gc gc = LIBXL_INIT_GC(ctx);
1075 flexarray_t *front;
1076 flexarray_t *back;
1077 char *backend_type;
1078 unsigned int boffset = 0;
1079 unsigned int foffset = 0;
1080 int devid;
1081 libxl__device device;
1082 int major, minor, rc;
1084 front = flexarray_make(16, 1);
1085 if (!front) {
1086 rc = ERROR_NOMEM;
1087 goto out;
1089 back = flexarray_make(16, 1);
1090 if (!back) {
1091 rc = ERROR_NOMEM;
1092 goto out_free;
1095 backend_type = libxl__device_disk_backend_type_of_phystype(disk->phystype);
1096 devid = libxl__device_disk_dev_number(disk->virtpath);
1097 if (devid==-1) {
1098 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
1099 " virtual disk identifier %s", disk->virtpath);
1100 rc = ERROR_INVAL;
1101 goto out_free;
1104 device.backend_devid = devid;
1105 device.backend_domid = disk->backend_domid;
1106 device.devid = devid;
1107 device.domid = disk->domid;
1108 device.kind = DEVICE_VBD;
1110 switch (disk->phystype) {
1111 case PHYSTYPE_PHY: {
1113 libxl__device_physdisk_major_minor(disk->physpath, &major, &minor);
1114 flexarray_set(back, boffset++, "physical-device");
1115 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor));
1117 flexarray_set(back, boffset++, "params");
1118 flexarray_set(back, boffset++, disk->physpath);
1120 device.backend_kind = DEVICE_VBD;
1121 break;
1123 case PHYSTYPE_FILE:
1124 /* let's pretend is tap:aio for the moment */
1125 disk->phystype = PHYSTYPE_AIO;
1126 case PHYSTYPE_AIO:
1127 case PHYSTYPE_QCOW:
1128 case PHYSTYPE_QCOW2:
1129 case PHYSTYPE_VHD:
1130 if (libxl__blktap_enabled(&gc)) {
1131 const char *dev = libxl__blktap_devpath(&gc,
1132 disk->physpath, disk->phystype);
1133 if (!dev) {
1134 rc = ERROR_FAIL;
1135 goto out_free;
1137 flexarray_set(back, boffset++, "tapdisk-params");
1138 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
1139 flexarray_set(back, boffset++, "params");
1140 flexarray_set(back, boffset++, libxl__strdup(&gc, dev));
1141 backend_type = "phy";
1142 libxl__device_physdisk_major_minor(dev, &major, &minor);
1143 flexarray_set(back, boffset++, "physical-device");
1144 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor));
1145 device.backend_kind = DEVICE_VBD;
1147 break;
1149 flexarray_set(back, boffset++, "params");
1150 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s",
1151 libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
1153 if (libxl__blktap_enabled(&gc))
1154 device.backend_kind = DEVICE_TAP;
1155 else
1156 device.backend_kind = DEVICE_QDISK;
1157 break;
1159 default:
1160 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
1161 rc = ERROR_INVAL;
1162 goto out_free;
1165 flexarray_set(back, boffset++, "frontend-id");
1166 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", disk->domid));
1167 flexarray_set(back, boffset++, "online");
1168 flexarray_set(back, boffset++, "1");
1169 flexarray_set(back, boffset++, "removable");
1170 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
1171 flexarray_set(back, boffset++, "bootable");
1172 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1173 flexarray_set(back, boffset++, "state");
1174 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1175 flexarray_set(back, boffset++, "dev");
1176 flexarray_set(back, boffset++, disk->virtpath);
1177 flexarray_set(back, boffset++, "type");
1178 flexarray_set(back, boffset++, backend_type);
1179 flexarray_set(back, boffset++, "mode");
1180 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1182 flexarray_set(front, foffset++, "backend-id");
1183 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", disk->backend_domid));
1184 flexarray_set(front, foffset++, "state");
1185 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
1186 flexarray_set(front, foffset++, "virtual-device");
1187 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", devid));
1188 flexarray_set(front, foffset++, "device-type");
1189 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1191 if (0 /* protocol != native*/) {
1192 flexarray_set(front, foffset++, "protocol");
1193 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1196 libxl__device_generic_add(ctx, &device,
1197 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1198 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1200 rc = 0;
1202 out_free:
1203 flexarray_free(back);
1204 flexarray_free(front);
1205 out:
1206 libxl__free_all(&gc);
1207 return rc;
1210 int libxl_device_disk_del(libxl_ctx *ctx,
1211 libxl_device_disk *disk, int wait)
1213 libxl__device device;
1214 int devid;
1216 devid = libxl__device_disk_dev_number(disk->virtpath);
1217 device.backend_domid = disk->backend_domid;
1218 device.backend_devid = devid;
1219 device.backend_kind =
1220 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1221 device.domid = disk->domid;
1222 device.devid = devid;
1223 device.kind = DEVICE_VBD;
1224 return libxl__device_del(ctx, &device, wait);
1227 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
1229 libxl__gc gc = LIBXL_INIT_GC(ctx);
1230 const char *dev = NULL;
1231 char *ret;
1232 int phystype = disk->phystype;
1233 switch (phystype) {
1234 case PHYSTYPE_PHY: {
1235 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1236 dev = disk->physpath;
1237 break;
1239 case PHYSTYPE_FILE:
1240 /* let's pretend is tap:aio for the moment */
1241 phystype = PHYSTYPE_AIO;
1242 case PHYSTYPE_AIO:
1243 case PHYSTYPE_QCOW:
1244 case PHYSTYPE_QCOW2:
1245 case PHYSTYPE_VHD:
1246 if (libxl__blktap_enabled(&gc))
1247 dev = libxl__blktap_devpath(&gc, disk->physpath, phystype);
1248 break;
1250 default:
1251 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1252 break;
1254 ret = strdup(dev);
1255 libxl__free_all(&gc);
1256 return ret;
1259 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1261 /* Nothing to do for PHYSTYPE_PHY. */
1263 /*
1264 * For other device types assume that the blktap2 process is
1265 * needed by the soon to be started domain and do nothing.
1266 */
1268 return 0;
1271 /******************************************************************************/
1272 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1274 libxl__gc gc = LIBXL_INIT_GC(ctx);
1275 flexarray_t *front;
1276 flexarray_t *back;
1277 unsigned int boffset = 0;
1278 unsigned int foffset = 0;
1279 libxl__device device;
1280 char *dompath, **l;
1281 unsigned int nb, rc;
1283 front = flexarray_make(16, 1);
1284 if (!front) {
1285 rc = ERROR_NOMEM;
1286 goto out;
1288 back = flexarray_make(16, 1);
1289 if (!back) {
1290 rc = ERROR_NOMEM;
1291 goto out_free;
1294 if (nic->devid == -1) {
1295 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1296 rc = ERROR_FAIL;
1297 goto out_free;
1299 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1300 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb))) {
1301 nic->devid = 0;
1302 } else {
1303 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1307 device.backend_devid = nic->devid;
1308 device.backend_domid = nic->backend_domid;
1309 device.backend_kind = DEVICE_VIF;
1310 device.devid = nic->devid;
1311 device.domid = nic->domid;
1312 device.kind = DEVICE_VIF;
1314 flexarray_set(back, boffset++, "frontend-id");
1315 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->domid));
1316 flexarray_set(back, boffset++, "online");
1317 flexarray_set(back, boffset++, "1");
1318 flexarray_set(back, boffset++, "state");
1319 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1320 flexarray_set(back, boffset++, "script");
1321 flexarray_set(back, boffset++, nic->script);
1322 flexarray_set(back, boffset++, "mac");
1323 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1324 nic->mac[0], nic->mac[1], nic->mac[2],
1325 nic->mac[3], nic->mac[4], nic->mac[5]));
1326 flexarray_set(back, boffset++, "bridge");
1327 flexarray_set(back, boffset++, libxl__strdup(&gc, nic->bridge));
1328 flexarray_set(back, boffset++, "handle");
1329 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->devid));
1331 flexarray_set(front, foffset++, "backend-id");
1332 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->backend_domid));
1333 flexarray_set(front, foffset++, "state");
1334 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
1335 flexarray_set(front, foffset++, "handle");
1336 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->devid));
1337 flexarray_set(front, foffset++, "mac");
1338 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1339 nic->mac[0], nic->mac[1], nic->mac[2],
1340 nic->mac[3], nic->mac[4], nic->mac[5]));
1341 if (0 /* protocol != native*/) {
1342 flexarray_set(front, foffset++, "protocol");
1343 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1346 libxl__device_generic_add(ctx, &device,
1347 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1348 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1350 /* FIXME: wait for plug */
1351 rc = 0;
1352 out_free:
1353 flexarray_free(back);
1354 flexarray_free(front);
1355 out:
1356 libxl__free_all(&gc);
1357 return rc;
1360 int libxl_device_nic_del(libxl_ctx *ctx,
1361 libxl_device_nic *nic, int wait)
1363 libxl__device device;
1365 device.backend_devid = nic->devid;
1366 device.backend_domid = nic->backend_domid;
1367 device.backend_kind = DEVICE_VIF;
1368 device.devid = nic->devid;
1369 device.domid = nic->domid;
1370 device.kind = DEVICE_VIF;
1372 return libxl__device_del(ctx, &device, wait);
1375 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1377 libxl__gc gc = LIBXL_INIT_GC(ctx);
1378 char *dompath, *nic_path_fe;
1379 char **l, **list;
1380 char *val, *tok;
1381 unsigned int nb_nics, i;
1382 libxl_nicinfo *res, *nics;
1384 dompath = libxl__xs_get_dompath(&gc, domid);
1385 if (!dompath)
1386 goto err;
1387 list = l = libxl__xs_directory(&gc, XBT_NULL,
1388 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb_nics);
1389 if (!l)
1390 goto err;
1391 nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
1392 if (!res)
1393 goto err;
1394 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
1395 nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
1397 nics->backend = xs_read(ctx->xsh, XBT_NULL,
1398 libxl__sprintf(&gc, "%s/backend", nic_path_fe), NULL);
1399 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", nic_path_fe));
1400 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
1402 nics->devid = strtoul(*l, NULL, 10);
1403 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", nic_path_fe));
1404 nics->state = val ? strtoul(val, NULL, 10) : -1;
1405 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", nic_path_fe));
1406 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1407 ++i, tok = strtok(NULL, ":")) {
1408 nics->mac[i] = strtoul(tok, NULL, 16);
1410 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", nic_path_fe));
1411 nics->evtch = val ? strtol(val, NULL, 10) : -1;
1412 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", nic_path_fe));
1413 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
1414 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", nic_path_fe));
1415 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
1416 nics->frontend = xs_read(ctx->xsh, XBT_NULL,
1417 libxl__sprintf(&gc, "%s/frontend", nics->backend), NULL);
1418 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", nics->backend));
1419 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1420 nics->script = xs_read(ctx->xsh, XBT_NULL,
1421 libxl__sprintf(&gc, "%s/script", nics->backend), NULL);
1424 libxl__free_all(&gc);
1425 return res;
1426 err:
1427 libxl__free_all(&gc);
1428 return NULL;
1431 /******************************************************************************/
1432 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
1434 libxl__gc gc = LIBXL_INIT_GC(ctx);
1435 flexarray_t *front, *back;
1436 unsigned int boffset = 0, foffset = 0;
1437 libxl__device device;
1438 char *dompath, *dom, **l;
1439 unsigned int nb;
1440 int rc;
1442 front = flexarray_make(16, 1);
1443 if (!front) {
1444 rc = ERROR_NOMEM;
1445 goto err;
1447 back = flexarray_make(16, 1);
1448 if (!back) {
1449 rc = ERROR_NOMEM;
1450 goto err_free;
1453 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1454 rc = ERROR_FAIL;
1455 goto err_free;
1457 dom = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/name", dompath));
1459 if (net2->devid == -1) {
1460 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1461 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb))) {
1462 net2->devid = 0;
1463 } else {
1464 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1468 device.backend_devid = net2->devid;
1469 device.backend_domid = net2->backend_domid;
1470 device.backend_kind = DEVICE_VIF2;
1471 device.devid = net2->devid;
1472 device.domid = net2->domid;
1473 device.kind = DEVICE_VIF2;
1475 flexarray_set(back, boffset++, "domain");
1476 flexarray_set(back, boffset++, dom);
1477 flexarray_set(back, boffset++, "frontend-id");
1478 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->domid));
1480 flexarray_set(back, boffset++, "local-trusted");
1481 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
1482 flexarray_set(back, boffset++, "mac");
1483 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1484 net2->back_mac[0], net2->back_mac[1],
1485 net2->back_mac[2], net2->back_mac[3],
1486 net2->back_mac[4], net2->back_mac[5]));
1488 flexarray_set(back, boffset++, "remote-trusted");
1489 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->trusted));
1490 flexarray_set(back, boffset++, "remote-mac");
1491 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1492 net2->front_mac[0], net2->front_mac[1],
1493 net2->front_mac[2], net2->front_mac[3],
1494 net2->front_mac[4], net2->front_mac[5]));
1496 flexarray_set(back, boffset++, "max-bypasses");
1497 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->max_bypasses));
1498 flexarray_set(back, boffset++, "filter-mac");
1499 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1500 flexarray_set(back, boffset++, "handle");
1501 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->devid));
1502 flexarray_set(back, boffset++, "online");
1503 flexarray_set(back, boffset++, "1");
1504 flexarray_set(back, boffset++, "state");
1505 flexarray_set(back, boffset++, "1");
1507 flexarray_set(front, foffset++, "backend-id");
1508 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->backend_domid));
1510 flexarray_set(front, foffset++, "local-trusted");
1511 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->trusted));
1512 flexarray_set(front, foffset++, "mac");
1513 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1514 net2->front_mac[0], net2->front_mac[1],
1515 net2->front_mac[2], net2->front_mac[3],
1516 net2->front_mac[4], net2->front_mac[5]));
1518 flexarray_set(front, foffset++, "remote-trusted");
1519 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
1520 flexarray_set(front, foffset++, "remote-mac");
1521 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1522 net2->back_mac[0], net2->back_mac[1],
1523 net2->back_mac[2], net2->back_mac[3],
1524 net2->back_mac[4], net2->back_mac[5]));
1526 flexarray_set(front, foffset++, "filter-mac");
1527 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1528 flexarray_set(front, foffset++, "state");
1529 flexarray_set(front, foffset++, "1");
1531 libxl__device_generic_add(ctx, &device,
1532 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1533 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1535 /* FIXME: wait for plug */
1536 rc = 0;
1537 err_free:
1538 flexarray_free(back);
1539 flexarray_free(front);
1540 err:
1541 libxl__free_all(&gc);
1542 return rc;
1545 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1547 libxl__gc gc = LIBXL_INIT_GC(ctx);
1548 char *dompath, *net2_path_fe;
1549 char **l;
1550 char *val, *tok;
1551 unsigned int nb_net2s, i;
1552 libxl_net2info *res, *net2s;
1554 dompath = libxl__xs_get_dompath(&gc, domid);
1555 if (!dompath)
1556 goto err;
1557 l = libxl__xs_directory(&gc, XBT_NULL,
1558 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb_net2s);
1559 if (!l)
1560 goto err;
1561 res = calloc(nb_net2s, sizeof (libxl_net2info));
1562 if (!res)
1563 goto err;
1564 net2s = res;
1565 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
1566 net2_path_fe = libxl__sprintf(&gc, "%s/device/vif2/%s", dompath, *l);
1568 net2s->backend = libxl__xs_read(&gc, XBT_NULL,
1569 libxl__sprintf(&gc, "%s/backend", net2_path_fe));
1570 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", net2_path_fe));
1571 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
1573 net2s->devid = strtoul(*l, NULL, 10);
1574 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", net2_path_fe));
1575 net2s->state = val ? strtoul(val, NULL, 10) : -1;
1577 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", net2_path_fe));
1578 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1579 ++i, tok = strtok(NULL, ":")) {
1580 net2s->mac[i] = strtoul(tok, NULL, 16);
1582 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-trusted", net2_path_fe));
1583 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
1585 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-mac", net2_path_fe));
1586 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1587 ++i, tok = strtok(NULL, ":")) {
1588 net2s->back_mac[i] = strtoul(tok, NULL, 16);
1590 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/filter-mac", net2_path_fe));
1591 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
1593 net2s->frontend = libxl__xs_read(&gc, XBT_NULL,
1594 libxl__sprintf(&gc, "%s/frontend", net2s->backend));
1595 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", net2s->backend));
1596 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1599 libxl__free_all(&gc);
1600 return res;
1601 err:
1602 libxl__free_all(&gc);
1603 return NULL;
1606 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
1608 libxl__device device;
1610 device.backend_devid = net2->devid;
1611 device.backend_domid = net2->backend_domid;
1612 device.backend_kind = DEVICE_VIF2;
1613 device.devid = net2->devid;
1614 device.domid = net2->domid;
1615 device.kind = DEVICE_VIF2;
1617 return libxl__device_del(ctx, &device, wait);
1621 /******************************************************************************/
1622 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1624 libxl__gc gc = LIBXL_INIT_GC(ctx);
1625 flexarray_t *front;
1626 flexarray_t *back;
1627 unsigned int boffset = 0;
1628 unsigned int foffset = 0;
1629 libxl__device device;
1630 int rc;
1632 front = flexarray_make(16, 1);
1633 if (!front) {
1634 rc = ERROR_NOMEM;
1635 goto out;
1637 back = flexarray_make(16, 1);
1638 if (!back) {
1639 rc = ERROR_NOMEM;
1640 goto out_free;
1643 device.backend_devid = console->devid;
1644 device.backend_domid = console->backend_domid;
1645 device.backend_kind = DEVICE_CONSOLE;
1646 device.devid = console->devid;
1647 device.domid = console->domid;
1648 device.kind = DEVICE_CONSOLE;
1650 flexarray_set(back, boffset++, "frontend-id");
1651 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", console->domid));
1652 flexarray_set(back, boffset++, "online");
1653 flexarray_set(back, boffset++, "1");
1654 flexarray_set(back, boffset++, "state");
1655 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1656 flexarray_set(back, boffset++, "domain");
1657 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
1658 flexarray_set(back, boffset++, "protocol");
1659 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
1661 flexarray_set(front, foffset++, "backend-id");
1662 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", console->backend_domid));
1663 flexarray_set(front, foffset++, "limit");
1664 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
1665 flexarray_set(front, foffset++, "type");
1666 if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
1667 flexarray_set(front, foffset++, "xenconsoled");
1668 else
1669 flexarray_set(front, foffset++, "ioemu");
1670 flexarray_set(front, foffset++, "output");
1671 flexarray_set(front, foffset++, console->output);
1673 if (device.devid == 0) {
1674 if (console->build_state == NULL) {
1675 rc = ERROR_INVAL;
1676 goto out_free;
1678 flexarray_set(front, foffset++, "port");
1679 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
1680 flexarray_set(front, foffset++, "ring-ref");
1681 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
1682 } else {
1683 flexarray_set(front, foffset++, "state");
1684 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
1685 flexarray_set(front, foffset++, "protocol");
1686 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
1689 libxl__device_generic_add(ctx, &device,
1690 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1691 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1692 rc = 0;
1693 out_free:
1694 flexarray_free(back);
1695 flexarray_free(front);
1696 out:
1697 libxl__free_all(&gc);
1698 return rc;
1701 /******************************************************************************/
1702 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1704 libxl__gc gc = LIBXL_INIT_GC(ctx);
1705 flexarray_t *front;
1706 flexarray_t *back;
1707 unsigned int boffset = 0;
1708 unsigned int foffset = 0;
1709 libxl__device device;
1710 int rc;
1712 front = flexarray_make(16, 1);
1713 if (!front) {
1714 rc = ERROR_NOMEM;
1715 goto out;
1717 back = flexarray_make(16, 1);
1718 if (!back) {
1719 rc = ERROR_NOMEM;
1720 goto out_free;
1723 device.backend_devid = vkb->devid;
1724 device.backend_domid = vkb->backend_domid;
1725 device.backend_kind = DEVICE_VKBD;
1726 device.devid = vkb->devid;
1727 device.domid = vkb->domid;
1728 device.kind = DEVICE_VKBD;
1730 flexarray_set(back, boffset++, "frontend-id");
1731 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vkb->domid));
1732 flexarray_set(back, boffset++, "online");
1733 flexarray_set(back, boffset++, "1");
1734 flexarray_set(back, boffset++, "state");
1735 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1736 flexarray_set(back, boffset++, "domain");
1737 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
1739 flexarray_set(front, foffset++, "backend-id");
1740 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vkb->backend_domid));
1741 flexarray_set(front, foffset++, "state");
1742 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
1744 libxl__device_generic_add(ctx, &device,
1745 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1746 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1747 rc = 0;
1748 out_free:
1749 flexarray_free(back);
1750 flexarray_free(front);
1751 out:
1752 libxl__free_all(&gc);
1753 return rc;
1756 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1758 return ERROR_NI;
1761 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1763 return ERROR_NI;
1766 static unsigned int libxl_append_disk_list_of_type(libxl_ctx *ctx,
1767 uint32_t domid,
1768 const char *type,
1769 libxl_device_disk **disks,
1770 unsigned int *ndisks)
1772 libxl__gc gc = LIBXL_INIT_GC(ctx);
1773 char *be_path = NULL;
1774 char **dir = NULL;
1775 unsigned int n = 0, len = 0;
1776 libxl_device_disk *pdisk = NULL, *pdisk_end = NULL;
1777 char *physpath_tmp = NULL;
1779 be_path = libxl__sprintf(&gc, "%s/backend/%s/%d",
1780 libxl__xs_get_dompath(&gc, 0), type, domid);
1781 dir = libxl__xs_directory(&gc, XBT_NULL, be_path, &n);
1782 if (dir) {
1783 *disks = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
1784 pdisk = *disks + *ndisks;
1785 *ndisks += n;
1786 pdisk_end = *disks + *ndisks;
1787 for (; pdisk < pdisk_end; pdisk++, dir++) {
1788 pdisk->backend_domid = 0;
1789 pdisk->domid = domid;
1790 physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path, *dir), &len);
1791 if (strchr(physpath_tmp, ':')) {
1792 pdisk->physpath = strdup(strchr(physpath_tmp, ':') + 1);
1793 free(physpath_tmp);
1794 } else {
1795 pdisk->physpath = physpath_tmp;
1797 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path, *dir)), &(pdisk->phystype));
1798 pdisk->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path, *dir), &len);
1799 pdisk->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path, *dir)));
1800 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path, *dir)), "w"))
1801 pdisk->readwrite = 1;
1802 else
1803 pdisk->readwrite = 0;
1804 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))));
1805 pdisk->is_cdrom = !strcmp(type, "cdrom");
1809 libxl__free_all(&gc);
1810 return n;
1813 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
1815 libxl_device_disk *disks = NULL;
1816 unsigned int ndisks = 0;
1818 *num = libxl_append_disk_list_of_type(ctx, domid, "vbd", &disks, &ndisks);
1819 *num += libxl_append_disk_list_of_type(ctx, domid, "tap", &disks, &ndisks);
1820 *num += libxl_append_disk_list_of_type(ctx, domid, "qdisk", &disks, &ndisks);
1822 return disks;
1825 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
1826 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
1828 libxl__gc gc = LIBXL_INIT_GC(ctx);
1829 char *dompath, *diskpath;
1830 char *val;
1832 dompath = libxl__xs_get_dompath(&gc, domid);
1833 diskinfo->devid = libxl__device_disk_dev_number(disk->virtpath);
1835 /* tap devices entries in xenstore are written as vbd devices. */
1836 diskpath = libxl__sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
1837 diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
1838 libxl__sprintf(&gc, "%s/backend", diskpath), NULL);
1839 if (!diskinfo->backend) {
1840 libxl__free_all(&gc);
1841 return ERROR_FAIL;
1843 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", diskpath));
1844 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
1845 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", diskpath));
1846 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
1847 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", diskpath));
1848 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
1849 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/ring-ref", diskpath));
1850 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
1851 diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
1852 libxl__sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
1853 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", diskinfo->backend));
1854 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1856 libxl__free_all(&gc);
1857 return 0;
1860 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1862 int num, i;
1863 uint32_t stubdomid;
1864 libxl_device_disk *disks;
1865 int ret = ERROR_FAIL;
1867 if (!disk->physpath) {
1868 disk->physpath = "";
1869 disk->phystype = PHYSTYPE_PHY;
1871 disks = libxl_device_disk_list(ctx, domid, &num);
1872 for (i = 0; i < num; i++) {
1873 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1874 /* found */
1875 break;
1877 if (i == num) {
1878 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual device not found");
1879 goto out;
1882 ret = 0;
1884 libxl_device_disk_del(ctx, disks + i, 1);
1885 libxl_device_disk_add(ctx, domid, disk);
1886 stubdomid = libxl_get_stubdom_id(ctx, domid);
1887 if (stubdomid) {
1888 disks[i].domid = stubdomid;
1889 libxl_device_disk_del(ctx, disks + i, 1);
1890 disk->domid = stubdomid;
1891 libxl_device_disk_add(ctx, stubdomid, disk);
1892 disk->domid = domid;
1894 out:
1895 for (i = 0; i < num; i++)
1896 libxl_device_disk_destroy(&disks[i]);
1897 free(disks);
1898 return ret;
1901 /******************************************************************************/
1902 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1904 libxl__gc gc = LIBXL_INIT_GC(ctx);
1905 flexarray_t *front;
1906 flexarray_t *back;
1907 unsigned int boffset = 0;
1908 unsigned int foffset = 0;
1909 libxl__device device;
1910 int rc;
1912 front = flexarray_make(16, 1);
1913 if (!front) {
1914 rc = ERROR_NOMEM;
1915 goto out;
1917 back = flexarray_make(16, 1);
1918 if (!back) {
1919 rc = ERROR_NOMEM;
1920 goto out_free;
1923 device.backend_devid = vfb->devid;
1924 device.backend_domid = vfb->backend_domid;
1925 device.backend_kind = DEVICE_VFB;
1926 device.devid = vfb->devid;
1927 device.domid = vfb->domid;
1928 device.kind = DEVICE_VFB;
1930 flexarray_set(back, boffset++, "frontend-id");
1931 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->domid));
1932 flexarray_set(back, boffset++, "online");
1933 flexarray_set(back, boffset++, "1");
1934 flexarray_set(back, boffset++, "state");
1935 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1936 flexarray_set(back, boffset++, "domain");
1937 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
1938 flexarray_set(back, boffset++, "vnc");
1939 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vnc));
1940 flexarray_set(back, boffset++, "vnclisten");
1941 flexarray_set(back, boffset++, vfb->vnclisten);
1942 flexarray_set(back, boffset++, "vncpasswd");
1943 flexarray_set(back, boffset++, vfb->vncpasswd);
1944 flexarray_set(back, boffset++, "vncdisplay");
1945 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncdisplay));
1946 flexarray_set(back, boffset++, "vncunused");
1947 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncunused));
1948 flexarray_set(back, boffset++, "sdl");
1949 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->sdl));
1950 flexarray_set(back, boffset++, "opengl");
1951 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->opengl));
1952 if (vfb->xauthority) {
1953 flexarray_set(back, boffset++, "xauthority");
1954 flexarray_set(back, boffset++, vfb->xauthority);
1956 if (vfb->display) {
1957 flexarray_set(back, boffset++, "display");
1958 flexarray_set(back, boffset++, vfb->display);
1961 flexarray_set(front, foffset++, "backend-id");
1962 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vfb->backend_domid));
1963 flexarray_set(front, foffset++, "state");
1964 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
1966 libxl__device_generic_add(ctx, &device,
1967 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1968 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1969 rc = 0;
1970 out_free:
1971 flexarray_free(front);
1972 flexarray_free(back);
1973 out:
1974 libxl__free_all(&gc);
1975 return rc;
1978 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1980 return ERROR_NI;
1983 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1985 return ERROR_NI;
1988 /******************************************************************************/
1990 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
1992 libxl__gc gc = LIBXL_INIT_GC(ctx);
1993 char *mem, *endptr;
1994 uint32_t memorykb;
1995 char *dompath = libxl__xs_get_dompath(&gc, domid);
1996 int rc = 1;
1998 mem = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/memory/target", dompath));
1999 if (!mem) {
2000 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
2001 goto out;
2003 memorykb = strtoul(mem, &endptr, 10);
2004 if (*endptr != '\0') {
2005 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
2006 goto out;
2009 if (max_memkb < memorykb) {
2010 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
2011 goto out;
2014 rc = 0;
2015 out:
2016 libxl__free_all(&gc);
2017 return rc;
2020 static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb)
2022 int rc;
2023 libxl_dominfo info;
2024 libxl_physinfo physinfo;
2025 char *target = NULL, *staticmax = NULL, *freememslack = NULL, *endptr = NULL;
2026 char *target_path = "/local/domain/0/memory/target";
2027 char *max_path = "/local/domain/0/memory/static-max";
2028 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2029 xs_transaction_t t;
2030 libxl_ctx *ctx = libxl__gc_owner(gc);
2031 uint32_t free_mem_slack_kb = 0;
2033 retry_transaction:
2034 t = xs_transaction_start(ctx->xsh);
2036 target = libxl__xs_read(gc, t, target_path);
2037 staticmax = libxl__xs_read(gc, t, max_path);
2038 freememslack = libxl__xs_read(gc, t, free_mem_slack_path);
2039 if (target && staticmax && freememslack) {
2040 rc = 0;
2041 goto out;
2044 if (target) {
2045 *target_memkb = strtoul(target, &endptr, 10);
2046 if (*endptr != '\0') {
2047 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2048 "invalid memory target %s from %s\n", target, target_path);
2049 rc = ERROR_FAIL;
2050 goto out;
2054 rc = libxl_domain_info(ctx, &info, 0);
2055 if (rc < 0)
2056 goto out;
2058 rc = libxl_get_physinfo(ctx, &physinfo);
2059 if (rc < 0)
2060 goto out;
2062 if (target == NULL) {
2063 libxl__xs_write(gc, t, target_path, "%"PRIu32,
2064 (uint32_t) info.current_memkb);
2065 *target_memkb = (uint32_t) info.current_memkb;
2067 if (staticmax == NULL)
2068 libxl__xs_write(gc, t, max_path, "%"PRIu32,
2069 (uint32_t) info.max_memkb);
2071 if (freememslack == NULL) {
2072 free_mem_slack_kb = (uint32_t) (PAGE_TO_MEMKB(physinfo.total_pages) -
2073 info.current_memkb);
2074 /* From empirical measurements the free_mem_slack shouldn't be more
2075 * than 15% of the total memory present on the system. */
2076 if (free_mem_slack_kb > PAGE_TO_MEMKB(physinfo.total_pages) * 0.15)
2077 free_mem_slack_kb = PAGE_TO_MEMKB(physinfo.total_pages) * 0.15;
2078 libxl__xs_write(gc, t, free_mem_slack_path, "%"PRIu32, free_mem_slack_kb);
2080 rc = 0;
2082 out:
2083 if (!xs_transaction_end(ctx->xsh, t, 0)) {
2084 if (errno == EAGAIN)
2085 goto retry_transaction;
2086 else
2087 rc = ERROR_FAIL;
2091 return rc;
2094 /* returns how much memory should be left free in the system */
2095 static int libxl__get_free_memory_slack(libxl__gc *gc, uint32_t *free_mem_slack)
2097 int rc;
2098 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2099 char *free_mem_slack_s, *endptr;
2100 uint32_t target_memkb;
2102 retry:
2103 free_mem_slack_s = libxl__xs_read(gc, XBT_NULL, free_mem_slack_path);
2104 if (!free_mem_slack_s) {
2105 rc = libxl__fill_dom0_memory_info(gc, &target_memkb);
2106 if (rc < 0)
2107 return rc;
2108 goto retry;
2109 } else {
2110 *free_mem_slack = strtoul(free_mem_slack_s, &endptr, 10);
2111 if (*endptr != '\0') {
2112 LIBXL__LOG_ERRNO(gc->owner, LIBXL__LOG_ERROR,
2113 "invalid free_mem_slack %s from %s\n",
2114 free_mem_slack_s, free_mem_slack_path);
2115 return ERROR_FAIL;
2118 return 0;
2121 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
2122 int32_t target_memkb, int relative, int enforce)
2124 libxl__gc gc = LIBXL_INIT_GC(ctx);
2125 int rc = 1, abort = 0;
2126 uint32_t memorykb = 0, videoram = 0;
2127 uint32_t current_target_memkb = 0, new_target_memkb = 0;
2128 char *memmax, *endptr, *videoram_s = NULL, *target = NULL;
2129 char *dompath = libxl__xs_get_dompath(&gc, domid);
2130 xc_domaininfo_t info;
2131 libxl_dominfo ptr;
2132 char *uuid;
2133 xs_transaction_t t;
2135 retry_transaction:
2136 t = xs_transaction_start(ctx->xsh);
2138 target = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2139 "%s/memory/target", dompath));
2140 if (!target && !domid) {
2141 xs_transaction_end(ctx->xsh, t, 1);
2142 rc = libxl__fill_dom0_memory_info(&gc, &current_target_memkb);
2143 if (rc < 0) {
2144 abort = 1;
2145 goto out;
2147 goto retry_transaction;
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 abort = 1;
2153 goto out;
2154 } else {
2155 current_target_memkb = strtoul(target, &endptr, 10);
2156 if (*endptr != '\0') {
2157 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2158 "invalid memory target %s from %s/memory/target\n",
2159 target, dompath);
2160 abort = 1;
2161 goto out;
2164 memmax = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2165 "%s/memory/static-max", dompath));
2166 if (!memmax) {
2167 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2168 "cannot get memory info from %s/memory/static-max\n",
2169 dompath);
2170 abort = 1;
2171 goto out;
2173 memorykb = strtoul(memmax, &endptr, 10);
2174 if (*endptr != '\0') {
2175 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2176 "invalid max memory %s from %s/memory/static-max\n",
2177 memmax, dompath);
2178 abort = 1;
2179 goto out;
2182 if (relative)
2183 new_target_memkb = current_target_memkb + target_memkb;
2184 else
2185 new_target_memkb = target_memkb;
2186 if (new_target_memkb > memorykb) {
2187 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2188 "memory_dynamic_max must be less than or equal to"
2189 " memory_static_max\n");
2190 abort = 1;
2191 goto out;
2194 if (!domid && new_target_memkb < LIBXL_MIN_DOM0_MEM) {
2195 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2196 "new target %d for dom0 is below the minimum threshold\n",
2197 new_target_memkb);
2198 abort = 1;
2199 goto out;
2201 videoram_s = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2202 "%s/memory/videoram", dompath));
2203 videoram = videoram_s ? atoi(videoram_s) : 0;
2205 if (enforce) {
2206 memorykb = new_target_memkb;
2207 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
2208 LIBXL_MAXMEM_CONSTANT);
2209 if (rc != 0) {
2210 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2211 "xc_domain_setmaxmem domid=%d memkb=%d failed "
2212 "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
2213 abort = 1;
2214 goto out;
2218 new_target_memkb -= videoram;
2219 rc = xc_domain_set_pod_target(ctx->xch, domid,
2220 new_target_memkb / 4, NULL, NULL, NULL);
2221 if (rc != 0) {
2222 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2223 "xc_domain_set_pod_target domid=%d, memkb=%d "
2224 "failed rc=%d\n", domid, new_target_memkb / 4,
2225 rc);
2226 abort = 1;
2227 goto out;
2230 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/memory/target",
2231 dompath), "%"PRIu32, new_target_memkb);
2232 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2233 if (rc != 1 || info.domain != domid) {
2234 abort = 1;
2235 goto out;
2237 xcinfo2xlinfo(&info, &ptr);
2238 uuid = libxl__uuid2string(&gc, ptr.uuid);
2239 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "/vm/%s/memory", uuid),
2240 "%"PRIu32, new_target_memkb / 1024);
2242 out:
2243 if (!xs_transaction_end(ctx->xsh, t, abort) && !abort)
2244 if (errno == EAGAIN)
2245 goto retry_transaction;
2247 libxl__free_all(&gc);
2248 return rc;
2251 int libxl_get_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t *out_target)
2253 libxl__gc gc = LIBXL_INIT_GC(ctx);
2254 int rc = 1;
2255 char *target = NULL, *endptr = NULL;
2256 char *dompath = libxl__xs_get_dompath(&gc, domid);
2257 uint32_t target_memkb;
2259 target = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc,
2260 "%s/memory/target", dompath));
2261 if (!target && !domid) {
2262 rc = libxl__fill_dom0_memory_info(&gc, &target_memkb);
2263 if (rc < 0)
2264 goto out;
2265 } else if (!target) {
2266 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2267 "cannot get target memory info from %s/memory/target\n",
2268 dompath);
2269 goto out;
2270 } else {
2271 target_memkb = strtoul(target, &endptr, 10);
2272 if (*endptr != '\0') {
2273 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2274 "invalid memory target %s from %s/memory/target\n",
2275 target, dompath);
2276 goto out;
2279 *out_target = target_memkb;
2280 rc = 0;
2282 out:
2283 libxl__free_all(&gc);
2284 return rc;
2287 int libxl_domain_need_memory(libxl_ctx *ctx, libxl_domain_build_info *b_info,
2288 libxl_device_model_info *dm_info, uint32_t *need_memkb)
2290 *need_memkb = b_info->target_memkb;
2291 if (b_info->hvm) {
2292 *need_memkb += b_info->shadow_memkb + LIBXL_HVM_EXTRA_MEMORY;
2293 if (strstr(dm_info->device_model, "stubdom-dm"))
2294 *need_memkb += 32 * 1024;
2295 } else
2296 *need_memkb += b_info->shadow_memkb + LIBXL_PV_EXTRA_MEMORY;
2297 if (*need_memkb % (2 * 1024))
2298 *need_memkb += (2 * 1024) - (*need_memkb % (2 * 1024));
2299 return 0;
2302 int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
2304 int rc = 0;
2305 libxl_physinfo info;
2306 uint32_t freemem_slack;
2307 libxl__gc gc = LIBXL_INIT_GC(ctx);
2309 rc = libxl_get_physinfo(ctx, &info);
2310 if (rc < 0)
2311 goto out;
2312 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2313 if (rc < 0)
2314 goto out;
2316 if ((info.free_pages + info.scrub_pages) * 4 > freemem_slack)
2317 *memkb = (info.free_pages + info.scrub_pages) * 4 - freemem_slack;
2318 else
2319 *memkb = 0;
2321 out:
2322 libxl__free_all(&gc);
2323 return rc;
2326 int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
2327 memory_kb, int wait_secs)
2329 int rc = 0;
2330 libxl_physinfo info;
2331 uint32_t freemem_slack;
2332 libxl__gc gc = LIBXL_INIT_GC(ctx);
2334 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2335 if (rc < 0)
2336 goto out;
2337 while (wait_secs > 0) {
2338 rc = libxl_get_physinfo(ctx, &info);
2339 if (rc < 0)
2340 goto out;
2341 if (info.free_pages * 4 - freemem_slack >= memory_kb) {
2342 rc = 0;
2343 goto out;
2345 wait_secs--;
2346 sleep(1);
2348 rc = ERROR_NOMEM;
2350 out:
2351 libxl__free_all(&gc);
2352 return rc;
2355 int libxl_wait_for_memory_target(libxl_ctx *ctx, uint32_t domid, int wait_secs)
2357 int rc = 0;
2358 uint32_t target_memkb = 0;
2359 libxl_dominfo info;
2361 do {
2362 wait_secs--;
2363 sleep(1);
2365 rc = libxl_get_memory_target(ctx, domid, &target_memkb);
2366 if (rc < 0)
2367 goto out;
2369 rc = libxl_domain_info(ctx, &info, domid);
2370 if (rc < 0)
2371 return rc;
2372 } while (wait_secs > 0 && info.current_memkb > target_memkb);
2374 if (info.current_memkb <= target_memkb)
2375 rc = 0;
2376 else
2377 rc = ERROR_FAIL;
2379 out:
2380 return 0;
2383 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
2385 int rc = -1;
2387 switch (button) {
2388 case POWER_BUTTON:
2389 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2390 break;
2391 case SLEEP_BUTTON:
2392 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2393 break;
2394 default:
2395 break;
2398 return rc;
2401 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
2403 xc_physinfo_t xcphysinfo = { 0 };
2404 int rc;
2406 rc = xc_physinfo(ctx->xch, &xcphysinfo);
2407 if (rc != 0) {
2408 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting physinfo");
2409 return ERROR_FAIL;
2411 physinfo->threads_per_core = xcphysinfo.threads_per_core;
2412 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
2413 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
2414 physinfo->nr_cpus = xcphysinfo.nr_cpus;
2415 physinfo->cpu_khz = xcphysinfo.cpu_khz;
2416 physinfo->total_pages = xcphysinfo.total_pages;
2417 physinfo->free_pages = xcphysinfo.free_pages;
2418 physinfo->scrub_pages = xcphysinfo.scrub_pages;
2419 physinfo->nr_nodes = xcphysinfo.nr_nodes;
2420 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
2421 physinfo->phys_cap = xcphysinfo.capabilities;
2423 return 0;
2426 int libxl_get_topologyinfo(libxl_ctx *ctx, libxl_topologyinfo *info)
2428 xc_topologyinfo_t tinfo;
2429 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_core_t, coremap);
2430 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_socket_t, socketmap);
2431 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_node_t, nodemap);
2432 int i;
2433 int rc = 0;
2435 rc += libxl_cpuarray_alloc(ctx, &info->coremap);
2436 rc += libxl_cpuarray_alloc(ctx, &info->socketmap);
2437 rc += libxl_cpuarray_alloc(ctx, &info->nodemap);
2438 if (rc)
2439 goto fail;
2441 coremap = xc_hypercall_buffer_alloc(ctx->xch, coremap, sizeof(*coremap) * info->coremap.entries);
2442 socketmap = xc_hypercall_buffer_alloc(ctx->xch, socketmap, sizeof(*socketmap) * info->socketmap.entries);
2443 nodemap = xc_hypercall_buffer_alloc(ctx->xch, nodemap, sizeof(*nodemap) * info->nodemap.entries);
2444 if ((coremap == NULL) || (socketmap == NULL) || (nodemap == NULL))
2445 goto fail;
2447 set_xen_guest_handle(tinfo.cpu_to_core, coremap);
2448 set_xen_guest_handle(tinfo.cpu_to_socket, socketmap);
2449 set_xen_guest_handle(tinfo.cpu_to_node, nodemap);
2450 tinfo.max_cpu_index = info->coremap.entries - 1;
2451 if (xc_topologyinfo(ctx->xch, &tinfo) != 0)
2452 goto fail;
2454 for (i = 0; i <= tinfo.max_cpu_index; i++) {
2455 if (i < info->coremap.entries)
2456 info->coremap.array[i] = (coremap[i] == INVALID_TOPOLOGY_ID) ?
2457 LIBXL_CPUARRAY_INVALID_ENTRY : coremap[i];
2458 if (i < info->socketmap.entries)
2459 info->socketmap.array[i] = (socketmap[i] == INVALID_TOPOLOGY_ID) ?
2460 LIBXL_CPUARRAY_INVALID_ENTRY : socketmap[i];
2461 if (i < info->nodemap.entries)
2462 info->nodemap.array[i] = (nodemap[i] == INVALID_TOPOLOGY_ID) ?
2463 LIBXL_CPUARRAY_INVALID_ENTRY : nodemap[i];
2466 xc_hypercall_buffer_free(ctx->xch, coremap);
2467 xc_hypercall_buffer_free(ctx->xch, socketmap);
2468 xc_hypercall_buffer_free(ctx->xch, nodemap);
2469 return 0;
2471 fail:
2472 xc_hypercall_buffer_free(ctx->xch, coremap);
2473 xc_hypercall_buffer_free(ctx->xch, socketmap);
2474 xc_hypercall_buffer_free(ctx->xch, nodemap);
2475 libxl_topologyinfo_destroy(info);
2476 return ERROR_FAIL;
2479 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
2481 union {
2482 xen_extraversion_t xen_extra;
2483 xen_compile_info_t xen_cc;
2484 xen_changeset_info_t xen_chgset;
2485 xen_capabilities_info_t xen_caps;
2486 xen_platform_parameters_t p_parms;
2487 xen_commandline_t xen_commandline;
2488 } u;
2489 long xen_version;
2490 libxl_version_info *info = &ctx->version_info;
2492 if (info->xen_version_extra != NULL)
2493 return info;
2495 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
2496 info->xen_version_major = xen_version >> 16;
2497 info->xen_version_minor = xen_version & 0xFF;
2499 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
2500 info->xen_version_extra = strdup(u.xen_extra);
2502 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
2503 info->compiler = strdup(u.xen_cc.compiler);
2504 info->compile_by = strdup(u.xen_cc.compile_by);
2505 info->compile_domain = strdup(u.xen_cc.compile_domain);
2506 info->compile_date = strdup(u.xen_cc.compile_date);
2508 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
2509 info->capabilities = strdup(u.xen_caps);
2511 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
2512 info->changeset = strdup(u.xen_chgset);
2514 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
2515 info->virt_start = u.p_parms.virt_start;
2517 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
2519 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
2520 info->commandline = strdup(u.xen_commandline);
2522 return info;
2525 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
2526 int *nb_vcpu, int *nrcpus)
2528 libxl_vcpuinfo *ptr, *ret;
2529 xc_domaininfo_t domaininfo;
2530 xc_vcpuinfo_t vcpuinfo;
2532 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2533 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist");
2534 return NULL;
2536 *nrcpus = libxl_get_max_cpus(ctx);
2537 ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
2538 if (!ptr) {
2539 return NULL;
2542 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
2543 if (libxl_cpumap_alloc(ctx, &ptr->cpumap)) {
2544 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap");
2545 return NULL;
2547 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
2548 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info");
2549 return NULL;
2551 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) {
2552 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity");
2553 return NULL;
2555 ptr->vcpuid = *nb_vcpu;
2556 ptr->cpu = vcpuinfo.cpu;
2557 ptr->online = !!vcpuinfo.online;
2558 ptr->blocked = !!vcpuinfo.blocked;
2559 ptr->running = !!vcpuinfo.running;
2560 ptr->vcpu_time = vcpuinfo.cpu_time;
2562 return ret;
2565 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
2566 libxl_cpumap *cpumap)
2568 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap->map)) {
2569 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting vcpu affinity");
2570 return ERROR_FAIL;
2572 return 0;
2575 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask)
2577 libxl__gc gc = LIBXL_INIT_GC(ctx);
2578 libxl_dominfo info;
2579 char *dompath;
2580 xs_transaction_t t;
2581 int i, rc = ERROR_FAIL;
2583 if (libxl_domain_info(ctx, &info, domid) < 0) {
2584 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2585 goto out;
2587 if (!(dompath = libxl__xs_get_dompath(&gc, domid)))
2588 goto out;
2590 retry_transaction:
2591 t = xs_transaction_start(ctx->xsh);
2592 for (i = 0; i <= info.vcpu_max_id; i++)
2593 libxl__xs_write(&gc, t,
2594 libxl__sprintf(&gc, "%s/cpu/%u/availability", dompath, i),
2595 "%s", ((1 << i) & bitmask) ? "online" : "offline");
2596 if (!xs_transaction_end(ctx->xsh, t, 0)) {
2597 if (errno == EAGAIN)
2598 goto retry_transaction;
2599 } else
2600 rc = 0;
2601 out:
2602 libxl__free_all(&gc);
2603 return rc;
2606 /*
2607 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
2608 */
2609 int libxl_get_sched_id(libxl_ctx *ctx)
2611 int sched, ret;
2613 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
2614 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2615 return ERROR_FAIL;
2617 return sched;
2620 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2622 struct xen_domctl_sched_credit sdom;
2623 int rc;
2625 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
2626 if (rc != 0) {
2627 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2628 return ERROR_FAIL;
2631 scinfo->weight = sdom.weight;
2632 scinfo->cap = sdom.cap;
2634 return 0;
2637 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2639 struct xen_domctl_sched_credit sdom;
2640 xc_domaininfo_t domaininfo;
2641 int rc;
2643 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
2644 if (rc < 0) {
2645 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2646 return ERROR_FAIL;
2648 if (rc != 1 || domaininfo.domain != domid)
2649 return ERROR_INVAL;
2652 if (scinfo->weight < 1 || scinfo->weight > 65535) {
2653 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2654 "Cpu weight out of range, valid values are within range from 1 to 65535");
2655 return ERROR_INVAL;
2658 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
2659 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2660 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
2661 ((domaininfo.max_vcpu_id + 1) * 100));
2662 return ERROR_INVAL;
2665 sdom.weight = scinfo->weight;
2666 sdom.cap = scinfo->cap;
2668 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
2669 if ( rc < 0 ) {
2670 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2671 return ERROR_FAIL;
2674 return 0;
2677 static int trigger_type_from_string(char *trigger_name)
2679 if (!strcmp(trigger_name, "nmi"))
2680 return XEN_DOMCTL_SENDTRIGGER_NMI;
2681 else if (!strcmp(trigger_name, "reset"))
2682 return XEN_DOMCTL_SENDTRIGGER_RESET;
2683 else if (!strcmp(trigger_name, "init"))
2684 return XEN_DOMCTL_SENDTRIGGER_INIT;
2685 else if (!strcmp(trigger_name, "power"))
2686 return XEN_DOMCTL_SENDTRIGGER_POWER;
2687 else if (!strcmp(trigger_name, "sleep"))
2688 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
2689 else
2690 return -1;
2693 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
2695 int rc = -1;
2696 int trigger_type = trigger_type_from_string(trigger_name);
2698 if (trigger_type == -1) {
2699 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2700 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
2701 return ERROR_INVAL;
2704 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
2705 if (rc != 0) {
2706 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2707 "Send trigger '%s' failed", trigger_name);
2708 return ERROR_FAIL;
2711 return 0;
2714 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
2716 libxl__gc gc = LIBXL_INIT_GC(ctx);
2717 char *dompath = libxl__xs_get_dompath(&gc, domid);
2719 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/control/sysrq", dompath), "%c", sysrq);
2721 libxl__free_all(&gc);
2722 return 0;
2725 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
2727 int ret;
2728 ret = xc_send_debug_keys(ctx->xch, keys);
2729 if ( ret < 0 ) {
2730 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "sending debug keys");
2731 return ERROR_FAIL;
2733 return 0;
2736 libxl_xen_console_reader *
2737 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
2739 libxl_xen_console_reader *cr;
2740 unsigned int size = 16384;
2741 char *buf = malloc(size);
2743 if (!buf) {
2744 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
2745 " size is %u", size);
2746 return NULL;
2749 cr = malloc(sizeof(libxl_xen_console_reader));
2750 if (!cr) {
2751 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc libxl_xen_console_reader");
2752 return NULL;
2755 memset(cr, 0, sizeof(libxl_xen_console_reader));
2756 cr->buffer = buf;
2757 cr->size = size;
2758 cr->count = size;
2759 cr->clear = clear;
2760 cr->incremental = 1;
2762 return cr;
2765 /* return values: *line_r
2766 * 1 success, whole line obtained from buffer non-0
2767 * 0 no more lines available right now 0
2768 * negative error code ERROR_* 0
2769 * On success *line_r is updated to point to a nul-terminated
2770 * string which is valid until the next call on the same console
2771 * reader. The libxl caller may overwrite parts of the string
2772 * if it wishes. */
2773 int libxl_xen_console_read_line(libxl_ctx *ctx,
2774 libxl_xen_console_reader *cr,
2775 char **line_r)
2777 int ret;
2779 memset(cr->buffer, 0, cr->size);
2780 ret = xc_readconsolering(ctx->xch, cr->buffer, &cr->count,
2781 cr->clear, cr->incremental, &cr->index);
2782 if (ret < 0) {
2783 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "reading console ring buffer");
2784 return ERROR_FAIL;
2786 if (!ret) {
2787 if (cr->count) {
2788 *line_r = cr->buffer;
2789 ret = 1;
2790 } else {
2791 *line_r = NULL;
2792 ret = 0;
2796 return ret;
2799 void libxl_xen_console_read_finish(libxl_ctx *ctx,
2800 libxl_xen_console_reader *cr)
2802 free(cr->buffer);
2803 free(cr);
2806 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
2808 libxl__gc gc = LIBXL_INIT_GC(ctx);
2809 char *dompath = libxl__xs_get_dompath(&gc, domid);
2810 char *vm_path, *start_time;
2811 uint32_t ret;
2813 vm_path = libxl__xs_read(
2814 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dompath));
2815 start_time = libxl__xs_read(
2816 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/start_time", vm_path));
2817 if (start_time == NULL) {
2818 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2819 "Can't get start time of domain '%d'", domid);
2820 ret = -1;
2821 }else{
2822 ret = strtoul(start_time, NULL, 10);
2824 libxl__free_all(&gc);
2825 return ret;
2828 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
2830 int rc;
2831 char _buf[32768];
2833 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
2834 0, _buf);
2835 if (rc < 0) {
2836 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2837 "Can not get tmem list");
2838 return NULL;
2841 return strdup(_buf);
2844 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
2846 int rc;
2848 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
2849 0, NULL);
2850 if (rc < 0) {
2851 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2852 "Can not freeze tmem pools");
2853 return ERROR_FAIL;
2856 return rc;
2859 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
2861 int rc;
2863 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
2864 0, NULL);
2865 if (rc < 0) {
2866 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2867 "Can not destroy tmem pools");
2868 return ERROR_FAIL;
2871 return rc;
2874 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
2876 int rc;
2878 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
2879 0, NULL);
2880 if (rc < 0) {
2881 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2882 "Can not thaw tmem pools");
2883 return ERROR_FAIL;
2886 return rc;
2889 static int32_t tmem_setop_from_string(char *set_name)
2891 if (!strcmp(set_name, "weight"))
2892 return TMEMC_SET_WEIGHT;
2893 else if (!strcmp(set_name, "cap"))
2894 return TMEMC_SET_CAP;
2895 else if (!strcmp(set_name, "compress"))
2896 return TMEMC_SET_COMPRESS;
2897 else
2898 return -1;
2901 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
2903 int rc;
2904 int32_t subop = tmem_setop_from_string(name);
2906 if (subop == -1) {
2907 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2908 "Invalid set, valid sets are <weight|cap|compress>");
2909 return ERROR_INVAL;
2911 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
2912 if (rc < 0) {
2913 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2914 "Can not set tmem %s", name);
2915 return ERROR_FAIL;
2918 return rc;
2921 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
2922 char* uuid, int auth)
2924 int rc;
2926 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
2927 if (rc < 0) {
2928 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2929 "Can not set tmem shared auth");
2930 return ERROR_FAIL;
2933 return rc;
2936 int libxl_tmem_freeable(libxl_ctx *ctx)
2938 int rc;
2940 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
2941 if (rc < 0) {
2942 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2943 "Can not get tmem freeable memory");
2944 return ERROR_FAIL;
2947 return rc;
2950 void libxl_file_reference_destroy(libxl_file_reference *f)
2952 libxl__file_reference_unmap(f);
2953 free(f->path);
2956 int libxl_get_freecpus(libxl_ctx *ctx, libxl_cpumap *cpumap)
2958 int ncpus;
2960 ncpus = libxl_get_max_cpus(ctx);
2961 if (ncpus == 0)
2962 return ERROR_FAIL;
2964 cpumap->map = xc_cpupool_freeinfo(ctx->xch);
2965 if (cpumap->map == NULL)
2966 return ERROR_FAIL;
2968 cpumap->size = (ncpus + 7) / 8;
2970 return 0;
2973 int libxl_create_cpupool(libxl_ctx *ctx, const char *name, int schedid,
2974 libxl_cpumap cpumap, libxl_uuid *uuid,
2975 uint32_t *poolid)
2977 libxl__gc gc = LIBXL_INIT_GC(ctx);
2978 int rc;
2979 int i;
2980 xs_transaction_t t;
2981 char *uuid_string;
2983 uuid_string = libxl__uuid2string(&gc, *uuid);
2984 if (!uuid_string) {
2985 libxl__free_all(&gc);
2986 return ERROR_NOMEM;
2989 rc = xc_cpupool_create(ctx->xch, poolid, schedid);
2990 if (rc) {
2991 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2992 "Could not create cpupool");
2993 libxl__free_all(&gc);
2994 return ERROR_FAIL;
2997 libxl_for_each_cpu(i, cpumap)
2998 if (libxl_cpumap_test(&cpumap, i)) {
2999 rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
3000 if (rc) {
3001 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3002 "Error moving cpu to cpupool");
3003 libxl_destroy_cpupool(ctx, *poolid);
3004 libxl__free_all(&gc);
3005 return ERROR_FAIL;
3009 for (;;) {
3010 t = xs_transaction_start(ctx->xsh);
3012 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/pool/%d", *poolid));
3013 libxl__xs_write(&gc, t,
3014 libxl__sprintf(&gc, "/local/pool/%d/uuid", *poolid),
3015 "%s", uuid_string);
3016 libxl__xs_write(&gc, t,
3017 libxl__sprintf(&gc, "/local/pool/%d/name", *poolid),
3018 "%s", name);
3020 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN)) {
3021 libxl__free_all(&gc);
3022 return 0;
3027 int libxl_destroy_cpupool(libxl_ctx *ctx, uint32_t poolid)
3029 libxl__gc gc = LIBXL_INIT_GC(ctx);
3030 int rc, i;
3031 xc_cpupoolinfo_t *info;
3032 xs_transaction_t t;
3033 libxl_cpumap cpumap;
3035 info = xc_cpupool_getinfo(ctx->xch, poolid);
3036 if (info == NULL) {
3037 libxl__free_all(&gc);
3038 return ERROR_NOMEM;
3041 rc = ERROR_INVAL;
3042 if ((info->cpupool_id != poolid) || (info->n_dom))
3043 goto out;
3045 rc = ERROR_NOMEM;
3046 if (libxl_cpumap_alloc(ctx, &cpumap))
3047 goto out;
3049 memcpy(cpumap.map, info->cpumap, cpumap.size);
3050 libxl_for_each_cpu(i, cpumap)
3051 if (libxl_cpumap_test(&cpumap, i)) {
3052 rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
3053 if (rc) {
3054 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3055 "Error removing cpu from cpupool");
3056 rc = ERROR_FAIL;
3057 goto out1;
3061 rc = xc_cpupool_destroy(ctx->xch, poolid);
3062 if (rc) {
3063 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "Could not destroy cpupool");
3064 rc = ERROR_FAIL;
3065 goto out1;
3068 for (;;) {
3069 t = xs_transaction_start(ctx->xsh);
3071 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/pool/%d", poolid));
3073 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3074 break;
3077 rc = 0;
3079 out1:
3080 libxl_cpumap_destroy(&cpumap);
3081 out:
3082 xc_cpupool_infofree(ctx->xch, info);
3083 libxl__free_all(&gc);
3085 return rc;
3088 int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid)
3090 libxl__gc gc = LIBXL_INIT_GC(ctx);
3091 xs_transaction_t t;
3092 xc_cpupoolinfo_t *info;
3093 int rc;
3095 info = xc_cpupool_getinfo(ctx->xch, poolid);
3096 if (info == NULL) {
3097 libxl__free_all(&gc);
3098 return ERROR_NOMEM;
3101 rc = ERROR_INVAL;
3102 if (info->cpupool_id != poolid)
3103 goto out;
3105 rc = 0;
3107 for (;;) {
3108 t = xs_transaction_start(ctx->xsh);
3110 libxl__xs_write(&gc, t,
3111 libxl__sprintf(&gc, "/local/pool/%d/name", poolid),
3112 "%s", name);
3114 if (xs_transaction_end(ctx->xsh, t, 0))
3115 break;
3117 if (errno == EAGAIN)
3118 continue;
3120 rc = ERROR_FAIL;
3121 break;
3124 out:
3125 xc_cpupool_infofree(ctx->xch, info);
3126 libxl__free_all(&gc);
3128 return rc;
3131 int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
3133 int rc;
3135 rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
3136 if (rc) {
3137 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3138 "Error moving cpu to cpupool");
3139 return ERROR_FAIL;
3141 return 0;
3144 int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3146 int rc = 0;
3147 int cpu;
3148 libxl_cpumap freemap;
3149 libxl_topologyinfo topology;
3151 if (libxl_get_freecpus(ctx, &freemap)) {
3152 return ERROR_FAIL;
3155 if (libxl_get_topologyinfo(ctx, &topology)) {
3156 rc = ERROR_FAIL;
3157 goto out;
3160 *cpus = 0;
3161 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3162 if (libxl_cpumap_test(&freemap, cpu) &&
3163 (topology.nodemap.array[cpu] == node) &&
3164 !libxl_cpupool_cpuadd(ctx, poolid, cpu)) {
3165 (*cpus)++;
3169 libxl_topologyinfo_destroy(&topology);
3171 out:
3172 libxl_cpumap_destroy(&freemap);
3173 return rc;
3176 int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
3178 int rc;
3180 rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
3181 if (rc) {
3182 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3183 "Error removing cpu from cpupool");
3184 return ERROR_FAIL;
3186 return 0;
3189 int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3191 int ret = 0;
3192 int n_pools;
3193 int p;
3194 int cpu;
3195 libxl_topologyinfo topology;
3196 libxl_cpupoolinfo *poolinfo;
3198 poolinfo = libxl_list_cpupool(ctx, &n_pools);
3199 if (!poolinfo) {
3200 return ERROR_NOMEM;
3203 if (libxl_get_topologyinfo(ctx, &topology)) {
3204 ret = ERROR_FAIL;
3205 goto out;
3208 *cpus = 0;
3209 for (p = 0; p < n_pools; p++) {
3210 if (poolinfo[p].poolid == poolid) {
3211 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3212 if ((topology.nodemap.array[cpu] == node) &&
3213 libxl_cpumap_test(&poolinfo[p].cpumap, cpu) &&
3214 !libxl_cpupool_cpuremove(ctx, poolid, cpu)) {
3215 (*cpus)++;
3221 libxl_topologyinfo_destroy(&topology);
3223 out:
3224 for (p = 0; p < n_pools; p++) {
3225 libxl_cpupoolinfo_destroy(poolinfo + p);
3228 return ret;
3231 int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
3233 libxl__gc gc = LIBXL_INIT_GC(ctx);
3234 int rc;
3235 char *dom_path;
3236 char *vm_path;
3237 char *poolname;
3238 xs_transaction_t t;
3240 dom_path = libxl__xs_get_dompath(&gc, domid);
3241 if (!dom_path) {
3242 libxl__free_all(&gc);
3243 return ERROR_FAIL;
3246 rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
3247 if (rc) {
3248 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3249 "Error moving domain to cpupool");
3250 libxl__free_all(&gc);
3251 return ERROR_FAIL;
3254 for (;;) {
3255 t = xs_transaction_start(ctx->xsh);
3257 poolname = libxl__cpupoolid_to_name(&gc, poolid);
3258 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
3259 if (!vm_path)
3260 break;
3262 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/pool_name", vm_path),
3263 "%s", poolname);
3265 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3266 break;
3269 libxl__free_all(&gc);
3270 return 0;