Skip to contentSkip to conversation

tests: Functions bus_foreach and device_find from libqos virtio API

PATCH In review Marc Marí 2 participants 2 messages Jul 3, 2014 – Jul 9, 2014

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

PATCH v4 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 |   78 +++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   24 ++++++++++++++
 tests/libqos/virtio.h     |   23 +++++++++++++
 tests/virtio-blk-test.c   |   66 +++++++++++++++++++++++++++++++++-----
 5 files changed, 185 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 +78 -0

78 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 +58 -8

89 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw

tests/libqos/virtio-pci.c line 49

Quoted 11 lines · Marc Marí
tests/libqos/virtio-pci.c new file +11 -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);
+    }
+
+    g_free(vpcidev);

Fine for now but eventually I think we need to pass ownership of vpcidev to ->func() and only free vpcidev if the device type didn't match.

tests/libqos/virtio-pci.c line 78

Quoted 9 lines
tests/libqos/virtio-pci.c new file +9 -0
+QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type)
+{
+    QVirtioPCIDevice *dev;
+
+    dev = g_malloc0(sizeof(*dev));
+    qvirtio_pci_foreach(bus, device_type, qvirtio_pci_assign_device, dev);
+
+    return dev;
+}

What if the device is not found, should we return NULL?