diff mbox

[5/7] qdev: add qdev_foreach()

Message ID 1258057742-18699-6-git-send-email-markmc@redhat.com
State New
Headers show

Commit Message

Mark McLoughlin Nov. 12, 2009, 8:29 p.m. UTC
No doubt this is the worst idea ever, but the requirement is simple -
some way to iterate all NICs in the system.

Signed-off-by: Mark McLoughlin <markmc@redhat.com>
---
 hw/qdev.c |   20 ++++++++++++++++++++
 hw/qdev.h |    3 +++
 2 files changed, 23 insertions(+), 0 deletions(-)

Comments

Gerd Hoffmann Nov. 16, 2009, 9:28 a.m. UTC | #1
On 11/12/09 21:29, Mark McLoughlin wrote:
> No doubt this is the worst idea ever, but the requirement is simple -
> some way to iterate all NICs in the system.

Doesn't have net.c a list of all vlanclientstates it could use instead?

cheers,
   Gerd
Mark McLoughlin Nov. 20, 2009, 2:20 p.m. UTC | #2
Hi Gerd,

Thanks for looking, you're absolutely right that this approach just
isn't right with e.g. -device

On Mon, 2009-11-16 at 10:28 +0100, Gerd Hoffmann wrote:
> On 11/12/09 21:29, Mark McLoughlin wrote:
> > No doubt this is the worst idea ever, but the requirement is simple -
> > some way to iterate all NICs in the system.
> 
> Doesn't have net.c a list of all vlanclientstates it could use instead?

It does, but there's currently no way of getting at each NIC's MAC
address.

I've pushed a series of patches to the fix-announce-self-rfc.v2 branch
on my tree:

  http://repo.or.cz/w/qemu/markmc.git

The idea is to have a NICState struct:

  struct NICState {
      VLANClientState nc;
      MACAddr *macaddr;
      void *opaque;
  };

and similar structs for each net backend.

This way we can have a qemu_foreach_nic() which iterates over each
client, checks whether it's a NIC and upcast to NICState.

Still a WIP, but how does that sound?

Thanks,
Mark.
Gerd Hoffmann Nov. 23, 2009, 9:55 a.m. UTC | #3
On 11/20/09 15:20, Mark McLoughlin wrote:
> I've pushed a series of patches to the fix-announce-self-rfc.v2 branch
> on my tree:
>
>    http://repo.or.cz/w/qemu/markmc.git

Looks much better.

If you have a special version for nics anyway (qemu_new_nic) you can 
have that one take a NICConf parameter I think.

Is it possible to have NICState embedded, i.e.

    typedef struct E1000State_st {
         [ ... ]
         NICState nic;
         [ ... ]
    };

instead of 'NICState *nic' ?

cheers,
   Gerd
diff mbox

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index b8ab449..d6e3184 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -401,6 +401,26 @@  BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
     return NULL;
 }
 
+static void qdev_foreach_recursive(BusState *bus, qdev_foreach_func func, void *opaque)
+{
+    DeviceState *dev;
+
+    QLIST_FOREACH(dev, &bus->children, sibling) {
+        BusState *child;
+
+        func(dev, opaque);
+
+        QLIST_FOREACH(child, &dev->child_bus, sibling) {
+            qdev_foreach_recursive(child, func, opaque);
+        }
+    }
+}
+
+void qdev_foreach(qdev_foreach_func func, void *opaque)
+{
+    qdev_foreach_recursive(main_system_bus, func, opaque);
+}
+
 static BusState *qbus_find_recursive(BusState *bus, const char *name,
                                      const BusInfo *info)
 {
diff --git a/hw/qdev.h b/hw/qdev.h
index 26e372c..712ae3b 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -153,6 +153,9 @@  CharDriverState *qdev_init_chardev(DeviceState *dev);
 
 BusState *qdev_get_parent_bus(DeviceState *dev);
 
+typedef void (*qdev_foreach_func)(DeviceState *dev, void *opaque);
+void qdev_foreach(qdev_foreach_func func, void *opaque);
+
 /* Convert from a base type to a parent type, with compile time checking.  */
 #ifdef __GNUC__
 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \