From fc504042f66ad7eff6adc4e60a0934d0c255a3cd Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Fri, 15 Mar 2024 18:48:29 +0900 Subject: [PATCH] Rename BpfNetMapsReader to NetworkStackBpfNetMaps BpfNetMapsReader is loaded to bootclasspath. So this class should not contain unnecessary codes. To make this clear, this CL renames to NetowrkStackBpfNetMaps and add comments Bug: 328732146 Test: TH Change-Id: I557627c9fae087eb6119c2ae31d46526fa9e5d7e --- .../src/android/net/ConnectivityManager.java | 2 +- ...eader.java => NetworkStackBpfNetMaps.java} | 19 +++++++++------- .../src/com/android/server/BpfNetMaps.java | 12 +++------- ...rTest.kt => NetworkStackBpfNetMapsTest.kt} | 22 ++++++++++++------- 4 files changed, 29 insertions(+), 26 deletions(-) rename framework/src/android/net/{BpfNetMapsReader.java => NetworkStackBpfNetMaps.java} (94%) rename tests/unit/java/android/net/{BpfNetMapsReaderTest.kt => NetworkStackBpfNetMapsTest.kt} (95%) diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java index 915ec52e77..6f57ca8503 100644 --- a/framework/src/android/net/ConnectivityManager.java +++ b/framework/src/android/net/ConnectivityManager.java @@ -6286,7 +6286,7 @@ public class ConnectivityManager { if (!SdkLevel.isAtLeastU()) { Log.wtf(TAG, "isUidNetworkingBlocked is not supported on pre-U devices"); } - final BpfNetMapsReader reader = BpfNetMapsReader.getInstance(); + final NetworkStackBpfNetMaps reader = NetworkStackBpfNetMaps.getInstance(); // Note that before V, the data saver status in bpf is written by ConnectivityService // when receiving {@link #ACTION_RESTRICT_BACKGROUND_CHANGED}. Thus, // the status is not synchronized. diff --git a/framework/src/android/net/BpfNetMapsReader.java b/framework/src/android/net/NetworkStackBpfNetMaps.java similarity index 94% rename from framework/src/android/net/BpfNetMapsReader.java rename to framework/src/android/net/NetworkStackBpfNetMaps.java index ee422abd9c..72da328cb3 100644 --- a/framework/src/android/net/BpfNetMapsReader.java +++ b/framework/src/android/net/NetworkStackBpfNetMaps.java @@ -46,12 +46,14 @@ import com.android.net.module.util.Struct.U32; import com.android.net.module.util.Struct.U8; /** - * A helper class to *read* java BpfMaps. + * A helper class to *read* java BpfMaps for network stack. + * BpfMap operations that are not used from network stack should be in + * {@link com.android.server.BpfNetMaps} * @hide */ @RequiresApi(Build.VERSION_CODES.TIRAMISU) // BPF maps were only mainlined in T -public class BpfNetMapsReader { - private static final String TAG = BpfNetMapsReader.class.getSimpleName(); +public class NetworkStackBpfNetMaps { + private static final String TAG = NetworkStackBpfNetMaps.class.getSimpleName(); // Locally store the handle of bpf maps. The FileDescriptors are statically cached inside the // BpfMap implementation. @@ -86,15 +88,15 @@ public class BpfNetMapsReader { } private static class SingletonHolder { - static final BpfNetMapsReader sInstance = new BpfNetMapsReader(); + static final NetworkStackBpfNetMaps sInstance = new NetworkStackBpfNetMaps(); } @NonNull - public static BpfNetMapsReader getInstance() { + public static NetworkStackBpfNetMaps getInstance() { return SingletonHolder.sInstance; } - private BpfNetMapsReader() { + private NetworkStackBpfNetMaps() { this(new Dependencies()); } @@ -102,10 +104,11 @@ public class BpfNetMapsReader { // concurrent access, the test needs to use a non-static approach for dependency injection and // mocking virtual bpf maps. @VisibleForTesting - public BpfNetMapsReader(@NonNull Dependencies deps) { + public NetworkStackBpfNetMaps(@NonNull Dependencies deps) { if (!SdkLevel.isAtLeastT()) { throw new UnsupportedOperationException( - BpfNetMapsReader.class.getSimpleName() + " is not supported below Android T"); + NetworkStackBpfNetMaps.class.getSimpleName() + + " is not supported below Android T"); } mDeps = deps; mConfigurationMap = mDeps.getConfigurationMap(); diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java index a7fddd08f0..487f25ce87 100644 --- a/service/src/com/android/server/BpfNetMaps.java +++ b/service/src/com/android/server/BpfNetMaps.java @@ -49,8 +49,8 @@ import static com.android.server.ConnectivityStatsLog.NETWORK_BPF_MAP_INFO; import android.app.StatsManager; import android.content.Context; -import android.net.BpfNetMapsReader; import android.net.INetd; +import android.net.NetworkStackBpfNetMaps; import android.net.UidOwnerValue; import android.os.Build; import android.os.RemoteException; @@ -535,14 +535,11 @@ public class BpfNetMaps { * @throws UnsupportedOperationException if called on pre-T devices. * @throws ServiceSpecificException in case of failure, with an error code indicating the * cause of the failure. - * - * @deprecated Use {@link BpfNetMapsReader#isChainEnabled} instead. */ - // TODO: Migrate the callers to use {@link BpfNetMapsReader#isChainEnabled} instead. @Deprecated @RequiresApi(Build.VERSION_CODES.TIRAMISU) public boolean isChainEnabled(final int childChain) { - return BpfNetMapsReader.isChainEnabled(sConfigurationMap, childChain); + return NetworkStackBpfNetMaps.isChainEnabled(sConfigurationMap, childChain); } private Set asSet(final int[] uids) { @@ -635,12 +632,9 @@ public class BpfNetMaps { * @throws UnsupportedOperationException if called on pre-T devices. * @throws ServiceSpecificException in case of failure, with an error code indicating the * cause of the failure. - * - * @deprecated use {@link BpfNetMapsReader#getUidRule} instead. */ - // TODO: Migrate the callers to use {@link BpfNetMapsReader#getUidRule} instead. public int getUidRule(final int childChain, final int uid) { - return BpfNetMapsReader.getUidRule(sUidOwnerMap, childChain, uid); + return NetworkStackBpfNetMaps.getUidRule(sUidOwnerMap, childChain, uid); } private Set getUidsMatchEnabled(final int childChain) throws ErrnoException { diff --git a/tests/unit/java/android/net/BpfNetMapsReaderTest.kt b/tests/unit/java/android/net/NetworkStackBpfNetMapsTest.kt similarity index 95% rename from tests/unit/java/android/net/BpfNetMapsReaderTest.kt rename to tests/unit/java/android/net/NetworkStackBpfNetMapsTest.kt index 8919666551..9238c86b23 100644 --- a/tests/unit/java/android/net/BpfNetMapsReaderTest.kt +++ b/tests/unit/java/android/net/NetworkStackBpfNetMapsTest.kt @@ -50,7 +50,7 @@ private const val NO_IIF = 0 // pre-T devices does not support Bpf. @RunWith(DevSdkIgnoreRunner::class) @IgnoreUpTo(VERSION_CODES.S_V2) -class BpfNetMapsReaderTest { +class NetworkStackBpfNetMapsTest { @Rule @JvmField val ignoreRule = DevSdkIgnoreRule() @@ -58,14 +58,15 @@ class BpfNetMapsReaderTest { private val testConfigurationMap: IBpfMap = TestBpfMap() private val testUidOwnerMap: IBpfMap = TestBpfMap() private val testDataSaverEnabledMap: IBpfMap = TestBpfMap() - private val bpfNetMapsReader = BpfNetMapsReader( - TestDependencies(testConfigurationMap, testUidOwnerMap, testDataSaverEnabledMap)) + private val bpfNetMapsReader = NetworkStackBpfNetMaps( + TestDependencies(testConfigurationMap, testUidOwnerMap, testDataSaverEnabledMap) + ) class TestDependencies( private val configMap: IBpfMap, private val uidOwnerMap: IBpfMap, private val dataSaverEnabledMap: IBpfMap - ) : BpfNetMapsReader.Dependencies() { + ) : NetworkStackBpfNetMaps.Dependencies() { override fun getConfigurationMap() = configMap override fun getUidOwnerMap() = uidOwnerMap override fun getDataSaverEnabledMap() = dataSaverEnabledMap @@ -99,11 +100,16 @@ class BpfNetMapsReaderTest { Modifier.isStatic(it.modifiers) && it.name.startsWith("FIREWALL_CHAIN_") } // Verify the size matches, this also verifies no common item in allow and deny chains. - assertEquals(BpfNetMapsConstants.ALLOW_CHAINS.size + - BpfNetMapsConstants.DENY_CHAINS.size, declaredChains.size) + assertEquals( + BpfNetMapsConstants.ALLOW_CHAINS.size + + BpfNetMapsConstants.DENY_CHAINS.size, + declaredChains.size + ) declaredChains.forEach { - assertTrue(BpfNetMapsConstants.ALLOW_CHAINS.contains(it.get(null)) || - BpfNetMapsConstants.DENY_CHAINS.contains(it.get(null))) + assertTrue( + BpfNetMapsConstants.ALLOW_CHAINS.contains(it.get(null)) || + BpfNetMapsConstants.DENY_CHAINS.contains(it.get(null)) + ) } }