2 * xen backend driver infrastructure
3 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; under version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
22 * TODO: add some xenbus / xenstore concepts overview here.
25 #include "qemu/osdep.h"
27 #include <sys/signal.h>
30 #include "sysemu/char.h"
32 #include "hw/xen/xen_backend.h"
34 #include <xen/grant_table.h>
36 /* ------------------------------------------------------------- */
39 xc_interface
*xen_xc
= NULL
;
40 xenforeignmemory_handle
*xen_fmem
= NULL
;
41 struct xs_handle
*xenstore
= NULL
;
42 const char *xen_protocol
;
47 QTAILQ_ENTRY(xs_dirs
) list
;
49 static QTAILQ_HEAD(xs_dirs_head
, xs_dirs
) xs_cleanup
=
50 QTAILQ_HEAD_INITIALIZER(xs_cleanup
);
52 static QTAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
= QTAILQ_HEAD_INITIALIZER(xendevs
);
55 /* ------------------------------------------------------------- */
57 static void xenstore_cleanup_dir(char *dir
)
61 d
= g_malloc(sizeof(*d
));
63 QTAILQ_INSERT_TAIL(&xs_cleanup
, d
, list
);
66 void xen_config_cleanup(void)
70 QTAILQ_FOREACH(d
, &xs_cleanup
, list
) {
71 xs_rm(xenstore
, 0, d
->xs_dir
);
75 int xenstore_write_str(const char *base
, const char *node
, const char *val
)
77 char abspath
[XEN_BUFSIZE
];
79 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
80 if (!xs_write(xenstore
, 0, abspath
, val
, strlen(val
))) {
86 char *xenstore_read_str(const char *base
, const char *node
)
88 char abspath
[XEN_BUFSIZE
];
90 char *str
, *ret
= NULL
;
92 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
93 str
= xs_read(xenstore
, 0, abspath
, &len
);
95 /* move to qemu-allocated memory to make sure
96 * callers can savely g_free() stuff. */
103 int xenstore_mkdir(char *path
, int p
)
105 struct xs_permissions perms
[2] = {
107 .id
= 0, /* set owner: dom0 */
114 if (!xs_mkdir(xenstore
, 0, path
)) {
115 xen_be_printf(NULL
, 0, "xs_mkdir %s: failed\n", path
);
118 xenstore_cleanup_dir(g_strdup(path
));
120 if (!xs_set_permissions(xenstore
, 0, path
, perms
, 2)) {
121 xen_be_printf(NULL
, 0, "xs_set_permissions %s: failed\n", path
);
127 int xenstore_write_int(const char *base
, const char *node
, int ival
)
131 snprintf(val
, sizeof(val
), "%d", ival
);
132 return xenstore_write_str(base
, node
, val
);
135 int xenstore_write_int64(const char *base
, const char *node
, int64_t ival
)
139 snprintf(val
, sizeof(val
), "%"PRId64
, ival
);
140 return xenstore_write_str(base
, node
, val
);
143 int xenstore_read_int(const char *base
, const char *node
, int *ival
)
148 val
= xenstore_read_str(base
, node
);
149 if (val
&& 1 == sscanf(val
, "%d", ival
)) {
156 int xenstore_read_uint64(const char *base
, const char *node
, uint64_t *uval
)
161 val
= xenstore_read_str(base
, node
);
162 if (val
&& 1 == sscanf(val
, "%"SCNu64
, uval
)) {
169 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
171 return xenstore_write_str(xendev
->be
, node
, val
);
174 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
176 return xenstore_write_int(xendev
->be
, node
, ival
);
179 int xenstore_write_be_int64(struct XenDevice
*xendev
, const char *node
, int64_t ival
)
181 return xenstore_write_int64(xendev
->be
, node
, ival
);
184 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
186 return xenstore_read_str(xendev
->be
, node
);
189 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
191 return xenstore_read_int(xendev
->be
, node
, ival
);
194 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
196 return xenstore_read_str(xendev
->fe
, node
);
199 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
201 return xenstore_read_int(xendev
->fe
, node
, ival
);
204 int xenstore_read_fe_uint64(struct XenDevice
*xendev
, const char *node
, uint64_t *uval
)
206 return xenstore_read_uint64(xendev
->fe
, node
, uval
);
209 /* ------------------------------------------------------------- */
211 const char *xenbus_strstate(enum xenbus_state state
)
213 static const char *const name
[] = {
214 [ XenbusStateUnknown
] = "Unknown",
215 [ XenbusStateInitialising
] = "Initialising",
216 [ XenbusStateInitWait
] = "InitWait",
217 [ XenbusStateInitialised
] = "Initialised",
218 [ XenbusStateConnected
] = "Connected",
219 [ XenbusStateClosing
] = "Closing",
220 [ XenbusStateClosed
] = "Closed",
222 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
225 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
229 rc
= xenstore_write_be_int(xendev
, "state", state
);
233 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
234 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
235 xendev
->be_state
= state
;
239 /* ------------------------------------------------------------- */
241 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
243 struct XenDevice
*xendev
;
245 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
246 if (xendev
->dom
!= dom
) {
249 if (xendev
->dev
!= dev
) {
252 if (strcmp(xendev
->type
, type
) != 0) {
261 * get xen backend device, allocate a new one if it doesn't exist.
263 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
264 struct XenDevOps
*ops
)
266 struct XenDevice
*xendev
;
268 xendev
= xen_be_find_xendev(type
, dom
, dev
);
273 /* init new xendev */
274 xendev
= g_malloc0(ops
->size
);
280 snprintf(xendev
->be
, sizeof(xendev
->be
), "backend/%s/%d/%d",
281 xendev
->type
, xendev
->dom
, xendev
->dev
);
282 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
283 xendev
->type
, xendev
->dev
);
285 xendev
->debug
= debug
;
286 xendev
->local_port
= -1;
288 xendev
->evtchndev
= xenevtchn_open(NULL
, 0);
289 if (xendev
->evtchndev
== NULL
) {
290 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
294 fcntl(xenevtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
296 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
297 xendev
->gnttabdev
= xengnttab_open(NULL
, 0);
298 if (xendev
->gnttabdev
== NULL
) {
299 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
300 xenevtchn_close(xendev
->evtchndev
);
305 xendev
->gnttabdev
= NULL
;
308 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
310 if (xendev
->ops
->alloc
) {
311 xendev
->ops
->alloc(xendev
);
318 * release xen backend device.
320 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
322 struct XenDevice
*xendev
, *xnext
;
325 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
326 * we save the next pointer in xnext because we might free xendev.
328 xnext
= xendevs
.tqh_first
;
331 xnext
= xendev
->next
.tqe_next
;
333 if (xendev
->dom
!= dom
) {
336 if (xendev
->dev
!= dev
&& dev
!= -1) {
340 if (xendev
->ops
->free
) {
341 xendev
->ops
->free(xendev
);
345 char token
[XEN_BUFSIZE
];
346 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
347 xs_unwatch(xenstore
, xendev
->fe
, token
);
351 if (xendev
->evtchndev
!= NULL
) {
352 xenevtchn_close(xendev
->evtchndev
);
354 if (xendev
->gnttabdev
!= NULL
) {
355 xengnttab_close(xendev
->gnttabdev
);
358 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
365 * Sync internal data structures on xenstore updates.
366 * Node specifies the changed field. node = NULL means
367 * update all fields (used for initialization).
369 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
371 if (node
== NULL
|| strcmp(node
, "online") == 0) {
372 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
378 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
379 if (xendev
->ops
->backend_changed
) {
380 xendev
->ops
->backend_changed(xendev
, node
);
385 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
389 if (node
== NULL
|| strcmp(node
, "state") == 0) {
390 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
391 fe_state
= XenbusStateUnknown
;
393 if (xendev
->fe_state
!= fe_state
) {
394 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
395 xenbus_strstate(xendev
->fe_state
),
396 xenbus_strstate(fe_state
));
398 xendev
->fe_state
= fe_state
;
400 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
401 g_free(xendev
->protocol
);
402 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
403 if (xendev
->protocol
) {
404 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
409 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
410 if (xendev
->ops
->frontend_changed
) {
411 xendev
->ops
->frontend_changed(xendev
, node
);
416 /* ------------------------------------------------------------- */
417 /* Check for possible state transitions and perform them. */
420 * Initial xendev setup. Read frontend path, register watch for it.
421 * Should succeed once xend finished setting up the backend device.
423 * Also sets initial state (-> Initializing) when done. Which
424 * only affects the xendev->be_state variable as xenbus should
425 * already be put into that state by xend.
427 static int xen_be_try_setup(struct XenDevice
*xendev
)
429 char token
[XEN_BUFSIZE
];
432 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
433 xen_be_printf(xendev
, 0, "reading backend state failed\n");
437 if (be_state
!= XenbusStateInitialising
) {
438 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
439 xenbus_strstate(be_state
));
443 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
444 if (xendev
->fe
== NULL
) {
445 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
449 /* setup frontend watch */
450 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
451 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
452 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
456 xen_be_set_state(xendev
, XenbusStateInitialising
);
458 xen_be_backend_changed(xendev
, NULL
);
459 xen_be_frontend_changed(xendev
, NULL
);
464 * Try initialize xendev. Prepare everything the backend can do
465 * without synchronizing with the frontend. Fakes hotplug-status. No
466 * hotplug involved here because this is about userspace drivers, thus
467 * there are kernel backend devices which could invoke hotplug.
469 * Goes to InitWait on success.
471 static int xen_be_try_init(struct XenDevice
*xendev
)
475 if (!xendev
->online
) {
476 xen_be_printf(xendev
, 1, "not online\n");
480 if (xendev
->ops
->init
) {
481 rc
= xendev
->ops
->init(xendev
);
484 xen_be_printf(xendev
, 1, "init() failed\n");
488 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
489 xen_be_set_state(xendev
, XenbusStateInitWait
);
494 * Try to initialise xendev. Depends on the frontend being ready
495 * for it (shared ring and evtchn info in xenstore, state being
496 * Initialised or Connected).
498 * Goes to Connected on success.
500 static int xen_be_try_initialise(struct XenDevice
*xendev
)
504 if (xendev
->fe_state
!= XenbusStateInitialised
&&
505 xendev
->fe_state
!= XenbusStateConnected
) {
506 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
507 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
509 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
514 if (xendev
->ops
->initialise
) {
515 rc
= xendev
->ops
->initialise(xendev
);
518 xen_be_printf(xendev
, 0, "initialise() failed\n");
522 xen_be_set_state(xendev
, XenbusStateConnected
);
527 * Try to let xendev know that it is connected. Depends on the
528 * frontend being Connected. Note that this may be called more
529 * than once since the backend state is not modified.
531 static void xen_be_try_connected(struct XenDevice
*xendev
)
533 if (!xendev
->ops
->connected
) {
537 if (xendev
->fe_state
!= XenbusStateConnected
) {
538 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
539 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
541 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
546 xendev
->ops
->connected(xendev
);
550 * Teardown connection.
552 * Goes to Closed when done.
554 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
556 if (xendev
->be_state
!= XenbusStateClosing
&&
557 xendev
->be_state
!= XenbusStateClosed
&&
558 xendev
->ops
->disconnect
) {
559 xendev
->ops
->disconnect(xendev
);
561 if (xendev
->be_state
!= state
) {
562 xen_be_set_state(xendev
, state
);
567 * Try to reset xendev, for reconnection by another frontend instance.
569 static int xen_be_try_reset(struct XenDevice
*xendev
)
571 if (xendev
->fe_state
!= XenbusStateInitialising
) {
575 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
576 xen_be_set_state(xendev
, XenbusStateInitialising
);
581 * state change dispatcher function
583 void xen_be_check_state(struct XenDevice
*xendev
)
587 /* frontend may request shutdown from almost anywhere */
588 if (xendev
->fe_state
== XenbusStateClosing
||
589 xendev
->fe_state
== XenbusStateClosed
) {
590 xen_be_disconnect(xendev
, xendev
->fe_state
);
594 /* check for possible backend state transitions */
596 switch (xendev
->be_state
) {
597 case XenbusStateUnknown
:
598 rc
= xen_be_try_setup(xendev
);
600 case XenbusStateInitialising
:
601 rc
= xen_be_try_init(xendev
);
603 case XenbusStateInitWait
:
604 rc
= xen_be_try_initialise(xendev
);
606 case XenbusStateConnected
:
607 /* xendev->be_state doesn't change */
608 xen_be_try_connected(xendev
);
611 case XenbusStateClosed
:
612 rc
= xen_be_try_reset(xendev
);
623 /* ------------------------------------------------------------- */
625 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
627 struct XenDevice
*xendev
;
628 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
630 unsigned int cdev
, j
;
633 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
634 snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
635 if (!xs_watch(xenstore
, path
, token
)) {
636 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
640 /* look for backends */
641 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
645 for (j
= 0; j
< cdev
; j
++) {
646 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
647 if (xendev
== NULL
) {
650 xen_be_check_state(xendev
);
656 static void xenstore_update_be(char *watch
, char *type
, int dom
,
657 struct XenDevOps
*ops
)
659 struct XenDevice
*xendev
;
660 char path
[XEN_BUFSIZE
], *bepath
;
661 unsigned int len
, dev
;
663 len
= snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
664 if (strncmp(path
, watch
, len
) != 0) {
667 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
669 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
677 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
678 if (xendev
!= NULL
) {
679 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
680 if (bepath
== NULL
) {
681 xen_be_del_xendev(dom
, dev
);
684 xen_be_backend_changed(xendev
, path
);
685 xen_be_check_state(xendev
);
690 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
695 len
= strlen(xendev
->fe
);
696 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
699 if (watch
[len
] != '/') {
702 node
= watch
+ len
+ 1;
704 xen_be_frontend_changed(xendev
, node
);
705 xen_be_check_state(xendev
);
708 static void xenstore_update(void *unused
)
711 intptr_t type
, ops
, ptr
;
712 unsigned int dom
, count
;
714 vec
= xs_read_watch(xenstore
, &count
);
719 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
720 &type
, &dom
, &ops
) == 3) {
721 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
723 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
724 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
731 static void xen_be_evtchn_event(void *opaque
)
733 struct XenDevice
*xendev
= opaque
;
736 port
= xenevtchn_pending(xendev
->evtchndev
);
737 if (port
!= xendev
->local_port
) {
738 xen_be_printf(xendev
, 0,
739 "xenevtchn_pending returned %d (expected %d)\n",
740 port
, xendev
->local_port
);
743 xenevtchn_unmask(xendev
->evtchndev
, port
);
745 if (xendev
->ops
->event
) {
746 xendev
->ops
->event(xendev
);
750 /* -------------------------------------------------------------------- */
752 int xen_be_init(void)
754 xenstore
= xs_daemon_open();
756 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
760 qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
);
762 if (xen_xc
== NULL
|| xen_fmem
== NULL
) {
763 /* Check if xen_init() have been called */
769 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
770 xs_daemon_close(xenstore
);
776 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
781 if (ops
->backend_register
) {
782 rc
= ops
->backend_register();
788 snprintf(path
, sizeof(path
), "device-model/%u/backends/%s", xen_domid
,
790 xenstore_mkdir(path
, XS_PERM_NONE
);
792 return xenstore_scan(type
, xen_domid
, ops
);
795 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
797 if (xendev
->local_port
!= -1) {
800 xendev
->local_port
= xenevtchn_bind_interdomain
801 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
802 if (xendev
->local_port
== -1) {
803 xen_be_printf(xendev
, 0, "xenevtchn_bind_interdomain failed\n");
806 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
807 qemu_set_fd_handler(xenevtchn_fd(xendev
->evtchndev
),
808 xen_be_evtchn_event
, NULL
, xendev
);
812 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
814 if (xendev
->local_port
== -1) {
817 qemu_set_fd_handler(xenevtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
818 xenevtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
819 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
820 xendev
->local_port
= -1;
823 int xen_be_send_notify(struct XenDevice
*xendev
)
825 return xenevtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
830 * 0 == errors (stderr + logfile).
831 * 1 == informative debug messages (logfile only).
832 * 2 == noisy debug messages (logfile only).
833 * 3 == will flood your log (logfile only).
835 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
840 if (msg_level
> xendev
->debug
) {
843 qemu_log("xen be: %s: ", xendev
->name
);
844 if (msg_level
== 0) {
845 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
848 if (msg_level
> debug
) {
851 qemu_log("xen be core: ");
852 if (msg_level
== 0) {
853 fprintf(stderr
, "xen be core: ");
857 qemu_log_vprintf(fmt
, args
);
859 if (msg_level
== 0) {
861 vfprintf(stderr
, fmt
, args
);