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
;
209 xendev
= xen_be_find_xendev(type
, dom
, dev
);
214 /* init new xendev */
215 xendev
= g_malloc0(ops
->size
);
221 snprintf(xendev
->be
, sizeof(xendev
->be
), "backend/%s/%d/%d",
222 xendev
->type
, xendev
->dom
, xendev
->dev
);
223 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
224 xendev
->type
, xendev
->dev
);
226 xendev
->debug
= debug
;
227 xendev
->local_port
= -1;
229 xendev
->evtchndev
= xen_xc_evtchn_open(NULL
, 0);
230 if (xendev
->evtchndev
== XC_HANDLER_INITIAL_VALUE
) {
231 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
235 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
237 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
238 xendev
->gnttabdev
= xen_xc_gnttab_open(NULL
, 0);
239 if (xendev
->gnttabdev
== XC_HANDLER_INITIAL_VALUE
) {
240 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
241 xc_evtchn_close(xendev
->evtchndev
);
246 xendev
->gnttabdev
= XC_HANDLER_INITIAL_VALUE
;
249 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
251 if (xendev
->ops
->alloc
) {
252 xendev
->ops
->alloc(xendev
);
259 * release xen backend device.
261 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
263 struct XenDevice
*xendev
, *xnext
;
266 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
267 * we save the next pointer in xnext because we might free xendev.
269 xnext
= xendevs
.tqh_first
;
272 xnext
= xendev
->next
.tqe_next
;
274 if (xendev
->dom
!= dom
) {
277 if (xendev
->dev
!= dev
&& dev
!= -1) {
281 if (xendev
->ops
->free
) {
282 xendev
->ops
->free(xendev
);
286 char token
[XEN_BUFSIZE
];
287 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
288 xs_unwatch(xenstore
, xendev
->fe
, token
);
292 if (xendev
->evtchndev
!= XC_HANDLER_INITIAL_VALUE
) {
293 xc_evtchn_close(xendev
->evtchndev
);
295 if (xendev
->gnttabdev
!= XC_HANDLER_INITIAL_VALUE
) {
296 xc_gnttab_close(xendev
->gnttabdev
);
299 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
306 * Sync internal data structures on xenstore updates.
307 * Node specifies the changed field. node = NULL means
308 * update all fields (used for initialization).
310 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
312 if (node
== NULL
|| strcmp(node
, "online") == 0) {
313 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
319 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
320 if (xendev
->ops
->backend_changed
) {
321 xendev
->ops
->backend_changed(xendev
, node
);
326 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
330 if (node
== NULL
|| strcmp(node
, "state") == 0) {
331 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
332 fe_state
= XenbusStateUnknown
;
334 if (xendev
->fe_state
!= fe_state
) {
335 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
336 xenbus_strstate(xendev
->fe_state
),
337 xenbus_strstate(fe_state
));
339 xendev
->fe_state
= fe_state
;
341 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
342 g_free(xendev
->protocol
);
343 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
344 if (xendev
->protocol
) {
345 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
350 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
351 if (xendev
->ops
->frontend_changed
) {
352 xendev
->ops
->frontend_changed(xendev
, node
);
357 /* ------------------------------------------------------------- */
358 /* Check for possible state transitions and perform them. */
361 * Initial xendev setup. Read frontend path, register watch for it.
362 * Should succeed once xend finished setting up the backend device.
364 * Also sets initial state (-> Initializing) when done. Which
365 * only affects the xendev->be_state variable as xenbus should
366 * already be put into that state by xend.
368 static int xen_be_try_setup(struct XenDevice
*xendev
)
370 char token
[XEN_BUFSIZE
];
373 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
374 xen_be_printf(xendev
, 0, "reading backend state failed\n");
378 if (be_state
!= XenbusStateInitialising
) {
379 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
380 xenbus_strstate(be_state
));
384 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
385 if (xendev
->fe
== NULL
) {
386 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
390 /* setup frontend watch */
391 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
392 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
393 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
397 xen_be_set_state(xendev
, XenbusStateInitialising
);
399 xen_be_backend_changed(xendev
, NULL
);
400 xen_be_frontend_changed(xendev
, NULL
);
405 * Try initialize xendev. Prepare everything the backend can do
406 * without synchronizing with the frontend. Fakes hotplug-status. No
407 * hotplug involved here because this is about userspace drivers, thus
408 * there are kernel backend devices which could invoke hotplug.
410 * Goes to InitWait on success.
412 static int xen_be_try_init(struct XenDevice
*xendev
)
416 if (!xendev
->online
) {
417 xen_be_printf(xendev
, 1, "not online\n");
421 if (xendev
->ops
->init
) {
422 rc
= xendev
->ops
->init(xendev
);
425 xen_be_printf(xendev
, 1, "init() failed\n");
429 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
430 xen_be_set_state(xendev
, XenbusStateInitWait
);
435 * Try to initialise xendev. Depends on the frontend being ready
436 * for it (shared ring and evtchn info in xenstore, state being
437 * Initialised or Connected).
439 * Goes to Connected on success.
441 static int xen_be_try_initialise(struct XenDevice
*xendev
)
445 if (xendev
->fe_state
!= XenbusStateInitialised
&&
446 xendev
->fe_state
!= XenbusStateConnected
) {
447 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
448 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
450 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
455 if (xendev
->ops
->initialise
) {
456 rc
= xendev
->ops
->initialise(xendev
);
459 xen_be_printf(xendev
, 0, "initialise() failed\n");
463 xen_be_set_state(xendev
, XenbusStateConnected
);
468 * Try to let xendev know that it is connected. Depends on the
469 * frontend being Connected. Note that this may be called more
470 * than once since the backend state is not modified.
472 static void xen_be_try_connected(struct XenDevice
*xendev
)
474 if (!xendev
->ops
->connected
) {
478 if (xendev
->fe_state
!= XenbusStateConnected
) {
479 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
480 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
482 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
487 xendev
->ops
->connected(xendev
);
491 * Teardown connection.
493 * Goes to Closed when done.
495 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
497 if (xendev
->be_state
!= XenbusStateClosing
&&
498 xendev
->be_state
!= XenbusStateClosed
&&
499 xendev
->ops
->disconnect
) {
500 xendev
->ops
->disconnect(xendev
);
502 if (xendev
->be_state
!= state
) {
503 xen_be_set_state(xendev
, state
);
508 * Try to reset xendev, for reconnection by another frontend instance.
510 static int xen_be_try_reset(struct XenDevice
*xendev
)
512 if (xendev
->fe_state
!= XenbusStateInitialising
) {
516 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
517 xen_be_set_state(xendev
, XenbusStateInitialising
);
522 * state change dispatcher function
524 void xen_be_check_state(struct XenDevice
*xendev
)
528 /* frontend may request shutdown from almost anywhere */
529 if (xendev
->fe_state
== XenbusStateClosing
||
530 xendev
->fe_state
== XenbusStateClosed
) {
531 xen_be_disconnect(xendev
, xendev
->fe_state
);
535 /* check for possible backend state transitions */
537 switch (xendev
->be_state
) {
538 case XenbusStateUnknown
:
539 rc
= xen_be_try_setup(xendev
);
541 case XenbusStateInitialising
:
542 rc
= xen_be_try_init(xendev
);
544 case XenbusStateInitWait
:
545 rc
= xen_be_try_initialise(xendev
);
547 case XenbusStateConnected
:
548 /* xendev->be_state doesn't change */
549 xen_be_try_connected(xendev
);
552 case XenbusStateClosed
:
553 rc
= xen_be_try_reset(xendev
);
564 /* ------------------------------------------------------------- */
566 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
568 struct XenDevice
*xendev
;
569 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
571 unsigned int cdev
, j
;
574 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
575 snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
576 if (!xs_watch(xenstore
, path
, token
)) {
577 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
581 /* look for backends */
582 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
586 for (j
= 0; j
< cdev
; j
++) {
587 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
588 if (xendev
== NULL
) {
591 xen_be_check_state(xendev
);
597 static void xenstore_update_be(char *watch
, char *type
, int dom
,
598 struct XenDevOps
*ops
)
600 struct XenDevice
*xendev
;
601 char path
[XEN_BUFSIZE
], *bepath
;
602 unsigned int len
, dev
;
604 len
= snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
605 if (strncmp(path
, watch
, len
) != 0) {
608 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
610 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
618 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
619 if (xendev
!= NULL
) {
620 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
621 if (bepath
== NULL
) {
622 xen_be_del_xendev(dom
, dev
);
625 xen_be_backend_changed(xendev
, path
);
626 xen_be_check_state(xendev
);
631 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
636 len
= strlen(xendev
->fe
);
637 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
640 if (watch
[len
] != '/') {
643 node
= watch
+ len
+ 1;
645 xen_be_frontend_changed(xendev
, node
);
646 xen_be_check_state(xendev
);
649 static void xenstore_update(void *unused
)
652 intptr_t type
, ops
, ptr
;
653 unsigned int dom
, count
;
655 vec
= xs_read_watch(xenstore
, &count
);
660 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
661 &type
, &dom
, &ops
) == 3) {
662 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
664 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
665 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
672 static void xen_be_evtchn_event(void *opaque
)
674 struct XenDevice
*xendev
= opaque
;
677 port
= xc_evtchn_pending(xendev
->evtchndev
);
678 if (port
!= xendev
->local_port
) {
679 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
680 port
, xendev
->local_port
);
683 xc_evtchn_unmask(xendev
->evtchndev
, port
);
685 if (xendev
->ops
->event
) {
686 xendev
->ops
->event(xendev
);
690 /* -------------------------------------------------------------------- */
692 int xen_be_init(void)
694 xenstore
= xs_daemon_open();
696 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
700 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0) {
704 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
705 /* Check if xen_init() have been called */
711 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
712 xs_daemon_close(xenstore
);
718 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
720 return xenstore_scan(type
, xen_domid
, ops
);
723 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
725 if (xendev
->local_port
!= -1) {
728 xendev
->local_port
= xc_evtchn_bind_interdomain
729 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
730 if (xendev
->local_port
== -1) {
731 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
734 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
735 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
736 xen_be_evtchn_event
, NULL
, xendev
);
740 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
742 if (xendev
->local_port
== -1) {
745 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
746 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
747 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
748 xendev
->local_port
= -1;
751 int xen_be_send_notify(struct XenDevice
*xendev
)
753 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
758 * 0 == errors (stderr + logfile).
759 * 1 == informative debug messages (logfile only).
760 * 2 == noisy debug messages (logfile only).
761 * 3 == will flood your log (logfile only).
763 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
768 if (msg_level
> xendev
->debug
) {
771 qemu_log("xen be: %s: ", xendev
->name
);
772 if (msg_level
== 0) {
773 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
776 if (msg_level
> debug
) {
779 qemu_log("xen be core: ");
780 if (msg_level
== 0) {
781 fprintf(stderr
, "xen be core: ");
785 qemu_log_vprintf(fmt
, args
);
787 if (msg_level
== 0) {
789 vfprintf(stderr
, fmt
, args
);