From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:53056) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDQyS-0003iU-8Z for qemu-devel@nongnu.org; Wed, 06 Mar 2013 21:54:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UDQyQ-0007O6-LK for qemu-devel@nongnu.org; Wed, 06 Mar 2013 21:54:28 -0500 Received: from mail-pa0-f47.google.com ([209.85.220.47]:46283) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UDQyQ-0007Nw-Bp for qemu-devel@nongnu.org; Wed, 06 Mar 2013 21:54:26 -0500 Received: by mail-pa0-f47.google.com with SMTP id bj3so73172pad.34 for ; Wed, 06 Mar 2013 18:54:25 -0800 (PST) From: Liu Ping Fan Date: Thu, 7 Mar 2013 10:53:17 +0800 Message-Id: <1362624800-10682-3-git-send-email-qemulist@gmail.com> In-Reply-To: <1362624800-10682-1-git-send-email-qemulist@gmail.com> References: <1362624800-10682-1-git-send-email-qemulist@gmail.com> Subject: [Qemu-devel] [PATCH v2 2/5] net: hub use lock to protect ports list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , "Michael S. Tsirkin" , mdroth , Anthony Liguori , Paolo Bonzini From: Liu Ping Fan Signed-off-by: Liu Ping Fan --- net/hub.c | 27 ++++++++++++++++++++++++++- 1 files changed, 26 insertions(+), 1 deletions(-) diff --git a/net/hub.c b/net/hub.c index 73c1f26..47fe72c 100644 --- a/net/hub.c +++ b/net/hub.c @@ -39,6 +39,8 @@ struct NetHub { int id; QLIST_ENTRY(NetHub) next; int num_ports; + /* protect ports */ + QemuMutex lock; QLIST_HEAD(, NetHubPort) ports; }; @@ -49,6 +51,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port, { NetHubPort *port; + qemu_mutex_lock(&hub->lock); QLIST_FOREACH(port, &hub->ports, next) { if (port == source_port) { continue; @@ -58,6 +61,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port, QEMU_NET_PACKET_FLAG_NONE, buf, len, NULL); event_notifier_set(&port->e); } + qemu_mutex_unlock(&hub->lock); return len; } @@ -74,6 +78,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->lock); QLIST_FOREACH(port, &hub->ports, next) { if (port == source_port) { continue; @@ -83,6 +88,7 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port, QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, NULL); event_notifier_set(&port->e); } + qemu_mutex_unlock(&hub->lock); return len; } @@ -93,6 +99,7 @@ static NetHub *net_hub_new(int id) hub = g_malloc(sizeof(*hub)); hub->id = id; hub->num_ports = 0; + qemu_mutex_init(&hub->lock); QLIST_INIT(&hub->ports); QLIST_INSERT_HEAD(&hubs, hub, next); @@ -106,16 +113,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->lock); QLIST_FOREACH(port, &hub->ports, next) { if (port == src_port) { continue; } if (qemu_can_send_packet(&port->nc)) { + qemu_mutex_unlock(&hub->lock); return 1; } } + qemu_mutex_unlock(&hub->lock); return 0; } @@ -183,7 +193,9 @@ static NetHubPort *net_hub_port_new(NetHub *hub, const char *name) port->id = id; port->hub = hub; event_notifier_init(&port->e, 0); + qemu_mutex_lock(&hub->lock); QLIST_INSERT_HEAD(&hub->ports, port, next); + qemu_mutex_unlock(&hub->lock); return port; } @@ -224,13 +236,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->lock); QLIST_FOREACH(port, &hub->ports, next) { peer = port->nc.peer; if (peer && strcmp(peer->name, name) == 0) { + qemu_mutex_unlock(&hub->lock); return peer; } } + qemu_mutex_unlock(&hub->lock); } } return NULL; @@ -247,12 +262,15 @@ NetClientState *net_hub_port_find(int hub_id) QLIST_FOREACH(hub, &hubs, next) { if (hub->id == hub_id) { + qemu_mutex_lock(&hub->lock); QLIST_FOREACH(port, &hub->ports, next) { nc = port->nc.peer; if (!nc) { + qemu_mutex_unlock(&hub->lock); return &(port->nc); } } + qemu_mutex_unlock(&hub->lock); break; } } @@ -271,12 +289,14 @@ void net_hub_info(Monitor *mon) QLIST_FOREACH(hub, &hubs, next) { monitor_printf(mon, "hub %d\n", hub->id); + qemu_mutex_lock(&hub->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->lock); } } @@ -333,6 +353,7 @@ void net_hub_check_clients(void) QLIST_FOREACH(hub, &hubs, next) { int has_nic = 0, has_host_dev = 0; + qemu_mutex_lock(&hub->lock); QLIST_FOREACH(port, &hub->ports, next) { peer = port->nc.peer; if (!peer) { @@ -355,6 +376,7 @@ void net_hub_check_clients(void) break; } } + qemu_mutex_unlock(&hub->lock); if (has_host_dev && !has_nic) { fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id); } @@ -370,12 +392,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; - QLIST_FOREACH(port, &source_port->hub->ports, next) { + qemu_mutex_lock(&hub->lock); + QLIST_FOREACH(port, &hub->ports, next) { if (port != source_port) { ret += qemu_net_queue_flush(port->nc.send_queue); } } + qemu_mutex_unlock(&hub->lock); return ret ? true : false; } -- 1.7.4.4