From nobody Fri May 17 23:56:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1697139518; cv=none; d=zohomail.com; s=zohoarc; b=Pf29RHolTxSQP/sE61mHOQCv3NKNLlOEtOFDxxZeZOtFXDdEE9+WXBC/BmdWj2zIOv9iQD+zPMrEAofJ2xgx7USDuGQUZmz/jAbj7ZhMmO1yHL04aiYmmgU3xKby1xsAKfexzt9KWabraXF6WFvdmxrr9GnvKOqnxqiyL65NhhM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1697139518; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=nEddbdOimmXXuPD9QGJYn0euQe96UZqPFxKfyDiD7qs=; b=JEuFEu+2+fp1vICxt7+l8wutSpQybkbaSFFm58j9M/Mo3WI21S4So+6mqI4zZwOb5Y4TDrEA+5ZOQwqBU2a5CrphWgrjQrf+7ZIC4dFJjcF0gEPhDuyrRKfDHkpkWBm2jDowkW8YpRQi/8LWC5s1mb2mrP0oG4PIJmRONNOeYHE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1697139518961811.506775711056; Thu, 12 Oct 2023 12:38:38 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-299-73gYhviyMYuAy-nRxsDLWA-1; Thu, 12 Oct 2023 15:38:35 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E1BD9855300; Thu, 12 Oct 2023 19:38:32 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8DE441C060DF; Thu, 12 Oct 2023 19:38:30 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 17D161946588; Thu, 12 Oct 2023 19:38:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 5562E1946586 for ; Thu, 12 Oct 2023 19:37:56 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 443B220296DC; Thu, 12 Oct 2023 19:37:56 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3C9FC20296DB for ; Thu, 12 Oct 2023 19:37:56 +0000 (UTC) Received: from us-smtp-inbound-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 12CAA855310 for ; Thu, 12 Oct 2023 19:37:56 +0000 (UTC) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by relay.mimecast.com with ESMTP id us-mta-605-Waa0OzVsOS6rnF0G4jLNkg-1; Thu, 12 Oct 2023 15:37:53 -0400 Received: from home-fedora.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id B662C20B74C0; Thu, 12 Oct 2023 12:37:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697139517; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=nEddbdOimmXXuPD9QGJYn0euQe96UZqPFxKfyDiD7qs=; b=V1a57DLmqPoXfVxD+PgiJgIvau5a2ApZAvD12z+Y86wJ08pZgRN9k1j6E0VTNoLRTpNh72 dbNR2/PTVn608k64rVif799dxuoaUtg+ELca9FrOb4A/SWcRExOnV4+Qmxw850FXZIf8JF 0uj9aJusKVKQipmmhXLt0Bp54B0tptM= X-MC-Unique: 73gYhviyMYuAy-nRxsDLWA-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: Waa0OzVsOS6rnF0G4jLNkg-1 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com B662C20B74C0 From: Praveen K Paladugu To: libvir-list@redhat.com Subject: [libvirt PATCH] hypervisor: Move interface mgmt methods to hypervisor Date: Thu, 12 Oct 2023 14:37:42 -0500 Message-ID: <20231012193742.64794-1-prapal@linux.microsoft.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: liuwe@microsoft.com Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: linux.microsoft.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1697139520148100001 Content-Type: text/plain; charset="utf-8"; x-default="true" Move guest interface management methods from qemu to hypervisor. These methods will be shared by networking support in ch driver. Signed-off-by: Praveen K Paladugu --- po/POTFILES | 1 + src/hypervisor/domain_interface.c | 279 ++++++++++++++++++++++++++++++ src/hypervisor/domain_interface.h | 38 ++++ src/hypervisor/meson.build | 1 + src/libvirt_private.syms | 6 + src/qemu/qemu_command.c | 7 +- src/qemu/qemu_hotplug.c | 3 +- src/qemu/qemu_interface.c | 246 +------------------------- src/qemu/qemu_interface.h | 6 - src/qemu/qemu_process.c | 3 +- 10 files changed, 341 insertions(+), 249 deletions(-) create mode 100644 src/hypervisor/domain_interface.c create mode 100644 src/hypervisor/domain_interface.h diff --git a/po/POTFILES b/po/POTFILES index 3a51aea5cb..023c041f61 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -92,6 +92,7 @@ src/hyperv/hyperv_util.c src/hyperv/hyperv_wmi.c src/hypervisor/domain_cgroup.c src/hypervisor/domain_driver.c +src/hypervisor/domain_interface.c src/hypervisor/virhostdev.c src/interface/interface_backend_netcf.c src/interface/interface_backend_udev.c diff --git a/src/hypervisor/domain_interface.c b/src/hypervisor/domain_inte= rface.c new file mode 100644 index 0000000000..b01b6ee767 --- /dev/null +++ b/src/hypervisor/domain_interface.c @@ -0,0 +1,279 @@ +/* + * Copyright Microsoft Corp. 2023 + * + * domain_interface.c: methods to manage guest/domain interfaces + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "virmacaddr.h" +#include "virnetdevtap.h" +#include "virconftypes.h" +#include "domain_conf.h" +#include "domain_interface.h" +#include "domain_nwfilter.h" +#include "domain_audit.h" +#include "virebtables.h" +#include "virlog.h" +#include "virfile.h" +#include "viralloc.h" +#include "virnetdevbridge.h" +#include "network_conf.h" + +#define VIR_FROM_THIS VIR_FROM_INTERFACE + +VIR_LOG_INIT("interface.interface_connect"); + +bool +virDomainInterfaceIsVnetCompatModel(const virDomainNetDef *net) +{ + return (virDomainNetIsVirtioModel(net) || + net->model =3D=3D VIR_DOMAIN_NET_MODEL_E1000E || + net->model =3D=3D VIR_DOMAIN_NET_MODEL_IGB || + net->model =3D=3D VIR_DOMAIN_NET_MODEL_VMXNET3); +} + +/* virDomainInterfaceEthernetConnect: + * @def: the definition of the VM + * @driver: qemu driver data + * @net: pointer to the VM's interface description + * @tapfd: array of file descriptor return value for the new device + * @tapfdsize: number of file descriptors in @tapfd + * + * Called *only* called if actualType is VIR_DOMAIN_NET_TYPE_ETHERNET + * (i.e. if the connection is made with a tap device) + */ +int +virDomainInterfaceEthernetConnect(virDomainDef *def, + virDomainNetDef *net, + ebtablesContext *ebtables, + bool macFilter, + bool privileged, + int *tapfd, + size_t tapfdSize) +{ + virMacAddr tapmac; + int ret =3D -1; + unsigned int tap_create_flags =3D VIR_NETDEV_TAP_CREATE_IFUP; + bool template_ifname =3D false; + const char *tunpath =3D "/dev/net/tun"; + const char *auditdev =3D tunpath; + + if (net->backend.tap) { + tunpath =3D net->backend.tap; + if (!privileged) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("cannot use custom tap device in session mode= ")); + goto cleanup; + } + } + VIR_WARN("PPK: interfaceEthernetConnect Called\n"); + if (virDomainInterfaceIsVnetCompatModel(net)) + tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_VNET_HDR; + + if (net->managed_tap =3D=3D VIR_TRISTATE_BOOL_NO) { + if (!net->ifname) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("target dev must be supplied when managed=3D'= no'")); + goto cleanup; + } + if (virNetDevExists(net->ifname) !=3D 1) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("target managed=3D'no' but specified dev does= n't exist")); + goto cleanup; + } + + tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING; + + if (virNetDevMacVLanIsMacvtap(net->ifname)) { + auditdev =3D net->ifname; + if (virNetDevMacVLanTapOpen(net->ifname, tapfd, tapfdSize) < 0) + goto cleanup; + if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, + virDomainInterfaceIsVnetCompatMod= el(net)) < 0) { + goto cleanup; + } + } else { + if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, + tap_create_flags) < 0) + goto cleanup; + } + } else { + + if (!net->ifname) + template_ifname =3D true; + + if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, + tap_create_flags) < 0) { + goto cleanup; + } + + /* The tap device's MAC address cannot match the MAC address + * used by the guest. This results in "received packet on + * vnetX with own address as source address" error logs from + * the kernel. + */ + virMacAddrSet(&tapmac, &net->mac); + if (tapmac.addr[0] =3D=3D 0xFE) + tapmac.addr[0] =3D 0xFA; + else + tapmac.addr[0] =3D 0xFE; + + if (virNetDevSetMAC(net->ifname, &tapmac) < 0) + goto cleanup; + + if (virNetDevSetOnline(net->ifname, true) < 0) + goto cleanup; + } + + if (net->script && + virNetDevRunEthernetScript(net->ifname, net->script) < 0) + goto cleanup; + + if (macFilter && + ebtablesAddForwardAllowIn(ebtables, + net->ifname, + &net->mac) < 0) + goto cleanup; + + if (net->filter && + virDomainConfNWFilterInstantiate(def->name, def->uuid, net, false)= < 0) { + goto cleanup; + } + + virDomainAuditNetDevice(def, net, auditdev, true); + + ret =3D 0; + + cleanup: + if (ret < 0) { + size_t i; + + virDomainAuditNetDevice(def, net, auditdev, false); + for (i =3D 0; i < tapfdSize && tapfd[i] >=3D 0; i++) + VIR_FORCE_CLOSE(tapfd[i]); + if (template_ifname) + VIR_FREE(net->ifname); + } + + return ret; +} + +/** + * qemuInterfaceStartDevice: + * @net: net device to start + * + * Based upon the type of device provided, perform the appropriate + * work to completely activate the device and make it reachable from + * the rest of the network. + */ +int +virDomainInterfaceStartDevice(virDomainNetDef *net) +{ + virDomainNetType actualType =3D virDomainNetGetActualType(net); + + switch (actualType) { + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_NETWORK: + if (virDomainNetGetActualBridgeMACTableManager(net) + =3D=3D VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { + /* libvirt is managing the FDB of the bridge this device + * is attaching to, so we have turned off learning and + * unicast_flood on the device to prevent the kernel from + * adding any FDB entries for it. This means we need to + * add an fdb entry ourselves, using the MAC address from + * the interface config. + */ + if (virNetDevBridgeFDBAdd(&net->mac, net->ifname, + VIR_NETDEVBRIDGE_FDB_FLAG_MASTER | + VIR_NETDEVBRIDGE_FDB_FLAG_TEMP) < 0) + return -1; + } + break; + + case VIR_DOMAIN_NET_TYPE_DIRECT: { + const char *physdev =3D virDomainNetGetActualDirectDev(net); + bool isOnline =3D true; + + /* set the physdev online if necessary. It may already be up, + * in which case we shouldn't re-up it just in case that causes + * some sort of "blip" in the physdev's status. + */ + if (physdev && virNetDevGetOnline(physdev, &isOnline) < 0) + return -1; + if (!isOnline && virNetDevSetOnline(physdev, true) < 0) + return -1; + + /* macvtap devices share their MAC address with the guest + * domain, and if they are set online prior to the domain CPUs + * being started, the host may send out traffic from this + * device that could confuse other entities on the network (in + * particular, if this new domain is the destination of a + * migration, and the source domain is still running, another + * host may mistakenly direct traffic for the guest to the + * destination domain rather than source domain). To prevent + * this, we create the macvtap device with IFF_UP false + * (i.e. "offline") then wait to bring it online until just as + * we are starting the domain CPUs. + */ + if (virNetDevSetOnline(net->ifname, true) < 0) + return -1; + break; + } + + case VIR_DOMAIN_NET_TYPE_ETHERNET: + if (virNetDevIPInfoAddToDev(net->ifname, &net->hostIP) < 0) + return -1; + + break; + + case VIR_DOMAIN_NET_TYPE_USER: + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + case VIR_DOMAIN_NET_TYPE_SERVER: + case VIR_DOMAIN_NET_TYPE_CLIENT: + case VIR_DOMAIN_NET_TYPE_MCAST: + case VIR_DOMAIN_NET_TYPE_UDP: + case VIR_DOMAIN_NET_TYPE_INTERNAL: + case VIR_DOMAIN_NET_TYPE_HOSTDEV: + case VIR_DOMAIN_NET_TYPE_VDPA: + case VIR_DOMAIN_NET_TYPE_NULL: + case VIR_DOMAIN_NET_TYPE_VDS: + case VIR_DOMAIN_NET_TYPE_LAST: + /* these types all require no action */ + break; + } + + return 0; +} + +/** + * virDomainInterfaceStartDevices: + * @def: domain definition + * + * Set all ifaces associated with this domain to the online state. + */ +int +virDomainInterfaceStartDevices(virDomainDef *def) +{ + size_t i; + + for (i =3D 0; i < def->nnets; i++) { + if (virDomainInterfaceStartDevice(def->nets[i]) < 0) + return -1; + } + return 0; +} diff --git a/src/hypervisor/domain_interface.h b/src/hypervisor/domain_inte= rface.h new file mode 100644 index 0000000000..4e76b84990 --- /dev/null +++ b/src/hypervisor/domain_interface.h @@ -0,0 +1,38 @@ +/* + * Copyright Microsoft Corp. 2023 + * + * domain_interface.h: methods to manage guest/domain interfaces + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "virebtables.h" + +int +virDomainInterfaceEthernetConnect(virDomainDef *def, + virDomainNetDef *net, + ebtablesContext *ebtables, + bool macFilter, + bool privileged, + int *tapfd, + size_t tapfdSize); + +bool +virDomainInterfaceIsVnetCompatModel(const virDomainNetDef *net); + +int virDomainInterfaceStartDevice(virDomainNetDef *net); +int virDomainInterfaceStartDevices(virDomainDef *def); diff --git a/src/hypervisor/meson.build b/src/hypervisor/meson.build index f35565b16b..819a9a82a2 100644 --- a/src/hypervisor/meson.build +++ b/src/hypervisor/meson.build @@ -1,6 +1,7 @@ hypervisor_sources =3D [ 'domain_cgroup.c', 'domain_driver.c', + 'domain_interface.c', 'virclosecallbacks.c', 'virhostdev.c', ] diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 4e475d5b1a..cae0ecb857 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1630,6 +1630,12 @@ virDomainDriverNodeDeviceReset; virDomainDriverParseBlkioDeviceStr; virDomainDriverSetupPersistentDefBlkioParams; =20 +# hypervisor/domain_interface.h +virDomainInterfaceEthernetConnect; +virDomainInterfaceIsVnetCompatModel; +virDomainInterfaceStartDevice; +virDomainInterfaceStartDevices; + =20 # hypervisor/virclosecallbacks.h virCloseCallbacksDomainAdd; diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 8a7b80719f..eda5327293 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -43,6 +43,7 @@ #include "domain_nwfilter.h" #include "domain_addr.h" #include "domain_conf.h" +#include "domain_interface.h" #include "netdev_bandwidth_conf.h" #include "virnetdevopenvswitch.h" #include "device_conf.h" @@ -53,6 +54,7 @@ #include "virmdev.h" #include "virutil.h" =20 + #include #include =20 @@ -8523,7 +8525,10 @@ qemuBuildInterfaceConnect(virDomainObj *vm, break; =20 case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (qemuInterfaceEthernetConnect(vm->def, priv->driver, net, + if (virDomainInterfaceEthernetConnect(vm->def, net, + priv->driver->ebtables, + priv->driver->config->macFilter, + priv->driver->privileged, tapfd, tapfdSize) < 0) return -1; vhostfd =3D true; diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 1f7f5bdd26..8e76cb8b38 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -39,6 +39,7 @@ #include "qemu_virtiofs.h" #include "domain_audit.h" #include "domain_cgroup.h" +#include "domain_interface.h" #include "netdev_bandwidth_conf.h" #include "domain_nwfilter.h" #include "virlog.h" @@ -1269,7 +1270,7 @@ qemuDomainAttachNetDevice(virQEMUDriver *driver, } =20 /* Set device online immediately */ - if (qemuInterfaceStartDevice(net) < 0) + if (virDomainInterfaceStartDevice(net) < 0) goto cleanup; =20 qemuDomainInterfaceSetDefaultQDisc(driver, net); diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index 8856bb95a8..48e3422ae1 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -24,6 +24,7 @@ #include "network_conf.h" #include "domain_audit.h" #include "domain_nwfilter.h" +#include "domain_interface.h" #include "qemu_interface.h" #include "viralloc.h" #include "virlog.h" @@ -41,110 +42,9 @@ =20 VIR_LOG_INIT("qemu.qemu_interface"); =20 -/** - * qemuInterfaceStartDevice: - * @net: net device to start - * - * Based upon the type of device provided, perform the appropriate - * work to completely activate the device and make it reachable from - * the rest of the network. - */ -int -qemuInterfaceStartDevice(virDomainNetDef *net) -{ - virDomainNetType actualType =3D virDomainNetGetActualType(net); - - switch (actualType) { - case VIR_DOMAIN_NET_TYPE_BRIDGE: - case VIR_DOMAIN_NET_TYPE_NETWORK: - if (virDomainNetGetActualBridgeMACTableManager(net) - =3D=3D VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { - /* libvirt is managing the FDB of the bridge this device - * is attaching to, so we have turned off learning and - * unicast_flood on the device to prevent the kernel from - * adding any FDB entries for it. This means we need to - * add an fdb entry ourselves, using the MAC address from - * the interface config. - */ - if (virNetDevBridgeFDBAdd(&net->mac, net->ifname, - VIR_NETDEVBRIDGE_FDB_FLAG_MASTER | - VIR_NETDEVBRIDGE_FDB_FLAG_TEMP) < 0) - return -1; - } - break; - - case VIR_DOMAIN_NET_TYPE_DIRECT: { - const char *physdev =3D virDomainNetGetActualDirectDev(net); - bool isOnline =3D true; - - /* set the physdev online if necessary. It may already be up, - * in which case we shouldn't re-up it just in case that causes - * some sort of "blip" in the physdev's status. - */ - if (physdev && virNetDevGetOnline(physdev, &isOnline) < 0) - return -1; - if (!isOnline && virNetDevSetOnline(physdev, true) < 0) - return -1; - - /* macvtap devices share their MAC address with the guest - * domain, and if they are set online prior to the domain CPUs - * being started, the host may send out traffic from this - * device that could confuse other entities on the network (in - * particular, if this new domain is the destination of a - * migration, and the source domain is still running, another - * host may mistakenly direct traffic for the guest to the - * destination domain rather than source domain). To prevent - * this, we create the macvtap device with IFF_UP false - * (i.e. "offline") then wait to bring it online until just as - * we are starting the domain CPUs. - */ - if (virNetDevSetOnline(net->ifname, true) < 0) - return -1; - break; - } - - case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (virNetDevIPInfoAddToDev(net->ifname, &net->hostIP) < 0) - return -1; - - break; =20 - case VIR_DOMAIN_NET_TYPE_USER: - case VIR_DOMAIN_NET_TYPE_VHOSTUSER: - case VIR_DOMAIN_NET_TYPE_SERVER: - case VIR_DOMAIN_NET_TYPE_CLIENT: - case VIR_DOMAIN_NET_TYPE_MCAST: - case VIR_DOMAIN_NET_TYPE_UDP: - case VIR_DOMAIN_NET_TYPE_INTERNAL: - case VIR_DOMAIN_NET_TYPE_HOSTDEV: - case VIR_DOMAIN_NET_TYPE_VDPA: - case VIR_DOMAIN_NET_TYPE_NULL: - case VIR_DOMAIN_NET_TYPE_VDS: - case VIR_DOMAIN_NET_TYPE_LAST: - /* these types all require no action */ - break; - } =20 - return 0; -} =20 -/** - * qemuInterfaceStartDevices: - * @def: domain definition - * - * Set all ifaces associated with this domain to the online state. - */ -int -qemuInterfaceStartDevices(virDomainDef *def) -{ - size_t i; - - for (i =3D 0; i < def->nnets; i++) { - if (qemuInterfaceStartDevice(def->nets[i]) < 0) - return -1; - } - return 0; -} =20 =20 /** @@ -166,7 +66,7 @@ qemuInterfaceStopDevice(virDomainNetDef *net) if (virDomainNetGetActualBridgeMACTableManager(net) =3D=3D VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { /* remove the FDB entries that were added during - * qemuInterfaceStartDevices() + * virDomainInterfaceStartDevices() */ if (virNetDevBridgeFDBDel(&net->mac, net->ifname, VIR_NETDEVBRIDGE_FDB_FLAG_MASTER | @@ -236,14 +136,7 @@ qemuInterfaceStopDevices(virDomainDef *def) } =20 =20 -static bool -qemuInterfaceIsVnetCompatModel(const virDomainNetDef *net) -{ - return (virDomainNetIsVirtioModel(net) || - net->model =3D=3D VIR_DOMAIN_NET_MODEL_E1000E || - net->model =3D=3D VIR_DOMAIN_NET_MODEL_IGB || - net->model =3D=3D VIR_DOMAIN_NET_MODEL_VMXNET3); -} + =20 =20 /** @@ -271,7 +164,7 @@ qemuInterfaceDirectConnect(virDomainDef *def, unsigned int macvlan_create_flags =3D VIR_NETDEV_MACVLAN_CREATE_WITH_T= AP; qemuDomainNetworkPrivate *netpriv =3D QEMU_DOMAIN_NETWORK_PRIVATE(net); =20 - if (qemuInterfaceIsVnetCompatModel(net)) + if (virDomainInterfaceIsVnetCompatModel(net)) macvlan_create_flags |=3D VIR_NETDEV_MACVLAN_VNET_HDR; =20 if (virNetDevMacVLanCreateWithVPortProfile(net->ifname, @@ -409,133 +302,6 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfig = *cfg, return *tapfd < 0 ? -1 : 0; } =20 - -/* qemuInterfaceEthernetConnect: - * @def: the definition of the VM - * @driver: qemu driver data - * @net: pointer to the VM's interface description - * @tapfd: array of file descriptor return value for the new device - * @tapfdsize: number of file descriptors in @tapfd - * - * Called *only* called if actualType is VIR_DOMAIN_NET_TYPE_ETHERNET - * (i.e. if the connection is made with a tap device) - */ -int -qemuInterfaceEthernetConnect(virDomainDef *def, - virQEMUDriver *driver, - virDomainNetDef *net, - int *tapfd, - size_t tapfdSize) -{ - virMacAddr tapmac; - int ret =3D -1; - unsigned int tap_create_flags =3D VIR_NETDEV_TAP_CREATE_IFUP; - bool template_ifname =3D false; - g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); - const char *tunpath =3D "/dev/net/tun"; - const char *auditdev =3D tunpath; - - if (net->backend.tap) { - tunpath =3D net->backend.tap; - if (!driver->privileged) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("cannot use custom tap device in session mode= ")); - goto cleanup; - } - } - - if (qemuInterfaceIsVnetCompatModel(net)) - tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_VNET_HDR; - - if (net->managed_tap =3D=3D VIR_TRISTATE_BOOL_NO) { - if (!net->ifname) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("target dev must be supplied when managed=3D'= no'")); - goto cleanup; - } - if (virNetDevExists(net->ifname) !=3D 1) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("target managed=3D'no' but specified dev does= n't exist")); - goto cleanup; - } - - tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING; - - if (virNetDevMacVLanIsMacvtap(net->ifname)) { - auditdev =3D net->ifname; - if (virNetDevMacVLanTapOpen(net->ifname, tapfd, tapfdSize) < 0) - goto cleanup; - if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, - qemuInterfaceIsVnetCompatModel(ne= t)) < 0) { - goto cleanup; - } - } else { - if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, - tap_create_flags) < 0) - goto cleanup; - } - } else { - - if (!net->ifname) - template_ifname =3D true; - - if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, - tap_create_flags) < 0) { - goto cleanup; - } - - /* The tap device's MAC address cannot match the MAC address - * used by the guest. This results in "received packet on - * vnetX with own address as source address" error logs from - * the kernel. - */ - virMacAddrSet(&tapmac, &net->mac); - if (tapmac.addr[0] =3D=3D 0xFE) - tapmac.addr[0] =3D 0xFA; - else - tapmac.addr[0] =3D 0xFE; - - if (virNetDevSetMAC(net->ifname, &tapmac) < 0) - goto cleanup; - - if (virNetDevSetOnline(net->ifname, true) < 0) - goto cleanup; - } - - if (net->script && - virNetDevRunEthernetScript(net->ifname, net->script) < 0) - goto cleanup; - - if (cfg->macFilter && - ebtablesAddForwardAllowIn(driver->ebtables, - net->ifname, - &net->mac) < 0) - goto cleanup; - - if (net->filter && - virDomainConfNWFilterInstantiate(def->name, def->uuid, net, false)= < 0) { - goto cleanup; - } - - virDomainAuditNetDevice(def, net, auditdev, true); - - ret =3D 0; - - cleanup: - if (ret < 0) { - size_t i; - - virDomainAuditNetDevice(def, net, auditdev, false); - for (i =3D 0; i < tapfdSize && tapfd[i] >=3D 0; i++) - VIR_FORCE_CLOSE(tapfd[i]); - if (template_ifname) - VIR_FREE(net->ifname); - } - - return ret; -} - - /* qemuInterfaceBridgeConnect: * @def: the definition of the VM * @driver: qemu driver data @@ -578,7 +344,7 @@ qemuInterfaceBridgeConnect(virDomainDef *def, if (!net->ifname) template_ifname =3D true; =20 - if (qemuInterfaceIsVnetCompatModel(net)) + if (virDomainInterfaceIsVnetCompatModel(net)) tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_VNET_HDR; =20 if (driver->privileged) { @@ -598,7 +364,7 @@ qemuInterfaceBridgeConnect(virDomainDef *def, * is attaching to, so we need to turn off learning and * unicast_flood on the device to prevent the kernel from * adding any FDB entries for it. We will add an fdb - * entry ourselves (during qemuInterfaceStartDevices(), + * entry ourselves (during virDomainInterfaceStartDevices(), * using the MAC address from the interface config. */ if (virNetDevBridgePortSetLearning(brname, net->ifname, false)= < 0) diff --git a/src/qemu/qemu_interface.h b/src/qemu/qemu_interface.h index 6eed3e6bd7..feb299dd6c 100644 --- a/src/qemu/qemu_interface.h +++ b/src/qemu/qemu_interface.h @@ -37,12 +37,6 @@ int qemuInterfaceDirectConnect(virDomainDef *def, size_t tapfdSize, virNetDevVPortProfileOp vmop); =20 -int qemuInterfaceEthernetConnect(virDomainDef *def, - virQEMUDriver *driver, - virDomainNetDef *net, - int *tapfd, - size_t tapfdSize); - int qemuInterfaceBridgeConnect(virDomainDef *def, virQEMUDriver *driver, virDomainNetDef *net, diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index ae0bb7bf80..8a87e7e685 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -74,6 +74,7 @@ #include "virhostcpu.h" #include "domain_audit.h" #include "domain_cgroup.h" +#include "domain_interface.h" #include "domain_nwfilter.h" #include "domain_postparse.h" #include "domain_validate.h" @@ -3121,7 +3122,7 @@ qemuProcessStartCPUs(virQEMUDriver *driver, virDomain= Obj *vm, g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); =20 /* Bring up netdevs before starting CPUs */ - if (qemuInterfaceStartDevices(vm->def) < 0) + if (virDomainInterfaceStartDevices(vm->def) < 0) return -1; =20 VIR_DEBUG("Using lock state '%s'", NULLSTR(priv->lockState)); --=20 2.41.0