1 /* 2 * Copyright (c) 2025-2026 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HWNETWORKSLICEMANAGER_H 17 #define HWNETWORKSLICEMANAGER_H 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 #include <set> 23 24 #include "urspconfig.h" 25 #include "networkslice_service.h" 26 #include "networksliceutil.h" 27 #include "model/fqdnips.h" 28 #include "model/networksliceinfo.h" 29 #include "model/hwosappId.h" 30 31 namespace OHOS { 32 namespace NetManagerStandard { 33 struct AddrInfo { 34 uint32_t type_; 35 std::string addr_; 36 }; 37 class HwNetworkSliceManager { 38 DECLARE_DELAYED_SINGLETON(HwNetworkSliceManager); 39 public: 40 void Init(); 41 std::vector<std::shared_ptr<NetworkSliceInfo>> mNetworkSliceInfos; 42 void InitNetworkSliceInfos(); 43 void HandleUrspChanged(std::map<std::string, std::string> data); 44 void CleanEnvironment(); 45 std::shared_ptr<NetworkSliceInfo> RequestNetworkSlice(std::shared_ptr<TrafficDescriptorsInfo> td); 46 void RequestNetworkSliceForFqdn(int uid, std::string fqdn, std::list<AddrInfo> addresses); 47 std::shared_ptr<NetworkSliceInfo> GetNetworkSliceInfoByParaRsd( 48 RouteSelectionDescriptorInfo& rsd, NetworkSliceInfo::ParaType type); 49 std::shared_ptr<NetworkSliceInfo> GetNetworkSliceInfoByParaNull(NetworkSliceInfo::ParaType type); 50 std::shared_ptr<NetworkSliceInfo> GetNetworkSliceInfoByParaNetCap(NetCap netCap); 51 bool isUpToToplimit(); 52 void ChangeNetworkSliceCounter(int changeType); 53 std::shared_ptr<NetworkSliceInfo> HandleRsdRequestAgain(std::shared_ptr<NetworkSliceInfo> requestAgain, 54 std::shared_ptr<TrafficDescriptorsInfo> requestTd, std::shared_ptr<TrafficDescriptorsInfo> tdsInUrsp); 55 void TryAddSignedUid(int uid, TrafficDescriptorsInfo tds, std::shared_ptr<NetworkSliceInfo> nsi); 56 std::shared_ptr<NetworkSliceInfo> HandleMultipleUrspFirstBind(std::shared_ptr<NetworkSliceInfo> requestAgain, 57 std::shared_ptr<TrafficDescriptorsInfo> requestTd, std::shared_ptr<TrafficDescriptorsInfo> tdsInUrsp); 58 int BindNetworkSliceProcessToNetworkForRequestAgain(int uid, std::shared_ptr<NetworkSliceInfo> nsi, 59 std::shared_ptr<FqdnIps> fqdnIps, std::shared_ptr<TrafficDescriptorsInfo> tds); 60 int BindNetworkSliceProcessToNetwork(int uid, const std::set<int>& triggerActivationUids, 61 std::shared_ptr<NetworkSliceInfo> nsi, std::shared_ptr<FqdnIps> fqdnIps, 62 std::shared_ptr<TrafficDescriptorsInfo> tds); 63 std::map<std::string, std::string> FillNetworkSliceRequest(std::shared_ptr<TrafficDescriptorsInfo> td); 64 void FillUidBindParasForRequestAgain(std::map<std::string, std::string>& bindParas, int uid, 65 std::shared_ptr<NetworkSliceInfo> nsi, std::shared_ptr<TrafficDescriptorsInfo> tds); 66 void FillUidBindParas(std::map<std::string, std::string>& bindParas, std::shared_ptr<TrafficDescriptorsInfo> tds, 67 const std::set<int>& triggerActivationUids, std::shared_ptr<NetworkSliceInfo> nsi, int uid); 68 std::set<int> GetAutoUids(const std::shared_ptr<TrafficDescriptorsInfo>& tds); 69 std::string GetUidsFromAppIds(const std::string& originAppIds); 70 std::set<std::string> GetAppIdsWithoutOsId(const std::string& originAppIds); 71 void FillIpBindParas(std::map<std::string, std::string>& bindParas, std::shared_ptr<TrafficDescriptorsInfo> tds, 72 std::shared_ptr<FqdnIps> fqdnIps, std::shared_ptr<NetworkSliceInfo> nsi); 73 std::shared_ptr<NetworkSliceInfo> HandleInvalidNetwork(std::shared_ptr<NetworkSliceInfo> requestAgain, 74 std::shared_ptr<TrafficDescriptorsInfo> tdsInUrsp, std::shared_ptr<TrafficDescriptorsInfo> requestTd); 75 bool isCanRequestNetwork(); 76 void RequestNetwork(int uid, std::shared_ptr<NetworkSliceInfo> networkSliceInfo); 77 void TryToActivateSliceForForegroundApp(); 78 void RequestNetworkSliceForPackageName(int uid, std::string& packageName); 79 void FillRsdIntoNetworkRequest(const sptr<NetSpecifier> request, const RouteSelectionDescriptorInfo& rsd, 80 const TrafficDescriptorsInfo& tds); 81 void RequestNetwork(int uid, std::shared_ptr<NetworkSliceInfo> networkSliceInfo, int requestId, int timeoutMs); 82 void RequestNetworkSliceForIp(int uid, std::string ip, std::string protocolId, std::string remotePort); 83 void FillIpBindParasForFqdn(std::map<std::string, std::string>& bindParas, const FqdnIps& newFqdnIps); 84 void FillIpBindParasForIpTriad(std::map<std::string, std::string>& bindParas, 85 std::shared_ptr<TrafficDescriptorsInfo> tds); 86 int BindProcessToNetwork(std::map<std::string, std::string> bindParas); 87 bool isCanMatchNetworkSlices(); 88 bool isEnvironmentReady(); 89 bool isUrspAvailable(); 90 void SetUrspAvailable(bool urspAvailable); 91 void HandleIpReport(std::map<std::string, std::string> data); 92 bool isNeedToRequestSliceForAppIdAuto(std::string appId); 93 bool isNeedToRequestSliceForFqdnAuto(std::string fqdn, int uid); 94 bool isNeedToRequestSliceForDnnAuto(std::string dnn, int uid); 95 bool isCooperativeApp(std::string packageName); 96 bool isCooperativeApp(int uid); 97 void ReadAppIdWhiteList(TrafficDescriptorWhiteList whiteList); 98 void ReadFqdnWhiteList(TrafficDescriptorWhiteList whiteList); 99 void ReadCctWhiteList(TrafficDescriptorWhiteList whiteList); 100 void ReadDnnWhiteList(TrafficDescriptorWhiteList whiteList); 101 void GetTrafficDescriptorWhiteList(TrafficDescriptorWhiteList whiteList); 102 int UnbindAllRoute(); 103 int UnbindSingleNetId(int netId); 104 int UnbindUids(uint netId, const std::string& uids, uint8_t urspPrecedence); 105 void UnbindAllProccessToNetwork(); 106 int UnbindProcessToNetwork(std::string uids, int netId); 107 void RestoreSliceEnvironment(); 108 void RequestMatchAllSlice(); 109 void OnNetworkAvailable(NetCap netCap, int32_t netId); 110 void OnNetworkLost(NetCap netCap, int32_t netId); 111 void OnNetworkUnavailable(NetCap netCap); 112 void RecoveryNetworkSlice(std::shared_ptr<NetworkSliceInfo> networkSliceInfo); 113 void ReleaseNetworkSlice(std::shared_ptr<NetworkSliceInfo> networkSliceInfo); 114 void CleanRouteSelectionDescriptor(std::shared_ptr<NetworkSliceInfo> networkSliceInfo); 115 void UnbindProcessToNetworkForSingleUid(int uid, std::shared_ptr<NetworkSliceInfo> nsi, bool isNeedToRemoveUid); 116 void BindAllProccessToNetwork(); 117 void ReleaseNetworkSliceByApp(int32_t uid); 118 void HandleUidRemoved(std::string packageName); 119 void HandleUidGone(int uid); 120 void onWifiNetworkStateChanged(bool isWifiConnect); 121 void DumpNetworkSliceInfos(); 122 void GetRSDByNetCap(int32_t netcap, std::map<std::string, std::string>& sliceParasbyNetcap); 123 private: 124 std::atomic<int> mNetworkSliceCounter; 125 std::atomic<bool> mIsReady = false; 126 std::atomic<bool> mHasMatchAllSlice = false; 127 std::atomic<bool> mIsMatchAllRequsted = false; 128 bool mIsUrspAvailable = false; 129 bool mIsMatchRequesting = false; 130 std::map<int, std::map<std::string, std::string>> networkSliceParas; 131 std::vector<HwOsAppId> mWhiteListForOsAppId = std::vector<HwOsAppId>(); 132 std::vector<std::string> mWhiteListForDnn = std::vector<std::string>(); 133 std::vector<std::string> mWhiteListForFqdn = std::vector<std::string>(); 134 std::vector<std::string> mWhiteListForCct = std::vector<std::string>(); 135 std::vector<std::string> mWhiteListForCooperativeApp = std::vector<std::string>(); 136 }; 137 138 } // namespace NetManagerStandard 139 } // namespace OHOS 140 141 #endif // NETWORKSLICEMANAGER_H 142