debuggers.hg

view tools/libxl/libxl.c @ 22425:c553d3035fd5

tools: libxl: fix cpuid compilation errors for ia64

ia64 does not have cpuid. So break out cpuid-related functions into a
separate file, with stubs for ia64.

Signed-off-by: KUWAMURA Shin'ya <kuwa@jp.fujitsu.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author KUWAMURA Shin'ya <kuwa@jp.fujitsu.com>
date Tue Nov 09 17:43:12 2010 +0000 (2010-11-09)
parents 477dda47486e
children 9ffd785319e0
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 device.backend_kind = DEVICE_TAP;
1860 break;
1862 default:
1863 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
1864 rc = ERROR_INVAL;
1865 goto out_free;
1868 flexarray_set(back, boffset++, "frontend-id");
1869 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", disk->domid));
1870 flexarray_set(back, boffset++, "online");
1871 flexarray_set(back, boffset++, "1");
1872 flexarray_set(back, boffset++, "removable");
1873 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", (disk->unpluggable) ? 1 : 0));
1874 flexarray_set(back, boffset++, "bootable");
1875 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1876 flexarray_set(back, boffset++, "state");
1877 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
1878 flexarray_set(back, boffset++, "dev");
1879 flexarray_set(back, boffset++, disk->virtpath);
1880 flexarray_set(back, boffset++, "type");
1881 flexarray_set(back, boffset++, backend_type);
1882 flexarray_set(back, boffset++, "mode");
1883 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1885 flexarray_set(front, foffset++, "backend-id");
1886 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", disk->backend_domid));
1887 flexarray_set(front, foffset++, "state");
1888 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
1889 flexarray_set(front, foffset++, "virtual-device");
1890 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", devid));
1891 flexarray_set(front, foffset++, "device-type");
1892 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1894 if (0 /* protocol != native*/) {
1895 flexarray_set(front, foffset++, "protocol");
1896 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1899 libxl__device_generic_add(ctx, &device,
1900 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
1901 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
1903 rc = 0;
1905 out_free:
1906 flexarray_free(back);
1907 flexarray_free(front);
1908 out:
1909 libxl__free_all(&gc);
1910 return rc;
1913 int libxl_device_disk_del(libxl_ctx *ctx,
1914 libxl_device_disk *disk, int wait)
1916 libxl__device device;
1917 int devid;
1919 devid = libxl__device_disk_dev_number(disk->virtpath);
1920 device.backend_domid = disk->backend_domid;
1921 device.backend_devid = devid;
1922 device.backend_kind =
1923 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1924 device.domid = disk->domid;
1925 device.devid = devid;
1926 device.kind = DEVICE_VBD;
1927 return libxl__device_del(ctx, &device, wait);
1930 char * libxl_device_disk_local_attach(libxl_ctx *ctx, libxl_device_disk *disk)
1932 libxl__gc gc = LIBXL_INIT_GC(ctx);
1933 const char *dev = NULL;
1934 char *ret;
1935 int phystype = disk->phystype;
1936 switch (phystype) {
1937 case PHYSTYPE_PHY: {
1938 fprintf(stderr, "attaching PHY disk %s to domain 0\n", disk->physpath);
1939 dev = disk->physpath;
1940 break;
1942 case PHYSTYPE_FILE:
1943 /* let's pretend is tap:aio for the moment */
1944 phystype = PHYSTYPE_AIO;
1945 case PHYSTYPE_AIO:
1946 case PHYSTYPE_QCOW:
1947 case PHYSTYPE_QCOW2:
1948 case PHYSTYPE_VHD:
1949 if (libxl__blktap_enabled(&gc))
1950 dev = libxl__blktap_devpath(&gc, disk->physpath, phystype);
1951 break;
1953 default:
1954 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk physical type: %d\n", phystype);
1955 break;
1957 ret = strdup(dev);
1958 libxl__free_all(&gc);
1959 return ret;
1962 int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk)
1964 /* Nothing to do for PHYSTYPE_PHY. */
1966 /*
1967 * For other device types assume that the blktap2 process is
1968 * needed by the soon to be started domain and do nothing.
1969 */
1971 return 0;
1974 /******************************************************************************/
1975 int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1977 libxl__gc gc = LIBXL_INIT_GC(ctx);
1978 flexarray_t *front;
1979 flexarray_t *back;
1980 unsigned int boffset = 0;
1981 unsigned int foffset = 0;
1982 libxl__device device;
1983 char *dompath, **l;
1984 unsigned int nb, rc;
1986 front = flexarray_make(16, 1);
1987 if (!front) {
1988 rc = ERROR_NOMEM;
1989 goto out;
1991 back = flexarray_make(16, 1);
1992 if (!back) {
1993 rc = ERROR_NOMEM;
1994 goto out_free;
1997 if (nic->devid == -1) {
1998 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
1999 rc = ERROR_FAIL;
2000 goto out_free;
2002 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
2003 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb))) {
2004 nic->devid = 0;
2005 } else {
2006 nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;
2010 device.backend_devid = nic->devid;
2011 device.backend_domid = nic->backend_domid;
2012 device.backend_kind = DEVICE_VIF;
2013 device.devid = nic->devid;
2014 device.domid = nic->domid;
2015 device.kind = DEVICE_VIF;
2017 flexarray_set(back, boffset++, "frontend-id");
2018 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->domid));
2019 flexarray_set(back, boffset++, "online");
2020 flexarray_set(back, boffset++, "1");
2021 flexarray_set(back, boffset++, "state");
2022 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2023 flexarray_set(back, boffset++, "script");
2024 flexarray_set(back, boffset++, nic->script);
2025 flexarray_set(back, boffset++, "mac");
2026 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2027 nic->mac[0], nic->mac[1], nic->mac[2],
2028 nic->mac[3], nic->mac[4], nic->mac[5]));
2029 flexarray_set(back, boffset++, "bridge");
2030 flexarray_set(back, boffset++, libxl__strdup(&gc, nic->bridge));
2031 flexarray_set(back, boffset++, "handle");
2032 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", nic->devid));
2034 flexarray_set(front, foffset++, "backend-id");
2035 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->backend_domid));
2036 flexarray_set(front, foffset++, "state");
2037 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2038 flexarray_set(front, foffset++, "handle");
2039 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", nic->devid));
2040 flexarray_set(front, foffset++, "mac");
2041 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2042 nic->mac[0], nic->mac[1], nic->mac[2],
2043 nic->mac[3], nic->mac[4], nic->mac[5]));
2044 if (0 /* protocol != native*/) {
2045 flexarray_set(front, foffset++, "protocol");
2046 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
2049 libxl__device_generic_add(ctx, &device,
2050 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2051 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2053 /* FIXME: wait for plug */
2054 rc = 0;
2055 out_free:
2056 flexarray_free(back);
2057 flexarray_free(front);
2058 out:
2059 libxl__free_all(&gc);
2060 return rc;
2063 int libxl_device_nic_del(libxl_ctx *ctx,
2064 libxl_device_nic *nic, int wait)
2066 libxl__device device;
2068 device.backend_devid = nic->devid;
2069 device.backend_domid = nic->backend_domid;
2070 device.backend_kind = DEVICE_VIF;
2071 device.devid = nic->devid;
2072 device.domid = nic->domid;
2073 device.kind = DEVICE_VIF;
2075 return libxl__device_del(ctx, &device, wait);
2078 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
2080 libxl__gc gc = LIBXL_INIT_GC(ctx);
2081 char *dompath, *nic_path_fe;
2082 char **l, **list;
2083 char *val, *tok;
2084 unsigned int nb_nics, i;
2085 libxl_nicinfo *res, *nics;
2087 dompath = libxl__xs_get_dompath(&gc, domid);
2088 if (!dompath)
2089 goto err;
2090 list = l = libxl__xs_directory(&gc, XBT_NULL,
2091 libxl__sprintf(&gc, "%s/device/vif", dompath), &nb_nics);
2092 if (!l)
2093 goto err;
2094 nics = res = calloc(nb_nics, sizeof (libxl_device_nic));
2095 if (!res)
2096 goto err;
2097 for (*nb = nb_nics; nb_nics > 0; --nb_nics, ++l, ++nics) {
2098 nic_path_fe = libxl__sprintf(&gc, "%s/device/vif/%s", dompath, *l);
2100 nics->backend = xs_read(ctx->xsh, XBT_NULL,
2101 libxl__sprintf(&gc, "%s/backend", nic_path_fe), NULL);
2102 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", nic_path_fe));
2103 nics->backend_id = val ? strtoul(val, NULL, 10) : -1;
2105 nics->devid = strtoul(*l, NULL, 10);
2106 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", nic_path_fe));
2107 nics->state = val ? strtoul(val, NULL, 10) : -1;
2108 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", nic_path_fe));
2109 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2110 ++i, tok = strtok(NULL, ":")) {
2111 nics->mac[i] = strtoul(tok, NULL, 16);
2113 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", nic_path_fe));
2114 nics->evtch = val ? strtol(val, NULL, 10) : -1;
2115 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/tx-ring-ref", nic_path_fe));
2116 nics->rref_tx = val ? strtol(val, NULL, 10) : -1;
2117 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/rx-ring-ref", nic_path_fe));
2118 nics->rref_rx = val ? strtol(val, NULL, 10) : -1;
2119 nics->frontend = xs_read(ctx->xsh, XBT_NULL,
2120 libxl__sprintf(&gc, "%s/frontend", nics->backend), NULL);
2121 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", nics->backend));
2122 nics->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2123 nics->script = xs_read(ctx->xsh, XBT_NULL,
2124 libxl__sprintf(&gc, "%s/script", nics->backend), NULL);
2127 libxl__free_all(&gc);
2128 return res;
2129 err:
2130 libxl__free_all(&gc);
2131 return NULL;
2134 /******************************************************************************/
2135 int libxl_device_net2_add(libxl_ctx *ctx, uint32_t domid, libxl_device_net2 *net2)
2137 libxl__gc gc = LIBXL_INIT_GC(ctx);
2138 flexarray_t *front, *back;
2139 unsigned int boffset = 0, foffset = 0;
2140 libxl__device device;
2141 char *dompath, *dom, **l;
2142 unsigned int nb;
2143 int rc;
2145 front = flexarray_make(16, 1);
2146 if (!front) {
2147 rc = ERROR_NOMEM;
2148 goto err;
2150 back = flexarray_make(16, 1);
2151 if (!back) {
2152 rc = ERROR_NOMEM;
2153 goto err_free;
2156 if (!(dompath = libxl__xs_get_dompath(&gc, domid))) {
2157 rc = ERROR_FAIL;
2158 goto err_free;
2160 dom = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/name", dompath));
2162 if (net2->devid == -1) {
2163 if (!(l = libxl__xs_directory(&gc, XBT_NULL,
2164 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb))) {
2165 net2->devid = 0;
2166 } else {
2167 net2->devid = strtoul(l[nb - 1], NULL, 10) + 1;
2171 device.backend_devid = net2->devid;
2172 device.backend_domid = net2->backend_domid;
2173 device.backend_kind = DEVICE_VIF2;
2174 device.devid = net2->devid;
2175 device.domid = net2->domid;
2176 device.kind = DEVICE_VIF2;
2178 flexarray_set(back, boffset++, "domain");
2179 flexarray_set(back, boffset++, dom);
2180 flexarray_set(back, boffset++, "frontend-id");
2181 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->domid));
2183 flexarray_set(back, boffset++, "local-trusted");
2184 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
2185 flexarray_set(back, boffset++, "mac");
2186 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2187 net2->back_mac[0], net2->back_mac[1],
2188 net2->back_mac[2], net2->back_mac[3],
2189 net2->back_mac[4], net2->back_mac[5]));
2191 flexarray_set(back, boffset++, "remote-trusted");
2192 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->trusted));
2193 flexarray_set(back, boffset++, "remote-mac");
2194 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2195 net2->front_mac[0], net2->front_mac[1],
2196 net2->front_mac[2], net2->front_mac[3],
2197 net2->front_mac[4], net2->front_mac[5]));
2199 flexarray_set(back, boffset++, "max-bypasses");
2200 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->max_bypasses));
2201 flexarray_set(back, boffset++, "filter-mac");
2202 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
2203 flexarray_set(back, boffset++, "handle");
2204 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", net2->devid));
2205 flexarray_set(back, boffset++, "online");
2206 flexarray_set(back, boffset++, "1");
2207 flexarray_set(back, boffset++, "state");
2208 flexarray_set(back, boffset++, "1");
2210 flexarray_set(front, foffset++, "backend-id");
2211 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->backend_domid));
2213 flexarray_set(front, foffset++, "local-trusted");
2214 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->trusted));
2215 flexarray_set(front, foffset++, "mac");
2216 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2217 net2->front_mac[0], net2->front_mac[1],
2218 net2->front_mac[2], net2->front_mac[3],
2219 net2->front_mac[4], net2->front_mac[5]));
2221 flexarray_set(front, foffset++, "remote-trusted");
2222 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", net2->back_trusted));
2223 flexarray_set(front, foffset++, "remote-mac");
2224 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%02x:%02x:%02x:%02x:%02x:%02x",
2225 net2->back_mac[0], net2->back_mac[1],
2226 net2->back_mac[2], net2->back_mac[3],
2227 net2->back_mac[4], net2->back_mac[5]));
2229 flexarray_set(front, foffset++, "filter-mac");
2230 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", !!(net2->filter_mac)));
2231 flexarray_set(front, foffset++, "state");
2232 flexarray_set(front, foffset++, "1");
2234 libxl__device_generic_add(ctx, &device,
2235 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2236 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2238 /* FIXME: wait for plug */
2239 rc = 0;
2240 err_free:
2241 flexarray_free(back);
2242 flexarray_free(front);
2243 err:
2244 libxl__free_all(&gc);
2245 return rc;
2248 libxl_net2info *libxl_device_net2_list(libxl_ctx *ctx, uint32_t domid, unsigned int *nb)
2250 libxl__gc gc = LIBXL_INIT_GC(ctx);
2251 char *dompath, *net2_path_fe;
2252 char **l;
2253 char *val, *tok;
2254 unsigned int nb_net2s, i;
2255 libxl_net2info *res, *net2s;
2257 dompath = libxl__xs_get_dompath(&gc, domid);
2258 if (!dompath)
2259 goto err;
2260 l = libxl__xs_directory(&gc, XBT_NULL,
2261 libxl__sprintf(&gc, "%s/device/vif2", dompath), &nb_net2s);
2262 if (!l)
2263 goto err;
2264 res = calloc(nb_net2s, sizeof (libxl_net2info));
2265 if (!res)
2266 goto err;
2267 net2s = res;
2268 for (*nb = nb_net2s; nb_net2s > 0; --nb_net2s, ++l, ++net2s) {
2269 net2_path_fe = libxl__sprintf(&gc, "%s/device/vif2/%s", dompath, *l);
2271 net2s->backend = libxl__xs_read(&gc, XBT_NULL,
2272 libxl__sprintf(&gc, "%s/backend", net2_path_fe));
2273 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", net2_path_fe));
2274 net2s->backend_id = val ? strtoul(val, NULL, 10) : -1;
2276 net2s->devid = strtoul(*l, NULL, 10);
2277 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", net2_path_fe));
2278 net2s->state = val ? strtoul(val, NULL, 10) : -1;
2280 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/mac", net2_path_fe));
2281 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2282 ++i, tok = strtok(NULL, ":")) {
2283 net2s->mac[i] = strtoul(tok, NULL, 16);
2285 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-trusted", net2_path_fe));
2286 net2s->trusted = val ? strtoul(val, NULL, 10) : -1;
2288 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/remote-mac", net2_path_fe));
2289 for (i = 0, tok = strtok(val, ":"); tok && (i < 6);
2290 ++i, tok = strtok(NULL, ":")) {
2291 net2s->back_mac[i] = strtoul(tok, NULL, 16);
2293 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/filter-mac", net2_path_fe));
2294 net2s->filter_mac = val ? strtoul(val, NULL, 10) : -1;
2296 net2s->frontend = libxl__xs_read(&gc, XBT_NULL,
2297 libxl__sprintf(&gc, "%s/frontend", net2s->backend));
2298 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", net2s->backend));
2299 net2s->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2302 libxl__free_all(&gc);
2303 return res;
2304 err:
2305 libxl__free_all(&gc);
2306 return NULL;
2309 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
2311 libxl__device device;
2313 device.backend_devid = net2->devid;
2314 device.backend_domid = net2->backend_domid;
2315 device.backend_kind = DEVICE_VIF2;
2316 device.devid = net2->devid;
2317 device.domid = net2->domid;
2318 device.kind = DEVICE_VIF2;
2320 return libxl__device_del(ctx, &device, wait);
2324 /******************************************************************************/
2325 int libxl_device_console_add(libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
2327 libxl__gc gc = LIBXL_INIT_GC(ctx);
2328 flexarray_t *front;
2329 flexarray_t *back;
2330 unsigned int boffset = 0;
2331 unsigned int foffset = 0;
2332 libxl__device device;
2333 int rc;
2335 front = flexarray_make(16, 1);
2336 if (!front) {
2337 rc = ERROR_NOMEM;
2338 goto out;
2340 back = flexarray_make(16, 1);
2341 if (!back) {
2342 rc = ERROR_NOMEM;
2343 goto out_free;
2346 device.backend_devid = console->devid;
2347 device.backend_domid = console->backend_domid;
2348 device.backend_kind = DEVICE_CONSOLE;
2349 device.devid = console->devid;
2350 device.domid = console->domid;
2351 device.kind = DEVICE_CONSOLE;
2353 flexarray_set(back, boffset++, "frontend-id");
2354 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", console->domid));
2355 flexarray_set(back, boffset++, "online");
2356 flexarray_set(back, boffset++, "1");
2357 flexarray_set(back, boffset++, "state");
2358 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2359 flexarray_set(back, boffset++, "domain");
2360 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
2361 flexarray_set(back, boffset++, "protocol");
2362 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
2364 flexarray_set(front, foffset++, "backend-id");
2365 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", console->backend_domid));
2366 flexarray_set(front, foffset++, "limit");
2367 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
2368 flexarray_set(front, foffset++, "type");
2369 if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
2370 flexarray_set(front, foffset++, "xenconsoled");
2371 else
2372 flexarray_set(front, foffset++, "ioemu");
2373 flexarray_set(front, foffset++, "output");
2374 flexarray_set(front, foffset++, console->output);
2376 if (device.devid == 0) {
2377 if (console->build_state == NULL) {
2378 rc = ERROR_INVAL;
2379 goto out_free;
2381 flexarray_set(front, foffset++, "port");
2382 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%"PRIu32, console->build_state->console_port));
2383 flexarray_set(front, foffset++, "ring-ref");
2384 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%lu", console->build_state->console_mfn));
2385 } else {
2386 flexarray_set(front, foffset++, "state");
2387 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2388 flexarray_set(front, foffset++, "protocol");
2389 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
2392 libxl__device_generic_add(ctx, &device,
2393 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2394 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2395 rc = 0;
2396 out_free:
2397 flexarray_free(back);
2398 flexarray_free(front);
2399 out:
2400 libxl__free_all(&gc);
2401 return rc;
2404 /******************************************************************************/
2405 int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
2407 libxl__gc gc = LIBXL_INIT_GC(ctx);
2408 flexarray_t *front;
2409 flexarray_t *back;
2410 unsigned int boffset = 0;
2411 unsigned int foffset = 0;
2412 libxl__device device;
2413 int rc;
2415 front = flexarray_make(16, 1);
2416 if (!front) {
2417 rc = ERROR_NOMEM;
2418 goto out;
2420 back = flexarray_make(16, 1);
2421 if (!back) {
2422 rc = ERROR_NOMEM;
2423 goto out_free;
2426 device.backend_devid = vkb->devid;
2427 device.backend_domid = vkb->backend_domid;
2428 device.backend_kind = DEVICE_VKBD;
2429 device.devid = vkb->devid;
2430 device.domid = vkb->domid;
2431 device.kind = DEVICE_VKBD;
2433 flexarray_set(back, boffset++, "frontend-id");
2434 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vkb->domid));
2435 flexarray_set(back, boffset++, "online");
2436 flexarray_set(back, boffset++, "1");
2437 flexarray_set(back, boffset++, "state");
2438 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2439 flexarray_set(back, boffset++, "domain");
2440 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
2442 flexarray_set(front, foffset++, "backend-id");
2443 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vkb->backend_domid));
2444 flexarray_set(front, foffset++, "state");
2445 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2447 libxl__device_generic_add(ctx, &device,
2448 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2449 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2450 rc = 0;
2451 out_free:
2452 flexarray_free(back);
2453 flexarray_free(front);
2454 out:
2455 libxl__free_all(&gc);
2456 return rc;
2459 int libxl_device_vkb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2461 return ERROR_NI;
2464 int libxl_device_vkb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2466 return ERROR_NI;
2469 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int *num)
2471 libxl__gc gc = LIBXL_INIT_GC(ctx);
2472 char *be_path_tap, *be_path_vbd;
2473 libxl_device_disk *dend, *disks, *ret = NULL;
2474 char **b, **l = NULL;
2475 unsigned int numl, len;
2476 char *type;
2478 be_path_vbd = libxl__sprintf(&gc, "%s/backend/vbd/%d", libxl__xs_get_dompath(&gc, 0), domid);
2479 be_path_tap = libxl__sprintf(&gc, "%s/backend/tap/%d", libxl__xs_get_dompath(&gc, 0), domid);
2481 b = l = libxl__xs_directory(&gc, XBT_NULL, be_path_vbd, &numl);
2482 if (l) {
2483 ret = realloc(ret, sizeof(libxl_device_disk) * numl);
2484 disks = ret;
2485 *num = numl;
2486 dend = ret + *num;
2487 for (; disks < dend; ++disks, ++l) {
2488 disks->backend_domid = 0;
2489 disks->domid = domid;
2490 disks->physpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path_vbd, *l), &len);
2491 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path_vbd, *l)), &(disks->phystype));
2492 disks->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path_vbd, *l), &len);
2493 disks->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path_vbd, *l)));
2494 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path_vbd, *l)), "w"))
2495 disks->readwrite = 1;
2496 else
2497 disks->readwrite = 0;
2498 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))));
2499 disks->is_cdrom = !strcmp(type, "cdrom");
2502 b = l = libxl__xs_directory(&gc, XBT_NULL, be_path_tap, &numl);
2503 if (l) {
2504 ret = realloc(ret, sizeof(libxl_device_disk) * (*num + numl));
2505 disks = ret + *num;
2506 *num += numl;
2507 for (dend = ret + *num; disks < dend; ++disks, ++l) {
2508 disks->backend_domid = 0;
2509 disks->domid = domid;
2510 disks->physpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/params", be_path_tap, *l), &len);
2511 libxl_string_to_phystype(ctx, libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/type", be_path_tap, *l)), &(disks->phystype));
2512 disks->virtpath = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/%s/dev", be_path_tap, *l), &len);
2513 disks->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/removable", be_path_tap, *l)));
2514 if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/%s/mode", be_path_tap, *l)), "w"))
2515 disks->readwrite = 1;
2516 else
2517 disks->readwrite = 0;
2518 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))));
2519 disks->is_cdrom = !strcmp(type, "cdrom");
2522 libxl__free_all(&gc);
2523 return ret;
2526 int libxl_device_disk_getinfo(libxl_ctx *ctx, uint32_t domid,
2527 libxl_device_disk *disk, libxl_diskinfo *diskinfo)
2529 libxl__gc gc = LIBXL_INIT_GC(ctx);
2530 char *dompath, *diskpath;
2531 char *val;
2533 dompath = libxl__xs_get_dompath(&gc, domid);
2534 diskinfo->devid = libxl__device_disk_dev_number(disk->virtpath);
2536 /* tap devices entries in xenstore are written as vbd devices. */
2537 diskpath = libxl__sprintf(&gc, "%s/device/vbd/%d", dompath, diskinfo->devid);
2538 diskinfo->backend = xs_read(ctx->xsh, XBT_NULL,
2539 libxl__sprintf(&gc, "%s/backend", diskpath), NULL);
2540 if (!diskinfo->backend) {
2541 libxl__free_all(&gc);
2542 return ERROR_FAIL;
2544 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend-id", diskpath));
2545 diskinfo->backend_id = val ? strtoul(val, NULL, 10) : -1;
2546 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/state", diskpath));
2547 diskinfo->state = val ? strtoul(val, NULL, 10) : -1;
2548 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/event-channel", diskpath));
2549 diskinfo->evtch = val ? strtoul(val, NULL, 10) : -1;
2550 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/ring-ref", diskpath));
2551 diskinfo->rref = val ? strtoul(val, NULL, 10) : -1;
2552 diskinfo->frontend = xs_read(ctx->xsh, XBT_NULL,
2553 libxl__sprintf(&gc, "%s/frontend", diskinfo->backend), NULL);
2554 val = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/frontend-id", diskinfo->backend));
2555 diskinfo->frontend_id = val ? strtoul(val, NULL, 10) : -1;
2557 libxl__free_all(&gc);
2558 return 0;
2561 int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
2563 int num, i;
2564 uint32_t stubdomid;
2565 libxl_device_disk *disks;
2566 int ret = ERROR_FAIL;
2568 if (!disk->physpath) {
2569 disk->physpath = "";
2570 disk->phystype = PHYSTYPE_PHY;
2572 disks = libxl_device_disk_list(ctx, domid, &num);
2573 for (i = 0; i < num; i++) {
2574 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
2575 /* found */
2576 break;
2578 if (i == num) {
2579 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Virtual device not found");
2580 goto out;
2583 ret = 0;
2585 libxl_device_disk_del(ctx, disks + i, 1);
2586 libxl_device_disk_add(ctx, domid, disk);
2587 stubdomid = libxl_get_stubdom_id(ctx, domid);
2588 if (stubdomid) {
2589 disks[i].domid = stubdomid;
2590 libxl_device_disk_del(ctx, disks + i, 1);
2591 disk->domid = stubdomid;
2592 libxl_device_disk_add(ctx, stubdomid, disk);
2593 disk->domid = domid;
2595 out:
2596 for (i = 0; i < num; i++)
2597 libxl_device_disk_destroy(&disks[i]);
2598 free(disks);
2599 return ret;
2602 /******************************************************************************/
2603 static int libxl_build_xenpv_qemu_args(libxl__gc *gc,
2604 uint32_t domid,
2605 libxl_device_vfb *vfb,
2606 libxl_device_model_info *info)
2608 libxl_ctx *ctx = libxl__gc_owner(gc);
2609 memset(info, 0x00, sizeof(libxl_device_model_info));
2611 if (vfb != NULL) {
2612 info->vnc = vfb->vnc;
2613 if (vfb->vnclisten)
2614 info->vnclisten = libxl__strdup(gc, vfb->vnclisten);
2615 info->vncdisplay = vfb->vncdisplay;
2616 info->vncunused = vfb->vncunused;
2617 if (vfb->vncpasswd)
2618 info->vncpasswd = vfb->vncpasswd;
2619 if (vfb->keymap)
2620 info->keymap = libxl__strdup(gc, vfb->keymap);
2621 info->sdl = vfb->sdl;
2622 info->opengl = vfb->opengl;
2623 } else
2624 info->nographic = 1;
2625 info->domid = domid;
2626 info->dom_name = libxl_domid_to_name(ctx, domid);
2627 info->device_model = libxl__abs_path(gc, "qemu-dm", libxl_libexec_path());
2628 info->type = XENPV;
2629 return 0;
2632 int libxl_create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb,
2633 libxl_device_model_starting **starting_r)
2635 libxl__gc gc = LIBXL_INIT_GC(ctx);
2636 libxl_device_model_info info;
2638 libxl_build_xenpv_qemu_args(&gc, domid, vfb, &info);
2639 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
2640 libxl__free_all(&gc);
2641 return 0;
2644 int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
2646 libxl__gc gc = LIBXL_INIT_GC(ctx);
2647 flexarray_t *front;
2648 flexarray_t *back;
2649 unsigned int boffset = 0;
2650 unsigned int foffset = 0;
2651 libxl__device device;
2652 int rc;
2654 front = flexarray_make(16, 1);
2655 if (!front) {
2656 rc = ERROR_NOMEM;
2657 goto out;
2659 back = flexarray_make(16, 1);
2660 if (!back) {
2661 rc = ERROR_NOMEM;
2662 goto out_free;
2665 device.backend_devid = vfb->devid;
2666 device.backend_domid = vfb->backend_domid;
2667 device.backend_kind = DEVICE_VFB;
2668 device.devid = vfb->devid;
2669 device.domid = vfb->domid;
2670 device.kind = DEVICE_VFB;
2672 flexarray_set(back, boffset++, "frontend-id");
2673 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->domid));
2674 flexarray_set(back, boffset++, "online");
2675 flexarray_set(back, boffset++, "1");
2676 flexarray_set(back, boffset++, "state");
2677 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", 1));
2678 flexarray_set(back, boffset++, "domain");
2679 flexarray_set(back, boffset++, libxl__domid_to_name(&gc, domid));
2680 flexarray_set(back, boffset++, "vnc");
2681 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vnc));
2682 flexarray_set(back, boffset++, "vnclisten");
2683 flexarray_set(back, boffset++, vfb->vnclisten);
2684 flexarray_set(back, boffset++, "vncpasswd");
2685 flexarray_set(back, boffset++, vfb->vncpasswd);
2686 flexarray_set(back, boffset++, "vncdisplay");
2687 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncdisplay));
2688 flexarray_set(back, boffset++, "vncunused");
2689 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->vncunused));
2690 flexarray_set(back, boffset++, "sdl");
2691 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->sdl));
2692 flexarray_set(back, boffset++, "opengl");
2693 flexarray_set(back, boffset++, libxl__sprintf(&gc, "%d", vfb->opengl));
2694 if (vfb->xauthority) {
2695 flexarray_set(back, boffset++, "xauthority");
2696 flexarray_set(back, boffset++, vfb->xauthority);
2698 if (vfb->display) {
2699 flexarray_set(back, boffset++, "display");
2700 flexarray_set(back, boffset++, vfb->display);
2703 flexarray_set(front, foffset++, "backend-id");
2704 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", vfb->backend_domid));
2705 flexarray_set(front, foffset++, "state");
2706 flexarray_set(front, foffset++, libxl__sprintf(&gc, "%d", 1));
2708 libxl__device_generic_add(ctx, &device,
2709 libxl__xs_kvs_of_flexarray(&gc, back, boffset),
2710 libxl__xs_kvs_of_flexarray(&gc, front, foffset));
2711 rc = 0;
2712 out_free:
2713 flexarray_free(front);
2714 flexarray_free(back);
2715 out:
2716 libxl__free_all(&gc);
2717 return rc;
2720 int libxl_device_vfb_clean_shutdown(libxl_ctx *ctx, uint32_t domid)
2722 return ERROR_NI;
2725 int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid)
2727 return ERROR_NI;
2730 /******************************************************************************/
2732 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
2734 libxl__gc gc = LIBXL_INIT_GC(ctx);
2735 char *mem, *endptr;
2736 uint32_t memorykb;
2737 char *dompath = libxl__xs_get_dompath(&gc, domid);
2738 int rc = 1;
2740 mem = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/memory/target", dompath));
2741 if (!mem) {
2742 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot get memory info from %s/memory/target\n", dompath);
2743 goto out;
2745 memorykb = strtoul(mem, &endptr, 10);
2746 if (*endptr != '\0') {
2747 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "invalid memory %s from %s/memory/target\n", mem, dompath);
2748 goto out;
2751 if (max_memkb < memorykb) {
2752 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
2753 goto out;
2756 rc = 0;
2757 out:
2758 libxl__free_all(&gc);
2759 return rc;
2762 static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb)
2764 int rc;
2765 libxl_dominfo info;
2766 libxl_physinfo physinfo;
2767 char *target = NULL, *endptr = NULL;
2768 char *target_path = "/local/domain/0/memory/target";
2769 char *max_path = "/local/domain/0/memory/static-max";
2770 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2771 xs_transaction_t t;
2772 libxl_ctx *ctx = libxl__gc_owner(gc);
2773 uint32_t free_mem_slack = 0;
2775 retry_transaction:
2776 t = xs_transaction_start(ctx->xsh);
2778 target = libxl__xs_read(gc, t, target_path);
2779 if (target) {
2780 *target_memkb = strtoul(target, &endptr, 10);
2781 if (*endptr != '\0') {
2782 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2783 "invalid memory target %s from %s\n", target, target_path);
2784 rc = ERROR_FAIL;
2785 goto out;
2787 rc = 0;
2788 goto out;
2791 rc = libxl_domain_info(ctx, &info, 0);
2792 if (rc < 0)
2793 return rc;
2795 rc = libxl_get_physinfo(ctx, &physinfo);
2796 if (rc < 0)
2797 return rc;
2799 libxl__xs_write(gc, t, target_path, "%"PRIu32,
2800 (uint32_t) info.current_memkb);
2801 libxl__xs_write(gc, t, max_path, "%"PRIu32,
2802 (uint32_t) info.max_memkb);
2804 free_mem_slack = (uint32_t) (PAGE_TO_MEMKB(physinfo.total_pages) -
2805 info.current_memkb);
2806 /* From empirical measurements the free_mem_slack shouldn't be more
2807 * than 15% of the total memory present on the system. */
2808 if (free_mem_slack > PAGE_TO_MEMKB(physinfo.total_pages) * 0.15)
2809 free_mem_slack = PAGE_TO_MEMKB(physinfo.total_pages) * 0.15;
2810 libxl__xs_write(gc, t, free_mem_slack_path, "%"PRIu32, free_mem_slack);
2812 *target_memkb = (uint32_t) info.current_memkb;
2813 rc = 0;
2815 out:
2816 if (!xs_transaction_end(ctx->xsh, t, 0))
2817 if (errno == EAGAIN)
2818 goto retry_transaction;
2821 return rc;
2824 /* returns how much memory should be left free in the system */
2825 static int libxl__get_free_memory_slack(libxl__gc *gc, uint32_t *free_mem_slack)
2827 int rc;
2828 char *free_mem_slack_path = "/local/domain/0/memory/freemem-slack";
2829 char *free_mem_slack_s, *endptr;
2830 uint32_t target_memkb;
2832 retry:
2833 free_mem_slack_s = libxl__xs_read(gc, XBT_NULL, free_mem_slack_path);
2834 if (!free_mem_slack_s) {
2835 rc = libxl__fill_dom0_memory_info(gc, &target_memkb);
2836 if (rc < 0)
2837 return rc;
2838 goto retry;
2839 } else {
2840 *free_mem_slack = strtoul(free_mem_slack_s, &endptr, 10);
2841 if (*endptr != '\0') {
2842 LIBXL__LOG_ERRNO(gc->owner, LIBXL__LOG_ERROR,
2843 "invalid free_mem_slack %s from %s\n",
2844 free_mem_slack_s, free_mem_slack_path);
2845 return ERROR_FAIL;
2848 return 0;
2851 int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
2852 int32_t target_memkb, int relative, int enforce)
2854 libxl__gc gc = LIBXL_INIT_GC(ctx);
2855 int rc = 1, abort = 0;
2856 uint32_t memorykb = 0, videoram = 0;
2857 uint32_t current_target_memkb = 0, new_target_memkb = 0;
2858 char *memmax, *endptr, *videoram_s = NULL, *target = NULL;
2859 char *dompath = libxl__xs_get_dompath(&gc, domid);
2860 xc_domaininfo_t info;
2861 libxl_dominfo ptr;
2862 char *uuid;
2863 xs_transaction_t t;
2865 retry_transaction:
2866 t = xs_transaction_start(ctx->xsh);
2868 target = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2869 "%s/memory/target", dompath));
2870 if (!target && !domid) {
2871 xs_transaction_end(ctx->xsh, t, 1);
2872 rc = libxl__fill_dom0_memory_info(&gc, &current_target_memkb);
2873 if (rc < 0) {
2874 abort = 1;
2875 goto out;
2877 goto retry_transaction;
2878 } else if (!target) {
2879 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2880 "cannot get target memory info from %s/memory/target\n",
2881 dompath);
2882 abort = 1;
2883 goto out;
2884 } else {
2885 current_target_memkb = strtoul(target, &endptr, 10);
2886 if (*endptr != '\0') {
2887 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2888 "invalid memory target %s from %s/memory/target\n",
2889 target, dompath);
2890 abort = 1;
2891 goto out;
2894 memmax = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2895 "%s/memory/static-max", dompath));
2896 if (!memmax) {
2897 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2898 "cannot get memory info from %s/memory/static-max\n",
2899 dompath);
2900 abort = 1;
2901 goto out;
2903 memorykb = strtoul(memmax, &endptr, 10);
2904 if (*endptr != '\0') {
2905 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2906 "invalid max memory %s from %s/memory/static-max\n",
2907 memmax, dompath);
2908 abort = 1;
2909 goto out;
2912 if (relative)
2913 new_target_memkb = current_target_memkb + target_memkb;
2914 else
2915 new_target_memkb = target_memkb;
2916 if (new_target_memkb > memorykb) {
2917 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2918 "memory_dynamic_max must be less than or equal to"
2919 " memory_static_max\n");
2920 abort = 1;
2921 goto out;
2924 if (!domid && new_target_memkb < LIBXL_MIN_DOM0_MEM) {
2925 LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
2926 "new target %d for dom0 is below the minimum threshold\n",
2927 new_target_memkb);
2928 abort = 1;
2929 goto out;
2931 videoram_s = libxl__xs_read(&gc, t, libxl__sprintf(&gc,
2932 "%s/memory/videoram", dompath));
2933 videoram = videoram_s ? atoi(videoram_s) : 0;
2935 if (enforce) {
2936 memorykb = new_target_memkb;
2937 rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
2938 LIBXL_MAXMEM_CONSTANT);
2939 if (rc != 0) {
2940 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2941 "xc_domain_setmaxmem domid=%d memkb=%d failed "
2942 "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
2943 abort = 1;
2944 goto out;
2948 new_target_memkb -= videoram;
2949 rc = xc_domain_set_pod_target(ctx->xch, domid,
2950 new_target_memkb / 4, NULL, NULL, NULL);
2951 if (rc != 0) {
2952 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2953 "xc_domain_set_pod_target domid=%d, memkb=%d "
2954 "failed rc=%d\n", domid, new_target_memkb / 4,
2955 rc);
2956 abort = 1;
2957 goto out;
2960 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/memory/target",
2961 dompath), "%"PRIu32, new_target_memkb);
2962 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &info);
2963 if (rc != 1 || info.domain != domid) {
2964 abort = 1;
2965 goto out;
2967 xcinfo2xlinfo(&info, &ptr);
2968 uuid = libxl__uuid2string(&gc, ptr.uuid);
2969 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "/vm/%s/memory", uuid),
2970 "%"PRIu32, new_target_memkb / 1024);
2972 out:
2973 if (!xs_transaction_end(ctx->xsh, t, abort) && !abort)
2974 if (errno == EAGAIN)
2975 goto retry_transaction;
2977 libxl__free_all(&gc);
2978 return rc;
2981 int libxl_get_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t *out_target)
2983 libxl__gc gc = LIBXL_INIT_GC(ctx);
2984 int rc = 1;
2985 char *target = NULL, *endptr = NULL;
2986 char *dompath = libxl__xs_get_dompath(&gc, domid);
2987 uint32_t target_memkb;
2989 target = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc,
2990 "%s/memory/target", dompath));
2991 if (!target && !domid) {
2992 rc = libxl__fill_dom0_memory_info(&gc, &target_memkb);
2993 if (rc < 0)
2994 goto out;
2995 } else if (!target) {
2996 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
2997 "cannot get target memory info from %s/memory/target\n",
2998 dompath);
2999 goto out;
3000 } else {
3001 target_memkb = strtoul(target, &endptr, 10);
3002 if (*endptr != '\0') {
3003 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
3004 "invalid memory target %s from %s/memory/target\n",
3005 target, dompath);
3006 goto out;
3009 *out_target = target_memkb;
3010 rc = 0;
3012 out:
3013 libxl__free_all(&gc);
3014 return rc;
3017 int libxl_domain_need_memory(libxl_ctx *ctx, libxl_domain_build_info *b_info,
3018 libxl_device_model_info *dm_info, uint32_t *need_memkb)
3020 *need_memkb = b_info->target_memkb;
3021 if (b_info->hvm) {
3022 *need_memkb += b_info->shadow_memkb + LIBXL_HVM_EXTRA_MEMORY;
3023 if (strstr(dm_info->device_model, "stubdom-dm"))
3024 *need_memkb += 32 * 1024;
3025 } else
3026 *need_memkb += LIBXL_PV_EXTRA_MEMORY;
3027 if (*need_memkb % (2 * 1024))
3028 *need_memkb += (2 * 1024) - (*need_memkb % (2 * 1024));
3029 return 0;
3032 int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
3034 int rc = 0;
3035 libxl_physinfo info;
3036 uint32_t freemem_slack;
3037 libxl__gc gc = LIBXL_INIT_GC(ctx);
3039 rc = libxl_get_physinfo(ctx, &info);
3040 if (rc < 0)
3041 goto out;
3042 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
3043 if (rc < 0)
3044 goto out;
3046 if ((info.free_pages + info.scrub_pages) * 4 > freemem_slack)
3047 *memkb = (info.free_pages + info.scrub_pages) * 4 - freemem_slack;
3048 else
3049 *memkb = 0;
3051 out:
3052 libxl__free_all(&gc);
3053 return rc;
3056 int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
3057 memory_kb, int wait_secs)
3059 int rc = 0;
3060 libxl_physinfo info;
3061 uint32_t freemem_slack;
3062 libxl__gc gc = LIBXL_INIT_GC(ctx);
3064 rc = libxl__get_free_memory_slack(&gc, &freemem_slack);
3065 if (rc < 0)
3066 goto out;
3067 while (wait_secs > 0) {
3068 rc = libxl_get_physinfo(ctx, &info);
3069 if (rc < 0)
3070 goto out;
3071 if (info.free_pages * 4 - freemem_slack >= memory_kb) {
3072 rc = 0;
3073 goto out;
3075 wait_secs--;
3076 sleep(1);
3078 rc = ERROR_NOMEM;
3080 out:
3081 libxl__free_all(&gc);
3082 return rc;
3085 int libxl_wait_for_memory_target(libxl_ctx *ctx, uint32_t domid, int wait_secs)
3087 int rc = 0;
3088 uint32_t target_memkb = 0;
3089 libxl_dominfo info;
3091 do {
3092 wait_secs--;
3093 sleep(1);
3095 rc = libxl_get_memory_target(ctx, domid, &target_memkb);
3096 if (rc < 0)
3097 goto out;
3099 rc = libxl_domain_info(ctx, &info, domid);
3100 if (rc < 0)
3101 return rc;
3102 } while (wait_secs > 0 && info.current_memkb > target_memkb);
3104 if (info.current_memkb <= target_memkb)
3105 rc = 0;
3106 else
3107 rc = ERROR_FAIL;
3109 out:
3110 return 0;
3113 int libxl_button_press(libxl_ctx *ctx, uint32_t domid, libxl_button button)
3115 int rc = -1;
3117 switch (button) {
3118 case POWER_BUTTON:
3119 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
3120 break;
3121 case SLEEP_BUTTON:
3122 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
3123 break;
3124 default:
3125 break;
3128 return rc;
3131 int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
3133 xc_physinfo_t xcphysinfo = { 0 };
3134 int rc;
3136 rc = xc_physinfo(ctx->xch, &xcphysinfo);
3137 if (rc != 0) {
3138 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting physinfo");
3139 return ERROR_FAIL;
3141 physinfo->threads_per_core = xcphysinfo.threads_per_core;
3142 physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
3143 physinfo->max_cpu_id = xcphysinfo.max_cpu_id;
3144 physinfo->nr_cpus = xcphysinfo.nr_cpus;
3145 physinfo->cpu_khz = xcphysinfo.cpu_khz;
3146 physinfo->total_pages = xcphysinfo.total_pages;
3147 physinfo->free_pages = xcphysinfo.free_pages;
3148 physinfo->scrub_pages = xcphysinfo.scrub_pages;
3149 physinfo->nr_nodes = xcphysinfo.nr_nodes;
3150 memcpy(physinfo->hw_cap,xcphysinfo.hw_cap, sizeof(physinfo->hw_cap));
3151 physinfo->phys_cap = xcphysinfo.capabilities;
3153 return 0;
3156 const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx)
3158 union {
3159 xen_extraversion_t xen_extra;
3160 xen_compile_info_t xen_cc;
3161 xen_changeset_info_t xen_chgset;
3162 xen_capabilities_info_t xen_caps;
3163 xen_platform_parameters_t p_parms;
3164 xen_commandline_t xen_commandline;
3165 } u;
3166 long xen_version;
3167 libxl_version_info *info = &ctx->version_info;
3169 if (info->xen_version_extra != NULL)
3170 return info;
3172 xen_version = xc_version(ctx->xch, XENVER_version, NULL);
3173 info->xen_version_major = xen_version >> 16;
3174 info->xen_version_minor = xen_version & 0xFF;
3176 xc_version(ctx->xch, XENVER_extraversion, &u.xen_extra);
3177 info->xen_version_extra = strdup(u.xen_extra);
3179 xc_version(ctx->xch, XENVER_compile_info, &u.xen_cc);
3180 info->compiler = strdup(u.xen_cc.compiler);
3181 info->compile_by = strdup(u.xen_cc.compile_by);
3182 info->compile_domain = strdup(u.xen_cc.compile_domain);
3183 info->compile_date = strdup(u.xen_cc.compile_date);
3185 xc_version(ctx->xch, XENVER_capabilities, &u.xen_caps);
3186 info->capabilities = strdup(u.xen_caps);
3188 xc_version(ctx->xch, XENVER_changeset, &u.xen_chgset);
3189 info->changeset = strdup(u.xen_chgset);
3191 xc_version(ctx->xch, XENVER_platform_parameters, &u.p_parms);
3192 info->virt_start = u.p_parms.virt_start;
3194 info->pagesize = xc_version(ctx->xch, XENVER_pagesize, NULL);
3196 xc_version(ctx->xch, XENVER_commandline, &u.xen_commandline);
3197 info->commandline = strdup(u.xen_commandline);
3199 return info;
3202 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
3203 int *nb_vcpu, int *nrcpus)
3205 libxl_vcpuinfo *ptr, *ret;
3206 xc_domaininfo_t domaininfo;
3207 xc_vcpuinfo_t vcpuinfo;
3209 if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
3210 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting infolist");
3211 return NULL;
3213 *nrcpus = libxl_get_max_cpus(ctx);
3214 ret = ptr = calloc(domaininfo.max_vcpu_id + 1, sizeof (libxl_vcpuinfo));
3215 if (!ptr) {
3216 return NULL;
3219 for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
3220 if (libxl_cpumap_alloc(ctx, &ptr->cpumap)) {
3221 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "allocating cpumap");
3222 return NULL;
3224 if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
3225 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu info");
3226 return NULL;
3228 if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap.map) == -1) {
3229 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting vcpu affinity");
3230 return NULL;
3232 ptr->vcpuid = *nb_vcpu;
3233 ptr->cpu = vcpuinfo.cpu;
3234 ptr->online = !!vcpuinfo.online;
3235 ptr->blocked = !!vcpuinfo.blocked;
3236 ptr->running = !!vcpuinfo.running;
3237 ptr->vcpu_time = vcpuinfo.cpu_time;
3239 return ret;
3242 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
3243 libxl_cpumap *cpumap)
3245 if (xc_vcpu_setaffinity(ctx->xch, domid, vcpuid, cpumap->map)) {
3246 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting vcpu affinity");
3247 return ERROR_FAIL;
3249 return 0;
3252 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask)
3254 libxl__gc gc = LIBXL_INIT_GC(ctx);
3255 libxl_dominfo info;
3256 char *dompath;
3257 xs_transaction_t t;
3258 int i, rc = ERROR_FAIL;
3260 if (libxl_domain_info(ctx, &info, domid) < 0) {
3261 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
3262 goto out;
3264 if (!(dompath = libxl__xs_get_dompath(&gc, domid)))
3265 goto out;
3267 retry_transaction:
3268 t = xs_transaction_start(ctx->xsh);
3269 for (i = 0; i <= info.vcpu_max_id; i++)
3270 libxl__xs_write(&gc, t,
3271 libxl__sprintf(&gc, "%s/cpu/%u/availability", dompath, i),
3272 "%s", ((1 << i) & bitmask) ? "online" : "offline");
3273 if (!xs_transaction_end(ctx->xsh, t, 0)) {
3274 if (errno == EAGAIN)
3275 goto retry_transaction;
3276 } else
3277 rc = 0;
3278 out:
3279 libxl__free_all(&gc);
3280 return rc;
3283 /*
3284 * returns one of the XEN_SCHEDULER_* constants from public/domctl.h
3285 */
3286 int libxl_get_sched_id(libxl_ctx *ctx)
3288 int sched, ret;
3290 if ((ret = xc_sched_id(ctx->xch, &sched)) != 0) {
3291 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
3292 return ERROR_FAIL;
3294 return sched;
3297 int libxl_sched_credit_domain_get(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
3299 struct xen_domctl_sched_credit sdom;
3300 int rc;
3302 rc = xc_sched_credit_domain_get(ctx->xch, domid, &sdom);
3303 if (rc != 0) {
3304 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
3305 return ERROR_FAIL;
3308 scinfo->weight = sdom.weight;
3309 scinfo->cap = sdom.cap;
3311 return 0;
3314 int libxl_sched_credit_domain_set(libxl_ctx *ctx, uint32_t domid, libxl_sched_credit *scinfo)
3316 struct xen_domctl_sched_credit sdom;
3317 xc_domaininfo_t domaininfo;
3318 int rc;
3320 rc = xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo);
3321 if (rc < 0) {
3322 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting domain info list");
3323 return ERROR_FAIL;
3325 if (rc != 1 || domaininfo.domain != domid)
3326 return ERROR_INVAL;
3329 if (scinfo->weight < 1 || scinfo->weight > 65535) {
3330 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3331 "Cpu weight out of range, valid values are within range from 1 to 65535");
3332 return ERROR_INVAL;
3335 if (scinfo->cap < 0 || scinfo->cap > (domaininfo.max_vcpu_id + 1) * 100) {
3336 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3337 "Cpu cap out of range, valid range is from 0 to %d for specified number of vcpus",
3338 ((domaininfo.max_vcpu_id + 1) * 100));
3339 return ERROR_INVAL;
3342 sdom.weight = scinfo->weight;
3343 sdom.cap = scinfo->cap;
3345 rc = xc_sched_credit_domain_set(ctx->xch, domid, &sdom);
3346 if ( rc < 0 ) {
3347 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "setting domain sched credit");
3348 return ERROR_FAIL;
3351 return 0;
3354 static int trigger_type_from_string(char *trigger_name)
3356 if (!strcmp(trigger_name, "nmi"))
3357 return XEN_DOMCTL_SENDTRIGGER_NMI;
3358 else if (!strcmp(trigger_name, "reset"))
3359 return XEN_DOMCTL_SENDTRIGGER_RESET;
3360 else if (!strcmp(trigger_name, "init"))
3361 return XEN_DOMCTL_SENDTRIGGER_INIT;
3362 else if (!strcmp(trigger_name, "power"))
3363 return XEN_DOMCTL_SENDTRIGGER_POWER;
3364 else if (!strcmp(trigger_name, "sleep"))
3365 return XEN_DOMCTL_SENDTRIGGER_SLEEP;
3366 else
3367 return -1;
3370 int libxl_send_trigger(libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid)
3372 int rc = -1;
3373 int trigger_type = trigger_type_from_string(trigger_name);
3375 if (trigger_type == -1) {
3376 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
3377 "Invalid trigger, valid triggers are <nmi|reset|init|power|sleep>");
3378 return ERROR_INVAL;
3381 rc = xc_domain_send_trigger(ctx->xch, domid, trigger_type, vcpuid);
3382 if (rc != 0) {
3383 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3384 "Send trigger '%s' failed", trigger_name);
3385 return ERROR_FAIL;
3388 return 0;
3391 int libxl_send_sysrq(libxl_ctx *ctx, uint32_t domid, char sysrq)
3393 libxl__gc gc = LIBXL_INIT_GC(ctx);
3394 char *dompath = libxl__xs_get_dompath(&gc, domid);
3396 libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/control/sysrq", dompath), "%c", sysrq);
3398 libxl__free_all(&gc);
3399 return 0;
3402 int libxl_send_debug_keys(libxl_ctx *ctx, char *keys)
3404 int ret;
3405 ret = xc_send_debug_keys(ctx->xch, keys);
3406 if ( ret < 0 ) {
3407 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "sending debug keys");
3408 return ERROR_FAIL;
3410 return 0;
3413 libxl_xen_console_reader *
3414 libxl_xen_console_read_start(libxl_ctx *ctx, int clear)
3416 libxl_xen_console_reader *cr;
3417 unsigned int size = 16384;
3418 char *buf = malloc(size);
3420 if (!buf) {
3421 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc buffer for libxl_xen_console_reader,"
3422 " size is %u", size);
3423 return NULL;
3426 cr = malloc(sizeof(libxl_xen_console_reader));
3427 if (!cr) {
3428 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot malloc libxl_xen_console_reader");
3429 return NULL;
3432 memset(cr, 0, sizeof(libxl_xen_console_reader));
3433 cr->buffer = buf;
3434 cr->size = size;
3435 cr->count = size;
3436 cr->clear = clear;
3437 cr->incremental = 1;
3439 return cr;
3442 /* return values: *line_r
3443 * 1 success, whole line obtained from buffer non-0
3444 * 0 no more lines available right now 0
3445 * negative error code ERROR_* 0
3446 * On success *line_r is updated to point to a nul-terminated
3447 * string which is valid until the next call on the same console
3448 * reader. The libxl caller may overwrite parts of the string
3449 * if it wishes. */
3450 int libxl_xen_console_read_line(libxl_ctx *ctx,
3451 libxl_xen_console_reader *cr,
3452 char **line_r)
3454 int ret;
3456 memset(cr->buffer, 0, cr->size);
3457 ret = xc_readconsolering(ctx->xch, cr->buffer, &cr->count,
3458 cr->clear, cr->incremental, &cr->index);
3459 if (ret < 0) {
3460 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "reading console ring buffer");
3461 return ERROR_FAIL;
3463 if (!ret) {
3464 if (cr->count) {
3465 *line_r = cr->buffer;
3466 ret = 1;
3467 } else {
3468 *line_r = NULL;
3469 ret = 0;
3473 return ret;
3476 void libxl_xen_console_read_finish(libxl_ctx *ctx,
3477 libxl_xen_console_reader *cr)
3479 free(cr->buffer);
3480 free(cr);
3483 uint32_t libxl_vm_get_start_time(libxl_ctx *ctx, uint32_t domid)
3485 libxl__gc gc = LIBXL_INIT_GC(ctx);
3486 char *dompath = libxl__xs_get_dompath(&gc, domid);
3487 char *vm_path, *start_time;
3488 uint32_t ret;
3490 vm_path = libxl__xs_read(
3491 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dompath));
3492 start_time = libxl__xs_read(
3493 &gc, XBT_NULL, libxl__sprintf(&gc, "%s/start_time", vm_path));
3494 if (start_time == NULL) {
3495 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
3496 "Can't get start time of domain '%d'", domid);
3497 ret = -1;
3498 }else{
3499 ret = strtoul(start_time, NULL, 10);
3501 libxl__free_all(&gc);
3502 return ret;
3505 char *libxl_tmem_list(libxl_ctx *ctx, uint32_t domid, int use_long)
3507 int rc;
3508 char _buf[32768];
3510 rc = xc_tmem_control(ctx->xch, -1, TMEMC_LIST, domid, 32768, use_long,
3511 0, _buf);
3512 if (rc < 0) {
3513 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3514 "Can not get tmem list");
3515 return NULL;
3518 return strdup(_buf);
3521 int libxl_tmem_freeze(libxl_ctx *ctx, uint32_t domid)
3523 int rc;
3525 rc = xc_tmem_control(ctx->xch, -1, TMEMC_FREEZE, domid, 0, 0,
3526 0, NULL);
3527 if (rc < 0) {
3528 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3529 "Can not freeze tmem pools");
3530 return ERROR_FAIL;
3533 return rc;
3536 int libxl_tmem_destroy(libxl_ctx *ctx, uint32_t domid)
3538 int rc;
3540 rc = xc_tmem_control(ctx->xch, -1, TMEMC_DESTROY, domid, 0, 0,
3541 0, NULL);
3542 if (rc < 0) {
3543 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3544 "Can not destroy tmem pools");
3545 return ERROR_FAIL;
3548 return rc;
3551 int libxl_tmem_thaw(libxl_ctx *ctx, uint32_t domid)
3553 int rc;
3555 rc = xc_tmem_control(ctx->xch, -1, TMEMC_THAW, domid, 0, 0,
3556 0, NULL);
3557 if (rc < 0) {
3558 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3559 "Can not thaw tmem pools");
3560 return ERROR_FAIL;
3563 return rc;
3566 static int32_t tmem_setop_from_string(char *set_name)
3568 if (!strcmp(set_name, "weight"))
3569 return TMEMC_SET_WEIGHT;
3570 else if (!strcmp(set_name, "cap"))
3571 return TMEMC_SET_CAP;
3572 else if (!strcmp(set_name, "compress"))
3573 return TMEMC_SET_COMPRESS;
3574 else
3575 return -1;
3578 int libxl_tmem_set(libxl_ctx *ctx, uint32_t domid, char* name, uint32_t set)
3580 int rc;
3581 int32_t subop = tmem_setop_from_string(name);
3583 if (subop == -1) {
3584 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, -1,
3585 "Invalid set, valid sets are <weight|cap|compress>");
3586 return ERROR_INVAL;
3588 rc = xc_tmem_control(ctx->xch, -1, subop, domid, set, 0, 0, NULL);
3589 if (rc < 0) {
3590 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3591 "Can not set tmem %s", name);
3592 return ERROR_FAIL;
3595 return rc;
3598 int libxl_tmem_shared_auth(libxl_ctx *ctx, uint32_t domid,
3599 char* uuid, int auth)
3601 int rc;
3603 rc = xc_tmem_auth(ctx->xch, domid, uuid, auth);
3604 if (rc < 0) {
3605 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3606 "Can not set tmem shared auth");
3607 return ERROR_FAIL;
3610 return rc;
3613 int libxl_tmem_freeable(libxl_ctx *ctx)
3615 int rc;
3617 rc = xc_tmem_control(ctx->xch, -1, TMEMC_QUERY_FREEABLE_MB, -1, 0, 0, 0, 0);
3618 if (rc < 0) {
3619 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3620 "Can not get tmem freeable memory");
3621 return ERROR_FAIL;
3624 return rc;
3627 void libxl_file_reference_destroy(libxl_file_reference *f)
3629 libxl__file_reference_unmap(f);
3630 free(f->path);
3633 int libxl_get_freecpus(libxl_ctx *ctx, libxl_cpumap *cpumap)
3635 int ncpus;
3637 ncpus = libxl_get_max_cpus(ctx);
3638 if (ncpus == 0)
3639 return ERROR_FAIL;
3641 cpumap->map = xc_cpupool_freeinfo(ctx->xch);
3642 if (cpumap->map == NULL)
3643 return ERROR_FAIL;
3645 cpumap->size = (ncpus + 7) / 8;
3647 return 0;
3650 int libxl_create_cpupool(libxl_ctx *ctx, char *name, int schedid,
3651 libxl_cpumap cpumap, libxl_uuid *uuid,
3652 uint32_t *poolid)
3654 libxl__gc gc = LIBXL_INIT_GC(ctx);
3655 int rc;
3656 int i;
3657 xs_transaction_t t;
3658 char *uuid_string;
3660 uuid_string = libxl__uuid2string(&gc, *uuid);
3661 if (!uuid_string)
3662 return ERROR_NOMEM;
3664 rc = xc_cpupool_create(ctx->xch, poolid, schedid);
3665 if (rc) {
3666 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3667 "Could not create cpupool");
3668 return ERROR_FAIL;
3671 libxl_for_each_cpu(i, cpumap)
3672 if (libxl_cpumap_test(&cpumap, i)) {
3673 rc = xc_cpupool_addcpu(ctx->xch, *poolid, i);
3674 if (rc) {
3675 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3676 "Error moving cpu to cpupool");
3677 libxl_destroy_cpupool(ctx, *poolid);
3678 return ERROR_FAIL;
3682 for (;;) {
3683 t = xs_transaction_start(ctx->xsh);
3685 xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/pool/%d", *poolid));
3686 libxl__xs_write(&gc, t,
3687 libxl__sprintf(&gc, "/local/pool/%d/uuid", *poolid),
3688 "%s", uuid_string);
3689 libxl__xs_write(&gc, t,
3690 libxl__sprintf(&gc, "/local/pool/%d/name", *poolid),
3691 "%s", name);
3693 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3694 return 0;
3698 int libxl_destroy_cpupool(libxl_ctx *ctx, uint32_t poolid)
3700 libxl__gc gc = LIBXL_INIT_GC(ctx);
3701 int rc, i;
3702 xc_cpupoolinfo_t *info;
3703 xs_transaction_t t;
3704 libxl_cpumap cpumap;
3706 info = xc_cpupool_getinfo(ctx->xch, poolid);
3707 if (info == NULL)
3708 return ERROR_NOMEM;
3710 rc = ERROR_INVAL;
3711 if ((info->cpupool_id != poolid) || (info->n_dom))
3712 goto out;
3714 rc = ERROR_NOMEM;
3715 if (libxl_cpumap_alloc(ctx, &cpumap))
3716 goto out;
3718 memcpy(cpumap.map, info->cpumap, cpumap.size);
3719 libxl_for_each_cpu(i, cpumap)
3720 if (libxl_cpumap_test(&cpumap, i)) {
3721 rc = xc_cpupool_removecpu(ctx->xch, poolid, i);
3722 if (rc) {
3723 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3724 "Error removing cpu from cpupool");
3725 rc = ERROR_FAIL;
3726 goto out1;
3730 rc = xc_cpupool_destroy(ctx->xch, poolid);
3731 if (rc) {
3732 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "Could not destroy cpupool");
3733 rc = ERROR_FAIL;
3734 goto out1;
3737 for (;;) {
3738 t = xs_transaction_start(ctx->xsh);
3740 xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "/local/pool/%d", poolid));
3742 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3743 break;
3746 rc = 0;
3748 out1:
3749 libxl_cpumap_destroy(&cpumap);
3750 out:
3751 xc_cpupool_infofree(ctx->xch, info);
3753 return rc;
3756 int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu)
3758 int rc;
3760 rc = xc_cpupool_addcpu(ctx->xch, poolid, cpu);
3761 if (rc) {
3762 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3763 "Error moving cpu to cpupool");
3764 return ERROR_FAIL;
3766 return 0;
3769 int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu)
3771 int rc;
3773 rc = xc_cpupool_removecpu(ctx->xch, poolid, cpu);
3774 if (rc) {
3775 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3776 "Error removing cpu from cpupool");
3777 return ERROR_FAIL;
3779 return 0;
3782 int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
3784 libxl__gc gc = LIBXL_INIT_GC(ctx);
3785 int rc;
3786 char *dom_path;
3787 char *vm_path;
3788 char *poolname;
3789 xs_transaction_t t;
3791 dom_path = libxl__xs_get_dompath(&gc, domid);
3792 if (!dom_path) {
3793 return ERROR_FAIL;
3796 rc = xc_cpupool_movedomain(ctx->xch, poolid, domid);
3797 if (rc) {
3798 LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc,
3799 "Error moving domain to cpupool");
3800 return ERROR_FAIL;
3803 for (;;) {
3804 t = xs_transaction_start(ctx->xsh);
3806 poolname = libxl__cpupoolid_to_name(&gc, poolid);
3807 vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", dom_path));
3808 if (!vm_path)
3809 break;
3811 libxl__xs_write(&gc, t, libxl__sprintf(&gc, "%s/pool_name", vm_path),
3812 "%s", poolname);
3814 if (xs_transaction_end(ctx->xsh, t, 0) || (errno != EAGAIN))
3815 break;
3818 return 0;