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
;
45 static QTAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
= QTAILQ_HEAD_INITIALIZER(xendevs
);
48 /* ------------------------------------------------------------- */
50 int xenstore_write_str(const char *base
, const char *node
, const char *val
)
52 char abspath
[XEN_BUFSIZE
];
54 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
55 if (!xs_write(xenstore
, 0, abspath
, val
, strlen(val
))) {
61 char *xenstore_read_str(const char *base
, const char *node
)
63 char abspath
[XEN_BUFSIZE
];
65 char *str
, *ret
= NULL
;
67 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
68 str
= xs_read(xenstore
, 0, abspath
, &len
);
70 /* move to qemu-allocated memory to make sure
71 * callers can savely g_free() stuff. */
78 int xenstore_write_int(const char *base
, const char *node
, int ival
)
82 snprintf(val
, sizeof(val
), "%d", ival
);
83 return xenstore_write_str(base
, node
, val
);
86 int xenstore_write_int64(const char *base
, const char *node
, int64_t ival
)
90 snprintf(val
, sizeof(val
), "%"PRId64
, ival
);
91 return xenstore_write_str(base
, node
, val
);
94 int xenstore_read_int(const char *base
, const char *node
, int *ival
)
99 val
= xenstore_read_str(base
, node
);
100 if (val
&& 1 == sscanf(val
, "%d", ival
)) {
107 int xenstore_read_uint64(const char *base
, const char *node
, uint64_t *uval
)
112 val
= xenstore_read_str(base
, node
);
113 if (val
&& 1 == sscanf(val
, "%"SCNu64
, uval
)) {
120 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
122 return xenstore_write_str(xendev
->be
, node
, val
);
125 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
127 return xenstore_write_int(xendev
->be
, node
, ival
);
130 int xenstore_write_be_int64(struct XenDevice
*xendev
, const char *node
, int64_t ival
)
132 return xenstore_write_int64(xendev
->be
, node
, ival
);
135 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
137 return xenstore_read_str(xendev
->be
, node
);
140 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
142 return xenstore_read_int(xendev
->be
, node
, ival
);
145 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
147 return xenstore_read_str(xendev
->fe
, node
);
150 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
152 return xenstore_read_int(xendev
->fe
, node
, ival
);
155 int xenstore_read_fe_uint64(struct XenDevice
*xendev
, const char *node
, uint64_t *uval
)
157 return xenstore_read_uint64(xendev
->fe
, node
, uval
);
160 /* ------------------------------------------------------------- */
162 const char *xenbus_strstate(enum xenbus_state state
)
164 static const char *const name
[] = {
165 [ XenbusStateUnknown
] = "Unknown",
166 [ XenbusStateInitialising
] = "Initialising",
167 [ XenbusStateInitWait
] = "InitWait",
168 [ XenbusStateInitialised
] = "Initialised",
169 [ XenbusStateConnected
] = "Connected",
170 [ XenbusStateClosing
] = "Closing",
171 [ XenbusStateClosed
] = "Closed",
173 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
176 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
180 rc
= xenstore_write_be_int(xendev
, "state", state
);
184 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
185 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
186 xendev
->be_state
= state
;
190 /* ------------------------------------------------------------- */
192 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
194 struct XenDevice
*xendev
;
196 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
197 if (xendev
->dom
!= dom
) {
200 if (xendev
->dev
!= dev
) {
203 if (strcmp(xendev
->type
, type
) != 0) {
212 * get xen backend device, allocate a new one if it doesn't exist.
214 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
215 struct XenDevOps
*ops
)
217 struct XenDevice
*xendev
;
219 xendev
= xen_be_find_xendev(type
, dom
, dev
);
224 /* init new xendev */
225 xendev
= g_malloc0(ops
->size
);
231 snprintf(xendev
->be
, sizeof(xendev
->be
), "backend/%s/%d/%d",
232 xendev
->type
, xendev
->dom
, xendev
->dev
);
233 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
234 xendev
->type
, xendev
->dev
);
236 xendev
->debug
= debug
;
237 xendev
->local_port
= -1;
239 xendev
->evtchndev
= xenevtchn_open(NULL
, 0);
240 if (xendev
->evtchndev
== NULL
) {
241 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
245 fcntl(xenevtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
247 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
248 xendev
->gnttabdev
= xengnttab_open(NULL
, 0);
249 if (xendev
->gnttabdev
== NULL
) {
250 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
251 xenevtchn_close(xendev
->evtchndev
);
256 xendev
->gnttabdev
= NULL
;
259 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
261 if (xendev
->ops
->alloc
) {
262 xendev
->ops
->alloc(xendev
);
269 * release xen backend device.
271 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
273 struct XenDevice
*xendev
, *xnext
;
276 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
277 * we save the next pointer in xnext because we might free xendev.
279 xnext
= xendevs
.tqh_first
;
282 xnext
= xendev
->next
.tqe_next
;
284 if (xendev
->dom
!= dom
) {
287 if (xendev
->dev
!= dev
&& dev
!= -1) {
291 if (xendev
->ops
->free
) {
292 xendev
->ops
->free(xendev
);
296 char token
[XEN_BUFSIZE
];
297 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
298 xs_unwatch(xenstore
, xendev
->fe
, token
);
302 if (xendev
->evtchndev
!= NULL
) {
303 xenevtchn_close(xendev
->evtchndev
);
305 if (xendev
->gnttabdev
!= NULL
) {
306 xengnttab_close(xendev
->gnttabdev
);
309 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
316 * Sync internal data structures on xenstore updates.
317 * Node specifies the changed field. node = NULL means
318 * update all fields (used for initialization).
320 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
322 if (node
== NULL
|| strcmp(node
, "online") == 0) {
323 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
329 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
330 if (xendev
->ops
->backend_changed
) {
331 xendev
->ops
->backend_changed(xendev
, node
);
336 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
340 if (node
== NULL
|| strcmp(node
, "state") == 0) {
341 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
342 fe_state
= XenbusStateUnknown
;
344 if (xendev
->fe_state
!= fe_state
) {
345 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
346 xenbus_strstate(xendev
->fe_state
),
347 xenbus_strstate(fe_state
));
349 xendev
->fe_state
= fe_state
;
351 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
352 g_free(xendev
->protocol
);
353 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
354 if (xendev
->protocol
) {
355 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
360 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
361 if (xendev
->ops
->frontend_changed
) {
362 xendev
->ops
->frontend_changed(xendev
, node
);
367 /* ------------------------------------------------------------- */
368 /* Check for possible state transitions and perform them. */
371 * Initial xendev setup. Read frontend path, register watch for it.
372 * Should succeed once xend finished setting up the backend device.
374 * Also sets initial state (-> Initializing) when done. Which
375 * only affects the xendev->be_state variable as xenbus should
376 * already be put into that state by xend.
378 static int xen_be_try_setup(struct XenDevice
*xendev
)
380 char token
[XEN_BUFSIZE
];
383 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
384 xen_be_printf(xendev
, 0, "reading backend state failed\n");
388 if (be_state
!= XenbusStateInitialising
) {
389 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
390 xenbus_strstate(be_state
));
394 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
395 if (xendev
->fe
== NULL
) {
396 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
400 /* setup frontend watch */
401 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
402 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
403 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
407 xen_be_set_state(xendev
, XenbusStateInitialising
);
409 xen_be_backend_changed(xendev
, NULL
);
410 xen_be_frontend_changed(xendev
, NULL
);
415 * Try initialize xendev. Prepare everything the backend can do
416 * without synchronizing with the frontend. Fakes hotplug-status. No
417 * hotplug involved here because this is about userspace drivers, thus
418 * there are kernel backend devices which could invoke hotplug.
420 * Goes to InitWait on success.
422 static int xen_be_try_init(struct XenDevice
*xendev
)
426 if (!xendev
->online
) {
427 xen_be_printf(xendev
, 1, "not online\n");
431 if (xendev
->ops
->init
) {
432 rc
= xendev
->ops
->init(xendev
);
435 xen_be_printf(xendev
, 1, "init() failed\n");
439 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
440 xen_be_set_state(xendev
, XenbusStateInitWait
);
445 * Try to initialise xendev. Depends on the frontend being ready
446 * for it (shared ring and evtchn info in xenstore, state being
447 * Initialised or Connected).
449 * Goes to Connected on success.
451 static int xen_be_try_initialise(struct XenDevice
*xendev
)
455 if (xendev
->fe_state
!= XenbusStateInitialised
&&
456 xendev
->fe_state
!= XenbusStateConnected
) {
457 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
458 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
460 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
465 if (xendev
->ops
->initialise
) {
466 rc
= xendev
->ops
->initialise(xendev
);
469 xen_be_printf(xendev
, 0, "initialise() failed\n");
473 xen_be_set_state(xendev
, XenbusStateConnected
);
478 * Try to let xendev know that it is connected. Depends on the
479 * frontend being Connected. Note that this may be called more
480 * than once since the backend state is not modified.
482 static void xen_be_try_connected(struct XenDevice
*xendev
)
484 if (!xendev
->ops
->connected
) {
488 if (xendev
->fe_state
!= XenbusStateConnected
) {
489 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
490 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
492 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
497 xendev
->ops
->connected(xendev
);
501 * Teardown connection.
503 * Goes to Closed when done.
505 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
507 if (xendev
->be_state
!= XenbusStateClosing
&&
508 xendev
->be_state
!= XenbusStateClosed
&&
509 xendev
->ops
->disconnect
) {
510 xendev
->ops
->disconnect(xendev
);
512 if (xendev
->be_state
!= state
) {
513 xen_be_set_state(xendev
, state
);
518 * Try to reset xendev, for reconnection by another frontend instance.
520 static int xen_be_try_reset(struct XenDevice
*xendev
)
522 if (xendev
->fe_state
!= XenbusStateInitialising
) {
526 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
527 xen_be_set_state(xendev
, XenbusStateInitialising
);
532 * state change dispatcher function
534 void xen_be_check_state(struct XenDevice
*xendev
)
538 /* frontend may request shutdown from almost anywhere */
539 if (xendev
->fe_state
== XenbusStateClosing
||
540 xendev
->fe_state
== XenbusStateClosed
) {
541 xen_be_disconnect(xendev
, xendev
->fe_state
);
545 /* check for possible backend state transitions */
547 switch (xendev
->be_state
) {
548 case XenbusStateUnknown
:
549 rc
= xen_be_try_setup(xendev
);
551 case XenbusStateInitialising
:
552 rc
= xen_be_try_init(xendev
);
554 case XenbusStateInitWait
:
555 rc
= xen_be_try_initialise(xendev
);
557 case XenbusStateConnected
:
558 /* xendev->be_state doesn't change */
559 xen_be_try_connected(xendev
);
562 case XenbusStateClosed
:
563 rc
= xen_be_try_reset(xendev
);
574 /* ------------------------------------------------------------- */
576 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
578 struct XenDevice
*xendev
;
579 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
581 unsigned int cdev
, j
;
584 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
585 snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
586 if (!xs_watch(xenstore
, path
, token
)) {
587 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
591 /* look for backends */
592 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
596 for (j
= 0; j
< cdev
; j
++) {
597 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
598 if (xendev
== NULL
) {
601 xen_be_check_state(xendev
);
607 static void xenstore_update_be(char *watch
, char *type
, int dom
,
608 struct XenDevOps
*ops
)
610 struct XenDevice
*xendev
;
611 char path
[XEN_BUFSIZE
], *bepath
;
612 unsigned int len
, dev
;
614 len
= snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
615 if (strncmp(path
, watch
, len
) != 0) {
618 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
620 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
628 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
629 if (xendev
!= NULL
) {
630 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
631 if (bepath
== NULL
) {
632 xen_be_del_xendev(dom
, dev
);
635 xen_be_backend_changed(xendev
, path
);
636 xen_be_check_state(xendev
);
641 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
646 len
= strlen(xendev
->fe
);
647 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
650 if (watch
[len
] != '/') {
653 node
= watch
+ len
+ 1;
655 xen_be_frontend_changed(xendev
, node
);
656 xen_be_check_state(xendev
);
659 static void xenstore_update(void *unused
)
662 intptr_t type
, ops
, ptr
;
663 unsigned int dom
, count
;
665 vec
= xs_read_watch(xenstore
, &count
);
670 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
671 &type
, &dom
, &ops
) == 3) {
672 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
674 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
675 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
682 static void xen_be_evtchn_event(void *opaque
)
684 struct XenDevice
*xendev
= opaque
;
687 port
= xenevtchn_pending(xendev
->evtchndev
);
688 if (port
!= xendev
->local_port
) {
689 xen_be_printf(xendev
, 0,
690 "xenevtchn_pending returned %d (expected %d)\n",
691 port
, xendev
->local_port
);
694 xenevtchn_unmask(xendev
->evtchndev
, port
);
696 if (xendev
->ops
->event
) {
697 xendev
->ops
->event(xendev
);
701 /* -------------------------------------------------------------------- */
703 int xen_be_init(void)
705 xenstore
= xs_daemon_open();
707 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
711 qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
);
713 if (xen_xc
== NULL
|| xen_fmem
== NULL
) {
714 /* Check if xen_init() have been called */
720 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
721 xs_daemon_close(xenstore
);
727 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
729 return xenstore_scan(type
, xen_domid
, ops
);
732 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
734 if (xendev
->local_port
!= -1) {
737 xendev
->local_port
= xenevtchn_bind_interdomain
738 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
739 if (xendev
->local_port
== -1) {
740 xen_be_printf(xendev
, 0, "xenevtchn_bind_interdomain failed\n");
743 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
744 qemu_set_fd_handler(xenevtchn_fd(xendev
->evtchndev
),
745 xen_be_evtchn_event
, NULL
, xendev
);
749 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
751 if (xendev
->local_port
== -1) {
754 qemu_set_fd_handler(xenevtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
755 xenevtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
756 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
757 xendev
->local_port
= -1;
760 int xen_be_send_notify(struct XenDevice
*xendev
)
762 return xenevtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
767 * 0 == errors (stderr + logfile).
768 * 1 == informative debug messages (logfile only).
769 * 2 == noisy debug messages (logfile only).
770 * 3 == will flood your log (logfile only).
772 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
777 if (msg_level
> xendev
->debug
) {
780 qemu_log("xen be: %s: ", xendev
->name
);
781 if (msg_level
== 0) {
782 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
785 if (msg_level
> debug
) {
788 qemu_log("xen be core: ");
789 if (msg_level
== 0) {
790 fprintf(stderr
, "xen be core: ");
794 qemu_log_vprintf(fmt
, args
);
796 if (msg_level
== 0) {
798 vfprintf(stderr
, fmt
, args
);