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 | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 23 | #include "BandwidthController.h" |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 24 | #include "FirewallController.h" |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 25 | #include "NetlinkListener.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 26 | #include "Network.h" |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 27 | #include "android-base/thread_annotations.h" |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 28 | #include "android-base/unique_fd.h" |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 29 | #include "bpf/BpfMap.h" |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 30 | #include "bpf/bpf_shared.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 31 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 32 | using android::bpf::BpfMap; |
| 33 | using android::bpf::IfaceValue; |
| 34 | using android::bpf::StatsKey; |
| 35 | using android::bpf::StatsValue; |
| 36 | using android::bpf::UidTag; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | namespace net { |
| 40 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 41 | class DumpWriter; |
| 42 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 43 | class TrafficController { |
| 44 | public: |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 45 | TrafficController(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Initialize the whole controller |
| 48 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 49 | netdutils::Status start(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Tag the socket with the specified tag and uid. In the qtaguid module, the |
| 52 | * first tag request that grab the spinlock of rb_tree can update the tag |
| 53 | * information first and other request need to wait until it finish. All the |
| 54 | * tag request will be addressed in the order of they obtaining the spinlock. |
| 55 | * In the eBPF implementation, the kernel will try to update the eBPF map |
| 56 | * entry with the tag request. And the hashmap update process is protected by |
| 57 | * the spinlock initialized with the map. So the behavior of two modules |
| 58 | * should be the same. No additional lock needed. |
| 59 | */ |
| 60 | int tagSocket(int sockFd, uint32_t tag, uid_t uid); |
| 61 | |
| 62 | /* |
| 63 | * The untag process is similiar to tag socket and both old qtaguid module and |
| 64 | * new eBPF module have spinlock inside the kernel for concurrent update. No |
| 65 | * external lock is required. |
| 66 | */ |
| 67 | int untagSocket(int sockFd); |
| 68 | |
| 69 | /* |
| 70 | * Similiar as above, no external lock required. |
| 71 | */ |
| 72 | int setCounterSet(int counterSetNum, uid_t uid); |
| 73 | |
| 74 | /* |
| 75 | * When deleting a tag data, the qtaguid module will grab the spinlock of each |
| 76 | * related rb_tree one by one and delete the tag information, counterSet |
| 77 | * information, iface stats information and uid stats information one by one. |
| 78 | * The new eBPF implementation is done similiarly by removing the entry on |
| 79 | * each map one by one. And deleting processes are also protected by the |
| 80 | * spinlock of the map. So no additional lock is required. |
| 81 | */ |
| 82 | int deleteTagData(uint32_t tag, uid_t uid); |
| 83 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 84 | /* |
| 85 | * Check if the current device have the bpf traffic stats accounting service |
| 86 | * running. |
| 87 | */ |
| 88 | bool checkBpfStatsEnable(); |
| 89 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 90 | /* |
| 91 | * Add the interface name and index pair into the eBPF map. |
| 92 | */ |
| 93 | int addInterface(const char* name, uint32_t ifaceIndex); |
| 94 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 95 | int changeUidOwnerRule(ChildChain chain, const uid_t uid, FirewallRule rule, FirewallType type); |
| 96 | |
| 97 | int removeUidOwnerRule(const uid_t uid); |
| 98 | |
| 99 | int replaceUidOwnerMap(const std::string& name, bool isWhitelist, |
| 100 | const std::vector<int32_t>& uids); |
| 101 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 102 | netdutils::Status updateOwnerMapEntry(BpfMap<uint32_t, uint8_t>& map, uid_t uid, |
| 103 | FirewallRule rule, FirewallType type); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 104 | |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 105 | void dump(DumpWriter& dw, bool verbose); |
| 106 | |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 107 | netdutils::Status replaceUidsInMap(BpfMap<uint32_t, uint8_t>& map, |
| 108 | const std::vector<int32_t>& uids, FirewallRule rule, |
| 109 | FirewallType type); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 110 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 111 | netdutils::Status updateBandwidthUidMap(const std::vector<std::string>& appStrUids, |
| 112 | BandwidthController::IptJumpOp jumpHandling, |
| 113 | BandwidthController::IptOp op); |
Chenbo Feng | ef29717 | 2018-03-26 10:53:33 -0700 | [diff] [blame] | 114 | static const String16 DUMP_KEYWORD; |
| 115 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 116 | int toggleUidOwnerMap(ChildChain chain, bool enable); |
| 117 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 118 | private: |
| 119 | /* |
| 120 | * mCookieTagMap: Store the corresponding tag and uid for a specific socket. |
Chenbo Feng | ef1cab3 | 2018-04-13 19:50:49 -0700 | [diff] [blame] | 121 | * DO NOT hold any locks when modifying this map, otherwise when the untag |
| 122 | * operation is waiting for a lock hold by other process and there are more |
| 123 | * sockets being closed than can fit in the socket buffer of the netlink socket |
| 124 | * that receives them, then the kernel will drop some of these sockets and we |
| 125 | * won't delete their tags. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 126 | * Map Key: uint64_t socket cookie |
| 127 | * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag. |
| 128 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 129 | BpfMap<uint64_t, UidTag> mCookieTagMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * mUidCounterSetMap: Store the counterSet of a specific uid. |
| 133 | * Map Key: uint32 uid. |
| 134 | * Map Value: uint32 counterSet specifies if the traffic is a background |
| 135 | * or foreground traffic. |
| 136 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 137 | BpfMap<uint32_t, uint8_t> mUidCounterSetMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 138 | |
| 139 | /* |
Chenbo Feng | bc4a15f | 2018-05-11 19:15:15 -0700 | [diff] [blame] | 140 | * mAppUidStatsMap: Store the total traffic stats for a uid regardless of |
| 141 | * tag, counterSet and iface. The stats is used by TrafficStats.getUidStats |
| 142 | * API to return persistent stats for a specific uid since device boot. |
| 143 | */ |
| 144 | BpfMap<uint32_t, StatsValue> mAppUidStatsMap; |
| 145 | |
| 146 | /* |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 147 | * mUidStatsMap: Store the traffic statistics for a specific combination of |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 148 | * 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 Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 151 | * 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 Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 156 | BpfMap<StatsKey, StatsValue> mUidStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 157 | |
| 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 Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 167 | BpfMap<StatsKey, StatsValue> mTagStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 168 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 169 | /* |
| 170 | * mIfaceIndexNameMap: Store the index name pair of each interface show up |
| 171 | * on the device since boot. The interface index is used by the eBPF program |
| 172 | * to correctly match the iface name when receiving a packet. |
| 173 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 174 | BpfMap<uint32_t, IfaceValue> mIfaceIndexNameMap; |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 175 | |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 176 | /* |
| 177 | * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf |
| 178 | * filter. |
| 179 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 180 | BpfMap<uint32_t, StatsValue> mIfaceStatsMap; |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 181 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 182 | /* |
| 183 | * mDozableUidMap: Store uids that have related rules in dozable mode owner match |
| 184 | * chain. |
| 185 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 186 | BpfMap<uint32_t, uint8_t> mDozableUidMap GUARDED_BY(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * mStandbyUidMap: Store uids that have related rules in standby mode owner match |
| 190 | * chain. |
| 191 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 192 | BpfMap<uint32_t, uint8_t> mStandbyUidMap GUARDED_BY(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * mPowerSaveUidMap: Store uids that have related rules in power save mode owner match |
| 196 | * chain. |
| 197 | */ |
Chenbo Feng | 4f6c237 | 2018-04-26 10:37:55 -0700 | [diff] [blame] | 198 | BpfMap<uint32_t, uint8_t> mPowerSaveUidMap GUARDED_BY(mOwnerMatchMutex); |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 199 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 200 | /* |
| 201 | * mBandwidthUidMap: Store uids that are used for bandwidth control uid match. |
| 202 | */ |
| 203 | BpfMap<uint32_t, uint8_t> mBandwidthUidMap; |
| 204 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 205 | std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener; |
| 206 | |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 207 | bool ebpfSupported; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 208 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 209 | std::mutex mOwnerMatchMutex; |
| 210 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 211 | netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name, |
| 212 | base::unique_fd& cg_fd); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 213 | |
Chenbo Feng | 89c12f1 | 2018-03-21 10:29:18 -0700 | [diff] [blame] | 214 | netdutils::Status initMaps(); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 215 | |
| 216 | BandwithMatchType jumpOpToMatch(BandwidthController::IptJumpOp jumpHandling); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 217 | // For testing |
| 218 | friend class TrafficControllerTest; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | } // namespace net |
| 222 | } // namespace android |
| 223 | |
| 224 | #endif // NETD_SERVER_TRAFFIC_CONTROLLER_H |