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 // 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 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 (!iface.ok()) {
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 (!statsEntry.ok()) {
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, Stats* stats);
114 int bpfGetIfaceStats(const char* iface, Stats* stats);
115 int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines);
116
117 int parseBpfNetworkStatsDev(std::vector<stats_line>* lines);
118 void groupNetworkStats(std::vector<stats_line>& lines);
119 int cleanStatsMap();
120 } // namespace bpf
121 } // namespace android
122
123 #endif // _BPF_NETWORKSTATS_H
124