debuggers.hg

view tools/libxl/libxl.c @ 20647:2c6a04fdf8fb

libxenlight: implement libxl_set_memory_target

This patch adds a target_memkb parameter to libxl_domain_build_info to
set the target memory for the VM at build time and a new function
called libxl_set_memory_target to dynamically modify the memory target
of a VM at run time. Finally a new command "mem-set" is added to xl
that calls directly libxl_set_memory_target.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Dec 09 10:44:11 2009 +0000 (2009-12-09)
parents 4a2fabce4508
children 042f50e7353f
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 int libxl_ctx_init(struct libxl_ctx *ctx)
38 {
39 memset(ctx, 0, sizeof(struct libxl_ctx));
40 ctx->alloc_maxsize = 256;
41 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
42 if (!ctx->alloc_ptrs)
43 return ERROR_NOMEM;
45 ctx->xch = xc_interface_open();
46 ctx->xsh = xs_daemon_open();
48 ctx->waitpid_instead= libxl_waitpid_instead_default;
49 return 0;
50 }
52 int libxl_ctx_free(struct libxl_ctx *ctx)
53 {
54 libxl_free_all(ctx);
55 free(ctx->alloc_ptrs);
56 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
57 if (!ctx->alloc_ptrs)
58 return ERROR_NOMEM;
59 return 0;
60 }
62 int libxl_ctx_close(struct libxl_ctx *ctx)
63 {
64 libxl_ctx_free(ctx);
65 free(ctx->alloc_ptrs);
66 xc_interface_close(ctx->xch);
67 xs_daemon_close(ctx->xsh);
68 return 0;
69 }
71 int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, void *log_data)
72 {
73 ctx->log_callback = log_callback;
74 ctx->log_userdata = log_data;
75 return 0;
76 }
78 /******************************************************************************/
80 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
81 uint32_t *domid)
82 {
83 int flags, ret, i;
84 char *uuid_string;
85 char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
86 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
87 "control", "attr", "messages" };
88 char *dom_path, *vm_path, *vss_path;
89 struct xs_permissions roperm[2];
90 struct xs_permissions rwperm[1];
91 xs_transaction_t t;
92 xen_domain_handle_t handle;
94 uuid_string = libxl_uuid_to_string(ctx, info->uuid);
95 if (!uuid_string) {
96 XL_LOG(ctx, XL_LOG_ERROR, "missing uuid");
97 return ERROR_FAIL;
98 }
100 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
101 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
102 *domid = 0;
104 /*
105 * info->uuid != xen_domain_handle_t
106 * See:
107 * http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
108 * http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
109 *
110 * A DCE 1.1 compatible source representation of UUIDs.
111 *
112 * struct uuid {
113 * uint32_t time_low;
114 * uint16_t time_mid;
115 * uint16_t time_hi_and_version;
116 * uint8_t clock_seq_hi_and_reserved;
117 * uint8_t clock_seq_low;
118 * uint8_t node[_UUID_NODE_LEN];
119 * };
120 */
121 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
122 memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
124 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
125 if (ret < 0) {
126 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
127 return ERROR_FAIL;
128 }
130 dom_path = libxl_xs_get_dompath(ctx, *domid);
131 if (!dom_path)
132 return ERROR_FAIL;
134 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
135 vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
136 if (!vm_path || !vss_path) {
137 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
138 return ERROR_FAIL;
139 }
141 roperm[0].id = 0;
142 roperm[0].perms = XS_PERM_NONE;
143 roperm[1].id = *domid;
144 roperm[1].perms = XS_PERM_READ;
145 rwperm[0].id = *domid;
146 rwperm[0].perms = XS_PERM_NONE;
148 retry_transaction:
149 t = xs_transaction_start(ctx->xsh);
150 xs_rm(ctx->xsh, t, dom_path);
151 xs_mkdir(ctx->xsh, t, dom_path);
152 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
154 xs_rm(ctx->xsh, t, vm_path);
155 xs_mkdir(ctx->xsh, t, vm_path);
156 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
158 xs_rm(ctx->xsh, t, vss_path);
159 xs_mkdir(ctx->xsh, t, vss_path);
160 xs_set_permissions(ctx->xsh, t, vss_path, rwperm, ARRAY_SIZE(rwperm));
162 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
163 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
164 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", dom_path), info->name, strlen(info->name));
166 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
167 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
168 xs_mkdir(ctx->xsh, t, path);
169 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
170 libxl_free(ctx, path);
171 }
172 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
173 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
174 xs_mkdir(ctx->xsh, t, path);
175 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
176 libxl_free(ctx, path);
177 }
179 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
180 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
182 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
183 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
185 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
187 if (!xs_transaction_end(ctx->xsh, t, 0))
188 if (errno == EAGAIN)
189 goto retry_transaction;
190 return 0;
191 }
193 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
194 {
195 char **vments = NULL, **localents = NULL;
196 int i;
198 build_pre(ctx, domid, info, state);
199 if (info->hvm) {
200 build_hvm(ctx, domid, info, state);
201 vments = libxl_calloc(ctx, 5, sizeof(char *));
202 vments[0] = "rtc/timeoffset";
203 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
204 vments[2] = "image/ostype";
205 vments[3] = "hvm";
206 } else {
207 build_pv(ctx, domid, info, state);
208 vments = libxl_calloc(ctx, 9, sizeof(char *));
209 i = 0;
210 vments[i++] = "image/ostype";
211 vments[i++] = "linux";
212 vments[i++] = "image/kernel";
213 vments[i++] = (char*) info->kernel;
214 if (info->u.pv.ramdisk) {
215 vments[i++] = "image/ramdisk";
216 vments[i++] = (char*) info->u.pv.ramdisk;
217 }
218 if (info->u.pv.cmdline) {
219 vments[i++] = "image/cmdline";
220 vments[i++] = (char*) info->u.pv.cmdline;
221 }
222 }
223 build_post(ctx, domid, info, state, vments, localents);
224 return 0;
225 }
227 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
228 uint32_t domid, int fd, libxl_domain_build_state *state,
229 libxl_device_model_info *dm_info)
230 {
231 char **vments = NULL, **localents = NULL;
232 int i;
234 build_pre(ctx, domid, info, state);
235 restore_common(ctx, domid, info, state, fd);
236 if (info->hvm) {
237 vments = libxl_calloc(ctx, 5, sizeof(char *));
238 vments[0] = "rtc/timeoffset";
239 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
240 vments[2] = "image/ostype";
241 vments[3] = "hvm";
242 } else {
243 vments = libxl_calloc(ctx, 9, sizeof(char *));
244 i = 0;
245 vments[i++] = "image/ostype";
246 vments[i++] = "linux";
247 vments[i++] = "image/kernel";
248 vments[i++] = (char*) info->kernel;
249 if (info->u.pv.ramdisk) {
250 vments[i++] = "image/ramdisk";
251 vments[i++] = (char*) info->u.pv.ramdisk;
252 }
253 if (info->u.pv.cmdline) {
254 vments[i++] = "image/cmdline";
255 vments[i++] = (char*) info->u.pv.cmdline;
256 }
257 }
258 build_post(ctx, domid, info, state, vments, localents);
259 if (info->hvm)
260 asprintf(&(dm_info->saved_state), "/var/lib/xen/qemu-save.%d", domid);
261 else
262 dm_info->saved_state = NULL;
264 return 0;
265 }
267 int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
268 {
269 if (is_hvm(ctx, domid)) {
270 XL_LOG(ctx, XL_LOG_DEBUG, "Called domain_resume on "
271 "non-cooperative hvm domain %u", domid);
272 return ERROR_NI;
273 }
274 if (xc_domain_resume(ctx->xch, domid, 1)) {
275 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
276 "xc_domain_resume failed for domain %u",
277 domid);
278 return ERROR_FAIL;
279 }
280 if (!xs_resume_domain(ctx->xsh, domid)) {
281 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
282 "xs_resume_domain failed for domain %u",
283 domid);
284 return ERROR_FAIL;
285 }
286 return 0;
287 }
289 struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
290 {
291 struct libxl_dominfo *ptr;
292 int index, i, ret, first_domain;
293 xc_domaininfo_t info[16];
294 int size = 16;
296 first_domain = 1;
297 index = 0;
298 ptr = calloc(size, sizeof(struct libxl_dominfo));
299 if (!ptr)
300 return NULL;
301 redo:
302 ret = xc_domain_getinfolist(ctx->xch, first_domain, 16, info);
303 for (i = 0; i < ret; i++) {
304 if (index == size) {
305 struct libxl_dominfo *ptr2;
307 ptr2 = calloc(size * 2, sizeof(struct libxl_dominfo));
308 if (!ptr2) {
309 free(ptr);
310 return NULL;
311 }
312 memcpy(ptr2, ptr, sizeof(struct libxl_dominfo) * size);
313 free(ptr);
314 ptr = ptr2;
315 size *= 2;
316 }
317 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
318 ptr[index].domid = info[i].domain;
319 first_domain = info[i].domain + 1;
320 index++;
321 }
322 if (ret == 16)
323 goto redo;
324 *nb_domain = index;
325 return ptr;
326 }
328 xc_dominfo_t * libxl_domain_infolist(struct libxl_ctx *ctx, int *nb_domain)
329 {
330 int index, first_domain;
331 xc_dominfo_t *info;
332 int size = 1024;
334 first_domain = 0;
335 index = 0;
336 info = (xc_dominfo_t *) calloc(size, sizeof(xc_dominfo_t));
337 if (!info) {
338 *nb_domain = 0;
339 return NULL;
340 }
341 *nb_domain = xc_domain_getinfo(ctx->xch, first_domain, 1024, info);
342 return info;
343 }
345 static int libxl_save_device_model(struct libxl_ctx *ctx, uint32_t domid, int fd)
346 {
347 int fd2, c;
348 char buf[1024];
349 char *filename = libxl_sprintf(ctx, "/var/lib/xen/qemu-save.%d", domid);
351 XL_LOG(ctx, XL_LOG_DEBUG, "Saving device model state to %s", filename);
352 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "save", strlen("save"));
353 libxl_wait_for_device_model(ctx, domid, "paused", NULL, NULL);
355 write(fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE));
356 fd2 = open(filename, O_RDONLY);
357 while ((c = read(fd2, buf, sizeof(buf))) != 0) {
358 write(fd, buf, c);
359 }
360 close(fd2);
361 unlink(filename);
362 return 0;
363 }
365 xc_dominfo_t *libxl_domain_info(struct libxl_ctx *ctx, uint32_t domid)
366 {
367 xc_dominfo_t *info;
368 int rc;
370 info = (xc_dominfo_t *) calloc(1, sizeof(xc_dominfo_t));
371 if (!info) {
372 return NULL;
373 }
374 rc = xc_domain_getinfo(ctx->xch, domid, 1, info);
375 if (rc != 1) {
376 free(info);
377 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Failed to get info for domain %u",
378 domid);
379 return NULL;
380 }
381 if (info->domid != domid) {
382 free(info);
383 XL_LOG(ctx, XL_LOG_ERROR, "Failed to get info for domain %u"
384 ", seems to not exist anymore", domid);
385 return NULL;
386 }
388 return info;
389 }
390 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
391 uint32_t domid, int fd)
392 {
393 int hvm = is_hvm(ctx, domid);
394 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
395 int debug = info != NULL && info->flags & XL_SUSPEND_LIVE;
398 core_suspend(ctx, domid, fd, hvm, live, debug);
399 if (hvm)
400 libxl_save_device_model(ctx, domid, fd);
402 return 0;
403 }
405 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
406 {
407 xc_domain_pause(ctx->xch, domid);
408 return 0;
409 }
411 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
412 {
413 char path[50];
414 char *state;
416 if (is_hvm(ctx, domid)) {
417 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
418 state = libxl_xs_read(ctx, XBT_NULL, path);
419 if (state != NULL && !strcmp(state, "paused")) {
420 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue", strlen("continue"));
421 libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL);
422 }
423 }
424 xc_domain_unpause(ctx->xch, domid);
426 return 0;
427 }
429 static char *req_table[] = {
430 [0] = "poweroff",
431 [1] = "reboot",
432 [2] = "suspend",
433 [3] = "crash",
434 [4] = "halt",
435 };
437 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
438 {
439 char *shutdown_path;
440 char *dom_path;
442 if (req > ARRAY_SIZE(req_table))
443 return ERROR_INVAL;
445 dom_path = libxl_xs_get_dompath(ctx, domid);
446 if (!dom_path)
447 return ERROR_FAIL;
449 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
451 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
452 if (/* hvm */ 0) {
453 unsigned long acpi_s_state = 0;
454 unsigned long pvdriver = 0;
455 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
456 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
457 if (!pvdriver && acpi_s_state != 0)
458 xc_domain_shutdown(ctx->xch, domid, req);
459 }
460 return 0;
461 }
463 int libxl_get_wait_fd(struct libxl_ctx *ctx, int *fd)
464 {
465 *fd = xs_fileno(ctx->xsh);
466 return 0;
467 }
469 int libxl_wait_for_domain_death(struct libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
470 {
471 waiter->path = strdup("@releaseDomain");
472 asprintf(&(waiter->token), "%d", DOMAIN_DEATH);
473 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
474 return -1;
475 return 0;
476 }
478 int libxl_wait_for_disk_ejects(struct libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
479 {
480 int i;
481 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
483 if (!domid)
484 domid = guest_domid;
486 for (i = 0; i < num_disks; i++) {
487 asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject", libxl_xs_get_dompath(ctx, domid), device_disk_dev_number(disks[i].virtpath));
488 asprintf(&(waiter[i].token), "%d", DISK_EJECT);
489 xs_watch(ctx->xsh, waiter->path, waiter->token);
490 }
491 return 0;
492 }
494 int libxl_get_event(struct libxl_ctx *ctx, libxl_event *event)
495 {
496 unsigned int num;
497 char **events = xs_read_watch(ctx->xsh, &num);
498 if (num != 2) {
499 free(events);
500 return -1;
501 }
502 event->path = strdup(events[XS_WATCH_PATH]);
503 event->token = strdup(events[XS_WATCH_TOKEN]);
504 event->type = atoi(event->token);
505 free(events);
506 return 0;
507 }
509 int libxl_stop_waiting(struct libxl_ctx *ctx, libxl_waiter *waiter)
510 {
511 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
512 return -1;
513 else
514 return 0;
515 }
517 int libxl_free_event(libxl_event *event)
518 {
519 free(event->path);
520 free(event->token);
521 return 0;
522 }
524 int libxl_free_waiter(libxl_waiter *waiter)
525 {
526 free(waiter->path);
527 free(waiter->token);
528 return 0;
529 }
531 int libxl_event_get_domain_death_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, xc_dominfo_t *info)
532 {
533 int nb_domain, i, rc = 0;
534 xc_dominfo_t *list = NULL;
536 if (event && event->type == DOMAIN_DEATH) {
537 list = libxl_domain_infolist(ctx, &nb_domain);
538 for (i = 0; i < nb_domain; i++) {
539 if (domid == list[i].domid) {
540 if (list[i].running || (!list[i].shutdown && !list[i].crashed && !list[i].dying))
541 goto out;
542 *info = list[i];
543 rc = 1;
544 goto out;
545 }
546 }
547 memset(info, 0x00, sizeof(xc_dominfo_t));
548 rc = 1;
549 goto out;
550 }
551 out:
552 free(list);
553 return rc;
554 }
556 int libxl_event_get_disk_eject_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
557 {
558 if (event && event->type == DISK_EJECT) {
559 char *path;
560 char *backend;
561 char *value = libxl_xs_read(ctx, XBT_NULL, event->path);
563 if (!value || strcmp(value, "eject"))
564 return 0;
566 path = strdup(event->path);
567 path[strlen(path) - 6] = '\0';
568 backend = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", path));
570 disk->backend_domid = 0;
571 disk->domid = domid;
572 disk->physpath = NULL;
573 disk->phystype = 0;
574 /* this value is returned to the user: do not free right away */
575 disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", backend));
576 disk->unpluggable = 1;
577 disk->readwrite = 0;
578 disk->is_cdrom = 1;
580 free(path);
581 return 1;
582 }
583 return 0;
584 }
586 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
587 {
588 char *pid;
589 int ret;
591 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
592 if (!pid) {
593 int stubdomid = libxl_get_stubdom_id(ctx, domid);
594 if (!stubdomid) {
595 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
596 return -1;
597 }
598 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
599 return libxl_domain_destroy(ctx, stubdomid, 0);
600 }
601 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
603 ret = kill(atoi(pid), SIGHUP);
604 if (ret < 0 && errno == ESRCH) {
605 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
606 ret = 0;
607 } else if (ret == 0) {
608 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
609 ret = 0;
610 } else {
611 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
612 atoi(pid));
613 }
614 return ret;
615 }
617 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
618 {
619 char *dom_path, *uuid_str;
620 char vm_path[UUID_LEN_STR + 5], vss_path[UUID_LEN_STR + 6], xapi_path[20];
621 xen_uuid_t *uuid;
622 int rc, dm_present;
624 if (is_hvm(ctx, domid)) {
625 dm_present = 1;
626 } else {
627 char *pid;
628 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
629 dm_present = (pid != NULL);
630 libxl_free(ctx, pid);
631 }
633 dom_path = libxl_xs_get_dompath(ctx, domid);
634 if (!dom_path)
635 return -1;
637 if (libxl_domid_to_uuid(ctx, &uuid, domid) < 0) {
638 XL_LOG(ctx, XL_LOG_ERROR, "failed ot get uuid for %d", domid);
639 return -1;
640 }
641 if (libxl_device_pci_shutdown(ctx, domid) < 0)
642 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
643 if (dm_present) {
644 xs_write(ctx->xsh, XBT_NULL,
645 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
646 "shutdown", strlen("shutdown"));
647 }
648 rc = xc_domain_pause(ctx->xch, domid);
649 if (rc < 0) {
650 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
651 return -1;
652 }
653 if (dm_present) {
654 if (libxl_destroy_device_model(ctx, domid) < 0)
655 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
656 }
657 if (libxl_devices_destroy(ctx, domid, force) < 0)
658 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
659 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
660 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
661 uuid_str = libxl_uuid_to_string(ctx, uuid);
662 snprintf(vm_path, sizeof(vm_path), "/vm/%s", uuid_str);
663 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
664 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
665 snprintf(vss_path, sizeof(vss_path), "/vss/%s", uuid_str);
666 if (!xs_rm(ctx->xsh, XBT_NULL, vss_path))
667 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vss_path);
668 libxl_free(ctx, uuid_str);
669 snprintf(xapi_path, sizeof(xapi_path), "/xapi/%u", domid);
670 if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
671 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
672 rc = xc_domain_destroy(ctx->xch, domid);
673 if (rc < 0) {
674 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
675 return -1;
676 }
677 return 0;
678 }
680 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num)
681 {
682 struct stat st;
683 const char *XENCONSOLE = "/usr/lib/xen/bin/xenconsole";
684 char *cmd;
686 if (stat(XENCONSOLE, &st) != 0) {
687 XL_LOG(ctx, XL_LOG_ERROR, "could not access %s", XENCONSOLE);
688 return ERROR_FAIL;
689 }
691 cmd = libxl_sprintf(ctx, "%s %d --num %d", XENCONSOLE, domid, cons_num);
692 return (system(cmd) != 0) ? ERROR_FAIL : 0;
693 }
695 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
696 libxl_device_model_info *info,
697 libxl_device_nic *vifs,
698 int num_vifs)
699 {
700 int num = 0, i;
701 flexarray_t *dm_args;
702 dm_args = flexarray_make(16, 1);
703 if (!dm_args)
704 return NULL;
706 flexarray_set(dm_args, num++, "qemu-dm");
707 flexarray_set(dm_args, num++, "-d");
709 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
711 if (info->dom_name) {
712 flexarray_set(dm_args, num++, "-domain-name");
713 flexarray_set(dm_args, num++, info->dom_name);
714 }
715 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
716 flexarray_set(dm_args, num++, "-vnc");
717 if (info->vncdisplay) {
718 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
719 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:%d", info->vnclisten, info->vncdisplay));
720 } else {
721 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
722 }
723 } else if (info->vnclisten) {
724 if (strchr(info->vnclisten, ':') != NULL) {
725 flexarray_set(dm_args, num++, info->vnclisten);
726 } else {
727 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
728 }
729 } else {
730 flexarray_set(dm_args, num++, "127.0.0.1:0");
731 }
732 if (info->vncunused) {
733 flexarray_set(dm_args, num++, "-vncunused");
734 }
735 }
736 if (info->sdl || info->opengl) {
737 flexarray_set(dm_args, num++, "-sdl");
738 if (info->opengl) {
739 flexarray_set(dm_args, num++, "-disable-opengl");
740 }
741 }
742 if (info->keymap) {
743 flexarray_set(dm_args, num++, "-k");
744 flexarray_set(dm_args, num++, info->keymap);
745 }
746 if (info->nographic && (!info->sdl && !info->vnc)) {
747 flexarray_set(dm_args, num++, "-nographic");
748 }
749 if (info->serial) {
750 flexarray_set(dm_args, num++, "-serial");
751 flexarray_set(dm_args, num++, info->serial);
752 }
753 if (info->type == XENFV) {
754 if (info->videoram) {
755 flexarray_set(dm_args, num++, "-videoram");
756 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
757 }
758 if (info->stdvga) {
759 flexarray_set(dm_args, num++, "-std-vga");
760 }
762 if (info->boot) {
763 flexarray_set(dm_args, num++, "-boot");
764 flexarray_set(dm_args, num++, info->boot);
765 }
766 if (info->usb) {
767 flexarray_set(dm_args, num++, "-usb");
768 if (info->usbdevice) {
769 flexarray_set(dm_args, num++, "-usbdevice");
770 flexarray_set(dm_args, num++, info->usbdevice);
771 }
772 }
773 if (info->apic) {
774 flexarray_set(dm_args, num++, "-acpi");
775 }
776 for (i = 0; i < num_vifs; i++) {
777 if (vifs[i].nictype == NICTYPE_IOEMU) {
778 flexarray_set(dm_args, num++, "-net");
779 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
780 vifs[i].devid, vifs[i].smac, vifs[i].model));
781 flexarray_set(dm_args, num++, "-net");
782 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
783 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
784 }
785 }
786 }
787 if (info->saved_state) {
788 flexarray_set(dm_args, num++, "-loadvm");
789 flexarray_set(dm_args, num++, info->saved_state);
790 }
791 for (i = 0; info->extra && info->extra[i] != NULL; i++)
792 flexarray_set(dm_args, num++, info->extra[i]);
793 flexarray_set(dm_args, num++, "-M");
794 if (info->type == XENPV)
795 flexarray_set(dm_args, num++, "xenpv");
796 else
797 flexarray_set(dm_args, num++, "xenfv");
798 flexarray_set(dm_args, num++, NULL);
800 return (char **) flexarray_contents(dm_args);
801 }
803 void dm_xenstore_record_pid(struct libxl_ctx *ctx, void *for_spawn,
804 pid_t innerchild) {
805 struct libxl_device_model_starting *starting = for_spawn;
806 struct libxl_ctx clone;
807 char *kvs[3];
808 int rc, cloned;
810 if (libxl_clone_context_xs(ctx, &clone)) {
811 XL_LOG(ctx, XL_LOG_ERROR, "Out of memory when cloning context");
812 /* Throw a prayer fallback */
813 clone = *ctx;
814 clone.xsh = xs_daemon_open();
815 cloned = 0;
816 } else {
817 cloned = 1;
818 }
819 /* we mustn't use the parent's handle in the child */
821 kvs[0] = "image/device-model-pid";
822 kvs[1] = libxl_sprintf(&clone, "%d", innerchild);
823 kvs[2] = NULL;
824 rc = libxl_xs_writev(&clone, XBT_NULL, starting->dom_path, kvs);
825 if (rc) XL_LOG_ERRNO(&clone, XL_LOG_ERROR,
826 "Couldn't record device model pid %ld at %s/%s",
827 (unsigned long)innerchild, starting->dom_path, kvs);
828 if (cloned) {
829 libxl_discard_cloned_context_xs(&clone);
830 } else {
831 xs_daemon_close(clone.xsh);
832 }
833 }
835 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
836 libxl_device_model_info *info,
837 libxl_device_vfb *vfb,
838 libxl_device_vkb *vkb)
839 {
840 memset(vfb, 0x00, sizeof(libxl_device_vfb));
841 memset(vkb, 0x00, sizeof(libxl_device_vkb));
843 vfb->backend_domid = 0;
844 vfb->devid = 0;
845 vfb->vnc = info->vnc;
846 vfb->vnclisten = info->vnclisten;
847 vfb->vncdisplay = info->vncdisplay;
848 vfb->vncunused = info->vncunused;
849 vfb->keymap = info->keymap;
850 vfb->sdl = info->sdl;
851 vfb->opengl = info->opengl;
853 vkb->backend_domid = 0;
854 vkb->devid = 0;
855 return 0;
856 }
858 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
859 {
860 int i;
861 char *vm_path;
862 char *dmargs, *path;
863 int dmargs_size;
864 struct xs_permissions roperm[2];
865 xs_transaction_t t;
867 roperm[0].id = 0;
868 roperm[0].perms = XS_PERM_NONE;
869 roperm[1].id = domid;
870 roperm[1].perms = XS_PERM_READ;
872 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
874 i = 0;
875 dmargs_size = 0;
876 while (args[i] != NULL) {
877 dmargs_size = dmargs_size + strlen(args[i]) + 1;
878 i++;
879 }
880 dmargs_size++;
881 dmargs = (char *) malloc(dmargs_size);
882 i = 1;
883 dmargs[0] = '\0';
884 while (args[i] != NULL) {
885 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
886 strcat(dmargs, " ");
887 strcat(dmargs, args[i]);
888 }
889 i++;
890 }
891 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
893 retry_transaction:
894 t = xs_transaction_start(ctx->xsh);
895 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
896 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
897 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
898 if (!xs_transaction_end(ctx->xsh, t, 0))
899 if (errno == EAGAIN)
900 goto retry_transaction;
901 free(dmargs);
902 return 0;
903 }
905 static int libxl_create_stubdom(struct libxl_ctx *ctx,
906 libxl_device_model_info *info,
907 libxl_device_disk *disks, int num_disks,
908 libxl_device_nic *vifs, int num_vifs,
909 libxl_device_vfb *vfb,
910 libxl_device_vkb *vkb,
911 libxl_device_model_starting **starting_r)
912 {
913 int i, num_console = 1;
914 libxl_device_console *console;
915 libxl_domain_create_info c_info;
916 libxl_domain_build_info b_info;
917 libxl_domain_build_state state;
918 uint32_t domid;
919 char **args;
920 xen_uuid_t uuid[16];
921 struct xs_permissions perm[2];
922 xs_transaction_t t;
923 libxl_device_model_starting *dm_starting = 0;
925 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
926 if (!args)
927 return ERROR_FAIL;
929 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
930 c_info.hvm = 0;
931 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
932 xen_uuid_generate(uuid);
933 c_info.uuid = uuid;
935 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
936 b_info.max_vcpus = 1;
937 b_info.max_memkb = 32 * 1024;
938 b_info.target_memkb = b_info.max_memkb;
939 b_info.kernel = "/usr/lib/xen/boot/ioemu-stubdom.gz";
940 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
941 b_info.u.pv.ramdisk = "";
942 b_info.u.pv.features = "";
943 b_info.hvm = 0;
945 libxl_domain_make(ctx, &c_info, &domid);
946 libxl_domain_build(ctx, &b_info, domid, &state);
948 libxl_write_dmargs(ctx, domid, info->domid, args);
949 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)), "%d", domid);
950 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)), "%d", info->domid);
951 xc_domain_set_target(ctx->xch, domid, info->domid);
952 xs_set_target(ctx->xsh, domid, info->domid);
954 perm[0].id = domid;
955 perm[0].perms = XS_PERM_NONE;
956 perm[1].id = info->domid;
957 perm[1].perms = XS_PERM_READ;
958 retry_transaction:
959 t = xs_transaction_start(ctx->xsh);
960 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
961 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
962 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
963 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
964 if (!xs_transaction_end(ctx->xsh, t, 0))
965 if (errno == EAGAIN)
966 goto retry_transaction;
968 for (i = 0; i < num_disks; i++) {
969 libxl_device_disk disk = disks[i];
970 disk_info_domid_fixup(&disk, domid);
971 libxl_device_disk_add(ctx, domid, &disk);
972 }
973 for (i = 0; i < num_vifs; i++) {
974 libxl_device_nic nic = vifs[i];
975 nic_info_domid_fixup(&nic, domid);
976 libxl_device_nic_add(ctx, domid, &nic);
977 }
978 vfb_info_domid_fixup(vfb, domid);
979 libxl_device_vfb_add(ctx, domid, vfb);
980 vkb_info_domid_fixup(vkb, domid);
981 libxl_device_vkb_add(ctx, domid, vkb);
983 if (info->serial)
984 num_console++;
985 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
986 for (i = 0; i < num_console; i++) {
987 if (!i)
988 init_console_info(&console[i], i, &state);
989 else
990 init_console_info(&console[i], i, NULL);
991 console_info_domid_fixup(&console[i], domid);
992 console[i].constype = CONSTYPE_IOEMU;
993 libxl_device_console_add(ctx, domid, &console[i]);
994 }
995 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
996 free(args);
997 return -1;
998 }
999 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
1000 free(args);
1001 return -1;
1004 libxl_domain_unpause(ctx, domid);
1006 if (starting_r) {
1007 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
1008 (*starting_r)->domid = info->domid;
1009 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
1010 (*starting_r)->for_spawn = NULL;
1013 free(args);
1014 return 0;
1017 int libxl_create_device_model(struct libxl_ctx *ctx,
1018 libxl_device_model_info *info,
1019 libxl_device_disk *disks, int num_disks,
1020 libxl_device_nic *vifs, int num_vifs,
1021 libxl_device_model_starting **starting_r)
1023 char *path, *logfile;
1024 int logfile_w, null;
1025 int rc;
1026 char **args;
1027 struct libxl_device_model_starting buf_starting, *p;
1029 if (strstr(info->device_model, "stubdom-dm")) {
1030 libxl_device_vfb vfb;
1031 libxl_device_vkb vkb;
1033 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
1034 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
1037 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
1038 if (!args)
1039 return ERROR_FAIL;
1041 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
1042 xs_mkdir(ctx->xsh, XBT_NULL, path);
1044 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
1045 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
1046 free(logfile);
1047 null = open("/dev/null", O_RDONLY);
1049 if (starting_r) {
1050 rc = ERROR_NOMEM;
1051 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
1052 if (!*starting_r) goto xit;
1053 p = *starting_r;
1054 p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
1055 } else {
1056 p = &buf_starting;
1057 p->for_spawn = NULL;
1060 p->domid = info->domid;
1061 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
1062 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
1064 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
1065 if (rc < 0) goto xit;
1066 if (!rc) { /* inner child */
1067 libxl_exec(ctx, null, logfile_w, logfile_w,
1068 info->device_model, args);
1071 rc = 0;
1072 xit:
1073 free(args);
1074 close(null);
1075 close(logfile_w);
1077 return rc;
1080 int libxl_detach_device_model(struct libxl_ctx *ctx,
1081 libxl_device_model_starting *starting) {
1082 int rc;
1083 rc = libxl_spawn_detach(ctx, starting->for_spawn);
1084 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
1085 libxl_free(ctx, starting);
1086 return rc;
1090 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
1091 libxl_device_model_starting *starting) {
1092 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
1093 libxl_spawn_check,
1094 starting->for_spawn);
1095 int detach = libxl_detach_device_model(ctx, starting);
1096 return problem ? problem : detach;
1097 return 0;
1101 /******************************************************************************/
1103 static int is_blktap2_supported(void)
1105 char buf[1024];
1106 FILE *f = fopen("/proc/devices", "r");
1109 while (fgets(buf, sizeof(buf), f) != NULL) {
1110 if (strstr(buf, "blktap2")) {
1111 fclose(f);
1112 return 1;
1115 fclose(f);
1116 return 0;
1119 static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type)
1121 char buf[1024];
1122 char *p;
1123 int devnum;
1124 FILE *f = fopen("/sys/class/blktap2/devices", "r");
1127 while (!feof(f)) {
1128 fscanf(f, "%d %s", &devnum, buf);
1129 p = strchr(buf, ':');
1130 if (p == NULL)
1131 continue;
1132 p++;
1133 if (!strcmp(p, name) && !strncmp(buf, type, 3)) {
1134 fclose(f);
1135 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", devnum);
1138 fclose(f);
1139 return NULL;
1142 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1144 flexarray_t *front;
1145 flexarray_t *back;
1146 char *backend_type;
1147 unsigned int boffset = 0;
1148 unsigned int foffset = 0;
1149 int devid;
1150 libxl_device device;
1151 int major, minor;
1153 front = flexarray_make(16, 1);
1154 if (!front)
1155 return ERROR_NOMEM;
1156 back = flexarray_make(16, 1);
1157 if (!back) /* leaks front if error */
1158 return ERROR_NOMEM;
1160 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
1161 devid = device_disk_dev_number(disk->virtpath);
1163 device.backend_devid = devid;
1164 device.backend_domid = disk->backend_domid;
1165 device.devid = devid;
1166 device.domid = disk->domid;
1167 device.kind = DEVICE_VBD;
1169 switch (disk->phystype) {
1170 case PHYSTYPE_PHY: {
1172 device_physdisk_major_minor(disk->physpath, &major, &minor);
1173 flexarray_set(back, boffset++, "physical-device");
1174 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1176 flexarray_set(back, boffset++, "params");
1177 flexarray_set(back, boffset++, disk->physpath);
1179 device.backend_kind = DEVICE_VBD;
1180 break;
1182 case PHYSTYPE_FILE:
1183 /* let's pretend is tap:aio for the moment */
1184 disk->phystype = PHYSTYPE_AIO;
1185 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
1186 if (is_blktap2_supported()) {
1187 int rc, c, p[2], tot;
1188 char buf[1024], *dev;
1189 if ((dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype))) == NULL) {
1190 if (pipe(p) < 0) {
1191 XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
1192 return -1;
1194 rc = fork();
1195 if (rc < 0) {
1196 XL_LOG(ctx, XL_LOG_ERROR, "Failed to fork a new process");
1197 return -1;
1198 } else if (!rc) { /* child */
1199 int null_r, null_w;
1200 char *args[4];
1201 args[0] = "tapdisk2";
1202 args[1] = "-n";
1203 args[2] = libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath);
1204 args[3] = NULL;
1206 null_r = open("/dev/null", O_RDONLY);
1207 null_w = open("/dev/null", O_WRONLY);
1208 libxl_exec(ctx, null_r, p[1], null_w, "/usr/sbin/tapdisk2", args);
1209 XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
1211 close(p[1]);
1212 tot = 0;
1213 while ((c = read(p[0], buf + tot, sizeof(buf) - tot)) > 0)
1214 tot = tot + c;
1215 close(p[0]);
1216 buf[tot - 1] = '\0';
1217 dev = buf;
1219 flexarray_set(back, boffset++, "tapdisk-params");
1220 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
1221 flexarray_set(back, boffset++, "params");
1222 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", dev));
1223 backend_type = "phy";
1224 device_physdisk_major_minor(dev, &major, &minor);
1225 flexarray_set(back, boffset++, "physical-device");
1226 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1227 device.backend_kind = DEVICE_VBD;
1229 break;
1231 flexarray_set(back, boffset++, "params");
1232 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
1233 device_disk_string_of_phystype(disk->phystype), disk->physpath));
1235 device.backend_kind = DEVICE_TAP;
1236 break;
1239 flexarray_set(back, boffset++, "frontend-id");
1240 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
1241 flexarray_set(back, boffset++, "online");
1242 flexarray_set(back, boffset++, "1");
1243 flexarray_set(back, boffset++, "removable");
1244 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
1245 flexarray_set(back, boffset++, "bootable");
1246 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1247 flexarray_set(back, boffset++, "state");
1248 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1249 flexarray_set(back, boffset++, "dev");
1250 flexarray_set(back, boffset++, disk->virtpath);
1251 flexarray_set(back, boffset++, "type");
1252 flexarray_set(back, boffset++, backend_type);
1253 flexarray_set(back, boffset++, "mode");
1254 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1256 flexarray_set(front, foffset++, "backend-id");
1257 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
1258 flexarray_set(front, foffset++, "state");
1259 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1260 flexarray_set(front, foffset++, "virtual-device");
1261 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
1262 flexarray_set(front, foffset++, "device-type");
1263 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1265 if (0 /* protocol != native*/) {
1266 flexarray_set(front, foffset++, "protocol");
1267 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1270 libxl_device_generic_add(ctx, &device,
1271 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1272 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1273 flexarray_free(back);
1274 flexarray_free(front);
1275 return 0;
1278 int libxl_device_disk_del(struct libxl_ctx *ctx,
1279 libxl_device_disk *disk, int wait)
1281 libxl_device device;
1282 int devid;
1284 devid = device_disk_dev_number(disk->virtpath);
1285 device.backend_domid = disk->backend_domid;
1286 device.backend_devid = devid;
1287 device.backend_kind =
1288 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1289 device.domid = disk->domid;
1290 device.devid = devid;
1291 device.kind = DEVICE_VBD;
1292 return libxl_device_del(ctx, &device, wait);
1295 /******************************************************************************/
1296 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1298 flexarray_t *front;
1299 flexarray_t *back;
1300 unsigned int boffset = 0;
1301 unsigned int foffset = 0;
1302 libxl_device device;
1304 front = flexarray_make(16, 1);
1305 if (!front)
1306 return ERROR_NOMEM;
1307 back = flexarray_make(16, 1);
1308 if (!back)
1309 return ERROR_NOMEM;
1311 device.backend_devid = nic->devid;
1312 device.backend_domid = nic->backend_domid;
1313 device.backend_kind = DEVICE_VIF;
1314 device.devid = nic->devid;
1315 device.domid = nic->domid;
1316 device.kind = DEVICE_VIF;
1318 flexarray_set(back, boffset++, "frontend-id");
1319 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
1320 flexarray_set(back, boffset++, "online");
1321 flexarray_set(back, boffset++, "1");
1322 flexarray_set(back, boffset++, "state");
1323 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1324 flexarray_set(back, boffset++, "script");
1325 flexarray_set(back, boffset++, nic->script);
1326 flexarray_set(back, boffset++, "mac");
1327 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1328 nic->mac[0], nic->mac[1], nic->mac[2],
1329 nic->mac[3], nic->mac[4], nic->mac[5]));
1330 flexarray_set(back, boffset++, "handle");
1331 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
1333 flexarray_set(front, foffset++, "backend-id");
1334 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
1335 flexarray_set(front, foffset++, "state");
1336 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1337 flexarray_set(front, foffset++, "handle");
1338 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
1339 flexarray_set(front, foffset++, "mac");
1340 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1341 nic->mac[0], nic->mac[1], nic->mac[2],
1342 nic->mac[3], nic->mac[4], nic->mac[5]));
1343 if (0 /* protocol != native*/) {
1344 flexarray_set(front, foffset++, "protocol");
1345 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1348 libxl_device_generic_add(ctx, &device,
1349 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1350 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1352 /* FIXME: wait for plug */
1353 flexarray_free(back);
1354 flexarray_free(front);
1355 return 0;
1358 int libxl_device_nic_del(struct libxl_ctx *ctx,
1359 libxl_device_nic *nic, int wait)
1361 libxl_device device;
1363 device.backend_devid = nic->devid;
1364 device.backend_domid = nic->backend_domid;
1365 device.backend_kind = DEVICE_VIF;
1366 device.devid = nic->devid;
1367 device.domid = nic->domid;
1368 device.kind = DEVICE_VIF;
1370 return libxl_device_del(ctx, &device, wait);
1373 /******************************************************************************/
1374 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1376 flexarray_t *front;
1377 flexarray_t *back;
1378 unsigned int boffset = 0;
1379 unsigned int foffset = 0;
1380 libxl_device device;
1382 if (console->build_state) {
1383 xs_transaction_t t;
1384 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
1385 ents[0] = "console/port";
1386 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
1387 ents[2] = "console/ring-ref";
1388 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
1389 ents[4] = "console/limit";
1390 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
1391 ents[6] = "console/type";
1392 if (console->constype == CONSTYPE_XENCONSOLED)
1393 ents[7] = "xenconsoled";
1394 else
1395 ents[7] = "ioemu";
1396 retry_transaction:
1397 t = xs_transaction_start(ctx->xsh);
1398 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
1399 if (!xs_transaction_end(ctx->xsh, t, 0))
1400 if (errno == EAGAIN)
1401 goto retry_transaction;
1404 front = flexarray_make(16, 1);
1405 if (!front)
1406 return ERROR_NOMEM;
1407 back = flexarray_make(16, 1);
1408 if (!back)
1409 return ERROR_NOMEM;
1411 device.backend_devid = console->devid;
1412 device.backend_domid = console->backend_domid;
1413 device.backend_kind = DEVICE_CONSOLE;
1414 device.devid = console->devid;
1415 device.domid = console->domid;
1416 device.kind = DEVICE_CONSOLE;
1418 flexarray_set(back, boffset++, "frontend-id");
1419 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
1420 flexarray_set(back, boffset++, "online");
1421 flexarray_set(back, boffset++, "1");
1422 flexarray_set(back, boffset++, "state");
1423 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1424 flexarray_set(back, boffset++, "domain");
1425 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1426 flexarray_set(back, boffset++, "protocol");
1427 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
1429 flexarray_set(front, foffset++, "backend-id");
1430 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
1431 flexarray_set(front, foffset++, "state");
1432 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1433 flexarray_set(front, foffset++, "limit");
1434 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
1435 flexarray_set(front, foffset++, "protocol");
1436 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
1437 flexarray_set(front, foffset++, "type");
1438 if (console->constype == CONSTYPE_XENCONSOLED)
1439 flexarray_set(front, foffset++, "xenconsoled");
1440 else
1441 flexarray_set(front, foffset++, "ioemu");
1443 libxl_device_generic_add(ctx, &device,
1444 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1445 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1446 flexarray_free(back);
1447 flexarray_free(front);
1449 return 0;
1452 /******************************************************************************/
1453 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1455 flexarray_t *front;
1456 flexarray_t *back;
1457 unsigned int boffset = 0;
1458 unsigned int foffset = 0;
1459 libxl_device device;
1461 front = flexarray_make(16, 1);
1462 if (!front)
1463 return ERROR_NOMEM;
1464 back = flexarray_make(16, 1);
1465 if (!back)
1466 return ERROR_NOMEM;
1468 device.backend_devid = vkb->devid;
1469 device.backend_domid = vkb->backend_domid;
1470 device.backend_kind = DEVICE_VKBD;
1471 device.devid = vkb->devid;
1472 device.domid = vkb->domid;
1473 device.kind = DEVICE_VKBD;
1475 flexarray_set(back, boffset++, "frontend-id");
1476 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
1477 flexarray_set(back, boffset++, "online");
1478 flexarray_set(back, boffset++, "1");
1479 flexarray_set(back, boffset++, "state");
1480 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1481 flexarray_set(back, boffset++, "domain");
1482 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1484 flexarray_set(front, foffset++, "backend-id");
1485 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
1486 flexarray_set(front, foffset++, "state");
1487 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1489 libxl_device_generic_add(ctx, &device,
1490 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1491 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1492 flexarray_free(back);
1493 flexarray_free(front);
1495 return 0;
1498 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1500 return ERROR_NI;
1503 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1505 return ERROR_NI;
1508 libxl_device_disk *libxl_device_disk_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
1510 char *be_path_tap, *be_path_vbd;
1511 libxl_device_disk *disks = NULL;
1512 char **l = NULL;
1513 unsigned int numl;
1514 int num_disks = 0, i;
1515 char *type;
1517 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
1518 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
1520 l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
1521 if (l) {
1522 num_disks += numl;
1523 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1524 for (i = 0; i < numl; i++) {
1525 disks[i].backend_domid = 0;
1526 disks[i].domid = domid;
1527 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, l[i]));
1528 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));
1529 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, l[i]));
1530 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, l[i])));
1531 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, l[i])), "w"))
1532 disks[i].readwrite = 1;
1533 else
1534 disks[i].readwrite = 0;
1535 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]))));
1536 disks[i].is_cdrom = !strcmp(type, "cdrom");
1538 free(l);
1540 l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
1541 if (l) {
1542 num_disks += numl;
1543 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1544 for (i = 0; i < numl; i++) {
1545 disks[i].backend_domid = 0;
1546 disks[i].domid = domid;
1547 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, l[i]));
1548 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));
1549 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, l[i]));
1550 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, l[i])));
1551 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, l[i])), "w"))
1552 disks[i].readwrite = 1;
1553 else
1554 disks[i].readwrite = 0;
1555 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]))));
1556 disks[i].is_cdrom = !strcmp(type, "cdrom");
1558 free(l);
1560 *num = num_disks;
1561 return disks;
1564 int libxl_cdrom_insert(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1566 int num, i;
1567 uint32_t stubdomid;
1568 libxl_device_disk *disks;
1570 if (!disk->physpath) {
1571 disk->physpath = "";
1572 disk->phystype = PHYSTYPE_PHY;
1574 disks = libxl_device_disk_list(ctx, domid, &num);
1575 for (i = 0; i < num; i++) {
1576 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1577 /* found */
1578 break;
1580 if (i == num) {
1581 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
1582 return -1;
1584 libxl_device_disk_del(ctx, disks + i, 1);
1585 libxl_device_disk_add(ctx, domid, disk);
1586 stubdomid = libxl_get_stubdom_id(ctx, domid);
1587 if (stubdomid) {
1588 disk_info_domid_fixup(disks + i, stubdomid);
1589 libxl_device_disk_del(ctx, disks + i, 1);
1590 disk_info_domid_fixup(disk, stubdomid);
1591 libxl_device_disk_add(ctx, stubdomid, disk);
1592 disk_info_domid_fixup(disk, domid);
1594 return 0;
1597 /******************************************************************************/
1598 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
1599 libxl_device_vfb *vfb,
1600 int num_console,
1601 libxl_device_console *console,
1602 libxl_device_model_info *info) {
1603 int i = 0, j = 0, num = 0;
1604 memset(info, 0x00, sizeof(libxl_device_model_info));
1606 info->vnc = vfb->vnc;
1607 if (vfb->vnclisten)
1608 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
1609 info->vncdisplay = vfb->vncdisplay;
1610 info->vncunused = vfb->vncunused;
1611 if (vfb->keymap)
1612 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
1613 info->sdl = vfb->sdl;
1614 info->opengl = vfb->opengl;
1615 for (i = 0; i < num_console; i++) {
1616 if (console->constype == CONSTYPE_IOEMU)
1617 num++;
1619 if (num > 0) {
1620 uint32_t guest_domid = libxl_is_stubdom(ctx, vfb->domid);
1621 if (guest_domid) {
1622 char *filename;
1623 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
1624 libxl_create_logfile(ctx, name, &filename);
1625 info->serial = libxl_sprintf(ctx, "file:%s", filename);
1626 free(filename);
1627 } else {
1628 info->serial = "pty";
1630 num--;
1632 if (num > 0) {
1633 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
1634 for (j = 0; j < num * 2; j = j + 2) {
1635 info->extra[j] = "-serial";
1636 info->extra[j + 1] = "pty";
1638 info->extra[j] = NULL;
1640 info->domid = vfb->domid;
1641 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
1642 info->device_model = "/usr/lib/xen/bin/qemu-dm";
1643 info->type = XENPV;
1644 return 0;
1647 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
1648 int num_console, libxl_device_console *console,
1649 struct libxl_device_model_starting **starting_r)
1651 libxl_device_model_info info;
1653 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
1654 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
1655 return 0;
1658 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1660 flexarray_t *front;
1661 flexarray_t *back;
1662 unsigned int boffset = 0;
1663 unsigned int foffset = 0;
1664 libxl_device device;
1666 front = flexarray_make(16, 1);
1667 if (!front)
1668 return ERROR_NOMEM;
1669 back = flexarray_make(16, 1);
1670 if (!back)
1671 return ERROR_NOMEM;
1673 device.backend_devid = vfb->devid;
1674 device.backend_domid = vfb->backend_domid;
1675 device.backend_kind = DEVICE_VFB;
1676 device.devid = vfb->devid;
1677 device.domid = vfb->domid;
1678 device.kind = DEVICE_VFB;
1680 flexarray_set(back, boffset++, "frontend-id");
1681 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
1682 flexarray_set(back, boffset++, "online");
1683 flexarray_set(back, boffset++, "1");
1684 flexarray_set(back, boffset++, "state");
1685 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1686 flexarray_set(back, boffset++, "domain");
1687 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1688 flexarray_set(back, boffset++, "vnc");
1689 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
1690 flexarray_set(back, boffset++, "vnclisten");
1691 flexarray_set(back, boffset++, vfb->vnclisten);
1692 flexarray_set(back, boffset++, "vncdisplay");
1693 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
1694 flexarray_set(back, boffset++, "vncunused");
1695 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
1696 flexarray_set(back, boffset++, "sdl");
1697 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
1698 flexarray_set(back, boffset++, "opengl");
1699 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
1700 if (vfb->xauthority) {
1701 flexarray_set(back, boffset++, "xauthority");
1702 flexarray_set(back, boffset++, vfb->xauthority);
1704 if (vfb->display) {
1705 flexarray_set(back, boffset++, "display");
1706 flexarray_set(back, boffset++, vfb->display);
1709 flexarray_set(front, foffset++, "backend-id");
1710 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
1711 flexarray_set(front, foffset++, "state");
1712 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1714 libxl_device_generic_add(ctx, &device,
1715 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1716 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1717 flexarray_free(front);
1718 flexarray_free(back);
1720 return 0;
1723 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1725 return ERROR_NI;
1728 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1730 return ERROR_NI;
1733 /******************************************************************************/
1735 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
1736 unsigned int bus, unsigned int dev,
1737 unsigned int func, unsigned int vdevfn)
1739 pcidev->domain = domain;
1740 pcidev->bus = bus;
1741 pcidev->dev = dev;
1742 pcidev->func = func;
1743 pcidev->vdevfn = vdevfn;
1744 return 0;
1747 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
1749 flexarray_t *front;
1750 flexarray_t *back;
1751 unsigned int boffset = 0;
1752 unsigned int foffset = 0;
1753 libxl_device device;
1754 int i;
1756 front = flexarray_make(16, 1);
1757 if (!front)
1758 return ERROR_NOMEM;
1759 back = flexarray_make(16, 1);
1760 if (!back)
1761 return ERROR_NOMEM;
1763 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
1765 /* add pci device */
1766 device.backend_devid = 0;
1767 device.backend_domid = 0;
1768 device.backend_kind = DEVICE_PCI;
1769 device.devid = 0;
1770 device.domid = domid;
1771 device.kind = DEVICE_PCI;
1773 flexarray_set(back, boffset++, "frontend-id");
1774 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
1775 flexarray_set(back, boffset++, "online");
1776 flexarray_set(back, boffset++, "1");
1777 flexarray_set(back, boffset++, "state");
1778 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1779 flexarray_set(back, boffset++, "domain");
1780 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1781 for (i = 0; i < num; i++) {
1782 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
1783 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1784 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
1785 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1786 if (pcidev->vdevfn) {
1787 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
1788 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1790 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
1791 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1792 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
1793 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1795 flexarray_set(back, boffset++, "num_devs");
1796 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
1798 flexarray_set(front, foffset++, "backend-id");
1799 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
1800 flexarray_set(front, foffset++, "state");
1801 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1803 libxl_device_generic_add(ctx, &device,
1804 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1805 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1807 flexarray_free(back);
1808 flexarray_free(front);
1809 return 0;
1812 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1814 flexarray_t *back;
1815 char *num_devs, *be_path;
1816 int num = 0;
1817 unsigned int boffset = 0;
1818 xs_transaction_t t;
1820 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1821 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
1822 if (!num_devs)
1823 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
1825 if (!is_hvm(ctx, domid)) {
1826 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
1827 return -1;
1830 back = flexarray_make(16, 1);
1831 if (!back)
1832 return ERROR_NOMEM;
1834 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
1835 num = atoi(num_devs);
1836 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
1837 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1838 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
1839 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1840 if (pcidev->vdevfn) {
1841 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
1842 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1844 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
1845 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1846 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
1847 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1848 flexarray_set(back, boffset++, "num_devs");
1849 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
1850 flexarray_set(back, boffset++, "state");
1851 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
1853 retry_transaction:
1854 t = xs_transaction_start(ctx->xsh);
1855 libxl_xs_writev(ctx, t, be_path,
1856 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
1857 if (!xs_transaction_end(ctx->xsh, t, 0))
1858 if (errno == EAGAIN)
1859 goto retry_transaction;
1861 flexarray_free(back);
1862 return 0;
1865 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1867 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
1868 int num, i, j;
1869 xs_transaction_t t;
1870 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
1872 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1873 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
1874 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
1875 if (!num_devs)
1876 return -1;
1877 num = atoi(num_devs);
1879 if (!is_hvm(ctx, domid)) {
1880 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1881 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1882 return -1;
1886 for (i = 0; i < num; i++) {
1887 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1888 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
1889 if (domain == pcidev->domain && bus == pcidev->bus &&
1890 pcidev->dev == dev && pcidev->func == func) {
1891 break;
1894 if (i == num) {
1895 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
1896 return -1;
1899 retry_transaction:
1900 t = xs_transaction_start(ctx->xsh);
1901 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
1902 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
1903 if (!xs_transaction_end(ctx->xsh, t, 0))
1904 if (errno == EAGAIN)
1905 goto retry_transaction;
1907 if (!is_hvm(ctx, domid)) {
1908 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1909 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1910 return -1;
1914 retry_transaction2:
1915 t = xs_transaction_start(ctx->xsh);
1916 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
1917 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
1918 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1919 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
1920 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
1921 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
1922 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
1923 for (j = i + 1; j < num; j++) {
1924 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
1925 tmp = libxl_xs_read(ctx, t, tmppath);
1926 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
1927 xs_rm(ctx->xsh, t, tmppath);
1928 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
1929 tmp = libxl_xs_read(ctx, t, tmppath);
1930 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
1931 xs_rm(ctx->xsh, t, tmppath);
1932 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
1933 tmp = libxl_xs_read(ctx, t, tmppath);
1934 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
1935 xs_rm(ctx->xsh, t, tmppath);
1936 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
1937 tmp = libxl_xs_read(ctx, t, tmppath);
1938 if (tmp) {
1939 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
1940 xs_rm(ctx->xsh, t, tmppath);
1942 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
1943 tmp = libxl_xs_read(ctx, t, tmppath);
1944 if (tmp) {
1945 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
1946 xs_rm(ctx->xsh, t, tmppath);
1948 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
1949 tmp = libxl_xs_read(ctx, t, tmppath);
1950 if (tmp) {
1951 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
1952 xs_rm(ctx->xsh, t, tmppath);
1955 if (!xs_transaction_end(ctx->xsh, t, 0))
1956 if (errno == EAGAIN)
1957 goto retry_transaction2;
1959 if (num == 1) {
1960 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
1961 libxl_device_destroy(ctx, be_path, 1);
1962 xs_rm(ctx->xsh, XBT_NULL, be_path);
1963 xs_rm(ctx->xsh, XBT_NULL, fe_path);
1964 return 0;
1967 return 0;
1970 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1972 char path[50];
1973 char *state, *vdevfn;
1974 int rc, hvm;
1975 int stubdomid = 0;
1977 /* TODO: check if the device can be assigned */
1979 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
1981 if ((stubdomid = libxl_get_stubdom_id(ctx, domid)) != 0) {
1982 libxl_device_pci pcidev_s = *pcidev;
1983 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
1986 hvm = is_hvm(ctx, domid);
1987 if (hvm) {
1988 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
1989 return -1;
1991 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
1992 state = libxl_xs_read(ctx, XBT_NULL, path);
1993 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
1994 if (pcidev->vdevfn)
1995 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
1996 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
1997 else
1998 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
1999 pcidev->bus, pcidev->dev, pcidev->func);
2000 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
2001 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
2002 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", 0,0) < 0)
2003 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
2004 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
2005 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
2006 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
2007 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
2008 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
2009 } else {
2010 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
2011 pcidev->bus, pcidev->dev, pcidev->func);
2012 FILE *f = fopen(sysfs_path, "r");
2013 unsigned int start = 0, end = 0, flags = 0, size = 0;
2014 int irq = 0;
2015 int i;
2017 if (f == NULL) {
2018 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2019 return -1;
2021 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
2022 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
2023 size = end - start + 1;
2024 if (start) {
2025 if (flags & PCI_BAR_IO) {
2026 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
2027 if (rc < 0)
2028 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
2029 } else {
2030 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
2031 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
2032 if (rc < 0)
2033 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
2037 fclose(f);
2038 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
2039 pcidev->bus, pcidev->dev, pcidev->func);
2040 f = fopen(sysfs_path, "r");
2041 if (f == NULL) {
2042 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2043 goto out;
2045 fscanf(f, "%u", &irq);
2046 if (irq) {
2047 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
2048 if (rc < 0) {
2049 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
2051 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
2052 if (rc < 0) {
2053 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
2056 fclose(f);
2058 out:
2059 if (!libxl_is_stubdom(ctx, domid)) {
2060 if ((rc = xc_assign_device(ctx->xch, domid, pcidev->value)) < 0)
2061 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
2064 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
2065 return 0;
2068 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
2070 char path[50];
2071 char *state;
2072 int hvm, rc;
2073 int stubdomid = 0;
2075 /* TODO: check if the device can be detached */
2076 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
2078 hvm = is_hvm(ctx, domid);
2079 if (hvm) {
2080 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
2081 return -1;
2083 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
2084 state = libxl_xs_read(ctx, XBT_NULL, path);
2085 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/parameter", domid);
2086 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
2087 pcidev->bus, pcidev->dev, pcidev->func);
2088 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/command", domid);
2089 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
2090 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", 0,0) < 0) {
2091 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
2092 return -1;
2094 snprintf(path, sizeof(path), "/local/domain/0/device-model/%d/state", domid);
2095 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
2096 } else {
2097 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
2098 pcidev->bus, pcidev->dev, pcidev->func);
2099 FILE *f = fopen(sysfs_path, "r");
2100 unsigned int start = 0, end = 0, flags = 0, size = 0;
2101 int irq = 0;
2102 int i;
2104 if (f == NULL) {
2105 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2106 goto skip1;
2108 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
2109 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
2110 size = end - start + 1;
2111 if (start) {
2112 if (flags & PCI_BAR_IO) {
2113 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
2114 if (rc < 0)
2115 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
2116 } else {
2117 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
2118 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
2119 if (rc < 0)
2120 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
2124 fclose(f);
2125 skip1:
2126 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
2127 pcidev->bus, pcidev->dev, pcidev->func);
2128 f = fopen(sysfs_path, "r");
2129 if (f == NULL) {
2130 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2131 goto out;
2133 fscanf(f, "%u", &irq);
2134 if (irq) {
2135 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
2136 if (rc < 0) {
2137 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
2139 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
2140 if (rc < 0) {
2141 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
2144 fclose(f);
2146 out:
2147 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
2149 if (!libxl_is_stubdom(ctx, domid)) {
2150 if ((rc = xc_deassign_device(ctx->xch, domid, pcidev->value)) < 0)
2151 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
2154 if ((stubdomid = libxl_get_stubdom_id(ctx, domid)) != 0) {
2155 libxl_device_pci pcidev_s = *pcidev;
2156 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
2159 return 0;
2162 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
2164 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
2165 int n, i;
2166 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
2167 libxl_device_pci *pcidevs;
2169 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
2170 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
2171 if (!num_devs) {
2172 *num = 0;
2173 return NULL;
2175 n = atoi(num_devs);
2176 pcidevs = (libxl_device_pci *) calloc(n, sizeof(libxl_device_pci));
2177 *num = n;
2179 for (i = 0; i < n; i++) {
2180 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
2181 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
2182 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
2183 if (xsvdevfn)
2184 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
2185 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
2186 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
2187 if (xsopts) {
2188 char *saveptr;
2189 char *p = strtok_r(xsopts, ",=", &saveptr);
2190 do {
2191 while (*p == ' ')
2192 p++;
2193 if (!strcmp(p, "msitranslate")) {
2194 p = strtok_r(NULL, ",=", &saveptr);
2195 pcidevs[i].msitranslate = atoi(p);
2196 } else if (!strcmp(p, "power_mgmt")) {
2197 p = strtok_r(NULL, ",=", &saveptr);
2198 pcidevs[i].power_mgmt = atoi(p);
2200 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
2203 return pcidevs;
2206 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
2208 libxl_device_pci *pcidevs;
2209 int num, i;
2211 pcidevs = libxl_device_pci_list(ctx, domid, &num);
2212 for (i = 0; i < num; i++) {
2213 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
2214 return -1;
2216 free(pcidevs);
2217 return 0;
2220 void nic_info_domid_fixup(libxl_device_nic *nic_info, int domid)
2222 nic_info->domid = domid;
2223 asprintf(&(nic_info->ifname), "tap%d.%d", domid, nic_info->devid - 1);
2226 void disk_info_domid_fixup(libxl_device_disk *disk_info, int domid)
2228 disk_info->domid = domid;
2231 void vfb_info_domid_fixup(libxl_device_vfb *vfb, int domid)
2233 vfb->domid = domid;
2236 void vkb_info_domid_fixup(libxl_device_vkb *vkb, int domid)
2238 vkb->domid = domid;
2241 void console_info_domid_fixup(libxl_device_console *console, int domid)
2243 console->domid = domid;
2246 void device_model_info_domid_fixup(libxl_device_model_info *dm_info, int domid)
2248 dm_info->domid = domid;
2251 void init_create_info(libxl_domain_create_info *c_info)
2253 memset(c_info, '\0', sizeof(*c_info));
2254 c_info->xsdata = NULL;
2255 c_info->platformdata = NULL;
2256 c_info->hvm = 1;
2257 c_info->ssidref = 0;
2260 void init_build_info(libxl_domain_build_info *b_info, libxl_domain_create_info *c_info)
2262 memset(b_info, '\0', sizeof(*b_info));
2263 b_info->timer_mode = -1;
2264 b_info->hpet = 1;
2265 b_info->vpt_align = -1;
2266 b_info->max_vcpus = 1;
2267 b_info->max_memkb = 32 * 1024;
2268 b_info->target_memkb = b_info->max_memkb;
2269 if (c_info->hvm) {
2270 b_info->shadow_memkb = libxl_get_required_shadow_memory(b_info->max_memkb, b_info->max_vcpus);
2271 b_info->video_memkb = 8 * 1024;
2272 b_info->kernel = "/usr/lib/xen/boot/hvmloader";
2273 b_info->hvm = 1;
2274 b_info->u.hvm.pae = 1;
2275 b_info->u.hvm.apic = 1;
2276 b_info->u.hvm.acpi = 1;
2277 b_info->u.hvm.nx = 1;
2278 b_info->u.hvm.viridian = 0;
2279 } else {
2280 b_info->u.pv.slack_memkb = 8 * 1024;
2284 void init_dm_info(libxl_device_model_info *dm_info,
2285 libxl_domain_create_info *c_info, libxl_domain_build_info *b_info)
2287 memset(dm_info, '\0', sizeof(*dm_info));
2289 dm_info->dom_name = c_info->name;
2290 dm_info->device_model = "/usr/lib/xen/bin/qemu-dm";
2291 dm_info->videoram = b_info->video_memkb / 1024;
2292 dm_info->apic = b_info->u.hvm.apic;
2294 dm_info->stdvga = 0;
2295 dm_info->vnc = 1;
2296 dm_info->vnclisten = "127.0.0.1";
2297 dm_info->vncdisplay = 0;
2298 dm_info->vncunused = 0;
2299 dm_info->keymap = NULL;
2300 dm_info->sdl = 0;
2301 dm_info->opengl = 0;
2302 dm_info->nographic = 0;
2303 dm_info->serial = NULL;
2304 dm_info->boot = "cda";
2305 dm_info->usb = 0;
2306 dm_info->usbdevice = NULL;
2309 void init_nic_info(libxl_device_nic *nic_info, int devnum)
2311 memset(nic_info, '\0', sizeof(*nic_info));
2314 nic_info->backend_domid = 0;
2315 nic_info->domid = 0;
2316 nic_info->devid = devnum;
2317 nic_info->mtu = 1492;
2318 nic_info->model = "e1000";
2319 srand(time(0));
2320 nic_info->mac[0] = 0x00;
2321 nic_info->mac[1] = 0x16;
2322 nic_info->mac[2] = 0x3e;
2323 nic_info->mac[3] = 1 + (int) (0x7f * (rand() / (RAND_MAX + 1.0)));
2324 nic_info->mac[4] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
2325 nic_info->mac[5] = 1 + (int) (0xff * (rand() / (RAND_MAX + 1.0)));
2326 asprintf(&(nic_info->smac), "%02x:%02x:%02x:%02x:%02x:%02x", nic_info->mac[0], nic_info->mac[1], nic_info->mac[2], nic_info->mac[3], nic_info->mac[4], nic_info->mac[5]);
2327 nic_info->ifname = NULL;
2328 nic_info->bridge = "xenbr0";
2329 nic_info->script = "/etc/xen/scripts/vif-bridge";
2330 nic_info->nictype = NICTYPE_IOEMU;
2333 void init_vfb_info(libxl_device_vfb *vfb, int dev_num)
2335 memset(vfb, 0x00, sizeof(libxl_device_vfb));
2336 vfb->devid = dev_num;
2337 vfb->vnc = 1;
2338 vfb->vnclisten = "127.0.0.1";
2339 vfb->vncdisplay = 0;
2340 vfb->vncunused = 1;
2341 vfb->keymap = NULL;
2342 vfb->sdl = 0;
2343 vfb->opengl = 0;
2346 void init_vkb_info(libxl_device_vkb *vkb, int dev_num)
2348 memset(vkb, 0x00, sizeof(libxl_device_vkb));
2349 vkb->devid = dev_num;
2352 void init_console_info(libxl_device_console *console, int dev_num, libxl_domain_build_state *state)
2354 memset(console, 0x00, sizeof(libxl_device_console));
2355 console->devid = dev_num;
2356 console->constype = CONSTYPE_XENCONSOLED;
2357 if (state)
2358 console->build_state = state;
2361 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
2363 int rc = 0;
2364 uint32_t videoram;
2365 char *videoram_s = NULL;
2366 char *dompath = libxl_xs_get_dompath(ctx, domid);
2368 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
2369 if (!videoram_s)
2370 return -1;
2371 videoram = atoi(videoram_s);
2373 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
2374 if ((rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT)) != 0) return rc;
2375 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
2376 return rc;