From 991a628dfeeab80dee38d637e115c004dca11b4c Mon Sep 17 00:00:00 2001 From: Jimi Chen Date: Thu, 15 May 2025 12:26:29 +0000 Subject: [PATCH] Add getInterfaceMtu method in ProcfsParsingUtils Flag: EXEMPT mainline Bug: 406085682 Test: atest NetworkStackTests:com.android.networkstack.util.ProcfsParsingUtilsTest Change-Id: I62e129c3903e0c432f4a4d84c7024cfb4e140dd5 --- .../net/module/util/ProcfsParsingUtils.java | 59 ++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/staticlibs/device/com/android/net/module/util/ProcfsParsingUtils.java b/staticlibs/device/com/android/net/module/util/ProcfsParsingUtils.java index 44fc94121b..151da67440 100644 --- a/staticlibs/device/com/android/net/module/util/ProcfsParsingUtils.java +++ b/staticlibs/device/com/android/net/module/util/ProcfsParsingUtils.java @@ -39,6 +39,12 @@ import java.util.List; public final class ProcfsParsingUtils { public static final String TAG = ProcfsParsingUtils.class.getSimpleName(); + // rfc894 describes the minimum length of the data field of a packet sent over an + // Ethernet is 1500 octets, thus the maximum length of an IP datagram + // sent over an Ethernet is 1500 octets. + public static final int DEFAULT_MTU = 1500; + public static final int DEFAULT_TRAFFIC_CLASS = 0; + private static final Inet4Address IPV4_ADDR_ALL_HOST_MULTICAST = (Inet4Address) InetAddresses.parseNumericAddress("224.0.0.1"); @@ -48,6 +54,7 @@ public final class ProcfsParsingUtils { private static final String IPV4_MCAST_PATH = "/proc/net/igmp"; private static final String IPV6_MCAST_PATH = "/proc/net/igmp6"; private static final String IPV4_DEFAULT_TTL_PATH = "/proc/sys/net/ipv4/ip_default_ttl"; + private static final String SYS_CLASS_NET_PATH = "/sys/class/net"; private ProcfsParsingUtils() { } @@ -86,10 +93,15 @@ public final class ProcfsParsingUtils { @VisibleForTesting public static int parseNdTrafficClass(final List lines) { if (lines.size() != 1) { - return 0; // default + return DEFAULT_TRAFFIC_CLASS; } - return Integer.parseInt(lines.get(0)); + try { + return Integer.parseInt(lines.get(0)); + } catch (NumberFormatException e) { + Log.e(TAG, "failed to parse ND traffic class.", e); + return DEFAULT_TRAFFIC_CLASS; + } } /** @@ -109,6 +121,32 @@ public final class ProcfsParsingUtils { } } + /** + * Parses an interface's Maximum Transmission Unit (MTU) value from a list of strings. + * + * This function expects a list containing a single string representing the interface MTU. + * If the list is empty or contains multiple lines, it assumes a default value for 1500. + * + * @param lines A list of strings, expected to contain the MTU value as its sole element. + * @return The parsed MTU value as an integer. Returns `IPV4_MIN_MTU` if `ipv4` is true + * and the input `lines` size is not 1, otherwise returns `IPV6_MIN_MTU` if `ipv4` + * is false and the input `lines` size is not 1. If `lines` has exactly one + * element, returns the integer parsed from that string. + */ + @VisibleForTesting + public static int parseInterfaceMtu(final List lines) { + if (lines.size() != 1) { + return DEFAULT_MTU; + } + + try { + return Integer.parseInt(lines.get(0)); + } catch (NumberFormatException e) { + Log.e(TAG, "failed to parse interface mtu.", e); + return DEFAULT_MTU; + } + } + /** * Parses anycast6 addresses associated with a specific interface from a list of strings. * @@ -296,6 +334,23 @@ public final class ProcfsParsingUtils { return parseNdTrafficClass(lines); } + /** + * Returns the interface MTU for the specified interface. + * The function loads the existing interface MTU from the file + * `/sys/class/net/{ifname}/mtu`. If the file does not exist, the + * function returns the default value 1280. + * rfc8200#5 describes IPv6 requires that every link in the Internet have an MTU of 1280 + * octets or greater. This is known as the IPv6 minimum link MTU. + * + * @param ifname The name of the interface. + * @return The traffic class for the interface. + */ + public static int getInterfaceMtu(final String ifname) { + final String mtuPath = SYS_CLASS_NET_PATH + ifname + "/mtu"; + final List lines = readFile(mtuPath); + return parseInterfaceMtu(lines); + } + /** * The function loads the existing IPv6 anycast address from the file `/proc/net/anycast6`. * If the file does not exist or the interface is not found, the function