From nobody Fri May 3 06:01:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=ucloud.cn ARC-Seal: i=1; a=rsa-sha256; t=1592451842; cv=none; d=zohomail.com; s=zohoarc; b=Twjcc3Z+aQITLxVNM2JUVJuajG40kfbqps6SEMERK5bozszAXiGhMpjBzSDAGwoOJRXlm6i5Sp0DCaiS52R5ZkT8cYX98Xhmtct+4JfV1M2dQF9vVg+3CSD4vBsYLSJdQBIy9J01HYYQFW3nOenuMe5llHhdrCJpfq3x71p6TlY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592451842; h=Content-Type:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=K2SWv+F2oijQwPEwBwKTq9rHxJiYbfIzIyj+GAPb69g=; b=VxQ04sE7RF7ZrtPD34LHSoiSBBrJb6QRgL+VFsHyhvF79eDkzayM/Xoao5RPj3Hind5N+xkrKGUk5B6qksmE51BHE9KsKwdxOjOngjwrhTkupIWN3Xi4J0dcfoKVOWis5X9sEgZ9kUvA6IYWXhWhNAqjx/wQd1mK6EcbReVEUG8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1592451842254430.6177735750997; Wed, 17 Jun 2020 20:44:02 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-46-5DBxrBdpNFeD8MVSWP_SwA-1; Wed, 17 Jun 2020 23:43:57 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 861BB801504; Thu, 18 Jun 2020 03:43:52 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2104919931; Thu, 18 Jun 2020 03:43:52 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 726821809547; Thu, 18 Jun 2020 03:43:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 05I3hljV015878 for ; Wed, 17 Jun 2020 23:43:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id EC22B20267F2; Thu, 18 Jun 2020 03:43:46 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E7E8C2029F62 for ; Thu, 18 Jun 2020 03:43:44 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D16571033644 for ; Thu, 18 Jun 2020 03:43:44 +0000 (UTC) Received: from m9785.mail.qiye.163.com (m9785.mail.qiye.163.com [220.181.97.85]) by relay.mimecast.com with ESMTP id us-mta-471-9_yJcWd3OVipf85yxBYOHA-1; Wed, 17 Jun 2020 23:43:40 -0400 Received: from localhost.localdomain (unknown [106.75.220.3]) by m9785.mail.qiye.163.com (Hmail) with ESMTPA id 3E97C5C17B4; Thu, 18 Jun 2020 11:43:35 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1592451840; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:content-type:content-type:list-id: list-help:list-unsubscribe:list-subscribe:list-post; bh=K2SWv+F2oijQwPEwBwKTq9rHxJiYbfIzIyj+GAPb69g=; b=Xzc354ULG0znp3Lz0XamQUOG1EFookP+popwVC0YszeUM1XXmuur5jv2VXsHWbmcq7ngMi qEwRvdhpl7khjeU/IBozIX4+GuUxY2xDJuBZPN8Dg4c7dgcTXtNAFBuW9S3nZKs/CgFqGu RZaO24jJ+qujR1potXbPn7AQmd47HJ8= X-MC-Unique: 5DBxrBdpNFeD8MVSWP_SwA-1 X-MC-Unique: 9_yJcWd3OVipf85yxBYOHA-1 From: Bingsong Si To: libvir-list@redhat.com Subject: [PATCHv2] network: Fix a race condition when shutdown & start vm at the same time Date: Thu, 18 Jun 2020 11:43:22 +0800 Message-Id: <20200618034322.909901-1-owen.si@ucloud.cn> X-HM-Spam-Status: e1kfGhgUHx5ZQUtXWQgYFAkeWUFZSFVOQ0xCQkJDSUtMS0JJSFlXWShZQU lCN1dZLVlBSVdZDwkaFQgSH1lBWRcyNQs4HD8jERoRFU8OCx4dGlA6OhxWVlVJTUhJTShJWVdZCQ 4XHghZQVk1NCk2OjckKS43PllXWRYaDxIVHRRZQVk0MFkG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6MRQ6CTo4CDgwTjU0GRkUCAlM Ey4wCUNVSlVKTkJJT05KQ0pMSU5NVTMWGhIXVRQMHhVVCBI7DhgXFA4fVRgVRVlXWRILWUFZSktN VUxOVUlJS1VIWVdZCAFZQU5ISU83Bg++ X-HM-Tid: 0a72c58657ca2087kuqy3e97c5c17b4 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: danielhb413@gmail.com, gongwei@smartx.com, laine@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When shutdown vm, the qemuProcessStop cleanup virtual interface in two step= s: 1. qemuProcessKill kill qemu process, and vif disappeared 2. ovs-vsctl del-port from the ovs bridge If start a vm in the middle of the two steps, the new vm will reused the vi= f, but removed from ovs bridge by step 2 https://www.redhat.com/archives/libvir-list/2020-June/msg00681.html Signed-off-by: Bingsong Si --- src/libvirt_private.syms | 1 + src/qemu/qemu_process.c | 9 ++++++--- src/util/virnetdevtap.c | 12 ++++++++++-- src/util/virnetdevtap.h | 2 ++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fc7406f2b7..5258c07368 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2655,6 +2655,7 @@ virNetDevTapDelete; virNetDevTapGetName; virNetDevTapGetRealDeviceName; virNetDevTapInterfaceStats; +virNetDevTapMutex; virNetDevTapReattachBridge; =20 =20 diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index d36088ba98..2493b092b1 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -7483,9 +7483,12 @@ void qemuProcessStop(virQEMUDriverPtr driver, if (vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PROFILE_MIDONE= T) { ignore_value(virNetDevMidonetUnbindPort(vport)); } else if (vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PROFILE= _OPENVSWITCH) { - ignore_value(virNetDevOpenvswitchRemovePort( - virDomainNetGetActualBridgeName(net), - net->ifname)); + virMutexLock(&virNetDevTapMutex); + if (!virNetDevExists(net->ifname)) + ignore_value(virNetDevOpenvswitchRemovePort( + virDomainNetGetActualBridgeName(net), + net->ifname)); + virMutexUnlock(&virNetDevTapMutex); } } =20 diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index 7bd30ea0f9..d685e30a50 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -54,6 +54,8 @@ =20 VIR_LOG_INIT("util.netdevtap"); =20 +virMutex virNetDevTapMutex =3D VIR_MUTEX_INITIALIZER; + /** * virNetDevTapGetName: * @tapfd: a tun/tap file descriptor @@ -238,6 +240,7 @@ int virNetDevTapCreate(char **ifname, if (!tunpath) tunpath =3D "/dev/net/tun"; =20 + virMutexLock(&virNetDevTapMutex); memset(&ifr, 0, sizeof(ifr)); for (i =3D 0; i < tapfdSize; i++) { if ((fd =3D open(tunpath, O_RDWR)) < 0) { @@ -302,6 +305,7 @@ int virNetDevTapCreate(char **ifname, ret =3D 0; =20 cleanup: + virMutexUnlock(&virNetDevTapMutex); if (ret < 0) { VIR_FORCE_CLOSE(fd); while (i--) @@ -369,6 +373,7 @@ int virNetDevTapCreate(char **ifname, int ret =3D -1; char *newifname =3D NULL; =20 + virMutexLock(&virNetDevTapMutex); if (tapfdSize > 1) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Multiqueue devices are not supported on this sys= tem")); @@ -379,8 +384,10 @@ int virNetDevTapCreate(char **ifname, * we have to create 'tap' interface first and * then rename it to 'vnet' */ - if ((s =3D virNetDevSetupControl("tap", &ifr)) < 0) + if ((s =3D virNetDevSetupControl("tap", &ifr)) < 0) { + virMutexUnlock(&virNetDevTapMutex); return -1; + } =20 if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) { virReportSystemError(errno, "%s", @@ -434,6 +441,7 @@ int virNetDevTapCreate(char **ifname, =20 ret =3D 0; cleanup: + virMutexUnlock(&virNetDevTapMutex); VIR_FORCE_CLOSE(s); =20 return ret; @@ -710,7 +718,7 @@ int virNetDevTapCreateInBridgePort(const char *brname, if (virNetDevSetMAC(*ifname, &tapmac) < 0) goto error; =20 - if (virNetDevTapAttachBridge(*ifname, brname, macaddr, vmuuid, + if (virNetDevTapReattachBridge(*ifname, brname, macaddr, vmuuid, virtPortProfile, virtVlan, isolatedPort, mtu, actualMTU) < 0) { goto error; diff --git a/src/util/virnetdevtap.h b/src/util/virnetdevtap.h index c6bd9285ba..f2d332e5aa 100644 --- a/src/util/virnetdevtap.h +++ b/src/util/virnetdevtap.h @@ -29,6 +29,8 @@ # define VIR_NETDEV_TAP_REQUIRE_MANUAL_CLEANUP 1 #endif =20 +extern virMutex virNetDevTapMutex; + int virNetDevTapCreate(char **ifname, const char *tunpath, int *tapfd, --=20 2.18.4