From patchwork Fri Apr 26 02:47:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 239630 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 DD7A62C0114 for ; Fri, 26 Apr 2013 12:50:47 +1000 (EST) Received: from localhost ([::1]:48924 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UVYkI-0005CG-6J for incoming@patchwork.ozlabs.org; Thu, 25 Apr 2013 22:50:46 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49485) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UVYj4-0003yc-D7 for qemu-devel@nongnu.org; Thu, 25 Apr 2013 22:49:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UVYj1-0008EB-75 for qemu-devel@nongnu.org; Thu, 25 Apr 2013 22:49:30 -0400 Received: from mail-pd0-f170.google.com ([209.85.192.170]:43350) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UVYj1-0008E7-0w for qemu-devel@nongnu.org; Thu, 25 Apr 2013 22:49:27 -0400 Received: by mail-pd0-f170.google.com with SMTP id 10so2169830pdi.1 for ; Thu, 25 Apr 2013 19:49:26 -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=lBHzSyFXfwMYOWsso3pn47Exb2q/vdGu8lvgi05seAcrvFn7tpRmVne28X62aPbQoI oHU8VM2ZtRk00mOI3CT6PmQpxEfGNOK2I3SFUqsK3Hm8qRIrLRJVbzfmiwOSJdHGatsR IbVWSPoSwL5rXa4K2439zkfWZqSWad23jRI98xcQNjCQJXKiWuS1s5wrVuknWasbePHR 4PHHXiPmLpKgqRjwOVe1dzsvoHfjnTFXGvO6n5d1KPHMZMFGDzLKhfGWYcMQn7DybXzf abPOLV0unRshWtRz4xApEdvyMC7/fM6mzg/Z9xG9ek6zqz6CDbWE9eCdWVdigtyGkkVP 4j8g== X-Received: by 10.66.8.133 with SMTP id r5mr31992441paa.219.1366944566211; Thu, 25 Apr 2013 19:49:26 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id ya4sm9616967pbb.24.2013.04.25.19.49.23 for (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Thu, 25 Apr 2013 19:49:25 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Fri, 26 Apr 2013 10:47:29 +0800 Message-Id: <1366944455-14239-9-git-send-email-qemulist@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1366944455-14239-1-git-send-email-qemulist@gmail.com> References: <1366944455-14239-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.192.170 Cc: mdroth , Jan Kiszka , Stefan Hajnoczi , Anthony Liguori , Paolo Bonzini Subject: [Qemu-devel] [RFC PATCH v5 08/14] 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; }