diff --git a/Tethering/tests/mts/src/android/tethering/mts/SkDestroyListenerTest.java b/Tethering/tests/mts/src/android/tethering/mts/SkDestroyListenerTest.java index 9494aa4aff..0e15dc855e 100644 --- a/Tethering/tests/mts/src/android/tethering/mts/SkDestroyListenerTest.java +++ b/Tethering/tests/mts/src/android/tethering/mts/SkDestroyListenerTest.java @@ -33,8 +33,8 @@ import android.system.Os; import android.util.Pair; import com.android.net.module.util.BpfDump; -import com.android.net.module.util.bpf.CookieTagMapKey; import com.android.net.module.util.bpf.CookieTagMapValue; +import com.android.net.module.util.Struct.S64; import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; import com.android.testutils.DevSdkIgnoreRunner; @@ -56,11 +56,11 @@ public class SkDestroyListenerTest { private static final String LINE_DELIMITER = "\\n"; private static final String DUMP_COMMAND = "dumpsys netstats --bpfRawMap --cookieTagMap"; - private Map parseBpfRawMap(final String dump) { - final Map map = new HashMap<>(); + private Map parseBpfRawMap(final String dump) { + final Map map = new HashMap<>(); for (final String line: dump.split(LINE_DELIMITER)) { - final Pair keyValue = - BpfDump.fromBase64EncodedString(CookieTagMapKey.class, + final Pair keyValue = + BpfDump.fromBase64EncodedString(S64.class, CookieTagMapValue.class, line.trim()); map.put(keyValue.first, keyValue.second); } @@ -69,7 +69,7 @@ public class SkDestroyListenerTest { private int countTaggedSocket() { final String dump = runShellCommandOrThrow(DUMP_COMMAND); - final Map cookieTagMap = parseBpfRawMap(dump); + final Map cookieTagMap = parseBpfRawMap(dump); int count = 0; for (final CookieTagMapValue value: cookieTagMap.values()) { if (value.tag == COOKIE_TAG && value.uid == Process.myUid()) { diff --git a/common/src/com/android/net/module/util/bpf/CookieTagMapKey.java b/common/src/com/android/net/module/util/bpf/CookieTagMapKey.java deleted file mode 100644 index 17da7a02f5..0000000000 --- a/common/src/com/android/net/module/util/bpf/CookieTagMapKey.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.net.module.util.bpf; - -import com.android.net.module.util.Struct; -import com.android.net.module.util.Struct.Field; -import com.android.net.module.util.Struct.Type; - -/** - * Key for cookie tag map. - */ -public class CookieTagMapKey extends Struct { - @Field(order = 0, type = Type.S64) - public final long socketCookie; - - public CookieTagMapKey(final long socketCookie) { - this.socketCookie = socketCookie; - } -} diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java index bfd8b1fe22..7dd7c5d760 100644 --- a/service-t/src/com/android/server/net/NetworkStatsService.java +++ b/service-t/src/com/android/server/net/NetworkStatsService.java @@ -186,8 +186,8 @@ import com.android.net.module.util.SharedLog; import com.android.net.module.util.SkDestroyListener; import com.android.net.module.util.Struct; import com.android.net.module.util.Struct.S32; +import com.android.net.module.util.Struct.S64; import com.android.net.module.util.Struct.U8; -import com.android.net.module.util.bpf.CookieTagMapKey; import com.android.net.module.util.bpf.CookieTagMapValue; import com.android.net.module.util.netlink.InetDiagMessage; import com.android.net.module.util.netlink.StructInetDiagSockId; @@ -455,7 +455,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { */ private SparseIntArray mActiveUidCounterSet = new SparseIntArray(); private final IBpfMap mUidCounterSetMap; - private final IBpfMap mCookieTagMap; + private final IBpfMap mCookieTagMap; private final IBpfMap mStatsMapA; private final IBpfMap mStatsMapB; private final IBpfMap mAppUidStatsMap; @@ -736,7 +736,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { mSkDestroyListener = mDeps.makeSkDestroyListener((message) -> { final StructInetDiagSockId sockId = message.inetDiagMsg.id; try { - mCookieTagMap.deleteEntry(new CookieTagMapKey(sockId.cookie)); + mCookieTagMap.deleteEntry(new S64(sockId.cookie)); } catch (ErrnoException e) { Log.e(TAG, "Failed to delete CookieTagMap entry for " + sockId.cookie + ": " + e); } @@ -906,10 +906,10 @@ public class NetworkStatsService extends INetworkStatsService.Stub { } /** Gets the cookie tag map */ - public IBpfMap getCookieTagMap() { + public IBpfMap getCookieTagMap() { try { return new BpfMap<>(COOKIE_TAG_MAP_PATH, - CookieTagMapKey.class, CookieTagMapValue.class); + S64.class, CookieTagMapValue.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open cookie tag map: " + e); return null; @@ -3327,7 +3327,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { return; } BpfDump.dumpMap(mCookieTagMap, pw, "mCookieTagMap", - (key, value) -> "cookie=" + key.socketCookie + (key, value) -> "cookie=" + key.val + " tag=0x" + Long.toHexString(value.tag) + " uid=" + value.uid); } diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java index 523ffee216..be1bbce18d 100644 --- a/service/src/com/android/server/BpfNetMaps.java +++ b/service/src/com/android/server/BpfNetMaps.java @@ -80,9 +80,9 @@ import com.android.net.module.util.SingleWriterBpfMap; import com.android.net.module.util.Struct; import com.android.net.module.util.Struct.Bool; import com.android.net.module.util.Struct.S32; +import com.android.net.module.util.Struct.S64; import com.android.net.module.util.Struct.U32; import com.android.net.module.util.Struct.U8; -import com.android.net.module.util.bpf.CookieTagMapKey; import com.android.net.module.util.bpf.CookieTagMapValue; import com.android.net.module.util.bpf.IngressDiscardKey; import com.android.net.module.util.bpf.IngressDiscardValue; @@ -134,7 +134,7 @@ public class BpfNetMaps { // BpfMap for UID_OWNER_MAP_PATH. This map is not accessed by others. private static IBpfMap sUidOwnerMap = null; private static IBpfMap sUidPermissionMap = null; - private static IBpfMap sCookieTagMap = null; + private static IBpfMap sCookieTagMap = null; // TODO: Add BOOL class and replace U8? private static IBpfMap sDataSaverEnabledMap = null; private static IBpfMap sIngressDiscardMap = null; @@ -177,7 +177,7 @@ public class BpfNetMaps { */ @VisibleForTesting public static void setCookieTagMapForTest( - IBpfMap cookieTagMap) { + IBpfMap cookieTagMap) { sCookieTagMap = cookieTagMap; } @@ -248,11 +248,11 @@ public class BpfNetMaps { } @RequiresApi(Build.VERSION_CODES.TIRAMISU) - private static IBpfMap getCookieTagMap() { + private static IBpfMap getCookieTagMap() { try { // Cannot use SingleWriterBpfMap because it's written by ClatCoordinator as well. return new BpfMap<>(COOKIE_TAG_MAP_PATH, - CookieTagMapKey.class, CookieTagMapValue.class); + S64.class, CookieTagMapValue.class); } catch (ErrnoException e) { throw new IllegalStateException("Cannot open cookie tag map", e); } @@ -1308,7 +1308,7 @@ public class BpfNetMaps { // to dump CookieTagMap. But the TagSocketTest in CTS depends on this dump so the tests // need to be updated before remove the dump from BpfNetMaps. BpfDump.dumpMap(sCookieTagMap, pw, "sCookieTagMap", - (key, value) -> "cookie=" + key.socketCookie + (key, value) -> "cookie=" + key.val + " tag=0x" + Long.toHexString(value.tag) + " uid=" + value.uid); BpfDump.dumpMap(sUidOwnerMap, pw, "sUidOwnerMap", diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java index e8087463ca..0902e1ac30 100644 --- a/service/src/com/android/server/connectivity/ClatCoordinator.java +++ b/service/src/com/android/server/connectivity/ClatCoordinator.java @@ -45,12 +45,12 @@ import com.android.net.module.util.BpfDump; import com.android.net.module.util.BpfMap; import com.android.net.module.util.IBpfMap; import com.android.net.module.util.InterfaceParams; +import com.android.net.module.util.Struct.S64; import com.android.net.module.util.TcUtils; import com.android.net.module.util.bpf.ClatEgress4Key; import com.android.net.module.util.bpf.ClatEgress4Value; import com.android.net.module.util.bpf.ClatIngress6Key; import com.android.net.module.util.bpf.ClatIngress6Value; -import com.android.net.module.util.bpf.CookieTagMapKey; import com.android.net.module.util.bpf.CookieTagMapValue; import java.io.FileDescriptor; @@ -134,7 +134,7 @@ public class ClatCoordinator { @Nullable private final IBpfMap mEgressMap; @Nullable - private final IBpfMap mCookieTagMap; + private final IBpfMap mCookieTagMap; @Nullable private ClatdTracker mClatdTracker = null; @@ -280,11 +280,11 @@ public class ClatCoordinator { /** Get cookie tag map */ @Nullable - public IBpfMap getBpfCookieTagMap() { + public IBpfMap getBpfCookieTagMap() { try { // also read and written from other locations return new BpfMap<>(COOKIE_TAG_MAP_PATH, - CookieTagMapKey.class, CookieTagMapValue.class); + S64.class, CookieTagMapValue.class); } catch (ErrnoException e) { Log.wtf(TAG, "Cannot open cookie tag map: " + e); return null; @@ -536,7 +536,7 @@ public class ClatCoordinator { // Tag raw socket with uid AID_CLAT and set tag as zero because tag is unused in bpf // program for counting data usage in netd.c. Tagging socket is used to avoid counting // duplicated clat traffic in bpf stat. - final CookieTagMapKey key = new CookieTagMapKey(cookie); + final S64 key = new S64(cookie); final CookieTagMapValue value = new CookieTagMapValue(AID_CLAT, 0 /* tag, unused */); try { mCookieTagMap.insertEntry(key, value); @@ -556,7 +556,7 @@ public class ClatCoordinator { // destroy listener only monitors on group INET_TCP, INET_UDP, INET6_TCP, INET6_UDP. // The other socket types, ex: raw, are not able to be removed automatically by the // listener. See TrafficController::makeSkDestroyListener. - final CookieTagMapKey key = new CookieTagMapKey(cookie); + final S64 key = new S64(cookie); try { mCookieTagMap.deleteEntry(key); } catch (ErrnoException | IllegalStateException e) { diff --git a/tests/unit/java/com/android/server/BpfNetMapsTest.java b/tests/unit/java/com/android/server/BpfNetMapsTest.java index 1d2e8b0c99..af7e9fed26 100644 --- a/tests/unit/java/com/android/server/BpfNetMapsTest.java +++ b/tests/unit/java/com/android/server/BpfNetMapsTest.java @@ -102,9 +102,9 @@ import com.android.modules.utils.build.SdkLevel; import com.android.net.module.util.IBpfMap; import com.android.net.module.util.Struct.Bool; import com.android.net.module.util.Struct.S32; +import com.android.net.module.util.Struct.S64; import com.android.net.module.util.Struct.U32; import com.android.net.module.util.Struct.U8; -import com.android.net.module.util.bpf.CookieTagMapKey; import com.android.net.module.util.bpf.CookieTagMapValue; import com.android.net.module.util.bpf.IngressDiscardKey; import com.android.net.module.util.bpf.IngressDiscardValue; @@ -181,8 +181,8 @@ public final class BpfNetMapsTest { new TestBpfMap<>(U32.class, Bool.class); private final IBpfMap mLocalNetAccessMap = new TestBpfMap<>(LocalNetAccessKey.class, Bool.class); - private final IBpfMap mCookieTagMap = - spy(new TestBpfMap<>(CookieTagMapKey.class, CookieTagMapValue.class)); + private final IBpfMap mCookieTagMap = + spy(new TestBpfMap<>(S64.class, CookieTagMapValue.class)); private final IBpfMap mDataSaverEnabledMap = new TestBpfMap<>(S32.class, U8.class); private final IBpfMap mIngressDiscardMap = new TestBpfMap<>(IngressDiscardKey.class, IngressDiscardValue.class); @@ -1132,7 +1132,7 @@ public final class BpfNetMapsTest { @IgnoreUpTo(Build.VERSION_CODES.S_V2) public void testPullBpfMapInfo() throws Exception { // mCookieTagMap has 1 entry - mCookieTagMap.updateEntry(new CookieTagMapKey(0), new CookieTagMapValue(0, 0)); + mCookieTagMap.updateEntry(new S64(0), new CookieTagMapValue(0, 0)); // mUidOwnerMap has 2 entries mUidOwnerMap.updateEntry(new S32(0), new UidOwnerValue(0, 0)); @@ -1299,7 +1299,7 @@ public final class BpfNetMapsTest { @Test @IgnoreUpTo(Build.VERSION_CODES.S_V2) public void testDumpCookieTagMap() throws Exception { - mCookieTagMap.updateEntry(new CookieTagMapKey(123), new CookieTagMapValue(456, 0x789)); + mCookieTagMap.updateEntry(new S64(123), new CookieTagMapValue(456, 0x789)); assertDumpContains(getDump(), "cookie=123 tag=0x789 uid=456"); } diff --git a/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java b/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java index 72dde7f791..db3ce36803 100644 --- a/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java +++ b/tests/unit/java/com/android/server/connectivity/ClatCoordinatorTest.java @@ -58,8 +58,8 @@ import com.android.net.module.util.bpf.ClatEgress4Key; import com.android.net.module.util.bpf.ClatEgress4Value; import com.android.net.module.util.bpf.ClatIngress6Key; import com.android.net.module.util.bpf.ClatIngress6Value; -import com.android.net.module.util.bpf.CookieTagMapKey; import com.android.net.module.util.bpf.CookieTagMapValue; +import com.android.net.module.util.Struct.S64; import com.android.testutils.DevSdkIgnoreRule; import com.android.testutils.DevSdkIgnoreRunner; import com.android.testutils.TestBpfMap; @@ -131,7 +131,7 @@ public class ClatCoordinatorTest { INET6_PFX96, INET6_LOCAL6); private static final ClatIngress6Value INGRESS_VALUE = new ClatIngress6Value(STACKED_IFINDEX, INET4_LOCAL4); - private static final CookieTagMapKey COOKIE_TAG_KEY = new CookieTagMapKey(RAW_SOCK_COOKIE); + private static final S64 COOKIE_TAG_KEY = new S64(RAW_SOCK_COOKIE); private static final CookieTagMapValue COOKIE_TAG_VALUE = new CookieTagMapValue(AID_CLAT, 0 /* tag, unused */); @@ -139,8 +139,8 @@ public class ClatCoordinatorTest { spy(new TestBpfMap<>(ClatIngress6Key.class, ClatIngress6Value.class)); private final TestBpfMap mEgressMap = spy(new TestBpfMap<>(ClatEgress4Key.class, ClatEgress4Value.class)); - private final TestBpfMap mCookieTagMap = - spy(new TestBpfMap<>(CookieTagMapKey.class, CookieTagMapValue.class)); + private final TestBpfMap mCookieTagMap = + spy(new TestBpfMap<>(S64.class, CookieTagMapValue.class)); @Mock private INetd mNetd; @Spy private TestDependencies mDeps = new TestDependencies(); @@ -341,7 +341,7 @@ public class ClatCoordinatorTest { /** Get cookie tag map */ @Override - public IBpfMap getBpfCookieTagMap() { + public IBpfMap getBpfCookieTagMap() { return mCookieTagMap; } @@ -632,7 +632,7 @@ public class ClatCoordinatorTest { throw new IOException(); } @Override - public IBpfMap getBpfCookieTagMap() { + public IBpfMap getBpfCookieTagMap() { return mock(IBpfMap.class); } } @@ -650,7 +650,7 @@ public class ClatCoordinatorTest { } @Override - public IBpfMap getBpfCookieTagMap() { + public IBpfMap getBpfCookieTagMap() { return mock(IBpfMap.class); } } @@ -739,7 +739,7 @@ public class ClatCoordinatorTest { public void testNotStartClatWithNullCookieTagMap() throws Exception { class FailureDependencies extends TestDependencies { @Override - public IBpfMap getBpfCookieTagMap() { + public IBpfMap getBpfCookieTagMap() { return null; } } diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java index 481c92e2bb..74073ad09c 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java @@ -167,8 +167,8 @@ import com.android.net.module.util.LocationPermissionChecker; import com.android.net.module.util.SkDestroyListener; import com.android.net.module.util.Struct; import com.android.net.module.util.Struct.S32; +import com.android.net.module.util.Struct.S64; import com.android.net.module.util.Struct.U8; -import com.android.net.module.util.bpf.CookieTagMapKey; import com.android.net.module.util.bpf.CookieTagMapValue; import com.android.net.module.util.netlink.InetDiagMessage; import com.android.server.connectivity.ConnectivityResources; @@ -291,8 +291,8 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { @Mock private SkDestroyListener mSkDestroyListener; - private TestBpfMap mCookieTagMap = new TestBpfMap<>( - CookieTagMapKey.class, CookieTagMapValue.class); + private TestBpfMap mCookieTagMap = new TestBpfMap<>( + S64.class, CookieTagMapValue.class); private TestBpfMap mStatsMapA = new TestBpfMap<>(StatsMapKey.class, StatsMapValue.class); private TestBpfMap mStatsMapB = new TestBpfMap<>(StatsMapKey.class, @@ -579,7 +579,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { } @Override - public IBpfMap getCookieTagMap() { + public IBpfMap getCookieTagMap() { return mCookieTagMap; } @@ -3017,8 +3017,8 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { private void initBpfMapsWithTagData(int uid) throws ErrnoException { // key needs to be unique, use some offset from uid. - mCookieTagMap.insertEntry(new CookieTagMapKey(1000 + uid), new CookieTagMapValue(uid, 1)); - mCookieTagMap.insertEntry(new CookieTagMapKey(2000 + uid), new CookieTagMapValue(uid, 2)); + mCookieTagMap.insertEntry(new S64(1000 + uid), new CookieTagMapValue(uid, 1)); + mCookieTagMap.insertEntry(new S64(2000 + uid), new CookieTagMapValue(uid, 2)); mStatsMapA.insertEntry(new StatsMapKey(uid, 1, 0, 10), new StatsMapValue(5, 5000, 3, 3000)); mStatsMapA.insertEntry(new StatsMapKey(uid, 2, 0, 10), new StatsMapValue(5, 5000, 3, 3000)); @@ -3101,14 +3101,14 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { initBpfMapsWithTagData(UID_BLUE); final String dump = getDump(new String[]{DUMPSYS_BPF_RAW_MAP, DUMPSYS_COOKIE_TAG_MAP}); - Map cookieTagMap = parseBpfRawMap( - CookieTagMapKey.class, CookieTagMapValue.class, dump); + Map cookieTagMap = parseBpfRawMap( + S64.class, CookieTagMapValue.class, dump); - final CookieTagMapValue val1 = cookieTagMap.get(new CookieTagMapKey(2002)); + final CookieTagMapValue val1 = cookieTagMap.get(new S64(2002)); assertEquals(1, val1.tag); assertEquals(1002, val1.uid); - final CookieTagMapValue val2 = cookieTagMap.get(new CookieTagMapKey(3002)); + final CookieTagMapValue val2 = cookieTagMap.get(new S64(3002)); assertEquals(2, val2.tag); assertEquals(1002, val2.uid); }