From patchwork Thu Jun 27 03:38:47 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 254940 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8F4602C0084 for ; Thu, 27 Jun 2013 13:41:18 +1000 (EST) Received: from localhost ([::1]:37374 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Us35A-0004v7-FA for incoming@patchwork.ozlabs.org; Wed, 26 Jun 2013 23:41:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47442) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Us34e-0004qy-8i for qemu-devel@nongnu.org; Wed, 26 Jun 2013 23:40:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Us34c-0004fy-Gk for qemu-devel@nongnu.org; Wed, 26 Jun 2013 23:40:44 -0400 Received: from mail-ie0-x235.google.com ([2607:f8b0:4001:c03::235]:56195) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Us34c-0004ez-9K for qemu-devel@nongnu.org; Wed, 26 Jun 2013 23:40:42 -0400 Received: by mail-ie0-f181.google.com with SMTP id x12so565396ief.40 for ; Wed, 26 Jun 2013 20:40:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=i9FqNRXYhCVQ9gvN7wmIiaDy3MI2tdjmftZ8U+yNk60=; b=LGCGP8PSY50fMaIkvWpnD2Cvj642Iise851PQuXxmosjtvj4cPFqdWsNW+0EwBC/gX jk5Z7c+u4hVuLtLukMbpAE9ZSsB54POwJQqCn4zjsQZngWPcI5gOHo4QK12Pop/+WQbx f14AoOqIilGGMRVKoTBuzlrajJxIhRJAF3snLPq5cY+js4Ans/tbT7fgEonry+6DCgrQ XvrkSGbEmVoQjaP4Lh9Ku9lqLnDc5vAt74Ri33S3tXOEAiveWi+6Whmhj5qUD5wuzJUL j+/X0Yz2PYKT80OywYisAOcsxOF6Nt/TxqopsazlYXqwT8g9WnJqOh5rm9Y18tJ0AC7z Whxg== X-Received: by 10.50.7.105 with SMTP id i9mr4565066iga.43.1372304441669; Wed, 26 Jun 2013 20:40:41 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id x10sm11317086igl.3.2013.06.26.20.40.38 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 26 Jun 2013 20:40:40 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Thu, 27 Jun 2013 11:38:47 +0800 Message-Id: <1372304329-6931-6-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1372304329-6931-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1372304329-6931-1-git-send-email-pingfank@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4001:c03::235 Cc: mdroth , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v3 5/7] net: introduce lock to protect net clients 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 Signed-off-by: Liu Ping Fan --- net/net.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/net/net.c b/net/net.c index 6fc50d8..9f951b8 100644 --- a/net/net.c +++ b/net/net.c @@ -45,6 +45,7 @@ # define CONFIG_NET_BRIDGE #endif +static QemuMutex net_clients_lock; static QTAILQ_HEAD(, NetClientState) net_clients; int default_net = 1; @@ -165,6 +166,7 @@ static char *assign_name(NetClientState *nc1, const char *model) char buf[256]; int id = 0; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc == nc1) { continue; @@ -173,6 +175,7 @@ static char *assign_name(NetClientState *nc1, const char *model) id++; } } + qemu_mutex_unlock(&net_clients_lock); snprintf(buf, sizeof(buf), "%s.%d", model, id); @@ -205,7 +208,9 @@ static void qemu_net_client_setup(NetClientState *nc, peer->peer = nc; } qemu_mutex_init(&nc->peer_lock); + qemu_mutex_lock(&net_clients_lock); QTAILQ_INSERT_TAIL(&net_clients, nc, next); + qemu_mutex_unlock(&net_clients_lock); nc->send_queue = qemu_new_net_queue(nc); nc->destructor = destructor; @@ -281,7 +286,9 @@ void *qemu_get_nic_opaque(NetClientState *nc) static void qemu_cleanup_net_client(NetClientState *nc) { + qemu_mutex_lock(&net_clients_lock); QTAILQ_REMOVE(&net_clients, nc, next); + qemu_mutex_unlock(&net_clients_lock); if (nc->info->cleanup) { nc->info->cleanup(nc); @@ -406,6 +413,7 @@ void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) { NetClientState *nc; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { if (nc->queue_index == 0) { @@ -413,6 +421,7 @@ void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) } } } + qemu_mutex_unlock(&net_clients_lock); } int qemu_can_send_packet_nolock(NetClientState *sender) @@ -615,13 +624,16 @@ NetClientState *qemu_find_netdev(const char *id) { NetClientState *nc; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) continue; if (!strcmp(nc->name, id)) { + qemu_mutex_unlock(&net_clients_lock); return nc; } } + qemu_mutex_unlock(&net_clients_lock); return NULL; } @@ -632,6 +644,7 @@ int qemu_find_net_clients_except(const char *id, NetClientState **ncs, NetClientState *nc; int ret = 0; + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (nc->info->type == type) { continue; @@ -643,6 +656,7 @@ int qemu_find_net_clients_except(const char *id, NetClientState **ncs, ret++; } } + qemu_mutex_unlock(&net_clients_lock); return ret; } @@ -1024,6 +1038,7 @@ void do_info_network(Monitor *mon, const QDict *qdict) net_hub_info(mon); + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { peer = nc->peer; type = nc->info->type; @@ -1041,6 +1056,7 @@ void do_info_network(Monitor *mon, const QDict *qdict) print_net_client(mon, peer); } } + qemu_mutex_unlock(&net_clients_lock); } void qmp_set_link(const char *name, bool up, Error **errp) @@ -1094,6 +1110,7 @@ void net_cleanup(void) qemu_del_net_client(nc); } } + qemu_mutex_destroy(&net_clients_lock); } void net_check_clients(void) @@ -1115,6 +1132,7 @@ void net_check_clients(void) net_hub_check_clients(); + qemu_mutex_lock(&net_clients_lock); QTAILQ_FOREACH(nc, &net_clients, next) { if (!nc->peer) { fprintf(stderr, "Warning: %s %s has no peer\n", @@ -1122,6 +1140,7 @@ void net_check_clients(void) "nic" : "netdev", nc->name); } } + qemu_mutex_unlock(&net_clients_lock); /* Check that all NICs requested via -net nic actually got created. * NICs created via -device don't need to be checked here because @@ -1179,6 +1198,7 @@ int net_init_clients(void) #endif } + qemu_mutex_init(&net_clients_lock); QTAILQ_INIT(&net_clients); if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)