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 
23 #include "bpf_def.h"
24 #include "bpf_mapper.h"
25 #include "net_manager_constants.h"
26 #include "net_stats_info.h"
27 
28 namespace OHOS::NetManagerStandard {
29 enum class StatsType {
30     STATS_TYPE_RX_BYTES = 0,
31     STATS_TYPE_RX_PACKETS = 1,
32     STATS_TYPE_TX_BYTES = 2,
33     STATS_TYPE_TX_PACKETS = 3,
34 };
35 
36 class NetsysBpfStats {
37 public:
38     NetsysBpfStats() = default;
39     ~NetsysBpfStats() = default;
40 
41     /**
42      * Get the Total Stats
43      *
44      * @param stats Output traffic data
45      * @param type StatsType traffic data type
46      * @return returns total stats
47      */
48     int32_t GetTotalStats(uint64_t &stats, StatsType type);
49 
50     /**
51      * Get the Uid Stats
52      *
53      * @param stats Output traffic data
54      * @param type StatsType traffic data type
55      * @param uid app uid
56      * @return returns uid stats
57      */
58     int32_t GetUidStats(uint64_t &stats, StatsType type, uint32_t uid);
59 
60     /**
61      * Get the Iface Stats
62      *
63      * @param stats Output traffic data
64      * @param type StatsType traffic data type
65      * @param interfaceName iface name
66      * @return returns iface stats.
67      */
68     int32_t GetIfaceStats(uint64_t &stats, StatsType type, const std::string &interfaceName);
69 
70     /**
71      * Get the sim Stats of uid
72      *
73      * @param stats Stats data.
74      * @return returns 0 for success other as failed.
75      */
76     int32_t GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats);
77 
78     /**
79      * Get the Iface with uid Stats
80      *
81      * @param stats Stats data.
82      * @return returns 0 for success other as failed.
83      */
84     int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats);
85 
86     /**
87      * Delete the Iface Stats with uid
88      *
89      * @param uid the uid of application
90      * @return returns 0 for success other as failed.
91      */
92     int32_t DeleteStatsInfo(const std::string &path, uint32_t uid);
93 
94     int32_t GetCookieStats(uint64_t &stats, StatsType statsType, uint64_t cookie);
95 
96 private:
97     static int32_t GetNumberFromStatsValue(uint64_t &stats, StatsType statsType, const stats_value &value);
98 };
99 } // namespace OHOS::NetManagerStandard
100 #endif // BPF_STATS_H
101