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.IBpfMap;
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.
@@ -41,7 +41,7 @@ public class BpfInterfaceMapUpdater {
// This is current path but may be changed soon.
private static final String IFACE_INDEX_NAME_MAP_PATH =
"/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 Handler mHandler;
private final Dependencies mDeps;
@@ -64,10 +64,10 @@ public class BpfInterfaceMapUpdater {
@VisibleForTesting
public static class Dependencies {
/** Create BpfMap for updating interface and index mapping. */
public IBpfMap<U32, InterfaceMapValue> getInterfaceMap() {
public IBpfMap<S32, InterfaceMapValue> getInterfaceMap() {
try {
return new BpfMap<>(IFACE_INDEX_NAME_MAP_PATH, BpfMap.BPF_F_RDWR,
U32.class, InterfaceMapValue.class);
S32.class, InterfaceMapValue.class);
} catch (ErrnoException e) {
Log.e(TAG, "Cannot create interface map: " + e);
return null;
@@ -126,7 +126,7 @@ public class BpfInterfaceMapUpdater {
}
try {
mBpfMap.updateEntry(new U32(iface.index), new InterfaceMapValue(ifaceName));
mBpfMap.updateEntry(new S32(iface.index), new InterfaceMapValue(ifaceName));
} catch (ErrnoException 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 */
public String getIfNameByIndex(final long index) {
public String getIfNameByIndex(final int index) {
try {
final InterfaceMapValue value = mBpfMap.getValue(new U32(index));
final InterfaceMapValue value = mBpfMap.getValue(new S32(index));
if (value == null) {
Log.e(TAG, "No if name entry for index " + index);
return null;

View File

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

View File

@@ -33,11 +33,11 @@ public class StatsMapKey extends Struct {
@Field(order = 2, type = Type.U32)
public final long counterSet;
@Field(order = 3, type = Type.U32)
public final long ifaceIndex;
@Field(order = 3, type = Type.S32)
public final int ifaceIndex;
public StatsMapKey(final long uid, final long tag, final long counterSet,
final long ifaceIndex) {
final int ifaceIndex) {
this.uid = uid;
this.tag = tag;
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.IBpfMap;
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.DevSdkIgnoreRunner;
import com.android.testutils.TestBpfMap;
@@ -69,14 +69,14 @@ public final class BpfInterfaceMapUpdaterTest {
private final TestLooper mLooper = new TestLooper();
private BaseNetdUnsolicitedEventListener mListener;
private BpfInterfaceMapUpdater mUpdater;
private IBpfMap<U32, InterfaceMapValue> mBpfMap =
spy(new TestBpfMap<>(U32.class, InterfaceMapValue.class));
private IBpfMap<S32, InterfaceMapValue> mBpfMap =
spy(new TestBpfMap<>(S32.class, InterfaceMapValue.class));
@Mock private INetd mNetd;
@Mock private Context mContext;
private class TestDependencies extends BpfInterfaceMapUpdater.Dependencies {
@Override
public IBpfMap<U32, InterfaceMapValue> getInterfaceMap() {
public IBpfMap<S32, InterfaceMapValue> getInterfaceMap() {
return mBpfMap;
}
@@ -114,7 +114,7 @@ public final class BpfInterfaceMapUpdaterTest {
ArgumentCaptor.forClass(BaseNetdUnsolicitedEventListener.class);
verify(mNetd).registerUnsolicitedEventListener(listenerCaptor.capture());
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)));
}
@@ -124,7 +124,7 @@ public final class BpfInterfaceMapUpdaterTest {
mListener.onInterfaceAdded(TEST_INTERFACE_NAME2);
mLooper.dispatchAll();
verify(mBpfMap).updateEntry(eq(new U32(TEST_INDEX2)),
verify(mBpfMap).updateEntry(eq(new S32(TEST_INDEX2)),
eq(new InterfaceMapValue(TEST_INTERFACE_NAME2)));
// Check that when onInterfaceRemoved is called, nothing happens.
@@ -135,7 +135,7 @@ public final class BpfInterfaceMapUpdaterTest {
@Test
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));
}
@@ -146,7 +146,7 @@ public final class BpfInterfaceMapUpdaterTest {
@Test
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));
}
@@ -163,8 +163,8 @@ public final class BpfInterfaceMapUpdaterTest {
@Test
public void testDump() throws ErrnoException {
mBpfMap.updateEntry(new U32(TEST_INDEX), new InterfaceMapValue(TEST_INTERFACE_NAME));
mBpfMap.updateEntry(new U32(TEST_INDEX2), new InterfaceMapValue(TEST_INTERFACE_NAME2));
mBpfMap.updateEntry(new S32(TEST_INDEX), new InterfaceMapValue(TEST_INTERFACE_NAME));
mBpfMap.updateEntry(new S32(TEST_INDEX2), new InterfaceMapValue(TEST_INTERFACE_NAME2));
final String dump = getDump();
assertDumpContains(dump, "IfaceIndexNameMap: OK");