blob: cbef8fca2af08c583c4fe061634b7fd3a2e5ec1b [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 Fengeac6c472018-02-05 15:06:23 -080027// Since we cannot garbage collect the stats map since device boot, we need to make these maps as
28// large as possible. The current rlimit of MEM_LOCK allows at most 10000 map entries for each
29// stats map. In the old qtaguid module, we don't have a total limit for data entries but only have
30// limitation of tags each uid can have. (default is 1024 in kernel);
31// cookie_uid_map: key: 8 bytes, value: 8 bytes, total:10000*8*2 bytes = 160Kbytes
32// uid_counter_set_map: key: 4 bytes, value: 4 bytes, total:10000*4*2 bytes = 80Kbytes
33// uid_stats_map: key: 16 bytes, value: 32 bytes, total:10000*16+10000*32 bytes = 480Kbytes
34// tag_stats_map: key: 16 bytes, value: 32 bytes, total:10000*16+10000*32 bytes = 480Kbytes
35// total: 1200Kbytes
36constexpr const int COOKIE_UID_MAP_SIZE = 10000;
37constexpr const int UID_COUNTERSET_MAP_SIZE = 10000;
38constexpr const int UID_STATS_MAP_SIZE = 10000;
39constexpr const int TAG_STATS_MAP_SIZE = 10000;
40constexpr const int UID_OWNER_MAP_SIZE = 10000;
Chenbo Fengf2759682017-10-10 17:31:57 -070041
42constexpr const int COUNTERSETS_LIMIT = 2;
43
Chenbo Fengc10a8a42017-12-15 13:56:33 -080044constexpr const int NONEXIST_COOKIE = 0;
45
Chenbo Fengf2759682017-10-10 17:31:57 -070046namespace android {
47namespace net {
48
Chenbo Fengf2759682017-10-10 17:31:57 -070049class TrafficController {
50 public:
51 /*
52 * Initialize the whole controller
53 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -080054 netdutils::Status start();
Chenbo Fengf2759682017-10-10 17:31:57 -070055 /*
56 * Tag the socket with the specified tag and uid. In the qtaguid module, the
57 * first tag request that grab the spinlock of rb_tree can update the tag
58 * information first and other request need to wait until it finish. All the
59 * tag request will be addressed in the order of they obtaining the spinlock.
60 * In the eBPF implementation, the kernel will try to update the eBPF map
61 * entry with the tag request. And the hashmap update process is protected by
62 * the spinlock initialized with the map. So the behavior of two modules
63 * should be the same. No additional lock needed.
64 */
65 int tagSocket(int sockFd, uint32_t tag, uid_t uid);
66
67 /*
68 * The untag process is similiar to tag socket and both old qtaguid module and
69 * new eBPF module have spinlock inside the kernel for concurrent update. No
70 * external lock is required.
71 */
72 int untagSocket(int sockFd);
73
74 /*
75 * Similiar as above, no external lock required.
76 */
77 int setCounterSet(int counterSetNum, uid_t uid);
78
79 /*
80 * When deleting a tag data, the qtaguid module will grab the spinlock of each
81 * related rb_tree one by one and delete the tag information, counterSet
82 * information, iface stats information and uid stats information one by one.
83 * The new eBPF implementation is done similiarly by removing the entry on
84 * each map one by one. And deleting processes are also protected by the
85 * spinlock of the map. So no additional lock is required.
86 */
87 int deleteTagData(uint32_t tag, uid_t uid);
88
Chenbo Feng07d43fe2017-12-21 14:38:51 -080089 /*
90 * Check if the current device have the bpf traffic stats accounting service
91 * running.
92 */
93 bool checkBpfStatsEnable();
94
Chenbo Fengf2759682017-10-10 17:31:57 -070095 private:
96 /*
97 * mCookieTagMap: Store the corresponding tag and uid for a specific socket.
98 * Map Key: uint64_t socket cookie
99 * Map Value: struct UidTag, contains a uint32 uid and a uint32 tag.
100 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800101 base::unique_fd mCookieTagMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700102
103 /*
104 * mUidCounterSetMap: Store the counterSet of a specific uid.
105 * Map Key: uint32 uid.
106 * Map Value: uint32 counterSet specifies if the traffic is a background
107 * or foreground traffic.
108 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800109 base::unique_fd mUidCounterSetMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700110
111 /*
112 * mUidStatsMap: Store the traffic statistics for a specific combination of
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800113 * uid, iface and counterSet. We maintain this map in addition to
114 * mTagStatsMap because we want to be able to track per-UID data usage even
115 * if mTagStatsMap is full.
Chenbo Fengf2759682017-10-10 17:31:57 -0700116 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
117 * information. The Tag in the StatsKey should always be 0.
118 * Map Value: struct Stats, contains packet count and byte count of each
119 * transport protocol on egress and ingress direction.
120 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800121 base::unique_fd mUidStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700122
123 /*
124 * mTagStatsMap: Store the traffic statistics for a specific combination of
125 * uid, tag, iface and counterSet. Only tagged socket stats should be stored
126 * in this map.
127 * Map Key: Struct StatsKey contains the uid, counterSet and ifaceIndex
128 * information. The tag field should not be 0.
129 * Map Value: struct Stats, contains packet count and byte count of each
130 * transport protocol on egress and ingress direction.
131 */
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800132 base::unique_fd mTagStatsMap;
Chenbo Fengf2759682017-10-10 17:31:57 -0700133
Chenbo Feng116d0552017-12-04 17:25:19 -0800134 std::unique_ptr<NetlinkListenerInterface> mSkDestroyListener;
135
Chenbo Feng33cc1032017-10-23 15:16:37 -0700136 bool ebpfSupported;
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800137
138 netdutils::Status loadAndAttachProgram(bpf_attach_type type, const char* path, const char* name,
139 base::unique_fd& cg_fd);
Chenbo Fenged37fea2017-12-13 19:35:01 -0800140
141 // For testing
142 friend class TrafficControllerTest;
Chenbo Fengf2759682017-10-10 17:31:57 -0700143};
144
145} // namespace net
146} // namespace android
147
148#endif // NETD_SERVER_TRAFFIC_CONTROLLER_H