Patchwork [06/16] net: Convert qdev_prop_vlan to peer with hub

login
register
mail settings
Submitter Stefan Hajnoczi
Date July 20, 2012, 12:01 p.m.
Message ID <1342785709-3152-7-git-send-email-stefanha@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/172210/
State New
Headers show

Comments

Stefan Hajnoczi - July 20, 2012, 12:01 p.m.
From: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>

Instead of using VLANState use net/hub.h to support the vlan qdev
property.  The vlan qdev property becomes an alias for the peer qdev
property but is represented as a VLAN ID number.  When a VLAN ID is
selected the device will really peer with a hub port.

Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 hw/qdev-properties.c |   37 ++++++++++++++++++++++++-------------
 hw/qdev.h            |    2 +-
 net.h                |    2 +-
 net/hub.c            |   25 +++++++++++++++++++++++++
 net/hub.h            |    1 +
 5 files changed, 52 insertions(+), 15 deletions(-)
Laszlo Ersek - July 23, 2012, 8:07 p.m.
On 07/20/12 14:01, Stefan Hajnoczi wrote:

> @@ -638,11 +642,17 @@ static void get_vlan(Object *obj, Visitor *v, void *opaque,
>  {
>      DeviceState *dev = DEVICE(obj);
>      Property *prop = opaque;
> -    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
> -    int64_t id;
> +    VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
> +    int64_t id = -1;
>  
> -    id = *ptr ? (*ptr)->id : -1;
> -    visit_type_int64(v, &id, name, errp);
> +    if (*ptr) {
> +        unsigned int hub_id;
> +        if (!net_hub_id_for_client(*ptr, &hub_id)) {
> +            id = (int64_t)hub_id;
> +        }
> +    }
> +
> +    visit_type_int(v, &id, name, errp);
>  }

Should we use uint32 here? (No particular reason, just for "cleanliness"
or whatever.)

Thanks,
Laszlo
Stefan Hajnoczi - July 24, 2012, 10:49 a.m.
On Mon, Jul 23, 2012 at 9:07 PM, Laszlo Ersek <lersek@redhat.com> wrote:
> On 07/20/12 14:01, Stefan Hajnoczi wrote:
>
>> @@ -638,11 +642,17 @@ static void get_vlan(Object *obj, Visitor *v, void *opaque,
>>  {
>>      DeviceState *dev = DEVICE(obj);
>>      Property *prop = opaque;
>> -    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
>> -    int64_t id;
>> +    VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
>> +    int64_t id = -1;
>>
>> -    id = *ptr ? (*ptr)->id : -1;
>> -    visit_type_int64(v, &id, name, errp);
>> +    if (*ptr) {
>> +        unsigned int hub_id;
>> +        if (!net_hub_id_for_client(*ptr, &hub_id)) {
>> +            id = (int64_t)hub_id;
>> +        }
>> +    }
>> +
>> +    visit_type_int(v, &id, name, errp);
>>  }
>
> Should we use uint32 here? (No particular reason, just for "cleanliness"
> or whatever.)

As part of the int64_t -> uint32_t type cleanup we can change this.  I
think it's a safe change to make since no reasonable command-line
would make use of the silent 32-bit truncation.

Stefan

Patch

diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
index 24b39e8..6ee1644 100644
--- a/hw/qdev-properties.c
+++ b/hw/qdev-properties.c
@@ -3,6 +3,7 @@ 
 #include "qerror.h"
 #include "blockdev.h"
 #include "hw/block-common.h"
+#include "net/hub.h"
 
 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
 {
@@ -624,13 +625,16 @@  PropertyInfo qdev_prop_netdev = {
 
 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
 {
-    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
+    VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
 
     if (*ptr) {
-        return snprintf(dest, len, "%d", (*ptr)->id);
-    } else {
-        return snprintf(dest, len, "<null>");
+        unsigned int id;
+        if (!net_hub_id_for_client(*ptr, &id)) {
+            return snprintf(dest, len, "%u", id);
+        }
     }
+
+    return snprintf(dest, len, "<null>");
 }
 
 static void get_vlan(Object *obj, Visitor *v, void *opaque,
@@ -638,11 +642,17 @@  static void get_vlan(Object *obj, Visitor *v, void *opaque,
 {
     DeviceState *dev = DEVICE(obj);
     Property *prop = opaque;
-    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
-    int64_t id;
+    VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
+    int64_t id = -1;
 
-    id = *ptr ? (*ptr)->id : -1;
-    visit_type_int64(v, &id, name, errp);
+    if (*ptr) {
+        unsigned int hub_id;
+        if (!net_hub_id_for_client(*ptr, &hub_id)) {
+            id = (int64_t)hub_id;
+        }
+    }
+
+    visit_type_int(v, &id, name, errp);
 }
 
 static void set_vlan(Object *obj, Visitor *v, void *opaque,
@@ -650,10 +660,10 @@  static void set_vlan(Object *obj, Visitor *v, void *opaque,
 {
     DeviceState *dev = DEVICE(obj);
     Property *prop = opaque;
-    VLANState **ptr = qdev_get_prop_ptr(dev, prop);
+    VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
     Error *local_err = NULL;
     int64_t id;
-    VLANState *vlan;
+    VLANClientState *hubport;
 
     if (dev->state != DEV_STATE_CREATED) {
         error_set(errp, QERR_PERMISSION_DENIED);
@@ -669,13 +679,14 @@  static void set_vlan(Object *obj, Visitor *v, void *opaque,
         *ptr = NULL;
         return;
     }
-    vlan = qemu_find_vlan(id, 1);
-    if (!vlan) {
+
+    hubport = net_hub_port_find(id);
+    if (!hubport) {
         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
                   name, prop->info->name);
         return;
     }
-    *ptr = vlan;
+    *ptr = hubport;
 }
 
 PropertyInfo qdev_prop_vlan = {
diff --git a/hw/qdev.h b/hw/qdev.h
index 247dd1e..d47c463 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -292,7 +292,7 @@  extern PropertyInfo qdev_prop_pci_host_devaddr;
 #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_PROP(_n, _s, _f, qdev_prop_vlan, VLANClientState*)
 #define DEFINE_PROP_DRIVE(_n, _s, _f) \
     DEFINE_PROP(_n, _s, _f, qdev_prop_drive, BlockDriverState *)
 #define DEFINE_PROP_MACADDR(_n, _s, _f)         \
diff --git a/net.h b/net.h
index e9c92b2..4bccead 100644
--- a/net.h
+++ b/net.h
@@ -24,7 +24,7 @@  typedef struct NICConf {
 
 #define DEFINE_NIC_PROPERTIES(_state, _conf)                            \
     DEFINE_PROP_MACADDR("mac",   _state, _conf.macaddr),                \
-    DEFINE_PROP_VLAN("vlan",     _state, _conf.vlan),                   \
+    DEFINE_PROP_VLAN("vlan",     _state, _conf.peer),                   \
     DEFINE_PROP_NETDEV("netdev", _state, _conf.peer),                   \
     DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1)
 
diff --git a/net/hub.c b/net/hub.c
index 5d6bbf6..6bd730f 100644
--- a/net/hub.c
+++ b/net/hub.c
@@ -189,6 +189,31 @@  VLANClientState *net_hub_find_client_by_name(unsigned int hub_id,
 }
 
 /**
+ * Find a available port on a hub; otherwise create one new port
+ */
+VLANClientState *net_hub_port_find(unsigned int hub_id)
+{
+    NetHub *hub;
+    NetHubPort *port;
+    VLANClientState *nc;
+
+    QLIST_FOREACH(hub, &hubs, next) {
+        if (hub->id == hub_id) {
+            QLIST_FOREACH(port, &hub->ports, next) {
+                nc = port->nc.peer;
+                if (!nc) {
+                    return &(port->nc);
+                }
+            }
+            break;
+        }
+    }
+
+    nc = net_hub_add_port(hub_id, NULL);
+    return nc;
+}
+
+/**
  * Print hub configuration
  */
 void net_hub_info(Monitor *mon)
diff --git a/net/hub.h b/net/hub.h
index 71dbcd0..20bda21 100644
--- a/net/hub.h
+++ b/net/hub.h
@@ -25,5 +25,6 @@  VLANClientState *net_hub_find_client_by_name(unsigned int hub_id,
 void net_hub_info(Monitor *mon);
 int net_hub_id_for_client(VLANClientState *nc, unsigned int *id);
 void net_hub_check_clients(void);
+VLANClientState *net_hub_port_find(unsigned int hub_id);
 
 #endif /* NET_HUB_H */