Skip to contentSkip to conversation

tests: Functions bus_foreach and device_find from libqos virtio API

PATCH Superseded Marc Marí 2 participants 2 messages Jun 30, 2014 – Jul 3, 2014

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

PATCH v3 1/1 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 |   72 +++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   31 +++++++++++++++++++
 tests/libqos/virtio.h     |   28 ++++++++++++++++++
 tests/virtio-blk-test.c   |   65 +++++++++++++++++++++++++++++++++++-----
 5 files changed, 190 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 7e53d0d..028c462 100644
@@ -292,6 +292,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
@@ -312,7 +313,7 @@ tests/eepro100-test$(EXESUF): tests/eepro100-test.o
 tests/vmxnet3-test$(EXESUF): tests/vmxnet3-test.o
 tests/ne2000-test$(EXESUF): tests/ne2000-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 +72 -0

72 lines not on this page · raw message

tests/libqos/virtio-pci.h new file +31 -0
new file mode 100644
index 0000000..f00a982
@@ -0,0 +1,31 @@
+/*
+ * 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;
+
+typedef struct QVirtioPCIForeachData {
+    void (*func)(QVirtioDevice *d, void *data);
+    uint16_t device_type;
+    void *user_data;
+} QVirtioPCIForeachData;
+
+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 +28 -0
new file mode 100644
index 0000000..52f6b5b
@@ -0,0 +1,28 @@
+/*
+ * 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 QVIRTQUEUE_MAX_SIZE     1024
+#define QVIRTIO_VENDOR_ID       0x1AF4
+
+#define QVIRTIO_NET_DEVICE_ID   0x1
+#define QVIRTIO_BLK_DEVICE_ID   0x2
+
+typedef struct QVirtioDevice QVirtioDevice;
+typedef struct QVirtQueue QVirtQueue;
+typedef struct QVRing QVRing;
+
+typedef struct QVirtioDevice {
+    /* Device type */
+    uint16_t device_type;
+} QVirtioDevice;
+
+#endif
tests/virtio-blk-test.c +57 -8

88 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw

tests/libqos/virtio-pci.c line 42

Quoted 9 lines · Marc Marí
tests/libqos/virtio-pci.c new file +9 -0
+static void qvirtio_pci_foreach_callback(
+                        QPCIDevice *dev, int devfn, void *data)
+{
+    QVirtioPCIForeachData *d = data;
+    QVirtioPCIDevice *vpcidev = qpcidevice_to_qvirtiodevice(dev);
+
+    if (vpcidev->vdev.device_type == d->device_type) {
+        d->func(&vpcidev->vdev, d->user_data);
+    }

We should think about memory management here. Who will free vpcidev?

In the case where device_type matches, ->func() should be responsible for the virtio device it receives.

In the case where device_type does not match, we must free vpcidev.

tests/libqos/virtio-pci.c line 50

Quoted 8 lines
tests/libqos/virtio-pci.c new file +8 -0
+}
+
+static void qvirtio_pci_assign_device(QVirtioDevice *d, void *data)
+{
+    QVirtioPCIDevice *vpcidev = data;
+    vpcidev->pdev               = ((QVirtioPCIDevice *)d)->pdev;
+    vpcidev->vdev.device_type   = ((QVirtioPCIDevice *)d)->vdev.device_type;
+}

This function may need to g_free(d) once you've settled on a memory management rules for virtio-pci.c.

tests/libqos/virtio-pci.h line 25

Quoted 5 lines
tests/libqos/virtio-pci.h new file +5 -0
+typedef struct QVirtioPCIForeachData {
+    void (*func)(QVirtioDevice *d, void *data);
+    uint16_t device_type;
+    void *user_data;
+} QVirtioPCIForeachData;

This is only used within virtio-pci.c. It should be declared there too since it doesn't need to be public.

Quoted 9 lines
tests/libqos/virtio.h new file +3 -0
new file mode 100644
index 0000000..52f6b5b
@@ -0,0 +1,28 @@
+/*
+ * libqos virtio definitions
+ *

tests/libqos/virtio.h line 13

Quoted 10 lines
tests/libqos/virtio.h new file +10 -0
+ * 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 QVIRTQUEUE_MAX_SIZE     1024

Unused, please drop.

tests/libqos/virtio.h line 21

Quoted 8 lines
tests/libqos/virtio.h new file +8 -0
+#define QVIRTIO_VENDOR_ID       0x1AF4
+
+#define QVIRTIO_NET_DEVICE_ID   0x1
+#define QVIRTIO_BLK_DEVICE_ID   0x2
+
+typedef struct QVirtioDevice QVirtioDevice;
+typedef struct QVirtQueue QVirtQueue;
+typedef struct QVRing QVRing;

QVirtQueue and QVRing are unused. Please drop them.

tests/libqos/virtio.h

Quoted 10 lines
tests/libqos/virtio.h new file +10 -0
+static void pci_basic(void)
+{
+    QVirtioPCIDevice *dev;
+    QPCIBus *bus;
+
+    bus = test_start();
+
+    dev = qvirtio_pci_device_find(bus, QVIRTIO_BLK_DEVICE_ID);
+    g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_BLK_DEVICE_ID);
+    g_assert_cmphex(dev->pdev->devfn, ==, ((PCI_SLOT << 3) | PCI_FN));

Should dev be freed once we are done with it?