blob: efacdcee9c8eec017fa1162e1193249171871470 [file] [log] [blame]
JP Abgrall4a5f5ca2011-06-15 18:37:39 -07001/*
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
Joel Scherpelzced1dd92017-06-28 10:19:52 +090019#include <map>
20#include <set>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070021#include <string>
Joel Scherpelzbcad6612017-05-30 10:55:11 +090022#include <utility>
23#include <vector>
JP Abgralldb7da582011-09-18 12:57:32 -070024
Lorenzo Colittidedd2712016-03-22 12:36:29 +090025#include <utils/RWLock.h>
JP Abgrallbaeccc42013-06-25 09:44:10 -070026
Lorenzo Colitti13debb82016-03-27 17:46:30 +090027#include "NetdConstants.h"
28
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070029class BandwidthController {
30public:
Lorenzo Colittidedd2712016-03-22 12:36:29 +090031 android::RWLock lock;
32
JP Abgrallfa6f46d2011-06-17 23:17:28 -070033 BandwidthController();
JP Abgrall0031cea2012-04-17 16:38:23 -070034
Joel Scherpelzbcad6612017-05-30 10:55:11 +090035 int setupIptablesHooks();
Chenbo Feng27fa2b82018-03-19 11:51:54 -070036 static bool getBpfStatsStatus();
JP Abgrall0031cea2012-04-17 16:38:23 -070037
38 int enableBandwidthControl(bool force);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090039 int disableBandwidthControl();
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090040 int enableDataSaver(bool enable);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070041
Joel Scherpelzbcad6612017-05-30 10:55:11 +090042 int setInterfaceSharedQuota(const std::string& iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070043 int getInterfaceSharedQuota(int64_t *bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090044 int removeInterfaceSharedQuota(const std::string& iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070045
Joel Scherpelzbcad6612017-05-30 10:55:11 +090046 int setInterfaceQuota(const std::string& iface, int64_t bytes);
47 int getInterfaceQuota(const std::string& iface, int64_t* bytes);
48 int removeInterfaceQuota(const std::string& iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070049
JP Abgrallfa6f46d2011-06-17 23:17:28 -070050 int addNaughtyApps(int numUids, char *appUids[]);
51 int removeNaughtyApps(int numUids, char *appUids[]);
JP Abgralle4788732013-07-02 20:28:45 -070052 int addNiceApps(int numUids, char *appUids[]);
53 int removeNiceApps(int numUids, char *appUids[]);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070054
JP Abgrall8a932722011-07-13 19:17:35 -070055 int setGlobalAlert(int64_t bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090056 int removeGlobalAlert();
57 int setGlobalAlertInForwardChain();
58 int removeGlobalAlertInForwardChain();
JP Abgrall8a932722011-07-13 19:17:35 -070059
60 int setSharedAlert(int64_t bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090061 int removeSharedAlert();
JP Abgrall8a932722011-07-13 19:17:35 -070062
Joel Scherpelzbcad6612017-05-30 10:55:11 +090063 int setInterfaceAlert(const std::string& iface, int64_t bytes);
64 int removeInterfaceAlert(const std::string& iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070065
Joel Scherpelzbcad6612017-05-30 10:55:11 +090066 static const char LOCAL_INPUT[];
67 static const char LOCAL_FORWARD[];
68 static const char LOCAL_OUTPUT[];
69 static const char LOCAL_RAW_PREROUTING[];
70 static const char LOCAL_MANGLE_POSTROUTING[];
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070071
Joel Scherpelzbcad6612017-05-30 10:55:11 +090072 private:
Joel Scherpelzced1dd92017-06-28 10:19:52 +090073 struct QuotaInfo {
JP Abgrall8a932722011-07-13 19:17:35 -070074 int64_t quota;
75 int64_t alert;
76 };
JP Abgralldb7da582011-09-18 12:57:32 -070077
JP Abgrall26e0d492011-06-24 19:21:51 -070078 enum IptIpVer { IptIpV4, IptIpV6 };
Lorenzo Colittid9db08c2017-04-28 11:06:40 +090079 enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend };
JP Abgralla9ba4cb2013-07-02 19:08:48 -070080 enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd };
Lorenzo Colittid9db08c2017-04-28 11:06:40 +090081 enum IptOp { IptOpInsert, IptOpDelete };
JP Abgrall26e0d492011-06-24 19:21:51 -070082 enum QuotaType { QuotaUnique, QuotaShared };
83 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall1fb02df2012-04-24 23:27:44 -070084#if LOG_NDEBUG
85 enum IptFailureLog { IptFailShow, IptFailHide };
86#else
87 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
88#endif
JP Abgralla9ba4cb2013-07-02 19:08:48 -070089
Lorenzo Colittiaff28792017-09-26 17:46:18 +090090 std::string makeDataSaverCommand(IptablesTarget target, bool enable);
91
Joel Scherpelzbcad6612017-05-30 10:55:11 +090092 int manipulateSpecialApps(const std::vector<std::string>& appStrUids, const std::string& chain,
93 IptJumpOp jumpHandling, IptOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070094
Joel Scherpelzbcad6612017-05-30 10:55:11 +090095 int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes);
96 int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070097
Joel Scherpelzbcad6612017-05-30 10:55:11 +090098 int updateQuota(const std::string& alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070099
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900100 int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes);
101 int removeCostlyAlert(const std::string& costName, int64_t* alertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700102
JP Abgrall0e540ec2013-08-26 15:13:10 -0700103 /*
104 * Attempt to find the bw_costly_* tables that need flushing,
105 * and flush them.
106 * If doClean then remove the tables also.
107 * Deals with both ip4 and ip6 tables.
108 */
109 void flushExistingCostlyTables(bool doClean);
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900110 static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700111
112 /*
113 * Attempt to flush our tables.
114 * If doClean then remove them also.
115 * Deals with both ip4 and ip6 tables.
116 */
117 void flushCleanTables(bool doClean);
118
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900119 // For testing.
120 friend class BandwidthControllerTest;
121 static int (*execFunction)(int, char **, int *, bool, bool);
122 static FILE *(*popenFunction)(const char *, const char *);
123 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
JP Abgralldb7da582011-09-18 12:57:32 -0700124
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900125 static const char *opToString(IptOp op);
126 static const char *jumpToString(IptJumpOp jumpHandling);
127
128 int64_t mSharedQuotaBytes = 0;
129 int64_t mSharedAlertBytes = 0;
130 int64_t mGlobalAlertBytes = 0;
JP Abgrallc6c67342011-10-07 16:28:54 -0700131 /*
132 * This tracks the number of tethers setup.
133 * The FORWARD chain is updated in the following cases:
134 * - The 1st time a globalAlert is setup and there are tethers setup.
135 * - Anytime a globalAlert is removed and there are tethers setup.
136 * - The 1st tether is setup and there is a globalAlert active.
137 * - The last tether is removed and there is a globalAlert active.
138 */
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900139 int mGlobalAlertTetherCount = 0;
JP Abgrallc6c67342011-10-07 16:28:54 -0700140
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900141 std::map<std::string, QuotaInfo> mQuotaIfaces;
142 std::set<std::string> mSharedQuotaIfaces;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700143};
144
145#endif