1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 NETWORK_PLUGIN_H 17 #define NETWORK_PLUGIN_H 18 19 #include <algorithm> 20 #include <ctime> 21 #include <fstream> 22 #include <iomanip> 23 #include <iostream> 24 #include <sstream> 25 #include <string> 26 #include <unordered_map> 27 #include <utility> 28 #include <dirent.h> 29 #include <fcntl.h> 30 #include <inttypes.h> 31 #include <unistd.h> 32 #include <sys/stat.h> 33 #include <sys/types.h> 34 35 #include "logging.h" 36 #include "network_plugin_config.pb.h" 37 #include "network_plugin_result.pb.h" 38 #include "plugin_module_api.h" 39 #include "net_stats_client.h" 40 41 struct NetDetails { 42 uint64_t tx; 43 uint64_t rx; 44 std::string type; 45 }; 46 47 struct NetworkCell { 48 int32_t pid; 49 uint64_t tx; 50 uint64_t rx; 51 struct timespec ts; 52 std::vector<NetDetails> details; 53 }; 54 55 struct NetSystemDetails { 56 std::string type; 57 uint64_t rxBytes; 58 uint64_t rxPackets; 59 uint64_t txBytes; 60 uint64_t txPackets; 61 }; 62 63 struct NetSystemData { 64 struct timespec ts; 65 uint64_t rxBytes; 66 uint64_t rxPackets; 67 uint64_t txBytes; 68 uint64_t txPackets; 69 std::vector<NetSystemDetails> details; 70 }; 71 72 enum NetworkNum { 73 IFACE_INDEX = 2, 74 UID_INDEX = 4, 75 RX_BYTES_INDEX = 6, 76 RX_PACKETS_INDEX = 7, 77 TX_BYTES_INDEX = 8, 78 TX_PACKETS_INDEX = 9, 79 DEC_BASE = 10, 80 }; 81 82 using TimeSpec = struct timespec; 83 84 struct NetFlowDetail { 85 std::string type; 86 uint64_t rxBytes; 87 uint64_t rxPackets; 88 uint64_t txBytes; 89 uint64_t txPackets; 90 }; 91 92 struct NetFlowData { 93 TimeSpec ts; 94 uint64_t rxBytes; 95 uint64_t rxPackets; 96 uint64_t txBytes; 97 uint64_t txPackets; 98 std::vector<NetFlowDetail> details; 99 }; 100 101 class NetworkPlugin { 102 public: 103 NetworkPlugin(); ~NetworkPlugin()104 ~NetworkPlugin() {}; 105 int Start(const uint8_t* configData, uint32_t configSize); 106 int Report(uint8_t* data, uint32_t dataSize); 107 int ReportOptimize(RandomWriteCtx* randomWrite); 108 int Stop(); 109 protected: 110 std::string GetRateNodePath(); 111 int32_t GetUid(int32_t pid); 112 bool ReadTxRxBytes(int32_t pid, NetworkCell &cell); 113 bool ReadSystemTxRxBytes(NetSystemData &systemData); 114 void AddNetDetails(NetworkCell& cell, NetDetails& data); 115 void AddNetSystemDetails(NetSystemData& systemData, NetSystemDetails& data); 116 std::string GetCmdArgs(const NetworkConfig& traceConfig); 117 // for UT setPathForTest(std::string path)118 void setPathForTest(std::string path) 119 { 120 fileForTest_ = path; 121 } getPathForTest()122 std::string getPathForTest() 123 { 124 return fileForTest_; 125 } 126 127 template <typename T> bool WriteNetWorkData(T& networkDatasProto); 128 //new version 129 template <typename T> bool WriteNetFlowData(T& networkDatasProto); WriteData(T & networkDatasProto,NetFlowData & netFlowData)130 template <typename T> void WriteData(T &networkDatasProto, NetFlowData &netFlowData) 131 { 132 networkDatasProto.set_pid(singlePid_); 133 networkDatasProto.set_tv_sec(netFlowData.ts.tv_sec); 134 networkDatasProto.set_tv_nsec(netFlowData.ts.tv_nsec); 135 networkDatasProto.set_rx_bytes(netFlowData.rxBytes); 136 networkDatasProto.set_rx_packets(netFlowData.rxPackets); 137 networkDatasProto.set_tx_bytes(netFlowData.txBytes); 138 networkDatasProto.set_tx_packets(netFlowData.txPackets); 139 for (auto& it:netFlowData.details) { 140 auto* data = networkDatasProto.add_details(); 141 data->set_type(it.type); 142 data->set_rx_bytes(it.rxBytes); 143 data->set_rx_packets(it.rxPackets); 144 data->set_tx_bytes(it.txBytes); 145 data->set_tx_packets(it.txPackets); 146 } 147 } 148 std::string GetBundleNameByPid(int32_t pid); 149 int32_t GetUidByConfiguredBundleName(std::string bundleName); 150 bool ScreenNetworkStatByUid(const std::vector<OHOS::NetManagerStandard::NetStatsInfo> infos, NetFlowData &data); 151 bool RetainAllNetworkStat(const std::vector<OHOS::NetManagerStandard::NetStatsInfo> infos, NetFlowData &data); 152 bool HandleData(NetFlowData present, NetFlowData &difference); 153 void Record(NetFlowData &newData); 154 // for UT 155 #ifdef NETWORK_PLUGIN_UNITTEST setSingleUid(int32_t singleUid)156 void setSingleUid(int32_t singleUid) 157 { 158 singleUid_ = singleUid; 159 } 160 #endif 161 162 private: 163 NetworkConfig protoConfig_; 164 std::unique_ptr<uint8_t[]> buffer_; 165 std::unique_ptr<FILE, int (*)(FILE*)> fp_; 166 std::unordered_map<int32_t, int32_t> pidUid_; 167 std::string fileForTest_; 168 //new version 169 int32_t singlePid_ = 0; 170 int32_t singleUid_ = -1; 171 NetFlowData previous_ = {{0, 0}, 0, 0, 0, 0, std::vector<NetFlowDetail>()}; 172 bool isFirst = true; 173 bool isNewVersion = false; 174 std::string bundleName_; 175 }; 176 #endif