qemu-devel
[Top][All Lists]
Advanced

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

[Qemu-devel] [PATCH v5 02/17] net: Use hubs for the vlan feature


From: zwu . kernel
Subject: [Qemu-devel] [PATCH v5 02/17] net: Use hubs for the vlan feature
Date: Tue, 19 Jun 2012 00:59:04 +0800

From: Stefan Hajnoczi <address@hidden>

Stop using the special-case vlan code in net.c.  Instead use the hub net
client to implement the vlan feature.  The next patch will remove vlan
code from net.c completely.

Signed-off-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Zhi Yong Wu <address@hidden>
---
 net.c           |   38 +++++++++++++++++++++-----------------
 net/dump.c      |   19 +++++++++++++------
 net/dump.h      |    2 +-
 net/slirp.c     |    8 ++++----
 net/slirp.h     |    2 +-
 net/socket.c    |   48 ++++++++++++++++++++++++++----------------------
 net/socket.h    |    2 +-
 net/tap-win32.c |    8 ++++----
 net/tap.c       |    6 +++---
 net/tap.h       |    2 +-
 net/vde.c       |    8 ++++----
 net/vde.h       |    2 +-
 12 files changed, 80 insertions(+), 65 deletions(-)

diff --git a/net.c b/net.c
index 4aa416c..9a63d22 100644
--- a/net.c
+++ b/net.c
@@ -25,6 +25,7 @@
 
 #include "config-host.h"
 
+#include "net/hub.h"
 #include "net/tap.h"
 #include "net/socket.h"
 #include "net/dump.h"
@@ -153,23 +154,25 @@ void qemu_macaddr_default_if_unset(MACAddr *macaddr)
     macaddr->a[5] = 0x56 + index++;
 }
 
+/**
+ * Generate a name for net client
+ *
+ * Only net clients created with the legacy -net option need this.  Naming is
+ * mandatory for net clients created with -netdev.
+ */
 static char *assign_name(VLANClientState *vc1, const char *model)
 {
-    VLANState *vlan;
     VLANClientState *vc;
     char buf[256];
     int id = 0;
 
-    QTAILQ_FOREACH(vlan, &vlans, next) {
-        QTAILQ_FOREACH(vc, &vlan->clients, next) {
-            if (vc != vc1 && strcmp(vc->model, model) == 0) {
-                id++;
-            }
-        }
-    }
-
     QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
-        if (vc != vc1 && strcmp(vc->model, model) == 0) {
+        if (vc == vc1) {
+            continue;
+        }
+        /* For compatibility only bump id for net clients on a vlan */
+        if (strcmp(vc->model, model) == 0 &&
+            net_hub_id_for_client(vc, NULL) == 0) {
             id++;
         }
     }
@@ -745,7 +748,7 @@ int net_handle_fd_param(Monitor *mon, const char *param)
     return fd;
 }
 
-static int net_init_nic(QemuOpts *opts, const char *name, VLANState *vlan)
+static int net_init_nic(QemuOpts *opts, const char *name, VLANClientState 
*peer)
 {
     int idx;
     NICInfo *nd;
@@ -768,8 +771,8 @@ static int net_init_nic(QemuOpts *opts, const char *name, 
VLANState *vlan)
             return -1;
         }
     } else {
-        assert(vlan);
-        nd->vlan = vlan;
+        assert(peer);
+        nd->netdev = peer;
     }
     if (name) {
         nd->name = g_strdup(name);
@@ -819,7 +822,7 @@ static int net_init_nic(QemuOpts *opts, const char *name, 
VLANState *vlan)
 
 typedef int (*net_client_init_func)(QemuOpts *opts,
                                     const char *name,
-                                    VLANState *vlan);
+                                    VLANClientState *peer);
 
 /* magic number, but compiler will warn if too small */
 #define NET_MAX_DESC 20
@@ -1133,7 +1136,7 @@ int net_client_init(QemuOpts *opts, int is_netdev, Error 
**errp)
         if (net_client_types[i].type != NULL &&
             !strcmp(net_client_types[i].type, type)) {
             Error *local_err = NULL;
-            VLANState *vlan = NULL;
+            VLANClientState *peer = NULL;
             int ret;
 
             qemu_opts_validate(opts, &net_client_types[i].desc[0], &local_err);
@@ -1146,12 +1149,12 @@ int net_client_init(QemuOpts *opts, int is_netdev, 
Error **errp)
              * netdev= parameter. */
             if (!(is_netdev ||
                   (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) 
{
-                vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
+                peer = net_hub_add_port(qemu_opt_get_number(opts, "vlan", 0));
             }
 
             ret = 0;
             if (net_client_types[i].init) {
-                ret = net_client_types[i].init(opts, name, vlan);
+                ret = net_client_types[i].init(opts, name, peer);
                 if (ret < 0) {
                     /* TODO push error reporting into init() methods */
                     error_set(errp, QERR_DEVICE_INIT_FAILED, type);
@@ -1316,6 +1319,7 @@ void do_info_network(Monitor *mon)
             print_net_client(mon, peer);
         }
     }
+    net_hub_info(mon);
 }
 
 void qmp_set_link(const char *name, bool up, Error **errp)
diff --git a/net/dump.c b/net/dump.c
index f835c51..a59133a 100644
--- a/net/dump.c
+++ b/net/dump.c
@@ -27,6 +27,7 @@
 #include "qemu-error.h"
 #include "qemu-log.h"
 #include "qemu-timer.h"
+#include "hub.h"
 
 typedef struct DumpState {
     VLANClientState nc;
@@ -99,7 +100,7 @@ static NetClientInfo net_dump_info = {
     .cleanup = dump_cleanup,
 };
 
-static int net_dump_init(VLANState *vlan, const char *device,
+static int net_dump_init(VLANClientState *peer, const char *device,
                          const char *name, const char *filename, int len)
 {
     struct pcap_file_hdr hdr;
@@ -128,7 +129,7 @@ static int net_dump_init(VLANState *vlan, const char 
*device,
         return -1;
     }
 
-    nc = qemu_new_net_client(&net_dump_info, vlan, NULL, device, name);
+    nc = qemu_new_net_client(&net_dump_info, NULL, peer, device, name);
 
     snprintf(nc->info_str, sizeof(nc->info_str),
              "dump to %s (len=%d)", filename, len);
@@ -144,21 +145,27 @@ static int net_dump_init(VLANState *vlan, const char 
*device,
     return 0;
 }
 
-int net_init_dump(QemuOpts *opts, const char *name, VLANState *vlan)
+int net_init_dump(QemuOpts *opts, const char *name, VLANClientState *peer)
 {
     int len;
     const char *file;
     char def_file[128];
 
-    assert(vlan);
+    assert(peer);
 
     file = qemu_opt_get(opts, "file");
     if (!file) {
-        snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id);
+        unsigned int id;
+        int ret;
+
+        ret = net_hub_id_for_client(peer, &id);
+        assert(ret == 0); /* peer must be on a hub */
+
+        snprintf(def_file, sizeof(def_file), "qemu-vlan%u.pcap", id);
         file = def_file;
     }
 
     len = qemu_opt_get_size(opts, "len", 65536);
 
-    return net_dump_init(vlan, "dump", name, file, len);
+    return net_dump_init(peer, "dump", name, file, len);
 }
diff --git a/net/dump.h b/net/dump.h
index 2b5d9ba..907eb03 100644
--- a/net/dump.h
+++ b/net/dump.h
@@ -27,6 +27,6 @@
 #include "net.h"
 #include "qemu-common.h"
 
-int net_init_dump(QemuOpts *opts, const char *name, VLANState *vlan);
+int net_init_dump(QemuOpts *opts, const char *name, VLANClientState *peer);
 
 #endif /* QEMU_NET_DUMP_H */
diff --git a/net/slirp.c b/net/slirp.c
index 37b6ccf..70e7c94 100644
--- a/net/slirp.c
+++ b/net/slirp.c
@@ -134,7 +134,7 @@ static NetClientInfo net_slirp_info = {
     .cleanup = net_slirp_cleanup,
 };
 
-static int net_slirp_init(VLANState *vlan, const char *model,
+static int net_slirp_init(VLANClientState *peer, const char *model,
                           const char *name, int restricted,
                           const char *vnetwork, const char *vhost,
                           const char *vhostname, const char *tftp_export,
@@ -237,7 +237,7 @@ static int net_slirp_init(VLANState *vlan, const char 
*model,
     }
 #endif
 
-    nc = qemu_new_net_client(&net_slirp_info, vlan, NULL, model, name);
+    nc = qemu_new_net_client(&net_slirp_info, NULL, peer, model, name);
 
     snprintf(nc->info_str, sizeof(nc->info_str),
              "net=%s,restrict=%s", inet_ntoa(net),
@@ -676,7 +676,7 @@ static int net_init_slirp_configs(const char *name, const 
char *value, void *opa
     return 0;
 }
 
-int net_init_slirp(QemuOpts *opts, const char *name, VLANState *vlan)
+int net_init_slirp(QemuOpts *opts, const char *name, VLANClientState *peer)
 {
     struct slirp_config_str *config;
     const char *vhost;
@@ -733,7 +733,7 @@ int net_init_slirp(QemuOpts *opts, const char *name, 
VLANState *vlan)
 
     qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0);
 
-    ret = net_slirp_init(vlan, "user", name, restricted, vnet, vhost,
+    ret = net_slirp_init(peer, "user", name, restricted, vnet, vhost,
                          vhostname, tftp_export, bootfile, vdhcp_start,
                          vnamesrv, smb_export, vsmbsrv);
 
diff --git a/net/slirp.h b/net/slirp.h
index 53fe95d..a1ea8f2 100644
--- a/net/slirp.h
+++ b/net/slirp.h
@@ -30,7 +30,7 @@
 
 #ifdef CONFIG_SLIRP
 
-int net_init_slirp(QemuOpts *opts, const char *name, VLANState *vlan);
+int net_init_slirp(QemuOpts *opts, const char *name, VLANClientState *peer);
 
 void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict);
 void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict);
diff --git a/net/socket.c b/net/socket.c
index fcd0a3c..f231f7b 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -44,7 +44,7 @@ typedef struct NetSocketState {
 } NetSocketState;
 
 typedef struct NetSocketListenState {
-    VLANState *vlan;
+    VLANClientState *peer;
     char *model;
     char *name;
     int fd;
@@ -245,7 +245,7 @@ static NetClientInfo net_dgram_socket_info = {
     .cleanup = net_socket_cleanup,
 };
 
-static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
+static NetSocketState *net_socket_fd_init_dgram(VLANClientState *peer,
                                                 const char *model,
                                                 const char *name,
                                                 int fd, int is_connected)
@@ -287,7 +287,7 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState 
*vlan,
         }
     }
 
-    nc = qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, model, name);
+    nc = qemu_new_net_client(&net_dgram_socket_info, NULL, peer, model, name);
 
     snprintf(nc->info_str, sizeof(nc->info_str),
             "socket: fd=%d (%s mcast=%s:%d)",
@@ -323,7 +323,7 @@ static NetClientInfo net_socket_info = {
     .cleanup = net_socket_cleanup,
 };
 
-static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
+static NetSocketState *net_socket_fd_init_stream(VLANClientState *peer,
                                                  const char *model,
                                                  const char *name,
                                                  int fd, int is_connected)
@@ -331,7 +331,7 @@ static NetSocketState *net_socket_fd_init_stream(VLANState 
*vlan,
     VLANClientState *nc;
     NetSocketState *s;
 
-    nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name);
+    nc = qemu_new_net_client(&net_socket_info, NULL, peer, model, name);
 
     snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
 
@@ -347,7 +347,7 @@ static NetSocketState *net_socket_fd_init_stream(VLANState 
*vlan,
     return s;
 }
 
-static NetSocketState *net_socket_fd_init(VLANState *vlan,
+static NetSocketState *net_socket_fd_init(VLANClientState *peer,
                                           const char *model, const char *name,
                                           int fd, int is_connected)
 {
@@ -362,13 +362,13 @@ static NetSocketState *net_socket_fd_init(VLANState *vlan,
     }
     switch(so_type) {
     case SOCK_DGRAM:
-        return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
+        return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
     case SOCK_STREAM:
-        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
+        return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
     default:
         /* who knows ... this could be a eg. a pty, do warn and continue as 
stream */
         fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not 
SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
-        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
+        return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
     }
     return NULL;
 }
@@ -390,15 +390,17 @@ static void net_socket_accept(void *opaque)
             break;
         }
     }
-    s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
-    if (s1) {
+    s1 = net_socket_fd_init(s->peer, s->model, s->name, fd, 1);
+    if (!s1) {
+        closesocket(fd);
+    } else {
         snprintf(s1->nc.info_str, sizeof(s1->nc.info_str),
                  "socket: connection from %s:%d",
                  inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
     }
 }
 
-static int net_socket_listen_init(VLANState *vlan,
+static int net_socket_listen_init(VLANClientState *peer,
                                   const char *model,
                                   const char *name,
                                   const char *host_str)
@@ -438,7 +440,7 @@ static int net_socket_listen_init(VLANState *vlan,
         closesocket(fd);
         return -1;
     }
-    s->vlan = vlan;
+    s->peer = peer;
     s->model = g_strdup(model);
     s->name = name ? g_strdup(name) : NULL;
     s->fd = fd;
@@ -446,7 +448,7 @@ static int net_socket_listen_init(VLANState *vlan,
     return 0;
 }
 
-static int net_socket_connect_init(VLANState *vlan,
+static int net_socket_connect_init(VLANClientState *peer,
                                    const char *model,
                                    const char *name,
                                    const char *host_str)
@@ -487,7 +489,7 @@ static int net_socket_connect_init(VLANState *vlan,
             break;
         }
     }
-    s = net_socket_fd_init(vlan, model, name, fd, connected);
+    s = net_socket_fd_init(peer, model, name, fd, connected);
     if (!s)
         return -1;
     snprintf(s->nc.info_str, sizeof(s->nc.info_str),
@@ -496,7 +498,7 @@ static int net_socket_connect_init(VLANState *vlan,
     return 0;
 }
 
-static int net_socket_mcast_init(VLANState *vlan,
+static int net_socket_mcast_init(VLANClientState *peer,
                                  const char *model,
                                  const char *name,
                                  const char *host_str,
@@ -522,7 +524,7 @@ static int net_socket_mcast_init(VLANState *vlan,
     if (fd < 0)
         return -1;
 
-    s = net_socket_fd_init(vlan, model, name, fd, 0);
+    s = net_socket_fd_init(peer, model, name, fd, 0);
     if (!s)
         return -1;
 
@@ -586,7 +588,7 @@ static int net_socket_udp_init(VLANState *vlan,
     return 0;
 }
 
-int net_init_socket(QemuOpts *opts, const char *name, VLANState *vlan)
+int net_init_socket(QemuOpts *opts, const char *name, VLANClientState *peer)
 {
     if (qemu_opt_get(opts, "fd")) {
         int fd;
@@ -604,7 +606,8 @@ int net_init_socket(QemuOpts *opts, const char *name, 
VLANState *vlan)
             return -1;
         }
 
-        if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) {
+        if (!net_socket_fd_init(peer, "socket", name, fd, 1)) {
+            close(fd);
             return -1;
         }
     } else if (qemu_opt_get(opts, "listen")) {
@@ -620,7 +623,7 @@ int net_init_socket(QemuOpts *opts, const char *name, 
VLANState *vlan)
 
         listen = qemu_opt_get(opts, "listen");
 
-        if (net_socket_listen_init(vlan, "socket", name, listen) == -1) {
+        if (net_socket_listen_init(peer, "socket", name, listen) == -1) {
             return -1;
         }
     } else if (qemu_opt_get(opts, "connect")) {
@@ -636,7 +639,7 @@ int net_init_socket(QemuOpts *opts, const char *name, 
VLANState *vlan)
 
         connect = qemu_opt_get(opts, "connect");
 
-        if (net_socket_connect_init(vlan, "socket", name, connect) == -1) {
+        if (net_socket_connect_init(peer, "socket", name, connect) == -1) {
             return -1;
         }
     } else if (qemu_opt_get(opts, "mcast")) {
@@ -652,7 +655,8 @@ int net_init_socket(QemuOpts *opts, const char *name, 
VLANState *vlan)
         mcast = qemu_opt_get(opts, "mcast");
         localaddr = qemu_opt_get(opts, "localaddr");
 
-        if (net_socket_mcast_init(vlan, "socket", name, mcast, localaddr) == 
-1) {
+        if (net_socket_mcast_init(peer, "socket", name,
+                                  mcast, localaddr) == -1) {
             return -1;
         }
     } else if (qemu_opt_get(opts, "udp")) {
diff --git a/net/socket.h b/net/socket.h
index e1fe959..c1ee5b3 100644
--- a/net/socket.h
+++ b/net/socket.h
@@ -27,6 +27,6 @@
 #include "net.h"
 #include "qemu-common.h"
 
-int net_init_socket(QemuOpts *opts, const char *name, VLANState *vlan);
+int net_init_socket(QemuOpts *opts, const char *name, VLANClientState *vlan);
 
 #endif /* QEMU_NET_SOCKET_H */
diff --git a/net/tap-win32.c b/net/tap-win32.c
index a801a55..c78db42 100644
--- a/net/tap-win32.c
+++ b/net/tap-win32.c
@@ -673,7 +673,7 @@ static NetClientInfo net_tap_win32_info = {
     .cleanup = tap_cleanup,
 };
 
-static int tap_win32_init(VLANState *vlan, const char *model,
+static int tap_win32_init(VLANClientState *peer, const char *model,
                           const char *name, const char *ifname)
 {
     VLANClientState *nc;
@@ -685,7 +685,7 @@ static int tap_win32_init(VLANState *vlan, const char 
*model,
         return -1;
     }
 
-    nc = qemu_new_net_client(&net_tap_win32_info, vlan, NULL, model, name);
+    nc = qemu_new_net_client(&net_tap_win32_info, NULL, peer, model, name);
 
     s = DO_UPCAST(TAPState, nc, nc);
 
@@ -699,7 +699,7 @@ static int tap_win32_init(VLANState *vlan, const char 
*model,
     return 0;
 }
 
-int net_init_tap(QemuOpts *opts, const char *name, VLANState *vlan)
+int net_init_tap(QemuOpts *opts, const char *name, VLANClientState *peer)
 {
     const char *ifname;
 
@@ -710,7 +710,7 @@ int net_init_tap(QemuOpts *opts, const char *name, 
VLANState *vlan)
         return -1;
     }
 
-    if (tap_win32_init(vlan, "tap", name, ifname) == -1) {
+    if (tap_win32_init(peer, "tap", name, ifname) == -1) {
         return -1;
     }
 
diff --git a/net/tap.c b/net/tap.c
index 5ac4ba3..4a611e8 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -321,7 +321,7 @@ static NetClientInfo net_tap_info = {
     .cleanup = tap_cleanup,
 };
 
-static TAPState *net_tap_fd_init(VLANState *vlan,
+static TAPState *net_tap_fd_init(VLANClientState *peer,
                                  const char *model,
                                  const char *name,
                                  int fd,
@@ -330,7 +330,7 @@ static TAPState *net_tap_fd_init(VLANState *vlan,
     VLANClientState *nc;
     TAPState *s;
 
-    nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
+    nc = qemu_new_net_client(&net_tap_info, NULL, peer, model, name);
 
     s = DO_UPCAST(TAPState, nc, nc);
 
@@ -582,7 +582,7 @@ static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
     return fd;
 }
 
-int net_init_tap(QemuOpts *opts, const char *name, VLANState *vlan)
+int net_init_tap(QemuOpts *opts, const char *name, VLANClientState *peer)
 {
     TAPState *s;
     int fd, vnet_hdr = 0;
diff --git a/net/tap.h b/net/tap.h
index b2a9450..d8bbc39 100644
--- a/net/tap.h
+++ b/net/tap.h
@@ -32,7 +32,7 @@
 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
 
-int net_init_tap(QemuOpts *opts, const char *name, VLANState *vlan);
+int net_init_tap(QemuOpts *opts, const char *name, VLANClientState *peer);
 
 int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int 
vnet_hdr_required);
 
diff --git a/net/vde.c b/net/vde.c
index 6b9d452..51fb167 100644
--- a/net/vde.c
+++ b/net/vde.c
@@ -75,7 +75,7 @@ static NetClientInfo net_vde_info = {
     .cleanup = vde_cleanup,
 };
 
-static int net_vde_init(VLANState *vlan, const char *model,
+static int net_vde_init(VLANClientState *peer, const char *model,
                         const char *name, const char *sock,
                         int port, const char *group, int mode)
 {
@@ -96,7 +96,7 @@ static int net_vde_init(VLANState *vlan, const char *model,
         return -1;
     }
 
-    nc = qemu_new_net_client(&net_vde_info, vlan, NULL, model, name);
+    nc = qemu_new_net_client(&net_vde_info, NULL, peer, model, name);
 
     snprintf(nc->info_str, sizeof(nc->info_str), "sock=%s,fd=%d",
              sock, vde_datafd(vde));
@@ -110,7 +110,7 @@ static int net_vde_init(VLANState *vlan, const char *model,
     return 0;
 }
 
-int net_init_vde(QemuOpts *opts, const char *name, VLANState *vlan)
+int net_init_vde(QemuOpts *opts, const char *name, VLANClientState *peer)
 {
     const char *sock;
     const char *group;
@@ -122,7 +122,7 @@ int net_init_vde(QemuOpts *opts, const char *name, 
VLANState *vlan)
     port = qemu_opt_get_number(opts, "port", 0);
     mode = qemu_opt_get_number(opts, "mode", 0700);
 
-    if (net_vde_init(vlan, "vde", name, sock, port, group, mode) == -1) {
+    if (net_vde_init(peer, "vde", name, sock, port, group, mode) == -1) {
         return -1;
     }
 
diff --git a/net/vde.h b/net/vde.h
index 732e575..5e8ae58 100644
--- a/net/vde.h
+++ b/net/vde.h
@@ -29,7 +29,7 @@
 
 #ifdef CONFIG_VDE
 
-int net_init_vde(QemuOpts *opts, const char *name, VLANState *vlan);
+int net_init_vde(QemuOpts *opts, const char *name, VLANClientState *peer);
 
 #endif /* CONFIG_VDE */
 
-- 
1.7.6




reply via email to

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