• 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 <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 
39 struct NetDetails {
40     uint64_t tx;
41     uint64_t rx;
42     std::string type;
43 };
44 
45 struct NetworkCell {
46     int32_t pid;
47     uint64_t tx;
48     uint64_t rx;
49     struct timespec ts;
50     std::vector<NetDetails> details;
51 };
52 
53 struct NetSystemDetails {
54     std::string type;
55     uint64_t rxBytes;
56     uint64_t rxPackets;
57     uint64_t txBytes;
58     uint64_t txPackets;
59 };
60 
61 struct NetSystemData {
62     struct timespec ts;
63     uint64_t rxBytes;
64     uint64_t rxPackets;
65     uint64_t txBytes;
66     uint64_t txPackets;
67     std::vector<NetSystemDetails> details;
68 };
69 
70 enum NetworkNum {
71     IFACE_INDEX = 2,
72     UID_INDEX = 4,
73     RX_BYTES_INDEX = 6,
74     RX_PACKETS_INDEX = 7,
75     TX_BYTES_INDEX = 8,
76     TX_PACKETS_INDEX = 9,
77     DEC_BASE = 10,
78 };
79 
80 class NetworkPlugin {
81 public:
82     NetworkPlugin();
~NetworkPlugin()83     ~NetworkPlugin() {};
84     int Start(const uint8_t* configData, uint32_t configSize);
85     int Report(uint8_t* configData, uint32_t configSize);
86     int Stop();
87 protected:
88     std::string GetRateNodePath();
89     int32_t GetUid(int32_t pid);
90     bool ReadTxRxBytes(int32_t pid, NetworkCell &cell);
91     bool ReadSystemTxRxBytes(NetSystemData &systemData);
92     void AddNetDetails(NetworkCell& cell, NetDetails& data);
93     void AddNetSystemDetails(NetSystemData& systemData, NetSystemDetails& data);
94     // for UT
setPathForTest(std::string path)95     void setPathForTest(std::string path)
96     {
97         fileForTest_ = path;
98     }
getPathForTest()99     std::string getPathForTest()
100     {
101         return fileForTest_;
102     }
103 
104 private:
105     NetworkConfig protoConfig_;
106     std::unique_ptr<uint8_t[]> buffer_;
107     std::unique_ptr<FILE, int (*)(FILE*)> fp_;
108     std::unordered_map<int32_t, int32_t> pidUid_;
109     std::string fileForTest_;
110 };
111 #endif