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