• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 #ifndef _BANDWIDTH_CONTROLLER_H
17 #define _BANDWIDTH_CONTROLLER_H
18 
19 #include <map>
20 #include <set>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 #include <mutex>
25 
26 #include "NetdConstants.h"
27 #include "netdbpf/bpf_shared.h"
28 
29 class BandwidthController {
30 public:
31     std::mutex lock;
32 
33     BandwidthController();
34 
35     int setupIptablesHooks();
36 
37     int enableBandwidthControl();
38     int disableBandwidthControl();
39     int enableDataSaver(bool enable);
40 
41     int setInterfaceSharedQuota(const std::string& iface, int64_t bytes);
42     int getInterfaceSharedQuota(int64_t *bytes);
43     int removeInterfaceSharedQuota(const std::string& iface);
44 
45     int setInterfaceQuota(const std::string& iface, int64_t bytes);
46     int getInterfaceQuota(const std::string& iface, int64_t* bytes);
47     int removeInterfaceQuota(const std::string& iface);
48 
49     int addNaughtyApps(const std::vector<uint32_t>& appUids);
50     int removeNaughtyApps(const std::vector<uint32_t>& appUids);
51     int addNiceApps(const std::vector<uint32_t>& appUids);
52     int removeNiceApps(const std::vector<uint32_t>& appUids);
53 
54     int setGlobalAlert(int64_t bytes);
55     int removeGlobalAlert();
56     int setGlobalAlertInForwardChain();
57     int removeGlobalAlertInForwardChain();
58 
59     int setSharedAlert(int64_t bytes);
60     int removeSharedAlert();
61 
62     int setInterfaceAlert(const std::string& iface, int64_t bytes);
63     int removeInterfaceAlert(const std::string& iface);
64 
65     static const char LOCAL_INPUT[];
66     static const char LOCAL_FORWARD[];
67     static const char LOCAL_OUTPUT[];
68     static const char LOCAL_RAW_PREROUTING[];
69     static const char LOCAL_MANGLE_POSTROUTING[];
70     static const char LOCAL_GLOBAL_ALERT[];
71 
72     enum IptJumpOp { IptJumpReject, IptJumpReturn };
73     enum IptOp { IptOpInsert, IptOpDelete };
74 
75   private:
76     struct QuotaInfo {
77         int64_t quota;
78         int64_t alert;
79     };
80 
81     enum IptIpVer { IptIpV4, IptIpV6 };
82     enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend };
83     enum QuotaType { QuotaUnique, QuotaShared };
84     enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
85 #if LOG_NDEBUG
86     enum IptFailureLog { IptFailShow, IptFailHide };
87 #else
88     enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
89 #endif
90 
91     std::string makeDataSaverCommand(IptablesTarget target, bool enable);
92 
93     int manipulateSpecialApps(const std::vector<uint32_t>& appStrUids, UidOwnerMatchType matchType,
94                               IptOp appOp);
95 
96     int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes);
97     int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes);
98 
99     int updateQuota(const std::string& alertName, int64_t bytes);
100 
101     int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes);
102     int removeCostlyAlert(const std::string& costName, int64_t* alertBytes);
103 
104     /*
105      * Attempt to find the bw_costly_* tables that need flushing,
106      * and flush them.
107      * If doClean then remove the tables also.
108      * Deals with both ip4 and ip6 tables.
109      */
110     void flushExistingCostlyTables(bool doClean);
111     static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove);
112 
113     /*
114      * Attempt to flush our tables.
115      * If doClean then remove them also.
116      * Deals with both ip4 and ip6 tables.
117      */
118     void flushCleanTables(bool doClean);
119 
120     // For testing.
121     friend class BandwidthControllerTest;
122     static int (*execFunction)(int, char **, int *, bool, bool);
123     static FILE *(*popenFunction)(const char *, const char *);
124     static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
125 
126     static const char *opToString(IptOp op);
127     static const char *jumpToString(IptJumpOp jumpHandling);
128 
129     int64_t mSharedQuotaBytes = 0;
130     int64_t mSharedAlertBytes = 0;
131     int64_t mGlobalAlertBytes = 0;
132 
133     std::map<std::string, QuotaInfo> mQuotaIfaces;
134     std::set<std::string> mSharedQuotaIfaces;
135 };
136 
137 #endif
138