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 struct xs_handle
*xenstore
= NULL
;
49 const char *xen_protocol
;
52 static QTAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
= QTAILQ_HEAD_INITIALIZER(xendevs
);
55 /* ------------------------------------------------------------- */
57 int xenstore_write_str(const char *base
, const char *node
, const char *val
)
59 char abspath
[XEN_BUFSIZE
];
61 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
62 if (!xs_write(xenstore
, 0, abspath
, val
, strlen(val
))) {
68 char *xenstore_read_str(const char *base
, const char *node
)
70 char abspath
[XEN_BUFSIZE
];
72 char *str
, *ret
= NULL
;
74 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
75 str
= xs_read(xenstore
, 0, abspath
, &len
);
77 /* move to qemu-allocated memory to make sure
78 * callers can savely g_free() stuff. */
85 int xenstore_write_int(const char *base
, const char *node
, int ival
)
89 snprintf(val
, sizeof(val
), "%d", ival
);
90 return xenstore_write_str(base
, node
, val
);
93 int xenstore_write_int64(const char *base
, const char *node
, int64_t ival
)
97 snprintf(val
, sizeof(val
), "%"PRId64
, ival
);
98 return xenstore_write_str(base
, node
, val
);
101 int xenstore_read_int(const char *base
, const char *node
, int *ival
)
106 val
= xenstore_read_str(base
, node
);
107 if (val
&& 1 == sscanf(val
, "%d", ival
)) {
114 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
116 return xenstore_write_str(xendev
->be
, node
, val
);
119 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
121 return xenstore_write_int(xendev
->be
, node
, ival
);
124 int xenstore_write_be_int64(struct XenDevice
*xendev
, const char *node
, int64_t ival
)
126 return xenstore_write_int64(xendev
->be
, node
, ival
);
129 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
131 return xenstore_read_str(xendev
->be
, node
);
134 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
136 return xenstore_read_int(xendev
->be
, node
, ival
);
139 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
141 return xenstore_read_str(xendev
->fe
, node
);
144 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
146 return xenstore_read_int(xendev
->fe
, node
, ival
);
149 /* ------------------------------------------------------------- */
151 const char *xenbus_strstate(enum xenbus_state state
)
153 static const char *const name
[] = {
154 [ XenbusStateUnknown
] = "Unknown",
155 [ XenbusStateInitialising
] = "Initialising",
156 [ XenbusStateInitWait
] = "InitWait",
157 [ XenbusStateInitialised
] = "Initialised",
158 [ XenbusStateConnected
] = "Connected",
159 [ XenbusStateClosing
] = "Closing",
160 [ XenbusStateClosed
] = "Closed",
162 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
165 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
169 rc
= xenstore_write_be_int(xendev
, "state", state
);
173 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
174 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
175 xendev
->be_state
= state
;
179 /* ------------------------------------------------------------- */
181 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
183 struct XenDevice
*xendev
;
185 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
186 if (xendev
->dom
!= dom
) {
189 if (xendev
->dev
!= dev
) {
192 if (strcmp(xendev
->type
, type
) != 0) {
201 * get xen backend device, allocate a new one if it doesn't exist.
203 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
204 struct XenDevOps
*ops
)
206 struct XenDevice
*xendev
;
208 xendev
= xen_be_find_xendev(type
, dom
, dev
);
213 /* init new xendev */
214 xendev
= g_malloc0(ops
->size
);
220 snprintf(xendev
->be
, sizeof(xendev
->be
), "backend/%s/%d/%d",
221 xendev
->type
, xendev
->dom
, xendev
->dev
);
222 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
223 xendev
->type
, xendev
->dev
);
225 xendev
->debug
= debug
;
226 xendev
->local_port
= -1;
228 xendev
->evtchndev
= xen_xc_evtchn_open(NULL
, 0);
229 if (xendev
->evtchndev
== XC_HANDLER_INITIAL_VALUE
) {
230 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
234 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
236 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
237 xendev
->gnttabdev
= xen_xc_gnttab_open(NULL
, 0);
238 if (xendev
->gnttabdev
== XC_HANDLER_INITIAL_VALUE
) {
239 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
240 xc_evtchn_close(xendev
->evtchndev
);
245 xendev
->gnttabdev
= XC_HANDLER_INITIAL_VALUE
;
248 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
250 if (xendev
->ops
->alloc
) {
251 xendev
->ops
->alloc(xendev
);
258 * release xen backend device.
260 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
262 struct XenDevice
*xendev
, *xnext
;
265 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
266 * we save the next pointer in xnext because we might free xendev.
268 xnext
= xendevs
.tqh_first
;
271 xnext
= xendev
->next
.tqe_next
;
273 if (xendev
->dom
!= dom
) {
276 if (xendev
->dev
!= dev
&& dev
!= -1) {
280 if (xendev
->ops
->free
) {
281 xendev
->ops
->free(xendev
);
285 char token
[XEN_BUFSIZE
];
286 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
287 xs_unwatch(xenstore
, xendev
->fe
, token
);
291 if (xendev
->evtchndev
!= XC_HANDLER_INITIAL_VALUE
) {
292 xc_evtchn_close(xendev
->evtchndev
);
294 if (xendev
->gnttabdev
!= XC_HANDLER_INITIAL_VALUE
) {
295 xc_gnttab_close(xendev
->gnttabdev
);
298 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
305 * Sync internal data structures on xenstore updates.
306 * Node specifies the changed field. node = NULL means
307 * update all fields (used for initialization).
309 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
311 if (node
== NULL
|| strcmp(node
, "online") == 0) {
312 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
318 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
319 if (xendev
->ops
->backend_changed
) {
320 xendev
->ops
->backend_changed(xendev
, node
);
325 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
329 if (node
== NULL
|| strcmp(node
, "state") == 0) {
330 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
331 fe_state
= XenbusStateUnknown
;
333 if (xendev
->fe_state
!= fe_state
) {
334 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
335 xenbus_strstate(xendev
->fe_state
),
336 xenbus_strstate(fe_state
));
338 xendev
->fe_state
= fe_state
;
340 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
341 g_free(xendev
->protocol
);
342 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
343 if (xendev
->protocol
) {
344 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
349 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
350 if (xendev
->ops
->frontend_changed
) {
351 xendev
->ops
->frontend_changed(xendev
, node
);
356 /* ------------------------------------------------------------- */
357 /* Check for possible state transitions and perform them. */
360 * Initial xendev setup. Read frontend path, register watch for it.
361 * Should succeed once xend finished setting up the backend device.
363 * Also sets initial state (-> Initializing) when done. Which
364 * only affects the xendev->be_state variable as xenbus should
365 * already be put into that state by xend.
367 static int xen_be_try_setup(struct XenDevice
*xendev
)
369 char token
[XEN_BUFSIZE
];
372 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
373 xen_be_printf(xendev
, 0, "reading backend state failed\n");
377 if (be_state
!= XenbusStateInitialising
) {
378 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
379 xenbus_strstate(be_state
));
383 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
384 if (xendev
->fe
== NULL
) {
385 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
389 /* setup frontend watch */
390 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
391 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
392 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
396 xen_be_set_state(xendev
, XenbusStateInitialising
);
398 xen_be_backend_changed(xendev
, NULL
);
399 xen_be_frontend_changed(xendev
, NULL
);
404 * Try initialize xendev. Prepare everything the backend can do
405 * without synchronizing with the frontend. Fakes hotplug-status. No
406 * hotplug involved here because this is about userspace drivers, thus
407 * there are kernel backend devices which could invoke hotplug.
409 * Goes to InitWait on success.
411 static int xen_be_try_init(struct XenDevice
*xendev
)
415 if (!xendev
->online
) {
416 xen_be_printf(xendev
, 1, "not online\n");
420 if (xendev
->ops
->init
) {
421 rc
= xendev
->ops
->init(xendev
);
424 xen_be_printf(xendev
, 1, "init() failed\n");
428 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
429 xen_be_set_state(xendev
, XenbusStateInitWait
);
434 * Try to initialise xendev. Depends on the frontend being ready
435 * for it (shared ring and evtchn info in xenstore, state being
436 * Initialised or Connected).
438 * Goes to Connected on success.
440 static int xen_be_try_initialise(struct XenDevice
*xendev
)
444 if (xendev
->fe_state
!= XenbusStateInitialised
&&
445 xendev
->fe_state
!= XenbusStateConnected
) {
446 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
447 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
449 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
454 if (xendev
->ops
->initialise
) {
455 rc
= xendev
->ops
->initialise(xendev
);
458 xen_be_printf(xendev
, 0, "initialise() failed\n");
462 xen_be_set_state(xendev
, XenbusStateConnected
);
467 * Try to let xendev know that it is connected. Depends on the
468 * frontend being Connected. Note that this may be called more
469 * than once since the backend state is not modified.
471 static void xen_be_try_connected(struct XenDevice
*xendev
)
473 if (!xendev
->ops
->connected
) {
477 if (xendev
->fe_state
!= XenbusStateConnected
) {
478 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
479 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
481 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
486 xendev
->ops
->connected(xendev
);
490 * Teardown connection.
492 * Goes to Closed when done.
494 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
496 if (xendev
->be_state
!= XenbusStateClosing
&&
497 xendev
->be_state
!= XenbusStateClosed
&&
498 xendev
->ops
->disconnect
) {
499 xendev
->ops
->disconnect(xendev
);
501 if (xendev
->be_state
!= state
) {
502 xen_be_set_state(xendev
, state
);
507 * Try to reset xendev, for reconnection by another frontend instance.
509 static int xen_be_try_reset(struct XenDevice
*xendev
)
511 if (xendev
->fe_state
!= XenbusStateInitialising
) {
515 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
516 xen_be_set_state(xendev
, XenbusStateInitialising
);
521 * state change dispatcher function
523 void xen_be_check_state(struct XenDevice
*xendev
)
527 /* frontend may request shutdown from almost anywhere */
528 if (xendev
->fe_state
== XenbusStateClosing
||
529 xendev
->fe_state
== XenbusStateClosed
) {
530 xen_be_disconnect(xendev
, xendev
->fe_state
);
534 /* check for possible backend state transitions */
536 switch (xendev
->be_state
) {
537 case XenbusStateUnknown
:
538 rc
= xen_be_try_setup(xendev
);
540 case XenbusStateInitialising
:
541 rc
= xen_be_try_init(xendev
);
543 case XenbusStateInitWait
:
544 rc
= xen_be_try_initialise(xendev
);
546 case XenbusStateConnected
:
547 /* xendev->be_state doesn't change */
548 xen_be_try_connected(xendev
);
551 case XenbusStateClosed
:
552 rc
= xen_be_try_reset(xendev
);
563 /* ------------------------------------------------------------- */
565 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
567 struct XenDevice
*xendev
;
568 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
570 unsigned int cdev
, j
;
573 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
574 snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
575 if (!xs_watch(xenstore
, path
, token
)) {
576 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
580 /* look for backends */
581 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
585 for (j
= 0; j
< cdev
; j
++) {
586 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
587 if (xendev
== NULL
) {
590 xen_be_check_state(xendev
);
596 static void xenstore_update_be(char *watch
, char *type
, int dom
,
597 struct XenDevOps
*ops
)
599 struct XenDevice
*xendev
;
600 char path
[XEN_BUFSIZE
], *bepath
;
601 unsigned int len
, dev
;
603 len
= snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
604 if (strncmp(path
, watch
, len
) != 0) {
607 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
609 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
617 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
618 if (xendev
!= NULL
) {
619 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
620 if (bepath
== NULL
) {
621 xen_be_del_xendev(dom
, dev
);
624 xen_be_backend_changed(xendev
, path
);
625 xen_be_check_state(xendev
);
630 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
635 len
= strlen(xendev
->fe
);
636 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
639 if (watch
[len
] != '/') {
642 node
= watch
+ len
+ 1;
644 xen_be_frontend_changed(xendev
, node
);
645 xen_be_check_state(xendev
);
648 static void xenstore_update(void *unused
)
651 intptr_t type
, ops
, ptr
;
652 unsigned int dom
, count
;
654 vec
= xs_read_watch(xenstore
, &count
);
659 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
660 &type
, &dom
, &ops
) == 3) {
661 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
663 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
664 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
671 static void xen_be_evtchn_event(void *opaque
)
673 struct XenDevice
*xendev
= opaque
;
676 port
= xc_evtchn_pending(xendev
->evtchndev
);
677 if (port
!= xendev
->local_port
) {
678 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
679 port
, xendev
->local_port
);
682 xc_evtchn_unmask(xendev
->evtchndev
, port
);
684 if (xendev
->ops
->event
) {
685 xendev
->ops
->event(xendev
);
689 /* -------------------------------------------------------------------- */
691 int xen_be_init(void)
693 xenstore
= xs_daemon_open();
695 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
699 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0) {
703 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
704 /* Check if xen_init() have been called */
710 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
711 xs_daemon_close(xenstore
);
717 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
719 return xenstore_scan(type
, xen_domid
, ops
);
722 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
724 if (xendev
->local_port
!= -1) {
727 xendev
->local_port
= xc_evtchn_bind_interdomain
728 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
729 if (xendev
->local_port
== -1) {
730 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
733 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
734 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
735 xen_be_evtchn_event
, NULL
, xendev
);
739 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
741 if (xendev
->local_port
== -1) {
744 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
745 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
746 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
747 xendev
->local_port
= -1;
750 int xen_be_send_notify(struct XenDevice
*xendev
)
752 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
757 * 0 == errors (stderr + logfile).
758 * 1 == informative debug messages (logfile only).
759 * 2 == noisy debug messages (logfile only).
760 * 3 == will flood your log (logfile only).
762 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
767 if (msg_level
> xendev
->debug
) {
770 qemu_log("xen be: %s: ", xendev
->name
);
771 if (msg_level
== 0) {
772 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
775 if (msg_level
> debug
) {
778 qemu_log("xen be core: ");
779 if (msg_level
== 0) {
780 fprintf(stderr
, "xen be core: ");
784 qemu_log_vprintf(fmt
, args
);
786 if (msg_level
== 0) {
788 vfprintf(stderr
, fmt
, args
);