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>
39 #include <xen/grant_table.h>
42 #include "qemu-char.h"
44 #include "xen_backend.h"
46 /* ------------------------------------------------------------- */
49 XenXC xen_xc
= XC_HANDLER_INITIAL_VALUE
;
50 XenGnttab xen_xcg
= XC_HANDLER_INITIAL_VALUE
;
51 struct xs_handle
*xenstore
= NULL
;
52 const char *xen_protocol
;
55 static QTAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
= QTAILQ_HEAD_INITIALIZER(xendevs
);
58 /* ------------------------------------------------------------- */
60 int xenstore_write_str(const char *base
, const char *node
, const char *val
)
62 char abspath
[XEN_BUFSIZE
];
64 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
65 if (!xs_write(xenstore
, 0, abspath
, val
, strlen(val
))) {
71 char *xenstore_read_str(const char *base
, const char *node
)
73 char abspath
[XEN_BUFSIZE
];
75 char *str
, *ret
= NULL
;
77 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
78 str
= xs_read(xenstore
, 0, abspath
, &len
);
80 /* move to qemu-allocated memory to make sure
81 * callers can savely g_free() stuff. */
88 int xenstore_write_int(const char *base
, const char *node
, int ival
)
92 snprintf(val
, sizeof(val
), "%d", ival
);
93 return xenstore_write_str(base
, node
, val
);
96 int xenstore_read_int(const char *base
, const char *node
, int *ival
)
101 val
= xenstore_read_str(base
, node
);
102 if (val
&& 1 == sscanf(val
, "%d", ival
)) {
109 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
111 return xenstore_write_str(xendev
->be
, node
, val
);
114 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
116 return xenstore_write_int(xendev
->be
, node
, ival
);
119 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
121 return xenstore_read_str(xendev
->be
, node
);
124 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
126 return xenstore_read_int(xendev
->be
, node
, ival
);
129 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
131 return xenstore_read_str(xendev
->fe
, node
);
134 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
136 return xenstore_read_int(xendev
->fe
, node
, ival
);
139 /* ------------------------------------------------------------- */
141 const char *xenbus_strstate(enum xenbus_state state
)
143 static const char *const name
[] = {
144 [ XenbusStateUnknown
] = "Unknown",
145 [ XenbusStateInitialising
] = "Initialising",
146 [ XenbusStateInitWait
] = "InitWait",
147 [ XenbusStateInitialised
] = "Initialised",
148 [ XenbusStateConnected
] = "Connected",
149 [ XenbusStateClosing
] = "Closing",
150 [ XenbusStateClosed
] = "Closed",
152 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
155 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
159 rc
= xenstore_write_be_int(xendev
, "state", state
);
163 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
164 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
165 xendev
->be_state
= state
;
169 /* ------------------------------------------------------------- */
171 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
173 struct XenDevice
*xendev
;
175 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
176 if (xendev
->dom
!= dom
) {
179 if (xendev
->dev
!= dev
) {
182 if (strcmp(xendev
->type
, type
) != 0) {
191 * get xen backend device, allocate a new one if it doesn't exist.
193 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
194 struct XenDevOps
*ops
)
196 struct XenDevice
*xendev
;
199 xendev
= xen_be_find_xendev(type
, dom
, dev
);
204 /* init new xendev */
205 xendev
= g_malloc0(ops
->size
);
211 dom0
= xs_get_domain_path(xenstore
, 0);
212 snprintf(xendev
->be
, sizeof(xendev
->be
), "%s/backend/%s/%d/%d",
213 dom0
, xendev
->type
, xendev
->dom
, xendev
->dev
);
214 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
215 xendev
->type
, xendev
->dev
);
218 xendev
->debug
= debug
;
219 xendev
->local_port
= -1;
221 xendev
->evtchndev
= xen_xc_evtchn_open(NULL
, 0);
222 if (xendev
->evtchndev
== XC_HANDLER_INITIAL_VALUE
) {
223 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
227 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
229 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
230 xendev
->gnttabdev
= xen_xc_gnttab_open(NULL
, 0);
231 if (xendev
->gnttabdev
== XC_HANDLER_INITIAL_VALUE
) {
232 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
233 xc_evtchn_close(xendev
->evtchndev
);
238 xendev
->gnttabdev
= XC_HANDLER_INITIAL_VALUE
;
241 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
243 if (xendev
->ops
->alloc
) {
244 xendev
->ops
->alloc(xendev
);
251 * release xen backend device.
253 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
255 struct XenDevice
*xendev
, *xnext
;
258 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
259 * we save the next pointer in xnext because we might free xendev.
261 xnext
= xendevs
.tqh_first
;
264 xnext
= xendev
->next
.tqe_next
;
266 if (xendev
->dom
!= dom
) {
269 if (xendev
->dev
!= dev
&& dev
!= -1) {
273 if (xendev
->ops
->free
) {
274 xendev
->ops
->free(xendev
);
278 char token
[XEN_BUFSIZE
];
279 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
280 xs_unwatch(xenstore
, xendev
->fe
, token
);
284 if (xendev
->evtchndev
!= XC_HANDLER_INITIAL_VALUE
) {
285 xc_evtchn_close(xendev
->evtchndev
);
287 if (xendev
->gnttabdev
!= XC_HANDLER_INITIAL_VALUE
) {
288 xc_gnttab_close(xendev
->gnttabdev
);
291 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
298 * Sync internal data structures on xenstore updates.
299 * Node specifies the changed field. node = NULL means
300 * update all fields (used for initialization).
302 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
304 if (node
== NULL
|| strcmp(node
, "online") == 0) {
305 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
311 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
312 if (xendev
->ops
->backend_changed
) {
313 xendev
->ops
->backend_changed(xendev
, node
);
318 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
322 if (node
== NULL
|| strcmp(node
, "state") == 0) {
323 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
324 fe_state
= XenbusStateUnknown
;
326 if (xendev
->fe_state
!= fe_state
) {
327 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
328 xenbus_strstate(xendev
->fe_state
),
329 xenbus_strstate(fe_state
));
331 xendev
->fe_state
= fe_state
;
333 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
334 g_free(xendev
->protocol
);
335 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
336 if (xendev
->protocol
) {
337 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
342 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
343 if (xendev
->ops
->frontend_changed
) {
344 xendev
->ops
->frontend_changed(xendev
, node
);
349 /* ------------------------------------------------------------- */
350 /* Check for possible state transitions and perform them. */
353 * Initial xendev setup. Read frontend path, register watch for it.
354 * Should succeed once xend finished setting up the backend device.
356 * Also sets initial state (-> Initializing) when done. Which
357 * only affects the xendev->be_state variable as xenbus should
358 * already be put into that state by xend.
360 static int xen_be_try_setup(struct XenDevice
*xendev
)
362 char token
[XEN_BUFSIZE
];
365 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
366 xen_be_printf(xendev
, 0, "reading backend state failed\n");
370 if (be_state
!= XenbusStateInitialising
) {
371 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
372 xenbus_strstate(be_state
));
376 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
377 if (xendev
->fe
== NULL
) {
378 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
382 /* setup frontend watch */
383 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
384 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
385 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
389 xen_be_set_state(xendev
, XenbusStateInitialising
);
391 xen_be_backend_changed(xendev
, NULL
);
392 xen_be_frontend_changed(xendev
, NULL
);
397 * Try initialize xendev. Prepare everything the backend can do
398 * without synchronizing with the frontend. Fakes hotplug-status. No
399 * hotplug involved here because this is about userspace drivers, thus
400 * there are kernel backend devices which could invoke hotplug.
402 * Goes to InitWait on success.
404 static int xen_be_try_init(struct XenDevice
*xendev
)
408 if (!xendev
->online
) {
409 xen_be_printf(xendev
, 1, "not online\n");
413 if (xendev
->ops
->init
) {
414 rc
= xendev
->ops
->init(xendev
);
417 xen_be_printf(xendev
, 1, "init() failed\n");
421 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
422 xen_be_set_state(xendev
, XenbusStateInitWait
);
427 * Try to initialise xendev. Depends on the frontend being ready
428 * for it (shared ring and evtchn info in xenstore, state being
429 * Initialised or Connected).
431 * Goes to Connected on success.
433 static int xen_be_try_initialise(struct XenDevice
*xendev
)
437 if (xendev
->fe_state
!= XenbusStateInitialised
&&
438 xendev
->fe_state
!= XenbusStateConnected
) {
439 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
440 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
442 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
447 if (xendev
->ops
->initialise
) {
448 rc
= xendev
->ops
->initialise(xendev
);
451 xen_be_printf(xendev
, 0, "initialise() failed\n");
455 xen_be_set_state(xendev
, XenbusStateConnected
);
460 * Try to let xendev know that it is connected. Depends on the
461 * frontend being Connected. Note that this may be called more
462 * than once since the backend state is not modified.
464 static void xen_be_try_connected(struct XenDevice
*xendev
)
466 if (!xendev
->ops
->connected
) {
470 if (xendev
->fe_state
!= XenbusStateConnected
) {
471 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
472 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
474 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
479 xendev
->ops
->connected(xendev
);
483 * Teardown connection.
485 * Goes to Closed when done.
487 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
489 if (xendev
->be_state
!= XenbusStateClosing
&&
490 xendev
->be_state
!= XenbusStateClosed
&&
491 xendev
->ops
->disconnect
) {
492 xendev
->ops
->disconnect(xendev
);
494 if (xendev
->be_state
!= state
) {
495 xen_be_set_state(xendev
, state
);
500 * Try to reset xendev, for reconnection by another frontend instance.
502 static int xen_be_try_reset(struct XenDevice
*xendev
)
504 if (xendev
->fe_state
!= XenbusStateInitialising
) {
508 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
509 xen_be_set_state(xendev
, XenbusStateInitialising
);
514 * state change dispatcher function
516 void xen_be_check_state(struct XenDevice
*xendev
)
520 /* frontend may request shutdown from almost anywhere */
521 if (xendev
->fe_state
== XenbusStateClosing
||
522 xendev
->fe_state
== XenbusStateClosed
) {
523 xen_be_disconnect(xendev
, xendev
->fe_state
);
527 /* check for possible backend state transitions */
529 switch (xendev
->be_state
) {
530 case XenbusStateUnknown
:
531 rc
= xen_be_try_setup(xendev
);
533 case XenbusStateInitialising
:
534 rc
= xen_be_try_init(xendev
);
536 case XenbusStateInitWait
:
537 rc
= xen_be_try_initialise(xendev
);
539 case XenbusStateConnected
:
540 /* xendev->be_state doesn't change */
541 xen_be_try_connected(xendev
);
544 case XenbusStateClosed
:
545 rc
= xen_be_try_reset(xendev
);
556 /* ------------------------------------------------------------- */
558 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
560 struct XenDevice
*xendev
;
561 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
562 char **dev
= NULL
, *dom0
;
563 unsigned int cdev
, j
;
566 dom0
= xs_get_domain_path(xenstore
, 0);
567 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
568 snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
570 if (!xs_watch(xenstore
, path
, token
)) {
571 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
575 /* look for backends */
576 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
580 for (j
= 0; j
< cdev
; j
++) {
581 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
582 if (xendev
== NULL
) {
585 xen_be_check_state(xendev
);
591 static void xenstore_update_be(char *watch
, char *type
, int dom
,
592 struct XenDevOps
*ops
)
594 struct XenDevice
*xendev
;
595 char path
[XEN_BUFSIZE
], *dom0
;
596 unsigned int len
, dev
;
598 dom0
= xs_get_domain_path(xenstore
, 0);
599 len
= snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
601 if (strncmp(path
, watch
, len
) != 0) {
604 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
606 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
615 /* FIXME: detect devices being deleted from xenstore ... */
616 xen_be_del_xendev(dom
, dev
);
619 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
620 if (xendev
!= NULL
) {
621 xen_be_backend_changed(xendev
, path
);
622 xen_be_check_state(xendev
);
626 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
631 len
= strlen(xendev
->fe
);
632 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
635 if (watch
[len
] != '/') {
638 node
= watch
+ len
+ 1;
640 xen_be_frontend_changed(xendev
, node
);
641 xen_be_check_state(xendev
);
644 static void xenstore_update(void *unused
)
647 intptr_t type
, ops
, ptr
;
648 unsigned int dom
, count
;
650 vec
= xs_read_watch(xenstore
, &count
);
655 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
656 &type
, &dom
, &ops
) == 3) {
657 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
659 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
660 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
667 static void xen_be_evtchn_event(void *opaque
)
669 struct XenDevice
*xendev
= opaque
;
672 port
= xc_evtchn_pending(xendev
->evtchndev
);
673 if (port
!= xendev
->local_port
) {
674 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
675 port
, xendev
->local_port
);
678 xc_evtchn_unmask(xendev
->evtchndev
, port
);
680 if (xendev
->ops
->event
) {
681 xendev
->ops
->event(xendev
);
685 /* -------------------------------------------------------------------- */
687 int xen_be_init(void)
689 xenstore
= xs_daemon_open();
691 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
695 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0) {
699 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
700 /* Check if xen_init() have been called */
706 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
707 xs_daemon_close(xenstore
);
713 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
715 return xenstore_scan(type
, xen_domid
, ops
);
718 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
720 if (xendev
->local_port
!= -1) {
723 xendev
->local_port
= xc_evtchn_bind_interdomain
724 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
725 if (xendev
->local_port
== -1) {
726 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
729 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
730 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
731 xen_be_evtchn_event
, NULL
, xendev
);
735 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
737 if (xendev
->local_port
== -1) {
740 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
741 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
742 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
743 xendev
->local_port
= -1;
746 int xen_be_send_notify(struct XenDevice
*xendev
)
748 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
753 * 0 == errors (stderr + logfile).
754 * 1 == informative debug messages (logfile only).
755 * 2 == noisy debug messages (logfile only).
756 * 3 == will flood your log (logfile only).
758 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
763 if (msg_level
> xendev
->debug
) {
766 qemu_log("xen be: %s: ", xendev
->name
);
767 if (msg_level
== 0) {
768 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
771 if (msg_level
> debug
) {
774 qemu_log("xen be core: ");
775 if (msg_level
== 0) {
776 fprintf(stderr
, "xen be core: ");
780 qemu_log_vprintf(fmt
, args
);
782 if (msg_level
== 0) {
784 vfprintf(stderr
, fmt
, args
);