Merge "Migrate BpfInterfaceMapUpdater from U32 to S32"

This commit is contained in:
Maciej Żenczykowski
2022-09-16 17:58:48 +00:00
committed by Gerrit Code Review
4 changed files with 22 additions and 23 deletions

View File

@@ -31,7 +31,7 @@ import com.android.net.module.util.BpfDump;
import com.android.net.module.util.BpfMap; import com.android.net.module.util.BpfMap;
import com.android.net.module.util.IBpfMap; import com.android.net.module.util.IBpfMap;
import com.android.net.module.util.InterfaceParams; import com.android.net.module.util.InterfaceParams;
import com.android.net.module.util.Struct.U32; import com.android.net.module.util.Struct.S32;
/** /**
* Monitor interface added (without removed) and right interface name and its index to bpf map. * Monitor interface added (without removed) and right interface name and its index to bpf map.
@@ -41,7 +41,7 @@ public class BpfInterfaceMapUpdater {
// This is current path but may be changed soon. // This is current path but may be changed soon.
private static final String IFACE_INDEX_NAME_MAP_PATH = private static final String IFACE_INDEX_NAME_MAP_PATH =
"/sys/fs/bpf/netd_shared/map_netd_iface_index_name_map"; "/sys/fs/bpf/netd_shared/map_netd_iface_index_name_map";
private final IBpfMap<U32, InterfaceMapValue> mBpfMap; private final IBpfMap<S32, InterfaceMapValue> mBpfMap;
private final INetd mNetd; private final INetd mNetd;
private final Handler mHandler; private final Handler mHandler;
private final Dependencies mDeps; private final Dependencies mDeps;
@@ -64,10 +64,10 @@ public class BpfInterfaceMapUpdater {
@VisibleForTesting @VisibleForTesting
public static class Dependencies { public static class Dependencies {
/** Create BpfMap for updating interface and index mapping. */ /** Create BpfMap for updating interface and index mapping. */
public IBpfMap<U32, InterfaceMapValue> getInterfaceMap() { public IBpfMap<S32, InterfaceMapValue> getInterfaceMap() {
try { try {
return new BpfMap<>(IFACE_INDEX_NAME_MAP_PATH, BpfMap.BPF_F_RDWR, return new BpfMap<>(IFACE_INDEX_NAME_MAP_PATH, BpfMap.BPF_F_RDWR,
U32.class, InterfaceMapValue.class); S32.class, InterfaceMapValue.class);
} catch (ErrnoException e) { } catch (ErrnoException e) {
Log.e(TAG, "Cannot create interface map: " + e); Log.e(TAG, "Cannot create interface map: " + e);
return null; return null;
@@ -126,7 +126,7 @@ public class BpfInterfaceMapUpdater {
} }
try { try {
mBpfMap.updateEntry(new U32(iface.index), new InterfaceMapValue(ifaceName)); mBpfMap.updateEntry(new S32(iface.index), new InterfaceMapValue(ifaceName));
} catch (ErrnoException e) { } catch (ErrnoException e) {
Log.e(TAG, "Unable to update entry for " + ifaceName + ", " + e); Log.e(TAG, "Unable to update entry for " + ifaceName + ", " + e);
} }
@@ -140,9 +140,9 @@ public class BpfInterfaceMapUpdater {
} }
/** get interface name by interface index from bpf map */ /** get interface name by interface index from bpf map */
public String getIfNameByIndex(final long index) { public String getIfNameByIndex(final int index) {
try { try {
final InterfaceMapValue value = mBpfMap.getValue(new U32(index)); final InterfaceMapValue value = mBpfMap.getValue(new S32(index));
if (value == null) { if (value == null) {
Log.e(TAG, "No if name entry for index " + index); Log.e(TAG, "No if name entry for index " + index);
return null; return null;

View File

@@ -2896,9 +2896,8 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
BpfDump.dumpMap(statsMap, pw, mapName, BpfDump.dumpMap(statsMap, pw, mapName,
"ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes rxPackets txBytes txPackets", "ifaceIndex ifaceName tag_hex uid_int cnt_set rxBytes rxPackets txBytes txPackets",
(key, value) -> { (key, value) -> {
final long ifIndex = key.ifaceIndex; final String ifName = mInterfaceMapUpdater.getIfNameByIndex(key.ifaceIndex);
final String ifName = mInterfaceMapUpdater.getIfNameByIndex(ifIndex); return key.ifaceIndex + " "
return ifIndex + " "
+ (ifName != null ? ifName : "unknown") + " " + (ifName != null ? ifName : "unknown") + " "
+ "0x" + Long.toHexString(key.tag) + " " + "0x" + Long.toHexString(key.tag) + " "
+ key.uid + " " + key.uid + " "

View File

@@ -33,11 +33,11 @@ public class StatsMapKey extends Struct {
@Field(order = 2, type = Type.U32) @Field(order = 2, type = Type.U32)
public final long counterSet; public final long counterSet;
@Field(order = 3, type = Type.U32) @Field(order = 3, type = Type.S32)
public final long ifaceIndex; public final int ifaceIndex;
public StatsMapKey(final long uid, final long tag, final long counterSet, public StatsMapKey(final long uid, final long tag, final long counterSet,
final long ifaceIndex) { final int ifaceIndex) {
this.uid = uid; this.uid = uid;
this.tag = tag; this.tag = tag;
this.counterSet = counterSet; this.counterSet = counterSet;

View File

@@ -42,7 +42,7 @@ import androidx.test.filters.SmallTest;
import com.android.net.module.util.BaseNetdUnsolicitedEventListener; import com.android.net.module.util.BaseNetdUnsolicitedEventListener;
import com.android.net.module.util.IBpfMap; import com.android.net.module.util.IBpfMap;
import com.android.net.module.util.InterfaceParams; import com.android.net.module.util.InterfaceParams;
import com.android.net.module.util.Struct.U32; import com.android.net.module.util.Struct.S32;
import com.android.testutils.DevSdkIgnoreRule; import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner; import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.TestBpfMap; import com.android.testutils.TestBpfMap;
@@ -69,14 +69,14 @@ public final class BpfInterfaceMapUpdaterTest {
private final TestLooper mLooper = new TestLooper(); private final TestLooper mLooper = new TestLooper();
private BaseNetdUnsolicitedEventListener mListener; private BaseNetdUnsolicitedEventListener mListener;
private BpfInterfaceMapUpdater mUpdater; private BpfInterfaceMapUpdater mUpdater;
private IBpfMap<U32, InterfaceMapValue> mBpfMap = private IBpfMap<S32, InterfaceMapValue> mBpfMap =
spy(new TestBpfMap<>(U32.class, InterfaceMapValue.class)); spy(new TestBpfMap<>(S32.class, InterfaceMapValue.class));
@Mock private INetd mNetd; @Mock private INetd mNetd;
@Mock private Context mContext; @Mock private Context mContext;
private class TestDependencies extends BpfInterfaceMapUpdater.Dependencies { private class TestDependencies extends BpfInterfaceMapUpdater.Dependencies {
@Override @Override
public IBpfMap<U32, InterfaceMapValue> getInterfaceMap() { public IBpfMap<S32, InterfaceMapValue> getInterfaceMap() {
return mBpfMap; return mBpfMap;
} }
@@ -114,7 +114,7 @@ public final class BpfInterfaceMapUpdaterTest {
ArgumentCaptor.forClass(BaseNetdUnsolicitedEventListener.class); ArgumentCaptor.forClass(BaseNetdUnsolicitedEventListener.class);
verify(mNetd).registerUnsolicitedEventListener(listenerCaptor.capture()); verify(mNetd).registerUnsolicitedEventListener(listenerCaptor.capture());
mListener = listenerCaptor.getValue(); mListener = listenerCaptor.getValue();
verify(mBpfMap).updateEntry(eq(new U32(TEST_INDEX)), verify(mBpfMap).updateEntry(eq(new S32(TEST_INDEX)),
eq(new InterfaceMapValue(TEST_INTERFACE_NAME))); eq(new InterfaceMapValue(TEST_INTERFACE_NAME)));
} }
@@ -124,7 +124,7 @@ public final class BpfInterfaceMapUpdaterTest {
mListener.onInterfaceAdded(TEST_INTERFACE_NAME2); mListener.onInterfaceAdded(TEST_INTERFACE_NAME2);
mLooper.dispatchAll(); mLooper.dispatchAll();
verify(mBpfMap).updateEntry(eq(new U32(TEST_INDEX2)), verify(mBpfMap).updateEntry(eq(new S32(TEST_INDEX2)),
eq(new InterfaceMapValue(TEST_INTERFACE_NAME2))); eq(new InterfaceMapValue(TEST_INTERFACE_NAME2)));
// Check that when onInterfaceRemoved is called, nothing happens. // Check that when onInterfaceRemoved is called, nothing happens.
@@ -135,7 +135,7 @@ public final class BpfInterfaceMapUpdaterTest {
@Test @Test
public void testGetIfNameByIndex() throws Exception { public void testGetIfNameByIndex() throws Exception {
mBpfMap.updateEntry(new U32(TEST_INDEX), new InterfaceMapValue(TEST_INTERFACE_NAME)); mBpfMap.updateEntry(new S32(TEST_INDEX), new InterfaceMapValue(TEST_INTERFACE_NAME));
assertEquals(TEST_INTERFACE_NAME, mUpdater.getIfNameByIndex(TEST_INDEX)); assertEquals(TEST_INTERFACE_NAME, mUpdater.getIfNameByIndex(TEST_INDEX));
} }
@@ -146,7 +146,7 @@ public final class BpfInterfaceMapUpdaterTest {
@Test @Test
public void testGetIfNameByIndexException() throws Exception { public void testGetIfNameByIndexException() throws Exception {
doThrow(new ErrnoException("", EPERM)).when(mBpfMap).getValue(new U32(TEST_INDEX)); doThrow(new ErrnoException("", EPERM)).when(mBpfMap).getValue(new S32(TEST_INDEX));
assertNull(mUpdater.getIfNameByIndex(TEST_INDEX)); assertNull(mUpdater.getIfNameByIndex(TEST_INDEX));
} }
@@ -163,8 +163,8 @@ public final class BpfInterfaceMapUpdaterTest {
@Test @Test
public void testDump() throws ErrnoException { public void testDump() throws ErrnoException {
mBpfMap.updateEntry(new U32(TEST_INDEX), new InterfaceMapValue(TEST_INTERFACE_NAME)); mBpfMap.updateEntry(new S32(TEST_INDEX), new InterfaceMapValue(TEST_INTERFACE_NAME));
mBpfMap.updateEntry(new U32(TEST_INDEX2), new InterfaceMapValue(TEST_INTERFACE_NAME2)); mBpfMap.updateEntry(new S32(TEST_INDEX2), new InterfaceMapValue(TEST_INTERFACE_NAME2));
final String dump = getDump(); final String dump = getDump();
assertDumpContains(dump, "IfaceIndexNameMap: OK"); assertDumpContains(dump, "IfaceIndexNameMap: OK");