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.
32 #include <sys/types.h>
35 #include <sys/signal.h>
38 #include "sysemu/char.h"
40 #include "hw/xen/xen_backend.h"
42 #include <xen/grant_table.h>
44 /* ------------------------------------------------------------- */
47 XenXC xen_xc
= XC_HANDLER_INITIAL_VALUE
;
48 XenGnttab xen_xcg
= XC_HANDLER_INITIAL_VALUE
;
49 struct xs_handle
*xenstore
= NULL
;
50 const char *xen_protocol
;
53 static QTAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
= QTAILQ_HEAD_INITIALIZER(xendevs
);
56 /* ------------------------------------------------------------- */
58 int xenstore_write_str(const char *base
, const char *node
, const char *val
)
60 char abspath
[XEN_BUFSIZE
];
62 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
63 if (!xs_write(xenstore
, 0, abspath
, val
, strlen(val
))) {
69 char *xenstore_read_str(const char *base
, const char *node
)
71 char abspath
[XEN_BUFSIZE
];
73 char *str
, *ret
= NULL
;
75 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
76 str
= xs_read(xenstore
, 0, abspath
, &len
);
78 /* move to qemu-allocated memory to make sure
79 * callers can savely g_free() stuff. */
86 int xenstore_write_int(const char *base
, const char *node
, int ival
)
90 snprintf(val
, sizeof(val
), "%d", ival
);
91 return xenstore_write_str(base
, node
, val
);
94 int xenstore_write_int64(const char *base
, const char *node
, int64_t ival
)
98 snprintf(val
, sizeof(val
), "%"PRId64
, ival
);
99 return xenstore_write_str(base
, node
, val
);
102 int xenstore_read_int(const char *base
, const char *node
, int *ival
)
107 val
= xenstore_read_str(base
, node
);
108 if (val
&& 1 == sscanf(val
, "%d", ival
)) {
115 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
117 return xenstore_write_str(xendev
->be
, node
, val
);
120 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
122 return xenstore_write_int(xendev
->be
, node
, ival
);
125 int xenstore_write_be_int64(struct XenDevice
*xendev
, const char *node
, int64_t ival
)
127 return xenstore_write_int64(xendev
->be
, node
, ival
);
130 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
132 return xenstore_read_str(xendev
->be
, node
);
135 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
137 return xenstore_read_int(xendev
->be
, node
, ival
);
140 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
142 return xenstore_read_str(xendev
->fe
, node
);
145 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
147 return xenstore_read_int(xendev
->fe
, node
, ival
);
150 /* ------------------------------------------------------------- */
152 const char *xenbus_strstate(enum xenbus_state state
)
154 static const char *const name
[] = {
155 [ XenbusStateUnknown
] = "Unknown",
156 [ XenbusStateInitialising
] = "Initialising",
157 [ XenbusStateInitWait
] = "InitWait",
158 [ XenbusStateInitialised
] = "Initialised",
159 [ XenbusStateConnected
] = "Connected",
160 [ XenbusStateClosing
] = "Closing",
161 [ XenbusStateClosed
] = "Closed",
163 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
166 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
170 rc
= xenstore_write_be_int(xendev
, "state", state
);
174 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
175 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
176 xendev
->be_state
= state
;
180 /* ------------------------------------------------------------- */
182 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
184 struct XenDevice
*xendev
;
186 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
187 if (xendev
->dom
!= dom
) {
190 if (xendev
->dev
!= dev
) {
193 if (strcmp(xendev
->type
, type
) != 0) {
202 * get xen backend device, allocate a new one if it doesn't exist.
204 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
205 struct XenDevOps
*ops
)
207 struct XenDevice
*xendev
;
210 xendev
= xen_be_find_xendev(type
, dom
, dev
);
215 /* init new xendev */
216 xendev
= g_malloc0(ops
->size
);
222 dom0
= xs_get_domain_path(xenstore
, 0);
223 snprintf(xendev
->be
, sizeof(xendev
->be
), "%s/backend/%s/%d/%d",
224 dom0
, xendev
->type
, xendev
->dom
, xendev
->dev
);
225 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
226 xendev
->type
, xendev
->dev
);
229 xendev
->debug
= debug
;
230 xendev
->local_port
= -1;
232 xendev
->evtchndev
= xen_xc_evtchn_open(NULL
, 0);
233 if (xendev
->evtchndev
== XC_HANDLER_INITIAL_VALUE
) {
234 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
238 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
240 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
241 xendev
->gnttabdev
= xen_xc_gnttab_open(NULL
, 0);
242 if (xendev
->gnttabdev
== XC_HANDLER_INITIAL_VALUE
) {
243 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
244 xc_evtchn_close(xendev
->evtchndev
);
249 xendev
->gnttabdev
= XC_HANDLER_INITIAL_VALUE
;
252 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
254 if (xendev
->ops
->alloc
) {
255 xendev
->ops
->alloc(xendev
);
262 * release xen backend device.
264 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
266 struct XenDevice
*xendev
, *xnext
;
269 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
270 * we save the next pointer in xnext because we might free xendev.
272 xnext
= xendevs
.tqh_first
;
275 xnext
= xendev
->next
.tqe_next
;
277 if (xendev
->dom
!= dom
) {
280 if (xendev
->dev
!= dev
&& dev
!= -1) {
284 if (xendev
->ops
->free
) {
285 xendev
->ops
->free(xendev
);
289 char token
[XEN_BUFSIZE
];
290 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
291 xs_unwatch(xenstore
, xendev
->fe
, token
);
295 if (xendev
->evtchndev
!= XC_HANDLER_INITIAL_VALUE
) {
296 xc_evtchn_close(xendev
->evtchndev
);
298 if (xendev
->gnttabdev
!= XC_HANDLER_INITIAL_VALUE
) {
299 xc_gnttab_close(xendev
->gnttabdev
);
302 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
309 * Sync internal data structures on xenstore updates.
310 * Node specifies the changed field. node = NULL means
311 * update all fields (used for initialization).
313 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
315 if (node
== NULL
|| strcmp(node
, "online") == 0) {
316 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
322 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
323 if (xendev
->ops
->backend_changed
) {
324 xendev
->ops
->backend_changed(xendev
, node
);
329 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
333 if (node
== NULL
|| strcmp(node
, "state") == 0) {
334 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
335 fe_state
= XenbusStateUnknown
;
337 if (xendev
->fe_state
!= fe_state
) {
338 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
339 xenbus_strstate(xendev
->fe_state
),
340 xenbus_strstate(fe_state
));
342 xendev
->fe_state
= fe_state
;
344 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
345 g_free(xendev
->protocol
);
346 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
347 if (xendev
->protocol
) {
348 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
353 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
354 if (xendev
->ops
->frontend_changed
) {
355 xendev
->ops
->frontend_changed(xendev
, node
);
360 /* ------------------------------------------------------------- */
361 /* Check for possible state transitions and perform them. */
364 * Initial xendev setup. Read frontend path, register watch for it.
365 * Should succeed once xend finished setting up the backend device.
367 * Also sets initial state (-> Initializing) when done. Which
368 * only affects the xendev->be_state variable as xenbus should
369 * already be put into that state by xend.
371 static int xen_be_try_setup(struct XenDevice
*xendev
)
373 char token
[XEN_BUFSIZE
];
376 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
377 xen_be_printf(xendev
, 0, "reading backend state failed\n");
381 if (be_state
!= XenbusStateInitialising
) {
382 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
383 xenbus_strstate(be_state
));
387 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
388 if (xendev
->fe
== NULL
) {
389 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
393 /* setup frontend watch */
394 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
395 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
396 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
400 xen_be_set_state(xendev
, XenbusStateInitialising
);
402 xen_be_backend_changed(xendev
, NULL
);
403 xen_be_frontend_changed(xendev
, NULL
);
408 * Try initialize xendev. Prepare everything the backend can do
409 * without synchronizing with the frontend. Fakes hotplug-status. No
410 * hotplug involved here because this is about userspace drivers, thus
411 * there are kernel backend devices which could invoke hotplug.
413 * Goes to InitWait on success.
415 static int xen_be_try_init(struct XenDevice
*xendev
)
419 if (!xendev
->online
) {
420 xen_be_printf(xendev
, 1, "not online\n");
424 if (xendev
->ops
->init
) {
425 rc
= xendev
->ops
->init(xendev
);
428 xen_be_printf(xendev
, 1, "init() failed\n");
432 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
433 xen_be_set_state(xendev
, XenbusStateInitWait
);
438 * Try to initialise xendev. Depends on the frontend being ready
439 * for it (shared ring and evtchn info in xenstore, state being
440 * Initialised or Connected).
442 * Goes to Connected on success.
444 static int xen_be_try_initialise(struct XenDevice
*xendev
)
448 if (xendev
->fe_state
!= XenbusStateInitialised
&&
449 xendev
->fe_state
!= XenbusStateConnected
) {
450 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
451 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
453 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
458 if (xendev
->ops
->initialise
) {
459 rc
= xendev
->ops
->initialise(xendev
);
462 xen_be_printf(xendev
, 0, "initialise() failed\n");
466 xen_be_set_state(xendev
, XenbusStateConnected
);
471 * Try to let xendev know that it is connected. Depends on the
472 * frontend being Connected. Note that this may be called more
473 * than once since the backend state is not modified.
475 static void xen_be_try_connected(struct XenDevice
*xendev
)
477 if (!xendev
->ops
->connected
) {
481 if (xendev
->fe_state
!= XenbusStateConnected
) {
482 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
483 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
485 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
490 xendev
->ops
->connected(xendev
);
494 * Teardown connection.
496 * Goes to Closed when done.
498 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
500 if (xendev
->be_state
!= XenbusStateClosing
&&
501 xendev
->be_state
!= XenbusStateClosed
&&
502 xendev
->ops
->disconnect
) {
503 xendev
->ops
->disconnect(xendev
);
505 if (xendev
->be_state
!= state
) {
506 xen_be_set_state(xendev
, state
);
511 * Try to reset xendev, for reconnection by another frontend instance.
513 static int xen_be_try_reset(struct XenDevice
*xendev
)
515 if (xendev
->fe_state
!= XenbusStateInitialising
) {
519 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
520 xen_be_set_state(xendev
, XenbusStateInitialising
);
525 * state change dispatcher function
527 void xen_be_check_state(struct XenDevice
*xendev
)
531 /* frontend may request shutdown from almost anywhere */
532 if (xendev
->fe_state
== XenbusStateClosing
||
533 xendev
->fe_state
== XenbusStateClosed
) {
534 xen_be_disconnect(xendev
, xendev
->fe_state
);
538 /* check for possible backend state transitions */
540 switch (xendev
->be_state
) {
541 case XenbusStateUnknown
:
542 rc
= xen_be_try_setup(xendev
);
544 case XenbusStateInitialising
:
545 rc
= xen_be_try_init(xendev
);
547 case XenbusStateInitWait
:
548 rc
= xen_be_try_initialise(xendev
);
550 case XenbusStateConnected
:
551 /* xendev->be_state doesn't change */
552 xen_be_try_connected(xendev
);
555 case XenbusStateClosed
:
556 rc
= xen_be_try_reset(xendev
);
567 /* ------------------------------------------------------------- */
569 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
571 struct XenDevice
*xendev
;
572 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
573 char **dev
= NULL
, *dom0
;
574 unsigned int cdev
, j
;
577 dom0
= xs_get_domain_path(xenstore
, 0);
578 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
579 snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
581 if (!xs_watch(xenstore
, path
, token
)) {
582 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
586 /* look for backends */
587 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
591 for (j
= 0; j
< cdev
; j
++) {
592 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
593 if (xendev
== NULL
) {
596 xen_be_check_state(xendev
);
602 static void xenstore_update_be(char *watch
, char *type
, int dom
,
603 struct XenDevOps
*ops
)
605 struct XenDevice
*xendev
;
606 char path
[XEN_BUFSIZE
], *dom0
, *bepath
;
607 unsigned int len
, dev
;
609 dom0
= xs_get_domain_path(xenstore
, 0);
610 len
= snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
612 if (strncmp(path
, watch
, len
) != 0) {
615 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
617 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
625 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
626 if (xendev
!= NULL
) {
627 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
628 if (bepath
== NULL
) {
629 xen_be_del_xendev(dom
, dev
);
632 xen_be_backend_changed(xendev
, path
);
633 xen_be_check_state(xendev
);
638 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
643 len
= strlen(xendev
->fe
);
644 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
647 if (watch
[len
] != '/') {
650 node
= watch
+ len
+ 1;
652 xen_be_frontend_changed(xendev
, node
);
653 xen_be_check_state(xendev
);
656 static void xenstore_update(void *unused
)
659 intptr_t type
, ops
, ptr
;
660 unsigned int dom
, count
;
662 vec
= xs_read_watch(xenstore
, &count
);
667 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
668 &type
, &dom
, &ops
) == 3) {
669 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
671 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
672 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
679 static void xen_be_evtchn_event(void *opaque
)
681 struct XenDevice
*xendev
= opaque
;
684 port
= xc_evtchn_pending(xendev
->evtchndev
);
685 if (port
!= xendev
->local_port
) {
686 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
687 port
, xendev
->local_port
);
690 xc_evtchn_unmask(xendev
->evtchndev
, port
);
692 if (xendev
->ops
->event
) {
693 xendev
->ops
->event(xendev
);
697 /* -------------------------------------------------------------------- */
699 int xen_be_init(void)
701 xenstore
= xs_daemon_open();
703 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
707 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0) {
711 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
712 /* Check if xen_init() have been called */
718 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
719 xs_daemon_close(xenstore
);
725 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
727 return xenstore_scan(type
, xen_domid
, ops
);
730 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
732 if (xendev
->local_port
!= -1) {
735 xendev
->local_port
= xc_evtchn_bind_interdomain
736 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
737 if (xendev
->local_port
== -1) {
738 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
741 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
742 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
743 xen_be_evtchn_event
, NULL
, xendev
);
747 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
749 if (xendev
->local_port
== -1) {
752 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
753 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
754 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
755 xendev
->local_port
= -1;
758 int xen_be_send_notify(struct XenDevice
*xendev
)
760 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
765 * 0 == errors (stderr + logfile).
766 * 1 == informative debug messages (logfile only).
767 * 2 == noisy debug messages (logfile only).
768 * 3 == will flood your log (logfile only).
770 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
775 if (msg_level
> xendev
->debug
) {
778 qemu_log("xen be: %s: ", xendev
->name
);
779 if (msg_level
== 0) {
780 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
783 if (msg_level
> debug
) {
786 qemu_log("xen be core: ");
787 if (msg_level
== 0) {
788 fprintf(stderr
, "xen be core: ");
792 qemu_log_vprintf(fmt
, args
);
794 if (msg_level
== 0) {
796 vfprintf(stderr
, fmt
, args
);