diff --git a/bpf_progs/bpf_shared.h b/bpf_progs/bpf_shared.h index 14fcdd695b..634fbf4106 100644 --- a/bpf_progs/bpf_shared.h +++ b/bpf_progs/bpf_shared.h @@ -147,9 +147,9 @@ enum StatsMapType { SELECT_MAP_B, }; -// TODO: change the configuration object from an 8-bit bitmask to an object with clearer +// TODO: change the configuration object from a bitmask to an object with clearer // semantics, like a struct. -typedef uint8_t BpfConfig; +typedef uint32_t BpfConfig; static const BpfConfig DEFAULT_CONFIG = 0; typedef struct { @@ -160,7 +160,9 @@ typedef struct { } UidOwnerValue; STRUCT_SIZE(UidOwnerValue, 2 * 4); // 8 +// Entry in the configuration map that stores which UID rules are enabled. #define UID_RULES_CONFIGURATION_KEY 1 +// Entry in the configuration map that stores which stats map is currently in use. #define CURRENT_STATS_MAP_CONFIGURATION_KEY 2 #define BPF_CLATD_PATH "/sys/fs/bpf/net_shared/" diff --git a/bpf_progs/netd.c b/bpf_progs/netd.c index 33381d7c89..b4ef7eb15a 100644 --- a/bpf_progs/netd.c +++ b/bpf_progs/netd.c @@ -62,7 +62,7 @@ DEFINE_BPF_MAP_GRW(stats_map_A, HASH, StatsKey, StatsValue, STATS_MAP_SIZE, AID_ DEFINE_BPF_MAP_GRW(stats_map_B, HASH, StatsKey, StatsValue, STATS_MAP_SIZE, AID_NET_BW_ACCT) DEFINE_BPF_MAP_GRW(iface_stats_map, HASH, uint32_t, StatsValue, IFACE_STATS_MAP_SIZE, AID_NET_BW_ACCT) -DEFINE_BPF_MAP_GRW(configuration_map, HASH, uint32_t, uint8_t, CONFIGURATION_MAP_SIZE, +DEFINE_BPF_MAP_GRW(configuration_map, HASH, uint32_t, uint32_t, CONFIGURATION_MAP_SIZE, AID_NET_BW_ACCT) DEFINE_BPF_MAP_GRW(uid_owner_map, HASH, uint32_t, UidOwnerValue, UID_OWNER_MAP_SIZE, AID_NET_BW_ACCT) @@ -234,7 +234,7 @@ static inline int bpf_owner_match(struct __sk_buff* skb, uint32_t uid, int direc } static __always_inline inline void update_stats_with_config(struct __sk_buff* skb, int direction, - StatsKey* key, uint8_t selectedMap) { + StatsKey* key, uint32_t selectedMap) { if (selectedMap == SELECT_MAP_A) { update_stats_map_A(skb, direction, key); } else if (selectedMap == SELECT_MAP_B) { @@ -286,7 +286,7 @@ static __always_inline inline int bpf_traffic_account(struct __sk_buff* skb, int if (counterSet) key.counterSet = (uint32_t)*counterSet; uint32_t mapSettingKey = CURRENT_STATS_MAP_CONFIGURATION_KEY; - uint8_t* selectedMap = bpf_configuration_map_lookup_elem(&mapSettingKey); + uint32_t* selectedMap = bpf_configuration_map_lookup_elem(&mapSettingKey); // Use asm("%0 &= 1" : "+r"(match)) before return match, // to help kernel's bpf verifier, so that it can be 100% certain diff --git a/netd/BpfHandler.h b/netd/BpfHandler.h index 2ede1c1bc7..05b9ebc81d 100644 --- a/netd/BpfHandler.h +++ b/netd/BpfHandler.h @@ -62,7 +62,7 @@ class BpfHandler { BpfMap mCookieTagMap; BpfMap mStatsMapA; BpfMap mStatsMapB; - BpfMap mConfigurationMap; + BpfMap mConfigurationMap; BpfMap mUidPermissionMap; std::mutex mMutex; diff --git a/netd/BpfHandlerTest.cpp b/netd/BpfHandlerTest.cpp index cd6b565653..12ae916495 100644 --- a/netd/BpfHandlerTest.cpp +++ b/netd/BpfHandlerTest.cpp @@ -48,7 +48,7 @@ class BpfHandlerTest : public ::testing::Test { BpfHandler mBh; BpfMap mFakeCookieTagMap; BpfMap mFakeStatsMapA; - BpfMap mFakeConfigurationMap; + BpfMap mFakeConfigurationMap; BpfMap mFakeUidPermissionMap; void SetUp() { diff --git a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp index 4d605ce30f..9ebef4d6d2 100644 --- a/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp +++ b/service-t/native/libs/libnetworkstats/BpfNetworkStats.cpp @@ -193,7 +193,7 @@ int parseBpfNetworkStatsDetail(std::vector* lines, return ret; } - BpfMapRO configurationMap(CONFIGURATION_MAP_PATH); + BpfMapRO configurationMap(CONFIGURATION_MAP_PATH); if (!configurationMap.isValid()) { int ret = -errno; ALOGE("get configuration map fd failed: %s", strerror(errno)); diff --git a/service/native/TrafficController.cpp b/service/native/TrafficController.cpp index 5581c40924..70c7c3425e 100644 --- a/service/native/TrafficController.cpp +++ b/service/native/TrafficController.cpp @@ -454,15 +454,15 @@ int TrafficController::replaceUidOwnerMap(const std::string& name, bool isAllowl int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) { std::lock_guard guard(mMutex); uint32_t key = UID_RULES_CONFIGURATION_KEY; - auto oldConfiguration = mConfigurationMap.readValue(key); - if (!oldConfiguration.ok()) { + auto oldConfigure = mConfigurationMap.readValue(key); + if (!oldConfigure.ok()) { ALOGE("Cannot read the old configuration from map: %s", - oldConfiguration.error().message().c_str()); - return -oldConfiguration.error().code(); + oldConfigure.error().message().c_str()); + return -oldConfigure.error().code(); } Status res; BpfConfig newConfiguration; - uint8_t match; + uint32_t match; switch (chain) { case DOZABLE: match = DOZABLE_MATCH; @@ -483,7 +483,7 @@ int TrafficController::toggleUidOwnerMap(ChildChain chain, bool enable) { return -EINVAL; } newConfiguration = - enable ? (oldConfiguration.value() | match) : (oldConfiguration.value() & (~match)); + enable ? (oldConfigure.value() | match) : (oldConfigure.value() & (~match)); res = mConfigurationMap.writeValue(key, newConfiguration, BPF_EXIST); if (!isOk(res)) { ALOGE("Failed to toggleUidOwnerMap(%d): %s", chain, res.msg().c_str()); @@ -495,17 +495,17 @@ Status TrafficController::swapActiveStatsMap() { std::lock_guard guard(mMutex); uint32_t key = CURRENT_STATS_MAP_CONFIGURATION_KEY; - auto oldConfiguration = mConfigurationMap.readValue(key); - if (!oldConfiguration.ok()) { + auto oldConfigure = mConfigurationMap.readValue(key); + if (!oldConfigure.ok()) { ALOGE("Cannot read the old configuration from map: %s", - oldConfiguration.error().message().c_str()); - return Status(oldConfiguration.error().code(), oldConfiguration.error().message()); + oldConfigure.error().message().c_str()); + return Status(oldConfigure.error().code(), oldConfigure.error().message()); } // Write to the configuration map to inform the kernel eBPF program to switch // from using one map to the other. Use flag BPF_EXIST here since the map should // be already populated in initMaps. - uint8_t newConfigure = (oldConfiguration.value() == SELECT_MAP_A) ? SELECT_MAP_B : SELECT_MAP_A; + uint32_t newConfigure = (oldConfigure.value() == SELECT_MAP_A) ? SELECT_MAP_B : SELECT_MAP_A; auto res = mConfigurationMap.writeValue(CURRENT_STATS_MAP_CONFIGURATION_KEY, newConfigure, BPF_EXIST); if (!res.ok()) { diff --git a/service/native/TrafficControllerTest.cpp b/service/native/TrafficControllerTest.cpp index ad53cb88bc..c44b9d6759 100644 --- a/service/native/TrafficControllerTest.cpp +++ b/service/native/TrafficControllerTest.cpp @@ -65,7 +65,7 @@ class TrafficControllerTest : public ::testing::Test { BpfMap mFakeCookieTagMap; BpfMap mFakeAppUidStatsMap; BpfMap mFakeStatsMapA; - BpfMap mFakeConfigurationMap; + BpfMap mFakeConfigurationMap; BpfMap mFakeUidOwnerMap; BpfMap mFakeUidPermissionMap; diff --git a/service/native/include/TrafficController.h b/service/native/include/TrafficController.h index 79e75ace49..d3d52e27e4 100644 --- a/service/native/include/TrafficController.h +++ b/service/native/include/TrafficController.h @@ -155,7 +155,7 @@ class TrafficController { * Userspace can do scraping and cleaning job on the other one depending on the * current configs. */ - bpf::BpfMap mConfigurationMap GUARDED_BY(mMutex); + bpf::BpfMap mConfigurationMap GUARDED_BY(mMutex); /* * mUidOwnerMap: Store uids that are used for bandwidth control uid match.