blob: 68bb477c93698f066b273787e743e3a2d1c033cb [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
Chenbo Fengf2759682017-10-10 17:31:57 -070027#define LOG_BUF_SIZE 65536
28
29#define BPF_PATH "/sys/fs/bpf"
30
Chenbo Fengc10a8a42017-12-15 13:56:33 -080031constexpr const char* BPF_EGRESS_PROG_PATH = BPF_PATH "/egress_prog";
32constexpr const char* BPF_INGRESS_PROG_PATH = BPF_PATH "/ingress_prog";
Chenbo Fengf2759682017-10-10 17:31:57 -070033
34constexpr const char* CGROUP_ROOT_PATH = "/dev/cg2_bpf";
35
36constexpr const int IPV6_TRANSPORT_PROTOCOL_OFFSET = 6;
37constexpr const int IPV4_TRANSPORT_PROTOCOL_OFFSET = 9;
38
39// TODO: change it to a reasonable size.
40constexpr const int COOKIE_UID_MAP_SIZE = 100;
41constexpr const int UID_COUNTERSET_MAP_SIZE = 100;
42constexpr const int UID_STATS_MAP_SIZE = 100;
43constexpr const int TAG_STATS_MAP_SIZE = 100;
44
45constexpr const int COUNTERSETS_LIMIT = 2;
46
Chenbo Fengc10a8a42017-12-15 13:56:33 -080047constexpr const int NONEXIST_COOKIE = 0;
48
Chenbo Fengf2759682017-10-10 17:31:57 -070049namespace android {
50namespace net {
51
Chenbo Fengf2759682017-10-10 17:31:57 -070052class TrafficController {
53 public:
54 /*
55 * Initialize the whole controller
56 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080057 netdutils::Status start();
Chenbo Fengf2759682017-10-10 17:31:57 -070058 /*
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 Fengf2759682017-10-10 17:31:57 -070092 private:
93 /*
94 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
95 * Map Key: uint64_t socket cookie
96 * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
97 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080098 base::unique_fd mCookieTagMap;
Chenbo Fengf2759682017-10-10 17:31:57 -070099
100 /*
101 * mUidCounterSetMap: Store the counterSet of a specific uid.
102 * Map Key: uint32 uid.
103 * Map Value: uint32 counterSet specifies if the traffic is a background
104 * or foreground traffic.
105 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800106 base::unique_fd mUidCounterSetMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700107
108 /*
109 * mUidStatsMap: Store the traffic statistics for a specific combination of
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800110 * uid, iface and counterSet. We maintain this map in addition to
111 * mTagStatsMap because we want to be able to track per-UID data usage even
112 * if mTagStatsMap is full.
Chenbo Fengf2759682017-10-10 17:31:57 -0700113 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
114 * information. The Tag in the StatsKey should always be 0.
115 * Map Value: struct Stats, contains packet count and byte count of each
116 * transport protocol on egress and ingress direction.
117 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800118 base::unique_fd mUidStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700119
120 /*
121 * mTagStatsMap: Store the traffic statistics for a specific combination of
122 * uid, tag, iface and counterSet. Only tagged socket stats should be stored
123 * in this map.
124 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
125 * information. The tag field should not 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 Fengc10a8a42017-12-15 13:56:33 -0800129 base::unique_fd mTagStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700130
Chenbo Feng116d0552017-12-04 17:25:19 -0800131 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
132
Chenbo Feng33cc1032017-10-23 15:16:37 -0700133 bool ebpfSupported;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800134
135 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
136 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800137
138 // For testing
139 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700140};
141
142} // namespace net
143} // namespace android
144
145#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H