From patchwork Wed Apr 17 08:39:23 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 237173 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 582E92C0166 for ; Wed, 17 Apr 2013 19:15:35 +1000 (EST) Received: from localhost ([::1]:45375 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USO0U-0006MR-Fy for incoming@patchwork.ozlabs.org; Wed, 17 Apr 2013 04:46:22 -0400 Received: from eggs.gnu.org ([208.118.235.92]:50886) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNwF-0002LE-Ip for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:42:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USNwD-0003lq-8D for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:41:59 -0400 Received: from mail-gh0-f182.google.com ([209.85.160.182]:56275) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USNwD-0003ll-4B for qemu-devel@nongnu.org; Wed, 17 Apr 2013 04:41:57 -0400 Received: by mail-gh0-f182.google.com with SMTP id z15so126095ghb.27 for ; Wed, 17 Apr 2013 01:41:56 -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=1XrfPrLWl4ASxycHd136k4FTBSGdXJWGQP9JmtFBzhw=; b=bf9vfxP1by/24eu/uTrninjBvtSkCghqT9vpbtAt+4My+NDmPZD9gozlC888gyVSsr pbP3AaNtvrkXQxinN6AsqhRs7Kpc2M0MPQj8MtCkL2x2q+IBxOWX67jHc5pELwHG1roB 8L+tqB8I21leCO8FDQEjdrxqk8aukmbyIvVgKjIC3C3J/wEnGkqwObz2GfPJG+dSa+s9 V6npy2JbBvT6InPJ8bsJMVuv6mkIOb6y5UL/hZFfvmAqW26Q4a8KiH7AHjIbjcDciDJt Tl6zSI+kLjczBNjjHRrSSta7kYEpwVOfWvQzSGxbdw15XocFaGmbQ1jlMb4QmAvUSgI8 rcLA== X-Received: by 10.236.193.10 with SMTP id j10mr3096657yhn.194.1366188116688; Wed, 17 Apr 2013 01:41:56 -0700 (PDT) Received: from localhost ([114.245.251.229]) by mx.google.com with ESMTPS id 30sm8071630yhb.6.2013.04.17.01.41.52 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Wed, 17 Apr 2013 01:41:56 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 17 Apr 2013 16:39:23 +0800 Message-Id: <1366187964-14265-15-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.182 Cc: mdroth , Paolo Bonzini , Stefan Hajnoczi , Anthony Liguori , Jan Kiszka Subject: [Qemu-devel] [RFC PATCH v4 14/15] slirp: handle race condition 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 Slirp and its peer can run on different context at the same time. Using lock to protect Signed-off-by: Liu Ping Fan --- slirp/slirp.c | 16 ++++++++++++++-- slirp/slirp.h | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/slirp/slirp.c b/slirp/slirp.c index 883b7bd..6bfcc67 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -206,6 +206,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork, slirp_init_once(); + qemu_mutex_init(&slirp->lock); slirp->restricted = restricted; if_init(slirp); @@ -248,6 +249,7 @@ void slirp_cleanup(Slirp *slirp) ip_cleanup(slirp); m_cleanup(slirp); + qemu_mutex_destroy(&slirp->lock); g_free(slirp->vdnssearch); g_free(slirp->tftp_prefix); @@ -411,6 +413,7 @@ gboolean slirp_handler(gpointer data) struct socket *so, *so_next; int ret; + qemu_mutex_lock(&slirp->lock); /* * See if anything has timed out */ @@ -594,6 +597,7 @@ gboolean slirp_handler(gpointer data) } if_start(slirp); + qemu_mutex_unlock(&slirp->lock); return true; } @@ -665,6 +669,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) return; proto = ntohs(*(uint16_t *)(pkt + 12)); + qemu_mutex_lock(&slirp->lock); switch(proto) { case ETH_P_ARP: arp_input(slirp, pkt, pkt_len); @@ -688,6 +693,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) default: break; } + qemu_mutex_unlock(&slirp->lock); } /* Output the IP packet to the ethernet device. Returns 0 if the packet must be @@ -860,15 +866,21 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, const uint8_t *buf, int size) { int ret; - struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); + struct socket *so; + + qemu_mutex_lock(&slirp->lock); + so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); - if (!so) + if (!so) { + qemu_mutex_unlock(&slirp->lock); return; + } ret = soreadbuf(so, (const char *)buf, size); if (ret > 0) tcp_output(sototcpcb(so)); + qemu_mutex_unlock(&slirp->lock); } static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp) diff --git a/slirp/slirp.h b/slirp/slirp.h index 008360e..7ab0c70 100644 --- a/slirp/slirp.h +++ b/slirp/slirp.h @@ -135,6 +135,7 @@ void free(void *ptr); #include "qemu/queue.h" #include "qemu/sockets.h" +#include "qemu/thread.h" #include "libslirp.h" #include "ip.h" @@ -207,6 +208,8 @@ struct Slirp { u_int last_slowtimo; int do_slowtimo; + /* lock to protect slirp running both on frontend or SlirpState context */ + QemuMutex lock; /* virtual network configuration */ struct in_addr vnetwork_addr; struct in_addr vnetwork_mask;