Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 22 | #include <netdutils/StatusOr.h> |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame^] | 23 | #include "FirewallController.h" |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 24 | #include "NetlinkListener.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 25 | #include "Network.h" |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame^] | 26 | #include "android-base/thread_annotations.h" |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 27 | #include "android-base/unique_fd.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 28 | |
Chenbo Feng | eac6c47 | 2018-02-05 15:06:23 -0800 | [diff] [blame] | 29 | // 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 Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 37 | // iface_index_name_map:key: 4 bytes, value: 32 bytes, total:10000*36 bytes = 360Kbytes |
| 38 | // total: 1560Kbytes |
Chenbo Feng | eac6c47 | 2018-02-05 15:06:23 -0800 | [diff] [blame] | 39 | constexpr const int COOKIE_UID_MAP_SIZE = 10000; |
| 40 | constexpr const int UID_COUNTERSET_MAP_SIZE = 10000; |
| 41 | constexpr const int UID_STATS_MAP_SIZE = 10000; |
| 42 | constexpr const int TAG_STATS_MAP_SIZE = 10000; |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 43 | constexpr const int IFACE_INDEX_NAME_MAP_SIZE = 1000; |
| 44 | constexpr const int IFACE_STATS_MAP_SIZE = 1000; |
Chenbo Feng | eac6c47 | 2018-02-05 15:06:23 -0800 | [diff] [blame] | 45 | constexpr const int UID_OWNER_MAP_SIZE = 10000; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 46 | |
| 47 | constexpr const int COUNTERSETS_LIMIT = 2; |
| 48 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 49 | constexpr const int NONEXIST_COOKIE = 0; |
| 50 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 51 | namespace android { |
| 52 | namespace net { |
| 53 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 54 | class TrafficController { |
| 55 | public: |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame^] | 56 | TrafficController(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Initialize the whole controller |
| 59 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 60 | netdutils::Status start(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 61 | /* |
| 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 Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 95 | /* |
| 96 | * Check if the current device have the bpf traffic stats accounting service |
| 97 | * running. |
| 98 | */ |
| 99 | bool checkBpfStatsEnable(); |
| 100 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 101 | /* |
| 102 | * Add the interface name and index pair into the eBPF map. |
| 103 | */ |
| 104 | int addInterface(const char* name, uint32_t ifaceIndex); |
| 105 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame^] | 106 | 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 | |
| 116 | int replaceUidsInMap(const base::unique_fd& map_fd, const std::vector<int32_t> &uids, |
| 117 | FirewallRule rule, FirewallType type); |
| 118 | |
| 119 | int toggleUidOwnerMap(ChildChain chain, bool enable); |
| 120 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 121 | private: |
| 122 | /* |
| 123 | * mCookieTagMap: Store the corresponding tag and uid for a specific socket. |
| 124 | * Map Key: uint64_t socket cookie |
| 125 | * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag. |
| 126 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 127 | base::unique_fd mCookieTagMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * mUidCounterSetMap: Store the counterSet of a specific uid. |
| 131 | * Map Key: uint32 uid. |
| 132 | * Map Value: uint32 counterSet specifies if the traffic is a background |
| 133 | * or foreground traffic. |
| 134 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 135 | base::unique_fd mUidCounterSetMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * mUidStatsMap: Store the traffic statistics for a specific combination of |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 139 | * uid, iface and counterSet. We maintain this map in addition to |
| 140 | * mTagStatsMap because we want to be able to track per-UID data usage even |
| 141 | * if mTagStatsMap is full. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 142 | * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex |
| 143 | * information. The Tag in the StatsKey should always be 0. |
| 144 | * Map Value: struct Stats, contains packet count and byte count of each |
| 145 | * transport protocol on egress and ingress direction. |
| 146 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 147 | base::unique_fd mUidStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * mTagStatsMap: Store the traffic statistics for a specific combination of |
| 151 | * uid, tag, iface and counterSet. Only tagged socket stats should be stored |
| 152 | * in this map. |
| 153 | * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex |
| 154 | * information. The tag field should not be 0. |
| 155 | * Map Value: struct Stats, contains packet count and byte count of each |
| 156 | * transport protocol on egress and ingress direction. |
| 157 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 158 | base::unique_fd mTagStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 159 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 160 | /* |
| 161 | * mIfaceIndexNameMap: Store the index name pair of each interface show up |
| 162 | * on the device since boot. The interface index is used by the eBPF program |
| 163 | * to correctly match the iface name when receiving a packet. |
| 164 | */ |
| 165 | base::unique_fd mIfaceIndexNameMap; |
| 166 | |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 167 | /* |
| 168 | * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf |
| 169 | * filter. |
| 170 | */ |
| 171 | base::unique_fd mIfaceStatsMap; |
| 172 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame^] | 173 | /* |
| 174 | * mDozableUidMap: Store uids that have related rules in dozable mode owner match |
| 175 | * chain. |
| 176 | */ |
| 177 | base::unique_fd mDozableUidMap GUARDED_BY(mOwnerMatchMutex); |
| 178 | |
| 179 | /* |
| 180 | * mStandbyUidMap: Store uids that have related rules in standby mode owner match |
| 181 | * chain. |
| 182 | */ |
| 183 | base::unique_fd mStandbyUidMap GUARDED_BY(mOwnerMatchMutex); |
| 184 | |
| 185 | /* |
| 186 | * mPowerSaveUidMap: Store uids that have related rules in power save mode owner match |
| 187 | * chain. |
| 188 | */ |
| 189 | base::unique_fd mPowerSaveUidMap GUARDED_BY(mOwnerMatchMutex); |
| 190 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 191 | std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener; |
| 192 | |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 193 | bool ebpfSupported; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 194 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame^] | 195 | std::mutex mOwnerMatchMutex; |
| 196 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 197 | netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name, |
| 198 | base::unique_fd& cg_fd); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 199 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame^] | 200 | netdutils::Status initMaps(); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 201 | // For testing |
| 202 | friend class TrafficControllerTest; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } // namespace net |
| 206 | } // namespace android |
| 207 | |
| 208 | #endif // NETD_SERVER_TRAFFIC_CONTROLLER_H |