1 /* 2 * Copyright (c) 2022 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 NETSYS_BPF_STATS_H 17 #define NETSYS_BPF_STATS_H 18 19 #include "bpf_wrappers.h" 20 #include "net_stats_constants.h" 21 #include "netsys_bpf_map.h" 22 23 namespace OHOS { 24 namespace NetManagerStandard { 25 enum class StatsType { 26 STATS_TYPE_RX_BYTES = 0, 27 STATS_TYPE_RX_PACKETS = 1, 28 STATS_TYPE_TX_BYTES = 2, 29 STATS_TYPE_TX_PACKETS = 3, 30 }; 31 32 class NetsysBpfStats { 33 public: 34 NetsysBpfStats(); 35 ~NetsysBpfStats(); 36 37 /** 38 * Get the Total Stats 39 * 40 * @param type StatsType traffic data type 41 * @return returns total stats 42 */ 43 int64_t GetTotalStats(StatsType type); 44 45 /** 46 * Get the Uid Stats 47 * 48 * @param type StatsType traffic data type 49 * @param uid app uid 50 * @return returns uid stats 51 */ 52 int64_t GetUidStats(StatsType type, uint32_t uid); 53 54 /** 55 * Get the Iface Stats 56 * 57 * @param type StatsType traffic data type 58 * @param interfaceName iface name 59 * @return returns iface stats. 60 */ 61 int64_t GetIfaceStats(StatsType type, const std::string &interfaceName); 62 63 /** 64 * Get the Iface Stats but for test only 65 * 66 * @param statsType traffic data type 67 * @param interfaceName iface name 68 * @param ifaceNameMap map storing relationship between ifacename and index 69 * @param ifaceStatsMap map storing index ifacename and stats 70 * @return returns iface stats 71 */ 72 int64_t BpfGetIfaceStats(const StatsType statsType, const std::string &interfaceName, 73 const NetsysBpfMap<uint32_t, IfaceName> &ifaceNameMap, 74 const NetsysBpfMap<uint32_t, StatsValue> &ifaceStatsMap); 75 /** 76 * Get the Uid Stat but for test only 77 * 78 * @param statsType traffic data type 79 * @param uid app uid 80 * @param appUidStatsMap map storing uid and stats 81 * @return returns uid stats 82 */ 83 int64_t BpfGetUidStats(StatsType statsType, uint32_t uid, const NetsysBpfMap<uint32_t, StatsValue> &appUidStatsMap); 84 85 /** 86 * For test only 87 * 88 * @param statsType traffic data type 89 * @param ifaceStatsMap map storing index ifacename and stats 90 * @return returns uid stats 91 */ 92 int64_t BpfGetTotalStats(StatsType statsType, const NetsysBpfMap<uint32_t, StatsValue> &ifaceStatsMap); 93 94 private: 95 bool IsStatsValueValid(StatsValue value); 96 bool GetIfaceName(const NetsysBpfMap<uint32_t, IfaceName> &ifaceNameMap, uint32_t ifaceIndex, 97 std::string &ifaceName); 98 int64_t CastResult(const NetStatsResultCode &code); 99 }; 100 } // namespace NetManagerStandard 101 } // namespace OHOS 102 #endif // NETSYS_BPF_STATS_H