debuggers.hg

view tools/libxl/libxl.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents 503b47f53b53
children fe30bd463e84
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 <fcntl.h>
24 #include <sys/select.h>
25 #include <sys/wait.h>
26 #include <signal.h>
27 #include <unistd.h> /* for write, unlink and close */
28 #include <stdint.h>
29 #include <inttypes.h>
30 #include <assert.h>
32 #include "libxl.h"
33 #include "libxl_utils.h"
34 #include "libxl_internal.h"
35 #include "flexarray.h"
37 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
39 int libxl_ctx_init(struct libxl_ctx *ctx, int version)
40 {
41 if (version != LIBXL_VERSION)
42 return ERROR_VERSION;
43 memset(ctx, 0, sizeof(struct libxl_ctx));
44 ctx->alloc_maxsize = 256;
45 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
46 if (!ctx->alloc_ptrs)
47 return ERROR_NOMEM;
49 ctx->xch = xc_interface_open();
50 if (ctx->xch == -1) {
51 free(ctx->alloc_ptrs);
52 return ERROR_FAIL;
53 }
55 ctx->xsh = xs_daemon_open();
56 if (!ctx->xsh) {
57 xc_interface_close(ctx->xch);
58 free(ctx->alloc_ptrs);
59 return ERROR_FAIL;
60 }
61 return 0;
62 }
64 int libxl_ctx_free(struct libxl_ctx *ctx)
65 {
66 libxl_free_all(ctx);
67 free(ctx->alloc_ptrs);
68 xc_interface_close(ctx->xch);
69 xs_daemon_close(ctx->xsh);
70 return 0;
71 }
73 int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, void *log_data)
74 {
75 ctx->log_callback = log_callback;
76 ctx->log_userdata = log_data;
77 return 0;
78 }
80 /******************************************************************************/
82 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
83 uint32_t *domid)
84 {
85 int flags, ret, i;
86 char *uuid_string;
87 char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
88 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
89 "control", "attr", "messages" };
90 char *dom_path, *vm_path, *vss_path;
91 struct xs_permissions roperm[2];
92 struct xs_permissions rwperm[1];
93 xs_transaction_t t;
94 xen_domain_handle_t handle;
96 uuid_string = string_of_uuid(ctx, info->uuid);
97 if (!uuid_string) {
98 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate uuid string");
99 return ERROR_FAIL;
100 }
102 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
103 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
104 *domid = -1;
106 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
107 memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
109 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
110 if (ret < 0) {
111 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
112 return ERROR_FAIL;
113 }
115 dom_path = libxl_xs_get_dompath(ctx, *domid);
116 if (!dom_path)
117 return ERROR_FAIL;
119 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
120 vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
121 if (!vm_path || !vss_path) {
122 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
123 return ERROR_FAIL;
124 }
126 roperm[0].id = 0;
127 roperm[0].perms = XS_PERM_NONE;
128 roperm[1].id = *domid;
129 roperm[1].perms = XS_PERM_READ;
130 rwperm[0].id = *domid;
131 rwperm[0].perms = XS_PERM_NONE;
133 retry_transaction:
134 t = xs_transaction_start(ctx->xsh);
135 xs_rm(ctx->xsh, t, dom_path);
136 xs_mkdir(ctx->xsh, t, dom_path);
137 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
139 xs_rm(ctx->xsh, t, vm_path);
140 xs_mkdir(ctx->xsh, t, vm_path);
141 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
143 xs_rm(ctx->xsh, t, vss_path);
144 xs_mkdir(ctx->xsh, t, vss_path);
145 xs_set_permissions(ctx->xsh, t, vss_path, rwperm, ARRAY_SIZE(rwperm));
147 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
148 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
149 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", dom_path), info->name, strlen(info->name));
151 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
152 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
153 xs_mkdir(ctx->xsh, t, path);
154 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
155 libxl_free(ctx, path);
156 }
157 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
158 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
159 xs_mkdir(ctx->xsh, t, path);
160 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
161 libxl_free(ctx, path);
162 }
164 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
165 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
167 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
168 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
170 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
172 if (!xs_transaction_end(ctx->xsh, t, 0))
173 if (errno == EAGAIN)
174 goto retry_transaction;
175 return 0;
176 }
178 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
179 {
180 char **vments = NULL, **localents = NULL;
181 int i, ret;
183 ret = build_pre(ctx, domid, info, state);
184 if (ret) goto out;
186 if (info->hvm) {
187 ret = build_hvm(ctx, domid, info, state);
188 if (ret) goto out;
190 vments = libxl_calloc(ctx, 5, sizeof(char *));
191 vments[0] = "rtc/timeoffset";
192 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
193 vments[2] = "image/ostype";
194 vments[3] = "hvm";
195 } else {
196 ret = build_pv(ctx, domid, info, state);
197 if (ret) goto out;
199 vments = libxl_calloc(ctx, 9, sizeof(char *));
200 i = 0;
201 vments[i++] = "image/ostype";
202 vments[i++] = "linux";
203 vments[i++] = "image/kernel";
204 vments[i++] = (char*) info->kernel;
205 if (info->u.pv.ramdisk) {
206 vments[i++] = "image/ramdisk";
207 vments[i++] = (char*) info->u.pv.ramdisk;
208 }
209 if (info->u.pv.cmdline) {
210 vments[i++] = "image/cmdline";
211 vments[i++] = (char*) info->u.pv.cmdline;
212 }
213 }
214 ret = build_post(ctx, domid, info, state, vments, localents);
215 out:
216 return ret;
217 }
219 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
220 uint32_t domid, int fd, libxl_domain_build_state *state,
221 libxl_device_model_info *dm_info)
222 {
223 char **vments = NULL, **localents = NULL;
224 int i, ret;
226 ret = build_pre(ctx, domid, info, state);
227 if (ret) goto out;
229 ret = restore_common(ctx, domid, info, state, fd);
230 if (ret) goto out;
232 if (info->hvm) {
233 vments = libxl_calloc(ctx, 5, sizeof(char *));
234 vments[0] = "rtc/timeoffset";
235 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
236 vments[2] = "image/ostype";
237 vments[3] = "hvm";
238 } else {
239 vments = libxl_calloc(ctx, 9, sizeof(char *));
240 i = 0;
241 vments[i++] = "image/ostype";
242 vments[i++] = "linux";
243 vments[i++] = "image/kernel";
244 vments[i++] = (char*) info->kernel;
245 if (info->u.pv.ramdisk) {
246 vments[i++] = "image/ramdisk";
247 vments[i++] = (char*) info->u.pv.ramdisk;
248 }
249 if (info->u.pv.cmdline) {
250 vments[i++] = "image/cmdline";
251 vments[i++] = (char*) info->u.pv.cmdline;
252 }
253 }
254 ret = build_post(ctx, domid, info, state, vments, localents);
255 if (ret) goto out;
257 if (info->hvm)
258 asprintf(&(dm_info->saved_state), "/var/lib/xen/qemu-save.%d", domid);
259 else
260 dm_info->saved_state = NULL;
261 out:
262 return ret;
263 }
265 int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
266 {
267 if (is_hvm(ctx, domid)) {
268 XL_LOG(ctx, XL_LOG_DEBUG, "Called domain_resume on "
269 "non-cooperative hvm domain %u", domid);
270 return ERROR_NI;
271 }
272 if (xc_domain_resume(ctx->xch, domid, 1)) {
273 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
274 "xc_domain_resume failed for domain %u",
275 domid);
276 return ERROR_FAIL;
277 }
278 if (!xs_resume_domain(ctx->xsh, domid)) {
279 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
280 "xs_resume_domain failed for domain %u",
281 domid);
282 return ERROR_FAIL;
283 }
284 return 0;
285 }
287 struct libxl_dominfo * libxl_list_domain(struct libxl_ctx *ctx, int *nb_domain)
288 {
289 struct libxl_dominfo *ptr;
290 int i, ret;
291 xc_domaininfo_t info[1024];
292 int size = 1024;
294 ptr = calloc(size, sizeof(struct libxl_dominfo));
295 if (!ptr)
296 return NULL;
298 ret = xc_domain_getinfolist(ctx->xch, 0, 1024, info);
299 for (i = 0; i < ret; i++) {
300 memcpy(&(ptr[i].uuid), info[i].handle, sizeof(xen_domain_handle_t));
301 ptr[i].domid = info[i].domain;
303 if (info[i].flags & XEN_DOMINF_dying)
304 ptr[i].dying = 1;
305 else if (info[i].flags & XEN_DOMINF_paused)
306 ptr[i].paused = 1;
307 else if (info[i].flags & XEN_DOMINF_blocked || info[i].flags & XEN_DOMINF_running)
308 ptr[i].running = 1;
309 ptr[i].max_memkb = PAGE_TO_MEMKB(info[i].tot_pages);
310 ptr[i].cpu_time = info[i].cpu_time;
311 ptr[i].vcpu_max_id = info[i].max_vcpu_id;
312 ptr[i].vcpu_online = info[i].nr_online_vcpus;
313 }
314 *nb_domain = ret;
315 return ptr;
316 }
318 /* this API call only list VM running on this host. a VM can be an aggregate of multiple domains. */
319 struct libxl_vminfo * libxl_list_vm(struct libxl_ctx *ctx, int *nb_vm)
320 {
321 struct libxl_vminfo *ptr;
322 int index, i, ret;
323 xc_domaininfo_t info[1024];
324 int size = 1024;
326 ptr = calloc(size, sizeof(struct libxl_dominfo));
327 if (!ptr)
328 return NULL;
330 ret = xc_domain_getinfolist(ctx->xch, 1, 1024, info);
331 for (index = i = 0; i < ret; i++) {
332 if (libxl_is_stubdom(ctx, info[i].domain, NULL))
333 continue;
334 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
335 ptr[index].domid = info[i].domain;
337 index++;
338 }
339 *nb_vm = index;
340 return ptr;
341 }
343 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
344 uint32_t domid, int fd)
345 {
346 int hvm = is_hvm(ctx, domid);
347 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
348 int debug = info != NULL && info->flags & XL_SUSPEND_LIVE;
350 core_suspend(ctx, domid, fd, hvm, live, debug);
351 if (hvm)
352 save_device_model(ctx, domid, fd);
353 return 0;
354 }
356 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
357 {
358 xc_domain_pause(ctx->xch, domid);
359 return 0;
360 }
362 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
363 {
364 char *path;
365 char *state;
367 if (is_hvm(ctx, domid)) {
368 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
369 state = libxl_xs_read(ctx, XBT_NULL, path);
370 if (state != NULL && !strcmp(state, "paused")) {
371 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue", strlen("continue"));
372 libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL);
373 }
374 }
375 xc_domain_unpause(ctx->xch, domid);
377 return 0;
378 }
380 static char *req_table[] = {
381 [0] = "poweroff",
382 [1] = "reboot",
383 [2] = "suspend",
384 [3] = "crash",
385 [4] = "halt",
386 };
388 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
389 {
390 char *shutdown_path;
391 char *dom_path;
393 if (req > ARRAY_SIZE(req_table))
394 return ERROR_INVAL;
396 dom_path = libxl_xs_get_dompath(ctx, domid);
397 if (!dom_path)
398 return ERROR_FAIL;
400 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
402 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
403 if (/* hvm */ 0) {
404 unsigned long acpi_s_state = 0;
405 unsigned long pvdriver = 0;
406 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
407 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
408 if (!pvdriver && acpi_s_state != 0)
409 xc_domain_shutdown(ctx->xch, domid, req);
410 }
411 return 0;
412 }
414 int libxl_get_wait_fd(struct libxl_ctx *ctx, int *fd)
415 {
416 *fd = xs_fileno(ctx->xsh);
417 return 0;
418 }
420 int libxl_wait_for_domain_death(struct libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
421 {
422 waiter->path = strdup("@releaseDomain");
423 asprintf(&(waiter->token), "%d", DOMAIN_DEATH);
424 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
425 return -1;
426 return 0;
427 }
429 int libxl_wait_for_disk_ejects(struct libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
430 {
431 int i;
432 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
434 if (!domid)
435 domid = guest_domid;
437 for (i = 0; i < num_disks; i++) {
438 asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject", libxl_xs_get_dompath(ctx, domid), device_disk_dev_number(disks[i].virtpath));
439 asprintf(&(waiter[i].token), "%d", DISK_EJECT);
440 xs_watch(ctx->xsh, waiter->path, waiter->token);
441 }
442 return 0;
443 }
445 int libxl_get_event(struct libxl_ctx *ctx, libxl_event *event)
446 {
447 unsigned int num;
448 char **events = xs_read_watch(ctx->xsh, &num);
449 if (num != 2) {
450 free(events);
451 return -1;
452 }
453 event->path = strdup(events[XS_WATCH_PATH]);
454 event->token = strdup(events[XS_WATCH_TOKEN]);
455 event->type = atoi(event->token);
456 free(events);
457 return 0;
458 }
460 int libxl_stop_waiting(struct libxl_ctx *ctx, libxl_waiter *waiter)
461 {
462 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
463 return -1;
464 else
465 return 0;
466 }
468 int libxl_free_event(libxl_event *event)
469 {
470 free(event->path);
471 free(event->token);
472 return 0;
473 }
475 int libxl_free_waiter(libxl_waiter *waiter)
476 {
477 free(waiter->path);
478 free(waiter->token);
479 return 0;
480 }
482 int libxl_event_get_domain_death_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, xc_domaininfo_t *info)
483 {
484 int rc = 0, ret;
486 if (event && event->type == DOMAIN_DEATH) {
487 ret = xc_domain_getinfolist(ctx->xch, domid, 1, info);
488 if (ret == 1 && info->domain == domid) {
489 if (info->flags & XEN_DOMINF_running ||
490 (!(info->flags & XEN_DOMINF_shutdown) && !(info->flags & XEN_DOMINF_dying)))
491 goto out;
492 rc = 1;
493 goto out;
494 }
495 memset(info, 0, sizeof(xc_dominfo_t));
496 rc = 1;
497 goto out;
498 }
499 out:
500 return rc;
501 }
503 int libxl_event_get_disk_eject_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
504 {
505 if (event && event->type == DISK_EJECT) {
506 char *path;
507 char *backend;
508 char *value = libxl_xs_read(ctx, XBT_NULL, event->path);
510 if (!value || strcmp(value, "eject"))
511 return 0;
513 path = strdup(event->path);
514 path[strlen(path) - 6] = '\0';
515 backend = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", path));
517 disk->backend_domid = 0;
518 disk->domid = domid;
519 disk->physpath = NULL;
520 disk->phystype = 0;
521 /* this value is returned to the user: do not free right away */
522 disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", backend));
523 disk->unpluggable = 1;
524 disk->readwrite = 0;
525 disk->is_cdrom = 1;
527 free(path);
528 return 1;
529 }
530 return 0;
531 }
533 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
534 {
535 char *pid;
536 int ret;
538 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
539 if (!pid) {
540 int stubdomid = libxl_get_stubdom_id(ctx, domid);
541 if (!stubdomid) {
542 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
543 return -1;
544 }
545 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
546 return libxl_domain_destroy(ctx, stubdomid, 0);
547 }
548 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
550 ret = kill(atoi(pid), SIGHUP);
551 if (ret < 0 && errno == ESRCH) {
552 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
553 ret = 0;
554 } else if (ret == 0) {
555 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
556 ret = 0;
557 } else {
558 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
559 atoi(pid));
560 }
561 return ret;
562 }
564 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
565 {
566 char *dom_path;
567 char *vm_path, *vss_path, *xapi_path;
568 int rc, dm_present;
570 if (is_hvm(ctx, domid)) {
571 dm_present = 1;
572 } else {
573 char *pid;
574 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
575 dm_present = (pid != NULL);
576 libxl_free(ctx, pid);
577 }
579 dom_path = libxl_xs_get_dompath(ctx, domid);
580 if (!dom_path)
581 return -1;
583 if (libxl_device_pci_shutdown(ctx, domid) < 0)
584 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
585 if (dm_present) {
586 xs_write(ctx->xsh, XBT_NULL,
587 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
588 "shutdown", strlen("shutdown"));
589 }
590 rc = xc_domain_pause(ctx->xch, domid);
591 if (rc < 0) {
592 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
593 return -1;
594 }
595 if (dm_present) {
596 if (libxl_destroy_device_model(ctx, domid) < 0)
597 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
598 }
599 if (libxl_devices_destroy(ctx, domid, force) < 0)
600 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
601 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
602 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
604 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", domid));
605 if (vm_path)
606 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
607 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
609 vss_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vss", domid));
610 if (vss_path)
611 if (!xs_rm(ctx->xsh, XBT_NULL, vss_path))
612 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vss_path);
614 xapi_path = libxl_sprintf(ctx, "/xapi/%u", domid);
615 if (xapi_path)
616 if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
617 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
619 rc = xc_domain_destroy(ctx->xch, domid);
620 if (rc < 0) {
621 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
622 return -1;
623 }
624 return 0;
625 }
627 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num)
628 {
629 struct stat st;
630 const char *XENCONSOLE = "/usr/lib/xen/bin/xenconsole";
631 char *cmd;
633 if (stat(XENCONSOLE, &st) != 0) {
634 XL_LOG(ctx, XL_LOG_ERROR, "could not access %s", XENCONSOLE);
635 return ERROR_FAIL;
636 }
638 cmd = libxl_sprintf(ctx, "%s %d --num %d", XENCONSOLE, domid, cons_num);
639 return (system(cmd) != 0) ? ERROR_FAIL : 0;
640 }
642 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
643 libxl_device_model_info *info,
644 libxl_device_nic *vifs,
645 int num_vifs)
646 {
647 int num = 0, i;
648 flexarray_t *dm_args;
649 dm_args = flexarray_make(16, 1);
650 if (!dm_args)
651 return NULL;
653 flexarray_set(dm_args, num++, "qemu-dm");
654 flexarray_set(dm_args, num++, "-d");
656 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
658 if (info->dom_name) {
659 flexarray_set(dm_args, num++, "-domain-name");
660 flexarray_set(dm_args, num++, info->dom_name);
661 }
662 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
663 flexarray_set(dm_args, num++, "-vnc");
664 if (info->vncdisplay) {
665 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
666 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:%d", info->vnclisten, info->vncdisplay));
667 } else {
668 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
669 }
670 } else if (info->vnclisten) {
671 if (strchr(info->vnclisten, ':') != NULL) {
672 flexarray_set(dm_args, num++, info->vnclisten);
673 } else {
674 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
675 }
676 } else {
677 flexarray_set(dm_args, num++, "127.0.0.1:0");
678 }
679 if (info->vncunused) {
680 flexarray_set(dm_args, num++, "-vncunused");
681 }
682 }
683 if (info->sdl || info->opengl) {
684 flexarray_set(dm_args, num++, "-sdl");
685 if (info->opengl) {
686 flexarray_set(dm_args, num++, "-disable-opengl");
687 }
688 }
689 if (info->keymap) {
690 flexarray_set(dm_args, num++, "-k");
691 flexarray_set(dm_args, num++, info->keymap);
692 }
693 if (info->nographic && (!info->sdl && !info->vnc)) {
694 flexarray_set(dm_args, num++, "-nographic");
695 }
696 if (info->serial) {
697 flexarray_set(dm_args, num++, "-serial");
698 flexarray_set(dm_args, num++, info->serial);
699 }
700 if (info->type == XENFV) {
701 if (info->videoram) {
702 flexarray_set(dm_args, num++, "-videoram");
703 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
704 }
705 if (info->stdvga) {
706 flexarray_set(dm_args, num++, "-std-vga");
707 }
709 if (info->boot) {
710 flexarray_set(dm_args, num++, "-boot");
711 flexarray_set(dm_args, num++, info->boot);
712 }
713 if (info->usb) {
714 flexarray_set(dm_args, num++, "-usb");
715 if (info->usbdevice) {
716 flexarray_set(dm_args, num++, "-usbdevice");
717 flexarray_set(dm_args, num++, info->usbdevice);
718 }
719 }
720 if (info->apic) {
721 flexarray_set(dm_args, num++, "-acpi");
722 }
723 for (i = 0; i < num_vifs; i++) {
724 if (vifs[i].nictype == NICTYPE_IOEMU) {
725 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
726 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
727 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
728 if (!vifs[i].ifname)
729 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid - 1);
730 flexarray_set(dm_args, num++, "-net");
731 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
732 vifs[i].devid, smac, vifs[i].model));
733 flexarray_set(dm_args, num++, "-net");
734 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
735 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
736 }
737 }
738 }
739 if (info->saved_state) {
740 flexarray_set(dm_args, num++, "-loadvm");
741 flexarray_set(dm_args, num++, info->saved_state);
742 }
743 for (i = 0; info->extra && info->extra[i] != NULL; i++)
744 flexarray_set(dm_args, num++, info->extra[i]);
745 flexarray_set(dm_args, num++, "-M");
746 if (info->type == XENPV)
747 flexarray_set(dm_args, num++, "xenpv");
748 else
749 flexarray_set(dm_args, num++, "xenfv");
750 flexarray_set(dm_args, num++, NULL);
752 return (char **) flexarray_contents(dm_args);
753 }
755 void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild)
756 {
757 struct libxl_device_model_starting *starting = for_spawn;
758 char *kvs[3];
759 int rc;
760 struct xs_handle *xsh;
762 xsh = xs_daemon_open();
763 /* we mustn't use the parent's handle in the child */
765 kvs[0] = "image/device-model-pid";
766 asprintf(&kvs[1], "%d", innerchild);
767 kvs[2] = NULL;
769 rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
770 if (rc)
771 return;
772 xs_daemon_close(xsh);
773 }
775 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
776 libxl_device_model_info *info,
777 libxl_device_vfb *vfb,
778 libxl_device_vkb *vkb)
779 {
780 memset(vfb, 0x00, sizeof(libxl_device_vfb));
781 memset(vkb, 0x00, sizeof(libxl_device_vkb));
783 vfb->backend_domid = 0;
784 vfb->devid = 0;
785 vfb->vnc = info->vnc;
786 vfb->vnclisten = info->vnclisten;
787 vfb->vncdisplay = info->vncdisplay;
788 vfb->vncunused = info->vncunused;
789 vfb->keymap = info->keymap;
790 vfb->sdl = info->sdl;
791 vfb->opengl = info->opengl;
793 vkb->backend_domid = 0;
794 vkb->devid = 0;
795 return 0;
796 }
798 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
799 {
800 int i;
801 char *vm_path;
802 char *dmargs, *path;
803 int dmargs_size;
804 struct xs_permissions roperm[2];
805 xs_transaction_t t;
807 roperm[0].id = 0;
808 roperm[0].perms = XS_PERM_NONE;
809 roperm[1].id = domid;
810 roperm[1].perms = XS_PERM_READ;
812 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
814 i = 0;
815 dmargs_size = 0;
816 while (args[i] != NULL) {
817 dmargs_size = dmargs_size + strlen(args[i]) + 1;
818 i++;
819 }
820 dmargs_size++;
821 dmargs = (char *) malloc(dmargs_size);
822 i = 1;
823 dmargs[0] = '\0';
824 while (args[i] != NULL) {
825 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
826 strcat(dmargs, " ");
827 strcat(dmargs, args[i]);
828 }
829 i++;
830 }
831 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
833 retry_transaction:
834 t = xs_transaction_start(ctx->xsh);
835 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
836 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
837 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
838 if (!xs_transaction_end(ctx->xsh, t, 0))
839 if (errno == EAGAIN)
840 goto retry_transaction;
841 free(dmargs);
842 return 0;
843 }
845 static int libxl_create_stubdom(struct libxl_ctx *ctx,
846 libxl_device_model_info *info,
847 libxl_device_disk *disks, int num_disks,
848 libxl_device_nic *vifs, int num_vifs,
849 libxl_device_vfb *vfb,
850 libxl_device_vkb *vkb,
851 libxl_device_model_starting **starting_r)
852 {
853 int i, num_console = 1, ret;
854 libxl_device_console *console;
855 libxl_domain_create_info c_info;
856 libxl_domain_build_info b_info;
857 libxl_domain_build_state state;
858 uint32_t domid;
859 char **args;
860 struct xs_permissions perm[2];
861 xs_transaction_t t;
862 libxl_device_model_starting *dm_starting = 0;
864 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
865 if (!args)
866 return ERROR_FAIL;
868 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
869 c_info.hvm = 0;
870 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
871 for (i = 0; i < 16; i++)
872 c_info.uuid[i] = info->uuid[i];
874 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
875 b_info.max_vcpus = 1;
876 b_info.max_memkb = 32 * 1024;
877 b_info.target_memkb = b_info.max_memkb;
878 b_info.kernel = "/usr/lib/xen/boot/ioemu-stubdom.gz";
879 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
880 b_info.u.pv.ramdisk = "";
881 b_info.u.pv.features = "";
882 b_info.hvm = 0;
884 ret = libxl_domain_make(ctx, &c_info, &domid);
885 if (ret) return ret;
886 ret = libxl_domain_build(ctx, &b_info, domid, &state);
887 if (ret) return ret;
889 libxl_write_dmargs(ctx, domid, info->domid, args);
890 libxl_xs_write(ctx, XBT_NULL,
891 libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)),
892 "%d", domid);
893 libxl_xs_write(ctx, XBT_NULL,
894 libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)),
895 "%d", info->domid);
896 xc_domain_set_target(ctx->xch, domid, info->domid);
897 xs_set_target(ctx->xsh, domid, info->domid);
899 perm[0].id = domid;
900 perm[0].perms = XS_PERM_NONE;
901 perm[1].id = info->domid;
902 perm[1].perms = XS_PERM_READ;
903 retry_transaction:
904 t = xs_transaction_start(ctx->xsh);
905 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
906 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
907 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
908 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
909 if (!xs_transaction_end(ctx->xsh, t, 0))
910 if (errno == EAGAIN)
911 goto retry_transaction;
913 for (i = 0; i < num_disks; i++) {
914 disks[i].domid = domid;
915 ret = libxl_device_disk_add(ctx, domid, &disks[i]);
916 if (ret) return ret;
917 }
918 for (i = 0; i < num_vifs; i++) {
919 vifs[i].domid = domid;
920 ret = libxl_device_nic_add(ctx, domid, &vifs[i]);
921 if (ret) return ret;
922 }
923 vfb->domid = domid;
924 ret = libxl_device_vfb_add(ctx, domid, vfb);
925 if (ret) return ret;
926 vkb->domid = domid;
927 ret = libxl_device_vkb_add(ctx, domid, vkb);
928 if (ret) return ret;
930 if (info->serial)
931 num_console++;
933 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
934 if (!console)
935 return ERROR_NOMEM;
937 for (i = 0; i < num_console; i++) {
938 console[i].devid = i;
939 console[i].constype = CONSTYPE_IOEMU;
940 console[i].domid = domid;
941 if (!i)
942 console[i].build_state = &state;
943 ret = libxl_device_console_add(ctx, domid, &console[i]);
944 if (ret) return ret;
945 }
946 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
947 free(args);
948 return -1;
949 }
950 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
951 free(args);
952 return -1;
953 }
955 libxl_domain_unpause(ctx, domid);
957 if (starting_r) {
958 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
959 (*starting_r)->domid = info->domid;
960 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
961 (*starting_r)->for_spawn = NULL;
962 }
964 free(args);
965 return 0;
966 }
968 int libxl_create_device_model(struct libxl_ctx *ctx,
969 libxl_device_model_info *info,
970 libxl_device_disk *disks, int num_disks,
971 libxl_device_nic *vifs, int num_vifs,
972 libxl_device_model_starting **starting_r)
973 {
974 char *path, *logfile;
975 int logfile_w, null;
976 int rc;
977 char **args;
978 struct libxl_device_model_starting buf_starting, *p;
980 if (strstr(info->device_model, "stubdom-dm")) {
981 libxl_device_vfb vfb;
982 libxl_device_vkb vkb;
984 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
985 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
986 }
988 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
989 if (!args)
990 return ERROR_FAIL;
992 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
993 xs_mkdir(ctx->xsh, XBT_NULL, path);
995 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
996 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
997 free(logfile);
998 null = open("/dev/null", O_RDONLY);
1000 if (starting_r) {
1001 rc = ERROR_NOMEM;
1002 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
1003 if (!*starting_r) goto xit;
1004 p = *starting_r;
1005 p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
1006 } else {
1007 p = &buf_starting;
1008 p->for_spawn = NULL;
1011 p->domid = info->domid;
1012 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
1013 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
1015 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
1016 if (rc < 0) goto xit;
1017 if (!rc) { /* inner child */
1018 libxl_exec(null, logfile_w, logfile_w,
1019 info->device_model, args);
1022 rc = 0;
1023 xit:
1024 free(args);
1025 close(null);
1026 close(logfile_w);
1028 return rc;
1031 int libxl_detach_device_model(struct libxl_ctx *ctx,
1032 libxl_device_model_starting *starting)
1034 int rc;
1035 rc = libxl_spawn_detach(ctx, starting->for_spawn);
1036 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
1037 libxl_free(ctx, starting);
1038 return rc;
1042 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
1043 libxl_device_model_starting *starting)
1045 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
1046 libxl_spawn_check,
1047 starting->for_spawn);
1048 int detach = libxl_detach_device_model(ctx, starting);
1049 return problem ? problem : detach;
1050 return 0;
1054 /******************************************************************************/
1056 static int is_blktap2_supported(void)
1058 char buf[1024];
1059 FILE *f = fopen("/proc/devices", "r");
1062 while (fgets(buf, sizeof(buf), f) != NULL) {
1063 if (strstr(buf, "blktap2")) {
1064 fclose(f);
1065 return 1;
1068 fclose(f);
1069 return 0;
1072 static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type)
1074 char buf[1024];
1075 char *p;
1076 int devnum;
1077 FILE *f = fopen("/sys/class/blktap2/devices", "r");
1080 while (!feof(f)) {
1081 fscanf(f, "%d %s", &devnum, buf);
1082 p = strchr(buf, ':');
1083 if (p == NULL)
1084 continue;
1085 p++;
1086 if (!strcmp(p, name) && !strncmp(buf, type, 3)) {
1087 fclose(f);
1088 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", devnum);
1091 fclose(f);
1092 return NULL;
1095 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1097 flexarray_t *front;
1098 flexarray_t *back;
1099 char *backend_type;
1100 unsigned int boffset = 0;
1101 unsigned int foffset = 0;
1102 int devid;
1103 libxl_device device;
1104 int major, minor;
1106 front = flexarray_make(16, 1);
1107 if (!front)
1108 return ERROR_NOMEM;
1109 back = flexarray_make(16, 1);
1110 if (!back) /* leaks front if error */
1111 return ERROR_NOMEM;
1113 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
1114 devid = device_disk_dev_number(disk->virtpath);
1115 if (devid==-1) {
1116 XL_LOG(ctx, XL_LOG_ERROR, "Invalid or unuspported"
1117 " virtual disk identifier %s", disk->virtpath);
1118 return ERROR_INVAL;
1121 device.backend_devid = devid;
1122 device.backend_domid = disk->backend_domid;
1123 device.devid = devid;
1124 device.domid = disk->domid;
1125 device.kind = DEVICE_VBD;
1127 switch (disk->phystype) {
1128 case PHYSTYPE_PHY: {
1130 device_physdisk_major_minor(disk->physpath, &major, &minor);
1131 flexarray_set(back, boffset++, "physical-device");
1132 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1134 flexarray_set(back, boffset++, "params");
1135 flexarray_set(back, boffset++, disk->physpath);
1137 device.backend_kind = DEVICE_VBD;
1138 break;
1140 case PHYSTYPE_FILE:
1141 /* let's pretend is tap:aio for the moment */
1142 disk->phystype = PHYSTYPE_AIO;
1143 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
1144 if (is_blktap2_supported()) {
1145 int rc, c, p[2], tot;
1146 char buf[1024], *dev;
1147 dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
1148 if (dev == NULL) {
1149 if (pipe(p) < 0) {
1150 XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
1151 return -1;
1153 rc = fork();
1154 if (rc < 0) {
1155 XL_LOG(ctx, XL_LOG_ERROR, "Failed to fork a new process");
1156 return -1;
1157 } else if (!rc) { /* child */
1158 int null_r, null_w;
1159 char *args[4];
1160 args[0] = "tapdisk2";
1161 args[1] = "-n";
1162 args[2] = libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath);
1163 args[3] = NULL;
1165 null_r = open("/dev/null", O_RDONLY);
1166 null_w = open("/dev/null", O_WRONLY);
1167 libxl_exec(null_r, p[1], null_w, "/usr/sbin/tapdisk2", args);
1168 XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
1170 close(p[1]);
1171 tot = 0;
1172 while ((c = read(p[0], buf + tot, sizeof(buf) - tot)) > 0)
1173 tot = tot + c;
1174 close(p[0]);
1175 buf[tot - 1] = '\0';
1176 dev = buf;
1178 flexarray_set(back, boffset++, "tapdisk-params");
1179 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
1180 flexarray_set(back, boffset++, "params");
1181 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", dev));
1182 backend_type = "phy";
1183 device_physdisk_major_minor(dev, &major, &minor);
1184 flexarray_set(back, boffset++, "physical-device");
1185 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1186 device.backend_kind = DEVICE_VBD;
1188 break;
1190 flexarray_set(back, boffset++, "params");
1191 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
1192 device_disk_string_of_phystype(disk->phystype), disk->physpath));
1194 device.backend_kind = DEVICE_TAP;
1195 break;
1196 default:
1197 XL_LOG(ctx, XL_LOG_ERROR, "unrecognized disk physical type: %d\n", disk->phystype);
1198 return ERROR_INVAL;
1201 flexarray_set(back, boffset++, "frontend-id");
1202 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
1203 flexarray_set(back, boffset++, "online");
1204 flexarray_set(back, boffset++, "1");
1205 flexarray_set(back, boffset++, "removable");
1206 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
1207 flexarray_set(back, boffset++, "bootable");
1208 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1209 flexarray_set(back, boffset++, "state");
1210 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1211 flexarray_set(back, boffset++, "dev");
1212 flexarray_set(back, boffset++, disk->virtpath);
1213 flexarray_set(back, boffset++, "type");
1214 flexarray_set(back, boffset++, backend_type);
1215 flexarray_set(back, boffset++, "mode");
1216 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1218 flexarray_set(front, foffset++, "backend-id");
1219 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
1220 flexarray_set(front, foffset++, "state");
1221 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1222 flexarray_set(front, foffset++, "virtual-device");
1223 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
1224 flexarray_set(front, foffset++, "device-type");
1225 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1227 if (0 /* protocol != native*/) {
1228 flexarray_set(front, foffset++, "protocol");
1229 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1232 libxl_device_generic_add(ctx, &device,
1233 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1234 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1235 flexarray_free(back);
1236 flexarray_free(front);
1237 return 0;
1240 int libxl_device_disk_del(struct libxl_ctx *ctx,
1241 libxl_device_disk *disk, int wait)
1243 libxl_device device;
1244 int devid;
1246 devid = device_disk_dev_number(disk->virtpath);
1247 device.backend_domid = disk->backend_domid;
1248 device.backend_devid = devid;
1249 device.backend_kind =
1250 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1251 device.domid = disk->domid;
1252 device.devid = devid;
1253 device.kind = DEVICE_VBD;
1254 return libxl_device_del(ctx, &device, wait);
1257 /******************************************************************************/
1258 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1260 flexarray_t *front;
1261 flexarray_t *back;
1262 unsigned int boffset = 0;
1263 unsigned int foffset = 0;
1264 libxl_device device;
1266 front = flexarray_make(16, 1);
1267 if (!front)
1268 return ERROR_NOMEM;
1269 back = flexarray_make(16, 1);
1270 if (!back)
1271 return ERROR_NOMEM;
1273 device.backend_devid = nic->devid;
1274 device.backend_domid = nic->backend_domid;
1275 device.backend_kind = DEVICE_VIF;
1276 device.devid = nic->devid;
1277 device.domid = nic->domid;
1278 device.kind = DEVICE_VIF;
1280 flexarray_set(back, boffset++, "frontend-id");
1281 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
1282 flexarray_set(back, boffset++, "online");
1283 flexarray_set(back, boffset++, "1");
1284 flexarray_set(back, boffset++, "state");
1285 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1286 flexarray_set(back, boffset++, "script");
1287 flexarray_set(back, boffset++, nic->script);
1288 flexarray_set(back, boffset++, "mac");
1289 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1290 nic->mac[0], nic->mac[1], nic->mac[2],
1291 nic->mac[3], nic->mac[4], nic->mac[5]));
1292 flexarray_set(back, boffset++, "handle");
1293 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
1295 flexarray_set(front, foffset++, "backend-id");
1296 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
1297 flexarray_set(front, foffset++, "state");
1298 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1299 flexarray_set(front, foffset++, "handle");
1300 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
1301 flexarray_set(front, foffset++, "mac");
1302 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1303 nic->mac[0], nic->mac[1], nic->mac[2],
1304 nic->mac[3], nic->mac[4], nic->mac[5]));
1305 if (0 /* protocol != native*/) {
1306 flexarray_set(front, foffset++, "protocol");
1307 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1310 libxl_device_generic_add(ctx, &device,
1311 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1312 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1314 /* FIXME: wait for plug */
1315 flexarray_free(back);
1316 flexarray_free(front);
1317 return 0;
1320 int libxl_device_nic_del(struct libxl_ctx *ctx,
1321 libxl_device_nic *nic, int wait)
1323 libxl_device device;
1325 device.backend_devid = nic->devid;
1326 device.backend_domid = nic->backend_domid;
1327 device.backend_kind = DEVICE_VIF;
1328 device.devid = nic->devid;
1329 device.domid = nic->domid;
1330 device.kind = DEVICE_VIF;
1332 return libxl_device_del(ctx, &device, wait);
1335 /******************************************************************************/
1336 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1338 flexarray_t *front;
1339 flexarray_t *back;
1340 unsigned int boffset = 0;
1341 unsigned int foffset = 0;
1342 libxl_device device;
1344 if (console->build_state) {
1345 xs_transaction_t t;
1346 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
1347 ents[0] = "console/port";
1348 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
1349 ents[2] = "console/ring-ref";
1350 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
1351 ents[4] = "console/limit";
1352 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
1353 ents[6] = "console/type";
1354 if (console->constype == CONSTYPE_XENCONSOLED)
1355 ents[7] = "xenconsoled";
1356 else
1357 ents[7] = "ioemu";
1358 retry_transaction:
1359 t = xs_transaction_start(ctx->xsh);
1360 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
1361 if (!xs_transaction_end(ctx->xsh, t, 0))
1362 if (errno == EAGAIN)
1363 goto retry_transaction;
1366 front = flexarray_make(16, 1);
1367 if (!front)
1368 return ERROR_NOMEM;
1369 back = flexarray_make(16, 1);
1370 if (!back)
1371 return ERROR_NOMEM;
1373 device.backend_devid = console->devid;
1374 device.backend_domid = console->backend_domid;
1375 device.backend_kind = DEVICE_CONSOLE;
1376 device.devid = console->devid;
1377 device.domid = console->domid;
1378 device.kind = DEVICE_CONSOLE;
1380 flexarray_set(back, boffset++, "frontend-id");
1381 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
1382 flexarray_set(back, boffset++, "online");
1383 flexarray_set(back, boffset++, "1");
1384 flexarray_set(back, boffset++, "state");
1385 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1386 flexarray_set(back, boffset++, "domain");
1387 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1388 flexarray_set(back, boffset++, "protocol");
1389 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
1391 flexarray_set(front, foffset++, "backend-id");
1392 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
1393 flexarray_set(front, foffset++, "state");
1394 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1395 flexarray_set(front, foffset++, "limit");
1396 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
1397 flexarray_set(front, foffset++, "protocol");
1398 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
1399 flexarray_set(front, foffset++, "type");
1400 if (console->constype == CONSTYPE_XENCONSOLED)
1401 flexarray_set(front, foffset++, "xenconsoled");
1402 else
1403 flexarray_set(front, foffset++, "ioemu");
1405 libxl_device_generic_add(ctx, &device,
1406 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1407 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1408 flexarray_free(back);
1409 flexarray_free(front);
1411 return 0;
1414 /******************************************************************************/
1415 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1417 flexarray_t *front;
1418 flexarray_t *back;
1419 unsigned int boffset = 0;
1420 unsigned int foffset = 0;
1421 libxl_device device;
1423 front = flexarray_make(16, 1);
1424 if (!front)
1425 return ERROR_NOMEM;
1426 back = flexarray_make(16, 1);
1427 if (!back)
1428 return ERROR_NOMEM;
1430 device.backend_devid = vkb->devid;
1431 device.backend_domid = vkb->backend_domid;
1432 device.backend_kind = DEVICE_VKBD;
1433 device.devid = vkb->devid;
1434 device.domid = vkb->domid;
1435 device.kind = DEVICE_VKBD;
1437 flexarray_set(back, boffset++, "frontend-id");
1438 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
1439 flexarray_set(back, boffset++, "online");
1440 flexarray_set(back, boffset++, "1");
1441 flexarray_set(back, boffset++, "state");
1442 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1443 flexarray_set(back, boffset++, "domain");
1444 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1446 flexarray_set(front, foffset++, "backend-id");
1447 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
1448 flexarray_set(front, foffset++, "state");
1449 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1451 libxl_device_generic_add(ctx, &device,
1452 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1453 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1454 flexarray_free(back);
1455 flexarray_free(front);
1457 return 0;
1460 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1462 return ERROR_NI;
1465 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1467 return ERROR_NI;
1470 libxl_device_disk *libxl_device_disk_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
1472 char *be_path_tap, *be_path_vbd;
1473 libxl_device_disk *disks = NULL;
1474 char **l = NULL;
1475 unsigned int numl;
1476 int num_disks = 0, i;
1477 char *type;
1479 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
1480 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
1482 l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
1483 if (l) {
1484 num_disks += numl;
1485 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1486 for (i = 0; i < numl; i++) {
1487 disks[i].backend_domid = 0;
1488 disks[i].domid = domid;
1489 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, l[i]));
1490 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_vbd, l[i])), &(disks[i].phystype));
1491 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, l[i]));
1492 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, l[i])));
1493 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, l[i])), "w"))
1494 disks[i].readwrite = 1;
1495 else
1496 disks[i].readwrite = 0;
1497 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, l[i]))));
1498 disks[i].is_cdrom = !strcmp(type, "cdrom");
1500 free(l);
1502 l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
1503 if (l) {
1504 num_disks += numl;
1505 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1506 for (i = 0; i < numl; i++) {
1507 disks[i].backend_domid = 0;
1508 disks[i].domid = domid;
1509 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, l[i]));
1510 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_tap, l[i])), &(disks[i].phystype));
1511 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, l[i]));
1512 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, l[i])));
1513 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, l[i])), "w"))
1514 disks[i].readwrite = 1;
1515 else
1516 disks[i].readwrite = 0;
1517 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, l[i]))));
1518 disks[i].is_cdrom = !strcmp(type, "cdrom");
1520 free(l);
1522 *num = num_disks;
1523 return disks;
1526 int libxl_cdrom_insert(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1528 int num, i;
1529 uint32_t stubdomid;
1530 libxl_device_disk *disks;
1532 if (!disk->physpath) {
1533 disk->physpath = "";
1534 disk->phystype = PHYSTYPE_PHY;
1536 disks = libxl_device_disk_list(ctx, domid, &num);
1537 for (i = 0; i < num; i++) {
1538 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1539 /* found */
1540 break;
1542 if (i == num) {
1543 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
1544 return -1;
1546 libxl_device_disk_del(ctx, disks + i, 1);
1547 libxl_device_disk_add(ctx, domid, disk);
1548 stubdomid = libxl_get_stubdom_id(ctx, domid);
1549 if (stubdomid) {
1550 disks[i].domid = stubdomid;
1551 libxl_device_disk_del(ctx, disks + i, 1);
1552 disk->domid = stubdomid;
1553 libxl_device_disk_add(ctx, stubdomid, disk);
1554 disk->domid = domid;
1556 return 0;
1559 /******************************************************************************/
1560 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
1561 libxl_device_vfb *vfb,
1562 int num_console,
1563 libxl_device_console *console,
1564 libxl_device_model_info *info)
1566 int i = 0, j = 0, num = 0;
1567 memset(info, 0x00, sizeof(libxl_device_model_info));
1569 info->vnc = vfb->vnc;
1570 if (vfb->vnclisten)
1571 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
1572 info->vncdisplay = vfb->vncdisplay;
1573 info->vncunused = vfb->vncunused;
1574 if (vfb->keymap)
1575 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
1576 info->sdl = vfb->sdl;
1577 info->opengl = vfb->opengl;
1578 for (i = 0; i < num_console; i++) {
1579 if (console->constype == CONSTYPE_IOEMU)
1580 num++;
1582 if (num > 0) {
1583 uint32_t guest_domid;
1584 if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
1585 char *filename;
1586 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
1587 libxl_create_logfile(ctx, name, &filename);
1588 info->serial = libxl_sprintf(ctx, "file:%s", filename);
1589 free(filename);
1590 } else {
1591 info->serial = "pty";
1593 num--;
1595 if (num > 0) {
1596 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
1597 for (j = 0; j < num * 2; j = j + 2) {
1598 info->extra[j] = "-serial";
1599 info->extra[j + 1] = "pty";
1601 info->extra[j] = NULL;
1603 info->domid = vfb->domid;
1604 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
1605 info->device_model = "/usr/lib/xen/bin/qemu-dm";
1606 info->type = XENPV;
1607 return 0;
1610 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
1611 int num_console, libxl_device_console *console,
1612 struct libxl_device_model_starting **starting_r)
1614 libxl_device_model_info info;
1616 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
1617 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
1618 return 0;
1621 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1623 flexarray_t *front;
1624 flexarray_t *back;
1625 unsigned int boffset = 0;
1626 unsigned int foffset = 0;
1627 libxl_device device;
1629 front = flexarray_make(16, 1);
1630 if (!front)
1631 return ERROR_NOMEM;
1632 back = flexarray_make(16, 1);
1633 if (!back)
1634 return ERROR_NOMEM;
1636 device.backend_devid = vfb->devid;
1637 device.backend_domid = vfb->backend_domid;
1638 device.backend_kind = DEVICE_VFB;
1639 device.devid = vfb->devid;
1640 device.domid = vfb->domid;
1641 device.kind = DEVICE_VFB;
1643 flexarray_set(back, boffset++, "frontend-id");
1644 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
1645 flexarray_set(back, boffset++, "online");
1646 flexarray_set(back, boffset++, "1");
1647 flexarray_set(back, boffset++, "state");
1648 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1649 flexarray_set(back, boffset++, "domain");
1650 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1651 flexarray_set(back, boffset++, "vnc");
1652 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
1653 flexarray_set(back, boffset++, "vnclisten");
1654 flexarray_set(back, boffset++, vfb->vnclisten);
1655 flexarray_set(back, boffset++, "vncdisplay");
1656 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
1657 flexarray_set(back, boffset++, "vncunused");
1658 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
1659 flexarray_set(back, boffset++, "sdl");
1660 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
1661 flexarray_set(back, boffset++, "opengl");
1662 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
1663 if (vfb->xauthority) {
1664 flexarray_set(back, boffset++, "xauthority");
1665 flexarray_set(back, boffset++, vfb->xauthority);
1667 if (vfb->display) {
1668 flexarray_set(back, boffset++, "display");
1669 flexarray_set(back, boffset++, vfb->display);
1672 flexarray_set(front, foffset++, "backend-id");
1673 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
1674 flexarray_set(front, foffset++, "state");
1675 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1677 libxl_device_generic_add(ctx, &device,
1678 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1679 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1680 flexarray_free(front);
1681 flexarray_free(back);
1683 return 0;
1686 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1688 return ERROR_NI;
1691 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1693 return ERROR_NI;
1696 /******************************************************************************/
1698 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
1699 unsigned int bus, unsigned int dev,
1700 unsigned int func, unsigned int vdevfn)
1702 pcidev->domain = domain;
1703 pcidev->bus = bus;
1704 pcidev->dev = dev;
1705 pcidev->func = func;
1706 pcidev->vdevfn = vdevfn;
1707 return 0;
1710 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
1712 flexarray_t *front;
1713 flexarray_t *back;
1714 unsigned int boffset = 0;
1715 unsigned int foffset = 0;
1716 libxl_device device;
1717 int i;
1719 front = flexarray_make(16, 1);
1720 if (!front)
1721 return ERROR_NOMEM;
1722 back = flexarray_make(16, 1);
1723 if (!back)
1724 return ERROR_NOMEM;
1726 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
1728 /* add pci device */
1729 device.backend_devid = 0;
1730 device.backend_domid = 0;
1731 device.backend_kind = DEVICE_PCI;
1732 device.devid = 0;
1733 device.domid = domid;
1734 device.kind = DEVICE_PCI;
1736 flexarray_set(back, boffset++, "frontend-id");
1737 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
1738 flexarray_set(back, boffset++, "online");
1739 flexarray_set(back, boffset++, "1");
1740 flexarray_set(back, boffset++, "state");
1741 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1742 flexarray_set(back, boffset++, "domain");
1743 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1744 for (i = 0; i < num; i++) {
1745 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
1746 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1747 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
1748 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1749 if (pcidev->vdevfn) {
1750 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
1751 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1753 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
1754 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1755 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
1756 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1758 flexarray_set(back, boffset++, "num_devs");
1759 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
1761 flexarray_set(front, foffset++, "backend-id");
1762 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
1763 flexarray_set(front, foffset++, "state");
1764 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1766 libxl_device_generic_add(ctx, &device,
1767 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1768 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1770 flexarray_free(back);
1771 flexarray_free(front);
1772 return 0;
1775 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1777 flexarray_t *back;
1778 char *num_devs, *be_path;
1779 int num = 0;
1780 unsigned int boffset = 0;
1781 xs_transaction_t t;
1783 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1784 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
1785 if (!num_devs)
1786 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
1788 if (!is_hvm(ctx, domid)) {
1789 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
1790 return -1;
1793 back = flexarray_make(16, 1);
1794 if (!back)
1795 return ERROR_NOMEM;
1797 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
1798 num = atoi(num_devs);
1799 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
1800 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1801 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
1802 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1803 if (pcidev->vdevfn) {
1804 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
1805 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1807 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
1808 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1809 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
1810 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1811 flexarray_set(back, boffset++, "num_devs");
1812 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
1813 flexarray_set(back, boffset++, "state");
1814 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
1816 retry_transaction:
1817 t = xs_transaction_start(ctx->xsh);
1818 libxl_xs_writev(ctx, t, be_path,
1819 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
1820 if (!xs_transaction_end(ctx->xsh, t, 0))
1821 if (errno == EAGAIN)
1822 goto retry_transaction;
1824 flexarray_free(back);
1825 return 0;
1828 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1830 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
1831 int num, i, j;
1832 xs_transaction_t t;
1833 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
1835 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1836 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
1837 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
1838 if (!num_devs)
1839 return -1;
1840 num = atoi(num_devs);
1842 if (!is_hvm(ctx, domid)) {
1843 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1844 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1845 return -1;
1849 for (i = 0; i < num; i++) {
1850 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1851 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
1852 if (domain == pcidev->domain && bus == pcidev->bus &&
1853 pcidev->dev == dev && pcidev->func == func) {
1854 break;
1857 if (i == num) {
1858 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
1859 return -1;
1862 retry_transaction:
1863 t = xs_transaction_start(ctx->xsh);
1864 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
1865 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
1866 if (!xs_transaction_end(ctx->xsh, t, 0))
1867 if (errno == EAGAIN)
1868 goto retry_transaction;
1870 if (!is_hvm(ctx, domid)) {
1871 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1872 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1873 return -1;
1877 retry_transaction2:
1878 t = xs_transaction_start(ctx->xsh);
1879 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
1880 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
1881 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1882 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
1883 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
1884 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
1885 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
1886 for (j = i + 1; j < num; j++) {
1887 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
1888 tmp = libxl_xs_read(ctx, t, tmppath);
1889 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
1890 xs_rm(ctx->xsh, t, tmppath);
1891 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
1892 tmp = libxl_xs_read(ctx, t, tmppath);
1893 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
1894 xs_rm(ctx->xsh, t, tmppath);
1895 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
1896 tmp = libxl_xs_read(ctx, t, tmppath);
1897 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
1898 xs_rm(ctx->xsh, t, tmppath);
1899 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
1900 tmp = libxl_xs_read(ctx, t, tmppath);
1901 if (tmp) {
1902 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
1903 xs_rm(ctx->xsh, t, tmppath);
1905 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
1906 tmp = libxl_xs_read(ctx, t, tmppath);
1907 if (tmp) {
1908 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
1909 xs_rm(ctx->xsh, t, tmppath);
1911 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
1912 tmp = libxl_xs_read(ctx, t, tmppath);
1913 if (tmp) {
1914 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
1915 xs_rm(ctx->xsh, t, tmppath);
1918 if (!xs_transaction_end(ctx->xsh, t, 0))
1919 if (errno == EAGAIN)
1920 goto retry_transaction2;
1922 if (num == 1) {
1923 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
1924 libxl_device_destroy(ctx, be_path, 1);
1925 xs_rm(ctx->xsh, XBT_NULL, be_path);
1926 xs_rm(ctx->xsh, XBT_NULL, fe_path);
1927 return 0;
1930 return 0;
1933 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1935 char *path;
1936 char *state, *vdevfn;
1937 int rc, hvm;
1938 int stubdomid = 0;
1940 /* TODO: check if the device can be assigned */
1942 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
1944 stubdomid = libxl_get_stubdom_id(ctx, domid);
1945 if (stubdomid != 0) {
1946 libxl_device_pci pcidev_s = *pcidev;
1947 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
1950 hvm = is_hvm(ctx, domid);
1951 if (hvm) {
1952 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
1953 return -1;
1955 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
1956 state = libxl_xs_read(ctx, XBT_NULL, path);
1957 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
1958 if (pcidev->vdevfn)
1959 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
1960 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
1961 else
1962 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
1963 pcidev->bus, pcidev->dev, pcidev->func);
1964 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
1965 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
1966 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", NULL, NULL) < 0)
1967 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
1968 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
1969 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
1970 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
1971 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
1972 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
1973 } else {
1974 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
1975 pcidev->bus, pcidev->dev, pcidev->func);
1976 FILE *f = fopen(sysfs_path, "r");
1977 unsigned int start = 0, end = 0, flags = 0, size = 0;
1978 int irq = 0;
1979 int i;
1981 if (f == NULL) {
1982 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
1983 return -1;
1985 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
1986 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
1987 size = end - start + 1;
1988 if (start) {
1989 if (flags & PCI_BAR_IO) {
1990 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
1991 if (rc < 0)
1992 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
1993 } else {
1994 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
1995 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
1996 if (rc < 0)
1997 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
2001 fclose(f);
2002 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
2003 pcidev->bus, pcidev->dev, pcidev->func);
2004 f = fopen(sysfs_path, "r");
2005 if (f == NULL) {
2006 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2007 goto out;
2009 fscanf(f, "%u", &irq);
2010 if (irq) {
2011 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
2012 if (rc < 0) {
2013 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
2015 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
2016 if (rc < 0) {
2017 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
2020 fclose(f);
2022 out:
2023 if (!libxl_is_stubdom(ctx, domid, NULL)) {
2024 rc = xc_assign_device(ctx->xch, domid, pcidev->value);
2025 if (rc < 0)
2026 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
2029 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
2030 return 0;
2033 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
2035 char *path;
2036 char *state;
2037 int hvm, rc;
2038 int stubdomid = 0;
2040 /* TODO: check if the device can be detached */
2041 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
2043 hvm = is_hvm(ctx, domid);
2044 if (hvm) {
2045 if (libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
2046 return -1;
2048 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
2049 state = libxl_xs_read(ctx, XBT_NULL, path);
2050 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
2051 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
2052 pcidev->bus, pcidev->dev, pcidev->func);
2053 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
2054 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
2055 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", NULL, NULL) < 0) {
2056 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
2057 return -1;
2059 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
2060 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
2061 } else {
2062 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
2063 pcidev->bus, pcidev->dev, pcidev->func);
2064 FILE *f = fopen(sysfs_path, "r");
2065 unsigned int start = 0, end = 0, flags = 0, size = 0;
2066 int irq = 0;
2067 int i;
2069 if (f == NULL) {
2070 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2071 goto skip1;
2073 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
2074 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
2075 size = end - start + 1;
2076 if (start) {
2077 if (flags & PCI_BAR_IO) {
2078 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
2079 if (rc < 0)
2080 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
2081 } else {
2082 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
2083 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
2084 if (rc < 0)
2085 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
2089 fclose(f);
2090 skip1:
2091 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
2092 pcidev->bus, pcidev->dev, pcidev->func);
2093 f = fopen(sysfs_path, "r");
2094 if (f == NULL) {
2095 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2096 goto out;
2098 fscanf(f, "%u", &irq);
2099 if (irq) {
2100 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
2101 if (rc < 0) {
2102 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
2104 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
2105 if (rc < 0) {
2106 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
2109 fclose(f);
2111 out:
2112 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
2114 if (!libxl_is_stubdom(ctx, domid, NULL)) {
2115 rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
2116 if (rc < 0)
2117 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
2120 stubdomid = libxl_get_stubdom_id(ctx, domid);
2121 if (stubdomid != 0) {
2122 libxl_device_pci pcidev_s = *pcidev;
2123 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
2126 return 0;
2129 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
2131 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
2132 int n, i;
2133 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
2134 libxl_device_pci *pcidevs;
2136 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
2137 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
2138 if (!num_devs) {
2139 *num = 0;
2140 return NULL;
2142 n = atoi(num_devs);
2143 pcidevs = calloc(n, sizeof(libxl_device_pci));
2144 *num = n;
2146 for (i = 0; i < n; i++) {
2147 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
2148 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
2149 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
2150 if (xsvdevfn)
2151 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
2152 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
2153 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
2154 if (xsopts) {
2155 char *saveptr;
2156 char *p = strtok_r(xsopts, ",=", &saveptr);
2157 do {
2158 while (*p == ' ')
2159 p++;
2160 if (!strcmp(p, "msitranslate")) {
2161 p = strtok_r(NULL, ",=", &saveptr);
2162 pcidevs[i].msitranslate = atoi(p);
2163 } else if (!strcmp(p, "power_mgmt")) {
2164 p = strtok_r(NULL, ",=", &saveptr);
2165 pcidevs[i].power_mgmt = atoi(p);
2167 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
2170 return pcidevs;
2173 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
2175 libxl_device_pci *pcidevs;
2176 int num, i;
2178 pcidevs = libxl_device_pci_list(ctx, domid, &num);
2179 for (i = 0; i < num; i++) {
2180 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
2181 return -1;
2183 free(pcidevs);
2184 return 0;
2187 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
2189 int rc = 0;
2190 uint32_t videoram;
2191 char *videoram_s = NULL;
2192 char *dompath = libxl_xs_get_dompath(ctx, domid);
2194 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
2195 if (!videoram_s)
2196 return -1;
2197 videoram = atoi(videoram_s);
2199 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
2200 rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT);
2201 if (rc != 0)
2202 return rc;
2203 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
2204 return rc;
2207 int libxl_button_press(struct libxl_ctx *ctx, uint32_t domid, libxl_button button)
2209 int rc = -1;
2211 switch (button) {
2212 case POWER_BUTTON:
2213 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_POWER, 0);
2214 break;
2215 case SLEEP_BUTTON:
2216 rc = xc_domain_send_trigger(ctx->xch, domid, XEN_DOMCTL_SENDTRIGGER_SLEEP, 0);
2217 break;
2218 default:
2219 break;
2222 return rc;