Skip to contentSkip to conversation

qdev-ify network cards

RFC PATCH In review Gerd Hoffmann 2 participants 12 messages Oct 16, 2009 – Oct 19, 2009

lore
Share & tools
lore
#0 lore raw

v3 cover qdev-ify network cards

Hi,

Short RfC patch series to get the discussion rolling. We really need to get the nic drivers qdev-ified properly, so qemu stops segfaulting on '-device $any_nic_here'.

New in v3:
* renamed macaddr_t to MACAddr and made it a struct.
* Adapted to netdev changes merged recently.
* Also convert ne2k_pci.
* Some new qdev properties and common bits for nics to ease conversion.
* load pxe rom unconditionally (ne2k_pci only).

New in v2:
* Fix some style nits and minor issues from review.
* Don't hook up the nic to vlan 0 by default, in preparation to other ways coming to hook up nics.
* Add pxe property for option rom loading.

To keep things small simple I've started with the ne2k_isa. All the PCI nics share the initialization path and thus I can't simply pick a single one as example and convert it.

The network card can now be created using ...

-device ne2k_isa,mac=00:11:22:33:44:55,vlan=1,irq=3,id=foo

'info qtree' shows ...

	[ ... ]
        bus: isa.0
          type ISA
          dev: ne2k_isa, id "foo"
            dev-prop: iobase = 0x300
            dev-prop: irq = 3
            dev-prop: mac = 00:11:22:33:44:55
            dev-prop: vlan = 1
            dev-prop: netdev = <null>
	[ ... ]

'info network' shows:

	[ ... ]
	VLAN 1 devices:
	  foo: model=ne2k_isa,macaddr=00:11:22:33:44:55

With a vlan specified the nic initialization code calls qemu_new_vlan_client() with the vlan specified using the vlan property. Likewise the device cleanup code should call qemu_del_vlan_client.

struct NICInfo is not needed at all here. I hope we can kill it long-term.

This patch series is also available here:
http://repo.or.cz/w/qemu/kraxel.git?a=shortlog;h=refs/heads/nic.v3

cheers,
Gerd

#1 lore raw

RFC PATCH v3 1/10 net: macaddr tweaks.

Add new type for mac addresses.

Add function which sets the qemu default mac address if it finds the mac address uninitialized (i.e. all zeros).

---
 net.c         |   15 +++++++++++++++
 net.h         |    5 +++++
 qemu-common.h |    1 +
 3 files changed, 21 insertions(+), 0 deletions(-)
net.c +15 -0
index 4708080..3b69d3b 100644
@@ -280,6 +280,21 @@ void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
              macaddr[3], macaddr[4], macaddr[5]);
 }
 
+void qemu_macaddr_default_if_unset(MACAddr *macaddr)
+{
+    static int index = 0;
+    static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
+
+    if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
+        return;
+    macaddr->a[0] = 0x52;
+    macaddr->a[1] = 0x54;
+    macaddr->a[2] = 0x00;
+    macaddr->a[3] = 0x12;
+    macaddr->a[4] = 0x34;
+    macaddr->a[5] = 0x56 + index++;
+}
+
 static char *assign_name(VLANClientState *vc1, const char *model)
 {
     VLANState *vlan;
net.h +5 -0
index 439de2a..605092a 100644
@@ -7,6 +7,10 @@
 #include "qemu-option.h"
 #include "net-queue.h"
 
+struct MACAddr {
+    uint8_t a[6];
+};
+
 /* VLANs support */
 
 typedef int (NetCanReceive)(VLANClientState *);
@@ -65,6 +69,7 @@ ssize_t qemu_send_packet_async(VLANClientState *vc, const uint8_t *buf,
 void qemu_purge_queued_packets(VLANClientState *vc);
 void qemu_flush_queued_packets(VLANClientState *vc);
 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]);
+void qemu_macaddr_default_if_unset(MACAddr *macaddr);
 int qemu_show_nic_models(const char *arg, const char *const *models);
 void qemu_check_nic_model(NICInfo *nd, const char *model);
 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
qemu-common.h +1 -0
index 8551862..980f362 100644
@@ -180,6 +180,7 @@ typedef struct PixelFormat PixelFormat;
 typedef struct TextConsole TextConsole;
 typedef TextConsole QEMUConsole;
 typedef struct CharDriverState CharDriverState;
+typedef struct MACAddr MACAddr;
 typedef struct VLANState VLANState;
 typedef struct VLANClientState VLANClientState;
 typedef struct QEMUFile QEMUFile;
Signature · 1 line
1.6.2.5
#2 lore raw

RFC PATCH v3 2/10 qdev: mac addr property fixups

In reply to Gerd Hoffmann
---
 hw/qdev-properties.c |   31 +++++++++++++++++++++----------
 hw/qdev.h            |    3 ++-
 2 files changed, 23 insertions(+), 11 deletions(-)
hw/qdev-properties.c +21 -10
index 5c627fa..1d68125 100644
@@ -1,4 +1,5 @@
 #include "sysemu.h"
+#include "net.h"
 #include "qdev.h"
 
 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
@@ -274,7 +275,7 @@ PropertyInfo qdev_prop_ptr = {
  */
 static int parse_mac(DeviceState *dev, Property *prop, const char *str)
 {
-    uint8_t *mac = qdev_get_prop_ptr(dev, prop);
+    MACAddr *mac = qdev_get_prop_ptr(dev, prop);
     int i, pos;
     char *p;
 
@@ -283,26 +284,31 @@ static int parse_mac(DeviceState *dev, Property *prop, const char *str)
             return -1;
         if (!qemu_isxdigit(str[pos+1]))
             return -1;
-        if (i == 5 && str[pos+2] != '\0')
-            return -1;
-        if (str[pos+2] != ':' && str[pos+2] != '-')
-            return -1;
-        mac[i] = strtol(str+pos, &p, 16);
+        if (i == 5) {
+            if (str[pos+2] != '\0')
+                return -1;
+        } else {
+            if (str[pos+2] != ':' && str[pos+2] != '-')
+                return -1;
+        }
+        mac->a[i] = strtol(str+pos, &p, 16);
     }
     return 0;
 }
 
 static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len)
 {
-    uint8_t *mac = qdev_get_prop_ptr(dev, prop);
+    MACAddr *mac = qdev_get_prop_ptr(dev, prop);
+
     return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x",
-                    mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+                    mac->a[0], mac->a[1], mac->a[2],
+                    mac->a[3], mac->a[4], mac->a[5]);
 }
 
 PropertyInfo qdev_prop_macaddr = {
-    .name  = "mac-addr",
+    .name  = "macaddr",
     .type  = PROP_TYPE_MACADDR,
-    .size  = 6,
+    .size  = sizeof(MACAddr),
     .parse = parse_mac,
     .print = print_mac,
 };
@@ -454,6 +460,11 @@ void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *valu
     qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
 }
 
+void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
+{
+    qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
+}
+
 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
 {
     qdev_prop_set(dev, name, &value, PROP_TYPE_PTR);
hw/qdev.h +2 -1
index 8cd843e..118e886 100644
@@ -230,7 +230,7 @@ extern PropertyInfo qdev_prop_pci_devfn;
 #define DEFINE_PROP_DRIVE(_n, _s, _f)             \
     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, DriveInfo*)
 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
-    DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, uint8_t[6])
+    DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, MACAddr)
 
 #define DEFINE_PROP_END_OF_LIST()               \
     {}
@@ -246,6 +246,7 @@ void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
 void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value);
+void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
 /* FIXME: Remove opaque pointer properties.  */
 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value);
 void qdev_prop_set_defaults(DeviceState *dev, Property *props);
Signature · 1 line
1.6.2.5
#3 lore raw

RFC PATCH v3 3/10 qdev: add netdev property

In reply to Gerd Hoffmann
---
 hw/qdev-properties.c |   36 ++++++++++++++++++++++++++++++++++++
 hw/qdev.h            |    4 ++++
 net.c                |    2 +-
 net.h                |    1 +
 4 files changed, 42 insertions(+), 1 deletions(-)
hw/qdev-properties.c +36 -0
index 1d68125..76925c8 100644
@@ -251,6 +251,37 @@ PropertyInfo qdev_prop_chr = {
     .print = print_chr,
 };
 
+/* --- netdev device --- */
+
+static int parse_netdev(DeviceState *dev, Property *prop, const char *str)
+{
+    VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
+
+    *ptr = qemu_find_netdev(str);
+    if (*ptr == NULL)
+        return -1;
+    return 0;
+}
+
+static int print_netdev(DeviceState *dev, Property *prop, char *dest, size_t len)
+{
+    VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
+
+    if (*ptr && (*ptr)->name) {
+        return snprintf(dest, len, "%s", (*ptr)->name);
+    } else {
+        return snprintf(dest, len, "<null>");
+    }
+}
+
+PropertyInfo qdev_prop_netdev = {
+    .name  = "netdev",
+    .type  = PROP_TYPE_NETDEV,
+    .size  = sizeof(VLANClientState*),
+    .parse = parse_netdev,
+    .print = print_netdev,
+};
+
 /* --- pointer --- */
 
 static int print_ptr(DeviceState *dev, Property *prop, char *dest, size_t len)
@@ -460,6 +491,11 @@ void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *valu
     qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
 }
 
+void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
+{
+    qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
+}
+
 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
 {
     qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
hw/qdev.h +4 -0
index 118e886..c0d59bc 100644
@@ -79,6 +79,7 @@ enum PropertyType {
     PROP_TYPE_MACADDR,
     PROP_TYPE_DRIVE,
     PROP_TYPE_CHR,
+    PROP_TYPE_NETDEV,
     PROP_TYPE_PTR,
 };
 
@@ -227,6 +228,8 @@ extern PropertyInfo qdev_prop_pci_devfn;
     DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
 #define DEFINE_PROP_CHR(_n, _s, _f)             \
     DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
+#define DEFINE_PROP_NETDEV(_n, _s, _f)             \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
 #define DEFINE_PROP_DRIVE(_n, _s, _f)             \
     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, DriveInfo*)
 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
@@ -245,6 +248,7 @@ void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value);
 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
+void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
 void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value);
 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
 /* FIXME: Remove opaque pointer properties.  */
net.c +1 -1
index 3b69d3b..75a01d2 100644
@@ -2363,7 +2363,7 @@ VLANState *qemu_find_vlan(int id, int allocate)
     return vlan;
 }
 
-static VLANClientState *qemu_find_netdev(const char *id)
+VLANClientState *qemu_find_netdev(const char *id)
 {
     VLANClientState *vc;
 
net.h +1 -0
index 605092a..6a24f55 100644
@@ -47,6 +47,7 @@ struct VLANState {
 };
 
 VLANState *qemu_find_vlan(int id, int allocate);
+VLANClientState *qemu_find_netdev(const char *id);
 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
                                       VLANClientState *peer,
                                       const char *model,
Signature · 1 line
1.6.2.5
#4 lore raw

RFC PATCH v3 4/10 qdev: add vlan property

In reply to Gerd Hoffmann
---
 hw/qdev-properties.c |   39 +++++++++++++++++++++++++++++++++++++++
 hw/qdev.h            |    6 ++++++
 2 files changed, 45 insertions(+), 0 deletions(-)
hw/qdev-properties.c +39 -0
index 76925c8..c9843a2 100644
@@ -282,6 +282,40 @@ PropertyInfo qdev_prop_netdev = {
     .print = print_netdev,
 };
 
+/* --- vlan --- */
+
+static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
+{
+    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
+    int id;
+
+    if (sscanf(str, "%d", &id) != 1)
+        return -1;
+    *ptr = qemu_find_vlan(id, 1);
+    if (*ptr == NULL)
+        return -1;
+    return 0;
+}
+
+static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
+{
+    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
+
+    if (*ptr) {
+        return snprintf(dest, len, "%d", (*ptr)->id);
+    } else {
+        return snprintf(dest, len, "<null>");
+    }
+}
+
+PropertyInfo qdev_prop_vlan = {
+    .name  = "vlan",
+    .type  = PROP_TYPE_VLAN,
+    .size  = sizeof(VLANClientState*),
+    .parse = parse_vlan,
+    .print = print_vlan,
+};
+
 /* --- pointer --- */
 
 static int print_ptr(DeviceState *dev, Property *prop, char *dest, size_t len)
@@ -496,6 +530,11 @@ void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *v
     qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
 }
 
+void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
+{
+    qdev_prop_set(dev, name, &value, PROP_TYPE_VLAN);
+}
+
 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
 {
     qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
hw/qdev.h +6 -0
index c0d59bc..c678ab2 100644
@@ -80,6 +80,7 @@ enum PropertyType {
     PROP_TYPE_DRIVE,
     PROP_TYPE_CHR,
     PROP_TYPE_NETDEV,
+    PROP_TYPE_VLAN,
     PROP_TYPE_PTR,
 };
 
@@ -191,6 +192,8 @@ extern PropertyInfo qdev_prop_chr;
 extern PropertyInfo qdev_prop_ptr;
 extern PropertyInfo qdev_prop_macaddr;
 extern PropertyInfo qdev_prop_drive;
+extern PropertyInfo qdev_prop_netdev;
+extern PropertyInfo qdev_prop_vlan;
 extern PropertyInfo qdev_prop_pci_devfn;
 
 #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
@@ -230,6 +233,8 @@ extern PropertyInfo qdev_prop_pci_devfn;
     DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*)
 #define DEFINE_PROP_NETDEV(_n, _s, _f)             \
     DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, VLANClientState*)
+#define DEFINE_PROP_VLAN(_n, _s, _f)             \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_vlan, VLANState*)
 #define DEFINE_PROP_DRIVE(_n, _s, _f)             \
     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, DriveInfo*)
 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
@@ -249,6 +254,7 @@ void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value);
 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value);
 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value);
 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value);
+void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value);
 void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value);
 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value);
 /* FIXME: Remove opaque pointer properties.  */
Signature · 1 line
1.6.2.5
#5 lore raw

RFC PATCH v3 5/10 qdev/net: common nic property bits

In reply to Gerd Hoffmann
---
 net.h |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
net.h +13 -0
index 6a24f55..77720af 100644
@@ -11,6 +11,19 @@ struct MACAddr {
     uint8_t a[6];
 };
 
+/* qdev nic properties */
+
+typedef struct NICConf {
+    MACAddr macaddr;
+    VLANState *vlan;
+    VLANClientState *peer;
+} NICConf;
+
+#define DEFINE_NIC_PROPERTIES(_state, _conf)                            \
+    DEFINE_PROP_MACADDR("mac",   _state, _conf.macaddr),                \
+    DEFINE_PROP_VLAN("vlan",     _state, _conf.vlan),                   \
+    DEFINE_PROP_NETDEV("netdev", _state, _conf.peer)
+
 /* VLANs support */
 
 typedef int (NetCanReceive)(VLANClientState *);
Signature · 1 line
1.6.2.5
#6 lore raw

RFC PATCH v3 6/10 ne2k_isa: qdev-ify.

In reply to Gerd Hoffmann
---
 hw/ne2000-isa.c |   12 ++++++++----
 hw/ne2000.c     |    6 +++---
 hw/ne2000.h     |    2 +-
 3 files changed, 12 insertions(+), 8 deletions(-)
hw/ne2000-isa.c +8 -4
index e346731..1bed5d4 100644
@@ -65,13 +65,14 @@ static int isa_ne2000_initfn(ISADevice *dev)
 
     isa_init_irq(dev, &s->irq, isa->isairq);
 
-    qdev_get_macaddr(&dev->qdev, s->macaddr);
+    qemu_macaddr_default_if_unset(&s->c.macaddr);
     ne2000_reset(s);
 
-    s->vc = qdev_get_vlan_client(&dev->qdev,
+    s->vc = qemu_new_vlan_client(s->c.vlan, s->c.peer,
+                                 dev->qdev.info->name, dev->qdev.id,
                                  ne2000_can_receive, ne2000_receive, NULL,
                                  isa_ne2000_cleanup, s);
-    qemu_format_nic_info_str(s->vc, s->macaddr);
+    qemu_format_nic_info_str(s->vc, s->c.macaddr.a);
 
     register_savevm("ne2000", -1, 2, ne2000_save, ne2000_load, s);
     return 0;
@@ -84,9 +85,11 @@ void isa_ne2000_init(int base, int irq, NICInfo *nd)
     qemu_check_nic_model(nd, "ne2k_isa");
 
     dev = isa_create("ne2k_isa");
-    dev->qdev.nd = nd; /* hack alert */
     qdev_prop_set_uint32(&dev->qdev, "iobase", base);
     qdev_prop_set_uint32(&dev->qdev, "irq",    irq);
+    qdev_prop_set_macaddr(&dev->qdev, "mac",   nd->macaddr);
+    if (nd->vlan)
+        qdev_prop_set_int32(&dev->qdev, "vlan",   nd->vlan->id);
     qdev_init_nofail(&dev->qdev);
 }
 
@@ -97,6 +100,7 @@ static ISADeviceInfo ne2000_isa_info = {
     .qdev.props = (Property[]) {
         DEFINE_PROP_HEX32("iobase", ISANE2000State, iobase, 0x300),
         DEFINE_PROP_UINT32("irq",   ISANE2000State, isairq, 9),
+        DEFINE_NIC_PROPERTIES(ISANE2000State, ne2000.c),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
hw/ne2000.c +3 -3
index 87f1e59..7ce56ff 100644
@@ -126,7 +126,7 @@ void ne2000_reset(NE2000State *s)
     int i;
 
     s->isr = ENISR_RESET;
-    memcpy(s->mem, s->macaddr, 6);
+    memcpy(s->mem, &s->c.macaddr, 6);
     s->mem[14] = 0x57;
     s->mem[15] = 0x57;
 
@@ -758,13 +758,13 @@ static int pci_ne2000_init(PCIDevice *pci_dev)
                            PCI_ADDRESS_SPACE_IO, ne2000_map);
     s = &d->ne2000;
     s->irq = d->dev.irq[0];
-    qdev_get_macaddr(&d->dev.qdev, s->macaddr);
+    qdev_get_macaddr(&d->dev.qdev, s->c.macaddr.a);
     ne2000_reset(s);
     s->vc = qdev_get_vlan_client(&d->dev.qdev,
                                  ne2000_can_receive, ne2000_receive, NULL,
                                  ne2000_cleanup, s);
 
-    qemu_format_nic_info_str(s->vc, s->macaddr);
+    qemu_format_nic_info_str(s->vc, s->c.macaddr.a);
 
     register_savevm("ne2000", -1, 3, pci_ne2000_save, pci_ne2000_load, d);
     return 0;
hw/ne2000.h +1 -1
index 92a2ddb..7842246 100644
@@ -23,7 +23,7 @@ typedef struct NE2000State {
     uint8_t mult[8]; /* multicast mask array */
     qemu_irq irq;
     VLANClientState *vc;
-    uint8_t macaddr[6];
+    NICConf c;
     uint8_t mem[NE2000_MEM_SIZE];
 } NE2000State;
 
Signature · 1 line
1.6.2.5
#7 lore raw

RFC PATCH v3 7/10 qdev: add qdev_prop_exists()

In reply to Gerd Hoffmann
---
 hw/qdev-properties.c |    5 +++++
 hw/qdev.h            |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
hw/qdev-properties.c +5 -0
index c9843a2..370f356 100644
@@ -452,6 +452,11 @@ static Property *qdev_prop_find(DeviceState *dev, const char *name)
     return NULL;
 }
 
+int qdev_prop_exists(DeviceState *dev, const char *name)
+{
+    return qdev_prop_find(dev, name) ? true : false;
+}
+
 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
 {
     Property *prop;
hw/qdev.h +1 -0
index c678ab2..5271a3c 100644
@@ -245,6 +245,7 @@ extern PropertyInfo qdev_prop_pci_devfn;
 
 /* Set properties between creation and init.  */
 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
+int qdev_prop_exists(DeviceState *dev, const char *name);
 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type);
 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value);
Signature · 1 line
1.6.2.5
#8 lore raw

RFC PATCH v3 8/10 prepare pci nic init path for qdev conversion

In reply to Gerd Hoffmann
---
 hw/pci.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
hw/pci.c +12 -2
index abf07ca..fe8e939 100644
@@ -859,10 +859,20 @@ PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
     dev = &pci_dev->qdev;
     if (nd->name)
         dev->id = qemu_strdup(nd->name);
-    dev->nd = nd;
+    if (qdev_prop_exists(dev, "mac")) {
+        /* qdev-ified */
+        qdev_prop_set_macaddr(dev, "mac", nd->macaddr);
+        if (nd->vlan)
+            qdev_prop_set_vlan(dev, "vlan", nd->vlan);
+        if (nd->netdev)
+            qdev_prop_set_netdev(dev, "netdev", nd->netdev);
+    } else {
+        /* legacy */
+        dev->nd = nd;
+        nd->private = dev;
+    }
     if (qdev_init(dev) < 0)
         return NULL;
-    nd->private = dev;
     return pci_dev;
 }
 
Signature · 1 line
1.6.2.5
#9 lore raw

RFC PATCH v3 9/10 ne2k_pci: qdev-ify.

In reply to Gerd Hoffmann
---
 hw/ne2000.c |   27 +++++++++++++++++++++------
 1 files changed, 21 insertions(+), 6 deletions(-)
hw/ne2000.c +21 -6
index 7ce56ff..f296b10 100644
@@ -758,22 +758,37 @@ static int pci_ne2000_init(PCIDevice *pci_dev)
                            PCI_ADDRESS_SPACE_IO, ne2000_map);
     s = &d->ne2000;
     s->irq = d->dev.irq[0];
-    qdev_get_macaddr(&d->dev.qdev, s->c.macaddr.a);
+
+    qemu_macaddr_default_if_unset(&s->c.macaddr);
     ne2000_reset(s);
-    s->vc = qdev_get_vlan_client(&d->dev.qdev,
+    s->vc = qemu_new_vlan_client(s->c.vlan, s->c.peer,
+                                 pci_dev->qdev.info->name, pci_dev->qdev.id,
                                  ne2000_can_receive, ne2000_receive, NULL,
                                  ne2000_cleanup, s);
-
     qemu_format_nic_info_str(s->vc, s->c.macaddr.a);
 
     register_savevm("ne2000", -1, 3, pci_ne2000_save, pci_ne2000_load, d);
     return 0;
 }
 
+static int pci_ne2000_exit(PCIDevice *pci_dev)
+{
+    PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
+    NE2000State *s = &d->ne2000;
+
+    qemu_del_vlan_client(s->vc);
+    return 0;
+}
+
 static PCIDeviceInfo ne2000_info = {
-    .qdev.name = "ne2k_pci",
-    .qdev.size = sizeof(PCINE2000State),
-    .init      = pci_ne2000_init,
+    .qdev.name  = "ne2k_pci",
+    .qdev.size  = sizeof(PCINE2000State),
+    .init       = pci_ne2000_init,
+    .exit       = pci_ne2000_exit,
+    .qdev.props = (Property[]) {
+        DEFINE_NIC_PROPERTIES(PCINE2000State, ne2000.c),
+        DEFINE_PROP_END_OF_LIST(),
+    }
 };
 
 static void ne2000_register_devices(void)
Signature · 1 line
1.6.2.5
#10 lore raw

RFC PATCH v3 10/10 ne2k_pci: load rom.

In reply to Gerd Hoffmann
---
 hw/ne2000.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
hw/ne2000.c +9 -0
index f296b10..6cb5f58 100644
@@ -25,6 +25,7 @@
 #include "pci.h"
 #include "net.h"
 #include "ne2000.h"
+#include "loader.h"
 
 /* debug NE2000 card */
 //#define DEBUG_NE2000
@@ -767,6 +768,14 @@ static int pci_ne2000_init(PCIDevice *pci_dev)
                                  ne2000_cleanup, s);
     qemu_format_nic_info_str(s->vc, s->c.macaddr.a);
 
+    if (!pci_dev->qdev.hotplugged) {
+        static int loaded = 0;
+        if (!loaded) {
+            rom_add_option("pxe-ne2k_pci.bin");
+            loaded = 1;
+        }
+    }
+
     register_savevm("ne2000", -1, 3, pci_ne2000_save, pci_ne2000_load, d);
     return 0;
 }
Signature · 1 line
1.6.2.5
Acked-by on the v3 series
#11 lore raw
In reply to Gerd Hoffmann
Quoted 6 lines · Gerd Hoffmann

Hi,

Short RfC patch series to get the discussion rolling. We really need to get the nic drivers qdev-ified properly, so qemu stops segfaulting on '-device $any_nic_here'.

Quoted 7 lines

New in v3:
* renamed macaddr_t to MACAddr and made it a struct.
* Adapted to netdev changes merged recently.
* Also convert ne2k_pci.
* Some new qdev properties and common bits for nics to ease conversion.
* load pxe rom unconditionally (ne2k_pci only).

Very nice, I like it.

Cheers,
Mark.