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
22 namespace android {
23 namespace bpf {
24
25 // TODO: set this to a proper value based on the map size;
26 constexpr int TAG_STATS_MAP_SOFT_LIMIT = 3;
27 constexpr int UID_ALL = -1;
28 constexpr int TAG_ALL = -1;
29 constexpr int TAG_NONE = 0;
30 constexpr int SET_ALL = -1;
31 constexpr int SET_DEFAULT = 0;
32 constexpr int SET_FOREGROUND = 1;
33
34 // The limit for stats received by a unknown interface;
35 constexpr const int64_t MAX_UNKNOWN_IFACE_BYTES = 100 * 1000;
36
37 // This is used by
38 // frameworks/base/core/jni/com_android_internal_net_NetworkStatsFactory.cpp
39 // make sure it is consistent with the JNI code before changing this.
40 struct stats_line {
41 char iface[32];
42 uint32_t uid;
43 uint32_t set;
44 uint32_t tag;
45 int64_t rxBytes;
46 int64_t rxPackets;
47 int64_t txBytes;
48 int64_t txPackets;
49
50 stats_line& operator=(const stats_line& rhs);
51 stats_line& operator+=(const stats_line& rhs);
52 };
53
54 bool operator==(const stats_line& lhs, const stats_line& rhs);
55 bool operator<(const stats_line& lhs, const stats_line& rhs);
56
57 // For test only
58 int bpfGetUidStatsInternal(uid_t uid, struct Stats* stats,
59 const BpfMap<uint32_t, StatsValue>& appUidStatsMap);
60 // For test only
61 int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
62 const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
63 const BpfMap<uint32_t, IfaceValue>& ifaceNameMap);
64 // For test only
65 int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>* lines,
66 const std::vector<std::string>& limitIfaces, int limitTag,
67 int limitUid, const BpfMap<StatsKey, StatsValue>& statsMap,
68 const BpfMap<uint32_t, IfaceValue>& ifaceMap);
69 // For test only
70 int cleanStatsMapInternal(const base::unique_fd& cookieTagMap, const base::unique_fd& tagStatsMap);
71 // For test only
72 template <class Key>
getIfaceNameFromMap(const BpfMap<uint32_t,IfaceValue> & ifaceMap,const BpfMap<Key,StatsValue> & statsMap,uint32_t ifaceIndex,char * ifname,const Key & curKey,int64_t * unknownIfaceBytesTotal)73 int getIfaceNameFromMap(const BpfMap<uint32_t, IfaceValue>& ifaceMap,
74 const BpfMap<Key, StatsValue>& statsMap, uint32_t ifaceIndex, char* ifname,
75 const Key& curKey, int64_t* unknownIfaceBytesTotal) {
76 auto iface = ifaceMap.readValue(ifaceIndex);
77 if (!isOk(iface)) {
78 maybeLogUnknownIface(ifaceIndex, statsMap, curKey, unknownIfaceBytesTotal);
79 return -ENODEV;
80 }
81 strlcpy(ifname, iface.value().name, sizeof(IfaceValue));
82 return 0;
83 }
84
85 template <class Key>
maybeLogUnknownIface(int ifaceIndex,const BpfMap<Key,StatsValue> & statsMap,const Key & curKey,int64_t * unknownIfaceBytesTotal)86 void maybeLogUnknownIface(int ifaceIndex, const BpfMap<Key, StatsValue>& statsMap,
87 const Key& curKey, int64_t* unknownIfaceBytesTotal) {
88 // Have we already logged an error?
89 if (*unknownIfaceBytesTotal == -1) {
90 return;
91 }
92
93 // Are we undercounting enough data to be worth logging?
94 auto statsEntry = statsMap.readValue(curKey);
95 if (!netdutils::isOk(statsEntry)) {
96 // No data is being undercounted.
97 return;
98 }
99
100 *unknownIfaceBytesTotal += (statsEntry.value().rxBytes + statsEntry.value().txBytes);
101 if (*unknownIfaceBytesTotal >= MAX_UNKNOWN_IFACE_BYTES) {
102 ALOGE("Unknown name for ifindex %d with more than %" PRId64 " bytes of traffic", ifaceIndex,
103 *unknownIfaceBytesTotal);
104 *unknownIfaceBytesTotal = -1;
105 }
106 }
107
108 // For test only
109 int parseBpfNetworkStatsDevInternal(std::vector<stats_line>* lines,
110 const BpfMap<uint32_t, StatsValue>& statsMap,
111 const BpfMap<uint32_t, IfaceValue>& ifaceMap);
112
113 int bpfGetUidStats(uid_t uid, struct Stats* stats);
114 int bpfGetIfaceStats(const char* iface, struct Stats* stats);
115 int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines,
116 const std::vector<std::string>& limitIfaces, int limitTag,
117 int limitUid);
118
119 int parseBpfNetworkStatsDev(std::vector<stats_line>* lines);
120 void groupNetworkStats(std::vector<stats_line>* lines);
121 int cleanStatsMap();
122 } // namespace bpf
123 } // namespace android
124
125 #endif // _BPF_NETWORKSTATS_H
126