diff mbox

[RFC,2/9] net: introduce one net host device class

Message ID 1332740423-8426-5-git-send-email-zwu.kernel@gmail.com
State New
Headers show

Commit Message

Zhiyong Wu March 26, 2012, 5:40 a.m. UTC
From: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>

Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
---
 net.c |   35 +++++++++++++++++++++++++++++++++++
 net.h |   27 +++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 0 deletions(-)

Comments

Paolo Bonzini March 27, 2012, 8:30 a.m. UTC | #1
Il 26/03/2012 07:40, zwu.kernel@gmail.com ha scritto:
> +    /*< public >*/
> +    QemuOpts *opts;
> +    Monitor *mon;
> +    const char *name;
> +    NetClientState *peer;
> +};

The QemuOpts should not be part of the struct.  You need to store them
into properties at initialization time, and forget them afterwards.

All the infrastructure to do this is already in qdev-properties.c.

Paolo
Zhiyong Wu March 27, 2012, 9:13 a.m. UTC | #2
On Tue, Mar 27, 2012 at 4:30 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> Il 26/03/2012 07:40, zwu.kernel@gmail.com ha scritto:
>> +    /*< public >*/
>> +    QemuOpts *opts;
>> +    Monitor *mon;
>> +    const char *name;
>> +    NetClientState *peer;
>> +};
>
> The QemuOpts should not be part of the struct.  You need to store them
> into properties at initialization time, and forget them afterwards.
This should be done in your QemuOpts QOM feature, right?:)
>
> All the infrastructure to do this is already in qdev-properties.c.
>
> Paolo
diff mbox

Patch

diff --git a/net.c b/net.c
index dd67d16..608c090 100644
--- a/net.c
+++ b/net.c
@@ -1231,3 +1231,38 @@  int net_client_parse(QemuOptsList *opts_list, const char *optarg)
     default_net = 0;
     return 0;
 }
+
+static int net_dev_init(HOSTDevice *host_dev)
+{
+    NETDevice *net_dev = NET_DEVICE(host_dev);
+    NETDeviceClass *dc = NETDEV_GET_CLASS(host_dev);
+
+    if (dc->init) {
+        return dc->init(net_dev);
+    }
+
+    return 0;
+}
+
+static void net_class_init(ObjectClass *klass, void *data)
+{
+
+    HOSTDeviceClass *k = HOSTDEV_CLASS(klass);
+
+    k->init = net_dev_init;
+}
+
+static TypeInfo net_type = {
+    .name          = TYPE_NETDEV,
+    .parent        = TYPE_HOSTDEV,
+    .instance_size = sizeof(NETDevice),
+    .class_init    = net_class_init,
+};
+
+static void net_register_types(void)
+{
+    type_register_static(&net_type);
+}
+
+type_init(net_register_types)
+
diff --git a/net.h b/net.h
index 60837ab..912fa2d 100644
--- a/net.h
+++ b/net.h
@@ -7,6 +7,33 @@ 
 #include "qemu-option.h"
 #include "net/queue.h"
 #include "vmstate.h"
+#include "qemu/hostdev.h"
+
+typedef struct NETDevice NETDevice;
+
+#define TYPE_NETDEV "net-dev"
+#define NET_DEVICE(obj) \
+     OBJECT_CHECK(NETDevice, (obj), TYPE_NETDEV)
+#define NETDEV_CLASS(klass) \
+     OBJECT_CLASS_CHECK(NETDeviceClass, (klass), TYPE_NETDEV)
+#define NETDEV_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(NETDeviceClass, (obj), TYPE_NETDEV)
+
+typedef struct NETDeviceClass {
+    HOSTDeviceClass parent_class;
+    int (*init)(NETDevice *net_dev);
+} NETDeviceClass;
+
+struct NETDevice {
+    /*< private >*/
+    HOSTDevice host_dev;
+
+    /*< public >*/
+    QemuOpts *opts;
+    Monitor *mon;
+    const char *name;
+    NetClientState *peer;
+};
 
 struct MACAddr {
     uint8_t a[6];