xen_backend.c 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  xen backend driver infrastructure
 *  (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; under version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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/>.
16 17 18
 *
 *  Contributions after 2012-01-13 are licensed under the terms of the
 *  GNU GPL, version 2 or (at your option) any later version.
19 20 21 22 23 24
 */

/*
 * TODO: add some xenbus / xenstore concepts overview here.
 */

P
Peter Maydell 已提交
25
#include "qemu/osdep.h"
26 27 28
#include <sys/mman.h>
#include <sys/signal.h>

29
#include "hw/hw.h"
30
#include "sysemu/char.h"
31
#include "qemu/log.h"
P
Paolo Bonzini 已提交
32
#include "hw/xen/xen_backend.h"
33

34 35
#include <xen/grant_table.h>

36 37 38
/* ------------------------------------------------------------- */

/* public */
39
XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
40
xenforeignmemory_handle *xen_fmem = NULL;
41
struct xs_handle *xenstore = NULL;
42
const char *xen_protocol;
43 44

/* private */
B
Blue Swirl 已提交
45
static QTAILQ_HEAD(XenDeviceHead, XenDevice) xendevs = QTAILQ_HEAD_INITIALIZER(xendevs);
46 47 48 49 50 51 52 53 54
static int debug = 0;

/* ------------------------------------------------------------- */

int xenstore_write_str(const char *base, const char *node, const char *val)
{
    char abspath[XEN_BUFSIZE];

    snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
55 56 57
    if (!xs_write(xenstore, 0, abspath, val, strlen(val))) {
        return -1;
    }
58 59 60 61 62 63 64 65 66 67 68 69 70
    return 0;
}

char *xenstore_read_str(const char *base, const char *node)
{
    char abspath[XEN_BUFSIZE];
    unsigned int len;
    char *str, *ret = NULL;

    snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
    str = xs_read(xenstore, 0, abspath, &len);
    if (str != NULL) {
        /* move to qemu-allocated memory to make sure
71 72
         * callers can savely g_free() stuff. */
        ret = g_strdup(str);
73 74 75 76 77 78 79
        free(str);
    }
    return ret;
}

int xenstore_write_int(const char *base, const char *node, int ival)
{
80
    char val[12];
81 82 83 84 85

    snprintf(val, sizeof(val), "%d", ival);
    return xenstore_write_str(base, node, val);
}

86 87 88 89 90 91 92 93
int xenstore_write_int64(const char *base, const char *node, int64_t ival)
{
    char val[21];

    snprintf(val, sizeof(val), "%"PRId64, ival);
    return xenstore_write_str(base, node, val);
}

94 95 96 97 98 99
int xenstore_read_int(const char *base, const char *node, int *ival)
{
    char *val;
    int rc = -1;

    val = xenstore_read_str(base, node);
100 101 102
    if (val && 1 == sscanf(val, "%d", ival)) {
        rc = 0;
    }
103
    g_free(val);
104 105 106
    return rc;
}

107 108 109 110 111 112 113 114 115 116 117 118 119
int xenstore_read_uint64(const char *base, const char *node, uint64_t *uval)
{
    char *val;
    int rc = -1;

    val = xenstore_read_str(base, node);
    if (val && 1 == sscanf(val, "%"SCNu64, uval)) {
        rc = 0;
    }
    g_free(val);
    return rc;
}

120 121 122 123 124 125 126 127 128 129
int xenstore_write_be_str(struct XenDevice *xendev, const char *node, const char *val)
{
    return xenstore_write_str(xendev->be, node, val);
}

int xenstore_write_be_int(struct XenDevice *xendev, const char *node, int ival)
{
    return xenstore_write_int(xendev->be, node, ival);
}

130 131 132 133 134
int xenstore_write_be_int64(struct XenDevice *xendev, const char *node, int64_t ival)
{
    return xenstore_write_int64(xendev->be, node, ival);
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
char *xenstore_read_be_str(struct XenDevice *xendev, const char *node)
{
    return xenstore_read_str(xendev->be, node);
}

int xenstore_read_be_int(struct XenDevice *xendev, const char *node, int *ival)
{
    return xenstore_read_int(xendev->be, node, ival);
}

char *xenstore_read_fe_str(struct XenDevice *xendev, const char *node)
{
    return xenstore_read_str(xendev->fe, node);
}

int xenstore_read_fe_int(struct XenDevice *xendev, const char *node, int *ival)
{
    return xenstore_read_int(xendev->fe, node, ival);
}

155 156 157 158 159
int xenstore_read_fe_uint64(struct XenDevice *xendev, const char *node, uint64_t *uval)
{
    return xenstore_read_uint64(xendev->fe, node, uval);
}

160 161 162 163
/* ------------------------------------------------------------- */

const char *xenbus_strstate(enum xenbus_state state)
{
164 165 166 167 168 169 170 171 172 173
    static const char *const name[] = {
        [ XenbusStateUnknown      ] = "Unknown",
        [ XenbusStateInitialising ] = "Initialising",
        [ XenbusStateInitWait     ] = "InitWait",
        [ XenbusStateInitialised  ] = "Initialised",
        [ XenbusStateConnected    ] = "Connected",
        [ XenbusStateClosing      ] = "Closing",
        [ XenbusStateClosed       ] = "Closed",
    };
    return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
174 175 176 177 178 179 180
}

int xen_be_set_state(struct XenDevice *xendev, enum xenbus_state state)
{
    int rc;

    rc = xenstore_write_be_int(xendev, "state", state);
181 182 183
    if (rc < 0) {
        return rc;
    }
184
    xen_be_printf(xendev, 1, "backend state: %s -> %s\n",
185
                  xenbus_strstate(xendev->be_state), xenbus_strstate(state));
186 187 188 189 190 191 192 193 194 195
    xendev->be_state = state;
    return 0;
}

/* ------------------------------------------------------------- */

struct XenDevice *xen_be_find_xendev(const char *type, int dom, int dev)
{
    struct XenDevice *xendev;

B
Blue Swirl 已提交
196
    QTAILQ_FOREACH(xendev, &xendevs, next) {
197 198 199 200 201 202 203 204 205 206
        if (xendev->dom != dom) {
            continue;
        }
        if (xendev->dev != dev) {
            continue;
        }
        if (strcmp(xendev->type, type) != 0) {
            continue;
        }
        return xendev;
207 208 209 210 211 212 213 214 215 216 217 218 219
    }
    return NULL;
}

/*
 * get xen backend device, allocate a new one if it doesn't exist.
 */
static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
                                           struct XenDevOps *ops)
{
    struct XenDevice *xendev;

    xendev = xen_be_find_xendev(type, dom, dev);
220 221 222
    if (xendev) {
        return xendev;
    }
223 224

    /* init new xendev */
225
    xendev = g_malloc0(ops->size);
226 227 228 229 230
    xendev->type  = type;
    xendev->dom   = dom;
    xendev->dev   = dev;
    xendev->ops   = ops;

231 232
    snprintf(xendev->be, sizeof(xendev->be), "backend/%s/%d/%d",
             xendev->type, xendev->dom, xendev->dev);
233
    snprintf(xendev->name, sizeof(xendev->name), "%s-%d",
234
             xendev->type, xendev->dev);
235 236 237 238

    xendev->debug      = debug;
    xendev->local_port = -1;

239 240
    xendev->evtchndev = xenevtchn_open(NULL, 0);
    if (xendev->evtchndev == NULL) {
241
        xen_be_printf(NULL, 0, "can't open evtchn device\n");
242
        g_free(xendev);
243
        return NULL;
244
    }
245
    fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
246 247

    if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
248 249
        xendev->gnttabdev = xengnttab_open(NULL, 0);
        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 254
            return NULL;
        }
255
    } else {
256
        xendev->gnttabdev = NULL;
257 258
    }

B
Blue Swirl 已提交
259
    QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
260

261 262 263
    if (xendev->ops->alloc) {
        xendev->ops->alloc(xendev);
    }
264 265 266 267 268 269 270 271 272 273 274 275

    return xendev;
}

/*
 * release xen backend device.
 */
static struct XenDevice *xen_be_del_xendev(int dom, int dev)
{
    struct XenDevice *xendev, *xnext;

    /*
B
Blue Swirl 已提交
276
     * This is pretty much like QTAILQ_FOREACH(xendev, &xendevs, next) but
277 278 279 280 281 282 283
     * we save the next pointer in xnext because we might free xendev.
     */
    xnext = xendevs.tqh_first;
    while (xnext) {
        xendev = xnext;
        xnext = xendev->next.tqe_next;

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
        if (xendev->dom != dom) {
            continue;
        }
        if (xendev->dev != dev && dev != -1) {
            continue;
        }

        if (xendev->ops->free) {
            xendev->ops->free(xendev);
        }

        if (xendev->fe) {
            char token[XEN_BUFSIZE];
            snprintf(token, sizeof(token), "fe:%p", xendev);
            xs_unwatch(xenstore, xendev->fe, token);
299
            g_free(xendev->fe);
300 301
        }

302 303
        if (xendev->evtchndev != NULL) {
            xenevtchn_close(xendev->evtchndev);
304
        }
305 306
        if (xendev->gnttabdev != NULL) {
            xengnttab_close(xendev->gnttabdev);
307 308 309
        }

        QTAILQ_REMOVE(&xendevs, xendev, next);
310
        g_free(xendev);
311 312 313 314 315 316 317 318 319 320 321 322
    }
    return NULL;
}

/*
 * Sync internal data structures on xenstore updates.
 * Node specifies the changed field.  node = NULL means
 * update all fields (used for initialization).
 */
static void xen_be_backend_changed(struct XenDevice *xendev, const char *node)
{
    if (node == NULL  ||  strcmp(node, "online") == 0) {
323 324 325
        if (xenstore_read_be_int(xendev, "online", &xendev->online) == -1) {
            xendev->online = 0;
        }
326 327 328
    }

    if (node) {
329 330 331 332
        xen_be_printf(xendev, 2, "backend update: %s\n", node);
        if (xendev->ops->backend_changed) {
            xendev->ops->backend_changed(xendev, node);
        }
333 334 335 336 337 338 339 340
    }
}

static void xen_be_frontend_changed(struct XenDevice *xendev, const char *node)
{
    int fe_state;

    if (node == NULL  ||  strcmp(node, "state") == 0) {
341 342 343 344 345 346 347 348 349
        if (xenstore_read_fe_int(xendev, "state", &fe_state) == -1) {
            fe_state = XenbusStateUnknown;
        }
        if (xendev->fe_state != fe_state) {
            xen_be_printf(xendev, 1, "frontend state: %s -> %s\n",
                          xenbus_strstate(xendev->fe_state),
                          xenbus_strstate(fe_state));
        }
        xendev->fe_state = fe_state;
350 351
    }
    if (node == NULL  ||  strcmp(node, "protocol") == 0) {
352
        g_free(xendev->protocol);
353 354 355 356
        xendev->protocol = xenstore_read_fe_str(xendev, "protocol");
        if (xendev->protocol) {
            xen_be_printf(xendev, 1, "frontend protocol: %s\n", xendev->protocol);
        }
357 358 359
    }

    if (node) {
360 361 362 363
        xen_be_printf(xendev, 2, "frontend update: %s\n", node);
        if (xendev->ops->frontend_changed) {
            xendev->ops->frontend_changed(xendev, node);
        }
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    }
}

/* ------------------------------------------------------------- */
/* Check for possible state transitions and perform them.        */

/*
 * Initial xendev setup.  Read frontend path, register watch for it.
 * Should succeed once xend finished setting up the backend device.
 *
 * Also sets initial state (-> Initializing) when done.  Which
 * only affects the xendev->be_state variable as xenbus should
 * already be put into that state by xend.
 */
static int xen_be_try_setup(struct XenDevice *xendev)
{
    char token[XEN_BUFSIZE];
    int be_state;

    if (xenstore_read_be_int(xendev, "state", &be_state) == -1) {
384 385
        xen_be_printf(xendev, 0, "reading backend state failed\n");
        return -1;
386 387 388
    }

    if (be_state != XenbusStateInitialising) {
389 390 391
        xen_be_printf(xendev, 0, "initial backend state is wrong (%s)\n",
                      xenbus_strstate(be_state));
        return -1;
392 393 394 395
    }

    xendev->fe = xenstore_read_be_str(xendev, "frontend");
    if (xendev->fe == NULL) {
396 397
        xen_be_printf(xendev, 0, "reading frontend path failed\n");
        return -1;
398 399 400 401 402
    }

    /* setup frontend watch */
    snprintf(token, sizeof(token), "fe:%p", xendev);
    if (!xs_watch(xenstore, xendev->fe, token)) {
403 404 405
        xen_be_printf(xendev, 0, "watching frontend path (%s) failed\n",
                      xendev->fe);
        return -1;
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    }
    xen_be_set_state(xendev, XenbusStateInitialising);

    xen_be_backend_changed(xendev, NULL);
    xen_be_frontend_changed(xendev, NULL);
    return 0;
}

/*
 * Try initialize xendev.  Prepare everything the backend can do
 * without synchronizing with the frontend.  Fakes hotplug-status.  No
 * hotplug involved here because this is about userspace drivers, thus
 * there are kernel backend devices which could invoke hotplug.
 *
 * Goes to InitWait on success.
 */
static int xen_be_try_init(struct XenDevice *xendev)
{
    int rc = 0;

    if (!xendev->online) {
427 428
        xen_be_printf(xendev, 1, "not online\n");
        return -1;
429 430
    }

431 432 433
    if (xendev->ops->init) {
        rc = xendev->ops->init(xendev);
    }
434
    if (rc != 0) {
435 436
        xen_be_printf(xendev, 1, "init() failed\n");
        return rc;
437 438 439 440 441 442 443 444
    }

    xenstore_write_be_str(xendev, "hotplug-status", "connected");
    xen_be_set_state(xendev, XenbusStateInitWait);
    return 0;
}

/*
445
 * Try to initialise xendev.  Depends on the frontend being ready
446 447 448 449 450
 * for it (shared ring and evtchn info in xenstore, state being
 * Initialised or Connected).
 *
 * Goes to Connected on success.
 */
451
static int xen_be_try_initialise(struct XenDevice *xendev)
452 453 454 455
{
    int rc = 0;

    if (xendev->fe_state != XenbusStateInitialised  &&
456 457 458 459 460 461 462
        xendev->fe_state != XenbusStateConnected) {
        if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
            xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
        } else {
            xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
            return -1;
        }
463 464
    }

465 466
    if (xendev->ops->initialise) {
        rc = xendev->ops->initialise(xendev);
467
    }
468
    if (rc != 0) {
469
        xen_be_printf(xendev, 0, "initialise() failed\n");
470
        return rc;
471 472 473 474 475 476
    }

    xen_be_set_state(xendev, XenbusStateConnected);
    return 0;
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
/*
 * Try to let xendev know that it is connected.  Depends on the
 * frontend being Connected.  Note that this may be called more
 * than once since the backend state is not modified.
 */
static void xen_be_try_connected(struct XenDevice *xendev)
{
    if (!xendev->ops->connected) {
        return;
    }

    if (xendev->fe_state != XenbusStateConnected) {
        if (xendev->ops->flags & DEVOPS_FLAG_IGNORE_STATE) {
            xen_be_printf(xendev, 2, "frontend not ready, ignoring\n");
        } else {
            xen_be_printf(xendev, 2, "frontend not ready (yet)\n");
            return;
        }
    }

    xendev->ops->connected(xendev);
}

500 501 502 503 504 505 506 507 508
/*
 * Teardown connection.
 *
 * Goes to Closed when done.
 */
static void xen_be_disconnect(struct XenDevice *xendev, enum xenbus_state state)
{
    if (xendev->be_state != XenbusStateClosing &&
        xendev->be_state != XenbusStateClosed  &&
509 510 511 512
        xendev->ops->disconnect) {
        xendev->ops->disconnect(xendev);
    }
    if (xendev->be_state != state) {
513
        xen_be_set_state(xendev, state);
514
    }
515 516 517 518 519 520 521
}

/*
 * Try to reset xendev, for reconnection by another frontend instance.
 */
static int xen_be_try_reset(struct XenDevice *xendev)
{
522
    if (xendev->fe_state != XenbusStateInitialising) {
523
        return -1;
524
    }
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

    xen_be_printf(xendev, 1, "device reset (for re-connect)\n");
    xen_be_set_state(xendev, XenbusStateInitialising);
    return 0;
}

/*
 * state change dispatcher function
 */
void xen_be_check_state(struct XenDevice *xendev)
{
    int rc = 0;

    /* frontend may request shutdown from almost anywhere */
    if (xendev->fe_state == XenbusStateClosing ||
540 541 542
        xendev->fe_state == XenbusStateClosed) {
        xen_be_disconnect(xendev, xendev->fe_state);
        return;
543 544 545 546
    }

    /* check for possible backend state transitions */
    for (;;) {
547 548 549 550 551 552 553 554
        switch (xendev->be_state) {
        case XenbusStateUnknown:
            rc = xen_be_try_setup(xendev);
            break;
        case XenbusStateInitialising:
            rc = xen_be_try_init(xendev);
            break;
        case XenbusStateInitWait:
555 556 557 558 559 560
            rc = xen_be_try_initialise(xendev);
            break;
        case XenbusStateConnected:
            /* xendev->be_state doesn't change */
            xen_be_try_connected(xendev);
            rc = -1;
561
            break;
562 563 564
        case XenbusStateClosed:
            rc = xen_be_try_reset(xendev);
            break;
565 566 567 568 569 570
        default:
            rc = -1;
        }
        if (rc != 0) {
            break;
        }
571 572 573 574 575 576 577 578 579
    }
}

/* ------------------------------------------------------------- */

static int xenstore_scan(const char *type, int dom, struct XenDevOps *ops)
{
    struct XenDevice *xendev;
    char path[XEN_BUFSIZE], token[XEN_BUFSIZE];
580
    char **dev = NULL;
581 582 583 584
    unsigned int cdev, j;

    /* setup watch */
    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 588
        xen_be_printf(NULL, 0, "xen be: watching backend path (%s) failed\n", path);
        return -1;
589 590 591 592
    }

    /* look for backends */
    dev = xs_directory(xenstore, 0, path, &cdev);
593 594 595
    if (!dev) {
        return 0;
    }
596
    for (j = 0; j < cdev; j++) {
597 598 599 600 601
        xendev = xen_be_get_xendev(type, dom, atoi(dev[j]), ops);
        if (xendev == NULL) {
            continue;
        }
        xen_be_check_state(xendev);
602 603 604 605 606 607
    }
    free(dev);
    return 0;
}

static void xenstore_update_be(char *watch, char *type, int dom,
608
                               struct XenDevOps *ops)
609 610
{
    struct XenDevice *xendev;
611
    char path[XEN_BUFSIZE], *bepath;
612 613
    unsigned int len, dev;

614
    len = snprintf(path, sizeof(path), "backend/%s/%d", type, dom);
615 616 617
    if (strncmp(path, watch, len) != 0) {
        return;
    }
618
    if (sscanf(watch+len, "/%u/%255s", &dev, path) != 2) {
619 620 621 622 623 624 625
        strcpy(path, "");
        if (sscanf(watch+len, "/%u", &dev) != 1) {
            dev = -1;
        }
    }
    if (dev == -1) {
        return;
626 627 628 629
    }

    xendev = xen_be_get_xendev(type, dom, dev, ops);
    if (xendev != NULL) {
630 631 632 633 634 635 636 637
        bepath = xs_read(xenstore, 0, xendev->be, &len);
        if (bepath == NULL) {
            xen_be_del_xendev(dom, dev);
        } else {
            free(bepath);
            xen_be_backend_changed(xendev, path);
            xen_be_check_state(xendev);
        }
638 639 640 641 642 643 644 645 646
    }
}

static void xenstore_update_fe(char *watch, struct XenDevice *xendev)
{
    char *node;
    unsigned int len;

    len = strlen(xendev->fe);
647 648 649 650 651 652
    if (strncmp(xendev->fe, watch, len) != 0) {
        return;
    }
    if (watch[len] != '/') {
        return;
    }
653 654 655 656 657 658 659 660 661 662 663 664 665
    node = watch + len + 1;

    xen_be_frontend_changed(xendev, node);
    xen_be_check_state(xendev);
}

static void xenstore_update(void *unused)
{
    char **vec = NULL;
    intptr_t type, ops, ptr;
    unsigned int dom, count;

    vec = xs_read_watch(xenstore, &count);
666 667 668
    if (vec == NULL) {
        goto cleanup;
    }
669 670

    if (sscanf(vec[XS_WATCH_TOKEN], "be:%" PRIxPTR ":%d:%" PRIxPTR,
671 672 673 674 675 676
               &type, &dom, &ops) == 3) {
        xenstore_update_be(vec[XS_WATCH_PATH], (void*)type, dom, (void*)ops);
    }
    if (sscanf(vec[XS_WATCH_TOKEN], "fe:%" PRIxPTR, &ptr) == 1) {
        xenstore_update_fe(vec[XS_WATCH_PATH], (void*)ptr);
    }
677 678

cleanup:
679
    free(vec);
680 681 682 683 684 685 686
}

static void xen_be_evtchn_event(void *opaque)
{
    struct XenDevice *xendev = opaque;
    evtchn_port_t port;

687
    port = xenevtchn_pending(xendev->evtchndev);
688
    if (port != xendev->local_port) {
689 690
        xen_be_printf(xendev, 0,
                      "xenevtchn_pending returned %d (expected %d)\n",
691 692
                      port, xendev->local_port);
        return;
693
    }
694
    xenevtchn_unmask(xendev->evtchndev, port);
695

696 697 698
    if (xendev->ops->event) {
        xendev->ops->event(xendev);
    }
699 700 701 702 703 704 705 706
}

/* -------------------------------------------------------------------- */

int xen_be_init(void)
{
    xenstore = xs_daemon_open();
    if (!xenstore) {
707 708
        xen_be_printf(NULL, 0, "can't connect to xenstored\n");
        return -1;
709 710
    }

711
    qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
712

713
    if (xen_xc == XC_HANDLER_INITIAL_VALUE || xen_fmem == NULL) {
A
Anthony PERARD 已提交
714
        /* Check if xen_init() have been called */
715
        goto err;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    }
    return 0;

err:
    qemu_set_fd_handler(xs_fileno(xenstore), NULL, NULL, NULL);
    xs_daemon_close(xenstore);
    xenstore = NULL;

    return -1;
}

int xen_be_register(const char *type, struct XenDevOps *ops)
{
    return xenstore_scan(type, xen_domid, ops);
}

int xen_be_bind_evtchn(struct XenDevice *xendev)
{
734 735 736
    if (xendev->local_port != -1) {
        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;
742 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 747 748 749 750
    return 0;
}

void xen_be_unbind_evtchn(struct XenDevice *xendev)
{
751 752 753
    if (xendev->local_port == -1) {
        return;
    }
754 755
    qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
    xenevtchn_unbind(xendev->evtchndev, xendev->local_port);
756 757 758 759 760 761
    xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
    xendev->local_port = -1;
}

int xen_be_send_notify(struct XenDevice *xendev)
{
762
    return xenevtchn_notify(xendev->evtchndev, xendev->local_port);
763 764 765 766 767 768 769 770 771 772 773 774 775 776
}

/*
 * msg_level:
 *  0 == errors (stderr + logfile).
 *  1 == informative debug messages (logfile only).
 *  2 == noisy debug messages (logfile only).
 *  3 == will flood your log (logfile only).
 */
void xen_be_printf(struct XenDevice *xendev, int msg_level, const char *fmt, ...)
{
    va_list args;

    if (xendev) {
777
        if (msg_level > xendev->debug) {
778
            return;
779
        }
780
        qemu_log("xen be: %s: ", xendev->name);
781
        if (msg_level == 0) {
782
            fprintf(stderr, "xen be: %s: ", xendev->name);
783
        }
784
    } else {
785
        if (msg_level > debug) {
786
            return;
787
        }
788
        qemu_log("xen be core: ");
789
        if (msg_level == 0) {
790
            fprintf(stderr, "xen be core: ");
791
        }
792 793 794 795 796 797 798 799 800 801 802
    }
    va_start(args, fmt);
    qemu_log_vprintf(fmt, args);
    va_end(args);
    if (msg_level == 0) {
        va_start(args, fmt);
        vfprintf(stderr, fmt, args);
        va_end(args);
    }
    qemu_log_flush();
}