blob: be2f6e66b71ef5126c483e307133c22932a6573d [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 Feng116d0552017-12-04 17:25:19 -080023#include "NetlinkListener.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070024#include "Network.h"
Chenbo Fengc10a8a42017-12-15 13:56:33 -080025#include "android-base/unique_fd.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070026
27#ifndef DEFAULT_OVERFLOWUID
28#define DEFAULT_OVERFLOWUID 65534
29#endif
30
31#define LOG_BUF_SIZE 65536
32
33#define BPF_PATH "/sys/fs/bpf"
34
35constexpr const char* COOKIE_UID_MAP_PATH = BPF_PATH "/traffic_cookie_uid_map";
36constexpr const char* UID_COUNTERSET_MAP_PATH = BPF_PATH "/traffic_uid_counterSet_map";
37constexpr const char* UID_STATS_MAP_PATH = BPF_PATH "/traffic_uid_stats_map";
38constexpr const char* TAG_STATS_MAP_PATH = BPF_PATH "/traffic_tag_stats_map";
Chenbo Fengc10a8a42017-12-15 13:56:33 -080039constexpr const char* BPF_EGRESS_PROG_PATH = BPF_PATH "/egress_prog";
40constexpr const char* BPF_INGRESS_PROG_PATH = BPF_PATH "/ingress_prog";
Chenbo Fengf2759682017-10-10 17:31:57 -070041
42constexpr const char* CGROUP_ROOT_PATH = "/dev/cg2_bpf";
43
44constexpr const int IPV6_TRANSPORT_PROTOCOL_OFFSET = 6;
45constexpr const int IPV4_TRANSPORT_PROTOCOL_OFFSET = 9;
46
47// TODO: change it to a reasonable size.
48constexpr const int COOKIE_UID_MAP_SIZE = 100;
49constexpr const int UID_COUNTERSET_MAP_SIZE = 100;
50constexpr const int UID_STATS_MAP_SIZE = 100;
51constexpr const int TAG_STATS_MAP_SIZE = 100;
52
53constexpr const int COUNTERSETS_LIMIT = 2;
54
Chenbo Fengc10a8a42017-12-15 13:56:33 -080055constexpr const int NONEXIST_COOKIE = 0;
56
Chenbo Fengf2759682017-10-10 17:31:57 -070057namespace android {
58namespace net {
59
60struct UidTag {
61 uint32_t uid;
62 uint32_t tag;
63};
64
65struct StatsKey {
66 uint32_t uid;
67 uint32_t tag;
68 uint32_t counterSet;
69 uint32_t ifaceIndex;
70};
71
Chenbo Fengc10a8a42017-12-15 13:56:33 -080072// TODO: verify if framework side still need the detail number about TCP and UDP
73// traffic. If not, remove the related tx/rx bytes and packets field to save
74// space and simplify the eBPF program.
Chenbo Fengf2759682017-10-10 17:31:57 -070075struct Stats {
76 uint64_t rxTcpPackets;
77 uint64_t rxTcpBytes;
78 uint64_t txTcpPackets;
79 uint64_t txTcpBytes;
80 uint64_t rxUdpPackets;
81 uint64_t rxUdpBytes;
82 uint64_t txUdpPackets;
83 uint64_t txUdpBytes;
84 uint64_t rxOtherPackets;
85 uint64_t rxOtherBytes;
86 uint64_t txOtherPackets;
87 uint64_t txOtherBytes;
88};
89
90class TrafficController {
91 public:
92 /*
93 * Initialize the whole controller
94 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080095 netdutils::Status start();
Chenbo Fengf2759682017-10-10 17:31:57 -070096 /*
97 * Tag the socket with the specified tag and uid. In the qtaguid module, the
98 * first tag request that grab the spinlock of rb_tree can update the tag
99 * information first and other request need to wait until it finish. All the
100 * tag request will be addressed in the order of they obtaining the spinlock.
101 * In the eBPF implementation, the kernel will try to update the eBPF map
102 * entry with the tag request. And the hashmap update process is protected by
103 * the spinlock initialized with the map. So the behavior of two modules
104 * should be the same. No additional lock needed.
105 */
106 int tagSocket(int sockFd, uint32_t tag, uid_t uid);
107
108 /*
109 * The untag process is similiar to tag socket and both old qtaguid module and
110 * new eBPF module have spinlock inside the kernel for concurrent update. No
111 * external lock is required.
112 */
113 int untagSocket(int sockFd);
114
115 /*
116 * Similiar as above, no external lock required.
117 */
118 int setCounterSet(int counterSetNum, uid_t uid);
119
120 /*
121 * When deleting a tag data, the qtaguid module will grab the spinlock of each
122 * related rb_tree one by one and delete the tag information, counterSet
123 * information, iface stats information and uid stats information one by one.
124 * The new eBPF implementation is done similiarly by removing the entry on
125 * each map one by one. And deleting processes are also protected by the
126 * spinlock of the map. So no additional lock is required.
127 */
128 int deleteTagData(uint32_t tag, uid_t uid);
129
Chenbo Fengf2759682017-10-10 17:31:57 -0700130 private:
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800131 struct StatsKey NONEXIST_STATSKEY = {
132 .uid = DEFAULT_OVERFLOWUID, .tag = 0, .counterSet = 0, .ifaceIndex = 0};
133
Chenbo Fengf2759682017-10-10 17:31:57 -0700134 /*
135 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
136 * Map Key: uint64_t socket cookie
137 * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
138 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800139 base::unique_fd mCookieTagMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700140
141 /*
142 * mUidCounterSetMap: Store the counterSet of a specific uid.
143 * Map Key: uint32 uid.
144 * Map Value: uint32 counterSet specifies if the traffic is a background
145 * or foreground traffic.
146 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800147 base::unique_fd mUidCounterSetMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700148
149 /*
150 * mUidStatsMap: Store the traffic statistics for a specific combination of
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800151 * uid, iface and counterSet. We maintain this map in addition to
152 * mTagStatsMap because we want to be able to track per-UID data usage even
153 * if mTagStatsMap is full.
Chenbo Fengf2759682017-10-10 17:31:57 -0700154 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
155 * information. The Tag in the StatsKey should always be 0.
156 * Map Value: struct Stats, contains packet count and byte count of each
157 * transport protocol on egress and ingress direction.
158 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800159 base::unique_fd mUidStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700160
161 /*
162 * mTagStatsMap: Store the traffic statistics for a specific combination of
163 * uid, tag, iface and counterSet. Only tagged socket stats should be stored
164 * in this map.
165 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
166 * information. The tag field should not be 0.
167 * Map Value: struct Stats, contains packet count and byte count of each
168 * transport protocol on egress and ingress direction.
169 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800170 base::unique_fd mTagStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700171
Chenbo Feng116d0552017-12-04 17:25:19 -0800172 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
173
Chenbo Feng33cc1032017-10-23 15:16:37 -0700174 bool ebpfSupported;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800175
176 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
177 base::unique_fd& cg_fd);
Chenbo Fengf2759682017-10-10 17:31:57 -0700178};
179
180} // namespace net
181} // namespace android
182
183#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H