qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: zwu.kernel@gmail.com
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, wuzhy@linux.vnet.ibm.com,
	stefanha@linux.vnet.ibm.com, kvm@vger.kernel.org,
	jan.kiszka@siemens.com
Subject: [Qemu-devel] [PATCH v3 01/16] net: Add a hub net client
Date: Fri, 25 May 2012 01:59:07 +0800	[thread overview]
Message-ID: <1337882362-20100-2-git-send-email-zwu.kernel@gmail.com> (raw)
In-Reply-To: <1337882362-20100-1-git-send-email-zwu.kernel@gmail.com>

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

The vlan feature can be implemented in terms of hubs.  By introducing a
hub net client it becomes possible to remove the special case vlan code
from net.c and push the vlan feature out of generic networking code.

Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Signed-off-by: Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
---
 Makefile.objs |    2 +-
 net.h         |    1 +
 net/hub.c     |  200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/hub.h     |   23 +++++++
 4 files changed, 225 insertions(+), 1 deletions(-)
 create mode 100644 net/hub.c
 create mode 100644 net/hub.h

diff --git a/Makefile.objs b/Makefile.objs
index 70c5c79..a3a3a8a 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -63,7 +63,7 @@ block-nested-$(CONFIG_RBD) += rbd.o
 block-obj-y +=  $(addprefix block/, $(block-nested-y))
 
 net-obj-y = net.o
-net-nested-y = queue.o checksum.o util.o
+net-nested-y = queue.o checksum.o util.o hub.o
 net-nested-y += socket.o
 net-nested-y += dump.o
 net-nested-$(CONFIG_POSIX) += tap.o
diff --git a/net.h b/net.h
index 64993b4..50c55ad 100644
--- a/net.h
+++ b/net.h
@@ -38,6 +38,7 @@ typedef enum {
     NET_CLIENT_TYPE_VDE,
     NET_CLIENT_TYPE_DUMP,
     NET_CLIENT_TYPE_BRIDGE,
+    NET_CLIENT_TYPE_HUB,
 
     NET_CLIENT_TYPE_MAX
 } net_client_type;
diff --git a/net/hub.c b/net/hub.c
new file mode 100644
index 0000000..0d3df7f
--- /dev/null
+++ b/net/hub.c
@@ -0,0 +1,200 @@
+/*
+ * Hub net client
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#include "monitor.h"
+#include "net.h"
+#include "hub.h"
+
+/*
+ * A hub broadcasts incoming packets to all its ports except the source port.
+ * Hubs can be used to provide independent network segments, also confusingly
+ * named the QEMU 'vlan' feature.
+ */
+
+typedef struct NetHub NetHub;
+
+typedef struct NetHubPort {
+    VLANClientState nc;
+    QLIST_ENTRY(NetHubPort) next;
+    NetHub *hub;
+    unsigned int id;
+} NetHubPort;
+
+struct NetHub {
+    unsigned int id;
+    QLIST_ENTRY(NetHub) next;
+    unsigned int num_ports;
+    QLIST_HEAD(, NetHubPort) ports;
+};
+
+static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs);
+
+static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
+                               const uint8_t *buf, size_t len)
+{
+    NetHubPort *port;
+
+    QLIST_FOREACH(port, &hub->ports, next) {
+        if (port == source_port) {
+            continue;
+        }
+
+        qemu_send_packet(&port->nc, buf, len);
+    }
+    return len;
+}
+
+static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
+                                   const struct iovec *iov, int iovcnt)
+{
+    NetHubPort *port;
+    ssize_t ret = 0;
+
+    QLIST_FOREACH(port, &hub->ports, next) {
+        if (port == source_port) {
+            continue;
+        }
+
+        ret = qemu_sendv_packet(&port->nc, iov, iovcnt);
+    }
+    return ret;
+}
+
+static NetHub *net_hub_new(unsigned int id)
+{
+    NetHub *hub;
+
+    hub = g_malloc(sizeof(*hub));
+    hub->id = id;
+    hub->num_ports = 0;
+    QLIST_INIT(&hub->ports);
+
+    QLIST_INSERT_HEAD(&hubs, hub, next);
+
+    return hub;
+}
+
+static ssize_t net_hub_port_receive(VLANClientState *nc,
+                                    const uint8_t *buf, size_t len)
+{
+    NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
+
+    return net_hub_receive(port->hub, port, buf, len);
+}
+
+static ssize_t net_hub_port_receive_iov(VLANClientState *nc,
+                                        const struct iovec *iov, int iovcnt)
+{
+    NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
+
+    return net_hub_receive_iov(port->hub, port, iov, iovcnt);
+}
+
+static void net_hub_port_cleanup(VLANClientState *nc)
+{
+    NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
+
+    QLIST_REMOVE(port, next);
+}
+
+static NetClientInfo net_hub_port_info = {
+    .type = NET_CLIENT_TYPE_HUB,
+    .size = sizeof(NetHubPort),
+    .receive = net_hub_port_receive,
+    .receive_iov = net_hub_port_receive_iov,
+    .cleanup = net_hub_port_cleanup,
+};
+
+static NetHubPort *net_hub_port_new(NetHub *hub)
+{
+    VLANClientState *nc;
+    NetHubPort *port;
+    unsigned int id = hub->num_ports++;
+    char name[128];
+
+    snprintf(name, sizeof name, "hub%uport%u", hub->id, id);
+
+    nc = qemu_new_net_client(&net_hub_port_info, NULL, NULL, "hub", name);
+    port = DO_UPCAST(NetHubPort, nc, nc);
+    port->id = id;
+    port->hub = hub;
+
+    QLIST_INSERT_HEAD(&hub->ports, port, next);
+
+    return port;
+}
+
+/**
+ * Create a port on a given hub
+ *
+ * If there is no existing hub with the given id then a new hub is created.
+ */
+VLANClientState *net_hub_add_port(unsigned int hub_id)
+{
+    NetHub *hub;
+    NetHubPort *port;
+
+    QLIST_FOREACH(hub, &hubs, next) {
+        if (hub->id == hub_id) {
+            break;
+        }
+    }
+
+    if (!hub) {
+        hub = net_hub_new(hub_id);
+    }
+
+    port = net_hub_port_new(hub);
+    return &port->nc;
+}
+
+/**
+ * Print hub configuration
+ */
+void net_hub_info(Monitor *mon)
+{
+    NetHub *hub;
+    NetHubPort *port;
+
+    QLIST_FOREACH(hub, &hubs, next) {
+        monitor_printf(mon, "hub %u\n", hub->id);
+        QLIST_FOREACH(port, &hub->ports, next) {
+            monitor_printf(mon, "    port %u peer %s\n", port->id,
+                           port->nc.peer ? port->nc.peer->name : "<none>");
+        }
+    }
+}
+
+/**
+ * Get the hub id that a client is connected to
+ *
+ * @id              Pointer for hub id output, may be NULL
+ */
+int net_hub_id_for_client(VLANClientState *nc, unsigned int *id)
+{
+    NetHub *hub;
+    NetHubPort *port;
+
+    QLIST_FOREACH(hub, &hubs, next) {
+        QLIST_FOREACH(port, &hub->ports, next) {
+            if (&port->nc == nc ||
+                (port->nc.peer && port->nc.peer == nc)) {
+                if (id) {
+                    *id = hub->id;
+                }
+                return 0;
+            }
+        }
+    }
+    return -ENOENT;
+}
diff --git a/net/hub.h b/net/hub.h
new file mode 100644
index 0000000..3ca05dc
--- /dev/null
+++ b/net/hub.h
@@ -0,0 +1,23 @@
+/*
+ * Hub net client
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef NET_HUB_H
+#define NET_HUB_H
+
+#include "qemu-common.h"
+
+VLANClientState *net_hub_add_port(unsigned int hub_id);
+void net_hub_info(Monitor *mon);
+int net_hub_id_for_client(VLANClientState *nc, unsigned int *id);
+
+#endif /* NET_HUB_H */
-- 
1.7.6

  reply	other threads:[~2012-05-24 17:59 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-24 17:59 [Qemu-devel] [PATCH v3 00/16] net: hub-based networking zwu.kernel
2012-05-24 17:59 ` zwu.kernel [this message]
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 02/16] net: Use hubs for the vlan feature zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 03/16] net: Look up 'vlan' net clients using hubs zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 04/16] hub: Check that hubs are configured correctly zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 05/16] net: Drop vlan argument to qemu_new_net_client() zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 06/16] net: Remove vlan qdev property zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 07/16] net: Remove vlan code from net.c zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 08/16] net: Remove VLANState zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 09/16] net: Rename non_vlan_clients to net_clients zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 10/16] net: Rename VLANClientState to NetClientState zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 11/16] net: Rename vc local variables to nc zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 12/16] net: Rename qemu_del_vlan_client() to qemu_del_net_client() zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 13/16] net: Make the monitor output more reasonable hub info zwu.kernel
2012-05-24 20:34   ` Jan Kiszka
2012-05-25  0:48     ` Zhi Yong Wu
2012-05-25 12:00     ` Zhi Yong Wu
2012-05-25 13:49       ` Jan Kiszka
2012-05-25 13:58         ` Zhi Yong Wu
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 14/16] net: cleanup deliver/deliver_iov func pointers zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 15/16] net: determine if packets can be sent before net queue deliver packets zwu.kernel
2012-05-24 17:59 ` [Qemu-devel] [PATCH v3 16/16] hub: add the support for hub own flow control zwu.kernel
2012-05-25  7:04   ` Paolo Bonzini
2012-05-25  7:48     ` Zhi Yong Wu
2012-05-25 10:08       ` Paolo Bonzini
2012-05-25 10:54         ` Zhi Yong Wu
2012-05-25  8:04     ` Zhi Yong Wu
2012-05-25  8:18     ` Zhi Yong Wu
2012-05-24 20:53 ` [Qemu-devel] [PATCH v3 00/16] net: hub-based networking Luiz Capitulino
2012-05-25  0:47   ` Zhi Yong Wu
2012-05-25 12:49     ` Luiz Capitulino
2012-05-25 10:07   ` Stefan Hajnoczi
2012-05-25 11:18     ` Markus Armbruster
2012-05-25 12:01       ` Stefan Hajnoczi
2012-05-25 12:30         ` Markus Armbruster
2012-05-25 12:53         ` Luiz Capitulino
2012-05-25 12:59           ` Paolo Bonzini
2012-05-25 13:07             ` Luiz Capitulino
2012-05-25 13:14               ` Paolo Bonzini
2012-05-25 13:18                 ` Luiz Capitulino
2012-05-25 13:19                   ` Paolo Bonzini
2012-05-25 13:30                     ` Luiz Capitulino
2012-05-25 13:37                       ` Paolo Bonzini
2012-05-25 13:43                         ` Luiz Capitulino
2012-05-25 13:47                           ` Paolo Bonzini
2012-05-25 13:56                             ` Luiz Capitulino
2012-05-28 11:17                               ` Stefan Hajnoczi
2012-05-28 13:25                                 ` Luiz Capitulino
     [not found]                                   ` <m3ehq3tne0.fsf@blackfin.pond.sub.org>
2012-06-04  4:48                                     ` Anthony Liguori
2012-06-04  7:24                                       ` Markus Armbruster
2012-06-04  4:56           ` Anthony Liguori
2012-06-04 13:09             ` Luiz Capitulino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1337882362-20100-2-git-send-email-zwu.kernel@gmail.com \
    --to=zwu.kernel@gmail.com \
    --cc=jan.kiszka@siemens.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@linux.vnet.ibm.com \
    --cc=wuzhy@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).