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, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * TODO: add some xenbus / xenstore concepts overview here.
30 #include <sys/types.h>
33 #include <sys/signal.h>
37 #include <xen/grant_table.h>
40 #include "qemu-char.h"
42 #include "xen_backend.h"
44 /* ------------------------------------------------------------- */
48 struct xs_handle
*xenstore
= NULL
;
49 const char *xen_protocol
;
52 static TAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
= TAILQ_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
)))
67 char *xenstore_read_str(const char *base
, const char *node
)
69 char abspath
[XEN_BUFSIZE
];
71 char *str
, *ret
= NULL
;
73 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
74 str
= xs_read(xenstore
, 0, abspath
, &len
);
76 /* move to qemu-allocated memory to make sure
77 * callers can savely qemu_free() stuff. */
78 ret
= qemu_strdup(str
);
84 int xenstore_write_int(const char *base
, const char *node
, int ival
)
88 snprintf(val
, sizeof(val
), "%d", ival
);
89 return xenstore_write_str(base
, node
, val
);
92 int xenstore_read_int(const char *base
, const char *node
, int *ival
)
97 val
= xenstore_read_str(base
, node
);
98 if (val
&& 1 == sscanf(val
, "%d", ival
))
104 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
106 return xenstore_write_str(xendev
->be
, node
, val
);
109 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
111 return xenstore_write_int(xendev
->be
, node
, ival
);
114 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
116 return xenstore_read_str(xendev
->be
, node
);
119 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
121 return xenstore_read_int(xendev
->be
, node
, ival
);
124 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
126 return xenstore_read_str(xendev
->fe
, node
);
129 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
131 return xenstore_read_int(xendev
->fe
, node
, ival
);
134 /* ------------------------------------------------------------- */
136 const char *xenbus_strstate(enum xenbus_state state
)
138 static const char *const name
[] = {
139 [ XenbusStateUnknown
] = "Unknown",
140 [ XenbusStateInitialising
] = "Initialising",
141 [ XenbusStateInitWait
] = "InitWait",
142 [ XenbusStateInitialised
] = "Initialised",
143 [ XenbusStateConnected
] = "Connected",
144 [ XenbusStateClosing
] = "Closing",
145 [ XenbusStateClosed
] = "Closed",
147 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
150 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
154 rc
= xenstore_write_be_int(xendev
, "state", state
);
157 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
158 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
159 xendev
->be_state
= state
;
163 /* ------------------------------------------------------------- */
165 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
167 struct XenDevice
*xendev
;
169 TAILQ_FOREACH(xendev
, &xendevs
, next
) {
170 if (xendev
->dom
!= dom
)
172 if (xendev
->dev
!= dev
)
174 if (strcmp(xendev
->type
, type
) != 0)
182 * get xen backend device, allocate a new one if it doesn't exist.
184 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
185 struct XenDevOps
*ops
)
187 struct XenDevice
*xendev
;
190 xendev
= xen_be_find_xendev(type
, dom
, dev
);
194 /* init new xendev */
195 xendev
= qemu_mallocz(ops
->size
);
201 dom0
= xs_get_domain_path(xenstore
, 0);
202 snprintf(xendev
->be
, sizeof(xendev
->be
), "%s/backend/%s/%d/%d",
203 dom0
, xendev
->type
, xendev
->dom
, xendev
->dev
);
204 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
205 xendev
->type
, xendev
->dev
);
208 xendev
->debug
= debug
;
209 xendev
->local_port
= -1;
211 xendev
->evtchndev
= xc_evtchn_open();
212 if (xendev
->evtchndev
< 0) {
213 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
217 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
219 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
220 xendev
->gnttabdev
= xc_gnttab_open();
221 if (xendev
->gnttabdev
< 0) {
222 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
223 xc_evtchn_close(xendev
->evtchndev
);
228 xendev
->gnttabdev
= -1;
231 TAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
233 if (xendev
->ops
->alloc
)
234 xendev
->ops
->alloc(xendev
);
240 * release xen backend device.
242 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
244 struct XenDevice
*xendev
, *xnext
;
247 * This is pretty much like TAILQ_FOREACH(xendev, &xendevs, next) but
248 * we save the next pointer in xnext because we might free xendev.
250 xnext
= xendevs
.tqh_first
;
253 xnext
= xendev
->next
.tqe_next
;
255 if (xendev
->dom
!= dom
)
257 if (xendev
->dev
!= dev
&& dev
!= -1)
260 if (xendev
->ops
->free
)
261 xendev
->ops
->free(xendev
);
264 char token
[XEN_BUFSIZE
];
265 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
266 xs_unwatch(xenstore
, xendev
->fe
, token
);
267 qemu_free(xendev
->fe
);
270 if (xendev
->evtchndev
>= 0)
271 xc_evtchn_close(xendev
->evtchndev
);
272 if (xendev
->gnttabdev
>= 0)
273 xc_gnttab_close(xendev
->gnttabdev
);
275 TAILQ_REMOVE(&xendevs
, xendev
, next
);
282 * Sync internal data structures on xenstore updates.
283 * Node specifies the changed field. node = NULL means
284 * update all fields (used for initialization).
286 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
288 if (node
== NULL
|| strcmp(node
, "online") == 0) {
289 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1)
294 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
295 if (xendev
->ops
->backend_changed
)
296 xendev
->ops
->backend_changed(xendev
, node
);
300 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
304 if (node
== NULL
|| strcmp(node
, "state") == 0) {
305 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1)
306 fe_state
= XenbusStateUnknown
;
307 if (xendev
->fe_state
!= fe_state
)
308 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
309 xenbus_strstate(xendev
->fe_state
),
310 xenbus_strstate(fe_state
));
311 xendev
->fe_state
= fe_state
;
313 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
314 qemu_free(xendev
->protocol
);
315 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
316 if (xendev
->protocol
)
317 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
321 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
322 if (xendev
->ops
->frontend_changed
)
323 xendev
->ops
->frontend_changed(xendev
, node
);
327 /* ------------------------------------------------------------- */
328 /* Check for possible state transitions and perform them. */
331 * Initial xendev setup. Read frontend path, register watch for it.
332 * Should succeed once xend finished setting up the backend device.
334 * Also sets initial state (-> Initializing) when done. Which
335 * only affects the xendev->be_state variable as xenbus should
336 * already be put into that state by xend.
338 static int xen_be_try_setup(struct XenDevice
*xendev
)
340 char token
[XEN_BUFSIZE
];
343 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
344 xen_be_printf(xendev
, 0, "reading backend state failed\n");
348 if (be_state
!= XenbusStateInitialising
) {
349 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
350 xenbus_strstate(be_state
));
354 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
355 if (xendev
->fe
== NULL
) {
356 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
360 /* setup frontend watch */
361 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
362 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
363 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
367 xen_be_set_state(xendev
, XenbusStateInitialising
);
369 xen_be_backend_changed(xendev
, NULL
);
370 xen_be_frontend_changed(xendev
, NULL
);
375 * Try initialize xendev. Prepare everything the backend can do
376 * without synchronizing with the frontend. Fakes hotplug-status. No
377 * hotplug involved here because this is about userspace drivers, thus
378 * there are kernel backend devices which could invoke hotplug.
380 * Goes to InitWait on success.
382 static int xen_be_try_init(struct XenDevice
*xendev
)
386 if (!xendev
->online
) {
387 xen_be_printf(xendev
, 1, "not online\n");
391 if (xendev
->ops
->init
)
392 rc
= xendev
->ops
->init(xendev
);
394 xen_be_printf(xendev
, 1, "init() failed\n");
398 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
399 xen_be_set_state(xendev
, XenbusStateInitWait
);
404 * Try to connect xendev. Depends on the frontend being ready
405 * for it (shared ring and evtchn info in xenstore, state being
406 * Initialised or Connected).
408 * Goes to Connected on success.
410 static int xen_be_try_connect(struct XenDevice
*xendev
)
414 if (xendev
->fe_state
!= XenbusStateInitialised
&&
415 xendev
->fe_state
!= XenbusStateConnected
) {
416 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
417 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
419 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
424 if (xendev
->ops
->connect
)
425 rc
= xendev
->ops
->connect(xendev
);
427 xen_be_printf(xendev
, 0, "connect() failed\n");
431 xen_be_set_state(xendev
, XenbusStateConnected
);
436 * Teardown connection.
438 * Goes to Closed when done.
440 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
442 if (xendev
->be_state
!= XenbusStateClosing
&&
443 xendev
->be_state
!= XenbusStateClosed
&&
444 xendev
->ops
->disconnect
)
445 xendev
->ops
->disconnect(xendev
);
446 if (xendev
->be_state
!= state
)
447 xen_be_set_state(xendev
, state
);
451 * Try to reset xendev, for reconnection by another frontend instance.
453 static int xen_be_try_reset(struct XenDevice
*xendev
)
455 if (xendev
->fe_state
!= XenbusStateInitialising
)
458 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
459 xen_be_set_state(xendev
, XenbusStateInitialising
);
464 * state change dispatcher function
466 void xen_be_check_state(struct XenDevice
*xendev
)
470 /* frontend may request shutdown from almost anywhere */
471 if (xendev
->fe_state
== XenbusStateClosing
||
472 xendev
->fe_state
== XenbusStateClosed
) {
473 xen_be_disconnect(xendev
, xendev
->fe_state
);
477 /* check for possible backend state transitions */
479 switch (xendev
->be_state
) {
480 case XenbusStateUnknown
:
481 rc
= xen_be_try_setup(xendev
);
483 case XenbusStateInitialising
:
484 rc
= xen_be_try_init(xendev
);
486 case XenbusStateInitWait
:
487 rc
= xen_be_try_connect(xendev
);
489 case XenbusStateClosed
:
490 rc
= xen_be_try_reset(xendev
);
500 /* ------------------------------------------------------------- */
502 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
504 struct XenDevice
*xendev
;
505 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
506 char **dev
= NULL
, *dom0
;
507 unsigned int cdev
, j
;
510 dom0
= xs_get_domain_path(xenstore
, 0);
511 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
512 snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
514 if (!xs_watch(xenstore
, path
, token
)) {
515 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
519 /* look for backends */
520 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
523 for (j
= 0; j
< cdev
; j
++) {
524 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
527 xen_be_check_state(xendev
);
533 static void xenstore_update_be(char *watch
, char *type
, int dom
,
534 struct XenDevOps
*ops
)
536 struct XenDevice
*xendev
;
537 char path
[XEN_BUFSIZE
], *dom0
;
538 unsigned int len
, dev
;
540 dom0
= xs_get_domain_path(xenstore
, 0);
541 len
= snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
543 if (strncmp(path
, watch
, len
) != 0)
545 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
547 if (sscanf(watch
+len
, "/%u", &dev
) != 1)
554 /* FIXME: detect devices being deleted from xenstore ... */
555 xen_be_del_xendev(dom
, dev
);
558 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
559 if (xendev
!= NULL
) {
560 xen_be_backend_changed(xendev
, path
);
561 xen_be_check_state(xendev
);
565 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
570 len
= strlen(xendev
->fe
);
571 if (strncmp(xendev
->fe
, watch
, len
) != 0)
573 if (watch
[len
] != '/')
575 node
= watch
+ len
+ 1;
577 xen_be_frontend_changed(xendev
, node
);
578 xen_be_check_state(xendev
);
581 static void xenstore_update(void *unused
)
584 intptr_t type
, ops
, ptr
;
585 unsigned int dom
, count
;
587 vec
= xs_read_watch(xenstore
, &count
);
591 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
592 &type
, &dom
, &ops
) == 3)
593 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
594 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1)
595 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
601 static void xen_be_evtchn_event(void *opaque
)
603 struct XenDevice
*xendev
= opaque
;
606 port
= xc_evtchn_pending(xendev
->evtchndev
);
607 if (port
!= xendev
->local_port
) {
608 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
609 port
, xendev
->local_port
);
612 xc_evtchn_unmask(xendev
->evtchndev
, port
);
614 if (xendev
->ops
->event
)
615 xendev
->ops
->event(xendev
);
618 /* -------------------------------------------------------------------- */
620 int xen_be_init(void)
622 xenstore
= xs_daemon_open();
624 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
628 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0)
631 xen_xc
= xc_interface_open();
633 xen_be_printf(NULL
, 0, "can't open xen interface\n");
639 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
640 xs_daemon_close(xenstore
);
646 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
648 return xenstore_scan(type
, xen_domid
, ops
);
651 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
653 if (xendev
->local_port
!= -1)
655 xendev
->local_port
= xc_evtchn_bind_interdomain
656 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
657 if (xendev
->local_port
== -1) {
658 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
661 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
662 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
663 xen_be_evtchn_event
, NULL
, xendev
);
667 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
669 if (xendev
->local_port
== -1)
671 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
672 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
673 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
674 xendev
->local_port
= -1;
677 int xen_be_send_notify(struct XenDevice
*xendev
)
679 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
684 * 0 == errors (stderr + logfile).
685 * 1 == informative debug messages (logfile only).
686 * 2 == noisy debug messages (logfile only).
687 * 3 == will flood your log (logfile only).
689 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
694 if (msg_level
> xendev
->debug
)
696 qemu_log("xen be: %s: ", xendev
->name
);
698 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
700 if (msg_level
> debug
)
702 qemu_log("xen be core: ");
704 fprintf(stderr
, "xen be core: ");
707 qemu_log_vprintf(fmt
, args
);
709 if (msg_level
== 0) {
711 vfprintf(stderr
, fmt
, args
);