Merge changes I26022db4,Ib45a130e,Ie39de2b3 into main

* changes:
  StatsValue - define operator+=
  eliminate 'struct Stats' in favour of StatsValue
  eliminate InitStats
This commit is contained in:
Treehugger Robot 2023-08-17 21:16:39 +00:00 • committed by Gerrit Code Review
commit 2238dc2ce3
5 changed files with 47 additions and 61 deletions

View file

@ -55,18 +55,21 @@ typedef struct {
} StatsValue;
STRUCT_SIZE(StatsValue, 4 * 8); // 32
#ifdef __cplusplus
static inline StatsValue& operator+=(StatsValue& lhs, const StatsValue& rhs) {
lhs.rxPackets += rhs.rxPackets;
lhs.rxBytes += rhs.rxBytes;
lhs.txPackets += rhs.txPackets;
lhs.txBytes += rhs.txBytes;
return lhs;
}
#endif
typedef struct {
char name[IFNAMSIZ];
} IfaceValue;
STRUCT_SIZE(IfaceValue, 16);
typedef struct {
uint64_t rxBytes;
uint64_t rxPackets;
uint64_t txBytes;
uint64_t txPackets;
} Stats;
typedef struct {
uint64_t timestampNs;
uint32_t ifindex;

View file

@ -51,7 +51,7 @@ enum StatsType {
TCP_TX_PACKETS = 5, // not supported, always returns UNKNOWN (-1)
};
static uint64_t getStatsType(Stats* stats, StatsType type) {
static uint64_t getStatsType(StatsValue* stats, StatsType type) {
switch (type) {
case RX_BYTES:
return stats->rxBytes;
@ -69,7 +69,7 @@ static uint64_t getStatsType(Stats* stats, StatsType type) {
}
static jlong nativeGetTotalStat(JNIEnv* env, jclass clazz, jint type) {
Stats stats = {};
StatsValue stats = {};
if (bpfGetIfaceStats(NULL, &stats) == 0) {
return getStatsType(&stats, (StatsType) type);
@ -84,7 +84,7 @@ static jlong nativeGetIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint t
return UNKNOWN;
}
Stats stats = {};
StatsValue stats = {};
if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
return getStatsType(&stats, (StatsType) type);
@ -94,7 +94,7 @@ static jlong nativeGetIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint t
}
static jlong nativeGetIfIndexStat(JNIEnv* env, jclass clazz, jint ifindex, jint type) {
Stats stats = {};
StatsValue stats = {};
if (bpfGetIfIndexStats(ifindex, &stats) == 0) {
return getStatsType(&stats, (StatsType) type);
} else {
@ -103,7 +103,7 @@ static jlong nativeGetIfIndexStat(JNIEnv* env, jclass clazz, jint ifindex, jint
}
static jlong nativeGetUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) {
Stats stats = {};
StatsValue stats = {};
if (bpfGetUidStats(uid, &stats) == 0) {
return getStatsType(&stats, (StatsType) type);

View file

@ -40,37 +40,26 @@ namespace bpf {
using base::Result;
// This explicitly zero-initializes the relevant Stats fields.
void InitStats(Stats* stats) {
stats->rxBytes = 0;
stats->rxPackets = 0;
stats->txBytes = 0;
stats->txPackets = 0;
}
int bpfGetUidStatsInternal(uid_t uid, Stats* stats,
int bpfGetUidStatsInternal(uid_t uid, StatsValue* stats,
const BpfMap<uint32_t, StatsValue>& appUidStatsMap) {
InitStats(stats);
auto statsEntry = appUidStatsMap.readValue(uid);
if (statsEntry.ok()) {
stats->rxPackets = statsEntry.value().rxPackets;
stats->txPackets = statsEntry.value().txPackets;
stats->rxBytes = statsEntry.value().rxBytes;
stats->txBytes = statsEntry.value().txBytes;
if (!statsEntry.ok()) {
*stats = {};
return (statsEntry.error().code() == ENOENT) ? 0 : -statsEntry.error().code();
}
return (statsEntry.ok() || statsEntry.error().code() == ENOENT) ? 0
: -statsEntry.error().code();
*stats = statsEntry.value();
return 0;
}
int bpfGetUidStats(uid_t uid, Stats* stats) {
int bpfGetUidStats(uid_t uid, StatsValue* stats) {
static BpfMapRO<uint32_t, StatsValue> appUidStatsMap(APP_UID_STATS_MAP_PATH);
return bpfGetUidStatsInternal(uid, stats, appUidStatsMap);
}
int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats,
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
const BpfMap<uint32_t, IfaceValue>& ifaceNameMap) {
InitStats(stats);
*stats = {};
int64_t unknownIfaceBytesTotal = 0;
const auto processIfaceStats =
[iface, stats, &ifaceNameMap, &unknownIfaceBytesTotal](
@ -86,10 +75,7 @@ int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
if (!statsEntry.ok()) {
return statsEntry.error();
}
stats->rxPackets += statsEntry.value().rxPackets;
stats->txPackets += statsEntry.value().txPackets;
stats->rxBytes += statsEntry.value().rxBytes;
stats->txBytes += statsEntry.value().txBytes;
*stats += statsEntry.value();
}
return Result<void>();
};
@ -97,27 +83,24 @@ int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
return res.ok() ? 0 : -res.error().code();
}
int bpfGetIfaceStats(const char* iface, Stats* stats) {
int bpfGetIfaceStats(const char* iface, StatsValue* stats) {
static BpfMapRO<uint32_t, StatsValue> ifaceStatsMap(IFACE_STATS_MAP_PATH);
static BpfMapRO<uint32_t, IfaceValue> ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH);
return bpfGetIfaceStatsInternal(iface, stats, ifaceStatsMap, ifaceIndexNameMap);
}
int bpfGetIfIndexStatsInternal(uint32_t ifindex, Stats* stats,
int bpfGetIfIndexStatsInternal(uint32_t ifindex, StatsValue* stats,
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap) {
InitStats(stats);
auto statsEntry = ifaceStatsMap.readValue(ifindex);
if (statsEntry.ok()) {
stats->rxPackets = statsEntry.value().rxPackets;
stats->txPackets = statsEntry.value().txPackets;
stats->rxBytes = statsEntry.value().rxBytes;
stats->txBytes = statsEntry.value().txBytes;
return 0;
if (!statsEntry.ok()) {
*stats = {};
return (statsEntry.error().code() == ENOENT) ? 0 : -statsEntry.error().code();
}
return (statsEntry.error().code() == ENOENT) ? 0 : -statsEntry.error().code();
*stats = statsEntry.value();
return 0;
}
int bpfGetIfIndexStats(int ifindex, Stats* stats) {
int bpfGetIfIndexStats(int ifindex, StatsValue* stats) {
static BpfMapRO<uint32_t, StatsValue> ifaceStatsMap(IFACE_STATS_MAP_PATH);
return bpfGetIfIndexStatsInternal(ifindex, stats, ifaceStatsMap);
}

View file

@ -116,7 +116,7 @@ class BpfNetworkStatsHelperTest : public testing::Test {
EXPECT_RESULT_OK(mFakeIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY));
}
void expectStatsEqual(const StatsValue& target, const Stats& result) {
void expectStatsEqual(const StatsValue& target, const StatsValue& result) {
EXPECT_EQ(target.rxPackets, result.rxPackets);
EXPECT_EQ(target.rxBytes, result.rxBytes);
EXPECT_EQ(target.txPackets, result.txPackets);
@ -194,7 +194,7 @@ TEST_F(BpfNetworkStatsHelperTest, TestUidStatsNoTraffic) {
.txPackets = 0,
.txBytes = 0,
};
Stats result1 = {};
StatsValue result1 = {};
ASSERT_EQ(0, bpfGetUidStatsInternal(TEST_UID1, &result1, mFakeAppUidStatsMap));
expectStatsEqual(value1, result1);
}
@ -217,11 +217,11 @@ TEST_F(BpfNetworkStatsHelperTest, TestGetUidStatsTotal) {
};
ASSERT_RESULT_OK(mFakeAppUidStatsMap.writeValue(TEST_UID1, value1, BPF_ANY));
ASSERT_RESULT_OK(mFakeAppUidStatsMap.writeValue(TEST_UID2, value2, BPF_ANY));
Stats result1 = {};
StatsValue result1 = {};
ASSERT_EQ(0, bpfGetUidStatsInternal(TEST_UID1, &result1, mFakeAppUidStatsMap));
expectStatsEqual(value1, result1);
Stats result2 = {};
StatsValue result2 = {};
ASSERT_EQ(0, bpfGetUidStatsInternal(TEST_UID2, &result2, mFakeAppUidStatsMap));
expectStatsEqual(value2, result2);
std::vector<stats_line> lines;
@ -255,15 +255,15 @@ TEST_F(BpfNetworkStatsHelperTest, TestGetIfaceStatsInternal) {
ifaceStatsKey = IFACE_INDEX3;
EXPECT_RESULT_OK(mFakeIfaceStatsMap.writeValue(ifaceStatsKey, value1, BPF_ANY));
Stats result1 = {};
StatsValue result1 = {};
ASSERT_EQ(0, bpfGetIfaceStatsInternal(IFACE_NAME1, &result1, mFakeIfaceStatsMap,
mFakeIfaceIndexNameMap));
expectStatsEqual(value1, result1);
Stats result2 = {};
StatsValue result2 = {};
ASSERT_EQ(0, bpfGetIfaceStatsInternal(IFACE_NAME2, &result2, mFakeIfaceStatsMap,
mFakeIfaceIndexNameMap));
expectStatsEqual(value2, result2);
Stats totalResult = {};
StatsValue totalResult = {};
ASSERT_EQ(0, bpfGetIfaceStatsInternal(NULL, &totalResult, mFakeIfaceStatsMap,
mFakeIfaceIndexNameMap));
StatsValue totalValue = {
@ -284,7 +284,7 @@ TEST_F(BpfNetworkStatsHelperTest, TestGetIfIndexStatsInternal) {
};
EXPECT_RESULT_OK(mFakeIfaceStatsMap.writeValue(IFACE_INDEX1, value, BPF_ANY));
Stats result = {};
StatsValue result = {};
ASSERT_EQ(0, bpfGetIfIndexStatsInternal(IFACE_INDEX1, &result, mFakeIfaceStatsMap));
expectStatsEqual(value, result);
}

View file

@ -56,14 +56,14 @@ bool operator==(const stats_line& lhs, const stats_line& rhs);
bool operator<(const stats_line& lhs, const stats_line& rhs);
// For test only
int bpfGetUidStatsInternal(uid_t uid, Stats* stats,
int bpfGetUidStatsInternal(uid_t uid, StatsValue* stats,
const BpfMap<uint32_t, StatsValue>& appUidStatsMap);
// For test only
int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats,
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
const BpfMap<uint32_t, IfaceValue>& ifaceNameMap);
// For test only
int bpfGetIfIndexStatsInternal(uint32_t ifindex, Stats* stats,
int bpfGetIfIndexStatsInternal(uint32_t ifindex, StatsValue* stats,
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap);
// For test only
int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>& lines,
@ -113,9 +113,9 @@ int parseBpfNetworkStatsDevInternal(std::vector<stats_line>& lines,
const BpfMap<uint32_t, StatsValue>& statsMap,
const BpfMap<uint32_t, IfaceValue>& ifaceMap);
int bpfGetUidStats(uid_t uid, Stats* stats);
int bpfGetIfaceStats(const char* iface, Stats* stats);
int bpfGetIfIndexStats(int ifindex, Stats* stats);
int bpfGetUidStats(uid_t uid, StatsValue* stats);
int bpfGetIfaceStats(const char* iface, StatsValue* stats);
int bpfGetIfIndexStats(int ifindex, StatsValue* stats);
int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines);
int parseBpfNetworkStatsDev(std::vector<stats_line>* lines);