From patchwork Wed Apr 17 08:39:18 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 237178 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 0AFA72C00EC for ; Wed, 17 Apr 2013 19:28:02 +1000 (EST) Received: from localhost ([::1]:42028 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNyY-0004S1-U8 for incoming@patchwork.ozlabs.org; Wed, 17 Apr 2013 04:44:22 -0400 Received: from eggs.gnu.org ([208.118.235.92]:50728) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNvT-00010H-4l for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:41:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USNvR-0003Zi-Rt for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:41:11 -0400 Received: from mail-ye0-f173.google.com ([209.85.213.173]:49812) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNvR-0003Zb-O3 for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:41:09 -0400 Received: by mail-ye0-f173.google.com with SMTP id q5so208451yen.32 for ; Wed, 17 Apr 2013 01:41:09 -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=KddcPjaJtmvoQvzIUKdiHwsOW0UfzU0TKA6Hus5xqxY=; b=WXr+kiRnIGP8KPOgW5JVhQsae0PKJT/tmJCGuvsc1AnH4ckrSIx4ni/PLmZmVCbsEt I8l/5h++uCWOD3IAW6e2jy6jX54fYa+qE0wrU2Edzb8gTTLFi2to0cgdnmkhQpA1C02x 8xCXnuvLtaKD05AdQMZkiy+MIqGX0rZDE1ZcDeTwrP7psk8RmO/yJ8tOrU63TRA7GWJy YiwEM2T5oTtcvP+ZclkQnNh6WDOpSMX0mXp86haJvOVKL0uB7W6JFbM2+AUG55oPEh3R kN7hk11eZ+xxOZS47KQct33LsCemqY5gGMYcK0x+VWGyNA7Ifwd8O+dSE5fSlKw914jr yZIg== X-Received: by 10.236.185.197 with SMTP id u45mr3114638yhm.17.1366188069229; Wed, 17 Apr 2013 01:41:09 -0700 (PDT) Received: from localhost ([114.245.251.229]) by mx.google.com with ESMTPS id 28sm8075113yhd.5.2013.04.17.01.41.04 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Wed, 17 Apr 2013 01:41:08 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 17 Apr 2013 16:39:18 +0800 Message-Id: <1366187964-14265-10-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.213.173 Cc: mdroth , Paolo Bonzini , Stefan Hajnoczi , Anthony Liguori , Jan Kiszka Subject: [Qemu-devel] [RFC PATCH v4 09/15] net: introduce lock to protect NetQueue 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 NetQueue will be accessed by nc and its peers at the same time, need lock to protect it. Signed-off-by: Liu Ping Fan --- net/queue.c | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-) diff --git a/net/queue.c b/net/queue.c index 859d02a..2856c1d 100644 --- a/net/queue.c +++ b/net/queue.c @@ -53,6 +53,7 @@ struct NetQueue { uint32_t nq_maxlen; uint32_t nq_count; + QemuMutex lock; QTAILQ_HEAD(packets, NetPacket) packets; unsigned delivering : 1; @@ -68,6 +69,7 @@ NetQueue *qemu_new_net_queue(void *opaque) queue->nq_maxlen = 10000; queue->nq_count = 0; + qemu_mutex_init(&queue->lock); QTAILQ_INIT(&queue->packets); queue->delivering = 0; @@ -107,7 +109,9 @@ static void qemu_net_queue_append(NetQueue *queue, memcpy(packet->data, buf, size); queue->nq_count++; + qemu_mutex_lock(&queue->lock); QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); + qemu_mutex_unlock(&queue->lock); } static void qemu_net_queue_append_iov(NetQueue *queue, @@ -142,7 +146,9 @@ static void qemu_net_queue_append_iov(NetQueue *queue, } queue->nq_count++; + qemu_mutex_lock(&queue->lock); QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); + qemu_mutex_unlock(&queue->lock); } static ssize_t qemu_net_queue_deliver(NetQueue *queue, @@ -229,6 +235,7 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from) { NetPacket *packet, *next; + qemu_mutex_lock(&queue->lock); QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { if (packet->sender == from) { QTAILQ_REMOVE(&queue->packets, packet, entry); @@ -236,10 +243,12 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from) g_free(packet); } } + qemu_mutex_unlock(&queue->lock); } bool qemu_net_queue_flush(NetQueue *queue) { + qemu_mutex_lock(&queue->lock); while (!QTAILQ_EMPTY(&queue->packets)) { NetPacket *packet; int ret; @@ -256,6 +265,7 @@ bool qemu_net_queue_flush(NetQueue *queue) if (ret == 0) { queue->nq_count++; QTAILQ_INSERT_HEAD(&queue->packets, packet, entry); + qemu_mutex_unlock(&queue->lock); return false; } @@ -265,5 +275,6 @@ bool qemu_net_queue_flush(NetQueue *queue) g_free(packet); } + qemu_mutex_unlock(&queue->lock); return true; }