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 "qemu-char.h"
40 #include "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_read_int(const char *base
, const char *node
, int *ival
)
99 val
= xenstore_read_str(base
, node
);
100 if (val
&& 1 == sscanf(val
, "%d", ival
)) {
107 int xenstore_write_be_str(struct XenDevice
*xendev
, const char *node
, const char *val
)
109 return xenstore_write_str(xendev
->be
, node
, val
);
112 int xenstore_write_be_int(struct XenDevice
*xendev
, const char *node
, int ival
)
114 return xenstore_write_int(xendev
->be
, node
, ival
);
117 char *xenstore_read_be_str(struct XenDevice
*xendev
, const char *node
)
119 return xenstore_read_str(xendev
->be
, node
);
122 int xenstore_read_be_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
124 return xenstore_read_int(xendev
->be
, node
, ival
);
127 char *xenstore_read_fe_str(struct XenDevice
*xendev
, const char *node
)
129 return xenstore_read_str(xendev
->fe
, node
);
132 int xenstore_read_fe_int(struct XenDevice
*xendev
, const char *node
, int *ival
)
134 return xenstore_read_int(xendev
->fe
, node
, ival
);
137 /* ------------------------------------------------------------- */
139 const char *xenbus_strstate(enum xenbus_state state
)
141 static const char *const name
[] = {
142 [ XenbusStateUnknown
] = "Unknown",
143 [ XenbusStateInitialising
] = "Initialising",
144 [ XenbusStateInitWait
] = "InitWait",
145 [ XenbusStateInitialised
] = "Initialised",
146 [ XenbusStateConnected
] = "Connected",
147 [ XenbusStateClosing
] = "Closing",
148 [ XenbusStateClosed
] = "Closed",
150 return (state
< ARRAY_SIZE(name
)) ? name
[state
] : "INVALID";
153 int xen_be_set_state(struct XenDevice
*xendev
, enum xenbus_state state
)
157 rc
= xenstore_write_be_int(xendev
, "state", state
);
161 xen_be_printf(xendev
, 1, "backend state: %s -> %s\n",
162 xenbus_strstate(xendev
->be_state
), xenbus_strstate(state
));
163 xendev
->be_state
= state
;
167 /* ------------------------------------------------------------- */
169 struct XenDevice
*xen_be_find_xendev(const char *type
, int dom
, int dev
)
171 struct XenDevice
*xendev
;
173 QTAILQ_FOREACH(xendev
, &xendevs
, next
) {
174 if (xendev
->dom
!= dom
) {
177 if (xendev
->dev
!= dev
) {
180 if (strcmp(xendev
->type
, type
) != 0) {
189 * get xen backend device, allocate a new one if it doesn't exist.
191 static struct XenDevice
*xen_be_get_xendev(const char *type
, int dom
, int dev
,
192 struct XenDevOps
*ops
)
194 struct XenDevice
*xendev
;
197 xendev
= xen_be_find_xendev(type
, dom
, dev
);
202 /* init new xendev */
203 xendev
= g_malloc0(ops
->size
);
209 dom0
= xs_get_domain_path(xenstore
, 0);
210 snprintf(xendev
->be
, sizeof(xendev
->be
), "%s/backend/%s/%d/%d",
211 dom0
, xendev
->type
, xendev
->dom
, xendev
->dev
);
212 snprintf(xendev
->name
, sizeof(xendev
->name
), "%s-%d",
213 xendev
->type
, xendev
->dev
);
216 xendev
->debug
= debug
;
217 xendev
->local_port
= -1;
219 xendev
->evtchndev
= xen_xc_evtchn_open(NULL
, 0);
220 if (xendev
->evtchndev
== XC_HANDLER_INITIAL_VALUE
) {
221 xen_be_printf(NULL
, 0, "can't open evtchn device\n");
225 fcntl(xc_evtchn_fd(xendev
->evtchndev
), F_SETFD
, FD_CLOEXEC
);
227 if (ops
->flags
& DEVOPS_FLAG_NEED_GNTDEV
) {
228 xendev
->gnttabdev
= xen_xc_gnttab_open(NULL
, 0);
229 if (xendev
->gnttabdev
== XC_HANDLER_INITIAL_VALUE
) {
230 xen_be_printf(NULL
, 0, "can't open gnttab device\n");
231 xc_evtchn_close(xendev
->evtchndev
);
236 xendev
->gnttabdev
= XC_HANDLER_INITIAL_VALUE
;
239 QTAILQ_INSERT_TAIL(&xendevs
, xendev
, next
);
241 if (xendev
->ops
->alloc
) {
242 xendev
->ops
->alloc(xendev
);
249 * release xen backend device.
251 static struct XenDevice
*xen_be_del_xendev(int dom
, int dev
)
253 struct XenDevice
*xendev
, *xnext
;
256 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
257 * we save the next pointer in xnext because we might free xendev.
259 xnext
= xendevs
.tqh_first
;
262 xnext
= xendev
->next
.tqe_next
;
264 if (xendev
->dom
!= dom
) {
267 if (xendev
->dev
!= dev
&& dev
!= -1) {
271 if (xendev
->ops
->free
) {
272 xendev
->ops
->free(xendev
);
276 char token
[XEN_BUFSIZE
];
277 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
278 xs_unwatch(xenstore
, xendev
->fe
, token
);
282 if (xendev
->evtchndev
!= XC_HANDLER_INITIAL_VALUE
) {
283 xc_evtchn_close(xendev
->evtchndev
);
285 if (xendev
->gnttabdev
!= XC_HANDLER_INITIAL_VALUE
) {
286 xc_gnttab_close(xendev
->gnttabdev
);
289 QTAILQ_REMOVE(&xendevs
, xendev
, next
);
296 * Sync internal data structures on xenstore updates.
297 * Node specifies the changed field. node = NULL means
298 * update all fields (used for initialization).
300 static void xen_be_backend_changed(struct XenDevice
*xendev
, const char *node
)
302 if (node
== NULL
|| strcmp(node
, "online") == 0) {
303 if (xenstore_read_be_int(xendev
, "online", &xendev
->online
) == -1) {
309 xen_be_printf(xendev
, 2, "backend update: %s\n", node
);
310 if (xendev
->ops
->backend_changed
) {
311 xendev
->ops
->backend_changed(xendev
, node
);
316 static void xen_be_frontend_changed(struct XenDevice
*xendev
, const char *node
)
320 if (node
== NULL
|| strcmp(node
, "state") == 0) {
321 if (xenstore_read_fe_int(xendev
, "state", &fe_state
) == -1) {
322 fe_state
= XenbusStateUnknown
;
324 if (xendev
->fe_state
!= fe_state
) {
325 xen_be_printf(xendev
, 1, "frontend state: %s -> %s\n",
326 xenbus_strstate(xendev
->fe_state
),
327 xenbus_strstate(fe_state
));
329 xendev
->fe_state
= fe_state
;
331 if (node
== NULL
|| strcmp(node
, "protocol") == 0) {
332 g_free(xendev
->protocol
);
333 xendev
->protocol
= xenstore_read_fe_str(xendev
, "protocol");
334 if (xendev
->protocol
) {
335 xen_be_printf(xendev
, 1, "frontend protocol: %s\n", xendev
->protocol
);
340 xen_be_printf(xendev
, 2, "frontend update: %s\n", node
);
341 if (xendev
->ops
->frontend_changed
) {
342 xendev
->ops
->frontend_changed(xendev
, node
);
347 /* ------------------------------------------------------------- */
348 /* Check for possible state transitions and perform them. */
351 * Initial xendev setup. Read frontend path, register watch for it.
352 * Should succeed once xend finished setting up the backend device.
354 * Also sets initial state (-> Initializing) when done. Which
355 * only affects the xendev->be_state variable as xenbus should
356 * already be put into that state by xend.
358 static int xen_be_try_setup(struct XenDevice
*xendev
)
360 char token
[XEN_BUFSIZE
];
363 if (xenstore_read_be_int(xendev
, "state", &be_state
) == -1) {
364 xen_be_printf(xendev
, 0, "reading backend state failed\n");
368 if (be_state
!= XenbusStateInitialising
) {
369 xen_be_printf(xendev
, 0, "initial backend state is wrong (%s)\n",
370 xenbus_strstate(be_state
));
374 xendev
->fe
= xenstore_read_be_str(xendev
, "frontend");
375 if (xendev
->fe
== NULL
) {
376 xen_be_printf(xendev
, 0, "reading frontend path failed\n");
380 /* setup frontend watch */
381 snprintf(token
, sizeof(token
), "fe:%p", xendev
);
382 if (!xs_watch(xenstore
, xendev
->fe
, token
)) {
383 xen_be_printf(xendev
, 0, "watching frontend path (%s) failed\n",
387 xen_be_set_state(xendev
, XenbusStateInitialising
);
389 xen_be_backend_changed(xendev
, NULL
);
390 xen_be_frontend_changed(xendev
, NULL
);
395 * Try initialize xendev. Prepare everything the backend can do
396 * without synchronizing with the frontend. Fakes hotplug-status. No
397 * hotplug involved here because this is about userspace drivers, thus
398 * there are kernel backend devices which could invoke hotplug.
400 * Goes to InitWait on success.
402 static int xen_be_try_init(struct XenDevice
*xendev
)
406 if (!xendev
->online
) {
407 xen_be_printf(xendev
, 1, "not online\n");
411 if (xendev
->ops
->init
) {
412 rc
= xendev
->ops
->init(xendev
);
415 xen_be_printf(xendev
, 1, "init() failed\n");
419 xenstore_write_be_str(xendev
, "hotplug-status", "connected");
420 xen_be_set_state(xendev
, XenbusStateInitWait
);
425 * Try to initialise xendev. Depends on the frontend being ready
426 * for it (shared ring and evtchn info in xenstore, state being
427 * Initialised or Connected).
429 * Goes to Connected on success.
431 static int xen_be_try_initialise(struct XenDevice
*xendev
)
435 if (xendev
->fe_state
!= XenbusStateInitialised
&&
436 xendev
->fe_state
!= XenbusStateConnected
) {
437 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
438 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
440 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
445 if (xendev
->ops
->initialise
) {
446 rc
= xendev
->ops
->initialise(xendev
);
449 xen_be_printf(xendev
, 0, "initialise() failed\n");
453 xen_be_set_state(xendev
, XenbusStateConnected
);
458 * Try to let xendev know that it is connected. Depends on the
459 * frontend being Connected. Note that this may be called more
460 * than once since the backend state is not modified.
462 static void xen_be_try_connected(struct XenDevice
*xendev
)
464 if (!xendev
->ops
->connected
) {
468 if (xendev
->fe_state
!= XenbusStateConnected
) {
469 if (xendev
->ops
->flags
& DEVOPS_FLAG_IGNORE_STATE
) {
470 xen_be_printf(xendev
, 2, "frontend not ready, ignoring\n");
472 xen_be_printf(xendev
, 2, "frontend not ready (yet)\n");
477 xendev
->ops
->connected(xendev
);
481 * Teardown connection.
483 * Goes to Closed when done.
485 static void xen_be_disconnect(struct XenDevice
*xendev
, enum xenbus_state state
)
487 if (xendev
->be_state
!= XenbusStateClosing
&&
488 xendev
->be_state
!= XenbusStateClosed
&&
489 xendev
->ops
->disconnect
) {
490 xendev
->ops
->disconnect(xendev
);
492 if (xendev
->be_state
!= state
) {
493 xen_be_set_state(xendev
, state
);
498 * Try to reset xendev, for reconnection by another frontend instance.
500 static int xen_be_try_reset(struct XenDevice
*xendev
)
502 if (xendev
->fe_state
!= XenbusStateInitialising
) {
506 xen_be_printf(xendev
, 1, "device reset (for re-connect)\n");
507 xen_be_set_state(xendev
, XenbusStateInitialising
);
512 * state change dispatcher function
514 void xen_be_check_state(struct XenDevice
*xendev
)
518 /* frontend may request shutdown from almost anywhere */
519 if (xendev
->fe_state
== XenbusStateClosing
||
520 xendev
->fe_state
== XenbusStateClosed
) {
521 xen_be_disconnect(xendev
, xendev
->fe_state
);
525 /* check for possible backend state transitions */
527 switch (xendev
->be_state
) {
528 case XenbusStateUnknown
:
529 rc
= xen_be_try_setup(xendev
);
531 case XenbusStateInitialising
:
532 rc
= xen_be_try_init(xendev
);
534 case XenbusStateInitWait
:
535 rc
= xen_be_try_initialise(xendev
);
537 case XenbusStateConnected
:
538 /* xendev->be_state doesn't change */
539 xen_be_try_connected(xendev
);
542 case XenbusStateClosed
:
543 rc
= xen_be_try_reset(xendev
);
554 /* ------------------------------------------------------------- */
556 static int xenstore_scan(const char *type
, int dom
, struct XenDevOps
*ops
)
558 struct XenDevice
*xendev
;
559 char path
[XEN_BUFSIZE
], token
[XEN_BUFSIZE
];
560 char **dev
= NULL
, *dom0
;
561 unsigned int cdev
, j
;
564 dom0
= xs_get_domain_path(xenstore
, 0);
565 snprintf(token
, sizeof(token
), "be:%p:%d:%p", type
, dom
, ops
);
566 snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
568 if (!xs_watch(xenstore
, path
, token
)) {
569 xen_be_printf(NULL
, 0, "xen be: watching backend path (%s) failed\n", path
);
573 /* look for backends */
574 dev
= xs_directory(xenstore
, 0, path
, &cdev
);
578 for (j
= 0; j
< cdev
; j
++) {
579 xendev
= xen_be_get_xendev(type
, dom
, atoi(dev
[j
]), ops
);
580 if (xendev
== NULL
) {
583 xen_be_check_state(xendev
);
589 static void xenstore_update_be(char *watch
, char *type
, int dom
,
590 struct XenDevOps
*ops
)
592 struct XenDevice
*xendev
;
593 char path
[XEN_BUFSIZE
], *dom0
, *bepath
;
594 unsigned int len
, dev
;
596 dom0
= xs_get_domain_path(xenstore
, 0);
597 len
= snprintf(path
, sizeof(path
), "%s/backend/%s/%d", dom0
, type
, dom
);
599 if (strncmp(path
, watch
, len
) != 0) {
602 if (sscanf(watch
+len
, "/%u/%255s", &dev
, path
) != 2) {
604 if (sscanf(watch
+len
, "/%u", &dev
) != 1) {
612 xendev
= xen_be_get_xendev(type
, dom
, dev
, ops
);
613 if (xendev
!= NULL
) {
614 bepath
= xs_read(xenstore
, 0, xendev
->be
, &len
);
615 if (bepath
== NULL
) {
616 xen_be_del_xendev(dom
, dev
);
619 xen_be_backend_changed(xendev
, path
);
620 xen_be_check_state(xendev
);
625 static void xenstore_update_fe(char *watch
, struct XenDevice
*xendev
)
630 len
= strlen(xendev
->fe
);
631 if (strncmp(xendev
->fe
, watch
, len
) != 0) {
634 if (watch
[len
] != '/') {
637 node
= watch
+ len
+ 1;
639 xen_be_frontend_changed(xendev
, node
);
640 xen_be_check_state(xendev
);
643 static void xenstore_update(void *unused
)
646 intptr_t type
, ops
, ptr
;
647 unsigned int dom
, count
;
649 vec
= xs_read_watch(xenstore
, &count
);
654 if (sscanf(vec
[XS_WATCH_TOKEN
], "be:%" PRIxPTR
":%d:%" PRIxPTR
,
655 &type
, &dom
, &ops
) == 3) {
656 xenstore_update_be(vec
[XS_WATCH_PATH
], (void*)type
, dom
, (void*)ops
);
658 if (sscanf(vec
[XS_WATCH_TOKEN
], "fe:%" PRIxPTR
, &ptr
) == 1) {
659 xenstore_update_fe(vec
[XS_WATCH_PATH
], (void*)ptr
);
666 static void xen_be_evtchn_event(void *opaque
)
668 struct XenDevice
*xendev
= opaque
;
671 port
= xc_evtchn_pending(xendev
->evtchndev
);
672 if (port
!= xendev
->local_port
) {
673 xen_be_printf(xendev
, 0, "xc_evtchn_pending returned %d (expected %d)\n",
674 port
, xendev
->local_port
);
677 xc_evtchn_unmask(xendev
->evtchndev
, port
);
679 if (xendev
->ops
->event
) {
680 xendev
->ops
->event(xendev
);
684 /* -------------------------------------------------------------------- */
686 int xen_be_init(void)
688 xenstore
= xs_daemon_open();
690 xen_be_printf(NULL
, 0, "can't connect to xenstored\n");
694 if (qemu_set_fd_handler(xs_fileno(xenstore
), xenstore_update
, NULL
, NULL
) < 0) {
698 if (xen_xc
== XC_HANDLER_INITIAL_VALUE
) {
699 /* Check if xen_init() have been called */
705 qemu_set_fd_handler(xs_fileno(xenstore
), NULL
, NULL
, NULL
);
706 xs_daemon_close(xenstore
);
712 int xen_be_register(const char *type
, struct XenDevOps
*ops
)
714 return xenstore_scan(type
, xen_domid
, ops
);
717 int xen_be_bind_evtchn(struct XenDevice
*xendev
)
719 if (xendev
->local_port
!= -1) {
722 xendev
->local_port
= xc_evtchn_bind_interdomain
723 (xendev
->evtchndev
, xendev
->dom
, xendev
->remote_port
);
724 if (xendev
->local_port
== -1) {
725 xen_be_printf(xendev
, 0, "xc_evtchn_bind_interdomain failed\n");
728 xen_be_printf(xendev
, 2, "bind evtchn port %d\n", xendev
->local_port
);
729 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
),
730 xen_be_evtchn_event
, NULL
, xendev
);
734 void xen_be_unbind_evtchn(struct XenDevice
*xendev
)
736 if (xendev
->local_port
== -1) {
739 qemu_set_fd_handler(xc_evtchn_fd(xendev
->evtchndev
), NULL
, NULL
, NULL
);
740 xc_evtchn_unbind(xendev
->evtchndev
, xendev
->local_port
);
741 xen_be_printf(xendev
, 2, "unbind evtchn port %d\n", xendev
->local_port
);
742 xendev
->local_port
= -1;
745 int xen_be_send_notify(struct XenDevice
*xendev
)
747 return xc_evtchn_notify(xendev
->evtchndev
, xendev
->local_port
);
752 * 0 == errors (stderr + logfile).
753 * 1 == informative debug messages (logfile only).
754 * 2 == noisy debug messages (logfile only).
755 * 3 == will flood your log (logfile only).
757 void xen_be_printf(struct XenDevice
*xendev
, int msg_level
, const char *fmt
, ...)
762 if (msg_level
> xendev
->debug
) {
765 qemu_log("xen be: %s: ", xendev
->name
);
766 if (msg_level
== 0) {
767 fprintf(stderr
, "xen be: %s: ", xendev
->name
);
770 if (msg_level
> debug
) {
773 qemu_log("xen be core: ");
774 if (msg_level
== 0) {
775 fprintf(stderr
, "xen be core: ");
779 qemu_log_vprintf(fmt
, args
);
781 if (msg_level
== 0) {
783 vfprintf(stderr
, fmt
, args
);