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 "bpf_shared.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 // For test only
59 int bpfGetUidStatsInternal(uid_t uid, Stats* stats,
60 const BpfMap<uint32_t, StatsValue>& appUidStatsMap);
61 // For test only
62 int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
63 const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
64 const BpfMap<uint32_t, IfaceValue>& ifaceNameMap);
65 // For test only
66 int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>* lines,
67 const std::vector<std::string>& limitIfaces, int limitTag,
68 int limitUid, const BpfMap<StatsKey, StatsValue>& statsMap,
69 const BpfMap<uint32_t, IfaceValue>& ifaceMap);
70 // For test only
71 int cleanStatsMapInternal(const base::unique_fd& cookieTagMap, const base::unique_fd& tagStatsMap);
72 // For test only
73 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)74 int getIfaceNameFromMap(const BpfMap<uint32_t, IfaceValue>& ifaceMap,
75 const BpfMap<Key, StatsValue>& statsMap, uint32_t ifaceIndex, char* ifname,
76 const Key& curKey, int64_t* unknownIfaceBytesTotal) {
77 auto iface = ifaceMap.readValue(ifaceIndex);
78 if (!iface.ok()) {
79 maybeLogUnknownIface(ifaceIndex, statsMap, curKey, unknownIfaceBytesTotal);
80 return -ENODEV;
81 }
82 strlcpy(ifname, iface.value().name, sizeof(IfaceValue));
83 return 0;
84 }
85
86 template <class Key>
maybeLogUnknownIface(int ifaceIndex,const BpfMap<Key,StatsValue> & statsMap,const Key & curKey,int64_t * unknownIfaceBytesTotal)87 void maybeLogUnknownIface(int ifaceIndex, const BpfMap<Key, StatsValue>& statsMap,
88 const Key& curKey, int64_t* unknownIfaceBytesTotal) {
89 // Have we already logged an error?
90 if (*unknownIfaceBytesTotal == -1) {
91 return;
92 }
93
94 // Are we undercounting enough data to be worth logging?
95 auto statsEntry = statsMap.readValue(curKey);
96 if (!statsEntry.ok()) {
97 // No data is being undercounted.
98 return;
99 }
100
101 *unknownIfaceBytesTotal += (statsEntry.value().rxBytes + statsEntry.value().txBytes);
102 if (*unknownIfaceBytesTotal >= MAX_UNKNOWN_IFACE_BYTES) {
103 ALOGE("Unknown name for ifindex %d with more than %" PRId64 " bytes of traffic", ifaceIndex,
104 *unknownIfaceBytesTotal);
105 *unknownIfaceBytesTotal = -1;
106 }
107 }
108
109 // For test only
110 int parseBpfNetworkStatsDevInternal(std::vector<stats_line>* lines,
111 const BpfMap<uint32_t, StatsValue>& statsMap,
112 const BpfMap<uint32_t, IfaceValue>& ifaceMap);
113
114 int bpfGetUidStats(uid_t uid, Stats* stats);
115 int bpfGetIfaceStats(const char* iface, Stats* stats);
116 int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines,
117 const std::vector<std::string>& limitIfaces, int limitTag,
118 int limitUid);
119
120 int parseBpfNetworkStatsDev(std::vector<stats_line>* lines);
121 void groupNetworkStats(std::vector<stats_line>* lines);
122 int cleanStatsMap();
123 } // namespace bpf
124 } // namespace android
125
126 #endif // _BPF_NETWORKSTATS_H
127