1 /* 2 * Copyright (c) 2022-2023 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 BPF_STATS_H 17 #define BPF_STATS_H 18 19 #include <vector> 20 #include <cstdint> 21 #include <string> 22 #include <mutex> 23 24 #include "bpf_def.h" 25 #include "bpf_mapper.h" 26 #include "net_manager_constants.h" 27 #include "net_stats_info.h" 28 29 namespace OHOS::NetManagerStandard { 30 enum class StatsType { 31 STATS_TYPE_RX_BYTES = 0, 32 STATS_TYPE_RX_PACKETS = 1, 33 STATS_TYPE_TX_BYTES = 2, 34 STATS_TYPE_TX_PACKETS = 3, 35 }; 36 37 class NetsysBpfStats { 38 public: 39 NetsysBpfStats() = default; 40 ~NetsysBpfStats() = default; 41 42 /** 43 * Get the Total Stats 44 * 45 * @param stats Output traffic data 46 * @param type StatsType traffic data type 47 * @return returns total stats 48 */ 49 int32_t GetTotalStats(uint64_t &stats, StatsType type); 50 51 /** 52 * Get the Uid Stats 53 * 54 * @param stats Output traffic data 55 * @param type StatsType traffic data type 56 * @param uid app uid 57 * @return returns uid stats 58 */ 59 int32_t GetUidStats(uint64_t &stats, StatsType type, uint32_t uid); 60 61 /** 62 * Get the Iface Stats 63 * 64 * @param stats Output traffic data 65 * @param type StatsType traffic data type 66 * @param interfaceName iface name 67 * @return returns iface stats. 68 */ 69 int32_t GetIfaceStats(uint64_t &stats, StatsType type, const std::string &interfaceName); 70 71 /** 72 * Get the sim Stats of uid 73 * 74 * @param stats Stats data. 75 * @return returns 0 for success other as failed. 76 */ 77 int32_t GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats); 78 79 /** 80 * Get the Iface with uid Stats 81 * 82 * @param stats Stats data. 83 * @return returns 0 for success other as failed. 84 */ 85 int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats); 86 87 /** 88 * Delete the Iface Stats with uid 89 * 90 * @param uid the uid of application 91 * @return returns 0 for success other as failed. 92 */ 93 int32_t DeleteStatsInfo(const std::string &path, uint32_t uid); 94 95 int32_t SetNetStateTrafficMap(uint8_t flag, uint64_t availableTraffic); 96 int32_t GetNetStateTrafficMap(uint8_t flag, uint64_t &availableTraffic); 97 98 int32_t GetNetStateIncreTrafficMap(std::vector<uint64_t> &keys); 99 int32_t ClearIncreaseTrafficMap(); 100 int32_t DeleteIncreaseTrafficMap(uint64_t ifIndex); 101 102 int32_t UpdateIfIndexMap(int8_t key, uint64_t index); 103 int32_t GetIfIndexMap(); 104 105 int32_t GetCookieStats(uint64_t &stats, StatsType statsType, uint64_t cookie); 106 int32_t SetNetStatusMap(uint8_t type, uint8_t value); 107 int32_t SetNetWlan1Map(uint64_t ifIndex); 108 109 private: 110 static int32_t GetNumberFromStatsValue(uint64_t &stats, StatsType statsType, const stats_value &value); 111 112 private: 113 std::mutex netStatusMapMutex_; 114 std::mutex netWlan1MapMutex_; 115 std::mutex ifaceStatsMapMutext_; 116 }; 117 } // namespace OHOS::NetManagerStandard 118 #endif // BPF_STATS_H 119