1 /* 2 * QTest testcase for VirtIO 9P 3 * 4 * Copyright (c) 2014 SUSE LINUX Products GmbH 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 */ 9 10 #include "qemu/osdep.h" 11 #include "libqtest.h" 12 #include "qemu-common.h" 13 #include "libqos/pci-pc.h" 14 #include "libqos/virtio.h" 15 #include "libqos/virtio-pci.h" 16 #include "libqos/malloc.h" 17 #include "libqos/malloc-pc.h" 18 #include "standard-headers/linux/virtio_ids.h" 19 #include "standard-headers/linux/virtio_pci.h" 20 21 static const char mount_tag[] = "qtest"; 22 static char *test_share; 23 24 static void qvirtio_9p_start(void) 25 { 26 char *args; 27 28 test_share = g_strdup("/tmp/qtest.XXXXXX"); 29 g_assert_nonnull(mkdtemp(test_share)); 30 31 args = g_strdup_printf("-fsdev local,id=fsdev0,security_model=none,path=%s " 32 "-device virtio-9p-pci,fsdev=fsdev0,mount_tag=%s", 33 test_share, mount_tag); 34 35 qtest_start(args); 36 g_free(args); 37 } 38 39 static void qvirtio_9p_stop(void) 40 { 41 qtest_end(); 42 rmdir(test_share); 43 g_free(test_share); 44 } 45 46 static void pci_nop(void) 47 { 48 qvirtio_9p_start(); 49 qvirtio_9p_stop(); 50 } 51 52 typedef struct { 53 QVirtioDevice *dev; 54 QGuestAllocator *alloc; 55 QPCIBus *bus; 56 QVirtQueue *vq; 57 } QVirtIO9P; 58 59 static QVirtIO9P *qvirtio_9p_pci_init(void) 60 { 61 QVirtIO9P *v9p; 62 QVirtioPCIDevice *dev; 63 64 v9p = g_new0(QVirtIO9P, 1); 65 v9p->alloc = pc_alloc_init(); 66 v9p->bus = qpci_init_pc(); 67 68 dev = qvirtio_pci_device_find(v9p->bus, VIRTIO_ID_9P); 69 g_assert_nonnull(dev); 70 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_9P); 71 v9p->dev = (QVirtioDevice *) dev; 72 73 qvirtio_pci_device_enable(dev); 74 qvirtio_reset(&qvirtio_pci, v9p->dev); 75 qvirtio_set_acknowledge(&qvirtio_pci, v9p->dev); 76 qvirtio_set_driver(&qvirtio_pci, v9p->dev); 77 78 v9p->vq = qvirtqueue_setup(&qvirtio_pci, v9p->dev, v9p->alloc, 0); 79 return v9p; 80 } 81 82 static void qvirtio_9p_pci_free(QVirtIO9P *v9p) 83 { 84 qvirtqueue_cleanup(&qvirtio_pci, v9p->vq, v9p->alloc); 85 pc_alloc_uninit(v9p->alloc); 86 qvirtio_pci_device_disable(container_of(v9p->dev, QVirtioPCIDevice, vdev)); 87 g_free(v9p->dev); 88 qpci_free_pc(v9p->bus); 89 g_free(v9p); 90 } 91 92 static void pci_basic_config(void) 93 { 94 QVirtIO9P *v9p; 95 void *addr; 96 size_t tag_len; 97 char *tag; 98 int i; 99 100 qvirtio_9p_start(); 101 v9p = qvirtio_9p_pci_init(); 102 103 addr = ((QVirtioPCIDevice *) v9p->dev)->addr + VIRTIO_PCI_CONFIG_OFF(false); 104 tag_len = qvirtio_config_readw(&qvirtio_pci, v9p->dev, 105 (uint64_t)(uintptr_t)addr); 106 g_assert_cmpint(tag_len, ==, strlen(mount_tag)); 107 addr += sizeof(uint16_t); 108 109 tag = g_malloc(tag_len); 110 for (i = 0; i < tag_len; i++) { 111 tag[i] = qvirtio_config_readb(&qvirtio_pci, v9p->dev, 112 (uint64_t)(uintptr_t)addr + i); 113 } 114 g_assert_cmpmem(tag, tag_len, mount_tag, tag_len); 115 g_free(tag); 116 117 qvirtio_9p_pci_free(v9p); 118 qvirtio_9p_stop(); 119 } 120 121 int main(int argc, char **argv) 122 { 123 g_test_init(&argc, &argv, NULL); 124 qtest_add_func("/virtio/9p/pci/nop", pci_nop); 125 qtest_add_func("/virtio/9p/pci/basic/configuration", pci_basic_config); 126 127 return g_test_run(); 128 } 129