From 770e0a7a8fffbaa47513f3d43f362f51cfba033f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Mon, 18 Jan 2021 20:14:03 -0800 Subject: [PATCH] refactor: TetherIngress becomes TetherDownstream6 (and friends) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Generated via: #!/bin/bash do_replace() { pushd "$1" >/dev/null shift git grep "$1" | cut -d: -f1 | sort -u | while read f; do sed -i -r "s@$1@$2@g" "${f}" git add "${f}" done popd >/dev/null } replace() { do_replace packages/modules/Connectivity/Tethering "$@" do_replace system/netd "$@" } do_reset() { pushd "$1" >/dev/null git rhh popd >/dev/null } main() { do_reset packages/modules/Connectivity/Tethering do_reset system/netd replace TetherIngressKey TetherDownstream6Key replace TetherIngressValue TetherDownstream6Value pushd packages/modules/Connectivity/Tethering/src/com/android/networkstack/tethering >/dev/null git mv TetherIngressKey.java TetherDownstream6Key.java git mv TetherIngressValue.java TetherDownstream6Value.java popd >/dev/null replace TETHER_INGRESS_PROG TETHER_DOWNSTREAM6_TC_PROG replace ingress_tether tether_downstream6 replace ingress/tether_ tether_downstream6_ replace TETHER_INGRESS_MAP TETHER_DOWNSTREAM6_MAP replace tether_ingress_map tether_downstream6_map replace getTetherIngressMapFd getTetherDownstream6MapFd replace getTetherIngressProgFd getTetherDownstream6TcProgFd replace mBpfIngressMap mBpfDownstream6Map replace bpfIngressMap bpfDownstream6Map replace printIngressMap printDownstream6Map replace 'BPF ingress map' 'BPF downstream ipv6 map' } main "$@"; exit Followed by: cd system/netd # change regexp in tests/binder_test.cpp:3374 git add tests/binder_test.cpp (and whitespace adjustments to prevent lines from going over 100 characters) Afterwards we have: $ adbz shell ls /sys/fs/bpf/tethering map_offload_tether_downstream6_map map_offload_tether_limit_map map_offload_tether_stats_map map_test_tether_downstream6_map prog_offload_schedcls_tether_downstream6_ether prog_offload_schedcls_tether_downstream6_rawip prog_test_xdp_drop_ipv4_udp_ether Test: atest, TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: Ib04dc0c6e3d034461f3f84e09f6fa93a862e1bb2 --- .../apishim/api31/BpfCoordinatorShimImpl.java | 22 ++-- Tethering/bpf_progs/offload.c | 22 ++-- Tethering/bpf_progs/test.c | 2 +- .../tethering/BpfCoordinator.java | 18 +-- ...ressKey.java => TetherDownstream6Key.java} | 10 +- ...Value.java => TetherDownstream6Value.java} | 8 +- .../networkstack/tethering/BpfMapTest.java | 103 +++++++++--------- .../unit/src/android/net/ip/IpServerTest.java | 44 ++++---- .../tethering/BpfCoordinatorTest.java | 33 +++--- 9 files changed, 134 insertions(+), 128 deletions(-) rename Tethering/src/com/android/networkstack/tethering/{TetherIngressKey.java => TetherDownstream6Key.java} (86%) rename Tethering/src/com/android/networkstack/tethering/{TetherIngressValue.java => TetherDownstream6Value.java} (89%) 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 4ebf9144f2..78fa533161 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 @@ -30,8 +30,8 @@ import androidx.annotation.Nullable; import com.android.networkstack.tethering.BpfCoordinator.Dependencies; import com.android.networkstack.tethering.BpfCoordinator.Ipv6ForwardingRule; import com.android.networkstack.tethering.BpfMap; -import com.android.networkstack.tethering.TetherIngressKey; -import com.android.networkstack.tethering.TetherIngressValue; +import com.android.networkstack.tethering.TetherDownstream6Key; +import com.android.networkstack.tethering.TetherDownstream6Value; import com.android.networkstack.tethering.TetherLimitKey; import com.android.networkstack.tethering.TetherLimitValue; import com.android.networkstack.tethering.TetherStatsKey; @@ -57,7 +57,7 @@ public class BpfCoordinatorShimImpl // BPF map of ingress queueing discipline which pre-processes the packets by the IPv6 // forwarding rules. @Nullable - private final BpfMap mBpfIngressMap; + private final BpfMap mBpfDownstream6Map; // BPF map of tethering statistics of the upstream interface since tethering startup. @Nullable @@ -69,25 +69,25 @@ public class BpfCoordinatorShimImpl public BpfCoordinatorShimImpl(@NonNull final Dependencies deps) { mLog = deps.getSharedLog().forSubComponent(TAG); - mBpfIngressMap = deps.getBpfIngressMap(); + mBpfDownstream6Map = deps.getBpfIngressMap(); mBpfStatsMap = deps.getBpfStatsMap(); mBpfLimitMap = deps.getBpfLimitMap(); } @Override public boolean isInitialized() { - return mBpfIngressMap != null && mBpfStatsMap != null && mBpfLimitMap != null; + return mBpfDownstream6Map != null && mBpfStatsMap != null && mBpfLimitMap != null; } @Override public boolean tetherOffloadRuleAdd(@NonNull final Ipv6ForwardingRule rule) { if (!isInitialized()) return false; - final TetherIngressKey key = rule.makeTetherIngressKey(); - final TetherIngressValue value = rule.makeTetherIngressValue(); + final TetherDownstream6Key key = rule.makeTetherDownstream6Key(); + final TetherDownstream6Value value = rule.makeTetherDownstream6Value(); try { - mBpfIngressMap.updateEntry(key, value); + mBpfDownstream6Map.updateEntry(key, value); } catch (ErrnoException e) { mLog.e("Could not update entry: ", e); return false; @@ -101,7 +101,7 @@ public class BpfCoordinatorShimImpl if (!isInitialized()) return false; try { - mBpfIngressMap.deleteEntry(rule.makeTetherIngressKey()); + mBpfDownstream6Map.deleteEntry(rule.makeTetherDownstream6Key()); } catch (ErrnoException e) { // Silent if the rule did not exist. if (e.errno != OsConstants.ENOENT) { @@ -234,8 +234,8 @@ public class BpfCoordinatorShimImpl @Override public String toString() { - return "mBpfIngressMap{" - + (mBpfIngressMap != null ? "initialized" : "not initialized") + "}, " + return "mBpfDownstream6Map{" + + (mBpfDownstream6Map != null ? "initialized" : "not initialized") + "}, " + "mBpfStatsMap{" + (mBpfStatsMap != null ? "initialized" : "not initialized") + "}, " + "mBpfLimitMap{" diff --git a/Tethering/bpf_progs/offload.c b/Tethering/bpf_progs/offload.c index c66bb5cf65..cb1c0e5966 100644 --- a/Tethering/bpf_progs/offload.c +++ b/Tethering/bpf_progs/offload.c @@ -24,7 +24,7 @@ #include "bpf_net_helpers.h" #include "netdbpf/bpf_shared.h" -DEFINE_BPF_MAP_GRW(tether_ingress_map, HASH, TetherIngressKey, TetherIngressValue, 64, +DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, TetherDownstream6Value, 64, AID_NETWORK_STACK) // Tethering stats, indexed by upstream interface. @@ -63,12 +63,12 @@ static inline __always_inline int do_forward(struct __sk_buff* skb, bool is_ethe (src32 & htonl(0xe0000000)) != htonl(0x20000000)) // 2000::/3 Global Unicast return TC_ACT_OK; - TetherIngressKey k = { + TetherDownstream6Key k = { .iif = skb->ifindex, .neigh6 = ip6->daddr, }; - TetherIngressValue* v = bpf_tether_ingress_map_lookup_elem(&k); + TetherDownstream6Value* v = bpf_tether_downstream6_map_lookup_elem(&k); // If we don't find any offload information then simply let the core stack handle it... if (!v) return TC_ACT_OK; @@ -162,8 +162,8 @@ static inline __always_inline int do_forward(struct __sk_buff* skb, bool is_ethe return bpf_redirect(v->oif, 0 /* this is effectively BPF_F_EGRESS */); } -DEFINE_BPF_PROG("schedcls/ingress/tether_ether", AID_ROOT, AID_ROOT, - sched_cls_ingress_tether_ether) +DEFINE_BPF_PROG("schedcls/tether_downstream6_ether", AID_ROOT, AID_ROOT, + sched_cls_tether_downstream6_ether) (struct __sk_buff* skb) { return do_forward(skb, true); } @@ -181,15 +181,15 @@ DEFINE_BPF_PROG("schedcls/ingress/tether_ether", AID_ROOT, AID_ROOT, // and thus a 5.4 kernel always supports this. // // Hence, this mandatory (must load successfully) implementation for 5.4+ kernels: -DEFINE_BPF_PROG_KVER("schedcls/ingress/tether_rawip$5_4", AID_ROOT, AID_ROOT, - sched_cls_ingress_tether_rawip_5_4, KVER(5, 4, 0)) +DEFINE_BPF_PROG_KVER("schedcls/tether_downstream6_rawip$5_4", AID_ROOT, AID_ROOT, + sched_cls_tether_downstream6_rawip_5_4, KVER(5, 4, 0)) (struct __sk_buff* skb) { return do_forward(skb, false); } // and this identical optional (may fail to load) implementation for [4.14..5.4) patched kernels: -DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/ingress/tether_rawip$4_14", AID_ROOT, AID_ROOT, - sched_cls_ingress_tether_rawip_4_14, KVER(4, 14, 0), +DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$4_14", AID_ROOT, AID_ROOT, + sched_cls_tether_downstream6_rawip_4_14, KVER(4, 14, 0), KVER(5, 4, 0)) (struct __sk_buff* skb) { return do_forward(skb, false); @@ -198,8 +198,8 @@ DEFINE_OPTIONAL_BPF_PROG_KVER_RANGE("schedcls/ingress/tether_rawip$4_14", AID_RO // and define a no-op stub for [4.9,4.14) and unpatched [4.14,5.4) kernels. // (if the above real 4.14+ program loaded successfully, then bpfloader will have already pinned // it at the same location this one would be pinned at and will thus skip loading this stub) -DEFINE_BPF_PROG_KVER_RANGE("schedcls/ingress/tether_rawip$stub", AID_ROOT, AID_ROOT, - sched_cls_ingress_tether_rawip_stub, KVER_NONE, KVER(5, 4, 0)) +DEFINE_BPF_PROG_KVER_RANGE("schedcls/tether_downstream6_rawip$stub", AID_ROOT, AID_ROOT, + sched_cls_tether_downstream6_rawip_stub, KVER_NONE, KVER(5, 4, 0)) (struct __sk_buff* skb) { return TC_ACT_OK; } diff --git a/Tethering/bpf_progs/test.c b/Tethering/bpf_progs/test.c index eeaaa62003..4dd33e60e8 100644 --- a/Tethering/bpf_progs/test.c +++ b/Tethering/bpf_progs/test.c @@ -23,7 +23,7 @@ #include "netdbpf/bpf_shared.h" // Used only by TetheringPrivilegedTests, not by production code. -DEFINE_BPF_MAP_GRW(tether_ingress_map, HASH, TetherIngressKey, TetherIngressValue, 16, +DEFINE_BPF_MAP_GRW(tether_downstream6_map, HASH, TetherDownstream6Key, TetherDownstream6Value, 16, AID_NETWORK_STACK) DEFINE_BPF_PROG_KVER("xdp/drop_ipv4_udp_ether", AID_ROOT, AID_ROOT, diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java index 64ac37ce03..fac5725fd3 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java @@ -74,7 +74,7 @@ public class BpfCoordinator { private static final String TAG = BpfCoordinator.class.getSimpleName(); private static final int DUMP_TIMEOUT_MS = 10_000; private static final String TETHER_INGRESS_FS_PATH = - "/sys/fs/bpf/map_offload_tether_ingress_map"; + "/sys/fs/bpf/map_offload_tether_downstream6_map"; private static final String TETHER_STATS_MAP_PATH = "/sys/fs/bpf/map_offload_tether_stats_map"; private static final String TETHER_LIMIT_MAP_PATH = @@ -191,10 +191,10 @@ public class BpfCoordinator { } /** Get ingress BPF map. */ - @Nullable public BpfMap getBpfIngressMap() { + @Nullable public BpfMap getBpfIngressMap() { try { return new BpfMap<>(TETHER_INGRESS_FS_PATH, - BpfMap.BPF_F_RDWR, TetherIngressKey.class, TetherIngressValue.class); + BpfMap.BPF_F_RDWR, TetherDownstream6Key.class, TetherDownstream6Value.class); } catch (ErrnoException e) { Log.e(TAG, "Cannot create ingress map: " + e); return null; @@ -554,19 +554,19 @@ public class BpfCoordinator { } /** - * Return a TetherIngressKey object built from the rule. + * Return a TetherDownstream6Key object built from the rule. */ @NonNull - public TetherIngressKey makeTetherIngressKey() { - return new TetherIngressKey(upstreamIfindex, address.getAddress()); + public TetherDownstream6Key makeTetherDownstream6Key() { + return new TetherDownstream6Key(upstreamIfindex, address.getAddress()); } /** - * Return a TetherIngressValue object built from the rule. + * Return a TetherDownstream6Value object built from the rule. */ @NonNull - public TetherIngressValue makeTetherIngressValue() { - return new TetherIngressValue(downstreamIfindex, dstMac, srcMac, ETH_P_IPV6, + public TetherDownstream6Value makeTetherDownstream6Value() { + return new TetherDownstream6Value(downstreamIfindex, dstMac, srcMac, ETH_P_IPV6, NetworkStackConstants.ETHER_MTU); } diff --git a/Tethering/src/com/android/networkstack/tethering/TetherIngressKey.java b/Tethering/src/com/android/networkstack/tethering/TetherDownstream6Key.java similarity index 86% rename from Tethering/src/com/android/networkstack/tethering/TetherIngressKey.java rename to Tethering/src/com/android/networkstack/tethering/TetherDownstream6Key.java index 78683c5e1c..3860cba745 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetherIngressKey.java +++ b/Tethering/src/com/android/networkstack/tethering/TetherDownstream6Key.java @@ -26,14 +26,14 @@ import java.net.UnknownHostException; import java.util.Arrays; /** The key of BpfMap which is used for bpf offload. */ -public class TetherIngressKey extends Struct { +public class TetherDownstream6Key extends Struct { @Field(order = 0, type = Type.U32) public final long iif; // The input interface index. @Field(order = 1, type = Type.ByteArray, arraysize = 16) public final byte[] neigh6; // The destination IPv6 address. - public TetherIngressKey(final long iif, final byte[] neigh6) { + public TetherDownstream6Key(final long iif, final byte[] neigh6) { try { final Inet6Address unused = (Inet6Address) InetAddress.getByAddress(neigh6); } catch (ClassCastException | UnknownHostException e) { @@ -48,9 +48,9 @@ public class TetherIngressKey extends Struct { public boolean equals(Object obj) { if (this == obj) return true; - if (!(obj instanceof TetherIngressKey)) return false; + if (!(obj instanceof TetherDownstream6Key)) return false; - final TetherIngressKey that = (TetherIngressKey) obj; + final TetherDownstream6Key that = (TetherDownstream6Key) obj; return iif == that.iif && Arrays.equals(neigh6, that.neigh6); } @@ -66,7 +66,7 @@ public class TetherIngressKey extends Struct { return String.format("iif: %d, neigh: %s", iif, Inet6Address.getByAddress(neigh6)); } catch (UnknownHostException e) { // Should not happen because construtor already verify neigh6. - throw new IllegalStateException("Invalid TetherIngressKey"); + throw new IllegalStateException("Invalid TetherDownstream6Key"); } } } diff --git a/Tethering/src/com/android/networkstack/tethering/TetherIngressValue.java b/Tethering/src/com/android/networkstack/tethering/TetherDownstream6Value.java similarity index 89% rename from Tethering/src/com/android/networkstack/tethering/TetherIngressValue.java rename to Tethering/src/com/android/networkstack/tethering/TetherDownstream6Value.java index e2116fc2d5..a56269dad7 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetherIngressValue.java +++ b/Tethering/src/com/android/networkstack/tethering/TetherDownstream6Value.java @@ -27,7 +27,7 @@ import com.android.net.module.util.Struct.Type; import java.util.Objects; /** The value of BpfMap which is used for bpf offload. */ -public class TetherIngressValue extends Struct { +public class TetherDownstream6Value extends Struct { @Field(order = 0, type = Type.U32) public final long oif; // The output interface index. @@ -42,7 +42,7 @@ public class TetherIngressValue extends Struct { @Field(order = 4, type = Type.U16) public final int pmtu; // The maximum L3 output path/route mtu. - public TetherIngressValue(final long oif, @NonNull final MacAddress ethDstMac, + public TetherDownstream6Value(final long oif, @NonNull final MacAddress ethDstMac, @NonNull final MacAddress ethSrcMac, final int ethProto, final int pmtu) { Objects.requireNonNull(ethSrcMac); Objects.requireNonNull(ethDstMac); @@ -58,9 +58,9 @@ public class TetherIngressValue extends Struct { public boolean equals(Object obj) { if (this == obj) return true; - if (!(obj instanceof TetherIngressValue)) return false; + if (!(obj instanceof TetherDownstream6Value)) return false; - final TetherIngressValue that = (TetherIngressValue) obj; + final TetherDownstream6Value that = (TetherDownstream6Value) obj; return oif == that.oif && ethDstMac.equals(that.ethDstMac) && ethSrcMac.equals(that.ethSrcMac) && ethProto == that.ethProto diff --git a/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java b/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java index dba8fab1fe..bff204b974 100644 --- a/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java +++ b/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java @@ -52,9 +52,9 @@ public final class BpfMapTest { // Sync from packages/modules/Connectivity/Tethering/bpf_progs/offload.c. private static final int TEST_MAP_SIZE = 16; private static final String TETHER_INGRESS_FS_PATH = - "/sys/fs/bpf/tethering/map_test_tether_ingress_map"; + "/sys/fs/bpf/tethering/map_test_tether_downstream6_map"; - private ArrayMap mTestData; + private ArrayMap mTestData; @BeforeClass public static void setupOnce() { @@ -70,15 +70,15 @@ public final class BpfMapTest { cleanTestMap(); mTestData = new ArrayMap<>(); - mTestData.put(createTetherIngressKey(101, "2001:db8::1"), - createTetherIngressValue(11, "00:00:00:00:00:0a", "11:11:11:00:00:0b", ETH_P_IPV6, - 1280)); - mTestData.put(createTetherIngressKey(102, "2001:db8::2"), - createTetherIngressValue(22, "00:00:00:00:00:0c", "22:22:22:00:00:0d", ETH_P_IPV6, - 1400)); - mTestData.put(createTetherIngressKey(103, "2001:db8::3"), - createTetherIngressValue(33, "00:00:00:00:00:0e", "33:33:33:00:00:0f", ETH_P_IPV6, - 1500)); + mTestData.put(createTetherDownstream6Key(101, "2001:db8::1"), + createTetherDownstream6Value(11, "00:00:00:00:00:0a", "11:11:11:00:00:0b", + ETH_P_IPV6, 1280)); + mTestData.put(createTetherDownstream6Key(102, "2001:db8::2"), + createTetherDownstream6Value(22, "00:00:00:00:00:0c", "22:22:22:00:00:0d", + ETH_P_IPV6, 1400)); + mTestData.put(createTetherDownstream6Key(103, "2001:db8::3"), + createTetherDownstream6Value(33, "00:00:00:00:00:0e", "33:33:33:00:00:0f", + ETH_P_IPV6, 1500)); } @After @@ -86,14 +86,14 @@ public final class BpfMapTest { cleanTestMap(); } - private BpfMap getTestMap() throws Exception { + private BpfMap getTestMap() throws Exception { return new BpfMap<>( TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_RDWR, - TetherIngressKey.class, TetherIngressValue.class); + TetherDownstream6Key.class, TetherDownstream6Value.class); } private void cleanTestMap() throws Exception { - try (BpfMap bpfMap = getTestMap()) { + try (BpfMap bpfMap = getTestMap()) { bpfMap.forEach((key, value) -> { try { assertTrue(bpfMap.deleteEntry(key)); @@ -105,24 +105,25 @@ public final class BpfMapTest { } } - private TetherIngressKey createTetherIngressKey(long iif, String address) throws Exception { + private TetherDownstream6Key createTetherDownstream6Key(long iif, String address) + throws Exception { final InetAddress ipv6Address = InetAddress.getByName(address); - return new TetherIngressKey(iif, ipv6Address.getAddress()); + return new TetherDownstream6Key(iif, ipv6Address.getAddress()); } - private TetherIngressValue createTetherIngressValue(long oif, String src, String dst, int proto, - int pmtu) throws Exception { + private TetherDownstream6Value createTetherDownstream6Value(long oif, String src, String dst, + int proto, int pmtu) throws Exception { final MacAddress srcMac = MacAddress.fromString(src); final MacAddress dstMac = MacAddress.fromString(dst); - return new TetherIngressValue(oif, dstMac, srcMac, proto, pmtu); + return new TetherDownstream6Value(oif, dstMac, srcMac, proto, pmtu); } @Test public void testGetFd() throws Exception { try (BpfMap readOnlyMap = new BpfMap<>(TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_RDONLY, - TetherIngressKey.class, TetherIngressValue.class)) { + TetherDownstream6Key.class, TetherDownstream6Value.class)) { assertNotNull(readOnlyMap); try { readOnlyMap.insertEntry(mTestData.keyAt(0), mTestData.valueAt(0)); @@ -132,7 +133,7 @@ public final class BpfMapTest { } } try (BpfMap writeOnlyMap = new BpfMap<>(TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_WRONLY, - TetherIngressKey.class, TetherIngressValue.class)) { + TetherDownstream6Key.class, TetherDownstream6Value.class)) { assertNotNull(writeOnlyMap); try { writeOnlyMap.getFirstKey(); @@ -142,14 +143,14 @@ public final class BpfMapTest { } } try (BpfMap readWriteMap = new BpfMap<>(TETHER_INGRESS_FS_PATH, BpfMap.BPF_F_RDWR, - TetherIngressKey.class, TetherIngressValue.class)) { + TetherDownstream6Key.class, TetherDownstream6Value.class)) { assertNotNull(readWriteMap); } } @Test public void testGetFirstKey() throws Exception { - try (BpfMap bpfMap = getTestMap()) { + try (BpfMap bpfMap = getTestMap()) { // getFirstKey on an empty map returns null. assertFalse(bpfMap.containsKey(mTestData.keyAt(0))); assertNull(bpfMap.getFirstKey()); @@ -163,9 +164,10 @@ public final class BpfMapTest { @Test public void testGetNextKey() throws Exception { - try (BpfMap bpfMap = getTestMap()) { + try (BpfMap bpfMap = getTestMap()) { // [1] If the passed-in key is not found on empty map, return null. - final TetherIngressKey nonexistentKey = createTetherIngressKey(1234, "2001:db8::10"); + final TetherDownstream6Key nonexistentKey = + createTetherDownstream6Key(1234, "2001:db8::10"); assertNull(bpfMap.getNextKey(nonexistentKey)); // [2] If the passed-in key is null on empty map, throw NullPointerException. @@ -175,13 +177,14 @@ public final class BpfMapTest { } catch (NullPointerException expected) { } // The BPF map has one entry now. - final ArrayMap resultMap = new ArrayMap<>(); + final ArrayMap resultMap = + new ArrayMap<>(); bpfMap.insertEntry(mTestData.keyAt(0), mTestData.valueAt(0)); resultMap.put(mTestData.keyAt(0), mTestData.valueAt(0)); // [3] If the passed-in key is the last key, return null. // Because there is only one entry in the map, the first key equals the last key. - final TetherIngressKey lastKey = bpfMap.getFirstKey(); + final TetherDownstream6Key lastKey = bpfMap.getFirstKey(); assertNull(bpfMap.getNextKey(lastKey)); // The BPF map has two entries now. @@ -189,7 +192,7 @@ public final class BpfMapTest { resultMap.put(mTestData.keyAt(1), mTestData.valueAt(1)); // [4] If the passed-in key is found, return the next key. - TetherIngressKey nextKey = bpfMap.getFirstKey(); + TetherDownstream6Key nextKey = bpfMap.getFirstKey(); while (nextKey != null) { if (resultMap.remove(nextKey).equals(nextKey)) { fail("Unexpected result: " + nextKey); @@ -211,23 +214,23 @@ public final class BpfMapTest { @Test public void testUpdateBpfMap() throws Exception { - try (BpfMap bpfMap = getTestMap()) { + try (BpfMap bpfMap = getTestMap()) { - final TetherIngressKey key = mTestData.keyAt(0); - final TetherIngressValue value = mTestData.valueAt(0); - final TetherIngressValue value2 = mTestData.valueAt(1); + final TetherDownstream6Key key = mTestData.keyAt(0); + final TetherDownstream6Value value = mTestData.valueAt(0); + final TetherDownstream6Value value2 = mTestData.valueAt(1); assertFalse(bpfMap.deleteEntry(key)); // updateEntry will create an entry if it does not exist already. bpfMap.updateEntry(key, value); assertTrue(bpfMap.containsKey(key)); - final TetherIngressValue result = bpfMap.getValue(key); + final TetherDownstream6Value result = bpfMap.getValue(key); assertEquals(value, result); // updateEntry will update an entry that already exists. bpfMap.updateEntry(key, value2); assertTrue(bpfMap.containsKey(key)); - final TetherIngressValue result2 = bpfMap.getValue(key); + final TetherDownstream6Value result2 = bpfMap.getValue(key); assertEquals(value2, result2); assertTrue(bpfMap.deleteEntry(key)); @@ -237,11 +240,11 @@ public final class BpfMapTest { @Test public void testInsertReplaceEntry() throws Exception { - try (BpfMap bpfMap = getTestMap()) { + try (BpfMap bpfMap = getTestMap()) { - final TetherIngressKey key = mTestData.keyAt(0); - final TetherIngressValue value = mTestData.valueAt(0); - final TetherIngressValue value2 = mTestData.valueAt(1); + final TetherDownstream6Key key = mTestData.keyAt(0); + final TetherDownstream6Value value = mTestData.valueAt(0); + final TetherDownstream6Value value2 = mTestData.valueAt(1); try { bpfMap.replaceEntry(key, value); @@ -251,7 +254,7 @@ public final class BpfMapTest { bpfMap.insertEntry(key, value); assertTrue(bpfMap.containsKey(key)); - final TetherIngressValue result = bpfMap.getValue(key); + final TetherDownstream6Value result = bpfMap.getValue(key); assertEquals(value, result); try { bpfMap.insertEntry(key, value); @@ -260,15 +263,15 @@ public final class BpfMapTest { bpfMap.replaceEntry(key, value2); assertTrue(bpfMap.containsKey(key)); - final TetherIngressValue result2 = bpfMap.getValue(key); + final TetherDownstream6Value result2 = bpfMap.getValue(key); assertEquals(value2, result2); } } @Test public void testIterateBpfMap() throws Exception { - try (BpfMap bpfMap = getTestMap()) { - final ArrayMap resultMap = + try (BpfMap bpfMap = getTestMap()) { + final ArrayMap resultMap = new ArrayMap<>(mTestData); for (int i = 0; i < resultMap.size(); i++) { @@ -286,7 +289,7 @@ public final class BpfMapTest { @Test public void testIterateEmptyMap() throws Exception { - try (BpfMap bpfMap = getTestMap()) { + try (BpfMap bpfMap = getTestMap()) { // Can't use an int because variables used in a lambda must be final. final AtomicInteger count = new AtomicInteger(); bpfMap.forEach((key, value) -> count.incrementAndGet()); @@ -297,8 +300,8 @@ public final class BpfMapTest { @Test public void testIterateDeletion() throws Exception { - try (BpfMap bpfMap = getTestMap()) { - final ArrayMap resultMap = + try (BpfMap bpfMap = getTestMap()) { + final ArrayMap resultMap = new ArrayMap<>(mTestData); for (int i = 0; i < resultMap.size(); i++) { @@ -326,13 +329,15 @@ public final class BpfMapTest { @Test public void testInsertOverflow() throws Exception { - try (BpfMap bpfMap = getTestMap()) { - final ArrayMap testData = new ArrayMap<>(); + try (BpfMap bpfMap = getTestMap()) { + final ArrayMap testData = + new ArrayMap<>(); // Build test data for TEST_MAP_SIZE + 1 entries. for (int i = 1; i <= TEST_MAP_SIZE + 1; i++) { - testData.put(createTetherIngressKey(i, "2001:db8::1"), createTetherIngressValue( - 100, "de:ad:be:ef:00:01", "de:ad:be:ef:00:02", ETH_P_IPV6, 1500)); + testData.put(createTetherDownstream6Key(i, "2001:db8::1"), + createTetherDownstream6Value(100, "de:ad:be:ef:00:01", "de:ad:be:ef:00:02", + ETH_P_IPV6, 1500)); } // Insert #TEST_MAP_SIZE test entries to the map. The map has reached the limit. diff --git a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java index 4763558236..048b756830 100644 --- a/Tethering/tests/unit/src/android/net/ip/IpServerTest.java +++ b/Tethering/tests/unit/src/android/net/ip/IpServerTest.java @@ -104,8 +104,8 @@ import com.android.networkstack.tethering.BpfCoordinator; import com.android.networkstack.tethering.BpfCoordinator.Ipv6ForwardingRule; import com.android.networkstack.tethering.BpfMap; import com.android.networkstack.tethering.PrivateAddressCoordinator; -import com.android.networkstack.tethering.TetherIngressKey; -import com.android.networkstack.tethering.TetherIngressValue; +import com.android.networkstack.tethering.TetherDownstream6Key; +import com.android.networkstack.tethering.TetherDownstream6Value; import com.android.networkstack.tethering.TetherLimitKey; import com.android.networkstack.tethering.TetherLimitValue; import com.android.networkstack.tethering.TetherStatsKey; @@ -172,7 +172,7 @@ public class IpServerTest { @Mock private PrivateAddressCoordinator mAddressCoordinator; @Mock private NetworkStatsManager mStatsManager; @Mock private TetheringConfiguration mTetherConfig; - @Mock private BpfMap mBpfIngressMap; + @Mock private BpfMap mBpfDownstream6Map; @Mock private BpfMap mBpfStatsMap; @Mock private BpfMap mBpfLimitMap; @@ -296,8 +296,8 @@ public class IpServerTest { } @Nullable - public BpfMap getBpfIngressMap() { - return mBpfIngressMap; + public BpfMap getBpfIngressMap() { + return mBpfDownstream6Map; } @Nullable @@ -770,15 +770,15 @@ public class IpServerTest { } @NonNull - private static TetherIngressKey makeIngressKey(int upstreamIfindex, + private static TetherDownstream6Key makeIngressKey(int upstreamIfindex, @NonNull final InetAddress dst) { - return new TetherIngressKey(upstreamIfindex, dst.getAddress()); + return new TetherDownstream6Key(upstreamIfindex, dst.getAddress()); } @NonNull - private static TetherIngressValue makeIngressValue(@NonNull final MacAddress dstMac) { - return new TetherIngressValue(TEST_IFACE_PARAMS.index, dstMac, TEST_IFACE_PARAMS.macAddr, - ETH_P_IPV6, NetworkStackConstants.ETHER_MTU); + private static TetherDownstream6Value makeIngressValue(@NonNull final MacAddress dstMac) { + return new TetherDownstream6Value(TEST_IFACE_PARAMS.index, dstMac, + TEST_IFACE_PARAMS.macAddr, ETH_P_IPV6, NetworkStackConstants.ETHER_MTU); } private T verifyWithOrder(@Nullable InOrder inOrder, @NonNull T t) { @@ -792,7 +792,7 @@ public class IpServerTest { private void verifyTetherOffloadRuleAdd(@Nullable InOrder inOrder, int upstreamIfindex, @NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception { if (mBpfDeps.isAtLeastS()) { - verifyWithOrder(inOrder, mBpfIngressMap).updateEntry( + verifyWithOrder(inOrder, mBpfDownstream6Map).updateEntry( makeIngressKey(upstreamIfindex, dst), makeIngressValue(dstMac)); } else { verifyWithOrder(inOrder, mNetd).tetherOffloadRuleAdd(matches(upstreamIfindex, dst, @@ -803,7 +803,7 @@ public class IpServerTest { private void verifyNeverTetherOffloadRuleAdd(int upstreamIfindex, @NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception { if (mBpfDeps.isAtLeastS()) { - verify(mBpfIngressMap, never()).updateEntry(makeIngressKey(upstreamIfindex, dst), + verify(mBpfDownstream6Map, never()).updateEntry(makeIngressKey(upstreamIfindex, dst), makeIngressValue(dstMac)); } else { verify(mNetd, never()).tetherOffloadRuleAdd(matches(upstreamIfindex, dst, dstMac)); @@ -812,7 +812,7 @@ public class IpServerTest { private void verifyNeverTetherOffloadRuleAdd() throws Exception { if (mBpfDeps.isAtLeastS()) { - verify(mBpfIngressMap, never()).updateEntry(any(), any()); + verify(mBpfDownstream6Map, never()).updateEntry(any(), any()); } else { verify(mNetd, never()).tetherOffloadRuleAdd(any()); } @@ -821,7 +821,7 @@ public class IpServerTest { private void verifyTetherOffloadRuleRemove(@Nullable InOrder inOrder, int upstreamIfindex, @NonNull final InetAddress dst, @NonNull final MacAddress dstMac) throws Exception { if (mBpfDeps.isAtLeastS()) { - verifyWithOrder(inOrder, mBpfIngressMap).deleteEntry(makeIngressKey(upstreamIfindex, + verifyWithOrder(inOrder, mBpfDownstream6Map).deleteEntry(makeIngressKey(upstreamIfindex, dst)); } else { // |dstMac| is not required for deleting rules. Used bacause tetherOffloadRuleRemove @@ -834,7 +834,7 @@ public class IpServerTest { private void verifyNeverTetherOffloadRuleRemove() throws Exception { if (mBpfDeps.isAtLeastS()) { - verify(mBpfIngressMap, never()).deleteEntry(any()); + verify(mBpfDownstream6Map, never()).deleteEntry(any()); } else { verify(mNetd, never()).tetherOffloadRuleRemove(any()); } @@ -848,7 +848,7 @@ public class IpServerTest { } private void resetNetdBpfMapAndCoordinator() throws Exception { - reset(mNetd, mBpfIngressMap, mBpfCoordinator); + reset(mNetd, mBpfDownstream6Map, mBpfCoordinator); when(mNetd.tetherOffloadGetStats()).thenReturn(new TetherStatsParcel[0]); when(mNetd.tetherOffloadGetAndClearStats(UPSTREAM_IFINDEX)) .thenReturn(buildEmptyTetherStatsParcel(UPSTREAM_IFINDEX)); @@ -874,14 +874,14 @@ public class IpServerTest { final MacAddress macB = MacAddress.fromString("11:22:33:00:00:0b"); resetNetdBpfMapAndCoordinator(); - verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfIngressMap); + verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfDownstream6Map); // TODO: Perhaps verify the interaction of tetherOffloadSetInterfaceQuota and // tetherOffloadGetAndClearStats in netd while the rules are changed. // Events on other interfaces are ignored. recvNewNeigh(notMyIfindex, neighA, NUD_REACHABLE, macA); - verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfIngressMap); + verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfDownstream6Map); // Events on this interface are received and sent to netd. recvNewNeigh(myIfindex, neighA, NUD_REACHABLE, macA); @@ -898,9 +898,9 @@ public class IpServerTest { // Link-local and multicast neighbors are ignored. recvNewNeigh(myIfindex, neighLL, NUD_REACHABLE, macA); - verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfIngressMap); + verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfDownstream6Map); recvNewNeigh(myIfindex, neighMC, NUD_REACHABLE, macA); - verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfIngressMap); + verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfDownstream6Map); // A neighbor that is no longer valid causes the rule to be removed. // NUD_FAILED events do not have a MAC address. @@ -922,7 +922,7 @@ public class IpServerTest { recvNewNeigh(myIfindex, neighB, NUD_REACHABLE, macB); resetNetdBpfMapAndCoordinator(); - InOrder inOrder = inOrder(mNetd, mBpfIngressMap); + InOrder inOrder = inOrder(mNetd, mBpfDownstream6Map); LinkProperties lp = new LinkProperties(); lp.setInterfaceName(UPSTREAM_IFACE2); dispatchTetherConnectionChanged(UPSTREAM_IFACE2, lp, -1); @@ -950,7 +950,7 @@ public class IpServerTest { recvNewNeigh(myIfindex, neighA, NUD_REACHABLE, macA); // Clear function is called by #updateIpv6ForwardingRules for the IPv6 upstream is lost. verify(mBpfCoordinator).tetherOffloadRuleClear(mIpServer); - verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfIngressMap); + verifyNoMoreInteractions(mBpfCoordinator, mNetd, mBpfDownstream6Map); // Rules can be added again once upstream IPv6 connectivity is available. lp.setInterfaceName(UPSTREAM_IFACE); 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 4abaf03f25..09167a949e 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/BpfCoordinatorTest.java @@ -154,7 +154,7 @@ public class BpfCoordinatorTest { @Mock private INetd mNetd; @Mock private IpServer mIpServer; @Mock private TetheringConfiguration mTetherConfig; - @Mock private BpfMap mBpfIngressMap; + @Mock private BpfMap mBpfDownstream6Map; // Late init since methods must be called by the thread that created this object. private TestableNetworkStatsProviderCbBinder mTetherStatsProviderCb; @@ -194,8 +194,8 @@ public class BpfCoordinatorTest { } @Nullable - public BpfMap getBpfIngressMap() { - return mBpfIngressMap; + public BpfMap getBpfIngressMap() { + return mBpfDownstream6Map; } @Nullable @@ -341,8 +341,8 @@ public class BpfCoordinatorTest { private void verifyTetherOffloadRuleAdd(@Nullable InOrder inOrder, @NonNull Ipv6ForwardingRule rule) throws Exception { if (mDeps.isAtLeastS()) { - verifyWithOrder(inOrder, mBpfIngressMap).updateEntry( - rule.makeTetherIngressKey(), rule.makeTetherIngressValue()); + verifyWithOrder(inOrder, mBpfDownstream6Map).updateEntry( + rule.makeTetherDownstream6Key(), rule.makeTetherDownstream6Value()); } else { verifyWithOrder(inOrder, mNetd).tetherOffloadRuleAdd(matches(rule)); } @@ -350,7 +350,7 @@ public class BpfCoordinatorTest { private void verifyNeverTetherOffloadRuleAdd() throws Exception { if (mDeps.isAtLeastS()) { - verify(mBpfIngressMap, never()).updateEntry(any(), any()); + verify(mBpfDownstream6Map, never()).updateEntry(any(), any()); } else { verify(mNetd, never()).tetherOffloadRuleAdd(any()); } @@ -359,7 +359,8 @@ public class BpfCoordinatorTest { private void verifyTetherOffloadRuleRemove(@Nullable InOrder inOrder, @NonNull final Ipv6ForwardingRule rule) throws Exception { if (mDeps.isAtLeastS()) { - verifyWithOrder(inOrder, mBpfIngressMap).deleteEntry(rule.makeTetherIngressKey()); + verifyWithOrder(inOrder, mBpfDownstream6Map).deleteEntry( + rule.makeTetherDownstream6Key()); } else { verifyWithOrder(inOrder, mNetd).tetherOffloadRuleRemove(matches(rule)); } @@ -367,7 +368,7 @@ public class BpfCoordinatorTest { private void verifyNeverTetherOffloadRuleRemove() throws Exception { if (mDeps.isAtLeastS()) { - verify(mBpfIngressMap, never()).deleteEntry(any()); + verify(mBpfDownstream6Map, never()).deleteEntry(any()); } else { verify(mNetd, never()).tetherOffloadRuleRemove(any()); } @@ -435,7 +436,7 @@ public class BpfCoordinatorTest { // BpfCoordinator#tetherOffloadRuleAdd and BpfCoordinator#tetherOffloadGetAndClearStats. // The #verifyTetherOffloadGetAndClearStats can't distinguish who has ever called // mBpfStatsMap#getValue and get a wrong calling count which counts all. - final InOrder inOrder = inOrder(mNetd, mBpfIngressMap, mBpfLimitMap, mBpfStatsMap); + final InOrder inOrder = inOrder(mNetd, mBpfDownstream6Map, mBpfLimitMap, mBpfStatsMap); final Ipv6ForwardingRule rule = buildTestForwardingRule(mobileIfIndex, NEIGH_A, MAC_A); coordinator.tetherOffloadRuleAdd(mIpServer, rule); verifyTetherOffloadRuleAdd(inOrder, rule); @@ -651,11 +652,11 @@ public class BpfCoordinatorTest { } @Test - public void testRuleMakeTetherIngressKey() throws Exception { + public void testRuleMakeTetherDownstream6Key() throws Exception { final Integer mobileIfIndex = 100; final Ipv6ForwardingRule rule = buildTestForwardingRule(mobileIfIndex, NEIGH_A, MAC_A); - final TetherIngressKey key = rule.makeTetherIngressKey(); + final TetherDownstream6Key key = rule.makeTetherDownstream6Key(); assertEquals(key.iif, (long) mobileIfIndex); assertTrue(Arrays.equals(key.neigh6, NEIGH_A.getAddress())); // iif (4) + neigh6 (16) = 20. @@ -663,11 +664,11 @@ public class BpfCoordinatorTest { } @Test - public void testRuleMakeTetherIngressValue() throws Exception { + public void testRuleMakeTetherDownstream6Value() throws Exception { final Integer mobileIfIndex = 100; final Ipv6ForwardingRule rule = buildTestForwardingRule(mobileIfIndex, NEIGH_A, MAC_A); - final TetherIngressValue value = rule.makeTetherIngressValue(); + final TetherDownstream6Value value = rule.makeTetherDownstream6Value(); assertEquals(value.oif, DOWNSTREAM_IFINDEX); assertEquals(value.ethDstMac, MAC_A); assertEquals(value.ethSrcMac, DOWNSTREAM_MAC); @@ -691,7 +692,7 @@ public class BpfCoordinatorTest { // Set the unlimited quota as default if the service has never applied a data limit for a // given upstream. Note that the data limit only be applied on an upstream which has rules. final Ipv6ForwardingRule rule = buildTestForwardingRule(mobileIfIndex, NEIGH_A, MAC_A); - final InOrder inOrder = inOrder(mNetd, mBpfIngressMap, mBpfLimitMap, mBpfStatsMap); + final InOrder inOrder = inOrder(mNetd, mBpfDownstream6Map, mBpfLimitMap, mBpfStatsMap); coordinator.tetherOffloadRuleAdd(mIpServer, rule); verifyTetherOffloadRuleAdd(inOrder, rule); verifyTetherOffloadSetInterfaceQuota(inOrder, mobileIfIndex, QUOTA_UNLIMITED, @@ -734,7 +735,7 @@ public class BpfCoordinatorTest { // Applying a data limit to the current upstream does not take any immediate action. // The data limit could be only set on an upstream which has rules. final long limit = 12345; - final InOrder inOrder = inOrder(mNetd, mBpfIngressMap, mBpfLimitMap, mBpfStatsMap); + final InOrder inOrder = inOrder(mNetd, mBpfDownstream6Map, mBpfLimitMap, mBpfStatsMap); mTetherStatsProvider.onSetLimit(mobileIface, limit); waitForIdle(); verifyNeverTetherOffloadSetInterfaceQuota(inOrder); @@ -779,7 +780,7 @@ public class BpfCoordinatorTest { coordinator.addUpstreamNameToLookupTable(ethIfIndex, ethIface); coordinator.addUpstreamNameToLookupTable(mobileIfIndex, mobileIface); - final InOrder inOrder = inOrder(mNetd, mBpfIngressMap, mBpfLimitMap, mBpfStatsMap); + final InOrder inOrder = inOrder(mNetd, mBpfDownstream6Map, mBpfLimitMap, mBpfStatsMap); // Before the rule test, here are the additional actions while the rules are changed. // - After adding the first rule on a given upstream, the coordinator adds a data limit.