Skip to contentSkip to conversation

qdev: add string and binary properties.

PATCH In review Gerd Hoffmann 2 participants 4 messages May 28, 2009 – May 29, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 qdev: add string and binary properties.

---
 hw/qdev.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/qdev.h |    5 +++++
 2 files changed, 66 insertions(+), 0 deletions(-)
hw/qdev.c +61 -0
index f271b7f..d723b3e 100644
@@ -35,12 +35,17 @@ struct DeviceProperty {
     const char *name;
     enum {
         PROP_INT,
+        PROP_STR,
+        PROP_BIN,
         PROP_PTR,
     } type;
     union {
         uint64_t i;
+        char *str;
+        uint8_t *bin;
         void *ptr;
     } value;
+    size_t len;
     DeviceProperty *next;
 };
 
@@ -146,6 +151,26 @@ void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value)
     prop->value.i = value;
 }
 
+void qdev_set_prop_str(DeviceState *dev, const char *name, const char *str)
+{
+    DeviceProperty *prop;
+
+    prop = create_prop(dev, name);
+    prop->type = PROP_STR;
+    prop->value.str = qemu_strdup(str);
+}
+
+void qdev_set_prop_bin(DeviceState *dev, const char *name, const uint8_t *bin, size_t len)
+{
+    DeviceProperty *prop;
+
+    prop = create_prop(dev, name);
+    prop->type = PROP_BIN;
+    prop->value.bin = qemu_malloc(len);
+    prop->len = len;
+    memcpy(prop->value.bin, bin, len);
+}
+
 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
 {
     DeviceProperty *prop;
@@ -203,6 +228,30 @@ uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def)
     return prop->value.i;
 }
 
+const char *qdev_get_prop_str(DeviceState *dev, const char *name, const char *def)
+{
+    DeviceProperty *prop;
+
+    prop = find_prop(dev, name);
+    if (!prop || prop->type != PROP_STR)
+        return def;
+
+    return prop->value.str;
+}
+
+int qdev_get_prop_bin(DeviceState *dev, const char *name, uint8_t *dest, size_t size)
+{
+    DeviceProperty *prop;
+
+    prop = find_prop(dev, name);
+    if (!prop || prop->type != PROP_BIN)
+        return -1;
+    if (size < prop->len)
+        return -1;
+    memcpy(dest, prop->value.bin, prop->len);
+    return 0;
+}
+
 void *qdev_get_prop_ptr(DeviceState *dev, const char *name)
 {
     DeviceProperty *prop;
@@ -326,6 +375,7 @@ void qbus_print(Monitor *mon, BusState *bus, int indent)
     struct DeviceState *dev;
     struct BusState *child;
     struct DeviceProperty *prop;
+    int i;
 
     monitor_printf(mon, "%*sbus: name %s, type %d\n", indent, "",
                    bus->name, bus->type);
@@ -336,6 +386,17 @@ void qbus_print(Monitor *mon, BusState *bus, int indent)
             case PROP_INT:
                 monitor_printf(mon, ", %s=%" PRId64, prop->name, prop->value.i);
                 break;
+            case PROP_STR:
+                monitor_printf(mon, ", %s=\"%s\"", prop->name, prop->value.str);
+                break;
+            case PROP_BIN:
+                monitor_printf(mon, ", %s=", prop->name);
+                for (i = 0; i < prop->len && i < 8; i++) {
+                    monitor_printf(mon, "%s%02x", 0 == i ? "" : ",",
+                                   prop->value.bin[i]);
+                }
+                monitor_printf(mon, "%s", i == prop->len ? "" : ",...");
+                break;
             case PROP_PTR:
                 monitor_printf(mon, ", %s=%p", prop->name, prop->value.ptr);
                 break;
hw/qdev.h +5 -0
index ab670de..2f5d9dc 100644
@@ -50,6 +50,9 @@ void qdev_free(DeviceState *dev);
 
 /* Set properties between creation and init.  */
 void qdev_set_prop_int(DeviceState *dev, const char *name, uint64_t value);
+void qdev_set_prop_str(DeviceState *dev, const char *name, const char *str);
+void qdev_set_prop_bin(DeviceState *dev, const char *name, const uint8_t *bin,
+                       size_t len);
 void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value);
 void qdev_set_netdev(DeviceState *dev, NICInfo *nd);
 
@@ -84,6 +87,8 @@ CharDriverState *qdev_init_chardev(DeviceState *dev);
 
 BusState *qdev_get_parent_bus(DeviceState *dev);
 uint64_t qdev_get_prop_int(DeviceState *dev, const char *name, uint64_t def);
+const char *qdev_get_prop_str(DeviceState *dev, const char *name, const char *def);
+int qdev_get_prop_bin(DeviceState *dev, const char *name, uint8_t *dest, size_t size);
 void *qdev_get_prop_ptr(DeviceState *dev, const char *name);
 
 /* Convery from a base type to a parent type, with compile time checking.  */
Signature · 1 line
1.6.2.2
#1 lore raw

PATCH 2/2 qdev: kill DeviceState->nd, use properties instead.

---
 hw/qdev.c |   18 ++++++++++++------
 hw/qdev.h |    1 -
 2 files changed, 12 insertions(+), 7 deletions(-)
hw/qdev.c +12 -6
index d723b3e..8c06fce 100644
@@ -182,8 +182,12 @@ void qdev_set_prop_ptr(DeviceState *dev, const char *name, void *value)
 
 void qdev_set_netdev(DeviceState *dev, NICInfo *nd)
 {
-    assert(!dev->nd);
-    dev->nd = nd;
+    qdev_set_prop_int(dev, "nic_vlan", nd->vlan->id);
+    qdev_set_prop_bin(dev, "nic_addr", nd->macaddr, 6);
+    if (nd->name)
+        qdev_set_prop_str(dev, "nic_name", nd->name);
+    if (nd->model)
+        qdev_set_prop_str(dev, "nic_model", nd->model);
 }
 
 
@@ -294,16 +298,18 @@ VLANClientState *qdev_get_vlan_client(DeviceState *dev,
                                       NetCleanup *cleanup,
                                       void *opaque)
 {
-    NICInfo *nd = dev->nd;
-    assert(nd);
-    return qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
+    int vlan = qdev_get_prop_int(dev, "nic_vlan", 0);
+
+    return qemu_new_vlan_client(qemu_find_vlan(vlan),
+                                qdev_get_prop_str(dev, "nic_model", NULL),
+                                qdev_get_prop_str(dev, "nic_name", NULL),
                                 fd_read, fd_can_read, cleanup, opaque);
 }
 
 
 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
 {
-    memcpy(macaddr, dev->nd->macaddr, 6);
+    qdev_get_prop_bin(dev, "nic_addr", macaddr, 6);
 }
 
 static int next_block_unit[IF_COUNT];
hw/qdev.h +0 -1
index 2f5d9dc..3adc680 100644
@@ -21,7 +21,6 @@ struct DeviceState {
     int num_gpio_in;
     qemu_irq *gpio_in;
     LIST_HEAD(, BusState) child_bus;
-    NICInfo *nd;
     LIST_ENTRY(DeviceState) sibling;
 };
 
Signature · 1 line
1.6.2.2
#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] qdev: kill DeviceState->nd, use properties instead.

Quoted 1 line

kill DeviceState->nd, use properties instead.

I'm not sure this is the right solution. Before changing this I think we need to figure out how we want to link devices to the host network configuration.

Paul

#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] qdev: kill DeviceState->nd, use properties instead.

Quoted 4 lines · Paul Brook
Quoted 1 line

kill DeviceState->nd, use properties instead.

I'm not sure this is the right solution. Before changing this I think we need to figure out how we want to link devices to the host network configuration.

Sure, when we replace the vlans with something else the vlan property wouldn't make sense any more (guess this is the discussion you are referring to). We can drop it once we have a replacement for the current model. Not (yet) having a replacement shouldn't stop us from putting qdev into use though.

cheers,
Gerd