virtio-9p-test.c 3.0 KB
Newer Older
A
Andreas Färber 已提交
1 2 3 4 5 6 7 8 9
/*
 * QTest testcase for VirtIO 9P
 *
 * Copyright (c) 2014 SUSE LINUX Products GmbH
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

P
Peter Maydell 已提交
10
#include "qemu/osdep.h"
A
Andreas Färber 已提交
11 12
#include "libqtest.h"
#include "qemu-common.h"
13
#include "libqos/libqos-pc.h"
14
#include "libqos/libqos-spapr.h"
15 16 17 18
#include "libqos/virtio.h"
#include "libqos/virtio-pci.h"
#include "standard-headers/linux/virtio_ids.h"
#include "standard-headers/linux/virtio_pci.h"
A
Andreas Färber 已提交
19

20 21
static const char mount_tag[] = "qtest";
static char *test_share;
A
Andreas Färber 已提交
22

23 24

static QOSState *qvirtio_9p_start(void)
A
Andreas Färber 已提交
25
{
26
    const char *arch = qtest_get_arch();
27 28
    const char *cmd = "-fsdev local,id=fsdev0,security_model=none,path=%s "
                      "-device virtio-9p-pci,fsdev=fsdev0,mount_tag=%s";
A
Andreas Färber 已提交
29

30 31
    test_share = g_strdup("/tmp/qtest.XXXXXX");
    g_assert_nonnull(mkdtemp(test_share));
A
Andreas Färber 已提交
32

33 34 35 36 37 38 39 40 41
    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
        return qtest_pc_boot(cmd, test_share, mount_tag);
    }
    if (strcmp(arch, "ppc64") == 0) {
        return qtest_spapr_boot(cmd, test_share, mount_tag);
    }

    g_printerr("virtio-9p tests are only available on x86 or ppc64\n");
    exit(EXIT_FAILURE);
42
}
A
Andreas Färber 已提交
43

44
static void qvirtio_9p_stop(QOSState *qs)
45
{
46
    qtest_shutdown(qs);
A
Andreas Färber 已提交
47
    rmdir(test_share);
48 49 50 51 52
    g_free(test_share);
}

static void pci_nop(void)
{
53 54 55 56
    QOSState *qs;

    qs = qvirtio_9p_start();
    qvirtio_9p_stop(qs);
57 58
}

59 60
typedef struct {
    QVirtioDevice *dev;
61
    QOSState *qs;
62 63 64
    QVirtQueue *vq;
} QVirtIO9P;

65
static QVirtIO9P *qvirtio_9p_pci_init(QOSState *qs)
66 67 68 69 70 71
{
    QVirtIO9P *v9p;
    QVirtioPCIDevice *dev;

    v9p = g_new0(QVirtIO9P, 1);

72 73
    v9p->qs = qs;
    dev = qvirtio_pci_device_find(v9p->qs->pcibus, VIRTIO_ID_9P);
74 75 76 77 78
    g_assert_nonnull(dev);
    g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_9P);
    v9p->dev = (QVirtioDevice *) dev;

    qvirtio_pci_device_enable(dev);
79 80 81
    qvirtio_reset(v9p->dev);
    qvirtio_set_acknowledge(v9p->dev);
    qvirtio_set_driver(v9p->dev);
82

83
    v9p->vq = qvirtqueue_setup(v9p->dev, v9p->qs->alloc, 0);
84 85 86 87 88
    return v9p;
}

static void qvirtio_9p_pci_free(QVirtIO9P *v9p)
{
89
    qvirtqueue_cleanup(v9p->dev->bus, v9p->vq, v9p->qs->alloc);
90 91 92 93 94
    qvirtio_pci_device_disable(container_of(v9p->dev, QVirtioPCIDevice, vdev));
    g_free(v9p->dev);
    g_free(v9p);
}

95
static void pci_config(void)
96 97 98 99 100
{
    QVirtIO9P *v9p;
    size_t tag_len;
    char *tag;
    int i;
101
    QOSState *qs;
102

103 104
    qs = qvirtio_9p_start();
    v9p = qvirtio_9p_pci_init(qs);
105

106
    tag_len = qvirtio_config_readw(v9p->dev, 0);
107 108 109 110
    g_assert_cmpint(tag_len, ==, strlen(mount_tag));

    tag = g_malloc(tag_len);
    for (i = 0; i < tag_len; i++) {
111
        tag[i] = qvirtio_config_readb(v9p->dev, i + 2);
112 113 114 115 116
    }
    g_assert_cmpmem(tag, tag_len, mount_tag, tag_len);
    g_free(tag);

    qvirtio_9p_pci_free(v9p);
117
    qvirtio_9p_stop(qs);
118 119
}

120 121 122 123
int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);
    qtest_add_func("/virtio/9p/pci/nop", pci_nop);
124
    qtest_add_func("/virtio/9p/pci/config", pci_config);
A
Andreas Färber 已提交
125

126
    return g_test_run();
A
Andreas Färber 已提交
127
}