remove comment for nonexistent structure member
[qemu.git] / hw / xen / xen_backend.c
blob60575ad38db0da5aa6afbb0428453c462020cf38
1 /*
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.
25 #include "qemu/osdep.h"
26 #include <sys/mman.h>
27 #include <sys/signal.h>
29 #include "hw/hw.h"
30 #include "sysemu/char.h"
31 #include "qemu/log.h"
32 #include "hw/xen/xen_backend.h"
34 #include <xen/grant_table.h>
36 /* ------------------------------------------------------------- */
38 /* public */
39 xc_interface *xen_xc = NULL;
40 xenforeignmemory_handle *xen_fmem = NULL;
41 struct xs_handle *xenstore = NULL;
42 const char *xen_protocol;
44 /* private */
45 static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
46 static int debug = 0;
48 /* ------------------------------------------------------------- */
50 int xenstore_write_str(const char *base, const char *node, const char *val)
52 char abspath[XEN_BUFSIZE];
54 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
55 if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
56 return -1;
58 return 0;
61 char *xenstore_read_str(const char *base, const char *node)
63 char abspath[XEN_BUFSIZE];
64 unsigned int len;
65 char *str, *ret = NULL;
67 snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
68 str = xs_read(xenstore, 0, abspath, &len);
69 if (str != NULL) {
70 /* move to qemu-allocated memory to make sure
71 * callers can savely g_free() stuff. */
72 ret = g_strdup(str);
73 free(str);
75 return ret;
78 int xenstore_write_int(const char *base, const char *node, int ival)
80 char val[12];
82 snprintf(val, sizeof(val), "%d", ival);
83 return xenstore_write_str(base, node, val);
86 int xenstore_write_int64(const char *base, const char *node, int64_t ival)
88 char val[21];
90 snprintf(val, sizeof(val), "%"PRId64, ival);
91 return xenstore_write_str(base, node, val);
94 int xenstore_read_int(const char *base, const char *node, int *ival)
96 char *val;
97 int rc = -1;
99 val = xenstore_read_str(base, node);
100 if (val && 1 == sscanf(val, "%d", ival)) {
101 rc = 0;
103 g_free(val);
104 return rc;
107 int xenstore_read_uint64(const char *base, const char *node, uint64_t *uval)
109 char *val;
110 int rc = -1;
112 val = xenstore_read_str(base, node);
113 if (val && 1 == sscanf(val, "%"SCNu64, uval)) {
114 rc = 0;
116 g_free(val);
117 return rc;
120 int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
122 return xenstore_write_str(xendev->be, node, val);
125 int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
127 return xenstore_write_int(xendev->be, node, ival);
130 int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
132 return xenstore_write_int64(xendev->be, node, ival);
135 char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
137 return xenstore_read_str(xendev->be, node);
140 int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
142 return xenstore_read_int(xendev->be, node, ival);
145 char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
147 return xenstore_read_str(xendev->fe, node);
150 int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
152 return xenstore_read_int(xendev->fe, node, ival);
155 int xenstore_read_fe_uint64(struct XenDevice *xendev, const char *node, uint64_t *uval)
157 return xenstore_read_uint64(xendev->fe, node, uval);
160 /* ------------------------------------------------------------- */
162 const char *xenbus_strstate(enum xenbus_state state)
164 static const char *const name[] = {
165 [ XenbusStateUnknown ] = "Unknown",
166 [ XenbusStateInitialising ] = "Initialising",
167 [ XenbusStateInitWait ] = "InitWait",
168 [ XenbusStateInitialised ] = "Initialised",
169 [ XenbusStateConnected ] = "Connected",
170 [ XenbusStateClosing ] = "Closing",
171 [ XenbusStateClosed ] = "Closed",
173 return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
176 int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
178 int rc;
180 rc = xenstore_write_be_int(xendev, "state", state);
181 if (rc < 0) {
182 return rc;
184 xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
185 xenbus_strstate(xendev->be_state), xenbus_strstate(state));
186 xendev->be_state = state;
187 return 0;
190 /* ------------------------------------------------------------- */
192 struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
194 struct XenDevice *xendev;
196 QTAILQ_FOREACH(xendev, &xendevs, next) {
197 if (xendev->dom != dom) {
198 continue;
200 if (xendev->dev != dev) {
201 continue;
203 if (strcmp(xendev->type, type) != 0) {
204 continue;
206 return xendev;
208 return NULL;
212 * get xen backend device, allocate a new one if it doesn't exist.
214 static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
215 struct XenDevOps *ops)
217 struct XenDevice *xendev;
219 xendev = xen_be_find_xendev(type, dom, dev);
220 if (xendev) {
221 return xendev;
224 /* init new xendev */
225 xendev = g_malloc0(ops->size);
226 xendev->type = type;
227 xendev->dom = dom;
228 xendev->dev = dev;
229 xendev->ops = ops;
231 snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
232 xendev->type, xendev->dom, xendev->dev);
233 snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
234 xendev->type, xendev->dev);
236 xendev->debug = debug;
237 xendev->local_port = -1;
239 xendev->evtchndev = xenevtchn_open(NULL, 0);
240 if (xendev->evtchndev == NULL) {
241 xen_be_printf(NULL, 0, "can't open evtchn device\n");
242 g_free(xendev);
243 return NULL;
245 fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
247 if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
248 xendev->gnttabdev = xengnttab_open(NULL, 0);
249 if (xendev->gnttabdev == NULL) {
250 xen_be_printf(NULL, 0, "can't open gnttab device\n");
251 xenevtchn_close(xendev->evtchndev);
252 g_free(xendev);
253 return NULL;
255 } else {
256 xendev->gnttabdev = NULL;
259 QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
261 if (xendev->ops->alloc) {
262 xendev->ops->alloc(xendev);
265 return xendev;
269 * release xen backend device.
271 static struct XenDevice *xen_be_del_xendev(int dom, int dev)
273 struct XenDevice *xendev, *xnext;
276 * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
277 * we save the next pointer in xnext because we might free xendev.
279 xnext = xendevs.tqh_first;
280 while (xnext) {
281 xendev = xnext;
282 xnext = xendev->next.tqe_next;
284 if (xendev->dom != dom) {
285 continue;
287 if (xendev->dev != dev && dev != -1) {
288 continue;
291 if (xendev->ops->free) {
292 xendev->ops->free(xendev);
295 if (xendev->fe) {
296 char token[XEN_BUFSIZE];
297 snprintf(token, sizeof(token), "fe:%p", xendev);
298 xs_unwatch(xenstore, xendev->fe, token);
299 g_free(xendev->fe);
302 if (xendev->evtchndev != NULL) {
303 xenevtchn_close(xendev->evtchndev);
305 if (xendev->gnttabdev != NULL) {
306 xengnttab_close(xendev->gnttabdev);
309 QTAILQ_REMOVE(&xendevs, xendev, next);
310 g_free(xendev);
312 return NULL;
316 * Sync internal data structures on xenstore updates.
317 * Node specifies the changed field. node = NULL means
318 * update all fields (used for initialization).
320 static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
322 if (node == NULL || strcmp(node, "online") == 0) {
323 if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
324 xendev->online = 0;
328 if (node) {
329 xen_be_printf(xendev, 2, "backend update: %s\n", node);
330 if (xendev->ops->backend_changed) {
331 xendev->ops->backend_changed(xendev, node);
336 static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
338 int fe_state;
340 if (node == NULL || strcmp(node, "state") == 0) {
341 if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
342 fe_state = XenbusStateUnknown;
344 if (xendev->fe_state != fe_state) {
345 xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
346 xenbus_strstate(xendev->fe_state),
347 xenbus_strstate(fe_state));
349 xendev->fe_state = fe_state;
351 if (node == NULL || strcmp(node, "protocol") == 0) {
352 g_free(xendev->protocol);
353 xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
354 if (xendev->protocol) {
355 xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol);
359 if (node) {
360 xen_be_printf(xendev, 2, "frontend update: %s\n", node);
361 if (xendev->ops->frontend_changed) {
362 xendev->ops->frontend_changed(xendev, node);
367 /* ------------------------------------------------------------- */
368 /* Check for possible state transitions and perform them. */
371 * Initial xendev setup. Read frontend path, register watch for it.
372 * Should succeed once xend finished setting up the backend device.
374 * Also sets initial state (-> Initializing) when done. Which
375 * only affects the xendev->be_state variable as xenbus should
376 * already be put into that state by xend.
378 static int xen_be_try_setup(struct XenDevice *xendev)
380 char token[XEN_BUFSIZE];
381 int be_state;
383 if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
384 xen_be_printf(xendev, 0, "reading backend state failed\n");
385 return -1;
388 if (be_state != XenbusStateInitialising) {
389 xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
390 xenbus_strstate(be_state));
391 return -1;
394 xendev->fe = xenstore_read_be_str(xendev, "frontend");
395 if (xendev->fe == NULL) {
396 xen_be_printf(xendev, 0, "reading frontend path failed\n");
397 return -1;
400 /* setup frontend watch */
401 snprintf(token, sizeof(token), "fe:%p", xendev);
402 if (!xs_watch(xenstore, xendev->fe, token)) {
403 xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
404 xendev->fe);
405 return -1;
407 xen_be_set_state(xendev, XenbusStateInitialising);
409 xen_be_backend_changed(xendev, NULL);
410 xen_be_frontend_changed(xendev, NULL);
411 return 0;
415 * Try initialize xendev. Prepare everything the backend can do
416 * without synchronizing with the frontend. Fakes hotplug-status. No
417 * hotplug involved here because this is about userspace drivers, thus
418 * there are kernel backend devices which could invoke hotplug.
420 * Goes to InitWait on success.
422 static int xen_be_try_init(struct XenDevice *xendev)
424 int rc = 0;
426 if (!xendev->online) {
427 xen_be_printf(xendev, 1, "not online\n");
428 return -1;
431 if (xendev->ops->init) {
432 rc = xendev->ops->init(xendev);
434 if (rc != 0) {
435 xen_be_printf(xendev, 1, "init() failed\n");
436 return rc;
439 xenstore_write_be_str(xendev, "hotplug-status", "connected");
440 xen_be_set_state(xendev, XenbusStateInitWait);
441 return 0;
445 * Try to initialise xendev. Depends on the frontend being ready
446 * for it (shared ring and evtchn info in xenstore, state being
447 * Initialised or Connected).
449 * Goes to Connected on success.
451 static int xen_be_try_initialise(struct XenDevice *xendev)
453 int rc = 0;
455 if (xendev->fe_state != XenbusStateInitialised &&
456 xendev->fe_state != XenbusStateConnected) {
457 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
458 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
459 } else {
460 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
461 return -1;
465 if (xendev->ops->initialise) {
466 rc = xendev->ops->initialise(xendev);
468 if (rc != 0) {
469 xen_be_printf(xendev, 0, "initialise() failed\n");
470 return rc;
473 xen_be_set_state(xendev, XenbusStateConnected);
474 return 0;
478 * Try to let xendev know that it is connected. Depends on the
479 * frontend being Connected. Note that this may be called more
480 * than once since the backend state is not modified.
482 static void xen_be_try_connected(struct XenDevice *xendev)
484 if (!xendev->ops->connected) {
485 return;
488 if (xendev->fe_state != XenbusStateConnected) {
489 if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
490 xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
491 } else {
492 xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
493 return;
497 xendev->ops->connected(xendev);
501 * Teardown connection.
503 * Goes to Closed when done.
505 static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
507 if (xendev->be_state != XenbusStateClosing &&
508 xendev->be_state != XenbusStateClosed &&
509 xendev->ops->disconnect) {
510 xendev->ops->disconnect(xendev);
512 if (xendev->be_state != state) {
513 xen_be_set_state(xendev, state);
518 * Try to reset xendev, for reconnection by another frontend instance.
520 static int xen_be_try_reset(struct XenDevice *xendev)
522 if (xendev->fe_state != XenbusStateInitialising) {
523 return -1;
526 xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
527 xen_be_set_state(xendev, XenbusStateInitialising);
528 return 0;
532 * state change dispatcher function
534 void xen_be_check_state(struct XenDevice *xendev)
536 int rc = 0;
538 /* frontend may request shutdown from almost anywhere */
539 if (xendev->fe_state == XenbusStateClosing ||
540 xendev->fe_state == XenbusStateClosed) {
541 xen_be_disconnect(xendev, xendev->fe_state);
542 return;
545 /* check for possible backend state transitions */
546 for (;;) {
547 switch (xendev->be_state) {
548 case XenbusStateUnknown:
549 rc = xen_be_try_setup(xendev);
550 break;
551 case XenbusStateInitialising:
552 rc = xen_be_try_init(xendev);
553 break;
554 case XenbusStateInitWait:
555 rc = xen_be_try_initialise(xendev);
556 break;
557 case XenbusStateConnected:
558 /* xendev->be_state doesn't change */
559 xen_be_try_connected(xendev);
560 rc = -1;
561 break;
562 case XenbusStateClosed:
563 rc = xen_be_try_reset(xendev);
564 break;
565 default:
566 rc = -1;
568 if (rc != 0) {
569 break;
574 /* ------------------------------------------------------------- */
576 static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
578 struct XenDevice *xendev;
579 char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
580 char **dev = NULL;
581 unsigned int cdev, j;
583 /* setup watch */
584 snprintf(token, sizeof(token), "be:%p:%d:%p", type, dom, ops);
585 snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
586 if (!xs_watch(xenstore, path, token)) {
587 xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path);
588 return -1;
591 /* look for backends */
592 dev = xs_directory(xenstore, 0, path, &cdev);
593 if (!dev) {
594 return 0;
596 for (j = 0; j < cdev; j++) {
597 xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
598 if (xendev == NULL) {
599 continue;
601 xen_be_check_state(xendev);
603 free(dev);
604 return 0;
607 static void xenstore_update_be(char *watch, char *type, int dom,
608 struct XenDevOps *ops)
610 struct XenDevice *xendev;
611 char path[XEN_BUFSIZE], *bepath;
612 unsigned int len, dev;
614 len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
615 if (strncmp(path, watch, len) != 0) {
616 return;
618 if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
619 strcpy(path, "");
620 if (sscanf(watch+len, "/%u", &dev) != 1) {
621 dev = -1;
624 if (dev == -1) {
625 return;
628 xendev = xen_be_get_xendev(type, dom, dev, ops);
629 if (xendev != NULL) {
630 bepath = xs_read(xenstore, 0, xendev->be, &len);
631 if (bepath == NULL) {
632 xen_be_del_xendev(dom, dev);
633 } else {
634 free(bepath);
635 xen_be_backend_changed(xendev, path);
636 xen_be_check_state(xendev);
641 static void xenstore_update_fe(char *watch, struct XenDevice *xendev)
643 char *node;
644 unsigned int len;
646 len = strlen(xendev->fe);
647 if (strncmp(xendev->fe, watch, len) != 0) {
648 return;
650 if (watch[len] != '/') {
651 return;
653 node = watch + len + 1;
655 xen_be_frontend_changed(xendev, node);
656 xen_be_check_state(xendev);
659 static void xenstore_update(void *unused)
661 char **vec = NULL;
662 intptr_t type, ops, ptr;
663 unsigned int dom, count;
665 vec = xs_read_watch(xenstore, &count);
666 if (vec == NULL) {
667 goto cleanup;
670 if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR,
671 &type, &dom, &ops) == 3) {
672 xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops);
674 if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
675 xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
678 cleanup:
679 free(vec);
682 static void xen_be_evtchn_event(void *opaque)
684 struct XenDevice *xendev = opaque;
685 evtchn_port_t port;
687 port = xenevtchn_pending(xendev->evtchndev);
688 if (port != xendev->local_port) {
689 xen_be_printf(xendev, 0,
690 "xenevtchn_pending returned %d (expected %d)\n",
691 port, xendev->local_port);
692 return;
694 xenevtchn_unmask(xendev->evtchndev, port);
696 if (xendev->ops->event) {
697 xendev->ops->event(xendev);
701 /* -------------------------------------------------------------------- */
703 int xen_be_init(void)
705 xenstore = xs_daemon_open();
706 if (!xenstore) {
707 xen_be_printf(NULL, 0, "can't connect to xenstored\n");
708 return -1;
711 qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
713 if (xen_xc == NULL || xen_fmem == NULL) {
714 /* Check if xen_init() have been called */
715 goto err;
717 return 0;
719 err:
720 qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
721 xs_daemon_close(xenstore);
722 xenstore = NULL;
724 return -1;
727 int xen_be_register(const char *type, struct XenDevOps *ops)
729 return xenstore_scan(type, xen_domid, ops);
732 int xen_be_bind_evtchn(struct XenDevice *xendev)
734 if (xendev->local_port != -1) {
735 return 0;
737 xendev->local_port = xenevtchn_bind_interdomain
738 (xendev->evtchndev, xendev->dom, xendev->remote_port);
739 if (xendev->local_port == -1) {
740 xen_be_printf(xendev, 0, "xenevtchn_bind_interdomain failed\n");
741 return -1;
743 xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
744 qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev),
745 xen_be_evtchn_event, NULL, xendev);
746 return 0;
749 void xen_be_unbind_evtchn(struct XenDevice *xendev)
751 if (xendev->local_port == -1) {
752 return;
754 qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
755 xenevtchn_unbind(xendev->evtchndev, xendev->local_port);
756 xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
757 xendev->local_port = -1;
760 int xen_be_send_notify(struct XenDevice *xendev)
762 return xenevtchn_notify(xendev->evtchndev, xendev->local_port);
766 * msg_level:
767 * 0 == errors (stderr + logfile).
768 * 1 == informative debug messages (logfile only).
769 * 2 == noisy debug messages (logfile only).
770 * 3 == will flood your log (logfile only).
772 void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
774 va_list args;
776 if (xendev) {
777 if (msg_level > xendev->debug) {
778 return;
780 qemu_log("xen be: %s: ", xendev->name);
781 if (msg_level == 0) {
782 fprintf(stderr, "xen be: %s: ", xendev->name);
784 } else {
785 if (msg_level > debug) {
786 return;
788 qemu_log("xen be core: ");
789 if (msg_level == 0) {
790 fprintf(stderr, "xen be core: ");
793 va_start(args, fmt);
794 qemu_log_vprintf(fmt, args);
795 va_end(args);
796 if (msg_level == 0) {
797 va_start(args, fmt);
798 vfprintf(stderr, fmt, args);
799 va_end(args);
801 qemu_log_flush();