From b5f97280d64512e74abd4d22a899a9a2bbf58de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Thu, 7 Aug 2025 12:47:59 -0700 Subject: [PATCH] replace TetherStatsKey with S32 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (mostly autogenerated, with minor tweaks to make it build) Test: TreeHugger Flag: EXEMPT mainline Signed-off-by: Maciej Żenczykowski Change-Id: I5a5ba231cb38886b9b681b4c08d89a18ad418f23 --- .../apishim/api31/BpfCoordinatorShimImpl.java | 13 ++++---- .../tethering/BpfCoordinator.java | 9 +++--- .../android/net/EthernetTetheringTest.java | 10 +++--- .../tethering/BpfCoordinatorTest.java | 21 ++++++------- .../net/module/util/bpf/TetherStatsKey.java | 31 ------------------- 5 files changed, 25 insertions(+), 59 deletions(-) delete mode 100644 common/src/com/android/net/module/util/bpf/TetherStatsKey.java diff --git a/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java b/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java index 23e0545bd3..d7868e3e0d 100644 --- a/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java +++ b/Tethering/apishim/31/com/android/networkstack/tethering/apishim/api31/BpfCoordinatorShimImpl.java @@ -36,7 +36,6 @@ import com.android.net.module.util.SharedLog; import com.android.net.module.util.Struct.S32; import com.android.net.module.util.bpf.Tether4Key; import com.android.net.module.util.bpf.Tether4Value; -import com.android.net.module.util.bpf.TetherStatsKey; import com.android.net.module.util.bpf.TetherStatsValue; import com.android.networkstack.tethering.BpfCoordinator.Dependencies; import com.android.networkstack.tethering.BpfCoordinator.Ipv6DownstreamRule; @@ -83,7 +82,7 @@ public class BpfCoordinatorShimImpl // BPF map of tethering statistics of the upstream interface since tethering startup. @Nullable - private final IBpfMap mBpfStatsMap; + private final IBpfMap mBpfStatsMap; // BPF map of per-interface quota for tethering offload. @Nullable @@ -250,7 +249,7 @@ public class BpfCoordinatorShimImpl try { // The reported tether stats are total data usage for all currently-active upstream // interfaces since tethering start. - mBpfStatsMap.forEach((key, value) -> tetherStatsList.put((int) key.ifindex, value)); + mBpfStatsMap.forEach((key, value) -> tetherStatsList.put((int) key.val, value)); } catch (ErrnoException e) { mLog.e("Fail to fetch tethering stats from BPF map: ", e); return null; @@ -267,7 +266,7 @@ public class BpfCoordinatorShimImpl TetherStatsValue statsValue = null; try { - statsValue = mBpfStatsMap.getValue(new TetherStatsKey(ifIndex)); + statsValue = mBpfStatsMap.getValue(new S32(ifIndex)); } catch (ErrnoException e) { // The BpfMap#getValue doesn't throw an errno ENOENT exception. Catch other error // while trying to get stats entry. @@ -285,7 +284,7 @@ public class BpfCoordinatorShimImpl // This function is the *only* thing that can create entries. // BpfMap#insertEntry use BPF_NOEXIST to create the entry. The entry is created // if and only if it doesn't exist. - mBpfStatsMap.insertEntry(new TetherStatsKey(ifIndex), new TetherStatsValue( + mBpfStatsMap.insertEntry(new S32(ifIndex), new TetherStatsValue( 0 /* rxPackets */, 0 /* rxBytes */, 0 /* rxErrors */, 0 /* txPackets */, 0 /* txBytes */, 0 /* txErrors */)); } catch (ErrnoException | IllegalArgumentException e) { @@ -329,7 +328,7 @@ public class BpfCoordinatorShimImpl TetherStatsValue statsValue = null; try { - statsValue = mBpfStatsMap.getValue(new TetherStatsKey(ifIndex)); + statsValue = mBpfStatsMap.getValue(new S32(ifIndex)); } catch (ErrnoException e) { mLog.e("Could not get stats entry for interface index " + ifIndex + ": ", e); return null; @@ -341,7 +340,7 @@ public class BpfCoordinatorShimImpl } try { - mBpfStatsMap.deleteEntry(new TetherStatsKey(ifIndex)); + mBpfStatsMap.deleteEntry(new S32(ifIndex)); } catch (ErrnoException e) { mLog.e("Could not delete stats entry for interface index " + ifIndex + ": ", e); return null; diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index ef26e3bd8e..c2226ccf19 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java @@ -76,7 +76,6 @@ import com.android.net.module.util.SharedLog; import com.android.net.module.util.Struct.S32; import com.android.net.module.util.bpf.Tether4Key; import com.android.net.module.util.bpf.Tether4Value; -import com.android.net.module.util.bpf.TetherStatsKey; import com.android.net.module.util.bpf.TetherStatsValue; import com.android.net.module.util.ip.ConntrackMonitor; import com.android.net.module.util.ip.ConntrackMonitor.ConntrackEventConsumer; @@ -448,11 +447,11 @@ public class BpfCoordinator { } /** Get stats BPF map. */ - @Nullable public IBpfMap getBpfStatsMap() { + @Nullable public IBpfMap getBpfStatsMap() { if (!isAtLeastS()) return null; try { return new BpfMap<>(TETHER_STATS_MAP_PATH, - TetherStatsKey.class, TetherStatsValue.class); + S32.class, TetherStatsValue.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create stats map: " + e); return null; @@ -1370,7 +1369,7 @@ public class BpfCoordinator { } } private void dumpBpfStats(@NonNull IndentingPrintWriter pw) { - try (IBpfMap map = mDeps.getBpfStatsMap()) { + try (IBpfMap map = mDeps.getBpfStatsMap()) { if (map == null) { pw.println("No BPF stats map"); return; @@ -1522,7 +1521,7 @@ public class BpfCoordinator { // expected argument order. // TODO: dump downstream4 map. if (CollectionUtils.contains(args, DUMPSYS_RAWMAP_ARG_STATS)) { - try (IBpfMap statsMap = mDeps.getBpfStatsMap()) { + try (IBpfMap statsMap = mDeps.getBpfStatsMap()) { BpfDump.dumpRawMap(statsMap, pw); } catch (IOException e) { pw.println("Error dumping stats map: " + e); diff --git a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java index 14795b7320..29f9376009 100644 --- a/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java +++ b/Tethering/tests/integration/src/android/net/EthernetTetheringTest.java @@ -75,13 +75,13 @@ import com.android.modules.utils.build.SdkLevel; import com.android.net.module.util.BpfDump; import com.android.net.module.util.Ipv6Utils; import com.android.net.module.util.Struct; +import com.android.net.module.util.Struct.S32; 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.Tether4Key; import com.android.net.module.util.bpf.Tether4Value; -import com.android.net.module.util.bpf.TetherStatsKey; import com.android.net.module.util.bpf.TetherStatsValue; import com.android.net.module.util.structs.Ipv4Header; import com.android.net.module.util.structs.UdpHeader; @@ -1055,15 +1055,15 @@ public class EthernetTetheringTest extends EthernetTetheringTestBase { // Dump stats map to verify. final String[] statsArgs = new String[] {DUMPSYS_TETHERING_RAWMAP_ARG, DUMPSYS_RAWMAP_ARG_STATS}; - final HashMap statsMap = pollRawMapFromDump( - TetherStatsKey.class, TetherStatsValue.class, Context.TETHERING_SERVICE, statsArgs); + final HashMap statsMap = pollRawMapFromDump( + S32.class, TetherStatsValue.class, Context.TETHERING_SERVICE, statsArgs); assertNotNull(statsMap); assertEquals(1, statsMap.size()); - final Map.Entry stats = + final Map.Entry stats = statsMap.entrySet().iterator().next(); - // TODO: verify the upstream index in TetherStatsKey. + // TODO: verify the upstream index in S32. final TetherStatsValue statsValue = stats.getValue(); assertEquals(RX_UDP_PACKET_COUNT, statsValue.rxPackets); diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java index 48332ce1b3..40de913747 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java @@ -124,7 +124,6 @@ import com.android.net.module.util.SharedLog; import com.android.net.module.util.Struct.S32; import com.android.net.module.util.bpf.Tether4Key; import com.android.net.module.util.bpf.Tether4Value; -import com.android.net.module.util.bpf.TetherStatsKey; import com.android.net.module.util.bpf.TetherStatsValue; import com.android.net.module.util.ip.ConntrackMonitor; import com.android.net.module.util.ip.ConntrackMonitor.ConntrackEventConsumer; @@ -480,8 +479,8 @@ public class BpfCoordinatorTest { spy(new TestBpfMap<>(TetherDownstream6Key.class, Tether6Value.class)); private final IBpfMap mBpfUpstream6Map = spy(new TestBpfMap<>(TetherUpstream6Key.class, Tether6Value.class)); - private final IBpfMap mBpfStatsMap = - spy(new TestBpfMap<>(TetherStatsKey.class, TetherStatsValue.class)); + private final IBpfMap mBpfStatsMap = + spy(new TestBpfMap<>(S32.class, TetherStatsValue.class)); private final IBpfMap mBpfLimitMap = spy(new TestBpfMap<>(S32.class, TetherLimitValue.class)); private final IBpfMap mBpfDevMap = @@ -554,7 +553,7 @@ public class BpfCoordinatorTest { } @Nullable - public IBpfMap getBpfStatsMap() { + public IBpfMap getBpfStatsMap() { return mBpfStatsMap; } @@ -677,7 +676,7 @@ public class BpfCoordinatorTest { // Update a stats entry or create if not exists. private void updateStatsEntryToStatsMap(@NonNull TetherStatsParcel stats) throws Exception { - final TetherStatsKey key = new TetherStatsKey(stats.ifIndex); + final S32 key = new S32(stats.ifIndex); final TetherStatsValue value = new TetherStatsValue(stats.rxPackets, stats.rxBytes, 0L /* rxErrors */, stats.txPackets, stats.txBytes, 0L /* txErrors */); mBpfStatsMap.updateEntry(key, value); @@ -925,7 +924,7 @@ public class BpfCoordinatorTest { private void verifyTetherOffloadSetInterfaceQuota(@Nullable InOrder inOrder, int ifIndex, long quotaBytes, boolean isInit) throws Exception { if (mDeps.isAtLeastS()) { - final TetherStatsKey key = new TetherStatsKey(ifIndex); + final S32 key = new S32(ifIndex); verifyWithOrder(inOrder, mBpfStatsMap).getValue(key); if (isInit) { verifyWithOrder(inOrder, mBpfStatsMap).insertEntry(key, new TetherStatsValue( @@ -953,8 +952,8 @@ public class BpfCoordinatorTest { private void verifyTetherOffloadGetAndClearStats(@NonNull InOrder inOrder, int ifIndex) throws Exception { if (mDeps.isAtLeastS()) { - inOrder.verify(mBpfStatsMap).getValue(new TetherStatsKey(ifIndex)); - inOrder.verify(mBpfStatsMap).deleteEntry(new TetherStatsKey(ifIndex)); + inOrder.verify(mBpfStatsMap).getValue(new S32(ifIndex)); + inOrder.verify(mBpfStatsMap).deleteEntry(new S32(ifIndex)); inOrder.verify(mBpfLimitMap).deleteEntry(new S32(ifIndex)); } else { inOrder.verify(mNetd).tetherOffloadGetAndClearStats(ifIndex); @@ -2729,7 +2728,7 @@ public class BpfCoordinatorTest { // dumpStats mBpfStatsMap.insertEntry( - new TetherStatsKey(UPSTREAM_IFINDEX), + new S32(UPSTREAM_IFINDEX), new TetherStatsValue( 0L /* rxPackets */, 0L /* rxBytes */, 0L /* rxErrors */, 0L /* txPackets */, 0L /* txBytes */, 0L /* txErrors */)); @@ -3010,8 +3009,8 @@ public class BpfCoordinatorTest { // When the last rule is removed, tetherOffloadGetAndClearStats will log a WTF (and // potentially crash the test) if the stats map is empty. final TetherStatsValue allZeros = new TetherStatsValue(0, 0, 0, 0, 0, 0); - when(mBpfStatsMap.getValue(new TetherStatsKey(UPSTREAM_IFINDEX))).thenReturn(allZeros); - when(mBpfStatsMap.getValue(new TetherStatsKey(UPSTREAM_IFINDEX2))).thenReturn(allZeros); + when(mBpfStatsMap.getValue(new S32(UPSTREAM_IFINDEX))).thenReturn(allZeros); + when(mBpfStatsMap.getValue(new S32(UPSTREAM_IFINDEX2))).thenReturn(allZeros); } @Test diff --git a/common/src/com/android/net/module/util/bpf/TetherStatsKey.java b/common/src/com/android/net/module/util/bpf/TetherStatsKey.java deleted file mode 100644 index 68111b6a05..0000000000 --- a/common/src/com/android/net/module/util/bpf/TetherStatsKey.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2020 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; - -/** The key of BpfMap which is used for tethering stats. */ -public class TetherStatsKey extends Struct { - @Field(order = 0, type = Type.S32) - public final int ifindex; // upstream interface index - - public TetherStatsKey(final int ifindex) { - this.ifindex = ifindex; - } -}