diff mbox

[v1,01/12] net: Add a hub net client

Message ID 1331283625-26439-2-git-send-email-zwu.kernel@gmail.com
State New
Headers show

Commit Message

Zhiyong Wu March 9, 2012, 9 a.m. UTC
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     |  204 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/hub.h     |   23 +++++++
 4 files changed, 229 insertions(+), 1 deletions(-)
 create mode 100644 net/hub.c
 create mode 100644 net/hub.h

Comments

Paolo Bonzini March 9, 2012, 10:33 a.m. UTC | #1
Il 09/03/2012 10:00, zwu.kernel@gmail.com ha scritto:
> +        /* TODO use qemu_send_packet() or need to call *_deliver_* directly? */
> +        /* TODO ignore return value? */
> +        qemu_send_packet(&port->nc, buf, len);

Did you see my message at

http://permalink.gmane.org/gmane.comp.emulators.qemu/134998

?

Paolo
Zhiyong Wu March 12, 2012, 8:59 a.m. UTC | #2
On Fri, Mar 9, 2012 at 6:33 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> Il 09/03/2012 10:00, zwu.kernel@gmail.com ha scritto:
>> +        /* TODO use qemu_send_packet() or need to call *_deliver_* directly? */
>> +        /* TODO ignore return value? */
>> +        qemu_send_packet(&port->nc, buf, len);
>
> Did you see my message at
>
> http://permalink.gmane.org/gmane.comp.emulators.qemu/134998
Yeah, i ever saw this, but read it just now. In your comments,
there're some places where i have not completely understand.

> This means VLANs will wait for all receivers to be ready and then do N-1
> synchronous sends.  Your code will queue N-1 asynchronous sends.

> However, then I noticed that qemu_can_send_packet is not called very
> much, and I do not understand why qemu_net_queue_send and
> qemu_net_queue_send_iov do not call qemu_can_send_packet before calling
> deliver/deliver_iov.

> If they did, hubs could then do their own flow control via can_receive.
>  When qemu_send_packet returns zero you increment a count of in-flight
> packets, and a sent-packet callback would decrement the same count.
sent-packet callback is sent_cb here? i noticed that sent_cb is currently NULL.
> When the count is non-zero, can_receive returns false (and vice versa).
>Can you elaborate this? since can_receive is called finally by qemu_send_packet, how can can_receive return false or true based on the count?
 > The sent_cb also needs to call qemu_flush_queued_packets when the
> count drop to zero.
When sent_cb is called, it means that the packet has been sent out, right?

> With this in place, I think the other TODO about the return value is
> easily solved; receive/receive_iov callbacks can simply return immediate
> success, and later block further sends.

> Due to the separate per-port send queues, when many sends are blocking
> many ports might linearize the same packet multiple times in
> qemu_net_queue_append_iov.  The limit however is packet size * number of
I think that when one packet need to be enqueued, we should at first
determine if it has existed in send queue.
> ports, which is not more than a few KB; it's more of a performance
> problem and VLANs/hubs should only be used when performance doesn't
> matter (as with the dump client).

> BTW, an additional cleanup that you can do on top is to remove the
> deliver/deliver_iov function pointers in net/queue.c and
> qemu_new_net_queue, because they will always be qemu_deliver_packet and
> qemu_deliver_packet_iov.

>
> ?
>
> Paolo
Paolo Bonzini March 12, 2012, 9:12 a.m. UTC | #3
Il 12/03/2012 09:59, Zhi Yong Wu ha scritto:
>> However, then I noticed that qemu_can_send_packet is not called very
>> much, and I do not understand why qemu_net_queue_send and
>> qemu_net_queue_send_iov do not call qemu_can_send_packet before calling
>> deliver/deliver_iov.
> 
>> If they did, hubs could then do their own flow control via can_receive.
>> When qemu_send_packet returns zero you increment a count of in-flight
>> packets, and a sent-packet callback would decrement the same count.
>
> sent-packet callback is sent_cb here? i noticed that sent_cb is currently NULL.

Yes.

>> When the count is non-zero, can_receive returns false (and vice versa).
>> Can you elaborate this? since can_receive is called finally by qemu_send_packet,
>> how can can_receive return false or true based on the count?

Based on counts from the previous sends.

Paolo
Zhiyong Wu March 12, 2012, 12:13 p.m. UTC | #4
On Mon, Mar 12, 2012 at 5:12 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> Il 12/03/2012 09:59, Zhi Yong Wu ha scritto:
>>> However, then I noticed that qemu_can_send_packet is not called very
>>> much, and I do not understand why qemu_net_queue_send and
>>> qemu_net_queue_send_iov do not call qemu_can_send_packet before calling
>>> deliver/deliver_iov.
>>
>>> If they did, hubs could then do their own flow control via can_receive.
>>> When qemu_send_packet returns zero you increment a count of in-flight
>>> packets, and a sent-packet callback would decrement the same count.
>>
>> sent-packet callback is sent_cb here? i noticed that sent_cb is currently NULL.
>
> Yes.
>
>>> When the count is non-zero, can_receive returns false (and vice versa).
>>> Can you elaborate this? since can_receive is called finally by qemu_send_packet,
>>> how can can_receive return false or true based on the count?
>
> Based on counts from the previous sends.
thanks.

I agree with your opition, but hope to get stefan's comments.
>
> Paolo
diff mbox

Patch

diff --git a/Makefile.objs b/Makefile.objs
index 808de6a..fc25979 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -45,7 +45,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 75a8c15..319c3bb 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..b3ac2ef
--- /dev/null
+++ b/net/hub.c
@@ -0,0 +1,204 @@ 
+/*
+ * 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;
+        }
+
+        /* TODO use qemu_send_packet() or need to call *_deliver_* directly? */
+        /* TODO ignore return value? */
+        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;
+        }
+
+        /* TODO use qemu_send_packet() or need to call *_deliver_* directly? */
+        /* TODO return value */
+        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 */