debuggers.hg

view tools/libxl/libxl.c @ 22726:03718b569d97

xl: Implement flexarray_append() and flexarray_vappend()

Makes a lot of code simpler and nicer and saves a fair amount of screen
real-estate

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Fri Jan 07 18:53:50 2011 +0000 (2011-01-07)
parents e1392f5327ce
children ca75ceb02221
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 int devid;
1079 libxl__device device;
1080 int major, minor, rc;
1082 front = flexarray_make(16, 1);
1083 if (!front) {
1084 rc = ERROR_NOMEM;
1085 goto out;
1087 back = flexarray_make(16, 1);
1088 if (!back) {
1089 rc = ERROR_NOMEM;
1090 goto out_free;
1093 backend_type = libxl__device_disk_backend_type_of_phystype(disk->phystype);
1094 devid = libxl__device_disk_dev_number(disk->virtpath);
1095 if (devid==-1) {
1096 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
1097 " virtual disk identifier %s", disk->virtpath);
1098 rc = ERROR_INVAL;
1099 goto out_free;
1102 device.backend_devid = devid;
1103 device.backend_domid = disk->backend_domid;
1104 device.devid = devid;
1105 device.domid = disk->domid;
1106 device.kind = DEVICE_VBD;
1108 switch (disk->phystype) {
1109 case PHYSTYPE_PHY: {
1111 libxl__device_physdisk_major_minor(disk->physpath, &major, &minor);
1112 flexarray_append(back, "physical-device");
1113 flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
1115 flexarray_append(back, "params");
1116 flexarray_append(back, disk->physpath);
1118 device.backend_kind = DEVICE_VBD;
1119 break;
1121 case PHYSTYPE_FILE:
1122 /* let's pretend is tap:aio for the moment */
1123 disk->phystype = PHYSTYPE_AIO;
1124 case PHYSTYPE_AIO:
1125 case PHYSTYPE_QCOW:
1126 case PHYSTYPE_QCOW2:
1127 case PHYSTYPE_VHD:
1128 if (libxl__blktap_enabled(&gc)) {
1129 const char *dev = libxl__blktap_devpath(&gc,
1130 disk->physpath, disk->phystype);
1131 if (!dev) {
1132 rc = ERROR_FAIL;
1133 goto out_free;
1135 flexarray_append(back, "tapdisk-params");
1136 flexarray_append(back, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
1137 flexarray_append(back, "params");
1138 flexarray_append(back, libxl__strdup(&gc, dev));
1139 backend_type = "phy";
1140 libxl__device_physdisk_major_minor(dev, &major, &minor);
1141 flexarray_append(back, "physical-device");
1142 flexarray_append(back, libxl__sprintf(&gc, "%x:%x", major, minor));
1143 device.backend_kind = DEVICE_VBD;
1145 break;
1147 flexarray_append(back, "params");
1148 flexarray_append(back, libxl__sprintf(&gc, "%s:%s",
1149 libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
1151 if (libxl__blktap_enabled(&gc))
1152 device.backend_kind = DEVICE_TAP;
1153 else
1154 device.backend_kind = DEVICE_QDISK;
1155 break;
1157 default:
1158 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
1159 rc = ERROR_INVAL;
1160 goto out_free;
1163 flexarray_append(back, "frontend-id");
1164 flexarray_append(back, libxl__sprintf(&gc, "%d", disk->domid));
1165 flexarray_append(back, "online");
1166 flexarray_append(back, "1");
1167 flexarray_append(back, "removable");
1168 flexarray_append(back, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
1169 flexarray_append(back, "bootable");
1170 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1171 flexarray_append(back, "state");
1172 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1173 flexarray_append(back, "dev");
1174 flexarray_append(back, disk->virtpath);
1175 flexarray_append(back, "type");
1176 flexarray_append(back, backend_type);
1177 flexarray_append(back, "mode");
1178 flexarray_append(back, disk->readwrite ? "w" : "r");
1180 flexarray_append(front, "backend-id");
1181 flexarray_append(front, libxl__sprintf(&gc, "%d", disk->backend_domid));
1182 flexarray_append(front, "state");
1183 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1184 flexarray_append(front, "virtual-device");
1185 flexarray_append(front, libxl__sprintf(&gc, "%d", devid));
1186 flexarray_append(front, "device-type");
1187 flexarray_append(front, disk->is_cdrom ? "cdrom" : "disk");
1189 if (0 /* protocol != native*/) {
1190 flexarray_append(front, "protocol");
1191 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
1194 libxl__device_generic_add(ctx, &device,
1195 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1196 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1198 rc = 0;
1200 out_free:
1201 flexarray_free(back);
1202 flexarray_free(front);
1203 out:
1204 libxl__free_all(&gc);
1205 return rc;
1208 int libxl_device_disk_del(libxl_ctx *ctx,
1209 libxl_device_disk *disk, int wait)
1211 libxl__device device;
1212 int devid;
1214 devid = libxl__device_disk_dev_number(disk->virtpath);
1215 device.backend_domid = disk->backend_domid;
1216 device.backend_devid = devid;
1217 device.backend_kind =
1218 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1219 device.domid = disk->domid;
1220 device.devid = devid;
1221 device.kind = DEVICE_VBD;
1222 return libxl__device_del(ctx, &device, wait);
1225 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
1227 libxl__gc gc = LIBXL_INIT_GC(ctx);
1228 const char *dev = NULL;
1229 char *ret;
1230 int phystype = disk->phystype;
1231 switch (phystype) {
1232 case PHYSTYPE_PHY: {
1233 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1234 dev = disk->physpath;
1235 break;
1237 case PHYSTYPE_FILE:
1238 /* let's pretend is tap:aio for the moment */
1239 phystype = PHYSTYPE_AIO;
1240 case PHYSTYPE_AIO:
1241 case PHYSTYPE_QCOW:
1242 case PHYSTYPE_QCOW2:
1243 case PHYSTYPE_VHD:
1244 if (libxl__blktap_enabled(&gc))
1245 dev = libxl__blktap_devpath(&gc, disk->physpath, phystype);
1246 break;
1248 default:
1249 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1250 break;
1252 ret = strdup(dev);
1253 libxl__free_all(&gc);
1254 return ret;
1257 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1259 /* Nothing to do for PHYSTYPE_PHY. */
1261 /*
1262 * For other device types assume that the blktap2 process is
1263 * needed by the soon to be started domain and do nothing.
1264 */
1266 return 0;
1269 /******************************************************************************/
1270 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1272 libxl__gc gc = LIBXL_INIT_GC(ctx);
1273 flexarray_t *front;
1274 flexarray_t *back;
1275 libxl__device device;
1276 char *dompath, **l;
1277 unsigned int nb, rc;
1279 front = flexarray_make(16, 1);
1280 if (!front) {
1281 rc = ERROR_NOMEM;
1282 goto out;
1284 back = flexarray_make(16, 1);
1285 if (!back) {
1286 rc = ERROR_NOMEM;
1287 goto out_free;
1290 if (nic->devid == -1) {
1291 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1292 rc = ERROR_FAIL;
1293 goto out_free;
1295 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1296 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb))) {
1297 nic->devid = 0;
1298 } else {
1299 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1303 device.backend_devid = nic->devid;
1304 device.backend_domid = nic->backend_domid;
1305 device.backend_kind = DEVICE_VIF;
1306 device.devid = nic->devid;
1307 device.domid = nic->domid;
1308 device.kind = DEVICE_VIF;
1310 flexarray_append(back, "frontend-id");
1311 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->domid));
1312 flexarray_append(back, "online");
1313 flexarray_append(back, "1");
1314 flexarray_append(back, "state");
1315 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1316 flexarray_append(back, "script");
1317 flexarray_append(back, nic->script);
1318 flexarray_append(back, "mac");
1319 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1320 nic->mac[0], nic->mac[1], nic->mac[2],
1321 nic->mac[3], nic->mac[4], nic->mac[5]));
1322 flexarray_append(back, "bridge");
1323 flexarray_append(back, libxl__strdup(&gc, nic->bridge));
1324 flexarray_append(back, "handle");
1325 flexarray_append(back, libxl__sprintf(&gc, "%d", nic->devid));
1327 flexarray_append(front, "backend-id");
1328 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->backend_domid));
1329 flexarray_append(front, "state");
1330 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1331 flexarray_append(front, "handle");
1332 flexarray_append(front, libxl__sprintf(&gc, "%d", nic->devid));
1333 flexarray_append(front, "mac");
1334 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1335 nic->mac[0], nic->mac[1], nic->mac[2],
1336 nic->mac[3], nic->mac[4], nic->mac[5]));
1337 if (0 /* protocol != native*/) {
1338 flexarray_append(front, "protocol");
1339 flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
1342 libxl__device_generic_add(ctx, &device,
1343 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1344 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1346 /* FIXME: wait for plug */
1347 rc = 0;
1348 out_free:
1349 flexarray_free(back);
1350 flexarray_free(front);
1351 out:
1352 libxl__free_all(&gc);
1353 return rc;
1356 int libxl_device_nic_del(libxl_ctx *ctx,
1357 libxl_device_nic *nic, int wait)
1359 libxl__device device;
1361 device.backend_devid = nic->devid;
1362 device.backend_domid = nic->backend_domid;
1363 device.backend_kind = DEVICE_VIF;
1364 device.devid = nic->devid;
1365 device.domid = nic->domid;
1366 device.kind = DEVICE_VIF;
1368 return libxl__device_del(ctx, &device, wait);
1371 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1373 libxl__gc gc = LIBXL_INIT_GC(ctx);
1374 char *dompath, *nic_path_fe;
1375 char **l, **list;
1376 char *val, *tok;
1377 unsigned int nb_nics, i;
1378 libxl_nicinfo *res, *nics;
1380 dompath = libxl__xs_get_dompath(&gc, domid);
1381 if (!dompath)
1382 goto err;
1383 list = l = libxl__xs_directory(&gc, XBT_NULL,
1384 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb_nics);
1385 if (!l)
1386 goto err;
1387 nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
1388 if (!res)
1389 goto err;
1390 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
1391 nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
1393 nics->backend = xs_read(ctx->xsh, XBT_NULL,
1394 libxl__sprintf(&gc, "%s/backend", nic_path_fe), NULL);
1395 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", nic_path_fe));
1396 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
1398 nics->devid = strtoul(*l, NULL, 10);
1399 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", nic_path_fe));
1400 nics->state = val ? strtoul(val, NULL, 10) : -1;
1401 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", nic_path_fe));
1402 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1403 ++i, tok = strtok(NULL, ":")) {
1404 nics->mac[i] = strtoul(tok, NULL, 16);
1406 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", nic_path_fe));
1407 nics->evtch = val ? strtol(val, NULL, 10) : -1;
1408 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", nic_path_fe));
1409 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
1410 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", nic_path_fe));
1411 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
1412 nics->frontend = xs_read(ctx->xsh, XBT_NULL,
1413 libxl__sprintf(&gc, "%s/frontend", nics->backend), NULL);
1414 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", nics->backend));
1415 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1416 nics->script = xs_read(ctx->xsh, XBT_NULL,
1417 libxl__sprintf(&gc, "%s/script", nics->backend), NULL);
1420 libxl__free_all(&gc);
1421 return res;
1422 err:
1423 libxl__free_all(&gc);
1424 return NULL;
1427 /******************************************************************************/
1428 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
1430 libxl__gc gc = LIBXL_INIT_GC(ctx);
1431 flexarray_t *front, *back;
1432 libxl__device device;
1433 char *dompath, *dom, **l;
1434 unsigned int nb;
1435 int rc;
1437 front = flexarray_make(16, 1);
1438 if (!front) {
1439 rc = ERROR_NOMEM;
1440 goto err;
1442 back = flexarray_make(16, 1);
1443 if (!back) {
1444 rc = ERROR_NOMEM;
1445 goto err_free;
1448 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1449 rc = ERROR_FAIL;
1450 goto err_free;
1452 dom = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/name", dompath));
1454 if (net2->devid == -1) {
1455 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
1456 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb))) {
1457 net2->devid = 0;
1458 } else {
1459 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
1463 device.backend_devid = net2->devid;
1464 device.backend_domid = net2->backend_domid;
1465 device.backend_kind = DEVICE_VIF2;
1466 device.devid = net2->devid;
1467 device.domid = net2->domid;
1468 device.kind = DEVICE_VIF2;
1470 flexarray_append(back, "domain");
1471 flexarray_append(back, dom);
1472 flexarray_append(back, "frontend-id");
1473 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->domid));
1475 flexarray_append(back, "local-trusted");
1476 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->back_trusted));
1477 flexarray_append(back, "mac");
1478 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1479 net2->back_mac[0], net2->back_mac[1],
1480 net2->back_mac[2], net2->back_mac[3],
1481 net2->back_mac[4], net2->back_mac[5]));
1483 flexarray_append(back, "remote-trusted");
1484 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->trusted));
1485 flexarray_append(back, "remote-mac");
1486 flexarray_append(back, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1487 net2->front_mac[0], net2->front_mac[1],
1488 net2->front_mac[2], net2->front_mac[3],
1489 net2->front_mac[4], net2->front_mac[5]));
1491 flexarray_append(back, "max-bypasses");
1492 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->max_bypasses));
1493 flexarray_append(back, "filter-mac");
1494 flexarray_append(back, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1495 flexarray_append(back, "handle");
1496 flexarray_append(back, libxl__sprintf(&gc, "%d", net2->devid));
1497 flexarray_append(back, "online");
1498 flexarray_append(back, "1");
1499 flexarray_append(back, "state");
1500 flexarray_append(back, "1");
1502 flexarray_append(front, "backend-id");
1503 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->backend_domid));
1505 flexarray_append(front, "local-trusted");
1506 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->trusted));
1507 flexarray_append(front, "mac");
1508 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1509 net2->front_mac[0], net2->front_mac[1],
1510 net2->front_mac[2], net2->front_mac[3],
1511 net2->front_mac[4], net2->front_mac[5]));
1513 flexarray_append(front, "remote-trusted");
1514 flexarray_append(front, libxl__sprintf(&gc, "%d", net2->back_trusted));
1515 flexarray_append(front, "remote-mac");
1516 flexarray_append(front, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1517 net2->back_mac[0], net2->back_mac[1],
1518 net2->back_mac[2], net2->back_mac[3],
1519 net2->back_mac[4], net2->back_mac[5]));
1521 flexarray_append(front, "filter-mac");
1522 flexarray_append(front, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
1523 flexarray_append(front, "state");
1524 flexarray_append(front, "1");
1526 libxl__device_generic_add(ctx, &device,
1527 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1528 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1530 /* FIXME: wait for plug */
1531 rc = 0;
1532 err_free:
1533 flexarray_free(back);
1534 flexarray_free(front);
1535 err:
1536 libxl__free_all(&gc);
1537 return rc;
1540 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
1542 libxl__gc gc = LIBXL_INIT_GC(ctx);
1543 char *dompath, *net2_path_fe;
1544 char **l;
1545 char *val, *tok;
1546 unsigned int nb_net2s, i;
1547 libxl_net2info *res, *net2s;
1549 dompath = libxl__xs_get_dompath(&gc, domid);
1550 if (!dompath)
1551 goto err;
1552 l = libxl__xs_directory(&gc, XBT_NULL,
1553 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb_net2s);
1554 if (!l)
1555 goto err;
1556 res = calloc(nb_net2s, sizeof (libxl_net2info));
1557 if (!res)
1558 goto err;
1559 net2s = res;
1560 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
1561 net2_path_fe = libxl__sprintf(&gc, "%s/device/vif2/%s", dompath, *l);
1563 net2s->backend = libxl__xs_read(&gc, XBT_NULL,
1564 libxl__sprintf(&gc, "%s/backend", net2_path_fe));
1565 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", net2_path_fe));
1566 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
1568 net2s->devid = strtoul(*l, NULL, 10);
1569 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", net2_path_fe));
1570 net2s->state = val ? strtoul(val, NULL, 10) : -1;
1572 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", net2_path_fe));
1573 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1574 ++i, tok = strtok(NULL, ":")) {
1575 net2s->mac[i] = strtoul(tok, NULL, 16);
1577 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-trusted", net2_path_fe));
1578 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
1580 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-mac", net2_path_fe));
1581 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
1582 ++i, tok = strtok(NULL, ":")) {
1583 net2s->back_mac[i] = strtoul(tok, NULL, 16);
1585 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/filter-mac", net2_path_fe));
1586 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
1588 net2s->frontend = libxl__xs_read(&gc, XBT_NULL,
1589 libxl__sprintf(&gc, "%s/frontend", net2s->backend));
1590 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", net2s->backend));
1591 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1594 libxl__free_all(&gc);
1595 return res;
1596 err:
1597 libxl__free_all(&gc);
1598 return NULL;
1601 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
1603 libxl__device device;
1605 device.backend_devid = net2->devid;
1606 device.backend_domid = net2->backend_domid;
1607 device.backend_kind = DEVICE_VIF2;
1608 device.devid = net2->devid;
1609 device.domid = net2->domid;
1610 device.kind = DEVICE_VIF2;
1612 return libxl__device_del(ctx, &device, wait);
1616 /******************************************************************************/
1617 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1619 libxl__gc gc = LIBXL_INIT_GC(ctx);
1620 flexarray_t *front;
1621 flexarray_t *back;
1622 libxl__device device;
1623 int rc;
1625 front = flexarray_make(16, 1);
1626 if (!front) {
1627 rc = ERROR_NOMEM;
1628 goto out;
1630 back = flexarray_make(16, 1);
1631 if (!back) {
1632 rc = ERROR_NOMEM;
1633 goto out_free;
1636 device.backend_devid = console->devid;
1637 device.backend_domid = console->backend_domid;
1638 device.backend_kind = DEVICE_CONSOLE;
1639 device.devid = console->devid;
1640 device.domid = console->domid;
1641 device.kind = DEVICE_CONSOLE;
1643 flexarray_append(back, "frontend-id");
1644 flexarray_append(back, libxl__sprintf(&gc, "%d", console->domid));
1645 flexarray_append(back, "online");
1646 flexarray_append(back, "1");
1647 flexarray_append(back, "state");
1648 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1649 flexarray_append(back, "domain");
1650 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1651 flexarray_append(back, "protocol");
1652 flexarray_append(back, LIBXL_XENCONSOLE_PROTOCOL);
1654 flexarray_append(front, "backend-id");
1655 flexarray_append(front, libxl__sprintf(&gc, "%d", console->backend_domid));
1656 flexarray_append(front, "limit");
1657 flexarray_append(front, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
1658 flexarray_append(front, "type");
1659 if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
1660 flexarray_append(front, "xenconsoled");
1661 else
1662 flexarray_append(front, "ioemu");
1663 flexarray_append(front, "output");
1664 flexarray_append(front, console->output);
1666 if (device.devid == 0) {
1667 if (console->build_state == NULL) {
1668 rc = ERROR_INVAL;
1669 goto out_free;
1671 flexarray_append(front, "port");
1672 flexarray_append(front, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
1673 flexarray_append(front, "ring-ref");
1674 flexarray_append(front, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
1675 } else {
1676 flexarray_append(front, "state");
1677 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1678 flexarray_append(front, "protocol");
1679 flexarray_append(front, LIBXL_XENCONSOLE_PROTOCOL);
1682 libxl__device_generic_add(ctx, &device,
1683 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1684 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1685 rc = 0;
1686 out_free:
1687 flexarray_free(back);
1688 flexarray_free(front);
1689 out:
1690 libxl__free_all(&gc);
1691 return rc;
1694 /******************************************************************************/
1695 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1697 libxl__gc gc = LIBXL_INIT_GC(ctx);
1698 flexarray_t *front;
1699 flexarray_t *back;
1700 libxl__device device;
1701 int rc;
1703 front = flexarray_make(16, 1);
1704 if (!front) {
1705 rc = ERROR_NOMEM;
1706 goto out;
1708 back = flexarray_make(16, 1);
1709 if (!back) {
1710 rc = ERROR_NOMEM;
1711 goto out_free;
1714 device.backend_devid = vkb->devid;
1715 device.backend_domid = vkb->backend_domid;
1716 device.backend_kind = DEVICE_VKBD;
1717 device.devid = vkb->devid;
1718 device.domid = vkb->domid;
1719 device.kind = DEVICE_VKBD;
1721 flexarray_append(back, "frontend-id");
1722 flexarray_append(back, libxl__sprintf(&gc, "%d", vkb->domid));
1723 flexarray_append(back, "online");
1724 flexarray_append(back, "1");
1725 flexarray_append(back, "state");
1726 flexarray_append(back, libxl__sprintf(&gc, "%d", 1));
1727 flexarray_append(back, "domain");
1728 flexarray_append(back, libxl__domid_to_name(&gc, domid));
1730 flexarray_append(front, "backend-id");
1731 flexarray_append(front, libxl__sprintf(&gc, "%d", vkb->backend_domid));
1732 flexarray_append(front, "state");
1733 flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
1735 libxl__device_generic_add(ctx, &device,
1736 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1737 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1738 rc = 0;
1739 out_free:
1740 flexarray_free(back);
1741 flexarray_free(front);
1742 out:
1743 libxl__free_all(&gc);
1744 return rc;
1747 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1749 return ERROR_NI;
1752 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1754 return ERROR_NI;
1757 static unsigned int libxl_append_disk_list_of_type(libxl_ctx *ctx,
1758 uint32_t domid,
1759 const char *type,
1760 libxl_device_disk **disks,
1761 unsigned int *ndisks)
1763 libxl__gc gc = LIBXL_INIT_GC(ctx);
1764 char *be_path = NULL;
1765 char **dir = NULL;
1766 unsigned int n = 0, len = 0;
1767 libxl_device_disk *pdisk = NULL, *pdisk_end = NULL;
1768 char *physpath_tmp = NULL;
1770 be_path = libxl__sprintf(&gc, "%s/backend/%s/%d",
1771 libxl__xs_get_dompath(&gc, 0), type, domid);
1772 dir = libxl__xs_directory(&gc, XBT_NULL, be_path, &n);
1773 if (dir) {
1774 *disks = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
1775 pdisk = *disks + *ndisks;
1776 *ndisks += n;
1777 pdisk_end = *disks + *ndisks;
1778 for (; pdisk < pdisk_end; pdisk++, dir++) {
1779 pdisk->backend_domid = 0;
1780 pdisk->domid = domid;
1781 physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path, *dir), &len);
1782 if (strchr(physpath_tmp, ':')) {
1783 pdisk->physpath = strdup(strchr(physpath_tmp, ':') + 1);
1784 free(physpath_tmp);
1785 } else {
1786 pdisk->physpath = physpath_tmp;
1788 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path, *dir)), &(pdisk->phystype));
1789 pdisk->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path, *dir), &len);
1790 pdisk->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path, *dir)));
1791 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path, *dir)), "w"))
1792 pdisk->readwrite = 1;
1793 else
1794 pdisk->readwrite = 0;
1795 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))));
1796 pdisk->is_cdrom = !strcmp(type, "cdrom");
1800 libxl__free_all(&gc);
1801 return n;
1804 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
1806 libxl_device_disk *disks = NULL;
1807 unsigned int ndisks = 0;
1809 *num = libxl_append_disk_list_of_type(ctx, domid, "vbd", &disks, &ndisks);
1810 *num += libxl_append_disk_list_of_type(ctx, domid, "tap", &disks, &ndisks);
1811 *num += libxl_append_disk_list_of_type(ctx, domid, "qdisk", &disks, &ndisks);
1813 return disks;
1816 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
1817 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
1819 libxl__gc gc = LIBXL_INIT_GC(ctx);
1820 char *dompath, *diskpath;
1821 char *val;
1823 dompath = libxl__xs_get_dompath(&gc, domid);
1824 diskinfo->devid = libxl__device_disk_dev_number(disk->virtpath);
1826 /* tap devices entries in xenstore are written as vbd devices. */
1827 diskpath = libxl__sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
1828 diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
1829 libxl__sprintf(&gc, "%s/backend", diskpath), NULL);
1830 if (!diskinfo->backend) {
1831 libxl__free_all(&gc);
1832 return ERROR_FAIL;
1834 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", diskpath));
1835 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
1836 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", diskpath));
1837 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
1838 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", diskpath));
1839 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
1840 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/ring-ref", diskpath));
1841 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
1842 diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
1843 libxl__sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
1844 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", diskinfo->backend));
1845 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
1847 libxl__free_all(&gc);
1848 return 0;
1851 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1853 int num, i;
1854 uint32_t stubdomid;
1855 libxl_device_disk *disks;
1856 int ret = ERROR_FAIL;
1858 if (!disk->physpath) {
1859 disk->physpath = "";
1860 disk->phystype = PHYSTYPE_PHY;
1862 disks = libxl_device_disk_list(ctx, domid, &num);
1863 for (i = 0; i < num; i++) {
1864 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1865 /* found */
1866 break;
1868 if (i == num) {
1869 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual device not found");
1870 goto out;
1873 ret = 0;
1875 libxl_device_disk_del(ctx, disks + i, 1);
1876 libxl_device_disk_add(ctx, domid, disk);
1877 stubdomid = libxl_get_stubdom_id(ctx, domid);
1878 if (stubdomid) {
1879 disks[i].domid = stubdomid;
1880 libxl_device_disk_del(ctx, disks + i, 1);
1881 disk->domid = stubdomid;
1882 libxl_device_disk_add(ctx, stubdomid, disk);
1883 disk->domid = domid;
1885 out:
1886 for (i = 0; i < num; i++)
1887 libxl_device_disk_destroy(&disks[i]);
1888 free(disks);
1889 return ret;
1892 /******************************************************************************/
1893 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1895 libxl__gc gc = LIBXL_INIT_GC(ctx);
1896 flexarray_t *front;
1897 flexarray_t *back;
1898 libxl__device device;
1899 int rc;
1901 front = flexarray_make(16, 1);
1902 if (!front) {
1903 rc = ERROR_NOMEM;
1904 goto out;
1906 back = flexarray_make(16, 1);
1907 if (!back) {
1908 rc = ERROR_NOMEM;
1909 goto out_free;
1912 device.backend_devid = vfb->devid;
1913 device.backend_domid = vfb->backend_domid;
1914 device.backend_kind = DEVICE_VFB;
1915 device.devid = vfb->devid;
1916 device.domid = vfb->domid;
1917 device.kind = DEVICE_VFB;
1919 flexarray_vappend(back, "frontend-id", libxl__sprintf(&gc, "%d", vfb->domid), NULL);
1920 flexarray_vappend(back, "online", "1", NULL);
1921 flexarray_vappend(back, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1922 flexarray_vappend(back, "domain", libxl__domid_to_name(&gc, domid), NULL);
1923 flexarray_vappend(back, "vnc", libxl__sprintf(&gc, "%d", vfb->vnc), NULL);
1924 flexarray_vappend(back, "vnclisten", vfb->vnclisten, NULL);
1925 flexarray_append(back, "vncpasswd");
1926 flexarray_append(back, vfb->vncpasswd);
1927 flexarray_vappend(back, "vncdisplay", libxl__sprintf(&gc, "%d", vfb->vncdisplay), NULL);
1928 flexarray_vappend(back, "vncunused", libxl__sprintf(&gc, "%d", vfb->vncunused), NULL);
1929 flexarray_vappend(back, "sdl", libxl__sprintf(&gc, "%d", vfb->sdl), NULL);
1930 flexarray_vappend(back, "opengl", libxl__sprintf(&gc, "%d", vfb->opengl), NULL);
1931 if (vfb->xauthority) {
1932 flexarray_vappend(back, "xauthority", vfb->xauthority, NULL);
1934 if (vfb->display) {
1935 flexarray_vappend(back, "display", vfb->display, NULL);
1938 flexarray_vappend(front, "backend-id", libxl__sprintf(&gc, "%d", vfb->backend_domid), NULL);
1939 flexarray_vappend(front, "state", libxl__sprintf(&gc, "%d", 1), NULL);
1941 libxl__device_generic_add(ctx, &device,
1942 libxl__xs_kvs_of_flexarray(&gc, back, back->count),
1943 libxl__xs_kvs_of_flexarray(&gc, front, front->count));
1944 rc = 0;
1945 out_free:
1946 flexarray_free(front);
1947 flexarray_free(back);
1948 out:
1949 libxl__free_all(&gc);
1950 return rc;
1953 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
1955 return ERROR_NI;
1958 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
1960 return ERROR_NI;
1963 /******************************************************************************/
1965 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
1967 libxl__gc gc = LIBXL_INIT_GC(ctx);
1968 char *mem, *endptr;
1969 uint32_t memorykb;
1970 char *dompath = libxl__xs_get_dompath(&gc, domid);
1971 int rc = 1;
1973 mem = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/memory/target", dompath));
1974 if (!mem) {
1975 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
1976 goto out;
1978 memorykb = strtoul(mem, &endptr, 10);
1979 if (*endptr != '\0') {
1980 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
1981 goto out;
1984 if (max_memkb < memorykb) {
1985 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
1986 goto out;
1989 rc = 0;
1990 out:
1991 libxl__free_all(&gc);
1992 return rc;
1995 static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb)
1997 int rc;
1998 libxl_dominfo info;
1999 libxl_physinfo physinfo;
2000 char *target = NULL, *staticmax = NULL, *freememslack = NULL, *endptr = NULL;
2001 char *target_path = "/local/domain/0/memory/target";
2002 char *max_path = "/local/domain/0/memory/static-max";
2003 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2004 xs_transaction_t t;
2005 libxl_ctx *ctx = libxl__gc_owner(gc);
2006 uint32_t free_mem_slack_kb = 0;
2008 retry_transaction:
2009 t = xs_transaction_start(ctx->xsh);
2011 target = libxl__xs_read(gc, t, target_path);
2012 staticmax = libxl__xs_read(gc, t, max_path);
2013 freememslack = libxl__xs_read(gc, t, free_mem_slack_path);
2014 if (target && staticmax && freememslack) {
2015 rc = 0;
2016 goto out;
2019 if (target) {
2020 *target_memkb = strtoul(target, &endptr, 10);
2021 if (*endptr != '\0') {
2022 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2023 "invalid memory target %s from %s\n", target, target_path);
2024 rc = ERROR_FAIL;
2025 goto out;
2029 rc = libxl_domain_info(ctx, &info, 0);
2030 if (rc < 0)
2031 goto out;
2033 rc = libxl_get_physinfo(ctx, &physinfo);
2034 if (rc < 0)
2035 goto out;
2037 if (target == NULL) {
2038 libxl__xs_write(gc, t, target_path, "%"PRIu32,
2039 (uint32_t) info.current_memkb);
2040 *target_memkb = (uint32_t) info.current_memkb;
2042 if (staticmax == NULL)
2043 libxl__xs_write(gc, t, max_path, "%"PRIu32,
2044 (uint32_t) info.max_memkb);
2046 if (freememslack == NULL) {
2047 free_mem_slack_kb = (uint32_t) (PAGE_TO_MEMKB(physinfo.total_pages) -
2048 info.current_memkb);
2049 /* From empirical measurements the free_mem_slack shouldn't be more
2050 * than 15% of the total memory present on the system. */
2051 if (free_mem_slack_kb > PAGE_TO_MEMKB(physinfo.total_pages) * 0.15)
2052 free_mem_slack_kb = PAGE_TO_MEMKB(physinfo.total_pages) * 0.15;
2053 libxl__xs_write(gc, t, free_mem_slack_path, "%"PRIu32, free_mem_slack_kb);
2055 rc = 0;
2057 out:
2058 if (!xs_transaction_end(ctx->xsh, t, 0)) {
2059 if (errno == EAGAIN)
2060 goto retry_transaction;
2061 else
2062 rc = ERROR_FAIL;
2066 return rc;
2069 /* returns how much memory should be left free in the system */
2070 static int libxl__get_free_memory_slack(libxl__gc *gc, uint32_t *free_mem_slack)
2072 int rc;
2073 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2074 char *free_mem_slack_s, *endptr;
2075 uint32_t target_memkb;
2077 retry:
2078 free_mem_slack_s = libxl__xs_read(gc, XBT_NULL, free_mem_slack_path);
2079 if (!free_mem_slack_s) {
2080 rc = libxl__fill_dom0_memory_info(gc, &target_memkb);
2081 if (rc < 0)
2082 return rc;
2083 goto retry;
2084 } else {
2085 *free_mem_slack = strtoul(free_mem_slack_s, &endptr, 10);
2086 if (*endptr != '\0') {
2087 LIBXL__LOG_ERRNO(gc->owner, LIBXL__LOG_ERROR,
2088 "invalid free_mem_slack %s from %s\n",
2089 free_mem_slack_s, free_mem_slack_path);
2090 return ERROR_FAIL;
2093 return 0;
2096 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
2097 int32_t target_memkb, int relative, int enforce)
2099 libxl__gc gc = LIBXL_INIT_GC(ctx);
2100 int rc = 1, abort = 0;
2101 uint32_t memorykb = 0, videoram = 0;
2102 uint32_t current_target_memkb = 0, new_target_memkb = 0;
2103 char *memmax, *endptr, *videoram_s = NULL, *target = NULL;
2104 char *dompath = libxl__xs_get_dompath(&gc, domid);
2105 xc_domaininfo_t info;
2106 libxl_dominfo ptr;
2107 char *uuid;
2108 xs_transaction_t t;
2110 retry_transaction:
2111 t = xs_transaction_start(ctx->xsh);
2113 target = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2114 "%s/memory/target", dompath));
2115 if (!target && !domid) {
2116 xs_transaction_end(ctx->xsh, t, 1);
2117 rc = libxl__fill_dom0_memory_info(&gc, &current_target_memkb);
2118 if (rc < 0) {
2119 abort = 1;
2120 goto out;
2122 goto retry_transaction;
2123 } else if (!target) {
2124 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2125 "cannot get target memory info from %s/memory/target\n",
2126 dompath);
2127 abort = 1;
2128 goto out;
2129 } else {
2130 current_target_memkb = strtoul(target, &endptr, 10);
2131 if (*endptr != '\0') {
2132 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2133 "invalid memory target %s from %s/memory/target\n",
2134 target, dompath);
2135 abort = 1;
2136 goto out;
2139 memmax = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2140 "%s/memory/static-max", dompath));
2141 if (!memmax) {
2142 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2143 "cannot get memory info from %s/memory/static-max\n",
2144 dompath);
2145 abort = 1;
2146 goto out;
2148 memorykb = strtoul(memmax, &endptr, 10);
2149 if (*endptr != '\0') {
2150 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2151 "invalid max memory %s from %s/memory/static-max\n",
2152 memmax, dompath);
2153 abort = 1;
2154 goto out;
2157 if (relative)
2158 new_target_memkb = current_target_memkb + target_memkb;
2159 else
2160 new_target_memkb = target_memkb;
2161 if (new_target_memkb > memorykb) {
2162 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2163 "memory_dynamic_max must be less than or equal to"
2164 " memory_static_max\n");
2165 abort = 1;
2166 goto out;
2169 if (!domid && new_target_memkb < LIBXL_MIN_DOM0_MEM) {
2170 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2171 "new target %d for dom0 is below the minimum threshold\n",
2172 new_target_memkb);
2173 abort = 1;
2174 goto out;
2176 videoram_s = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2177 "%s/memory/videoram", dompath));
2178 videoram = videoram_s ? atoi(videoram_s) : 0;
2180 if (enforce) {
2181 memorykb = new_target_memkb;
2182 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
2183 LIBXL_MAXMEM_CONSTANT);
2184 if (rc != 0) {
2185 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2186 "xc_domain_setmaxmem domid=%d memkb=%d failed "
2187 "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
2188 abort = 1;
2189 goto out;
2193 new_target_memkb -= videoram;
2194 rc = xc_domain_set_pod_target(ctx->xch, domid,
2195 new_target_memkb / 4, NULL, NULL, NULL);
2196 if (rc != 0) {
2197 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2198 "xc_domain_set_pod_target domid=%d, memkb=%d "
2199 "failed rc=%d\n", domid, new_target_memkb / 4,
2200 rc);
2201 abort = 1;
2202 goto out;
2205 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/memory/target",
2206 dompath), "%"PRIu32, new_target_memkb);
2207 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2208 if (rc != 1 || info.domain != domid) {
2209 abort = 1;
2210 goto out;
2212 xcinfo2xlinfo(&info, &ptr);
2213 uuid = libxl__uuid2string(&gc, ptr.uuid);
2214 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "/vm/%s/memory", uuid),
2215 "%"PRIu32, new_target_memkb / 1024);
2217 out:
2218 if (!xs_transaction_end(ctx->xsh, t, abort) && !abort)
2219 if (errno == EAGAIN)
2220 goto retry_transaction;
2222 libxl__free_all(&gc);
2223 return rc;
2226 int libxl_get_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t *out_target)
2228 libxl__gc gc = LIBXL_INIT_GC(ctx);
2229 int rc = 1;
2230 char *target = NULL, *endptr = NULL;
2231 char *dompath = libxl__xs_get_dompath(&gc, domid);
2232 uint32_t target_memkb;
2234 target = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc,
2235 "%s/memory/target", dompath));
2236 if (!target && !domid) {
2237 rc = libxl__fill_dom0_memory_info(&gc, &target_memkb);
2238 if (rc < 0)
2239 goto out;
2240 } else if (!target) {
2241 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2242 "cannot get target memory info from %s/memory/target\n",
2243 dompath);
2244 goto out;
2245 } else {
2246 target_memkb = strtoul(target, &endptr, 10);
2247 if (*endptr != '\0') {
2248 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2249 "invalid memory target %s from %s/memory/target\n",
2250 target, dompath);
2251 goto out;
2254 *out_target = target_memkb;
2255 rc = 0;
2257 out:
2258 libxl__free_all(&gc);
2259 return rc;
2262 int libxl_domain_need_memory(libxl_ctx *ctx, libxl_domain_build_info *b_info,
2263 libxl_device_model_info *dm_info, uint32_t *need_memkb)
2265 *need_memkb = b_info->target_memkb;
2266 if (b_info->hvm) {
2267 *need_memkb += b_info->shadow_memkb + LIBXL_HVM_EXTRA_MEMORY;
2268 if (strstr(dm_info->device_model, "stubdom-dm"))
2269 *need_memkb += 32 * 1024;
2270 } else
2271 *need_memkb += b_info->shadow_memkb + LIBXL_PV_EXTRA_MEMORY;
2272 if (*need_memkb % (2 * 1024))
2273 *need_memkb += (2 * 1024) - (*need_memkb % (2 * 1024));
2274 return 0;
2277 int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
2279 int rc = 0;
2280 libxl_physinfo info;
2281 uint32_t freemem_slack;
2282 libxl__gc gc = LIBXL_INIT_GC(ctx);
2284 rc = libxl_get_physinfo(ctx, &info);
2285 if (rc < 0)
2286 goto out;
2287 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
2288 if (rc < 0)
2289 goto out;
2291 if ((info.free_pages + info.scrub_pages) * 4 > freemem_slack)
2292 *memkb = (info.free_pages + info.scrub_pages) * 4 - freemem_slack;
2293 else
2294 *memkb = 0;
2296 out:
2297 libxl__free_all(&gc);
2298 return rc;
2301 int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
2302 memory_kb, int wait_secs)
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_free_memory_slack(&gc, &freemem_slack);
2310 if (rc < 0)
2311 goto out;
2312 while (wait_secs > 0) {
2313 rc = libxl_get_physinfo(ctx, &info);
2314 if (rc < 0)
2315 goto out;
2316 if (info.free_pages * 4 - freemem_slack >= memory_kb) {
2317 rc = 0;
2318 goto out;
2320 wait_secs--;
2321 sleep(1);
2323 rc = ERROR_NOMEM;
2325 out:
2326 libxl__free_all(&gc);
2327 return rc;
2330 int libxl_wait_for_memory_target(libxl_ctx *ctx, uint32_t domid, int wait_secs)
2332 int rc = 0;
2333 uint32_t target_memkb = 0;
2334 libxl_dominfo info;
2336 do {
2337 wait_secs--;
2338 sleep(1);
2340 rc = libxl_get_memory_target(ctx, domid, &target_memkb);
2341 if (rc < 0)
2342 goto out;
2344 rc = libxl_domain_info(ctx, &info, domid);
2345 if (rc < 0)
2346 return rc;
2347 } while (wait_secs > 0 && info.current_memkb > target_memkb);
2349 if (info.current_memkb <= target_memkb)
2350 rc = 0;
2351 else
2352 rc = ERROR_FAIL;
2354 out:
2355 return 0;
2358 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
2360 int rc = -1;
2362 switch (button) {
2363 case POWER_BUTTON:
2364 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2365 break;
2366 case SLEEP_BUTTON:
2367 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2368 break;
2369 default:
2370 break;
2373 return rc;
2376 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
2378 xc_physinfo_t xcphysinfo = { 0 };
2379 int rc;
2381 rc = xc_physinfo(ctx->xch, &xcphysinfo);
2382 if (rc != 0) {
2383 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting physinfo");
2384 return ERROR_FAIL;
2386 physinfo->threads_per_core = xcphysinfo.threads_per_core;
2387 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
2388 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
2389 physinfo->nr_cpus = xcphysinfo.nr_cpus;
2390 physinfo->cpu_khz = xcphysinfo.cpu_khz;
2391 physinfo->total_pages = xcphysinfo.total_pages;
2392 physinfo->free_pages = xcphysinfo.free_pages;
2393 physinfo->scrub_pages = xcphysinfo.scrub_pages;
2394 physinfo->nr_nodes = xcphysinfo.nr_nodes;
2395 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
2396 physinfo->phys_cap = xcphysinfo.capabilities;
2398 return 0;
2401 int libxl_get_topologyinfo(libxl_ctx *ctx, libxl_topologyinfo *info)
2403 xc_topologyinfo_t tinfo;
2404 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_core_t, coremap);
2405 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_socket_t, socketmap);
2406 DECLARE_HYPERCALL_BUFFER(xc_cpu_to_node_t, nodemap);
2407 int i;
2408 int rc = 0;
2410 rc += libxl_cpuarray_alloc(ctx, &info->coremap);
2411 rc += libxl_cpuarray_alloc(ctx, &info->socketmap);
2412 rc += libxl_cpuarray_alloc(ctx, &info->nodemap);
2413 if (rc)
2414 goto fail;
2416 coremap = xc_hypercall_buffer_alloc(ctx->xch, coremap, sizeof(*coremap) * info->coremap.entries);
2417 socketmap = xc_hypercall_buffer_alloc(ctx->xch, socketmap, sizeof(*socketmap) * info->socketmap.entries);
2418 nodemap = xc_hypercall_buffer_alloc(ctx->xch, nodemap, sizeof(*nodemap) * info->nodemap.entries);
2419 if ((coremap == NULL) || (socketmap == NULL) || (nodemap == NULL))
2420 goto fail;
2422 set_xen_guest_handle(tinfo.cpu_to_core, coremap);
2423 set_xen_guest_handle(tinfo.cpu_to_socket, socketmap);
2424 set_xen_guest_handle(tinfo.cpu_to_node, nodemap);
2425 tinfo.max_cpu_index = info->coremap.entries - 1;
2426 if (xc_topologyinfo(ctx->xch, &tinfo) != 0)
2427 goto fail;
2429 for (i = 0; i <= tinfo.max_cpu_index; i++) {
2430 if (i < info->coremap.entries)
2431 info->coremap.array[i] = (coremap[i] == INVALID_TOPOLOGY_ID) ?
2432 LIBXL_CPUARRAY_INVALID_ENTRY : coremap[i];
2433 if (i < info->socketmap.entries)
2434 info->socketmap.array[i] = (socketmap[i] == INVALID_TOPOLOGY_ID) ?
2435 LIBXL_CPUARRAY_INVALID_ENTRY : socketmap[i];
2436 if (i < info->nodemap.entries)
2437 info->nodemap.array[i] = (nodemap[i] == INVALID_TOPOLOGY_ID) ?
2438 LIBXL_CPUARRAY_INVALID_ENTRY : nodemap[i];
2441 xc_hypercall_buffer_free(ctx->xch, coremap);
2442 xc_hypercall_buffer_free(ctx->xch, socketmap);
2443 xc_hypercall_buffer_free(ctx->xch, nodemap);
2444 return 0;
2446 fail:
2447 xc_hypercall_buffer_free(ctx->xch, coremap);
2448 xc_hypercall_buffer_free(ctx->xch, socketmap);
2449 xc_hypercall_buffer_free(ctx->xch, nodemap);
2450 libxl_topologyinfo_destroy(info);
2451 return ERROR_FAIL;
2454 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
2456 union {
2457 xen_extraversion_t xen_extra;
2458 xen_compile_info_t xen_cc;
2459 xen_changeset_info_t xen_chgset;
2460 xen_capabilities_info_t xen_caps;
2461 xen_platform_parameters_t p_parms;
2462 xen_commandline_t xen_commandline;
2463 } u;
2464 long xen_version;
2465 libxl_version_info *info = &ctx->version_info;
2467 if (info->xen_version_extra != NULL)
2468 return info;
2470 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
2471 info->xen_version_major = xen_version >> 16;
2472 info->xen_version_minor = xen_version & 0xFF;
2474 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
2475 info->xen_version_extra = strdup(u.xen_extra);
2477 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
2478 info->compiler = strdup(u.xen_cc.compiler);
2479 info->compile_by = strdup(u.xen_cc.compile_by);
2480 info->compile_domain = strdup(u.xen_cc.compile_domain);
2481 info->compile_date = strdup(u.xen_cc.compile_date);
2483 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
2484 info->capabilities = strdup(u.xen_caps);
2486 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
2487 info->changeset = strdup(u.xen_chgset);
2489 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
2490 info->virt_start = u.p_parms.virt_start;
2492 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
2494 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
2495 info->commandline = strdup(u.xen_commandline);
2497 return info;
2500 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
2501 int *nb_vcpu, int *nrcpus)
2503 libxl_vcpuinfo *ptr, *ret;
2504 xc_domaininfo_t domaininfo;
2505 xc_vcpuinfo_t vcpuinfo;
2507 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
2508 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist");
2509 return NULL;
2511 *nrcpus = libxl_get_max_cpus(ctx);
2512 ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
2513 if (!ptr) {
2514 return NULL;
2517 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
2518 if (libxl_cpumap_alloc(ctx, &ptr->cpumap)) {
2519 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap");
2520 return NULL;
2522 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
2523 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info");
2524 return NULL;
2526 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) {
2527 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity");
2528 return NULL;
2530 ptr->vcpuid = *nb_vcpu;
2531 ptr->cpu = vcpuinfo.cpu;
2532 ptr->online = !!vcpuinfo.online;
2533 ptr->blocked = !!vcpuinfo.blocked;
2534 ptr->running = !!vcpuinfo.running;
2535 ptr->vcpu_time = vcpuinfo.cpu_time;
2537 return ret;
2540 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
2541 libxl_cpumap *cpumap)
2543 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap->map)) {
2544 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting vcpu affinity");
2545 return ERROR_FAIL;
2547 return 0;
2550 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask)
2552 libxl__gc gc = LIBXL_INIT_GC(ctx);
2553 libxl_dominfo info;
2554 char *dompath;
2555 xs_transaction_t t;
2556 int i, rc = ERROR_FAIL;
2558 if (libxl_domain_info(ctx, &info, domid) < 0) {
2559 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2560 goto out;
2562 if (!(dompath = libxl__xs_get_dompath(&gc, domid)))
2563 goto out;
2565 retry_transaction:
2566 t = xs_transaction_start(ctx->xsh);
2567 for (i = 0; i <= info.vcpu_max_id; i++)
2568 libxl__xs_write(&gc, t,
2569 libxl__sprintf(&gc, "%s/cpu/%u/availability", dompath, i),
2570 "%s", ((1 << i) & bitmask) ? "online" : "offline");
2571 if (!xs_transaction_end(ctx->xsh, t, 0)) {
2572 if (errno == EAGAIN)
2573 goto retry_transaction;
2574 } else
2575 rc = 0;
2576 out:
2577 libxl__free_all(&gc);
2578 return rc;
2581 /*
2582 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
2583 */
2584 int libxl_get_sched_id(libxl_ctx *ctx)
2586 int sched, ret;
2588 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
2589 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2590 return ERROR_FAIL;
2592 return sched;
2595 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2597 struct xen_domctl_sched_credit sdom;
2598 int rc;
2600 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
2601 if (rc != 0) {
2602 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2603 return ERROR_FAIL;
2606 scinfo->weight = sdom.weight;
2607 scinfo->cap = sdom.cap;
2609 return 0;
2612 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
2614 struct xen_domctl_sched_credit sdom;
2615 xc_domaininfo_t domaininfo;
2616 int rc;
2618 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
2619 if (rc < 0) {
2620 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
2621 return ERROR_FAIL;
2623 if (rc != 1 || domaininfo.domain != domid)
2624 return ERROR_INVAL;
2627 if (scinfo->weight < 1 || scinfo->weight > 65535) {
2628 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2629 "Cpu weight out of range, valid values are within range from 1 to 65535");
2630 return ERROR_INVAL;
2633 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
2634 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2635 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
2636 ((domaininfo.max_vcpu_id + 1) * 100));
2637 return ERROR_INVAL;
2640 sdom.weight = scinfo->weight;
2641 sdom.cap = scinfo->cap;
2643 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
2644 if ( rc < 0 ) {
2645 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
2646 return ERROR_FAIL;
2649 return 0;
2652 static int trigger_type_from_string(char *trigger_name)
2654 if (!strcmp(trigger_name, "nmi"))
2655 return XEN_DOMCTL_SENDTRIGGER_NMI;
2656 else if (!strcmp(trigger_name, "reset"))
2657 return XEN_DOMCTL_SENDTRIGGER_RESET;
2658 else if (!strcmp(trigger_name, "init"))
2659 return XEN_DOMCTL_SENDTRIGGER_INIT;
2660 else if (!strcmp(trigger_name, "power"))
2661 return XEN_DOMCTL_SENDTRIGGER_POWER;
2662 else if (!strcmp(trigger_name, "sleep"))
2663 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
2664 else
2665 return -1;
2668 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
2670 int rc = -1;
2671 int trigger_type = trigger_type_from_string(trigger_name);
2673 if (trigger_type == -1) {
2674 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2675 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
2676 return ERROR_INVAL;
2679 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
2680 if (rc != 0) {
2681 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2682 "Send trigger '%s' failed", trigger_name);
2683 return ERROR_FAIL;
2686 return 0;
2689 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
2691 libxl__gc gc = LIBXL_INIT_GC(ctx);
2692 char *dompath = libxl__xs_get_dompath(&gc, domid);
2694 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/control/sysrq", dompath), "%c", sysrq);
2696 libxl__free_all(&gc);
2697 return 0;
2700 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
2702 int ret;
2703 ret = xc_send_debug_keys(ctx->xch, keys);
2704 if ( ret < 0 ) {
2705 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "sending debug keys");
2706 return ERROR_FAIL;
2708 return 0;
2711 libxl_xen_console_reader *
2712 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
2714 libxl_xen_console_reader *cr;
2715 unsigned int size = 16384;
2716 char *buf = malloc(size);
2718 if (!buf) {
2719 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
2720 " size is %u", size);
2721 return NULL;
2724 cr = malloc(sizeof(libxl_xen_console_reader));
2725 if (!cr) {
2726 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc libxl_xen_console_reader");
2727 return NULL;
2730 memset(cr, 0, sizeof(libxl_xen_console_reader));
2731 cr->buffer = buf;
2732 cr->size = size;
2733 cr->count = size;
2734 cr->clear = clear;
2735 cr->incremental = 1;
2737 return cr;
2740 /* return values: *line_r
2741 * 1 success, whole line obtained from buffer non-0
2742 * 0 no more lines available right now 0
2743 * negative error code ERROR_* 0
2744 * On success *line_r is updated to point to a nul-terminated
2745 * string which is valid until the next call on the same console
2746 * reader. The libxl caller may overwrite parts of the string
2747 * if it wishes. */
2748 int libxl_xen_console_read_line(libxl_ctx *ctx,
2749 libxl_xen_console_reader *cr,
2750 char **line_r)
2752 int ret;
2754 memset(cr->buffer, 0, cr->size);
2755 ret = xc_readconsolering(ctx->xch, cr->buffer, &cr->count,
2756 cr->clear, cr->incremental, &cr->index);
2757 if (ret < 0) {
2758 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "reading console ring buffer");
2759 return ERROR_FAIL;
2761 if (!ret) {
2762 if (cr->count) {
2763 *line_r = cr->buffer;
2764 ret = 1;
2765 } else {
2766 *line_r = NULL;
2767 ret = 0;
2771 return ret;
2774 void libxl_xen_console_read_finish(libxl_ctx *ctx,
2775 libxl_xen_console_reader *cr)
2777 free(cr->buffer);
2778 free(cr);
2781 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
2783 libxl__gc gc = LIBXL_INIT_GC(ctx);
2784 char *dompath = libxl__xs_get_dompath(&gc, domid);
2785 char *vm_path, *start_time;
2786 uint32_t ret;
2788 vm_path = libxl__xs_read(
2789 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dompath));
2790 start_time = libxl__xs_read(
2791 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/start_time", vm_path));
2792 if (start_time == NULL) {
2793 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2794 "Can't get start time of domain '%d'", domid);
2795 ret = -1;
2796 }else{
2797 ret = strtoul(start_time, NULL, 10);
2799 libxl__free_all(&gc);
2800 return ret;
2803 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
2805 int rc;
2806 char _buf[32768];
2808 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
2809 0, _buf);
2810 if (rc < 0) {
2811 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2812 "Can not get tmem list");
2813 return NULL;
2816 return strdup(_buf);
2819 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
2821 int rc;
2823 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
2824 0, NULL);
2825 if (rc < 0) {
2826 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2827 "Can not freeze tmem pools");
2828 return ERROR_FAIL;
2831 return rc;
2834 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
2836 int rc;
2838 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
2839 0, NULL);
2840 if (rc < 0) {
2841 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2842 "Can not destroy tmem pools");
2843 return ERROR_FAIL;
2846 return rc;
2849 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
2851 int rc;
2853 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
2854 0, NULL);
2855 if (rc < 0) {
2856 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2857 "Can not thaw tmem pools");
2858 return ERROR_FAIL;
2861 return rc;
2864 static int32_t tmem_setop_from_string(char *set_name)
2866 if (!strcmp(set_name, "weight"))
2867 return TMEMC_SET_WEIGHT;
2868 else if (!strcmp(set_name, "cap"))
2869 return TMEMC_SET_CAP;
2870 else if (!strcmp(set_name, "compress"))
2871 return TMEMC_SET_COMPRESS;
2872 else
2873 return -1;
2876 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
2878 int rc;
2879 int32_t subop = tmem_setop_from_string(name);
2881 if (subop == -1) {
2882 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
2883 "Invalid set, valid sets are <weight|cap|compress>");
2884 return ERROR_INVAL;
2886 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
2887 if (rc < 0) {
2888 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2889 "Can not set tmem %s", name);
2890 return ERROR_FAIL;
2893 return rc;
2896 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
2897 char* uuid, int auth)
2899 int rc;
2901 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
2902 if (rc < 0) {
2903 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2904 "Can not set tmem shared auth");
2905 return ERROR_FAIL;
2908 return rc;
2911 int libxl_tmem_freeable(libxl_ctx *ctx)
2913 int rc;
2915 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
2916 if (rc < 0) {
2917 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2918 "Can not get tmem freeable memory");
2919 return ERROR_FAIL;
2922 return rc;
2925 void libxl_file_reference_destroy(libxl_file_reference *f)
2927 libxl__file_reference_unmap(f);
2928 free(f->path);
2931 int libxl_get_freecpus(libxl_ctx *ctx, libxl_cpumap *cpumap)
2933 int ncpus;
2935 ncpus = libxl_get_max_cpus(ctx);
2936 if (ncpus == 0)
2937 return ERROR_FAIL;
2939 cpumap->map = xc_cpupool_freeinfo(ctx->xch);
2940 if (cpumap->map == NULL)
2941 return ERROR_FAIL;
2943 cpumap->size = (ncpus + 7) / 8;
2945 return 0;
2948 int libxl_create_cpupool(libxl_ctx *ctx, const char *name, int schedid,
2949 libxl_cpumap cpumap, libxl_uuid *uuid,
2950 uint32_t *poolid)
2952 libxl__gc gc = LIBXL_INIT_GC(ctx);
2953 int rc;
2954 int i;
2955 xs_transaction_t t;
2956 char *uuid_string;
2958 uuid_string = libxl__uuid2string(&gc, *uuid);
2959 if (!uuid_string) {
2960 libxl__free_all(&gc);
2961 return ERROR_NOMEM;
2964 rc = xc_cpupool_create(ctx->xch, poolid, schedid);
2965 if (rc) {
2966 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2967 "Could not create cpupool");
2968 libxl__free_all(&gc);
2969 return ERROR_FAIL;
2972 libxl_for_each_cpu(i, cpumap)
2973 if (libxl_cpumap_test(&cpumap, i)) {
2974 rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
2975 if (rc) {
2976 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
2977 "Error moving cpu to cpupool");
2978 libxl_destroy_cpupool(ctx, *poolid);
2979 libxl__free_all(&gc);
2980 return ERROR_FAIL;
2984 for (;;) {
2985 t = xs_transaction_start(ctx->xsh);
2987 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/pool/%d", *poolid));
2988 libxl__xs_write(&gc, t,
2989 libxl__sprintf(&gc, "/local/pool/%d/uuid", *poolid),
2990 "%s", uuid_string);
2991 libxl__xs_write(&gc, t,
2992 libxl__sprintf(&gc, "/local/pool/%d/name", *poolid),
2993 "%s", name);
2995 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN)) {
2996 libxl__free_all(&gc);
2997 return 0;
3002 int libxl_destroy_cpupool(libxl_ctx *ctx, uint32_t poolid)
3004 libxl__gc gc = LIBXL_INIT_GC(ctx);
3005 int rc, i;
3006 xc_cpupoolinfo_t *info;
3007 xs_transaction_t t;
3008 libxl_cpumap cpumap;
3010 info = xc_cpupool_getinfo(ctx->xch, poolid);
3011 if (info == NULL) {
3012 libxl__free_all(&gc);
3013 return ERROR_NOMEM;
3016 rc = ERROR_INVAL;
3017 if ((info->cpupool_id != poolid) || (info->n_dom))
3018 goto out;
3020 rc = ERROR_NOMEM;
3021 if (libxl_cpumap_alloc(ctx, &cpumap))
3022 goto out;
3024 memcpy(cpumap.map, info->cpumap, cpumap.size);
3025 libxl_for_each_cpu(i, cpumap)
3026 if (libxl_cpumap_test(&cpumap, i)) {
3027 rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
3028 if (rc) {
3029 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3030 "Error removing cpu from cpupool");
3031 rc = ERROR_FAIL;
3032 goto out1;
3036 rc = xc_cpupool_destroy(ctx->xch, poolid);
3037 if (rc) {
3038 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "Could not destroy cpupool");
3039 rc = ERROR_FAIL;
3040 goto out1;
3043 for (;;) {
3044 t = xs_transaction_start(ctx->xsh);
3046 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/pool/%d", poolid));
3048 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3049 break;
3052 rc = 0;
3054 out1:
3055 libxl_cpumap_destroy(&cpumap);
3056 out:
3057 xc_cpupool_infofree(ctx->xch, info);
3058 libxl__free_all(&gc);
3060 return rc;
3063 int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid)
3065 libxl__gc gc = LIBXL_INIT_GC(ctx);
3066 xs_transaction_t t;
3067 xc_cpupoolinfo_t *info;
3068 int rc;
3070 info = xc_cpupool_getinfo(ctx->xch, poolid);
3071 if (info == NULL) {
3072 libxl__free_all(&gc);
3073 return ERROR_NOMEM;
3076 rc = ERROR_INVAL;
3077 if (info->cpupool_id != poolid)
3078 goto out;
3080 rc = 0;
3082 for (;;) {
3083 t = xs_transaction_start(ctx->xsh);
3085 libxl__xs_write(&gc, t,
3086 libxl__sprintf(&gc, "/local/pool/%d/name", poolid),
3087 "%s", name);
3089 if (xs_transaction_end(ctx->xsh, t, 0))
3090 break;
3092 if (errno == EAGAIN)
3093 continue;
3095 rc = ERROR_FAIL;
3096 break;
3099 out:
3100 xc_cpupool_infofree(ctx->xch, info);
3101 libxl__free_all(&gc);
3103 return rc;
3106 int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
3108 int rc;
3110 rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
3111 if (rc) {
3112 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3113 "Error moving cpu to cpupool");
3114 return ERROR_FAIL;
3116 return 0;
3119 int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3121 int rc = 0;
3122 int cpu;
3123 libxl_cpumap freemap;
3124 libxl_topologyinfo topology;
3126 if (libxl_get_freecpus(ctx, &freemap)) {
3127 return ERROR_FAIL;
3130 if (libxl_get_topologyinfo(ctx, &topology)) {
3131 rc = ERROR_FAIL;
3132 goto out;
3135 *cpus = 0;
3136 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3137 if (libxl_cpumap_test(&freemap, cpu) &&
3138 (topology.nodemap.array[cpu] == node) &&
3139 !libxl_cpupool_cpuadd(ctx, poolid, cpu)) {
3140 (*cpus)++;
3144 libxl_topologyinfo_destroy(&topology);
3146 out:
3147 libxl_cpumap_destroy(&freemap);
3148 return rc;
3151 int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
3153 int rc;
3155 rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
3156 if (rc) {
3157 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3158 "Error removing cpu from cpupool");
3159 return ERROR_FAIL;
3161 return 0;
3164 int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus)
3166 int ret = 0;
3167 int n_pools;
3168 int p;
3169 int cpu;
3170 libxl_topologyinfo topology;
3171 libxl_cpupoolinfo *poolinfo;
3173 poolinfo = libxl_list_cpupool(ctx, &n_pools);
3174 if (!poolinfo) {
3175 return ERROR_NOMEM;
3178 if (libxl_get_topologyinfo(ctx, &topology)) {
3179 ret = ERROR_FAIL;
3180 goto out;
3183 *cpus = 0;
3184 for (p = 0; p < n_pools; p++) {
3185 if (poolinfo[p].poolid == poolid) {
3186 for (cpu = 0; cpu < topology.nodemap.entries; cpu++) {
3187 if ((topology.nodemap.array[cpu] == node) &&
3188 libxl_cpumap_test(&poolinfo[p].cpumap, cpu) &&
3189 !libxl_cpupool_cpuremove(ctx, poolid, cpu)) {
3190 (*cpus)++;
3196 libxl_topologyinfo_destroy(&topology);
3198 out:
3199 for (p = 0; p < n_pools; p++) {
3200 libxl_cpupoolinfo_destroy(poolinfo + p);
3203 return ret;
3206 int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
3208 libxl__gc gc = LIBXL_INIT_GC(ctx);
3209 int rc;
3210 char *dom_path;
3211 char *vm_path;
3212 char *poolname;
3213 xs_transaction_t t;
3215 dom_path = libxl__xs_get_dompath(&gc, domid);
3216 if (!dom_path) {
3217 libxl__free_all(&gc);
3218 return ERROR_FAIL;
3221 rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
3222 if (rc) {
3223 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3224 "Error moving domain to cpupool");
3225 libxl__free_all(&gc);
3226 return ERROR_FAIL;
3229 for (;;) {
3230 t = xs_transaction_start(ctx->xsh);
3232 poolname = libxl__cpupoolid_to_name(&gc, poolid);
3233 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
3234 if (!vm_path)
3235 break;
3237 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/pool_name", vm_path),
3238 "%s", poolname);
3240 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3241 break;
3244 libxl__free_all(&gc);
3245 return 0;