Skip to content

Commit a400423

Browse files
author
Alexandru Bagu
committed
match nic mac for ip address fetch
1 parent 2dfe6a6 commit a400423

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

core/src/main/java/com/cloud/agent/api/GetVmIpAddressCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ public class GetVmIpAddressCommand extends Command {
2323

2424
String vmName;
2525
String vmNetworkCidr;
26+
String vmNetworkMac;
2627
boolean windows = false;
2728

28-
public GetVmIpAddressCommand(String vmName, String vmNetworkCidr, boolean windows) {
29+
public GetVmIpAddressCommand(String vmName, String vmNetworkCidr, String vmNetworkMac, boolean windows) {
2930
this.vmName = vmName;
3031
this.windows = windows;
3132
this.vmNetworkCidr = vmNetworkCidr;
33+
this.vmNetworkMac = vmNetworkMac;
3234
}
3335

3436
@Override
@@ -47,4 +49,8 @@ public boolean isWindows(){
4749
public String getVmNetworkCidr() {
4850
return vmNetworkCidr;
4951
}
52+
53+
public String getVmNetworkMac() {
54+
return vmNetworkMac;
55+
}
5056
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5738,7 +5738,6 @@ protected Answer execute(GetVmIpAddressCommand cmd) {
57385738
String details = "Unable to find IP Address of VM. ";
57395739
String vmName = cmd.getVmName();
57405740
boolean result = false;
5741-
String ip = null;
57425741
Answer answer = null;
57435742

57445743
VmwareContext context = getServiceContext();
@@ -5757,11 +5756,19 @@ protected Answer execute(GetVmIpAddressCommand cmd) {
57575756
if (toolsStatus == VirtualMachineToolsStatus.TOOLS_NOT_INSTALLED) {
57585757
details += "Vmware tools not installed.";
57595758
} else {
5760-
ip = guestInfo.getIpAddress();
5761-
if (ip != null) {
5762-
result = true;
5759+
var normalizedMac = cmd.getVmNetworkMac().replaceAll("-", ":");
5760+
for(var guestInfoNic : guestInfo.getNet()) {
5761+
var normalizedNicMac = guestInfoNic.getMacAddress().replaceAll("-", ":");
5762+
if (!result && normalizedNicMac.equalsIgnoreCase(normalizedMac)) {
5763+
result = true;
5764+
details = null;
5765+
for (var ipAddr : guestInfoNic.getIpAddress()) {
5766+
if(NetUtils.isIpWithInCidrRange(ipAddr, cmd.getVmNetworkCidr())) {
5767+
details = ipAddr;
5768+
}
5769+
}
5770+
}
57635771
}
5764-
details = ip;
57655772
}
57665773
} else {
57675774
details += "VM " + vmName + " no longer exists on vSphere host: " + hyperHost.getHyperHostName();

plugins/hypervisors/xenserver/src/test/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixRequestWrapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ public void testGetVmIpAddressCommand() throws XenAPIException, XmlRpcException
19341934
vmIpsMap.put("Test", "127.0.0.1");
19351935
rec.networks = vmIpsMap;
19361936

1937-
final GetVmIpAddressCommand getVmIpAddrCmd = new GetVmIpAddressCommand("Test", "127.0.0.1/24", false);
1937+
final GetVmIpAddressCommand getVmIpAddrCmd = new GetVmIpAddressCommand("Test", "127.0.0.1/24", "00:1b:63:84:45:e6", false);
19381938

19391939
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
19401940
assertNotNull(wrapper);

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -763,17 +763,22 @@ public VmIpAddrFetchThread(long vmId, String vmUuid, long nicId, String instance
763763

764764
@Override
765765
protected void runInContext() {
766-
GetVmIpAddressCommand cmd = new GetVmIpAddressCommand(vmName, networkCidr, isWindows);
766+
NicVO nic = _nicDao.findById(nicId);
767+
GetVmIpAddressCommand cmd = new GetVmIpAddressCommand(vmName, networkCidr, nic.getMacAddress(), isWindows);
767768
boolean decrementCount = true;
768769

769-
NicVO nic = _nicDao.findById(nicId);
770770
try {
771771
logger.debug("Trying IP retrieval for VM [id: {}, uuid: {}, name: {}], nic {}", vmId, vmUuid, vmName, nic);
772772
Answer answer = _agentMgr.send(hostId, cmd);
773773
if (answer.getResult()) {
774774
String vmIp = answer.getDetails();
775-
776-
if (NetUtils.isValidIp4(vmIp)) {
775+
if (null == vmIp) {
776+
// we got a valid response and the NIC does not have an IP assigned, as such we will update the database with null
777+
if (nic.getIPv4Address() != null) {
778+
nic.setIPv4Address(null);
779+
_nicDao.update(nicId, nic);
780+
}
781+
} else if (NetUtils.isValidIp4(vmIp)) {
777782
// set this vm ip addr in vm nic.
778783
if (nic != null) {
779784
nic.setIPv4Address(vmIp);
@@ -788,12 +793,6 @@ protected void runInContext() {
788793
}
789794
}
790795
} else {
791-
//previously vm has ip and nic table has ip address. After vm restart or stop/start
792-
//if vm doesnot get the ip then set the ip in nic table to null
793-
if (nic.getIPv4Address() != null) {
794-
nic.setIPv4Address(null);
795-
_nicDao.update(nicId, nic);
796-
}
797796
if (answer.getDetails() != null) {
798797
logger.debug("Failed to get vm ip for Vm [id: {}, uuid: {}, name: {}], details: {}",
799798
vmId, vmUuid, vmName, answer.getDetails());

0 commit comments

Comments
 (0)