qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC 3/9] net: adjust net common part for qomify -netdev


From: zwu . kernel
Subject: [Qemu-devel] [RFC 3/9] net: adjust net common part for qomify -netdev
Date: Mon, 26 Mar 2012 13:40:17 +0800

From: Zhi Yong Wu <address@hidden>

Signed-off-by: Zhi Yong Wu <address@hidden>
---
 net.c |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 net.h |    1 +
 vl.c  |   12 +++---
 3 files changed, 109 insertions(+), 12 deletions(-)

diff --git a/net.c b/net.c
index 608c090..ff8ddaf 100644
--- a/net.c
+++ b/net.c
@@ -624,10 +624,7 @@ static int net_init_nic(QemuOpts *opts,
         .help = "identifier for monitor commands", \
      }
 
-typedef int NetClientInitFunc(QemuOpts *opts,
-                              Monitor *mon,
-                              const char *name,
-                              NetClientState *peer);
+typedef int NetClientInitFunc(NETDevice *host_dev);
 
 /* magic number, but compiler will warn if too small */
 #define NET_MAX_DESC 20
@@ -956,7 +953,13 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int 
is_netdev)
 
             ret = 0;
             if (net_client_types[i].init) {
-                ret = net_client_types[i].init(opts, mon, name, peer);
+                NETDevice host_dev;
+                host_dev.mon = mon;
+                host_dev.opts = opts;
+                host_dev.name = g_strdup(name);
+                host_dev.peer = peer;
+                ret = net_client_types[i].init(&host_dev);
+                //ret = net_client_types[i].init(opts, mon, name, peer);
                 if (ret < 0) {
                     /* TODO push error reporting into init() methods */
                     qerror_report(QERR_DEVICE_INIT_FAILED, type);
@@ -972,6 +975,99 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int 
is_netdev)
     return -1;
 }
 
+static bool netdev_device_add(Monitor *mon,
+                              QemuOpts *opts,
+                              const char *name,
+                              NetClientState *peer)
+{
+    const char *type, *id;
+    NETDevice *net_dev;
+    HOSTDevice *host_dev;
+
+    type = qemu_opt_get(opts, "type");
+    if (!type) {
+        qerror_report(QERR_MISSING_PARAMETER, "type");
+        return false;
+    }
+
+    host_dev = hostdev_device_create(type);
+    if (!host_dev) {
+        return false;
+    }
+
+    net_dev = NET_DEVICE(host_dev);
+    net_dev->mon = mon;
+    net_dev->opts = opts;
+    net_dev->name = g_strdup(name);
+    net_dev->peer = peer;
+
+    hostdev_prop_set_peer(&net_dev->host_dev, "peer", peer);
+    //hostdev_prop_set_string(&net_dev->host_dev, "name", g_strdup(name));
+    //hostdev_prop_set_string(&net_dev->host_dev, "model", g_strdup(model));
+    //hostdev_prop_set_bit(&net_dev->host_dev, "receive_disabled", 
receive_disabled);
+
+    id = qemu_opts_id(opts);
+    if (hostdev_device_init(&net_dev->host_dev, type, id)) {
+        return false;
+    }
+
+    return true;
+}
+
+static int net_client_netdev_init(Monitor *mon, QemuOpts *opts, int is_netdev)
+{
+    const char *name;
+    const char *type;
+
+    type = qemu_opt_get(opts, "type");
+    if (!type) {
+        qerror_report(QERR_MISSING_PARAMETER, "type");
+        return -1;
+    }
+
+    if (is_netdev) {
+        if (strcmp(type, "tap") != 0 &&
+#ifdef CONFIG_NET_BRIDGE
+            strcmp(type, "bridge") != 0 &&
+#endif
+#ifdef CONFIG_SLIRP
+            strcmp(type, "user") != 0 &&
+#endif
+#ifdef CONFIG_VDE
+            strcmp(type, "vde") != 0 &&
+#endif
+            strcmp(type, "socket") != 0) {
+            qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
+                          "a netdev backend type");
+            return -1;
+        }
+
+        if (qemu_opt_get(opts, "vlan")) {
+            qerror_report(QERR_INVALID_PARAMETER, "vlan");
+            return -1;
+        }
+        if (qemu_opt_get(opts, "name")) {
+            qerror_report(QERR_INVALID_PARAMETER, "name");
+            return -1;
+        }
+        if (!qemu_opts_id(opts)) {
+            qerror_report(QERR_MISSING_PARAMETER, "id");
+            return -1;
+        }
+    }
+
+    name = qemu_opts_id(opts);
+    if (!name) {
+        name = qemu_opt_get(opts, "name");
+    }
+
+    if (!netdev_device_add(mon, opts, (char *)name, NULL)) {
+        return -1;
+    }
+
+    return 0;
+}
+
 static int net_host_check_device(const char *device)
 {
     int i;
@@ -1188,7 +1284,7 @@ static int net_init_client(QemuOpts *opts, void *dummy)
 
 static int net_init_netdev(QemuOpts *opts, void *dummy)
 {
-    return net_client_init(NULL, opts, 1);
+    return net_client_netdev_init(NULL, opts, 1);
 }
 
 int net_init_clients(void)
diff --git a/net.h b/net.h
index 912fa2d..3de60d4 100644
--- a/net.h
+++ b/net.h
@@ -88,6 +88,7 @@ typedef struct NetClientInfo {
 } NetClientInfo;
 
 struct NetClientState {
+    NETDevice net_dev;
     NetClientInfo *info;
     int link_down;
     QTAILQ_ENTRY(NetClientState) next;
diff --git a/vl.c b/vl.c
index 112b0e0..0fa8e03 100644
--- a/vl.c
+++ b/vl.c
@@ -2299,8 +2299,6 @@ int main(int argc, char **argv, char **envp)
 #endif
     }
 
-    module_call_init(MODULE_INIT_QOM);
-
     runstate_init();
 
     init_clocks();
@@ -3381,10 +3379,6 @@ int main(int argc, char **argv, char **envp)
     }
     configure_icount(icount_option);
 
-    if (net_init_clients() < 0) {
-        exit(1);
-    }
-
     /* init the bluetooth world */
     if (foreach_device_config(DEV_BT, bt_parse))
         exit(1);
@@ -3474,6 +3468,8 @@ int main(int argc, char **argv, char **envp)
     if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
         exit(1);
 
+    module_call_init(MODULE_INIT_QOM);
+
     /* must be after qdev registration but before machine init */
     if (vga_model) {
         select_vgahw(vga_model);
@@ -3514,6 +3510,10 @@ int main(int argc, char **argv, char **envp)
             exit(1);
     }
 
+    if (net_init_clients() < 0) {
+        exit(1);
+    }
+
     /* init generic devices */
     if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) 
!= 0)
         exit(1);
-- 
1.7.6




reply via email to

[Prev in Thread] Current Thread [Next in Thread]