diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index 9f542f4aef60c7e70fe79b72f1c7b1c11e30f83f..81e18abf9ba0aa76fcb10cbaa042bd6ef7454904 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<TetherUpstream6Key, Tether6Value> 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 0e8b044b1a296d7e1207b6b4be378107f254b55f..d5d71bc1a7f1c59d02b019302c27f580cfe8b95b 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<TetherDownstream6Key, Tether6Value> 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<TetherDownstream6Key, Tether6Value> 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 27c0f9f5cddc3221c7c1a16def2ff5075c889b42..4ec1562acbd3d7aef66bb36988b652f2187fe9c6 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<S32, InterfaceMapValue> 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 a36149ef8166769ff3216d29b0e12d7b01fb6080..78c4504977e778609e08481939459e88a0eebd08 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<S32, U8> getUidCounterSetMap() { try { - return new BpfMap<S32, U8>(UID_COUNTERSET_MAP_PATH, BpfMap.BPF_F_RDWR, + return new BpfMap<S32, U8>(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<CookieTagMapKey, CookieTagMapValue> getCookieTagMap() { try { return new BpfMap<CookieTagMapKey, CookieTagMapValue>(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<StatsMapKey, StatsMapValue> getStatsMapA() { try { return new BpfMap<StatsMapKey, StatsMapValue>(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<StatsMapKey, StatsMapValue> getStatsMapB() { try { return new BpfMap<StatsMapKey, StatsMapValue>(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<UidStatsMapKey, StatsMapValue> getAppUidStatsMap() { try { return new BpfMap<UidStatsMapKey, StatsMapValue>(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<S32, StatsMapValue> getIfaceStatsMap() { try { return new BpfMap<S32, StatsMapValue>(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 ad9cfbe10e6287f97ff7bd22e99f46b4d2194b2f..aa402859e332cb57996a812506ff92b595595878 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<S32, U32> 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<S32, UidOwnerValue> 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<S32, U8> 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<CookieTagMapKey, CookieTagMapValue> 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<S32, U8> 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<IngressDiscardKey, IngressDiscardValue> 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 17de1461fb77968970d27e23add7f6ea6c708f41..daaf91daf9a7cf9cd523e2c1c242f53f12e38664 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<ClatIngress6Key, ClatIngress6Value> 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<ClatEgress4Key, ClatEgress4Value> 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<CookieTagMapKey, CookieTagMapValue> 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 8d566b6f0718970e9c384344b7a9d01c321d765c..ea3878f9fcebf2ec91c7fa7fd39969f84cec333b 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<String>(); mIfaceIndexToPolicyIdBpfMapIndex = new HashMap<Integer, SparseIntArray>(); mBpfDscpIpv4Policies = new BpfMap<Struct.S32, DscpPolicyValue>(IPV4_POLICY_MAP_PATH, - BpfMap.BPF_F_RDWR, Struct.S32.class, DscpPolicyValue.class); + Struct.S32.class, DscpPolicyValue.class); mBpfDscpIpv6Policies = new BpfMap<Struct.S32, DscpPolicyValue>(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 acb3ca5406acd15676439bb35f5e16afe6b1fa15..b62a430c7d08ce5ed468a9d28396a0f268d0c907 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<Struct.S32, Struct.S64>(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 ea6c3ec507257f32c00d1b188b926b624c099726..da77ae88605bb8746cde96133a4451012b9c86c6 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<K extends Struct, V extends Struct> implements IBpfMap<K, V> 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<K> key, + final Class<V> 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)