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
))) {
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
)) {
105 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
107 return xenstore_write_str(xendev
->be
, node
, val
);
110 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
112 return xenstore_write_int(xendev
->be
, node
, ival
);
115 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
117 return xenstore_read_str(xendev
->be
, node
);
120 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
122 return xenstore_read_int(xendev
->be
, node
, ival
);
125 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
127 return xenstore_read_str(xendev
->fe
, node
);
130 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
132 return xenstore_read_int(xendev
->fe
, node
, ival
);
135 /* ------------------------------------------------------------- */
137 const char *xenbus_strstate(enum xenbus_state state
)
139 static const char *const name
[] = {
140 [ XenbusStateUnknown
] = "Unknown",
141 [ XenbusStateInitialising
] = "Initialising",
142 [ XenbusStateInitWait
] = "InitWait",
143 [ XenbusStateInitialised
] = "Initialised",
144 [ XenbusStateConnected
] = "Connected",
145 [ XenbusStateClosing
] = "Closing",
146 [ XenbusStateClosed
] = "Closed",
148 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
151 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
155 rc
= xenstore_write_be_int(xendev
, "state", state
);
159 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
160 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
161 xendev
->be_state
= state
;
165 /* ------------------------------------------------------------- */
167 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
169 struct XenDevice
*xendev
;
171 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
172 if (xendev
->dom
!= dom
) {
175 if (xendev
->dev
!= dev
) {
178 if (strcmp(xendev
->type
, type
) != 0) {
187 * get xen backend device, allocate a new one if it doesn't exist.
189 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
190 struct XenDevOps
*ops
)
192 struct XenDevice
*xendev
;
195 xendev
= xen_be_find_xendev(type
, dom
, dev
);
200 /* init new xendev */
201 xendev
= qemu_mallocz(ops
->size
);
207 dom0
= xs_get_domain_path(xenstore
, 0);
208 snprintf(xendev
->be
, sizeof(xendev
->be
), "%s/backend/%s/%d/%d",
209 dom0
, xendev
->type
, xendev
->dom
, xendev
->dev
);
210 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
211 xendev
->type
, xendev
->dev
);
214 xendev
->debug
= debug
;
215 xendev
->local_port
= -1;
217 xendev
->evtchndev
= xc_evtchn_open();
218 if (xendev
->evtchndev
< 0) {
219 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
223 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
225 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
226 xendev
->gnttabdev
= xc_gnttab_open();
227 if (xendev
->gnttabdev
< 0) {
228 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
229 xc_evtchn_close(xendev
->evtchndev
);
234 xendev
->gnttabdev
= -1;
237 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
239 if (xendev
->ops
->alloc
) {
240 xendev
->ops
->alloc(xendev
);
247 * release xen backend device.
249 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
251 struct XenDevice
*xendev
, *xnext
;
254 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
255 * we save the next pointer in xnext because we might free xendev.
257 xnext
= xendevs
.tqh_first
;
260 xnext
= xendev
->next
.tqe_next
;
262 if (xendev
->dom
!= dom
) {
265 if (xendev
->dev
!= dev
&& dev
!= -1) {
269 if (xendev
->ops
->free
) {
270 xendev
->ops
->free(xendev
);
274 char token
[XEN_BUFSIZE
];
275 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
276 xs_unwatch(xenstore
, xendev
->fe
, token
);
277 qemu_free(xendev
->fe
);
280 if (xendev
->evtchndev
>= 0) {
281 xc_evtchn_close(xendev
->evtchndev
);
283 if (xendev
->gnttabdev
>= 0) {
284 xc_gnttab_close(xendev
->gnttabdev
);
287 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
294 * Sync internal data structures on xenstore updates.
295 * Node specifies the changed field. node = NULL means
296 * update all fields (used for initialization).
298 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
300 if (node
== NULL
|| strcmp(node
, "online") == 0) {
301 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
307 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
308 if (xendev
->ops
->backend_changed
) {
309 xendev
->ops
->backend_changed(xendev
, node
);
314 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
318 if (node
== NULL
|| strcmp(node
, "state") == 0) {
319 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
320 fe_state
= XenbusStateUnknown
;
322 if (xendev
->fe_state
!= fe_state
) {
323 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
324 xenbus_strstate(xendev
->fe_state
),
325 xenbus_strstate(fe_state
));
327 xendev
->fe_state
= fe_state
;
329 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
330 qemu_free(xendev
->protocol
);
331 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
332 if (xendev
->protocol
) {
333 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
338 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
339 if (xendev
->ops
->frontend_changed
) {
340 xendev
->ops
->frontend_changed(xendev
, node
);
345 /* ------------------------------------------------------------- */
346 /* Check for possible state transitions and perform them. */
349 * Initial xendev setup. Read frontend path, register watch for it.
350 * Should succeed once xend finished setting up the backend device.
352 * Also sets initial state (-> Initializing) when done. Which
353 * only affects the xendev->be_state variable as xenbus should
354 * already be put into that state by xend.
356 static int xen_be_try_setup(struct XenDevice
*xendev
)
358 char token
[XEN_BUFSIZE
];
361 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
362 xen_be_printf(xendev
, 0, "reading backend state failed\n");
366 if (be_state
!= XenbusStateInitialising
) {
367 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
368 xenbus_strstate(be_state
));
372 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
373 if (xendev
->fe
== NULL
) {
374 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
378 /* setup frontend watch */
379 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
380 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
381 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
385 xen_be_set_state(xendev
, XenbusStateInitialising
);
387 xen_be_backend_changed(xendev
, NULL
);
388 xen_be_frontend_changed(xendev
, NULL
);
393 * Try initialize xendev. Prepare everything the backend can do
394 * without synchronizing with the frontend. Fakes hotplug-status. No
395 * hotplug involved here because this is about userspace drivers, thus
396 * there are kernel backend devices which could invoke hotplug.
398 * Goes to InitWait on success.
400 static int xen_be_try_init(struct XenDevice
*xendev
)
404 if (!xendev
->online
) {
405 xen_be_printf(xendev
, 1, "not online\n");
409 if (xendev
->ops
->init
) {
410 rc
= xendev
->ops
->init(xendev
);
413 xen_be_printf(xendev
, 1, "init() failed\n");
417 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
418 xen_be_set_state(xendev
, XenbusStateInitWait
);
423 * Try to connect xendev. Depends on the frontend being ready
424 * for it (shared ring and evtchn info in xenstore, state being
425 * Initialised or Connected).
427 * Goes to Connected on success.
429 static int xen_be_try_connect(struct XenDevice
*xendev
)
433 if (xendev
->fe_state
!= XenbusStateInitialised
&&
434 xendev
->fe_state
!= XenbusStateConnected
) {
435 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
436 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
438 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
443 if (xendev
->ops
->connect
) {
444 rc
= xendev
->ops
->connect(xendev
);
447 xen_be_printf(xendev
, 0, "connect() failed\n");
451 xen_be_set_state(xendev
, XenbusStateConnected
);
456 * Teardown connection.
458 * Goes to Closed when done.
460 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
462 if (xendev
->be_state
!= XenbusStateClosing
&&
463 xendev
->be_state
!= XenbusStateClosed
&&
464 xendev
->ops
->disconnect
) {
465 xendev
->ops
->disconnect(xendev
);
467 if (xendev
->be_state
!= state
) {
468 xen_be_set_state(xendev
, state
);
473 * Try to reset xendev, for reconnection by another frontend instance.
475 static int xen_be_try_reset(struct XenDevice
*xendev
)
477 if (xendev
->fe_state
!= XenbusStateInitialising
) {
481 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
482 xen_be_set_state(xendev
, XenbusStateInitialising
);
487 * state change dispatcher function
489 void xen_be_check_state(struct XenDevice
*xendev
)
493 /* frontend may request shutdown from almost anywhere */
494 if (xendev
->fe_state
== XenbusStateClosing
||
495 xendev
->fe_state
== XenbusStateClosed
) {
496 xen_be_disconnect(xendev
, xendev
->fe_state
);
500 /* check for possible backend state transitions */
502 switch (xendev
->be_state
) {
503 case XenbusStateUnknown
:
504 rc
= xen_be_try_setup(xendev
);
506 case XenbusStateInitialising
:
507 rc
= xen_be_try_init(xendev
);
509 case XenbusStateInitWait
:
510 rc
= xen_be_try_connect(xendev
);
512 case XenbusStateClosed
:
513 rc
= xen_be_try_reset(xendev
);
524 /* ------------------------------------------------------------- */
526 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
528 struct XenDevice
*xendev
;
529 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
530 char **dev
= NULL
, *dom0
;
531 unsigned int cdev
, j
;
534 dom0
= xs_get_domain_path(xenstore
, 0);
535 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
536 snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
538 if (!xs_watch(xenstore
, path
, token
)) {
539 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
543 /* look for backends */
544 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
548 for (j
= 0; j
< cdev
; j
++) {
549 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
550 if (xendev
== NULL
) {
553 xen_be_check_state(xendev
);
559 static void xenstore_update_be(char *watch
, char *type
, int dom
,
560 struct XenDevOps
*ops
)
562 struct XenDevice
*xendev
;
563 char path
[XEN_BUFSIZE
], *dom0
;
564 unsigned int len
, dev
;
566 dom0
= xs_get_domain_path(xenstore
, 0);
567 len
= snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
569 if (strncmp(path
, watch
, len
) != 0) {
572 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
574 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
583 /* FIXME: detect devices being deleted from xenstore ... */
584 xen_be_del_xendev(dom
, dev
);
587 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
588 if (xendev
!= NULL
) {
589 xen_be_backend_changed(xendev
, path
);
590 xen_be_check_state(xendev
);
594 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
599 len
= strlen(xendev
->fe
);
600 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
603 if (watch
[len
] != '/') {
606 node
= watch
+ len
+ 1;
608 xen_be_frontend_changed(xendev
, node
);
609 xen_be_check_state(xendev
);
612 static void xenstore_update(void *unused
)
615 intptr_t type
, ops
, ptr
;
616 unsigned int dom
, count
;
618 vec
= xs_read_watch(xenstore
, &count
);
623 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
624 &type
, &dom
, &ops
) == 3) {
625 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
627 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
628 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
635 static void xen_be_evtchn_event(void *opaque
)
637 struct XenDevice
*xendev
= opaque
;
640 port
= xc_evtchn_pending(xendev
->evtchndev
);
641 if (port
!= xendev
->local_port
) {
642 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
643 port
, xendev
->local_port
);
646 xc_evtchn_unmask(xendev
->evtchndev
, port
);
648 if (xendev
->ops
->event
) {
649 xendev
->ops
->event(xendev
);
653 /* -------------------------------------------------------------------- */
655 int xen_be_init(void)
657 xenstore
= xs_daemon_open();
659 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
663 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0) {
667 xen_xc
= xc_interface_open();
669 xen_be_printf(NULL
, 0, "can't open xen interface\n");
675 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
676 xs_daemon_close(xenstore
);
682 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
684 return xenstore_scan(type
, xen_domid
, ops
);
687 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
689 if (xendev
->local_port
!= -1) {
692 xendev
->local_port
= xc_evtchn_bind_interdomain
693 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
694 if (xendev
->local_port
== -1) {
695 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
698 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
699 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
700 xen_be_evtchn_event
, NULL
, xendev
);
704 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
706 if (xendev
->local_port
== -1) {
709 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
710 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
711 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
712 xendev
->local_port
= -1;
715 int xen_be_send_notify(struct XenDevice
*xendev
)
717 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
722 * 0 == errors (stderr + logfile).
723 * 1 == informative debug messages (logfile only).
724 * 2 == noisy debug messages (logfile only).
725 * 3 == will flood your log (logfile only).
727 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
732 if (msg_level
> xendev
->debug
) {
735 qemu_log("xen be: %s: ", xendev
->name
);
736 if (msg_level
== 0) {
737 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
740 if (msg_level
> debug
) {
743 qemu_log("xen be core: ");
744 if (msg_level
== 0) {
745 fprintf(stderr
, "xen be core: ");
749 qemu_log_vprintf(fmt
, args
);
751 if (msg_level
== 0) {
753 vfprintf(stderr
, fmt
, args
);