blob: 79f7d141872c5e9e1253c5e66a35e324c3cace3d [file] [log] [blame]
Chenbo Fengf2759682017-10-10 17:31:57 -07001/*
2 * Copyright (C) 2017 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 NETD_SERVER_TRAFFIC_CONTROLLER_H
18#define NETD_SERVER_TRAFFIC_CONTROLLER_H
19
20#include <linux/bpf.h>
21
Chenbo Fengc10a8a42017-12-15 13:56:33 -080022#include <netdutils/StatusOr.h>
Chenbo Feng89c12f12018-03-21 10:29:18 -070023#include "FirewallController.h"
Chenbo Feng116d0552017-12-04 17:25:19 -080024#include "NetlinkListener.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070025#include "Network.h"
Chenbo Feng89c12f12018-03-21 10:29:18 -070026#include "android-base/thread_annotations.h"
Chenbo Fengc10a8a42017-12-15 13:56:33 -080027#include "android-base/unique_fd.h"
Chenbo Feng4f6c2372018-04-26 10:37:55 -070028#include "bpf/BpfMap.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070029
Chenbo Feng4f6c2372018-04-26 10:37:55 -070030using android::bpf::BpfMap;
31using android::bpf::IfaceValue;
32using android::bpf::StatsKey;
33using android::bpf::StatsValue;
34using android::bpf::UidTag;
Chenbo Fengf2759682017-10-10 17:31:57 -070035
36namespace android {
37namespace net {
38
Chenbo Fengef297172018-03-26 10:53:33 -070039class DumpWriter;
40
Chenbo Fengf2759682017-10-10 17:31:57 -070041class TrafficController {
42 public:
Chenbo Feng89c12f12018-03-21 10:29:18 -070043 TrafficController();
Chenbo Fengf2759682017-10-10 17:31:57 -070044 /*
45 * Initialize the whole controller
46 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080047 netdutils::Status start();
Chenbo Fengf2759682017-10-10 17:31:57 -070048 /*
49 * Tag the socket with the specified tag and uid. In the qtaguid module, the
50 * first tag request that grab the spinlock of rb_tree can update the tag
51 * information first and other request need to wait until it finish. All the
52 * tag request will be addressed in the order of they obtaining the spinlock.
53 * In the eBPF implementation, the kernel will try to update the eBPF map
54 * entry with the tag request. And the hashmap update process is protected by
55 * the spinlock initialized with the map. So the behavior of two modules
56 * should be the same. No additional lock needed.
57 */
58 int tagSocket(int sockFd, uint32_t tag, uid_t uid);
59
60 /*
61 * The untag process is similiar to tag socket and both old qtaguid module and
62 * new eBPF module have spinlock inside the kernel for concurrent update. No
63 * external lock is required.
64 */
65 int untagSocket(int sockFd);
66
67 /*
68 * Similiar as above, no external lock required.
69 */
70 int setCounterSet(int counterSetNum, uid_t uid);
71
72 /*
73 * When deleting a tag data, the qtaguid module will grab the spinlock of each
74 * related rb_tree one by one and delete the tag information, counterSet
75 * information, iface stats information and uid stats information one by one.
76 * The new eBPF implementation is done similiarly by removing the entry on
77 * each map one by one. And deleting processes are also protected by the
78 * spinlock of the map. So no additional lock is required.
79 */
80 int deleteTagData(uint32_t tag, uid_t uid);
81
Chenbo Feng07d43fe2017-12-21 14:38:51 -080082 /*
83 * Check if the current device have the bpf traffic stats accounting service
84 * running.
85 */
86 bool checkBpfStatsEnable();
87
Chenbo Feng7e974052018-02-28 22:57:21 -080088 /*
89 * Add the interface name and index pair into the eBPF map.
90 */
91 int addInterface(const char* name, uint32_t ifaceIndex);
92
Chenbo Feng89c12f12018-03-21 10:29:18 -070093 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
94
95 int removeUidOwnerRule(const uid_t uid);
96
97 int replaceUidOwnerMap(const std::string& name, bool isWhitelist,
98 const std::vector<int32_t>& uids);
99
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700100 netdutils::Status updateOwnerMapEntry(BpfMap<uint32_t, uint8_t>& map, uid_t uid,
101 FirewallRule rule, FirewallType type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700102
Chenbo Fengef297172018-03-26 10:53:33 -0700103 void dump(DumpWriter& dw, bool verbose);
104
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700105 netdutils::Status replaceUidsInMap(BpfMap<uint32_t, uint8_t>& map,
106 const std::vector<int32_t>& uids, FirewallRule rule,
107 FirewallType type);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700108
Chenbo Fengef297172018-03-26 10:53:33 -0700109 static const String16 DUMP_KEYWORD;
110
Chenbo Feng89c12f12018-03-21 10:29:18 -0700111 int toggleUidOwnerMap(ChildChain chain, bool enable);
112
Chenbo Fengf2759682017-10-10 17:31:57 -0700113 private:
114 /*
115 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
Chenbo Fengef1cab32018-04-13 19:50:49 -0700116 * DO NOT hold any locks when modifying this map, otherwise when the untag
117 * operation is waiting for a lock hold by other process and there are more
118 * sockets being closed than can fit in the socket buffer of the netlink socket
119 * that receives them, then the kernel will drop some of these sockets and we
120 * won't delete their tags.
Chenbo Fengf2759682017-10-10 17:31:57 -0700121 * Map Key: uint64_t socket cookie
122 * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
123 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700124 BpfMap<uint64_t, UidTag> mCookieTagMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700125
126 /*
127 * mUidCounterSetMap: Store the counterSet of a specific uid.
128 * Map Key: uint32 uid.
129 * Map Value: uint32 counterSet specifies if the traffic is a background
130 * or foreground traffic.
131 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700132 BpfMap<uint32_t, uint8_t> mUidCounterSetMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700133
134 /*
Chenbo Fengbc4a15f2018-05-11 19:15:15 -0700135 * mAppUidStatsMap: Store the total traffic stats for a uid regardless of
136 * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats
137 * API to return persistent stats for a specific uid since device boot.
138 */
139 BpfMap<uint32_t, StatsValue> mAppUidStatsMap;
140
141 /*
Chenbo Fengf2759682017-10-10 17:31:57 -0700142 * mUidStatsMap: Store the traffic statistics for a specific combination of
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800143 * uid, iface and counterSet. We maintain this map in addition to
144 * mTagStatsMap because we want to be able to track per-UID data usage even
145 * if mTagStatsMap is full.
Chenbo Fengf2759682017-10-10 17:31:57 -0700146 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
147 * information. The Tag in the StatsKey should always be 0.
148 * Map Value: struct Stats, contains packet count and byte count of each
149 * transport protocol on egress and ingress direction.
150 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700151 BpfMap<StatsKey, StatsValue> mUidStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700152
153 /*
154 * mTagStatsMap: Store the traffic statistics for a specific combination of
155 * uid, tag, iface and counterSet. Only tagged socket stats should be stored
156 * in this map.
157 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
158 * information. The tag field should not be 0.
159 * Map Value: struct Stats, contains packet count and byte count of each
160 * transport protocol on egress and ingress direction.
161 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700162 BpfMap<StatsKey, StatsValue> mTagStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700163
Chenbo Feng7e974052018-02-28 22:57:21 -0800164 /*
165 * mIfaceIndexNameMap: Store the index name pair of each interface show up
166 * on the device since boot. The interface index is used by the eBPF program
167 * to correctly match the iface name when receiving a packet.
168 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700169 BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap;
Chenbo Feng7e974052018-02-28 22:57:21 -0800170
Chenbo Feng5ed17992018-03-13 21:30:49 -0700171 /*
172 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
173 * filter.
174 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700175 BpfMap<uint32_t, StatsValue> mIfaceStatsMap;
Chenbo Feng5ed17992018-03-13 21:30:49 -0700176
Chenbo Feng89c12f12018-03-21 10:29:18 -0700177 /*
178 * mDozableUidMap: Store uids that have related rules in dozable mode owner match
179 * chain.
180 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700181 BpfMap<uint32_t, uint8_t> mDozableUidMap GUARDED_BY(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700182
183 /*
184 * mStandbyUidMap: Store uids that have related rules in standby mode owner match
185 * chain.
186 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700187 BpfMap<uint32_t, uint8_t> mStandbyUidMap GUARDED_BY(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700188
189 /*
190 * mPowerSaveUidMap: Store uids that have related rules in power save mode owner match
191 * chain.
192 */
Chenbo Feng4f6c2372018-04-26 10:37:55 -0700193 BpfMap<uint32_t, uint8_t> mPowerSaveUidMap GUARDED_BY(mOwnerMatchMutex);
Chenbo Feng89c12f12018-03-21 10:29:18 -0700194
Chenbo Feng116d0552017-12-04 17:25:19 -0800195 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
196
Chenbo Feng33cc1032017-10-23 15:16:37 -0700197 bool ebpfSupported;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800198
Chenbo Feng89c12f12018-03-21 10:29:18 -0700199 std::mutex mOwnerMatchMutex;
200
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800201 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
202 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800203
Chenbo Feng89c12f12018-03-21 10:29:18 -0700204 netdutils::Status initMaps();
Chenbo Fenged37fea2017-12-13 19:35:01 -0800205 // For testing
206 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700207};
208
209} // namespace net
210} // namespace android
211
212#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H