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 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* data, uint32_t dataSize); 87 int ReportOptimize(RandomWriteCtx* randomWrite); 88 int Stop(); 89 protected: 90 std::string GetRateNodePath(); 91 int32_t GetUid(int32_t pid); 92 bool ReadTxRxBytes(int32_t pid, NetworkCell &cell); 93 bool ReadSystemTxRxBytes(NetSystemData &systemData); 94 void AddNetDetails(NetworkCell& cell, NetDetails& data); 95 void AddNetSystemDetails(NetSystemData& systemData, NetSystemDetails& data); 96 // for UT setPathForTest(std::string path)97 void setPathForTest(std::string path) 98 { 99 fileForTest_ = path; 100 } getPathForTest()101 std::string getPathForTest() 102 { 103 return fileForTest_; 104 } 105 106 template <typename T> bool WriteNetWorkData(T& networkDatasProto); 107 108 private: 109 NetworkConfig protoConfig_; 110 std::unique_ptr<uint8_t[]> buffer_; 111 std::unique_ptr<FILE, int (*)(FILE*)> fp_; 112 std::unordered_map<int32_t, int32_t> pidUid_; 113 std::string fileForTest_; 114 }; 115 #endif