debuggers.hg

view tools/libxl/libxl.c @ 20820:819c21064517

libxenlight: don't use the cloning logic in dm_xenstore_record_pid.

use call to lowlevel functions to do the same things.

Signed-off-by: Vincent Hanquez <vincent.hanquez@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jan 08 11:46:17 2010 +0000 (2010-01-08)
parents dda8dc485d67
children 9a49e6a66bf0
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, int version)
38 {
39 if (version != LIBXL_VERSION)
40 return ERROR_VERSION;
41 memset(ctx, 0, sizeof(struct libxl_ctx));
42 ctx->alloc_maxsize = 256;
43 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
44 if (!ctx->alloc_ptrs)
45 return ERROR_NOMEM;
47 ctx->xch = xc_interface_open();
48 if (ctx->xch == -1) {
49 free(ctx->alloc_ptrs);
50 return ERROR_FAIL;
51 }
53 ctx->xsh = xs_daemon_open();
54 if (!ctx->xsh) {
55 xc_interface_close(ctx->xch);
56 free(ctx->alloc_ptrs);
57 return ERROR_FAIL;
58 }
59 return 0;
60 }
62 int libxl_ctx_free(struct libxl_ctx *ctx)
63 {
64 libxl_free_all(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 = string_of_uuid(ctx, info->uuid);
95 if (!uuid_string) {
96 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate uuid string");
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 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
105 memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
107 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
108 if (ret < 0) {
109 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
110 return ERROR_FAIL;
111 }
113 dom_path = libxl_xs_get_dompath(ctx, *domid);
114 if (!dom_path)
115 return ERROR_FAIL;
117 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
118 vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
119 if (!vm_path || !vss_path) {
120 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
121 return ERROR_FAIL;
122 }
124 roperm[0].id = 0;
125 roperm[0].perms = XS_PERM_NONE;
126 roperm[1].id = *domid;
127 roperm[1].perms = XS_PERM_READ;
128 rwperm[0].id = *domid;
129 rwperm[0].perms = XS_PERM_NONE;
131 retry_transaction:
132 t = xs_transaction_start(ctx->xsh);
133 xs_rm(ctx->xsh, t, dom_path);
134 xs_mkdir(ctx->xsh, t, dom_path);
135 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
137 xs_rm(ctx->xsh, t, vm_path);
138 xs_mkdir(ctx->xsh, t, vm_path);
139 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
141 xs_rm(ctx->xsh, t, vss_path);
142 xs_mkdir(ctx->xsh, t, vss_path);
143 xs_set_permissions(ctx->xsh, t, vss_path, rwperm, ARRAY_SIZE(rwperm));
145 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
146 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
147 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", dom_path), info->name, strlen(info->name));
149 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
150 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
151 xs_mkdir(ctx->xsh, t, path);
152 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
153 libxl_free(ctx, path);
154 }
155 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
156 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
157 xs_mkdir(ctx->xsh, t, path);
158 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
159 libxl_free(ctx, path);
160 }
162 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
163 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
165 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
166 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
168 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
170 if (!xs_transaction_end(ctx->xsh, t, 0))
171 if (errno == EAGAIN)
172 goto retry_transaction;
173 return 0;
174 }
176 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
177 {
178 char **vments = NULL, **localents = NULL;
179 int i;
181 build_pre(ctx, domid, info, state);
182 if (info->hvm) {
183 build_hvm(ctx, domid, info, state);
184 vments = libxl_calloc(ctx, 5, sizeof(char *));
185 vments[0] = "rtc/timeoffset";
186 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
187 vments[2] = "image/ostype";
188 vments[3] = "hvm";
189 } else {
190 build_pv(ctx, domid, info, state);
191 vments = libxl_calloc(ctx, 9, sizeof(char *));
192 i = 0;
193 vments[i++] = "image/ostype";
194 vments[i++] = "linux";
195 vments[i++] = "image/kernel";
196 vments[i++] = (char*) info->kernel;
197 if (info->u.pv.ramdisk) {
198 vments[i++] = "image/ramdisk";
199 vments[i++] = (char*) info->u.pv.ramdisk;
200 }
201 if (info->u.pv.cmdline) {
202 vments[i++] = "image/cmdline";
203 vments[i++] = (char*) info->u.pv.cmdline;
204 }
205 }
206 build_post(ctx, domid, info, state, vments, localents);
207 return 0;
208 }
210 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
211 uint32_t domid, int fd, libxl_domain_build_state *state,
212 libxl_device_model_info *dm_info)
213 {
214 char **vments = NULL, **localents = NULL;
215 int i;
217 build_pre(ctx, domid, info, state);
218 restore_common(ctx, domid, info, state, fd);
219 if (info->hvm) {
220 vments = libxl_calloc(ctx, 5, sizeof(char *));
221 vments[0] = "rtc/timeoffset";
222 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
223 vments[2] = "image/ostype";
224 vments[3] = "hvm";
225 } else {
226 vments = libxl_calloc(ctx, 9, sizeof(char *));
227 i = 0;
228 vments[i++] = "image/ostype";
229 vments[i++] = "linux";
230 vments[i++] = "image/kernel";
231 vments[i++] = (char*) info->kernel;
232 if (info->u.pv.ramdisk) {
233 vments[i++] = "image/ramdisk";
234 vments[i++] = (char*) info->u.pv.ramdisk;
235 }
236 if (info->u.pv.cmdline) {
237 vments[i++] = "image/cmdline";
238 vments[i++] = (char*) info->u.pv.cmdline;
239 }
240 }
241 build_post(ctx, domid, info, state, vments, localents);
242 if (info->hvm)
243 asprintf(&(dm_info->saved_state), "/var/lib/xen/qemu-save.%d", domid);
244 else
245 dm_info->saved_state = NULL;
247 return 0;
248 }
250 int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
251 {
252 if (is_hvm(ctx, domid)) {
253 XL_LOG(ctx, XL_LOG_DEBUG, "Called domain_resume on "
254 "non-cooperative hvm domain %u", domid);
255 return ERROR_NI;
256 }
257 if (xc_domain_resume(ctx->xch, domid, 1)) {
258 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
259 "xc_domain_resume failed for domain %u",
260 domid);
261 return ERROR_FAIL;
262 }
263 if (!xs_resume_domain(ctx->xsh, domid)) {
264 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
265 "xs_resume_domain failed for domain %u",
266 domid);
267 return ERROR_FAIL;
268 }
269 return 0;
270 }
272 struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
273 {
274 struct libxl_dominfo *ptr;
275 int index, i, ret, first_domain;
276 xc_domaininfo_t info[16];
277 int size = 16;
279 first_domain = 1;
280 index = 0;
281 ptr = calloc(size, sizeof(struct libxl_dominfo));
282 if (!ptr)
283 return NULL;
284 redo:
285 ret = xc_domain_getinfolist(ctx->xch, first_domain, 16, info);
286 for (i = 0; i < ret; i++) {
287 if (index == size) {
288 struct libxl_dominfo *ptr2;
290 ptr2 = calloc(size * 2, sizeof(struct libxl_dominfo));
291 if (!ptr2) {
292 free(ptr);
293 return NULL;
294 }
295 memcpy(ptr2, ptr, sizeof(struct libxl_dominfo) * size);
296 free(ptr);
297 ptr = ptr2;
298 size *= 2;
299 }
300 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
301 ptr[index].domid = info[i].domain;
303 if (info[i].flags & XEN_DOMINF_dying)
304 ptr[index].dying = 1;
305 else if (info[i].flags & XEN_DOMINF_paused)
306 ptr[index].paused = 1;
307 else if (info[i].flags & XEN_DOMINF_blocked || info[i].flags & XEN_DOMINF_running)
308 ptr[index].running = 1;
310 first_domain = info[i].domain + 1;
311 index++;
312 }
313 if (ret == 16)
314 goto redo;
315 *nb_domain = index;
316 return ptr;
317 }
319 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
320 uint32_t domid, int fd)
321 {
322 int hvm = is_hvm(ctx, domid);
323 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
324 int debug = info != NULL && info->flags & XL_SUSPEND_LIVE;
326 core_suspend(ctx, domid, fd, hvm, live, debug);
327 if (hvm)
328 save_device_model(ctx, domid, fd);
329 return 0;
330 }
332 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
333 {
334 xc_domain_pause(ctx->xch, domid);
335 return 0;
336 }
338 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
339 {
340 char *path;
341 char *state;
343 if (is_hvm(ctx, domid)) {
344 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
345 state = libxl_xs_read(ctx, XBT_NULL, path);
346 if (state != NULL && !strcmp(state, "paused")) {
347 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue", strlen("continue"));
348 libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL);
349 }
350 }
351 xc_domain_unpause(ctx->xch, domid);
353 return 0;
354 }
356 static char *req_table[] = {
357 [0] = "poweroff",
358 [1] = "reboot",
359 [2] = "suspend",
360 [3] = "crash",
361 [4] = "halt",
362 };
364 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
365 {
366 char *shutdown_path;
367 char *dom_path;
369 if (req > ARRAY_SIZE(req_table))
370 return ERROR_INVAL;
372 dom_path = libxl_xs_get_dompath(ctx, domid);
373 if (!dom_path)
374 return ERROR_FAIL;
376 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
378 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
379 if (/* hvm */ 0) {
380 unsigned long acpi_s_state = 0;
381 unsigned long pvdriver = 0;
382 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
383 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
384 if (!pvdriver && acpi_s_state != 0)
385 xc_domain_shutdown(ctx->xch, domid, req);
386 }
387 return 0;
388 }
390 int libxl_get_wait_fd(struct libxl_ctx *ctx, int *fd)
391 {
392 *fd = xs_fileno(ctx->xsh);
393 return 0;
394 }
396 int libxl_wait_for_domain_death(struct libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
397 {
398 waiter->path = strdup("@releaseDomain");
399 asprintf(&(waiter->token), "%d", DOMAIN_DEATH);
400 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
401 return -1;
402 return 0;
403 }
405 int libxl_wait_for_disk_ejects(struct libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
406 {
407 int i;
408 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
410 if (!domid)
411 domid = guest_domid;
413 for (i = 0; i < num_disks; i++) {
414 asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject", libxl_xs_get_dompath(ctx, domid), device_disk_dev_number(disks[i].virtpath));
415 asprintf(&(waiter[i].token), "%d", DISK_EJECT);
416 xs_watch(ctx->xsh, waiter->path, waiter->token);
417 }
418 return 0;
419 }
421 int libxl_get_event(struct libxl_ctx *ctx, libxl_event *event)
422 {
423 unsigned int num;
424 char **events = xs_read_watch(ctx->xsh, &num);
425 if (num != 2) {
426 free(events);
427 return -1;
428 }
429 event->path = strdup(events[XS_WATCH_PATH]);
430 event->token = strdup(events[XS_WATCH_TOKEN]);
431 event->type = atoi(event->token);
432 free(events);
433 return 0;
434 }
436 int libxl_stop_waiting(struct libxl_ctx *ctx, libxl_waiter *waiter)
437 {
438 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
439 return -1;
440 else
441 return 0;
442 }
444 int libxl_free_event(libxl_event *event)
445 {
446 free(event->path);
447 free(event->token);
448 return 0;
449 }
451 int libxl_free_waiter(libxl_waiter *waiter)
452 {
453 free(waiter->path);
454 free(waiter->token);
455 return 0;
456 }
458 int libxl_event_get_domain_death_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, xc_domaininfo_t *info)
459 {
460 int rc = 0, ret;
462 if (event && event->type == DOMAIN_DEATH) {
463 ret = xc_domain_getinfolist(ctx->xch, domid, 1, info);
464 if (ret == 1 && info->domain == domid) {
465 if (info->flags & XEN_DOMINF_running ||
466 (!(info->flags & XEN_DOMINF_shutdown) && !(info->flags & XEN_DOMINF_dying)))
467 goto out;
468 rc = 1;
469 goto out;
470 }
471 memset(info, 0, sizeof(xc_dominfo_t));
472 rc = 1;
473 goto out;
474 }
475 out:
476 return rc;
477 }
479 int libxl_event_get_disk_eject_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
480 {
481 if (event && event->type == DISK_EJECT) {
482 char *path;
483 char *backend;
484 char *value = libxl_xs_read(ctx, XBT_NULL, event->path);
486 if (!value || strcmp(value, "eject"))
487 return 0;
489 path = strdup(event->path);
490 path[strlen(path) - 6] = '\0';
491 backend = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", path));
493 disk->backend_domid = 0;
494 disk->domid = domid;
495 disk->physpath = NULL;
496 disk->phystype = 0;
497 /* this value is returned to the user: do not free right away */
498 disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", backend));
499 disk->unpluggable = 1;
500 disk->readwrite = 0;
501 disk->is_cdrom = 1;
503 free(path);
504 return 1;
505 }
506 return 0;
507 }
509 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
510 {
511 char *pid;
512 int ret;
514 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
515 if (!pid) {
516 int stubdomid = libxl_get_stubdom_id(ctx, domid);
517 if (!stubdomid) {
518 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
519 return -1;
520 }
521 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
522 return libxl_domain_destroy(ctx, stubdomid, 0);
523 }
524 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
526 ret = kill(atoi(pid), SIGHUP);
527 if (ret < 0 && errno == ESRCH) {
528 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
529 ret = 0;
530 } else if (ret == 0) {
531 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
532 ret = 0;
533 } else {
534 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
535 atoi(pid));
536 }
537 return ret;
538 }
540 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
541 {
542 char *dom_path;
543 char *vm_path, *vss_path, *xapi_path;
544 int rc, dm_present;
546 if (is_hvm(ctx, domid)) {
547 dm_present = 1;
548 } else {
549 char *pid;
550 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
551 dm_present = (pid != NULL);
552 libxl_free(ctx, pid);
553 }
555 dom_path = libxl_xs_get_dompath(ctx, domid);
556 if (!dom_path)
557 return -1;
559 if (libxl_device_pci_shutdown(ctx, domid) < 0)
560 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
561 if (dm_present) {
562 xs_write(ctx->xsh, XBT_NULL,
563 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
564 "shutdown", strlen("shutdown"));
565 }
566 rc = xc_domain_pause(ctx->xch, domid);
567 if (rc < 0) {
568 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
569 return -1;
570 }
571 if (dm_present) {
572 if (libxl_destroy_device_model(ctx, domid) < 0)
573 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
574 }
575 if (libxl_devices_destroy(ctx, domid, force) < 0)
576 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
577 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
578 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
580 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", domid));
581 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
582 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
584 vss_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vss", domid));
585 if (!xs_rm(ctx->xsh, XBT_NULL, vss_path))
586 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vss_path);
588 xapi_path = libxl_sprintf(ctx, "/xapi/%u", domid);
589 if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
590 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
592 rc = xc_domain_destroy(ctx->xch, domid);
593 if (rc < 0) {
594 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
595 return -1;
596 }
597 return 0;
598 }
600 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num)
601 {
602 struct stat st;
603 const char *XENCONSOLE = "/usr/lib/xen/bin/xenconsole";
604 char *cmd;
606 if (stat(XENCONSOLE, &st) != 0) {
607 XL_LOG(ctx, XL_LOG_ERROR, "could not access %s", XENCONSOLE);
608 return ERROR_FAIL;
609 }
611 cmd = libxl_sprintf(ctx, "%s %d --num %d", XENCONSOLE, domid, cons_num);
612 return (system(cmd) != 0) ? ERROR_FAIL : 0;
613 }
615 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
616 libxl_device_model_info *info,
617 libxl_device_nic *vifs,
618 int num_vifs)
619 {
620 int num = 0, i;
621 flexarray_t *dm_args;
622 dm_args = flexarray_make(16, 1);
623 if (!dm_args)
624 return NULL;
626 flexarray_set(dm_args, num++, "qemu-dm");
627 flexarray_set(dm_args, num++, "-d");
629 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
631 if (info->dom_name) {
632 flexarray_set(dm_args, num++, "-domain-name");
633 flexarray_set(dm_args, num++, info->dom_name);
634 }
635 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
636 flexarray_set(dm_args, num++, "-vnc");
637 if (info->vncdisplay) {
638 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
639 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:%d", info->vnclisten, info->vncdisplay));
640 } else {
641 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
642 }
643 } else if (info->vnclisten) {
644 if (strchr(info->vnclisten, ':') != NULL) {
645 flexarray_set(dm_args, num++, info->vnclisten);
646 } else {
647 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
648 }
649 } else {
650 flexarray_set(dm_args, num++, "127.0.0.1:0");
651 }
652 if (info->vncunused) {
653 flexarray_set(dm_args, num++, "-vncunused");
654 }
655 }
656 if (info->sdl || info->opengl) {
657 flexarray_set(dm_args, num++, "-sdl");
658 if (info->opengl) {
659 flexarray_set(dm_args, num++, "-disable-opengl");
660 }
661 }
662 if (info->keymap) {
663 flexarray_set(dm_args, num++, "-k");
664 flexarray_set(dm_args, num++, info->keymap);
665 }
666 if (info->nographic && (!info->sdl && !info->vnc)) {
667 flexarray_set(dm_args, num++, "-nographic");
668 }
669 if (info->serial) {
670 flexarray_set(dm_args, num++, "-serial");
671 flexarray_set(dm_args, num++, info->serial);
672 }
673 if (info->type == XENFV) {
674 if (info->videoram) {
675 flexarray_set(dm_args, num++, "-videoram");
676 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
677 }
678 if (info->stdvga) {
679 flexarray_set(dm_args, num++, "-std-vga");
680 }
682 if (info->boot) {
683 flexarray_set(dm_args, num++, "-boot");
684 flexarray_set(dm_args, num++, info->boot);
685 }
686 if (info->usb) {
687 flexarray_set(dm_args, num++, "-usb");
688 if (info->usbdevice) {
689 flexarray_set(dm_args, num++, "-usbdevice");
690 flexarray_set(dm_args, num++, info->usbdevice);
691 }
692 }
693 if (info->apic) {
694 flexarray_set(dm_args, num++, "-acpi");
695 }
696 for (i = 0; i < num_vifs; i++) {
697 if (vifs[i].nictype == NICTYPE_IOEMU) {
698 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
699 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
700 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
701 if (!vifs[i].ifname)
702 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid - 1);
703 flexarray_set(dm_args, num++, "-net");
704 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
705 vifs[i].devid, smac, vifs[i].model));
706 flexarray_set(dm_args, num++, "-net");
707 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
708 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
709 }
710 }
711 }
712 if (info->saved_state) {
713 flexarray_set(dm_args, num++, "-loadvm");
714 flexarray_set(dm_args, num++, info->saved_state);
715 }
716 for (i = 0; info->extra && info->extra[i] != NULL; i++)
717 flexarray_set(dm_args, num++, info->extra[i]);
718 flexarray_set(dm_args, num++, "-M");
719 if (info->type == XENPV)
720 flexarray_set(dm_args, num++, "xenpv");
721 else
722 flexarray_set(dm_args, num++, "xenfv");
723 flexarray_set(dm_args, num++, NULL);
725 return (char **) flexarray_contents(dm_args);
726 }
728 void dm_xenstore_record_pid(struct libxl_ctx *ctx, void *for_spawn,
729 pid_t innerchild) {
730 struct libxl_device_model_starting *starting = for_spawn;
731 char *kvs[3];
732 int rc;
733 struct xs_handle *xsh;
735 xsh = xs_daemon_open();
736 /* we mustn't use the parent's handle in the child */
738 kvs[0] = "image/device-model-pid";
739 asprintf(&kvs[1], "%d", innerchild);
740 kvs[2] = NULL;
742 rc = xs_writev(xsh, XBT_NULL, starting->dom_path, kvs);
743 if (rc)
744 return;
745 xs_daemon_close(xsh);
746 }
748 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
749 libxl_device_model_info *info,
750 libxl_device_vfb *vfb,
751 libxl_device_vkb *vkb)
752 {
753 memset(vfb, 0x00, sizeof(libxl_device_vfb));
754 memset(vkb, 0x00, sizeof(libxl_device_vkb));
756 vfb->backend_domid = 0;
757 vfb->devid = 0;
758 vfb->vnc = info->vnc;
759 vfb->vnclisten = info->vnclisten;
760 vfb->vncdisplay = info->vncdisplay;
761 vfb->vncunused = info->vncunused;
762 vfb->keymap = info->keymap;
763 vfb->sdl = info->sdl;
764 vfb->opengl = info->opengl;
766 vkb->backend_domid = 0;
767 vkb->devid = 0;
768 return 0;
769 }
771 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
772 {
773 int i;
774 char *vm_path;
775 char *dmargs, *path;
776 int dmargs_size;
777 struct xs_permissions roperm[2];
778 xs_transaction_t t;
780 roperm[0].id = 0;
781 roperm[0].perms = XS_PERM_NONE;
782 roperm[1].id = domid;
783 roperm[1].perms = XS_PERM_READ;
785 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
787 i = 0;
788 dmargs_size = 0;
789 while (args[i] != NULL) {
790 dmargs_size = dmargs_size + strlen(args[i]) + 1;
791 i++;
792 }
793 dmargs_size++;
794 dmargs = (char *) malloc(dmargs_size);
795 i = 1;
796 dmargs[0] = '\0';
797 while (args[i] != NULL) {
798 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
799 strcat(dmargs, " ");
800 strcat(dmargs, args[i]);
801 }
802 i++;
803 }
804 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
806 retry_transaction:
807 t = xs_transaction_start(ctx->xsh);
808 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
809 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
810 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
811 if (!xs_transaction_end(ctx->xsh, t, 0))
812 if (errno == EAGAIN)
813 goto retry_transaction;
814 free(dmargs);
815 return 0;
816 }
818 static int libxl_create_stubdom(struct libxl_ctx *ctx,
819 libxl_device_model_info *info,
820 libxl_device_disk *disks, int num_disks,
821 libxl_device_nic *vifs, int num_vifs,
822 libxl_device_vfb *vfb,
823 libxl_device_vkb *vkb,
824 libxl_device_model_starting **starting_r)
825 {
826 int i, num_console = 1;
827 libxl_device_console *console;
828 libxl_domain_create_info c_info;
829 libxl_domain_build_info b_info;
830 libxl_domain_build_state state;
831 uint32_t domid;
832 char **args;
833 struct xs_permissions perm[2];
834 xs_transaction_t t;
835 libxl_device_model_starting *dm_starting = 0;
837 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
838 if (!args)
839 return ERROR_FAIL;
841 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
842 c_info.hvm = 0;
843 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
844 for (i = 0; i < 16; i++)
845 c_info.uuid[i] = info->uuid[i];
847 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
848 b_info.max_vcpus = 1;
849 b_info.max_memkb = 32 * 1024;
850 b_info.target_memkb = b_info.max_memkb;
851 b_info.kernel = "/usr/lib/xen/boot/ioemu-stubdom.gz";
852 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
853 b_info.u.pv.ramdisk = "";
854 b_info.u.pv.features = "";
855 b_info.hvm = 0;
857 libxl_domain_make(ctx, &c_info, &domid);
858 libxl_domain_build(ctx, &b_info, domid, &state);
860 libxl_write_dmargs(ctx, domid, info->domid, args);
861 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)), "%d", domid);
862 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)), "%d", info->domid);
863 xc_domain_set_target(ctx->xch, domid, info->domid);
864 xs_set_target(ctx->xsh, domid, info->domid);
866 perm[0].id = domid;
867 perm[0].perms = XS_PERM_NONE;
868 perm[1].id = info->domid;
869 perm[1].perms = XS_PERM_READ;
870 retry_transaction:
871 t = xs_transaction_start(ctx->xsh);
872 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
873 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
874 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
875 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
876 if (!xs_transaction_end(ctx->xsh, t, 0))
877 if (errno == EAGAIN)
878 goto retry_transaction;
880 for (i = 0; i < num_disks; i++) {
881 disks[i].domid = domid;
882 libxl_device_disk_add(ctx, domid, &disks[i]);
883 }
884 for (i = 0; i < num_vifs; i++) {
885 vifs[i].domid = domid;
886 libxl_device_nic_add(ctx, domid, &vifs[i]);
887 }
888 vfb->domid = domid;
889 libxl_device_vfb_add(ctx, domid, vfb);
890 vkb->domid = domid;
891 libxl_device_vkb_add(ctx, domid, vkb);
893 if (info->serial)
894 num_console++;
895 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
896 for (i = 0; i < num_console; i++) {
897 console[i].devid = i;
898 console[i].constype = CONSTYPE_IOEMU;
899 console[i].domid = domid;
900 if (!i)
901 console[i].build_state = &state;
902 libxl_device_console_add(ctx, domid, &console[i]);
903 }
904 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
905 free(args);
906 return -1;
907 }
908 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
909 free(args);
910 return -1;
911 }
913 libxl_domain_unpause(ctx, domid);
915 if (starting_r) {
916 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
917 (*starting_r)->domid = info->domid;
918 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
919 (*starting_r)->for_spawn = NULL;
920 }
922 free(args);
923 return 0;
924 }
926 int libxl_create_device_model(struct libxl_ctx *ctx,
927 libxl_device_model_info *info,
928 libxl_device_disk *disks, int num_disks,
929 libxl_device_nic *vifs, int num_vifs,
930 libxl_device_model_starting **starting_r)
931 {
932 char *path, *logfile;
933 int logfile_w, null;
934 int rc;
935 char **args;
936 struct libxl_device_model_starting buf_starting, *p;
938 if (strstr(info->device_model, "stubdom-dm")) {
939 libxl_device_vfb vfb;
940 libxl_device_vkb vkb;
942 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
943 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
944 }
946 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
947 if (!args)
948 return ERROR_FAIL;
950 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
951 xs_mkdir(ctx->xsh, XBT_NULL, path);
953 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
954 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
955 free(logfile);
956 null = open("/dev/null", O_RDONLY);
958 if (starting_r) {
959 rc = ERROR_NOMEM;
960 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
961 if (!*starting_r) goto xit;
962 p = *starting_r;
963 p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
964 } else {
965 p = &buf_starting;
966 p->for_spawn = NULL;
967 }
969 p->domid = info->domid;
970 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
971 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
973 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
974 if (rc < 0) goto xit;
975 if (!rc) { /* inner child */
976 libxl_exec(ctx, null, logfile_w, logfile_w,
977 info->device_model, args);
978 }
980 rc = 0;
981 xit:
982 free(args);
983 close(null);
984 close(logfile_w);
986 return rc;
987 }
989 int libxl_detach_device_model(struct libxl_ctx *ctx,
990 libxl_device_model_starting *starting)
991 {
992 int rc;
993 rc = libxl_spawn_detach(ctx, starting->for_spawn);
994 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
995 libxl_free(ctx, starting);
996 return rc;
997 }
1000 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
1001 libxl_device_model_starting *starting)
1003 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
1004 libxl_spawn_check,
1005 starting->for_spawn);
1006 int detach = libxl_detach_device_model(ctx, starting);
1007 return problem ? problem : detach;
1008 return 0;
1012 /******************************************************************************/
1014 static int is_blktap2_supported(void)
1016 char buf[1024];
1017 FILE *f = fopen("/proc/devices", "r");
1020 while (fgets(buf, sizeof(buf), f) != NULL) {
1021 if (strstr(buf, "blktap2")) {
1022 fclose(f);
1023 return 1;
1026 fclose(f);
1027 return 0;
1030 static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type)
1032 char buf[1024];
1033 char *p;
1034 int devnum;
1035 FILE *f = fopen("/sys/class/blktap2/devices", "r");
1038 while (!feof(f)) {
1039 fscanf(f, "%d %s", &devnum, buf);
1040 p = strchr(buf, ':');
1041 if (p == NULL)
1042 continue;
1043 p++;
1044 if (!strcmp(p, name) && !strncmp(buf, type, 3)) {
1045 fclose(f);
1046 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", devnum);
1049 fclose(f);
1050 return NULL;
1053 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1055 flexarray_t *front;
1056 flexarray_t *back;
1057 char *backend_type;
1058 unsigned int boffset = 0;
1059 unsigned int foffset = 0;
1060 int devid;
1061 libxl_device device;
1062 int major, minor;
1064 front = flexarray_make(16, 1);
1065 if (!front)
1066 return ERROR_NOMEM;
1067 back = flexarray_make(16, 1);
1068 if (!back) /* leaks front if error */
1069 return ERROR_NOMEM;
1071 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
1072 devid = device_disk_dev_number(disk->virtpath);
1074 device.backend_devid = devid;
1075 device.backend_domid = disk->backend_domid;
1076 device.devid = devid;
1077 device.domid = disk->domid;
1078 device.kind = DEVICE_VBD;
1080 switch (disk->phystype) {
1081 case PHYSTYPE_PHY: {
1083 device_physdisk_major_minor(disk->physpath, &major, &minor);
1084 flexarray_set(back, boffset++, "physical-device");
1085 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1087 flexarray_set(back, boffset++, "params");
1088 flexarray_set(back, boffset++, disk->physpath);
1090 device.backend_kind = DEVICE_VBD;
1091 break;
1093 case PHYSTYPE_FILE:
1094 /* let's pretend is tap:aio for the moment */
1095 disk->phystype = PHYSTYPE_AIO;
1096 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
1097 if (is_blktap2_supported()) {
1098 int rc, c, p[2], tot;
1099 char buf[1024], *dev;
1100 dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
1101 if (dev == NULL) {
1102 if (pipe(p) < 0) {
1103 XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
1104 return -1;
1106 rc = fork();
1107 if (rc < 0) {
1108 XL_LOG(ctx, XL_LOG_ERROR, "Failed to fork a new process");
1109 return -1;
1110 } else if (!rc) { /* child */
1111 int null_r, null_w;
1112 char *args[4];
1113 args[0] = "tapdisk2";
1114 args[1] = "-n";
1115 args[2] = libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath);
1116 args[3] = NULL;
1118 null_r = open("/dev/null", O_RDONLY);
1119 null_w = open("/dev/null", O_WRONLY);
1120 libxl_exec(ctx, null_r, p[1], null_w, "/usr/sbin/tapdisk2", args);
1121 XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
1123 close(p[1]);
1124 tot = 0;
1125 while ((c = read(p[0], buf + tot, sizeof(buf) - tot)) > 0)
1126 tot = tot + c;
1127 close(p[0]);
1128 buf[tot - 1] = '\0';
1129 dev = buf;
1131 flexarray_set(back, boffset++, "tapdisk-params");
1132 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
1133 flexarray_set(back, boffset++, "params");
1134 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", dev));
1135 backend_type = "phy";
1136 device_physdisk_major_minor(dev, &major, &minor);
1137 flexarray_set(back, boffset++, "physical-device");
1138 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1139 device.backend_kind = DEVICE_VBD;
1141 break;
1143 flexarray_set(back, boffset++, "params");
1144 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
1145 device_disk_string_of_phystype(disk->phystype), disk->physpath));
1147 device.backend_kind = DEVICE_TAP;
1148 break;
1151 flexarray_set(back, boffset++, "frontend-id");
1152 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
1153 flexarray_set(back, boffset++, "online");
1154 flexarray_set(back, boffset++, "1");
1155 flexarray_set(back, boffset++, "removable");
1156 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
1157 flexarray_set(back, boffset++, "bootable");
1158 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1159 flexarray_set(back, boffset++, "state");
1160 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1161 flexarray_set(back, boffset++, "dev");
1162 flexarray_set(back, boffset++, disk->virtpath);
1163 flexarray_set(back, boffset++, "type");
1164 flexarray_set(back, boffset++, backend_type);
1165 flexarray_set(back, boffset++, "mode");
1166 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1168 flexarray_set(front, foffset++, "backend-id");
1169 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
1170 flexarray_set(front, foffset++, "state");
1171 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1172 flexarray_set(front, foffset++, "virtual-device");
1173 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
1174 flexarray_set(front, foffset++, "device-type");
1175 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1177 if (0 /* protocol != native*/) {
1178 flexarray_set(front, foffset++, "protocol");
1179 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1182 libxl_device_generic_add(ctx, &device,
1183 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1184 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1185 flexarray_free(back);
1186 flexarray_free(front);
1187 return 0;
1190 int libxl_device_disk_del(struct libxl_ctx *ctx,
1191 libxl_device_disk *disk, int wait)
1193 libxl_device device;
1194 int devid;
1196 devid = device_disk_dev_number(disk->virtpath);
1197 device.backend_domid = disk->backend_domid;
1198 device.backend_devid = devid;
1199 device.backend_kind =
1200 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1201 device.domid = disk->domid;
1202 device.devid = devid;
1203 device.kind = DEVICE_VBD;
1204 return libxl_device_del(ctx, &device, wait);
1207 /******************************************************************************/
1208 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1210 flexarray_t *front;
1211 flexarray_t *back;
1212 unsigned int boffset = 0;
1213 unsigned int foffset = 0;
1214 libxl_device device;
1216 front = flexarray_make(16, 1);
1217 if (!front)
1218 return ERROR_NOMEM;
1219 back = flexarray_make(16, 1);
1220 if (!back)
1221 return ERROR_NOMEM;
1223 device.backend_devid = nic->devid;
1224 device.backend_domid = nic->backend_domid;
1225 device.backend_kind = DEVICE_VIF;
1226 device.devid = nic->devid;
1227 device.domid = nic->domid;
1228 device.kind = DEVICE_VIF;
1230 flexarray_set(back, boffset++, "frontend-id");
1231 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
1232 flexarray_set(back, boffset++, "online");
1233 flexarray_set(back, boffset++, "1");
1234 flexarray_set(back, boffset++, "state");
1235 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1236 flexarray_set(back, boffset++, "script");
1237 flexarray_set(back, boffset++, nic->script);
1238 flexarray_set(back, boffset++, "mac");
1239 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1240 nic->mac[0], nic->mac[1], nic->mac[2],
1241 nic->mac[3], nic->mac[4], nic->mac[5]));
1242 flexarray_set(back, boffset++, "handle");
1243 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
1245 flexarray_set(front, foffset++, "backend-id");
1246 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
1247 flexarray_set(front, foffset++, "state");
1248 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1249 flexarray_set(front, foffset++, "handle");
1250 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
1251 flexarray_set(front, foffset++, "mac");
1252 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1253 nic->mac[0], nic->mac[1], nic->mac[2],
1254 nic->mac[3], nic->mac[4], nic->mac[5]));
1255 if (0 /* protocol != native*/) {
1256 flexarray_set(front, foffset++, "protocol");
1257 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1260 libxl_device_generic_add(ctx, &device,
1261 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1262 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1264 /* FIXME: wait for plug */
1265 flexarray_free(back);
1266 flexarray_free(front);
1267 return 0;
1270 int libxl_device_nic_del(struct libxl_ctx *ctx,
1271 libxl_device_nic *nic, int wait)
1273 libxl_device device;
1275 device.backend_devid = nic->devid;
1276 device.backend_domid = nic->backend_domid;
1277 device.backend_kind = DEVICE_VIF;
1278 device.devid = nic->devid;
1279 device.domid = nic->domid;
1280 device.kind = DEVICE_VIF;
1282 return libxl_device_del(ctx, &device, wait);
1285 /******************************************************************************/
1286 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1288 flexarray_t *front;
1289 flexarray_t *back;
1290 unsigned int boffset = 0;
1291 unsigned int foffset = 0;
1292 libxl_device device;
1294 if (console->build_state) {
1295 xs_transaction_t t;
1296 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
1297 ents[0] = "console/port";
1298 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
1299 ents[2] = "console/ring-ref";
1300 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
1301 ents[4] = "console/limit";
1302 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
1303 ents[6] = "console/type";
1304 if (console->constype == CONSTYPE_XENCONSOLED)
1305 ents[7] = "xenconsoled";
1306 else
1307 ents[7] = "ioemu";
1308 retry_transaction:
1309 t = xs_transaction_start(ctx->xsh);
1310 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
1311 if (!xs_transaction_end(ctx->xsh, t, 0))
1312 if (errno == EAGAIN)
1313 goto retry_transaction;
1316 front = flexarray_make(16, 1);
1317 if (!front)
1318 return ERROR_NOMEM;
1319 back = flexarray_make(16, 1);
1320 if (!back)
1321 return ERROR_NOMEM;
1323 device.backend_devid = console->devid;
1324 device.backend_domid = console->backend_domid;
1325 device.backend_kind = DEVICE_CONSOLE;
1326 device.devid = console->devid;
1327 device.domid = console->domid;
1328 device.kind = DEVICE_CONSOLE;
1330 flexarray_set(back, boffset++, "frontend-id");
1331 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
1332 flexarray_set(back, boffset++, "online");
1333 flexarray_set(back, boffset++, "1");
1334 flexarray_set(back, boffset++, "state");
1335 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1336 flexarray_set(back, boffset++, "domain");
1337 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1338 flexarray_set(back, boffset++, "protocol");
1339 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
1341 flexarray_set(front, foffset++, "backend-id");
1342 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
1343 flexarray_set(front, foffset++, "state");
1344 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1345 flexarray_set(front, foffset++, "limit");
1346 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
1347 flexarray_set(front, foffset++, "protocol");
1348 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
1349 flexarray_set(front, foffset++, "type");
1350 if (console->constype == CONSTYPE_XENCONSOLED)
1351 flexarray_set(front, foffset++, "xenconsoled");
1352 else
1353 flexarray_set(front, foffset++, "ioemu");
1355 libxl_device_generic_add(ctx, &device,
1356 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1357 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1358 flexarray_free(back);
1359 flexarray_free(front);
1361 return 0;
1364 /******************************************************************************/
1365 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1367 flexarray_t *front;
1368 flexarray_t *back;
1369 unsigned int boffset = 0;
1370 unsigned int foffset = 0;
1371 libxl_device device;
1373 front = flexarray_make(16, 1);
1374 if (!front)
1375 return ERROR_NOMEM;
1376 back = flexarray_make(16, 1);
1377 if (!back)
1378 return ERROR_NOMEM;
1380 device.backend_devid = vkb->devid;
1381 device.backend_domid = vkb->backend_domid;
1382 device.backend_kind = DEVICE_VKBD;
1383 device.devid = vkb->devid;
1384 device.domid = vkb->domid;
1385 device.kind = DEVICE_VKBD;
1387 flexarray_set(back, boffset++, "frontend-id");
1388 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
1389 flexarray_set(back, boffset++, "online");
1390 flexarray_set(back, boffset++, "1");
1391 flexarray_set(back, boffset++, "state");
1392 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1393 flexarray_set(back, boffset++, "domain");
1394 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1396 flexarray_set(front, foffset++, "backend-id");
1397 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
1398 flexarray_set(front, foffset++, "state");
1399 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1401 libxl_device_generic_add(ctx, &device,
1402 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1403 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1404 flexarray_free(back);
1405 flexarray_free(front);
1407 return 0;
1410 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1412 return ERROR_NI;
1415 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1417 return ERROR_NI;
1420 libxl_device_disk *libxl_device_disk_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
1422 char *be_path_tap, *be_path_vbd;
1423 libxl_device_disk *disks = NULL;
1424 char **l = NULL;
1425 unsigned int numl;
1426 int num_disks = 0, i;
1427 char *type;
1429 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
1430 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
1432 l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
1433 if (l) {
1434 num_disks += numl;
1435 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1436 for (i = 0; i < numl; i++) {
1437 disks[i].backend_domid = 0;
1438 disks[i].domid = domid;
1439 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, l[i]));
1440 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));
1441 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, l[i]));
1442 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, l[i])));
1443 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, l[i])), "w"))
1444 disks[i].readwrite = 1;
1445 else
1446 disks[i].readwrite = 0;
1447 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]))));
1448 disks[i].is_cdrom = !strcmp(type, "cdrom");
1450 free(l);
1452 l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
1453 if (l) {
1454 num_disks += numl;
1455 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1456 for (i = 0; i < numl; i++) {
1457 disks[i].backend_domid = 0;
1458 disks[i].domid = domid;
1459 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, l[i]));
1460 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));
1461 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, l[i]));
1462 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, l[i])));
1463 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, l[i])), "w"))
1464 disks[i].readwrite = 1;
1465 else
1466 disks[i].readwrite = 0;
1467 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]))));
1468 disks[i].is_cdrom = !strcmp(type, "cdrom");
1470 free(l);
1472 *num = num_disks;
1473 return disks;
1476 int libxl_cdrom_insert(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1478 int num, i;
1479 uint32_t stubdomid;
1480 libxl_device_disk *disks;
1482 if (!disk->physpath) {
1483 disk->physpath = "";
1484 disk->phystype = PHYSTYPE_PHY;
1486 disks = libxl_device_disk_list(ctx, domid, &num);
1487 for (i = 0; i < num; i++) {
1488 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1489 /* found */
1490 break;
1492 if (i == num) {
1493 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
1494 return -1;
1496 libxl_device_disk_del(ctx, disks + i, 1);
1497 libxl_device_disk_add(ctx, domid, disk);
1498 stubdomid = libxl_get_stubdom_id(ctx, domid);
1499 if (stubdomid) {
1500 disks[i].domid = stubdomid;
1501 libxl_device_disk_del(ctx, disks + i, 1);
1502 disk->domid = stubdomid;
1503 libxl_device_disk_add(ctx, stubdomid, disk);
1504 disk->domid = domid;
1506 return 0;
1509 /******************************************************************************/
1510 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
1511 libxl_device_vfb *vfb,
1512 int num_console,
1513 libxl_device_console *console,
1514 libxl_device_model_info *info) {
1515 int i = 0, j = 0, num = 0;
1516 memset(info, 0x00, sizeof(libxl_device_model_info));
1518 info->vnc = vfb->vnc;
1519 if (vfb->vnclisten)
1520 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
1521 info->vncdisplay = vfb->vncdisplay;
1522 info->vncunused = vfb->vncunused;
1523 if (vfb->keymap)
1524 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
1525 info->sdl = vfb->sdl;
1526 info->opengl = vfb->opengl;
1527 for (i = 0; i < num_console; i++) {
1528 if (console->constype == CONSTYPE_IOEMU)
1529 num++;
1531 if (num > 0) {
1532 uint32_t guest_domid = libxl_is_stubdom(ctx, vfb->domid);
1533 if (guest_domid) {
1534 char *filename;
1535 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
1536 libxl_create_logfile(ctx, name, &filename);
1537 info->serial = libxl_sprintf(ctx, "file:%s", filename);
1538 free(filename);
1539 } else {
1540 info->serial = "pty";
1542 num--;
1544 if (num > 0) {
1545 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
1546 for (j = 0; j < num * 2; j = j + 2) {
1547 info->extra[j] = "-serial";
1548 info->extra[j + 1] = "pty";
1550 info->extra[j] = NULL;
1552 info->domid = vfb->domid;
1553 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
1554 info->device_model = "/usr/lib/xen/bin/qemu-dm";
1555 info->type = XENPV;
1556 return 0;
1559 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
1560 int num_console, libxl_device_console *console,
1561 struct libxl_device_model_starting **starting_r)
1563 libxl_device_model_info info;
1565 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
1566 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
1567 return 0;
1570 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1572 flexarray_t *front;
1573 flexarray_t *back;
1574 unsigned int boffset = 0;
1575 unsigned int foffset = 0;
1576 libxl_device device;
1578 front = flexarray_make(16, 1);
1579 if (!front)
1580 return ERROR_NOMEM;
1581 back = flexarray_make(16, 1);
1582 if (!back)
1583 return ERROR_NOMEM;
1585 device.backend_devid = vfb->devid;
1586 device.backend_domid = vfb->backend_domid;
1587 device.backend_kind = DEVICE_VFB;
1588 device.devid = vfb->devid;
1589 device.domid = vfb->domid;
1590 device.kind = DEVICE_VFB;
1592 flexarray_set(back, boffset++, "frontend-id");
1593 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
1594 flexarray_set(back, boffset++, "online");
1595 flexarray_set(back, boffset++, "1");
1596 flexarray_set(back, boffset++, "state");
1597 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1598 flexarray_set(back, boffset++, "domain");
1599 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1600 flexarray_set(back, boffset++, "vnc");
1601 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
1602 flexarray_set(back, boffset++, "vnclisten");
1603 flexarray_set(back, boffset++, vfb->vnclisten);
1604 flexarray_set(back, boffset++, "vncdisplay");
1605 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
1606 flexarray_set(back, boffset++, "vncunused");
1607 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
1608 flexarray_set(back, boffset++, "sdl");
1609 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
1610 flexarray_set(back, boffset++, "opengl");
1611 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
1612 if (vfb->xauthority) {
1613 flexarray_set(back, boffset++, "xauthority");
1614 flexarray_set(back, boffset++, vfb->xauthority);
1616 if (vfb->display) {
1617 flexarray_set(back, boffset++, "display");
1618 flexarray_set(back, boffset++, vfb->display);
1621 flexarray_set(front, foffset++, "backend-id");
1622 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
1623 flexarray_set(front, foffset++, "state");
1624 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1626 libxl_device_generic_add(ctx, &device,
1627 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1628 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1629 flexarray_free(front);
1630 flexarray_free(back);
1632 return 0;
1635 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1637 return ERROR_NI;
1640 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1642 return ERROR_NI;
1645 /******************************************************************************/
1647 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
1648 unsigned int bus, unsigned int dev,
1649 unsigned int func, unsigned int vdevfn)
1651 pcidev->domain = domain;
1652 pcidev->bus = bus;
1653 pcidev->dev = dev;
1654 pcidev->func = func;
1655 pcidev->vdevfn = vdevfn;
1656 return 0;
1659 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
1661 flexarray_t *front;
1662 flexarray_t *back;
1663 unsigned int boffset = 0;
1664 unsigned int foffset = 0;
1665 libxl_device device;
1666 int i;
1668 front = flexarray_make(16, 1);
1669 if (!front)
1670 return ERROR_NOMEM;
1671 back = flexarray_make(16, 1);
1672 if (!back)
1673 return ERROR_NOMEM;
1675 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
1677 /* add pci device */
1678 device.backend_devid = 0;
1679 device.backend_domid = 0;
1680 device.backend_kind = DEVICE_PCI;
1681 device.devid = 0;
1682 device.domid = domid;
1683 device.kind = DEVICE_PCI;
1685 flexarray_set(back, boffset++, "frontend-id");
1686 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
1687 flexarray_set(back, boffset++, "online");
1688 flexarray_set(back, boffset++, "1");
1689 flexarray_set(back, boffset++, "state");
1690 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1691 flexarray_set(back, boffset++, "domain");
1692 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1693 for (i = 0; i < num; i++) {
1694 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
1695 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1696 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
1697 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1698 if (pcidev->vdevfn) {
1699 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
1700 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1702 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
1703 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1704 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
1705 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1707 flexarray_set(back, boffset++, "num_devs");
1708 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
1710 flexarray_set(front, foffset++, "backend-id");
1711 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
1712 flexarray_set(front, foffset++, "state");
1713 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1715 libxl_device_generic_add(ctx, &device,
1716 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1717 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1719 flexarray_free(back);
1720 flexarray_free(front);
1721 return 0;
1724 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1726 flexarray_t *back;
1727 char *num_devs, *be_path;
1728 int num = 0;
1729 unsigned int boffset = 0;
1730 xs_transaction_t t;
1732 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1733 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
1734 if (!num_devs)
1735 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
1737 if (!is_hvm(ctx, domid)) {
1738 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
1739 return -1;
1742 back = flexarray_make(16, 1);
1743 if (!back)
1744 return ERROR_NOMEM;
1746 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
1747 num = atoi(num_devs);
1748 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
1749 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1750 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
1751 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1752 if (pcidev->vdevfn) {
1753 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
1754 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1756 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
1757 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1758 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
1759 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1760 flexarray_set(back, boffset++, "num_devs");
1761 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
1762 flexarray_set(back, boffset++, "state");
1763 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
1765 retry_transaction:
1766 t = xs_transaction_start(ctx->xsh);
1767 libxl_xs_writev(ctx, t, be_path,
1768 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
1769 if (!xs_transaction_end(ctx->xsh, t, 0))
1770 if (errno == EAGAIN)
1771 goto retry_transaction;
1773 flexarray_free(back);
1774 return 0;
1777 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1779 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
1780 int num, i, j;
1781 xs_transaction_t t;
1782 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
1784 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1785 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
1786 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
1787 if (!num_devs)
1788 return -1;
1789 num = atoi(num_devs);
1791 if (!is_hvm(ctx, domid)) {
1792 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1793 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1794 return -1;
1798 for (i = 0; i < num; i++) {
1799 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1800 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
1801 if (domain == pcidev->domain && bus == pcidev->bus &&
1802 pcidev->dev == dev && pcidev->func == func) {
1803 break;
1806 if (i == num) {
1807 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
1808 return -1;
1811 retry_transaction:
1812 t = xs_transaction_start(ctx->xsh);
1813 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
1814 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
1815 if (!xs_transaction_end(ctx->xsh, t, 0))
1816 if (errno == EAGAIN)
1817 goto retry_transaction;
1819 if (!is_hvm(ctx, domid)) {
1820 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1821 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1822 return -1;
1826 retry_transaction2:
1827 t = xs_transaction_start(ctx->xsh);
1828 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
1829 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
1830 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1831 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
1832 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
1833 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
1834 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
1835 for (j = i + 1; j < num; j++) {
1836 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
1837 tmp = libxl_xs_read(ctx, t, tmppath);
1838 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
1839 xs_rm(ctx->xsh, t, tmppath);
1840 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
1841 tmp = libxl_xs_read(ctx, t, tmppath);
1842 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
1843 xs_rm(ctx->xsh, t, tmppath);
1844 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
1845 tmp = libxl_xs_read(ctx, t, tmppath);
1846 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
1847 xs_rm(ctx->xsh, t, tmppath);
1848 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
1849 tmp = libxl_xs_read(ctx, t, tmppath);
1850 if (tmp) {
1851 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
1852 xs_rm(ctx->xsh, t, tmppath);
1854 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
1855 tmp = libxl_xs_read(ctx, t, tmppath);
1856 if (tmp) {
1857 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
1858 xs_rm(ctx->xsh, t, tmppath);
1860 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
1861 tmp = libxl_xs_read(ctx, t, tmppath);
1862 if (tmp) {
1863 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
1864 xs_rm(ctx->xsh, t, tmppath);
1867 if (!xs_transaction_end(ctx->xsh, t, 0))
1868 if (errno == EAGAIN)
1869 goto retry_transaction2;
1871 if (num == 1) {
1872 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
1873 libxl_device_destroy(ctx, be_path, 1);
1874 xs_rm(ctx->xsh, XBT_NULL, be_path);
1875 xs_rm(ctx->xsh, XBT_NULL, fe_path);
1876 return 0;
1879 return 0;
1882 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1884 char *path;
1885 char *state, *vdevfn;
1886 int rc, hvm;
1887 int stubdomid = 0;
1889 /* TODO: check if the device can be assigned */
1891 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
1893 stubdomid = libxl_get_stubdom_id(ctx, domid);
1894 if (stubdomid != 0) {
1895 libxl_device_pci pcidev_s = *pcidev;
1896 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
1899 hvm = is_hvm(ctx, domid);
1900 if (hvm) {
1901 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
1902 return -1;
1904 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
1905 state = libxl_xs_read(ctx, XBT_NULL, path);
1906 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
1907 if (pcidev->vdevfn)
1908 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
1909 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
1910 else
1911 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
1912 pcidev->bus, pcidev->dev, pcidev->func);
1913 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
1914 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
1915 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", 0,0) < 0)
1916 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
1917 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
1918 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
1919 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
1920 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
1921 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
1922 } else {
1923 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
1924 pcidev->bus, pcidev->dev, pcidev->func);
1925 FILE *f = fopen(sysfs_path, "r");
1926 unsigned int start = 0, end = 0, flags = 0, size = 0;
1927 int irq = 0;
1928 int i;
1930 if (f == NULL) {
1931 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
1932 return -1;
1934 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
1935 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
1936 size = end - start + 1;
1937 if (start) {
1938 if (flags & PCI_BAR_IO) {
1939 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
1940 if (rc < 0)
1941 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
1942 } else {
1943 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
1944 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
1945 if (rc < 0)
1946 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
1950 fclose(f);
1951 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
1952 pcidev->bus, pcidev->dev, pcidev->func);
1953 f = fopen(sysfs_path, "r");
1954 if (f == NULL) {
1955 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
1956 goto out;
1958 fscanf(f, "%u", &irq);
1959 if (irq) {
1960 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
1961 if (rc < 0) {
1962 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
1964 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
1965 if (rc < 0) {
1966 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
1969 fclose(f);
1971 out:
1972 if (!libxl_is_stubdom(ctx, domid)) {
1973 rc = xc_assign_device(ctx->xch, domid, pcidev->value);
1974 if (rc < 0)
1975 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
1978 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
1979 return 0;
1982 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1984 char *path;
1985 char *state;
1986 int hvm, rc;
1987 int stubdomid = 0;
1989 /* TODO: check if the device can be detached */
1990 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
1992 hvm = is_hvm(ctx, domid);
1993 if (hvm) {
1994 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
1995 return -1;
1997 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
1998 state = libxl_xs_read(ctx, XBT_NULL, path);
1999 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
2000 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
2001 pcidev->bus, pcidev->dev, pcidev->func);
2002 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
2003 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
2004 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", 0,0) < 0) {
2005 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
2006 return -1;
2008 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
2009 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
2010 } else {
2011 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
2012 pcidev->bus, pcidev->dev, pcidev->func);
2013 FILE *f = fopen(sysfs_path, "r");
2014 unsigned int start = 0, end = 0, flags = 0, size = 0;
2015 int irq = 0;
2016 int i;
2018 if (f == NULL) {
2019 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2020 goto skip1;
2022 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
2023 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
2024 size = end - start + 1;
2025 if (start) {
2026 if (flags & PCI_BAR_IO) {
2027 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
2028 if (rc < 0)
2029 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
2030 } else {
2031 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
2032 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
2033 if (rc < 0)
2034 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
2038 fclose(f);
2039 skip1:
2040 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
2041 pcidev->bus, pcidev->dev, pcidev->func);
2042 f = fopen(sysfs_path, "r");
2043 if (f == NULL) {
2044 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2045 goto out;
2047 fscanf(f, "%u", &irq);
2048 if (irq) {
2049 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
2050 if (rc < 0) {
2051 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
2053 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
2054 if (rc < 0) {
2055 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
2058 fclose(f);
2060 out:
2061 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
2063 if (!libxl_is_stubdom(ctx, domid)) {
2064 rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
2065 if (rc < 0)
2066 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
2069 stubdomid = libxl_get_stubdom_id(ctx, domid);
2070 if (stubdomid != 0) {
2071 libxl_device_pci pcidev_s = *pcidev;
2072 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
2075 return 0;
2078 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
2080 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
2081 int n, i;
2082 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
2083 libxl_device_pci *pcidevs;
2085 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
2086 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
2087 if (!num_devs) {
2088 *num = 0;
2089 return NULL;
2091 n = atoi(num_devs);
2092 pcidevs = calloc(n, sizeof(libxl_device_pci));
2093 *num = n;
2095 for (i = 0; i < n; i++) {
2096 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
2097 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
2098 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
2099 if (xsvdevfn)
2100 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
2101 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
2102 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
2103 if (xsopts) {
2104 char *saveptr;
2105 char *p = strtok_r(xsopts, ",=", &saveptr);
2106 do {
2107 while (*p == ' ')
2108 p++;
2109 if (!strcmp(p, "msitranslate")) {
2110 p = strtok_r(NULL, ",=", &saveptr);
2111 pcidevs[i].msitranslate = atoi(p);
2112 } else if (!strcmp(p, "power_mgmt")) {
2113 p = strtok_r(NULL, ",=", &saveptr);
2114 pcidevs[i].power_mgmt = atoi(p);
2116 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
2119 return pcidevs;
2122 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
2124 libxl_device_pci *pcidevs;
2125 int num, i;
2127 pcidevs = libxl_device_pci_list(ctx, domid, &num);
2128 for (i = 0; i < num; i++) {
2129 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
2130 return -1;
2132 free(pcidevs);
2133 return 0;
2136 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
2138 int rc = 0;
2139 uint32_t videoram;
2140 char *videoram_s = NULL;
2141 char *dompath = libxl_xs_get_dompath(ctx, domid);
2143 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
2144 if (!videoram_s)
2145 return -1;
2146 videoram = atoi(videoram_s);
2148 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
2149 rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT);
2150 if (rc != 0)
2151 return rc;
2152 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
2153 return rc;