From 84d983dc0a7f4b1bef8af60ef82cb906cb1fe4f8 Mon Sep 17 00:00:00 2001 From: Pearl1594 Date: Tue, 1 Dec 2026 15:41:11 -0500 Subject: [PATCH 1/2] Support multiple guest physical networks for KVM trunking --- api/src/main/java/com/cloud/host/Host.java | 10 ++- .../com/cloud/server/ManagementService.java | 10 ++- .../apache/cloudstack/api/ApiConstants.java | 1 + .../admin/host/FindHostsForMigrationCmd.java | 4 +- .../cloudstack/api/response/HostResponse.java | 16 +++- .../cloud/agent/api/CheckNetworkAnswer.java | 26 +++++++ .../com/cloud/agent/api/MigrateCommand.java | 13 ++-- .../agent/api/PrepareForMigrationAnswer.java | 13 ++++ .../agent/api/VlanTrunkMigrationHelper.java | 73 ++++++++++++------- .../cloud/agent/manager/AgentManagerImpl.java | 10 +-- .../cloud/vm/VirtualMachineManagerImpl.java | 2 +- .../orchestration/NetworkOrchestrator.java | 26 +++++++ .../com/cloud/host/dao/HostDetailsDao.java | 5 +- .../cloud/host/dao/HostDetailsDaoImpl.java | 4 +- .../StorageSystemDataMotionStrategy.java | 4 +- .../kvm/resource/BridgeVifDriver.java | 23 +++--- .../resource/LibvirtComputingResource.java | 34 +-------- .../LibvirtCheckNetworkCommandWrapper.java | 27 +++++-- .../wrapper/LibvirtMigrateCommandWrapper.java | 28 +++++-- ...virtPrepareForMigrationCommandWrapper.java | 9 ++- .../wrapper/LibvirtReadyCommandWrapper.java | 2 - .../motion/LinstorDataMotionStrategy.java | 4 +- .../motion/StorPoolDataMotionStrategy.java | 4 +- .../cloud/api/query/dao/HostJoinDaoImpl.java | 25 ++++++- .../deploy/DeploymentPlanningManagerImpl.java | 50 ++++++++----- .../com/cloud/network/NetworkServiceImpl.java | 22 +++--- .../cloud/server/ManagementServerImpl.java | 56 +++++++++++--- ui/public/locales/en.json | 1 + ui/src/components/view/DetailsTab.vue | 9 +++ ui/src/components/view/ListView.vue | 18 ++++- ui/src/config/section/infra/hosts.js | 2 +- 31 files changed, 374 insertions(+), 157 deletions(-) diff --git a/api/src/main/java/com/cloud/host/Host.java b/api/src/main/java/com/cloud/host/Host.java index 09325a8f4264..3f811017dd62 100644 --- a/api/src/main/java/com/cloud/host/Host.java +++ b/api/src/main/java/com/cloud/host/Host.java @@ -77,9 +77,15 @@ public static String[] toStrings(Host.Type... types) { String HOST_QEMU_RBD_SUPPORT = "host.qemu.rbd.support"; String HOST_SSH_PORT = "host.ssh.port"; String HOST_CDROM_MAX_COUNT = "host.cdrom.max.count"; - String HOST_VLAN_FILTERING_ENABLED = "vlan.filtering.enabled"; + // Prefix for a per-Guest-physical-network host_details key: the full key is this plus "." plus the + // physical network id, e.g. "vlan.filtering.enabled.205". A host's guest bridges are independent of + // each other, so this is never stored as a single value for the whole host. + String HOST_VLAN_FILTERING_ENABLED_PREFIX = "vlan.filtering.enabled."; String HOST_VLAN_TRUNK_XML_SUPPORTED = "vlan.trunk.xml.supported"; - String HOST_GUEST_BRIDGE_STP_ENABLED = "guest.bridge.stp.enabled"; + // Prefix for a per-Guest-physical-network host_details key: the full key is this plus "." plus the + // physical network id, e.g. "guest.bridge.stp.enabled.205". STP is a property of one bridge, not the + // whole host, so this is never stored as a single value for the whole host. + String HOST_GUEST_BRIDGE_STP_ENABLED_PREFIX = "guest.bridge.stp.enabled."; String GUEST_OS_CATEGORY_ID = "guest.os.category.id"; String GUEST_OS_RULE = "guest.os.rule"; String HOST_CPU_MAX_SPEED_MHZ = "host.cpu.max.speed.mhz"; diff --git a/api/src/main/java/com/cloud/server/ManagementService.java b/api/src/main/java/com/cloud/server/ManagementService.java index cdc51da159a9..38d0fc56f4b2 100644 --- a/api/src/main/java/com/cloud/server/ManagementService.java +++ b/api/src/main/java/com/cloud/server/ManagementService.java @@ -480,11 +480,13 @@ public interface ManagementService { String getMultiNetworkNicUnsuitableReason(VirtualMachine vm, Host host); /** - * If host's guest bridge has STP enabled, returns a human-readable caution about the forward-delay outage - * every new port on it incurs (migration, nic plug, vm start); null otherwise. Applies to every VM, not - * just ones with a multi-VLAN trunk NIC - unlike getMultiNetworkNicUnsuitableReason, never excludes the host. + * Returns a human-readable migration caution for this VM on this host, checked per nic against its own + * network's physical network: either that the nic's bridge has STP enabled (forward-delay outage on migrate, + * nic plug, vm start), or that the VM has a multi-VLAN trunk NIC but this host's libvirt version doesn't + * support native VLAN trunk XML (VLAN membership is applied manually after migration instead). Null if + * neither applies. Unlike getMultiNetworkNicUnsuitableReason, never excludes the host. */ - String getGuestBridgeStpMigrationCaution(Host host); + String getGuestBridgeStpMigrationCaution(VirtualMachine vm, Host host); /** * Apply affinity group constraints and other exclusion rules for VM migration. diff --git a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java index a9e06d8acb7c..fa0ce6d8e20a 100644 --- a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java +++ b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java @@ -336,6 +336,7 @@ public class ApiConstants { public static final String IMAGE_PATH = "imagepath"; public static final String INSTANCE_CONVERSION_SUPPORTED = "instanceconversionsupported"; public static final String VLAN_FILTERING_ENABLED = "vlanfilteringenabled"; + public static final String VLAN_FILTERING_ENABLED_BY_PHYSICAL_NETWORK = "vlanfilteringenabledbyphysicalnetwork"; public static final String VLAN_TRUNK_XML_SUPPORTED = "vlantrunkxmlsupported"; public static final String INTERNAL_DNS1 = "internaldns1"; public static final String INTERNAL_DNS2 = "internaldns2"; diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/host/FindHostsForMigrationCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/host/FindHostsForMigrationCmd.java index 313cc6488912..b00587b972f5 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/host/FindHostsForMigrationCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/host/FindHostsForMigrationCmd.java @@ -96,7 +96,9 @@ public void execute() { if (!suitableForMigration && vm != null) { hostResponse.setUnsuitableReason(_mgr.getMultiNetworkNicUnsuitableReason(vm, host)); } - hostResponse.setMigrationCaution(_mgr.getGuestBridgeStpMigrationCaution(host)); + if (vm != null) { + hostResponse.setMigrationCaution(_mgr.getGuestBridgeStpMigrationCaution(vm, host)); + } hostResponse.setObjectName("host"); hostResponses.add(hostResponse); diff --git a/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java b/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java index 52804cc980cd..1049c9441a49 100644 --- a/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java +++ b/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java @@ -316,9 +316,15 @@ public class HostResponse extends BaseResponseWithAnnotations { private Boolean instanceConversionSupported; @SerializedName(ApiConstants.VLAN_FILTERING_ENABLED) - @Param(description = "true if vlan_filtering is enabled on this host's guest bridge, so it can accept multi-VLAN trunk nics", since = "24.0.0") + @Param(description = "true only if vlan_filtering is enabled on every one of this host's Guest physical networks; " + + "see vlanfilteringenabledbyphysicalnetwork for the state of each one", since = "24.0.0") private Boolean vlanFilteringEnabled; + @SerializedName(ApiConstants.VLAN_FILTERING_ENABLED_BY_PHYSICAL_NETWORK) + @Param(description = "whether vlan_filtering is enabled on this host's guest bridge for each Guest physical network, " + + "keyed by physical network id - a host can have it enabled on one physical network and not another", since = "24.0.0") + private Map vlanFilteringEnabledByPhysicalNetwork; + @SerializedName(ApiConstants.VLAN_TRUNK_XML_SUPPORTED) @Param(description = "true if this host's libvirt version supports trunk vlan tap membership natively", since = "24.0.0") private Boolean vlanTrunkXmlSupported; @@ -916,6 +922,10 @@ public void setVlanFilteringEnabled(Boolean vlanFilteringEnabled) { this.vlanFilteringEnabled = vlanFilteringEnabled; } + public void setVlanFilteringEnabledByPhysicalNetwork(Map vlanFilteringEnabledByPhysicalNetwork) { + this.vlanFilteringEnabledByPhysicalNetwork = vlanFilteringEnabledByPhysicalNetwork; + } + public void setVlanTrunkXmlSupported(Boolean vlanTrunkXmlSupported) { this.vlanTrunkXmlSupported = vlanTrunkXmlSupported; } @@ -1020,6 +1030,10 @@ public Boolean getVlanFilteringEnabled() { return vlanFilteringEnabled; } + public Map getVlanFilteringEnabledByPhysicalNetwork() { + return vlanFilteringEnabledByPhysicalNetwork; + } + public Boolean getVlanTrunkXmlSupported() { return vlanTrunkXmlSupported; } diff --git a/core/src/main/java/com/cloud/agent/api/CheckNetworkAnswer.java b/core/src/main/java/com/cloud/agent/api/CheckNetworkAnswer.java index 392ad35e7cb6..b91df3dbdc94 100644 --- a/core/src/main/java/com/cloud/agent/api/CheckNetworkAnswer.java +++ b/core/src/main/java/com/cloud/agent/api/CheckNetworkAnswer.java @@ -19,10 +19,20 @@ package com.cloud.agent.api; +import java.util.Map; + public class CheckNetworkAnswer extends Answer { // indicate if agent reconnect is needed after setupNetworkNames command private boolean _reconnect; + // Each Guest physical network's own guest bridge may or may not have vlan_filtering enabled, + // independent of any other physical network on this host, so this is reported per physical + // network rather than once for the whole host. + private Map vlanFilteringEnabledByPhysicalNetwork; + + // Same per-physical-network reasoning as above: STP is a property of one bridge, not the whole host. + private Map stpEnabledByPhysicalNetwork; + public CheckNetworkAnswer() { } @@ -39,4 +49,20 @@ public boolean needReconnect() { return _reconnect; } + public Map getVlanFilteringEnabledByPhysicalNetwork() { + return vlanFilteringEnabledByPhysicalNetwork; + } + + public void setVlanFilteringEnabledByPhysicalNetwork(Map vlanFilteringEnabledByPhysicalNetwork) { + this.vlanFilteringEnabledByPhysicalNetwork = vlanFilteringEnabledByPhysicalNetwork; + } + + public Map getStpEnabledByPhysicalNetwork() { + return stpEnabledByPhysicalNetwork; + } + + public void setStpEnabledByPhysicalNetwork(Map stpEnabledByPhysicalNetwork) { + this.stpEnabledByPhysicalNetwork = stpEnabledByPhysicalNetwork; + } + } diff --git a/core/src/main/java/com/cloud/agent/api/MigrateCommand.java b/core/src/main/java/com/cloud/agent/api/MigrateCommand.java index eaaee660ba85..483cb5fd7438 100644 --- a/core/src/main/java/com/cloud/agent/api/MigrateCommand.java +++ b/core/src/main/java/com/cloud/agent/api/MigrateCommand.java @@ -48,9 +48,12 @@ public class MigrateCommand extends Command { Map vlanToPersistenceMap = new HashMap<>(); // null means unknown capability, not false - private Boolean destVlanFilteringEnabled; private Boolean destVlanTrunkXmlSupported; private Map nicBridgeMapping = new HashMap<>(); + // whether vlan_filtering is enabled on the destination bridge resolved for each nic MAC - read live + // off the destination host's own bridge (PrepareForMigrationAnswer), not a single cached host-wide + // flag, since a host can have vlan_filtering enabled on one Guest physical network's bridge and not another + private Map nicVlanFilteringMapping = new HashMap<>(); public Map getDpdkInterfaceMapping() { return dpdkInterfaceMapping; @@ -164,12 +167,12 @@ public void setClvmCrossPoolMigration(boolean clvmCrossPoolMigration) { this.clvmCrossPoolMigration = clvmCrossPoolMigration; } - public Boolean getDestVlanFilteringEnabled() { - return destVlanFilteringEnabled; + public Map getNicVlanFilteringMapping() { + return nicVlanFilteringMapping; } - public void setDestVlanFilteringEnabled(Boolean destVlanFilteringEnabled) { - this.destVlanFilteringEnabled = destVlanFilteringEnabled; + public void setNicVlanFilteringMapping(Map nicVlanFilteringMapping) { + this.nicVlanFilteringMapping = nicVlanFilteringMapping; } public Boolean getDestVlanTrunkXmlSupported() { diff --git a/core/src/main/java/com/cloud/agent/api/PrepareForMigrationAnswer.java b/core/src/main/java/com/cloud/agent/api/PrepareForMigrationAnswer.java index ae6a824de966..2ba65ea17590 100644 --- a/core/src/main/java/com/cloud/agent/api/PrepareForMigrationAnswer.java +++ b/core/src/main/java/com/cloud/agent/api/PrepareForMigrationAnswer.java @@ -33,6 +33,11 @@ public class PrepareForMigrationAnswer extends Answer { // destination bridge name per nic MAC, computed by the destination's own local capability private Map nicBridgeMapping = new HashMap<>(); + // whether vlan_filtering is enabled on the destination bridge resolved for each nic MAC above - + // read live off the destination host's own bridge, not a single cached host-wide flag, since a + // host can have vlan_filtering enabled on one Guest physical network's bridge and not another + private Map nicVlanFilteringMapping = new HashMap<>(); + protected PrepareForMigrationAnswer() { } @@ -71,4 +76,12 @@ public Map getNicBridgeMapping() { public void setNicBridgeMapping(Map nicBridgeMapping) { this.nicBridgeMapping = nicBridgeMapping; } + + public Map getNicVlanFilteringMapping() { + return nicVlanFilteringMapping; + } + + public void setNicVlanFilteringMapping(Map nicVlanFilteringMapping) { + this.nicVlanFilteringMapping = nicVlanFilteringMapping; + } } diff --git a/core/src/main/java/com/cloud/agent/api/VlanTrunkMigrationHelper.java b/core/src/main/java/com/cloud/agent/api/VlanTrunkMigrationHelper.java index ab5bf2afb922..8fcfe7b1b0e3 100644 --- a/core/src/main/java/com/cloud/agent/api/VlanTrunkMigrationHelper.java +++ b/core/src/main/java/com/cloud/agent/api/VlanTrunkMigrationHelper.java @@ -18,7 +18,6 @@ import java.net.URI; import java.util.Collection; -import java.util.List; import java.util.Map; import org.apache.commons.collections.MapUtils; @@ -58,43 +57,50 @@ public static void populateVlanTrunkMigrationDetails(MigrateCommand migrateComma migrateCommand.setNicBridgeMapping(nicBridgeMapping); } - DetailVO destVlanFilteringDetail = hostDetailsDao.findDetail(destHostId, Host.HOST_VLAN_FILTERING_ENABLED); - migrateCommand.setDestVlanFilteringEnabled(destVlanFilteringDetail == null ? null : Boolean.parseBoolean(destVlanFilteringDetail.getValue())); + // per-nic, read live off the destination host's own resolved bridge during PrepareForMigration - + // not a single host-wide flag, since a host can have vlan_filtering enabled on one Guest physical + // network's bridge and not another + Map nicVlanFilteringMapping = prepareForMigrationAnswer.getNicVlanFilteringMapping(); + if (MapUtils.isNotEmpty(nicVlanFilteringMapping)) { + migrateCommand.setNicVlanFilteringMapping(nicVlanFilteringMapping); + } + DetailVO destVlanTrunkXmlDetail = hostDetailsDao.findDetail(destHostId, Host.HOST_VLAN_TRUNK_XML_SUPPORTED); migrateCommand.setDestVlanTrunkXmlSupported(destVlanTrunkXmlDetail == null ? null : Boolean.parseBoolean(destVlanTrunkXmlDetail.getValue())); } - public static boolean vmNeedsPostMigrationVlanTrunkMembership(long vmId, long dstHostId, NicDao nicDao, HostDetailsDao hostDetailsDao) { + public static boolean vmNeedsPostMigrationVlanTrunkMembership(long vmId, long dstHostId, NicDao nicDao, NetworkDao networkDao, HostDetailsDao hostDetailsDao) { DetailVO trunkXmlDetail = hostDetailsDao.findDetail(dstHostId, Host.HOST_VLAN_TRUNK_XML_SUPPORTED); if (trunkXmlDetail != null && Boolean.parseBoolean(trunkXmlDetail.getValue())) { return false; } - DetailVO vlanFilteringDetail = hostDetailsDao.findDetail(dstHostId, Host.HOST_VLAN_FILTERING_ENABLED); - boolean destVlanFilteringEnabled = vlanFilteringDetail != null && Boolean.parseBoolean(vlanFilteringDetail.getValue()); - return nicDao.listByVmId(vmId).stream().anyMatch(nic -> nicNeedsManualVlanMembership(nic, destVlanFilteringEnabled)); + return nicDao.listByVmId(vmId).stream().anyMatch(nic -> nicNeedsManualVlanMembership(nic, dstHostId, networkDao, hostDetailsDao)); } - // A multi-VLAN trunk nic needs a VLAN-filtering host; a Guest VLAN 1 nic needs a legacy host, as the shared bridge's PVID is VLAN 1. Null when the host fits. + // A multi-VLAN trunk nic needs a VLAN-filtering host (on its own physical network's bridge); a Guest VLAN 1 + // nic needs a legacy (non-filtering) host on ITS physical network, as the shared bridge's PVID is VLAN 1. + // Each nic is checked against its own network's physical network, not one flag for the whole host. Null when the host fits. public static String getVlanFilteringUnsuitableReason(long vmId, long hostId, NicDao nicDao, NetworkDao networkDao, HostDetailsDao hostDetailsDao) { - List nics = nicDao.listByVmId(vmId); - boolean hasTrunkNic = hasTrunkNic(nics); - boolean hasNativeVlanNic = hasNativeVlanGuestNic(nics, networkDao); - if (!hasTrunkNic && !hasNativeVlanNic) { - return null; - } - boolean vlanFilteringEnabled = isVlanFilteringEnabled(hostId, hostDetailsDao); - if (hasTrunkNic && !vlanFilteringEnabled) { - return "Instance has a multi-VLAN trunk NIC, but this host does not have VLAN filtering enabled"; - } - if (hasNativeVlanNic && vlanFilteringEnabled) { - return NATIVE_VLAN_ON_FILTERING_HOST_REASON; + for (NicVO nic : nicDao.listByVmId(vmId)) { + Long physicalNetworkId = resolvePhysicalNetworkId(nic, networkDao); + if (physicalNetworkId == null) { + continue; + } + boolean vlanFilteringEnabled = isVlanFilteringEnabled(hostId, physicalNetworkId, hostDetailsDao); + if (nic.getMultiNetwork() && !vlanFilteringEnabled) { + return "Instance has a multi-VLAN trunk NIC, but this host's bridge for that NIC's physical network does not have VLAN filtering enabled"; + } + if (isNativeVlanGuestNic(nic, networkDao) && vlanFilteringEnabled) { + return NATIVE_VLAN_ON_FILTERING_HOST_REASON; + } } return null; } // For a network about to be plugged into a running Instance on this host. public static String getNativeVlanUnsuitableReason(Network network, long hostId, HostDetailsDao hostDetailsDao) { - return isNativeVlanGuestNetwork(network) && isVlanFilteringEnabled(hostId, hostDetailsDao) ? NATIVE_VLAN_ON_FILTERING_HOST_REASON : null; + return isNativeVlanGuestNetwork(network) && network.getPhysicalNetworkId() != null + && isVlanFilteringEnabled(hostId, network.getPhysicalNetworkId(), hostDetailsDao) ? NATIVE_VLAN_ON_FILTERING_HOST_REASON : null; } public static boolean hasTrunkNic(Collection nics) { @@ -110,11 +116,16 @@ public static boolean isNativeVlanGuestNetwork(Network network) { return network.getTrafficType() == Networks.TrafficType.Guest && isNativeVlan(network.getBroadcastUri()); } - private static boolean isVlanFilteringEnabled(long hostId, HostDetailsDao hostDetailsDao) { - DetailVO vlanFilteringDetail = hostDetailsDao.findDetail(hostId, Host.HOST_VLAN_FILTERING_ENABLED); + private static boolean isVlanFilteringEnabled(long hostId, long physicalNetworkId, HostDetailsDao hostDetailsDao) { + DetailVO vlanFilteringDetail = hostDetailsDao.findDetail(hostId, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + physicalNetworkId); return vlanFilteringDetail != null && Boolean.parseBoolean(vlanFilteringDetail.getValue()); } + private static Long resolvePhysicalNetworkId(NicVO nic, NetworkDao networkDao) { + NetworkVO network = networkDao.findById(nic.getNetworkId()); + return network == null ? null : network.getPhysicalNetworkId(); + } + // a stopped Instance's nics carry no broadcast URI (released on stop), so fall back to the network's private static boolean isNativeVlanGuestNic(NicVO nic, NetworkDao networkDao) { if (nic.getMultiNetwork()) { @@ -136,14 +147,20 @@ private static boolean isNativeVlan(URI broadcastUri) { && "1".equals(Networks.BroadcastDomainType.getValue(broadcastUri)); } - public static boolean nicNeedsManualVlanMembership(NicVO nic, boolean vlanFilteringEnabled) { - return nic.getBroadcastUri() != null && Networks.BroadcastDomainType.getSchemeValue(nic.getBroadcastUri()) == Networks.BroadcastDomainType.Vlan - && (nic.getMultiNetwork() || vlanFilteringEnabled); + public static boolean nicNeedsManualVlanMembership(NicVO nic, long hostId, NetworkDao networkDao, HostDetailsDao hostDetailsDao) { + if (nic.getBroadcastUri() == null || Networks.BroadcastDomainType.getSchemeValue(nic.getBroadcastUri()) != Networks.BroadcastDomainType.Vlan) { + return false; + } + if (nic.getMultiNetwork()) { + return true; + } + Long physicalNetworkId = resolvePhysicalNetworkId(nic, networkDao); + return physicalNetworkId != null && isVlanFilteringEnabled(hostId, physicalNetworkId, hostDetailsDao); } public static void sendPostMigrationVlanTrunkMembershipIfNeeded(VirtualMachineTO vmTO, long dstHostId, - NicDao nicDao, HostDetailsDao hostDetailsDao, CommandSender commandSender, Logger logger) { - if (!vmNeedsPostMigrationVlanTrunkMembership(vmTO.getId(), dstHostId, nicDao, hostDetailsDao)) { + NicDao nicDao, NetworkDao networkDao, HostDetailsDao hostDetailsDao, CommandSender commandSender, Logger logger) { + if (!vmNeedsPostMigrationVlanTrunkMembership(vmTO.getId(), dstHostId, nicDao, networkDao, hostDetailsDao)) { return; } final String warningMsg = "Post-migration VLAN trunk membership tasks did not complete for VM [{}] " diff --git a/engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java index e6b547ef9dcf..5d2bc0548fae 100644 --- a/engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java @@ -817,8 +817,6 @@ protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, fi String vddkSupport = detailsMap.get(Host.HOST_VDDK_SUPPORT); String vddkLibDir = detailsMap.get(Host.HOST_VDDK_LIB_DIR); String vddkVersion = detailsMap.get(Host.HOST_VDDK_VERSION); - String vlanFilteringEnabled = detailsMap.get(Host.HOST_VLAN_FILTERING_ENABLED); - String guestBridgeStpEnabled = detailsMap.get(Host.HOST_GUEST_BRIDGE_STP_ENABLED); String vlanTrunkXmlSupported = detailsMap.get(Host.HOST_VLAN_TRUNK_XML_SUPPORTED); String vmwareCbtSupport = detailsMap.get(Host.HOST_VDDK_BLOCKCOPY_SUPPORT); String vmwareCbtInPlaceFinalizationSupport = detailsMap.get(Host.HOST_VDDK_BLOCKCOPY_INPLACE_FINALIZATION_SUPPORT); @@ -832,7 +830,7 @@ protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, fi String vddkRbdDirectImportSupport = detailsMap.get(Host.HOST_VDDK_RBD_DIRECT_IMPORT_SUPPORT); logger.debug("Got HOST_UEFI_ENABLE [{}] for host [{}]:", uefiEnabled, host); if (ObjectUtils.anyNotNull(uefiEnabled, diskOnlyVmSnapshotNvramSupport, virtv2vVersion, ovftoolVersion, vddkSupport, vddkLibDir, vddkVersion, - vlanFilteringEnabled, guestBridgeStpEnabled, vlanTrunkXmlSupported, + vlanTrunkXmlSupported, vmwareCbtSupport, vmwareCbtInPlaceFinalizationSupport, vmwareCbtRbdSupport, qemuImgVersion, qemuNbdVersion, qemuIoVersion, virtV2vInPlaceVersion, virtv2vInPlaceSupport, qemuImgRbdSupport, vddkRbdDirectImportSupport)) { _hostDao.loadDetails(host); @@ -855,12 +853,6 @@ protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, fi host.getDetails().put(Host.HOST_VDDK_SUPPORT, vddkSupport); updateNeeded = true; } - if (syncBooleanHostCapability(host, Host.HOST_VLAN_FILTERING_ENABLED, vlanFilteringEnabled)) { - updateNeeded = true; - } - if (syncBooleanHostCapability(host, Host.HOST_GUEST_BRIDGE_STP_ENABLED, guestBridgeStpEnabled)) { - updateNeeded = true; - } if (syncBooleanHostCapability(host, Host.HOST_VLAN_TRUNK_XML_SUPPORTED, vlanTrunkXmlSupported)) { updateNeeded = true; } diff --git a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java index cd5cac7228eb..2db53de70c44 100755 --- a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java @@ -6597,7 +6597,7 @@ private boolean hasClvmVolumes(long vmId) { } boolean vmNeedsPostMigrationVlanTrunkMembership(VMInstanceVO vm, long dstHostId) { - return VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(vm.getId(), dstHostId, _nicsDao, hostDetailsDao); + return VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(vm.getId(), dstHostId, _nicsDao, _networkDao, hostDetailsDao); } private void executePreMigrationCommand(VMInstanceVO vm, VirtualMachineTO to, long srcHostId) { diff --git a/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java b/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java index d20f9e7b336b..f5abb73e77e0 100644 --- a/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java +++ b/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java @@ -60,6 +60,7 @@ import org.apache.cloudstack.network.dao.NetworkPermissionDao; import org.apache.cloudstack.reservation.dao.ReservationDao; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; @@ -290,6 +291,8 @@ public class NetworkOrchestrator extends ManagerBase implements NetworkOrchestra @Inject DataCenterDao _dcDao; @Inject + com.cloud.host.dao.HostDetailsDao _hostDetailsDao; + @Inject VlanDao _vlanDao; @Inject IPAddressDao _ipAddressDao; @@ -4735,8 +4738,31 @@ public void processConnect(final Host host, final StartupCommand cmd, final bool throw new ConnectionException(false, "Reinitialize agent after network setup."); } logger.debug("Network setup is correct on Agent"); + persistPerPhysicalNetworkHostCapabilities(hostId, answer); + return; + } + } + + // Each Guest physical network's own guest bridge may or may not have vlan_filtering/STP enabled, independent + // of any other physical network on this host, so each capability is stored per physical network rather than + // once for the whole host. + private void persistPerPhysicalNetworkHostCapabilities(long hostId, CheckNetworkAnswer answer) { + final Map details = new HashMap<>(); + addPerPhysicalNetworkDetails(details, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX, answer.getVlanFilteringEnabledByPhysicalNetwork()); + addPerPhysicalNetworkDetails(details, Host.HOST_GUEST_BRIDGE_STP_ENABLED_PREFIX, answer.getStpEnabledByPhysicalNetwork()); + if (details.isEmpty()) { return; } + _hostDetailsDao.persist(hostId, details); + } + + private void addPerPhysicalNetworkDetails(Map details, String keyPrefix, Map byPhysicalNetwork) { + if (MapUtils.isEmpty(byPhysicalNetwork)) { + return; + } + for (final Map.Entry entry : byPhysicalNetwork.entrySet()) { + details.put(keyPrefix + entry.getKey(), String.valueOf(entry.getValue())); + } } @Override diff --git a/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDao.java b/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDao.java index e60a904d7e02..b7274d32c459 100644 --- a/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDao.java +++ b/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDao.java @@ -38,7 +38,8 @@ public interface HostDetailsDao extends GenericDao { void replaceExternalDetails(long hostId, Map details); - // Ids of hosts with Host.HOST_VLAN_FILTERING_ENABLED=true, i.e. hosts able to run a multi-VLAN trunk nic. - Set findHostIdsWithVlanFilteringEnabled(); + // Ids of hosts with vlan_filtering enabled on the given Guest physical network's bridge, i.e. hosts + // able to run a multi-VLAN trunk nic on that physical network. + Set findHostIdsWithVlanFilteringEnabled(long physicalNetworkId); } diff --git a/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDaoImpl.java b/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDaoImpl.java index cf0b801ddd95..c3b8d26e7410 100644 --- a/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/host/dao/HostDetailsDaoImpl.java @@ -143,8 +143,8 @@ public List findByName(String name) { } @Override - public Set findHostIdsWithVlanFilteringEnabled() { - return findByName(Host.HOST_VLAN_FILTERING_ENABLED).stream() + public Set findHostIdsWithVlanFilteringEnabled(long physicalNetworkId) { + return findByName(Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + physicalNetworkId).stream() .filter(detail -> Boolean.parseBoolean(detail.getValue())) .map(DetailVO::getHostId) .collect(Collectors.toSet()); diff --git a/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java b/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java index 17f84b780698..f703d3e949bc 100644 --- a/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java +++ b/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java @@ -186,6 +186,8 @@ public class StorageSystemDataMotionStrategy implements DataMotionStrategy { @Inject private NicDao nicDao; @Inject + private com.cloud.network.dao.NetworkDao networkDao; + @Inject protected PrimaryDataStoreDao _storagePoolDao; @Inject private SnapshotDao _snapshotDao; @@ -2236,7 +2238,7 @@ public void copyAsync(Map volumeDataStoreMap, VirtualMach handlePostMigration(success, srcVolumeInfoToDestVolumeInfo, vmTO, srcHost, destHost); if (success) { - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, destHost.getId(), nicDao, hostDetailsDao, agentManager::send, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, destHost.getId(), nicDao, networkDao, hostDetailsDao, agentManager::send, logger); } if (!success) { diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java index b9915a91856b..78bf1dc6898e 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java @@ -213,8 +213,12 @@ public static boolean isUntaggedBroadcast(final NicTO nic) { } protected boolean usesSharedVlanAwareBridge(final NicTO nic) { - return nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan && _libvirtComputingResource.hostSupportsVlanFiltering() - && !isUntaggedBroadcast(nic); + if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vlan || isUntaggedBroadcast(nic)) { + return false; + } + String trafficLabel = nic.getName(); + String brName = trafficLabel != null && !trafficLabel.isEmpty() ? trafficLabel : _bridges.get("guest"); + return _libvirtComputingResource.isBridgeVlanFilteringEnabled(brName); } protected void plugTrunkVlanNic(LibvirtVMDef.InterfaceDef intf, NicTO nic, String trafficLabel, String guestOsType, String nicAdapter, @@ -222,13 +226,13 @@ protected void plugTrunkVlanNic(LibvirtVMDef.InterfaceDef intf, NicTO nic, Strin if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vlan) { throw new InternalErrorException("Multi-VLAN trunk nics are only supported on VLAN-isolated guest networks"); } - if (!_libvirtComputingResource.hostSupportsVlanFiltering()) { - throw new InternalErrorException("vlan_filtering is not enabled on this host's guest bridge; " - + "this host cannot accept a multi-VLAN trunk nic"); - } - String brName = trafficLabel != null && !trafficLabel.isEmpty() ? trafficLabel : _bridges.get("guest"); + if (!_libvirtComputingResource.isBridgeVlanFilteringEnabled(brName)) { + throw new InternalErrorException("vlan_filtering is not enabled on guest bridge " + brName + + "; this host cannot accept a multi-VLAN trunk nic on this physical network"); + } + Integer primaryVlanTag = parseVlanTag(nic.getBroadcastUri(), "primary network of nic " + nic.getMac()); List vlanTags = collectTrunkVlanTags(nic); warnIfUplinkMissingVlanMembership(brName, vlanTags); @@ -337,8 +341,9 @@ public void updateVlanTrunkMembership(Domain vm, LibvirtVMDef.InterfaceDef iface if (!usesSharedBridge) { throw new InternalErrorException("Nic " + nic.getMac() + " does not use the shared VLAN-aware bridge; cannot update its VLAN membership live"); } - if (!_libvirtComputingResource.hostSupportsVlanFiltering()) { - throw new InternalErrorException("vlan_filtering is not enabled on this host's guest bridge; cannot update multi-VLAN trunk membership live"); + if (!_libvirtComputingResource.isBridgeVlanFilteringEnabled(iface.getBrName())) { + throw new InternalErrorException("vlan_filtering is not enabled on guest bridge " + iface.getBrName() + + "; cannot update multi-VLAN trunk membership live"); } Integer primaryVlanTag = parseVlanTag(nic.getBroadcastUri(), "primary network of nic " + nic.getMac()); diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index cadb70a0d4e5..8eceef479641 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -38,8 +38,6 @@ import static com.cloud.host.Host.HOST_VDDK_BLOCKCOPY_INPLACE_FINALIZATION_SUPPORT; import static com.cloud.host.Host.HOST_VDDK_BLOCKCOPY_RBD_SUPPORT; import static com.cloud.host.Host.HOST_VDDK_BLOCKCOPY_SUPPORT; -import static com.cloud.host.Host.HOST_GUEST_BRIDGE_STP_ENABLED; -import static com.cloud.host.Host.HOST_VLAN_FILTERING_ENABLED; import static com.cloud.host.Host.HOST_VLAN_TRUNK_XML_SUPPORTED; import static com.cloud.host.Host.HOST_VOLUME_ENCRYPTION; import static org.apache.cloudstack.utils.linux.KVMHostInfo.isHostS390x; @@ -534,8 +532,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv protected String linkLocalBridgeName; protected String publicBridgeName; protected String guestBridgeName; - // Guest traffic label (BridgeVifDriver#plugTrunkVlanNic prefers it over guestBridgeName) - protected String guestTrafficLabel; protected String privateIp; protected String pool; protected String localGateway; @@ -4511,8 +4507,6 @@ public StartupCommand[] initialize() { cmd.getHostDetails().put(HOST_INSTANCE_CONVERSION, String.valueOf(instanceConversionSupported)); cmd.getHostDetails().put(HOST_VDDK_SUPPORT, String.valueOf(hostSupportsVddk())); cmd.getHostDetails().put(HOST_CDROM_MAX_COUNT, String.valueOf(LibvirtVMDef.MAX_CDROMS_PER_VM)); - cmd.getHostDetails().put(HOST_VLAN_FILTERING_ENABLED, String.valueOf(hostSupportsVlanFiltering())); - cmd.getHostDetails().put(HOST_GUEST_BRIDGE_STP_ENABLED, String.valueOf(isGuestBridgeStpEnabled())); cmd.getHostDetails().put(HOST_VLAN_TRUNK_XML_SUPPORTED, String.valueOf(hostSupportsVlanTrunkXml())); cmd.getHostDetails().put(HOST_VDDK_BLOCKCOPY_SUPPORT, String.valueOf(hostSupportsVddkBlockCopy())); cmd.getHostDetails().put(HOST_VDDK_BLOCKCOPY_INPLACE_FINALIZATION_SUPPORT, String.valueOf(hostSupportsVddkBlockCopyInPlaceFinalization())); @@ -6384,25 +6378,7 @@ public boolean hostSupportsVlanTrunkXml() { return hypervisorLibvirtVersion >= MIN_LIBVIRT_VERSION_FOR_VLAN_TRUNK; } - /** - * Live state: whether vlan_filtering is currently enabled on this host's guest bridge. - * A vlan_filtering=0 bridge floods every tagged frame to every port regardless of tag, - * so this must be true before any multi-VLAN trunk nic can be placed on this host. - */ - public boolean hostSupportsVlanFiltering() { - return isBridgeVlanFilteringEnabled(getEffectiveGuestBridgeName()); - } - - public void setGuestTrafficLabel(String guestTrafficLabel) { - this.guestTrafficLabel = guestTrafficLabel; - } - - // Mirrors BridgeVifDriver#plugTrunkVlanNic's bridge resolution - protected String getEffectiveGuestBridgeName() { - return StringUtils.isNotBlank(guestTrafficLabel) ? guestTrafficLabel : guestBridgeName; - } - - protected boolean isBridgeVlanFilteringEnabled(String bridgeName) { + public boolean isBridgeVlanFilteringEnabled(String bridgeName) { if (StringUtils.isBlank(bridgeName)) { return false; } @@ -6418,12 +6394,8 @@ protected boolean isBridgeVlanFilteringEnabled(String bridgeName) { } } - // whether STP is enabled on this host's guest bridge - delays new ports (vm start, nic plug, migration) ~2x the forward delay before they forward traffic - public boolean isGuestBridgeStpEnabled() { - return isBridgeStpEnabled(getEffectiveGuestBridgeName()); - } - - protected boolean isBridgeStpEnabled(String bridgeName) { + // whether STP is enabled on a guest bridge - delays new ports (vm start, nic plug, migration) ~2x the forward delay before they forward traffic + public boolean isBridgeStpEnabled(String bridgeName) { if (StringUtils.isBlank(bridgeName)) { return false; } diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapper.java index eaa0dec5fe7f..76501e2ef25d 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapper.java @@ -19,7 +19,9 @@ package com.cloud.hypervisor.kvm.resource.wrapper; +import java.util.HashMap; import java.util.List; +import java.util.Map; import com.cloud.agent.api.Answer; import com.cloud.agent.api.CheckNetworkAnswer; @@ -38,12 +40,8 @@ public final class LibvirtCheckNetworkCommandWrapper extends CommandWrapper phyNics = command.getPhysicalNetworkInfoList(); String errMsg = null; - String guestNetworkLabel = null; for (final PhysicalNetworkSetupInfo nic : phyNics) { - if (StringUtils.isNotBlank(nic.getGuestNetworkName())) { - guestNetworkLabel = nic.getGuestNetworkName(); - } if (!libvirtComputingResource.checkNetwork(Networks.TrafficType.Guest, nic.getGuestNetworkName())) { errMsg = "Can not find network: " + nic.getGuestNetworkName(); break; @@ -56,12 +54,27 @@ public Answer execute(final CheckNetworkCommand command, final LibvirtComputingR } } - libvirtComputingResource.setGuestTrafficLabel(guestNetworkLabel); - if (errMsg != null) { return new CheckNetworkAnswer(command, false, errMsg); } else { - return new CheckNetworkAnswer(command, true, null); + final CheckNetworkAnswer answer = new CheckNetworkAnswer(command, true, null); + answer.setVlanFilteringEnabledByPhysicalNetwork(getPerPhysicalNetworkCapability(phyNics, libvirtComputingResource::isBridgeVlanFilteringEnabled)); + answer.setStpEnabledByPhysicalNetwork(getPerPhysicalNetworkCapability(phyNics, libvirtComputingResource::isBridgeStpEnabled)); + return answer; + } + } + + // Each physical network's own guest bridge may or may not have vlan_filtering/STP enabled, independent of any + // other physical network on this host, so each capability is reported per physical network rather than once for the host. + private Map getPerPhysicalNetworkCapability(final List phyNics, + final java.util.function.Predicate bridgeCapability) { + final Map capabilityByPhysicalNetwork = new HashMap<>(); + for (final PhysicalNetworkSetupInfo nic : phyNics) { + if (nic.getPhysicalNetworkId() == null || StringUtils.isBlank(nic.getGuestNetworkName())) { + continue; + } + capabilityByPhysicalNetwork.put(nic.getPhysicalNetworkId(), bridgeCapability.test(nic.getGuestNetworkName())); } + return capabilityByPhysicalNetwork; } } diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java index f15ad8e562f8..1831b911c4d9 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java @@ -699,13 +699,9 @@ protected String replaceDpdkInterfaces(String xmlDesc, Map dpdkP protected String replaceVlanTrunkInterfaces(String xmlDesc, MigrateCommand command, LibvirtComputingResource libvirtComputingResource) throws ParserConfigurationException, IOException, SAXException, TransformerException { NicTO[] nics = command.getVirtualMachine().getNics(); - boolean sourceVlanFilteringEnabled = libvirtComputingResource.hostSupportsVlanFiltering(); - // destination-shared-bridge is also a rewrite trigger: a legacy-source nic's bridge name (breth1-) - // doesn't exist on a shared-bridge destination, needs rewriting to cloudbr1 just as much as the reverse - boolean destVlanFilteringEnabled = Boolean.TRUE.equals(command.getDestVlanFilteringEnabled()); boolean anyNicNeedsHandling = false; for (NicTO nic : nics) { - if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan && (sourceVlanFilteringEnabled || destVlanFilteringEnabled || nic.isTrunkVlan())) { + if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan && nicNeedsVlanTrunkHandling(nic, command, libvirtComputingResource)) { failIfBroadcastUriMissing(nic); anyNicNeedsHandling = true; } @@ -736,7 +732,7 @@ protected String replaceVlanTrunkInterfaces(String xmlDesc, MigrateCommand comma String mac = findInterfaceMacAddress(interfaceElement); NicTO nic = mac == null ? null : findNicByMac(nics, mac); if (nic == null || nic.getBroadcastType() != Networks.BroadcastDomainType.Vlan - || !(sourceVlanFilteringEnabled || destVlanFilteringEnabled || nic.isTrunkVlan())) { + || !nicNeedsVlanTrunkHandling(nic, command, libvirtComputingResource)) { continue; } replaceVlanTrunkInterface(doc, interfaceElement, nic, command); @@ -746,6 +742,24 @@ protected String replaceVlanTrunkInterfaces(String xmlDesc, MigrateCommand comma return LibvirtXMLParser.getXml(doc); } + // A vlan-broadcast nic needs its bridge/trunk XML rewritten for migration whenever either side of the move + // could put it on a different kind of bridge: its own source bridge, or its destination bridge, may have + // vlan_filtering on while the other doesn't - each host's own bridges are independent, not a single + // host-wide flag, so this is resolved per nic rather than once for the whole migration. + private boolean nicNeedsVlanTrunkHandling(NicTO nic, MigrateCommand command, LibvirtComputingResource libvirtComputingResource) { + if (nic.isTrunkVlan()) { + return true; + } + boolean sourceVlanFilteringEnabled = libvirtComputingResource.isBridgeVlanFilteringEnabled(resolveNicBridge(nic, libvirtComputingResource)); + boolean destVlanFilteringEnabled = Boolean.TRUE.equals(command.getNicVlanFilteringMapping().get(nic.getMac())); + return sourceVlanFilteringEnabled || destVlanFilteringEnabled; + } + + private String resolveNicBridge(NicTO nic, LibvirtComputingResource libvirtComputingResource) { + String trafficLabel = nic.getName(); + return StringUtils.isNotBlank(trafficLabel) ? trafficLabel : libvirtComputingResource.getGuestBridgeName(); + } + // A VLAN nic (or associated network) without a broadcast URI would be migrated onto the destination with no VLAN membership. private void failIfBroadcastUriMissing(NicTO nic) { boolean missing = nic.getBroadcastUri() == null; @@ -784,7 +798,7 @@ private String findInterfaceMacAddress(Element interfaceElement) { private void replaceVlanTrunkInterface(Document doc, Element interfaceElement, NicTO nic, MigrateCommand command) { String destBridge = command.getNicBridgeMapping().get(nic.getMac()); - Boolean destVlanFilteringEnabled = command.getDestVlanFilteringEnabled(); + Boolean destVlanFilteringEnabled = command.getNicVlanFilteringMapping().get(nic.getMac()); Boolean destVlanTrunkXmlSupported = command.getDestVlanTrunkXmlSupported(); if (StringUtils.isBlank(destBridge) || destVlanFilteringEnabled == null || destVlanTrunkXmlSupported == null) { throw new CloudRuntimeException(String.format( diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java index 7d02c728b526..a14f9c9540f7 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java @@ -71,6 +71,7 @@ public Answer execute(final PrepareForMigrationCommand command, final LibvirtCom Map dpdkInterfaceMapping = new HashMap<>(); Map nicBridgeMapping = new HashMap<>(); + Map nicVlanFilteringMapping = new HashMap<>(); boolean skipDisconnect = false; @@ -93,6 +94,7 @@ public Answer execute(final PrepareForMigrationCommand command, final LibvirtCom } if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan && interfaceDef != null) { nicBridgeMapping.put(nic.getMac(), interfaceDef.getBrName()); + nicVlanFilteringMapping.put(nic.getMac(), libvirtComputingResource.isBridgeVlanFilteringEnabled(interfaceDef.getBrName())); } } @@ -146,7 +148,7 @@ public Answer execute(final PrepareForMigrationCommand command, final LibvirtCom } logger.info("Successfully prepared destination host for migration of VM {}", vm.getName()); - return createPrepareForMigrationAnswer(command, dpdkInterfaceMapping, nicBridgeMapping, libvirtComputingResource, vm); + return createPrepareForMigrationAnswer(command, dpdkInterfaceMapping, nicBridgeMapping, nicVlanFilteringMapping, libvirtComputingResource, vm); } catch (final LibvirtException | CloudRuntimeException | InternalErrorException | URISyntaxException e) { if (MapUtils.isNotEmpty(dpdkInterfaceMapping)) { for (DpdkTO to : dpdkInterfaceMapping.values()) { @@ -163,6 +165,7 @@ public Answer execute(final PrepareForMigrationCommand command, final LibvirtCom protected PrepareForMigrationAnswer createPrepareForMigrationAnswer(PrepareForMigrationCommand command, Map dpdkInterfaceMapping, Map nicBridgeMapping, + Map nicVlanFilteringMapping, LibvirtComputingResource libvirtComputingResource, VirtualMachineTO vm) { PrepareForMigrationAnswer answer = new PrepareForMigrationAnswer(command); @@ -175,6 +178,10 @@ protected PrepareForMigrationAnswer createPrepareForMigrationAnswer(PrepareForMi answer.setNicBridgeMapping(nicBridgeMapping); } + if (MapUtils.isNotEmpty(nicVlanFilteringMapping)) { + answer.setNicVlanFilteringMapping(nicVlanFilteringMapping); + } + int newCpuShares = libvirtComputingResource.calculateCpuShares(vm); logger.debug(String.format("Setting CPU shares to [%s] for the migration of VM [%s].", newCpuShares, vm)); answer.setNewVmCpuShares(newCpuShares); diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java index 62fab3b40b46..c00ef942b0a3 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtReadyCommandWrapper.java @@ -70,8 +70,6 @@ public Answer execute(final ReadyCommand command, final LibvirtComputingResource hostDetails.put(Host.HOST_OVFTOOL_VERSION, libvirtComputingResource.getHostOvfToolVersion()); } - hostDetails.put(Host.HOST_VLAN_FILTERING_ENABLED, Boolean.toString(libvirtComputingResource.hostSupportsVlanFiltering())); - hostDetails.put(Host.HOST_GUEST_BRIDGE_STP_ENABLED, Boolean.toString(libvirtComputingResource.isGuestBridgeStpEnabled())); hostDetails.put(Host.HOST_VLAN_TRUNK_XML_SUPPORTED, Boolean.toString(libvirtComputingResource.hostSupportsVlanTrunkXml())); return new ReadyAnswer(command, hostDetails); diff --git a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/motion/LinstorDataMotionStrategy.java b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/motion/LinstorDataMotionStrategy.java index 35b1521f4d4a..0ae892958d41 100644 --- a/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/motion/LinstorDataMotionStrategy.java +++ b/plugins/storage/volume/linstor/src/main/java/org/apache/cloudstack/storage/motion/LinstorDataMotionStrategy.java @@ -127,6 +127,8 @@ public class LinstorDataMotionStrategy implements DataMotionStrategy { @Inject private NicDao nicDao; @Inject + private com.cloud.network.dao.NetworkDao networkDao; + @Inject private PrimaryDataStoreDao _storagePoolDao; @Override @@ -560,7 +562,7 @@ public void copyAsync(Map volumeDataStoreMap, VirtualMach throw new CloudRuntimeException(errMsg); } - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, destHost.getId(), nicDao, hostDetailsDao, _agentManager::send, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, destHost.getId(), nicDao, networkDao, hostDetailsDao, _agentManager::send, logger); } catch (AgentUnavailableException | OperationTimedoutException | CloudRuntimeException ex) { errMsg = String.format( "Copy volume(s) of VM [%s] to storage(s) [%s] and VM to host [%s] failed in LinstorDataMotionStrategy.copyAsync. Error message: [%s].", diff --git a/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/motion/StorPoolDataMotionStrategy.java b/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/motion/StorPoolDataMotionStrategy.java index 16b176844faf..b4c87d6e0f9f 100644 --- a/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/motion/StorPoolDataMotionStrategy.java +++ b/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/motion/StorPoolDataMotionStrategy.java @@ -128,6 +128,8 @@ public class StorPoolDataMotionStrategy implements DataMotionStrategy { @Inject private NicDao nicDao; @Inject + private com.cloud.network.dao.NetworkDao networkDao; + @Inject private SnapshotDetailsDao _snapshotDetailsDao; @Inject private VMTemplateDetailsDao _vmTemplateDetailsDao; @@ -389,7 +391,7 @@ public void copyAsync(Map volumeDataStoreMap, VirtualMach handlePostMigration(success, srcVolumeInfoToDestVolumeInfo, vmTO, destHost); if (success) { - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, destHost.getId(), nicDao, hostDetailsDao, _agentManager::send, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, destHost.getId(), nicDao, networkDao, hostDetailsDao, _agentManager::send, logger); } if (migrateAnswer == null) { diff --git a/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java index f942a38746c6..7f8b2042f883 100644 --- a/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.inject.Inject; @@ -55,6 +56,8 @@ import com.cloud.host.HostStats; import com.cloud.host.dao.HostDetailsDao; import com.cloud.hypervisor.Hypervisor; +import com.cloud.network.Networks; +import com.cloud.network.dao.PhysicalNetworkTrafficTypeDao; import com.cloud.resource.ResourceState; import com.cloud.storage.StorageStats; import com.cloud.user.AccountManager; @@ -84,6 +87,8 @@ public class HostJoinDaoImpl extends GenericDaoBase implements private AnnotationDao annotationDao; @Inject private AccountManager accountManager; + @Inject + private PhysicalNetworkTrafficTypeDao physicalNetworkTrafficTypeDao; private final SearchBuilder hostSearch; @@ -121,6 +126,13 @@ private boolean containsHostHATag(final String tags) { return result; } + // Falls back to the physical network id itself if no Guest traffic label is set for it on this + // hypervisor type - keeps the map populated rather than silently dropping the entry. + private String resolveGuestBridgeName(Long physicalNetworkId, Hypervisor.HypervisorType hypervisorType) { + String tag = physicalNetworkTrafficTypeDao.getNetworkTag(physicalNetworkId, Networks.TrafficType.Guest, hypervisorType); + return StringUtils.isNotBlank(tag) ? tag : String.valueOf(physicalNetworkId); + } + private void setNewHostResponseBase(HostJoinVO host, EnumSet details, HostResponse hostResponse) { hostResponse.setId(host.getUuid()); hostResponse.setCapabilities(host.getCapabilities()); @@ -283,7 +295,18 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail } else { hostResponse.setUefiCapability(new Boolean(false)); } - hostResponse.setVlanFilteringEnabled(Boolean.parseBoolean((String) hostDetails.get(Host.HOST_VLAN_FILTERING_ENABLED))); + Map vlanFilteringEnabledByPhysicalNetwork = hostDetails.entrySet().stream() + .filter(entry -> entry.getKey().startsWith(Host.HOST_VLAN_FILTERING_ENABLED_PREFIX)) + .collect(Collectors.toMap( + entry -> resolveGuestBridgeName( + Long.valueOf(entry.getKey().substring(Host.HOST_VLAN_FILTERING_ENABLED_PREFIX.length())), + host.getHypervisorType()), + entry -> Boolean.parseBoolean(entry.getValue()))); + hostResponse.setVlanFilteringEnabledByPhysicalNetwork(vlanFilteringEnabledByPhysicalNetwork); + // true only if vlan_filtering is enabled on every Guest physical network this host has reported - + // each one is independent, so a host with no reported physical networks is not "all enabled" + hostResponse.setVlanFilteringEnabled(!vlanFilteringEnabledByPhysicalNetwork.isEmpty() + && vlanFilteringEnabledByPhysicalNetwork.values().stream().allMatch(Boolean::booleanValue)); hostResponse.setVlanTrunkXmlSupported(Boolean.parseBoolean((String) hostDetails.get(Host.HOST_VLAN_TRUNK_XML_SUPPORTED))); } if (details.contains(HostDetails.all) && diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index b686f351adfc..4dc722c65ea0 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -153,6 +153,7 @@ import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.VirtualMachineProfile; import com.cloud.network.dao.NetworkDao; +import com.cloud.network.dao.NetworkVO; import com.cloud.vm.NicVO; import com.cloud.vm.dao.NicDao; import com.cloud.vm.dao.UserVmDao; @@ -489,26 +490,41 @@ private void avoidDifferentArchResources(VirtualMachineProfile vmProfile, DataCe @Override public void avoidHostsNotReadyForMultiNetworkNics(VirtualMachineProfile vmProfile, DataCenter dc, ExcludeList avoids) { List nics = _nicDao.listByVmId(vmProfile.getId()); - boolean needsFilteringHost = VlanTrunkMigrationHelper.hasTrunkNic(nics); - boolean needsLegacyHost = !needsFilteringHost && VlanTrunkMigrationHelper.hasNativeVlanGuestNic(nics, _networkDao); - if (!needsFilteringHost && !needsLegacyHost) { - return; - } - Set filteringHostIds = _hostDetailsDao.findHostIdsWithVlanFilteringEnabled(); - // without VLAN-filtering hosts a VLAN 1 nic is placeable anywhere - if (needsLegacyHost && filteringHostIds.isEmpty()) { - return; - } - List candidateHostIds = _hostDao.listEnabledIdsByDataCenterId(dc.getId()); - if (CollectionUtils.isEmpty(candidateHostIds)) { - return; + List candidateHostIds = null; + Set unsuitableHostIds = new HashSet<>(); + // checked per nic against its own network's physical network, not once for the whole VM - a host can + // have VLAN filtering enabled on one Guest physical network's bridge and not another + for (NicVO nic : nics) { + boolean needsFilteringHost = nic.getMultiNetwork(); + boolean needsLegacyHost = !needsFilteringHost && VlanTrunkMigrationHelper.hasNativeVlanGuestNic(Arrays.asList(nic), _networkDao); + if (!needsFilteringHost && !needsLegacyHost) { + continue; + } + NetworkVO network = _networkDao.findById(nic.getNetworkId()); + Long physicalNetworkId = network == null ? null : network.getPhysicalNetworkId(); + if (physicalNetworkId == null) { + continue; + } + Set filteringHostIds = _hostDetailsDao.findHostIdsWithVlanFilteringEnabled(physicalNetworkId); + // without VLAN-filtering hosts on this physical network a VLAN 1 nic on it is placeable anywhere + if (needsLegacyHost && filteringHostIds.isEmpty()) { + continue; + } + if (candidateHostIds == null) { + candidateHostIds = _hostDao.listEnabledIdsByDataCenterId(dc.getId()); + if (CollectionUtils.isEmpty(candidateHostIds)) { + return; + } + } + for (Long hostId : candidateHostIds) { + if (filteringHostIds.contains(hostId) ? needsLegacyHost : needsFilteringHost) { + unsuitableHostIds.add(hostId); + } + } } - List unsuitableHostIds = candidateHostIds.stream() - .filter(hostId -> filteringHostIds.contains(hostId) ? needsLegacyHost : needsFilteringHost) - .collect(Collectors.toList()); if (!unsuitableHostIds.isEmpty()) { logger.debug("Excluding {} host(s) unsuitable for the multi-VLAN trunk or VLAN 1 nics of VM [{}]", unsuitableHostIds.size(), vmProfile); - avoids.addHostList(unsuitableHostIds); + avoids.addHostList(new ArrayList<>(unsuitableHostIds)); } } diff --git a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java index 1702cac1620d..4512e41d9bbd 100644 --- a/server/src/main/java/com/cloud/network/NetworkServiceImpl.java +++ b/server/src/main/java/com/cloud/network/NetworkServiceImpl.java @@ -935,7 +935,7 @@ public Nic associateNetworkToNic(AssociateNetworkToNicCmd cmd) boolean vmRunning = vm.getState() == VirtualMachine.State.Running; if (vmRunning) { - checkHostReadyForVlanTrunk(vm); + checkHostReadyForVlanTrunk(vm, nic); } boolean nicWasAlreadyMultiNetwork = nic.getMultiNetwork(); @@ -1143,7 +1143,7 @@ public Nic disassociateNetworkFromNic(DisassociateNetworkFromNicCmd cmd) throws boolean vmRunning = vm.getState() == VirtualMachine.State.Running; if (vmRunning) { - checkHostReadyForVlanTrunk(vm); + checkHostReadyForVlanTrunk(vm, nic); } NetworkVO targetNetwork = _networksDao.findById(targetNetworkId); @@ -1310,13 +1310,11 @@ private void sendDhcpEntryCommandForAssociation(VirtualMachine vm, NicVO nic, Ne @Override public void reapplyLiveVlanMembership(VirtualMachine vm) throws ResourceUnavailableException { if (vm.getHostId() == null - || !VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(vm.getId(), vm.getHostId(), _nicDao, _hostDetailsDao)) { + || !VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(vm.getId(), vm.getHostId(), _nicDao, _networksDao, _hostDetailsDao)) { return; } - DetailVO vlanFilteringDetail = _hostDetailsDao.findDetail(vm.getHostId(), Host.HOST_VLAN_FILTERING_ENABLED); - boolean vlanFilteringEnabled = vlanFilteringDetail != null && Boolean.parseBoolean(vlanFilteringDetail.getValue()); for (NicVO nic : _nicDao.listByVmId(vm.getId())) { - if (VlanTrunkMigrationHelper.nicNeedsManualVlanMembership(nic, vlanFilteringEnabled)) { + if (VlanTrunkMigrationHelper.nicNeedsManualVlanMembership(nic, vm.getHostId(), _networksDao, _hostDetailsDao)) { updateLiveVlanTrunkMembership(vm, nic); } } @@ -1633,16 +1631,20 @@ public void doInTransactionWithoutResult(TransactionStatus status) { } } - private void checkHostReadyForVlanTrunk(VirtualMachine vm) { + private void checkHostReadyForVlanTrunk(VirtualMachine vm, NicVO nic) { if (vm.getHostId() == null) { throw new CloudRuntimeException(String.format("Instance %s is Running but has no host assigned", vm.getUuid())); } - DetailVO vlanFilteringDetail = _hostDetailsDao.findDetail(vm.getHostId(), Host.HOST_VLAN_FILTERING_ENABLED); + NetworkVO network = _networksDao.findById(nic.getNetworkId()); + Long physicalNetworkId = network == null ? null : network.getPhysicalNetworkId(); + DetailVO vlanFilteringDetail = physicalNetworkId == null ? null + : _hostDetailsDao.findDetail(vm.getHostId(), Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + physicalNetworkId); if (vlanFilteringDetail == null || !Boolean.parseBoolean(vlanFilteringDetail.getValue())) { HostVO host = _hostDao.findById(vm.getHostId()); throw new InvalidParameterValueException(String.format( - "Host %s does not have VLAN filtering enabled on its guest bridge, so a multi-VLAN trunk nic cannot be associated on this Instance while it is running", - host != null ? host.getUuid() : vm.getHostId())); + "Host %s does not have VLAN filtering enabled on the guest bridge for nic %s's physical network, " + + "so a multi-VLAN trunk nic cannot be associated on this Instance while it is running", + host != null ? host.getUuid() : vm.getHostId(), nic.getUuid())); } } diff --git a/server/src/main/java/com/cloud/server/ManagementServerImpl.java b/server/src/main/java/com/cloud/server/ManagementServerImpl.java index 89a1e5291a29..31b5228e30f7 100644 --- a/server/src/main/java/com/cloud/server/ManagementServerImpl.java +++ b/server/src/main/java/com/cloud/server/ManagementServerImpl.java @@ -1787,20 +1787,56 @@ public String getMultiNetworkNicUnsuitableReason(final VirtualMachine vm, final } @Override - public String getGuestBridgeStpMigrationCaution(final Host host) { - // a legacy host's guest nics use per-VLAN dynamic bridges instead, so stp on the shared bridge is irrelevant there - final DetailVO vlanFilteringDetail = _detailsDao.findDetail(host.getId(), Host.HOST_VLAN_FILTERING_ENABLED); - final boolean vlanFilteringEnabled = vlanFilteringDetail != null && Boolean.parseBoolean(vlanFilteringDetail.getValue()); - if (!vlanFilteringEnabled) { + public String getGuestBridgeStpMigrationCaution(final VirtualMachine vm, final Host host) { + final List nics = nicDao.listByVmId(vm.getId()); + final String stpCaution = getStpMigrationCaution(nics, host); + if (stpCaution != null) { + return stpCaution; + } + return getVlanTrunkXmlMigrationCaution(nics, host); + } + + // Checked per nic against its own network's physical network, not once for the whole host - a host can have + // STP enabled on one Guest physical network's bridge and not another, and STP only matters for a nic that + // actually rides that bridge (a trunk nic, or an ordinary nic whose bridge has vlan_filtering enabled). + private String getStpMigrationCaution(final List nics, final Host host) { + for (final NicVO nic : nics) { + if (nic.getBroadcastUri() == null || Networks.BroadcastDomainType.getSchemeValue(nic.getBroadcastUri()) != Networks.BroadcastDomainType.Vlan) { + continue; + } + final NetworkVO network = networkDao.findById(nic.getNetworkId()); + final Long physicalNetworkId = network == null ? null : network.getPhysicalNetworkId(); + if (physicalNetworkId == null) { + continue; + } + final DetailVO vlanFilteringDetail = _detailsDao.findDetail(host.getId(), Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + physicalNetworkId); + final boolean vlanFilteringEnabled = vlanFilteringDetail != null && Boolean.parseBoolean(vlanFilteringDetail.getValue()); + if (!(nic.getMultiNetwork() || vlanFilteringEnabled)) { + continue; + } + final DetailVO stpDetail = _detailsDao.findDetail(host.getId(), Host.HOST_GUEST_BRIDGE_STP_ENABLED_PREFIX + physicalNetworkId); + final boolean stpEnabled = stpDetail != null && Boolean.parseBoolean(stpDetail.getValue()); + if (stpEnabled) { + return "This host's guest bridge has STP enabled; a new NIC here (from this migration, a nic plug, or a VM start) " + + "will be unreachable for about the bridge's forward delay x2 while STP's listening/learning states run"; + } + } + return null; + } + + // Host-wide, not per-bridge: native VLAN trunk XML support depends on the host's libvirt version, not on + // which bridge a nic rides. + private String getVlanTrunkXmlMigrationCaution(final List nics, final Host host) { + if (!VlanTrunkMigrationHelper.hasTrunkNic(nics)) { return null; } - final DetailVO stpDetail = _detailsDao.findDetail(host.getId(), Host.HOST_GUEST_BRIDGE_STP_ENABLED); - final boolean stpEnabled = stpDetail != null && Boolean.parseBoolean(stpDetail.getValue()); - if (!stpEnabled) { + final DetailVO trunkXmlDetail = _detailsDao.findDetail(host.getId(), Host.HOST_VLAN_TRUNK_XML_SUPPORTED); + final boolean trunkXmlSupported = trunkXmlDetail != null && Boolean.parseBoolean(trunkXmlDetail.getValue()); + if (trunkXmlSupported) { return null; } - return "This host's guest bridge has STP enabled; a new NIC here (from this migration, a nic plug, or a VM start) " - + "will be unreachable for about the bridge's forward delay x2 while STP's listening/learning states run"; + return "This host's libvirt version does not support native VLAN trunk XML; the multi-VLAN trunk NIC's " + + "VLAN membership will be applied manually after migration instead"; } private void validateVgpuProfileForVmMigration(final VirtualMachineProfile vmProfile) { diff --git a/ui/public/locales/en.json b/ui/public/locales/en.json index 9391cd5ea272..281beac00926 100644 --- a/ui/public/locales/en.json +++ b/ui/public/locales/en.json @@ -1350,6 +1350,7 @@ "label.host.vddk.version": "VDDK Version", "label.host.tag": "Host tag", "label.host.vlanfilteringenabled": "VLAN filtering", +"label.host.vlanfilteringenabledbyphysicalnetwork": "VLAN filtering by physical network", "label.host.virtv2v.inplace.support": "Virt-v2v In-Place Support", "label.host.virtv2v.inplace.version": "Virt-v2v In-Place Version", "label.host.virtv2v.version": "Virt-v2v Version", diff --git a/ui/src/components/view/DetailsTab.vue b/ui/src/components/view/DetailsTab.vue index 80769fd883b1..d022531190b7 100644 --- a/ui/src/components/view/DetailsTab.vue +++ b/ui/src/components/view/DetailsTab.vue @@ -219,6 +219,15 @@ + +
+ {{ $t('label.host.vlanfilteringenabledbyphysicalnetwork') }} +
+
+ +
+
+
{{ $t('label.' + String(item).toLowerCase()) }} diff --git a/ui/src/components/view/ListView.vue b/ui/src/components/view/ListView.vue index 4a99b8b146b7..7ad61554f0fb 100644 --- a/ui/src/components/view/ListView.vue +++ b/ui/src/components/view/ListView.vue @@ -178,10 +178,10 @@ - +   - {{ $t('label.host.vlanfilteringenabled') }} + {{ vlanFilteringBadge(record).text }} @@ -1203,7 +1203,7 @@ export default { if (newData === oldData) return this.items.forEach(record => { this.resourceIdToValidLinksMap[record.id] = validateLinks(this.$router, false, record) - if (this.$route.path.startsWith('/host') && record.vlanfilteringenabled && record.zoneid) { + if (this.$route.path.startsWith('/host') && this.vlanFilteringBadge(record) && record.zoneid) { fetchMultiNetworkNicEnabledForZone(record.zoneid) } }) @@ -1545,6 +1545,18 @@ export default { var duration = Date.parse(enddate) - Date.parse(startdate) return (duration > 0 ? duration / 1000.0 : 0) + '' }, + vlanFilteringBadge (record) { + const byPhysicalNetwork = record.vlanfilteringenabledbyphysicalnetwork || {} + const total = Object.keys(byPhysicalNetwork).length + const enabled = Object.values(byPhysicalNetwork).filter(v => v).length + if (enabled === 0) { + return null + } + return { + text: enabled + '/' + total + ' ' + this.$t('label.host.vlanfilteringenabled'), + color: enabled === total ? 'blue' : 'orange' + } + }, getUsageTypes () { if (this.$route.path.split('/')[1] === 'usage') { getAPI('listUsageTypes').then(json => { diff --git a/ui/src/config/section/infra/hosts.js b/ui/src/config/section/infra/hosts.js index 74727d7f8ae8..3ff3e3c441cb 100644 --- a/ui/src/config/section/infra/hosts.js +++ b/ui/src/config/section/infra/hosts.js @@ -45,7 +45,7 @@ export default { fields.push('managementservername') return fields }, - details: ['name', 'id', 'resourcestate', 'ipaddress', 'hypervisor', 'externalprovisioner', 'arch', 'type', 'clustername', 'podname', 'zonename', 'vlanfilteringenabled', 'vlantrunkxmlsupported', 'lastpinged', 'storageaccessgroups', 'clusterstorageaccessgroups', 'podstorageaccessgroups', 'zonestorageaccessgroups', 'managementservername', 'disconnected', 'created', 'externaldetails'], + details: ['name', 'id', 'resourcestate', 'ipaddress', 'hypervisor', 'externalprovisioner', 'arch', 'type', 'clustername', 'podname', 'zonename', 'vlanfilteringenabled', 'vlanfilteringenabledbyphysicalnetwork', 'vlantrunkxmlsupported', 'lastpinged', 'storageaccessgroups', 'clusterstorageaccessgroups', 'podstorageaccessgroups', 'zonestorageaccessgroups', 'managementservername', 'disconnected', 'created', 'externaldetails'], tabs: [{ name: 'details', component: shallowRef(defineAsyncComponent(() => import('@/components/view/DetailsTab.vue'))) From 2b8fe811a9a937a3b10469dd7724a2d6d850cc35 Mon Sep 17 00:00:00 2001 From: Pearl1594 Date: Tue, 1 Dec 2026 16:18:29 -0500 Subject: [PATCH 2/2] update tests --- .../api/VlanTrunkMigrationHelperTest.java | 95 +++++++++---------- .../vm/VirtualMachineManagerImplTest.java | 18 +++- .../kvm/resource/BridgeVifDriverTest.java | 42 ++++---- .../LibvirtComputingResourceTest.java | 31 ------ ...LibvirtCheckNetworkCommandWrapperTest.java | 65 +++++++------ .../LibvirtMigrateCommandWrapperTest.java | 22 ++--- ...PrepareForMigrationCommandWrapperTest.java | 6 +- .../DeploymentPlanningManagerImplTest.java | 13 ++- .../cloud/network/NetworkServiceImplTest.java | 22 +++-- .../com/cloud/vm/UserVmManagerImplTest.java | 12 ++- 10 files changed, 162 insertions(+), 164 deletions(-) diff --git a/core/src/test/java/com/cloud/agent/api/VlanTrunkMigrationHelperTest.java b/core/src/test/java/com/cloud/agent/api/VlanTrunkMigrationHelperTest.java index b28f83624cfc..4c94aea492b7 100644 --- a/core/src/test/java/com/cloud/agent/api/VlanTrunkMigrationHelperTest.java +++ b/core/src/test/java/com/cloud/agent/api/VlanTrunkMigrationHelperTest.java @@ -57,6 +57,8 @@ public class VlanTrunkMigrationHelperTest { private static final long VM_ID = 1L; private static final long DEST_HOST_ID = 2L; + private static final long NETWORK_ID = 5L; + private static final long PHYSICAL_NETWORK_ID = 10L; private HostDetailsDao hostDetailsDao; private NicDao nicDao; @@ -80,13 +82,24 @@ private MigrateCommand newMigrateCommand() { return new MigrateCommand("i-2-3-VM", "10.0.0.2", false, vmTO, false); } + // multiNetwork nics still resolve a network/physical-network id in the current code (nicNeedsManualVlanMembership + // only skips that lookup for broadcast-URI-less nics), so this is stubbed regardless of the multiNetwork flag. private NicVO vlanNic(boolean multiNetwork) { NicVO nic = mock(NicVO.class); when(nic.getBroadcastUri()).thenReturn(Networks.BroadcastDomainType.Vlan.toUri(123)); when(nic.getMultiNetwork()).thenReturn(multiNetwork); + lenient().when(nic.getNetworkId()).thenReturn(NETWORK_ID); + NetworkVO network = mock(NetworkVO.class); + lenient().when(network.getPhysicalNetworkId()).thenReturn(PHYSICAL_NETWORK_ID); + lenient().when(networkDao.findById(NETWORK_ID)).thenReturn(network); return nic; } + private void givenHostVlanFiltering(boolean enabled) { + String key = Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + PHYSICAL_NETWORK_ID; + when(hostDetailsDao.findDetail(DEST_HOST_ID, key)).thenReturn(new DetailVO(DEST_HOST_ID, key, Boolean.toString(enabled))); + } + // -- populateVlanTrunkMigrationDetails -- @Test @@ -113,28 +126,28 @@ public void populateVlanTrunkMigrationDetailsLeavesNicBridgeMappingNullWhenEmpty } @Test - public void populateVlanTrunkMigrationDetailsParsesDestHostDetails() { + public void populateVlanTrunkMigrationDetailsCopiesNicVlanFilteringMappingAndParsesDestTrunkXmlSupport() { MigrateCommand migrateCommand = newMigrateCommand(); PrepareForMigrationAnswer answer = mock(PrepareForMigrationAnswer.class); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "true")); + Map nicVlanFilteringMapping = Collections.singletonMap("02:00:00:00:00:01", true); + when(answer.getNicVlanFilteringMapping()).thenReturn(nicVlanFilteringMapping); when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)) .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED, "false")); VlanTrunkMigrationHelper.populateVlanTrunkMigrationDetails(migrateCommand, answer, hostDetailsDao, DEST_HOST_ID); - assertTrue(migrateCommand.getDestVlanFilteringEnabled()); + assertEquals(nicVlanFilteringMapping, migrateCommand.getNicVlanFilteringMapping()); assertFalse(migrateCommand.getDestVlanTrunkXmlSupported()); } @Test - public void populateVlanTrunkMigrationDetailsLeavesFieldsNullWhenNoHostDetails() { + public void populateVlanTrunkMigrationDetailsLeavesFieldsEmptyOrNullWhenNothingProvided() { MigrateCommand migrateCommand = newMigrateCommand(); PrepareForMigrationAnswer answer = mock(PrepareForMigrationAnswer.class); VlanTrunkMigrationHelper.populateVlanTrunkMigrationDetails(migrateCommand, answer, hostDetailsDao, DEST_HOST_ID); - assertNull(migrateCommand.getDestVlanFilteringEnabled()); + assertTrue(migrateCommand.getNicVlanFilteringMapping().isEmpty()); assertNull(migrateCommand.getDestVlanTrunkXmlSupported()); } @@ -145,65 +158,57 @@ public void vmNeedsPostMigrationVlanTrunkMembershipFalseWhenDestSupportsTrunkXml when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)) .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED, "true")); - assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, hostDetailsDao)); + assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao)); verify(nicDao, never()).listByVmId(VM_ID); } @Test - public void vmNeedsPostMigrationVlanTrunkMembershipTrueForTrunkNicOnManualFallbackHostEvenWithoutDestFiltering() { + public void vmNeedsPostMigrationVlanTrunkMembershipTrueForTrunkNicRegardlessOfDestFiltering() { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "false")); List nics = Collections.singletonList(vlanNic(true)); when(nicDao.listByVmId(VM_ID)).thenReturn(nics); - assertTrue(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, hostDetailsDao)); + assertTrue(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao)); } @Test public void vmNeedsPostMigrationVlanTrunkMembershipTrueForOrdinaryVlanNicWhenDestFilteringEnabled() { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "true")); + givenHostVlanFiltering(true); List nics = Collections.singletonList(vlanNic(false)); when(nicDao.listByVmId(VM_ID)).thenReturn(nics); - assertTrue(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, hostDetailsDao)); + assertTrue(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao)); } @Test public void vmNeedsPostMigrationVlanTrunkMembershipFalseForOrdinaryVlanNicWhenDestNotFiltering() { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(null); List nics = Collections.singletonList(vlanNic(false)); when(nicDao.listByVmId(VM_ID)).thenReturn(nics); - assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, hostDetailsDao)); + assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao)); } @Test public void vmNeedsPostMigrationVlanTrunkMembershipIgnoresNicsWithoutBroadcastUri() { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "true")); NicVO nic = mock(NicVO.class); when(nic.getBroadcastUri()).thenReturn(null); when(nicDao.listByVmId(VM_ID)).thenReturn(Collections.singletonList(nic)); - assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, hostDetailsDao)); + assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao)); } @Test public void vmNeedsPostMigrationVlanTrunkMembershipIgnoresNonVlanBroadcastType() { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "true")); NicVO nic = mock(NicVO.class); URI vxlanUri = Networks.BroadcastDomainType.Vxlan.toUri(555); when(nic.getBroadcastUri()).thenReturn(vxlanUri); when(nicDao.listByVmId(VM_ID)).thenReturn(Collections.singletonList(nic)); - assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, hostDetailsDao)); + assertFalse(VlanTrunkMigrationHelper.vmNeedsPostMigrationVlanTrunkMembership(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao)); } // -- sendPostMigrationVlanTrunkMembershipIfNeeded -- @@ -213,7 +218,7 @@ public void sendPostMigrationVlanTrunkMembershipIfNeededNoOpWhenNotNeeded() thro when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)) .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED, "true")); - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, hostDetailsDao, commandSender, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao, commandSender, logger); verify(commandSender, never()).send(any(), any()); } @@ -221,15 +226,13 @@ public void sendPostMigrationVlanTrunkMembershipIfNeededNoOpWhenNotNeeded() thro @Test public void sendPostMigrationVlanTrunkMembershipIfNeededSendsAndLogsNothingOnSuccess() throws Exception { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "false")); List trunkNics = Collections.singletonList(vlanNic(true)); when(nicDao.listByVmId(VM_ID)).thenReturn(trunkNics); Answer successAnswer = mock(Answer.class); when(successAnswer.getResult()).thenReturn(true); when(commandSender.send(eq(DEST_HOST_ID), any(PostMigrationCommand.class))).thenReturn(successAnswer); - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, hostDetailsDao, commandSender, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao, commandSender, logger); verify(commandSender, times(1)).send(eq(DEST_HOST_ID), any(PostMigrationCommand.class)); verify(logger, never()).warn(any(String.class), any(Object.class), any(Object.class), any(Object.class)); @@ -238,13 +241,11 @@ public void sendPostMigrationVlanTrunkMembershipIfNeededSendsAndLogsNothingOnSuc @Test public void sendPostMigrationVlanTrunkMembershipIfNeededWarnsOnNullAnswer() throws Exception { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "false")); List trunkNics = Collections.singletonList(vlanNic(true)); when(nicDao.listByVmId(VM_ID)).thenReturn(trunkNics); when(commandSender.send(eq(DEST_HOST_ID), any(PostMigrationCommand.class))).thenReturn(null); - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, hostDetailsDao, commandSender, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao, commandSender, logger); verify(logger, times(1)).warn(any(String.class), eq(vmTO), eq(DEST_HOST_ID), any()); } @@ -252,8 +253,6 @@ public void sendPostMigrationVlanTrunkMembershipIfNeededWarnsOnNullAnswer() thro @Test public void sendPostMigrationVlanTrunkMembershipIfNeededWarnsOnFailedAnswer() throws Exception { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "false")); List trunkNics = Collections.singletonList(vlanNic(true)); when(nicDao.listByVmId(VM_ID)).thenReturn(trunkNics); Answer failedAnswer = mock(Answer.class); @@ -261,7 +260,7 @@ public void sendPostMigrationVlanTrunkMembershipIfNeededWarnsOnFailedAnswer() th when(failedAnswer.getDetails()).thenReturn("bridge vlan add failed"); when(commandSender.send(eq(DEST_HOST_ID), any(PostMigrationCommand.class))).thenReturn(failedAnswer); - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, hostDetailsDao, commandSender, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao, commandSender, logger); verify(logger, times(1)).warn(any(String.class), eq(vmTO), eq(DEST_HOST_ID), eq("bridge vlan add failed")); } @@ -269,14 +268,12 @@ public void sendPostMigrationVlanTrunkMembershipIfNeededWarnsOnFailedAnswer() th @Test public void sendPostMigrationVlanTrunkMembershipIfNeededWarnsOnSendException() throws Exception { when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, "false")); List trunkNics = Collections.singletonList(vlanNic(true)); when(nicDao.listByVmId(VM_ID)).thenReturn(trunkNics); when(commandSender.send(eq(DEST_HOST_ID), any(PostMigrationCommand.class))) .thenThrow(new AgentUnavailableException("agent down", DEST_HOST_ID)); - VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, hostDetailsDao, commandSender, logger); + VlanTrunkMigrationHelper.sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao, commandSender, logger); verify(logger, times(1)).warn(any(String.class), eq(vmTO), eq(DEST_HOST_ID), any(String.class), any(AgentUnavailableException.class)); } @@ -287,25 +284,20 @@ private NicVO nicOnVlan(int vlan, boolean multiNetwork) { return nicOnVlan(vlan, multiNetwork, Networks.TrafficType.Guest); } + // The physical network is resolved for every nic (not just non-trunk ones), so it is stubbed unconditionally here. private NicVO nicOnVlan(int vlan, boolean multiNetwork, Networks.TrafficType trafficType) { URI broadcastUri = Networks.BroadcastDomainType.Vlan.toUri(vlan); NicVO nic = mock(NicVO.class); when(nic.getBroadcastUri()).thenReturn(broadcastUri); when(nic.getMultiNetwork()).thenReturn(multiNetwork); - if (!multiNetwork) { - lenient().when(nic.getNetworkId()).thenReturn(5L); - NetworkVO network = mock(NetworkVO.class); - lenient().when(network.getTrafficType()).thenReturn(trafficType); - lenient().when(networkDao.findById(5L)).thenReturn(network); - } + lenient().when(nic.getNetworkId()).thenReturn(NETWORK_ID); + NetworkVO network = mock(NetworkVO.class); + lenient().when(network.getTrafficType()).thenReturn(trafficType); + lenient().when(network.getPhysicalNetworkId()).thenReturn(PHYSICAL_NETWORK_ID); + lenient().when(networkDao.findById(NETWORK_ID)).thenReturn(network); return nic; } - private void givenHostVlanFiltering(boolean enabled) { - when(hostDetailsDao.findDetail(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED)) - .thenReturn(new DetailVO(DEST_HOST_ID, Host.HOST_VLAN_FILTERING_ENABLED, Boolean.toString(enabled))); - } - @Test public void getVlanFilteringUnsuitableReasonRejectsVlanOneNicOnFilteringHost() { List nics = Collections.singletonList(nicOnVlan(1, false)); @@ -344,7 +336,7 @@ public void getVlanFilteringUnsuitableReasonRejectsTrunkNicOnLegacyHost() { @Test public void getVlanFilteringUnsuitableReasonUsesNetworkVlanForStoppedNicWithoutBroadcastUri() { NicVO nic = mock(NicVO.class); - when(nic.getNetworkId()).thenReturn(5L); + when(nic.getNetworkId()).thenReturn(NETWORK_ID); when(nic.getBroadcastUri()).thenReturn(null); List nics = Collections.singletonList(nic); when(nicDao.listByVmId(VM_ID)).thenReturn(nics); @@ -352,7 +344,8 @@ public void getVlanFilteringUnsuitableReasonUsesNetworkVlanForStoppedNicWithoutB URI vlanOne = Networks.BroadcastDomainType.Vlan.toUri(1); when(network.getBroadcastUri()).thenReturn(vlanOne); when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest); - when(networkDao.findById(5L)).thenReturn(network); + when(network.getPhysicalNetworkId()).thenReturn(PHYSICAL_NETWORK_ID); + when(networkDao.findById(NETWORK_ID)).thenReturn(network); givenHostVlanFiltering(true); assertTrue(VlanTrunkMigrationHelper.getVlanFilteringUnsuitableReason(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao).contains("VLAN 1")); @@ -364,6 +357,7 @@ public void getNativeVlanUnsuitableReasonRejectsVlanOneNetworkOnFilteringHostOnl URI vlanOne = Networks.BroadcastDomainType.Vlan.toUri(1); when(network.getBroadcastUri()).thenReturn(vlanOne); when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest); + when(network.getPhysicalNetworkId()).thenReturn(PHYSICAL_NETWORK_ID); givenHostVlanFiltering(true); assertTrue(VlanTrunkMigrationHelper.getNativeVlanUnsuitableReason(network, DEST_HOST_ID, hostDetailsDao).contains("VLAN 1")); @@ -413,12 +407,11 @@ public void hasTrunkNicDetectsMultiNetworkNic() { } @Test - public void getVlanFilteringUnsuitableReasonDoesNotLookUpNetworksOrHostForNicsOnOtherVlans() { + public void getVlanFilteringUnsuitableReasonReturnsNullForNicOnOtherVlanRegardlessOfHostFiltering() { List nics = Collections.singletonList(nicOnVlan(100, false)); when(nicDao.listByVmId(VM_ID)).thenReturn(nics); + givenHostVlanFiltering(true); assertNull(VlanTrunkMigrationHelper.getVlanFilteringUnsuitableReason(VM_ID, DEST_HOST_ID, nicDao, networkDao, hostDetailsDao)); - verify(networkDao, never()).findById(any(Long.class)); - verify(hostDetailsDao, never()).findDetail(any(Long.class), any(String.class)); } } diff --git a/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java b/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java index fe51440bd81b..79aa7d1754f2 100644 --- a/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java +++ b/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java @@ -1352,17 +1352,22 @@ public void testOrchestrateStartExcludesHostWithoutVlanFilteringForMultiNetworkN NicVO trunkNic = mock(NicVO.class); when(trunkNic.getMultiNetwork()).thenReturn(true); + when(trunkNic.getNetworkId()).thenReturn(5L); when(_nicsDao.listByVmId(vmInstance.getId())).thenReturn(java.util.Collections.singletonList(trunkNic)); + NetworkVO trunkNicNetwork = mock(NetworkVO.class); + when(trunkNicNetwork.getPhysicalNetworkId()).thenReturn(10L); + when(networkDao.findById(5L)).thenReturn(trunkNicNetwork); + Host notReadyHost = mock(Host.class); when(notReadyHost.getId()).thenReturn(1L); - when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(null); + when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 10L)).thenReturn(null); Host readyHost = mock(Host.class); when(readyHost.getId()).thenReturn(2L); DetailVO readyDetail = mock(DetailVO.class); when(readyDetail.getValue()).thenReturn("true"); - when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(readyDetail); + when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 10L)).thenReturn(readyDetail); Pod destPod = mock(Pod.class); when(destPod.getId()).thenReturn(2L); @@ -1413,7 +1418,7 @@ public void testOrchestrateStartExcludesHostWithoutVlanFilteringForMultiNetworkN // other orchestrateStart tests confirms execution got past the readiness check this time) verify(_dpMgr, Mockito.times(2)).planDeployment(any(VirtualMachineProfileImpl.class), any(DataCenterDeployment.class), any(ExcludeList.class), any(DeploymentPlanner.class)); verify(_stateMachine, Mockito.never()).transitTo(vmInstance, VirtualMachine.Event.OperationRetry, new Pair(vmInstance.getHostId(), 1L), vmInstanceDaoMock); - verify(hostDetailsDao, Mockito.atLeastOnce()).findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED); + verify(hostDetailsDao, Mockito.atLeastOnce()).findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 10L); verify(_stateMachine, Mockito.atLeastOnce()).transitTo(vmInstance, VirtualMachine.Event.OperationRetry, new Pair(vmInstance.getHostId(), 2L), vmInstanceDaoMock); assertEquals(vmInstance.getPodIdToDeployIn(), (Long) destPod.getId()); } @@ -1444,7 +1449,6 @@ public void vmNeedsPostMigrationVlanTrunkMembershipFalseWhenDestSupportsTrunkXml @Test public void vmNeedsPostMigrationVlanTrunkMembershipTrueForTrunkNicOnNonTrunkXmlDest() { when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(null); NicVO trunkNic = mockVlanNic(100, true); when(_nicsDao.listByVmId(1L)).thenReturn(java.util.Collections.singletonList(trunkNic)); @@ -1454,8 +1458,12 @@ public void vmNeedsPostMigrationVlanTrunkMembershipTrueForTrunkNicOnNonTrunkXmlD @Test public void vmNeedsPostMigrationVlanTrunkMembershipFalseForOrdinaryNicWithoutVlanFilteringDest() { when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_TRUNK_XML_SUPPORTED)).thenReturn(null); - when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(null); NicVO ordinaryNic = mockVlanNic(100, false); + when(ordinaryNic.getNetworkId()).thenReturn(5L); + NetworkVO network = mock(NetworkVO.class); + when(network.getPhysicalNetworkId()).thenReturn(10L); + when(networkDao.findById(5L)).thenReturn(network); + when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 10L)).thenReturn(null); when(_nicsDao.listByVmId(1L)).thenReturn(java.util.Collections.singletonList(ordinaryNic)); Assert.assertFalse(virtualMachineManagerImpl.vmNeedsPostMigrationVlanTrunkMembership(mockVmWithId(1L), 2L)); diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriverTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriverTest.java index 8a63adb5b879..8d4dac9f0a67 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriverTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriverTest.java @@ -147,7 +147,7 @@ private NicTO buildTrunkNic(int primaryVlan, List associatedVlans) { @Test public void plugTrunkVlanNicOnOldLibvirtSkipsXmlAndLeavesManualMembershipToPostAttachHook() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(false); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); NicTO nic = buildTrunkNic(100, Collections.singletonList(200)); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); @@ -159,7 +159,7 @@ public void plugTrunkVlanNicOnOldLibvirtSkipsXmlAndLeavesManualMembershipToPostA @Test(expected = InternalErrorException.class) public void plugTrunkVlanNicFailsWhenVlanFilteringNotEnabled() throws InternalErrorException { - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(false); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(false); NicTO nic = buildTrunkNic(100, Collections.singletonList(200)); driver.plugTrunkVlanNic(new LibvirtVMDef.InterfaceDef(), nic, null, null, null, 0); } @@ -193,10 +193,10 @@ public void usesSharedVlanAwareBridgeTrueOnlyForVlanNicOnFilteringReadyHost() { NicTO nic = new NicTO(); nic.setBroadcastType(Networks.BroadcastDomainType.Vlan); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); Assert.assertTrue(driver.usesSharedVlanAwareBridge(nic)); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(false); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(false); Assert.assertFalse(driver.usesSharedVlanAwareBridge(nic)); nic.setBroadcastType(Networks.BroadcastDomainType.Vxlan); @@ -205,7 +205,6 @@ public void usesSharedVlanAwareBridgeTrueOnlyForVlanNicOnFilteringReadyHost() { @Test public void ensureVlanTrunkMembershipSkipsUntaggedOrdinaryNicOnFilteringReadyHost() throws InternalErrorException { - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); NicTO nic = new NicTO(); nic.setBroadcastType(Networks.BroadcastDomainType.Vlan); nic.setBroadcastUri(URI.create("vlan://untagged")); @@ -222,7 +221,7 @@ public void ensureVlanTrunkMembershipSkipsUntaggedOrdinaryNicOnFilteringReadyHos @Test public void ensureVlanTrunkMembershipAppliesNativeVlanForOrdinaryNicOnFilteringReadyHost() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(false); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); NicTO nic = buildTrunkNic(100, null); nic.setTrunkVlan(false); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); @@ -236,7 +235,7 @@ public void ensureVlanTrunkMembershipAppliesNativeVlanForOrdinaryNicOnFilteringR @Test public void ensureVlanTrunkMembershipNoOpsForOrdinaryNicWhenHostNotFilteringReady() throws InternalErrorException { - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(false); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(false); NicTO nic = buildTrunkNic(100, null); nic.setTrunkVlan(false); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); @@ -281,10 +280,11 @@ public void ensureVlanTrunkMembershipRemovesDefaultVlanOneOnOldLibvirt() throws @Test public void updateVlanTrunkMembershipRestoresOriginalMembershipWhenLaterAddFailsOnOldLibvirt() throws Exception { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(false); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); NicTO nic = buildTrunkNic(100, java.util.Arrays.asList(200, 300)); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); + intf.defBridgeNet(BRIDGE_NAME, null, nic.getMac(), LibvirtVMDef.InterfaceDef.NicModel.VIRTIO); intf.setDevName("vnet5"); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); Map original = new java.util.LinkedHashMap<>(); original.put(100, true); Map afterPartialApply = new java.util.LinkedHashMap<>(original); @@ -314,10 +314,11 @@ public void ensureVlanTrunkMembershipFailsWhenTapNameUnknown() throws InternalEr @Test public void updateVlanTrunkMembershipUpdatesXmlOnModernLibvirt() throws Exception { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); NicTO nic = buildTrunkNic(100, Collections.singletonList(200)); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); + intf.defBridgeNet(BRIDGE_NAME, null, nic.getMac(), LibvirtVMDef.InterfaceDef.NicModel.VIRTIO); intf.setDevName("vnet5"); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); org.libvirt.Domain vm = Mockito.mock(org.libvirt.Domain.class); driver.updateVlanTrunkMembership(vm, intf, nic); @@ -329,10 +330,11 @@ public void updateVlanTrunkMembershipUpdatesXmlOnModernLibvirt() throws Exceptio @Test public void updateVlanTrunkMembershipAppliesDiffOnOldLibvirt() throws Exception { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(false); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); NicTO nic = buildTrunkNic(100, Collections.singletonList(200)); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); + intf.defBridgeNet(BRIDGE_NAME, null, nic.getMac(), LibvirtVMDef.InterfaceDef.NicModel.VIRTIO); intf.setDevName("vnet5"); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); org.libvirt.Domain vm = Mockito.mock(org.libvirt.Domain.class); Map currentMembership = new HashMap<>(); currentMembership.put(100, true); @@ -350,7 +352,7 @@ public void updateVlanTrunkMembershipAppliesDiffOnOldLibvirt() throws Exception @Test public void updateVlanTrunkMembershipChecksUplinkMembershipForTheResolvedPif() throws Exception { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); Mockito.doReturn(Collections.emptyMap()).when(driver).readCurrentVlanMembership(Mockito.anyString()); NicTO nic = buildTrunkNic(100, Collections.singletonList(200)); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); @@ -368,7 +370,6 @@ public void updateVlanTrunkMembershipChecksUplinkMembershipForTheResolvedPif() t public void updateVlanTrunkMembershipFailsForNonTrunkNic() throws Exception { NicTO nic = new NicTO(); nic.setBroadcastType(Networks.BroadcastDomainType.Vlan); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(false); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); org.libvirt.Domain vm = Mockito.mock(org.libvirt.Domain.class); @@ -385,7 +386,7 @@ public void plugTrunkVlanNicFailsForNonVlanPrimaryBroadcastType() throws Interna @Test(expected = InternalErrorException.class) public void plugTrunkVlanNicFailsForNonVlanAssociatedNetwork() throws InternalErrorException { - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); NicTO nic = buildTrunkNic(100, null); NetworkTO badAssociation = new NetworkTO(); badAssociation.setBroadcastType(Networks.BroadcastDomainType.Vxlan); @@ -396,7 +397,7 @@ public void plugTrunkVlanNicFailsForNonVlanAssociatedNetwork() throws InternalEr @Test public void plugTrunkVlanNicBuildsInterfaceWithAllVlanTagsOnGuestBridge() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); NicTO nic = buildTrunkNic(100, java.util.Arrays.asList(200, 300)); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); @@ -410,7 +411,7 @@ public void plugTrunkVlanNicBuildsInterfaceWithAllVlanTagsOnGuestBridge() throws @Test public void plugTrunkVlanNicFailsClearlyWhenAssociatedNetworkHasNoVlanYet() { - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); NicTO nic = buildTrunkNic(100, null); NetworkTO notYetImplemented = new NetworkTO(); notYetImplemented.setBroadcastType(Networks.BroadcastDomainType.Vlan); @@ -429,7 +430,7 @@ public void plugTrunkVlanNicFailsClearlyWhenAssociatedNetworkHasNoVlanYet() { @Test public void plugTrunkVlanNicUsesTrafficLabelBridgeWhenPresent() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled("customLabel")).thenReturn(true); NicTO nic = buildTrunkNic(100, null); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); @@ -443,7 +444,8 @@ public void plugTrunkVlanNicUsesTrafficLabelBridgeWhenPresent() throws InternalE @Test public void plugTrunkVlanNicNeverModifiesTheUplinkItself() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled("customLabel")).thenReturn(true); Mockito.doReturn(Collections.emptyMap()).when(driver).readCurrentVlanMembership(Mockito.anyString()); driver.plugTrunkVlanNic(new LibvirtVMDef.InterfaceDef(), buildTrunkNic(100, null), null, null, null, 0); @@ -458,7 +460,7 @@ public void plugTrunkVlanNicNeverModifiesTheUplinkItself() throws InternalErrorE @Test public void plugTrunkVlanNicChecksUplinkMembershipForTheResolvedPif() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); Mockito.doReturn(Collections.emptyMap()).when(driver).readCurrentVlanMembership(Mockito.anyString()); driver.plugTrunkVlanNic(new LibvirtVMDef.InterfaceDef(), buildTrunkNic(100, Collections.singletonList(200)), null, null, null, 0); @@ -469,7 +471,7 @@ public void plugTrunkVlanNicChecksUplinkMembershipForTheResolvedPif() throws Int @Test public void plugTrunkVlanNicSucceedsWhenUplinkMembershipCannotBeRead() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled(BRIDGE_NAME)).thenReturn(true); Mockito.doThrow(new InternalErrorException("bridge command failed")).when(driver).readCurrentVlanMembership(Mockito.anyString()); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); @@ -482,7 +484,7 @@ public void plugTrunkVlanNicSucceedsWhenUplinkMembershipCannotBeRead() throws In @Test public void plugTrunkVlanNicSucceedsWhenUplinkPifUnknownForBridge() throws InternalErrorException { Mockito.when(libvirtComputingResource.hostSupportsVlanTrunkXml()).thenReturn(true); - Mockito.when(libvirtComputingResource.hostSupportsVlanFiltering()).thenReturn(true); + Mockito.when(libvirtComputingResource.isBridgeVlanFilteringEnabled("unknownLabel")).thenReturn(true); NicTO nic = buildTrunkNic(100, null); LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef(); diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index 49ac50a9f599..75060cd2cd57 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -7782,35 +7782,4 @@ public void getPeriodAndQuotaTestPeriodIsEqualToMaximumWhenRequired() { Assert.assertEquals(expectedResult, result); } - // getEffectiveGuestBridgeName() must mirror BridgeVifDriver#plugTrunkVlanNic's resolution - - @Test - public void getEffectiveGuestBridgeNameFallsBackToGuestNetworkDeviceWhenNoLabelReported() { - libvirtComputingResourceSpy.guestBridgeName = "cloudbr1"; - Assert.assertEquals("cloudbr1", libvirtComputingResourceSpy.getEffectiveGuestBridgeName()); - } - - @Test - public void getEffectiveGuestBridgeNamePrefersGuestTrafficLabelOverGuestNetworkDeviceWhenTheyDiffer() { - libvirtComputingResourceSpy.guestBridgeName = "cloudbr1"; - libvirtComputingResourceSpy.setGuestTrafficLabel("cloudbr2"); - Assert.assertEquals("cloudbr2", libvirtComputingResourceSpy.getEffectiveGuestBridgeName()); - } - - @Test - public void getEffectiveGuestBridgeNameRevertsToGuestNetworkDeviceOnceLabelIsCleared() { - libvirtComputingResourceSpy.guestBridgeName = "cloudbr1"; - libvirtComputingResourceSpy.setGuestTrafficLabel("cloudbr2"); - Assert.assertEquals("cloudbr2", libvirtComputingResourceSpy.getEffectiveGuestBridgeName()); - - libvirtComputingResourceSpy.setGuestTrafficLabel(null); - Assert.assertEquals("cloudbr1", libvirtComputingResourceSpy.getEffectiveGuestBridgeName()); - } - - @Test - public void getEffectiveGuestBridgeNameTreatsBlankLabelAsNoLabel() { - libvirtComputingResourceSpy.guestBridgeName = "cloudbr1"; - libvirtComputingResourceSpy.setGuestTrafficLabel(" "); - Assert.assertEquals("cloudbr1", libvirtComputingResourceSpy.getEffectiveGuestBridgeName()); - } } diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapperTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapperTest.java index 24d65821ac13..286a86342694 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapperTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckNetworkCommandWrapperTest.java @@ -17,12 +17,16 @@ package com.cloud.hypervisor.kvm.resource.wrapper; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.verify; +import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; @@ -30,11 +34,13 @@ import org.mockito.junit.MockitoJUnitRunner; import com.cloud.agent.api.Answer; +import com.cloud.agent.api.CheckNetworkAnswer; import com.cloud.agent.api.CheckNetworkCommand; import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; import com.cloud.network.PhysicalNetworkSetupInfo; -// Covers the guest traffic label tracking that getEffectiveGuestBridgeName() relies on +// Covers reporting vlan_filtering/STP per Guest physical network, not once for the whole host - a host can +// have filtering enabled on one physical network's bridge and not another. @RunWith(MockitoJUnitRunner.class) public class LibvirtCheckNetworkCommandWrapperTest { @@ -43,56 +49,55 @@ public class LibvirtCheckNetworkCommandWrapperTest { private final LibvirtCheckNetworkCommandWrapper wrapper = new LibvirtCheckNetworkCommandWrapper(); - private PhysicalNetworkSetupInfo infoWithGuestLabel(String guestLabel) { + private PhysicalNetworkSetupInfo physicalNetworkInfo(Long physicalNetworkId, String guestLabel) { PhysicalNetworkSetupInfo info = new PhysicalNetworkSetupInfo(); + info.setPhysicalNetworkId(physicalNetworkId); info.setGuestNetworkName(guestLabel); return info; } @Test - public void setsGuestTrafficLabelWhenPhysicalNetworkCarriesOne() { - when(libvirtComputingResource.checkNetwork(org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any())).thenReturn(true); - List phyNics = Collections.singletonList(infoWithGuestLabel("cloudbr1")); + public void reportsVlanFilteringAndStpPerPhysicalNetwork() { + when(libvirtComputingResource.checkNetwork(any(), any())).thenReturn(true); + when(libvirtComputingResource.isBridgeVlanFilteringEnabled("cloudbr1")).thenReturn(false); + when(libvirtComputingResource.isBridgeVlanFilteringEnabled("cloudbr2")).thenReturn(true); + when(libvirtComputingResource.isBridgeStpEnabled("cloudbr1")).thenReturn(true); + when(libvirtComputingResource.isBridgeStpEnabled("cloudbr2")).thenReturn(false); + List phyNics = Arrays.asList(physicalNetworkInfo(201L, "cloudbr1"), physicalNetworkInfo(205L, "cloudbr2")); CheckNetworkCommand command = new CheckNetworkCommand(phyNics); Answer answer = wrapper.execute(command, libvirtComputingResource); assertTrue(answer.getResult()); - verify(libvirtComputingResource).setGuestTrafficLabel("cloudbr1"); + CheckNetworkAnswer checkNetworkAnswer = (CheckNetworkAnswer) answer; + Map vlanFiltering = checkNetworkAnswer.getVlanFilteringEnabledByPhysicalNetwork(); + assertEquals(false, vlanFiltering.get(201L)); + assertEquals(true, vlanFiltering.get(205L)); + Map stp = checkNetworkAnswer.getStpEnabledByPhysicalNetwork(); + assertEquals(true, stp.get(201L)); + assertEquals(false, stp.get(205L)); } @Test - public void clearsGuestTrafficLabelWhenNoPhysicalNetworkCarriesOne() { - when(libvirtComputingResource.checkNetwork(org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any())).thenReturn(true); - List phyNics = Collections.singletonList(infoWithGuestLabel(null)); + public void skipsPhysicalNetworksWithNoGuestLabelOrNoPhysicalNetworkId() { + when(libvirtComputingResource.checkNetwork(any(), any())).thenReturn(true); + List phyNics = Arrays.asList(physicalNetworkInfo(null, "cloudbr1"), physicalNetworkInfo(205L, null)); CheckNetworkCommand command = new CheckNetworkCommand(phyNics); Answer answer = wrapper.execute(command, libvirtComputingResource); - assertTrue(answer.getResult()); - verify(libvirtComputingResource).setGuestTrafficLabel(null); - } - - @Test - public void revertsGuestTrafficLabelOnceOperatorRemovesIt() { - when(libvirtComputingResource.checkNetwork(org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any())).thenReturn(true); - - wrapper.execute(new CheckNetworkCommand(Collections.singletonList(infoWithGuestLabel("cloudbr1"))), libvirtComputingResource); - verify(libvirtComputingResource).setGuestTrafficLabel("cloudbr1"); - - // Operator removes the label on a later connect/reconnect - the resource must be told, not left stale. - wrapper.execute(new CheckNetworkCommand(Collections.singletonList(infoWithGuestLabel(null))), libvirtComputingResource); - verify(libvirtComputingResource).setGuestTrafficLabel(null); + CheckNetworkAnswer checkNetworkAnswer = (CheckNetworkAnswer) answer; + assertTrue(checkNetworkAnswer.getVlanFilteringEnabledByPhysicalNetwork().isEmpty()); + assertTrue(checkNetworkAnswer.getStpEnabledByPhysicalNetwork().isEmpty()); } @Test - public void blankGuestNetworkNameDoesNotOverrideAnEarlierRealLabelInTheSameConnect() { - when(libvirtComputingResource.checkNetwork(org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any())).thenReturn(true); - List phyNics = java.util.Arrays.asList(infoWithGuestLabel("cloudbr1"), infoWithGuestLabel("")); - CheckNetworkCommand command = new CheckNetworkCommand(phyNics); + public void doesNotReportCapabilitiesWhenANetworkIsMissing() { + when(libvirtComputingResource.checkNetwork(any(), any())).thenReturn(false); + CheckNetworkCommand command = new CheckNetworkCommand(Collections.singletonList(physicalNetworkInfo(201L, "cloudbr1"))); - wrapper.execute(command, libvirtComputingResource); + Answer answer = wrapper.execute(command, libvirtComputingResource); - verify(libvirtComputingResource).setGuestTrafficLabel("cloudbr1"); + assertFalse(answer.getResult()); } } diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapperTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapperTest.java index 235002a2d45c..a4ff08be2333 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapperTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapperTest.java @@ -998,7 +998,8 @@ public void replaceVlanTrunkInterfacesLeavesOrdinaryVlanNicUntouchedOnNonFilteri NicTO nic = buildVlanNicTO(false, 100, null); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(false).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); + Mockito.doReturn("cloudbr0").when(libvirtComputingResourceMock).getGuestBridgeName(); + Mockito.doReturn(false).when(libvirtComputingResourceMock).isBridgeVlanFilteringEnabled("cloudbr0"); String result = libvirtMigrateCmdWrapper.replaceVlanTrunkInterfaces(vlanNicDomainXml, migrateCommandMock, libvirtComputingResourceMock); @@ -1013,8 +1014,9 @@ public void replaceVlanTrunkInterfacesRewritesOrdinaryNicBridgeWhenOnlyDestIsFil NicTO nic = buildVlanNicTO(false, 100, null); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(false).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); - Mockito.doReturn(true).when(migrateCommandMock).getDestVlanFilteringEnabled(); + Mockito.doReturn("cloudbr0").when(libvirtComputingResourceMock).getGuestBridgeName(); + Mockito.doReturn(false).when(libvirtComputingResourceMock).isBridgeVlanFilteringEnabled("cloudbr0"); + Mockito.doReturn(Map.of(vlanNicMac, true)).when(migrateCommandMock).getNicVlanFilteringMapping(); Mockito.doReturn(false).when(migrateCommandMock).getDestVlanTrunkXmlSupported(); Mockito.doReturn(Map.of(vlanNicMac, "destcloudbr0")).when(migrateCommandMock).getNicBridgeMapping(); @@ -1029,8 +1031,7 @@ public void replaceVlanTrunkInterfacesInsertsTrunkVlanBlockWhenDestSupportsTrunk NicTO nic = buildVlanNicTO(true, 100, Collections.singletonList(200)); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(true).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); - Mockito.doReturn(true).when(migrateCommandMock).getDestVlanFilteringEnabled(); + Mockito.doReturn(Map.of(vlanNicMac, true)).when(migrateCommandMock).getNicVlanFilteringMapping(); Mockito.doReturn(true).when(migrateCommandMock).getDestVlanTrunkXmlSupported(); Mockito.doReturn(Map.of(vlanNicMac, "destcloudbr0")).when(migrateCommandMock).getNicBridgeMapping(); @@ -1050,8 +1051,7 @@ public void replaceVlanTrunkInterfacesSkipsTrunkVlanBlockForUntaggedOrdinaryNicW nic.setBroadcastUri(java.net.URI.create("vlan://untagged")); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(true).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); - Mockito.doReturn(true).when(migrateCommandMock).getDestVlanFilteringEnabled(); + Mockito.doReturn(Map.of(vlanNicMac, true)).when(migrateCommandMock).getNicVlanFilteringMapping(); Mockito.doReturn(true).when(migrateCommandMock).getDestVlanTrunkXmlSupported(); Mockito.doReturn(Map.of(vlanNicMac, "destcloudbr0")).when(migrateCommandMock).getNicBridgeMapping(); @@ -1068,7 +1068,8 @@ public void replaceVlanTrunkInterfacesRefusesVlanNicWithoutBroadcastUriOnFilteri nic.setBroadcastUri(null); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(true).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); + Mockito.doReturn("cloudbr0").when(libvirtComputingResourceMock).getGuestBridgeName(); + Mockito.doReturn(true).when(libvirtComputingResourceMock).isBridgeVlanFilteringEnabled("cloudbr0"); libvirtMigrateCmdWrapper.replaceVlanTrunkInterfaces(vlanNicDomainXml, migrateCommandMock, libvirtComputingResourceMock); } @@ -1080,7 +1081,6 @@ public void replaceVlanTrunkInterfacesRefusesNicWithAssociatedNetworkWithoutBroa nic.getAssociatedNetworks().get(0).setBroadcastUri(null); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(true).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); libvirtMigrateCmdWrapper.replaceVlanTrunkInterfaces(vlanNicDomainXml, migrateCommandMock, libvirtComputingResourceMock); } @@ -1091,8 +1091,7 @@ public void replaceVlanTrunkInterfacesStripsVlanBlockWhenDestDoesNotSupportTrunk NicTO nic = buildVlanNicTO(true, 100, Collections.singletonList(200)); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(true).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); - Mockito.doReturn(true).when(migrateCommandMock).getDestVlanFilteringEnabled(); + Mockito.doReturn(Map.of(vlanNicMac, true)).when(migrateCommandMock).getNicVlanFilteringMapping(); Mockito.doReturn(false).when(migrateCommandMock).getDestVlanTrunkXmlSupported(); Mockito.doReturn(Map.of(vlanNicMac, "destcloudbr0")).when(migrateCommandMock).getNicBridgeMapping(); @@ -1108,7 +1107,6 @@ public void replaceVlanTrunkInterfacesFailsClosedWhenDestCapabilityUnknown() NicTO nic = buildVlanNicTO(true, 100, null); Mockito.doReturn(new NicTO[]{nic}).when(virtualMachineTOMock).getNics(); Mockito.doReturn(virtualMachineTOMock).when(migrateCommandMock).getVirtualMachine(); - Mockito.doReturn(true).when(libvirtComputingResourceMock).hostSupportsVlanFiltering(); Mockito.doReturn(Map.of()).when(migrateCommandMock).getNicBridgeMapping(); libvirtMigrateCmdWrapper.replaceVlanTrunkInterfaces(vlanNicDomainXml, migrateCommandMock, libvirtComputingResourceMock); diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapperTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapperTest.java index 9a98b5c99fb2..ebe499153b86 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapperTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapperTest.java @@ -57,7 +57,7 @@ public void createPrepareForMigrationAnswerTestDpdkInterfaceNotEmptyShouldSetPar Map dpdkInterfaceMapping = new HashMap<>(); dpdkInterfaceMapping.put("Interface", new DpdkTO()); - PrepareForMigrationAnswer prepareForMigrationAnswer = libvirtPrepareForMigrationCommandWrapperSpy.createPrepareForMigrationAnswer(prepareForMigrationCommandMock, dpdkInterfaceMapping, null, + PrepareForMigrationAnswer prepareForMigrationAnswer = libvirtPrepareForMigrationCommandWrapperSpy.createPrepareForMigrationAnswer(prepareForMigrationCommandMock, dpdkInterfaceMapping, null, null, libvirtComputingResourceMock, virtualMachineTOMock); Assert.assertEquals(prepareForMigrationAnswer.getDpdkInterfaceMapping(), dpdkInterfaceMapping); @@ -68,7 +68,7 @@ public void createPrepareForMigrationAnswerTestNicBridgeMappingNotEmptyShouldSet Map nicBridgeMapping = new HashMap<>(); nicBridgeMapping.put("02:00:00:00:00:01", "cloudbr0"); - PrepareForMigrationAnswer prepareForMigrationAnswer = libvirtPrepareForMigrationCommandWrapperSpy.createPrepareForMigrationAnswer(prepareForMigrationCommandMock, null, nicBridgeMapping, + PrepareForMigrationAnswer prepareForMigrationAnswer = libvirtPrepareForMigrationCommandWrapperSpy.createPrepareForMigrationAnswer(prepareForMigrationCommandMock, null, nicBridgeMapping, null, libvirtComputingResourceMock, virtualMachineTOMock); Assert.assertEquals(nicBridgeMapping, prepareForMigrationAnswer.getNicBridgeMapping()); @@ -78,7 +78,7 @@ public void createPrepareForMigrationAnswerTestNicBridgeMappingNotEmptyShouldSet public void createPrepareForMigrationAnswerTestVerifyThatCpuSharesIsSet() { int cpuShares = 1000; Mockito.doReturn(cpuShares).when(libvirtComputingResourceMock).calculateCpuShares(virtualMachineTOMock); - PrepareForMigrationAnswer prepareForMigrationAnswer = libvirtPrepareForMigrationCommandWrapperSpy.createPrepareForMigrationAnswer(prepareForMigrationCommandMock, null, null, + PrepareForMigrationAnswer prepareForMigrationAnswer = libvirtPrepareForMigrationCommandWrapperSpy.createPrepareForMigrationAnswer(prepareForMigrationCommandMock, null, null, null, libvirtComputingResourceMock, virtualMachineTOMock); Assert.assertEquals(cpuShares, prepareForMigrationAnswer.getNewVmCpuShares().intValue()); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 978cbfd98af0..b51a8543203a 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -517,7 +517,12 @@ public void avoidHostsNotReadyForMultiNetworkNicsExcludesNotReadyHosts() { NicVO trunkNic = Mockito.mock(NicVO.class); Mockito.when(trunkNic.getMultiNetwork()).thenReturn(true); + Mockito.when(trunkNic.getNetworkId()).thenReturn(5L); Mockito.when(nicDao.listByVmId(instanceId)).thenReturn(Arrays.asList(trunkNic)); + long physicalNetworkId = 20L; + NetworkVO network = Mockito.mock(NetworkVO.class); + Mockito.when(network.getPhysicalNetworkId()).thenReturn(physicalNetworkId); + Mockito.when(networkDao.findById(5L)).thenReturn(network); long readyHostId = 10L; long notReadyHostId = 11L; @@ -525,7 +530,7 @@ public void avoidHostsNotReadyForMultiNetworkNicsExcludesNotReadyHosts() { Mockito.when(hostDao.listEnabledIdsByDataCenterId(dataCenterId)) .thenReturn(Arrays.asList(readyHostId, notReadyHostId, noDetailHostId)); - Mockito.when(hostDetailsDao.findHostIdsWithVlanFilteringEnabled()) + Mockito.when(hostDetailsDao.findHostIdsWithVlanFilteringEnabled(physicalNetworkId)) .thenReturn(Collections.singleton(readyHostId)); _dpm.avoidHostsNotReadyForMultiNetworkNics(vmProfile, dc, avoids); @@ -551,12 +556,14 @@ public void avoidHostsNotReadyForMultiNetworkNicsExcludesFilteringHostsForGuestV Mockito.when(nicDao.listByVmId(instanceId)).thenReturn(Arrays.asList(vlanOneNic)); NetworkVO network = Mockito.mock(NetworkVO.class); Mockito.when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest); + long physicalNetworkId = 20L; + Mockito.when(network.getPhysicalNetworkId()).thenReturn(physicalNetworkId); Mockito.when(networkDao.findById(5L)).thenReturn(network); long filteringHostId = 10L; long legacyHostId = 11L; Mockito.when(hostDao.listEnabledIdsByDataCenterId(dataCenterId)).thenReturn(Arrays.asList(filteringHostId, legacyHostId)); - Mockito.when(hostDetailsDao.findHostIdsWithVlanFilteringEnabled()).thenReturn(Collections.singleton(filteringHostId)); + Mockito.when(hostDetailsDao.findHostIdsWithVlanFilteringEnabled(physicalNetworkId)).thenReturn(Collections.singleton(filteringHostId)); _dpm.avoidHostsNotReadyForMultiNetworkNics(vmProfile, dc, avoids); @@ -580,7 +587,7 @@ public void avoidHostsNotReadyForMultiNetworkNicsDoesNoLookupsForNicsOnOtherVlan assertAvoidIsEmpty(avoids, true, true, true, true); Mockito.verify(networkDao, Mockito.never()).findById(Mockito.anyLong()); - Mockito.verify(hostDetailsDao, Mockito.never()).findHostIdsWithVlanFilteringEnabled(); + Mockito.verify(hostDetailsDao, Mockito.never()).findHostIdsWithVlanFilteringEnabled(Mockito.anyLong()); Mockito.verify(hostDao, Mockito.never()).listEnabledIdsByDataCenterId(Mockito.anyLong()); } diff --git a/server/src/test/java/com/cloud/network/NetworkServiceImplTest.java b/server/src/test/java/com/cloud/network/NetworkServiceImplTest.java index 406223904b35..73934de7a97f 100644 --- a/server/src/test/java/com/cloud/network/NetworkServiceImplTest.java +++ b/server/src/test/java/com/cloud/network/NetworkServiceImplTest.java @@ -1522,7 +1522,7 @@ private UserVmVO mockRunningVmOnReadyHost() { Mockito.when(userVmDao.findById(5L)).thenReturn(vm); DetailVO readyDetail = Mockito.mock(DetailVO.class); Mockito.when(readyDetail.getValue()).thenReturn("true"); - Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(readyDetail); + Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 1L)).thenReturn(readyDetail); return vm; } @@ -1687,7 +1687,9 @@ public void associateNetworkToNicFailsWhenRunningVmHostIsNotVlanFilteringReady() Mockito.when(nicDao.findById(11L)).thenReturn(nic); UserVmVO vm = mockVm(VirtualMachine.State.Running, 1L); Mockito.when(userVmDao.findById(5L)).thenReturn(vm); - Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(null); + NetworkVO primaryNetwork = mockNetwork(205L, Network.GuestType.Isolated, 1L, 1L, null, Networks.BroadcastDomainType.Vlan, vlanUri(1174)); + Mockito.when(networkDao.findById(205L)).thenReturn(primaryNetwork); + Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 1L)).thenReturn(null); service.associateNetworkToNic(mockAssociateCmd(11L, Arrays.asList(206L))); } @@ -1793,7 +1795,7 @@ public void associateNetworkToNicFailsWhenLiveUpdateCommandFails() throws Except Mockito.when(userVmDao.findById(5L)).thenReturn(vm); DetailVO readyDetail = Mockito.mock(DetailVO.class); Mockito.when(readyDetail.getValue()).thenReturn("true"); - Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(readyDetail); + Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 1L)).thenReturn(readyDetail); NetworkVO primaryNetwork = mockNetwork(205L, Network.GuestType.Isolated, 1L, 1L, null, Networks.BroadcastDomainType.Vlan, vlanUri(1174)); Mockito.when(networkDao.findById(205L)).thenReturn(primaryNetwork); Mockito.when(nicNetworkMapDao.listByNicId(11L)).thenReturn(new ArrayList<>()); @@ -2042,7 +2044,7 @@ public void associateNetworkToNicFailsWhenRequestedNetworkSharesVlanWithAnExisti Mockito.when(userVmDao.findById(5L)).thenReturn(vm); DetailVO readyDetail = Mockito.mock(DetailVO.class); Mockito.when(readyDetail.getValue()).thenReturn("true"); - Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(readyDetail); + Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 1L)).thenReturn(readyDetail); NetworkVO primaryNetwork = mockNetwork(205L, Network.GuestType.Isolated, 1L, 1L, null, Networks.BroadcastDomainType.Vlan, vlanUri(1174)); Mockito.when(networkDao.findById(205L)).thenReturn(primaryNetwork); Mockito.when(nicNetworkMapDao.listByNicId(11L)).thenReturn(new ArrayList<>()); @@ -2383,9 +2385,11 @@ public void disassociateNetworkFromNicUpdatesLiveVlanMembershipOnARunningVm() th Mockito.when(vm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); Mockito.when(vm.getInstanceName()).thenReturn("i-2-11-VM"); Mockito.when(userVmDao.findById(5L)).thenReturn(vm); + NetworkVO primaryNetwork = mockNetwork(205L, Network.GuestType.Isolated, 1L, 1L, null, Networks.BroadcastDomainType.Vlan, vlanUri(1174)); + Mockito.when(networkDao.findById(205L)).thenReturn(primaryNetwork); DetailVO readyDetail = Mockito.mock(DetailVO.class); Mockito.when(readyDetail.getValue()).thenReturn("true"); - Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(readyDetail); + Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 1L)).thenReturn(readyDetail); NicNetworkMapVO association = new NicNetworkMapVO(11L, 206L, "10.1.1.50", null); Mockito.when(nicNetworkMapDao.findByNicIdAndNetworkId(11L, 206L)).thenReturn(association); Mockito.when(networkModel.getNicProfile(Mockito.eq(vm), Mockito.eq(nic), Mockito.any())).thenReturn(null); @@ -2413,9 +2417,11 @@ public void disassociateNetworkFromNicRemovesDhcpEntryOnARunningVm() throws Exce Mockito.when(vm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); Mockito.when(vm.getInstanceName()).thenReturn("i-2-11-VM"); Mockito.when(userVmDao.findById(5L)).thenReturn(vm); + NetworkVO primaryNetwork = mockNetwork(205L, Network.GuestType.Isolated, 1L, 1L, null, Networks.BroadcastDomainType.Vlan, vlanUri(1174)); + Mockito.when(networkDao.findById(205L)).thenReturn(primaryNetwork); DetailVO readyDetail = Mockito.mock(DetailVO.class); Mockito.when(readyDetail.getValue()).thenReturn("true"); - Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(readyDetail); + Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 1L)).thenReturn(readyDetail); NicNetworkMapVO association = new NicNetworkMapVO(11L, 206L, "10.1.1.50", null); Mockito.when(nicNetworkMapDao.findByNicIdAndNetworkId(11L, 206L)).thenReturn(association); NetworkVO associatedNetwork = mockNetwork(206L, Network.GuestType.Isolated, 1L, 1L, null, Networks.BroadcastDomainType.Vlan, vlanUri(1180)); @@ -2459,9 +2465,11 @@ public void disassociateNetworkFromNicSucceedsWhenMetadataRefreshFails() throws Mockito.when(vm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM); Mockito.when(vm.getInstanceName()).thenReturn("i-2-11-VM"); Mockito.when(userVmDao.findById(5L)).thenReturn(vm); + NetworkVO primaryNetwork = mockNetwork(205L, Network.GuestType.Isolated, 1L, 1L, null, Networks.BroadcastDomainType.Vlan, vlanUri(1174)); + Mockito.when(networkDao.findById(205L)).thenReturn(primaryNetwork); DetailVO readyDetail = Mockito.mock(DetailVO.class); Mockito.when(readyDetail.getValue()).thenReturn("true"); - Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(readyDetail); + Mockito.when(hostDetailsDao.findDetail(1L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 1L)).thenReturn(readyDetail); NicNetworkMapVO association = new NicNetworkMapVO(11L, 206L, "10.1.1.50", null); Mockito.when(nicNetworkMapDao.findByNicIdAndNetworkId(11L, 206L)).thenReturn(association); Mockito.when(networkModel.getNicProfile(Mockito.eq(vm), Mockito.eq(nic), Mockito.any())).thenReturn(null); diff --git a/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java b/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java index faa464cb85db..a7bfc7f7bd83 100644 --- a/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java +++ b/server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java @@ -873,10 +873,14 @@ public void isHostReadyForMultiNetworkNicsIsReadyForAVmWithNoTrunkNic() { public void isHostReadyForMultiNetworkNicsRejectsATrunkNicVmOnAHostWithoutVlanFiltering() { VMInstanceVO vm = mockVmWithId(1L); NicVO trunkNic = mockNicWithMultiNetwork(true); + Mockito.when(trunkNic.getNetworkId()).thenReturn(50L); + NetworkVO network = Mockito.mock(NetworkVO.class); + Mockito.when(network.getPhysicalNetworkId()).thenReturn(99L); + Mockito.when(_networkDao.findById(50L)).thenReturn(network); Mockito.when(nicDao.listByVmId(1L)).thenReturn(Collections.singletonList(trunkNic)); Host host = Mockito.mock(Host.class); Mockito.when(host.getId()).thenReturn(2L); - Mockito.when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(null); + Mockito.when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 99L)).thenReturn(null); Assert.assertFalse(userVmManagerImpl.isHostReadyForMultiNetworkNics(vm, host)); } @@ -885,12 +889,16 @@ public void isHostReadyForMultiNetworkNicsRejectsATrunkNicVmOnAHostWithoutVlanFi public void isHostReadyForMultiNetworkNicsAcceptsATrunkNicVmOnAHostWithVlanFiltering() { VMInstanceVO vm = mockVmWithId(1L); NicVO trunkNic = mockNicWithMultiNetwork(true); + Mockito.when(trunkNic.getNetworkId()).thenReturn(50L); + NetworkVO network = Mockito.mock(NetworkVO.class); + Mockito.when(network.getPhysicalNetworkId()).thenReturn(99L); + Mockito.when(_networkDao.findById(50L)).thenReturn(network); Mockito.when(nicDao.listByVmId(1L)).thenReturn(Collections.singletonList(trunkNic)); Host host = Mockito.mock(Host.class); Mockito.when(host.getId()).thenReturn(2L); DetailVO detail = Mockito.mock(DetailVO.class); Mockito.when(detail.getValue()).thenReturn("true"); - Mockito.when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED)).thenReturn(detail); + Mockito.when(hostDetailsDao.findDetail(2L, Host.HOST_VLAN_FILTERING_ENABLED_PREFIX + 99L)).thenReturn(detail); Assert.assertTrue(userVmManagerImpl.isHostReadyForMultiNetworkNics(vm, host)); }