From 85d98ea79f9f4ded1b22b72369e3f8db68708aa7 Mon Sep 17 00:00:00 2001 From: Hungming Chen Date: Sat, 18 Feb 2023 21:06:04 +0800 Subject: [PATCH] BpfCoordinator: refactor updateUpstreamNetworkState to get MTU This is a preparation for CLAT offload. Move the getting the MTU into a function for refactoring updateUpstreamNetworkState() which could be simpler. Bug: 258637850 Test: atest BpfCoordinatorTest Change-Id: I8f22b022d4d00597810b56ab6e3e55ed7ab9076d --- .../tethering/BpfCoordinator.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index 9f8d9b1a0a..976f5df143 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java @@ -37,6 +37,7 @@ import static com.android.networkstack.tethering.util.TetheringUtils.getTetherin import android.app.usage.NetworkStatsManager; import android.net.INetd; +import android.net.LinkProperties; import android.net.MacAddress; import android.net.NetworkStats; import android.net.NetworkStats.Entry; @@ -878,6 +879,27 @@ public class BpfCoordinator { return true; } + private int getMtu(@NonNull final String ifaceName, @NonNull final LinkProperties lp) { + int mtu = INVALID_MTU; + + if (ifaceName.equals(lp.getInterfaceName())) { + mtu = lp.getMtu(); + } + + // Get mtu via kernel if mtu is not found in LinkProperties. + if (mtu == INVALID_MTU) { + mtu = mDeps.getNetworkInterfaceMtu(ifaceName); + } + + // Use default mtu if can't find any. + if (mtu == INVALID_MTU) mtu = NetworkStackConstants.ETHER_MTU; + + // Clamp to minimum ipv4 mtu + if (mtu < IPV4_MIN_MTU) mtu = IPV4_MIN_MTU; + + return mtu; + } + /** * Call when UpstreamNetworkState may be changed. * If upstream has ipv4 for tethering, update this new UpstreamNetworkState @@ -900,16 +922,7 @@ public class BpfCoordinator { final String ifaceName = ns.linkProperties.getInterfaceName(); final InterfaceParams params = mDeps.getInterfaceParams(ifaceName); final boolean isVcn = isVcnInterface(ifaceName); - mtu = ns.linkProperties.getMtu(); - if (mtu == INVALID_MTU) { - // Get mtu via kernel if mtu is not found in LinkProperties. - mtu = mDeps.getNetworkInterfaceMtu(ifaceName); - } - - // Use default mtu if can't find any. - if (mtu == INVALID_MTU) mtu = NetworkStackConstants.ETHER_MTU; - // Clamp to minimum ipv4 mtu - if (mtu < IPV4_MIN_MTU) mtu = IPV4_MIN_MTU; + mtu = getMtu(ifaceName, ns.linkProperties); if (!isVcn && params != null && !params.hasMacAddress /* raw ip upstream only */) { upstreamIndex = params.index;