[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [RFC] Functions bus_foreach and device_find from libqos
From: |
Marc Marí |
Subject: |
Re: [Qemu-devel] [RFC] Functions bus_foreach and device_find from libqos virtio API |
Date: |
Tue, 24 Jun 2014 10:02:08 +0200 |
Hello
> > + for (slot = 0; slot < 32; slot++) {
> > + for (fn = 0; fn < 8; fn++) {
> > + dev = g_malloc0(sizeof(*dev));
> > + dev->bus = bus;
> > + dev->devfn = QPCI_DEVFN(slot, fn);
> > +
> > + if (qpci_config_readw(dev, PCI_VENDOR_ID) == 0xFFFF) {
> > + g_free(dev);
> > + continue;
> > + }
> > +
> > + if (device_type != (uint16_t)-1 &&
> > + qpci_config_readw(dev, PCI_SUBSYSTEM_ID) !=
> > device_type) {
> > + continue;
> > + }
> > +
> > + func(qpcidevice_to_qvirtiodevice(dev), data);
> > + }
> > + }
>
> Can you reuse qpci_device_foreach() instead of duplicating this code?
I've been thinking and I don't know how to join this definition:
void qvirtio_pci_foreach(uint16_t device_id, void (*func)(QVirtioDevice
*d, void *data), void *data);
With this definition:
void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
void (*func)(QPCIDevice *dev, int devfn, void *data), void *data)
The function parameter lacks one parameter (which is PCI specific)
Any idea?
> > +
> > +#endif
> > diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
> > index d53f875..4f5b1e0 100644
> > --- a/tests/virtio-blk-test.c
> > +++ b/tests/virtio-blk-test.c
> > @@ -2,6 +2,7 @@
> > * QTest testcase for VirtIO Block Device
> > *
> > * Copyright (c) 2014 SUSE LINUX Products GmbH
> > + * 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.
> > @@ -9,26 +10,51 @@
> >
> > #include <glib.h>
> > #include <string.h>
> > +#include <stdlib.h>
> > +#include <unistd.h>
> > +#include <stdio.h>
> > #include "libqtest.h"
> > -#include "qemu/osdep.h"
> > +#include "libqos/virtio.h"
> > +/*#include "qemu/osdep.h"*/
> >
> > -/* Tests only initialization so far. TODO: Replace with functional
> > tests */ -static void pci_nop(void)
> > +#define TEST_IMAGE_SIZE (64 * 1024 * 1024)
> > +
> > +static char tmp_path[] = "/tmp/qtest.XXXXXX";
> > +extern QVirtioBus qvirtio_pci;
> > +
> > +static void pci_basic(void)
> > {
> > + QVirtioDevice *dev;
> > + dev = qvirtio_pci.device_find(VIRTIO_BLK_DEVICE_ID);
> > + fprintf(stderr, "Device: %x %x %x\n",
> > + dev->device_id, dev->location,
> > dev->device_type); }
> >
> > int main(int argc, char **argv)
> > {
> > int ret;
> > + int fd;
> > + char test_start[100];
> >
> > g_test_init(&argc, &argv, NULL);
> > - qtest_add_func("/virtio/blk/pci/nop", pci_nop);
> > + qtest_add_func("/virtio/blk/pci/basic", pci_basic);
> >
> > - qtest_start("-drive id=drv0,if=none,file=/dev/null "
> > - "-device virtio-blk-pci,drive=drv0");
> > + /* Create a temporary raw image */
> > + fd = mkstemp(tmp_path);
> > + g_assert_cmpint(fd, >=, 0);
> > + ret = ftruncate(fd, TEST_IMAGE_SIZE);
> > + g_assert_cmpint(ret, ==, 0);
> > + close(fd);
> > +
> > + sprintf(test_start, "-drive if=none,id=drive0,file=%s "
> > + "-device virtio-blk-pci,drive=drive0",
> > tmp_path);
> > + qtest_start(test_start);
>
> Every test case should probably start a fresh QEMU with a fresh disk
> image. That way no device register state or disk image state can
> affect later test cases.
Is it better to create a new temporary file (and so, re-run qemu), or
just truncate every time?