refactor: continued

Test: builds, atest, TreeHugger
  git grep 'makeIngressKey|makeIngressValue|BpfIngressMap|TETHER_INGRESS_FS_PATH'
  no longer finds anything
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I27200d33cbc4ea2094b18e05bf93ab54c564da0b
This commit is contained in:
Maciej Żenczykowski
2021-01-20 02:46:38 -08:00
parent 770e0a7a8f
commit a0e2c0445d
5 changed files with 27 additions and 23 deletions

View File

@@ -69,7 +69,7 @@ public class BpfCoordinatorShimImpl
public BpfCoordinatorShimImpl(@NonNull final Dependencies deps) { public BpfCoordinatorShimImpl(@NonNull final Dependencies deps) {
mLog = deps.getSharedLog().forSubComponent(TAG); mLog = deps.getSharedLog().forSubComponent(TAG);
mBpfDownstream6Map = deps.getBpfIngressMap(); mBpfDownstream6Map = deps.getBpfDownstream6Map();
mBpfStatsMap = deps.getBpfStatsMap(); mBpfStatsMap = deps.getBpfStatsMap();
mBpfLimitMap = deps.getBpfLimitMap(); mBpfLimitMap = deps.getBpfLimitMap();
} }

View File

@@ -73,7 +73,7 @@ import java.util.Objects;
public class BpfCoordinator { public class BpfCoordinator {
private static final String TAG = BpfCoordinator.class.getSimpleName(); private static final String TAG = BpfCoordinator.class.getSimpleName();
private static final int DUMP_TIMEOUT_MS = 10_000; private static final int DUMP_TIMEOUT_MS = 10_000;
private static final String TETHER_INGRESS_FS_PATH = private static final String TETHER_DOWNSTREAM6_FS_PATH =
"/sys/fs/bpf/map_offload_tether_downstream6_map"; "/sys/fs/bpf/map_offload_tether_downstream6_map";
private static final String TETHER_STATS_MAP_PATH = private static final String TETHER_STATS_MAP_PATH =
"/sys/fs/bpf/map_offload_tether_stats_map"; "/sys/fs/bpf/map_offload_tether_stats_map";
@@ -190,13 +190,14 @@ public class BpfCoordinator {
return SdkLevel.isAtLeastS(); return SdkLevel.isAtLeastS();
} }
/** Get ingress BPF map. */ /** Get downstream6 BPF map. */
@Nullable public BpfMap<TetherDownstream6Key, TetherDownstream6Value> getBpfIngressMap() { @Nullable public BpfMap<TetherDownstream6Key, TetherDownstream6Value>
getBpfDownstream6Map() {
try { try {
return new BpfMap<>(TETHER_INGRESS_FS_PATH, return new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH,
BpfMap.BPF_F_RDWR, TetherDownstream6Key.class, TetherDownstream6Value.class); BpfMap.BPF_F_RDWR, TetherDownstream6Key.class, TetherDownstream6Value.class);
} catch (ErrnoException e) { } catch (ErrnoException e) {
Log.e(TAG, "Cannot create ingress map: " + e); Log.e(TAG, "Cannot create downstream6 map: " + e);
return null; return null;
} }
} }

View File

@@ -51,7 +51,7 @@ import java.util.concurrent.atomic.AtomicInteger;
public final class BpfMapTest { public final class BpfMapTest {
// Sync from packages/modules/Connectivity/Tethering/bpf_progs/offload.c. // Sync from packages/modules/Connectivity/Tethering/bpf_progs/offload.c.
private static final int TEST_MAP_SIZE = 16; private static final int TEST_MAP_SIZE = 16;
private static final String TETHER_INGRESS_FS_PATH = private static final String TETHER_DOWNSTREAM6_FS_PATH =
"/sys/fs/bpf/tethering/map_test_tether_downstream6_map"; "/sys/fs/bpf/tethering/map_test_tether_downstream6_map";
private ArrayMap<TetherDownstream6Key, TetherDownstream6Value> mTestData; private ArrayMap<TetherDownstream6Key, TetherDownstream6Value> mTestData;
@@ -88,7 +88,7 @@ public final class BpfMapTest {
private BpfMap<TetherDownstream6Key, TetherDownstream6Value> getTestMap() throws Exception { private BpfMap<TetherDownstream6Key, TetherDownstream6Value> getTestMap() throws Exception {
return new BpfMap<>( return new BpfMap<>(
TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_RDWR, TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR,
TetherDownstream6Key.class, TetherDownstream6Value.class); TetherDownstream6Key.class, TetherDownstream6Value.class);
} }
@@ -122,7 +122,7 @@ public final class BpfMapTest {
@Test @Test
public void testGetFd() throws Exception { public void testGetFd() throws Exception {
try (BpfMap readOnlyMap = new BpfMap<>(TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_RDONLY, try (BpfMap readOnlyMap = new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_RDONLY,
TetherDownstream6Key.class, TetherDownstream6Value.class)) { TetherDownstream6Key.class, TetherDownstream6Value.class)) {
assertNotNull(readOnlyMap); assertNotNull(readOnlyMap);
try { try {
@@ -132,7 +132,7 @@ public final class BpfMapTest {
assertEquals(OsConstants.EPERM, expected.errno); assertEquals(OsConstants.EPERM, expected.errno);
} }
} }
try (BpfMap writeOnlyMap = new BpfMap<>(TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_WRONLY, try (BpfMap writeOnlyMap = new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_WRONLY,
TetherDownstream6Key.class, TetherDownstream6Value.class)) { TetherDownstream6Key.class, TetherDownstream6Value.class)) {
assertNotNull(writeOnlyMap); assertNotNull(writeOnlyMap);
try { try {
@@ -142,7 +142,7 @@ public final class BpfMapTest {
assertEquals(OsConstants.EPERM, expected.errno); assertEquals(OsConstants.EPERM, expected.errno);
} }
} }
try (BpfMap readWriteMap = new BpfMap<>(TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_RDWR, try (BpfMap readWriteMap = new BpfMap<>(TETHER_DOWNSTREAM6_FS_PATH, BpfMap.BPF_F_RDWR,
TetherDownstream6Key.class, TetherDownstream6Value.class)) { TetherDownstream6Key.class, TetherDownstream6Value.class)) {
assertNotNull(readWriteMap); assertNotNull(readWriteMap);
} }

View File

@@ -296,7 +296,8 @@ public class IpServerTest {
} }
@Nullable @Nullable
public BpfMap<TetherDownstream6Key, TetherDownstream6Value> getBpfIngressMap() { public BpfMap<TetherDownstream6Key, TetherDownstream6Value>
getBpfDownstream6Map() {
return mBpfDownstream6Map; return mBpfDownstream6Map;
} }
@@ -770,13 +771,13 @@ public class IpServerTest {
} }
@NonNull @NonNull
private static TetherDownstream6Key makeIngressKey(int upstreamIfindex, private static TetherDownstream6Key makeDownstream6Key(int upstreamIfindex,
@NonNull final InetAddress dst) { @NonNull final InetAddress dst) {
return new TetherDownstream6Key(upstreamIfindex, dst.getAddress()); return new TetherDownstream6Key(upstreamIfindex, dst.getAddress());
} }
@NonNull @NonNull
private static TetherDownstream6Value makeIngressValue(@NonNull final MacAddress dstMac) { private static TetherDownstream6Value makeDownstream6Value(@NonNull final MacAddress dstMac) {
return new TetherDownstream6Value(TEST_IFACE_PARAMS.index, dstMac, return new TetherDownstream6Value(TEST_IFACE_PARAMS.index, dstMac,
TEST_IFACE_PARAMS.macAddr, ETH_P_IPV6, NetworkStackConstants.ETHER_MTU); TEST_IFACE_PARAMS.macAddr, ETH_P_IPV6, NetworkStackConstants.ETHER_MTU);
} }
@@ -793,7 +794,7 @@ public class IpServerTest {
@NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception { @NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception {
if (mBpfDeps.isAtLeastS()) { if (mBpfDeps.isAtLeastS()) {
verifyWithOrder(inOrder, mBpfDownstream6Map).updateEntry( verifyWithOrder(inOrder, mBpfDownstream6Map).updateEntry(
makeIngressKey(upstreamIfindex, dst), makeIngressValue(dstMac)); makeDownstream6Key(upstreamIfindex, dst), makeDownstream6Value(dstMac));
} else { } else {
verifyWithOrder(inOrder, mNetd).tetherOffloadRuleAdd(matches(upstreamIfindex, dst, verifyWithOrder(inOrder, mNetd).tetherOffloadRuleAdd(matches(upstreamIfindex, dst,
dstMac)); dstMac));
@@ -803,8 +804,9 @@ public class IpServerTest {
private void verifyNeverTetherOffloadRuleAdd(int upstreamIfindex, private void verifyNeverTetherOffloadRuleAdd(int upstreamIfindex,
@NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception { @NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception {
if (mBpfDeps.isAtLeastS()) { if (mBpfDeps.isAtLeastS()) {
verify(mBpfDownstream6Map, never()).updateEntry(makeIngressKey(upstreamIfindex, dst), verify(mBpfDownstream6Map, never()).updateEntry(
makeIngressValue(dstMac)); makeDownstream6Key(upstreamIfindex, dst),
makeDownstream6Value(dstMac));
} else { } else {
verify(mNetd, never()).tetherOffloadRuleAdd(matches(upstreamIfindex, dst, dstMac)); verify(mNetd, never()).tetherOffloadRuleAdd(matches(upstreamIfindex, dst, dstMac));
} }
@@ -821,8 +823,8 @@ public class IpServerTest {
private void verifyTetherOffloadRuleRemove(@Nullable InOrder inOrder, int upstreamIfindex, private void verifyTetherOffloadRuleRemove(@Nullable InOrder inOrder, int upstreamIfindex,
@NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception { @NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception {
if (mBpfDeps.isAtLeastS()) { if (mBpfDeps.isAtLeastS()) {
verifyWithOrder(inOrder, mBpfDownstream6Map).deleteEntry(makeIngressKey(upstreamIfindex, verifyWithOrder(inOrder, mBpfDownstream6Map).deleteEntry(makeDownstream6Key(
dst)); upstreamIfindex, dst));
} else { } else {
// |dstMac| is not required for deleting rules. Used bacause tetherOffloadRuleRemove // |dstMac| is not required for deleting rules. Used bacause tetherOffloadRuleRemove
// uses a whole rule to be a argument. // uses a whole rule to be a argument.

View File

@@ -194,7 +194,8 @@ public class BpfCoordinatorTest {
} }
@Nullable @Nullable
public BpfMap<TetherDownstream6Key, TetherDownstream6Value> getBpfIngressMap() { public BpfMap<TetherDownstream6Key, TetherDownstream6Value>
getBpfDownstream6Map() {
return mBpfDownstream6Map; return mBpfDownstream6Map;
} }
@@ -846,7 +847,7 @@ public class BpfCoordinatorTest {
private void checkBpfDisabled() throws Exception { private void checkBpfDisabled() throws Exception {
// The caller may mock the global dependencies |mDeps| which is used in // The caller may mock the global dependencies |mDeps| which is used in
// #makeBpfCoordinator for testing. // #makeBpfCoordinator for testing.
// See #testBpfDisabledbyNoBpfIngressMap. // See #testBpfDisabledbyNoBpfDownstream6Map.
final BpfCoordinator coordinator = makeBpfCoordinator(); final BpfCoordinator coordinator = makeBpfCoordinator();
coordinator.startPolling(); coordinator.startPolling();
@@ -909,9 +910,9 @@ public class BpfCoordinatorTest {
@Test @Test
@IgnoreUpTo(Build.VERSION_CODES.R) @IgnoreUpTo(Build.VERSION_CODES.R)
public void testBpfDisabledbyNoBpfIngressMap() throws Exception { public void testBpfDisabledbyNoBpfDownstream6Map() throws Exception {
setupFunctioningNetdInterface(); setupFunctioningNetdInterface();
doReturn(null).when(mDeps).getBpfIngressMap(); doReturn(null).when(mDeps).getBpfDownstream6Map();
checkBpfDisabled(); checkBpfDisabled();
} }