JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 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 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <set> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 21 | #include <string> |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 22 | #include <utility> |
| 23 | #include <vector> |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 24 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 25 | #include <utils/RWLock.h> |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 26 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 27 | #include "NetdConstants.h" |
| 28 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 29 | class BandwidthController { |
| 30 | public: |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 31 | android::RWLock lock; |
| 32 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 33 | BandwidthController(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 34 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 35 | int setupIptablesHooks(); |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 36 | static bool getBpfStatus(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 37 | |
| 38 | int enableBandwidthControl(bool force); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 39 | int disableBandwidthControl(); |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 40 | int enableDataSaver(bool enable); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 41 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 42 | int setInterfaceSharedQuota(const std::string& iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 43 | int getInterfaceSharedQuota(int64_t *bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 44 | int removeInterfaceSharedQuota(const std::string& iface); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 45 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 46 | 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 Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 49 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 50 | int addNaughtyApps(int numUids, char *appUids[]); |
| 51 | int removeNaughtyApps(int numUids, char *appUids[]); |
JP Abgrall | e478873 | 2013-07-02 20:28:45 -0700 | [diff] [blame] | 52 | int addNiceApps(int numUids, char *appUids[]); |
| 53 | int removeNiceApps(int numUids, char *appUids[]); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 54 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 55 | int setGlobalAlert(int64_t bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 56 | int removeGlobalAlert(); |
| 57 | int setGlobalAlertInForwardChain(); |
| 58 | int removeGlobalAlertInForwardChain(); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 59 | |
| 60 | int setSharedAlert(int64_t bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 61 | int removeSharedAlert(); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 62 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 63 | int setInterfaceAlert(const std::string& iface, int64_t bytes); |
| 64 | int removeInterfaceAlert(const std::string& iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 65 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 66 | 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 Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 71 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 72 | enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd }; |
| 73 | enum IptOp { IptOpInsert, IptOpDelete }; |
| 74 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 75 | private: |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 76 | struct QuotaInfo { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 77 | int64_t quota; |
| 78 | int64_t alert; |
| 79 | }; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 80 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 81 | enum IptIpVer { IptIpV4, IptIpV6 }; |
Lorenzo Colitti | d9db08c | 2017-04-28 11:06:40 +0900 | [diff] [blame] | 82 | enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend }; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 83 | enum QuotaType { QuotaUnique, QuotaShared }; |
| 84 | enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 85 | #if LOG_NDEBUG |
| 86 | enum IptFailureLog { IptFailShow, IptFailHide }; |
| 87 | #else |
| 88 | enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow }; |
| 89 | #endif |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 90 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 91 | std::string makeDataSaverCommand(IptablesTarget target, bool enable); |
| 92 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 93 | int manipulateSpecialApps(const std::vector<std::string>& appStrUids, const std::string& chain, |
| 94 | IptJumpOp jumpHandling, IptOp appOp); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 95 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 96 | int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes); |
| 97 | int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 98 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 99 | int updateQuota(const std::string& alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 100 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 101 | int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes); |
| 102 | int removeCostlyAlert(const std::string& costName, int64_t* alertBytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 103 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 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); |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 111 | static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 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 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 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 *); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 125 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 126 | static const char *opToString(IptOp op); |
| 127 | static const char *jumpToString(IptJumpOp jumpHandling); |
| 128 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame^] | 129 | bool mBpfSupported; |
| 130 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 131 | int64_t mSharedQuotaBytes = 0; |
| 132 | int64_t mSharedAlertBytes = 0; |
| 133 | int64_t mGlobalAlertBytes = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 134 | /* |
| 135 | * This tracks the number of tethers setup. |
| 136 | * The FORWARD chain is updated in the following cases: |
| 137 | * - The 1st time a globalAlert is setup and there are tethers setup. |
| 138 | * - Anytime a globalAlert is removed and there are tethers setup. |
| 139 | * - The 1st tether is setup and there is a globalAlert active. |
| 140 | * - The last tether is removed and there is a globalAlert active. |
| 141 | */ |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 142 | int mGlobalAlertTetherCount = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 143 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 144 | std::map<std::string, QuotaInfo> mQuotaIfaces; |
| 145 | std::set<std::string> mSharedQuotaIfaces; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | #endif |