Copilot commented on code in PR #14803:
URL: https://github.com/apache/cloudstack/pull/14803#discussion_r4670419122


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java:
##########
@@ -695,6 +695,11 @@ protected String replaceDpdkInterfaces(String xmlDesc, 
Map<String, DpdkTO> dpdkP
         return LibvirtXMLParser.getXml(doc);
     }
 
+    // Trunk nics always need it; ordinary nics only when the zone opted in to 
the shared VLAN-aware bridge
+    private boolean needsVlanRewrite(NicTO nic, boolean 
sourceVlanFilteringEnabled, boolean destVlanFilteringEnabled) {
+        return nic.isTrunkVlan() || (nic.isSharedVlanBridgeEnabled() && 
(sourceVlanFilteringEnabled || destVlanFilteringEnabled));

Review Comment:
   If `multi.network.nic.enabled` is disabled while an ordinary VLAN NIC is 
running on a shared bridge, this predicate skips migration rewriting despite 
preparation creating a dedicated destination bridge. On pre-11 libvirt, 
migration retains the shared-bridge source and the membership hook also skips 
the NIC, leaving its tap on the default VLAN. Detect the live source topology 
and use the prepared destination mapping, or prevent disabling the setting 
until affected VMs are stopped.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPostMigrationCommandWrapper.java:
##########
@@ -87,16 +88,63 @@ public Answer execute(final PostMigrationCommand command, 
final LibvirtComputing
     }
 
     private void applyManualVlanTrunkMembership(Connect conn, String vmName, 
NicTO[] nics, LibvirtComputingResource libvirtComputingResource)
-            throws InternalErrorException {
+            throws InternalErrorException, LibvirtException {
         if (nics == null) {
             return;
         }
+        InternalErrorException firstFailure = null;
         for (NicTO nic : nics) {
             if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vlan) {
                 continue;
             }
             LibvirtVMDef.InterfaceDef liveInterface = 
libvirtComputingResource.getInterface(conn, vmName, nic.getMac());
-            libvirtComputingResource.getVifDriver(nic.getType(), 
nic.getName()).ensureVlanTrunkMembership(liveInterface, nic);
+            try {
+                libvirtComputingResource.getVifDriver(nic.getType(), 
nic.getName()).ensureVlanTrunkMembership(liveInterface, nic);
+            } catch (InternalErrorException e) {
+                // keep going so every other nic of the VM is also applied or 
cut off, not left on VLAN 1
+                failClosed(conn, vmName, nic, libvirtComputingResource);
+                firstFailure = firstFailure == null ? e : firstFailure;
+                continue;
+            }
+            restoreLinkIfCutOff(conn, vmName, nic, liveInterface, 
libvirtComputingResource);

Review Comment:
   A failed interface lookup or link-state update can abort the loop before 
later VLAN NICs receive membership or have their links disabled. Repeated 
failure on an early NIC makes every retry stop there, leaving later taps on the 
default VLAN. Catch these errors per NIC, retain the failure, and continue 
through all NICs before failing the command.



##########
core/src/main/java/com/cloud/agent/api/VlanTrunkMigrationHelper.java:
##########
@@ -143,20 +148,63 @@ public static boolean nicNeedsManualVlanMembership(NicVO 
nic, boolean vlanFilter
 
     public static void 
sendPostMigrationVlanTrunkMembershipIfNeeded(VirtualMachineTO vmTO, long 
dstHostId,
             NicDao nicDao, HostDetailsDao hostDetailsDao, CommandSender 
commandSender, Logger logger) {
+        sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, dstHostId, nicDao, 
hostDetailsDao, commandSender, logger, POST_MIGRATION_RETRY_DELAY_MS);
+    }
+
+    static void sendPostMigrationVlanTrunkMembershipIfNeeded(VirtualMachineTO 
vmTO, long dstHostId,
+            NicDao nicDao, HostDetailsDao hostDetailsDao, CommandSender 
commandSender, Logger logger, long retryDelayMs) {
         if (!vmNeedsPostMigrationVlanTrunkMembership(vmTO.getId(), dstHostId, 
nicDao, hostDetailsDao)) {
             return;
         }
-        final String warningMsg = "Post-migration VLAN trunk membership tasks 
did not complete for VM [{}] "
-                + "on destination host [{}]: {}. Migration completed but VLAN 
membership may need manual correction.";
+        String failure = sendPostMigrationCommandWithRetry(vmTO, dstHostId, 
commandSender, logger, retryDelayMs);
+        if (failure != null) {
+            logger.error("Post-migration VLAN trunk membership tasks did not 
complete for VM [{}] on destination host [{}]: {}. "
+                    + "Migration completed but its tap may still be on the 
bridge's default VLAN; correct the VLAN membership or restart the VM.",
+                    vmTO, dstHostId, failure);
+        }
+    }
+
+    // returns null once an attempt succeeds, else the last failure after all 
attempts
+    public static String sendPostMigrationCommandWithRetry(VirtualMachineTO 
vmTO, long dstHostId, CommandSender commandSender, Logger logger) {
+        return sendPostMigrationCommandWithRetry(vmTO, dstHostId, 
commandSender, logger, POST_MIGRATION_RETRY_DELAY_MS);
+    }
+
+    static String sendPostMigrationCommandWithRetry(VirtualMachineTO vmTO, 
long dstHostId, CommandSender commandSender, Logger logger, long retryDelayMs) {
+        String failure = null;
+        for (int attempt = 1; attempt <= POST_MIGRATION_MAX_ATTEMPTS; 
attempt++) {
+            failure = sendPostMigrationCommand(vmTO, dstHostId, commandSender);
+            if (failure == null) {
+                return null;
+            }
+            logger.warn("Post-migration attempt {}/{} failed for VM [{}] on 
destination host [{}]: {}",
+                    attempt, POST_MIGRATION_MAX_ATTEMPTS, vmTO, dstHostId, 
failure);
+            if (attempt < POST_MIGRATION_MAX_ATTEMPTS && 
!pauseBeforePostMigrationRetry(retryDelayMs)) {
+                break;
+            }
+        }
+        return failure;
+    }
+
+    // returns null on success, else the reason
+    private static String sendPostMigrationCommand(VirtualMachineTO vmTO, long 
dstHostId, CommandSender commandSender) {
         try {
-            PostMigrationCommand postMigrationCommand = new 
PostMigrationCommand(vmTO, vmTO.getName());
-            Answer postMigrationAnswer = commandSender.send(dstHostId, 
postMigrationCommand);
-            if (postMigrationAnswer == null || 
!postMigrationAnswer.getResult()) {
-                String details = postMigrationAnswer != null ? 
postMigrationAnswer.getDetails() : "null answer returned";
-                logger.warn(warningMsg, vmTO, dstHostId, details);
+            Answer postMigrationAnswer = commandSender.send(dstHostId, new 
PostMigrationCommand(vmTO, vmTO.getName()));
+            if (postMigrationAnswer != null && 
postMigrationAnswer.getResult()) {
+                return null;
             }
+            return postMigrationAnswer != null ? 
postMigrationAnswer.getDetails() : "null answer returned";
         } catch (Exception e) {
-            logger.warn(warningMsg, vmTO, dstHostId, e.getMessage(), e);
+            return e.getMessage();

Review Comment:
   A failed answer with null details, or an exception with a null message, 
returns null here. The retry loop treats that as success, stopping after the 
first failed attempt and suppressing the final alert. Return a non-null 
fallback for every failure, reserving null exclusively for successful answers.



##########
core/src/main/java/com/cloud/agent/api/VlanTrunkMigrationHelper.java:
##########
@@ -143,20 +148,63 @@ public static boolean nicNeedsManualVlanMembership(NicVO 
nic, boolean vlanFilter
 
     public static void 
sendPostMigrationVlanTrunkMembershipIfNeeded(VirtualMachineTO vmTO, long 
dstHostId,
             NicDao nicDao, HostDetailsDao hostDetailsDao, CommandSender 
commandSender, Logger logger) {
+        sendPostMigrationVlanTrunkMembershipIfNeeded(vmTO, dstHostId, nicDao, 
hostDetailsDao, commandSender, logger, POST_MIGRATION_RETRY_DELAY_MS);
+    }
+
+    static void sendPostMigrationVlanTrunkMembershipIfNeeded(VirtualMachineTO 
vmTO, long dstHostId,
+            NicDao nicDao, HostDetailsDao hostDetailsDao, CommandSender 
commandSender, Logger logger, long retryDelayMs) {
         if (!vmNeedsPostMigrationVlanTrunkMembership(vmTO.getId(), dstHostId, 
nicDao, hostDetailsDao)) {
             return;
         }
-        final String warningMsg = "Post-migration VLAN trunk membership tasks 
did not complete for VM [{}] "
-                + "on destination host [{}]: {}. Migration completed but VLAN 
membership may need manual correction.";
+        String failure = sendPostMigrationCommandWithRetry(vmTO, dstHostId, 
commandSender, logger, retryDelayMs);
+        if (failure != null) {
+            logger.error("Post-migration VLAN trunk membership tasks did not 
complete for VM [{}] on destination host [{}]: {}. "
+                    + "Migration completed but its tap may still be on the 
bridge's default VLAN; correct the VLAN membership or restart the VM.",
+                    vmTO, dstHostId, failure);

Review Comment:
   For migrations that also move storage, 
StorageSystemDataMotionStrategy.copyAsync() uses this helper, which only logs 
the exhausted retries. Unlike the host-only migration path, 
orchestrateMigrateWithStorage() does not subsequently call 
executePostMigrationCommand(), so a NIC left down produces no management-server 
alert despite the PR's stated behavior. Propagate the failure to that caller 
and publish a migration alert there, or add an alert callback to this helper.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPostMigrationCommandWrapper.java:
##########
@@ -87,16 +88,63 @@ public Answer execute(final PostMigrationCommand command, 
final LibvirtComputing
     }
 
     private void applyManualVlanTrunkMembership(Connect conn, String vmName, 
NicTO[] nics, LibvirtComputingResource libvirtComputingResource)
-            throws InternalErrorException {
+            throws InternalErrorException, LibvirtException {
         if (nics == null) {
             return;
         }
+        InternalErrorException firstFailure = null;
         for (NicTO nic : nics) {
             if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vlan) {
                 continue;
             }
             LibvirtVMDef.InterfaceDef liveInterface = 
libvirtComputingResource.getInterface(conn, vmName, nic.getMac());
-            libvirtComputingResource.getVifDriver(nic.getType(), 
nic.getName()).ensureVlanTrunkMembership(liveInterface, nic);
+            try {
+                libvirtComputingResource.getVifDriver(nic.getType(), 
nic.getName()).ensureVlanTrunkMembership(liveInterface, nic);
+            } catch (InternalErrorException e) {
+                // keep going so every other nic of the VM is also applied or 
cut off, not left on VLAN 1
+                failClosed(conn, vmName, nic, libvirtComputingResource);
+                firstFailure = firstFailure == null ? e : firstFailure;
+                continue;
+            }
+            restoreLinkIfCutOff(conn, vmName, nic, liveInterface, 
libvirtComputingResource);
+        }
+        if (firstFailure != null) {
+            throw firstFailure;
+        }
+    }
+
+    // Without its VLANs the tap sits on the bridge's default VLAN 1, so cut 
the nic off rather than leave it there
+    private void failClosed(Connect conn, String vmName, NicTO nic, 
LibvirtComputingResource libvirtComputingResource) {
+        if (!nic.isEnabled()) {
+            return;
+        }
+        try {
+            setNicLinkState(conn, vmName, nic, false, 
libvirtComputingResource);
+            logger.error("Could not apply the VLAN membership of nic {} of VM 
{}; its link was set down so it does not sit on the bridge's default VLAN. "
+                    + "The link is restored once the membership is applied; 
otherwise correct it or restart the VM.", nic.getMac(), vmName);
+        } catch (LibvirtException e) {
+            logger.error("Could not apply the VLAN membership of nic {} of VM 
{} and could not set its link down either, so it may still be on the bridge's 
default VLAN: {}",
+                    nic.getMac(), vmName, e.getMessage());
+        }
+    }
+
+    // The link follows the nic's enabled state, so an enabled nic found down 
was cut off by an earlier failed attempt, possibly on another host
+    private void restoreLinkIfCutOff(Connect conn, String vmName, NicTO nic, 
LibvirtVMDef.InterfaceDef liveInterface,
+            LibvirtComputingResource libvirtComputingResource) throws 
LibvirtException {
+        if (nic.isEnabled() && !liveInterface.isLinkStateUp()) {
+            setNicLinkState(conn, vmName, nic, true, libvirtComputingResource);

Review Comment:
   An enabled NIC cut off after a failed migration remains down when its next 
destination supports VLAN XML: the scheduling predicate skips 
PostMigrationCommand unless CLVM volumes require it. Migration preserves the 
link-down XML, so successful VLAN configuration alone does not reconnect the 
NIC. Schedule link recovery independently of manual VLAN programming in both 
host-only and storage-migration callers.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java:
##########
@@ -100,6 +105,10 @@ public Answer execute(final PlugNicCommand command, final 
LibvirtComputingResour
                     throw new InternalErrorException("Failed to locate live 
tap for trunk nic " + nic.getMac() + ": " + e.getMessage());
                 }
             }
+            if (linkDownUntilVlansApplied) {
+                interfaceDef.setLinkStateUp(true);

Review Comment:
   This unconditionally enables a disabled VLAN NIC after applying its 
membership. `BridgeVifDriver.plug` initializes the link from `nic.isEnabled()`, 
but the new restoration step overrides that requested state on hosts requiring 
manual VLAN configuration. Restore the NIC's enabled state rather than always 
bringing the link up.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/BridgeVifDriver.java:
##########
@@ -212,22 +212,43 @@ public static boolean isUntaggedBroadcast(final NicTO 
nic) {
         return nic.getBroadcastUri() != null && 
"untagged".equalsIgnoreCase(Networks.BroadcastDomainType.getValue(nic.getBroadcastUri()));
     }
 
+    // the nic's own guest traffic label, else the default guest bridge
+    protected String resolveGuestBridgeName(final NicTO nic) {
+        String trafficLabel = nic.getName();
+        return StringUtils.isNotEmpty(trafficLabel) ? trafficLabel : 
_bridges.get("guest");
+    }
+
+    // Opt-in via the zone setting (carried on the nic) and decided per the 
nic's own bridge: a host can carry several
+    // guest bridges, only some with vlan_filtering on. Without the setting, 
vlan_filtering alone never changes the topology.
     protected boolean usesSharedVlanAwareBridge(final NicTO nic) {
-        return nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan && 
_libvirtComputingResource.hostSupportsVlanFiltering()
-                && !isUntaggedBroadcast(nic);
+        if (!nic.isSharedVlanBridgeEnabled() || nic.getBroadcastType() != 
Networks.BroadcastDomainType.Vlan || isUntaggedBroadcast(nic)) {
+            return false;

Review Comment:
   The new opt-out is not reflected in 
NetworkServiceImpl.reapplyLiveVlanMembership: its management-side predicate 
still selects ordinary VLAN NICs whenever the host has filtering enabled. With 
the zone setting false on libvirt <11, a memory-snapshot revert therefore sends 
UpdateNicVlanMembershipCommand for a dedicated-bridge NIC, which 
updateVlanTrunkMembership rejects as not using the shared bridge. 
VMSnapshotManagerImpl then reports failure after the snapshot has already been 
reverted. Apply the same zone opt-in to the management-side membership 
predicates so these NICs are skipped.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to