Skip to contentSkip to conversation

replace duplicated code.

PATCH In review Glauber Costa 3 participants 5 messages Jun 17, 2009 – Jun 18, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 replace duplicated code.

pci_register_simple() does exactly the same of great part of the code in pci nic initialization. Replace it.

---
 hw/pci.c  |   12 ++++--------
 hw/qdev.c |    1 +
 2 files changed, 5 insertions(+), 8 deletions(-)
hw/pci.c +4 -8
index baffadd..0169b78 100644
@@ -818,20 +818,16 @@ static const char * const pci_nic_names[] = {
 PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
                   const char *default_model)
 {
-    DeviceState *dev;
     int i;
 
     qemu_check_nic_model_list(nd, pci_nic_models, default_model);
 
     for (i = 0; pci_nic_models[i]; i++) {
         if (strcmp(nd->model, pci_nic_models[i]) == 0) {
-            dev = qdev_create(&bus->qbus, pci_nic_names[i]);
-            qdev_set_prop_int(dev, "devfn", devfn);
-            qdev_set_prop_ptr(dev, "name", (void *)pci_nic_names[i]);
-            qdev_set_netdev(dev, nd);
-            qdev_init(dev);
-            nd->private = dev;
-            return (PCIDevice *)dev;
+            PCIDevice *d;
+            d = pci_create_simple(bus, devfn, pci_nic_names[i]);
+            qdev_set_netdev((DeviceState *)d, nd);
+            return d;
         }
     }
 
hw/qdev.c +1 -0
index 5175fe1..c7446d5 100644
@@ -164,6 +164,7 @@ void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
 {
     assert(!dev->nd);
     dev->nd = nd;
+    nd->private = dev;
 }
 
 
Signature · 1 line
1.6.2.2
#1 lore raw
Quoted 17 lines · Glauber Costa

pci_register_simple() does exactly the same of great part of the code in pci nic initialization. Replace it.

---
 hw/pci.c  |   12 ++++--------
 hw/qdev.c |    1 +
 2 files changed, 5 insertions(+), 8 deletions(-)
hw/pci.c +0 -0
index baffadd..0169b78 100644
@@ -818,20 +818,16 @@ static const char * const pci_nic_names[] = {
 PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn,
                   const char *default_model)
 {

hw/pci.c static const char * const pci_nic_names[] = { · line 830

Quoted 10 lines
hw/pci.c +0 -4
-    DeviceState *dev;
     int i;
 
     qemu_check_nic_model_list(nd, pci_nic_models, default_model);
 
     for (i = 0; pci_nic_models[i]; i++) {
         if (strcmp(nd->model, pci_nic_models[i]) == 0) {
-            dev = qdev_create(&bus->qbus, pci_nic_names[i]);
-            qdev_set_prop_int(dev, "devfn", devfn);
-            qdev_set_prop_ptr(dev, "name", (void *)pci_nic_names[i]);

So, this patch depends on your "allow for name property" patch - resend as a series, maybe with this one first?

hw/pci.c static const char * const pci_nic_names[] = { · line 829

Quoted 7 lines
hw/pci.c +3 -4
-            qdev_set_netdev(dev, nd);
-            qdev_init(dev);
-            nd->private = dev;
-            return (PCIDevice *)dev;
+            PCIDevice *d;
+            d = pci_create_simple(bus, devfn, pci_nic_names[i]);
+            qdev_set_netdev((DeviceState *)d, nd);

I'd prefer &d->qdev here for type safety.

Cheers,
Mark.

#2 lore raw
In reply to Glauber Costa

hw/pci.c static const char * const pci_nic_names[] = {

Quoted 8 lines
hw/pci.c +2 -5
-            dev = qdev_create(&bus->qbus, pci_nic_names[i]);
-            qdev_set_prop_int(dev, "devfn", devfn);
-            qdev_set_prop_ptr(dev, "name", (void *)pci_nic_names[i]);
-            qdev_set_netdev(dev, nd);
-            qdev_init(dev);
 
+            d = pci_create_simple(bus, devfn, pci_nic_names[i]);
+            qdev_set_netdev((DeviceState *)d, nd);

You have changed the ordering of the qdev_set_netdev and qdev_init calls. I don't think this is correct, the init callback will not see the nicinfo then.

cheers,
Gerd

#3 lore raw
Quoted 6 lines · Gerd Hoffmann
Quoted 5 lines
hw/pci.c +0 -5
-            dev = qdev_create(&bus->qbus, pci_nic_names[i]);
-            qdev_set_prop_int(dev, "devfn", devfn);
-            qdev_set_prop_ptr(dev, "name", (void *)pci_nic_names[i]);
-            qdev_set_netdev(dev, nd);
-            qdev_init(dev);
Quoted 6 lines

hw/pci.c static const char * const pci_nic_names[] = { · line 829

Quoted 2 lines
hw/pci.c +2 -0
+            d = pci_create_simple(bus, devfn, pci_nic_names[i]);
+            qdev_set_netdev((DeviceState *)d, nd);

You have changed the ordering of the qdev_set_netdev and qdev_init calls. I don't think this is correct, the init callback will not see the nicinfo then.

I see.

How would you feel about changing the nd field of qdev for a void * one, call it private, or whatever?

then we could have a generic qdev_set_private(dev, p), that does it for everybody. I must say I kinda dislike that specific dependency on nd for net devices. Ideally, all devices should be treated as the same.

#4 lore raw
Quoted 11 lines · Glauber Costa
Quoted 3 lines

You have changed the ordering of the qdev_set_netdev and qdev_init calls. I don't think this is correct, the init callback will not see the nicinfo then.

I see.

How would you feel about changing the nd field of qdev for a void * one, call it private, or whatever?

then we could have a generic qdev_set_private(dev, p), that does it for everybody. I must say I kinda dislike that specific dependency on nd for net devices. Ideally, all devices should be treated as the same.

What problem you are trying to solve?
Unless there is something urgent to fix I'd leave it as is for now.

We need some sane way to link guest config (hopefully done by config file soon) to host config. Link nic to vlan in that case, but disks and chardevs share the same issue. I consider DeviceState->nd being temporary and expect will go away once we figured that out. It is a bigger undertaking though ...

cheers,
Gerd