Skip to content

Commit 6da9414

Browse files
alexandru-baguLocharla, Sandeep
authored andcommitted
VMware: match nic mac for ip address fetch (apache#10641)
1 parent 23dbe3c commit 6da9414

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
@@ -5838,11 +5838,20 @@ protected Answer execute(GetVmIpAddressCommand cmd) {
58385838
if (toolsStatus == VirtualMachineToolsStatus.TOOLS_NOT_INSTALLED) {
58395839
details += "Vmware tools not installed.";
58405840
} else {
5841-
ip = guestInfo.getIpAddress();
5842-
if (ip != null) {
5843-
result = true;
5841+
var normalizedMac = cmd.getMacAddress().replaceAll("-", ":");
5842+
for(var guestInfoNic : guestInfo.getNet()) {
5843+
var normalizedNicMac = guestInfoNic.getMacAddress().replaceAll("-", ":");
5844+
if (!result && normalizedNicMac.equalsIgnoreCase(normalizedMac)) {
5845+
result = true;
5846+
details = null;
5847+
for (var ipAddr : guestInfoNic.getIpAddress()) {
5848+
if (NetUtils.isValidIp4(ipAddr) && (cmd.getVmNetworkCidr() == null || NetUtils.isIpWithInCidrRange(ipAddr, cmd.getVmNetworkCidr()))) {
5849+
details = ipAddr;
5850+
}
5851+
}
5852+
break;
5853+
}
58445854
}
5845-
details = ip;
58465855
}
58475856
} else {
58485857
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
@@ -772,7 +772,7 @@ private class VmIpAddrFetchThread extends ManagedContextRunnable {
772772
String networkCidr;
773773
String macAddress;
774774

775-
public VmIpAddrFetchThread(long vmId, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
775+
public VmIpAddrFetchThread(long vmId, String vmUuid, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
776776
this.vmId = vmId;
777777
this.vmUuid = vmUuid;
778778
this.nicId = nicId;
@@ -794,8 +794,13 @@ protected void runInContext() {
794794
Answer answer = _agentMgr.send(hostId, cmd);
795795
if (answer.getResult()) {
796796
String vmIp = answer.getDetails();
797-
798-
if (NetUtils.isValidIp4(vmIp)) {
797+
if (vmIp == null) {
798+
// we got a valid response and the NIC does not have an IP assigned, as such we will update the database with null
799+
if (nic.getIPv4Address() != null) {
800+
nic.setIPv4Address(null);
801+
_nicDao.update(nicId, nic);
802+
}
803+
} else if (NetUtils.isValidIp4(vmIp)) {
799804
// set this vm ip addr in vm nic.
800805
if (nic != null) {
801806
nic.setIPv4Address(vmIp);
@@ -810,12 +815,8 @@ protected void runInContext() {
810815
}
811816
}
812817
} else {
813-
//previously vm has ip and nic table has ip address. After vm restart or stop/start
814-
//if vm doesnot get the ip then set the ip in nic table to null
815-
if (nic.getIPv4Address() != null) {
816-
nic.setIPv4Address(null);
817-
_nicDao.update(nicId, nic);
818-
}
818+
// since no changes are being done, we should not decrement IP usage
819+
decrementCount = false;
819820
if (answer.getDetails() != null) {
820821
logger.debug("Failed to get vm ip for Vm [id: {}, uuid: {}, name: {}], details: {}",
821822
vmId, vmUuid, vmName, answer.getDetails());
@@ -2723,7 +2724,8 @@ protected void runInContext() {
27232724
VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(userVm);
27242725
VirtualMachine vm = vmProfile.getVirtualMachine();
27252726
boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
2726-
_vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, nicId, vmInstance.getInstanceName(),
2727+
2728+
_vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, vmInstance.getUuid(), nicId, vmInstance.getInstanceName(),
27272729
isWindows, vm.getHostId(), network.getCidr(), nicVo.getMacAddress()));
27282730

27292731
}

0 commit comments

Comments
 (0)