• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 NETWORK_PLUGIN_H
17 #define NETWORK_PLUGIN_H
18 
19 #include <algorithm>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <iomanip>
24 #include <string>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <unordered_map>
29 #include <utility>
30 #include <sstream>
31 #include <iostream>
32 #include <fstream>
33 
34 #include <time.h>
35 
36 #include "logging.h"
37 #include "network_plugin_config.pb.h"
38 #include "network_plugin_result.pb.h"
39 
40 struct NetDetails {
41     uint64_t tx;
42     uint64_t rx;
43     std::string type;
44 };
45 
46 struct NetworkCell {
47     int32_t pid;
48     uint64_t tx;
49     uint64_t rx;
50     struct timespec ts;
51     std::vector<NetDetails> details;
52 };
53 
54 struct NetSystemDetails {
55     std::string type;
56     uint64_t rxBytes;
57     uint64_t rxPackets;
58     uint64_t txBytes;
59     uint64_t txPackets;
60 };
61 
62 struct NetSystemData {
63     struct timespec ts;
64     uint64_t rxBytes;
65     uint64_t rxPackets;
66     uint64_t txBytes;
67     uint64_t txPackets;
68     std::vector<NetSystemDetails> details;
69 };
70 
71 enum NetworkNum {
72     IFACE_INDEX = 2,
73     UID_INDEX = 4,
74     RX_BYTES_INDEX = 6,
75     RX_PACKETS_INDEX = 7,
76     TX_BYTES_INDEX = 8,
77     TX_PACKETS_INDEX = 9,
78     DEC_BASE = 10,
79 };
80 
81 class NetworkPlugin {
82 public:
83     NetworkPlugin();
~NetworkPlugin()84     ~NetworkPlugin() {};
85     int Start(const uint8_t* configData, uint32_t configSize);
86     int Report(uint8_t* configData, uint32_t configSize);
87     int Stop();
88 protected:
89     std::string GetRateNodePath();
90     int32_t GetUid(int32_t pid);
91     bool ReadTxRxBytes(int32_t pid, NetworkCell &cell);
92     bool ReadSystemTxRxBytes(NetSystemData &systemData);
93     void AddNetDetails(NetworkCell& cell, NetDetails& data);
94     void AddNetSystemDetails(NetSystemData& systemData, NetSystemDetails& data);
95     // for UT
setPathForTest(std::string path)96     void setPathForTest(std::string path)
97     {
98         fileForTest_ = path;
99     }
getPathForTest()100     std::string getPathForTest()
101     {
102         return fileForTest_;
103     }
104 
105 private:
106     NetworkConfig protoConfig_;
107     std::unique_ptr<uint8_t[]> buffer_;
108     std::unique_ptr<FILE, int (*)(FILE*)> fp_;
109     std::unordered_map<int32_t, int32_t> pidUid_;
110     std::string fileForTest_;
111 };
112 #endif