Skip to contentSkip to conversation

Virtio PCI libqos driver

PATCH Superseded Marc Marí 4 participants 19 messages Jul 24, 2014 – Jul 29, 2014

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

cover Virtio PCI libqos driver

Add functions for virtio PCI libqos driver. Add more debugging tools. Solve bugs found while generating tests.

Marc Marí (7):
tests: Functions bus_foreach and device_find from libqos virtio API
tests: Add virtio device initialization
libqtest: add QTEST_LOG for debugging qtest testcases
libqos: Correct mask to align size to PAGE_SIZE in malloc-pc
libqos: Change free function called in malloc
virtio-blk: Correct bug in support for flexible descriptor layout
libqos: Added basic virtqueue support to virtio implementation

 hw/block/virtio-blk.c     |   14 +--
 tests/Makefile            |    3 +-
 tests/libqos/malloc-pc.c  |    2 +-
 tests/libqos/malloc.h     |    2 +-
 tests/libqos/virtio-pci.c |  189 +++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   48 ++++++++++
 tests/libqos/virtio.c     |  120 ++++++++++++++++++++++++
 tests/libqos/virtio.h     |  127 +++++++++++++++++++++++++
 tests/libqtest.c          |   11 ++-
 tests/virtio-blk-test.c   |  228 +++++++++++++++++++++++++++++++++++++++++++--
 10 files changed, 725 insertions(+), 19 deletions(-)
 create mode 100644 tests/libqos/virtio-pci.c
 create mode 100644 tests/libqos/virtio-pci.h
 create mode 100644 tests/libqos/virtio.c
 create mode 100644 tests/libqos/virtio.h
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH 1/7 tests: Functions bus_foreach and device_find from libqos virtio API

Virtio header has been changed to compile and work with a real device. Functions bus_foreach and device_find have been implemented for PCI. Virtio-blk test case now opens a fake device.

---
 tests/Makefile            |    3 +-
 tests/libqos/virtio-pci.c |   75 +++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   24 +++++++++++++++
 tests/libqos/virtio.h     |   23 ++++++++++++++
 tests/virtio-blk-test.c   |   67 +++++++++++++++++++++++++++++++++++-----
 5 files changed, 183 insertions(+), 9 deletions(-)
 create mode 100644 tests/libqos/virtio-pci.c
 create mode 100644 tests/libqos/virtio-pci.h
 create mode 100644 tests/libqos/virtio.h
tests/Makefile +2 -1
index 4b2e1bb..7c0f670 100644
@@ -294,6 +294,7 @@ libqos-obj-y += tests/libqos/i2c.o
 libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
+libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio-pci.o
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
@@ -315,7 +316,7 @@ tests/vmxnet3-test$(EXESUF): tests/vmxnet3-test.o
 tests/ne2000-test$(EXESUF): tests/ne2000-test.o
 tests/wdt_ib700-test$(EXESUF): tests/wdt_ib700-test.o
 tests/virtio-balloon-test$(EXESUF): tests/virtio-balloon-test.o
-tests/virtio-blk-test$(EXESUF): tests/virtio-blk-test.o
+tests/virtio-blk-test$(EXESUF): tests/virtio-blk-test.o $(libqos-virtio-obj-y)
 tests/virtio-net-test$(EXESUF): tests/virtio-net-test.o
 tests/virtio-rng-test$(EXESUF): tests/virtio-rng-test.o
 tests/virtio-scsi-test$(EXESUF): tests/virtio-scsi-test.o
tests/libqos/virtio-pci.c new file +75 -0

75 lines not on this page · raw message

tests/libqos/virtio-pci.h new file +24 -0
new file mode 100644
index 0000000..5101abb
@@ -0,0 +1,24 @@
+/*
+ * libqos virtio PCI definitions
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef LIBQOS_VIRTIO_PCI_H
+#define LIBQOS_VIRTIO_PCI_H
+
+#include "libqos/virtio.h"
+#include "libqos/pci.h"
+
+typedef struct QVirtioPCIDevice {
+    QVirtioDevice vdev;
+    QPCIDevice *pdev;
+} QVirtioPCIDevice;
+
+void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
+                void (*func)(QVirtioDevice *d, void *data), void *data);
+QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type);
+#endif
tests/libqos/virtio.h new file +23 -0
new file mode 100644
index 0000000..2a05798
@@ -0,0 +1,23 @@
+/*
+ * libqos virtio definitions
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef LIBQOS_VIRTIO_H
+#define LIBQOS_VIRTIO_H
+
+#define QVIRTIO_VENDOR_ID       0x1AF4
+
+#define QVIRTIO_NET_DEVICE_ID   0x1
+#define QVIRTIO_BLK_DEVICE_ID   0x2
+
+typedef struct QVirtioDevice {
+    /* Device type */
+    uint16_t device_type;
+} QVirtioDevice;
+
+#endif
tests/virtio-blk-test.c +59 -8

90 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/7] tests: Functions bus_foreach and device_find from libqos virtio API

Quoted 10 lines · Marc Marí
tests/virtio-blk-test.c +10 -0
+static QPCIBus *test_start(void)
+{
+    char cmdline[100];
+    char tmp_path[] = "/tmp/qtest.XXXXXX";
+    int fd, ret;
+
+    /* Create a temporary raw image */
+    fd = mkstemp(tmp_path);
+    g_assert_cmpint(fd, >=, 0);
+    ret = ftruncate(fd, TEST_IMAGE_SIZE);

tests/virtio-blk-test.c line 46

Quoted 10 lines
tests/virtio-blk-test.c +10 -0
+    g_assert_cmpint(ret, ==, 0);
+    close(fd);
+
+    last_tmp_path = g_malloc0(strlen(tmp_path));
+    strcpy(last_tmp_path, tmp_path);
+
+    snprintf(cmdline, 100, "-drive if=none,id=drive0,file=%s "
+                            "-device virtio-blk-pci,drive=drive0,addr=%x.%x",
+                            tmp_path, PCI_SLOT, PCI_FN);
+    qtest_start(cmdline);

Please unlink the temporary disk image file here.

The QEMU process has a file descriptor open when we reach this point, so it's safe to delete it on disk (the file stays allocated until the last file descriptor is closed).

This is important so that the temporary file is always deleted in failure cases. We do not reach test_end() when an assertion fails.

author
#3 lore raw

PATCH 2/7 tests: Add virtio device initialization

In reply to Marc Marí

Add functions to read and write virtio header fields. Add feature checking and status bit setting in virtio-blk-device.

---
 tests/Makefile            |    2 +-
 tests/libqos/virtio-pci.c |   32 ++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   17 +++++++++++++++++
 tests/libqos/virtio.c     |   31 +++++++++++++++++++++++++++++++
 tests/libqos/virtio.h     |   19 +++++++++++++++++++
 tests/virtio-blk-test.c   |   14 ++++++++++++++
 6 files changed, 114 insertions(+), 1 deletion(-)
 create mode 100644 tests/libqos/virtio.c
tests/Makefile +1 -1
index 7c0f670..e0e203f 100644
@@ -294,7 +294,7 @@ libqos-obj-y += tests/libqos/i2c.o
 libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
-libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio-pci.o
+libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
tests/libqos/virtio-pci.c +32 -0
index fde1b1f..a04281e 100644
@@ -55,6 +55,30 @@ static void qvirtio_pci_assign_device(QVirtioDevice *d, void *data)
     *vpcidev = (QVirtioPCIDevice *)d;
 }
 
+static uint32_t qvirtio_pci_get_features(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_DEVICE_FEATURES);
+}
+
+static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS);
+}
+
+static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t val)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS, val);
+}
+
+const QVirtioBus qvirtio_pci = {
+    .get_features = qvirtio_pci_get_features,
+    .get_status = qvirtio_pci_get_status,
+    .set_status = qvirtio_pci_set_status,
+};
+
 void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
                 void (*func)(QVirtioDevice *d, void *data), void *data)
 {
@@ -73,3 +97,11 @@ QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type)
 
     return dev;
 }
+
+void qvirtio_pci_enable_device(QVirtioPCIDevice *d)
+{
+    qpci_device_enable(d->pdev);
+    d->addr = qpci_iomap(d->pdev, 0);
+    g_assert(d->addr != NULL);
+}
+
tests/libqos/virtio-pci.h +17 -0
index 5101abb..ef7744e 100644
@@ -13,12 +13,29 @@
 #include "libqos/virtio.h"
 #include "libqos/pci.h"
 
+#define QVIRTIO_DEVICE_FEATURES         0x00
+#define QVIRTIO_GUEST_FEATURES          0x04
+#define QVIRTIO_QUEUE_ADDRESS           0x08
+#define QVIRTIO_QUEUE_SIZE              0x0C
+#define QVIRTIO_QUEUE_SELECT            0x0E
+#define QVIRTIO_QUEUE_NOTIFY            0x10
+#define QVIRTIO_DEVICE_STATUS           0x12
+#define QVIRTIO_ISR_STATUS              0x13
+#define QVIRTIO_MSIX_CONF_VECTOR        0x14
+#define QVIRTIO_MSIX_QUEUE_VECTOR       0x16
+#define QVIRTIO_DEVICE_SPECIFIC_MSIX    0x18
+#define QVIRTIO_DEVICE_SPECIFIC_NO_MSIX 0x14
+
 typedef struct QVirtioPCIDevice {
     QVirtioDevice vdev;
     QPCIDevice *pdev;
+    void *addr;
 } QVirtioPCIDevice;
 
+extern const QVirtioBus qvirtio_pci;
+
 void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
                 void (*func)(QVirtioDevice *d, void *data), void *data);
 QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type);
+void qvirtio_pci_enable_device(QVirtioPCIDevice *d);
 #endif
tests/libqos/virtio.c new file +31 -0
new file mode 100644
index 0000000..118cb15
@@ -0,0 +1,31 @@
+/*
+ * libqos virtio driver
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include <glib.h>
+#include "libqtest.h"
+#include "libqos/virtio.h"
+
+void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d)
+{
+    bus->set_status(d, QVIRTIO_RESET);
+    g_assert_cmphex(bus->get_status(d), ==, QVIRTIO_RESET);
+}
+
+void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d)
+{
+    bus->set_status(d, bus->get_status(d) | QVIRTIO_ACKNOWLEDGE);
+    g_assert_cmphex(bus->get_status(d), ==, QVIRTIO_ACKNOWLEDGE);
+}
+
+void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d)
+{
+    bus->set_status(d, bus->get_status(d) | QVIRTIO_DRIVER);
+    g_assert_cmphex(bus->get_status(d), ==,
+                                    QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE);
+}
tests/libqos/virtio.h +19 -0
index 2a05798..a2e3219 100644
@@ -12,6 +12,10 @@
 
 #define QVIRTIO_VENDOR_ID       0x1AF4
 
+#define QVIRTIO_RESET           0x0
+#define QVIRTIO_ACKNOWLEDGE     0x1
+#define QVIRTIO_DRIVER          0x2
+
 #define QVIRTIO_NET_DEVICE_ID   0x1
 #define QVIRTIO_BLK_DEVICE_ID   0x2
 
@@ -20,4 +24,19 @@ typedef struct QVirtioDevice {
     uint16_t device_type;
 } QVirtioDevice;
 
+typedef struct QVirtioBus {
+    /* Get features of the device */
+    uint32_t (*get_features)(QVirtioDevice *d);
+
+    /* Get status of the device */
+    uint8_t (*get_status)(QVirtioDevice *d);
+
+    /* Set status of the device  */
+    void (*set_status)(QVirtioDevice *d, uint8_t val);
+} QVirtioBus;
+
+void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d);
+void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d);
+void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d);
+
 #endif
tests/virtio-blk-test.c +14 -0
index 52894f4..83be520 100644
@@ -59,6 +59,8 @@ static void pci_basic(void)
 {
     QVirtioPCIDevice *dev;
     QPCIBus *bus;
+    void *addr;
+    uint64_t capacity;
 
     bus = test_start();
 
@@ -67,6 +69,18 @@ static void pci_basic(void)
     g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_BLK_DEVICE_ID);
     g_assert_cmphex(dev->pdev->devfn, ==, ((PCI_SLOT << 3) | PCI_FN));
 
+    qvirtio_pci_enable_device(dev);
+    qvirtio_reset(&qvirtio_pci, &dev->vdev);
+    qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
+    qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
+
+    /* MSI-X is not enabled */
+    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
+
+    capacity = qpci_io_readl(dev->pdev, addr) |
+                qpci_io_readl(dev->pdev, addr+4);
+    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE/512);
+
     g_free(dev);
     test_end();
 }
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [Qemu-devel] [PATCH 2/7] tests: Add virtio device initialization

tests/libqos/virtio-pci.c static void qvirtio_pci_assign_device(QVirtioDevice *d, void *data) · line 62

Quoted 5 lines · Marc Marí
tests/libqos/virtio-pci.c +5 -0
+static uint32_t qvirtio_pci_get_features(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_DEVICE_FEATURES);
+}

Unused? If it's unused, then it's untested.

tests/libqos/virtio-pci.c static void qvirtio_pci_assign_device(QVirtioDevice *d, void *data) · line 68

Quoted 6 lines
tests/libqos/virtio-pci.c +6 -0
+
+static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS);
+}

Unused?

tests/libqos/virtio-pci.c static void qvirtio_pci_assign_device(QVirtioDevice *d, void *data) · line 73

Quoted 5 lines
tests/libqos/virtio-pci.c +5 -0
+
+static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t val)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS, val);

Unused?

tests/libqos/virtio-pci.c QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type) · line 106

Quoted 11 lines
tests/libqos/virtio-pci.c +7 -0
@@ -73,3 +97,11 @@ QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type)
 
     return dev;
 }
+
+void qvirtio_pci_enable_device(QVirtioPCIDevice *d)
+{
+    qpci_device_enable(d->pdev);
+    d->addr = qpci_iomap(d->pdev, 0);
+    g_assert(d->addr != NULL);
+}

Where is qpci_iounmap() called to clean up?

Quoted 5 lines
tests/virtio-blk-test.c +1 -0
@@ -67,6 +69,18 @@ static void pci_basic(void)
     g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_BLK_DEVICE_ID);
     g_assert_cmphex(dev->pdev->devfn, ==, ((PCI_SLOT << 3) | PCI_FN));
 
+    qvirtio_pci_enable_device(dev);

tests/virtio-blk-test.c static void pci_basic(void) · line 82

Quoted 10 lines
tests/virtio-blk-test.c +10 -0
+    qvirtio_reset(&qvirtio_pci, &dev->vdev);
+    qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
+    qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
+
+    /* MSI-X is not enabled */
+    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
+
+    capacity = qpci_io_readl(dev->pdev, addr) |
+                qpci_io_readl(dev->pdev, addr+4);
+    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE/512);

Please add a qvirtio_config_read() function instead of directly accessing the virtio configuration space via PCI.

Stefan

author
#5 lore raw

Re: [Qemu-devel] [PATCH 2/7] tests: Add virtio device initialization

Quoted 8 lines · El Fri, 25 Jul 2014 16:19:41 +0100 Stefan Hajnoczi <[email protected]>…
Quoted 5 lines · Marc Marí
tests/libqos/virtio-pci.c +4 -0
+static uint32_t qvirtio_pci_get_features(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readl(dev->pdev, dev->addr +

QVIRTIO_DEVICE_FEATURES); +}

Unused? If it's unused, then it's untested.

Yes, moved to the other patch for v2

Quoted 9 lines
Quoted 7 lines
tests/libqos/virtio-pci.c +5 -0
+
+static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readb(dev->pdev, dev->addr +

QVIRTIO_DEVICE_STATUS); +}

Unused?

Used in virtio.c (qvirtio_reset / qvirtio_set_acknowledge / qvirtio_set_driver).

Quoted 8 lines
Quoted 6 lines
tests/libqos/virtio-pci.c +5 -0
+
+static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t val)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS,

val);

Unused?

Also in virtio.c

Quoted 14 lines

tests/libqos/virtio-pci.c QVirtioPCIDevice · line 106

Quoted 12 lines
tests/libqos/virtio-pci.c +7 -0
@@ -73,3 +97,11 @@ QVirtioPCIDevice
 *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type) 
     return dev;
 }
+
+void qvirtio_pci_enable_device(QVirtioPCIDevice *d)
+{
+    qpci_device_enable(d->pdev);
+    d->addr = qpci_iomap(d->pdev, 0);
+    g_assert(d->addr != NULL);
+}

Where is qpci_iounmap() called to clean up?

Missed. Also, it is unimplemented.
Marc

#6 lore raw

Re: [Qemu-devel] [PATCH 2/7] tests: Add virtio device initialization

Quoted 14 lines · Marc Marí
Quoted 5 lines
Quoted 5 lines
tests/libqos/virtio-pci.c +1 -0
@@ -73,3 +97,11 @@ QVirtioPCIDevice
 *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type) 
     return dev;
 }
+
Quoted 8 lines

tests/libqos/virtio-pci.c QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type) · line 106

Quoted 6 lines
tests/libqos/virtio-pci.c +6 -0
+void qvirtio_pci_enable_device(QVirtioPCIDevice *d)
+{
+    qpci_device_enable(d->pdev);
+    d->addr = qpci_iomap(d->pdev, 0);
+    g_assert(d->addr != NULL);
+}

Where is qpci_iounmap() called to clean up?

Quoted 1 line

Missed. Also, it is unimplemented.

It would be much harder to add in the appropriate guest_free() calls later so users should still call it. Just like we should call guest_free() even if it is currently unimplemented.

Stefan

author
#7 lore raw

PATCH 3/7 libqtest: add QTEST_LOG for debugging qtest testcases

In reply to Marc Marí
---
 tests/libqtest.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
tests/libqtest.c +10 -1
index 98e8f4b..056275d 100644
@@ -167,11 +167,12 @@ QTestState *qtest_init(const char *extra_args)
     if (s->qemu_pid == 0) {
         command = g_strdup_printf("exec %s "
                                   "-qtest unix:%s,nowait "
-                                  "-qtest-log /dev/null "
+                                  "-qtest-log %s "
                                   "-qmp unix:%s,nowait "
                                   "-machine accel=qtest "
                                   "-display none "
                                   "%s", qemu_binary, socket_path,
+                                  getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
                                   qmp_socket_path,
                                   extra_args ?: "");
         execlp("/bin/sh", "sh", "-c", command, NULL);
@@ -397,10 +398,18 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
 
     /* No need to send anything for an empty QObject.  */
     if (qobj) {
+        size_t len;
+        int log = getenv("QTEST_LOG") != NULL;
         QString *qstr = qobject_to_json(qobj);
         const char *str = qstring_get_str(qstr);
         size_t size = qstring_get_length(qstr);
 
+        if (log) {
+            len = write(2, str, size);
+            if (len != size) {
+                fprintf(stderr, "Could not log\n");
+            }
+        }
         /* Send QMP request */
         socket_send(s->qmp_fd, str, size);
 
Signature · 1 line
1.7.10.4
#8 lore raw

Re: [Qemu-devel] [PATCH 3/7] libqtest: add QTEST_LOG for debugging qtest testcases

Quoted 4 lines · Marc Marí
tests/libqtest.c +0 -0
@@ -397,10 +398,18 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
 
     /* No need to send anything for an empty QObject.  */
     if (qobj) {

tests/libqtest.c QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap) · line 410

Quoted 10 lines
tests/libqtest.c +6 -0
+        size_t len;
+        int log = getenv("QTEST_LOG") != NULL;
         QString *qstr = qobject_to_json(qobj);
         const char *str = qstring_get_str(qstr);
         size_t size = qstring_get_length(qstr);
 
+        if (log) {
+            len = write(2, str, size);
+            if (len != size) {
+                fprintf(stderr, "Could not log\n");

It's a bit funny that we print an error message to stderr after failing to write to stderr.

Why not just fprintf(stderr, "%s", str) instead of using write()?

author
#9 lore raw

PATCH 4/7 libqos: Correct mask to align size to PAGE_SIZE in malloc-pc

In reply to Marc Marí
---
 tests/libqos/malloc-pc.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc-pc.c +1 -1
index db1496c..2efd095 100644
@@ -36,7 +36,7 @@ static uint64_t pc_alloc(QGuestAllocator *allocator, size_t size)
 
 
     size += (PAGE_SIZE - 1);
-    size &= PAGE_SIZE;
+    size &= -PAGE_SIZE;
 
     g_assert_cmpint((s->start + size), <=, s->end);
 
Signature · 1 line
1.7.10.4
Reviewed-by on v1 4/7
#11 lore raw

Re: [Qemu-devel] [PATCH 4/7] libqos: Correct mask to align size to PAGE_SIZE in malloc-pc

In reply to Marc Marí
Quoted 11 lines · Marc Marí
---
  tests/libqos/malloc-pc.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc-pc.c +0 -0
index db1496c..2efd095 100644

tests/libqos/malloc-pc.c static uint64_t pc_alloc(QGuestAllocator *allocator, size_t size) · line 42

Quoted 8 lines
tests/libqos/malloc-pc.c +1 -1
@@ -36,7 +36,7 @@ static uint64_t pc_alloc(QGuestAllocator *allocator, size_t size)
 
 
      size += (PAGE_SIZE - 1);
-    size &= PAGE_SIZE;
+    size &= -PAGE_SIZE;
 
      g_assert_cmpint((s->start + size), <=, s->end);
 
#12 lore raw

Re: [Qemu-devel] [PATCH 4/7] libqos: Correct mask to align size to PAGE_SIZE in malloc-pc

In reply to Marc Marí
Quoted 11 lines · Marc Marí
---
 tests/libqos/malloc-pc.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc-pc.c +0 -0
index db1496c..2efd095 100644

tests/libqos/malloc-pc.c static uint64_t pc_alloc(QGuestAllocator *allocator, size_t size) · line 39

Quoted 5 lines
tests/libqos/malloc-pc.c +1 -1
@@ -36,7 +36,7 @@ static uint64_t pc_alloc(QGuestAllocator *allocator, size_t size)
 
 
     size += (PAGE_SIZE - 1);
-    size &= PAGE_SIZE;
+    size &= -PAGE_SIZE;

Do we have access to the osdep.h ROUND_UP macro from libqos? If so, we could replace these two lines with

     size = ROUND_UP(size, PAGE_SIZE);

thanks
-- PMM

author
#13 lore raw

PATCH 5/7 libqos: Change free function called in malloc

In reply to Marc Marí
---
 tests/libqos/malloc.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc.h +1 -1
index 46f6000..5565381 100644
@@ -32,7 +32,7 @@ static inline uint64_t guest_alloc(QGuestAllocator *allocator, size_t size)
 
 static inline void guest_free(QGuestAllocator *allocator, uint64_t addr)
 {
-    allocator->alloc(allocator, addr);
+    allocator->free(allocator, addr);
 }
 
 #endif
Signature · 1 line
1.7.10.4
Reviewed-by on v1 5/7
#14 lore raw

Re: [Qemu-devel] [PATCH 5/7] libqos: Change free function called in malloc

Quoted 10 lines · Marc Marí
---
 tests/libqos/malloc.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc.h +0 -0
index 46f6000..5565381 100644

tests/libqos/malloc.h static inline uint64_t guest_alloc(QGuestAllocator *allocator, size_t size) · line 36

Quoted 6 lines
tests/libqos/malloc.h +1 -1
@@ -32,7 +32,7 @@ static inline uint64_t guest_alloc(QGuestAllocator *allocator, size_t size)
 
 static inline void guest_free(QGuestAllocator *allocator, uint64_t addr)
 {
-    allocator->alloc(allocator, addr);
+    allocator->free(allocator, addr);
 }

Hahahahahahaha!

Reviewed-by on v1 5/7
#15 lore raw

Re: [Qemu-devel] [PATCH 5/7] libqos: Change free function called in malloc

In reply to Marc Marí
Quoted 10 lines · Marc Marí
---
  tests/libqos/malloc.h |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc.h +0 -0
index 46f6000..5565381 100644

tests/libqos/malloc.h static inline uint64_t guest_alloc(QGuestAllocator *allocator, size_t size) · line 38

Quoted 8 lines
tests/libqos/malloc.h +1 -1
@@ -32,7 +32,7 @@ static inline uint64_t guest_alloc(QGuestAllocator *allocator, size_t size)
 
  static inline void guest_free(QGuestAllocator *allocator, uint64_t addr)
  {
-    allocator->alloc(allocator, addr);
+    allocator->free(allocator, addr);
  }
 
  #endif

Imagine my surprise when I noticed the allocator was summoning up millions of bytes. "Oh good!"

author
#16 lore raw

PATCH 6/7 virtio-blk: Correct bug in support for flexible descriptor layout

In reply to Marc Marí

Without this correction, only a three descriptor layout is accepted, and requests with just two descriptors are not completed and no error message is displayed.

---
 hw/block/virtio-blk.c |   14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
hw/block/virtio-blk.c +7 -7
index c241c50..302c39e 100644
@@ -404,19 +404,19 @@ void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
          * NB: per existing s/n string convention the string is
          * terminated by '\0' only when shorter than buffer.
          */
-        strncpy(req->elem.in_sg[0].iov_base,
-                s->blk.serial ? s->blk.serial : "",
-                MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
+        const char *serial = s->blk.serial ? s->blk.serial : "";
+        size_t size = MIN(strlen(serial) + 1,
+                          MIN(iov_size(in_iov, in_num),
+                              VIRTIO_BLK_ID_BYTES));
+        iov_from_buf(in_iov, in_num, 0, serial, size);
         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
         virtio_blk_free_request(req);
     } else if (type & VIRTIO_BLK_T_OUT) {
-        qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
-                                 req->elem.out_num - 1);
+        qemu_iovec_init_external(&req->qiov, iov, out_num);
         virtio_blk_handle_write(req, mrb);
     } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) {
         /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */
-        qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
-                                 req->elem.in_num - 1);
+        qemu_iovec_init_external(&req->qiov, in_iov, in_num);
         virtio_blk_handle_read(req);
     } else {
         virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
Signature · 1 line
1.7.10.4
author
#17 lore raw

PATCH 7/7 libqos: Added basic virtqueue support to virtio implementation

In reply to Marc Marí

Add status changing.
Add basic virtqueue support for adding and sending virtqueue requests. Add ISR checking.

---
 tests/libqos/virtio-pci.c |   86 ++++++++++++++++++++++++-
 tests/libqos/virtio-pci.h |    7 ++
 tests/libqos/virtio.c     |   89 ++++++++++++++++++++++++++
 tests/libqos/virtio.h     |   87 ++++++++++++++++++++++++-
 tests/virtio-blk-test.c   |  155 +++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 417 insertions(+), 7 deletions(-)
tests/libqos/virtio-pci.c +84 -2
index a04281e..fdbb10e 100644
@@ -8,11 +8,15 @@
  */
 
 #include <glib.h>
+#include <stdio.h>
+#include <inttypes.h>
 #include "libqtest.h"
 #include "libqos/virtio.h"
 #include "libqos/virtio-pci.h"
 #include "libqos/pci.h"
 #include "libqos/pci-pc.h"
+#include "libqos/malloc.h"
+#include "libqos/malloc-pc.h"
 
 #include "hw/pci/pci_regs.h"
 
@@ -61,22 +65,100 @@ static uint32_t qvirtio_pci_get_features(QVirtioDevice *d)
     return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_DEVICE_FEATURES);
 }
 
+static void qvirtio_pci_set_features(QVirtioDevice *d, uint32_t features)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_GUEST_FEATURES, features);
+}
+
 static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
     return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS);
 }
 
-static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t val)
+static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t status)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS, status);
+}
+
+static uint8_t qvirtio_pci_get_isr_status(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_ISR_STATUS);
+}
+
+static void qvirtio_pci_queue_select(QVirtioDevice *d, uint16_t index)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_QUEUE_SELECT, index);
+}
+
+static uint16_t qvirtio_pci_get_queue_size(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readw(dev->pdev, dev->addr + QVIRTIO_QUEUE_SIZE);
+}
+
+static void qvirtio_pci_set_queue_address(QVirtioDevice *d, uint16_t addr)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_QUEUE_ADDRESS, addr);
+}
+
+static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
+                                        QGuestAllocator *alloc, uint16_t index)
+{
+    uint16_t aux;
+    uint64_t addr;
+    QVirtQueue *vq;
+
+    vq = g_malloc0(sizeof(*vq));
+
+    qvirtio_pci_queue_select(d, index);
+    vq->index = index;
+    vq->size = qvirtio_pci_get_queue_size(d);
+    vq->free_head = 0;
+    vq->num_free = vq->size;
+    vq->align = QVIRTIO_PCI_ALIGN;
+
+    /* Check different than 0 */
+    g_assert_cmpint(vq->size, !=, 0);
+
+    /* Check power of 2 */
+    aux = vq->size;
+    while ((aux & 1) != 0) {
+        aux = aux >> 1;
+    }
+    g_assert_cmpint(aux, !=, 1);
+
+    addr = guest_alloc(alloc, qvring_size(vq->size, QVIRTIO_PCI_ALIGN));
+    qvring_init(alloc, vq, addr);
+    qvirtio_pci_set_queue_address(d, vq->desc/4096);
+
+    /* TODO: MSI-X configuration */
+
+    return vq;
+}
+
+static void qvirtio_pci_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS, val);
+    qpci_io_writew(dev->pdev, dev->addr + QVIRTIO_QUEUE_NOTIFY, vq->index);
 }
 
 const QVirtioBus qvirtio_pci = {
     .get_features = qvirtio_pci_get_features,
+    .set_features = qvirtio_pci_set_features,
     .get_status = qvirtio_pci_get_status,
     .set_status = qvirtio_pci_set_status,
+    .get_isr_status = qvirtio_pci_get_isr_status,
+    .queue_select = qvirtio_pci_queue_select,
+    .get_queue_size = qvirtio_pci_get_queue_size,
+    .set_queue_address = qvirtio_pci_set_queue_address,
+    .virtqueue_setup = qvirtio_pci_virtqueue_setup,
+    .virtqueue_kick = qvirtio_pci_virtqueue_kick,
 };
 
 void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
tests/libqos/virtio-pci.h +7 -0
index ef7744e..ec48d19 100644
@@ -26,6 +26,13 @@
 #define QVIRTIO_DEVICE_SPECIFIC_MSIX    0x18
 #define QVIRTIO_DEVICE_SPECIFIC_NO_MSIX 0x14
 
+#define QVIRTIO_F_NOTIFY_ON_EMPTY       0x01000000
+#define QVIRTIO_F_ANY_LAYOUT            0x08000000
+#define QVIRTIO_F_RING_INDIRECT_DESC    0x10000000
+#define QVIRTIO_F_RING_EVENT_IDX        0x20000000
+
+#define QVIRTIO_PCI_ALIGN   4096
+
 typedef struct QVirtioPCIDevice {
     QVirtioDevice vdev;
     QPCIDevice *pdev;
tests/libqos/virtio.c +89 -0

98 lines not on this page · raw message

tests/libqos/virtio.h +86 -1

119 lines not on this page · raw message

tests/virtio-blk-test.c +151 -4

175 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#18 lore raw

Re: [Qemu-devel] [PATCH 7/7] libqos: Added basic virtqueue support to virtio implementation

tests/libqos/virtio-pci.c static uint32_t qvirtio_pci_get_features(QVirtioDevice *d) · line 108

Quoted 5 lines · Marc Marí
tests/libqos/virtio-pci.c +5 -0
+static void qvirtio_pci_set_queue_address(QVirtioDevice *d, uint16_t addr)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_QUEUE_ADDRESS, addr);
+}

Why is addr uint16_t? It should be a 32-bit Page Frame Number). It's probably clearer to name it "pfn" instead of "addr" since it's not an address.