Add more locks and comments for TrafficController
Add additional comments for the locking design inside TrafficController
and include the cookieTagMap and uidCounterSetMap into lock protection
to make the TrafficController safer for concurrent access.
Bug: 130334320
Test: netd_unit_test
Change-Id: I9194cdea2449dcf9b62cf00eab4bee48c09e6c84
diff --git a/server/TrafficController.cpp b/server/TrafficController.cpp
index 9d9a3b5..c4571c7 100644
--- a/server/TrafficController.cpp
+++ b/server/TrafficController.cpp
@@ -313,6 +313,7 @@
}
// Rx handler extracts nfgenmsg looks up and invokes registered dispatch function.
const auto rxHandler = [this](const nlmsghdr&, const Slice msg) {
+ std::lock_guard guard(mMutex);
inet_diag_msg diagmsg = {};
if (extract(msg, diagmsg) < sizeof(inet_diag_msg)) {
ALOGE("Unrecognized netlink message: %s", toString(msg).c_str());
@@ -412,6 +413,7 @@
}
int TrafficController::untagSocket(int sockFd) {
+ std::lock_guard guard(mMutex);
if (mBpfLevel == BpfLevel::NONE) {
if (legacy_untagSocket(sockFd)) return -errno;
return 0;
diff --git a/server/TrafficController.h b/server/TrafficController.h
index 20221df..7ec0279 100644
--- a/server/TrafficController.h
+++ b/server/TrafficController.h
@@ -138,7 +138,7 @@
* Map Key: uint64_t socket cookie
* Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
*/
- BpfMap<uint64_t, UidTag> mCookieTagMap;
+ BpfMap<uint64_t, UidTag> mCookieTagMap GUARDED_BY(mMutex);
/*
* mUidCounterSetMap: Store the counterSet of a specific uid.
@@ -146,7 +146,7 @@
* Map Value: uint32 counterSet specifies if the traffic is a background
* or foreground traffic.
*/
- BpfMap<uint32_t, uint8_t> mUidCounterSetMap;
+ BpfMap<uint32_t, uint8_t> mUidCounterSetMap GUARDED_BY(mMutex);
/*
* mAppUidStatsMap: Store the total traffic stats for a uid regardless of
@@ -222,7 +222,9 @@
// mUidPermissionMap, and mPrivilegedUser. These data members are controlled by netd but can
// be modified from different threads. TrafficController provides several APIs directly
// called by the binder RPC, and different binder threads can concurrently access these data
- // members mentioned above.
+ // members mentioned above. Some of the data members such as mUidPermissionMap and
+ // mPrivilegedUsers are also accessed from a different thread when tagging sockets or
+ // setting the counterSet through FwmarkServer
// 2. Coordinate the deletion of uid stats in mStatsMapA and mStatsMapB. The system server
// always call into netd to ask for a live stats map change before it pull and clean up the
// stats from the inactive map. The mMutex will block netd from accessing the stats map when