From 0068ea051ca3ece6aa39d7e0760f3b54403be8a4 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 00:44:31 +0000 Subject: [PATCH 01/15] update services/netconnmanager/include/network.h. Signed-off-by: harrey --- services/netconnmanager/include/network.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/netconnmanager/include/network.h b/services/netconnmanager/include/network.h index bbac31c9d..c02d1dddc 100644 --- a/services/netconnmanager/include/network.h +++ b/services/netconnmanager/include/network.h @@ -100,6 +100,7 @@ private: int32_t netId_ = 0; uint32_t supplierId_ = 0; NetLinkInfo netLinkInfo_; + std::shared_mutex netLinkInfoMutex_; NetConnState state_ = NET_CONN_STATE_UNKNOWN; NetDetectionStatus detectResult_ = UNKNOWN_STATE; std::atomic_bool isPhyNetCreated_ = false; -- Gitee From 6131968c5cba8f79d7db53904ff16687b15bb6bf Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 01:08:27 +0000 Subject: [PATCH 02/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 87 ++++++++++++++++++------- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 8a9f73761..f5e475072 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -119,6 +119,7 @@ bool Network::CreateVirtualNetwork() NETMGR_LOG_D("Enter create virtual network"); if (!isVirtualCreated_) { // Create a virtual network here + std::shared_lock lock(netLinkInfoMutex_); bool hasDns = netLinkInfo_.dnsList_.size() ? true : false; if (NetsysController::GetInstance().NetworkCreateVirtual(netId_, hasDns) != NETMANAGER_SUCCESS) { std::string errMsg = std::string(ERROR_MSG_CREATE_VIRTUAL_NETWORK_FAILED).append(std::to_string(netId_)); @@ -132,6 +133,7 @@ bool Network::CreateVirtualNetwork() bool Network::IsIfaceNameInUse() { + std::shared_lock lock(netLinkInfoMutex_); return NetConnServiceIface().IsIfaceNameInUse(netLinkInfo_.ifaceName_, netId_); } @@ -151,13 +153,16 @@ bool Network::ReleaseBasicNetwork() std::string netCapabilities = GetNetCapabilitiesAsString(supplierId_); NETMGR_LOG_D("ReleaseBasicNetwork supplierId %{public}u, netId %{public}d, netCapabilities %{public}s", supplierId_, netId_, netCapabilities.c_str()); + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); if (!IsIfaceNameInUse() || isNeedResume_) { - for (const auto &inetAddr : netLinkInfo_.netAddrList_) { + for (const auto &inetAddr : netLinkInfoBck.netAddrList_) { int32_t prefixLen = inetAddr.prefixlen_ == 0 ? Ipv4PrefixLen(inetAddr.netMask_) : inetAddr.prefixlen_; - NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, + NetsysController::GetInstance().DelInterfaceAddress(netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen); } - for (const auto &route : netLinkInfo_.routeList_) { + for (const auto &route : netLinkInfoBck.routeList_) { if (route.destination_.address_ != LOCAL_ROUTE_NEXT_HOP && route.destination_.address_ != LOCAL_ROUTE_IPV6_DESTINATION) { auto family = GetAddrFamily(route.destination_.address_); @@ -168,26 +173,27 @@ bool Network::ReleaseBasicNetwork() } isNeedResume_ = false; } else { - for (const auto &inetAddr : netLinkInfo_.netAddrList_) { + for (const auto &inetAddr : netLinkInfoBck.netAddrList_) { int32_t prefixLen = inetAddr.prefixlen_ == 0 ? Ipv4PrefixLen(inetAddr.netMask_) : inetAddr.prefixlen_; - NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, + NetsysController::GetInstance().DelInterfaceAddress(netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen, netCapabilities); } } - for (const auto &route : netLinkInfo_.routeList_) { + for (const auto &route : netLinkInfoBck.routeList_) { auto destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_); NetsysController::GetInstance().NetworkRemoveRoute(netId_, route.iface_, destAddress, route.gateway_.address_); } - NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_); + NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfoBck.ifaceName_); NetsysController::GetInstance().NetworkDestroy(netId_); NetsysController::GetInstance().DestroyNetworkCache(netId_); + std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.Initialize(); isPhyNetCreated_ = false; return true; } -bool Network::ReleaseVirtualNetwork() +bool Network::ReleaseVirtualNetwork()//记得修改 { NETMGR_LOG_D("Enter release virtual network"); if (isVirtualCreated_) { @@ -201,6 +207,7 @@ bool Network::ReleaseVirtualNetwork() NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_); NetsysController::GetInstance().NetworkDestroy(netId_); NetsysController::GetInstance().DestroyNetworkCache(netId_); + std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.Initialize(); isVirtualCreated_ = false; } @@ -220,13 +227,16 @@ bool Network::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo) UpdateDns(netLinkInfo); UpdateMtu(netLinkInfo); UpdateTcpBufferSize(netLinkInfo); - + std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_ = netLinkInfo; + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); if (IsNat464Prefered()) { if (nat464Service_ == nullptr) { - nat464Service_ = std::make_unique(netId_, netLinkInfo_.ifaceName_); + nat464Service_ = std::make_unique(netId_, netLinkInfoBck.ifaceName_); } - nat464Service_->MaybeUpdateV6Iface(netLinkInfo_.ifaceName_); + nat464Service_->MaybeUpdateV6Iface(netLinkInfoBck.ifaceName_); nat464Service_->UpdateService(NAT464_SERVICE_CONTINUE); } else if (nat464Service_ != nullptr) { nat464Service_->UpdateService(NAT464_SERVICE_STOP); @@ -246,6 +256,7 @@ bool Network::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo) NetLinkInfo Network::GetNetLinkInfo() const { + std::shared_lock lock(netLinkInfoMutex_); NetLinkInfo linkInfo = netLinkInfo_; if (netSupplierType_ == BEARER_VPN) { return linkInfo; @@ -263,23 +274,29 @@ NetLinkInfo Network::GetNetLinkInfo() const HttpProxy Network::GetHttpProxy() const { + std::shared_lock lock(netLinkInfoMutex_); return netLinkInfo_.httpProxy_; } std::string Network::GetIfaceName() const { + std::shared_lock lock(netLinkInfoMutex_); return netLinkInfo_.ifaceName_; } std::string Network::GetIdent() const { + std::shared_lock lock(netLinkInfoMutex_); return netLinkInfo_.ident_; } void Network::UpdateInterfaces(const NetLinkInfo &newNetLinkInfo) { NETMGR_LOG_D("Network UpdateInterfaces in."); - if (newNetLinkInfo.ifaceName_ == netLinkInfo_.ifaceName_) { + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); + if (newNetLinkInfo.ifaceName_ == netLinkInfoBck.ifaceName_) { NETMGR_LOG_D("Network UpdateInterfaces out. same with before."); return; } @@ -292,12 +309,13 @@ void Network::UpdateInterfaces(const NetLinkInfo &newNetLinkInfo) SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_INTERFACE_FAILED); } } - if (!netLinkInfo_.ifaceName_.empty()) { - ret = NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_); + if (!netLinkInfoBck.ifaceName_.empty()) { + ret = NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfoBck.ifaceName_); if (ret != NETMANAGER_SUCCESS) { SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_REMOVE_NET_INTERFACE_FAILED); } } + std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.ifaceName_ = newNetLinkInfo.ifaceName_; NETMGR_LOG_D("Network UpdateInterfaces out."); } @@ -306,8 +324,11 @@ void Network::UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) { // netLinkInfo_ represents the old, netLinkInfo represents the new // Update: remove old Ips first, then add the new Ips - NETMGR_LOG_I("UpdateIpAddrs, old ip addrs size: [%{public}zu]", netLinkInfo_.netAddrList_.size()); - for (const auto &inetAddr : netLinkInfo_.netAddrList_) { + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); + NETMGR_LOG_I("UpdateIpAddrs, old ip addrs size: [%{public}zu]", netLinkInfoBck.netAddrList_.size()); + for (const auto &inetAddr : netLinkInfoBck.netAddrList_) { if (newNetLinkInfo.HasNetAddr(inetAddr)) { NETMGR_LOG_W("Same ip address:[%{public}s], there is not need to be deleted", CommonUtils::ToAnonymousIp(inetAddr.address_).c_str()); @@ -318,7 +339,7 @@ void Network::UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) : ((family == AF_INET6) ? Ipv6PrefixLen(inetAddr.netMask_) : Ipv4PrefixLen(inetAddr.netMask_)); int32_t ret = - NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, prefixLen); + NetsysController::GetInstance().DelInterfaceAddress(netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen); if (NETMANAGER_SUCCESS != ret) { SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_DELETE_NET_IP_ADDR_FAILED); } @@ -326,6 +347,7 @@ void Network::UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) if ((ret == ERRNO_EADDRNOTAVAIL) || (ret == 0)) { NETMGR_LOG_W("remove route info of ip address:[%{public}s]", CommonUtils::ToAnonymousIp(inetAddr.address_).c_str()); + std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.routeList_.remove_if([family](const Route &route) { INetAddr::IpType addrFamily = INetAddr::IpType::UNKNOWN; if (family == AF_INET) { @@ -345,6 +367,7 @@ void Network::HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) { NETMGR_LOG_I("HandleUpdateIpAddrs, new ip addrs size: [%{public}zu]", newNetLinkInfo.netAddrList_.size()); for (const auto &inetAddr : newNetLinkInfo.netAddrList_) { + std::shared_lock lock(netLinkInfoMutex_); if (netLinkInfo_.HasNetAddr(inetAddr)) { NETMGR_LOG_W("Same ip address:[%{public}s], there is no need to add it again", CommonUtils::ToAnonymousIp(inetAddr.address_).c_str()); @@ -365,8 +388,12 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info // Update: remove old routes first, then add the new routes + std::unique_lock lock(netLinkInfoMutex_); NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); - for (const auto &route : netLinkInfo_.routeList_) { + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); + for (const auto &route : netLinkInfoBck.routeList_) { if (newNetLinkInfo.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is not need to be deleted", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); @@ -389,7 +416,7 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) NETMGR_LOG_D("UpdateRoutes, new routes: [%{public}s]", newNetLinkInfo.ToStringRoute("").c_str()); for (const auto &route : newNetLinkInfo.routeList_) { - if (netLinkInfo_.HasRoute(route)) { + if (netLinkInfoBck.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is no need to add it again", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); continue; @@ -447,6 +474,7 @@ void Network::UpdateDns(const NetLinkInfo &netLinkInfo) void Network::UpdateMtu(const NetLinkInfo &netLinkInfo) { NETMGR_LOG_D("Network UpdateMtu in."); + std::shared_lock lock(netLinkInfoMutex_); if (netLinkInfo.mtu_ == netLinkInfo_.mtu_) { NETMGR_LOG_D("Network UpdateMtu out. same with before."); return; @@ -462,6 +490,7 @@ void Network::UpdateMtu(const NetLinkInfo &netLinkInfo) void Network::UpdateTcpBufferSize(const NetLinkInfo &netLinkInfo) { NETMGR_LOG_D("Network UpdateTcpBufferSize in."); + std::shared_lock lock(netLinkInfoMutex_); if (netLinkInfo.tcpBufferSizes_ == netLinkInfo_.tcpBufferSizes_) { NETMGR_LOG_D("Network UpdateTcpBufferSize out. same with before."); return; @@ -476,6 +505,7 @@ void Network::UpdateTcpBufferSize(const NetLinkInfo &netLinkInfo) void Network::UpdateStatsCached(const NetLinkInfo &netLinkInfo) { NETMGR_LOG_D("Network UpdateStatsCached in."); + std::shared_lock lock(netLinkInfoMutex_); if (netLinkInfo.ifaceName_ == netLinkInfo_.ifaceName_ && netLinkInfo.ident_ == netLinkInfo_.ident_) { NETMGR_LOG_D("Network UpdateStatsCached out. same with before"); return; @@ -598,6 +628,7 @@ void Network::InitNetMonitor() { NETMGR_LOG_D("Enter InitNetMonitor"); std::weak_ptr monitorCallback = shared_from_this(); + std::shared_lock lock(netLinkInfoMutex_); netMonitor_ = std::make_shared(netId_, netSupplierType_, netLinkInfo_, monitorCallback, isScreenOn_, isFallbackProbeWithProxy_); if (netMonitor_ == nullptr) { @@ -695,10 +726,13 @@ void Network::UpdateNetConnState(NetConnState netConnState) SendConnectionChangedBroadcast(netConnState); if (IsNat464Prefered()) { + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); if (nat464Service_ == nullptr) { - nat464Service_ = std::make_unique(netId_, netLinkInfo_.ifaceName_); + nat464Service_ = std::make_unique(netId_, netLinkInfoBck.ifaceName_); } - nat464Service_->MaybeUpdateV6Iface(netLinkInfo_.ifaceName_); + nat464Service_->MaybeUpdateV6Iface(netLinkInfoBck.ifaceName_); nat464Service_->UpdateService(NAT464_SERVICE_CONTINUE); } else if (nat464Service_ != nullptr) { nat464Service_->UpdateService(NAT464_SERVICE_STOP); @@ -719,6 +753,7 @@ void Network::SendConnectionChangedBroadcast(const NetConnState &netConnState) c void Network::SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg) { + std::unique_lock lock(netLinkInfoMutex_); struct EventInfo eventInfo = {.netlinkInfo = netLinkInfo_.ToString(" "), .supplierId = static_cast(supplierId_), .errorType = static_cast(errorType), @@ -728,6 +763,7 @@ void Network::SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const s void Network::ResetNetlinkInfo() { + std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.Initialize(); detectResult_ = UNKNOWN_STATE; } @@ -754,6 +790,7 @@ void Network::OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, con bool Network::ResumeNetworkInfo() { + std::shared_lock lock(netLinkInfoMutex_); NetLinkInfo nli = netLinkInfo_; if (netCaps_.find(NetCap::NET_CAPABILITY_INTERNET) != netCaps_.end()) { isNeedResume_ = true; @@ -789,11 +826,14 @@ bool Network::IsNat464Prefered() if (netSupplierType_ != BEARER_CELLULAR && netSupplierType_ != BEARER_WIFI && netSupplierType_ != BEARER_ETHERNET) { return false; } - if (std::any_of(netLinkInfo_.netAddrList_.begin(), netLinkInfo_.netAddrList_.end(), + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); + if (std::any_of(netLinkInfoBck.netAddrList_.begin(), netLinkInfoBck.netAddrList_.end(), [](const INetAddr &i) { return i.type_ != INetAddr::IPV6; })) { return false; } - if (netLinkInfo_.ifaceName_.empty() || !IsConnected()) { + if (netLinkInfoBck.ifaceName_.empty() || !IsConnected()) { return false; } return true; @@ -802,6 +842,7 @@ bool Network::IsNat464Prefered() void Network::CloseSocketsUid(uint32_t uid) { + std::shared_lock lock(netLinkInfoMutex_); for (const auto &inetAddr : netLinkInfo_.netAddrList_) { NetsysController::GetInstance().CloseSocketsUid(inetAddr.address_, uid); } -- Gitee From e42107547ed63d9f7c51d198204fe4bc18491be5 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 01:13:06 +0000 Subject: [PATCH 03/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 9e87118a3..a6bf8945d 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -193,20 +193,24 @@ bool Network::ReleaseBasicNetwork() return true; } -bool Network::ReleaseVirtualNetwork()//记得修改 +bool Network::ReleaseVirtualNetwork() { NETMGR_LOG_D("Enter release virtual network"); if (isVirtualCreated_) { - for (const auto &inetAddr : netLinkInfo_.netAddrList_) { + std::shared_lock lock(netLinkInfoMutex_); + NetlinkInfo netLinkInfoBck = netlinkInfo_; + lock.unlock(); + for (const auto &inetAddr : netLinkInfoBck.netAddrList_) { int32_t prefixLen = inetAddr.prefixlen_; if (prefixLen == 0) { prefixLen = Ipv4PrefixLen(inetAddr.netMask_); } - NetsysController::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_, inetAddr.address_, prefixLen); + NetsysController::GetInstance().DelInterfaceAddress(netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen); } - NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfo_.ifaceName_); + NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfoBck.ifaceName_); NetsysController::GetInstance().NetworkDestroy(netId_, true); NetsysController::GetInstance().DestroyNetworkCache(netId_, true); + std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.Initialize(); isVirtualCreated_ = false; } -- Gitee From fba214174e3a6e51d51c854ed15b2ad93c0034c2 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 01:39:23 +0000 Subject: [PATCH 04/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index a6bf8945d..f83fa050d 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -205,7 +205,8 @@ bool Network::ReleaseVirtualNetwork() if (prefixLen == 0) { prefixLen = Ipv4PrefixLen(inetAddr.netMask_); } - NetsysController::GetInstance().DelInterfaceAddress(netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen); + NetsysController::GetInstance().DelInterfaceAddress( + netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen); } NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfoBck.ifaceName_); NetsysController::GetInstance().NetworkDestroy(netId_, true); @@ -342,7 +343,8 @@ void Network::UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) : ((family == AF_INET6) ? Ipv6PrefixLen(inetAddr.netMask_) : Ipv4PrefixLen(inetAddr.netMask_)); int32_t ret = - NetsysController::GetInstance().DelInterfaceAddress(netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen); + NetsysController::GetInstance().DelInterfaceAddress( + netLinkInfoBck.ifaceName_, inetAddr.address_, prefixLen); if (NETMANAGER_SUCCESS != ret) { SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_DELETE_NET_IP_ADDR_FAILED); } @@ -391,8 +393,10 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info // Update: remove old routes first, then add the new routes - std::unique_lock lock(netLinkInfoMutex_); - NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); + { + std::unique_lock lock(netLinkInfoMutex_); + NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); + } std::shared_lock lock(netLinkInfoMutex_); NetlinkInfo netLinkInfoBck = netlinkInfo_; lock.unlock(); -- Gitee From 65b9eb4c7ef031645d0544e07868edea076467d3 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 01:59:25 +0000 Subject: [PATCH 05/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index f83fa050d..4c77aa63e 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -393,14 +393,10 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info // Update: remove old routes first, then add the new routes - { - std::unique_lock lock(netLinkInfoMutex_); - NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); - } + std::unique_lock lock(netLinkInfoMutex_); + NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; - lock.unlock(); - for (const auto &route : netLinkInfoBck.routeList_) { + for (const auto &route : netlinkInfo_.routeList_) { if (newNetLinkInfo.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is not need to be deleted", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); @@ -423,7 +419,7 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) NETMGR_LOG_D("UpdateRoutes, new routes: [%{public}s]", newNetLinkInfo.ToStringRoute("").c_str()); for (const auto &route : newNetLinkInfo.routeList_) { - if (netLinkInfoBck.HasRoute(route)) { + if (netlinkInfo_.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is no need to add it again", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); continue; -- Gitee From 8592c1d541185804a985351b888b42254176729d Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 02:24:07 +0000 Subject: [PATCH 06/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 4c77aa63e..2155403ba 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -154,7 +154,7 @@ bool Network::ReleaseBasicNetwork() NETMGR_LOG_D("ReleaseBasicNetwork supplierId %{public}u, netId %{public}d, netCapabilities %{public}s", supplierId_, netId_, netCapabilities.c_str()); std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; + NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); if (!IsIfaceNameInUse() || isNeedResume_) { for (const auto &inetAddr : netLinkInfoBck.netAddrList_) { @@ -198,7 +198,7 @@ bool Network::ReleaseVirtualNetwork() NETMGR_LOG_D("Enter release virtual network"); if (isVirtualCreated_) { std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; + NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); for (const auto &inetAddr : netLinkInfoBck.netAddrList_) { int32_t prefixLen = inetAddr.prefixlen_; @@ -234,7 +234,7 @@ bool Network::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo) std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_ = netLinkInfo; std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; + NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); if (IsNat464Prefered()) { if (nat464Service_ == nullptr) { @@ -298,7 +298,7 @@ void Network::UpdateInterfaces(const NetLinkInfo &newNetLinkInfo) { NETMGR_LOG_D("Network UpdateInterfaces in."); std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; + NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); if (newNetLinkInfo.ifaceName_ == netLinkInfoBck.ifaceName_) { NETMGR_LOG_D("Network UpdateInterfaces out. same with before."); @@ -329,7 +329,7 @@ void Network::UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) // netLinkInfo_ represents the old, netLinkInfo represents the new // Update: remove old Ips first, then add the new Ips std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; + NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); NETMGR_LOG_I("UpdateIpAddrs, old ip addrs size: [%{public}zu]", netLinkInfoBck.netAddrList_.size()); for (const auto &inetAddr : netLinkInfoBck.netAddrList_) { @@ -396,7 +396,7 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) std::unique_lock lock(netLinkInfoMutex_); NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); std::shared_lock lock(netLinkInfoMutex_); - for (const auto &route : netlinkInfo_.routeList_) { + for (const auto &route : netLinkInfo_.routeList_) { if (newNetLinkInfo.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is not need to be deleted", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); @@ -419,7 +419,7 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) NETMGR_LOG_D("UpdateRoutes, new routes: [%{public}s]", newNetLinkInfo.ToStringRoute("").c_str()); for (const auto &route : newNetLinkInfo.routeList_) { - if (netlinkInfo_.HasRoute(route)) { + if (netLinkInfo_.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is no need to add it again", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); continue; @@ -720,7 +720,7 @@ void Network::UpdateNetConnState(NetConnState netConnState) break; case NET_CONN_STATE_DISCONNECTED: state_ = netConnState; - ResetNetlinkInfo(); + ResetNetLinkInfo(); break; default: state_ = NET_CONN_STATE_UNKNOWN; @@ -730,7 +730,7 @@ void Network::UpdateNetConnState(NetConnState netConnState) SendConnectionChangedBroadcast(netConnState); if (IsNat464Prefered()) { std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; + NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); if (nat464Service_ == nullptr) { nat464Service_ = std::make_unique(netId_, netLinkInfoBck.ifaceName_); @@ -764,7 +764,7 @@ void Network::SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const s EventReport::SendSupplierFaultEvent(eventInfo); } -void Network::ResetNetlinkInfo() +void Network::ResetNetLinkInfo() { std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.Initialize(); @@ -830,7 +830,7 @@ bool Network::IsNat464Prefered() return false; } std::shared_lock lock(netLinkInfoMutex_); - NetlinkInfo netLinkInfoBck = netlinkInfo_; + NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); if (std::any_of(netLinkInfoBck.netAddrList_.begin(), netLinkInfoBck.netAddrList_.end(), [](const INetAddr &i) { return i.type_ != INetAddr::IPV6; })) { -- Gitee From 004dbfd792c81f12af249bef5934bfae94129dc5 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 02:46:41 +0000 Subject: [PATCH 07/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 2155403ba..388132eb0 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -187,7 +187,7 @@ bool Network::ReleaseBasicNetwork() NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfoBck.ifaceName_); NetsysController::GetInstance().NetworkDestroy(netId_); NetsysController::GetInstance().DestroyNetworkCache(netId_); - std::unique_lock lock(netLinkInfoMutex_); + std::unique_lock wlock(netLinkInfoMutex_); netLinkInfo_.Initialize(); isPhyNetCreated_ = false; return true; @@ -211,7 +211,7 @@ bool Network::ReleaseVirtualNetwork() NetsysController::GetInstance().NetworkRemoveInterface(netId_, netLinkInfoBck.ifaceName_); NetsysController::GetInstance().NetworkDestroy(netId_, true); NetsysController::GetInstance().DestroyNetworkCache(netId_, true); - std::unique_lock lock(netLinkInfoMutex_); + std::unique_lock wlock(netLinkInfoMutex_); netLinkInfo_.Initialize(); isVirtualCreated_ = false; } @@ -231,7 +231,7 @@ bool Network::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo) UpdateDns(netLinkInfo); UpdateMtu(netLinkInfo); UpdateTcpBufferSize(netLinkInfo); - std::unique_lock lock(netLinkInfoMutex_); + std::unique_lock wlock(netLinkInfoMutex_); netLinkInfo_ = netLinkInfo; std::shared_lock lock(netLinkInfoMutex_); NetLinkInfo netLinkInfoBck = netLinkInfo_; @@ -393,7 +393,7 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info // Update: remove old routes first, then add the new routes - std::unique_lock lock(netLinkInfoMutex_); + std::unique_lock wlock(netLinkInfoMutex_); NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); std::shared_lock lock(netLinkInfoMutex_); for (const auto &route : netLinkInfo_.routeList_) { -- Gitee From c679bf621015195d55b58cf14daa7c3fe8cd2283 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 03:18:31 +0000 Subject: [PATCH 08/15] update services/netconnmanager/include/network.h. Signed-off-by: harrey --- services/netconnmanager/include/network.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/netconnmanager/include/network.h b/services/netconnmanager/include/network.h index c02d1dddc..67850f2cc 100644 --- a/services/netconnmanager/include/network.h +++ b/services/netconnmanager/include/network.h @@ -100,7 +100,7 @@ private: int32_t netId_ = 0; uint32_t supplierId_ = 0; NetLinkInfo netLinkInfo_; - std::shared_mutex netLinkInfoMutex_; + mutable std::shared_mutex netLinkInfoMutex_; NetConnState state_ = NET_CONN_STATE_UNKNOWN; NetDetectionStatus detectResult_ = UNKNOWN_STATE; std::atomic_bool isPhyNetCreated_ = false; -- Gitee From c4a2b38baf431e699cb964a351543949cfad3800 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 03:31:51 +0000 Subject: [PATCH 09/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 388132eb0..28b8b45fa 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -233,6 +233,7 @@ bool Network::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo) UpdateTcpBufferSize(netLinkInfo); std::unique_lock wlock(netLinkInfoMutex_); netLinkInfo_ = netLinkInfo; + wlock.unlock(); std::shared_lock lock(netLinkInfoMutex_); NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); @@ -393,10 +394,11 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info // Update: remove old routes first, then add the new routes - std::unique_lock wlock(netLinkInfoMutex_); - NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfo_.ToStringRoute("").c_str()); std::shared_lock lock(netLinkInfoMutex_); - for (const auto &route : netLinkInfo_.routeList_) { + NetLinkInfo netLinkInfoBck = netLinkInfo_; + lock.unlock(); + NETMGR_LOG_D("UpdateRoutes, old routes: [%{public}s]", netLinkInfoBck.ToStringRoute("").c_str()); + for (const auto &route : netLinkInfoBck.routeList_) { if (newNetLinkInfo.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is not need to be deleted", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); @@ -419,7 +421,7 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) NETMGR_LOG_D("UpdateRoutes, new routes: [%{public}s]", newNetLinkInfo.ToStringRoute("").c_str()); for (const auto &route : newNetLinkInfo.routeList_) { - if (netLinkInfo_.HasRoute(route)) { + if (netLinkInfoBck.HasRoute(route)) { NETMGR_LOG_W("Same route:[%{public}s] ifo, there is no need to add it again", CommonUtils::ToAnonymousIp(route.destination_.address_).c_str()); continue; @@ -756,7 +758,7 @@ void Network::SendConnectionChangedBroadcast(const NetConnState &netConnState) c void Network::SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg) { - std::unique_lock lock(netLinkInfoMutex_); + std::shared_lock lock(netLinkInfoMutex_); struct EventInfo eventInfo = {.netlinkInfo = netLinkInfo_.ToString(" "), .supplierId = static_cast(supplierId_), .errorType = static_cast(errorType), -- Gitee From 9729ea4e64dfa7ee281cc58151b0c4fbf8333edb Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 04:38:47 +0000 Subject: [PATCH 10/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 28b8b45fa..c42267857 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -392,7 +392,6 @@ void Network::HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { - // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info // Update: remove old routes first, then add the new routes std::shared_lock lock(netLinkInfoMutex_); NetLinkInfo netLinkInfoBck = netLinkInfo_; -- Gitee From adc199690be81f492c8297366d7605ccbe9bfbfa Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 04:41:29 +0000 Subject: [PATCH 11/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index c42267857..9a5d75fb4 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -320,7 +320,7 @@ void Network::UpdateInterfaces(const NetLinkInfo &newNetLinkInfo) SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_REMOVE_NET_INTERFACE_FAILED); } } - std::unique_lock lock(netLinkInfoMutex_); + std::unique_lock wlock(netLinkInfoMutex_); netLinkInfo_.ifaceName_ = newNetLinkInfo.ifaceName_; NETMGR_LOG_D("Network UpdateInterfaces out."); } @@ -765,7 +765,7 @@ void Network::SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const s EventReport::SendSupplierFaultEvent(eventInfo); } -void Network::ResetNetLinkInfo() +void Network::ResetNetlinkInfo() { std::unique_lock lock(netLinkInfoMutex_); netLinkInfo_.Initialize(); -- Gitee From aeed0911718819c2b2d8d320c257e5a0658e288e Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 05:36:29 +0000 Subject: [PATCH 12/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 9a5d75fb4..1343d2c2d 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -392,7 +392,6 @@ void Network::HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { - // Update: remove old routes first, then add the new routes std::shared_lock lock(netLinkInfoMutex_); NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); @@ -721,7 +720,7 @@ void Network::UpdateNetConnState(NetConnState netConnState) break; case NET_CONN_STATE_DISCONNECTED: state_ = netConnState; - ResetNetLinkInfo(); + ResetNetlinkInfo(); break; default: state_ = NET_CONN_STATE_UNKNOWN; -- Gitee From b2254a8d714170c9cd2aa001839954504c12cec4 Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 06:16:07 +0000 Subject: [PATCH 13/15] update services/netconnmanager/src/network.cpp. Signed-off-by: harrey --- services/netconnmanager/src/network.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/netconnmanager/src/network.cpp b/services/netconnmanager/src/network.cpp index 1343d2c2d..f5561ee78 100644 --- a/services/netconnmanager/src/network.cpp +++ b/services/netconnmanager/src/network.cpp @@ -392,6 +392,8 @@ void Network::HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo) void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) { + // netLinkInfo_ contains the old routes info, netLinkInfo contains the new routes info + // Update: remove old routes first, then add the new routes std::shared_lock lock(netLinkInfoMutex_); NetLinkInfo netLinkInfoBck = netLinkInfo_; lock.unlock(); @@ -439,7 +441,6 @@ void Network::UpdateRoutes(const NetLinkInfo &newNetLinkInfo) SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_ADD_NET_ROUTES_FAILED); } } - NETMGR_LOG_D("Network UpdateRoutes out."); if (newNetLinkInfo.routeList_.empty()) { SendSupplierFaultHiSysEvent(FAULT_UPDATE_NETLINK_INFO_FAILED, ERROR_MSG_UPDATE_NET_ROUTES_FAILED); } -- Gitee From e6ede5e4c6687a63b22e0bd83ef3c9bf74bf57bd Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 07:02:17 +0000 Subject: [PATCH 14/15] update interfaces/innerkits/netconnclient/include/net_conn_client.h. Signed-off-by: harrey --- interfaces/innerkits/netconnclient/include/net_conn_client.h | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/innerkits/netconnclient/include/net_conn_client.h b/interfaces/innerkits/netconnclient/include/net_conn_client.h index 9eab8ce0a..da537ac0f 100644 --- a/interfaces/innerkits/netconnclient/include/net_conn_client.h +++ b/interfaces/innerkits/netconnclient/include/net_conn_client.h @@ -32,7 +32,6 @@ #include "net_supplier_callback_base.h" #include "i_net_factoryreset_callback.h" #include "safe_map.h" -#include "system_ability_status_change_stub.h" namespace OHOS { namespace nmd { -- Gitee From e52f15bc0827ab5e91fa9bb83f49fb35743b9bec Mon Sep 17 00:00:00 2001 From: harrey Date: Fri, 21 Feb 2025 07:38:09 +0000 Subject: [PATCH 15/15] update interfaces/innerkits/netconnclient/include/net_conn_client.h. Signed-off-by: harrey --- interfaces/innerkits/netconnclient/include/net_conn_client.h | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/innerkits/netconnclient/include/net_conn_client.h b/interfaces/innerkits/netconnclient/include/net_conn_client.h index da537ac0f..9eab8ce0a 100644 --- a/interfaces/innerkits/netconnclient/include/net_conn_client.h +++ b/interfaces/innerkits/netconnclient/include/net_conn_client.h @@ -32,6 +32,7 @@ #include "net_supplier_callback_base.h" #include "i_net_factoryreset_callback.h" #include "safe_map.h" +#include "system_ability_status_change_stub.h" namespace OHOS { namespace nmd { -- Gitee