From 06155c8040d6dc77199d520f31693783dc6f90fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Thu, 16 Nov 2023 22:41:08 -0800 Subject: [PATCH] BpfMap - add R/W constructor alias MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test: TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: I7cef2b19f55e6e056b46bb1b28f85c7f7376f6f4 --- .../networkstack/tethering/BpfCoordinator.java | 14 +++++++------- .../android/networkstack/tethering/BpfMapTest.java | 10 +++++----- .../android/server/net/BpfInterfaceMapUpdater.java | 2 +- .../android/server/net/NetworkStatsService.java | 12 ++++++------ service/src/com/android/server/BpfNetMaps.java | 12 ++++++------ .../server/connectivity/ClatCoordinator.java | 6 +++--- .../server/connectivity/DscpPolicyTracker.java | 4 ++-- .../com/android/net/module/util/BpfBitmap.java | 3 +-- .../device/com/android/net/module/util/BpfMap.java | 11 +++++++++++ 9 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index 9f542f4aef..81e18abf9b 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java @@ -379,7 +379,7 @@ public class BpfCoordinator { if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_DOWNSTREAM4_MAP_PATH, - BpfMap.BPF_F_RDWR, Tether4Key.class, Tether4Value.class); + Tether4Key.class, Tether4Value.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create downstream4 map: " + e); return null; @@ -391,7 +391,7 @@ public class BpfCoordinator { if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_UPSTREAM4_MAP_PATH, - BpfMap.BPF_F_RDWR, Tether4Key.class, Tether4Value.class); + Tether4Key.class, Tether4Value.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create upstream4 map: " + e); return null; @@ -403,7 +403,7 @@ public class BpfCoordinator { if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH, - BpfMap.BPF_F_RDWR, TetherDownstream6Key.class, Tether6Value.class); + TetherDownstream6Key.class, Tether6Value.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create downstream6 map: " + e); return null; @@ -414,7 +414,7 @@ public class BpfCoordinator { @Nullable public IBpfMap getBpfUpstream6Map() { if (!isAtLeastS()) return null; try { - return new BpfMap<>(TETHER_UPSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR, + return new BpfMap<>(TETHER_UPSTREAM6_FS_PATH, TetherUpstream6Key.class, Tether6Value.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create upstream6 map: " + e); @@ -427,7 +427,7 @@ public class BpfCoordinator { if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_STATS_MAP_PATH, - BpfMap.BPF_F_RDWR, TetherStatsKey.class, TetherStatsValue.class); + TetherStatsKey.class, TetherStatsValue.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create stats map: " + e); return null; @@ -439,7 +439,7 @@ public class BpfCoordinator { if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_LIMIT_MAP_PATH, - BpfMap.BPF_F_RDWR, TetherLimitKey.class, TetherLimitValue.class); + TetherLimitKey.class, TetherLimitValue.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create limit map: " + e); return null; @@ -451,7 +451,7 @@ public class BpfCoordinator { if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_DEV_MAP_PATH, - BpfMap.BPF_F_RDWR, TetherDevKey.class, TetherDevValue.class); + TetherDevKey.class, TetherDevValue.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create dev map: " + e); return null; diff --git a/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java b/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java index 0e8b044b1a..d5d71bc1a7 100644 --- a/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java +++ b/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java @@ -84,7 +84,7 @@ public final class BpfMapTest { private void initTestMap() throws Exception { mTestMap = new BpfMap<>( - TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR, + TETHER_DOWNSTREAM6_FS_PATH, TetherDownstream6Key.class, Tether6Value.class); mTestMap.forEach((key, value) -> { @@ -135,7 +135,7 @@ public final class BpfMapTest { assertEquals(OsConstants.EPERM, expected.errno); } } - try (BpfMap readWriteMap = new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR, + try (BpfMap readWriteMap = new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH, TetherDownstream6Key.class, Tether6Value.class)) { assertNotNull(readWriteMap); } @@ -389,7 +389,7 @@ public final class BpfMapTest { public void testOpenNonexistentMap() throws Exception { try { final BpfMap nonexistentMap = new BpfMap<>( - "/sys/fs/bpf/tethering/nonexistent", BpfMap.BPF_F_RDWR, + "/sys/fs/bpf/tethering/nonexistent", TetherDownstream6Key.class, Tether6Value.class); } catch (ErrnoException expected) { assertEquals(OsConstants.ENOENT, expected.errno); @@ -409,8 +409,8 @@ public final class BpfMapTest { final int before = getNumOpenFds(); for (int i = 0; i < iterations; i++) { try (BpfMap map = new BpfMap<>( - TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR, - TetherDownstream6Key.class, Tether6Value.class)) { + TETHER_DOWNSTREAM6_FS_PATH, + TetherDownstream6Key.class, Tether6Value.class)) { // do nothing } } diff --git a/service-t/src/com/android/server/net/BpfInterfaceMapUpdater.java b/service-t/src/com/android/server/net/BpfInterfaceMapUpdater.java index 27c0f9f5cd..4ec1562acb 100644 --- a/service-t/src/com/android/server/net/BpfInterfaceMapUpdater.java +++ b/service-t/src/com/android/server/net/BpfInterfaceMapUpdater.java @@ -66,7 +66,7 @@ public class BpfInterfaceMapUpdater { /** Create BpfMap for updating interface and index mapping. */ public IBpfMap getInterfaceMap() { try { - return new BpfMap<>(IFACE_INDEX_NAME_MAP_PATH, BpfMap.BPF_F_RDWR, + return new BpfMap<>(IFACE_INDEX_NAME_MAP_PATH, S32.class, InterfaceMapValue.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create interface map: " + e); diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java index a36149ef81..78c4504977 100644 --- a/service-t/src/com/android/server/net/NetworkStatsService.java +++ b/service-t/src/com/android/server/net/NetworkStatsService.java @@ -808,7 +808,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { /** Get counter sets map for each UID. */ public IBpfMap getUidCounterSetMap() { try { - return new BpfMap(UID_COUNTERSET_MAP_PATH, BpfMap.BPF_F_RDWR, + return new BpfMap(UID_COUNTERSET_MAP_PATH, S32.class, U8.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open uid counter set map: " + e); @@ -820,7 +820,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public IBpfMap getCookieTagMap() { try { return new BpfMap(COOKIE_TAG_MAP_PATH, - BpfMap.BPF_F_RDWR, CookieTagMapKey.class, CookieTagMapValue.class); + CookieTagMapKey.class, CookieTagMapValue.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open cookie tag map: " + e); return null; @@ -831,7 +831,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public IBpfMap getStatsMapA() { try { return new BpfMap(STATS_MAP_A_PATH, - BpfMap.BPF_F_RDWR, StatsMapKey.class, StatsMapValue.class); + StatsMapKey.class, StatsMapValue.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open stats map A: " + e); return null; @@ -842,7 +842,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public IBpfMap getStatsMapB() { try { return new BpfMap(STATS_MAP_B_PATH, - BpfMap.BPF_F_RDWR, StatsMapKey.class, StatsMapValue.class); + StatsMapKey.class, StatsMapValue.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open stats map B: " + e); return null; @@ -853,7 +853,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public IBpfMap getAppUidStatsMap() { try { return new BpfMap(APP_UID_STATS_MAP_PATH, - BpfMap.BPF_F_RDWR, UidStatsMapKey.class, StatsMapValue.class); + UidStatsMapKey.class, StatsMapValue.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open app uid stats map: " + e); return null; @@ -864,7 +864,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { public IBpfMap getIfaceStatsMap() { try { return new BpfMap(IFACE_STATS_MAP_PATH, - BpfMap.BPF_F_RDWR, S32.class, StatsMapValue.class); + S32.class, StatsMapValue.class); } catch (ErrnoException e) { throw new IllegalStateException("Failed to open interface stats map", e); } diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java index ad9cfbe10e..aa402859e3 100644 --- a/service/src/com/android/server/BpfNetMaps.java +++ b/service/src/com/android/server/BpfNetMaps.java @@ -187,7 +187,7 @@ public class BpfNetMaps { private static IBpfMap getConfigurationMap() { try { return new BpfMap<>( - CONFIGURATION_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U32.class); + CONFIGURATION_MAP_PATH, S32.class, U32.class); } catch (ErrnoException e) { throw new IllegalStateException("Cannot open netd configuration map", e); } @@ -196,7 +196,7 @@ public class BpfNetMaps { private static IBpfMap getUidOwnerMap() { try { return new BpfMap<>( - UID_OWNER_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, UidOwnerValue.class); + UID_OWNER_MAP_PATH, S32.class, UidOwnerValue.class); } catch (ErrnoException e) { throw new IllegalStateException("Cannot open uid owner map", e); } @@ -205,7 +205,7 @@ public class BpfNetMaps { private static IBpfMap getUidPermissionMap() { try { return new BpfMap<>( - UID_PERMISSION_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U8.class); + UID_PERMISSION_MAP_PATH, S32.class, U8.class); } catch (ErrnoException e) { throw new IllegalStateException("Cannot open uid permission map", e); } @@ -213,7 +213,7 @@ public class BpfNetMaps { private static IBpfMap getCookieTagMap() { try { - return new BpfMap<>(COOKIE_TAG_MAP_PATH, BpfMap.BPF_F_RDWR, + return new BpfMap<>(COOKIE_TAG_MAP_PATH, CookieTagMapKey.class, CookieTagMapValue.class); } catch (ErrnoException e) { throw new IllegalStateException("Cannot open cookie tag map", e); @@ -223,7 +223,7 @@ public class BpfNetMaps { private static IBpfMap getDataSaverEnabledMap() { try { return new BpfMap<>( - DATA_SAVER_ENABLED_MAP_PATH, BpfMap.BPF_F_RDWR, S32.class, U8.class); + DATA_SAVER_ENABLED_MAP_PATH, S32.class, U8.class); } catch (ErrnoException e) { throw new IllegalStateException("Cannot open data saver enabled map", e); } @@ -231,7 +231,7 @@ public class BpfNetMaps { private static IBpfMap getIngressDiscardMap() { try { - return new BpfMap<>(INGRESS_DISCARD_MAP_PATH, BpfMap.BPF_F_RDWR, + return new BpfMap<>(INGRESS_DISCARD_MAP_PATH, IngressDiscardKey.class, IngressDiscardValue.class); } catch (ErrnoException e) { throw new IllegalStateException("Cannot open ingress discard map", e); diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java index 17de1461fb..daaf91daf9 100644 --- a/service/src/com/android/server/connectivity/ClatCoordinator.java +++ b/service/src/com/android/server/connectivity/ClatCoordinator.java @@ -256,7 +256,7 @@ public class ClatCoordinator { public IBpfMap getBpfIngress6Map() { try { return new BpfMap<>(CLAT_INGRESS6_MAP_PATH, - BpfMap.BPF_F_RDWR, ClatIngress6Key.class, ClatIngress6Value.class); + ClatIngress6Key.class, ClatIngress6Value.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create ingress6 map: " + e); return null; @@ -268,7 +268,7 @@ public class ClatCoordinator { public IBpfMap getBpfEgress4Map() { try { return new BpfMap<>(CLAT_EGRESS4_MAP_PATH, - BpfMap.BPF_F_RDWR, ClatEgress4Key.class, ClatEgress4Value.class); + ClatEgress4Key.class, ClatEgress4Value.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create egress4 map: " + e); return null; @@ -280,7 +280,7 @@ public class ClatCoordinator { public IBpfMap getBpfCookieTagMap() { try { return new BpfMap<>(COOKIE_TAG_MAP_PATH, - BpfMap.BPF_F_RDWR, CookieTagMapKey.class, CookieTagMapValue.class); + CookieTagMapKey.class, CookieTagMapValue.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open cookie tag map: " + e); return null; diff --git a/service/src/com/android/server/connectivity/DscpPolicyTracker.java b/service/src/com/android/server/connectivity/DscpPolicyTracker.java index 8d566b6f07..ea3878f9fc 100644 --- a/service/src/com/android/server/connectivity/DscpPolicyTracker.java +++ b/service/src/com/android/server/connectivity/DscpPolicyTracker.java @@ -86,9 +86,9 @@ public class DscpPolicyTracker { mAttachedIfaces = new HashSet(); mIfaceIndexToPolicyIdBpfMapIndex = new HashMap(); mBpfDscpIpv4Policies = new BpfMap(IPV4_POLICY_MAP_PATH, - BpfMap.BPF_F_RDWR, Struct.S32.class, DscpPolicyValue.class); + Struct.S32.class, DscpPolicyValue.class); mBpfDscpIpv6Policies = new BpfMap(IPV6_POLICY_MAP_PATH, - BpfMap.BPF_F_RDWR, Struct.S32.class, DscpPolicyValue.class); + Struct.S32.class, DscpPolicyValue.class); } private boolean isUnusedIndex(int index) { diff --git a/staticlibs/device/com/android/net/module/util/BpfBitmap.java b/staticlibs/device/com/android/net/module/util/BpfBitmap.java index acb3ca5406..b62a430c7d 100644 --- a/staticlibs/device/com/android/net/module/util/BpfBitmap.java +++ b/staticlibs/device/com/android/net/module/util/BpfBitmap.java @@ -38,8 +38,7 @@ public class BpfBitmap { * @param path The path of the BPF map. */ public BpfBitmap(@NonNull String path) throws ErrnoException { - mBpfMap = new BpfMap(path, BpfMap.BPF_F_RDWR, - Struct.S32.class, Struct.S64.class); + mBpfMap = new BpfMap<>(path, Struct.S32.class, Struct.S64.class); } /** diff --git a/staticlibs/device/com/android/net/module/util/BpfMap.java b/staticlibs/device/com/android/net/module/util/BpfMap.java index ea6c3ec507..da77ae8860 100644 --- a/staticlibs/device/com/android/net/module/util/BpfMap.java +++ b/staticlibs/device/com/android/net/module/util/BpfMap.java @@ -108,6 +108,17 @@ public class BpfMap implements IBpfMap mMapFd = cachedBpfFdGet(path, flag, mKeySize, mValueSize); } + /** + * Create a R/W BpfMap map wrapper with "path" of filesystem. + * + * @throws ErrnoException if the BPF map associated with {@code path} cannot be retrieved. + * @throws NullPointerException if {@code path} is null. + */ + public BpfMap(@NonNull final String path, final Class key, + final Class value) throws ErrnoException, NullPointerException { + this(path, BPF_F_RDWR, key, value); + } + /** * Update an existing or create a new key -> value entry in an eBbpf map. * (use insertOrReplaceEntry() if you need to know whether insert or replace happened)