debuggers.hg

view tools/libxl/libxl.c @ 22468:9ffd785319e0

tools/libxl: use qdisk if blktap2 is not available

Whenever blktap2 is not available use qdisk as block backend instead.

[ This feature will only work with the relevant changesets from
qemu-xen-unstable, recently applied. -iwj ]

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
date Tue Nov 23 19:23:22 2010 +0000 (2010-11-23)
parents c553d3035fd5
children b609b1b983d6
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) return rc;
519 xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
521 if (!xs_transaction_end(ctx->xsh, t, 0))
522 if (errno == EAGAIN)
523 goto retry_transaction;
525 libxl__free_all(&gc);
526 return 0;
527 }
529 static void xcinfo2xlinfo(const xc_domaininfo_t *xcinfo,
530 libxl_dominfo *xlinfo)
531 {
532 memcpy(&(xlinfo->uuid), xcinfo->handle, sizeof(xen_domain_handle_t));
533 xlinfo->domid = xcinfo->domain;
535 xlinfo->dying = !!(xcinfo->flags&XEN_DOMINF_dying);
536 xlinfo->shutdown = !!(xcinfo->flags&XEN_DOMINF_shutdown);
537 xlinfo->paused = !!(xcinfo->flags&XEN_DOMINF_paused);
538 xlinfo->blocked = !!(xcinfo->flags&XEN_DOMINF_blocked);
539 xlinfo->running = !!(xcinfo->flags&XEN_DOMINF_running);
541 if (xlinfo->shutdown || xlinfo->dying)
542 xlinfo->shutdown_reason = (xcinfo->flags>>XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask;
543 else
544 xlinfo->shutdown_reason = ~0;
546 xlinfo->current_memkb = PAGE_TO_MEMKB(xcinfo->tot_pages);
547 xlinfo->max_memkb = PAGE_TO_MEMKB(xcinfo->max_pages);
548 xlinfo->cpu_time = xcinfo->cpu_time;
549 xlinfo->vcpu_max_id = xcinfo->max_vcpu_id;
550 xlinfo->vcpu_online = xcinfo->nr_online_vcpus;
551 }
553 libxl_dominfo * libxl_list_domain(libxl_ctx *ctx, int *nb_domain)
554 {
555 libxl_dominfo *ptr;
556 int i, ret;
557 xc_domaininfo_t info[1024];
558 int size = 1024;
560 ptr = calloc(size, sizeof(libxl_dominfo));
561 if (!ptr) {
562 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating domain info");
563 return NULL;
564 }
566 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
567 if (ret<0) {
568 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
569 return NULL;
570 }
572 for (i = 0; i < ret; i++) {
573 xcinfo2xlinfo(&info[i], &ptr[i]);
574 }
575 *nb_domain = ret;
576 return ptr;
577 }
579 int libxl_domain_info(libxl_ctx *ctx, libxl_dominfo *info_r,
580 uint32_t domid) {
581 xc_domaininfo_t xcinfo;
582 int ret;
584 ret = xc_domain_getinfolist(ctx->xch, domid, 1, &xcinfo);
585 if (ret<0) {
586 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
587 return ERROR_FAIL;
588 }
589 if (ret==0 || xcinfo.domain != domid) return ERROR_INVAL;
591 xcinfo2xlinfo(&xcinfo, info_r);
592 return 0;
593 }
595 libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx *ctx, int *nb_pool)
596 {
597 libxl_cpupoolinfo *ptr, *tmp;
598 int i;
599 xc_cpupoolinfo_t *info;
600 uint32_t poolid;
602 ptr = NULL;
604 poolid = 0;
605 for (i = 0;; i++) {
606 info = xc_cpupool_getinfo(ctx->xch, poolid);
607 if (info == NULL)
608 break;
609 tmp = realloc(ptr, (i + 1) * sizeof(libxl_cpupoolinfo));
610 if (!tmp) {
611 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpupool info");
612 free(ptr);
613 xc_cpupool_infofree(ctx->xch, info);
614 return NULL;
615 }
616 ptr = tmp;
617 ptr[i].poolid = info->cpupool_id;
618 ptr[i].sched_id = info->sched_id;
619 ptr[i].n_dom = info->n_dom;
620 if (libxl_cpumap_alloc(ctx, &ptr[i].cpumap)) {
621 xc_cpupool_infofree(ctx->xch, info);
622 break;
623 }
624 memcpy(ptr[i].cpumap.map, info->cpumap, ptr[i].cpumap.size);
625 poolid = info->cpupool_id + 1;
626 xc_cpupool_infofree(ctx->xch, info);
627 }
629 *nb_pool = i;
630 return ptr;
631 }
633 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
634 libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm)
635 {
636 libxl_vminfo *ptr;
637 int index, i, ret;
638 xc_domaininfo_t info[1024];
639 int size = 1024;
641 ptr = calloc(size, sizeof(libxl_dominfo));
642 if (!ptr)
643 return NULL;
645 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
646 if (ret<0) {
647 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "geting domain info list");
648 return NULL;
649 }
650 for (index = i = 0; i < ret; i++) {
651 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
652 continue;
653 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
654 ptr[index].domid = info[i].domain;
656 index++;
657 }
658 *nb_vm = index;
659 return ptr;
660 }
662 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
663 uint32_t domid, int fd)
664 {
665 int hvm = libxl__domain_is_hvm(ctx, domid);
666 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
667 int debug = info != NULL && info->flags & XL_SUSPEND_DEBUG;
668 int rc = 0;
670 rc = libxl__domain_suspend_common(ctx, domid, fd, hvm, live, debug);
671 if (!rc && hvm)
672 rc = libxl__domain_save_device_model(ctx, domid, fd);
673 return rc;
674 }
676 int libxl_domain_pause(libxl_ctx *ctx, uint32_t domid)
677 {
678 int ret;
679 ret = xc_domain_pause(ctx->xch, domid);
680 if (ret<0) {
681 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "pausing domain %d", domid);
682 return ERROR_FAIL;
683 }
684 return 0;
685 }
687 int libxl_domain_core_dump(libxl_ctx *ctx, uint32_t domid,
688 const char *filename)
689 {
690 int ret;
691 ret = xc_domain_dumpcore(ctx->xch, domid, filename);
692 if (ret<0) {
693 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "core dumping domain %d to %s",
694 domid, filename);
695 return ERROR_FAIL;
696 }
697 return 0;
698 }
700 int libxl_domain_unpause(libxl_ctx *ctx, uint32_t domid)
701 {
702 libxl__gc gc = LIBXL_INIT_GC(ctx);
703 char *path;
704 char *state;
705 int ret, rc = 0;
707 if (libxl__domain_is_hvm(ctx, domid)) {
708 path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d/state", domid);
709 state = libxl__xs_read(&gc, XBT_NULL, path);
710 if (state != NULL && !strcmp(state, "paused")) {
711 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d/command", domid), "continue");
712 libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL);
713 }
714 }
715 ret = xc_domain_unpause(ctx->xch, domid);
716 if (ret<0) {
717 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain %d", domid);
718 rc = ERROR_FAIL;
719 }
720 libxl__free_all(&gc);
721 return rc;
722 }
724 static char *req_table[] = {
725 [0] = "poweroff",
726 [1] = "reboot",
727 [2] = "suspend",
728 [3] = "crash",
729 [4] = "halt",
730 };
732 int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid, int req)
733 {
734 libxl__gc gc = LIBXL_INIT_GC(ctx);
735 char *shutdown_path;
736 char *dom_path;
738 if (req > ARRAY_SIZE(req_table)) {
739 libxl__free_all(&gc);
740 return ERROR_INVAL;
741 }
743 dom_path = libxl__xs_get_dompath(&gc, domid);
744 if (!dom_path) {
745 libxl__free_all(&gc);
746 return ERROR_FAIL;
747 }
749 shutdown_path = libxl__sprintf(&gc, "%s/control/shutdown", dom_path);
751 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
752 if (libxl__domain_is_hvm(ctx,domid)) {
753 unsigned long acpi_s_state = 0;
754 unsigned long pvdriver = 0;
755 int ret;
756 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
757 if (ret<0) {
758 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting ACPI S-state");
759 return ERROR_FAIL;
760 }
761 ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
762 if (ret<0) {
763 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting HVM callback IRQ");
764 return ERROR_FAIL;
765 }
766 if (!pvdriver || acpi_s_state != 0) {
767 ret = xc_domain_shutdown(ctx->xch, domid, req);
768 if (ret<0) {
769 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unpausing domain");
770 return ERROR_FAIL;
771 }
772 }
773 }
774 libxl__free_all(&gc);
775 return 0;
776 }
778 int libxl_get_wait_fd(libxl_ctx *ctx, int *fd)
779 {
780 *fd = xs_fileno(ctx->xsh);
781 return 0;
782 }
784 int libxl_wait_for_domain_death(libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
785 {
786 waiter->path = strdup("@releaseDomain");
787 if (asprintf(&(waiter->token), "%d", LIBXL_EVENT_DOMAIN_DEATH) < 0)
788 return -1;
789 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
790 return -1;
791 return 0;
792 }
794 int libxl_wait_for_disk_ejects(libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
795 {
796 libxl__gc gc = LIBXL_INIT_GC(ctx);
797 int i, rc = -1;
798 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
800 if (!domid)
801 domid = guest_domid;
803 for (i = 0; i < num_disks; i++) {
804 if (asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject",
805 libxl__xs_get_dompath(&gc, domid),
806 libxl__device_disk_dev_number(disks[i].virtpath)) < 0)
807 goto out;
808 if (asprintf(&(waiter[i].token), "%d", LIBXL_EVENT_DISK_EJECT) < 0)
809 goto out;
810 xs_watch(ctx->xsh, waiter->path, waiter->token);
811 }
812 rc = 0;
813 out:
814 libxl__free_all(&gc);
815 return rc;
816 }
818 int libxl_get_event(libxl_ctx *ctx, libxl_event *event)
819 {
820 unsigned int num;
821 char **events = xs_read_watch(ctx->xsh, &num);
822 if (num != 2) {
823 free(events);
824 return ERROR_FAIL;
825 }
826 event->path = strdup(events[XS_WATCH_PATH]);
827 event->token = strdup(events[XS_WATCH_TOKEN]);
828 event->type = atoi(event->token);
829 free(events);
830 return 0;
831 }
833 int libxl_stop_waiting(libxl_ctx *ctx, libxl_waiter *waiter)
834 {
835 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
836 return ERROR_FAIL;
837 else
838 return 0;
839 }
841 int libxl_free_event(libxl_event *event)
842 {
843 free(event->path);
844 free(event->token);
845 return 0;
846 }
848 int libxl_free_waiter(libxl_waiter *waiter)
849 {
850 free(waiter->path);
851 free(waiter->token);
852 return 0;
853 }
855 int libxl_event_get_domain_death_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_dominfo *info)
856 {
857 if (libxl_domain_info(ctx, info, domid) < 0)
858 return 0;
860 if (info->running || (!info->shutdown && !info->dying))
861 return ERROR_INVAL;
863 return 1;
864 }
866 int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
867 {
868 libxl__gc gc = LIBXL_INIT_GC(ctx);
869 char *path;
870 char *backend;
871 char *value;
873 value = libxl__xs_read(&gc, XBT_NULL, event->path);
875 if (!value || strcmp(value, "eject")) {
876 libxl__free_all(&gc);
877 return 0;
878 }
880 path = strdup(event->path);
881 path[strlen(path) - 6] = '\0';
882 backend = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend", path));
884 disk->backend_domid = 0;
885 disk->domid = domid;
886 disk->physpath = NULL;
887 disk->phystype = 0;
888 /* this value is returned to the user: do not free right away */
889 disk->virtpath = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/dev", backend));
890 disk->unpluggable = 1;
891 disk->readwrite = 0;
892 disk->is_cdrom = 1;
894 free(path);
895 libxl__free_all(&gc);
896 return 1;
897 }
899 static int libxl_destroy_device_model(libxl_ctx *ctx, uint32_t domid)
900 {
901 libxl__gc gc = LIBXL_INIT_GC(ctx);
902 char *pid;
903 int ret;
905 pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/image/device-model-pid", domid));
906 if (!pid) {
907 int stubdomid = libxl_get_stubdom_id(ctx, domid);
908 if (!stubdomid) {
909 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't find device model's pid");
910 ret = ERROR_INVAL;
911 goto out;
912 }
913 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
914 ret = libxl_domain_destroy(ctx, stubdomid, 0);
915 if (ret)
916 goto out;
917 } else {
918 ret = kill(atoi(pid), SIGHUP);
919 if (ret < 0 && errno == ESRCH) {
920 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device Model already exited");
921 ret = 0;
922 } else if (ret == 0) {
923 LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Device Model signaled");
924 ret = 0;
925 } else {
926 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "failed to kill Device Model [%d]",
927 atoi(pid));
928 ret = ERROR_FAIL;
929 goto out;
930 }
931 }
932 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", domid));
934 out:
935 libxl__free_all(&gc);
936 return ret;
937 }
939 int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
940 {
941 libxl__gc gc = LIBXL_INIT_GC(ctx);
942 char *dom_path;
943 char *vm_path;
944 int rc, dm_present;
946 if (libxl__domain_is_hvm(ctx, domid)) {
947 dm_present = 1;
948 } else {
949 char *pid;
950 pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/image/device-model-pid", domid));
951 dm_present = (pid != NULL);
952 }
954 dom_path = libxl__xs_get_dompath(&gc, domid);
955 if (!dom_path) {
956 rc = ERROR_FAIL;
957 goto out;
958 }
960 if (libxl_device_pci_shutdown(ctx, domid) < 0)
961 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "pci shutdown failed for domid %d", domid);
962 if (dm_present) {
963 xs_write(ctx->xsh, XBT_NULL,
964 libxl__sprintf(&gc, "/local/domain/0/device-model/%d/command", domid),
965 "shutdown", strlen("shutdown"));
966 }
967 rc = xc_domain_pause(ctx->xch, domid);
968 if (rc < 0) {
969 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
970 }
971 if (dm_present) {
972 if (libxl_destroy_device_model(ctx, domid) < 0)
973 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
974 }
975 if (libxl__devices_destroy(ctx, domid, force) < 0)
976 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
978 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
979 if (vm_path)
980 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
981 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", vm_path);
983 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
984 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", dom_path);
986 libxl__userdata_destroyall(ctx, domid);
988 rc = xc_domain_destroy(ctx->xch, domid);
989 if (rc < 0) {
990 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
991 rc = ERROR_FAIL;
992 goto out;
993 }
994 rc = 0;
995 out:
996 libxl__free_all(&gc);
997 return 0;
998 }
1000 int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num, libxl_console_constype type)
1002 libxl__gc gc = LIBXL_INIT_GC(ctx);
1003 char *p = libxl__sprintf(&gc, "%s/xenconsole", libxl_private_bindir_path());
1004 char *domid_s = libxl__sprintf(&gc, "%d", domid);
1005 char *cons_num_s = libxl__sprintf(&gc, "%d", cons_num);
1006 char *cons_type_s;
1008 switch (type) {
1009 case LIBXL_CONSTYPE_PV:
1010 cons_type_s = "pv";
1011 break;
1012 case LIBXL_CONSTYPE_SERIAL:
1013 cons_type_s = "serial";
1014 break;
1015 default:
1016 goto out;
1019 execl(p, p, domid_s, "--num", cons_num_s, "--type", cons_type_s, (void *)NULL);
1021 out:
1022 libxl__free_all(&gc);
1023 return ERROR_FAIL;
1026 int libxl_primary_console_exec(libxl_ctx *ctx, uint32_t domid_vm)
1028 uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm);
1029 if (stubdomid)
1030 return libxl_console_exec(ctx, stubdomid, 1, LIBXL_CONSTYPE_PV);
1031 else {
1032 if (libxl__domain_is_hvm(ctx, domid_vm))
1033 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_SERIAL);
1034 else
1035 return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_PV);
1039 int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
1041 libxl__gc gc = LIBXL_INIT_GC(ctx);
1042 const char *vnc_port, *vfb_back;
1043 const char *vnc_listen = NULL, *vnc_pass = NULL;
1044 int port = 0, autopass_fd = -1;
1045 char *vnc_bin, *args[] = {
1046 "vncviewer",
1047 NULL, /* hostname:display */
1048 NULL, /* -autopass */
1049 NULL,
1050 };
1052 vnc_port = libxl__xs_read(&gc, XBT_NULL,
1053 libxl__sprintf(&gc,
1054 "/local/domain/%d/console/vnc-port", domid));
1055 if ( vnc_port )
1056 port = atoi(vnc_port) - 5900;
1058 vfb_back = libxl__xs_read(&gc, XBT_NULL,
1059 libxl__sprintf(&gc,
1060 "/local/domain/%d/device/vfb/0/backend", domid));
1061 if ( vfb_back ) {
1062 vnc_listen = libxl__xs_read(&gc, XBT_NULL,
1063 libxl__sprintf(&gc,
1064 "/local/domain/%d/console/vnc-listen", domid));
1065 if ( autopass )
1066 vnc_pass = libxl__xs_read(&gc, XBT_NULL,
1067 libxl__sprintf(&gc,
1068 "/local/domain/%d/console/vnc-pass", domid));
1071 if ( NULL == vnc_listen )
1072 vnc_listen = "localhost";
1074 if ( (vnc_bin = getenv("VNCVIEWER")) )
1075 args[0] = vnc_bin;
1077 args[1] = libxl__sprintf(&gc, "%s:%d", vnc_listen, port);
1079 if ( vnc_pass ) {
1080 char tmpname[] = "/tmp/vncautopass.XXXXXX";
1081 autopass_fd = mkstemp(tmpname);
1082 if ( autopass_fd < 0 )
1083 goto skip_autopass;
1085 if ( unlink(tmpname) )
1086 /* should never happen */
1087 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unlink %s failed", tmpname);
1089 if ( libxl_write_exactly(ctx, autopass_fd, vnc_pass, strlen(vnc_pass),
1090 tmpname, "vnc password") ) {
1091 do { close(autopass_fd); } while(errno == EINTR);
1092 goto skip_autopass;
1095 args[2] = "-autopass";
1098 skip_autopass:
1099 libxl__free_all(&gc);
1100 libxl__exec(autopass_fd, -1, -1, args[0], args);
1101 return 0;
1104 static char ** libxl_build_device_model_args_old(libxl__gc *gc,
1105 libxl_device_model_info *info,
1106 libxl_device_nic *vifs,
1107 int num_vifs)
1109 int num = 0, i;
1110 flexarray_t *dm_args;
1111 dm_args = flexarray_make(16, 1);
1113 if (!dm_args)
1114 return NULL;
1116 flexarray_set(dm_args, num++, "qemu-dm");
1117 flexarray_set(dm_args, num++, "-d");
1119 flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid));
1121 if (info->dom_name) {
1122 flexarray_set(dm_args, num++, "-domain-name");
1123 flexarray_set(dm_args, num++, info->dom_name);
1125 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
1126 flexarray_set(dm_args, num++, "-vnc");
1127 if (info->vncdisplay) {
1128 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
1129 flexarray_set(
1130 dm_args, num++,
1131 libxl__sprintf(gc, "%s:%d%s",
1132 info->vnclisten,
1133 info->vncdisplay,
1134 info->vncpasswd ? ",password" : ""));
1135 } else {
1136 flexarray_set(dm_args, num++, libxl__sprintf(gc, "127.0.0.1:%d", info->vncdisplay));
1138 } else if (info->vnclisten) {
1139 if (strchr(info->vnclisten, ':') != NULL) {
1140 flexarray_set(dm_args, num++, info->vnclisten);
1141 } else {
1142 flexarray_set(dm_args, num++, libxl__sprintf(gc, "%s:0", info->vnclisten));
1144 } else {
1145 flexarray_set(dm_args, num++, "127.0.0.1:0");
1147 if (info->vncunused) {
1148 flexarray_set(dm_args, num++, "-vncunused");
1151 if (info->sdl) {
1152 flexarray_set(dm_args, num++, "-sdl");
1153 if (!info->opengl) {
1154 flexarray_set(dm_args, num++, "-disable-opengl");
1157 if (info->keymap) {
1158 flexarray_set(dm_args, num++, "-k");
1159 flexarray_set(dm_args, num++, info->keymap);
1161 if (info->nographic && (!info->sdl && !info->vnc)) {
1162 flexarray_set(dm_args, num++, "-nographic");
1164 if (info->serial) {
1165 flexarray_set(dm_args, num++, "-serial");
1166 flexarray_set(dm_args, num++, info->serial);
1168 if (info->type == XENFV) {
1169 int ioemu_vifs = 0;
1171 if (info->videoram) {
1172 flexarray_set(dm_args, num++, "-videoram");
1173 flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->videoram));
1175 if (info->stdvga) {
1176 flexarray_set(dm_args, num++, "-std-vga");
1179 if (info->boot) {
1180 flexarray_set(dm_args, num++, "-boot");
1181 flexarray_set(dm_args, num++, info->boot);
1183 if (info->usb || info->usbdevice) {
1184 flexarray_set(dm_args, num++, "-usb");
1185 if (info->usbdevice) {
1186 flexarray_set(dm_args, num++, "-usbdevice");
1187 flexarray_set(dm_args, num++, info->usbdevice);
1190 if (info->soundhw) {
1191 flexarray_set(dm_args, num++, "-soundhw");
1192 flexarray_set(dm_args, num++, info->soundhw);
1194 if (info->apic) {
1195 flexarray_set(dm_args, num++, "-acpi");
1197 if (info->vcpus > 1) {
1198 flexarray_set(dm_args, num++, "-vcpus");
1199 flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus));
1201 if (info->vcpu_avail) {
1202 flexarray_set(dm_args, num++, "-vcpu_avail");
1203 flexarray_set(dm_args, num++, libxl__sprintf(gc, "0x%x", info->vcpu_avail));
1205 for (i = 0; i < num_vifs; i++) {
1206 if (vifs[i].nictype == NICTYPE_IOEMU) {
1207 char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1208 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
1209 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
1210 char *ifname;
1211 if (!vifs[i].ifname)
1212 ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid);
1213 else
1214 ifname = vifs[i].ifname;
1215 flexarray_set(dm_args, num++, "-net");
1216 flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s",
1217 vifs[i].devid, smac, vifs[i].model));
1218 flexarray_set(dm_args, num++, "-net");
1219 flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,bridge=%s,script=no",
1220 vifs[i].devid, ifname, vifs[i].bridge));
1221 ioemu_vifs++;
1224 /* If we have no emulated nics, tell qemu not to create any */
1225 if ( ioemu_vifs == 0 ) {
1226 flexarray_set(dm_args, num++, "-net");
1227 flexarray_set(dm_args, num++, "none");
1230 if (info->saved_state) {
1231 flexarray_set(dm_args, num++, "-loadvm");
1232 flexarray_set(dm_args, num++, info->saved_state);
1234 for (i = 0; info->extra && info->extra[i] != NULL; i++)
1235 flexarray_set(dm_args, num++, info->extra[i]);
1236 flexarray_set(dm_args, num++, "-M");
1237 if (info->type == XENPV)
1238 flexarray_set(dm_args, num++, "xenpv");
1239 else
1240 flexarray_set(dm_args, num++, "xenfv");
1241 flexarray_set(dm_args, num++, NULL);
1242 return (char **) flexarray_contents(dm_args);
1245 static char ** libxl_build_device_model_args_new(libxl__gc *gc,
1246 libxl_device_model_info *info,
1247 libxl_device_nic *vifs,
1248 int num_vifs)
1250 int num = 0, i;
1251 flexarray_t *dm_args;
1252 int nb;
1253 libxl_device_disk *disks;
1255 dm_args = flexarray_make(16, 1);
1256 if (!dm_args)
1257 return NULL;
1259 flexarray_set(dm_args, num++, "qemu-system-xen");
1260 flexarray_set(dm_args, num++, "-xen-domid");
1262 flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->domid));
1264 if (info->dom_name) {
1265 flexarray_set(dm_args, num++, "-name");
1266 flexarray_set(dm_args, num++, info->dom_name);
1268 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
1269 int display = 0;
1270 const char *listen = "127.0.0.1";
1272 flexarray_set(dm_args, num++, "-vnc");
1274 if (info->vncdisplay) {
1275 display = info->vncdisplay;
1276 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
1277 listen = info->vnclisten;
1279 } else if (info->vnclisten) {
1280 listen = info->vnclisten;
1283 if (strchr(listen, ':') != NULL)
1284 flexarray_set(dm_args, num++,
1285 libxl__sprintf(gc, "%s%s", listen,
1286 info->vncunused ? ",to=99" : ""));
1287 else
1288 flexarray_set(dm_args, num++,
1289 libxl__sprintf(gc, "%s:%d%s", listen, display,
1290 info->vncunused ? ",to=99" : ""));
1292 if (info->sdl) {
1293 flexarray_set(dm_args, num++, "-sdl");
1295 if (info->keymap) {
1296 flexarray_set(dm_args, num++, "-k");
1297 flexarray_set(dm_args, num++, info->keymap);
1299 if (info->nographic && (!info->sdl && !info->vnc)) {
1300 flexarray_set(dm_args, num++, "-nographic");
1302 if (info->serial) {
1303 flexarray_set(dm_args, num++, "-serial");
1304 flexarray_set(dm_args, num++, info->serial);
1306 if (info->type == XENFV) {
1307 int ioemu_vifs = 0;
1309 if (info->stdvga) {
1310 flexarray_set(dm_args, num++, "-vga");
1311 flexarray_set(dm_args, num++, "std");
1314 if (info->boot) {
1315 flexarray_set(dm_args, num++, "-boot");
1316 flexarray_set(dm_args, num++, libxl__sprintf(gc, "order=%s", info->boot));
1318 if (info->usb || info->usbdevice) {
1319 flexarray_set(dm_args, num++, "-usb");
1320 if (info->usbdevice) {
1321 flexarray_set(dm_args, num++, "-usbdevice");
1322 flexarray_set(dm_args, num++, info->usbdevice);
1325 if (info->soundhw) {
1326 flexarray_set(dm_args, num++, "-soundhw");
1327 flexarray_set(dm_args, num++, info->soundhw);
1329 if (!info->apic) {
1330 flexarray_set(dm_args, num++, "-no-acpi");
1332 if (info->vcpus > 1) {
1333 flexarray_set(dm_args, num++, "-smp");
1334 if (info->vcpu_avail)
1335 flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d,maxcpus=%d", info->vcpus, info->vcpu_avail));
1336 else
1337 flexarray_set(dm_args, num++, libxl__sprintf(gc, "%d", info->vcpus));
1339 for (i = 0; i < num_vifs; i++) {
1340 if (vifs[i].nictype == NICTYPE_IOEMU) {
1341 char *smac = libxl__sprintf(gc, "%02x:%02x:%02x:%02x:%02x:%02x",
1342 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
1343 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
1344 if (!vifs[i].ifname)
1345 vifs[i].ifname = libxl__sprintf(gc, "tap%d.%d", info->domid, vifs[i].devid);
1346 flexarray_set(dm_args, num++, "-net");
1347 flexarray_set(dm_args, num++, libxl__sprintf(gc, "nic,vlan=%d,macaddr=%s,model=%s",
1348 vifs[i].devid, smac, vifs[i].model));
1349 flexarray_set(dm_args, num++, "-net");
1350 flexarray_set(dm_args, num++, libxl__sprintf(gc, "tap,vlan=%d,ifname=%s,script=no",
1351 vifs[i].devid, vifs[i].ifname));
1352 ioemu_vifs++;
1355 /* If we have no emulated nics, tell qemu not to create any */
1356 if ( ioemu_vifs == 0 ) {
1357 flexarray_set(dm_args, num++, "-net");
1358 flexarray_set(dm_args, num++, "none");
1361 if (info->saved_state) {
1362 flexarray_set(dm_args, num++, "-loadvm");
1363 flexarray_set(dm_args, num++, info->saved_state);
1365 for (i = 0; info->extra && info->extra[i] != NULL; i++)
1366 flexarray_set(dm_args, num++, info->extra[i]);
1367 flexarray_set(dm_args, num++, "-M");
1368 if (info->type == XENPV)
1369 flexarray_set(dm_args, num++, "xenpv");
1370 else
1371 flexarray_set(dm_args, num++, "xenfv");
1373 disks = libxl_device_disk_list(libxl__gc_owner(gc), info->domid, &nb);
1374 for (i; i < nb; i++) {
1375 if ( disks[i].is_cdrom ) {
1376 flexarray_set(dm_args, num++, "-cdrom");
1377 flexarray_set(dm_args, num++, disks[i].physpath);
1378 }else{
1379 flexarray_set(dm_args, num++, libxl__sprintf(gc, "-%s", disks[i].virtpath));
1380 flexarray_set(dm_args, num++, disks[i].physpath);
1382 libxl_device_disk_destroy(&disks[i]);
1384 free(disks);
1385 flexarray_set(dm_args, num++, NULL);
1386 return (char **) flexarray_contents(dm_args);
1389 static char ** libxl_build_device_model_args(libxl__gc *gc,
1390 libxl_device_model_info *info,
1391 libxl_device_nic *vifs,
1392 int num_vifs)
1394 libxl_ctx *ctx = libxl__gc_owner(gc);
1395 int new_qemu;
1397 new_qemu = libxl_check_device_model_version(ctx, info->device_model);
1399 if (new_qemu == 1) {
1400 return libxl_build_device_model_args_new(gc, info, vifs, num_vifs);
1401 } else {
1402 return libxl_build_device_model_args_old(gc, info, vifs, num_vifs);
1406 static void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
1408 libxl_device_model_starting *starting = for_spawn;
1409 struct xs_handle *xsh;
1410 char *path = NULL, *pid = NULL;
1411 int len;
1413 if (asprintf(&path, "%s/%s", starting->dom_path, "image/device-model-pid") < 0)
1414 goto out;
1416 len = asprintf(&pid, "%d", innerchild);
1417 if (len < 0)
1418 goto out;
1420 /* we mustn't use the parent's handle in the child */
1421 xsh = xs_daemon_open();
1423 xs_write(xsh, XBT_NULL, path, pid, len);
1425 xs_daemon_close(xsh);
1426 out:
1427 free(path);
1428 free(pid);
1431 static int libxl_vfb_and_vkb_from_device_model_info(libxl_ctx *ctx,
1432 libxl_device_model_info *info,
1433 libxl_device_vfb *vfb,
1434 libxl_device_vkb *vkb)
1436 memset(vfb, 0x00, sizeof(libxl_device_vfb));
1437 memset(vkb, 0x00, sizeof(libxl_device_vkb));
1439 vfb->backend_domid = 0;
1440 vfb->devid = 0;
1441 vfb->vnc = info->vnc;
1442 vfb->vnclisten = info->vnclisten;
1443 vfb->vncdisplay = info->vncdisplay;
1444 vfb->vncunused = info->vncunused;
1445 vfb->vncpasswd = info->vncpasswd;
1446 vfb->keymap = info->keymap;
1447 vfb->sdl = info->sdl;
1448 vfb->opengl = info->opengl;
1450 vkb->backend_domid = 0;
1451 vkb->devid = 0;
1452 return 0;
1455 static int libxl_write_dmargs(libxl_ctx *ctx, int domid, int guest_domid, char **args)
1457 libxl__gc gc = LIBXL_INIT_GC(ctx);
1458 int i;
1459 char *vm_path;
1460 char *dmargs, *path;
1461 int dmargs_size;
1462 struct xs_permissions roperm[2];
1463 xs_transaction_t t;
1465 roperm[0].id = 0;
1466 roperm[0].perms = XS_PERM_NONE;
1467 roperm[1].id = domid;
1468 roperm[1].perms = XS_PERM_READ;
1470 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "/local/domain/%d/vm", guest_domid));
1472 i = 0;
1473 dmargs_size = 0;
1474 while (args[i] != NULL) {
1475 dmargs_size = dmargs_size + strlen(args[i]) + 1;
1476 i++;
1478 dmargs_size++;
1479 dmargs = (char *) malloc(dmargs_size);
1480 i = 1;
1481 dmargs[0] = '\0';
1482 while (args[i] != NULL) {
1483 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
1484 strcat(dmargs, " ");
1485 strcat(dmargs, args[i]);
1487 i++;
1489 path = libxl__sprintf(&gc, "%s/image/dmargs", vm_path);
1491 retry_transaction:
1492 t = xs_transaction_start(ctx->xsh);
1493 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
1494 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
1495 xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
1496 if (!xs_transaction_end(ctx->xsh, t, 0))
1497 if (errno == EAGAIN)
1498 goto retry_transaction;
1499 free(dmargs);
1500 libxl__free_all(&gc);
1501 return 0;
1504 static int libxl_create_stubdom(libxl_ctx *ctx,
1505 libxl_device_model_info *info,
1506 libxl_device_disk *disks, int num_disks,
1507 libxl_device_nic *vifs, int num_vifs,
1508 libxl_device_vfb *vfb,
1509 libxl_device_vkb *vkb,
1510 libxl_device_model_starting **starting_r)
1512 libxl__gc gc = LIBXL_INIT_GC(ctx);
1513 int i, num_console = 1, ret;
1514 libxl_device_console *console;
1515 libxl_domain_create_info c_info;
1516 libxl_domain_build_info b_info;
1517 libxl_domain_build_state state;
1518 uint32_t domid;
1519 char **args;
1520 struct xs_permissions perm[2];
1521 xs_transaction_t t;
1522 libxl_device_model_starting *dm_starting = 0;
1524 args = libxl_build_device_model_args(&gc, info, vifs, num_vifs);
1525 if (!args) {
1526 ret = ERROR_FAIL;
1527 goto out;
1530 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
1531 c_info.hvm = 0;
1532 c_info.name = libxl__sprintf(&gc, "%s-dm", libxl__domid_to_name(&gc, info->domid));
1534 libxl_uuid_copy(&c_info.uuid, &info->uuid);
1536 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
1537 b_info.max_vcpus = 1;
1538 b_info.max_memkb = 32 * 1024;
1539 b_info.target_memkb = b_info.max_memkb;
1540 b_info.kernel.path = libxl__abs_path(&gc, "ioemu-stubdom.gz", libxl_xenfirmwaredir_path());
1541 b_info.u.pv.cmdline = libxl__sprintf(&gc, " -d %d", info->domid);
1542 b_info.u.pv.ramdisk.path = "";
1543 b_info.u.pv.features = "";
1544 b_info.hvm = 0;
1546 ret = libxl_domain_make(ctx, &c_info, &domid);
1547 if (ret)
1548 goto out_free;
1549 ret = libxl_domain_build(ctx, &b_info, domid, &state);
1550 if (ret)
1551 goto out_free;
1553 libxl_write_dmargs(ctx, domid, info->domid, args);
1554 libxl__xs_write(&gc, XBT_NULL,
1555 libxl__sprintf(&gc, "%s/image/device-model-domid", libxl__xs_get_dompath(&gc, info->domid)),
1556 "%d", domid);
1557 libxl__xs_write(&gc, XBT_NULL,
1558 libxl__sprintf(&gc, "%s/target", libxl__xs_get_dompath(&gc, domid)),
1559 "%d", info->domid);
1560 ret = xc_domain_set_target(ctx->xch, domid, info->domid);
1561 if (ret<0) {
1562 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting target domain %d -> %d", domid, info->domid);
1563 ret = ERROR_FAIL;
1564 goto out_free;
1566 xs_set_target(ctx->xsh, domid, info->domid);
1568 perm[0].id = domid;
1569 perm[0].perms = XS_PERM_NONE;
1570 perm[1].id = info->domid;
1571 perm[1].perms = XS_PERM_READ;
1572 retry_transaction:
1573 t = xs_transaction_start(ctx->xsh);
1574 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid));
1575 xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
1576 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/%d/device/vfs", domid));
1577 xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
1578 if (!xs_transaction_end(ctx->xsh, t, 0))
1579 if (errno == EAGAIN)
1580 goto retry_transaction;
1582 for (i = 0; i < num_disks; i++) {
1583 disks[i].domid = domid;
1584 ret = libxl_device_disk_add(ctx, domid, &disks[i]);
1585 if (ret)
1586 goto out_free;
1588 for (i = 0; i < num_vifs; i++) {
1589 vifs[i].domid = domid;
1590 ret = libxl_device_nic_add(ctx, domid, &vifs[i]);
1591 if (ret)
1592 goto out_free;
1594 vfb->domid = domid;
1595 ret = libxl_device_vfb_add(ctx, domid, vfb);
1596 if (ret)
1597 goto out_free;
1598 vkb->domid = domid;
1599 ret = libxl_device_vkb_add(ctx, domid, vkb);
1600 if (ret)
1601 goto out_free;
1603 if (info->serial)
1604 num_console++;
1606 console = libxl__calloc(&gc, num_console, sizeof(libxl_device_console));
1607 if (!console) {
1608 ret = ERROR_NOMEM;
1609 goto out_free;
1612 for (i = 0; i < num_console; i++) {
1613 console[i].devid = i;
1614 console[i].consback = LIBXL_CONSBACK_IOEMU;
1615 console[i].domid = domid;
1616 if (!i) {
1617 char *filename;
1618 char *name = libxl__sprintf(&gc, "qemu-dm-%s", libxl_domid_to_name(ctx, info->domid));
1619 libxl_create_logfile(ctx, name, &filename);
1620 console[i].output = libxl__sprintf(&gc, "file:%s", filename);
1621 console[i].build_state = &state;
1622 free(filename);
1623 } else
1624 console[i].output = "pty";
1625 ret = libxl_device_console_add(ctx, domid, &console[i]);
1626 if (ret)
1627 goto out_free;
1629 if (libxl_create_xenpv_qemu(ctx, domid, vfb, &dm_starting) < 0) {
1630 ret = ERROR_FAIL;
1631 goto out_free;
1633 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
1634 ret = ERROR_FAIL;
1635 goto out_free;
1638 libxl_domain_unpause(ctx, domid);
1640 if (starting_r) {
1641 *starting_r = calloc(sizeof(libxl_device_model_starting), 1);
1642 (*starting_r)->domid = info->domid;
1643 (*starting_r)->dom_path = libxl__xs_get_dompath(&gc, info->domid);
1644 (*starting_r)->for_spawn = NULL;
1647 ret = 0;
1649 out_free:
1650 free(args);
1651 out:
1652 libxl__free_all(&gc);
1653 return ret;
1656 int libxl_create_device_model(libxl_ctx *ctx,
1657 libxl_device_model_info *info,
1658 libxl_device_disk *disks, int num_disks,
1659 libxl_device_nic *vifs, int num_vifs,
1660 libxl_device_model_starting **starting_r)
1662 libxl__gc gc = LIBXL_INIT_GC(ctx);
1663 char *path, *logfile;
1664 int logfile_w, null;
1665 int rc;
1666 char **args;
1667 libxl_device_model_starting buf_starting, *p;
1668 xs_transaction_t t;
1669 char *vm_path;
1670 char **pass_stuff;
1672 if (strstr(info->device_model, "stubdom-dm")) {
1673 libxl_device_vfb vfb;
1674 libxl_device_vkb vkb;
1676 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
1677 rc = libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
1678 goto out;
1681 args = libxl_build_device_model_args(&gc, info, vifs, num_vifs);
1682 if (!args) {
1683 rc = ERROR_FAIL;
1684 goto out;
1687 path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid);
1688 xs_mkdir(ctx->xsh, XBT_NULL, path);
1689 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/disable_pf", path), "%d", !info->xen_platform_pci);
1691 libxl_create_logfile(ctx, libxl__sprintf(&gc, "qemu-dm-%s", info->dom_name), &logfile);
1692 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
1693 free(logfile);
1694 null = open("/dev/null", O_RDONLY);
1696 if (starting_r) {
1697 rc = ERROR_NOMEM;
1698 *starting_r = calloc(sizeof(libxl_device_model_starting), 1);
1699 if (!*starting_r)
1700 goto out_close;
1701 p = *starting_r;
1702 p->for_spawn = calloc(sizeof(libxl__spawn_starting), 1);
1703 } else {
1704 p = &buf_starting;
1705 p->for_spawn = NULL;
1708 p->domid = info->domid;
1709 p->dom_path = libxl__xs_get_dompath(&gc, info->domid);
1710 if (!p->dom_path) {
1711 rc = ERROR_FAIL;
1712 goto out_close;
1715 if (info->vncpasswd) {
1716 retry_transaction:
1717 /* Find uuid and the write the vnc password to xenstore for qemu. */
1718 t = xs_transaction_start(ctx->xsh);
1719 vm_path = libxl__xs_read(&gc,t,libxl__sprintf(&gc, "%s/vm", p->dom_path));
1720 if (vm_path) {
1721 /* Now write the vncpassword into it. */
1722 pass_stuff = libxl__calloc(&gc, 3, sizeof(char *));
1723 pass_stuff[0] = "vncpasswd";
1724 pass_stuff[1] = info->vncpasswd;
1725 libxl__xs_writev(&gc,t,vm_path,pass_stuff);
1726 if (!xs_transaction_end(ctx->xsh, t, 0))
1727 if (errno == EAGAIN)
1728 goto retry_transaction;
1732 rc = libxl__spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
1733 if (rc < 0)
1734 goto out_close;
1735 if (!rc) { /* inner child */
1736 libxl__exec(null, logfile_w, logfile_w,
1737 libxl__abs_path(&gc, info->device_model, libxl_libexec_path()),
1738 args);
1741 rc = 0;
1743 out_close:
1744 close(null);
1745 close(logfile_w);
1746 free(args);
1747 out:
1748 libxl__free_all(&gc);
1749 return rc;
1752 int libxl_detach_device_model(libxl_ctx *ctx,
1753 libxl_device_model_starting *starting)
1755 int rc;
1756 rc = libxl__spawn_detach(ctx, starting->for_spawn);
1757 if (starting->for_spawn)
1758 free(starting->for_spawn);
1759 free(starting);
1760 return rc;
1764 int libxl_confirm_device_model_startup(libxl_ctx *ctx,
1765 libxl_device_model_starting *starting)
1767 int problem = libxl__wait_for_device_model(ctx, starting->domid, "running", NULL, NULL);
1768 int detach;
1769 if ( !problem )
1770 problem = libxl__spawn_check(ctx, starting->for_spawn);
1771 detach = libxl_detach_device_model(ctx, starting);
1772 return problem ? problem : detach;
1776 /******************************************************************************/
1778 int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1780 libxl__gc gc = LIBXL_INIT_GC(ctx);
1781 flexarray_t *front;
1782 flexarray_t *back;
1783 char *backend_type;
1784 unsigned int boffset = 0;
1785 unsigned int foffset = 0;
1786 int devid;
1787 libxl__device device;
1788 int major, minor, rc;
1790 front = flexarray_make(16, 1);
1791 if (!front) {
1792 rc = ERROR_NOMEM;
1793 goto out;
1795 back = flexarray_make(16, 1);
1796 if (!back) {
1797 rc = ERROR_NOMEM;
1798 goto out_free;
1801 backend_type = libxl__device_disk_backend_type_of_phystype(disk->phystype);
1802 devid = libxl__device_disk_dev_number(disk->virtpath);
1803 if (devid==-1) {
1804 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Invalid or unsupported"
1805 " virtual disk identifier %s", disk->virtpath);
1806 rc = ERROR_INVAL;
1807 goto out_free;
1810 device.backend_devid = devid;
1811 device.backend_domid = disk->backend_domid;
1812 device.devid = devid;
1813 device.domid = disk->domid;
1814 device.kind = DEVICE_VBD;
1816 switch (disk->phystype) {
1817 case PHYSTYPE_PHY: {
1819 libxl__device_physdisk_major_minor(disk->physpath, &major, &minor);
1820 flexarray_set(back, boffset++, "physical-device");
1821 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor));
1823 flexarray_set(back, boffset++, "params");
1824 flexarray_set(back, boffset++, disk->physpath);
1826 device.backend_kind = DEVICE_VBD;
1827 break;
1829 case PHYSTYPE_FILE:
1830 /* let's pretend is tap:aio for the moment */
1831 disk->phystype = PHYSTYPE_AIO;
1832 case PHYSTYPE_AIO:
1833 case PHYSTYPE_QCOW:
1834 case PHYSTYPE_QCOW2:
1835 case PHYSTYPE_VHD:
1836 if (libxl__blktap_enabled(&gc)) {
1837 const char *dev = libxl__blktap_devpath(&gc,
1838 disk->physpath, disk->phystype);
1839 if (!dev) {
1840 rc = ERROR_FAIL;
1841 goto out_free;
1843 flexarray_set(back, boffset++, "tapdisk-params");
1844 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s", libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
1845 flexarray_set(back, boffset++, "params");
1846 flexarray_set(back, boffset++, libxl__strdup(&gc, dev));
1847 backend_type = "phy";
1848 libxl__device_physdisk_major_minor(dev, &major, &minor);
1849 flexarray_set(back, boffset++, "physical-device");
1850 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%x:%x", major, minor));
1851 device.backend_kind = DEVICE_VBD;
1853 break;
1855 flexarray_set(back, boffset++, "params");
1856 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%s:%s",
1857 libxl__device_disk_string_of_phystype(disk->phystype), disk->physpath));
1859 if (libxl__blktap_enabled(&gc))
1860 device.backend_kind = DEVICE_TAP;
1861 else
1862 device.backend_kind = DEVICE_QDISK;
1863 break;
1865 default:
1866 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
1867 rc = ERROR_INVAL;
1868 goto out_free;
1871 flexarray_set(back, boffset++, "frontend-id");
1872 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", disk->domid));
1873 flexarray_set(back, boffset++, "online");
1874 flexarray_set(back, boffset++, "1");
1875 flexarray_set(back, boffset++, "removable");
1876 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
1877 flexarray_set(back, boffset++, "bootable");
1878 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1879 flexarray_set(back, boffset++, "state");
1880 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1881 flexarray_set(back, boffset++, "dev");
1882 flexarray_set(back, boffset++, disk->virtpath);
1883 flexarray_set(back, boffset++, "type");
1884 flexarray_set(back, boffset++, backend_type);
1885 flexarray_set(back, boffset++, "mode");
1886 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1888 flexarray_set(front, foffset++, "backend-id");
1889 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", disk->backend_domid));
1890 flexarray_set(front, foffset++, "state");
1891 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
1892 flexarray_set(front, foffset++, "virtual-device");
1893 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", devid));
1894 flexarray_set(front, foffset++, "device-type");
1895 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1897 if (0 /* protocol != native*/) {
1898 flexarray_set(front, foffset++, "protocol");
1899 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1902 libxl__device_generic_add(ctx, &device,
1903 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1904 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1906 rc = 0;
1908 out_free:
1909 flexarray_free(back);
1910 flexarray_free(front);
1911 out:
1912 libxl__free_all(&gc);
1913 return rc;
1916 int libxl_device_disk_del(libxl_ctx *ctx,
1917 libxl_device_disk *disk, int wait)
1919 libxl__device device;
1920 int devid;
1922 devid = libxl__device_disk_dev_number(disk->virtpath);
1923 device.backend_domid = disk->backend_domid;
1924 device.backend_devid = devid;
1925 device.backend_kind =
1926 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1927 device.domid = disk->domid;
1928 device.devid = devid;
1929 device.kind = DEVICE_VBD;
1930 return libxl__device_del(ctx, &device, wait);
1933 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
1935 libxl__gc gc = LIBXL_INIT_GC(ctx);
1936 const char *dev = NULL;
1937 char *ret;
1938 int phystype = disk->phystype;
1939 switch (phystype) {
1940 case PHYSTYPE_PHY: {
1941 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1942 dev = disk->physpath;
1943 break;
1945 case PHYSTYPE_FILE:
1946 /* let's pretend is tap:aio for the moment */
1947 phystype = PHYSTYPE_AIO;
1948 case PHYSTYPE_AIO:
1949 case PHYSTYPE_QCOW:
1950 case PHYSTYPE_QCOW2:
1951 case PHYSTYPE_VHD:
1952 if (libxl__blktap_enabled(&gc))
1953 dev = libxl__blktap_devpath(&gc, disk->physpath, phystype);
1954 break;
1956 default:
1957 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1958 break;
1960 ret = strdup(dev);
1961 libxl__free_all(&gc);
1962 return ret;
1965 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1967 /* Nothing to do for PHYSTYPE_PHY. */
1969 /*
1970 * For other device types assume that the blktap2 process is
1971 * needed by the soon to be started domain and do nothing.
1972 */
1974 return 0;
1977 /******************************************************************************/
1978 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1980 libxl__gc gc = LIBXL_INIT_GC(ctx);
1981 flexarray_t *front;
1982 flexarray_t *back;
1983 unsigned int boffset = 0;
1984 unsigned int foffset = 0;
1985 libxl__device device;
1986 char *dompath, **l;
1987 unsigned int nb, rc;
1989 front = flexarray_make(16, 1);
1990 if (!front) {
1991 rc = ERROR_NOMEM;
1992 goto out;
1994 back = flexarray_make(16, 1);
1995 if (!back) {
1996 rc = ERROR_NOMEM;
1997 goto out_free;
2000 if (nic->devid == -1) {
2001 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
2002 rc = ERROR_FAIL;
2003 goto out_free;
2005 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
2006 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb))) {
2007 nic->devid = 0;
2008 } else {
2009 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
2013 device.backend_devid = nic->devid;
2014 device.backend_domid = nic->backend_domid;
2015 device.backend_kind = DEVICE_VIF;
2016 device.devid = nic->devid;
2017 device.domid = nic->domid;
2018 device.kind = DEVICE_VIF;
2020 flexarray_set(back, boffset++, "frontend-id");
2021 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->domid));
2022 flexarray_set(back, boffset++, "online");
2023 flexarray_set(back, boffset++, "1");
2024 flexarray_set(back, boffset++, "state");
2025 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2026 flexarray_set(back, boffset++, "script");
2027 flexarray_set(back, boffset++, nic->script);
2028 flexarray_set(back, boffset++, "mac");
2029 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2030 nic->mac[0], nic->mac[1], nic->mac[2],
2031 nic->mac[3], nic->mac[4], nic->mac[5]));
2032 flexarray_set(back, boffset++, "bridge");
2033 flexarray_set(back, boffset++, libxl__strdup(&gc, nic->bridge));
2034 flexarray_set(back, boffset++, "handle");
2035 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->devid));
2037 flexarray_set(front, foffset++, "backend-id");
2038 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->backend_domid));
2039 flexarray_set(front, foffset++, "state");
2040 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2041 flexarray_set(front, foffset++, "handle");
2042 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->devid));
2043 flexarray_set(front, foffset++, "mac");
2044 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2045 nic->mac[0], nic->mac[1], nic->mac[2],
2046 nic->mac[3], nic->mac[4], nic->mac[5]));
2047 if (0 /* protocol != native*/) {
2048 flexarray_set(front, foffset++, "protocol");
2049 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
2052 libxl__device_generic_add(ctx, &device,
2053 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2054 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2056 /* FIXME: wait for plug */
2057 rc = 0;
2058 out_free:
2059 flexarray_free(back);
2060 flexarray_free(front);
2061 out:
2062 libxl__free_all(&gc);
2063 return rc;
2066 int libxl_device_nic_del(libxl_ctx *ctx,
2067 libxl_device_nic *nic, int wait)
2069 libxl__device device;
2071 device.backend_devid = nic->devid;
2072 device.backend_domid = nic->backend_domid;
2073 device.backend_kind = DEVICE_VIF;
2074 device.devid = nic->devid;
2075 device.domid = nic->domid;
2076 device.kind = DEVICE_VIF;
2078 return libxl__device_del(ctx, &device, wait);
2081 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
2083 libxl__gc gc = LIBXL_INIT_GC(ctx);
2084 char *dompath, *nic_path_fe;
2085 char **l, **list;
2086 char *val, *tok;
2087 unsigned int nb_nics, i;
2088 libxl_nicinfo *res, *nics;
2090 dompath = libxl__xs_get_dompath(&gc, domid);
2091 if (!dompath)
2092 goto err;
2093 list = l = libxl__xs_directory(&gc, XBT_NULL,
2094 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb_nics);
2095 if (!l)
2096 goto err;
2097 nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
2098 if (!res)
2099 goto err;
2100 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
2101 nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
2103 nics->backend = xs_read(ctx->xsh, XBT_NULL,
2104 libxl__sprintf(&gc, "%s/backend", nic_path_fe), NULL);
2105 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", nic_path_fe));
2106 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
2108 nics->devid = strtoul(*l, NULL, 10);
2109 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", nic_path_fe));
2110 nics->state = val ? strtoul(val, NULL, 10) : -1;
2111 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", nic_path_fe));
2112 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2113 ++i, tok = strtok(NULL, ":")) {
2114 nics->mac[i] = strtoul(tok, NULL, 16);
2116 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", nic_path_fe));
2117 nics->evtch = val ? strtol(val, NULL, 10) : -1;
2118 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", nic_path_fe));
2119 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
2120 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", nic_path_fe));
2121 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
2122 nics->frontend = xs_read(ctx->xsh, XBT_NULL,
2123 libxl__sprintf(&gc, "%s/frontend", nics->backend), NULL);
2124 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", nics->backend));
2125 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2126 nics->script = xs_read(ctx->xsh, XBT_NULL,
2127 libxl__sprintf(&gc, "%s/script", nics->backend), NULL);
2130 libxl__free_all(&gc);
2131 return res;
2132 err:
2133 libxl__free_all(&gc);
2134 return NULL;
2137 /******************************************************************************/
2138 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
2140 libxl__gc gc = LIBXL_INIT_GC(ctx);
2141 flexarray_t *front, *back;
2142 unsigned int boffset = 0, foffset = 0;
2143 libxl__device device;
2144 char *dompath, *dom, **l;
2145 unsigned int nb;
2146 int rc;
2148 front = flexarray_make(16, 1);
2149 if (!front) {
2150 rc = ERROR_NOMEM;
2151 goto err;
2153 back = flexarray_make(16, 1);
2154 if (!back) {
2155 rc = ERROR_NOMEM;
2156 goto err_free;
2159 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
2160 rc = ERROR_FAIL;
2161 goto err_free;
2163 dom = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/name", dompath));
2165 if (net2->devid == -1) {
2166 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
2167 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb))) {
2168 net2->devid = 0;
2169 } else {
2170 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
2174 device.backend_devid = net2->devid;
2175 device.backend_domid = net2->backend_domid;
2176 device.backend_kind = DEVICE_VIF2;
2177 device.devid = net2->devid;
2178 device.domid = net2->domid;
2179 device.kind = DEVICE_VIF2;
2181 flexarray_set(back, boffset++, "domain");
2182 flexarray_set(back, boffset++, dom);
2183 flexarray_set(back, boffset++, "frontend-id");
2184 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->domid));
2186 flexarray_set(back, boffset++, "local-trusted");
2187 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
2188 flexarray_set(back, boffset++, "mac");
2189 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2190 net2->back_mac[0], net2->back_mac[1],
2191 net2->back_mac[2], net2->back_mac[3],
2192 net2->back_mac[4], net2->back_mac[5]));
2194 flexarray_set(back, boffset++, "remote-trusted");
2195 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->trusted));
2196 flexarray_set(back, boffset++, "remote-mac");
2197 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2198 net2->front_mac[0], net2->front_mac[1],
2199 net2->front_mac[2], net2->front_mac[3],
2200 net2->front_mac[4], net2->front_mac[5]));
2202 flexarray_set(back, boffset++, "max-bypasses");
2203 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->max_bypasses));
2204 flexarray_set(back, boffset++, "filter-mac");
2205 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
2206 flexarray_set(back, boffset++, "handle");
2207 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->devid));
2208 flexarray_set(back, boffset++, "online");
2209 flexarray_set(back, boffset++, "1");
2210 flexarray_set(back, boffset++, "state");
2211 flexarray_set(back, boffset++, "1");
2213 flexarray_set(front, foffset++, "backend-id");
2214 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->backend_domid));
2216 flexarray_set(front, foffset++, "local-trusted");
2217 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->trusted));
2218 flexarray_set(front, foffset++, "mac");
2219 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2220 net2->front_mac[0], net2->front_mac[1],
2221 net2->front_mac[2], net2->front_mac[3],
2222 net2->front_mac[4], net2->front_mac[5]));
2224 flexarray_set(front, foffset++, "remote-trusted");
2225 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
2226 flexarray_set(front, foffset++, "remote-mac");
2227 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2228 net2->back_mac[0], net2->back_mac[1],
2229 net2->back_mac[2], net2->back_mac[3],
2230 net2->back_mac[4], net2->back_mac[5]));
2232 flexarray_set(front, foffset++, "filter-mac");
2233 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
2234 flexarray_set(front, foffset++, "state");
2235 flexarray_set(front, foffset++, "1");
2237 libxl__device_generic_add(ctx, &device,
2238 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2239 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2241 /* FIXME: wait for plug */
2242 rc = 0;
2243 err_free:
2244 flexarray_free(back);
2245 flexarray_free(front);
2246 err:
2247 libxl__free_all(&gc);
2248 return rc;
2251 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
2253 libxl__gc gc = LIBXL_INIT_GC(ctx);
2254 char *dompath, *net2_path_fe;
2255 char **l;
2256 char *val, *tok;
2257 unsigned int nb_net2s, i;
2258 libxl_net2info *res, *net2s;
2260 dompath = libxl__xs_get_dompath(&gc, domid);
2261 if (!dompath)
2262 goto err;
2263 l = libxl__xs_directory(&gc, XBT_NULL,
2264 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb_net2s);
2265 if (!l)
2266 goto err;
2267 res = calloc(nb_net2s, sizeof (libxl_net2info));
2268 if (!res)
2269 goto err;
2270 net2s = res;
2271 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
2272 net2_path_fe = libxl__sprintf(&gc, "%s/device/vif2/%s", dompath, *l);
2274 net2s->backend = libxl__xs_read(&gc, XBT_NULL,
2275 libxl__sprintf(&gc, "%s/backend", net2_path_fe));
2276 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", net2_path_fe));
2277 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
2279 net2s->devid = strtoul(*l, NULL, 10);
2280 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", net2_path_fe));
2281 net2s->state = val ? strtoul(val, NULL, 10) : -1;
2283 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", net2_path_fe));
2284 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2285 ++i, tok = strtok(NULL, ":")) {
2286 net2s->mac[i] = strtoul(tok, NULL, 16);
2288 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-trusted", net2_path_fe));
2289 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
2291 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-mac", net2_path_fe));
2292 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2293 ++i, tok = strtok(NULL, ":")) {
2294 net2s->back_mac[i] = strtoul(tok, NULL, 16);
2296 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/filter-mac", net2_path_fe));
2297 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
2299 net2s->frontend = libxl__xs_read(&gc, XBT_NULL,
2300 libxl__sprintf(&gc, "%s/frontend", net2s->backend));
2301 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", net2s->backend));
2302 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2305 libxl__free_all(&gc);
2306 return res;
2307 err:
2308 libxl__free_all(&gc);
2309 return NULL;
2312 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
2314 libxl__device device;
2316 device.backend_devid = net2->devid;
2317 device.backend_domid = net2->backend_domid;
2318 device.backend_kind = DEVICE_VIF2;
2319 device.devid = net2->devid;
2320 device.domid = net2->domid;
2321 device.kind = DEVICE_VIF2;
2323 return libxl__device_del(ctx, &device, wait);
2327 /******************************************************************************/
2328 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
2330 libxl__gc gc = LIBXL_INIT_GC(ctx);
2331 flexarray_t *front;
2332 flexarray_t *back;
2333 unsigned int boffset = 0;
2334 unsigned int foffset = 0;
2335 libxl__device device;
2336 int rc;
2338 front = flexarray_make(16, 1);
2339 if (!front) {
2340 rc = ERROR_NOMEM;
2341 goto out;
2343 back = flexarray_make(16, 1);
2344 if (!back) {
2345 rc = ERROR_NOMEM;
2346 goto out_free;
2349 device.backend_devid = console->devid;
2350 device.backend_domid = console->backend_domid;
2351 device.backend_kind = DEVICE_CONSOLE;
2352 device.devid = console->devid;
2353 device.domid = console->domid;
2354 device.kind = DEVICE_CONSOLE;
2356 flexarray_set(back, boffset++, "frontend-id");
2357 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", console->domid));
2358 flexarray_set(back, boffset++, "online");
2359 flexarray_set(back, boffset++, "1");
2360 flexarray_set(back, boffset++, "state");
2361 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2362 flexarray_set(back, boffset++, "domain");
2363 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
2364 flexarray_set(back, boffset++, "protocol");
2365 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
2367 flexarray_set(front, foffset++, "backend-id");
2368 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", console->backend_domid));
2369 flexarray_set(front, foffset++, "limit");
2370 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
2371 flexarray_set(front, foffset++, "type");
2372 if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
2373 flexarray_set(front, foffset++, "xenconsoled");
2374 else
2375 flexarray_set(front, foffset++, "ioemu");
2376 flexarray_set(front, foffset++, "output");
2377 flexarray_set(front, foffset++, console->output);
2379 if (device.devid == 0) {
2380 if (console->build_state == NULL) {
2381 rc = ERROR_INVAL;
2382 goto out_free;
2384 flexarray_set(front, foffset++, "port");
2385 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
2386 flexarray_set(front, foffset++, "ring-ref");
2387 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
2388 } else {
2389 flexarray_set(front, foffset++, "state");
2390 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2391 flexarray_set(front, foffset++, "protocol");
2392 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
2395 libxl__device_generic_add(ctx, &device,
2396 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2397 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2398 rc = 0;
2399 out_free:
2400 flexarray_free(back);
2401 flexarray_free(front);
2402 out:
2403 libxl__free_all(&gc);
2404 return rc;
2407 /******************************************************************************/
2408 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
2410 libxl__gc gc = LIBXL_INIT_GC(ctx);
2411 flexarray_t *front;
2412 flexarray_t *back;
2413 unsigned int boffset = 0;
2414 unsigned int foffset = 0;
2415 libxl__device device;
2416 int rc;
2418 front = flexarray_make(16, 1);
2419 if (!front) {
2420 rc = ERROR_NOMEM;
2421 goto out;
2423 back = flexarray_make(16, 1);
2424 if (!back) {
2425 rc = ERROR_NOMEM;
2426 goto out_free;
2429 device.backend_devid = vkb->devid;
2430 device.backend_domid = vkb->backend_domid;
2431 device.backend_kind = DEVICE_VKBD;
2432 device.devid = vkb->devid;
2433 device.domid = vkb->domid;
2434 device.kind = DEVICE_VKBD;
2436 flexarray_set(back, boffset++, "frontend-id");
2437 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vkb->domid));
2438 flexarray_set(back, boffset++, "online");
2439 flexarray_set(back, boffset++, "1");
2440 flexarray_set(back, boffset++, "state");
2441 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2442 flexarray_set(back, boffset++, "domain");
2443 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
2445 flexarray_set(front, foffset++, "backend-id");
2446 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vkb->backend_domid));
2447 flexarray_set(front, foffset++, "state");
2448 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2450 libxl__device_generic_add(ctx, &device,
2451 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2452 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2453 rc = 0;
2454 out_free:
2455 flexarray_free(back);
2456 flexarray_free(front);
2457 out:
2458 libxl__free_all(&gc);
2459 return rc;
2462 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2464 return ERROR_NI;
2467 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2469 return ERROR_NI;
2472 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
2474 libxl__gc gc = LIBXL_INIT_GC(ctx);
2475 char *be_path_tap, *be_path_vbd;
2476 libxl_device_disk *dend, *disks, *ret = NULL;
2477 char **b, **l = NULL;
2478 unsigned int numl, len;
2479 char *type;
2481 be_path_vbd = libxl__sprintf(&gc, "%s/backend/vbd/%d", libxl__xs_get_dompath(&gc, 0), domid);
2482 be_path_tap = libxl__sprintf(&gc, "%s/backend/tap/%d", libxl__xs_get_dompath(&gc, 0), domid);
2484 b = l = libxl__xs_directory(&gc, XBT_NULL, be_path_vbd, &numl);
2485 if (l) {
2486 ret = realloc(ret, sizeof(libxl_device_disk) * numl);
2487 disks = ret;
2488 *num = numl;
2489 dend = ret + *num;
2490 for (; disks < dend; ++disks, ++l) {
2491 disks->backend_domid = 0;
2492 disks->domid = domid;
2493 disks->physpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path_vbd, *l), &len);
2494 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path_vbd, *l)), &(disks->phystype));
2495 disks->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path_vbd, *l), &len);
2496 disks->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path_vbd, *l)));
2497 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path_vbd, *l)), "w"))
2498 disks->readwrite = 1;
2499 else
2500 disks->readwrite = 0;
2501 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_vbd, *l))));
2502 disks->is_cdrom = !strcmp(type, "cdrom");
2505 b = l = libxl__xs_directory(&gc, XBT_NULL, be_path_tap, &numl);
2506 if (l) {
2507 ret = realloc(ret, sizeof(libxl_device_disk) * (*num + numl));
2508 disks = ret + *num;
2509 *num += numl;
2510 for (dend = ret + *num; disks < dend; ++disks, ++l) {
2511 disks->backend_domid = 0;
2512 disks->domid = domid;
2513 disks->physpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path_tap, *l), &len);
2514 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path_tap, *l)), &(disks->phystype));
2515 disks->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path_tap, *l), &len);
2516 disks->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path_tap, *l)));
2517 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path_tap, *l)), "w"))
2518 disks->readwrite = 1;
2519 else
2520 disks->readwrite = 0;
2521 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_tap, *l))));
2522 disks->is_cdrom = !strcmp(type, "cdrom");
2525 libxl__free_all(&gc);
2526 return ret;
2529 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
2530 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
2532 libxl__gc gc = LIBXL_INIT_GC(ctx);
2533 char *dompath, *diskpath;
2534 char *val;
2536 dompath = libxl__xs_get_dompath(&gc, domid);
2537 diskinfo->devid = libxl__device_disk_dev_number(disk->virtpath);
2539 /* tap devices entries in xenstore are written as vbd devices. */
2540 diskpath = libxl__sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
2541 diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
2542 libxl__sprintf(&gc, "%s/backend", diskpath), NULL);
2543 if (!diskinfo->backend) {
2544 libxl__free_all(&gc);
2545 return ERROR_FAIL;
2547 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", diskpath));
2548 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
2549 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", diskpath));
2550 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
2551 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", diskpath));
2552 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
2553 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/ring-ref", diskpath));
2554 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
2555 diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
2556 libxl__sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
2557 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", diskinfo->backend));
2558 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2560 libxl__free_all(&gc);
2561 return 0;
2564 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
2566 int num, i;
2567 uint32_t stubdomid;
2568 libxl_device_disk *disks;
2569 int ret = ERROR_FAIL;
2571 if (!disk->physpath) {
2572 disk->physpath = "";
2573 disk->phystype = PHYSTYPE_PHY;
2575 disks = libxl_device_disk_list(ctx, domid, &num);
2576 for (i = 0; i < num; i++) {
2577 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
2578 /* found */
2579 break;
2581 if (i == num) {
2582 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual device not found");
2583 goto out;
2586 ret = 0;
2588 libxl_device_disk_del(ctx, disks + i, 1);
2589 libxl_device_disk_add(ctx, domid, disk);
2590 stubdomid = libxl_get_stubdom_id(ctx, domid);
2591 if (stubdomid) {
2592 disks[i].domid = stubdomid;
2593 libxl_device_disk_del(ctx, disks + i, 1);
2594 disk->domid = stubdomid;
2595 libxl_device_disk_add(ctx, stubdomid, disk);
2596 disk->domid = domid;
2598 out:
2599 for (i = 0; i < num; i++)
2600 libxl_device_disk_destroy(&disks[i]);
2601 free(disks);
2602 return ret;
2605 /******************************************************************************/
2606 static int libxl_build_xenpv_qemu_args(libxl__gc *gc,
2607 uint32_t domid,
2608 libxl_device_vfb *vfb,
2609 libxl_device_model_info *info)
2611 libxl_ctx *ctx = libxl__gc_owner(gc);
2612 memset(info, 0x00, sizeof(libxl_device_model_info));
2614 if (vfb != NULL) {
2615 info->vnc = vfb->vnc;
2616 if (vfb->vnclisten)
2617 info->vnclisten = libxl__strdup(gc, vfb->vnclisten);
2618 info->vncdisplay = vfb->vncdisplay;
2619 info->vncunused = vfb->vncunused;
2620 if (vfb->vncpasswd)
2621 info->vncpasswd = vfb->vncpasswd;
2622 if (vfb->keymap)
2623 info->keymap = libxl__strdup(gc, vfb->keymap);
2624 info->sdl = vfb->sdl;
2625 info->opengl = vfb->opengl;
2626 } else
2627 info->nographic = 1;
2628 info->domid = domid;
2629 info->dom_name = libxl_domid_to_name(ctx, domid);
2630 info->device_model = libxl__abs_path(gc, "qemu-dm", libxl_libexec_path());
2631 info->type = XENPV;
2632 return 0;
2635 int libxl_create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb,
2636 libxl_device_model_starting **starting_r)
2638 libxl__gc gc = LIBXL_INIT_GC(ctx);
2639 libxl_device_model_info info;
2641 libxl_build_xenpv_qemu_args(&gc, domid, vfb, &info);
2642 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
2643 libxl__free_all(&gc);
2644 return 0;
2647 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
2649 libxl__gc gc = LIBXL_INIT_GC(ctx);
2650 flexarray_t *front;
2651 flexarray_t *back;
2652 unsigned int boffset = 0;
2653 unsigned int foffset = 0;
2654 libxl__device device;
2655 int rc;
2657 front = flexarray_make(16, 1);
2658 if (!front) {
2659 rc = ERROR_NOMEM;
2660 goto out;
2662 back = flexarray_make(16, 1);
2663 if (!back) {
2664 rc = ERROR_NOMEM;
2665 goto out_free;
2668 device.backend_devid = vfb->devid;
2669 device.backend_domid = vfb->backend_domid;
2670 device.backend_kind = DEVICE_VFB;
2671 device.devid = vfb->devid;
2672 device.domid = vfb->domid;
2673 device.kind = DEVICE_VFB;
2675 flexarray_set(back, boffset++, "frontend-id");
2676 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->domid));
2677 flexarray_set(back, boffset++, "online");
2678 flexarray_set(back, boffset++, "1");
2679 flexarray_set(back, boffset++, "state");
2680 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2681 flexarray_set(back, boffset++, "domain");
2682 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
2683 flexarray_set(back, boffset++, "vnc");
2684 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vnc));
2685 flexarray_set(back, boffset++, "vnclisten");
2686 flexarray_set(back, boffset++, vfb->vnclisten);
2687 flexarray_set(back, boffset++, "vncpasswd");
2688 flexarray_set(back, boffset++, vfb->vncpasswd);
2689 flexarray_set(back, boffset++, "vncdisplay");
2690 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncdisplay));
2691 flexarray_set(back, boffset++, "vncunused");
2692 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncunused));
2693 flexarray_set(back, boffset++, "sdl");
2694 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->sdl));
2695 flexarray_set(back, boffset++, "opengl");
2696 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->opengl));
2697 if (vfb->xauthority) {
2698 flexarray_set(back, boffset++, "xauthority");
2699 flexarray_set(back, boffset++, vfb->xauthority);
2701 if (vfb->display) {
2702 flexarray_set(back, boffset++, "display");
2703 flexarray_set(back, boffset++, vfb->display);
2706 flexarray_set(front, foffset++, "backend-id");
2707 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vfb->backend_domid));
2708 flexarray_set(front, foffset++, "state");
2709 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2711 libxl__device_generic_add(ctx, &device,
2712 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2713 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2714 rc = 0;
2715 out_free:
2716 flexarray_free(front);
2717 flexarray_free(back);
2718 out:
2719 libxl__free_all(&gc);
2720 return rc;
2723 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2725 return ERROR_NI;
2728 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2730 return ERROR_NI;
2733 /******************************************************************************/
2735 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
2737 libxl__gc gc = LIBXL_INIT_GC(ctx);
2738 char *mem, *endptr;
2739 uint32_t memorykb;
2740 char *dompath = libxl__xs_get_dompath(&gc, domid);
2741 int rc = 1;
2743 mem = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/memory/target", dompath));
2744 if (!mem) {
2745 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
2746 goto out;
2748 memorykb = strtoul(mem, &endptr, 10);
2749 if (*endptr != '\0') {
2750 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
2751 goto out;
2754 if (max_memkb < memorykb) {
2755 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
2756 goto out;
2759 rc = 0;
2760 out:
2761 libxl__free_all(&gc);
2762 return rc;
2765 static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb)
2767 int rc;
2768 libxl_dominfo info;
2769 libxl_physinfo physinfo;
2770 char *target = NULL, *endptr = NULL;
2771 char *target_path = "/local/domain/0/memory/target";
2772 char *max_path = "/local/domain/0/memory/static-max";
2773 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2774 xs_transaction_t t;
2775 libxl_ctx *ctx = libxl__gc_owner(gc);
2776 uint32_t free_mem_slack = 0;
2778 retry_transaction:
2779 t = xs_transaction_start(ctx->xsh);
2781 target = libxl__xs_read(gc, t, target_path);
2782 if (target) {
2783 *target_memkb = strtoul(target, &endptr, 10);
2784 if (*endptr != '\0') {
2785 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2786 "invalid memory target %s from %s\n", target, target_path);
2787 rc = ERROR_FAIL;
2788 goto out;
2790 rc = 0;
2791 goto out;
2794 rc = libxl_domain_info(ctx, &info, 0);
2795 if (rc < 0)
2796 return rc;
2798 rc = libxl_get_physinfo(ctx, &physinfo);
2799 if (rc < 0)
2800 return rc;
2802 libxl__xs_write(gc, t, target_path, "%"PRIu32,
2803 (uint32_t) info.current_memkb);
2804 libxl__xs_write(gc, t, max_path, "%"PRIu32,
2805 (uint32_t) info.max_memkb);
2807 free_mem_slack = (uint32_t) (PAGE_TO_MEMKB(physinfo.total_pages) -
2808 info.current_memkb);
2809 /* From empirical measurements the free_mem_slack shouldn't be more
2810 * than 15% of the total memory present on the system. */
2811 if (free_mem_slack > PAGE_TO_MEMKB(physinfo.total_pages) * 0.15)
2812 free_mem_slack = PAGE_TO_MEMKB(physinfo.total_pages) * 0.15;
2813 libxl__xs_write(gc, t, free_mem_slack_path, "%"PRIu32, free_mem_slack);
2815 *target_memkb = (uint32_t) info.current_memkb;
2816 rc = 0;
2818 out:
2819 if (!xs_transaction_end(ctx->xsh, t, 0))
2820 if (errno == EAGAIN)
2821 goto retry_transaction;
2824 return rc;
2827 /* returns how much memory should be left free in the system */
2828 static int libxl__get_free_memory_slack(libxl__gc *gc, uint32_t *free_mem_slack)
2830 int rc;
2831 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2832 char *free_mem_slack_s, *endptr;
2833 uint32_t target_memkb;
2835 retry:
2836 free_mem_slack_s = libxl__xs_read(gc, XBT_NULL, free_mem_slack_path);
2837 if (!free_mem_slack_s) {
2838 rc = libxl__fill_dom0_memory_info(gc, &target_memkb);
2839 if (rc < 0)
2840 return rc;
2841 goto retry;
2842 } else {
2843 *free_mem_slack = strtoul(free_mem_slack_s, &endptr, 10);
2844 if (*endptr != '\0') {
2845 LIBXL__LOG_ERRNO(gc->owner, LIBXL__LOG_ERROR,
2846 "invalid free_mem_slack %s from %s\n",
2847 free_mem_slack_s, free_mem_slack_path);
2848 return ERROR_FAIL;
2851 return 0;
2854 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
2855 int32_t target_memkb, int relative, int enforce)
2857 libxl__gc gc = LIBXL_INIT_GC(ctx);
2858 int rc = 1, abort = 0;
2859 uint32_t memorykb = 0, videoram = 0;
2860 uint32_t current_target_memkb = 0, new_target_memkb = 0;
2861 char *memmax, *endptr, *videoram_s = NULL, *target = NULL;
2862 char *dompath = libxl__xs_get_dompath(&gc, domid);
2863 xc_domaininfo_t info;
2864 libxl_dominfo ptr;
2865 char *uuid;
2866 xs_transaction_t t;
2868 retry_transaction:
2869 t = xs_transaction_start(ctx->xsh);
2871 target = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2872 "%s/memory/target", dompath));
2873 if (!target && !domid) {
2874 xs_transaction_end(ctx->xsh, t, 1);
2875 rc = libxl__fill_dom0_memory_info(&gc, &current_target_memkb);
2876 if (rc < 0) {
2877 abort = 1;
2878 goto out;
2880 goto retry_transaction;
2881 } else if (!target) {
2882 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2883 "cannot get target memory info from %s/memory/target\n",
2884 dompath);
2885 abort = 1;
2886 goto out;
2887 } else {
2888 current_target_memkb = strtoul(target, &endptr, 10);
2889 if (*endptr != '\0') {
2890 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2891 "invalid memory target %s from %s/memory/target\n",
2892 target, dompath);
2893 abort = 1;
2894 goto out;
2897 memmax = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2898 "%s/memory/static-max", dompath));
2899 if (!memmax) {
2900 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2901 "cannot get memory info from %s/memory/static-max\n",
2902 dompath);
2903 abort = 1;
2904 goto out;
2906 memorykb = strtoul(memmax, &endptr, 10);
2907 if (*endptr != '\0') {
2908 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2909 "invalid max memory %s from %s/memory/static-max\n",
2910 memmax, dompath);
2911 abort = 1;
2912 goto out;
2915 if (relative)
2916 new_target_memkb = current_target_memkb + target_memkb;
2917 else
2918 new_target_memkb = target_memkb;
2919 if (new_target_memkb > memorykb) {
2920 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2921 "memory_dynamic_max must be less than or equal to"
2922 " memory_static_max\n");
2923 abort = 1;
2924 goto out;
2927 if (!domid && new_target_memkb < LIBXL_MIN_DOM0_MEM) {
2928 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2929 "new target %d for dom0 is below the minimum threshold\n",
2930 new_target_memkb);
2931 abort = 1;
2932 goto out;
2934 videoram_s = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2935 "%s/memory/videoram", dompath));
2936 videoram = videoram_s ? atoi(videoram_s) : 0;
2938 if (enforce) {
2939 memorykb = new_target_memkb;
2940 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
2941 LIBXL_MAXMEM_CONSTANT);
2942 if (rc != 0) {
2943 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2944 "xc_domain_setmaxmem domid=%d memkb=%d failed "
2945 "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
2946 abort = 1;
2947 goto out;
2951 new_target_memkb -= videoram;
2952 rc = xc_domain_set_pod_target(ctx->xch, domid,
2953 new_target_memkb / 4, NULL, NULL, NULL);
2954 if (rc != 0) {
2955 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2956 "xc_domain_set_pod_target domid=%d, memkb=%d "
2957 "failed rc=%d\n", domid, new_target_memkb / 4,
2958 rc);
2959 abort = 1;
2960 goto out;
2963 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/memory/target",
2964 dompath), "%"PRIu32, new_target_memkb);
2965 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2966 if (rc != 1 || info.domain != domid) {
2967 abort = 1;
2968 goto out;
2970 xcinfo2xlinfo(&info, &ptr);
2971 uuid = libxl__uuid2string(&gc, ptr.uuid);
2972 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "/vm/%s/memory", uuid),
2973 "%"PRIu32, new_target_memkb / 1024);
2975 out:
2976 if (!xs_transaction_end(ctx->xsh, t, abort) && !abort)
2977 if (errno == EAGAIN)
2978 goto retry_transaction;
2980 libxl__free_all(&gc);
2981 return rc;
2984 int libxl_get_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t *out_target)
2986 libxl__gc gc = LIBXL_INIT_GC(ctx);
2987 int rc = 1;
2988 char *target = NULL, *endptr = NULL;
2989 char *dompath = libxl__xs_get_dompath(&gc, domid);
2990 uint32_t target_memkb;
2992 target = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc,
2993 "%s/memory/target", dompath));
2994 if (!target && !domid) {
2995 rc = libxl__fill_dom0_memory_info(&gc, &target_memkb);
2996 if (rc < 0)
2997 goto out;
2998 } else if (!target) {
2999 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
3000 "cannot get target memory info from %s/memory/target\n",
3001 dompath);
3002 goto out;
3003 } else {
3004 target_memkb = strtoul(target, &endptr, 10);
3005 if (*endptr != '\0') {
3006 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
3007 "invalid memory target %s from %s/memory/target\n",
3008 target, dompath);
3009 goto out;
3012 *out_target = target_memkb;
3013 rc = 0;
3015 out:
3016 libxl__free_all(&gc);
3017 return rc;
3020 int libxl_domain_need_memory(libxl_ctx *ctx, libxl_domain_build_info *b_info,
3021 libxl_device_model_info *dm_info, uint32_t *need_memkb)
3023 *need_memkb = b_info->target_memkb;
3024 if (b_info->hvm) {
3025 *need_memkb += b_info->shadow_memkb + LIBXL_HVM_EXTRA_MEMORY;
3026 if (strstr(dm_info->device_model, "stubdom-dm"))
3027 *need_memkb += 32 * 1024;
3028 } else
3029 *need_memkb += LIBXL_PV_EXTRA_MEMORY;
3030 if (*need_memkb % (2 * 1024))
3031 *need_memkb += (2 * 1024) - (*need_memkb % (2 * 1024));
3032 return 0;
3035 int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
3037 int rc = 0;
3038 libxl_physinfo info;
3039 uint32_t freemem_slack;
3040 libxl__gc gc = LIBXL_INIT_GC(ctx);
3042 rc = libxl_get_physinfo(ctx, &info);
3043 if (rc < 0)
3044 goto out;
3045 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
3046 if (rc < 0)
3047 goto out;
3049 if ((info.free_pages + info.scrub_pages) * 4 > freemem_slack)
3050 *memkb = (info.free_pages + info.scrub_pages) * 4 - freemem_slack;
3051 else
3052 *memkb = 0;
3054 out:
3055 libxl__free_all(&gc);
3056 return rc;
3059 int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
3060 memory_kb, int wait_secs)
3062 int rc = 0;
3063 libxl_physinfo info;
3064 uint32_t freemem_slack;
3065 libxl__gc gc = LIBXL_INIT_GC(ctx);
3067 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
3068 if (rc < 0)
3069 goto out;
3070 while (wait_secs > 0) {
3071 rc = libxl_get_physinfo(ctx, &info);
3072 if (rc < 0)
3073 goto out;
3074 if (info.free_pages * 4 - freemem_slack >= memory_kb) {
3075 rc = 0;
3076 goto out;
3078 wait_secs--;
3079 sleep(1);
3081 rc = ERROR_NOMEM;
3083 out:
3084 libxl__free_all(&gc);
3085 return rc;
3088 int libxl_wait_for_memory_target(libxl_ctx *ctx, uint32_t domid, int wait_secs)
3090 int rc = 0;
3091 uint32_t target_memkb = 0;
3092 libxl_dominfo info;
3094 do {
3095 wait_secs--;
3096 sleep(1);
3098 rc = libxl_get_memory_target(ctx, domid, &target_memkb);
3099 if (rc < 0)
3100 goto out;
3102 rc = libxl_domain_info(ctx, &info, domid);
3103 if (rc < 0)
3104 return rc;
3105 } while (wait_secs > 0 && info.current_memkb > target_memkb);
3107 if (info.current_memkb <= target_memkb)
3108 rc = 0;
3109 else
3110 rc = ERROR_FAIL;
3112 out:
3113 return 0;
3116 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
3118 int rc = -1;
3120 switch (button) {
3121 case POWER_BUTTON:
3122 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
3123 break;
3124 case SLEEP_BUTTON:
3125 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
3126 break;
3127 default:
3128 break;
3131 return rc;
3134 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
3136 xc_physinfo_t xcphysinfo = { 0 };
3137 int rc;
3139 rc = xc_physinfo(ctx->xch, &xcphysinfo);
3140 if (rc != 0) {
3141 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting physinfo");
3142 return ERROR_FAIL;
3144 physinfo->threads_per_core = xcphysinfo.threads_per_core;
3145 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
3146 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
3147 physinfo->nr_cpus = xcphysinfo.nr_cpus;
3148 physinfo->cpu_khz = xcphysinfo.cpu_khz;
3149 physinfo->total_pages = xcphysinfo.total_pages;
3150 physinfo->free_pages = xcphysinfo.free_pages;
3151 physinfo->scrub_pages = xcphysinfo.scrub_pages;
3152 physinfo->nr_nodes = xcphysinfo.nr_nodes;
3153 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
3154 physinfo->phys_cap = xcphysinfo.capabilities;
3156 return 0;
3159 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
3161 union {
3162 xen_extraversion_t xen_extra;
3163 xen_compile_info_t xen_cc;
3164 xen_changeset_info_t xen_chgset;
3165 xen_capabilities_info_t xen_caps;
3166 xen_platform_parameters_t p_parms;
3167 xen_commandline_t xen_commandline;
3168 } u;
3169 long xen_version;
3170 libxl_version_info *info = &ctx->version_info;
3172 if (info->xen_version_extra != NULL)
3173 return info;
3175 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
3176 info->xen_version_major = xen_version >> 16;
3177 info->xen_version_minor = xen_version & 0xFF;
3179 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
3180 info->xen_version_extra = strdup(u.xen_extra);
3182 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
3183 info->compiler = strdup(u.xen_cc.compiler);
3184 info->compile_by = strdup(u.xen_cc.compile_by);
3185 info->compile_domain = strdup(u.xen_cc.compile_domain);
3186 info->compile_date = strdup(u.xen_cc.compile_date);
3188 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
3189 info->capabilities = strdup(u.xen_caps);
3191 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
3192 info->changeset = strdup(u.xen_chgset);
3194 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
3195 info->virt_start = u.p_parms.virt_start;
3197 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
3199 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
3200 info->commandline = strdup(u.xen_commandline);
3202 return info;
3205 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
3206 int *nb_vcpu, int *nrcpus)
3208 libxl_vcpuinfo *ptr, *ret;
3209 xc_domaininfo_t domaininfo;
3210 xc_vcpuinfo_t vcpuinfo;
3212 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
3213 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist");
3214 return NULL;
3216 *nrcpus = libxl_get_max_cpus(ctx);
3217 ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
3218 if (!ptr) {
3219 return NULL;
3222 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
3223 if (libxl_cpumap_alloc(ctx, &ptr->cpumap)) {
3224 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap");
3225 return NULL;
3227 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
3228 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info");
3229 return NULL;
3231 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) {
3232 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity");
3233 return NULL;
3235 ptr->vcpuid = *nb_vcpu;
3236 ptr->cpu = vcpuinfo.cpu;
3237 ptr->online = !!vcpuinfo.online;
3238 ptr->blocked = !!vcpuinfo.blocked;
3239 ptr->running = !!vcpuinfo.running;
3240 ptr->vcpu_time = vcpuinfo.cpu_time;
3242 return ret;
3245 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
3246 libxl_cpumap *cpumap)
3248 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap->map)) {
3249 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting vcpu affinity");
3250 return ERROR_FAIL;
3252 return 0;
3255 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask)
3257 libxl__gc gc = LIBXL_INIT_GC(ctx);
3258 libxl_dominfo info;
3259 char *dompath;
3260 xs_transaction_t t;
3261 int i, rc = ERROR_FAIL;
3263 if (libxl_domain_info(ctx, &info, domid) < 0) {
3264 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
3265 goto out;
3267 if (!(dompath = libxl__xs_get_dompath(&gc, domid)))
3268 goto out;
3270 retry_transaction:
3271 t = xs_transaction_start(ctx->xsh);
3272 for (i = 0; i <= info.vcpu_max_id; i++)
3273 libxl__xs_write(&gc, t,
3274 libxl__sprintf(&gc, "%s/cpu/%u/availability", dompath, i),
3275 "%s", ((1 << i) & bitmask) ? "online" : "offline");
3276 if (!xs_transaction_end(ctx->xsh, t, 0)) {
3277 if (errno == EAGAIN)
3278 goto retry_transaction;
3279 } else
3280 rc = 0;
3281 out:
3282 libxl__free_all(&gc);
3283 return rc;
3286 /*
3287 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
3288 */
3289 int libxl_get_sched_id(libxl_ctx *ctx)
3291 int sched, ret;
3293 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
3294 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
3295 return ERROR_FAIL;
3297 return sched;
3300 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
3302 struct xen_domctl_sched_credit sdom;
3303 int rc;
3305 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
3306 if (rc != 0) {
3307 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
3308 return ERROR_FAIL;
3311 scinfo->weight = sdom.weight;
3312 scinfo->cap = sdom.cap;
3314 return 0;
3317 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
3319 struct xen_domctl_sched_credit sdom;
3320 xc_domaininfo_t domaininfo;
3321 int rc;
3323 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
3324 if (rc < 0) {
3325 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
3326 return ERROR_FAIL;
3328 if (rc != 1 || domaininfo.domain != domid)
3329 return ERROR_INVAL;
3332 if (scinfo->weight < 1 || scinfo->weight > 65535) {
3333 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3334 "Cpu weight out of range, valid values are within range from 1 to 65535");
3335 return ERROR_INVAL;
3338 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
3339 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3340 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
3341 ((domaininfo.max_vcpu_id + 1) * 100));
3342 return ERROR_INVAL;
3345 sdom.weight = scinfo->weight;
3346 sdom.cap = scinfo->cap;
3348 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
3349 if ( rc < 0 ) {
3350 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
3351 return ERROR_FAIL;
3354 return 0;
3357 static int trigger_type_from_string(char *trigger_name)
3359 if (!strcmp(trigger_name, "nmi"))
3360 return XEN_DOMCTL_SENDTRIGGER_NMI;
3361 else if (!strcmp(trigger_name, "reset"))
3362 return XEN_DOMCTL_SENDTRIGGER_RESET;
3363 else if (!strcmp(trigger_name, "init"))
3364 return XEN_DOMCTL_SENDTRIGGER_INIT;
3365 else if (!strcmp(trigger_name, "power"))
3366 return XEN_DOMCTL_SENDTRIGGER_POWER;
3367 else if (!strcmp(trigger_name, "sleep"))
3368 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
3369 else
3370 return -1;
3373 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
3375 int rc = -1;
3376 int trigger_type = trigger_type_from_string(trigger_name);
3378 if (trigger_type == -1) {
3379 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
3380 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
3381 return ERROR_INVAL;
3384 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
3385 if (rc != 0) {
3386 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3387 "Send trigger '%s' failed", trigger_name);
3388 return ERROR_FAIL;
3391 return 0;
3394 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
3396 libxl__gc gc = LIBXL_INIT_GC(ctx);
3397 char *dompath = libxl__xs_get_dompath(&gc, domid);
3399 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/control/sysrq", dompath), "%c", sysrq);
3401 libxl__free_all(&gc);
3402 return 0;
3405 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
3407 int ret;
3408 ret = xc_send_debug_keys(ctx->xch, keys);
3409 if ( ret < 0 ) {
3410 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "sending debug keys");
3411 return ERROR_FAIL;
3413 return 0;
3416 libxl_xen_console_reader *
3417 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
3419 libxl_xen_console_reader *cr;
3420 unsigned int size = 16384;
3421 char *buf = malloc(size);
3423 if (!buf) {
3424 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
3425 " size is %u", size);
3426 return NULL;
3429 cr = malloc(sizeof(libxl_xen_console_reader));
3430 if (!cr) {
3431 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc libxl_xen_console_reader");
3432 return NULL;
3435 memset(cr, 0, sizeof(libxl_xen_console_reader));
3436 cr->buffer = buf;
3437 cr->size = size;
3438 cr->count = size;
3439 cr->clear = clear;
3440 cr->incremental = 1;
3442 return cr;
3445 /* return values: *line_r
3446 * 1 success, whole line obtained from buffer non-0
3447 * 0 no more lines available right now 0
3448 * negative error code ERROR_* 0
3449 * On success *line_r is updated to point to a nul-terminated
3450 * string which is valid until the next call on the same console
3451 * reader. The libxl caller may overwrite parts of the string
3452 * if it wishes. */
3453 int libxl_xen_console_read_line(libxl_ctx *ctx,
3454 libxl_xen_console_reader *cr,
3455 char **line_r)
3457 int ret;
3459 memset(cr->buffer, 0, cr->size);
3460 ret = xc_readconsolering(ctx->xch, cr->buffer, &cr->count,
3461 cr->clear, cr->incremental, &cr->index);
3462 if (ret < 0) {
3463 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "reading console ring buffer");
3464 return ERROR_FAIL;
3466 if (!ret) {
3467 if (cr->count) {
3468 *line_r = cr->buffer;
3469 ret = 1;
3470 } else {
3471 *line_r = NULL;
3472 ret = 0;
3476 return ret;
3479 void libxl_xen_console_read_finish(libxl_ctx *ctx,
3480 libxl_xen_console_reader *cr)
3482 free(cr->buffer);
3483 free(cr);
3486 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
3488 libxl__gc gc = LIBXL_INIT_GC(ctx);
3489 char *dompath = libxl__xs_get_dompath(&gc, domid);
3490 char *vm_path, *start_time;
3491 uint32_t ret;
3493 vm_path = libxl__xs_read(
3494 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dompath));
3495 start_time = libxl__xs_read(
3496 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/start_time", vm_path));
3497 if (start_time == NULL) {
3498 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
3499 "Can't get start time of domain '%d'", domid);
3500 ret = -1;
3501 }else{
3502 ret = strtoul(start_time, NULL, 10);
3504 libxl__free_all(&gc);
3505 return ret;
3508 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
3510 int rc;
3511 char _buf[32768];
3513 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
3514 0, _buf);
3515 if (rc < 0) {
3516 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3517 "Can not get tmem list");
3518 return NULL;
3521 return strdup(_buf);
3524 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
3526 int rc;
3528 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
3529 0, NULL);
3530 if (rc < 0) {
3531 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3532 "Can not freeze tmem pools");
3533 return ERROR_FAIL;
3536 return rc;
3539 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
3541 int rc;
3543 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
3544 0, NULL);
3545 if (rc < 0) {
3546 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3547 "Can not destroy tmem pools");
3548 return ERROR_FAIL;
3551 return rc;
3554 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
3556 int rc;
3558 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
3559 0, NULL);
3560 if (rc < 0) {
3561 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3562 "Can not thaw tmem pools");
3563 return ERROR_FAIL;
3566 return rc;
3569 static int32_t tmem_setop_from_string(char *set_name)
3571 if (!strcmp(set_name, "weight"))
3572 return TMEMC_SET_WEIGHT;
3573 else if (!strcmp(set_name, "cap"))
3574 return TMEMC_SET_CAP;
3575 else if (!strcmp(set_name, "compress"))
3576 return TMEMC_SET_COMPRESS;
3577 else
3578 return -1;
3581 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
3583 int rc;
3584 int32_t subop = tmem_setop_from_string(name);
3586 if (subop == -1) {
3587 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
3588 "Invalid set, valid sets are <weight|cap|compress>");
3589 return ERROR_INVAL;
3591 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
3592 if (rc < 0) {
3593 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3594 "Can not set tmem %s", name);
3595 return ERROR_FAIL;
3598 return rc;
3601 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
3602 char* uuid, int auth)
3604 int rc;
3606 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
3607 if (rc < 0) {
3608 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3609 "Can not set tmem shared auth");
3610 return ERROR_FAIL;
3613 return rc;
3616 int libxl_tmem_freeable(libxl_ctx *ctx)
3618 int rc;
3620 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
3621 if (rc < 0) {
3622 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3623 "Can not get tmem freeable memory");
3624 return ERROR_FAIL;
3627 return rc;
3630 void libxl_file_reference_destroy(libxl_file_reference *f)
3632 libxl__file_reference_unmap(f);
3633 free(f->path);
3636 int libxl_get_freecpus(libxl_ctx *ctx, libxl_cpumap *cpumap)
3638 int ncpus;
3640 ncpus = libxl_get_max_cpus(ctx);
3641 if (ncpus == 0)
3642 return ERROR_FAIL;
3644 cpumap->map = xc_cpupool_freeinfo(ctx->xch);
3645 if (cpumap->map == NULL)
3646 return ERROR_FAIL;
3648 cpumap->size = (ncpus + 7) / 8;
3650 return 0;
3653 int libxl_create_cpupool(libxl_ctx *ctx, char *name, int schedid,
3654 libxl_cpumap cpumap, libxl_uuid *uuid,
3655 uint32_t *poolid)
3657 libxl__gc gc = LIBXL_INIT_GC(ctx);
3658 int rc;
3659 int i;
3660 xs_transaction_t t;
3661 char *uuid_string;
3663 uuid_string = libxl__uuid2string(&gc, *uuid);
3664 if (!uuid_string)
3665 return ERROR_NOMEM;
3667 rc = xc_cpupool_create(ctx->xch, poolid, schedid);
3668 if (rc) {
3669 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3670 "Could not create cpupool");
3671 return ERROR_FAIL;
3674 libxl_for_each_cpu(i, cpumap)
3675 if (libxl_cpumap_test(&cpumap, i)) {
3676 rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
3677 if (rc) {
3678 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3679 "Error moving cpu to cpupool");
3680 libxl_destroy_cpupool(ctx, *poolid);
3681 return ERROR_FAIL;
3685 for (;;) {
3686 t = xs_transaction_start(ctx->xsh);
3688 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/pool/%d", *poolid));
3689 libxl__xs_write(&gc, t,
3690 libxl__sprintf(&gc, "/local/pool/%d/uuid", *poolid),
3691 "%s", uuid_string);
3692 libxl__xs_write(&gc, t,
3693 libxl__sprintf(&gc, "/local/pool/%d/name", *poolid),
3694 "%s", name);
3696 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3697 return 0;
3701 int libxl_destroy_cpupool(libxl_ctx *ctx, uint32_t poolid)
3703 libxl__gc gc = LIBXL_INIT_GC(ctx);
3704 int rc, i;
3705 xc_cpupoolinfo_t *info;
3706 xs_transaction_t t;
3707 libxl_cpumap cpumap;
3709 info = xc_cpupool_getinfo(ctx->xch, poolid);
3710 if (info == NULL)
3711 return ERROR_NOMEM;
3713 rc = ERROR_INVAL;
3714 if ((info->cpupool_id != poolid) || (info->n_dom))
3715 goto out;
3717 rc = ERROR_NOMEM;
3718 if (libxl_cpumap_alloc(ctx, &cpumap))
3719 goto out;
3721 memcpy(cpumap.map, info->cpumap, cpumap.size);
3722 libxl_for_each_cpu(i, cpumap)
3723 if (libxl_cpumap_test(&cpumap, i)) {
3724 rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
3725 if (rc) {
3726 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3727 "Error removing cpu from cpupool");
3728 rc = ERROR_FAIL;
3729 goto out1;
3733 rc = xc_cpupool_destroy(ctx->xch, poolid);
3734 if (rc) {
3735 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "Could not destroy cpupool");
3736 rc = ERROR_FAIL;
3737 goto out1;
3740 for (;;) {
3741 t = xs_transaction_start(ctx->xsh);
3743 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/pool/%d", poolid));
3745 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3746 break;
3749 rc = 0;
3751 out1:
3752 libxl_cpumap_destroy(&cpumap);
3753 out:
3754 xc_cpupool_infofree(ctx->xch, info);
3756 return rc;
3759 int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
3761 int rc;
3763 rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
3764 if (rc) {
3765 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3766 "Error moving cpu to cpupool");
3767 return ERROR_FAIL;
3769 return 0;
3772 int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
3774 int rc;
3776 rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
3777 if (rc) {
3778 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3779 "Error removing cpu from cpupool");
3780 return ERROR_FAIL;
3782 return 0;
3785 int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
3787 libxl__gc gc = LIBXL_INIT_GC(ctx);
3788 int rc;
3789 char *dom_path;
3790 char *vm_path;
3791 char *poolname;
3792 xs_transaction_t t;
3794 dom_path = libxl__xs_get_dompath(&gc, domid);
3795 if (!dom_path) {
3796 return ERROR_FAIL;
3799 rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
3800 if (rc) {
3801 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3802 "Error moving domain to cpupool");
3803 return ERROR_FAIL;
3806 for (;;) {
3807 t = xs_transaction_start(ctx->xsh);
3809 poolname = libxl__cpupoolid_to_name(&gc, poolid);
3810 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
3811 if (!vm_path)
3812 break;
3814 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/pool_name", vm_path),
3815 "%s", poolname);
3817 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3818 break;
3821 return 0;