blob: b810bf8ea59780aad2724444e8d4d2d3efa0a92f [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 Fengf2759682017-10-10 17:31:57 -070028
Chenbo Fengeac6c472018-02-05 15:06:23 -080029// Since we cannot garbage collect the stats map since device boot, we need to make these maps as
30// large as possible. The current rlimit of MEM_LOCK allows at most 10000 map entries for each
31// stats map. In the old qtaguid module, we don't have a total limit for data entries but only have
32// limitation of tags each uid can have. (default is 1024 in kernel);
33// cookie_uid_map: key: 8 bytes, value: 8 bytes, total:10000*8*2 bytes = 160Kbytes
34// uid_counter_set_map: key: 4 bytes, value: 4 bytes, total:10000*4*2 bytes = 80Kbytes
35// uid_stats_map: key: 16 bytes, value: 32 bytes, total:10000*16+10000*32 bytes = 480Kbytes
36// tag_stats_map: key: 16 bytes, value: 32 bytes, total:10000*16+10000*32 bytes = 480Kbytes
Chenbo Feng7e974052018-02-28 22:57:21 -080037// iface_index_name_map:key: 4 bytes, value: 32 bytes, total:10000*36 bytes = 360Kbytes
38// total: 1560Kbytes
Chenbo Fengeac6c472018-02-05 15:06:23 -080039constexpr const int COOKIE_UID_MAP_SIZE = 10000;
40constexpr const int UID_COUNTERSET_MAP_SIZE = 10000;
41constexpr const int UID_STATS_MAP_SIZE = 10000;
42constexpr const int TAG_STATS_MAP_SIZE = 10000;
Chenbo Feng5ed17992018-03-13 21:30:49 -070043constexpr const int IFACE_INDEX_NAME_MAP_SIZE = 1000;
44constexpr const int IFACE_STATS_MAP_SIZE = 1000;
Chenbo Fengeac6c472018-02-05 15:06:23 -080045constexpr const int UID_OWNER_MAP_SIZE = 10000;
Chenbo Fengf2759682017-10-10 17:31:57 -070046
47constexpr const int COUNTERSETS_LIMIT = 2;
48
49namespace android {
50namespace net {
51
Chenbo Fengef297172018-03-26 10:53:33 -070052class DumpWriter;
53
Chenbo Fengf2759682017-10-10 17:31:57 -070054class TrafficController {
55 public:
Chenbo Feng89c12f12018-03-21 10:29:18 -070056 TrafficController();
Chenbo Fengf2759682017-10-10 17:31:57 -070057 /*
58 * Initialize the whole controller
59 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080060 netdutils::Status start();
Chenbo Fengf2759682017-10-10 17:31:57 -070061 /*
62 * Tag the socket with the specified tag and uid. In the qtaguid module, the
63 * first tag request that grab the spinlock of rb_tree can update the tag
64 * information first and other request need to wait until it finish. All the
65 * tag request will be addressed in the order of they obtaining the spinlock.
66 * In the eBPF implementation, the kernel will try to update the eBPF map
67 * entry with the tag request. And the hashmap update process is protected by
68 * the spinlock initialized with the map. So the behavior of two modules
69 * should be the same. No additional lock needed.
70 */
71 int tagSocket(int sockFd, uint32_t tag, uid_t uid);
72
73 /*
74 * The untag process is similiar to tag socket and both old qtaguid module and
75 * new eBPF module have spinlock inside the kernel for concurrent update. No
76 * external lock is required.
77 */
78 int untagSocket(int sockFd);
79
80 /*
81 * Similiar as above, no external lock required.
82 */
83 int setCounterSet(int counterSetNum, uid_t uid);
84
85 /*
86 * When deleting a tag data, the qtaguid module will grab the spinlock of each
87 * related rb_tree one by one and delete the tag information, counterSet
88 * information, iface stats information and uid stats information one by one.
89 * The new eBPF implementation is done similiarly by removing the entry on
90 * each map one by one. And deleting processes are also protected by the
91 * spinlock of the map. So no additional lock is required.
92 */
93 int deleteTagData(uint32_t tag, uid_t uid);
94
Chenbo Feng07d43fe2017-12-21 14:38:51 -080095 /*
96 * Check if the current device have the bpf traffic stats accounting service
97 * running.
98 */
99 bool checkBpfStatsEnable();
100
Chenbo Feng7e974052018-02-28 22:57:21 -0800101 /*
102 * Add the interface name and index pair into the eBPF map.
103 */
104 int addInterface(const char* name, uint32_t ifaceIndex);
105
Chenbo Feng89c12f12018-03-21 10:29:18 -0700106 int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type);
107
108 int removeUidOwnerRule(const uid_t uid);
109
110 int replaceUidOwnerMap(const std::string& name, bool isWhitelist,
111 const std::vector<int32_t>& uids);
112
113 int updateOwnerMapEntry(const base::unique_fd& map_fd, uid_t uid, FirewallRule rule,
114 FirewallType type);
115
Chenbo Fengef297172018-03-26 10:53:33 -0700116 void dump(DumpWriter& dw, bool verbose);
117
Chenbo Feng89c12f12018-03-21 10:29:18 -0700118 int replaceUidsInMap(const base::unique_fd& map_fd, const std::vector<int32_t> &uids,
119 FirewallRule rule, FirewallType type);
120
Chenbo Fengef297172018-03-26 10:53:33 -0700121 static const String16 DUMP_KEYWORD;
122
Chenbo Feng89c12f12018-03-21 10:29:18 -0700123 int toggleUidOwnerMap(ChildChain chain, bool enable);
124
Chenbo Fengf2759682017-10-10 17:31:57 -0700125 private:
126 /*
127 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
Chenbo Fengef1cab32018-04-13 19:50:49 -0700128 * DO NOT hold any locks when modifying this map, otherwise when the untag
129 * operation is waiting for a lock hold by other process and there are more
130 * sockets being closed than can fit in the socket buffer of the netlink socket
131 * that receives them, then the kernel will drop some of these sockets and we
132 * won't delete their tags.
Chenbo Fengf2759682017-10-10 17:31:57 -0700133 * Map Key: uint64_t socket cookie
134 * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
135 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800136 base::unique_fd mCookieTagMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700137
138 /*
139 * mUidCounterSetMap: Store the counterSet of a specific uid.
140 * Map Key: uint32 uid.
141 * Map Value: uint32 counterSet specifies if the traffic is a background
142 * or foreground traffic.
143 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800144 base::unique_fd mUidCounterSetMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700145
146 /*
147 * mUidStatsMap: Store the traffic statistics for a specific combination of
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800148 * uid, iface and counterSet. We maintain this map in addition to
149 * mTagStatsMap because we want to be able to track per-UID data usage even
150 * if mTagStatsMap is full.
Chenbo Fengf2759682017-10-10 17:31:57 -0700151 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
152 * information. The Tag in the StatsKey should always be 0.
153 * Map Value: struct Stats, contains packet count and byte count of each
154 * transport protocol on egress and ingress direction.
155 */
Chenbo Fengef1cab32018-04-13 19:50:49 -0700156 base::unique_fd mUidStatsMap GUARDED_BY(mDeleteStatsMutex);
Chenbo Fengf2759682017-10-10 17:31:57 -0700157
158 /*
159 * mTagStatsMap: Store the traffic statistics for a specific combination of
160 * uid, tag, iface and counterSet. Only tagged socket stats should be stored
161 * in this map.
162 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
163 * information. The tag field should not be 0.
164 * Map Value: struct Stats, contains packet count and byte count of each
165 * transport protocol on egress and ingress direction.
166 */
Chenbo Fengef1cab32018-04-13 19:50:49 -0700167 base::unique_fd mTagStatsMap GUARDED_BY(mDeleteStatsMutex);
168
Chenbo Fengf2759682017-10-10 17:31:57 -0700169
Chenbo Feng7e974052018-02-28 22:57:21 -0800170 /*
171 * mIfaceIndexNameMap: Store the index name pair of each interface show up
172 * on the device since boot. The interface index is used by the eBPF program
173 * to correctly match the iface name when receiving a packet.
174 */
175 base::unique_fd mIfaceIndexNameMap;
176
Chenbo Feng5ed17992018-03-13 21:30:49 -0700177 /*
178 * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf
179 * filter.
180 */
181 base::unique_fd mIfaceStatsMap;
182
Chenbo Feng89c12f12018-03-21 10:29:18 -0700183 /*
184 * mDozableUidMap: Store uids that have related rules in dozable mode owner match
185 * chain.
186 */
187 base::unique_fd mDozableUidMap GUARDED_BY(mOwnerMatchMutex);
188
189 /*
190 * mStandbyUidMap: Store uids that have related rules in standby mode owner match
191 * chain.
192 */
193 base::unique_fd mStandbyUidMap GUARDED_BY(mOwnerMatchMutex);
194
195 /*
196 * mPowerSaveUidMap: Store uids that have related rules in power save mode owner match
197 * chain.
198 */
199 base::unique_fd mPowerSaveUidMap GUARDED_BY(mOwnerMatchMutex);
200
Chenbo Feng116d0552017-12-04 17:25:19 -0800201 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
202
Chenbo Feng33cc1032017-10-23 15:16:37 -0700203 bool ebpfSupported;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800204
Chenbo Feng89c12f12018-03-21 10:29:18 -0700205 std::mutex mOwnerMatchMutex;
206
Chenbo Fengef1cab32018-04-13 19:50:49 -0700207 // When aquiring both mOwnerMatchMutex and mDeleteStatsMutex,
208 // mOwnerMatchMutex must be grabbed first to prevent protential deadlock.
209 // This lock need to be hold when deleting from any stats map which we
210 // can iterate which are uidStatsMap and tagStatsMap. We don't need this
211 // lock to guard mUidCounterSetMap because we always directly look up /
212 // write / delete the map by uid. Also we don't need this lock for
213 // mCookieTagMap since the only time we need to iterate the map is
214 // deleteTagStats and we don't care if we failed and started from the
215 // beginning, since we will eventually scan through the map and delete all
216 // target entries.
217 std::mutex mDeleteStatsMutex;
218
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800219 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
220 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800221
Chenbo Feng89c12f12018-03-21 10:29:18 -0700222 netdutils::Status initMaps();
Chenbo Fenged37fea2017-12-13 19:35:01 -0800223 // For testing
224 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700225};
226
227} // namespace net
228} // namespace android
229
230#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H