Skip to content

Commit 8e4dc0a

Browse files
VMware: match nic mac for ip address fetch (#10641)
1 parent e12813d commit 8e4dc0a

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5837,11 +5837,20 @@ protected Answer execute(GetVmIpAddressCommand cmd) {
58375837
if (toolsStatus == VirtualMachineToolsStatus.TOOLS_NOT_INSTALLED) {
58385838
details += "Vmware tools not installed.";
58395839
} else {
5840-
ip = guestInfo.getIpAddress();
5841-
if (ip != null) {
5842-
result = true;
5840+
var normalizedMac = cmd.getMacAddress().replaceAll("-", ":");
5841+
for(var guestInfoNic : guestInfo.getNet()) {
5842+
var normalizedNicMac = guestInfoNic.getMacAddress().replaceAll("-", ":");
5843+
if (!result && normalizedNicMac.equalsIgnoreCase(normalizedMac)) {
5844+
result = true;
5845+
details = null;
5846+
for (var ipAddr : guestInfoNic.getIpAddress()) {
5847+
if (NetUtils.isValidIp4(ipAddr) && (cmd.getVmNetworkCidr() == null || NetUtils.isIpWithInCidrRange(ipAddr, cmd.getVmNetworkCidr()))) {
5848+
details = ipAddr;
5849+
}
5850+
}
5851+
break;
5852+
}
58435853
}
5844-
details = ip;
58455854
}
58465855
} else {
58475856
details += "VM " + vmName + " no longer exists on vSphere host: " + hyperHost.getHyperHostName();

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ private class VmIpAddrFetchThread extends ManagedContextRunnable {
753753
String networkCidr;
754754
String macAddress;
755755

756-
public VmIpAddrFetchThread(long vmId, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
756+
public VmIpAddrFetchThread(long vmId, String vmUuid, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
757757
this.vmId = vmId;
758758
this.vmUuid = vmUuid;
759759
this.nicId = nicId;
@@ -775,8 +775,13 @@ protected void runInContext() {
775775
Answer answer = _agentMgr.send(hostId, cmd);
776776
if (answer.getResult()) {
777777
String vmIp = answer.getDetails();
778-
779-
if (NetUtils.isValidIp4(vmIp)) {
778+
if (vmIp == null) {
779+
// we got a valid response and the NIC does not have an IP assigned, as such we will update the database with null
780+
if (nic.getIPv4Address() != null) {
781+
nic.setIPv4Address(null);
782+
_nicDao.update(nicId, nic);
783+
}
784+
} else if (NetUtils.isValidIp4(vmIp)) {
780785
// set this vm ip addr in vm nic.
781786
if (nic != null) {
782787
nic.setIPv4Address(vmIp);
@@ -791,12 +796,8 @@ protected void runInContext() {
791796
}
792797
}
793798
} else {
794-
//previously vm has ip and nic table has ip address. After vm restart or stop/start
795-
//if vm doesnot get the ip then set the ip in nic table to null
796-
if (nic.getIPv4Address() != null) {
797-
nic.setIPv4Address(null);
798-
_nicDao.update(nicId, nic);
799-
}
799+
// since no changes are being done, we should not decrement IP usage
800+
decrementCount = false;
800801
if (answer.getDetails() != null) {
801802
logger.debug("Failed to get vm ip for Vm [id: {}, uuid: {}, name: {}], details: {}",
802803
vmId, vmUuid, vmName, answer.getDetails());
@@ -2693,7 +2694,8 @@ protected void runInContext() {
26932694
VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(userVm);
26942695
VirtualMachine vm = vmProfile.getVirtualMachine();
26952696
boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
2696-
_vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, nicId, vmInstance.getInstanceName(),
2697+
2698+
_vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, vmInstance.getUuid(), nicId, vmInstance.getInstanceName(),
26972699
isWindows, vm.getHostId(), network.getCidr(), nicVo.getMacAddress()));
26982700

26992701
}

0 commit comments

Comments
 (0)