From patchwork Wed Mar 13 05:59:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 227159 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BEF662C0092 for ; Wed, 13 Mar 2013 17:01:06 +1100 (EST) Received: from localhost ([::1]:39890 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UFekL-00034E-1G for incoming@patchwork.ozlabs.org; Wed, 13 Mar 2013 02:01:05 -0400 Received: from eggs.gnu.org ([208.118.235.92]:41616) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UFej1-0001ls-3K for qemu-devel@nongnu.org; Wed, 13 Mar 2013 01:59:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UFeix-0000jW-7T for qemu-devel@nongnu.org; Wed, 13 Mar 2013 01:59:43 -0400 Received: from mail-ia0-x22f.google.com ([2607:f8b0:4001:c02::22f]:36435) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UFeix-0000jO-1A for qemu-devel@nongnu.org; Wed, 13 Mar 2013 01:59:39 -0400 Received: by mail-ia0-f175.google.com with SMTP id o25so569561iad.6 for ; Tue, 12 Mar 2013 22:59:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=E3Plmxjk1pnR4spat08f7o713YiTfpuCxquf2ALdvBI=; b=zZRwSEVWxyI0I58ODIN49jX32Pw6F7FIZlEBe2UOamsJl1eaEDVjk19D81vA74dh3C eF5I/mHf6x7GHpeW9x3F4ZbtdI0M4VEJV6yHpaZEkRmwnl/nJJkIYnUU87z4z2WxxyOp lbX4wWP+HxecHgMj80Sf06vj8zWiUfuHPHFuhh0Cn5NoXJfg8IN/bNQCw8/pNMkMpjR4 eCpPc6L89ZcimZwmasWALSzG6EoQJY5e4zAfntYanDzf1tLoO/zUCpYEw2Co3pp+CFhe N8do1+P/C/GzTV5jyBC0uThRYuTpfQb4PnqDSYq1FdLjpRPjTs5qQ8U49nMvSD2uByFH ORQA== X-Received: by 10.50.12.133 with SMTP id y5mr14914417igb.108.1363154378448; Tue, 12 Mar 2013 22:59:38 -0700 (PDT) Received: from localhost ([222.128.144.239]) by mx.google.com with ESMTPS id ip2sm1115109igc.5.2013.03.12.22.59.33 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Tue, 12 Mar 2013 22:59:37 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 13 Mar 2013 13:59:06 +0800 Message-Id: <1363154346-14827-3-git-send-email-qemulist@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1363154346-14827-1-git-send-email-qemulist@gmail.com> References: <1363154346-14827-1-git-send-email-qemulist@gmail.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4001:c02::22f Cc: Stefan Hajnoczi , Paolo Bonzini , mdroth , Anthony Liguori , "Michael S. Tsirkin" Subject: [Qemu-devel] [RFC PATCH 2/2] net: port hub onto glib X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Liu Ping Fan Attach each hubport with a GSource. Currently the GSource is attached to default main context, and the hub still run on iothread, but in future, after making the whole layer thread-safe, we will admit ports to run on different thread(GMainContext). Signed-off-by: Liu Ping Fan --- hw/qdev-properties-system.c | 1 + include/net/net.h | 7 +++++-- include/net/queue.h | 14 ++++++++++++++ net/hub.c | 34 ++++++++++++++++++++++++++++++++-- net/net.c | 1 + net/queue.c | 4 ++-- 6 files changed, 55 insertions(+), 6 deletions(-) diff --git a/hw/qdev-properties-system.c b/hw/qdev-properties-system.c index ce3af22..bb2448d 100644 --- a/hw/qdev-properties-system.c +++ b/hw/qdev-properties-system.c @@ -307,6 +307,7 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque, name, prop->info->name); return; } + hubport->info->bind_ctx(hubport, NULL); *ptr = hubport; } diff --git a/include/net/net.h b/include/net/net.h index 4f3eed1..3f01711 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -65,10 +65,13 @@ typedef struct NetClientSource { GSource source; /* fix me, to expand as array in future */ GPollFD gfd; - bool last_rd_sts; - bool last_wr_sts; + Pollable readable; Pollable writeable; + /* status returned by pollable last time */ + bool last_rd_sts; + bool last_wr_sts; + void *opaque; } NetClientSource; diff --git a/include/net/queue.h b/include/net/queue.h index fc02b33..f60e57f 100644 --- a/include/net/queue.h +++ b/include/net/queue.h @@ -38,6 +38,20 @@ NetQueue *qemu_new_net_queue(void *opaque); void qemu_del_net_queue(NetQueue *queue); +void qemu_net_queue_append(NetQueue *queue, + NetClientState *sender, + unsigned flags, + const uint8_t *buf, + size_t size, + NetPacketSent *sent_cb); + +void qemu_net_queue_append_iov(NetQueue *queue, + NetClientState *sender, + unsigned flags, + const struct iovec *iov, + int iovcnt, + NetPacketSent *sent_cb); + ssize_t qemu_net_queue_send(NetQueue *queue, NetClientState *sender, unsigned flags, diff --git a/net/hub.c b/net/hub.c index df32074..ab47b1b 100644 --- a/net/hub.c +++ b/net/hub.c @@ -31,6 +31,7 @@ typedef struct NetHubPort { QLIST_ENTRY(NetHubPort) next; NetHub *hub; int id; + EventNotifier e; } NetHubPort; struct NetHub { @@ -52,7 +53,9 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port, continue; } - qemu_send_packet(&port->nc, buf, len); + qemu_net_queue_append(port->nc.peer->send_queue, &port->nc, + QEMU_NET_PACKET_FLAG_NONE, buf, len, NULL); + event_notifier_set(&port->e); } return len; } @@ -68,7 +71,9 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port, continue; } - qemu_sendv_packet(&port->nc, iov, iovcnt); + qemu_net_queue_append_iov(port->nc.peer->send_queue, &port->nc, + QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, NULL); + event_notifier_set(&port->e); } return len; } @@ -129,6 +134,11 @@ static void net_hub_port_cleanup(NetClientState *nc) QLIST_REMOVE(port, next); } +static void net_hub_port_bind(NetClientState *nc, GMainContext *ctx) +{ + g_source_attach(&nc->nsrc->source, ctx); +} + static NetClientInfo net_hub_port_info = { .type = NET_CLIENT_OPTIONS_KIND_HUBPORT, .size = sizeof(NetHubPort), @@ -136,14 +146,29 @@ static NetClientInfo net_hub_port_info = { .receive = net_hub_port_receive, .receive_iov = net_hub_port_receive_iov, .cleanup = net_hub_port_cleanup, + .bind_ctx = net_hub_port_bind, }; +static gboolean hub_port_handler(gpointer data) +{ + NetClientSource *nsrc = (NetClientSource *)data; + NetHubPort *port = (NetHubPort *)nsrc->opaque; + + if (nsrc->gfd.revents & G_IO_IN) { + event_notifier_test_and_clear(&port->e); + qemu_net_queue_flush(port->nc.peer->send_queue); + return true; + } + return false; +} + static NetHubPort *net_hub_port_new(NetHub *hub, const char *name) { NetClientState *nc; NetHubPort *port; int id = hub->num_ports++; char default_name[128]; + NetClientSource *nsrc; if (!name) { snprintf(default_name, sizeof(default_name), @@ -155,6 +180,11 @@ static NetHubPort *net_hub_port_new(NetHub *hub, const char *name) port = DO_UPCAST(NetHubPort, nc, nc); port->id = id; port->hub = hub; + event_notifier_init(&port->e, 0); + nsrc = (NetClientSource *)g_source_new(&net_gsource_funcs, + sizeof(NetClientSource)); + net_init_gsource(nc, nsrc, event_notifier_get_fd(&port->e), G_IO_IN, + NULL, NULL, hub_port_handler, port); QLIST_INSERT_HEAD(&hub->ports, port, next); diff --git a/net/net.c b/net/net.c index fd5269f..646cad8 100644 --- a/net/net.c +++ b/net/net.c @@ -871,6 +871,7 @@ static int net_client_init1(const void *object, int is_netdev, Error **errp) (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC || !opts->nic->has_netdev)) { peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL); + peer->info->bind_ctx(peer, NULL); } if (net_client_init_fun[opts->kind](opts, name, peer) < 0) { diff --git a/net/queue.c b/net/queue.c index 859d02a..67959f8 100644 --- a/net/queue.c +++ b/net/queue.c @@ -87,7 +87,7 @@ void qemu_del_net_queue(NetQueue *queue) g_free(queue); } -static void qemu_net_queue_append(NetQueue *queue, +void qemu_net_queue_append(NetQueue *queue, NetClientState *sender, unsigned flags, const uint8_t *buf, @@ -110,7 +110,7 @@ static void qemu_net_queue_append(NetQueue *queue, QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); } -static void qemu_net_queue_append_iov(NetQueue *queue, +void qemu_net_queue_append_iov(NetQueue *queue, NetClientState *sender, unsigned flags, const struct iovec *iov,