From: Stefan Hajnoczi <stefa...@linux.vnet.ibm.com>

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 <stefa...@linux.vnet.ibm.com>
Signed-off-by: Zhi Yong Wu <wu...@linux.vnet.ibm.com>
---
 net.c           |   46 +++++++++++++++++++++++++---------------------
 net/dump.c      |   20 ++++++++++++++------
 net/dump.h      |    2 +-
 net/slirp.c     |    8 ++++----
 net/slirp.h     |    2 +-
 net/socket.c    |   48 ++++++++++++++++++++++++++----------------------
 net/socket.h    |    2 +-
 net/tap-win32.c |    9 +++++----
 net/tap.c       |    7 ++++---
 net/tap.h       |    3 ++-
 net/vde.c       |    9 +++++----
 net/vde.h       |    3 ++-
 12 files changed, 90 insertions(+), 69 deletions(-)

diff --git a/net.c b/net.c
index 1922d8a..8c02559 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++;
         }
     }
@@ -748,7 +751,7 @@ int net_handle_fd_param(Monitor *mon, const char *param)
 static int net_init_nic(QemuOpts *opts,
                         Monitor *mon,
                         const char *name,
-                        VLANState *vlan)
+                        VLANClientState *peer)
 {
     int idx;
     NICInfo *nd;
@@ -771,8 +774,8 @@ static int net_init_nic(QemuOpts *opts,
             return -1;
         }
     } else {
-        assert(vlan);
-        nd->vlan = vlan;
+        assert(peer);
+        nd->netdev = peer;
     }
     if (name) {
         nd->name = g_strdup(name);
@@ -820,17 +823,17 @@ static int net_init_nic(QemuOpts *opts,
         .help = "identifier for monitor commands", \
      }
 
-typedef int (*net_client_init_func)(QemuOpts *opts,
-                                    Monitor *mon,
-                                    const char *name,
-                                    VLANState *vlan);
+typedef int NetClientInitFunc(QemuOpts *opts,
+                              Monitor *mon,
+                              const char *name,
+                              VLANClientState *peer);
 
 /* magic number, but compiler will warn if too small */
 #define NET_MAX_DESC 20
 
 static const struct {
     const char *type;
-    net_client_init_func init;
+    NetClientInitFunc *init;
     QemuOptDesc desc[NET_MAX_DESC];
 } net_client_types[NET_CLIENT_TYPE_MAX] = {
     [NET_CLIENT_TYPE_NONE] = {
@@ -1136,7 +1139,7 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int 
is_netdev)
     for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
         if (net_client_types[i].type != NULL &&
             !strcmp(net_client_types[i].type, type)) {
-            VLANState *vlan = NULL;
+            VLANClientState *peer = NULL;
             int ret;
 
             if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
@@ -1147,12 +1150,12 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int 
is_netdev)
              * 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, mon, name, vlan);
+                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);
@@ -1297,6 +1300,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 4b48d48..37cec3c 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,28 @@ static int net_dump_init(VLANState *vlan, const char 
*device,
     return 0;
 }
 
-int net_init_dump(QemuOpts *opts, Monitor *mon, const char *name, VLANState 
*vlan)
+int net_init_dump(QemuOpts *opts, Monitor *mon, 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 fdc91ad..408c652 100644
--- a/net/dump.h
+++ b/net/dump.h
@@ -28,6 +28,6 @@
 #include "qemu-common.h"
 
 int net_init_dump(QemuOpts *opts, Monitor *mon,
-                  const char *name, VLANState *vlan);
+                  const char *name, VLANClientState *peer);
 
 #endif /* QEMU_NET_DUMP_H */
diff --git a/net/slirp.c b/net/slirp.c
index 96f5032..fa7c7fc 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),
@@ -679,7 +679,7 @@ static int net_init_slirp_configs(const char *name, const 
char *value, void *opa
 int net_init_slirp(QemuOpts *opts,
                    Monitor *mon,
                    const char *name,
-                   VLANState *vlan)
+                   VLANClientState *peer)
 {
     struct slirp_config_str *config;
     const char *vhost;
@@ -736,7 +736,7 @@ int net_init_slirp(QemuOpts *opts,
 
     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 c17de8e..32041cb 100644
--- a/net/slirp.h
+++ b/net/slirp.h
@@ -33,7 +33,7 @@
 int net_init_slirp(QemuOpts *opts,
                    Monitor *mon,
                    const char *name,
-                   VLANState *vlan);
+                   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 0bcf229..ed28cbd 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -43,7 +43,7 @@ typedef struct NetSocketState {
 } NetSocketState;
 
 typedef struct NetSocketListenState {
-    VLANState *vlan;
+    VLANClientState *peer;
     char *model;
     char *name;
     int fd;
@@ -244,7 +244,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)
@@ -286,7 +286,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)",
@@ -322,7 +322,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)
@@ -330,7 +330,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);
 
@@ -346,7 +346,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)
 {
@@ -361,13 +361,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;
 }
@@ -389,15 +389,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)
@@ -437,7 +439,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;
@@ -445,7 +447,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)
@@ -486,7 +488,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),
@@ -495,7 +497,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,
@@ -521,7 +523,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;
 
@@ -588,7 +590,7 @@ static int net_socket_udp_init(VLANState *vlan,
 int net_init_socket(QemuOpts *opts,
                     Monitor *mon,
                     const char *name,
-                    VLANState *vlan)
+                    VLANClientState *peer)
 {
     if (qemu_opt_get(opts, "fd")) {
         int fd;
@@ -606,7 +608,8 @@ int net_init_socket(QemuOpts *opts,
             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")) {
@@ -622,7 +625,7 @@ int net_init_socket(QemuOpts *opts,
 
         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")) {
@@ -638,7 +641,7 @@ int net_init_socket(QemuOpts *opts,
 
         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")) {
@@ -654,7 +657,8 @@ int net_init_socket(QemuOpts *opts,
         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 ea46f02..f6172bc 100644
--- a/net/socket.h
+++ b/net/socket.h
@@ -28,6 +28,6 @@
 #include "qemu-common.h"
 
 int net_init_socket(QemuOpts *opts, Monitor *mon,
-                    const char *name, VLANState *vlan);
+                    const char *name, VLANClientState *peer);
 
 #endif /* QEMU_NET_SOCKET_H */
diff --git a/net/tap-win32.c b/net/tap-win32.c
index 596132e..2436c26 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,8 @@ static int tap_win32_init(VLANState *vlan, const char 
*model,
     return 0;
 }
 
-int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState 
*vlan)
+int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name,
+                 VLANClientState *peer)
 {
     const char *ifname;
 
@@ -710,7 +711,7 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, 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 f240028..8855c57 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);
 
@@ -583,7 +583,8 @@ static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
     return fd;
 }
 
-int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState 
*vlan)
+int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name,
+                 VLANClientState *peer)
 {
     TAPState *s;
     int fd, vnet_hdr = 0;
diff --git a/net/tap.h b/net/tap.h
index 56c591f..c194684 100644
--- a/net/tap.h
+++ b/net/tap.h
@@ -32,7 +32,8 @@
 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
 
-int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState 
*vlan);
+int net_init_tap(QemuOpts *opts, Monitor *mon, 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 ac48ab2..d998daf 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,8 @@ static int net_vde_init(VLANState *vlan, const char *model,
     return 0;
 }
 
-int net_init_vde(QemuOpts *opts, Monitor *mon, const char *name, VLANState 
*vlan)
+int net_init_vde(QemuOpts *opts, Monitor *mon, const char *name,
+                 VLANClientState *peer)
 {
     const char *sock;
     const char *group;
@@ -122,7 +123,7 @@ int net_init_vde(QemuOpts *opts, Monitor *mon, 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 3e6ca3e..69c4e92 100644
--- a/net/vde.h
+++ b/net/vde.h
@@ -29,7 +29,8 @@
 
 #ifdef CONFIG_VDE
 
-int net_init_vde(QemuOpts *opts, Monitor *mon, const char *name, VLANState 
*vlan);
+int net_init_vde(QemuOpts *opts, Monitor *mon, const char *name,
+                 VLANClientState *peer);
 
 #endif /* CONFIG_VDE */
 
-- 
1.7.6


Reply via email to