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 | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 23 | #include "NetlinkListener.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 24 | #include "Network.h" |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 25 | #include "android-base/unique_fd.h" |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 26 | |
Chenbo Feng | eac6c47 | 2018-02-05 15:06:23 -0800 | [diff] [blame] | 27 | // Since we cannot garbage collect the stats map since device boot, we need to make these maps as |
| 28 | // large as possible. The current rlimit of MEM_LOCK allows at most 10000 map entries for each |
| 29 | // stats map. In the old qtaguid module, we don't have a total limit for data entries but only have |
| 30 | // limitation of tags each uid can have. (default is 1024 in kernel); |
| 31 | // cookie_uid_map: key: 8 bytes, value: 8 bytes, total:10000*8*2 bytes = 160Kbytes |
| 32 | // uid_counter_set_map: key: 4 bytes, value: 4 bytes, total:10000*4*2 bytes = 80Kbytes |
| 33 | // uid_stats_map: key: 16 bytes, value: 32 bytes, total:10000*16+10000*32 bytes = 480Kbytes |
| 34 | // 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] | 35 | // iface_index_name_map:key: 4 bytes, value: 32 bytes, total:10000*36 bytes = 360Kbytes |
| 36 | // total: 1560Kbytes |
Chenbo Feng | eac6c47 | 2018-02-05 15:06:23 -0800 | [diff] [blame] | 37 | constexpr const int COOKIE_UID_MAP_SIZE = 10000; |
| 38 | constexpr const int UID_COUNTERSET_MAP_SIZE = 10000; |
| 39 | constexpr const int UID_STATS_MAP_SIZE = 10000; |
| 40 | constexpr const int TAG_STATS_MAP_SIZE = 10000; |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 41 | constexpr const int IFACE_INDEX_NAME_MAP_SIZE = 1000; |
| 42 | constexpr const int IFACE_STATS_MAP_SIZE = 1000; |
Chenbo Feng | eac6c47 | 2018-02-05 15:06:23 -0800 | [diff] [blame] | 43 | constexpr const int UID_OWNER_MAP_SIZE = 10000; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 44 | |
| 45 | constexpr const int COUNTERSETS_LIMIT = 2; |
| 46 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 47 | constexpr const int NONEXIST_COOKIE = 0; |
| 48 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 49 | namespace android { |
| 50 | namespace net { |
| 51 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 52 | class TrafficController { |
| 53 | public: |
| 54 | /* |
| 55 | * Initialize the whole controller |
| 56 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 57 | netdutils::Status start(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Tag the socket with the specified tag and uid. In the qtaguid module, the |
| 60 | * first tag request that grab the spinlock of rb_tree can update the tag |
| 61 | * information first and other request need to wait until it finish. All the |
| 62 | * tag request will be addressed in the order of they obtaining the spinlock. |
| 63 | * In the eBPF implementation, the kernel will try to update the eBPF map |
| 64 | * entry with the tag request. And the hashmap update process is protected by |
| 65 | * the spinlock initialized with the map. So the behavior of two modules |
| 66 | * should be the same. No additional lock needed. |
| 67 | */ |
| 68 | int tagSocket(int sockFd, uint32_t tag, uid_t uid); |
| 69 | |
| 70 | /* |
| 71 | * The untag process is similiar to tag socket and both old qtaguid module and |
| 72 | * new eBPF module have spinlock inside the kernel for concurrent update. No |
| 73 | * external lock is required. |
| 74 | */ |
| 75 | int untagSocket(int sockFd); |
| 76 | |
| 77 | /* |
| 78 | * Similiar as above, no external lock required. |
| 79 | */ |
| 80 | int setCounterSet(int counterSetNum, uid_t uid); |
| 81 | |
| 82 | /* |
| 83 | * When deleting a tag data, the qtaguid module will grab the spinlock of each |
| 84 | * related rb_tree one by one and delete the tag information, counterSet |
| 85 | * information, iface stats information and uid stats information one by one. |
| 86 | * The new eBPF implementation is done similiarly by removing the entry on |
| 87 | * each map one by one. And deleting processes are also protected by the |
| 88 | * spinlock of the map. So no additional lock is required. |
| 89 | */ |
| 90 | int deleteTagData(uint32_t tag, uid_t uid); |
| 91 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 92 | /* |
| 93 | * Check if the current device have the bpf traffic stats accounting service |
| 94 | * running. |
| 95 | */ |
| 96 | bool checkBpfStatsEnable(); |
| 97 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 98 | /* |
| 99 | * Add the interface name and index pair into the eBPF map. |
| 100 | */ |
| 101 | int addInterface(const char* name, uint32_t ifaceIndex); |
| 102 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 103 | private: |
| 104 | /* |
| 105 | * mCookieTagMap: Store the corresponding tag and uid for a specific socket. |
| 106 | * Map Key: uint64_t socket cookie |
| 107 | * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag. |
| 108 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 109 | base::unique_fd mCookieTagMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * mUidCounterSetMap: Store the counterSet of a specific uid. |
| 113 | * Map Key: uint32 uid. |
| 114 | * Map Value: uint32 counterSet specifies if the traffic is a background |
| 115 | * or foreground traffic. |
| 116 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 117 | base::unique_fd mUidCounterSetMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * mUidStatsMap: Store the traffic statistics for a specific combination of |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 121 | * uid, iface and counterSet. We maintain this map in addition to |
| 122 | * mTagStatsMap because we want to be able to track per-UID data usage even |
| 123 | * if mTagStatsMap is full. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 124 | * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex |
| 125 | * information. The Tag in the StatsKey should always be 0. |
| 126 | * Map Value: struct Stats, contains packet count and byte count of each |
| 127 | * transport protocol on egress and ingress direction. |
| 128 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 129 | base::unique_fd mUidStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * mTagStatsMap: Store the traffic statistics for a specific combination of |
| 133 | * uid, tag, iface and counterSet. Only tagged socket stats should be stored |
| 134 | * in this map. |
| 135 | * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex |
| 136 | * information. The tag field should not be 0. |
| 137 | * Map Value: struct Stats, contains packet count and byte count of each |
| 138 | * transport protocol on egress and ingress direction. |
| 139 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 140 | base::unique_fd mTagStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 141 | |
Chenbo Feng | 7e97405 | 2018-02-28 22:57:21 -0800 | [diff] [blame] | 142 | /* |
| 143 | * mIfaceIndexNameMap: Store the index name pair of each interface show up |
| 144 | * on the device since boot. The interface index is used by the eBPF program |
| 145 | * to correctly match the iface name when receiving a packet. |
| 146 | */ |
| 147 | base::unique_fd mIfaceIndexNameMap; |
| 148 | |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 149 | /* |
| 150 | * mIfaceStataMap: Store per iface traffic stats gathered from xt_bpf |
| 151 | * filter. |
| 152 | */ |
| 153 | base::unique_fd mIfaceStatsMap; |
| 154 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 155 | std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener; |
| 156 | |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 157 | bool ebpfSupported; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 158 | |
| 159 | netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name, |
| 160 | base::unique_fd& cg_fd); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 161 | |
| 162 | // For testing |
| 163 | friend class TrafficControllerTest; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } // namespace net |
| 167 | } // namespace android |
| 168 | |
| 169 | #endif // NETD_SERVER_TRAFFIC_CONTROLLER_H |