Skip to content

Commit 0b9b688

Browse files
alexandru-bagudhslove
authored andcommitted
VMware: match nic mac for ip address fetch (apache#10641)
1 parent 33f6aa8 commit 0b9b688

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-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: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ private class VmIpAddrFetchThread extends ManagedContextRunnable {
824824
String networkCidr;
825825
String macAddress;
826826

827-
public VmIpAddrFetchThread(long vmId, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
827+
public VmIpAddrFetchThread(long vmId, String vmUuid, long nicId, String instanceName, boolean windows, Long hostId, String networkCidr, String macAddress) {
828828
this.vmId = vmId;
829829
this.vmUuid = vmUuid;
830830
this.nicId = nicId;
@@ -846,8 +846,13 @@ protected void runInContext() {
846846
Answer answer = _agentMgr.send(hostId, cmd);
847847
if (answer.getResult()) {
848848
String vmIp = answer.getDetails();
849-
850-
if (NetUtils.isValidIp4(vmIp)) {
849+
if (vmIp == null) {
850+
// we got a valid response and the NIC does not have an IP assigned, as such we will update the database with null
851+
if (nic.getIPv4Address() != null) {
852+
nic.setIPv4Address(null);
853+
_nicDao.update(nicId, nic);
854+
}
855+
} else if (NetUtils.isValidIp4(vmIp)) {
851856
// set this vm ip addr in vm nic.
852857
if (nic != null) {
853858
nic.setIPv4Address(vmIp);
@@ -862,12 +867,8 @@ protected void runInContext() {
862867
}
863868
}
864869
} else {
865-
//previously vm has ip and nic table has ip address. After vm restart or stop/start
866-
//if vm doesnot get the ip then set the ip in nic table to null
867-
if (nic.getIPv4Address() != null) {
868-
nic.setIPv4Address(null);
869-
_nicDao.update(nicId, nic);
870-
}
870+
// since no changes are being done, we should not decrement IP usage
871+
decrementCount = false;
871872
if (answer.getDetails() != null) {
872873
logger.debug("Failed to get vm ip for Vm [id: {}, uuid: {}, name: {}], details: {}",
873874
vmId, vmUuid, vmName, answer.getDetails());
@@ -2781,7 +2782,7 @@ protected void runInContext() {
27812782
VirtualMachine vm = vmProfile.getVirtualMachine();
27822783
boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
27832784

2784-
_vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, nicId, vmInstance.getInstanceName(),
2785+
_vmIpFetchThreadExecutor.execute(new VmIpAddrFetchThread(vmId, vmInstance.getUuid(), nicId, vmInstance.getInstanceName(),
27852786
isWindows, vm.getHostId(), network.getCidr(), nicVo.getMacAddress()));
27862787

27872788
}

0 commit comments

Comments
 (0)