Add BPF helper function for system server
Add the map iterate and stats reading helper function used by system
server into libbpf. The methods are used by both NetworkStatsService and
NetworkStatsFactory JNI library.
Delete the function that scan through cookieTagMap and delete the stats
entry that belongs to a not existing tag. This function is handed over
to system server since system server can hold a lock to prevent
processes to access bpf maps when deleting the map entries.
Bug: 30950746
Test: run cts -m CtsNetTestCases -t android.net.cts.TrafficStatsTest
Change-Id: Ie08c817d709f0ea32405989816bd6a016ebc6bf1
diff --git a/server/TrafficControllerTest.cpp b/server/TrafficControllerTest.cpp
index 7c41fa9..3194b33 100644
--- a/server/TrafficControllerTest.cpp
+++ b/server/TrafficControllerTest.cpp
@@ -57,7 +57,10 @@
constexpr int TEST_COUNTERSET = 1;
constexpr int DEFAULT_COUNTERSET = 0;
-#define SKIP_IF_BPF_NOT_SUPPORTED do { if (!bpfSupported()) return; } while(0);
+#define SKIP_IF_BPF_NOT_SUPPORTED \
+ do { \
+ if (!hasBpfSupport()) return; \
+ } while (0);
class TrafficControllerTest : public ::testing::Test {
protected:
@@ -80,11 +83,11 @@
ASSERT_LE(0, mFakeUidCounterSetMap);
mFakeUidStatsMap = unique_fd(createMap(BPF_MAP_TYPE_HASH, sizeof(struct StatsKey),
- sizeof(struct Stats), TEST_MAP_SIZE, 0));
+ sizeof(struct StatsValue), TEST_MAP_SIZE, 0));
ASSERT_LE(0, mFakeUidStatsMap);
mFakeTagStatsMap = unique_fd(createMap(BPF_MAP_TYPE_HASH, sizeof(struct StatsKey),
- sizeof(struct Stats), TEST_MAP_SIZE, 0));
+ sizeof(struct StatsValue), TEST_MAP_SIZE, 0));
ASSERT_LE(0, mFakeTagStatsMap);
// Make sure trafficController use the eBPF code path.
@@ -127,7 +130,7 @@
UidTag cookieMapkey = {.uid = (uint32_t)uid, .tag = tag};
EXPECT_EQ(0, writeToMapEntry(mFakeCookieTagMap, &cookie, &cookieMapkey, BPF_ANY));
*key = {.uid = uid, .tag = tag, .counterSet = TEST_COUNTERSET, .ifaceIndex = 1};
- Stats statsMapValue = {.rxTcpPackets = 1, .rxTcpBytes = 100};
+ StatsValue statsMapValue = {.rxTcpPackets = 1, .rxTcpBytes = 100};
int counterSet = TEST_COUNTERSET;
EXPECT_EQ(0, writeToMapEntry(mFakeUidCounterSetMap, &uid, &counterSet, BPF_ANY));
EXPECT_EQ(0, writeToMapEntry(mFakeTagStatsMap, key, &statsMapValue, BPF_ANY));
@@ -248,7 +251,7 @@
int counterSetResult;
ASSERT_EQ(0, findMapEntry(mFakeUidCounterSetMap, &uid, &counterSetResult));
ASSERT_EQ(TEST_COUNTERSET, counterSetResult);
- Stats statsMapResult;
+ StatsValue statsMapResult;
ASSERT_EQ(-1, findMapEntry(mFakeTagStatsMap, &tagStatsMapKey, &statsMapResult));
ASSERT_EQ(0, findMapEntry(mFakeUidStatsMap, &tagStatsMapKey, &statsMapResult));
ASSERT_EQ((uint64_t)1, statsMapResult.rxTcpPackets);
@@ -268,7 +271,7 @@
ASSERT_EQ(-1, findMapEntry(mFakeCookieTagMap, &cookie, &cookieMapkey));
int counterSetResult;
ASSERT_EQ(-1, findMapEntry(mFakeUidCounterSetMap, &uid, &counterSetResult));
- Stats statsMapResult;
+ StatsValue statsMapResult;
ASSERT_EQ(-1, findMapEntry(mFakeTagStatsMap, &tagStatsMapKey, &statsMapResult));
ASSERT_EQ(-1, findMapEntry(mFakeUidStatsMap, &tagStatsMapKey, &statsMapResult));
StatsKey removedStatsKey= {.uid = 0, .tag = 0, .counterSet = COUNTERSETS_LIMIT,
@@ -299,7 +302,7 @@
int counterSetResult;
ASSERT_EQ(0, findMapEntry(mFakeUidCounterSetMap, &uid, &counterSetResult));
ASSERT_EQ(TEST_COUNTERSET, counterSetResult);
- Stats statsMapResult;
+ StatsValue statsMapResult;
ASSERT_EQ(-1, findMapEntry(mFakeTagStatsMap, &tagStatsMapKey1, &statsMapResult));
ASSERT_EQ(0, findMapEntry(mFakeTagStatsMap, &tagStatsMapKey2, &statsMapResult));
ASSERT_EQ((uint64_t)1, statsMapResult.rxTcpPackets);
@@ -328,7 +331,7 @@
ASSERT_EQ(0, findMapEntry(mFakeUidCounterSetMap, &uid1, &counterSetResult));
ASSERT_EQ(TEST_COUNTERSET, counterSetResult);
ASSERT_EQ(-1, findMapEntry(mFakeUidCounterSetMap, &uid2, &counterSetResult));
- Stats statsMapResult;
+ StatsValue statsMapResult;
ASSERT_EQ(-1, findMapEntry(mFakeTagStatsMap, &tagStatsMapKey2, &statsMapResult));
ASSERT_EQ(-1, findMapEntry(mFakeUidStatsMap, &tagStatsMapKey2, &statsMapResult));
ASSERT_EQ(0, findMapEntry(mFakeUidStatsMap, &tagStatsMapKey1, &statsMapResult));