From patchwork Wed Apr 17 08:39:17 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 237177 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 32D472C0148 for ; Wed, 17 Apr 2013 19:27:02 +1000 (EST) Received: from localhost ([::1]:39931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNxL-0003Iv-K7 for incoming@patchwork.ozlabs.org; Wed, 17 Apr 2013 04:43:07 -0400 Received: from eggs.gnu.org ([208.118.235.92]:50655) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNvJ-0000ji-CJ for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:41:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USNvI-0003RB-0N for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:41:01 -0400 Received: from mail-gh0-f180.google.com ([209.85.160.180]:33486) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNvH-0003R2-R6 for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:40:59 -0400 Received: by mail-gh0-f180.google.com with SMTP id f13so126445ghb.11 for ; Wed, 17 Apr 2013 01:40:59 -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=u3n+t8oAU228Pzjo739JEqCH/iCzKBjcvuH6V8bhBuM=; b=SeOLrTa8HuUu79Q/vm8jky8z15ANxq0rw216VThurMdwjOzxhGvmPBHUXeRL4AXUQ9 tBe397kQwWyAzDNB8SzeHZ0F4UxWZCU4If37gOcSAofzYf77yhZsvnrtWadCNHdJXguw RG1XJsAzEHK29EYVCavd0Bn+tdM7fP+4Vf8i0mXzhYeNVYNm3phRSNQ5hUu+ua8TW3lJ jkHt6huNRo7gfIJth7Yp9czzbY94ZWmGXDArV0jzyhOZbVerHAyOFdw8Kq9hf1sq7Tv+ 7+2Oa6btYwDqP0L293np0WjByD7LwvFo4w3rEDeflAbqw66DRC9cpnjhGLzo8yYuwEX6 I4bQ== X-Received: by 10.236.52.97 with SMTP id d61mr3278560yhc.23.1366188059478; Wed, 17 Apr 2013 01:40:59 -0700 (PDT) Received: from localhost ([114.245.251.229]) by mx.google.com with ESMTPS id h68sm7990428yhj.24.2013.04.17.01.40.54 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Wed, 17 Apr 2013 01:40:58 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 17 Apr 2013 16:39:17 +0800 Message-Id: <1366187964-14265-9-git-send-email-qemulist@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1366187964-14265-1-git-send-email-qemulist@gmail.com> References: <1366187964-14265-1-git-send-email-qemulist@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.160.180 Cc: mdroth , Paolo Bonzini , Stefan Hajnoczi , Anthony Liguori , Jan Kiszka Subject: [Qemu-devel] [RFC PATCH v4 08/15] net: hub use lock to protect ports list 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 Hub ports will run on multi-threads, so use lock to protect them. Signed-off-by: Liu Ping Fan --- net/hub.c | 25 ++++++++++++++++++++++++- 1 files changed, 24 insertions(+), 1 deletions(-) diff --git a/net/hub.c b/net/hub.c index df32074..812a6dc 100644 --- a/net/hub.c +++ b/net/hub.c @@ -37,6 +37,7 @@ struct NetHub { int id; QLIST_ENTRY(NetHub) next; int num_ports; + QemuMutex ports_lock; QLIST_HEAD(, NetHubPort) ports; }; @@ -47,6 +48,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port, { NetHubPort *port; + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &hub->ports, next) { if (port == source_port) { continue; @@ -54,6 +56,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port, qemu_send_packet(&port->nc, buf, len); } + qemu_mutex_unlock(&hub->ports_lock); return len; } @@ -63,6 +66,7 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port, NetHubPort *port; ssize_t len = iov_size(iov, iovcnt); + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &hub->ports, next) { if (port == source_port) { continue; @@ -70,6 +74,7 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port, qemu_sendv_packet(&port->nc, iov, iovcnt); } + qemu_mutex_unlock(&hub->ports_lock); return len; } @@ -80,6 +85,7 @@ static NetHub *net_hub_new(int id) hub = g_malloc(sizeof(*hub)); hub->id = id; hub->num_ports = 0; + qemu_mutex_init(&hub->ports_lock); QLIST_INIT(&hub->ports); QLIST_INSERT_HEAD(&hubs, hub, next); @@ -93,16 +99,19 @@ static int net_hub_port_can_receive(NetClientState *nc) NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc); NetHub *hub = src_port->hub; + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &hub->ports, next) { if (port == src_port) { continue; } if (qemu_can_send_packet(&port->nc)) { + qemu_mutex_unlock(&hub->ports_lock); return 1; } } + qemu_mutex_unlock(&hub->ports_lock); return 0; } @@ -155,8 +164,9 @@ static NetHubPort *net_hub_port_new(NetHub *hub, const char *name) port = DO_UPCAST(NetHubPort, nc, nc); port->id = id; port->hub = hub; - + qemu_mutex_lock(&hub->ports_lock); QLIST_INSERT_HEAD(&hub->ports, port, next); + qemu_mutex_unlock(&hub->ports_lock); return port; } @@ -197,13 +207,16 @@ NetClientState *net_hub_find_client_by_name(int hub_id, const char *name) QLIST_FOREACH(hub, &hubs, next) { if (hub->id == hub_id) { + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &hub->ports, next) { peer = port->nc.peer; if (peer && strcmp(peer->name, name) == 0) { + qemu_mutex_unlock(&hub->ports_lock); return peer; } } + qemu_mutex_unlock(&hub->ports_lock); } } return NULL; @@ -220,12 +233,15 @@ NetClientState *net_hub_port_find(int hub_id) QLIST_FOREACH(hub, &hubs, next) { if (hub->id == hub_id) { + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &hub->ports, next) { nc = port->nc.peer; if (!nc) { + qemu_mutex_unlock(&hub->ports_lock); return &(port->nc); } } + qemu_mutex_unlock(&hub->ports_lock); break; } } @@ -244,12 +260,14 @@ void net_hub_info(Monitor *mon) QLIST_FOREACH(hub, &hubs, next) { monitor_printf(mon, "hub %d\n", hub->id); + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &hub->ports, next) { if (port->nc.peer) { monitor_printf(mon, " \\ "); print_net_client(mon, port->nc.peer); } } + qemu_mutex_unlock(&hub->ports_lock); } } @@ -306,6 +324,7 @@ void net_hub_check_clients(void) QLIST_FOREACH(hub, &hubs, next) { int has_nic = 0, has_host_dev = 0; + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &hub->ports, next) { peer = port->nc.peer; if (!peer) { @@ -328,6 +347,7 @@ void net_hub_check_clients(void) break; } } + qemu_mutex_unlock(&hub->ports_lock); if (has_host_dev && !has_nic) { fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id); } @@ -343,12 +363,15 @@ bool net_hub_flush(NetClientState *nc) { NetHubPort *port; NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc); + NetHub *hub = source_port->hub; int ret = 0; + qemu_mutex_lock(&hub->ports_lock); QLIST_FOREACH(port, &source_port->hub->ports, next) { if (port != source_port) { ret += qemu_net_queue_flush(port->nc.send_queue); } } + qemu_mutex_unlock(&hub->ports_lock); return ret ? true : false; }