From patchwork Mon Oct 4 16:13:29 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 66711 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 54401B70D8 for ; Tue, 5 Oct 2010 03:23:50 +1100 (EST) Received: from localhost ([127.0.0.1]:49408 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P2noI-0007XZ-Ow for incoming@patchwork.ozlabs.org; Mon, 04 Oct 2010 12:22:42 -0400 Received: from [140.186.70.92] (port=59048 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P2nlJ-0006Vq-JF for qemu-devel@nongnu.org; Mon, 04 Oct 2010 12:19:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P2nlI-0002yx-5O for qemu-devel@nongnu.org; Mon, 04 Oct 2010 12:19:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33842) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P2nlH-0002yt-TQ for qemu-devel@nongnu.org; Mon, 04 Oct 2010 12:19:36 -0400 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o94GJZu4019150 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 4 Oct 2010 12:19:35 -0400 Received: from redhat.com (vpn2-11-69.ams2.redhat.com [10.36.11.69]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id o94GJXLf003751; Mon, 4 Oct 2010 12:19:33 -0400 Date: Mon, 4 Oct 2010 18:13:29 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org, Alex Williamson Message-ID: <20101004161329.GA5925@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Subject: [Qemu-devel] [PATCHv3] net: delay freeing peer host device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org With -netdev, virtio devices present offload features to guest, depending on the backend used. Thus, removing host netdev peer while guest is active leads to guest-visible inconsistency and/or crashes. As a solution, while guest (NIC) peer device exists, we prevent the host peer pointer from being deleted. This patch does this by adding peer_deleted flag in nic state: if host device is going away while guest device is around, set this flag and keep a shell of the host device around for as long as guest device exists. The link is put down so all packets will get discarded. Note: this does not fix the fact that hotplug is asynchronous and depends on the guest. At the moment, management can detect that pci device deletion is delayed by doing e.g. info net. As a next step, we shall add commands that control hotplug/unplug without removing the device, an event to report that guest has responded to the hotplug event, and a command to cause instant device removal. Signed-off-by: Michael S. Tsirkin --- So the below has survived some stress-testing, I'll put this on my tree unless there are any objections. Changes from v2: fix crash on repeated netdev_del net.c | 50 ++++++++++++++++++++++++++++++++++++++++---------- net.h | 1 + 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/net.c b/net.c index 3d0fde7..032f09e 100644 --- a/net.c +++ b/net.c @@ -281,29 +281,59 @@ NICState *qemu_new_nic(NetClientInfo *info, return nic; } -void qemu_del_vlan_client(VLANClientState *vc) +static void qemu_free_vlan_client(VLANClientState *vc) { - if (vc->vlan) { - QTAILQ_REMOVE(&vc->vlan->clients, vc, next); - } else { + if (!vc->vlan) { if (vc->send_queue) { qemu_del_net_queue(vc->send_queue); } - QTAILQ_REMOVE(&non_vlan_clients, vc, next); if (vc->peer) { vc->peer->peer = NULL; } } - - if (vc->info->cleanup) { - vc->info->cleanup(vc); - } - qemu_free(vc->name); qemu_free(vc->model); qemu_free(vc); } +void qemu_del_vlan_client(VLANClientState *vc) +{ + /* If there is a peer NIC, delete and cleanup client, but do not free. */ + if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) { + NICState *nic = DO_UPCAST(NICState, nc, vc->peer); + if (nic->peer_deleted) { + return; + } + nic->peer_deleted = true; + /* Let NIC know peer is gone. */ + vc->peer->link_down = true; + if (vc->peer->info->link_status_changed) { + vc->peer->info->link_status_changed(vc->peer); + } + if (vc->info->cleanup) { + vc->info->cleanup(vc); + } + QTAILQ_REMOVE(&non_vlan_clients, vc, next); + return; + } + + /* If this is a peer NIC and peer has already been deleted, free it now. */ + if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) { + NICState *nic = DO_UPCAST(NICState, nc, vc); + if (nic->peer_deleted) { + qemu_free_vlan_client(vc->peer); + } + } + + if (vc->vlan) { + QTAILQ_REMOVE(&vc->vlan->clients, vc, next); + } else { + QTAILQ_REMOVE(&non_vlan_clients, vc, next); + } + + qemu_free_vlan_client(vc); +} + VLANClientState * qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, const char *client_str) diff --git a/net.h b/net.h index 518cf9c..44c31a9 100644 --- a/net.h +++ b/net.h @@ -72,6 +72,7 @@ typedef struct NICState { VLANClientState nc; NICConf *conf; void *opaque; + bool peer_deleted; } NICState; struct VLANState {