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 | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 27 | // TODO: change it to a reasonable size. |
Chenbo Feng | 05393d8 | 2018-01-09 15:18:43 -0800 | [diff] [blame] | 28 | constexpr const int COOKIE_UID_MAP_SIZE = 1000; |
| 29 | constexpr const int UID_COUNTERSET_MAP_SIZE = 1000; |
| 30 | constexpr const int UID_STATS_MAP_SIZE = 1000; |
| 31 | constexpr const int TAG_STATS_MAP_SIZE = 1000; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 32 | |
| 33 | constexpr const int COUNTERSETS_LIMIT = 2; |
| 34 | |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 35 | constexpr const int NONEXIST_COOKIE = 0; |
| 36 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace net { |
| 39 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 40 | class TrafficController { |
| 41 | public: |
| 42 | /* |
| 43 | * Initialize the whole controller |
| 44 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 45 | netdutils::Status start(); |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Tag the socket with the specified tag and uid. In the qtaguid module, the |
| 48 | * first tag request that grab the spinlock of rb_tree can update the tag |
| 49 | * information first and other request need to wait until it finish. All the |
| 50 | * tag request will be addressed in the order of they obtaining the spinlock. |
| 51 | * In the eBPF implementation, the kernel will try to update the eBPF map |
| 52 | * entry with the tag request. And the hashmap update process is protected by |
| 53 | * the spinlock initialized with the map. So the behavior of two modules |
| 54 | * should be the same. No additional lock needed. |
| 55 | */ |
| 56 | int tagSocket(int sockFd, uint32_t tag, uid_t uid); |
| 57 | |
| 58 | /* |
| 59 | * The untag process is similiar to tag socket and both old qtaguid module and |
| 60 | * new eBPF module have spinlock inside the kernel for concurrent update. No |
| 61 | * external lock is required. |
| 62 | */ |
| 63 | int untagSocket(int sockFd); |
| 64 | |
| 65 | /* |
| 66 | * Similiar as above, no external lock required. |
| 67 | */ |
| 68 | int setCounterSet(int counterSetNum, uid_t uid); |
| 69 | |
| 70 | /* |
| 71 | * When deleting a tag data, the qtaguid module will grab the spinlock of each |
| 72 | * related rb_tree one by one and delete the tag information, counterSet |
| 73 | * information, iface stats information and uid stats information one by one. |
| 74 | * The new eBPF implementation is done similiarly by removing the entry on |
| 75 | * each map one by one. And deleting processes are also protected by the |
| 76 | * spinlock of the map. So no additional lock is required. |
| 77 | */ |
| 78 | int deleteTagData(uint32_t tag, uid_t uid); |
| 79 | |
Chenbo Feng | 07d43fe | 2017-12-21 14:38:51 -0800 | [diff] [blame] | 80 | /* |
| 81 | * Check if the current device have the bpf traffic stats accounting service |
| 82 | * running. |
| 83 | */ |
| 84 | bool checkBpfStatsEnable(); |
| 85 | |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 86 | private: |
| 87 | /* |
| 88 | * mCookieTagMap: Store the corresponding tag and uid for a specific socket. |
| 89 | * Map Key: uint64_t socket cookie |
| 90 | * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag. |
| 91 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 92 | base::unique_fd mCookieTagMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * mUidCounterSetMap: Store the counterSet of a specific uid. |
| 96 | * Map Key: uint32 uid. |
| 97 | * Map Value: uint32 counterSet specifies if the traffic is a background |
| 98 | * or foreground traffic. |
| 99 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 100 | base::unique_fd mUidCounterSetMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * mUidStatsMap: Store the traffic statistics for a specific combination of |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 104 | * uid, iface and counterSet. We maintain this map in addition to |
| 105 | * mTagStatsMap because we want to be able to track per-UID data usage even |
| 106 | * if mTagStatsMap is full. |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 107 | * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex |
| 108 | * information. The Tag in the StatsKey should always be 0. |
| 109 | * Map Value: struct Stats, contains packet count and byte count of each |
| 110 | * transport protocol on egress and ingress direction. |
| 111 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 112 | base::unique_fd mUidStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * mTagStatsMap: Store the traffic statistics for a specific combination of |
| 116 | * uid, tag, iface and counterSet. Only tagged socket stats should be stored |
| 117 | * in this map. |
| 118 | * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex |
| 119 | * information. The tag field should not be 0. |
| 120 | * Map Value: struct Stats, contains packet count and byte count of each |
| 121 | * transport protocol on egress and ingress direction. |
| 122 | */ |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 123 | base::unique_fd mTagStatsMap; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 124 | |
Chenbo Feng | 116d055 | 2017-12-04 17:25:19 -0800 | [diff] [blame] | 125 | std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener; |
| 126 | |
Chenbo Feng | 33cc103 | 2017-10-23 15:16:37 -0700 | [diff] [blame] | 127 | bool ebpfSupported; |
Chenbo Feng | c10a8a4 | 2017-12-15 13:56:33 -0800 | [diff] [blame] | 128 | |
| 129 | netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name, |
| 130 | base::unique_fd& cg_fd); |
Chenbo Feng | ed37fea | 2017-12-13 19:35:01 -0800 | [diff] [blame] | 131 | |
| 132 | // For testing |
| 133 | friend class TrafficControllerTest; |
Chenbo Feng | f275968 | 2017-10-10 17:31:57 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace net |
| 137 | } // namespace android |
| 138 | |
| 139 | #endif // NETD_SERVER_TRAFFIC_CONTROLLER_H |