blob: bbb36403169324f59962817b30a63511f5a1b2c2 [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
22#include "Network.h"
23
24#ifndef DEFAULT_OVERFLOWUID
25#define DEFAULT_OVERFLOWUID 65534
26#endif
27
28#define LOG_BUF_SIZE 65536
29
30#define BPF_PATH "/sys/fs/bpf"
31
32constexpr const char* COOKIE_UID_MAP_PATH = BPF_PATH "/traffic_cookie_uid_map";
33constexpr const char* UID_COUNTERSET_MAP_PATH = BPF_PATH "/traffic_uid_counterSet_map";
34constexpr const char* UID_STATS_MAP_PATH = BPF_PATH "/traffic_uid_stats_map";
35constexpr const char* TAG_STATS_MAP_PATH = BPF_PATH "/traffic_tag_stats_map";
36
37constexpr const char* CGROUP_ROOT_PATH = "/dev/cg2_bpf";
38
39constexpr const int IPV6_TRANSPORT_PROTOCOL_OFFSET = 6;
40constexpr const int IPV4_TRANSPORT_PROTOCOL_OFFSET = 9;
41
42// TODO: change it to a reasonable size.
43constexpr const int COOKIE_UID_MAP_SIZE = 100;
44constexpr const int UID_COUNTERSET_MAP_SIZE = 100;
45constexpr const int UID_STATS_MAP_SIZE = 100;
46constexpr const int TAG_STATS_MAP_SIZE = 100;
47
48constexpr const int COUNTERSETS_LIMIT = 2;
49
50namespace android {
51namespace net {
52
53struct UidTag {
54 uint32_t uid;
55 uint32_t tag;
56};
57
58struct StatsKey {
59 uint32_t uid;
60 uint32_t tag;
61 uint32_t counterSet;
62 uint32_t ifaceIndex;
63};
64
65struct Stats {
66 uint64_t rxTcpPackets;
67 uint64_t rxTcpBytes;
68 uint64_t txTcpPackets;
69 uint64_t txTcpBytes;
70 uint64_t rxUdpPackets;
71 uint64_t rxUdpBytes;
72 uint64_t txUdpPackets;
73 uint64_t txUdpBytes;
74 uint64_t rxOtherPackets;
75 uint64_t rxOtherBytes;
76 uint64_t txOtherPackets;
77 uint64_t txOtherBytes;
78};
79
80class TrafficController {
81 public:
82 /*
83 * Initialize the whole controller
84 */
85 int start();
86 /*
87 * Tag the socket with the specified tag and uid. In the qtaguid module, the
88 * first tag request that grab the spinlock of rb_tree can update the tag
89 * information first and other request need to wait until it finish. All the
90 * tag request will be addressed in the order of they obtaining the spinlock.
91 * In the eBPF implementation, the kernel will try to update the eBPF map
92 * entry with the tag request. And the hashmap update process is protected by
93 * the spinlock initialized with the map. So the behavior of two modules
94 * should be the same. No additional lock needed.
95 */
96 int tagSocket(int sockFd, uint32_t tag, uid_t uid);
97
98 /*
99 * The untag process is similiar to tag socket and both old qtaguid module and
100 * new eBPF module have spinlock inside the kernel for concurrent update. No
101 * external lock is required.
102 */
103 int untagSocket(int sockFd);
104
105 /*
106 * Similiar as above, no external lock required.
107 */
108 int setCounterSet(int counterSetNum, uid_t uid);
109
110 /*
111 * When deleting a tag data, the qtaguid module will grab the spinlock of each
112 * related rb_tree one by one and delete the tag information, counterSet
113 * information, iface stats information and uid stats information one by one.
114 * The new eBPF implementation is done similiarly by removing the entry on
115 * each map one by one. And deleting processes are also protected by the
116 * spinlock of the map. So no additional lock is required.
117 */
118 int deleteTagData(uint32_t tag, uid_t uid);
119
120 /* Old api for debugging the qtaguid module. When the pacifier is on, all
121 * the command will return successful but no actual action is taken.
122 */
123 int setPacifier(uint32_t on);
124
125 private:
126 /*
127 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
128 * Map Key: uint64_t socket cookie
129 * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
130 */
131 int mCookieTagMap;
132
133 /*
134 * mUidCounterSetMap: Store the counterSet of a specific uid.
135 * Map Key: uint32 uid.
136 * Map Value: uint32 counterSet specifies if the traffic is a background
137 * or foreground traffic.
138 */
139 int mUidCounterSetMap;
140
141 /*
142 * mUidStatsMap: Store the traffic statistics for a specific combination of
143 * uid, iface and counterSet.
144 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
145 * information. The Tag in the StatsKey should always be 0.
146 * Map Value: struct Stats, contains packet count and byte count of each
147 * transport protocol on egress and ingress direction.
148 */
149 int mUidStatsMap;
150
151 /*
152 * mTagStatsMap: Store the traffic statistics for a specific combination of
153 * uid, tag, iface and counterSet. Only tagged socket stats should be stored
154 * in this map.
155 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
156 * information. The tag field should not be 0.
157 * Map Value: struct Stats, contains packet count and byte count of each
158 * transport protocol on egress and ingress direction.
159 */
160 int mTagStatsMap;
161
162 /*
163 * IngressProgram: Program attached to the root cgroup directory and
164 * monitoring the traffic on ingress side.
165 */
166 int mInProgFd;
167
168 /*
169 * EgressProgram: Program attached to the root cgroup directory and
170 * monitoring the traffic on egress side.
171 */
172 int mOutProgFd;
173};
174
175} // namespace net
176} // namespace android
177
178#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H