Skip to content

Commit 038b24d

Browse files
authored
utils: do not require an IPv4 default route to find the default NIC (#14060)
A management server does not need an IPv4 default route. It can be IPv6-first, or reach its POD network and KVM agents over IPv4 without having a default gateway on that family at all. getDefaultEthDevice() only looked at the IPv4 routing table and picked column 5 of the route as the device name: ip route show default 0.0.0.0/0 | head -1 | awk '{print $5}' That has two problems: * Anything the shell or 'ip' writes on stderr ends up in the captured output (Script merges stderr into stdout), so the method can return a string that is not a device name at all. getAllDefaultNicIps() then passes it to NetworkInterface.getByName(), which returns null, and the unchecked dereference throws a NullPointerException. On a host without an IPv4 default route this aborts the boot of the management server: ERROR [o.a.c.s.l.CloudStackExtendedLifeCycle] Error on configuring bean RootCAProvider - Cannot invoke "java.net.NetworkInterface.getInterfaceAddresses()" because "nic" is null at com.cloud.utils.net.NetUtils.getAllDefaultNicIps(NetUtils.java:298) at o.a.c.ca.provider.RootCAProvider.loadManagementKeyStore(RootCAProvider.java:409) ERROR [o.a.c.s.m.m.i.DefaultModuleDefinitionSet] Failed to load module [root-ca] * Column 5 is only the device for routes of the form "default via <gw> dev <name> ...". For an on-link default route such as "default dev eth0 scope link" or "default dev eno1 proto kernel metric 256" it returns "link" or "kernel". The device name is now taken from the token following "dev", which is correct for every route layout including multipath routes, and the IPv6 routing table is consulted when there is no IPv4 default route. The results of NetworkInterface.getByName() are null checked in both getDefaultHostIp() and getAllDefaultNicIps() so an unresolvable device name degrades to "no default NIC" instead of an exception.
1 parent 2737087 commit 038b24d

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎utils/src/main/java/com/cloud/utils/net/NetUtils.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ public static String getDefaultHostIp() {
267267
return null;
268268
}
269269

270+
if (nic == null) {
271+
LOGGER.warn("Unable to find a network interface named [{}], cannot determine the default host IP.", pubNic);
272+
return null;
273+
}
274+
270275
String[] info = null;
271276
try {
272277
info = NetUtils.getNetworkParams(nic);
@@ -295,6 +300,11 @@ public static List<String> getAllDefaultNicIps() {
295300
return addrs;
296301
}
297302

303+
if (nic == null) {
304+
LOGGER.warn("Unable to find a network interface named [{}], no default NIC IPs will be returned.", pubNic);
305+
return addrs;
306+
}
307+
298308
for (InterfaceAddress address : nic.getInterfaceAddresses()) {
299309
addrs.add(address.getAddress().getHostAddress().split("%")[0]);
300310
}
@@ -306,7 +316,25 @@ public static String getDefaultEthDevice() {
306316
final String defDev = Script.runSimpleBashScript("/sbin/route -n get default 2> /dev/null | grep interface | awk '{print $2}'");
307317
return defDev;
308318
}
309-
return Script.runSimpleBashScript("ip route show default 0.0.0.0/0 | head -1 | awk '{print $5}'");
319+
final String defaultIp4Device = getDefaultEthDevice(false);
320+
if (defaultIp4Device != null) {
321+
return defaultIp4Device;
322+
}
323+
LOGGER.debug("No IPv4 default route found, falling back to the IPv6 default route to determine the default network device.");
324+
return getDefaultEthDevice(true);
325+
}
326+
327+
/**
328+
* Returns the name of the network device used by the IPv4 or IPv6 default route, or null when there is no such
329+
* default route.
330+
*
331+
* An IPv4 default route is not a requirement, a host can be IPv6-only or have IPv4 connectivity without a default
332+
* route, therefore both address families are looked up separately.
333+
*/
334+
protected static String getDefaultEthDevice(final boolean ipv6) {
335+
final String command = String.format("ip -%d route show default | awk '{for (i = 1; i < NF; i++) if ($i == \"dev\") {print $(i + 1); exit}}'",
336+
ipv6 ? 6 : 4);
337+
return Script.runSimpleBashScript(command);
310338
}
311339

312340
public static String getLocalIPString() {

‎utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@
4343
import java.util.TreeSet;
4444
import java.util.stream.Collectors;
4545

46+
import org.apache.commons.lang3.SystemUtils;
4647
import org.junit.Assert;
48+
import org.junit.Assume;
4749
import org.junit.Test;
4850

4951
import com.cloud.utils.Pair;
5052
import com.cloud.utils.exception.CloudRuntimeException;
5153
import com.cloud.utils.net.NetUtils.SupersetOrSubset;
54+
import com.cloud.utils.script.Script;
5255
import com.googlecode.ipv6.IPv6Address;
5356
import com.googlecode.ipv6.IPv6Network;
5457

@@ -748,6 +751,41 @@ public void testAllIpsOfDefaultNic() {
748751
}
749752
}
750753

754+
@Test
755+
public void testAllIpsOfDefaultNicWhenDeviceDoesNotExist() {
756+
try (MockedStatic<Script> scriptMocked = Mockito.mockStatic(Script.class)) {
757+
scriptMocked.when(() -> Script.runSimpleBashScript(Mockito.anyString())).thenReturn("nonexistent0");
758+
Assert.assertTrue(NetUtils.getAllDefaultNicIps().isEmpty());
759+
}
760+
}
761+
762+
@Test
763+
public void testAllIpsOfDefaultNicWithoutDefaultRoute() {
764+
try (MockedStatic<Script> scriptMocked = Mockito.mockStatic(Script.class)) {
765+
scriptMocked.when(() -> Script.runSimpleBashScript(Mockito.anyString())).thenReturn(null);
766+
Assert.assertTrue(NetUtils.getAllDefaultNicIps().isEmpty());
767+
}
768+
}
769+
770+
@Test
771+
public void testGetDefaultEthDeviceFallsBackToIpv6() {
772+
Assume.assumeFalse(SystemUtils.IS_OS_MAC);
773+
try (MockedStatic<Script> scriptMocked = Mockito.mockStatic(Script.class)) {
774+
scriptMocked.when(() -> Script.runSimpleBashScript(Mockito.contains("ip -4"))).thenReturn(null);
775+
scriptMocked.when(() -> Script.runSimpleBashScript(Mockito.contains("ip -6"))).thenReturn("eth0");
776+
Assert.assertEquals("eth0", NetUtils.getDefaultEthDevice());
777+
}
778+
}
779+
780+
@Test
781+
public void testGetDefaultEthDevicePrefersIpv4() {
782+
Assume.assumeFalse(SystemUtils.IS_OS_MAC);
783+
try (MockedStatic<Script> scriptMocked = Mockito.mockStatic(Script.class)) {
784+
scriptMocked.when(() -> Script.runSimpleBashScript(Mockito.contains("ip -4"))).thenReturn("eth0");
785+
Assert.assertEquals("eth0", NetUtils.getDefaultEthDevice());
786+
}
787+
}
788+
751789
@Test
752790
public void testIsIPv6EUI64() {
753791
assertTrue(NetUtils.isIPv6EUI64("fe80::5054:8fff:fe9f:af61"));

0 commit comments

Comments
 (0)