Simplify the traffic stats entry struct
Since the framework API no longer support traffic stats detail such as
rxTcpPacket number or rxTcpBytes, The eBPF program and the native helper
functions no longer need to store those information as well. Removing
them from the struct StatsValue can save some space per stats entry and
reduce the total size of stats map.
Bug: 30950746
Test: run cts -m CtsNetTestCases -t android.net.cts.TrafficStatsTest
Change-Id: I70c24b762ecc9d58fc4a3ac48a7944416eff7c81
diff --git a/libbpf/BpfNetworkStats.cpp b/libbpf/BpfNetworkStats.cpp
index 5e6679f..bbe4c30 100644
--- a/libbpf/BpfNetworkStats.cpp
+++ b/libbpf/BpfNetworkStats.cpp
@@ -43,14 +43,10 @@
if (bpf::findMapEntry(map_fd, &curKey, &statsEntry) < 0) {
return -errno;
}
- stats->rxPackets +=
- statsEntry.rxTcpPackets + statsEntry.rxUdpPackets + statsEntry.rxOtherPackets;
- stats->txPackets +=
- statsEntry.txTcpPackets + statsEntry.txUdpPackets + statsEntry.txOtherPackets;
- stats->rxBytes +=
- statsEntry.rxTcpBytes + statsEntry.rxUdpBytes + statsEntry.rxOtherBytes;
- stats->txBytes +=
- statsEntry.txTcpBytes + statsEntry.txUdpBytes + statsEntry.txOtherBytes;
+ stats->rxPackets += statsEntry.rxPackets;
+ stats->txPackets += statsEntry.txPackets;
+ stats->rxBytes += statsEntry.rxBytes;
+ stats->txBytes += statsEntry.txBytes;
}
}
// Return errno if getNextMapKey return error before hit to the end of the map.
@@ -118,12 +114,10 @@
newLine.uid = statsKey.uid;
newLine.set = statsKey.counterSet;
newLine.tag = statsKey.tag;
- newLine.rxPackets =
- statsEntry.rxTcpPackets + statsEntry.rxUdpPackets + statsEntry.rxOtherPackets;
- newLine.txPackets =
- statsEntry.txTcpPackets + statsEntry.txUdpPackets + statsEntry.txOtherPackets;
- newLine.rxBytes = statsEntry.rxTcpBytes + statsEntry.rxUdpBytes + statsEntry.rxOtherBytes;
- newLine.txBytes = statsEntry.txTcpBytes + statsEntry.txUdpBytes + statsEntry.txOtherBytes;
+ newLine.rxPackets = statsEntry.rxPackets;
+ newLine.txPackets = statsEntry.txPackets;
+ newLine.rxBytes = statsEntry.rxBytes;
+ newLine.txBytes = statsEntry.txBytes;
return newLine;
}
diff --git a/libbpf/BpfNetworkStatsTest.cpp b/libbpf/BpfNetworkStatsTest.cpp
index 951bda9..eb20e0e 100644
--- a/libbpf/BpfNetworkStatsTest.cpp
+++ b/libbpf/BpfNetworkStatsTest.cpp
@@ -143,18 +143,10 @@
}
TEST_F(BpfNetworkStatsHelperTest, TestGetUidStatsTotal) {
- StatsValue value1 = {.rxTcpBytes = TEST_BYTES0,
- .rxTcpPackets = TEST_PACKET0,
- .txTcpBytes = TEST_BYTES1,
- .txTcpPackets = TEST_PACKET1,
- .rxUdpPackets = 0,
- .rxUdpBytes = 0,
- .txUdpPackets = 0,
- .txUdpBytes = 0,
- .rxOtherBytes = TEST_BYTES2,
- .rxOtherPackets = TEST_PACKET2,
- .txOtherBytes = TEST_BYTES3,
- .txOtherPackets = TEST_PACKET3};
+ StatsValue value1 = {.rxBytes = TEST_BYTES0,
+ .rxPackets = TEST_PACKET0,
+ .txBytes = TEST_BYTES1,
+ .txPackets = TEST_PACKET1,};
populateFakeStats(TEST_UID1, 0, IFACE0, TEST_COUNTERSET0, &value1, mFakeUidStatsMap);
populateFakeStats(TEST_UID1, 0, IFACE0, TEST_COUNTERSET1, &value1, mFakeUidStatsMap);
populateFakeStats(TEST_UID2, 0, IFACE0, TEST_COUNTERSET1, &value1, mFakeUidStatsMap);
@@ -220,18 +212,10 @@
const char* iface = "lo";
int ifaceIndex = if_nametoindex(iface);
ASSERT_LT(0, ifaceIndex);
- StatsValue value1 = {.rxTcpBytes = TEST_BYTES0,
- .rxTcpPackets = TEST_PACKET0,
- .txTcpBytes = TEST_BYTES1,
- .txTcpPackets = TEST_PACKET1,
- .rxUdpPackets = 0,
- .rxUdpBytes = 0,
- .txUdpPackets = 0,
- .txUdpBytes = 0,
- .rxOtherBytes = TEST_BYTES2,
- .rxOtherPackets = TEST_PACKET2,
- .txOtherBytes = TEST_BYTES3,
- .txOtherPackets = TEST_PACKET3};
+ StatsValue value1 = {.rxBytes = TEST_BYTES0,
+ .rxPackets = TEST_PACKET0,
+ .txBytes = TEST_BYTES1,
+ .txPackets = TEST_PACKET1,};
populateFakeStats(0, 0, 0, COUNTERSETS_LIMIT, &value1, mFakeTagStatsMap);
populateFakeStats(TEST_UID1, TEST_TAG, ifaceIndex, TEST_COUNTERSET0, &value1, mFakeTagStatsMap);
populateFakeStats(TEST_UID1, TEST_TAG, ifaceIndex + 1, TEST_COUNTERSET0, &value1,
@@ -259,18 +243,10 @@
const char* iface = "lo";
int ifaceIndex = if_nametoindex(iface);
ASSERT_LT(0, ifaceIndex);
- StatsValue value1 = {.rxTcpBytes = TEST_BYTES0,
- .rxTcpPackets = TEST_PACKET0,
- .txTcpBytes = TEST_BYTES1,
- .txTcpPackets = TEST_PACKET1,
- .rxUdpPackets = 0,
- .rxUdpBytes = 0,
- .txUdpPackets = 0,
- .txUdpBytes = 0,
- .rxOtherBytes = TEST_BYTES2,
- .rxOtherPackets = TEST_PACKET2,
- .txOtherBytes = TEST_BYTES3,
- .txOtherPackets = TEST_PACKET3};
+ StatsValue value1 = {.rxBytes = TEST_BYTES0,
+ .rxPackets = TEST_PACKET0,
+ .txBytes = TEST_BYTES1,
+ .txPackets = TEST_PACKET1,};
populateFakeStats(0, 0, 0, COUNTERSETS_LIMIT, &value1, mFakeUidStatsMap);
populateFakeStats(TEST_UID1, 0, ifaceIndex, TEST_COUNTERSET0, &value1, mFakeUidStatsMap);
populateFakeStats(TEST_UID1, 0, ifaceIndex + 1, TEST_COUNTERSET0, &value1, mFakeUidStatsMap);
diff --git a/libbpf/include/bpf/BpfUtils.h b/libbpf/include/bpf/BpfUtils.h
index 9556e32..743613a 100644
--- a/libbpf/include/bpf/BpfUtils.h
+++ b/libbpf/include/bpf/BpfUtils.h
@@ -77,22 +77,11 @@
uint32_t ifaceIndex;
};
-// TODO: verify if framework side still need the detail number about TCP and UDP
-// traffic. If not, remove the related tx/rx bytes and packets field to save
-// space and simplify the eBPF program.
struct StatsValue {
- uint64_t rxTcpPackets;
- uint64_t rxTcpBytes;
- uint64_t txTcpPackets;
- uint64_t txTcpBytes;
- uint64_t rxUdpPackets;
- uint64_t rxUdpBytes;
- uint64_t txUdpPackets;
- uint64_t txUdpBytes;
- uint64_t rxOtherPackets;
- uint64_t rxOtherBytes;
- uint64_t txOtherPackets;
- uint64_t txOtherBytes;
+ uint64_t rxPackets;
+ uint64_t rxBytes;
+ uint64_t txPackets;
+ uint64_t txBytes;
};
struct Stats {