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_read_uint64(const char *base
, const char *node
, uint64_t *uval
)
119 val
= xenstore_read_str(base
, node
);
120 if (val
&& 1 == sscanf(val
, "%"SCNu64
, uval
)) {
127 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
129 return xenstore_write_str(xendev
->be
, node
, val
);
132 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
134 return xenstore_write_int(xendev
->be
, node
, ival
);
137 int xenstore_write_be_int64(struct XenDevice
*xendev
, const char *node
, int64_t ival
)
139 return xenstore_write_int64(xendev
->be
, node
, ival
);
142 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
144 return xenstore_read_str(xendev
->be
, node
);
147 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
149 return xenstore_read_int(xendev
->be
, node
, ival
);
152 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
154 return xenstore_read_str(xendev
->fe
, node
);
157 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
159 return xenstore_read_int(xendev
->fe
, node
, ival
);
162 int xenstore_read_fe_uint64(struct XenDevice
*xendev
, const char *node
, uint64_t *uval
)
164 return xenstore_read_uint64(xendev
->fe
, node
, uval
);
167 /* ------------------------------------------------------------- */
169 const char *xenbus_strstate(enum xenbus_state state
)
171 static const char *const name
[] = {
172 [ XenbusStateUnknown
] = "Unknown",
173 [ XenbusStateInitialising
] = "Initialising",
174 [ XenbusStateInitWait
] = "InitWait",
175 [ XenbusStateInitialised
] = "Initialised",
176 [ XenbusStateConnected
] = "Connected",
177 [ XenbusStateClosing
] = "Closing",
178 [ XenbusStateClosed
] = "Closed",
180 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
183 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
187 rc
= xenstore_write_be_int(xendev
, "state", state
);
191 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
192 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
193 xendev
->be_state
= state
;
197 /* ------------------------------------------------------------- */
199 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
201 struct XenDevice
*xendev
;
203 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
204 if (xendev
->dom
!= dom
) {
207 if (xendev
->dev
!= dev
) {
210 if (strcmp(xendev
->type
, type
) != 0) {
219 * get xen backend device, allocate a new one if it doesn't exist.
221 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
222 struct XenDevOps
*ops
)
224 struct XenDevice
*xendev
;
226 xendev
= xen_be_find_xendev(type
, dom
, dev
);
231 /* init new xendev */
232 xendev
= g_malloc0(ops
->size
);
238 snprintf(xendev
->be
, sizeof(xendev
->be
), "backend/%s/%d/%d",
239 xendev
->type
, xendev
->dom
, xendev
->dev
);
240 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
241 xendev
->type
, xendev
->dev
);
243 xendev
->debug
= debug
;
244 xendev
->local_port
= -1;
246 xendev
->evtchndev
= xen_xc_evtchn_open(NULL
, 0);
247 if (xendev
->evtchndev
== XC_HANDLER_INITIAL_VALUE
) {
248 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
252 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
254 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
255 xendev
->gnttabdev
= xen_xc_gnttab_open(NULL
, 0);
256 if (xendev
->gnttabdev
== XC_HANDLER_INITIAL_VALUE
) {
257 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
258 xc_evtchn_close(xendev
->evtchndev
);
263 xendev
->gnttabdev
= XC_HANDLER_INITIAL_VALUE
;
266 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
268 if (xendev
->ops
->alloc
) {
269 xendev
->ops
->alloc(xendev
);
276 * release xen backend device.
278 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
280 struct XenDevice
*xendev
, *xnext
;
283 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
284 * we save the next pointer in xnext because we might free xendev.
286 xnext
= xendevs
.tqh_first
;
289 xnext
= xendev
->next
.tqe_next
;
291 if (xendev
->dom
!= dom
) {
294 if (xendev
->dev
!= dev
&& dev
!= -1) {
298 if (xendev
->ops
->free
) {
299 xendev
->ops
->free(xendev
);
303 char token
[XEN_BUFSIZE
];
304 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
305 xs_unwatch(xenstore
, xendev
->fe
, token
);
309 if (xendev
->evtchndev
!= XC_HANDLER_INITIAL_VALUE
) {
310 xc_evtchn_close(xendev
->evtchndev
);
312 if (xendev
->gnttabdev
!= XC_HANDLER_INITIAL_VALUE
) {
313 xc_gnttab_close(xendev
->gnttabdev
);
316 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
323 * Sync internal data structures on xenstore updates.
324 * Node specifies the changed field. node = NULL means
325 * update all fields (used for initialization).
327 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
329 if (node
== NULL
|| strcmp(node
, "online") == 0) {
330 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
336 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
337 if (xendev
->ops
->backend_changed
) {
338 xendev
->ops
->backend_changed(xendev
, node
);
343 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
347 if (node
== NULL
|| strcmp(node
, "state") == 0) {
348 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
349 fe_state
= XenbusStateUnknown
;
351 if (xendev
->fe_state
!= fe_state
) {
352 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
353 xenbus_strstate(xendev
->fe_state
),
354 xenbus_strstate(fe_state
));
356 xendev
->fe_state
= fe_state
;
358 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
359 g_free(xendev
->protocol
);
360 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
361 if (xendev
->protocol
) {
362 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
367 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
368 if (xendev
->ops
->frontend_changed
) {
369 xendev
->ops
->frontend_changed(xendev
, node
);
374 /* ------------------------------------------------------------- */
375 /* Check for possible state transitions and perform them. */
378 * Initial xendev setup. Read frontend path, register watch for it.
379 * Should succeed once xend finished setting up the backend device.
381 * Also sets initial state (-> Initializing) when done. Which
382 * only affects the xendev->be_state variable as xenbus should
383 * already be put into that state by xend.
385 static int xen_be_try_setup(struct XenDevice
*xendev
)
387 char token
[XEN_BUFSIZE
];
390 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
391 xen_be_printf(xendev
, 0, "reading backend state failed\n");
395 if (be_state
!= XenbusStateInitialising
) {
396 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
397 xenbus_strstate(be_state
));
401 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
402 if (xendev
->fe
== NULL
) {
403 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
407 /* setup frontend watch */
408 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
409 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
410 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
414 xen_be_set_state(xendev
, XenbusStateInitialising
);
416 xen_be_backend_changed(xendev
, NULL
);
417 xen_be_frontend_changed(xendev
, NULL
);
422 * Try initialize xendev. Prepare everything the backend can do
423 * without synchronizing with the frontend. Fakes hotplug-status. No
424 * hotplug involved here because this is about userspace drivers, thus
425 * there are kernel backend devices which could invoke hotplug.
427 * Goes to InitWait on success.
429 static int xen_be_try_init(struct XenDevice
*xendev
)
433 if (!xendev
->online
) {
434 xen_be_printf(xendev
, 1, "not online\n");
438 if (xendev
->ops
->init
) {
439 rc
= xendev
->ops
->init(xendev
);
442 xen_be_printf(xendev
, 1, "init() failed\n");
446 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
447 xen_be_set_state(xendev
, XenbusStateInitWait
);
452 * Try to initialise xendev. Depends on the frontend being ready
453 * for it (shared ring and evtchn info in xenstore, state being
454 * Initialised or Connected).
456 * Goes to Connected on success.
458 static int xen_be_try_initialise(struct XenDevice
*xendev
)
462 if (xendev
->fe_state
!= XenbusStateInitialised
&&
463 xendev
->fe_state
!= XenbusStateConnected
) {
464 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
465 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
467 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
472 if (xendev
->ops
->initialise
) {
473 rc
= xendev
->ops
->initialise(xendev
);
476 xen_be_printf(xendev
, 0, "initialise() failed\n");
480 xen_be_set_state(xendev
, XenbusStateConnected
);
485 * Try to let xendev know that it is connected. Depends on the
486 * frontend being Connected. Note that this may be called more
487 * than once since the backend state is not modified.
489 static void xen_be_try_connected(struct XenDevice
*xendev
)
491 if (!xendev
->ops
->connected
) {
495 if (xendev
->fe_state
!= XenbusStateConnected
) {
496 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
497 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
499 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
504 xendev
->ops
->connected(xendev
);
508 * Teardown connection.
510 * Goes to Closed when done.
512 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
514 if (xendev
->be_state
!= XenbusStateClosing
&&
515 xendev
->be_state
!= XenbusStateClosed
&&
516 xendev
->ops
->disconnect
) {
517 xendev
->ops
->disconnect(xendev
);
519 if (xendev
->be_state
!= state
) {
520 xen_be_set_state(xendev
, state
);
525 * Try to reset xendev, for reconnection by another frontend instance.
527 static int xen_be_try_reset(struct XenDevice
*xendev
)
529 if (xendev
->fe_state
!= XenbusStateInitialising
) {
533 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
534 xen_be_set_state(xendev
, XenbusStateInitialising
);
539 * state change dispatcher function
541 void xen_be_check_state(struct XenDevice
*xendev
)
545 /* frontend may request shutdown from almost anywhere */
546 if (xendev
->fe_state
== XenbusStateClosing
||
547 xendev
->fe_state
== XenbusStateClosed
) {
548 xen_be_disconnect(xendev
, xendev
->fe_state
);
552 /* check for possible backend state transitions */
554 switch (xendev
->be_state
) {
555 case XenbusStateUnknown
:
556 rc
= xen_be_try_setup(xendev
);
558 case XenbusStateInitialising
:
559 rc
= xen_be_try_init(xendev
);
561 case XenbusStateInitWait
:
562 rc
= xen_be_try_initialise(xendev
);
564 case XenbusStateConnected
:
565 /* xendev->be_state doesn't change */
566 xen_be_try_connected(xendev
);
569 case XenbusStateClosed
:
570 rc
= xen_be_try_reset(xendev
);
581 /* ------------------------------------------------------------- */
583 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
585 struct XenDevice
*xendev
;
586 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
588 unsigned int cdev
, j
;
591 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
592 snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
593 if (!xs_watch(xenstore
, path
, token
)) {
594 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
598 /* look for backends */
599 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
603 for (j
= 0; j
< cdev
; j
++) {
604 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
605 if (xendev
== NULL
) {
608 xen_be_check_state(xendev
);
614 static void xenstore_update_be(char *watch
, char *type
, int dom
,
615 struct XenDevOps
*ops
)
617 struct XenDevice
*xendev
;
618 char path
[XEN_BUFSIZE
], *bepath
;
619 unsigned int len
, dev
;
621 len
= snprintf(path
, sizeof(path
), "backend/%s/%d", type
, dom
);
622 if (strncmp(path
, watch
, len
) != 0) {
625 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
627 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
635 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
636 if (xendev
!= NULL
) {
637 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
638 if (bepath
== NULL
) {
639 xen_be_del_xendev(dom
, dev
);
642 xen_be_backend_changed(xendev
, path
);
643 xen_be_check_state(xendev
);
648 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
653 len
= strlen(xendev
->fe
);
654 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
657 if (watch
[len
] != '/') {
660 node
= watch
+ len
+ 1;
662 xen_be_frontend_changed(xendev
, node
);
663 xen_be_check_state(xendev
);
666 static void xenstore_update(void *unused
)
669 intptr_t type
, ops
, ptr
;
670 unsigned int dom
, count
;
672 vec
= xs_read_watch(xenstore
, &count
);
677 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
678 &type
, &dom
, &ops
) == 3) {
679 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
681 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
682 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
689 static void xen_be_evtchn_event(void *opaque
)
691 struct XenDevice
*xendev
= opaque
;
694 port
= xc_evtchn_pending(xendev
->evtchndev
);
695 if (port
!= xendev
->local_port
) {
696 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
697 port
, xendev
->local_port
);
700 xc_evtchn_unmask(xendev
->evtchndev
, port
);
702 if (xendev
->ops
->event
) {
703 xendev
->ops
->event(xendev
);
707 /* -------------------------------------------------------------------- */
709 int xen_be_init(void)
711 xenstore
= xs_daemon_open();
713 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
717 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0) {
721 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
722 /* Check if xen_init() have been called */
728 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
729 xs_daemon_close(xenstore
);
735 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
737 return xenstore_scan(type
, xen_domid
, ops
);
740 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
742 if (xendev
->local_port
!= -1) {
745 xendev
->local_port
= xc_evtchn_bind_interdomain
746 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
747 if (xendev
->local_port
== -1) {
748 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
751 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
752 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
753 xen_be_evtchn_event
, NULL
, xendev
);
757 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
759 if (xendev
->local_port
== -1) {
762 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
763 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
764 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
765 xendev
->local_port
= -1;
768 int xen_be_send_notify(struct XenDevice
*xendev
)
770 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
775 * 0 == errors (stderr + logfile).
776 * 1 == informative debug messages (logfile only).
777 * 2 == noisy debug messages (logfile only).
778 * 3 == will flood your log (logfile only).
780 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
785 if (msg_level
> xendev
->debug
) {
788 qemu_log("xen be: %s: ", xendev
->name
);
789 if (msg_level
== 0) {
790 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
793 if (msg_level
> debug
) {
796 qemu_log("xen be core: ");
797 if (msg_level
== 0) {
798 fprintf(stderr
, "xen be core: ");
802 qemu_log_vprintf(fmt
, args
);
804 if (msg_level
== 0) {
806 vfprintf(stderr
, fmt
, args
);