Skip to content

Commit 0a2c926

Browse files
committed
Merge branch 'source_interface_fix' into management_ip_addr_resolution
2 parents d2c8aa7 + 7f659e4 commit 0a2c926

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

agent/src/main/java/com/cloud/agent/properties/AgentProperties.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public class AgentProperties{
108108
*/
109109
public static final Property<String> PRIVATE_NETWORK_DEVICE = new Property<>("private.network.device", "cloudbr1");
110110

111+
/**
112+
* Private NIC device address. If this property is commented, it will be autodetected on service startup.<br>
113+
* Data type: String.<br>
114+
* Default value: <code>cloudbr1</code>
115+
*/
116+
public static final Property<String> PRIVATE_NETWORK_DEVICE_ADDRESS = new Property<>("private.network.address", null, String.class);
117+
111118
/**
112119
* Guest NIC device. If this property is commented, the value of the private NIC device will be used.<br>
113120
* Data type: String.<br>

core/src/main/java/com/cloud/resource/ServerResourceBase.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@
2222
import java.io.File;
2323
import java.io.PrintWriter;
2424
import java.io.StringWriter;
25+
import java.net.DatagramSocket;
26+
import java.net.InetAddress;
27+
import java.net.InetSocketAddress;
2528
import java.net.NetworkInterface;
2629
import java.net.SocketException;
2730
import java.util.ArrayList;
2831
import java.util.Arrays;
2932
import java.util.Collections;
3033
import java.util.Date;
3134
import java.util.LinkedList;
35+
import java.util.LinkedHashSet;
3236
import java.util.List;
3337
import java.util.Map;
38+
import java.util.Set;
3439

3540
import javax.naming.ConfigurationException;
3641

@@ -75,6 +80,12 @@ public boolean configure(final String name, Map<String, Object> params) throws C
7580

7681
defineResourceNetworkInterfaces(params);
7782

83+
if (privateNic == null) {
84+
checkForPrivateInterfaceDefinedByIp(params);
85+
}
86+
if (privateNic == null) {
87+
tryToAutoDiscoverResourcePrivateNetworkInterfaceByRouteLookup(params);
88+
}
7889
if (privateNic == null) {
7990
tryToAutoDiscoverResourcePrivateNetworkInterface();
8091
}
@@ -106,8 +117,74 @@ protected void defineResourceNetworkInterfaces(Map<String, Object> params) {
106117
this.storageNic2 = NetUtils.getNetworkInterface(storageNic2);
107118
}
108119

120+
private void checkForPrivateInterfaceDefinedByIp(Map<String, Object> params) {
121+
final String ifAddr = (String) params.get("private.network.address");
122+
if (ifAddr != null) {
123+
logger.debug(String.format("Trying to use private address to resolve interface: [%s]", ifAddr));
124+
try {
125+
InetAddress rawAddr = InetAddress.getByAddress(InetAddress.getByName(ifAddr).getAddress());
126+
final NetworkInterface nic = NetworkInterface.getByInetAddress(rawAddr);
127+
if (nic != null) {
128+
logger.info(String.format("Using NIC [%s] as private NIC. Source: InterfaceAddress [%s]", nic, ifAddr));
129+
privateNic = nic;
130+
} else {
131+
logger.info(String.format("Unable to found private NIC with defined ip [%s]", ifAddr));
132+
}
133+
} catch (Throwable e) {
134+
// Logging only, if this method was unnable to find a valid interface, iteration will be tested
135+
logger.info(String.format("Unable to use private address to get the management interface: [%s]", e.getMessage()));
136+
}
137+
}
138+
}
139+
140+
private String[] collectMgmtHostIp(Map<String, Object> params) {
141+
final String hosts = (String) params.get("host");
142+
if (hosts != null && hosts.trim().length() > 0) {
143+
logger.info(String.format("Parsing host setting: [%s]", hosts));
144+
final Set<String> hostCollection = new LinkedHashSet<String>();
145+
if (hosts.contains(",")) {
146+
for(final String ip : hosts.split(",")) {
147+
final String h = ip.contains("@") ? ip.split("@")[0] : ip;
148+
hostCollection.add(h.trim());
149+
}
150+
} else {
151+
hostCollection.add(hosts.trim());
152+
}
153+
return hostCollection.isEmpty() ? null : hostCollection.toArray(new String[0]);
154+
}
155+
return null;
156+
}
157+
158+
protected void tryToAutoDiscoverResourcePrivateNetworkInterfaceByRouteLookup(Map<String, Object> params) throws ConfigurationException {
159+
logger.info("Trying to autodiscover this resource's private network interface by route lookup");
160+
final String[] mgmtIps = collectMgmtHostIp(params);
161+
if (mgmtIps == null) {
162+
logger.info("Unable to resolve any management server ip address. Aborting private network search by route lookup.");
163+
return;
164+
}
165+
for (String mgmtIp : mgmtIps) {
166+
logger.info(String.format("Using management server IP [%s] to lookup", mgmtIp));
167+
try {
168+
try (DatagramSocket socket = new DatagramSocket()) {
169+
socket.connect(new InetSocketAddress(mgmtIp, 8250));
170+
// Asking for source address to mgmgt destination to O.S routing tables.
171+
InetAddress localAddress = socket.getLocalAddress();
172+
NetworkInterface nic = NetworkInterface.getByInetAddress(localAddress);
173+
if (nic != null) {
174+
logger.info(String.format("Using NIC [%s] as private NIC.", nic));
175+
privateNic = nic;
176+
break;
177+
}
178+
}
179+
} catch (Throwable e) {
180+
// Logging only, if this method was unnable to find a valid interface, iteration will be tested
181+
logger.debug(String.format("Unable to use routing table to determine private management interface: [%s]", e.getMessage()));
182+
}
183+
}
184+
}
185+
109186
protected void tryToAutoDiscoverResourcePrivateNetworkInterface() throws ConfigurationException {
110-
logger.info("Trying to autodiscover this resource's private network interface.");
187+
logger.info("Trying to autodiscover this resource's private network interface by simple iteration.");
111188

112189
List<NetworkInterface> nics;
113190
try {

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,8 @@ protected String getDefaultTungstenScriptsDir() {
10511051

10521052
@Override
10531053
public boolean configure(final String name, final Map<String, Object> params) throws ConfigurationException {
1054+
params.put(AgentProperties.HOST.getName(), AgentPropertiesFileHandler.getPropertyValue(AgentProperties.HOST));
1055+
params.put(AgentProperties.PRIVATE_NETWORK_DEVICE_ADDRESS.getName(), AgentPropertiesFileHandler.getPropertyValue(AgentProperties.PRIVATE_NETWORK_DEVICE_ADDRESS));
10541056
boolean success = super.configure(name, params);
10551057
if (!success) {
10561058
return false;

0 commit comments

Comments
 (0)