• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
130     template <typename T> void WriteData(T& networkDatasProto, NetFlowData &netFlowData);
131     std::string GetBundleNameByPid(int32_t pid);
132     int32_t GetUidByConfiguredBundleName(std::string bundleName);
133     bool ScreenNetworkStatByUid(const std::vector<OHOS::NetManagerStandard::NetStatsInfo> infos, NetFlowData &data);
134     bool RetainAllNetworkStat(const std::vector<OHOS::NetManagerStandard::NetStatsInfo> infos, NetFlowData &data);
135     bool HandleData(NetFlowData present, NetFlowData &difference);
136     void Record(NetFlowData &newData);
137     // for UT
138 #ifdef NETWORK_PLUGIN_UNITTEST
setSingleUid(int32_t singleUid)139     void setSingleUid(int32_t singleUid)
140     {
141         singleUid_ = singleUid;
142     }
143 #endif
144 
145 private:
146     NetworkConfig protoConfig_;
147     std::unique_ptr<uint8_t[]> buffer_;
148     std::unique_ptr<FILE, int (*)(FILE*)> fp_;
149     std::unordered_map<int32_t, int32_t> pidUid_;
150     std::string fileForTest_;
151     //new version
152     int32_t singlePid_ = 0;
153     int32_t singleUid_ = -1;
154     NetFlowData previous_ = {{0, 0}, 0, 0, 0, 0, std::vector<NetFlowDetail>()};
155     bool isFirst = true;
156     bool isNewVersion = false;
157     std::string bundleName_;
158 };
159 #endif