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/>.
19 * TODO: add some xenbus / xenstore concepts overview here.
29 #include <sys/types.h>
32 #include <sys/signal.h>
36 #include <xen/grant_table.h>
39 #include "qemu-char.h"
41 #include "xen_backend.h"
43 /* ------------------------------------------------------------- */
47 struct xs_handle
*xenstore
= NULL
;
48 const char *xen_protocol
;
51 static QTAILQ_HEAD(XenDeviceHead
, XenDevice
) xendevs
= QTAILQ_HEAD_INITIALIZER(xendevs
);
54 /* ------------------------------------------------------------- */
56 int xenstore_write_str(const char *base
, const char *node
, const char *val
)
58 char abspath
[XEN_BUFSIZE
];
60 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
61 if (!xs_write(xenstore
, 0, abspath
, val
, strlen(val
)))
66 char *xenstore_read_str(const char *base
, const char *node
)
68 char abspath
[XEN_BUFSIZE
];
70 char *str
, *ret
= NULL
;
72 snprintf(abspath
, sizeof(abspath
), "%s/%s", base
, node
);
73 str
= xs_read(xenstore
, 0, abspath
, &len
);
75 /* move to qemu-allocated memory to make sure
76 * callers can savely qemu_free() stuff. */
77 ret
= qemu_strdup(str
);
83 int xenstore_write_int(const char *base
, const char *node
, int ival
)
87 snprintf(val
, sizeof(val
), "%d", ival
);
88 return xenstore_write_str(base
, node
, val
);
91 int xenstore_read_int(const char *base
, const char *node
, int *ival
)
96 val
= xenstore_read_str(base
, node
);
97 if (val
&& 1 == sscanf(val
, "%d", ival
))
103 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
105 return xenstore_write_str(xendev
->be
, node
, val
);
108 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
110 return xenstore_write_int(xendev
->be
, node
, ival
);
113 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
115 return xenstore_read_str(xendev
->be
, node
);
118 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
120 return xenstore_read_int(xendev
->be
, node
, ival
);
123 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
125 return xenstore_read_str(xendev
->fe
, node
);
128 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
130 return xenstore_read_int(xendev
->fe
, node
, ival
);
133 /* ------------------------------------------------------------- */
135 const char *xenbus_strstate(enum xenbus_state state
)
137 static const char *const name
[] = {
138 [ XenbusStateUnknown
] = "Unknown",
139 [ XenbusStateInitialising
] = "Initialising",
140 [ XenbusStateInitWait
] = "InitWait",
141 [ XenbusStateInitialised
] = "Initialised",
142 [ XenbusStateConnected
] = "Connected",
143 [ XenbusStateClosing
] = "Closing",
144 [ XenbusStateClosed
] = "Closed",
146 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
149 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
153 rc
= xenstore_write_be_int(xendev
, "state", state
);
156 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
157 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
158 xendev
->be_state
= state
;
162 /* ------------------------------------------------------------- */
164 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
166 struct XenDevice
*xendev
;
168 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
169 if (xendev
->dom
!= dom
)
171 if (xendev
->dev
!= dev
)
173 if (strcmp(xendev
->type
, type
) != 0)
181 * get xen backend device, allocate a new one if it doesn't exist.
183 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
184 struct XenDevOps
*ops
)
186 struct XenDevice
*xendev
;
189 xendev
= xen_be_find_xendev(type
, dom
, dev
);
193 /* init new xendev */
194 xendev
= qemu_mallocz(ops
->size
);
200 dom0
= xs_get_domain_path(xenstore
, 0);
201 snprintf(xendev
->be
, sizeof(xendev
->be
), "%s/backend/%s/%d/%d",
202 dom0
, xendev
->type
, xendev
->dom
, xendev
->dev
);
203 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
204 xendev
->type
, xendev
->dev
);
207 xendev
->debug
= debug
;
208 xendev
->local_port
= -1;
210 xendev
->evtchndev
= xc_evtchn_open();
211 if (xendev
->evtchndev
< 0) {
212 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
216 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
218 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
219 xendev
->gnttabdev
= xc_gnttab_open();
220 if (xendev
->gnttabdev
< 0) {
221 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
222 xc_evtchn_close(xendev
->evtchndev
);
227 xendev
->gnttabdev
= -1;
230 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
232 if (xendev
->ops
->alloc
)
233 xendev
->ops
->alloc(xendev
);
239 * release xen backend device.
241 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
243 struct XenDevice
*xendev
, *xnext
;
246 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
247 * we save the next pointer in xnext because we might free xendev.
249 xnext
= xendevs
.tqh_first
;
252 xnext
= xendev
->next
.tqe_next
;
254 if (xendev
->dom
!= dom
)
256 if (xendev
->dev
!= dev
&& dev
!= -1)
259 if (xendev
->ops
->free
)
260 xendev
->ops
->free(xendev
);
263 char token
[XEN_BUFSIZE
];
264 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
265 xs_unwatch(xenstore
, xendev
->fe
, token
);
266 qemu_free(xendev
->fe
);
269 if (xendev
->evtchndev
>= 0)
270 xc_evtchn_close(xendev
->evtchndev
);
271 if (xendev
->gnttabdev
>= 0)
272 xc_gnttab_close(xendev
->gnttabdev
);
274 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
281 * Sync internal data structures on xenstore updates.
282 * Node specifies the changed field. node = NULL means
283 * update all fields (used for initialization).
285 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
287 if (node
== NULL
|| strcmp(node
, "online") == 0) {
288 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1)
293 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
294 if (xendev
->ops
->backend_changed
)
295 xendev
->ops
->backend_changed(xendev
, node
);
299 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
303 if (node
== NULL
|| strcmp(node
, "state") == 0) {
304 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1)
305 fe_state
= XenbusStateUnknown
;
306 if (xendev
->fe_state
!= fe_state
)
307 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
308 xenbus_strstate(xendev
->fe_state
),
309 xenbus_strstate(fe_state
));
310 xendev
->fe_state
= fe_state
;
312 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
313 qemu_free(xendev
->protocol
);
314 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
315 if (xendev
->protocol
)
316 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
320 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
321 if (xendev
->ops
->frontend_changed
)
322 xendev
->ops
->frontend_changed(xendev
, node
);
326 /* ------------------------------------------------------------- */
327 /* Check for possible state transitions and perform them. */
330 * Initial xendev setup. Read frontend path, register watch for it.
331 * Should succeed once xend finished setting up the backend device.
333 * Also sets initial state (-> Initializing) when done. Which
334 * only affects the xendev->be_state variable as xenbus should
335 * already be put into that state by xend.
337 static int xen_be_try_setup(struct XenDevice
*xendev
)
339 char token
[XEN_BUFSIZE
];
342 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
343 xen_be_printf(xendev
, 0, "reading backend state failed\n");
347 if (be_state
!= XenbusStateInitialising
) {
348 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
349 xenbus_strstate(be_state
));
353 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
354 if (xendev
->fe
== NULL
) {
355 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
359 /* setup frontend watch */
360 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
361 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
362 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
366 xen_be_set_state(xendev
, XenbusStateInitialising
);
368 xen_be_backend_changed(xendev
, NULL
);
369 xen_be_frontend_changed(xendev
, NULL
);
374 * Try initialize xendev. Prepare everything the backend can do
375 * without synchronizing with the frontend. Fakes hotplug-status. No
376 * hotplug involved here because this is about userspace drivers, thus
377 * there are kernel backend devices which could invoke hotplug.
379 * Goes to InitWait on success.
381 static int xen_be_try_init(struct XenDevice
*xendev
)
385 if (!xendev
->online
) {
386 xen_be_printf(xendev
, 1, "not online\n");
390 if (xendev
->ops
->init
)
391 rc
= xendev
->ops
->init(xendev
);
393 xen_be_printf(xendev
, 1, "init() failed\n");
397 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
398 xen_be_set_state(xendev
, XenbusStateInitWait
);
403 * Try to connect xendev. Depends on the frontend being ready
404 * for it (shared ring and evtchn info in xenstore, state being
405 * Initialised or Connected).
407 * Goes to Connected on success.
409 static int xen_be_try_connect(struct XenDevice
*xendev
)
413 if (xendev
->fe_state
!= XenbusStateInitialised
&&
414 xendev
->fe_state
!= XenbusStateConnected
) {
415 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
416 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
418 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
423 if (xendev
->ops
->connect
)
424 rc
= xendev
->ops
->connect(xendev
);
426 xen_be_printf(xendev
, 0, "connect() failed\n");
430 xen_be_set_state(xendev
, XenbusStateConnected
);
435 * Teardown connection.
437 * Goes to Closed when done.
439 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
441 if (xendev
->be_state
!= XenbusStateClosing
&&
442 xendev
->be_state
!= XenbusStateClosed
&&
443 xendev
->ops
->disconnect
)
444 xendev
->ops
->disconnect(xendev
);
445 if (xendev
->be_state
!= state
)
446 xen_be_set_state(xendev
, state
);
450 * Try to reset xendev, for reconnection by another frontend instance.
452 static int xen_be_try_reset(struct XenDevice
*xendev
)
454 if (xendev
->fe_state
!= XenbusStateInitialising
)
457 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
458 xen_be_set_state(xendev
, XenbusStateInitialising
);
463 * state change dispatcher function
465 void xen_be_check_state(struct XenDevice
*xendev
)
469 /* frontend may request shutdown from almost anywhere */
470 if (xendev
->fe_state
== XenbusStateClosing
||
471 xendev
->fe_state
== XenbusStateClosed
) {
472 xen_be_disconnect(xendev
, xendev
->fe_state
);
476 /* check for possible backend state transitions */
478 switch (xendev
->be_state
) {
479 case XenbusStateUnknown
:
480 rc
= xen_be_try_setup(xendev
);
482 case XenbusStateInitialising
:
483 rc
= xen_be_try_init(xendev
);
485 case XenbusStateInitWait
:
486 rc
= xen_be_try_connect(xendev
);
488 case XenbusStateClosed
:
489 rc
= xen_be_try_reset(xendev
);
499 /* ------------------------------------------------------------- */
501 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
503 struct XenDevice
*xendev
;
504 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
505 char **dev
= NULL
, *dom0
;
506 unsigned int cdev
, j
;
509 dom0
= xs_get_domain_path(xenstore
, 0);
510 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
511 snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
513 if (!xs_watch(xenstore
, path
, token
)) {
514 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
518 /* look for backends */
519 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
522 for (j
= 0; j
< cdev
; j
++) {
523 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
526 xen_be_check_state(xendev
);
532 static void xenstore_update_be(char *watch
, char *type
, int dom
,
533 struct XenDevOps
*ops
)
535 struct XenDevice
*xendev
;
536 char path
[XEN_BUFSIZE
], *dom0
;
537 unsigned int len
, dev
;
539 dom0
= xs_get_domain_path(xenstore
, 0);
540 len
= snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
542 if (strncmp(path
, watch
, len
) != 0)
544 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
546 if (sscanf(watch
+len
, "/%u", &dev
) != 1)
553 /* FIXME: detect devices being deleted from xenstore ... */
554 xen_be_del_xendev(dom
, dev
);
557 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
558 if (xendev
!= NULL
) {
559 xen_be_backend_changed(xendev
, path
);
560 xen_be_check_state(xendev
);
564 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
569 len
= strlen(xendev
->fe
);
570 if (strncmp(xendev
->fe
, watch
, len
) != 0)
572 if (watch
[len
] != '/')
574 node
= watch
+ len
+ 1;
576 xen_be_frontend_changed(xendev
, node
);
577 xen_be_check_state(xendev
);
580 static void xenstore_update(void *unused
)
583 intptr_t type
, ops
, ptr
;
584 unsigned int dom
, count
;
586 vec
= xs_read_watch(xenstore
, &count
);
590 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
591 &type
, &dom
, &ops
) == 3)
592 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
593 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1)
594 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
600 static void xen_be_evtchn_event(void *opaque
)
602 struct XenDevice
*xendev
= opaque
;
605 port
= xc_evtchn_pending(xendev
->evtchndev
);
606 if (port
!= xendev
->local_port
) {
607 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
608 port
, xendev
->local_port
);
611 xc_evtchn_unmask(xendev
->evtchndev
, port
);
613 if (xendev
->ops
->event
)
614 xendev
->ops
->event(xendev
);
617 /* -------------------------------------------------------------------- */
619 int xen_be_init(void)
621 xenstore
= xs_daemon_open();
623 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
627 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0)
630 xen_xc
= xc_interface_open();
632 xen_be_printf(NULL
, 0, "can't open xen interface\n");
638 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
639 xs_daemon_close(xenstore
);
645 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
647 return xenstore_scan(type
, xen_domid
, ops
);
650 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
652 if (xendev
->local_port
!= -1)
654 xendev
->local_port
= xc_evtchn_bind_interdomain
655 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
656 if (xendev
->local_port
== -1) {
657 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
660 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
661 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
662 xen_be_evtchn_event
, NULL
, xendev
);
666 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
668 if (xendev
->local_port
== -1)
670 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
671 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
672 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
673 xendev
->local_port
= -1;
676 int xen_be_send_notify(struct XenDevice
*xendev
)
678 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
683 * 0 == errors (stderr + logfile).
684 * 1 == informative debug messages (logfile only).
685 * 2 == noisy debug messages (logfile only).
686 * 3 == will flood your log (logfile only).
688 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
693 if (msg_level
> xendev
->debug
)
695 qemu_log("xen be: %s: ", xendev
->name
);
697 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
699 if (msg_level
> debug
)
701 qemu_log("xen be core: ");
703 fprintf(stderr
, "xen be core: ");
706 qemu_log_vprintf(fmt
, args
);
708 if (msg_level
== 0) {
710 vfprintf(stderr
, fmt
, args
);