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