1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef _BPF_NETWORKSTATS_H
18 #define _BPF_NETWORKSTATS_H
19
20 #include <bpf/BpfMap.h>
21 #include "netd.h"
22
23 namespace android {
24 namespace bpf {
25
26 // TODO: set this to a proper value based on the map size;
27 constexpr int TAG_STATS_MAP_SOFT_LIMIT = 3;
28 constexpr int UID_ALL = -1;
29 //constexpr int TAG_ALL = -1;
30 constexpr int TAG_NONE = 0;
31 constexpr int SET_ALL = -1;
32 constexpr int SET_DEFAULT = 0;
33 constexpr int SET_FOREGROUND = 1;
34
35 // The limit for stats received by a unknown interface;
36 constexpr const int64_t MAX_UNKNOWN_IFACE_BYTES = 100 * 1000;
37
38 // This is used by
39 // frameworks/base/core/jni/com_android_internal_net_NetworkStatsFactory.cpp
40 // make sure it is consistent with the JNI code before changing this.
41 struct stats_line {
42 char iface[32];
43 uint32_t uid;
44 uint32_t set;
45 uint32_t tag;
46 int64_t rxBytes;
47 int64_t rxPackets;
48 int64_t txBytes;
49 int64_t txPackets;
50
51 stats_line& operator=(const stats_line& rhs);
52 stats_line& operator+=(const stats_line& rhs);
53 };
54
55 bool operator==(const stats_line& lhs, const stats_line& rhs);
56 bool operator<(const stats_line& lhs, const stats_line& rhs);
57
58 // This mirrors BpfMap.h's:
59 // Result<Value> readValue(const Key key) const
60 // for a BpfMap<uint32_t, IfaceValue>
61 using IfIndexToNameFunc = std::function<Result<IfaceValue>(const uint32_t)>;
62
63 // For test only
64 int bpfGetUidStatsInternal(uid_t uid, StatsValue* stats,
65 const BpfMapRO<uint32_t, StatsValue>& appUidStatsMap);
66 // For test only
67 int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats,
68 const BpfMapRO<uint32_t, StatsValue>& ifaceStatsMap,
69 const IfIndexToNameFunc ifindex2name);
70 // For test only
71 int bpfGetIfIndexStatsInternal(uint32_t ifindex, StatsValue* stats,
72 const BpfMapRO<uint32_t, StatsValue>& ifaceStatsMap);
73 // For test only
74 int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>& lines,
75 const BpfMapRO<StatsKey, StatsValue>& statsMap,
76 const IfIndexToNameFunc ifindex2name);
77 // For test only
78 int cleanStatsMapInternal(const base::unique_fd& cookieTagMap, const base::unique_fd& tagStatsMap);
79
80 template <class Key>
maybeLogUnknownIface(int ifaceIndex,const BpfMapRO<Key,StatsValue> & statsMap,const Key & curKey,int64_t * unknownIfaceBytesTotal)81 void maybeLogUnknownIface(int ifaceIndex, const BpfMapRO<Key, StatsValue>& statsMap,
82 const Key& curKey, int64_t* unknownIfaceBytesTotal) {
83 // Have we already logged an error?
84 if (*unknownIfaceBytesTotal == -1) {
85 return;
86 }
87
88 // Are we undercounting enough data to be worth logging?
89 auto statsEntry = statsMap.readValue(curKey);
90 if (!statsEntry.ok()) {
91 // No data is being undercounted.
92 return;
93 }
94
95 *unknownIfaceBytesTotal += (statsEntry.value().rxBytes + statsEntry.value().txBytes);
96 if (*unknownIfaceBytesTotal >= MAX_UNKNOWN_IFACE_BYTES) {
97 ALOGE("Unknown name for ifindex %d with more than %" PRId64 " bytes of traffic", ifaceIndex,
98 *unknownIfaceBytesTotal);
99 *unknownIfaceBytesTotal = -1;
100 }
101 }
102
103 // For test only
104 int parseBpfNetworkStatsDevInternal(std::vector<stats_line>& lines,
105 const BpfMapRO<uint32_t, StatsValue>& statsMap,
106 const IfIndexToNameFunc ifindex2name);
107
108 void bpfRegisterIface(const char* iface);
109 int bpfGetUidStats(uid_t uid, StatsValue* stats);
110 int bpfGetIfaceStats(const char* iface, StatsValue* stats);
111 int bpfGetIfIndexStats(int ifindex, StatsValue* stats);
112 int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines);
113
114 int parseBpfNetworkStatsDev(std::vector<stats_line>* lines);
115 void groupNetworkStats(std::vector<stats_line>& lines);
116 int cleanStatsMap();
117 } // namespace bpf
118 } // namespace android
119
120 #endif // _BPF_NETWORKSTATS_H
121